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.
29 #include <sys/types.h>
41 #include <sys/zfs_ioctl.h>
44 #include "zfs_namecheck.h"
46 #include "libzfs_impl.h"
47 #include "zfs_comutil.h"
48 #include "zfeature_common.h"
50 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
52 #define BACKUP_SLICE "s2"
54 typedef struct prop_flags {
55 int create:1; /* Validate property on creation */
56 int import:1; /* Validate property on import */
60 * ====================================================================
61 * zpool property functions
62 * ====================================================================
66 zpool_get_all_props(zpool_handle_t *zhp)
69 libzfs_handle_t *hdl = zhp->zpool_hdl;
71 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
73 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
76 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
77 if (errno == ENOMEM) {
78 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
79 zcmd_free_nvlists(&zc);
83 zcmd_free_nvlists(&zc);
88 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
89 zcmd_free_nvlists(&zc);
93 zcmd_free_nvlists(&zc);
99 zpool_props_refresh(zpool_handle_t *zhp)
103 old_props = zhp->zpool_props;
105 if (zpool_get_all_props(zhp) != 0)
108 nvlist_free(old_props);
113 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
119 zprop_source_t source;
121 nvl = zhp->zpool_props;
122 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
123 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
125 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
127 source = ZPROP_SRC_DEFAULT;
128 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
139 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
143 zprop_source_t source;
145 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
147 * zpool_get_all_props() has most likely failed because
148 * the pool is faulted, but if all we need is the top level
149 * vdev's guid then get it from the zhp config nvlist.
151 if ((prop == ZPOOL_PROP_GUID) &&
152 (nvlist_lookup_nvlist(zhp->zpool_config,
153 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
154 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
158 return (zpool_prop_default_numeric(prop));
161 nvl = zhp->zpool_props;
162 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
163 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
165 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
167 source = ZPROP_SRC_DEFAULT;
168 value = zpool_prop_default_numeric(prop);
178 * Map VDEV STATE to printed strings.
181 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
184 case VDEV_STATE_CLOSED:
185 case VDEV_STATE_OFFLINE:
186 return (gettext("OFFLINE"));
187 case VDEV_STATE_REMOVED:
188 return (gettext("REMOVED"));
189 case VDEV_STATE_CANT_OPEN:
190 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
191 return (gettext("FAULTED"));
192 else if (aux == VDEV_AUX_SPLIT_POOL)
193 return (gettext("SPLIT"));
195 return (gettext("UNAVAIL"));
196 case VDEV_STATE_FAULTED:
197 return (gettext("FAULTED"));
198 case VDEV_STATE_DEGRADED:
199 return (gettext("DEGRADED"));
200 case VDEV_STATE_HEALTHY:
201 return (gettext("ONLINE"));
204 return (gettext("UNKNOWN"));
208 * Map POOL STATE to printed strings.
211 zpool_pool_state_to_name(pool_state_t state)
214 case POOL_STATE_ACTIVE:
215 return (gettext("ACTIVE"));
216 case POOL_STATE_EXPORTED:
217 return (gettext("EXPORTED"));
218 case POOL_STATE_DESTROYED:
219 return (gettext("DESTROYED"));
220 case POOL_STATE_SPARE:
221 return (gettext("SPARE"));
222 case POOL_STATE_L2CACHE:
223 return (gettext("L2CACHE"));
224 case POOL_STATE_UNINITIALIZED:
225 return (gettext("UNINITIALIZED"));
226 case POOL_STATE_UNAVAIL:
227 return (gettext("UNAVAIL"));
228 case POOL_STATE_POTENTIALLY_ACTIVE:
229 return (gettext("POTENTIALLY_ACTIVE"));
232 return (gettext("UNKNOWN"));
236 * Get a zpool property value for 'prop' and return the value in
237 * a pre-allocated buffer.
240 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
241 zprop_source_t *srctype, boolean_t literal)
245 zprop_source_t src = ZPROP_SRC_NONE;
250 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
252 case ZPOOL_PROP_NAME:
253 (void) strlcpy(buf, zpool_get_name(zhp), len);
256 case ZPOOL_PROP_HEALTH:
258 zpool_pool_state_to_name(POOL_STATE_UNAVAIL), len);
261 case ZPOOL_PROP_GUID:
262 intval = zpool_get_prop_int(zhp, prop, &src);
263 (void) snprintf(buf, len, "%llu", intval);
266 case ZPOOL_PROP_ALTROOT:
267 case ZPOOL_PROP_CACHEFILE:
268 case ZPOOL_PROP_COMMENT:
269 if (zhp->zpool_props != NULL ||
270 zpool_get_all_props(zhp) == 0) {
272 zpool_get_prop_string(zhp, prop, &src),
278 (void) strlcpy(buf, "-", len);
287 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
288 prop != ZPOOL_PROP_NAME)
291 switch (zpool_prop_get_type(prop)) {
292 case PROP_TYPE_STRING:
293 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
297 case PROP_TYPE_NUMBER:
298 intval = zpool_get_prop_int(zhp, prop, &src);
301 case ZPOOL_PROP_SIZE:
302 case ZPOOL_PROP_ALLOCATED:
303 case ZPOOL_PROP_FREE:
304 case ZPOOL_PROP_FREEING:
305 case ZPOOL_PROP_LEAKED:
307 (void) snprintf(buf, len, "%llu",
308 (u_longlong_t)intval);
310 (void) zfs_nicenum(intval, buf, len);
313 case ZPOOL_PROP_EXPANDSZ:
315 (void) strlcpy(buf, "-", len);
316 } else if (literal) {
317 (void) snprintf(buf, len, "%llu",
318 (u_longlong_t)intval);
320 (void) zfs_nicenum(intval, buf, len);
323 case ZPOOL_PROP_CAPACITY:
325 (void) snprintf(buf, len, "%llu",
326 (u_longlong_t)intval);
328 (void) snprintf(buf, len, "%llu%%",
329 (u_longlong_t)intval);
332 case ZPOOL_PROP_FRAGMENTATION:
333 if (intval == UINT64_MAX) {
334 (void) strlcpy(buf, "-", len);
336 (void) snprintf(buf, len, "%llu%%",
337 (u_longlong_t)intval);
340 case ZPOOL_PROP_DEDUPRATIO:
341 (void) snprintf(buf, len, "%llu.%02llux",
342 (u_longlong_t)(intval / 100),
343 (u_longlong_t)(intval % 100));
345 case ZPOOL_PROP_HEALTH:
346 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
347 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
348 verify(nvlist_lookup_uint64_array(nvroot,
349 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
352 (void) strlcpy(buf, zpool_state_to_name(intval,
355 case ZPOOL_PROP_VERSION:
356 if (intval >= SPA_VERSION_FEATURES) {
357 (void) snprintf(buf, len, "-");
362 (void) snprintf(buf, len, "%llu", intval);
366 case PROP_TYPE_INDEX:
367 intval = zpool_get_prop_int(zhp, prop, &src);
368 if (zpool_prop_index_to_string(prop, intval, &strval)
371 (void) strlcpy(buf, strval, len);
385 * Check if the bootfs name has the same pool name as it is set to.
386 * Assuming bootfs is a valid dataset name.
389 bootfs_name_valid(const char *pool, char *bootfs)
391 int len = strlen(pool);
393 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
396 if (strncmp(pool, bootfs, len) == 0 &&
397 (bootfs[len] == '/' || bootfs[len] == '\0'))
404 zpool_is_bootable(zpool_handle_t *zhp)
406 char bootfs[ZPOOL_MAXNAMELEN];
408 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
409 sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-",
410 sizeof (bootfs)) != 0);
415 * Given an nvlist of zpool properties to be set, validate that they are
416 * correct, and parse any numeric properties (index, boolean, etc) if they are
417 * specified as strings.
420 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
421 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
429 struct stat64 statbuf;
432 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
433 (void) no_memory(hdl);
438 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
439 const char *propname = nvpair_name(elem);
441 prop = zpool_name_to_prop(propname);
442 if (prop == ZPROP_INVAL && zpool_prop_feature(propname)) {
444 char *fname = strchr(propname, '@') + 1;
446 err = zfeature_lookup_name(fname, NULL);
448 ASSERT3U(err, ==, ENOENT);
449 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
450 "invalid feature '%s'"), fname);
451 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
455 if (nvpair_type(elem) != DATA_TYPE_STRING) {
456 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
457 "'%s' must be a string"), propname);
458 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
462 (void) nvpair_value_string(elem, &strval);
463 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) {
464 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
465 "property '%s' can only be set to "
466 "'enabled'"), propname);
467 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
471 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
472 (void) no_memory(hdl);
479 * Make sure this property is valid and applies to this type.
481 if (prop == ZPROP_INVAL) {
482 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
483 "invalid property '%s'"), propname);
484 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
488 if (zpool_prop_readonly(prop)) {
489 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
490 "is readonly"), propname);
491 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
495 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
496 &strval, &intval, errbuf) != 0)
500 * Perform additional checking for specific properties.
503 case ZPOOL_PROP_VERSION:
504 if (intval < version ||
505 !SPA_VERSION_IS_SUPPORTED(intval)) {
506 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
507 "property '%s' number %d is invalid."),
509 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
514 case ZPOOL_PROP_BOOTFS:
515 if (flags.create || flags.import) {
516 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
517 "property '%s' cannot be set at creation "
518 "or import time"), propname);
519 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
523 if (version < SPA_VERSION_BOOTFS) {
524 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
525 "pool must be upgraded to support "
526 "'%s' property"), propname);
527 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
532 * bootfs property value has to be a dataset name and
533 * the dataset has to be in the same pool as it sets to.
535 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
537 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
538 "is an invalid name"), strval);
539 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
543 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
544 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
545 "could not open pool '%s'"), poolname);
546 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
552 case ZPOOL_PROP_ALTROOT:
553 if (!flags.create && !flags.import) {
554 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
555 "property '%s' can only be set during pool "
556 "creation or import"), propname);
557 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
561 if (strval[0] != '/') {
562 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
563 "bad alternate root '%s'"), strval);
564 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
569 case ZPOOL_PROP_CACHEFILE:
570 if (strval[0] == '\0')
573 if (strcmp(strval, "none") == 0)
576 if (strval[0] != '/') {
577 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
578 "property '%s' must be empty, an "
579 "absolute path, or 'none'"), propname);
580 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
584 slash = strrchr(strval, '/');
586 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
587 strcmp(slash, "/..") == 0) {
588 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
589 "'%s' is not a valid file"), strval);
590 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
596 if (strval[0] != '\0' &&
597 (stat64(strval, &statbuf) != 0 ||
598 !S_ISDIR(statbuf.st_mode))) {
599 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
600 "'%s' is not a valid directory"),
602 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
609 case ZPOOL_PROP_COMMENT:
610 for (check = strval; *check != '\0'; check++) {
611 if (!isprint(*check)) {
613 dgettext(TEXT_DOMAIN,
614 "comment may only have printable "
616 (void) zfs_error(hdl, EZFS_BADPROP,
621 if (strlen(strval) > ZPROP_MAX_COMMENT) {
622 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
623 "comment must not exceed %d characters"),
625 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
629 case ZPOOL_PROP_READONLY:
631 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
632 "property '%s' can only be set at "
633 "import time"), propname);
634 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
643 nvlist_free(retprops);
648 * Set zpool property : propname=propval.
651 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
653 zfs_cmd_t zc = { 0 };
656 nvlist_t *nvl = NULL;
659 prop_flags_t flags = { 0 };
661 (void) snprintf(errbuf, sizeof (errbuf),
662 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
665 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
666 return (no_memory(zhp->zpool_hdl));
668 if (nvlist_add_string(nvl, propname, propval) != 0) {
670 return (no_memory(zhp->zpool_hdl));
673 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
674 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
675 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
684 * Execute the corresponding ioctl() to set this property.
686 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
688 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
693 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
695 zcmd_free_nvlists(&zc);
699 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
701 (void) zpool_props_refresh(zhp);
707 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
709 libzfs_handle_t *hdl = zhp->zpool_hdl;
711 char buf[ZFS_MAXPROPLEN];
712 nvlist_t *features = NULL;
714 boolean_t firstexpand = (NULL == *plp);
716 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
720 while (*last != NULL)
721 last = &(*last)->pl_next;
724 features = zpool_get_features(zhp);
726 if ((*plp)->pl_all && firstexpand) {
727 for (int i = 0; i < SPA_FEATURES; i++) {
728 zprop_list_t *entry = zfs_alloc(hdl,
729 sizeof (zprop_list_t));
730 entry->pl_prop = ZPROP_INVAL;
731 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
732 spa_feature_table[i].fi_uname);
733 entry->pl_width = strlen(entry->pl_user_prop);
734 entry->pl_all = B_TRUE;
737 last = &entry->pl_next;
741 /* add any unsupported features */
742 for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL);
743 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
748 if (zfeature_is_supported(nvpair_name(nvp)))
751 propname = zfs_asprintf(hdl, "unsupported@%s",
755 * Before adding the property to the list make sure that no
756 * other pool already added the same property.
760 while (entry != NULL) {
761 if (entry->pl_user_prop != NULL &&
762 strcmp(propname, entry->pl_user_prop) == 0) {
766 entry = entry->pl_next;
773 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
774 entry->pl_prop = ZPROP_INVAL;
775 entry->pl_user_prop = propname;
776 entry->pl_width = strlen(entry->pl_user_prop);
777 entry->pl_all = B_TRUE;
780 last = &entry->pl_next;
783 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
788 if (entry->pl_prop != ZPROP_INVAL &&
789 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
790 NULL, B_FALSE) == 0) {
791 if (strlen(buf) > entry->pl_width)
792 entry->pl_width = strlen(buf);
800 * Get the state for the given feature on the given ZFS pool.
803 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
807 boolean_t found = B_FALSE;
808 nvlist_t *features = zpool_get_features(zhp);
810 const char *feature = strchr(propname, '@') + 1;
812 supported = zpool_prop_feature(propname);
813 ASSERT(supported || zpool_prop_unsupported(propname));
816 * Convert from feature name to feature guid. This conversion is
817 * unecessary for unsupported@... properties because they already
824 ret = zfeature_lookup_name(feature, &fid);
826 (void) strlcpy(buf, "-", len);
829 feature = spa_feature_table[fid].fi_guid;
832 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
837 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
840 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
842 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
847 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
849 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
852 (void) strlcpy(buf, "-", len);
861 * Don't start the slice at the default block of 34; many storage
862 * devices will use a stripe width of 128k, so start there instead.
864 #define NEW_START_BLOCK 256
867 * Validate the given pool name, optionally putting an extended error message in
871 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
877 ret = pool_namecheck(pool, &why, &what);
880 * The rules for reserved pool names were extended at a later point.
881 * But we need to support users with existing pools that may now be
882 * invalid. So we only check for this expanded set of names during a
883 * create (or import), and only in userland.
885 if (ret == 0 && !isopen &&
886 (strncmp(pool, "mirror", 6) == 0 ||
887 strncmp(pool, "raidz", 5) == 0 ||
888 strncmp(pool, "spare", 5) == 0 ||
889 strcmp(pool, "log") == 0)) {
892 dgettext(TEXT_DOMAIN, "name is reserved"));
900 case NAME_ERR_TOOLONG:
902 dgettext(TEXT_DOMAIN, "name is too long"));
905 case NAME_ERR_INVALCHAR:
907 dgettext(TEXT_DOMAIN, "invalid character "
908 "'%c' in pool name"), what);
911 case NAME_ERR_NOLETTER:
912 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
913 "name must begin with a letter"));
916 case NAME_ERR_RESERVED:
917 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
918 "name is reserved"));
921 case NAME_ERR_DISKLIKE:
922 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
923 "pool name is reserved"));
926 case NAME_ERR_LEADING_SLASH:
927 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
928 "leading slash in name"));
931 case NAME_ERR_EMPTY_COMPONENT:
932 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
933 "empty component in name"));
936 case NAME_ERR_TRAILING_SLASH:
937 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
938 "trailing slash in name"));
941 case NAME_ERR_MULTIPLE_AT:
942 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
943 "multiple '@' delimiters in name"));
955 * Open a handle to the given pool, even if the pool is currently in the FAULTED
959 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
965 * Make sure the pool name is valid.
967 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
968 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
969 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
974 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
977 zhp->zpool_hdl = hdl;
978 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
980 if (zpool_refresh_stats(zhp, &missing) != 0) {
986 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
987 (void) zfs_error_fmt(hdl, EZFS_NOENT,
988 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
997 * Like the above, but silent on error. Used when iterating over pools (because
998 * the configuration cache may be out of date).
1001 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1003 zpool_handle_t *zhp;
1006 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1009 zhp->zpool_hdl = hdl;
1010 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1012 if (zpool_refresh_stats(zhp, &missing) != 0) {
1028 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1032 zpool_open(libzfs_handle_t *hdl, const char *pool)
1034 zpool_handle_t *zhp;
1036 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1039 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1040 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1041 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1050 * Close the handle. Simply frees the memory associated with the handle.
1053 zpool_close(zpool_handle_t *zhp)
1055 nvlist_free(zhp->zpool_config);
1056 nvlist_free(zhp->zpool_old_config);
1057 nvlist_free(zhp->zpool_props);
1062 * Return the name of the pool.
1065 zpool_get_name(zpool_handle_t *zhp)
1067 return (zhp->zpool_name);
1072 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1075 zpool_get_state(zpool_handle_t *zhp)
1077 return (zhp->zpool_state);
1081 * Create the named pool, using the provided vdev list. It is assumed
1082 * that the consumer has already validated the contents of the nvlist, so we
1083 * don't have to worry about error semantics.
1086 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1087 nvlist_t *props, nvlist_t *fsprops)
1089 zfs_cmd_t zc = { 0 };
1090 nvlist_t *zc_fsprops = NULL;
1091 nvlist_t *zc_props = NULL;
1095 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1096 "cannot create '%s'"), pool);
1098 if (!zpool_name_valid(hdl, B_FALSE, pool))
1099 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1101 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1105 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1107 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1108 SPA_VERSION_1, flags, msg)) == NULL) {
1117 zoned = ((nvlist_lookup_string(fsprops,
1118 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1119 strcmp(zonestr, "on") == 0);
1121 if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
1122 fsprops, zoned, NULL, NULL, msg)) == NULL) {
1126 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1129 if (nvlist_add_nvlist(zc_props,
1130 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1135 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1138 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1140 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1142 zcmd_free_nvlists(&zc);
1143 nvlist_free(zc_props);
1144 nvlist_free(zc_fsprops);
1149 * This can happen if the user has specified the same
1150 * device multiple times. We can't reliably detect this
1151 * until we try to add it and see we already have a
1154 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1155 "one or more vdevs refer to the same device"));
1156 return (zfs_error(hdl, EZFS_BADDEV, msg));
1160 * This happens if the record size is smaller or larger
1161 * than the allowed size range, or not a power of 2.
1163 * NOTE: although zfs_valid_proplist is called earlier,
1164 * this case may have slipped through since the
1165 * pool does not exist yet and it is therefore
1166 * impossible to read properties e.g. max blocksize
1169 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1170 "record size invalid"));
1171 return (zfs_error(hdl, EZFS_BADPROP, msg));
1175 * This occurs when one of the devices is below
1176 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1177 * device was the problem device since there's no
1178 * reliable way to determine device size from userland.
1183 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1185 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1186 "one or more devices is less than the "
1187 "minimum size (%s)"), buf);
1189 return (zfs_error(hdl, EZFS_BADDEV, msg));
1192 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1193 "one or more devices is out of space"));
1194 return (zfs_error(hdl, EZFS_BADDEV, msg));
1197 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1198 "cache device must be a disk or disk slice"));
1199 return (zfs_error(hdl, EZFS_BADDEV, msg));
1202 return (zpool_standard_error(hdl, errno, msg));
1207 zcmd_free_nvlists(&zc);
1208 nvlist_free(zc_props);
1209 nvlist_free(zc_fsprops);
1214 * Destroy the given pool. It is up to the caller to ensure that there are no
1215 * datasets left in the pool.
1218 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1220 zfs_cmd_t zc = { 0 };
1221 zfs_handle_t *zfp = NULL;
1222 libzfs_handle_t *hdl = zhp->zpool_hdl;
1225 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1226 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1229 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1230 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1232 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1233 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1234 "cannot destroy '%s'"), zhp->zpool_name);
1236 if (errno == EROFS) {
1237 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1238 "one or more devices is read only"));
1239 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1241 (void) zpool_standard_error(hdl, errno, msg);
1250 remove_mountpoint(zfp);
1258 * Add the given vdevs to the pool. The caller must have already performed the
1259 * necessary verification to ensure that the vdev specification is well-formed.
1262 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1264 zfs_cmd_t zc = { 0 };
1266 libzfs_handle_t *hdl = zhp->zpool_hdl;
1268 nvlist_t **spares, **l2cache;
1269 uint_t nspares, nl2cache;
1271 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1272 "cannot add to '%s'"), zhp->zpool_name);
1274 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1275 SPA_VERSION_SPARES &&
1276 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1277 &spares, &nspares) == 0) {
1278 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1279 "upgraded to add hot spares"));
1280 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1283 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1284 SPA_VERSION_L2CACHE &&
1285 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1286 &l2cache, &nl2cache) == 0) {
1287 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1288 "upgraded to add cache devices"));
1289 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1292 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1294 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1296 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1300 * This can happen if the user has specified the same
1301 * device multiple times. We can't reliably detect this
1302 * until we try to add it and see we already have a
1305 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1306 "one or more vdevs refer to the same device"));
1307 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1312 * This occurrs when one of the devices is below
1313 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1314 * device was the problem device since there's no
1315 * reliable way to determine device size from userland.
1320 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1322 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1323 "device is less than the minimum "
1326 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1330 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1331 "pool must be upgraded to add these vdevs"));
1332 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1336 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1337 "root pool can not have multiple vdevs"
1338 " or separate logs"));
1339 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1343 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1344 "cache device must be a disk or disk slice"));
1345 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1349 (void) zpool_standard_error(hdl, errno, msg);
1357 zcmd_free_nvlists(&zc);
1363 * Exports the pool from the system. The caller must ensure that there are no
1364 * mounted datasets in the pool.
1367 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1368 const char *log_str)
1370 zfs_cmd_t zc = { 0 };
1373 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1374 "cannot export '%s'"), zhp->zpool_name);
1376 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1377 zc.zc_cookie = force;
1378 zc.zc_guid = hardforce;
1379 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1381 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1384 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1385 "use '-f' to override the following errors:\n"
1386 "'%s' has an active shared spare which could be"
1387 " used by other pools once '%s' is exported."),
1388 zhp->zpool_name, zhp->zpool_name);
1389 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1392 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1401 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1403 return (zpool_export_common(zhp, force, B_FALSE, log_str));
1407 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1409 return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1413 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1416 nvlist_t *nv = NULL;
1422 if (!hdl->libzfs_printerr || config == NULL)
1425 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1426 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1430 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1432 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1434 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1435 strftime(timestr, 128, 0, &t) != 0) {
1437 (void) printf(dgettext(TEXT_DOMAIN,
1438 "Would be able to return %s "
1439 "to its state as of %s.\n"),
1442 (void) printf(dgettext(TEXT_DOMAIN,
1443 "Pool %s returned to its state as of %s.\n"),
1447 (void) printf(dgettext(TEXT_DOMAIN,
1448 "%s approximately %lld "),
1449 dryrun ? "Would discard" : "Discarded",
1451 (void) printf(dgettext(TEXT_DOMAIN,
1452 "minutes of transactions.\n"));
1453 } else if (loss > 0) {
1454 (void) printf(dgettext(TEXT_DOMAIN,
1455 "%s approximately %lld "),
1456 dryrun ? "Would discard" : "Discarded", loss);
1457 (void) printf(dgettext(TEXT_DOMAIN,
1458 "seconds of transactions.\n"));
1464 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1467 nvlist_t *nv = NULL;
1469 uint64_t edata = UINT64_MAX;
1474 if (!hdl->libzfs_printerr)
1478 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1480 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1482 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1483 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1484 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1485 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1488 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1489 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1492 (void) printf(dgettext(TEXT_DOMAIN,
1493 "Recovery is possible, but will result in some data loss.\n"));
1495 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1496 strftime(timestr, 128, 0, &t) != 0) {
1497 (void) printf(dgettext(TEXT_DOMAIN,
1498 "\tReturning the pool to its state as of %s\n"
1499 "\tshould correct the problem. "),
1502 (void) printf(dgettext(TEXT_DOMAIN,
1503 "\tReverting the pool to an earlier state "
1504 "should correct the problem.\n\t"));
1508 (void) printf(dgettext(TEXT_DOMAIN,
1509 "Approximately %lld minutes of data\n"
1510 "\tmust be discarded, irreversibly. "), (loss + 30) / 60);
1511 } else if (loss > 0) {
1512 (void) printf(dgettext(TEXT_DOMAIN,
1513 "Approximately %lld seconds of data\n"
1514 "\tmust be discarded, irreversibly. "), loss);
1516 if (edata != 0 && edata != UINT64_MAX) {
1518 (void) printf(dgettext(TEXT_DOMAIN,
1519 "After rewind, at least\n"
1520 "\tone persistent user-data error will remain. "));
1522 (void) printf(dgettext(TEXT_DOMAIN,
1523 "After rewind, several\n"
1524 "\tpersistent user-data errors will remain. "));
1527 (void) printf(dgettext(TEXT_DOMAIN,
1528 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1529 reason >= 0 ? "clear" : "import", name);
1531 (void) printf(dgettext(TEXT_DOMAIN,
1532 "A scrub of the pool\n"
1533 "\tis strongly recommended after recovery.\n"));
1537 (void) printf(dgettext(TEXT_DOMAIN,
1538 "Destroy and re-create the pool from\n\ta backup source.\n"));
1542 * zpool_import() is a contracted interface. Should be kept the same
1545 * Applications should use zpool_import_props() to import a pool with
1546 * new properties value to be set.
1549 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1552 nvlist_t *props = NULL;
1555 if (altroot != NULL) {
1556 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1557 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1558 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1562 if (nvlist_add_string(props,
1563 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1564 nvlist_add_string(props,
1565 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1567 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1568 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1573 ret = zpool_import_props(hdl, config, newname, props,
1580 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1586 uint64_t is_log = 0;
1588 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1592 (void) printf("\t%*s%s%s\n", indent, "", name,
1593 is_log ? " [log]" : "");
1595 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1596 &child, &children) != 0)
1599 for (c = 0; c < children; c++) {
1600 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
1601 print_vdev_tree(hdl, vname, child[c], indent + 2);
1607 zpool_print_unsup_feat(nvlist_t *config)
1609 nvlist_t *nvinfo, *unsup_feat;
1611 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1613 verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1616 for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1617 nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1620 verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1621 verify(nvpair_value_string(nvp, &desc) == 0);
1623 if (strlen(desc) > 0)
1624 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1626 (void) printf("\t%s\n", nvpair_name(nvp));
1631 * Import the given pool using the known configuration and a list of
1632 * properties to be set. The configuration should have come from
1633 * zpool_find_import(). The 'newname' parameters control whether the pool
1634 * is imported with a different name.
1637 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1638 nvlist_t *props, int flags)
1640 zfs_cmd_t zc = { 0 };
1641 zpool_rewind_policy_t policy;
1642 nvlist_t *nv = NULL;
1643 nvlist_t *nvinfo = NULL;
1644 nvlist_t *missing = NULL;
1651 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1654 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1655 "cannot import pool '%s'"), origname);
1657 if (newname != NULL) {
1658 if (!zpool_name_valid(hdl, B_FALSE, newname))
1659 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1660 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1662 thename = (char *)newname;
1667 if (props != NULL) {
1669 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1671 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1674 if ((props = zpool_valid_proplist(hdl, origname,
1675 props, version, flags, errbuf)) == NULL)
1677 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1684 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1686 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1689 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1690 zcmd_free_nvlists(&zc);
1693 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1694 zcmd_free_nvlists(&zc);
1698 zc.zc_cookie = flags;
1699 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1701 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1702 zcmd_free_nvlists(&zc);
1709 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1711 zcmd_free_nvlists(&zc);
1713 zpool_get_rewind_policy(config, &policy);
1719 * Dry-run failed, but we print out what success
1720 * looks like if we found a best txg
1722 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1723 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1729 if (newname == NULL)
1730 (void) snprintf(desc, sizeof (desc),
1731 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1734 (void) snprintf(desc, sizeof (desc),
1735 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1740 if (nv != NULL && nvlist_lookup_nvlist(nv,
1741 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1742 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1743 (void) printf(dgettext(TEXT_DOMAIN, "This "
1744 "pool uses the following feature(s) not "
1745 "supported by this system:\n"));
1746 zpool_print_unsup_feat(nv);
1747 if (nvlist_exists(nvinfo,
1748 ZPOOL_CONFIG_CAN_RDONLY)) {
1749 (void) printf(dgettext(TEXT_DOMAIN,
1750 "All unsupported features are only "
1751 "required for writing to the pool."
1752 "\nThe pool can be imported using "
1753 "'-o readonly=on'.\n"));
1757 * Unsupported version.
1759 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1763 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1767 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1768 "one or more devices is read only"));
1769 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1773 if (nv && nvlist_lookup_nvlist(nv,
1774 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1775 nvlist_lookup_nvlist(nvinfo,
1776 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1777 (void) printf(dgettext(TEXT_DOMAIN,
1778 "The devices below are missing, use "
1779 "'-m' to import the pool anyway:\n"));
1780 print_vdev_tree(hdl, NULL, missing, 2);
1781 (void) printf("\n");
1783 (void) zpool_standard_error(hdl, error, desc);
1787 (void) zpool_standard_error(hdl, error, desc);
1791 (void) zpool_standard_error(hdl, error, desc);
1792 zpool_explain_recover(hdl,
1793 newname ? origname : thename, -error, nv);
1800 zpool_handle_t *zhp;
1803 * This should never fail, but play it safe anyway.
1805 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1807 else if (zhp != NULL)
1809 if (policy.zrp_request &
1810 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1811 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1812 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1825 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1827 zfs_cmd_t zc = { 0 };
1829 libzfs_handle_t *hdl = zhp->zpool_hdl;
1831 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1832 zc.zc_cookie = func;
1834 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
1835 (errno == ENOENT && func != POOL_SCAN_NONE))
1838 if (func == POOL_SCAN_SCRUB) {
1839 (void) snprintf(msg, sizeof (msg),
1840 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1841 } else if (func == POOL_SCAN_NONE) {
1842 (void) snprintf(msg, sizeof (msg),
1843 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1846 assert(!"unexpected result");
1849 if (errno == EBUSY) {
1851 pool_scan_stat_t *ps = NULL;
1854 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1855 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1856 (void) nvlist_lookup_uint64_array(nvroot,
1857 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1858 if (ps && ps->pss_func == POOL_SCAN_SCRUB)
1859 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1861 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1862 } else if (errno == ENOENT) {
1863 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1865 return (zpool_standard_error(hdl, errno, msg));
1871 * This provides a very minimal check whether a given string is likely a
1872 * c#t#d# style string. Users of this are expected to do their own
1873 * verification of the s# part.
1875 #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1]))
1878 * More elaborate version for ones which may start with "/dev/dsk/"
1882 ctd_check_path(char *str)
1885 * If it starts with a slash, check the last component.
1887 if (str && str[0] == '/') {
1888 char *tmp = strrchr(str, '/');
1891 * If it ends in "/old", check the second-to-last
1892 * component of the string instead.
1894 if (tmp != str && strcmp(tmp, "/old") == 0) {
1895 for (tmp--; *tmp != '/'; tmp--)
1900 return (CTD_CHECK(str));
1905 * Find a vdev that matches the search criteria specified. We use the
1906 * the nvpair name to determine how we should look for the device.
1907 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1908 * spare; but FALSE if its an INUSE spare.
1911 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1912 boolean_t *l2cache, boolean_t *log)
1919 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1921 /* Nothing to look for */
1922 if (search == NULL || pair == NULL)
1925 /* Obtain the key we will use to search */
1926 srchkey = nvpair_name(pair);
1928 switch (nvpair_type(pair)) {
1929 case DATA_TYPE_UINT64:
1930 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1931 uint64_t srchval, theguid;
1933 verify(nvpair_value_uint64(pair, &srchval) == 0);
1934 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1936 if (theguid == srchval)
1941 case DATA_TYPE_STRING: {
1942 char *srchval, *val;
1944 verify(nvpair_value_string(pair, &srchval) == 0);
1945 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1949 * Search for the requested value. Special cases:
1951 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
1952 * "s0" or "s0/old". The "s0" part is hidden from the user,
1953 * but included in the string, so this matches around it.
1954 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1956 * Otherwise, all other searches are simple string compares.
1959 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
1960 ctd_check_path(val)) {
1961 uint64_t wholedisk = 0;
1963 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1966 int slen = strlen(srchval);
1967 int vlen = strlen(val);
1969 if (slen != vlen - 2)
1973 * make_leaf_vdev() should only set
1974 * wholedisk for ZPOOL_CONFIG_PATHs which
1975 * will include "/dev/dsk/", giving plenty of
1976 * room for the indices used next.
1981 * strings identical except trailing "s0"
1983 if (strcmp(&val[vlen - 2], "s0") == 0 &&
1984 strncmp(srchval, val, slen) == 0)
1988 * strings identical except trailing "s0/old"
1990 if (strcmp(&val[vlen - 6], "s0/old") == 0 &&
1991 strcmp(&srchval[slen - 4], "/old") == 0 &&
1992 strncmp(srchval, val, slen - 4) == 0)
1997 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
1999 if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2001 char *type, *idx, *end, *p;
2002 uint64_t id, vdev_id;
2005 * Determine our vdev type, keeping in mind
2006 * that the srchval is composed of a type and
2007 * vdev id pair (i.e. mirror-4).
2009 if ((type = strdup(srchval)) == NULL)
2012 if ((p = strrchr(type, '-')) == NULL) {
2020 * If the types don't match then keep looking.
2022 if (strncmp(val, type, strlen(val)) != 0) {
2027 verify(strncmp(type, VDEV_TYPE_RAIDZ,
2028 strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2029 strncmp(type, VDEV_TYPE_MIRROR,
2030 strlen(VDEV_TYPE_MIRROR)) == 0);
2031 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2035 vdev_id = strtoull(idx, &end, 10);
2042 * Now verify that we have the correct vdev id.
2051 if (strcmp(srchval, val) == 0)
2060 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2061 &child, &children) != 0)
2064 for (c = 0; c < children; c++) {
2065 if ((ret = vdev_to_nvlist_iter(child[c], search,
2066 avail_spare, l2cache, NULL)) != NULL) {
2068 * The 'is_log' value is only set for the toplevel
2069 * vdev, not the leaf vdevs. So we always lookup the
2070 * log device from the root of the vdev tree (where
2071 * 'log' is non-NULL).
2074 nvlist_lookup_uint64(child[c],
2075 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2083 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2084 &child, &children) == 0) {
2085 for (c = 0; c < children; c++) {
2086 if ((ret = vdev_to_nvlist_iter(child[c], search,
2087 avail_spare, l2cache, NULL)) != NULL) {
2088 *avail_spare = B_TRUE;
2094 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2095 &child, &children) == 0) {
2096 for (c = 0; c < children; c++) {
2097 if ((ret = vdev_to_nvlist_iter(child[c], search,
2098 avail_spare, l2cache, NULL)) != NULL) {
2109 * Given a physical path (minus the "/devices" prefix), find the
2113 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2114 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2116 nvlist_t *search, *nvroot, *ret;
2118 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2119 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2121 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2124 *avail_spare = B_FALSE;
2128 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2129 nvlist_free(search);
2135 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2138 zpool_vdev_is_interior(const char *name)
2140 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2141 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2147 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2148 boolean_t *l2cache, boolean_t *log)
2150 char buf[MAXPATHLEN];
2152 nvlist_t *nvroot, *search, *ret;
2155 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2157 guid = strtoull(path, &end, 10);
2158 if (guid != 0 && *end == '\0') {
2159 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2160 } else if (zpool_vdev_is_interior(path)) {
2161 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2162 } else if (path[0] != '/') {
2163 (void) snprintf(buf, sizeof (buf), "%s%s", _PATH_DEV, path);
2164 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
2166 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2169 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2172 *avail_spare = B_FALSE;
2176 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2177 nvlist_free(search);
2183 vdev_online(nvlist_t *nv)
2187 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2188 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2189 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2196 * Helper function for zpool_get_physpaths().
2199 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2200 size_t *bytes_written)
2202 size_t bytes_left, pos, rsz;
2206 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2208 return (EZFS_NODEVICE);
2210 pos = *bytes_written;
2211 bytes_left = physpath_size - pos;
2212 format = (pos == 0) ? "%s" : " %s";
2214 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2215 *bytes_written += rsz;
2217 if (rsz >= bytes_left) {
2218 /* if physpath was not copied properly, clear it */
2219 if (bytes_left != 0) {
2222 return (EZFS_NOSPC);
2228 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2229 size_t *rsz, boolean_t is_spare)
2234 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2235 return (EZFS_INVALCONFIG);
2237 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2239 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2240 * For a spare vdev, we only want to boot from the active
2245 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2248 return (EZFS_INVALCONFIG);
2251 if (vdev_online(nv)) {
2252 if ((ret = vdev_get_one_physpath(nv, physpath,
2253 phypath_size, rsz)) != 0)
2256 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2257 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2258 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2263 if (nvlist_lookup_nvlist_array(nv,
2264 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2265 return (EZFS_INVALCONFIG);
2267 for (i = 0; i < count; i++) {
2268 ret = vdev_get_physpaths(child[i], physpath,
2269 phypath_size, rsz, is_spare);
2270 if (ret == EZFS_NOSPC)
2275 return (EZFS_POOL_INVALARG);
2279 * Get phys_path for a root pool config.
2280 * Return 0 on success; non-zero on failure.
2283 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2286 nvlist_t *vdev_root;
2293 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2295 return (EZFS_INVALCONFIG);
2297 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2298 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2299 &child, &count) != 0)
2300 return (EZFS_INVALCONFIG);
2303 * root pool can only have a single top-level vdev.
2305 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2306 return (EZFS_POOL_INVALARG);
2308 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2311 /* No online devices */
2313 return (EZFS_NODEVICE);
2319 * Get phys_path for a root pool
2320 * Return 0 on success; non-zero on failure.
2323 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2325 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2330 * If the device has being dynamically expanded then we need to relabel
2331 * the disk to use the new unallocated space.
2334 zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2337 char path[MAXPATHLEN];
2340 int (*_efi_use_whole_disk)(int);
2342 if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2343 "efi_use_whole_disk")) == NULL)
2346 (void) snprintf(path, sizeof (path), "%s/%s", ZFS_RDISK_ROOT, name);
2348 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2349 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2350 "relabel '%s': unable to open device"), name);
2351 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2355 * It's possible that we might encounter an error if the device
2356 * does not have any unallocated space left. If so, we simply
2357 * ignore that error and continue on.
2359 error = _efi_use_whole_disk(fd);
2361 if (error && error != VT_ENOSPC) {
2362 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2363 "relabel '%s': unable to read disk capacity"), name);
2364 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2366 #endif /* illumos */
2371 * Bring the specified vdev online. The 'flags' parameter is a set of the
2372 * ZFS_ONLINE_* flags.
2375 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2376 vdev_state_t *newstate)
2378 zfs_cmd_t zc = { 0 };
2381 boolean_t avail_spare, l2cache, islog;
2382 libzfs_handle_t *hdl = zhp->zpool_hdl;
2384 if (flags & ZFS_ONLINE_EXPAND) {
2385 (void) snprintf(msg, sizeof (msg),
2386 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2388 (void) snprintf(msg, sizeof (msg),
2389 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2392 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2393 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2395 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2397 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2400 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2402 if (flags & ZFS_ONLINE_EXPAND ||
2403 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2404 char *pathname = NULL;
2405 uint64_t wholedisk = 0;
2407 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2409 verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
2413 * XXX - L2ARC 1.0 devices can't support expansion.
2416 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2417 "cannot expand cache devices"));
2418 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2422 pathname += strlen(ZFS_DISK_ROOT) + 1;
2423 (void) zpool_relabel_disk(hdl, pathname);
2427 zc.zc_cookie = VDEV_STATE_ONLINE;
2430 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2431 if (errno == EINVAL) {
2432 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2433 "from this pool into a new one. Use '%s' "
2434 "instead"), "zpool detach");
2435 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2437 return (zpool_standard_error(hdl, errno, msg));
2440 *newstate = zc.zc_cookie;
2445 * Take the specified vdev offline
2448 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2450 zfs_cmd_t zc = { 0 };
2453 boolean_t avail_spare, l2cache;
2454 libzfs_handle_t *hdl = zhp->zpool_hdl;
2456 (void) snprintf(msg, sizeof (msg),
2457 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2459 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2460 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2462 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2464 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2467 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2469 zc.zc_cookie = VDEV_STATE_OFFLINE;
2470 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2472 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2479 * There are no other replicas of this device.
2481 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2485 * The log device has unplayed logs
2487 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2490 return (zpool_standard_error(hdl, errno, msg));
2495 * Mark the given vdev faulted.
2498 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2500 zfs_cmd_t zc = { 0 };
2502 libzfs_handle_t *hdl = zhp->zpool_hdl;
2504 (void) snprintf(msg, sizeof (msg),
2505 dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2507 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2509 zc.zc_cookie = VDEV_STATE_FAULTED;
2512 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2519 * There are no other replicas of this device.
2521 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2524 return (zpool_standard_error(hdl, errno, msg));
2530 * Mark the given vdev degraded.
2533 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2535 zfs_cmd_t zc = { 0 };
2537 libzfs_handle_t *hdl = zhp->zpool_hdl;
2539 (void) snprintf(msg, sizeof (msg),
2540 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
2542 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2544 zc.zc_cookie = VDEV_STATE_DEGRADED;
2547 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2550 return (zpool_standard_error(hdl, errno, msg));
2554 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2558 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2564 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2566 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2569 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2570 children == 2 && child[which] == tgt)
2573 for (c = 0; c < children; c++)
2574 if (is_replacing_spare(child[c], tgt, which))
2582 * Attach new_disk (fully described by nvroot) to old_disk.
2583 * If 'replacing' is specified, the new disk will replace the old one.
2586 zpool_vdev_attach(zpool_handle_t *zhp,
2587 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2589 zfs_cmd_t zc = { 0 };
2593 boolean_t avail_spare, l2cache, islog;
2598 nvlist_t *config_root;
2599 libzfs_handle_t *hdl = zhp->zpool_hdl;
2600 boolean_t rootpool = zpool_is_bootable(zhp);
2603 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2604 "cannot replace %s with %s"), old_disk, new_disk);
2606 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2607 "cannot attach %s to %s"), new_disk, old_disk);
2609 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2610 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2612 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2615 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2618 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2620 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2621 zc.zc_cookie = replacing;
2623 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2624 &child, &children) != 0 || children != 1) {
2625 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2626 "new device must be a single disk"));
2627 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2630 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2631 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2633 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2637 * If the target is a hot spare that has been swapped in, we can only
2638 * replace it with another hot spare.
2641 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2642 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2643 NULL) == NULL || !avail_spare) &&
2644 is_replacing_spare(config_root, tgt, 1)) {
2645 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2646 "can only be replaced by another hot spare"));
2648 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2653 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2656 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2658 zcmd_free_nvlists(&zc);
2663 * XXX need a better way to prevent user from
2664 * booting up a half-baked vdev.
2666 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2667 "sure to wait until resilver is done "
2668 "before rebooting.\n"));
2669 (void) fprintf(stderr, "\n");
2670 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "If "
2671 "you boot from pool '%s', you may need to update\n"
2672 "boot code on newly attached disk '%s'.\n\n"
2673 "Assuming you use GPT partitioning and 'da0' is "
2674 "your new boot disk\n"
2675 "you may use the following command:\n\n"
2676 "\tgpart bootcode -b /boot/pmbr -p "
2677 "/boot/gptzfsboot -i 1 da0\n\n"),
2678 zhp->zpool_name, new_disk);
2686 * Can't attach to or replace this type of vdev.
2689 uint64_t version = zpool_get_prop_int(zhp,
2690 ZPOOL_PROP_VERSION, NULL);
2693 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2694 "cannot replace a log with a spare"));
2695 else if (version >= SPA_VERSION_MULTI_REPLACE)
2696 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2697 "already in replacing/spare config; wait "
2698 "for completion or use 'zpool detach'"));
2700 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2701 "cannot replace a replacing device"));
2703 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2704 "can only attach to mirrors and top-level "
2707 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2712 * The new device must be a single disk.
2714 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2715 "new device must be a single disk"));
2716 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2720 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2722 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2727 * The new device is too small.
2729 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2730 "device is too small"));
2731 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2736 * The new device has a different alignment requirement.
2738 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2739 "devices have different sector alignment"));
2740 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2745 * The resulting top-level vdev spec won't fit in the label.
2747 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2751 (void) zpool_standard_error(hdl, errno, msg);
2758 * Detach the specified device.
2761 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2763 zfs_cmd_t zc = { 0 };
2766 boolean_t avail_spare, l2cache;
2767 libzfs_handle_t *hdl = zhp->zpool_hdl;
2769 (void) snprintf(msg, sizeof (msg),
2770 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2772 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2773 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2775 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2778 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2781 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2783 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2785 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2792 * Can't detach from this type of vdev.
2794 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2795 "applicable to mirror and replacing vdevs"));
2796 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2801 * There are no other replicas of this device.
2803 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2807 (void) zpool_standard_error(hdl, errno, msg);
2814 * Find a mirror vdev in the source nvlist.
2816 * The mchild array contains a list of disks in one of the top-level mirrors
2817 * of the source pool. The schild array contains a list of disks that the
2818 * user specified on the command line. We loop over the mchild array to
2819 * see if any entry in the schild array matches.
2821 * If a disk in the mchild array is found in the schild array, we return
2822 * the index of that entry. Otherwise we return -1.
2825 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2826 nvlist_t **schild, uint_t schildren)
2830 for (mc = 0; mc < mchildren; mc++) {
2832 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2833 mchild[mc], B_FALSE);
2835 for (sc = 0; sc < schildren; sc++) {
2836 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2837 schild[sc], B_FALSE);
2838 boolean_t result = (strcmp(mpath, spath) == 0);
2854 * Split a mirror pool. If newroot points to null, then a new nvlist
2855 * is generated and it is the responsibility of the caller to free it.
2858 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2859 nvlist_t *props, splitflags_t flags)
2861 zfs_cmd_t zc = { 0 };
2863 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2864 nvlist_t **varray = NULL, *zc_props = NULL;
2865 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2866 libzfs_handle_t *hdl = zhp->zpool_hdl;
2868 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2871 (void) snprintf(msg, sizeof (msg),
2872 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2874 if (!zpool_name_valid(hdl, B_FALSE, newname))
2875 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2877 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2878 (void) fprintf(stderr, gettext("Internal error: unable to "
2879 "retrieve pool configuration\n"));
2883 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2885 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2888 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2889 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2890 props, vers, flags, msg)) == NULL)
2894 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2896 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2897 "Source pool is missing vdev tree"));
2898 nvlist_free(zc_props);
2902 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2905 if (*newroot == NULL ||
2906 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2907 &newchild, &newchildren) != 0)
2910 for (c = 0; c < children; c++) {
2911 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2913 nvlist_t **mchild, *vdev;
2918 * Unlike cache & spares, slogs are stored in the
2919 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2921 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2923 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2925 if (is_log || is_hole) {
2927 * Create a hole vdev and put it in the config.
2929 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2931 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2932 VDEV_TYPE_HOLE) != 0)
2934 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2939 varray[vcount++] = vdev;
2943 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
2945 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2946 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2947 "Source pool must be composed only of mirrors\n"));
2948 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2952 verify(nvlist_lookup_nvlist_array(child[c],
2953 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2955 /* find or add an entry for this top-level vdev */
2956 if (newchildren > 0 &&
2957 (entry = find_vdev_entry(zhp, mchild, mchildren,
2958 newchild, newchildren)) >= 0) {
2959 /* We found a disk that the user specified. */
2960 vdev = mchild[entry];
2963 /* User didn't specify a disk for this vdev. */
2964 vdev = mchild[mchildren - 1];
2967 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
2971 /* did we find every disk the user specified? */
2972 if (found != newchildren) {
2973 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
2974 "include at most one disk from each mirror"));
2975 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2979 /* Prepare the nvlist for populating. */
2980 if (*newroot == NULL) {
2981 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
2984 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
2985 VDEV_TYPE_ROOT) != 0)
2988 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
2991 /* Add all the children we found */
2992 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
2993 lastlog == 0 ? vcount : lastlog) != 0)
2997 * If we're just doing a dry run, exit now with success.
3000 memory_err = B_FALSE;
3005 /* now build up the config list & call the ioctl */
3006 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3009 if (nvlist_add_nvlist(newconfig,
3010 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3011 nvlist_add_string(newconfig,
3012 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3013 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3017 * The new pool is automatically part of the namespace unless we
3018 * explicitly export it.
3021 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3022 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3023 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3024 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3026 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3029 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3030 retval = zpool_standard_error(hdl, errno, msg);
3035 memory_err = B_FALSE;
3038 if (varray != NULL) {
3041 for (v = 0; v < vcount; v++)
3042 nvlist_free(varray[v]);
3045 zcmd_free_nvlists(&zc);
3046 nvlist_free(zc_props);
3047 nvlist_free(newconfig);
3049 nvlist_free(*newroot);
3057 return (no_memory(hdl));
3063 * Remove the given device. Currently, this is supported only for hot spares
3064 * and level 2 cache devices.
3067 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3069 zfs_cmd_t zc = { 0 };
3072 boolean_t avail_spare, l2cache, islog;
3073 libzfs_handle_t *hdl = zhp->zpool_hdl;
3076 (void) snprintf(msg, sizeof (msg),
3077 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3079 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3080 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3082 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3084 * XXX - this should just go away.
3086 if (!avail_spare && !l2cache && !islog) {
3087 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3088 "only inactive hot spares, cache, top-level, "
3089 "or log devices can be removed"));
3090 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3093 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3094 if (islog && version < SPA_VERSION_HOLES) {
3095 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3096 "pool must be upgrade to support log removal"));
3097 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3100 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3102 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3105 return (zpool_standard_error(hdl, errno, msg));
3109 * Clear the errors for the pool, or the particular device if specified.
3112 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3114 zfs_cmd_t zc = { 0 };
3117 zpool_rewind_policy_t policy;
3118 boolean_t avail_spare, l2cache;
3119 libzfs_handle_t *hdl = zhp->zpool_hdl;
3120 nvlist_t *nvi = NULL;
3124 (void) snprintf(msg, sizeof (msg),
3125 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3128 (void) snprintf(msg, sizeof (msg),
3129 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3132 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3134 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3135 &l2cache, NULL)) == 0)
3136 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3139 * Don't allow error clearing for hot spares. Do allow
3140 * error clearing for l2cache devices.
3143 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3145 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3149 zpool_get_rewind_policy(rewindnvl, &policy);
3150 zc.zc_cookie = policy.zrp_request;
3152 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3155 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3158 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3160 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3161 zcmd_free_nvlists(&zc);
3166 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
3167 errno != EPERM && errno != EACCES)) {
3168 if (policy.zrp_request &
3169 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3170 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3171 zpool_rewind_exclaim(hdl, zc.zc_name,
3172 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
3176 zcmd_free_nvlists(&zc);
3180 zcmd_free_nvlists(&zc);
3181 return (zpool_standard_error(hdl, errno, msg));
3185 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3188 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3190 zfs_cmd_t zc = { 0 };
3192 libzfs_handle_t *hdl = zhp->zpool_hdl;
3194 (void) snprintf(msg, sizeof (msg),
3195 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3198 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3200 zc.zc_cookie = ZPOOL_NO_REWIND;
3202 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3205 return (zpool_standard_error(hdl, errno, msg));
3209 * Change the GUID for a pool.
3212 zpool_reguid(zpool_handle_t *zhp)
3215 libzfs_handle_t *hdl = zhp->zpool_hdl;
3216 zfs_cmd_t zc = { 0 };
3218 (void) snprintf(msg, sizeof (msg),
3219 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3221 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3222 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3225 return (zpool_standard_error(hdl, errno, msg));
3232 zpool_reopen(zpool_handle_t *zhp)
3234 zfs_cmd_t zc = { 0 };
3236 libzfs_handle_t *hdl = zhp->zpool_hdl;
3238 (void) snprintf(msg, sizeof (msg),
3239 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
3242 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3243 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
3245 return (zpool_standard_error(hdl, errno, msg));
3249 * Convert from a devid string to a path.
3252 devid_to_path(char *devid_str)
3257 devid_nmlist_t *list = NULL;
3260 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3263 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3265 devid_str_free(minor);
3272 * In a case the strdup() fails, we will just return NULL below.
3274 path = strdup(list[0].devname);
3276 devid_free_nmlist(list);
3282 * Convert from a path to a devid string.
3285 path_to_devid(const char *path)
3292 if ((fd = open(path, O_RDONLY)) < 0)
3297 if (devid_get(fd, &devid) == 0) {
3298 if (devid_get_minor_name(fd, &minor) == 0)
3299 ret = devid_str_encode(devid, minor);
3301 devid_str_free(minor);
3313 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3314 * ignore any failure here, since a common case is for an unprivileged user to
3315 * type 'zpool status', and we'll display the correct information anyway.
3318 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3320 zfs_cmd_t zc = { 0 };
3322 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3323 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3324 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3327 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3331 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3332 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3333 * We also check if this is a whole disk, in which case we strip off the
3334 * trailing 's0' slice name.
3336 * This routine is also responsible for identifying when disks have been
3337 * reconfigured in a new location. The kernel will have opened the device by
3338 * devid, but the path will still refer to the old location. To catch this, we
3339 * first do a path -> devid translation (which is fast for the common case). If
3340 * the devid matches, we're done. If not, we do a reverse devid -> path
3341 * translation and issue the appropriate ioctl() to update the path of the vdev.
3342 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3346 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3357 have_stats = nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3358 (uint64_t **)&vs, &vsc) == 0;
3359 have_path = nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0;
3362 * If the device is not currently present, assume it will not
3363 * come back at the same device path. Display the device by GUID.
3365 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3366 have_path && have_stats && vs->vs_state <= VDEV_STATE_CANT_OPEN) {
3367 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3369 (void) snprintf(buf, sizeof (buf), "%llu",
3370 (u_longlong_t)value);
3372 } else if (have_path) {
3375 * If the device is dead (faulted, offline, etc) then don't
3376 * bother opening it. Otherwise we may be forcing the user to
3377 * open a misbehaving device, which can have undesirable
3380 if ((have_stats == 0 ||
3381 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3383 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3385 * Determine if the current path is correct.
3387 char *newdevid = path_to_devid(path);
3389 if (newdevid == NULL ||
3390 strcmp(devid, newdevid) != 0) {
3393 if ((newpath = devid_to_path(devid)) != NULL) {
3395 * Update the path appropriately.
3397 set_path(zhp, nv, newpath);
3398 if (nvlist_add_string(nv,
3399 ZPOOL_CONFIG_PATH, newpath) == 0)
3400 verify(nvlist_lookup_string(nv,
3408 devid_str_free(newdevid);
3412 if (strncmp(path, ZFS_DISK_ROOTD, strlen(ZFS_DISK_ROOTD)) == 0)
3413 path += strlen(ZFS_DISK_ROOTD);
3415 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3416 &value) == 0 && value) {
3417 int pathlen = strlen(path);
3418 char *tmp = zfs_strdup(hdl, path);
3421 * If it starts with c#, and ends with "s0", chop
3422 * the "s0" off, or if it ends with "s0/old", remove
3423 * the "s0" from the middle.
3425 if (CTD_CHECK(tmp)) {
3426 if (strcmp(&tmp[pathlen - 2], "s0") == 0) {
3427 tmp[pathlen - 2] = '\0';
3428 } else if (pathlen > 6 &&
3429 strcmp(&tmp[pathlen - 6], "s0/old") == 0) {
3430 (void) strcpy(&tmp[pathlen - 6],
3436 #else /* !illumos */
3437 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3438 path += sizeof(_PATH_DEV) - 1;
3439 #endif /* illumos */
3441 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3444 * If it's a raidz device, we need to stick in the parity level.
3446 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3447 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3449 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3450 (u_longlong_t)value);
3455 * We identify each top-level vdev by using a <type-id>
3456 * naming convention.
3461 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3463 (void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3469 return (zfs_strdup(hdl, path));
3473 zbookmark_mem_compare(const void *a, const void *b)
3475 return (memcmp(a, b, sizeof (zbookmark_phys_t)));
3479 * Retrieve the persistent error log, uniquify the members, and return to the
3483 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3485 zfs_cmd_t zc = { 0 };
3487 zbookmark_phys_t *zb = NULL;
3491 * Retrieve the raw error list from the kernel. If the number of errors
3492 * has increased, allocate more space and continue until we get the
3495 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3499 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3500 count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL)
3502 zc.zc_nvlist_dst_size = count;
3503 (void) strcpy(zc.zc_name, zhp->zpool_name);
3505 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3507 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3508 if (errno == ENOMEM) {
3511 count = zc.zc_nvlist_dst_size;
3512 dst = zfs_alloc(zhp->zpool_hdl, count *
3513 sizeof (zbookmark_phys_t));
3516 zc.zc_nvlist_dst = (uintptr_t)dst;
3526 * Sort the resulting bookmarks. This is a little confusing due to the
3527 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3528 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3529 * _not_ copied as part of the process. So we point the start of our
3530 * array appropriate and decrement the total number of elements.
3532 zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
3533 zc.zc_nvlist_dst_size;
3534 count -= zc.zc_nvlist_dst_size;
3536 qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
3538 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3541 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3543 for (i = 0; i < count; i++) {
3546 /* ignoring zb_blkid and zb_level for now */
3547 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3548 zb[i-1].zb_object == zb[i].zb_object)
3551 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3553 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3554 zb[i].zb_objset) != 0) {
3558 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3559 zb[i].zb_object) != 0) {
3563 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3570 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3574 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3575 return (no_memory(zhp->zpool_hdl));
3579 * Upgrade a ZFS pool to the latest on-disk version.
3582 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3584 zfs_cmd_t zc = { 0 };
3585 libzfs_handle_t *hdl = zhp->zpool_hdl;
3587 (void) strcpy(zc.zc_name, zhp->zpool_name);
3588 zc.zc_cookie = new_version;
3590 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3591 return (zpool_standard_error_fmt(hdl, errno,
3592 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3598 zfs_save_arguments(int argc, char **argv, char *string, int len)
3600 (void) strlcpy(string, basename(argv[0]), len);
3601 for (int i = 1; i < argc; i++) {
3602 (void) strlcat(string, " ", len);
3603 (void) strlcat(string, argv[i], len);
3608 zpool_log_history(libzfs_handle_t *hdl, const char *message)
3610 zfs_cmd_t zc = { 0 };
3614 args = fnvlist_alloc();
3615 fnvlist_add_string(args, "message", message);
3616 err = zcmd_write_src_nvlist(hdl, &zc, args);
3618 err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
3620 zcmd_free_nvlists(&zc);
3625 * Perform ioctl to get some command history of a pool.
3627 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3628 * logical offset of the history buffer to start reading from.
3630 * Upon return, 'off' is the next logical offset to read from and
3631 * 'len' is the actual amount of bytes read into 'buf'.
3634 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3636 zfs_cmd_t zc = { 0 };
3637 libzfs_handle_t *hdl = zhp->zpool_hdl;
3639 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3641 zc.zc_history = (uint64_t)(uintptr_t)buf;
3642 zc.zc_history_len = *len;
3643 zc.zc_history_offset = *off;
3645 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3648 return (zfs_error_fmt(hdl, EZFS_PERM,
3649 dgettext(TEXT_DOMAIN,
3650 "cannot show history for pool '%s'"),
3653 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3654 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3655 "'%s'"), zhp->zpool_name));
3657 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3658 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3659 "'%s', pool must be upgraded"), zhp->zpool_name));
3661 return (zpool_standard_error_fmt(hdl, errno,
3662 dgettext(TEXT_DOMAIN,
3663 "cannot get history for '%s'"), zhp->zpool_name));
3667 *len = zc.zc_history_len;
3668 *off = zc.zc_history_offset;
3674 * Process the buffer of nvlists, unpacking and storing each nvlist record
3675 * into 'records'. 'leftover' is set to the number of bytes that weren't
3676 * processed as there wasn't a complete record.
3679 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3680 nvlist_t ***records, uint_t *numrecords)
3686 while (bytes_read > sizeof (reclen)) {
3688 /* get length of packed record (stored as little endian) */
3689 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3690 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3692 if (bytes_read < sizeof (reclen) + reclen)
3696 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3698 bytes_read -= sizeof (reclen) + reclen;
3699 buf += sizeof (reclen) + reclen;
3701 /* add record to nvlist array */
3703 if (ISP2(*numrecords + 1)) {
3704 *records = realloc(*records,
3705 *numrecords * 2 * sizeof (nvlist_t *));
3707 (*records)[*numrecords - 1] = nv;
3710 *leftover = bytes_read;
3714 /* from spa_history.c: spa_history_create_obj() */
3715 #define HIS_BUF_LEN_DEF (128 << 10)
3716 #define HIS_BUF_LEN_MAX (1 << 30)
3719 * Retrieve the command history of a pool.
3722 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3725 uint64_t buflen = HIS_BUF_LEN_DEF;
3727 nvlist_t **records = NULL;
3728 uint_t numrecords = 0;
3731 buf = malloc(buflen);
3735 uint64_t bytes_read = buflen;
3738 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3741 /* if nothing else was read in, we're at EOF, just return */
3742 if (bytes_read == 0)
3745 if ((err = zpool_history_unpack(buf, bytes_read,
3746 &leftover, &records, &numrecords)) != 0)
3749 if (leftover == bytes_read) {
3751 * no progress made, because buffer is not big enough
3752 * to hold this record; resize and retry.
3757 if ((buflen >= HIS_BUF_LEN_MAX) ||
3758 ((buf = malloc(buflen)) == NULL)) {
3770 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3771 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3772 records, numrecords) == 0);
3774 for (i = 0; i < numrecords; i++)
3775 nvlist_free(records[i]);
3782 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3783 char *pathname, size_t len)
3785 zfs_cmd_t zc = { 0 };
3786 boolean_t mounted = B_FALSE;
3787 char *mntpnt = NULL;
3788 char dsname[MAXNAMELEN];
3791 /* special case for the MOS */
3792 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
3796 /* get the dataset's name */
3797 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3799 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3800 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3801 /* just write out a path of two object numbers */
3802 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3806 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3808 /* find out if the dataset is mounted */
3809 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3811 /* get the corrupted object's path */
3812 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3814 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3817 (void) snprintf(pathname, len, "%s%s", mntpnt,
3820 (void) snprintf(pathname, len, "%s:%s",
3821 dsname, zc.zc_value);
3824 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
3831 * Read the EFI label from the config, if a label does not exist then
3832 * pass back the error to the caller. If the caller has passed a non-NULL
3833 * diskaddr argument then we set it to the starting address of the EFI
3837 read_efi_label(nvlist_t *config, diskaddr_t *sb)
3841 char diskname[MAXPATHLEN];
3844 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3847 (void) snprintf(diskname, sizeof (diskname), "%s%s", ZFS_RDISK_ROOT,
3848 strrchr(path, '/'));
3849 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
3850 struct dk_gpt *vtoc;
3852 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3854 *sb = vtoc->efi_parts[0].p_start;
3863 * determine where a partition starts on a disk in the current
3867 find_start_block(nvlist_t *config)
3871 diskaddr_t sb = MAXOFFSET_T;
3874 if (nvlist_lookup_nvlist_array(config,
3875 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3876 if (nvlist_lookup_uint64(config,
3877 ZPOOL_CONFIG_WHOLE_DISK,
3878 &wholedisk) != 0 || !wholedisk) {
3879 return (MAXOFFSET_T);
3881 if (read_efi_label(config, &sb) < 0)
3886 for (c = 0; c < children; c++) {
3887 sb = find_start_block(child[c]);
3888 if (sb != MAXOFFSET_T) {
3892 return (MAXOFFSET_T);
3894 #endif /* illumos */
3897 * Label an individual disk. The name provided is the short name,
3898 * stripped of any leading /dev path.
3901 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name)
3904 char path[MAXPATHLEN];
3905 struct dk_gpt *vtoc;
3907 size_t resv = EFI_MIN_RESV_SIZE;
3908 uint64_t slice_size;
3909 diskaddr_t start_block;
3912 /* prepare an error message just in case */
3913 (void) snprintf(errbuf, sizeof (errbuf),
3914 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3919 verify(nvlist_lookup_nvlist(zhp->zpool_config,
3920 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3922 if (zhp->zpool_start_block == 0)
3923 start_block = find_start_block(nvroot);
3925 start_block = zhp->zpool_start_block;
3926 zhp->zpool_start_block = start_block;
3929 start_block = NEW_START_BLOCK;
3932 (void) snprintf(path, sizeof (path), "%s/%s%s", ZFS_RDISK_ROOT, name,
3935 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
3937 * This shouldn't happen. We've long since verified that this
3938 * is a valid device.
3941 dgettext(TEXT_DOMAIN, "unable to open device"));
3942 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
3945 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
3947 * The only way this can fail is if we run out of memory, or we
3948 * were unable to read the disk's capacity
3950 if (errno == ENOMEM)
3951 (void) no_memory(hdl);
3954 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3955 "unable to read disk capacity"), name);
3957 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
3960 slice_size = vtoc->efi_last_u_lba + 1;
3961 slice_size -= EFI_MIN_RESV_SIZE;
3962 if (start_block == MAXOFFSET_T)
3963 start_block = NEW_START_BLOCK;
3964 slice_size -= start_block;
3966 vtoc->efi_parts[0].p_start = start_block;
3967 vtoc->efi_parts[0].p_size = slice_size;
3970 * Why we use V_USR: V_BACKUP confuses users, and is considered
3971 * disposable by some EFI utilities (since EFI doesn't have a backup
3972 * slice). V_UNASSIGNED is supposed to be used only for zero size
3973 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
3974 * etc. were all pretty specific. V_USR is as close to reality as we
3975 * can get, in the absence of V_OTHER.
3977 vtoc->efi_parts[0].p_tag = V_USR;
3978 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
3980 vtoc->efi_parts[8].p_start = slice_size + start_block;
3981 vtoc->efi_parts[8].p_size = resv;
3982 vtoc->efi_parts[8].p_tag = V_RESERVED;
3984 if (efi_write(fd, vtoc) != 0) {
3986 * Some block drivers (like pcata) may not support EFI
3987 * GPT labels. Print out a helpful error message dir-
3988 * ecting the user to manually label the disk and give
3994 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3995 "try using fdisk(1M) and then provide a specific slice"));
3996 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4001 #endif /* illumos */
4006 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
4012 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
4013 if (strcmp(type, VDEV_TYPE_FILE) == 0 ||
4014 strcmp(type, VDEV_TYPE_HOLE) == 0 ||
4015 strcmp(type, VDEV_TYPE_MISSING) == 0) {
4016 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4017 "vdev type '%s' is not supported"), type);
4018 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
4021 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
4022 &child, &children) == 0) {
4023 for (c = 0; c < children; c++) {
4024 if (!supported_dump_vdev_type(hdl, child[c], errbuf))
4032 * Check if this zvol is allowable for use as a dump device; zero if
4033 * it is, > 0 if it isn't, < 0 if it isn't a zvol.
4035 * Allowable storage configurations include mirrors, all raidz variants, and
4036 * pools with log, cache, and spare devices. Pools which are backed by files or
4037 * have missing/hole vdevs are not suitable.
4040 zvol_check_dump_config(char *arg)
4042 zpool_handle_t *zhp = NULL;
4043 nvlist_t *config, *nvroot;
4047 libzfs_handle_t *hdl;
4049 char poolname[ZPOOL_MAXNAMELEN];
4050 int pathlen = strlen(ZVOL_FULL_DEV_DIR);
4053 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
4057 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4058 "dump is not supported on device '%s'"), arg);
4060 if ((hdl = libzfs_init()) == NULL)
4062 libzfs_print_on_error(hdl, B_TRUE);
4064 volname = arg + pathlen;
4066 /* check the configuration of the pool */
4067 if ((p = strchr(volname, '/')) == NULL) {
4068 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4069 "malformed dataset name"));
4070 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4072 } else if (p - volname >= ZFS_MAXNAMELEN) {
4073 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4074 "dataset name is too long"));
4075 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
4078 (void) strncpy(poolname, volname, p - volname);
4079 poolname[p - volname] = '\0';
4082 if ((zhp = zpool_open(hdl, poolname)) == NULL) {
4083 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4084 "could not open pool '%s'"), poolname);
4085 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4088 config = zpool_get_config(zhp, NULL);
4089 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4091 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4092 "could not obtain vdev configuration for '%s'"), poolname);
4093 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4097 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4098 &top, &toplevels) == 0);
4100 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {