4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011 by Delphix. All rights reserved.
39 #include <sys/efi_partition.h>
41 #include <sys/zfs_ioctl.h>
44 #include "zfs_namecheck.h"
46 #include "libzfs_impl.h"
47 #include "zfs_comutil.h"
49 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
51 typedef struct prop_flags {
52 int create:1; /* Validate property on creation */
53 int import:1; /* Validate property on import */
57 * ====================================================================
58 * zpool property functions
59 * ====================================================================
63 zpool_get_all_props(zpool_handle_t *zhp)
65 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
66 libzfs_handle_t *hdl = zhp->zpool_hdl;
68 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
70 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
73 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
74 if (errno == ENOMEM) {
75 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
76 zcmd_free_nvlists(&zc);
80 zcmd_free_nvlists(&zc);
85 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
86 zcmd_free_nvlists(&zc);
90 zcmd_free_nvlists(&zc);
96 zpool_props_refresh(zpool_handle_t *zhp)
100 old_props = zhp->zpool_props;
102 if (zpool_get_all_props(zhp) != 0)
105 nvlist_free(old_props);
110 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
116 zprop_source_t source;
118 nvl = zhp->zpool_props;
119 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
120 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
122 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
124 source = ZPROP_SRC_DEFAULT;
125 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
136 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
140 zprop_source_t source;
142 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
144 * zpool_get_all_props() has most likely failed because
145 * the pool is faulted, but if all we need is the top level
146 * vdev's guid then get it from the zhp config nvlist.
148 if ((prop == ZPOOL_PROP_GUID) &&
149 (nvlist_lookup_nvlist(zhp->zpool_config,
150 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
151 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
155 return (zpool_prop_default_numeric(prop));
158 nvl = zhp->zpool_props;
159 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
160 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
162 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
164 source = ZPROP_SRC_DEFAULT;
165 value = zpool_prop_default_numeric(prop);
175 * Map VDEV STATE to printed strings.
178 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
183 case VDEV_STATE_CLOSED:
184 case VDEV_STATE_OFFLINE:
185 return (gettext("OFFLINE"));
186 case VDEV_STATE_REMOVED:
187 return (gettext("REMOVED"));
188 case VDEV_STATE_CANT_OPEN:
189 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
190 return (gettext("FAULTED"));
191 else if (aux == VDEV_AUX_SPLIT_POOL)
192 return (gettext("SPLIT"));
194 return (gettext("UNAVAIL"));
195 case VDEV_STATE_FAULTED:
196 return (gettext("FAULTED"));
197 case VDEV_STATE_DEGRADED:
198 return (gettext("DEGRADED"));
199 case VDEV_STATE_HEALTHY:
200 return (gettext("ONLINE"));
203 return (gettext("UNKNOWN"));
207 * Get a zpool property value for 'prop' and return the value in
208 * a pre-allocated buffer.
211 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
212 zprop_source_t *srctype)
216 zprop_source_t src = ZPROP_SRC_NONE;
221 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
223 case ZPOOL_PROP_NAME:
224 (void) strlcpy(buf, zpool_get_name(zhp), len);
227 case ZPOOL_PROP_HEALTH:
228 (void) strlcpy(buf, "FAULTED", len);
231 case ZPOOL_PROP_GUID:
232 intval = zpool_get_prop_int(zhp, prop, &src);
233 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
236 case ZPOOL_PROP_ALTROOT:
237 case ZPOOL_PROP_CACHEFILE:
238 case ZPOOL_PROP_COMMENT:
239 if (zhp->zpool_props != NULL ||
240 zpool_get_all_props(zhp) == 0) {
242 zpool_get_prop_string(zhp, prop, &src),
250 (void) strlcpy(buf, "-", len);
259 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
260 prop != ZPOOL_PROP_NAME)
263 switch (zpool_prop_get_type(prop)) {
264 case PROP_TYPE_STRING:
265 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
269 case PROP_TYPE_NUMBER:
270 intval = zpool_get_prop_int(zhp, prop, &src);
273 case ZPOOL_PROP_SIZE:
274 case ZPOOL_PROP_ALLOCATED:
275 case ZPOOL_PROP_FREE:
276 case ZPOOL_PROP_ASHIFT:
277 (void) zfs_nicenum(intval, buf, len);
280 case ZPOOL_PROP_CAPACITY:
281 (void) snprintf(buf, len, "%llu%%",
282 (u_longlong_t)intval);
285 case ZPOOL_PROP_DEDUPRATIO:
286 (void) snprintf(buf, len, "%llu.%02llux",
287 (u_longlong_t)(intval / 100),
288 (u_longlong_t)(intval % 100));
291 case ZPOOL_PROP_HEALTH:
292 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
293 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
294 verify(nvlist_lookup_uint64_array(nvroot,
295 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
298 (void) strlcpy(buf, zpool_state_to_name(intval,
302 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
306 case PROP_TYPE_INDEX:
307 intval = zpool_get_prop_int(zhp, prop, &src);
308 if (zpool_prop_index_to_string(prop, intval, &strval)
311 (void) strlcpy(buf, strval, len);
325 * Check if the bootfs name has the same pool name as it is set to.
326 * Assuming bootfs is a valid dataset name.
329 bootfs_name_valid(const char *pool, char *bootfs)
331 int len = strlen(pool);
333 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
336 if (strncmp(pool, bootfs, len) == 0 &&
337 (bootfs[len] == '/' || bootfs[len] == '\0'))
344 * Inspect the configuration to determine if any of the devices contain
348 pool_uses_efi(nvlist_t *config)
353 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
354 &child, &children) != 0)
355 return (read_efi_label(config, NULL) >= 0);
357 for (c = 0; c < children; c++) {
358 if (pool_uses_efi(child[c]))
365 pool_is_bootable(zpool_handle_t *zhp)
367 char bootfs[ZPOOL_MAXNAMELEN];
369 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
370 sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
371 sizeof (bootfs)) != 0);
376 * Given an nvlist of zpool properties to be set, validate that they are
377 * correct, and parse any numeric properties (index, boolean, etc) if they are
378 * specified as strings.
381 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
382 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
390 struct stat64 statbuf;
394 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
395 (void) no_memory(hdl);
400 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
401 const char *propname = nvpair_name(elem);
404 * Make sure this property is valid and applies to this type.
406 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
407 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
408 "invalid property '%s'"), propname);
409 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
413 if (zpool_prop_readonly(prop)) {
414 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
415 "is readonly"), propname);
416 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
420 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
421 &strval, &intval, errbuf) != 0)
425 * Perform additional checking for specific properties.
430 case ZPOOL_PROP_VERSION:
431 if (intval < version || intval > SPA_VERSION) {
432 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
433 "property '%s' number %d is invalid."),
435 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
440 case ZPOOL_PROP_ASHIFT:
442 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
443 "property '%s' can only be set at "
444 "creation time"), propname);
445 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
449 if (intval != 0 && (intval < 9 || intval > 13)) {
450 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
451 "property '%s' number %d is invalid."),
453 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
458 case ZPOOL_PROP_BOOTFS:
459 if (flags.create || flags.import) {
460 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
461 "property '%s' cannot be set at creation "
462 "or import time"), propname);
463 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
467 if (version < SPA_VERSION_BOOTFS) {
468 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
469 "pool must be upgraded to support "
470 "'%s' property"), propname);
471 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
476 * bootfs property value has to be a dataset name and
477 * the dataset has to be in the same pool as it sets to.
479 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
481 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
482 "is an invalid name"), strval);
483 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
487 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
488 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
489 "could not open pool '%s'"), poolname);
490 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
493 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
494 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
496 #if defined(__sun__) || defined(__sun)
498 * bootfs property cannot be set on a disk which has
501 if (pool_uses_efi(nvroot)) {
502 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
503 "property '%s' not supported on "
504 "EFI labeled devices"), propname);
505 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
513 case ZPOOL_PROP_ALTROOT:
514 if (!flags.create && !flags.import) {
515 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
516 "property '%s' can only be set during pool "
517 "creation or import"), propname);
518 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
522 if (strval[0] != '/') {
523 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
524 "bad alternate root '%s'"), strval);
525 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
530 case ZPOOL_PROP_CACHEFILE:
531 if (strval[0] == '\0')
534 if (strcmp(strval, "none") == 0)
537 if (strval[0] != '/') {
538 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
539 "property '%s' must be empty, an "
540 "absolute path, or 'none'"), propname);
541 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
545 slash = strrchr(strval, '/');
547 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
548 strcmp(slash, "/..") == 0) {
549 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
550 "'%s' is not a valid file"), strval);
551 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
557 if (strval[0] != '\0' &&
558 (stat64(strval, &statbuf) != 0 ||
559 !S_ISDIR(statbuf.st_mode))) {
560 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
561 "'%s' is not a valid directory"),
563 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
570 case ZPOOL_PROP_COMMENT:
571 for (check = strval; *check != '\0'; check++) {
572 if (!isprint(*check)) {
574 dgettext(TEXT_DOMAIN,
575 "comment may only have printable "
577 (void) zfs_error(hdl, EZFS_BADPROP,
582 if (strlen(strval) > ZPROP_MAX_COMMENT) {
583 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
584 "comment must not exceed %d characters"),
586 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
590 case ZPOOL_PROP_READONLY:
592 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
593 "property '%s' can only be set at "
594 "import time"), propname);
595 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
604 nvlist_free(retprops);
609 * Set zpool property : propname=propval.
612 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
614 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
617 nvlist_t *nvl = NULL;
620 prop_flags_t flags = { 0 };
622 (void) snprintf(errbuf, sizeof (errbuf),
623 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
626 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
627 return (no_memory(zhp->zpool_hdl));
629 if (nvlist_add_string(nvl, propname, propval) != 0) {
631 return (no_memory(zhp->zpool_hdl));
634 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
635 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
636 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
645 * Execute the corresponding ioctl() to set this property.
647 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
649 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
654 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
656 zcmd_free_nvlists(&zc);
660 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
662 (void) zpool_props_refresh(zhp);
668 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
670 libzfs_handle_t *hdl = zhp->zpool_hdl;
672 char buf[ZFS_MAXPROPLEN];
674 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
677 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
682 if (entry->pl_prop != ZPROP_INVAL &&
683 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
685 if (strlen(buf) > entry->pl_width)
686 entry->pl_width = strlen(buf);
695 * Don't start the slice at the default block of 34; many storage
696 * devices will use a stripe width of 128k, other vendors prefer a 1m
697 * alignment. It is best to play it safe and ensure a 1m alignment
698 * given 512B blocks. When the block size is larger by a power of 2
699 * we will still be 1m aligned. Some devices are sensitive to the
700 * partition ending alignment as well.
702 #define NEW_START_BLOCK 2048
703 #define PARTITION_END_ALIGNMENT 2048
706 * Validate the given pool name, optionally putting an extended error message in
710 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
716 ret = pool_namecheck(pool, &why, &what);
719 * The rules for reserved pool names were extended at a later point.
720 * But we need to support users with existing pools that may now be
721 * invalid. So we only check for this expanded set of names during a
722 * create (or import), and only in userland.
724 if (ret == 0 && !isopen &&
725 (strncmp(pool, "mirror", 6) == 0 ||
726 strncmp(pool, "raidz", 5) == 0 ||
727 strncmp(pool, "spare", 5) == 0 ||
728 strcmp(pool, "log") == 0)) {
731 dgettext(TEXT_DOMAIN, "name is reserved"));
739 case NAME_ERR_TOOLONG:
741 dgettext(TEXT_DOMAIN, "name is too long"));
744 case NAME_ERR_INVALCHAR:
746 dgettext(TEXT_DOMAIN, "invalid character "
747 "'%c' in pool name"), what);
750 case NAME_ERR_NOLETTER:
751 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
752 "name must begin with a letter"));
755 case NAME_ERR_RESERVED:
756 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
757 "name is reserved"));
760 case NAME_ERR_DISKLIKE:
761 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
762 "pool name is reserved"));
765 case NAME_ERR_LEADING_SLASH:
766 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
767 "leading slash in name"));
770 case NAME_ERR_EMPTY_COMPONENT:
771 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
772 "empty component in name"));
775 case NAME_ERR_TRAILING_SLASH:
776 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
777 "trailing slash in name"));
780 case NAME_ERR_MULTIPLE_AT:
781 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
782 "multiple '@' delimiters in name"));
785 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
786 "permission set is missing '@'"));
797 * Open a handle to the given pool, even if the pool is currently in the FAULTED
801 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
807 * Make sure the pool name is valid.
809 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
810 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
811 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
816 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
819 zhp->zpool_hdl = hdl;
820 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
822 if (zpool_refresh_stats(zhp, &missing) != 0) {
828 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
829 (void) zfs_error_fmt(hdl, EZFS_NOENT,
830 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
839 * Like the above, but silent on error. Used when iterating over pools (because
840 * the configuration cache may be out of date).
843 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
848 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
851 zhp->zpool_hdl = hdl;
852 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
854 if (zpool_refresh_stats(zhp, &missing) != 0) {
870 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
874 zpool_open(libzfs_handle_t *hdl, const char *pool)
878 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
881 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
882 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
883 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
892 * Close the handle. Simply frees the memory associated with the handle.
895 zpool_close(zpool_handle_t *zhp)
897 if (zhp->zpool_config)
898 nvlist_free(zhp->zpool_config);
899 if (zhp->zpool_old_config)
900 nvlist_free(zhp->zpool_old_config);
901 if (zhp->zpool_props)
902 nvlist_free(zhp->zpool_props);
907 * Return the name of the pool.
910 zpool_get_name(zpool_handle_t *zhp)
912 return (zhp->zpool_name);
917 * Return the state of the pool (ACTIVE or UNAVAILABLE)
920 zpool_get_state(zpool_handle_t *zhp)
922 return (zhp->zpool_state);
926 * Create the named pool, using the provided vdev list. It is assumed
927 * that the consumer has already validated the contents of the nvlist, so we
928 * don't have to worry about error semantics.
931 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
932 nvlist_t *props, nvlist_t *fsprops)
934 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
935 nvlist_t *zc_fsprops = NULL;
936 nvlist_t *zc_props = NULL;
941 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
942 "cannot create '%s'"), pool);
944 if (!zpool_name_valid(hdl, B_FALSE, pool))
945 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
947 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
951 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
953 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
954 SPA_VERSION_1, flags, msg)) == NULL) {
963 zoned = ((nvlist_lookup_string(fsprops,
964 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
965 strcmp(zonestr, "on") == 0);
967 if ((zc_fsprops = zfs_valid_proplist(hdl,
968 ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
972 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
975 if (nvlist_add_nvlist(zc_props,
976 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
981 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
984 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
986 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
988 zcmd_free_nvlists(&zc);
989 nvlist_free(zc_props);
990 nvlist_free(zc_fsprops);
995 * This can happen if the user has specified the same
996 * device multiple times. We can't reliably detect this
997 * until we try to add it and see we already have a
998 * label. This can also happen under if the device is
999 * part of an active md or lvm device.
1001 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1002 "one or more vdevs refer to the same device, or one of\n"
1003 "the devices is part of an active md or lvm device"));
1004 return (zfs_error(hdl, EZFS_BADDEV, msg));
1008 * This occurs when one of the devices is below
1009 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1010 * device was the problem device since there's no
1011 * reliable way to determine device size from userland.
1016 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1018 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1019 "one or more devices is less than the "
1020 "minimum size (%s)"), buf);
1022 return (zfs_error(hdl, EZFS_BADDEV, msg));
1025 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1026 "one or more devices is out of space"));
1027 return (zfs_error(hdl, EZFS_BADDEV, msg));
1030 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1031 "cache device must be a disk or disk slice"));
1032 return (zfs_error(hdl, EZFS_BADDEV, msg));
1035 return (zpool_standard_error(hdl, errno, msg));
1040 * If this is an alternate root pool, then we automatically set the
1041 * mountpoint of the root dataset to be '/'.
1043 if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
1047 verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
1048 verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1055 zcmd_free_nvlists(&zc);
1056 nvlist_free(zc_props);
1057 nvlist_free(zc_fsprops);
1062 * Destroy the given pool. It is up to the caller to ensure that there are no
1063 * datasets left in the pool.
1066 zpool_destroy(zpool_handle_t *zhp)
1068 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1069 zfs_handle_t *zfp = NULL;
1070 libzfs_handle_t *hdl = zhp->zpool_hdl;
1073 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1074 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1077 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1079 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1080 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1081 "cannot destroy '%s'"), zhp->zpool_name);
1083 if (errno == EROFS) {
1084 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1085 "one or more devices is read only"));
1086 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1088 (void) zpool_standard_error(hdl, errno, msg);
1097 remove_mountpoint(zfp);
1105 * Add the given vdevs to the pool. The caller must have already performed the
1106 * necessary verification to ensure that the vdev specification is well-formed.
1109 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1111 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1113 libzfs_handle_t *hdl = zhp->zpool_hdl;
1115 nvlist_t **spares, **l2cache;
1116 uint_t nspares, nl2cache;
1118 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1119 "cannot add to '%s'"), zhp->zpool_name);
1121 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1122 SPA_VERSION_SPARES &&
1123 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1124 &spares, &nspares) == 0) {
1125 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1126 "upgraded to add hot spares"));
1127 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1130 if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1131 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1134 for (s = 0; s < nspares; s++) {
1137 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1138 &path) == 0 && pool_uses_efi(spares[s])) {
1139 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1140 "device '%s' contains an EFI label and "
1141 "cannot be used on root pools."),
1142 zpool_vdev_name(hdl, NULL, spares[s],
1144 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1149 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1150 SPA_VERSION_L2CACHE &&
1151 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1152 &l2cache, &nl2cache) == 0) {
1153 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1154 "upgraded to add cache devices"));
1155 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1158 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1160 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1162 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1166 * This can happen if the user has specified the same
1167 * device multiple times. We can't reliably detect this
1168 * until we try to add it and see we already have a
1171 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1172 "one or more vdevs refer to the same device"));
1173 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1178 * This occurrs when one of the devices is below
1179 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1180 * device was the problem device since there's no
1181 * reliable way to determine device size from userland.
1186 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1188 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1189 "device is less than the minimum "
1192 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1196 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1197 "pool must be upgraded to add these vdevs"));
1198 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1202 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1203 "root pool can not have multiple vdevs"
1204 " or separate logs"));
1205 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1209 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1210 "cache device must be a disk or disk slice"));
1211 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1215 (void) zpool_standard_error(hdl, errno, msg);
1223 zcmd_free_nvlists(&zc);
1229 * Exports the pool from the system. The caller must ensure that there are no
1230 * mounted datasets in the pool.
1233 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1235 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1238 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1239 "cannot export '%s'"), zhp->zpool_name);
1241 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1242 zc.zc_cookie = force;
1243 zc.zc_guid = hardforce;
1245 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1248 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1249 "use '-f' to override the following errors:\n"
1250 "'%s' has an active shared spare which could be"
1251 " used by other pools once '%s' is exported."),
1252 zhp->zpool_name, zhp->zpool_name);
1253 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1256 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1265 zpool_export(zpool_handle_t *zhp, boolean_t force)
1267 return (zpool_export_common(zhp, force, B_FALSE));
1271 zpool_export_force(zpool_handle_t *zhp)
1273 return (zpool_export_common(zhp, B_TRUE, B_TRUE));
1277 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1280 nvlist_t *nv = NULL;
1286 if (!hdl->libzfs_printerr || config == NULL)
1289 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0)
1292 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1294 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1296 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1297 strftime(timestr, 128, "%c", &t) != 0) {
1299 (void) printf(dgettext(TEXT_DOMAIN,
1300 "Would be able to return %s "
1301 "to its state as of %s.\n"),
1304 (void) printf(dgettext(TEXT_DOMAIN,
1305 "Pool %s returned to its state as of %s.\n"),
1309 (void) printf(dgettext(TEXT_DOMAIN,
1310 "%s approximately %lld "),
1311 dryrun ? "Would discard" : "Discarded",
1312 ((longlong_t)loss + 30) / 60);
1313 (void) printf(dgettext(TEXT_DOMAIN,
1314 "minutes of transactions.\n"));
1315 } else if (loss > 0) {
1316 (void) printf(dgettext(TEXT_DOMAIN,
1317 "%s approximately %lld "),
1318 dryrun ? "Would discard" : "Discarded",
1320 (void) printf(dgettext(TEXT_DOMAIN,
1321 "seconds of transactions.\n"));
1327 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1330 nvlist_t *nv = NULL;
1332 uint64_t edata = UINT64_MAX;
1337 if (!hdl->libzfs_printerr)
1341 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1343 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1345 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1346 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1347 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1350 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1351 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1354 (void) printf(dgettext(TEXT_DOMAIN,
1355 "Recovery is possible, but will result in some data loss.\n"));
1357 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1358 strftime(timestr, 128, "%c", &t) != 0) {
1359 (void) printf(dgettext(TEXT_DOMAIN,
1360 "\tReturning the pool to its state as of %s\n"
1361 "\tshould correct the problem. "),
1364 (void) printf(dgettext(TEXT_DOMAIN,
1365 "\tReverting the pool to an earlier state "
1366 "should correct the problem.\n\t"));
1370 (void) printf(dgettext(TEXT_DOMAIN,
1371 "Approximately %lld minutes of data\n"
1372 "\tmust be discarded, irreversibly. "),
1373 ((longlong_t)loss + 30) / 60);
1374 } else if (loss > 0) {
1375 (void) printf(dgettext(TEXT_DOMAIN,
1376 "Approximately %lld seconds of data\n"
1377 "\tmust be discarded, irreversibly. "),
1380 if (edata != 0 && edata != UINT64_MAX) {
1382 (void) printf(dgettext(TEXT_DOMAIN,
1383 "After rewind, at least\n"
1384 "\tone persistent user-data error will remain. "));
1386 (void) printf(dgettext(TEXT_DOMAIN,
1387 "After rewind, several\n"
1388 "\tpersistent user-data errors will remain. "));
1391 (void) printf(dgettext(TEXT_DOMAIN,
1392 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1393 reason >= 0 ? "clear" : "import", name);
1395 (void) printf(dgettext(TEXT_DOMAIN,
1396 "A scrub of the pool\n"
1397 "\tis strongly recommended after recovery.\n"));
1401 (void) printf(dgettext(TEXT_DOMAIN,
1402 "Destroy and re-create the pool from\n\ta backup source.\n"));
1406 * zpool_import() is a contracted interface. Should be kept the same
1409 * Applications should use zpool_import_props() to import a pool with
1410 * new properties value to be set.
1413 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1416 nvlist_t *props = NULL;
1419 if (altroot != NULL) {
1420 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1421 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1422 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1426 if (nvlist_add_string(props,
1427 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1428 nvlist_add_string(props,
1429 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1431 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1432 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1437 ret = zpool_import_props(hdl, config, newname, props,
1445 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1451 uint64_t is_log = 0;
1453 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1457 (void) printf("\t%*s%s%s\n", indent, "", name,
1458 is_log ? " [log]" : "");
1460 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1461 &child, &children) != 0)
1464 for (c = 0; c < children; c++) {
1465 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
1466 print_vdev_tree(hdl, vname, child[c], indent + 2);
1472 * Import the given pool using the known configuration and a list of
1473 * properties to be set. The configuration should have come from
1474 * zpool_find_import(). The 'newname' parameters control whether the pool
1475 * is imported with a different name.
1478 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1479 nvlist_t *props, int flags)
1481 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1482 zpool_rewind_policy_t policy;
1483 nvlist_t *nv = NULL;
1484 nvlist_t *nvinfo = NULL;
1485 nvlist_t *missing = NULL;
1492 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1495 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1496 "cannot import pool '%s'"), origname);
1498 if (newname != NULL) {
1499 if (!zpool_name_valid(hdl, B_FALSE, newname))
1500 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1501 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1503 thename = (char *)newname;
1510 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1512 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1515 if ((props = zpool_valid_proplist(hdl, origname,
1516 props, version, flags, errbuf)) == NULL) {
1518 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1524 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1526 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1529 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1533 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1538 zc.zc_cookie = flags;
1539 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1541 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1542 zcmd_free_nvlists(&zc);
1549 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1550 zpool_get_rewind_policy(config, &policy);
1556 * Dry-run failed, but we print out what success
1557 * looks like if we found a best txg
1559 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1560 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1566 if (newname == NULL)
1567 (void) snprintf(desc, sizeof (desc),
1568 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1571 (void) snprintf(desc, sizeof (desc),
1572 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1578 * Unsupported version.
1580 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1584 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1588 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1589 "one or more devices is read only"));
1590 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1594 if (nv && nvlist_lookup_nvlist(nv,
1595 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1596 nvlist_lookup_nvlist(nvinfo,
1597 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1598 (void) printf(dgettext(TEXT_DOMAIN,
1599 "The devices below are missing, use "
1600 "'-m' to import the pool anyway:\n"));
1601 print_vdev_tree(hdl, NULL, missing, 2);
1602 (void) printf("\n");
1604 (void) zpool_standard_error(hdl, error, desc);
1608 (void) zpool_standard_error(hdl, error, desc);
1612 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1613 "one or more devices are already in use\n"));
1614 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1618 (void) zpool_standard_error(hdl, error, desc);
1619 zpool_explain_recover(hdl,
1620 newname ? origname : thename, -error, nv);
1627 zpool_handle_t *zhp;
1630 * This should never fail, but play it safe anyway.
1632 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1634 else if (zhp != NULL)
1636 if (policy.zrp_request &
1637 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1638 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1639 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1645 zcmd_free_nvlists(&zc);
1655 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1657 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1659 libzfs_handle_t *hdl = zhp->zpool_hdl;
1661 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1662 zc.zc_cookie = func;
1664 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
1665 (errno == ENOENT && func != POOL_SCAN_NONE))
1668 if (func == POOL_SCAN_SCRUB) {
1669 (void) snprintf(msg, sizeof (msg),
1670 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1671 } else if (func == POOL_SCAN_NONE) {
1672 (void) snprintf(msg, sizeof (msg),
1673 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1676 assert(!"unexpected result");
1679 if (errno == EBUSY) {
1681 pool_scan_stat_t *ps = NULL;
1684 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1685 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1686 (void) nvlist_lookup_uint64_array(nvroot,
1687 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1688 if (ps && ps->pss_func == POOL_SCAN_SCRUB)
1689 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1691 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1692 } else if (errno == ENOENT) {
1693 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1695 return (zpool_standard_error(hdl, errno, msg));
1700 * Find a vdev that matches the search criteria specified. We use the
1701 * the nvpair name to determine how we should look for the device.
1702 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1703 * spare; but FALSE if its an INUSE spare.
1706 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1707 boolean_t *l2cache, boolean_t *log)
1714 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1716 /* Nothing to look for */
1717 if (search == NULL || pair == NULL)
1720 /* Obtain the key we will use to search */
1721 srchkey = nvpair_name(pair);
1723 switch (nvpair_type(pair)) {
1724 case DATA_TYPE_UINT64:
1725 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1726 uint64_t srchval, theguid;
1728 verify(nvpair_value_uint64(pair, &srchval) == 0);
1729 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1731 if (theguid == srchval)
1736 case DATA_TYPE_STRING: {
1737 char *srchval, *val;
1739 verify(nvpair_value_string(pair, &srchval) == 0);
1740 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1744 * Search for the requested value. Special cases:
1746 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in with a
1747 * partition suffix "1", "-part1", or "p1". The suffix is hidden
1748 * from the user, but included in the string, so this matches around
1750 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1752 * Otherwise, all other searches are simple string compares.
1754 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) {
1755 uint64_t wholedisk = 0;
1757 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1760 char buf[MAXPATHLEN];
1762 zfs_append_partition(srchval, buf, sizeof (buf));
1763 if (strcmp(val, buf) == 0)
1768 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
1769 char *type, *idx, *end, *p;
1770 uint64_t id, vdev_id;
1773 * Determine our vdev type, keeping in mind
1774 * that the srchval is composed of a type and
1775 * vdev id pair (i.e. mirror-4).
1777 if ((type = strdup(srchval)) == NULL)
1780 if ((p = strrchr(type, '-')) == NULL) {
1788 * If the types don't match then keep looking.
1790 if (strncmp(val, type, strlen(val)) != 0) {
1795 verify(strncmp(type, VDEV_TYPE_RAIDZ,
1796 strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1797 strncmp(type, VDEV_TYPE_MIRROR,
1798 strlen(VDEV_TYPE_MIRROR)) == 0);
1799 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
1803 vdev_id = strtoull(idx, &end, 10);
1810 * Now verify that we have the correct vdev id.
1819 if (strcmp(srchval, val) == 0)
1828 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1829 &child, &children) != 0)
1832 for (c = 0; c < children; c++) {
1833 if ((ret = vdev_to_nvlist_iter(child[c], search,
1834 avail_spare, l2cache, NULL)) != NULL) {
1836 * The 'is_log' value is only set for the toplevel
1837 * vdev, not the leaf vdevs. So we always lookup the
1838 * log device from the root of the vdev tree (where
1839 * 'log' is non-NULL).
1842 nvlist_lookup_uint64(child[c],
1843 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
1851 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1852 &child, &children) == 0) {
1853 for (c = 0; c < children; c++) {
1854 if ((ret = vdev_to_nvlist_iter(child[c], search,
1855 avail_spare, l2cache, NULL)) != NULL) {
1856 *avail_spare = B_TRUE;
1862 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1863 &child, &children) == 0) {
1864 for (c = 0; c < children; c++) {
1865 if ((ret = vdev_to_nvlist_iter(child[c], search,
1866 avail_spare, l2cache, NULL)) != NULL) {
1877 * Given a physical path (minus the "/devices" prefix), find the
1881 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
1882 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
1884 nvlist_t *search, *nvroot, *ret;
1886 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1887 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
1889 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1892 *avail_spare = B_FALSE;
1896 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1897 nvlist_free(search);
1903 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
1906 zpool_vdev_is_interior(const char *name)
1908 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1909 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
1915 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
1916 boolean_t *l2cache, boolean_t *log)
1918 char buf[MAXPATHLEN];
1920 nvlist_t *nvroot, *search, *ret;
1923 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1925 guid = strtoull(path, &end, 10);
1926 if (guid != 0 && *end == '\0') {
1927 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
1928 } else if (zpool_vdev_is_interior(path)) {
1929 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
1930 } else if (path[0] != '/') {
1931 if (zfs_resolve_shortname(path, buf, sizeof (buf)) < 0) {
1932 nvlist_free(search);
1935 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
1937 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
1940 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1943 *avail_spare = B_FALSE;
1947 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1948 nvlist_free(search);
1954 vdev_online(nvlist_t *nv)
1958 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
1959 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
1960 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
1967 * Helper function for zpool_get_physpaths().
1970 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
1971 size_t *bytes_written)
1973 size_t bytes_left, pos, rsz;
1977 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
1979 return (EZFS_NODEVICE);
1981 pos = *bytes_written;
1982 bytes_left = physpath_size - pos;
1983 format = (pos == 0) ? "%s" : " %s";
1985 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
1986 *bytes_written += rsz;
1988 if (rsz >= bytes_left) {
1989 /* if physpath was not copied properly, clear it */
1990 if (bytes_left != 0) {
1993 return (EZFS_NOSPC);
1999 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2000 size_t *rsz, boolean_t is_spare)
2005 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2006 return (EZFS_INVALCONFIG);
2008 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2010 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2011 * For a spare vdev, we only want to boot from the active
2016 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2019 return (EZFS_INVALCONFIG);
2022 if (vdev_online(nv)) {
2023 if ((ret = vdev_get_one_physpath(nv, physpath,
2024 phypath_size, rsz)) != 0)
2027 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2028 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2029 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2034 if (nvlist_lookup_nvlist_array(nv,
2035 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2036 return (EZFS_INVALCONFIG);
2038 for (i = 0; i < count; i++) {
2039 ret = vdev_get_physpaths(child[i], physpath,
2040 phypath_size, rsz, is_spare);
2041 if (ret == EZFS_NOSPC)
2046 return (EZFS_POOL_INVALARG);
2050 * Get phys_path for a root pool config.
2051 * Return 0 on success; non-zero on failure.
2054 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2057 nvlist_t *vdev_root;
2064 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2066 return (EZFS_INVALCONFIG);
2068 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2069 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2070 &child, &count) != 0)
2071 return (EZFS_INVALCONFIG);
2074 * root pool can not have EFI labeled disks and can only have
2075 * a single top-level vdev.
2077 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
2078 pool_uses_efi(vdev_root))
2079 return (EZFS_POOL_INVALARG);
2081 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2084 /* No online devices */
2086 return (EZFS_NODEVICE);
2092 * Get phys_path for a root pool
2093 * Return 0 on success; non-zero on failure.
2096 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2098 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2103 * If the device has being dynamically expanded then we need to relabel
2104 * the disk to use the new unallocated space.
2107 zpool_relabel_disk(libzfs_handle_t *hdl, const char *path, const char *msg)
2111 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
2112 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2113 "relabel '%s': unable to open device: %d"), path, errno);
2114 return (zfs_error(hdl, EZFS_OPENFAILED, msg));
2118 * It's possible that we might encounter an error if the device
2119 * does not have any unallocated space left. If so, we simply
2120 * ignore that error and continue on.
2122 * Also, we don't call efi_rescan() - that would just return EBUSY.
2123 * The module will do it for us in vdev_disk_open().
2125 error = efi_use_whole_disk(fd);
2127 if (error && error != VT_ENOSPC) {
2128 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2129 "relabel '%s': unable to read disk capacity"), path);
2130 return (zfs_error(hdl, EZFS_NOCAP, msg));
2136 * Bring the specified vdev online. The 'flags' parameter is a set of the
2137 * ZFS_ONLINE_* flags.
2140 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2141 vdev_state_t *newstate)
2143 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2146 boolean_t avail_spare, l2cache, islog;
2147 libzfs_handle_t *hdl = zhp->zpool_hdl;
2150 if (flags & ZFS_ONLINE_EXPAND) {
2151 (void) snprintf(msg, sizeof (msg),
2152 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2154 (void) snprintf(msg, sizeof (msg),
2155 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2158 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2159 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2161 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2163 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2166 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2168 if (flags & ZFS_ONLINE_EXPAND ||
2169 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2170 uint64_t wholedisk = 0;
2172 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2176 * XXX - L2ARC 1.0 devices can't support expansion.
2179 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2180 "cannot expand cache devices"));
2181 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2185 const char *fullpath = path;
2186 char buf[MAXPATHLEN];
2188 if (path[0] != '/') {
2189 error = zfs_resolve_shortname(path, buf,
2192 return (zfs_error(hdl, EZFS_NODEVICE,
2198 error = zpool_relabel_disk(hdl, fullpath, msg);
2204 zc.zc_cookie = VDEV_STATE_ONLINE;
2207 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2208 if (errno == EINVAL) {
2209 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2210 "from this pool into a new one. Use '%s' "
2211 "instead"), "zpool detach");
2212 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2214 return (zpool_standard_error(hdl, errno, msg));
2217 *newstate = zc.zc_cookie;
2222 * Take the specified vdev offline
2225 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2227 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2230 boolean_t avail_spare, l2cache;
2231 libzfs_handle_t *hdl = zhp->zpool_hdl;
2233 (void) snprintf(msg, sizeof (msg),
2234 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2236 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2237 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2239 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2241 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2244 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2246 zc.zc_cookie = VDEV_STATE_OFFLINE;
2247 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2249 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2256 * There are no other replicas of this device.
2258 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2262 * The log device has unplayed logs
2264 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2267 return (zpool_standard_error(hdl, errno, msg));
2272 * Mark the given vdev faulted.
2275 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2277 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2279 libzfs_handle_t *hdl = zhp->zpool_hdl;
2281 (void) snprintf(msg, sizeof (msg),
2282 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
2284 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2286 zc.zc_cookie = VDEV_STATE_FAULTED;
2289 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2296 * There are no other replicas of this device.
2298 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2301 return (zpool_standard_error(hdl, errno, msg));
2307 * Mark the given vdev degraded.
2310 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2312 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2314 libzfs_handle_t *hdl = zhp->zpool_hdl;
2316 (void) snprintf(msg, sizeof (msg),
2317 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid);
2319 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2321 zc.zc_cookie = VDEV_STATE_DEGRADED;
2324 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2327 return (zpool_standard_error(hdl, errno, msg));
2331 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2335 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2341 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2343 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2346 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2347 children == 2 && child[which] == tgt)
2350 for (c = 0; c < children; c++)
2351 if (is_replacing_spare(child[c], tgt, which))
2359 * Attach new_disk (fully described by nvroot) to old_disk.
2360 * If 'replacing' is specified, the new disk will replace the old one.
2363 zpool_vdev_attach(zpool_handle_t *zhp,
2364 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2366 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2370 boolean_t avail_spare, l2cache, islog;
2375 nvlist_t *config_root;
2376 libzfs_handle_t *hdl = zhp->zpool_hdl;
2377 boolean_t rootpool = pool_is_bootable(zhp);
2380 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2381 "cannot replace %s with %s"), old_disk, new_disk);
2383 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2384 "cannot attach %s to %s"), new_disk, old_disk);
2387 * If this is a root pool, make sure that we're not attaching an
2388 * EFI labeled device.
2390 if (rootpool && pool_uses_efi(nvroot)) {
2391 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2392 "EFI labeled devices are not supported on root pools."));
2393 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2396 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2397 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2399 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2402 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2405 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2407 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2408 zc.zc_cookie = replacing;
2410 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2411 &child, &children) != 0 || children != 1) {
2412 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2413 "new device must be a single disk"));
2414 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2417 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2418 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2420 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2424 * If the target is a hot spare that has been swapped in, we can only
2425 * replace it with another hot spare.
2428 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2429 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2430 NULL) == NULL || !avail_spare) &&
2431 is_replacing_spare(config_root, tgt, 1)) {
2432 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2433 "can only be replaced by another hot spare"));
2435 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2440 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2443 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2445 zcmd_free_nvlists(&zc);
2450 * XXX need a better way to prevent user from
2451 * booting up a half-baked vdev.
2453 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2454 "sure to wait until resilver is done "
2455 "before rebooting.\n"));
2463 * Can't attach to or replace this type of vdev.
2466 uint64_t version = zpool_get_prop_int(zhp,
2467 ZPOOL_PROP_VERSION, NULL);
2470 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2471 "cannot replace a log with a spare"));
2472 else if (version >= SPA_VERSION_MULTI_REPLACE)
2473 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2474 "already in replacing/spare config; wait "
2475 "for completion or use 'zpool detach'"));
2477 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2478 "cannot replace a replacing device"));
2480 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2481 "can only attach to mirrors and top-level "
2484 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2489 * The new device must be a single disk.
2491 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2492 "new device must be a single disk"));
2493 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2497 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2499 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2504 * The new device is too small.
2506 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2507 "device is too small"));
2508 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2513 * The new device has a different alignment requirement.
2515 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2516 "devices have different sector alignment"));
2517 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2522 * The resulting top-level vdev spec won't fit in the label.
2524 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2528 (void) zpool_standard_error(hdl, errno, msg);
2535 * Detach the specified device.
2538 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2540 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2543 boolean_t avail_spare, l2cache;
2544 libzfs_handle_t *hdl = zhp->zpool_hdl;
2546 (void) snprintf(msg, sizeof (msg),
2547 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2549 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2550 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2552 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2555 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2558 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2560 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2562 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2569 * Can't detach from this type of vdev.
2571 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2572 "applicable to mirror and replacing vdevs"));
2573 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2578 * There are no other replicas of this device.
2580 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2584 (void) zpool_standard_error(hdl, errno, msg);
2591 * Find a mirror vdev in the source nvlist.
2593 * The mchild array contains a list of disks in one of the top-level mirrors
2594 * of the source pool. The schild array contains a list of disks that the
2595 * user specified on the command line. We loop over the mchild array to
2596 * see if any entry in the schild array matches.
2598 * If a disk in the mchild array is found in the schild array, we return
2599 * the index of that entry. Otherwise we return -1.
2602 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2603 nvlist_t **schild, uint_t schildren)
2607 for (mc = 0; mc < mchildren; mc++) {
2609 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2610 mchild[mc], B_FALSE);
2612 for (sc = 0; sc < schildren; sc++) {
2613 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2614 schild[sc], B_FALSE);
2615 boolean_t result = (strcmp(mpath, spath) == 0);
2631 * Split a mirror pool. If newroot points to null, then a new nvlist
2632 * is generated and it is the responsibility of the caller to free it.
2635 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2636 nvlist_t *props, splitflags_t flags)
2638 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2640 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2641 nvlist_t **varray = NULL, *zc_props = NULL;
2642 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2643 libzfs_handle_t *hdl = zhp->zpool_hdl;
2645 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2648 (void) snprintf(msg, sizeof (msg),
2649 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2651 if (!zpool_name_valid(hdl, B_FALSE, newname))
2652 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2654 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2655 (void) fprintf(stderr, gettext("Internal error: unable to "
2656 "retrieve pool configuration\n"));
2660 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2662 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2665 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2666 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2667 props, vers, flags, msg)) == NULL)
2671 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2673 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2674 "Source pool is missing vdev tree"));
2676 nvlist_free(zc_props);
2680 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2683 if (*newroot == NULL ||
2684 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2685 &newchild, &newchildren) != 0)
2688 for (c = 0; c < children; c++) {
2689 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2691 nvlist_t **mchild, *vdev;
2696 * Unlike cache & spares, slogs are stored in the
2697 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2699 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2701 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2703 if (is_log || is_hole) {
2705 * Create a hole vdev and put it in the config.
2707 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2709 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2710 VDEV_TYPE_HOLE) != 0)
2712 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2717 varray[vcount++] = vdev;
2721 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
2723 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2724 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2725 "Source pool must be composed only of mirrors\n"));
2726 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2730 verify(nvlist_lookup_nvlist_array(child[c],
2731 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2733 /* find or add an entry for this top-level vdev */
2734 if (newchildren > 0 &&
2735 (entry = find_vdev_entry(zhp, mchild, mchildren,
2736 newchild, newchildren)) >= 0) {
2737 /* We found a disk that the user specified. */
2738 vdev = mchild[entry];
2741 /* User didn't specify a disk for this vdev. */
2742 vdev = mchild[mchildren - 1];
2745 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
2749 /* did we find every disk the user specified? */
2750 if (found != newchildren) {
2751 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
2752 "include at most one disk from each mirror"));
2753 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2757 /* Prepare the nvlist for populating. */
2758 if (*newroot == NULL) {
2759 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
2762 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
2763 VDEV_TYPE_ROOT) != 0)
2766 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
2769 /* Add all the children we found */
2770 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
2771 lastlog == 0 ? vcount : lastlog) != 0)
2775 * If we're just doing a dry run, exit now with success.
2778 memory_err = B_FALSE;
2783 /* now build up the config list & call the ioctl */
2784 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
2787 if (nvlist_add_nvlist(newconfig,
2788 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
2789 nvlist_add_string(newconfig,
2790 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
2791 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
2795 * The new pool is automatically part of the namespace unless we
2796 * explicitly export it.
2799 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
2800 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2801 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
2802 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
2804 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
2807 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
2808 retval = zpool_standard_error(hdl, errno, msg);
2813 memory_err = B_FALSE;
2816 if (varray != NULL) {
2819 for (v = 0; v < vcount; v++)
2820 nvlist_free(varray[v]);
2823 zcmd_free_nvlists(&zc);
2825 nvlist_free(zc_props);
2827 nvlist_free(newconfig);
2829 nvlist_free(*newroot);
2837 return (no_memory(hdl));
2843 * Remove the given device. Currently, this is supported only for hot spares
2844 * and level 2 cache devices.
2847 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
2849 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2852 boolean_t avail_spare, l2cache, islog;
2853 libzfs_handle_t *hdl = zhp->zpool_hdl;
2856 (void) snprintf(msg, sizeof (msg),
2857 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
2859 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2860 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2862 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2864 * XXX - this should just go away.
2866 if (!avail_spare && !l2cache && !islog) {
2867 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2868 "only inactive hot spares, cache, top-level, "
2869 "or log devices can be removed"));
2870 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2873 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
2874 if (islog && version < SPA_VERSION_HOLES) {
2875 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2876 "pool must be upgrade to support log removal"));
2877 return (zfs_error(hdl, EZFS_BADVERSION, msg));
2880 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2882 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
2885 return (zpool_standard_error(hdl, errno, msg));
2889 * Clear the errors for the pool, or the particular device if specified.
2892 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
2894 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2897 zpool_rewind_policy_t policy;
2898 boolean_t avail_spare, l2cache;
2899 libzfs_handle_t *hdl = zhp->zpool_hdl;
2900 nvlist_t *nvi = NULL;
2904 (void) snprintf(msg, sizeof (msg),
2905 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2908 (void) snprintf(msg, sizeof (msg),
2909 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2912 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2914 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
2915 &l2cache, NULL)) == 0)
2916 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2919 * Don't allow error clearing for hot spares. Do allow
2920 * error clearing for l2cache devices.
2923 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2925 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
2929 zpool_get_rewind_policy(rewindnvl, &policy);
2930 zc.zc_cookie = policy.zrp_request;
2932 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
2935 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
2938 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
2940 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
2941 zcmd_free_nvlists(&zc);
2946 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
2947 errno != EPERM && errno != EACCES)) {
2948 if (policy.zrp_request &
2949 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2950 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
2951 zpool_rewind_exclaim(hdl, zc.zc_name,
2952 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
2956 zcmd_free_nvlists(&zc);
2960 zcmd_free_nvlists(&zc);
2961 return (zpool_standard_error(hdl, errno, msg));
2965 * Similar to zpool_clear(), but takes a GUID (used by fmd).
2968 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
2970 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2972 libzfs_handle_t *hdl = zhp->zpool_hdl;
2974 (void) snprintf(msg, sizeof (msg),
2975 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
2976 (u_longlong_t)guid);
2978 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2980 zc.zc_cookie = ZPOOL_NO_REWIND;
2982 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
2985 return (zpool_standard_error(hdl, errno, msg));
2989 * Change the GUID for a pool.
2992 zpool_reguid(zpool_handle_t *zhp)
2995 libzfs_handle_t *hdl = zhp->zpool_hdl;
2996 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2998 (void) snprintf(msg, sizeof (msg),
2999 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3001 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3002 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3005 return (zpool_standard_error(hdl, errno, msg));
3009 * Convert from a devid string to a path.
3012 devid_to_path(char *devid_str)
3017 devid_nmlist_t *list = NULL;
3020 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3023 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3025 devid_str_free(minor);
3031 if ((path = strdup(list[0].devname)) == NULL)
3034 devid_free_nmlist(list);
3040 * Convert from a path to a devid string.
3043 path_to_devid(const char *path)
3049 if ((fd = open(path, O_RDONLY)) < 0)
3054 if (devid_get(fd, &devid) == 0) {
3055 if (devid_get_minor_name(fd, &minor) == 0)
3056 ret = devid_str_encode(devid, minor);
3058 devid_str_free(minor);
3067 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3068 * ignore any failure here, since a common case is for an unprivileged user to
3069 * type 'zpool status', and we'll display the correct information anyway.
3072 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3074 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3076 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3077 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3078 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3081 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3085 * Remove partition suffix from a vdev path. Partition suffixes may take three
3086 * forms: "-partX", "pX", or "X", where X is a string of digits. The second
3087 * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
3088 * third case only occurs when preceded by a string matching the regular
3089 * expression "^[hs]d[a-z]+", i.e. a scsi or ide disk.
3092 strip_partition(libzfs_handle_t *hdl, char *path)
3094 char *tmp = zfs_strdup(hdl, path);
3095 char *part = NULL, *d = NULL;
3097 if ((part = strstr(tmp, "-part")) && part != tmp) {
3099 } else if ((part = strrchr(tmp, 'p')) &&
3100 part > tmp + 1 && isdigit(*(part-1))) {
3102 } else if ((tmp[0] == 'h' || tmp[0] == 's') && tmp[1] == 'd') {
3103 for (d = &tmp[2]; isalpha(*d); part = ++d);
3105 if (part && d && *d != '\0') {
3106 for (; isdigit(*d); d++);
3113 #define PATH_BUF_LEN 64
3116 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3117 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3118 * We also check if this is a whole disk, in which case we strip off the
3119 * trailing 's0' slice name.
3121 * This routine is also responsible for identifying when disks have been
3122 * reconfigured in a new location. The kernel will have opened the device by
3123 * devid, but the path will still refer to the old location. To catch this, we
3124 * first do a path -> devid translation (which is fast for the common case). If
3125 * the devid matches, we're done. If not, we do a reverse devid -> path
3126 * translation and issue the appropriate ioctl() to update the path of the vdev.
3127 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3131 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3134 char *path, *devid, *type;
3136 char buf[PATH_BUF_LEN];
3140 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
3142 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3144 (void) snprintf(buf, sizeof (buf), "%llu",
3145 (u_longlong_t)value);
3147 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3149 * If the device is dead (faulted, offline, etc) then don't
3150 * bother opening it. Otherwise we may be forcing the user to
3151 * open a misbehaving device, which can have undesirable
3154 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3155 (uint64_t **)&vs, &vsc) != 0 ||
3156 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3158 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3160 * Determine if the current path is correct.
3162 char *newdevid = path_to_devid(path);
3164 if (newdevid == NULL ||
3165 strcmp(devid, newdevid) != 0) {
3168 if ((newpath = devid_to_path(devid)) != NULL) {
3170 * Update the path appropriately.
3172 set_path(zhp, nv, newpath);
3173 if (nvlist_add_string(nv,
3174 ZPOOL_CONFIG_PATH, newpath) == 0)
3175 verify(nvlist_lookup_string(nv,
3183 devid_str_free(newdevid);
3187 * For a block device only use the name.
3189 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
3190 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
3191 path = strrchr(path, '/');
3196 * Remove the partition from the path it this is a whole disk.
3198 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3199 &value) == 0 && value) {
3200 return strip_partition(hdl, path);
3203 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3206 * If it's a raidz device, we need to stick in the parity level.
3208 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3209 char tmpbuf[PATH_BUF_LEN];
3211 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3213 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s%llu", path,
3214 (u_longlong_t)value);
3219 * We identify each top-level vdev by using a <type-id>
3220 * naming convention.
3225 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3227 (void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3233 return (zfs_strdup(hdl, path));
3237 zbookmark_compare(const void *a, const void *b)
3239 return (memcmp(a, b, sizeof (zbookmark_t)));
3243 * Retrieve the persistent error log, uniquify the members, and return to the
3247 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3249 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3251 zbookmark_t *zb = NULL;
3255 * Retrieve the raw error list from the kernel. If the number of errors
3256 * has increased, allocate more space and continue until we get the
3259 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3263 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3264 count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
3266 zc.zc_nvlist_dst_size = count;
3267 (void) strcpy(zc.zc_name, zhp->zpool_name);
3269 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3271 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3272 if (errno == ENOMEM) {
3273 count = zc.zc_nvlist_dst_size;
3274 if ((zc.zc_nvlist_dst = (uintptr_t)
3275 zfs_alloc(zhp->zpool_hdl, count *
3276 sizeof (zbookmark_t))) == (uintptr_t)NULL)
3287 * Sort the resulting bookmarks. This is a little confusing due to the
3288 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3289 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3290 * _not_ copied as part of the process. So we point the start of our
3291 * array appropriate and decrement the total number of elements.
3293 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
3294 zc.zc_nvlist_dst_size;
3295 count -= zc.zc_nvlist_dst_size;
3297 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
3299 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3302 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3304 for (i = 0; i < count; i++) {
3307 /* ignoring zb_blkid and zb_level for now */
3308 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3309 zb[i-1].zb_object == zb[i].zb_object)
3312 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3314 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3315 zb[i].zb_objset) != 0) {
3319 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3320 zb[i].zb_object) != 0) {
3324 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3331 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3335 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3336 return (no_memory(zhp->zpool_hdl));
3340 * Upgrade a ZFS pool to the latest on-disk version.
3343 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3345 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3346 libzfs_handle_t *hdl = zhp->zpool_hdl;
3348 (void) strcpy(zc.zc_name, zhp->zpool_name);
3349 zc.zc_cookie = new_version;
3351 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3352 return (zpool_standard_error_fmt(hdl, errno,
3353 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3359 zpool_set_history_str(const char *subcommand, int argc, char **argv,
3364 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
3365 for (i = 1; i < argc; i++) {
3366 if (strlen(history_str) + 1 + strlen(argv[i]) >
3369 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
3370 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
3375 * Stage command history for logging.
3378 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
3380 if (history_str == NULL)
3383 if (strlen(history_str) > HIS_MAX_RECORD_LEN)
3386 if (hdl->libzfs_log_str != NULL)
3387 free(hdl->libzfs_log_str);
3389 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
3390 return (no_memory(hdl));
3396 * Perform ioctl to get some command history of a pool.
3398 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3399 * logical offset of the history buffer to start reading from.
3401 * Upon return, 'off' is the next logical offset to read from and
3402 * 'len' is the actual amount of bytes read into 'buf'.
3405 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3407 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3408 libzfs_handle_t *hdl = zhp->zpool_hdl;
3410 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3412 zc.zc_history = (uint64_t)(uintptr_t)buf;
3413 zc.zc_history_len = *len;
3414 zc.zc_history_offset = *off;
3416 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3419 return (zfs_error_fmt(hdl, EZFS_PERM,
3420 dgettext(TEXT_DOMAIN,
3421 "cannot show history for pool '%s'"),
3424 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3425 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3426 "'%s'"), zhp->zpool_name));
3428 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3429 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3430 "'%s', pool must be upgraded"), zhp->zpool_name));
3432 return (zpool_standard_error_fmt(hdl, errno,
3433 dgettext(TEXT_DOMAIN,
3434 "cannot get history for '%s'"), zhp->zpool_name));
3438 *len = zc.zc_history_len;
3439 *off = zc.zc_history_offset;
3445 * Process the buffer of nvlists, unpacking and storing each nvlist record
3446 * into 'records'. 'leftover' is set to the number of bytes that weren't
3447 * processed as there wasn't a complete record.
3450 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3451 nvlist_t ***records, uint_t *numrecords)
3457 while (bytes_read > sizeof (reclen)) {
3459 /* get length of packed record (stored as little endian) */
3460 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3461 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3463 if (bytes_read < sizeof (reclen) + reclen)
3467 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3469 bytes_read -= sizeof (reclen) + reclen;
3470 buf += sizeof (reclen) + reclen;
3472 /* add record to nvlist array */
3474 if (ISP2(*numrecords + 1)) {
3475 *records = realloc(*records,
3476 *numrecords * 2 * sizeof (nvlist_t *));
3478 (*records)[*numrecords - 1] = nv;
3481 *leftover = bytes_read;
3485 #define HIS_BUF_LEN (128*1024)
3488 * Retrieve the command history of a pool.
3491 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3493 char buf[HIS_BUF_LEN];
3495 nvlist_t **records = NULL;
3496 uint_t numrecords = 0;
3500 uint64_t bytes_read = sizeof (buf);
3503 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3506 /* if nothing else was read in, we're at EOF, just return */
3510 if ((err = zpool_history_unpack(buf, bytes_read,
3511 &leftover, &records, &numrecords)) != 0)
3519 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3520 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3521 records, numrecords) == 0);
3523 for (i = 0; i < numrecords; i++)
3524 nvlist_free(records[i]);
3531 * Retrieve the next event. If there is a new event available 'nvp' will
3532 * contain a newly allocated nvlist and 'dropped' will be set to the number
3533 * of missed events since the last call to this function. When 'nvp' is
3534 * set to NULL it indicates no new events are available. In either case
3535 * the function returns 0 and it is up to the caller to free 'nvp'. In
3536 * the case of a fatal error the function will return a non-zero value.
3537 * When the function is called in blocking mode it will not return until
3538 * a new event is available.
3541 zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
3542 int *dropped, int block, int cleanup_fd)
3544 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3549 zc.zc_cleanup_fd = cleanup_fd;
3552 zc.zc_guid = ZEVENT_NONBLOCK;
3554 if (zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE) != 0)
3558 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
3561 error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
3562 dgettext(TEXT_DOMAIN, "zfs shutdown"));
3565 /* Blocking error case should not occur */
3567 error = zpool_standard_error_fmt(hdl, errno,
3568 dgettext(TEXT_DOMAIN, "cannot get event"));
3572 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3573 error = zfs_error_fmt(hdl, EZFS_NOMEM,
3574 dgettext(TEXT_DOMAIN, "cannot get event"));
3580 error = zpool_standard_error_fmt(hdl, errno,
3581 dgettext(TEXT_DOMAIN, "cannot get event"));
3586 error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
3590 *dropped = (int)zc.zc_cookie;
3592 zcmd_free_nvlists(&zc);
3601 zpool_events_clear(libzfs_handle_t *hdl, int *count)
3603 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3606 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3607 "cannot clear events"));
3609 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
3610 return (zpool_standard_error_fmt(hdl, errno, msg));
3613 *count = (int)zc.zc_cookie; /* # of events cleared */
3619 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3620 char *pathname, size_t len)
3622 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3623 boolean_t mounted = B_FALSE;
3624 char *mntpnt = NULL;
3625 char dsname[MAXNAMELEN];
3628 /* special case for the MOS */
3629 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", (longlong_t)obj);
3633 /* get the dataset's name */
3634 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3636 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3637 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3638 /* just write out a path of two object numbers */
3639 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3640 (longlong_t)dsobj, (longlong_t)obj);
3643 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3645 /* find out if the dataset is mounted */
3646 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3648 /* get the corrupted object's path */
3649 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3651 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3654 (void) snprintf(pathname, len, "%s%s", mntpnt,
3657 (void) snprintf(pathname, len, "%s:%s",
3658 dsname, zc.zc_value);
3661 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, (longlong_t)obj);
3667 * Read the EFI label from the config, if a label does not exist then
3668 * pass back the error to the caller. If the caller has passed a non-NULL
3669 * diskaddr argument then we set it to the starting address of the EFI
3673 read_efi_label(nvlist_t *config, diskaddr_t *sb)
3677 char diskname[MAXPATHLEN];
3680 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3683 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
3684 strrchr(path, '/'));
3685 if ((fd = open(diskname, O_RDWR|O_DIRECT)) >= 0) {
3686 struct dk_gpt *vtoc;
3688 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3690 *sb = vtoc->efi_parts[0].p_start;
3699 * determine where a partition starts on a disk in the current
3703 find_start_block(nvlist_t *config)
3707 diskaddr_t sb = MAXOFFSET_T;
3710 if (nvlist_lookup_nvlist_array(config,
3711 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3712 if (nvlist_lookup_uint64(config,
3713 ZPOOL_CONFIG_WHOLE_DISK,
3714 &wholedisk) != 0 || !wholedisk) {
3715 return (MAXOFFSET_T);
3717 if (read_efi_label(config, &sb) < 0)
3722 for (c = 0; c < children; c++) {
3723 sb = find_start_block(child[c]);
3724 if (sb != MAXOFFSET_T) {
3728 return (MAXOFFSET_T);
3732 zpool_label_disk_wait(char *path, int timeout)
3734 struct stat64 statbuf;
3738 * Wait timeout miliseconds for a newly created device to be available
3739 * from the given path. There is a small window when a /dev/ device
3740 * will exist and the udev link will not, so we must wait for the
3741 * symlink. Depending on the udev rules this may take a few seconds.
3743 for (i = 0; i < timeout; i++) {
3747 if ((stat64(path, &statbuf) == 0) && (errno == 0))
3755 zpool_label_disk_check(char *path)
3757 struct dk_gpt *vtoc;
3760 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0)
3763 if ((err = efi_alloc_and_read(fd, &vtoc)) != 0) {
3768 if (vtoc->efi_flags & EFI_GPT_PRIMARY_CORRUPT) {
3780 * Label an individual disk. The name provided is the short name,
3781 * stripped of any leading /dev path.
3784 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
3786 char path[MAXPATHLEN];
3787 struct dk_gpt *vtoc;
3789 size_t resv = EFI_MIN_RESV_SIZE;
3790 uint64_t slice_size;
3791 diskaddr_t start_block;
3794 /* prepare an error message just in case */
3795 (void) snprintf(errbuf, sizeof (errbuf),
3796 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3801 if (pool_is_bootable(zhp)) {
3802 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3803 "EFI labeled devices are not supported on root "
3805 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3808 verify(nvlist_lookup_nvlist(zhp->zpool_config,
3809 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3811 if (zhp->zpool_start_block == 0)
3812 start_block = find_start_block(nvroot);
3814 start_block = zhp->zpool_start_block;
3815 zhp->zpool_start_block = start_block;
3818 start_block = NEW_START_BLOCK;
3821 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
3824 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
3826 * This shouldn't happen. We've long since verified that this
3827 * is a valid device.
3829 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
3830 "label '%s': unable to open device: %d"), path, errno);
3831 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
3834 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
3836 * The only way this can fail is if we run out of memory, or we
3837 * were unable to read the disk's capacity
3839 if (errno == ENOMEM)
3840 (void) no_memory(hdl);
3843 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
3844 "label '%s': unable to read disk capacity"), path);
3846 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
3849 slice_size = vtoc->efi_last_u_lba + 1;
3850 slice_size -= EFI_MIN_RESV_SIZE;
3851 if (start_block == MAXOFFSET_T)
3852 start_block = NEW_START_BLOCK;
3853 slice_size -= start_block;
3854 slice_size = P2ALIGN(slice_size, PARTITION_END_ALIGNMENT);
3856 vtoc->efi_parts[0].p_start = start_block;
3857 vtoc->efi_parts[0].p_size = slice_size;
3860 * Why we use V_USR: V_BACKUP confuses users, and is considered
3861 * disposable by some EFI utilities (since EFI doesn't have a backup
3862 * slice). V_UNASSIGNED is supposed to be used only for zero size
3863 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
3864 * etc. were all pretty specific. V_USR is as close to reality as we
3865 * can get, in the absence of V_OTHER.
3867 vtoc->efi_parts[0].p_tag = V_USR;
3868 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
3870 vtoc->efi_parts[8].p_start = slice_size + start_block;
3871 vtoc->efi_parts[8].p_size = resv;
3872 vtoc->efi_parts[8].p_tag = V_RESERVED;
3874 if ((rval = efi_write(fd, vtoc)) != 0 || (rval = efi_rescan(fd)) != 0) {
3876 * Some block drivers (like pcata) may not support EFI
3877 * GPT labels. Print out a helpful error message dir-
3878 * ecting the user to manually label the disk and give
3884 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "try using "
3885 "parted(8) and then provide a specific slice: %d"), rval);
3886 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3892 /* Wait for the first expected slice to appear. */
3893 (void) snprintf(path, sizeof (path), "%s/%s%s%s", DISK_ROOT, name,
3894 isdigit(name[strlen(name)-1]) ? "p" : "", FIRST_SLICE);
3895 rval = zpool_label_disk_wait(path, 3000);
3897 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "failed to "
3898 "detect device partitions on '%s': %d"), path, rval);
3899 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3902 /* We can't be to paranoid. Read the label back and verify it. */
3903 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
3904 rval = zpool_label_disk_check(path);
3906 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "freshly written "
3907 "EFI label on '%s' is damaged. Ensure\nthis device "
3908 "is not in in use, and is functioning properly: %d"),
3910 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));