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 (c) 2011, 2015 by Delphix. All rights reserved.
25 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
26 * Copyright 2016 Nexenta Systems, Inc.
27 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
28 * Copyright (c) 2017 Datto Inc.
31 #include <sys/types.h>
43 #include <sys/zfs_ioctl.h>
46 #include "zfs_namecheck.h"
48 #include "libzfs_impl.h"
49 #include "zfs_comutil.h"
50 #include "zfeature_common.h"
52 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
54 #define BACKUP_SLICE "s2"
56 typedef struct prop_flags {
57 int create:1; /* Validate property on creation */
58 int import:1; /* Validate property on import */
62 * ====================================================================
63 * zpool property functions
64 * ====================================================================
68 zpool_get_all_props(zpool_handle_t *zhp)
71 libzfs_handle_t *hdl = zhp->zpool_hdl;
73 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
75 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
78 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
79 if (errno == ENOMEM) {
80 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
81 zcmd_free_nvlists(&zc);
85 zcmd_free_nvlists(&zc);
90 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
91 zcmd_free_nvlists(&zc);
95 zcmd_free_nvlists(&zc);
101 zpool_props_refresh(zpool_handle_t *zhp)
105 old_props = zhp->zpool_props;
107 if (zpool_get_all_props(zhp) != 0)
110 nvlist_free(old_props);
115 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
121 zprop_source_t source;
123 nvl = zhp->zpool_props;
124 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
125 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
127 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
129 source = ZPROP_SRC_DEFAULT;
130 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
141 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
145 zprop_source_t source;
147 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
149 * zpool_get_all_props() has most likely failed because
150 * the pool is faulted, but if all we need is the top level
151 * vdev's guid then get it from the zhp config nvlist.
153 if ((prop == ZPOOL_PROP_GUID) &&
154 (nvlist_lookup_nvlist(zhp->zpool_config,
155 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
156 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
160 return (zpool_prop_default_numeric(prop));
163 nvl = zhp->zpool_props;
164 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
165 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
167 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
169 source = ZPROP_SRC_DEFAULT;
170 value = zpool_prop_default_numeric(prop);
180 * Map VDEV STATE to printed strings.
183 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
186 case VDEV_STATE_CLOSED:
187 case VDEV_STATE_OFFLINE:
188 return (gettext("OFFLINE"));
189 case VDEV_STATE_REMOVED:
190 return (gettext("REMOVED"));
191 case VDEV_STATE_CANT_OPEN:
192 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
193 return (gettext("FAULTED"));
194 else if (aux == VDEV_AUX_SPLIT_POOL)
195 return (gettext("SPLIT"));
197 return (gettext("UNAVAIL"));
198 case VDEV_STATE_FAULTED:
199 return (gettext("FAULTED"));
200 case VDEV_STATE_DEGRADED:
201 return (gettext("DEGRADED"));
202 case VDEV_STATE_HEALTHY:
203 return (gettext("ONLINE"));
209 return (gettext("UNKNOWN"));
213 * Map POOL STATE to printed strings.
216 zpool_pool_state_to_name(pool_state_t state)
219 case POOL_STATE_ACTIVE:
220 return (gettext("ACTIVE"));
221 case POOL_STATE_EXPORTED:
222 return (gettext("EXPORTED"));
223 case POOL_STATE_DESTROYED:
224 return (gettext("DESTROYED"));
225 case POOL_STATE_SPARE:
226 return (gettext("SPARE"));
227 case POOL_STATE_L2CACHE:
228 return (gettext("L2CACHE"));
229 case POOL_STATE_UNINITIALIZED:
230 return (gettext("UNINITIALIZED"));
231 case POOL_STATE_UNAVAIL:
232 return (gettext("UNAVAIL"));
233 case POOL_STATE_POTENTIALLY_ACTIVE:
234 return (gettext("POTENTIALLY_ACTIVE"));
237 return (gettext("UNKNOWN"));
241 * Get a zpool property value for 'prop' and return the value in
242 * a pre-allocated buffer.
245 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
246 zprop_source_t *srctype, boolean_t literal)
250 zprop_source_t src = ZPROP_SRC_NONE;
255 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
257 case ZPOOL_PROP_NAME:
258 (void) strlcpy(buf, zpool_get_name(zhp), len);
261 case ZPOOL_PROP_HEALTH:
263 zpool_pool_state_to_name(POOL_STATE_UNAVAIL), len);
266 case ZPOOL_PROP_GUID:
267 intval = zpool_get_prop_int(zhp, prop, &src);
268 (void) snprintf(buf, len, "%llu", intval);
271 case ZPOOL_PROP_ALTROOT:
272 case ZPOOL_PROP_CACHEFILE:
273 case ZPOOL_PROP_COMMENT:
274 if (zhp->zpool_props != NULL ||
275 zpool_get_all_props(zhp) == 0) {
277 zpool_get_prop_string(zhp, prop, &src),
283 (void) strlcpy(buf, "-", len);
292 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
293 prop != ZPOOL_PROP_NAME)
296 switch (zpool_prop_get_type(prop)) {
297 case PROP_TYPE_STRING:
298 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
302 case PROP_TYPE_NUMBER:
303 intval = zpool_get_prop_int(zhp, prop, &src);
306 case ZPOOL_PROP_SIZE:
307 case ZPOOL_PROP_ALLOCATED:
308 case ZPOOL_PROP_FREE:
309 case ZPOOL_PROP_FREEING:
310 case ZPOOL_PROP_LEAKED:
312 (void) snprintf(buf, len, "%llu",
313 (u_longlong_t)intval);
315 (void) zfs_nicenum(intval, buf, len);
318 case ZPOOL_PROP_EXPANDSZ:
320 (void) strlcpy(buf, "-", len);
321 } else if (literal) {
322 (void) snprintf(buf, len, "%llu",
323 (u_longlong_t)intval);
325 (void) zfs_nicenum(intval, buf, len);
328 case ZPOOL_PROP_CAPACITY:
330 (void) snprintf(buf, len, "%llu",
331 (u_longlong_t)intval);
333 (void) snprintf(buf, len, "%llu%%",
334 (u_longlong_t)intval);
337 case ZPOOL_PROP_FRAGMENTATION:
338 if (intval == UINT64_MAX) {
339 (void) strlcpy(buf, "-", len);
341 (void) snprintf(buf, len, "%llu%%",
342 (u_longlong_t)intval);
345 case ZPOOL_PROP_DEDUPRATIO:
346 (void) snprintf(buf, len, "%llu.%02llux",
347 (u_longlong_t)(intval / 100),
348 (u_longlong_t)(intval % 100));
350 case ZPOOL_PROP_HEALTH:
351 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
352 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
353 verify(nvlist_lookup_uint64_array(nvroot,
354 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
357 (void) strlcpy(buf, zpool_state_to_name(intval,
360 case ZPOOL_PROP_VERSION:
361 if (intval >= SPA_VERSION_FEATURES) {
362 (void) snprintf(buf, len, "-");
367 (void) snprintf(buf, len, "%llu", intval);
371 case PROP_TYPE_INDEX:
372 intval = zpool_get_prop_int(zhp, prop, &src);
373 if (zpool_prop_index_to_string(prop, intval, &strval)
376 (void) strlcpy(buf, strval, len);
390 * Check if the bootfs name has the same pool name as it is set to.
391 * Assuming bootfs is a valid dataset name.
394 bootfs_name_valid(const char *pool, char *bootfs)
396 int len = strlen(pool);
398 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
401 if (strncmp(pool, bootfs, len) == 0 &&
402 (bootfs[len] == '/' || bootfs[len] == '\0'))
409 zpool_is_bootable(zpool_handle_t *zhp)
411 char bootfs[ZFS_MAX_DATASET_NAME_LEN];
413 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
414 sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-",
415 sizeof (bootfs)) != 0);
420 * Given an nvlist of zpool properties to be set, validate that they are
421 * correct, and parse any numeric properties (index, boolean, etc) if they are
422 * specified as strings.
425 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
426 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
434 struct stat64 statbuf;
437 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
438 (void) no_memory(hdl);
443 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
444 const char *propname = nvpair_name(elem);
446 prop = zpool_name_to_prop(propname);
447 if (prop == ZPROP_INVAL && zpool_prop_feature(propname)) {
449 char *fname = strchr(propname, '@') + 1;
451 err = zfeature_lookup_name(fname, NULL);
453 ASSERT3U(err, ==, ENOENT);
454 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
455 "invalid feature '%s'"), fname);
456 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
460 if (nvpair_type(elem) != DATA_TYPE_STRING) {
461 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
462 "'%s' must be a string"), propname);
463 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
467 (void) nvpair_value_string(elem, &strval);
468 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) {
469 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
470 "property '%s' can only be set to "
471 "'enabled'"), propname);
472 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
476 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
477 (void) no_memory(hdl);
484 * Make sure this property is valid and applies to this type.
486 if (prop == ZPROP_INVAL) {
487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
488 "invalid property '%s'"), propname);
489 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
493 if (zpool_prop_readonly(prop)) {
494 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
495 "is readonly"), propname);
496 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
500 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
501 &strval, &intval, errbuf) != 0)
505 * Perform additional checking for specific properties.
508 case ZPOOL_PROP_VERSION:
509 if (intval < version ||
510 !SPA_VERSION_IS_SUPPORTED(intval)) {
511 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
512 "property '%s' number %d is invalid."),
514 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
519 case ZPOOL_PROP_BOOTFS:
520 if (flags.create || flags.import) {
521 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
522 "property '%s' cannot be set at creation "
523 "or import time"), propname);
524 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
528 if (version < SPA_VERSION_BOOTFS) {
529 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
530 "pool must be upgraded to support "
531 "'%s' property"), propname);
532 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
537 * bootfs property value has to be a dataset name and
538 * the dataset has to be in the same pool as it sets to.
540 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
542 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
543 "is an invalid name"), strval);
544 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
548 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
549 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
550 "could not open pool '%s'"), poolname);
551 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
557 case ZPOOL_PROP_ALTROOT:
558 if (!flags.create && !flags.import) {
559 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
560 "property '%s' can only be set during pool "
561 "creation or import"), propname);
562 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
566 if (strval[0] != '/') {
567 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
568 "bad alternate root '%s'"), strval);
569 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
574 case ZPOOL_PROP_CACHEFILE:
575 if (strval[0] == '\0')
578 if (strcmp(strval, "none") == 0)
581 if (strval[0] != '/') {
582 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
583 "property '%s' must be empty, an "
584 "absolute path, or 'none'"), propname);
585 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
589 slash = strrchr(strval, '/');
591 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
592 strcmp(slash, "/..") == 0) {
593 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
594 "'%s' is not a valid file"), strval);
595 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
601 if (strval[0] != '\0' &&
602 (stat64(strval, &statbuf) != 0 ||
603 !S_ISDIR(statbuf.st_mode))) {
604 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
605 "'%s' is not a valid directory"),
607 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
614 case ZPOOL_PROP_COMMENT:
615 for (check = strval; *check != '\0'; check++) {
616 if (!isprint(*check)) {
618 dgettext(TEXT_DOMAIN,
619 "comment may only have printable "
621 (void) zfs_error(hdl, EZFS_BADPROP,
626 if (strlen(strval) > ZPROP_MAX_COMMENT) {
627 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
628 "comment must not exceed %d characters"),
630 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
634 case ZPOOL_PROP_READONLY:
636 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
637 "property '%s' can only be set at "
638 "import time"), propname);
639 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
645 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
646 "property '%s'(%d) not defined"), propname, prop);
653 nvlist_free(retprops);
658 * Set zpool property : propname=propval.
661 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
663 zfs_cmd_t zc = { 0 };
666 nvlist_t *nvl = NULL;
669 prop_flags_t flags = { 0 };
671 (void) snprintf(errbuf, sizeof (errbuf),
672 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
675 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
676 return (no_memory(zhp->zpool_hdl));
678 if (nvlist_add_string(nvl, propname, propval) != 0) {
680 return (no_memory(zhp->zpool_hdl));
683 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
684 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
685 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
694 * Execute the corresponding ioctl() to set this property.
696 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
698 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
703 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
705 zcmd_free_nvlists(&zc);
709 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
711 (void) zpool_props_refresh(zhp);
717 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
719 libzfs_handle_t *hdl = zhp->zpool_hdl;
721 char buf[ZFS_MAXPROPLEN];
722 nvlist_t *features = NULL;
724 boolean_t firstexpand = (NULL == *plp);
726 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
730 while (*last != NULL)
731 last = &(*last)->pl_next;
734 features = zpool_get_features(zhp);
736 if ((*plp)->pl_all && firstexpand) {
737 for (int i = 0; i < SPA_FEATURES; i++) {
738 zprop_list_t *entry = zfs_alloc(hdl,
739 sizeof (zprop_list_t));
740 entry->pl_prop = ZPROP_INVAL;
741 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
742 spa_feature_table[i].fi_uname);
743 entry->pl_width = strlen(entry->pl_user_prop);
744 entry->pl_all = B_TRUE;
747 last = &entry->pl_next;
751 /* add any unsupported features */
752 for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL);
753 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
758 if (zfeature_is_supported(nvpair_name(nvp)))
761 propname = zfs_asprintf(hdl, "unsupported@%s",
765 * Before adding the property to the list make sure that no
766 * other pool already added the same property.
770 while (entry != NULL) {
771 if (entry->pl_user_prop != NULL &&
772 strcmp(propname, entry->pl_user_prop) == 0) {
776 entry = entry->pl_next;
783 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
784 entry->pl_prop = ZPROP_INVAL;
785 entry->pl_user_prop = propname;
786 entry->pl_width = strlen(entry->pl_user_prop);
787 entry->pl_all = B_TRUE;
790 last = &entry->pl_next;
793 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
798 if (entry->pl_prop != ZPROP_INVAL &&
799 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
800 NULL, B_FALSE) == 0) {
801 if (strlen(buf) > entry->pl_width)
802 entry->pl_width = strlen(buf);
810 * Get the state for the given feature on the given ZFS pool.
813 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
817 boolean_t found = B_FALSE;
818 nvlist_t *features = zpool_get_features(zhp);
820 const char *feature = strchr(propname, '@') + 1;
822 supported = zpool_prop_feature(propname);
823 ASSERT(supported || zpool_prop_unsupported(propname));
826 * Convert from feature name to feature guid. This conversion is
827 * unecessary for unsupported@... properties because they already
834 ret = zfeature_lookup_name(feature, &fid);
836 (void) strlcpy(buf, "-", len);
839 feature = spa_feature_table[fid].fi_guid;
842 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
847 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
850 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
852 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
857 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
859 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
862 (void) strlcpy(buf, "-", len);
871 * Don't start the slice at the default block of 34; many storage
872 * devices will use a stripe width of 128k, so start there instead.
874 #define NEW_START_BLOCK 256
877 * Validate the given pool name, optionally putting an extended error message in
881 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
887 ret = pool_namecheck(pool, &why, &what);
890 * The rules for reserved pool names were extended at a later point.
891 * But we need to support users with existing pools that may now be
892 * invalid. So we only check for this expanded set of names during a
893 * create (or import), and only in userland.
895 if (ret == 0 && !isopen &&
896 (strncmp(pool, "mirror", 6) == 0 ||
897 strncmp(pool, "raidz", 5) == 0 ||
898 strncmp(pool, "spare", 5) == 0 ||
899 strcmp(pool, "log") == 0)) {
902 dgettext(TEXT_DOMAIN, "name is reserved"));
910 case NAME_ERR_TOOLONG:
912 dgettext(TEXT_DOMAIN, "name is too long"));
915 case NAME_ERR_INVALCHAR:
917 dgettext(TEXT_DOMAIN, "invalid character "
918 "'%c' in pool name"), what);
921 case NAME_ERR_NOLETTER:
922 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
923 "name must begin with a letter"));
926 case NAME_ERR_RESERVED:
927 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
928 "name is reserved"));
931 case NAME_ERR_DISKLIKE:
932 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
933 "pool name is reserved"));
936 case NAME_ERR_LEADING_SLASH:
937 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
938 "leading slash in name"));
941 case NAME_ERR_EMPTY_COMPONENT:
942 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
943 "empty component in name"));
946 case NAME_ERR_TRAILING_SLASH:
947 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
948 "trailing slash in name"));
951 case NAME_ERR_MULTIPLE_DELIMITERS:
952 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
953 "multiple '@' and/or '#' delimiters in "
958 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
959 "(%d) not defined"), why);
970 * Open a handle to the given pool, even if the pool is currently in the FAULTED
974 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
980 * Make sure the pool name is valid.
982 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
983 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
984 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
989 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
992 zhp->zpool_hdl = hdl;
993 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
995 if (zpool_refresh_stats(zhp, &missing) != 0) {
1001 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1002 (void) zfs_error_fmt(hdl, EZFS_NOENT,
1003 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1012 * Like the above, but silent on error. Used when iterating over pools (because
1013 * the configuration cache may be out of date).
1016 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1018 zpool_handle_t *zhp;
1021 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1024 zhp->zpool_hdl = hdl;
1025 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1027 if (zpool_refresh_stats(zhp, &missing) != 0) {
1043 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1047 zpool_open(libzfs_handle_t *hdl, const char *pool)
1049 zpool_handle_t *zhp;
1051 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1054 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1055 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1056 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1065 * Close the handle. Simply frees the memory associated with the handle.
1068 zpool_close(zpool_handle_t *zhp)
1070 nvlist_free(zhp->zpool_config);
1071 nvlist_free(zhp->zpool_old_config);
1072 nvlist_free(zhp->zpool_props);
1077 * Return the name of the pool.
1080 zpool_get_name(zpool_handle_t *zhp)
1082 return (zhp->zpool_name);
1087 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1090 zpool_get_state(zpool_handle_t *zhp)
1092 return (zhp->zpool_state);
1096 * Create the named pool, using the provided vdev list. It is assumed
1097 * that the consumer has already validated the contents of the nvlist, so we
1098 * don't have to worry about error semantics.
1101 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1102 nvlist_t *props, nvlist_t *fsprops)
1104 zfs_cmd_t zc = { 0 };
1105 nvlist_t *zc_fsprops = NULL;
1106 nvlist_t *zc_props = NULL;
1110 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1111 "cannot create '%s'"), pool);
1113 if (!zpool_name_valid(hdl, B_FALSE, pool))
1114 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1116 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1120 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1122 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1123 SPA_VERSION_1, flags, msg)) == NULL) {
1132 zoned = ((nvlist_lookup_string(fsprops,
1133 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1134 strcmp(zonestr, "on") == 0);
1136 if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
1137 fsprops, zoned, NULL, NULL, msg)) == NULL) {
1141 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1144 if (nvlist_add_nvlist(zc_props,
1145 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1150 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1153 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1155 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1157 zcmd_free_nvlists(&zc);
1158 nvlist_free(zc_props);
1159 nvlist_free(zc_fsprops);
1164 * This can happen if the user has specified the same
1165 * device multiple times. We can't reliably detect this
1166 * until we try to add it and see we already have a
1169 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1170 "one or more vdevs refer to the same device"));
1171 return (zfs_error(hdl, EZFS_BADDEV, msg));
1175 * This happens if the record size is smaller or larger
1176 * than the allowed size range, or not a power of 2.
1178 * NOTE: although zfs_valid_proplist is called earlier,
1179 * this case may have slipped through since the
1180 * pool does not exist yet and it is therefore
1181 * impossible to read properties e.g. max blocksize
1184 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1185 "record size invalid"));
1186 return (zfs_error(hdl, EZFS_BADPROP, msg));
1190 * This occurs when one of the devices is below
1191 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1192 * device was the problem device since there's no
1193 * reliable way to determine device size from userland.
1198 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1200 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1201 "one or more devices is less than the "
1202 "minimum size (%s)"), buf);
1204 return (zfs_error(hdl, EZFS_BADDEV, msg));
1207 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1208 "one or more devices is out of space"));
1209 return (zfs_error(hdl, EZFS_BADDEV, msg));
1212 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1213 "cache device must be a disk or disk slice"));
1214 return (zfs_error(hdl, EZFS_BADDEV, msg));
1217 return (zpool_standard_error(hdl, errno, msg));
1222 zcmd_free_nvlists(&zc);
1223 nvlist_free(zc_props);
1224 nvlist_free(zc_fsprops);
1229 * Destroy the given pool. It is up to the caller to ensure that there are no
1230 * datasets left in the pool.
1233 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1235 zfs_cmd_t zc = { 0 };
1236 zfs_handle_t *zfp = NULL;
1237 libzfs_handle_t *hdl = zhp->zpool_hdl;
1240 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1241 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1244 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1245 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1247 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1248 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1249 "cannot destroy '%s'"), zhp->zpool_name);
1251 if (errno == EROFS) {
1252 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1253 "one or more devices is read only"));
1254 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1256 (void) zpool_standard_error(hdl, errno, msg);
1265 remove_mountpoint(zfp);
1273 * Add the given vdevs to the pool. The caller must have already performed the
1274 * necessary verification to ensure that the vdev specification is well-formed.
1277 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1279 zfs_cmd_t zc = { 0 };
1281 libzfs_handle_t *hdl = zhp->zpool_hdl;
1283 nvlist_t **spares, **l2cache;
1284 uint_t nspares, nl2cache;
1286 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1287 "cannot add to '%s'"), zhp->zpool_name);
1289 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1290 SPA_VERSION_SPARES &&
1291 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1292 &spares, &nspares) == 0) {
1293 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1294 "upgraded to add hot spares"));
1295 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1298 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1299 SPA_VERSION_L2CACHE &&
1300 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1301 &l2cache, &nl2cache) == 0) {
1302 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1303 "upgraded to add cache devices"));
1304 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1307 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1309 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1311 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1315 * This can happen if the user has specified the same
1316 * device multiple times. We can't reliably detect this
1317 * until we try to add it and see we already have a
1320 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1321 "one or more vdevs refer to the same device"));
1322 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1327 * This occurrs when one of the devices is below
1328 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1329 * device was the problem device since there's no
1330 * reliable way to determine device size from userland.
1335 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1337 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1338 "device is less than the minimum "
1341 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1345 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1346 "pool must be upgraded to add these vdevs"));
1347 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1351 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1352 "root pool can not have multiple vdevs"
1353 " or separate logs"));
1354 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1358 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1359 "cache device must be a disk or disk slice"));
1360 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1364 (void) zpool_standard_error(hdl, errno, msg);
1372 zcmd_free_nvlists(&zc);
1378 * Exports the pool from the system. The caller must ensure that there are no
1379 * mounted datasets in the pool.
1382 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1383 const char *log_str)
1385 zfs_cmd_t zc = { 0 };
1388 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1389 "cannot export '%s'"), zhp->zpool_name);
1391 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1392 zc.zc_cookie = force;
1393 zc.zc_guid = hardforce;
1394 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1396 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1399 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1400 "use '-f' to override the following errors:\n"
1401 "'%s' has an active shared spare which could be"
1402 " used by other pools once '%s' is exported."),
1403 zhp->zpool_name, zhp->zpool_name);
1404 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1407 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1416 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1418 return (zpool_export_common(zhp, force, B_FALSE, log_str));
1422 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1424 return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1428 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1431 nvlist_t *nv = NULL;
1437 if (!hdl->libzfs_printerr || config == NULL)
1440 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1441 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1445 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1447 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1449 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1450 strftime(timestr, 128, 0, &t) != 0) {
1452 (void) printf(dgettext(TEXT_DOMAIN,
1453 "Would be able to return %s "
1454 "to its state as of %s.\n"),
1457 (void) printf(dgettext(TEXT_DOMAIN,
1458 "Pool %s returned to its state as of %s.\n"),
1462 (void) printf(dgettext(TEXT_DOMAIN,
1463 "%s approximately %lld "),
1464 dryrun ? "Would discard" : "Discarded",
1466 (void) printf(dgettext(TEXT_DOMAIN,
1467 "minutes of transactions.\n"));
1468 } else if (loss > 0) {
1469 (void) printf(dgettext(TEXT_DOMAIN,
1470 "%s approximately %lld "),
1471 dryrun ? "Would discard" : "Discarded", loss);
1472 (void) printf(dgettext(TEXT_DOMAIN,
1473 "seconds of transactions.\n"));
1479 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1482 nvlist_t *nv = NULL;
1484 uint64_t edata = UINT64_MAX;
1489 if (!hdl->libzfs_printerr)
1493 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1495 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1497 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1498 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1499 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1500 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1503 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1504 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1507 (void) printf(dgettext(TEXT_DOMAIN,
1508 "Recovery is possible, but will result in some data loss.\n"));
1510 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1511 strftime(timestr, 128, 0, &t) != 0) {
1512 (void) printf(dgettext(TEXT_DOMAIN,
1513 "\tReturning the pool to its state as of %s\n"
1514 "\tshould correct the problem. "),
1517 (void) printf(dgettext(TEXT_DOMAIN,
1518 "\tReverting the pool to an earlier state "
1519 "should correct the problem.\n\t"));
1523 (void) printf(dgettext(TEXT_DOMAIN,
1524 "Approximately %lld minutes of data\n"
1525 "\tmust be discarded, irreversibly. "), (loss + 30) / 60);
1526 } else if (loss > 0) {
1527 (void) printf(dgettext(TEXT_DOMAIN,
1528 "Approximately %lld seconds of data\n"
1529 "\tmust be discarded, irreversibly. "), loss);
1531 if (edata != 0 && edata != UINT64_MAX) {
1533 (void) printf(dgettext(TEXT_DOMAIN,
1534 "After rewind, at least\n"
1535 "\tone persistent user-data error will remain. "));
1537 (void) printf(dgettext(TEXT_DOMAIN,
1538 "After rewind, several\n"
1539 "\tpersistent user-data errors will remain. "));
1542 (void) printf(dgettext(TEXT_DOMAIN,
1543 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1544 reason >= 0 ? "clear" : "import", name);
1546 (void) printf(dgettext(TEXT_DOMAIN,
1547 "A scrub of the pool\n"
1548 "\tis strongly recommended after recovery.\n"));
1552 (void) printf(dgettext(TEXT_DOMAIN,
1553 "Destroy and re-create the pool from\n\ta backup source.\n"));
1557 * zpool_import() is a contracted interface. Should be kept the same
1560 * Applications should use zpool_import_props() to import a pool with
1561 * new properties value to be set.
1564 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1567 nvlist_t *props = NULL;
1570 if (altroot != NULL) {
1571 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1572 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1573 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1577 if (nvlist_add_string(props,
1578 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1579 nvlist_add_string(props,
1580 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1582 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1583 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1588 ret = zpool_import_props(hdl, config, newname, props,
1595 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1601 uint64_t is_log = 0;
1603 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1607 (void) printf("\t%*s%s%s\n", indent, "", name,
1608 is_log ? " [log]" : "");
1610 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1611 &child, &children) != 0)
1614 for (c = 0; c < children; c++) {
1615 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
1616 print_vdev_tree(hdl, vname, child[c], indent + 2);
1622 zpool_print_unsup_feat(nvlist_t *config)
1624 nvlist_t *nvinfo, *unsup_feat;
1626 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1628 verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1631 for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1632 nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1635 verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1636 verify(nvpair_value_string(nvp, &desc) == 0);
1638 if (strlen(desc) > 0)
1639 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1641 (void) printf("\t%s\n", nvpair_name(nvp));
1646 * Import the given pool using the known configuration and a list of
1647 * properties to be set. The configuration should have come from
1648 * zpool_find_import(). The 'newname' parameters control whether the pool
1649 * is imported with a different name.
1652 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1653 nvlist_t *props, int flags)
1655 zfs_cmd_t zc = { 0 };
1656 zpool_rewind_policy_t policy;
1657 nvlist_t *nv = NULL;
1658 nvlist_t *nvinfo = NULL;
1659 nvlist_t *missing = NULL;
1666 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1669 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1670 "cannot import pool '%s'"), origname);
1672 if (newname != NULL) {
1673 if (!zpool_name_valid(hdl, B_FALSE, newname))
1674 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1675 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1677 thename = (char *)newname;
1682 if (props != NULL) {
1684 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1686 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1689 if ((props = zpool_valid_proplist(hdl, origname,
1690 props, version, flags, errbuf)) == NULL)
1692 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1699 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1701 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1704 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1705 zcmd_free_nvlists(&zc);
1708 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1709 zcmd_free_nvlists(&zc);
1713 zc.zc_cookie = flags;
1714 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1716 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1717 zcmd_free_nvlists(&zc);
1724 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1726 zcmd_free_nvlists(&zc);
1728 zpool_get_rewind_policy(config, &policy);
1734 * Dry-run failed, but we print out what success
1735 * looks like if we found a best txg
1737 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1738 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1744 if (newname == NULL)
1745 (void) snprintf(desc, sizeof (desc),
1746 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1749 (void) snprintf(desc, sizeof (desc),
1750 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1755 if (nv != NULL && nvlist_lookup_nvlist(nv,
1756 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1757 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1758 (void) printf(dgettext(TEXT_DOMAIN, "This "
1759 "pool uses the following feature(s) not "
1760 "supported by this system:\n"));
1761 zpool_print_unsup_feat(nv);
1762 if (nvlist_exists(nvinfo,
1763 ZPOOL_CONFIG_CAN_RDONLY)) {
1764 (void) printf(dgettext(TEXT_DOMAIN,
1765 "All unsupported features are only "
1766 "required for writing to the pool."
1767 "\nThe pool can be imported using "
1768 "'-o readonly=on'.\n"));
1772 * Unsupported version.
1774 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1778 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1782 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1783 "one or more devices is read only"));
1784 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1788 if (nv && nvlist_lookup_nvlist(nv,
1789 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1790 nvlist_lookup_nvlist(nvinfo,
1791 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1792 (void) printf(dgettext(TEXT_DOMAIN,
1793 "The devices below are missing, use "
1794 "'-m' to import the pool anyway:\n"));
1795 print_vdev_tree(hdl, NULL, missing, 2);
1796 (void) printf("\n");
1798 (void) zpool_standard_error(hdl, error, desc);
1802 (void) zpool_standard_error(hdl, error, desc);
1805 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1806 "new name of at least one dataset is longer than "
1807 "the maximum allowable length"));
1808 (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
1811 (void) zpool_standard_error(hdl, error, desc);
1812 zpool_explain_recover(hdl,
1813 newname ? origname : thename, -error, nv);
1820 zpool_handle_t *zhp;
1823 * This should never fail, but play it safe anyway.
1825 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1827 else if (zhp != NULL)
1829 if (policy.zrp_request &
1830 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1831 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1832 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1845 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd)
1847 zfs_cmd_t zc = { 0 };
1850 libzfs_handle_t *hdl = zhp->zpool_hdl;
1852 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1853 zc.zc_cookie = func;
1856 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0)
1861 /* ECANCELED on a scrub means we resumed a paused scrub */
1862 if (err == ECANCELED && func == POOL_SCAN_SCRUB &&
1863 cmd == POOL_SCRUB_NORMAL)
1866 if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL)
1869 if (func == POOL_SCAN_SCRUB) {
1870 if (cmd == POOL_SCRUB_PAUSE) {
1871 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1872 "cannot pause scrubbing %s"), zc.zc_name);
1874 assert(cmd == POOL_SCRUB_NORMAL);
1875 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1876 "cannot scrub %s"), zc.zc_name);
1878 } else if (func == POOL_SCAN_NONE) {
1879 (void) snprintf(msg, sizeof (msg),
1880 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1883 assert(!"unexpected result");
1888 pool_scan_stat_t *ps = NULL;
1891 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1892 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1893 (void) nvlist_lookup_uint64_array(nvroot,
1894 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1895 if (ps && ps->pss_func == POOL_SCAN_SCRUB) {
1896 if (cmd == POOL_SCRUB_PAUSE)
1897 return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg));
1899 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1901 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1903 } else if (err == ENOENT) {
1904 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1906 return (zpool_standard_error(hdl, err, msg));
1912 * This provides a very minimal check whether a given string is likely a
1913 * c#t#d# style string. Users of this are expected to do their own
1914 * verification of the s# part.
1916 #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1]))
1919 * More elaborate version for ones which may start with "/dev/dsk/"
1923 ctd_check_path(char *str)
1926 * If it starts with a slash, check the last component.
1928 if (str && str[0] == '/') {
1929 char *tmp = strrchr(str, '/');
1932 * If it ends in "/old", check the second-to-last
1933 * component of the string instead.
1935 if (tmp != str && strcmp(tmp, "/old") == 0) {
1936 for (tmp--; *tmp != '/'; tmp--)
1941 return (CTD_CHECK(str));
1946 * Find a vdev that matches the search criteria specified. We use the
1947 * the nvpair name to determine how we should look for the device.
1948 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1949 * spare; but FALSE if its an INUSE spare.
1952 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1953 boolean_t *l2cache, boolean_t *log)
1960 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1962 /* Nothing to look for */
1963 if (search == NULL || pair == NULL)
1966 /* Obtain the key we will use to search */
1967 srchkey = nvpair_name(pair);
1969 switch (nvpair_type(pair)) {
1970 case DATA_TYPE_UINT64:
1971 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1972 uint64_t srchval, theguid;
1974 verify(nvpair_value_uint64(pair, &srchval) == 0);
1975 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1977 if (theguid == srchval)
1982 case DATA_TYPE_STRING: {
1983 char *srchval, *val;
1985 verify(nvpair_value_string(pair, &srchval) == 0);
1986 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1990 * Search for the requested value. Special cases:
1992 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
1993 * "s0" or "s0/old". The "s0" part is hidden from the user,
1994 * but included in the string, so this matches around it.
1995 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1997 * Otherwise, all other searches are simple string compares.
2000 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
2001 ctd_check_path(val)) {
2002 uint64_t wholedisk = 0;
2004 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2007 int slen = strlen(srchval);
2008 int vlen = strlen(val);
2010 if (slen != vlen - 2)
2014 * make_leaf_vdev() should only set
2015 * wholedisk for ZPOOL_CONFIG_PATHs which
2016 * will include "/dev/dsk/", giving plenty of
2017 * room for the indices used next.
2022 * strings identical except trailing "s0"
2024 if (strcmp(&val[vlen - 2], "s0") == 0 &&
2025 strncmp(srchval, val, slen) == 0)
2029 * strings identical except trailing "s0/old"
2031 if (strcmp(&val[vlen - 6], "s0/old") == 0 &&
2032 strcmp(&srchval[slen - 4], "/old") == 0 &&
2033 strncmp(srchval, val, slen - 4) == 0)
2038 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2040 if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2042 char *type, *idx, *end, *p;
2043 uint64_t id, vdev_id;
2046 * Determine our vdev type, keeping in mind
2047 * that the srchval is composed of a type and
2048 * vdev id pair (i.e. mirror-4).
2050 if ((type = strdup(srchval)) == NULL)
2053 if ((p = strrchr(type, '-')) == NULL) {
2061 * If the types don't match then keep looking.
2063 if (strncmp(val, type, strlen(val)) != 0) {
2068 verify(strncmp(type, VDEV_TYPE_RAIDZ,
2069 strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2070 strncmp(type, VDEV_TYPE_MIRROR,
2071 strlen(VDEV_TYPE_MIRROR)) == 0);
2072 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2076 vdev_id = strtoull(idx, &end, 10);
2083 * Now verify that we have the correct vdev id.
2092 if (strcmp(srchval, val) == 0)
2101 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2102 &child, &children) != 0)
2105 for (c = 0; c < children; c++) {
2106 if ((ret = vdev_to_nvlist_iter(child[c], search,
2107 avail_spare, l2cache, NULL)) != NULL) {
2109 * The 'is_log' value is only set for the toplevel
2110 * vdev, not the leaf vdevs. So we always lookup the
2111 * log device from the root of the vdev tree (where
2112 * 'log' is non-NULL).
2115 nvlist_lookup_uint64(child[c],
2116 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2124 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2125 &child, &children) == 0) {
2126 for (c = 0; c < children; c++) {
2127 if ((ret = vdev_to_nvlist_iter(child[c], search,
2128 avail_spare, l2cache, NULL)) != NULL) {
2129 *avail_spare = B_TRUE;
2135 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2136 &child, &children) == 0) {
2137 for (c = 0; c < children; c++) {
2138 if ((ret = vdev_to_nvlist_iter(child[c], search,
2139 avail_spare, l2cache, NULL)) != NULL) {
2150 * Given a physical path (minus the "/devices" prefix), find the
2154 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2155 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2157 nvlist_t *search, *nvroot, *ret;
2159 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2160 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2162 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2165 *avail_spare = B_FALSE;
2169 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2170 nvlist_free(search);
2176 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2179 zpool_vdev_is_interior(const char *name)
2181 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2182 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2188 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2189 boolean_t *l2cache, boolean_t *log)
2191 char buf[MAXPATHLEN];
2193 nvlist_t *nvroot, *search, *ret;
2196 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2198 guid = strtoull(path, &end, 10);
2199 if (guid != 0 && *end == '\0') {
2200 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2201 } else if (zpool_vdev_is_interior(path)) {
2202 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2203 } else if (path[0] != '/') {
2204 (void) snprintf(buf, sizeof (buf), "%s%s", _PATH_DEV, path);
2205 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
2207 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2210 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2213 *avail_spare = B_FALSE;
2217 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2218 nvlist_free(search);
2224 vdev_online(nvlist_t *nv)
2228 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2229 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2230 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2237 * Helper function for zpool_get_physpaths().
2240 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2241 size_t *bytes_written)
2243 size_t bytes_left, pos, rsz;
2247 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2249 return (EZFS_NODEVICE);
2251 pos = *bytes_written;
2252 bytes_left = physpath_size - pos;
2253 format = (pos == 0) ? "%s" : " %s";
2255 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2256 *bytes_written += rsz;
2258 if (rsz >= bytes_left) {
2259 /* if physpath was not copied properly, clear it */
2260 if (bytes_left != 0) {
2263 return (EZFS_NOSPC);
2269 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2270 size_t *rsz, boolean_t is_spare)
2275 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2276 return (EZFS_INVALCONFIG);
2278 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2280 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2281 * For a spare vdev, we only want to boot from the active
2286 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2289 return (EZFS_INVALCONFIG);
2292 if (vdev_online(nv)) {
2293 if ((ret = vdev_get_one_physpath(nv, physpath,
2294 phypath_size, rsz)) != 0)
2297 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2298 strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
2299 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2300 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2305 if (nvlist_lookup_nvlist_array(nv,
2306 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2307 return (EZFS_INVALCONFIG);
2309 for (i = 0; i < count; i++) {
2310 ret = vdev_get_physpaths(child[i], physpath,
2311 phypath_size, rsz, is_spare);
2312 if (ret == EZFS_NOSPC)
2317 return (EZFS_POOL_INVALARG);
2321 * Get phys_path for a root pool config.
2322 * Return 0 on success; non-zero on failure.
2325 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2328 nvlist_t *vdev_root;
2335 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2337 return (EZFS_INVALCONFIG);
2339 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2340 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2341 &child, &count) != 0)
2342 return (EZFS_INVALCONFIG);
2345 * root pool can only have a single top-level vdev.
2347 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2348 return (EZFS_POOL_INVALARG);
2350 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2353 /* No online devices */
2355 return (EZFS_NODEVICE);
2361 * Get phys_path for a root pool
2362 * Return 0 on success; non-zero on failure.
2365 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2367 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2372 * If the device has being dynamically expanded then we need to relabel
2373 * the disk to use the new unallocated space.
2376 zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2379 char path[MAXPATHLEN];
2382 int (*_efi_use_whole_disk)(int);
2384 if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2385 "efi_use_whole_disk")) == NULL)
2388 (void) snprintf(path, sizeof (path), "%s/%s", ZFS_RDISK_ROOT, name);
2390 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2391 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2392 "relabel '%s': unable to open device"), name);
2393 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2397 * It's possible that we might encounter an error if the device
2398 * does not have any unallocated space left. If so, we simply
2399 * ignore that error and continue on.
2401 error = _efi_use_whole_disk(fd);
2403 if (error && error != VT_ENOSPC) {
2404 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2405 "relabel '%s': unable to read disk capacity"), name);
2406 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2408 #endif /* illumos */
2413 * Bring the specified vdev online. The 'flags' parameter is a set of the
2414 * ZFS_ONLINE_* flags.
2417 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2418 vdev_state_t *newstate)
2420 zfs_cmd_t zc = { 0 };
2423 boolean_t avail_spare, l2cache, islog;
2424 libzfs_handle_t *hdl = zhp->zpool_hdl;
2426 if (flags & ZFS_ONLINE_EXPAND) {
2427 (void) snprintf(msg, sizeof (msg),
2428 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2430 (void) snprintf(msg, sizeof (msg),
2431 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2434 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2435 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2437 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2439 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2442 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2444 if (flags & ZFS_ONLINE_EXPAND ||
2445 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2446 char *pathname = NULL;
2447 uint64_t wholedisk = 0;
2449 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2451 verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
2455 * XXX - L2ARC 1.0 devices can't support expansion.
2458 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2459 "cannot expand cache devices"));
2460 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2464 pathname += strlen(ZFS_DISK_ROOT) + 1;
2465 (void) zpool_relabel_disk(hdl, pathname);
2469 zc.zc_cookie = VDEV_STATE_ONLINE;
2472 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2473 if (errno == EINVAL) {
2474 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2475 "from this pool into a new one. Use '%s' "
2476 "instead"), "zpool detach");
2477 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2479 return (zpool_standard_error(hdl, errno, msg));
2482 *newstate = zc.zc_cookie;
2487 * Take the specified vdev offline
2490 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2492 zfs_cmd_t zc = { 0 };
2495 boolean_t avail_spare, l2cache;
2496 libzfs_handle_t *hdl = zhp->zpool_hdl;
2498 (void) snprintf(msg, sizeof (msg),
2499 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2501 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2502 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2504 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2506 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2509 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2511 zc.zc_cookie = VDEV_STATE_OFFLINE;
2512 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2514 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2521 * There are no other replicas of this device.
2523 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2527 * The log device has unplayed logs
2529 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2532 return (zpool_standard_error(hdl, errno, msg));
2537 * Mark the given vdev faulted.
2540 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2542 zfs_cmd_t zc = { 0 };
2544 libzfs_handle_t *hdl = zhp->zpool_hdl;
2546 (void) snprintf(msg, sizeof (msg),
2547 dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2549 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2551 zc.zc_cookie = VDEV_STATE_FAULTED;
2554 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2561 * There are no other replicas of this device.
2563 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2566 return (zpool_standard_error(hdl, errno, msg));
2572 * Mark the given vdev degraded.
2575 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2577 zfs_cmd_t zc = { 0 };
2579 libzfs_handle_t *hdl = zhp->zpool_hdl;
2581 (void) snprintf(msg, sizeof (msg),
2582 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
2584 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2586 zc.zc_cookie = VDEV_STATE_DEGRADED;
2589 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2592 return (zpool_standard_error(hdl, errno, msg));
2596 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2600 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2606 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2608 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2611 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2612 children == 2 && child[which] == tgt)
2615 for (c = 0; c < children; c++)
2616 if (is_replacing_spare(child[c], tgt, which))
2624 * Attach new_disk (fully described by nvroot) to old_disk.
2625 * If 'replacing' is specified, the new disk will replace the old one.
2628 zpool_vdev_attach(zpool_handle_t *zhp,
2629 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2631 zfs_cmd_t zc = { 0 };
2635 boolean_t avail_spare, l2cache, islog;
2640 nvlist_t *config_root;
2641 libzfs_handle_t *hdl = zhp->zpool_hdl;
2642 boolean_t rootpool = zpool_is_bootable(zhp);
2645 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2646 "cannot replace %s with %s"), old_disk, new_disk);
2648 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2649 "cannot attach %s to %s"), new_disk, old_disk);
2651 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2652 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2654 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2657 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2660 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2662 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2663 zc.zc_cookie = replacing;
2665 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2666 &child, &children) != 0 || children != 1) {
2667 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2668 "new device must be a single disk"));
2669 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2672 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2673 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2675 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2679 * If the target is a hot spare that has been swapped in, we can only
2680 * replace it with another hot spare.
2683 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2684 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2685 NULL) == NULL || !avail_spare) &&
2686 is_replacing_spare(config_root, tgt, 1)) {
2687 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2688 "can only be replaced by another hot spare"));
2690 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2695 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2698 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2700 zcmd_free_nvlists(&zc);
2705 * XXX need a better way to prevent user from
2706 * booting up a half-baked vdev.
2708 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2709 "sure to wait until resilver is done "
2710 "before rebooting.\n"));
2711 (void) fprintf(stderr, "\n");
2712 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "If "
2713 "you boot from pool '%s', you may need to update\n"
2714 "boot code on newly attached disk '%s'.\n\n"
2715 "Assuming you use GPT partitioning and 'da0' is "
2716 "your new boot disk\n"
2717 "you may use the following command:\n\n"
2718 "\tgpart bootcode -b /boot/pmbr -p "
2719 "/boot/gptzfsboot -i 1 da0\n\n"),
2720 zhp->zpool_name, new_disk);
2728 * Can't attach to or replace this type of vdev.
2731 uint64_t version = zpool_get_prop_int(zhp,
2732 ZPOOL_PROP_VERSION, NULL);
2735 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2736 "cannot replace a log with a spare"));
2737 else if (version >= SPA_VERSION_MULTI_REPLACE)
2738 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2739 "already in replacing/spare config; wait "
2740 "for completion or use 'zpool detach'"));
2742 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2743 "cannot replace a replacing device"));
2745 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2746 "can only attach to mirrors and top-level "
2749 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2754 * The new device must be a single disk.
2756 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2757 "new device must be a single disk"));
2758 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2762 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2764 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2769 * The new device is too small.
2771 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2772 "device is too small"));
2773 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2778 * The new device has a different alignment requirement.
2780 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2781 "devices have different sector alignment"));
2782 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2787 * The resulting top-level vdev spec won't fit in the label.
2789 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2793 (void) zpool_standard_error(hdl, errno, msg);
2800 * Detach the specified device.
2803 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2805 zfs_cmd_t zc = { 0 };
2808 boolean_t avail_spare, l2cache;
2809 libzfs_handle_t *hdl = zhp->zpool_hdl;
2811 (void) snprintf(msg, sizeof (msg),
2812 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2814 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2815 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2817 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2820 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2823 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2825 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2827 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2834 * Can't detach from this type of vdev.
2836 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2837 "applicable to mirror and replacing vdevs"));
2838 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2843 * There are no other replicas of this device.
2845 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2849 (void) zpool_standard_error(hdl, errno, msg);
2856 * Find a mirror vdev in the source nvlist.
2858 * The mchild array contains a list of disks in one of the top-level mirrors
2859 * of the source pool. The schild array contains a list of disks that the
2860 * user specified on the command line. We loop over the mchild array to
2861 * see if any entry in the schild array matches.
2863 * If a disk in the mchild array is found in the schild array, we return
2864 * the index of that entry. Otherwise we return -1.
2867 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2868 nvlist_t **schild, uint_t schildren)
2872 for (mc = 0; mc < mchildren; mc++) {
2874 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2875 mchild[mc], B_FALSE);
2877 for (sc = 0; sc < schildren; sc++) {
2878 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2879 schild[sc], B_FALSE);
2880 boolean_t result = (strcmp(mpath, spath) == 0);
2896 * Split a mirror pool. If newroot points to null, then a new nvlist
2897 * is generated and it is the responsibility of the caller to free it.
2900 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2901 nvlist_t *props, splitflags_t flags)
2903 zfs_cmd_t zc = { 0 };
2905 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2906 nvlist_t **varray = NULL, *zc_props = NULL;
2907 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2908 libzfs_handle_t *hdl = zhp->zpool_hdl;
2910 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2913 (void) snprintf(msg, sizeof (msg),
2914 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2916 if (!zpool_name_valid(hdl, B_FALSE, newname))
2917 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2919 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2920 (void) fprintf(stderr, gettext("Internal error: unable to "
2921 "retrieve pool configuration\n"));
2925 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2927 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2930 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2931 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2932 props, vers, flags, msg)) == NULL)
2936 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2938 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2939 "Source pool is missing vdev tree"));
2940 nvlist_free(zc_props);
2944 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2947 if (*newroot == NULL ||
2948 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2949 &newchild, &newchildren) != 0)
2952 for (c = 0; c < children; c++) {
2953 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2955 nvlist_t **mchild, *vdev;
2960 * Unlike cache & spares, slogs are stored in the
2961 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2963 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2965 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2967 if (is_log || is_hole) {
2969 * Create a hole vdev and put it in the config.
2971 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2973 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2974 VDEV_TYPE_HOLE) != 0)
2976 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2981 varray[vcount++] = vdev;
2985 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
2987 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2988 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2989 "Source pool must be composed only of mirrors\n"));
2990 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2994 verify(nvlist_lookup_nvlist_array(child[c],
2995 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2997 /* find or add an entry for this top-level vdev */
2998 if (newchildren > 0 &&
2999 (entry = find_vdev_entry(zhp, mchild, mchildren,
3000 newchild, newchildren)) >= 0) {
3001 /* We found a disk that the user specified. */
3002 vdev = mchild[entry];
3005 /* User didn't specify a disk for this vdev. */
3006 vdev = mchild[mchildren - 1];
3009 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3013 /* did we find every disk the user specified? */
3014 if (found != newchildren) {
3015 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3016 "include at most one disk from each mirror"));
3017 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3021 /* Prepare the nvlist for populating. */
3022 if (*newroot == NULL) {
3023 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3026 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3027 VDEV_TYPE_ROOT) != 0)
3030 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3033 /* Add all the children we found */
3034 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3035 lastlog == 0 ? vcount : lastlog) != 0)
3039 * If we're just doing a dry run, exit now with success.
3042 memory_err = B_FALSE;
3047 /* now build up the config list & call the ioctl */
3048 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3051 if (nvlist_add_nvlist(newconfig,
3052 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3053 nvlist_add_string(newconfig,
3054 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3055 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3059 * The new pool is automatically part of the namespace unless we
3060 * explicitly export it.
3063 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3064 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3065 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3066 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3068 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3071 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3072 retval = zpool_standard_error(hdl, errno, msg);
3077 memory_err = B_FALSE;
3080 if (varray != NULL) {
3083 for (v = 0; v < vcount; v++)
3084 nvlist_free(varray[v]);
3087 zcmd_free_nvlists(&zc);
3088 nvlist_free(zc_props);
3089 nvlist_free(newconfig);
3091 nvlist_free(*newroot);
3099 return (no_memory(hdl));
3105 * Remove the given device. Currently, this is supported only for hot spares
3106 * and level 2 cache devices.
3109 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3111 zfs_cmd_t zc = { 0 };
3114 boolean_t avail_spare, l2cache, islog;
3115 libzfs_handle_t *hdl = zhp->zpool_hdl;
3118 (void) snprintf(msg, sizeof (msg),
3119 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3121 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3122 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3124 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3126 * XXX - this should just go away.
3128 if (!avail_spare && !l2cache && !islog) {
3129 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3130 "only inactive hot spares, cache, top-level, "
3131 "or log devices can be removed"));
3132 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3135 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3136 if (islog && version < SPA_VERSION_HOLES) {
3137 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3138 "pool must be upgrade to support log removal"));
3139 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3142 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3144 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3147 return (zpool_standard_error(hdl, errno, msg));
3151 * Clear the errors for the pool, or the particular device if specified.
3154 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3156 zfs_cmd_t zc = { 0 };
3159 zpool_rewind_policy_t policy;
3160 boolean_t avail_spare, l2cache;
3161 libzfs_handle_t *hdl = zhp->zpool_hdl;
3162 nvlist_t *nvi = NULL;
3166 (void) snprintf(msg, sizeof (msg),
3167 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3170 (void) snprintf(msg, sizeof (msg),
3171 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3174 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3176 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3177 &l2cache, NULL)) == 0)
3178 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3181 * Don't allow error clearing for hot spares. Do allow
3182 * error clearing for l2cache devices.
3185 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3187 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3191 zpool_get_rewind_policy(rewindnvl, &policy);
3192 zc.zc_cookie = policy.zrp_request;
3194 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3197 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3200 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3202 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3203 zcmd_free_nvlists(&zc);
3208 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
3209 errno != EPERM && errno != EACCES)) {
3210 if (policy.zrp_request &
3211 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3212 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3213 zpool_rewind_exclaim(hdl, zc.zc_name,
3214 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
3218 zcmd_free_nvlists(&zc);
3222 zcmd_free_nvlists(&zc);
3223 return (zpool_standard_error(hdl, errno, msg));
3227 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3230 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3232 zfs_cmd_t zc = { 0 };
3234 libzfs_handle_t *hdl = zhp->zpool_hdl;
3236 (void) snprintf(msg, sizeof (msg),
3237 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3240 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3242 zc.zc_cookie = ZPOOL_NO_REWIND;
3244 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3247 return (zpool_standard_error(hdl, errno, msg));
3251 * Change the GUID for a pool.
3254 zpool_reguid(zpool_handle_t *zhp)
3257 libzfs_handle_t *hdl = zhp->zpool_hdl;
3258 zfs_cmd_t zc = { 0 };
3260 (void) snprintf(msg, sizeof (msg),
3261 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3263 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3264 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3267 return (zpool_standard_error(hdl, errno, msg));
3274 zpool_reopen(zpool_handle_t *zhp)
3276 zfs_cmd_t zc = { 0 };
3278 libzfs_handle_t *hdl = zhp->zpool_hdl;
3280 (void) snprintf(msg, sizeof (msg),
3281 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
3284 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3285 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
3287 return (zpool_standard_error(hdl, errno, msg));
3291 * Convert from a devid string to a path.
3294 devid_to_path(char *devid_str)
3299 devid_nmlist_t *list = NULL;
3302 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3305 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3307 devid_str_free(minor);
3314 * In a case the strdup() fails, we will just return NULL below.
3316 path = strdup(list[0].devname);
3318 devid_free_nmlist(list);
3324 * Convert from a path to a devid string.
3327 path_to_devid(const char *path)
3334 if ((fd = open(path, O_RDONLY)) < 0)
3339 if (devid_get(fd, &devid) == 0) {
3340 if (devid_get_minor_name(fd, &minor) == 0)
3341 ret = devid_str_encode(devid, minor);
3343 devid_str_free(minor);
3355 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3356 * ignore any failure here, since a common case is for an unprivileged user to
3357 * type 'zpool status', and we'll display the correct information anyway.
3360 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3362 zfs_cmd_t zc = { 0 };
3364 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3365 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3366 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3369 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3373 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3374 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3375 * We also check if this is a whole disk, in which case we strip off the
3376 * trailing 's0' slice name.
3378 * This routine is also responsible for identifying when disks have been
3379 * reconfigured in a new location. The kernel will have opened the device by
3380 * devid, but the path will still refer to the old location. To catch this, we
3381 * first do a path -> devid translation (which is fast for the common case). If
3382 * the devid matches, we're done. If not, we do a reverse devid -> path
3383 * translation and issue the appropriate ioctl() to update the path of the vdev.
3384 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3388 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3399 have_stats = nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3400 (uint64_t **)&vs, &vsc) == 0;
3401 have_path = nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0;
3404 * If the device is not currently present, assume it will not
3405 * come back at the same device path. Display the device by GUID.
3407 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3408 have_path && have_stats && vs->vs_state <= VDEV_STATE_CANT_OPEN) {
3409 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3411 (void) snprintf(buf, sizeof (buf), "%llu",
3412 (u_longlong_t)value);
3414 } else if (have_path) {
3417 * If the device is dead (faulted, offline, etc) then don't
3418 * bother opening it. Otherwise we may be forcing the user to
3419 * open a misbehaving device, which can have undesirable
3422 if ((have_stats == 0 ||
3423 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3425 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3427 * Determine if the current path is correct.
3429 char *newdevid = path_to_devid(path);
3431 if (newdevid == NULL ||
3432 strcmp(devid, newdevid) != 0) {
3435 if ((newpath = devid_to_path(devid)) != NULL) {
3437 * Update the path appropriately.
3439 set_path(zhp, nv, newpath);
3440 if (nvlist_add_string(nv,
3441 ZPOOL_CONFIG_PATH, newpath) == 0)
3442 verify(nvlist_lookup_string(nv,
3450 devid_str_free(newdevid);
3454 if (strncmp(path, ZFS_DISK_ROOTD, strlen(ZFS_DISK_ROOTD)) == 0)
3455 path += strlen(ZFS_DISK_ROOTD);
3457 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3458 &value) == 0 && value) {
3459 int pathlen = strlen(path);
3460 char *tmp = zfs_strdup(hdl, path);
3463 * If it starts with c#, and ends with "s0", chop
3464 * the "s0" off, or if it ends with "s0/old", remove
3465 * the "s0" from the middle.
3467 if (CTD_CHECK(tmp)) {
3468 if (strcmp(&tmp[pathlen - 2], "s0") == 0) {
3469 tmp[pathlen - 2] = '\0';
3470 } else if (pathlen > 6 &&
3471 strcmp(&tmp[pathlen - 6], "s0/old") == 0) {
3472 (void) strcpy(&tmp[pathlen - 6],
3478 #else /* !illumos */
3479 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3480 path += sizeof(_PATH_DEV) - 1;
3481 #endif /* illumos */
3483 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3486 * If it's a raidz device, we need to stick in the parity level.
3488 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3489 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3491 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3492 (u_longlong_t)value);
3497 * We identify each top-level vdev by using a <type-id>
3498 * naming convention.
3503 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3505 (void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3511 return (zfs_strdup(hdl, path));
3515 zbookmark_mem_compare(const void *a, const void *b)
3517 return (memcmp(a, b, sizeof (zbookmark_phys_t)));
3521 * Retrieve the persistent error log, uniquify the members, and return to the
3525 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3527 zfs_cmd_t zc = { 0 };
3529 zbookmark_phys_t *zb = NULL;
3533 * Retrieve the raw error list from the kernel. If the number of errors
3534 * has increased, allocate more space and continue until we get the
3537 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3541 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3542 count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL)
3544 zc.zc_nvlist_dst_size = count;
3545 (void) strcpy(zc.zc_name, zhp->zpool_name);
3547 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3549 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3550 if (errno == ENOMEM) {
3553 count = zc.zc_nvlist_dst_size;
3554 dst = zfs_alloc(zhp->zpool_hdl, count *
3555 sizeof (zbookmark_phys_t));
3558 zc.zc_nvlist_dst = (uintptr_t)dst;
3568 * Sort the resulting bookmarks. This is a little confusing due to the
3569 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3570 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3571 * _not_ copied as part of the process. So we point the start of our
3572 * array appropriate and decrement the total number of elements.
3574 zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
3575 zc.zc_nvlist_dst_size;
3576 count -= zc.zc_nvlist_dst_size;
3578 qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
3580 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3583 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3585 for (i = 0; i < count; i++) {
3588 /* ignoring zb_blkid and zb_level for now */
3589 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3590 zb[i-1].zb_object == zb[i].zb_object)
3593 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3595 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3596 zb[i].zb_objset) != 0) {
3600 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3601 zb[i].zb_object) != 0) {
3605 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3612 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3616 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3617 return (no_memory(zhp->zpool_hdl));
3621 * Upgrade a ZFS pool to the latest on-disk version.
3624 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3626 zfs_cmd_t zc = { 0 };
3627 libzfs_handle_t *hdl = zhp->zpool_hdl;
3629 (void) strcpy(zc.zc_name, zhp->zpool_name);
3630 zc.zc_cookie = new_version;
3632 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3633 return (zpool_standard_error_fmt(hdl, errno,
3634 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3640 zfs_save_arguments(int argc, char **argv, char *string, int len)
3642 (void) strlcpy(string, basename(argv[0]), len);
3643 for (int i = 1; i < argc; i++) {
3644 (void) strlcat(string, " ", len);
3645 (void) strlcat(string, argv[i], len);
3650 zpool_log_history(libzfs_handle_t *hdl, const char *message)
3652 zfs_cmd_t zc = { 0 };
3656 args = fnvlist_alloc();
3657 fnvlist_add_string(args, "message", message);
3658 err = zcmd_write_src_nvlist(hdl, &zc, args);
3660 err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
3662 zcmd_free_nvlists(&zc);
3667 * Perform ioctl to get some command history of a pool.
3669 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3670 * logical offset of the history buffer to start reading from.
3672 * Upon return, 'off' is the next logical offset to read from and
3673 * 'len' is the actual amount of bytes read into 'buf'.
3676 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3678 zfs_cmd_t zc = { 0 };
3679 libzfs_handle_t *hdl = zhp->zpool_hdl;
3681 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3683 zc.zc_history = (uint64_t)(uintptr_t)buf;
3684 zc.zc_history_len = *len;
3685 zc.zc_history_offset = *off;
3687 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3690 return (zfs_error_fmt(hdl, EZFS_PERM,
3691 dgettext(TEXT_DOMAIN,
3692 "cannot show history for pool '%s'"),
3695 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3696 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3697 "'%s'"), zhp->zpool_name));
3699 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3700 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3701 "'%s', pool must be upgraded"), zhp->zpool_name));
3703 return (zpool_standard_error_fmt(hdl, errno,
3704 dgettext(TEXT_DOMAIN,
3705 "cannot get history for '%s'"), zhp->zpool_name));
3709 *len = zc.zc_history_len;
3710 *off = zc.zc_history_offset;
3716 * Process the buffer of nvlists, unpacking and storing each nvlist record
3717 * into 'records'. 'leftover' is set to the number of bytes that weren't
3718 * processed as there wasn't a complete record.
3721 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3722 nvlist_t ***records, uint_t *numrecords)
3728 while (bytes_read > sizeof (reclen)) {
3730 /* get length of packed record (stored as little endian) */
3731 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3732 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3734 if (bytes_read < sizeof (reclen) + reclen)
3738 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3740 bytes_read -= sizeof (reclen) + reclen;
3741 buf += sizeof (reclen) + reclen;
3743 /* add record to nvlist array */
3745 if (ISP2(*numrecords + 1)) {
3746 *records = realloc(*records,
3747 *numrecords * 2 * sizeof (nvlist_t *));
3749 (*records)[*numrecords - 1] = nv;
3752 *leftover = bytes_read;
3756 /* from spa_history.c: spa_history_create_obj() */
3757 #define HIS_BUF_LEN_DEF (128 << 10)
3758 #define HIS_BUF_LEN_MAX (1 << 30)
3761 * Retrieve the command history of a pool.
3764 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3767 uint64_t buflen = HIS_BUF_LEN_DEF;
3769 nvlist_t **records = NULL;
3770 uint_t numrecords = 0;
3773 buf = malloc(buflen);
3777 uint64_t bytes_read = buflen;
3780 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3783 /* if nothing else was read in, we're at EOF, just return */
3784 if (bytes_read == 0)
3787 if ((err = zpool_history_unpack(buf, bytes_read,
3788 &leftover, &records, &numrecords)) != 0)
3791 if (leftover == bytes_read) {
3793 * no progress made, because buffer is not big enough
3794 * to hold this record; resize and retry.
3799 if ((buflen >= HIS_BUF_LEN_MAX) ||
3800 ((buf = malloc(buflen)) == NULL)) {
3812 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3813 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3814 records, numrecords) == 0);
3816 for (i = 0; i < numrecords; i++)
3817 nvlist_free(records[i]);
3824 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3825 char *pathname, size_t len)
3827 zfs_cmd_t zc = { 0 };
3828 boolean_t mounted = B_FALSE;
3829 char *mntpnt = NULL;
3830 char dsname[ZFS_MAX_DATASET_NAME_LEN];
3833 /* special case for the MOS */
3834 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
3838 /* get the dataset's name */
3839 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3841 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3842 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3843 /* just write out a path of two object numbers */
3844 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3848 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3850 /* find out if the dataset is mounted */
3851 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3853 /* get the corrupted object's path */
3854 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3856 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3859 (void) snprintf(pathname, len, "%s%s", mntpnt,
3862 (void) snprintf(pathname, len, "%s:%s",
3863 dsname, zc.zc_value);
3866 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
3873 * Read the EFI label from the config, if a label does not exist then
3874 * pass back the error to the caller. If the caller has passed a non-NULL
3875 * diskaddr argument then we set it to the starting address of the EFI
3879 read_efi_label(nvlist_t *config, diskaddr_t *sb)
3883 char diskname[MAXPATHLEN];
3886 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3889 (void) snprintf(diskname, sizeof (diskname), "%s%s", ZFS_RDISK_ROOT,
3890 strrchr(path, '/'));
3891 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
3892 struct dk_gpt *vtoc;
3894 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3896 *sb = vtoc->efi_parts[0].p_start;
3905 * determine where a partition starts on a disk in the current
3909 find_start_block(nvlist_t *config)
3913 diskaddr_t sb = MAXOFFSET_T;
3916 if (nvlist_lookup_nvlist_array(config,
3917 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3918 if (nvlist_lookup_uint64(config,
3919 ZPOOL_CONFIG_WHOLE_DISK,
3920 &wholedisk) != 0 || !wholedisk) {
3921 return (MAXOFFSET_T);
3923 if (read_efi_label(config, &sb) < 0)
3928 for (c = 0; c < children; c++) {
3929 sb = find_start_block(child[c]);
3930 if (sb != MAXOFFSET_T) {
3934 return (MAXOFFSET_T);
3936 #endif /* illumos */
3939 * Label an individual disk. The name provided is the short name,
3940 * stripped of any leading /dev path.
3943 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name)
3946 char path[MAXPATHLEN];
3947 struct dk_gpt *vtoc;
3949 size_t resv = EFI_MIN_RESV_SIZE;
3950 uint64_t slice_size;
3951 diskaddr_t start_block;
3954 /* prepare an error message just in case */
3955 (void) snprintf(errbuf, sizeof (errbuf),
3956 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3961 verify(nvlist_lookup_nvlist(zhp->zpool_config,
3962 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3964 if (zhp->zpool_start_block == 0)
3965 start_block = find_start_block(nvroot);
3967 start_block = zhp->zpool_start_block;
3968 zhp->zpool_start_block = start_block;
3971 start_block = NEW_START_BLOCK;
3974 (void) snprintf(path, sizeof (path), "%s/%s%s", ZFS_RDISK_ROOT, name,
3977 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
3979 * This shouldn't happen. We've long since verified that this
3980 * is a valid device.
3983 dgettext(TEXT_DOMAIN, "unable to open device"));
3984 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
3987 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
3989 * The only way this can fail is if we run out of memory, or we
3990 * were unable to read the disk's capacity
3992 if (errno == ENOMEM)
3993 (void) no_memory(hdl);
3996 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3997 "unable to read disk capacity"), name);
3999 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
4002 slice_size = vtoc->efi_last_u_lba + 1;
4003 slice_size -= EFI_MIN_RESV_SIZE;
4004 if (start_block == MAXOFFSET_T)
4005 start_block = NEW_START_BLOCK;
4006 slice_size -= start_block;
4008 vtoc->efi_parts[0].p_start = start_block;
4009 vtoc->efi_parts[0].p_size = slice_size;
4012 * Why we use V_USR: V_BACKUP confuses users, and is considered
4013 * disposable by some EFI utilities (since EFI doesn't have a backup
4014 * slice). V_UNASSIGNED is supposed to be used only for zero size
4015 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4016 * etc. were all pretty specific. V_USR is as close to reality as we
4017 * can get, in the absence of V_OTHER.
4019 vtoc->efi_parts[0].p_tag = V_USR;
4020 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
4022 vtoc->efi_parts[8].p_start = slice_size + start_block;
4023 vtoc->efi_parts[8].p_size = resv;
4024 vtoc->efi_parts[8].p_tag = V_RESERVED;
4026 if (efi_write(fd, vtoc) != 0) {
4028 * Some block drivers (like pcata) may not support EFI
4029 * GPT labels. Print out a helpful error message dir-
4030 * ecting the user to manually label the disk and give
4036 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4037 "try using fdisk(1M) and then provide a specific slice"));
4038 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4043 #endif /* illumos */
4048 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
4054 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
4055 if (strcmp(type, VDEV_TYPE_FILE) == 0 ||
4056 strcmp(type, VDEV_TYPE_HOLE) == 0 ||
4057 strcmp(type, VDEV_TYPE_MISSING) == 0) {
4058 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4059 "vdev type '%s' is not supported"), type);
4060 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
4063 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
4064 &child, &children) == 0) {
4065 for (c = 0; c < children; c++) {
4066 if (!supported_dump_vdev_type(hdl, child[c], errbuf))
4074 * Check if this zvol is allowable for use as a dump device; zero if
4075 * it is, > 0 if it isn't, < 0 if it isn't a zvol.
4077 * Allowable storage configurations include mirrors, all raidz variants, and
4078 * pools with log, cache, and spare devices. Pools which are backed by files or
4079 * have missing/hole vdevs are not suitable.
4082 zvol_check_dump_config(char *arg)
4084 zpool_handle_t *zhp = NULL;
4085 nvlist_t *config, *nvroot;
4089 libzfs_handle_t *hdl;
4091 char poolname[ZFS_MAX_DATASET_NAME_LEN];
4092 int pathlen = strlen(ZVOL_FULL_DEV_DIR);
4095 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
4099 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4100 "dump is not supported on device '%s'"), arg);
4102 if ((hdl = libzfs_init()) == NULL)
4104 libzfs_print_on_error(hdl, B_TRUE);
4106 volname = arg + pathlen;
4108 /* check the configuration of the pool */
4109 if ((p = strchr(volname, '/')) == NULL) {
4110 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4111 "malformed dataset name"));
4112 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4114 } else if (p - volname >= ZFS_MAX_DATASET_NAME_LEN) {
4115 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4116 "dataset name is too long"));
4117 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
4120 (void) strncpy(poolname, volname, p - volname);
4121 poolname[p - volname] = '\0';
4124 if ((zhp = zpool_open(hdl, poolname)) == NULL) {
4125 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4126 "could not open pool '%s'"), poolname);
4127 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4130 config = zpool_get_config(zhp, NULL);
4131 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4133 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4134 "could not obtain vdev configuration for '%s'"), poolname);
4135 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4139 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4140 &top, &toplevels) == 0);
4142 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
4155 zpool_nextboot(libzfs_handle_t *hdl, uint64_t pool_guid, uint64_t dev_guid,
4156 const char *command)
4158 zfs_cmd_t zc = { 0 };
4164 args = fnvlist_alloc();
4165 fnvlist_add_uint64(args, ZPOOL_CONFIG_POOL_GUID, pool_guid);
4166 fnvlist_add_uint64(args, ZPOOL_CONFIG_GUID, dev_guid);
4167 fnvlist_add_string(args, "command", command);
4168 error = zcmd_write_src_nvlist(hdl, &zc, args);
4170 error = ioctl(hdl->libzfs_fd, ZFS_IOC_NEXTBOOT, &zc);
4171 zcmd_free_nvlists(&zc);