4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
26 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
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 DISK_ROOT "/dev/dsk"
53 #define RDISK_ROOT "/dev/rdsk"
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"));
206 return (gettext("UNKNOWN"));
210 * Map POOL STATE to printed strings.
213 zpool_pool_state_to_name(pool_state_t state)
216 case POOL_STATE_ACTIVE:
217 return (gettext("ACTIVE"));
218 case POOL_STATE_EXPORTED:
219 return (gettext("EXPORTED"));
220 case POOL_STATE_DESTROYED:
221 return (gettext("DESTROYED"));
222 case POOL_STATE_SPARE:
223 return (gettext("SPARE"));
224 case POOL_STATE_L2CACHE:
225 return (gettext("L2CACHE"));
226 case POOL_STATE_UNINITIALIZED:
227 return (gettext("UNINITIALIZED"));
228 case POOL_STATE_UNAVAIL:
229 return (gettext("UNAVAIL"));
230 case POOL_STATE_POTENTIALLY_ACTIVE:
231 return (gettext("POTENTIALLY_ACTIVE"));
234 return (gettext("UNKNOWN"));
238 * Get a zpool property value for 'prop' and return the value in
239 * a pre-allocated buffer.
242 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
243 zprop_source_t *srctype, boolean_t literal)
247 zprop_source_t src = ZPROP_SRC_NONE;
252 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
254 case ZPOOL_PROP_NAME:
255 (void) strlcpy(buf, zpool_get_name(zhp), len);
258 case ZPOOL_PROP_HEALTH:
260 zpool_pool_state_to_name(POOL_STATE_UNAVAIL), len);
263 case ZPOOL_PROP_GUID:
264 intval = zpool_get_prop_int(zhp, prop, &src);
265 (void) snprintf(buf, len, "%llu", intval);
268 case ZPOOL_PROP_ALTROOT:
269 case ZPOOL_PROP_CACHEFILE:
270 case ZPOOL_PROP_COMMENT:
271 if (zhp->zpool_props != NULL ||
272 zpool_get_all_props(zhp) == 0) {
274 zpool_get_prop_string(zhp, prop, &src),
280 (void) strlcpy(buf, "-", len);
289 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
290 prop != ZPOOL_PROP_NAME)
293 switch (zpool_prop_get_type(prop)) {
294 case PROP_TYPE_STRING:
295 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
299 case PROP_TYPE_NUMBER:
300 intval = zpool_get_prop_int(zhp, prop, &src);
303 case ZPOOL_PROP_SIZE:
304 case ZPOOL_PROP_ALLOCATED:
305 case ZPOOL_PROP_FREE:
306 case ZPOOL_PROP_FREEING:
307 case ZPOOL_PROP_LEAKED:
309 (void) snprintf(buf, len, "%llu",
310 (u_longlong_t)intval);
312 (void) zfs_nicenum(intval, buf, len);
315 case ZPOOL_PROP_EXPANDSZ:
317 (void) strlcpy(buf, "-", len);
318 } else if (literal) {
319 (void) snprintf(buf, len, "%llu",
320 (u_longlong_t)intval);
322 (void) zfs_nicenum(intval, buf, len);
325 case ZPOOL_PROP_CAPACITY:
327 (void) snprintf(buf, len, "%llu",
328 (u_longlong_t)intval);
330 (void) snprintf(buf, len, "%llu%%",
331 (u_longlong_t)intval);
334 case ZPOOL_PROP_FRAGMENTATION:
335 if (intval == UINT64_MAX) {
336 (void) strlcpy(buf, "-", len);
338 (void) snprintf(buf, len, "%llu%%",
339 (u_longlong_t)intval);
342 case ZPOOL_PROP_DEDUPRATIO:
343 (void) snprintf(buf, len, "%llu.%02llux",
344 (u_longlong_t)(intval / 100),
345 (u_longlong_t)(intval % 100));
347 case ZPOOL_PROP_HEALTH:
348 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
349 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
350 verify(nvlist_lookup_uint64_array(nvroot,
351 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
354 (void) strlcpy(buf, zpool_state_to_name(intval,
357 case ZPOOL_PROP_VERSION:
358 if (intval >= SPA_VERSION_FEATURES) {
359 (void) snprintf(buf, len, "-");
364 (void) snprintf(buf, len, "%llu", intval);
368 case PROP_TYPE_INDEX:
369 intval = zpool_get_prop_int(zhp, prop, &src);
370 if (zpool_prop_index_to_string(prop, intval, &strval)
373 (void) strlcpy(buf, strval, len);
387 * Check if the bootfs name has the same pool name as it is set to.
388 * Assuming bootfs is a valid dataset name.
391 bootfs_name_valid(const char *pool, char *bootfs)
393 int len = strlen(pool);
395 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
398 if (strncmp(pool, bootfs, len) == 0 &&
399 (bootfs[len] == '/' || bootfs[len] == '\0'))
406 * Inspect the configuration to determine if any of the devices contain
410 pool_uses_efi(nvlist_t *config)
416 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
417 &child, &children) != 0)
418 return (read_efi_label(config, NULL) >= 0);
420 for (c = 0; c < children; c++) {
421 if (pool_uses_efi(child[c]))
429 zpool_is_bootable(zpool_handle_t *zhp)
431 char bootfs[ZPOOL_MAXNAMELEN];
433 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
434 sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-",
435 sizeof (bootfs)) != 0);
440 * Given an nvlist of zpool properties to be set, validate that they are
441 * correct, and parse any numeric properties (index, boolean, etc) if they are
442 * specified as strings.
445 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
446 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
454 struct stat64 statbuf;
458 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
459 (void) no_memory(hdl);
464 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
465 const char *propname = nvpair_name(elem);
467 prop = zpool_name_to_prop(propname);
468 if (prop == ZPROP_INVAL && zpool_prop_feature(propname)) {
470 char *fname = strchr(propname, '@') + 1;
472 err = zfeature_lookup_name(fname, NULL);
474 ASSERT3U(err, ==, ENOENT);
475 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
476 "invalid feature '%s'"), fname);
477 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
481 if (nvpair_type(elem) != DATA_TYPE_STRING) {
482 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
483 "'%s' must be a string"), propname);
484 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
488 (void) nvpair_value_string(elem, &strval);
489 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) {
490 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
491 "property '%s' can only be set to "
492 "'enabled'"), propname);
493 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
497 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
498 (void) no_memory(hdl);
505 * Make sure this property is valid and applies to this type.
507 if (prop == ZPROP_INVAL) {
508 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
509 "invalid property '%s'"), propname);
510 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
514 if (zpool_prop_readonly(prop)) {
515 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
516 "is readonly"), propname);
517 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
521 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
522 &strval, &intval, errbuf) != 0)
526 * Perform additional checking for specific properties.
529 case ZPOOL_PROP_VERSION:
530 if (intval < version ||
531 !SPA_VERSION_IS_SUPPORTED(intval)) {
532 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
533 "property '%s' number %d is invalid."),
535 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
540 case ZPOOL_PROP_BOOTFS:
541 if (flags.create || flags.import) {
542 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
543 "property '%s' cannot be set at creation "
544 "or import time"), propname);
545 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
549 if (version < SPA_VERSION_BOOTFS) {
550 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
551 "pool must be upgraded to support "
552 "'%s' property"), propname);
553 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
558 * bootfs property value has to be a dataset name and
559 * the dataset has to be in the same pool as it sets to.
561 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
563 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
564 "is an invalid name"), strval);
565 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
569 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
570 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
571 "could not open pool '%s'"), poolname);
572 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
575 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
576 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
580 * bootfs property cannot be set on a disk which has
583 if (pool_uses_efi(nvroot)) {
584 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
585 "property '%s' not supported on "
586 "EFI labeled devices"), propname);
587 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
595 case ZPOOL_PROP_ALTROOT:
596 if (!flags.create && !flags.import) {
597 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
598 "property '%s' can only be set during pool "
599 "creation or import"), propname);
600 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
604 if (strval[0] != '/') {
605 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
606 "bad alternate root '%s'"), strval);
607 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
612 case ZPOOL_PROP_CACHEFILE:
613 if (strval[0] == '\0')
616 if (strcmp(strval, "none") == 0)
619 if (strval[0] != '/') {
620 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
621 "property '%s' must be empty, an "
622 "absolute path, or 'none'"), propname);
623 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
627 slash = strrchr(strval, '/');
629 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
630 strcmp(slash, "/..") == 0) {
631 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
632 "'%s' is not a valid file"), strval);
633 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
639 if (strval[0] != '\0' &&
640 (stat64(strval, &statbuf) != 0 ||
641 !S_ISDIR(statbuf.st_mode))) {
642 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
643 "'%s' is not a valid directory"),
645 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
652 case ZPOOL_PROP_COMMENT:
653 for (check = strval; *check != '\0'; check++) {
654 if (!isprint(*check)) {
656 dgettext(TEXT_DOMAIN,
657 "comment may only have printable "
659 (void) zfs_error(hdl, EZFS_BADPROP,
664 if (strlen(strval) > ZPROP_MAX_COMMENT) {
665 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
666 "comment must not exceed %d characters"),
668 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
672 case ZPOOL_PROP_READONLY:
674 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
675 "property '%s' can only be set at "
676 "import time"), propname);
677 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
686 nvlist_free(retprops);
691 * Set zpool property : propname=propval.
694 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
696 zfs_cmd_t zc = { 0 };
699 nvlist_t *nvl = NULL;
702 prop_flags_t flags = { 0 };
704 (void) snprintf(errbuf, sizeof (errbuf),
705 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
708 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
709 return (no_memory(zhp->zpool_hdl));
711 if (nvlist_add_string(nvl, propname, propval) != 0) {
713 return (no_memory(zhp->zpool_hdl));
716 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
717 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
718 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
727 * Execute the corresponding ioctl() to set this property.
729 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
731 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
736 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
738 zcmd_free_nvlists(&zc);
742 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
744 (void) zpool_props_refresh(zhp);
750 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
752 libzfs_handle_t *hdl = zhp->zpool_hdl;
754 char buf[ZFS_MAXPROPLEN];
755 nvlist_t *features = NULL;
757 boolean_t firstexpand = (NULL == *plp);
759 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
763 while (*last != NULL)
764 last = &(*last)->pl_next;
767 features = zpool_get_features(zhp);
769 if ((*plp)->pl_all && firstexpand) {
770 for (int i = 0; i < SPA_FEATURES; i++) {
771 zprop_list_t *entry = zfs_alloc(hdl,
772 sizeof (zprop_list_t));
773 entry->pl_prop = ZPROP_INVAL;
774 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
775 spa_feature_table[i].fi_uname);
776 entry->pl_width = strlen(entry->pl_user_prop);
777 entry->pl_all = B_TRUE;
780 last = &entry->pl_next;
784 /* add any unsupported features */
785 for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL);
786 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
791 if (zfeature_is_supported(nvpair_name(nvp)))
794 propname = zfs_asprintf(hdl, "unsupported@%s",
798 * Before adding the property to the list make sure that no
799 * other pool already added the same property.
803 while (entry != NULL) {
804 if (entry->pl_user_prop != NULL &&
805 strcmp(propname, entry->pl_user_prop) == 0) {
809 entry = entry->pl_next;
816 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
817 entry->pl_prop = ZPROP_INVAL;
818 entry->pl_user_prop = propname;
819 entry->pl_width = strlen(entry->pl_user_prop);
820 entry->pl_all = B_TRUE;
823 last = &entry->pl_next;
826 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
831 if (entry->pl_prop != ZPROP_INVAL &&
832 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
833 NULL, B_FALSE) == 0) {
834 if (strlen(buf) > entry->pl_width)
835 entry->pl_width = strlen(buf);
843 * Get the state for the given feature on the given ZFS pool.
846 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
850 boolean_t found = B_FALSE;
851 nvlist_t *features = zpool_get_features(zhp);
853 const char *feature = strchr(propname, '@') + 1;
855 supported = zpool_prop_feature(propname);
856 ASSERT(supported || zpool_prop_unsupported(propname));
859 * Convert from feature name to feature guid. This conversion is
860 * unecessary for unsupported@... properties because they already
867 ret = zfeature_lookup_name(feature, &fid);
869 (void) strlcpy(buf, "-", len);
872 feature = spa_feature_table[fid].fi_guid;
875 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
880 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
883 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
885 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
890 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
892 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
895 (void) strlcpy(buf, "-", len);
904 * Don't start the slice at the default block of 34; many storage
905 * devices will use a stripe width of 128k, so start there instead.
907 #define NEW_START_BLOCK 256
910 * Validate the given pool name, optionally putting an extended error message in
914 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
920 ret = pool_namecheck(pool, &why, &what);
923 * The rules for reserved pool names were extended at a later point.
924 * But we need to support users with existing pools that may now be
925 * invalid. So we only check for this expanded set of names during a
926 * create (or import), and only in userland.
928 if (ret == 0 && !isopen &&
929 (strncmp(pool, "mirror", 6) == 0 ||
930 strncmp(pool, "raidz", 5) == 0 ||
931 strncmp(pool, "spare", 5) == 0 ||
932 strcmp(pool, "log") == 0)) {
935 dgettext(TEXT_DOMAIN, "name is reserved"));
943 case NAME_ERR_TOOLONG:
945 dgettext(TEXT_DOMAIN, "name is too long"));
948 case NAME_ERR_INVALCHAR:
950 dgettext(TEXT_DOMAIN, "invalid character "
951 "'%c' in pool name"), what);
954 case NAME_ERR_NOLETTER:
955 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
956 "name must begin with a letter"));
959 case NAME_ERR_RESERVED:
960 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
961 "name is reserved"));
964 case NAME_ERR_DISKLIKE:
965 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
966 "pool name is reserved"));
969 case NAME_ERR_LEADING_SLASH:
970 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
971 "leading slash in name"));
974 case NAME_ERR_EMPTY_COMPONENT:
975 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
976 "empty component in name"));
979 case NAME_ERR_TRAILING_SLASH:
980 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
981 "trailing slash in name"));
984 case NAME_ERR_MULTIPLE_AT:
985 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
986 "multiple '@' delimiters in name"));
998 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1002 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
1004 zpool_handle_t *zhp;
1008 * Make sure the pool name is valid.
1010 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
1011 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1012 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1017 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1020 zhp->zpool_hdl = hdl;
1021 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1023 if (zpool_refresh_stats(zhp, &missing) != 0) {
1029 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1030 (void) zfs_error_fmt(hdl, EZFS_NOENT,
1031 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1040 * Like the above, but silent on error. Used when iterating over pools (because
1041 * the configuration cache may be out of date).
1044 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1046 zpool_handle_t *zhp;
1049 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1052 zhp->zpool_hdl = hdl;
1053 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1055 if (zpool_refresh_stats(zhp, &missing) != 0) {
1071 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1075 zpool_open(libzfs_handle_t *hdl, const char *pool)
1077 zpool_handle_t *zhp;
1079 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1082 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1083 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1084 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1093 * Close the handle. Simply frees the memory associated with the handle.
1096 zpool_close(zpool_handle_t *zhp)
1098 if (zhp->zpool_config)
1099 nvlist_free(zhp->zpool_config);
1100 if (zhp->zpool_old_config)
1101 nvlist_free(zhp->zpool_old_config);
1102 if (zhp->zpool_props)
1103 nvlist_free(zhp->zpool_props);
1108 * Return the name of the pool.
1111 zpool_get_name(zpool_handle_t *zhp)
1113 return (zhp->zpool_name);
1118 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1121 zpool_get_state(zpool_handle_t *zhp)
1123 return (zhp->zpool_state);
1127 * Create the named pool, using the provided vdev list. It is assumed
1128 * that the consumer has already validated the contents of the nvlist, so we
1129 * don't have to worry about error semantics.
1132 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1133 nvlist_t *props, nvlist_t *fsprops)
1135 zfs_cmd_t zc = { 0 };
1136 nvlist_t *zc_fsprops = NULL;
1137 nvlist_t *zc_props = NULL;
1141 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1142 "cannot create '%s'"), pool);
1144 if (!zpool_name_valid(hdl, B_FALSE, pool))
1145 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1147 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1151 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1153 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1154 SPA_VERSION_1, flags, msg)) == NULL) {
1163 zoned = ((nvlist_lookup_string(fsprops,
1164 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1165 strcmp(zonestr, "on") == 0);
1167 if ((zc_fsprops = zfs_valid_proplist(hdl,
1168 ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
1172 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1175 if (nvlist_add_nvlist(zc_props,
1176 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1181 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1184 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1186 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1188 zcmd_free_nvlists(&zc);
1189 nvlist_free(zc_props);
1190 nvlist_free(zc_fsprops);
1195 * This can happen if the user has specified the same
1196 * device multiple times. We can't reliably detect this
1197 * until we try to add it and see we already have a
1200 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1201 "one or more vdevs refer to the same device"));
1202 return (zfs_error(hdl, EZFS_BADDEV, msg));
1206 * This occurs when one of the devices is below
1207 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1208 * device was the problem device since there's no
1209 * reliable way to determine device size from userland.
1214 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1216 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1217 "one or more devices is less than the "
1218 "minimum size (%s)"), buf);
1220 return (zfs_error(hdl, EZFS_BADDEV, msg));
1223 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1224 "one or more devices is out of space"));
1225 return (zfs_error(hdl, EZFS_BADDEV, msg));
1228 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1229 "cache device must be a disk or disk slice"));
1230 return (zfs_error(hdl, EZFS_BADDEV, msg));
1233 return (zpool_standard_error(hdl, errno, msg));
1238 zcmd_free_nvlists(&zc);
1239 nvlist_free(zc_props);
1240 nvlist_free(zc_fsprops);
1245 * Destroy the given pool. It is up to the caller to ensure that there are no
1246 * datasets left in the pool.
1249 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1251 zfs_cmd_t zc = { 0 };
1252 zfs_handle_t *zfp = NULL;
1253 libzfs_handle_t *hdl = zhp->zpool_hdl;
1256 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1257 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1260 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1261 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1263 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1264 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1265 "cannot destroy '%s'"), zhp->zpool_name);
1267 if (errno == EROFS) {
1268 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1269 "one or more devices is read only"));
1270 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1272 (void) zpool_standard_error(hdl, errno, msg);
1281 remove_mountpoint(zfp);
1289 * Add the given vdevs to the pool. The caller must have already performed the
1290 * necessary verification to ensure that the vdev specification is well-formed.
1293 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1295 zfs_cmd_t zc = { 0 };
1297 libzfs_handle_t *hdl = zhp->zpool_hdl;
1299 nvlist_t **spares, **l2cache;
1300 uint_t nspares, nl2cache;
1302 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1303 "cannot add to '%s'"), zhp->zpool_name);
1305 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1306 SPA_VERSION_SPARES &&
1307 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1308 &spares, &nspares) == 0) {
1309 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1310 "upgraded to add hot spares"));
1311 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1314 if (zpool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1315 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1318 for (s = 0; s < nspares; s++) {
1321 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1322 &path) == 0 && pool_uses_efi(spares[s])) {
1323 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1324 "device '%s' contains an EFI label and "
1325 "cannot be used on root pools."),
1326 zpool_vdev_name(hdl, NULL, spares[s],
1328 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1333 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1334 SPA_VERSION_L2CACHE &&
1335 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1336 &l2cache, &nl2cache) == 0) {
1337 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1338 "upgraded to add cache devices"));
1339 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1342 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1344 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1346 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1350 * This can happen if the user has specified the same
1351 * device multiple times. We can't reliably detect this
1352 * until we try to add it and see we already have a
1355 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1356 "one or more vdevs refer to the same device"));
1357 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1362 * This occurrs when one of the devices is below
1363 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1364 * device was the problem device since there's no
1365 * reliable way to determine device size from userland.
1370 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1372 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1373 "device is less than the minimum "
1376 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1380 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1381 "pool must be upgraded to add these vdevs"));
1382 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1386 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1387 "root pool can not have multiple vdevs"
1388 " or separate logs"));
1389 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1393 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1394 "cache device must be a disk or disk slice"));
1395 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1399 (void) zpool_standard_error(hdl, errno, msg);
1407 zcmd_free_nvlists(&zc);
1413 * Exports the pool from the system. The caller must ensure that there are no
1414 * mounted datasets in the pool.
1417 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1418 const char *log_str)
1420 zfs_cmd_t zc = { 0 };
1423 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1424 "cannot export '%s'"), zhp->zpool_name);
1426 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1427 zc.zc_cookie = force;
1428 zc.zc_guid = hardforce;
1429 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1431 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1434 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1435 "use '-f' to override the following errors:\n"
1436 "'%s' has an active shared spare which could be"
1437 " used by other pools once '%s' is exported."),
1438 zhp->zpool_name, zhp->zpool_name);
1439 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1442 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1451 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1453 return (zpool_export_common(zhp, force, B_FALSE, log_str));
1457 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1459 return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1463 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1466 nvlist_t *nv = NULL;
1472 if (!hdl->libzfs_printerr || config == NULL)
1475 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1476 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1480 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1482 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1484 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1485 strftime(timestr, 128, 0, &t) != 0) {
1487 (void) printf(dgettext(TEXT_DOMAIN,
1488 "Would be able to return %s "
1489 "to its state as of %s.\n"),
1492 (void) printf(dgettext(TEXT_DOMAIN,
1493 "Pool %s returned to its state as of %s.\n"),
1497 (void) printf(dgettext(TEXT_DOMAIN,
1498 "%s approximately %lld "),
1499 dryrun ? "Would discard" : "Discarded",
1501 (void) printf(dgettext(TEXT_DOMAIN,
1502 "minutes of transactions.\n"));
1503 } else if (loss > 0) {
1504 (void) printf(dgettext(TEXT_DOMAIN,
1505 "%s approximately %lld "),
1506 dryrun ? "Would discard" : "Discarded", loss);
1507 (void) printf(dgettext(TEXT_DOMAIN,
1508 "seconds of transactions.\n"));
1514 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1517 nvlist_t *nv = NULL;
1519 uint64_t edata = UINT64_MAX;
1524 if (!hdl->libzfs_printerr)
1528 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1530 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1532 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1533 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1534 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1535 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1538 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1539 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1542 (void) printf(dgettext(TEXT_DOMAIN,
1543 "Recovery is possible, but will result in some data loss.\n"));
1545 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1546 strftime(timestr, 128, 0, &t) != 0) {
1547 (void) printf(dgettext(TEXT_DOMAIN,
1548 "\tReturning the pool to its state as of %s\n"
1549 "\tshould correct the problem. "),
1552 (void) printf(dgettext(TEXT_DOMAIN,
1553 "\tReverting the pool to an earlier state "
1554 "should correct the problem.\n\t"));
1558 (void) printf(dgettext(TEXT_DOMAIN,
1559 "Approximately %lld minutes of data\n"
1560 "\tmust be discarded, irreversibly. "), (loss + 30) / 60);
1561 } else if (loss > 0) {
1562 (void) printf(dgettext(TEXT_DOMAIN,
1563 "Approximately %lld seconds of data\n"
1564 "\tmust be discarded, irreversibly. "), loss);
1566 if (edata != 0 && edata != UINT64_MAX) {
1568 (void) printf(dgettext(TEXT_DOMAIN,
1569 "After rewind, at least\n"
1570 "\tone persistent user-data error will remain. "));
1572 (void) printf(dgettext(TEXT_DOMAIN,
1573 "After rewind, several\n"
1574 "\tpersistent user-data errors will remain. "));
1577 (void) printf(dgettext(TEXT_DOMAIN,
1578 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1579 reason >= 0 ? "clear" : "import", name);
1581 (void) printf(dgettext(TEXT_DOMAIN,
1582 "A scrub of the pool\n"
1583 "\tis strongly recommended after recovery.\n"));
1587 (void) printf(dgettext(TEXT_DOMAIN,
1588 "Destroy and re-create the pool from\n\ta backup source.\n"));
1592 * zpool_import() is a contracted interface. Should be kept the same
1595 * Applications should use zpool_import_props() to import a pool with
1596 * new properties value to be set.
1599 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1602 nvlist_t *props = NULL;
1605 if (altroot != NULL) {
1606 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1607 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1608 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1612 if (nvlist_add_string(props,
1613 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1614 nvlist_add_string(props,
1615 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1617 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1618 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1623 ret = zpool_import_props(hdl, config, newname, props,
1631 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1637 uint64_t is_log = 0;
1639 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1643 (void) printf("\t%*s%s%s\n", indent, "", name,
1644 is_log ? " [log]" : "");
1646 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1647 &child, &children) != 0)
1650 for (c = 0; c < children; c++) {
1651 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
1652 print_vdev_tree(hdl, vname, child[c], indent + 2);
1658 zpool_print_unsup_feat(nvlist_t *config)
1660 nvlist_t *nvinfo, *unsup_feat;
1662 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1664 verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1667 for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1668 nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1671 verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1672 verify(nvpair_value_string(nvp, &desc) == 0);
1674 if (strlen(desc) > 0)
1675 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1677 (void) printf("\t%s\n", nvpair_name(nvp));
1682 * Import the given pool using the known configuration and a list of
1683 * properties to be set. The configuration should have come from
1684 * zpool_find_import(). The 'newname' parameters control whether the pool
1685 * is imported with a different name.
1688 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1689 nvlist_t *props, int flags)
1691 zfs_cmd_t zc = { 0 };
1692 zpool_rewind_policy_t policy;
1693 nvlist_t *nv = NULL;
1694 nvlist_t *nvinfo = NULL;
1695 nvlist_t *missing = NULL;
1702 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1705 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1706 "cannot import pool '%s'"), origname);
1708 if (newname != NULL) {
1709 if (!zpool_name_valid(hdl, B_FALSE, newname))
1710 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1711 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1713 thename = (char *)newname;
1720 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1722 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1725 if ((props = zpool_valid_proplist(hdl, origname,
1726 props, version, flags, errbuf)) == NULL) {
1728 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1734 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1736 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1739 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1743 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1748 zc.zc_cookie = flags;
1749 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1751 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1752 zcmd_free_nvlists(&zc);
1759 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1760 zpool_get_rewind_policy(config, &policy);
1766 * Dry-run failed, but we print out what success
1767 * looks like if we found a best txg
1769 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1770 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1776 if (newname == NULL)
1777 (void) snprintf(desc, sizeof (desc),
1778 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1781 (void) snprintf(desc, sizeof (desc),
1782 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1787 if (nv != NULL && nvlist_lookup_nvlist(nv,
1788 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1789 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1790 (void) printf(dgettext(TEXT_DOMAIN, "This "
1791 "pool uses the following feature(s) not "
1792 "supported by this system:\n"));
1793 zpool_print_unsup_feat(nv);
1794 if (nvlist_exists(nvinfo,
1795 ZPOOL_CONFIG_CAN_RDONLY)) {
1796 (void) printf(dgettext(TEXT_DOMAIN,
1797 "All unsupported features are only "
1798 "required for writing to the pool."
1799 "\nThe pool can be imported using "
1800 "'-o readonly=on'.\n"));
1804 * Unsupported version.
1806 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1810 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1814 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1815 "one or more devices is read only"));
1816 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1820 if (nv && nvlist_lookup_nvlist(nv,
1821 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1822 nvlist_lookup_nvlist(nvinfo,
1823 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1824 (void) printf(dgettext(TEXT_DOMAIN,
1825 "The devices below are missing, use "
1826 "'-m' to import the pool anyway:\n"));
1827 print_vdev_tree(hdl, NULL, missing, 2);
1828 (void) printf("\n");
1830 (void) zpool_standard_error(hdl, error, desc);
1834 (void) zpool_standard_error(hdl, error, desc);
1838 (void) zpool_standard_error(hdl, error, desc);
1839 zpool_explain_recover(hdl,
1840 newname ? origname : thename, -error, nv);
1847 zpool_handle_t *zhp;
1850 * This should never fail, but play it safe anyway.
1852 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1854 else if (zhp != NULL)
1856 if (policy.zrp_request &
1857 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1858 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1859 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1865 zcmd_free_nvlists(&zc);
1875 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1877 zfs_cmd_t zc = { 0 };
1879 libzfs_handle_t *hdl = zhp->zpool_hdl;
1881 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1882 zc.zc_cookie = func;
1884 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
1885 (errno == ENOENT && func != POOL_SCAN_NONE))
1888 if (func == POOL_SCAN_SCRUB) {
1889 (void) snprintf(msg, sizeof (msg),
1890 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1891 } else if (func == POOL_SCAN_NONE) {
1892 (void) snprintf(msg, sizeof (msg),
1893 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1896 assert(!"unexpected result");
1899 if (errno == EBUSY) {
1901 pool_scan_stat_t *ps = NULL;
1904 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1905 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1906 (void) nvlist_lookup_uint64_array(nvroot,
1907 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1908 if (ps && ps->pss_func == POOL_SCAN_SCRUB)
1909 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1911 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1912 } else if (errno == ENOENT) {
1913 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1915 return (zpool_standard_error(hdl, errno, msg));
1921 * This provides a very minimal check whether a given string is likely a
1922 * c#t#d# style string. Users of this are expected to do their own
1923 * verification of the s# part.
1925 #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1]))
1928 * More elaborate version for ones which may start with "/dev/dsk/"
1932 ctd_check_path(char *str) {
1934 * If it starts with a slash, check the last component.
1936 if (str && str[0] == '/') {
1937 char *tmp = strrchr(str, '/');
1940 * If it ends in "/old", check the second-to-last
1941 * component of the string instead.
1943 if (tmp != str && strcmp(tmp, "/old") == 0) {
1944 for (tmp--; *tmp != '/'; tmp--)
1949 return (CTD_CHECK(str));
1954 * Find a vdev that matches the search criteria specified. We use the
1955 * the nvpair name to determine how we should look for the device.
1956 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1957 * spare; but FALSE if its an INUSE spare.
1960 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1961 boolean_t *l2cache, boolean_t *log)
1968 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1970 /* Nothing to look for */
1971 if (search == NULL || pair == NULL)
1974 /* Obtain the key we will use to search */
1975 srchkey = nvpair_name(pair);
1977 switch (nvpair_type(pair)) {
1978 case DATA_TYPE_UINT64:
1979 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1980 uint64_t srchval, theguid;
1982 verify(nvpair_value_uint64(pair, &srchval) == 0);
1983 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1985 if (theguid == srchval)
1990 case DATA_TYPE_STRING: {
1991 char *srchval, *val;
1993 verify(nvpair_value_string(pair, &srchval) == 0);
1994 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1998 * Search for the requested value. Special cases:
2000 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2001 * "s0" or "s0/old". The "s0" part is hidden from the user,
2002 * but included in the string, so this matches around it.
2003 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2005 * Otherwise, all other searches are simple string compares.
2008 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
2009 ctd_check_path(val)) {
2010 uint64_t wholedisk = 0;
2012 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2015 int slen = strlen(srchval);
2016 int vlen = strlen(val);
2018 if (slen != vlen - 2)
2022 * make_leaf_vdev() should only set
2023 * wholedisk for ZPOOL_CONFIG_PATHs which
2024 * will include "/dev/dsk/", giving plenty of
2025 * room for the indices used next.
2030 * strings identical except trailing "s0"
2032 if (strcmp(&val[vlen - 2], "s0") == 0 &&
2033 strncmp(srchval, val, slen) == 0)
2037 * strings identical except trailing "s0/old"
2039 if (strcmp(&val[vlen - 6], "s0/old") == 0 &&
2040 strcmp(&srchval[slen - 4], "/old") == 0 &&
2041 strncmp(srchval, val, slen - 4) == 0)
2046 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2048 if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2050 char *type, *idx, *end, *p;
2051 uint64_t id, vdev_id;
2054 * Determine our vdev type, keeping in mind
2055 * that the srchval is composed of a type and
2056 * vdev id pair (i.e. mirror-4).
2058 if ((type = strdup(srchval)) == NULL)
2061 if ((p = strrchr(type, '-')) == NULL) {
2069 * If the types don't match then keep looking.
2071 if (strncmp(val, type, strlen(val)) != 0) {
2076 verify(strncmp(type, VDEV_TYPE_RAIDZ,
2077 strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2078 strncmp(type, VDEV_TYPE_MIRROR,
2079 strlen(VDEV_TYPE_MIRROR)) == 0);
2080 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2084 vdev_id = strtoull(idx, &end, 10);
2091 * Now verify that we have the correct vdev id.
2100 if (strcmp(srchval, val) == 0)
2109 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2110 &child, &children) != 0)
2113 for (c = 0; c < children; c++) {
2114 if ((ret = vdev_to_nvlist_iter(child[c], search,
2115 avail_spare, l2cache, NULL)) != NULL) {
2117 * The 'is_log' value is only set for the toplevel
2118 * vdev, not the leaf vdevs. So we always lookup the
2119 * log device from the root of the vdev tree (where
2120 * 'log' is non-NULL).
2123 nvlist_lookup_uint64(child[c],
2124 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2132 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2133 &child, &children) == 0) {
2134 for (c = 0; c < children; c++) {
2135 if ((ret = vdev_to_nvlist_iter(child[c], search,
2136 avail_spare, l2cache, NULL)) != NULL) {
2137 *avail_spare = B_TRUE;
2143 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2144 &child, &children) == 0) {
2145 for (c = 0; c < children; c++) {
2146 if ((ret = vdev_to_nvlist_iter(child[c], search,
2147 avail_spare, l2cache, NULL)) != NULL) {
2158 * Given a physical path (minus the "/devices" prefix), find the
2162 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2163 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2165 nvlist_t *search, *nvroot, *ret;
2167 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2168 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2170 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2173 *avail_spare = B_FALSE;
2177 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2178 nvlist_free(search);
2184 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2187 zpool_vdev_is_interior(const char *name)
2189 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2190 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2196 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2197 boolean_t *l2cache, boolean_t *log)
2199 char buf[MAXPATHLEN];
2201 nvlist_t *nvroot, *search, *ret;
2204 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2206 guid = strtoull(path, &end, 10);
2207 if (guid != 0 && *end == '\0') {
2208 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2209 } else if (zpool_vdev_is_interior(path)) {
2210 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2211 } else if (path[0] != '/') {
2212 (void) snprintf(buf, sizeof (buf), "%s%s", _PATH_DEV, path);
2213 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
2215 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2218 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2221 *avail_spare = B_FALSE;
2225 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2226 nvlist_free(search);
2232 vdev_online(nvlist_t *nv)
2236 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2237 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2238 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2245 * Helper function for zpool_get_physpaths().
2248 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2249 size_t *bytes_written)
2251 size_t bytes_left, pos, rsz;
2255 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2257 return (EZFS_NODEVICE);
2259 pos = *bytes_written;
2260 bytes_left = physpath_size - pos;
2261 format = (pos == 0) ? "%s" : " %s";
2263 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2264 *bytes_written += rsz;
2266 if (rsz >= bytes_left) {
2267 /* if physpath was not copied properly, clear it */
2268 if (bytes_left != 0) {
2271 return (EZFS_NOSPC);
2277 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2278 size_t *rsz, boolean_t is_spare)
2283 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2284 return (EZFS_INVALCONFIG);
2286 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2288 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2289 * For a spare vdev, we only want to boot from the active
2294 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2297 return (EZFS_INVALCONFIG);
2300 if (vdev_online(nv)) {
2301 if ((ret = vdev_get_one_physpath(nv, physpath,
2302 phypath_size, rsz)) != 0)
2305 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2306 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2307 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2312 if (nvlist_lookup_nvlist_array(nv,
2313 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2314 return (EZFS_INVALCONFIG);
2316 for (i = 0; i < count; i++) {
2317 ret = vdev_get_physpaths(child[i], physpath,
2318 phypath_size, rsz, is_spare);
2319 if (ret == EZFS_NOSPC)
2324 return (EZFS_POOL_INVALARG);
2328 * Get phys_path for a root pool config.
2329 * Return 0 on success; non-zero on failure.
2332 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2335 nvlist_t *vdev_root;
2342 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2344 return (EZFS_INVALCONFIG);
2346 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2347 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2348 &child, &count) != 0)
2349 return (EZFS_INVALCONFIG);
2352 * root pool can not have EFI labeled disks and can only have
2353 * a single top-level vdev.
2355 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
2356 pool_uses_efi(vdev_root))
2357 return (EZFS_POOL_INVALARG);
2359 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2362 /* No online devices */
2364 return (EZFS_NODEVICE);
2370 * Get phys_path for a root pool
2371 * Return 0 on success; non-zero on failure.
2374 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2376 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2381 * If the device has being dynamically expanded then we need to relabel
2382 * the disk to use the new unallocated space.
2385 zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2388 char path[MAXPATHLEN];
2391 int (*_efi_use_whole_disk)(int);
2393 if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2394 "efi_use_whole_disk")) == NULL)
2397 (void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name);
2399 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2400 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2401 "relabel '%s': unable to open device"), name);
2402 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2406 * It's possible that we might encounter an error if the device
2407 * does not have any unallocated space left. If so, we simply
2408 * ignore that error and continue on.
2410 error = _efi_use_whole_disk(fd);
2412 if (error && error != VT_ENOSPC) {
2413 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2414 "relabel '%s': unable to read disk capacity"), name);
2415 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2422 * Bring the specified vdev online. The 'flags' parameter is a set of the
2423 * ZFS_ONLINE_* flags.
2426 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2427 vdev_state_t *newstate)
2429 zfs_cmd_t zc = { 0 };
2432 boolean_t avail_spare, l2cache, islog;
2433 libzfs_handle_t *hdl = zhp->zpool_hdl;
2435 if (flags & ZFS_ONLINE_EXPAND) {
2436 (void) snprintf(msg, sizeof (msg),
2437 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2439 (void) snprintf(msg, sizeof (msg),
2440 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2443 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2444 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2446 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2448 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2451 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2453 if (flags & ZFS_ONLINE_EXPAND ||
2454 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2455 char *pathname = NULL;
2456 uint64_t wholedisk = 0;
2458 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2460 verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
2464 * XXX - L2ARC 1.0 devices can't support expansion.
2467 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2468 "cannot expand cache devices"));
2469 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2473 pathname += strlen(DISK_ROOT) + 1;
2474 (void) zpool_relabel_disk(hdl, pathname);
2478 zc.zc_cookie = VDEV_STATE_ONLINE;
2481 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2482 if (errno == EINVAL) {
2483 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2484 "from this pool into a new one. Use '%s' "
2485 "instead"), "zpool detach");
2486 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2488 return (zpool_standard_error(hdl, errno, msg));
2491 *newstate = zc.zc_cookie;
2496 * Take the specified vdev offline
2499 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2501 zfs_cmd_t zc = { 0 };
2504 boolean_t avail_spare, l2cache;
2505 libzfs_handle_t *hdl = zhp->zpool_hdl;
2507 (void) snprintf(msg, sizeof (msg),
2508 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2510 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2511 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2513 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2515 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2518 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2520 zc.zc_cookie = VDEV_STATE_OFFLINE;
2521 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2523 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2530 * There are no other replicas of this device.
2532 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2536 * The log device has unplayed logs
2538 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2541 return (zpool_standard_error(hdl, errno, msg));
2546 * Mark the given vdev faulted.
2549 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2551 zfs_cmd_t zc = { 0 };
2553 libzfs_handle_t *hdl = zhp->zpool_hdl;
2555 (void) snprintf(msg, sizeof (msg),
2556 dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2558 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2560 zc.zc_cookie = VDEV_STATE_FAULTED;
2563 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2570 * There are no other replicas of this device.
2572 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2575 return (zpool_standard_error(hdl, errno, msg));
2581 * Mark the given vdev degraded.
2584 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2586 zfs_cmd_t zc = { 0 };
2588 libzfs_handle_t *hdl = zhp->zpool_hdl;
2590 (void) snprintf(msg, sizeof (msg),
2591 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
2593 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2595 zc.zc_cookie = VDEV_STATE_DEGRADED;
2598 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2601 return (zpool_standard_error(hdl, errno, msg));
2605 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2609 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2615 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2617 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2620 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2621 children == 2 && child[which] == tgt)
2624 for (c = 0; c < children; c++)
2625 if (is_replacing_spare(child[c], tgt, which))
2633 * Attach new_disk (fully described by nvroot) to old_disk.
2634 * If 'replacing' is specified, the new disk will replace the old one.
2637 zpool_vdev_attach(zpool_handle_t *zhp,
2638 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2640 zfs_cmd_t zc = { 0 };
2644 boolean_t avail_spare, l2cache, islog;
2649 nvlist_t *config_root;
2650 libzfs_handle_t *hdl = zhp->zpool_hdl;
2651 boolean_t rootpool = zpool_is_bootable(zhp);
2654 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2655 "cannot replace %s with %s"), old_disk, new_disk);
2657 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2658 "cannot attach %s to %s"), new_disk, old_disk);
2661 * If this is a root pool, make sure that we're not attaching an
2662 * EFI labeled device.
2664 if (rootpool && pool_uses_efi(nvroot)) {
2665 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2666 "EFI labeled devices are not supported on root pools."));
2667 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2670 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2671 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2673 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2676 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2679 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2681 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2682 zc.zc_cookie = replacing;
2684 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2685 &child, &children) != 0 || children != 1) {
2686 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2687 "new device must be a single disk"));
2688 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2691 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2692 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2694 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2698 * If the target is a hot spare that has been swapped in, we can only
2699 * replace it with another hot spare.
2702 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2703 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2704 NULL) == NULL || !avail_spare) &&
2705 is_replacing_spare(config_root, tgt, 1)) {
2706 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2707 "can only be replaced by another hot spare"));
2709 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2714 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2717 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2719 zcmd_free_nvlists(&zc);
2724 * XXX need a better way to prevent user from
2725 * booting up a half-baked vdev.
2727 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2728 "sure to wait until resilver is done "
2729 "before rebooting.\n"));
2730 (void) fprintf(stderr, "\n");
2731 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "If "
2732 "you boot from pool '%s', you may need to update\n"
2733 "boot code on newly attached disk '%s'.\n\n"
2734 "Assuming you use GPT partitioning and 'da0' is "
2735 "your new boot disk\n"
2736 "you may use the following command:\n\n"
2737 "\tgpart bootcode -b /boot/pmbr -p "
2738 "/boot/gptzfsboot -i 1 da0\n\n"),
2739 zhp->zpool_name, new_disk);
2747 * Can't attach to or replace this type of vdev.
2750 uint64_t version = zpool_get_prop_int(zhp,
2751 ZPOOL_PROP_VERSION, NULL);
2754 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2755 "cannot replace a log with a spare"));
2756 else if (version >= SPA_VERSION_MULTI_REPLACE)
2757 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2758 "already in replacing/spare config; wait "
2759 "for completion or use 'zpool detach'"));
2761 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2762 "cannot replace a replacing device"));
2764 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2765 "can only attach to mirrors and top-level "
2768 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2773 * The new device must be a single disk.
2775 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2776 "new device must be a single disk"));
2777 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2781 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2783 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2788 * The new device is too small.
2790 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2791 "device is too small"));
2792 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2797 * The new device has a different alignment requirement.
2799 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2800 "devices have different sector alignment"));
2801 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2806 * The resulting top-level vdev spec won't fit in the label.
2808 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2812 (void) zpool_standard_error(hdl, errno, msg);
2819 * Detach the specified device.
2822 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2824 zfs_cmd_t zc = { 0 };
2827 boolean_t avail_spare, l2cache;
2828 libzfs_handle_t *hdl = zhp->zpool_hdl;
2830 (void) snprintf(msg, sizeof (msg),
2831 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2833 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2834 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2836 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2839 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2842 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2844 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2846 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2853 * Can't detach from this type of vdev.
2855 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2856 "applicable to mirror and replacing vdevs"));
2857 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2862 * There are no other replicas of this device.
2864 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2868 (void) zpool_standard_error(hdl, errno, msg);
2875 * Find a mirror vdev in the source nvlist.
2877 * The mchild array contains a list of disks in one of the top-level mirrors
2878 * of the source pool. The schild array contains a list of disks that the
2879 * user specified on the command line. We loop over the mchild array to
2880 * see if any entry in the schild array matches.
2882 * If a disk in the mchild array is found in the schild array, we return
2883 * the index of that entry. Otherwise we return -1.
2886 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2887 nvlist_t **schild, uint_t schildren)
2891 for (mc = 0; mc < mchildren; mc++) {
2893 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2894 mchild[mc], B_FALSE);
2896 for (sc = 0; sc < schildren; sc++) {
2897 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2898 schild[sc], B_FALSE);
2899 boolean_t result = (strcmp(mpath, spath) == 0);
2915 * Split a mirror pool. If newroot points to null, then a new nvlist
2916 * is generated and it is the responsibility of the caller to free it.
2919 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2920 nvlist_t *props, splitflags_t flags)
2922 zfs_cmd_t zc = { 0 };
2924 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2925 nvlist_t **varray = NULL, *zc_props = NULL;
2926 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2927 libzfs_handle_t *hdl = zhp->zpool_hdl;
2929 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2932 (void) snprintf(msg, sizeof (msg),
2933 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2935 if (!zpool_name_valid(hdl, B_FALSE, newname))
2936 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2938 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2939 (void) fprintf(stderr, gettext("Internal error: unable to "
2940 "retrieve pool configuration\n"));
2944 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2946 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2949 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2950 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2951 props, vers, flags, msg)) == NULL)
2955 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2957 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2958 "Source pool is missing vdev tree"));
2960 nvlist_free(zc_props);
2964 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2967 if (*newroot == NULL ||
2968 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2969 &newchild, &newchildren) != 0)
2972 for (c = 0; c < children; c++) {
2973 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2975 nvlist_t **mchild, *vdev;
2980 * Unlike cache & spares, slogs are stored in the
2981 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2983 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2985 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2987 if (is_log || is_hole) {
2989 * Create a hole vdev and put it in the config.
2991 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2993 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2994 VDEV_TYPE_HOLE) != 0)
2996 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
3001 varray[vcount++] = vdev;
3005 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
3007 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
3008 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3009 "Source pool must be composed only of mirrors\n"));
3010 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3014 verify(nvlist_lookup_nvlist_array(child[c],
3015 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3017 /* find or add an entry for this top-level vdev */
3018 if (newchildren > 0 &&
3019 (entry = find_vdev_entry(zhp, mchild, mchildren,
3020 newchild, newchildren)) >= 0) {
3021 /* We found a disk that the user specified. */
3022 vdev = mchild[entry];
3025 /* User didn't specify a disk for this vdev. */
3026 vdev = mchild[mchildren - 1];
3029 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3033 /* did we find every disk the user specified? */
3034 if (found != newchildren) {
3035 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3036 "include at most one disk from each mirror"));
3037 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3041 /* Prepare the nvlist for populating. */
3042 if (*newroot == NULL) {
3043 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3046 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3047 VDEV_TYPE_ROOT) != 0)
3050 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3053 /* Add all the children we found */
3054 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3055 lastlog == 0 ? vcount : lastlog) != 0)
3059 * If we're just doing a dry run, exit now with success.
3062 memory_err = B_FALSE;
3067 /* now build up the config list & call the ioctl */
3068 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3071 if (nvlist_add_nvlist(newconfig,
3072 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3073 nvlist_add_string(newconfig,
3074 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3075 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3079 * The new pool is automatically part of the namespace unless we
3080 * explicitly export it.
3083 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3084 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3085 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3086 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3088 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3091 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3092 retval = zpool_standard_error(hdl, errno, msg);
3097 memory_err = B_FALSE;
3100 if (varray != NULL) {
3103 for (v = 0; v < vcount; v++)
3104 nvlist_free(varray[v]);
3107 zcmd_free_nvlists(&zc);
3109 nvlist_free(zc_props);
3111 nvlist_free(newconfig);
3113 nvlist_free(*newroot);
3121 return (no_memory(hdl));
3127 * Remove the given device. Currently, this is supported only for hot spares
3128 * and level 2 cache devices.
3131 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3133 zfs_cmd_t zc = { 0 };
3136 boolean_t avail_spare, l2cache, islog;
3137 libzfs_handle_t *hdl = zhp->zpool_hdl;
3140 (void) snprintf(msg, sizeof (msg),
3141 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3143 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3144 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3146 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3148 * XXX - this should just go away.
3150 if (!avail_spare && !l2cache && !islog) {
3151 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3152 "only inactive hot spares, cache, top-level, "
3153 "or log devices can be removed"));
3154 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3157 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3158 if (islog && version < SPA_VERSION_HOLES) {
3159 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3160 "pool must be upgrade to support log removal"));
3161 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3164 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3166 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3169 return (zpool_standard_error(hdl, errno, msg));
3173 * Clear the errors for the pool, or the particular device if specified.
3176 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3178 zfs_cmd_t zc = { 0 };
3181 zpool_rewind_policy_t policy;
3182 boolean_t avail_spare, l2cache;
3183 libzfs_handle_t *hdl = zhp->zpool_hdl;
3184 nvlist_t *nvi = NULL;
3188 (void) snprintf(msg, sizeof (msg),
3189 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3192 (void) snprintf(msg, sizeof (msg),
3193 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3196 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3198 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3199 &l2cache, NULL)) == 0)
3200 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3203 * Don't allow error clearing for hot spares. Do allow
3204 * error clearing for l2cache devices.
3207 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3209 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3213 zpool_get_rewind_policy(rewindnvl, &policy);
3214 zc.zc_cookie = policy.zrp_request;
3216 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3219 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3222 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3224 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3225 zcmd_free_nvlists(&zc);
3230 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
3231 errno != EPERM && errno != EACCES)) {
3232 if (policy.zrp_request &
3233 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3234 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3235 zpool_rewind_exclaim(hdl, zc.zc_name,
3236 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
3240 zcmd_free_nvlists(&zc);
3244 zcmd_free_nvlists(&zc);
3245 return (zpool_standard_error(hdl, errno, msg));
3249 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3252 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3254 zfs_cmd_t zc = { 0 };
3256 libzfs_handle_t *hdl = zhp->zpool_hdl;
3258 (void) snprintf(msg, sizeof (msg),
3259 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3262 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3264 zc.zc_cookie = ZPOOL_NO_REWIND;
3266 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3269 return (zpool_standard_error(hdl, errno, msg));
3273 * Change the GUID for a pool.
3276 zpool_reguid(zpool_handle_t *zhp)
3279 libzfs_handle_t *hdl = zhp->zpool_hdl;
3280 zfs_cmd_t zc = { 0 };
3282 (void) snprintf(msg, sizeof (msg),
3283 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3285 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3286 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3289 return (zpool_standard_error(hdl, errno, msg));
3296 zpool_reopen(zpool_handle_t *zhp)
3298 zfs_cmd_t zc = { 0 };
3300 libzfs_handle_t *hdl = zhp->zpool_hdl;
3302 (void) snprintf(msg, sizeof (msg),
3303 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
3306 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3307 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
3309 return (zpool_standard_error(hdl, errno, msg));
3313 * Convert from a devid string to a path.
3316 devid_to_path(char *devid_str)
3321 devid_nmlist_t *list = NULL;
3324 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3327 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3329 devid_str_free(minor);
3335 if ((path = strdup(list[0].devname)) == NULL)
3338 devid_free_nmlist(list);
3344 * Convert from a path to a devid string.
3347 path_to_devid(const char *path)
3354 if ((fd = open(path, O_RDONLY)) < 0)
3359 if (devid_get(fd, &devid) == 0) {
3360 if (devid_get_minor_name(fd, &minor) == 0)
3361 ret = devid_str_encode(devid, minor);
3363 devid_str_free(minor);
3375 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3376 * ignore any failure here, since a common case is for an unprivileged user to
3377 * type 'zpool status', and we'll display the correct information anyway.
3380 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3382 zfs_cmd_t zc = { 0 };
3384 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3385 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3386 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3389 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3393 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3394 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3395 * We also check if this is a whole disk, in which case we strip off the
3396 * trailing 's0' slice name.
3398 * This routine is also responsible for identifying when disks have been
3399 * reconfigured in a new location. The kernel will have opened the device by
3400 * devid, but the path will still refer to the old location. To catch this, we
3401 * first do a path -> devid translation (which is fast for the common case). If
3402 * the devid matches, we're done. If not, we do a reverse devid -> path
3403 * translation and issue the appropriate ioctl() to update the path of the vdev.
3404 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3408 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3419 have_stats = nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3420 (uint64_t **)&vs, &vsc) == 0;
3421 have_path = nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0;
3424 * If the device is not currently present, assume it will not
3425 * come back at the same device path. Display the device by GUID.
3427 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3428 have_path && have_stats && vs->vs_state <= VDEV_STATE_CANT_OPEN) {
3429 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3431 (void) snprintf(buf, sizeof (buf), "%llu",
3432 (u_longlong_t)value);
3434 } else if (have_path) {
3437 * If the device is dead (faulted, offline, etc) then don't
3438 * bother opening it. Otherwise we may be forcing the user to
3439 * open a misbehaving device, which can have undesirable
3442 if ((have_stats == 0 ||
3443 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3445 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3447 * Determine if the current path is correct.
3449 char *newdevid = path_to_devid(path);
3451 if (newdevid == NULL ||
3452 strcmp(devid, newdevid) != 0) {
3455 if ((newpath = devid_to_path(devid)) != NULL) {
3457 * Update the path appropriately.
3459 set_path(zhp, nv, newpath);
3460 if (nvlist_add_string(nv,
3461 ZPOOL_CONFIG_PATH, newpath) == 0)
3462 verify(nvlist_lookup_string(nv,
3470 devid_str_free(newdevid);
3474 if (strncmp(path, "/dev/dsk/", 9) == 0)
3477 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3478 &value) == 0 && value) {
3479 int pathlen = strlen(path);
3480 char *tmp = zfs_strdup(hdl, path);
3483 * If it starts with c#, and ends with "s0", chop
3484 * the "s0" off, or if it ends with "s0/old", remove
3485 * the "s0" from the middle.
3487 if (CTD_CHECK(tmp)) {
3488 if (strcmp(&tmp[pathlen - 2], "s0") == 0) {
3489 tmp[pathlen - 2] = '\0';
3490 } else if (pathlen > 6 &&
3491 strcmp(&tmp[pathlen - 6], "s0/old") == 0) {
3492 (void) strcpy(&tmp[pathlen - 6],
3499 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3500 path += sizeof(_PATH_DEV) - 1;
3503 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3506 * If it's a raidz device, we need to stick in the parity level.
3508 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3509 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3511 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3512 (u_longlong_t)value);
3517 * We identify each top-level vdev by using a <type-id>
3518 * naming convention.
3523 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3525 (void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3531 return (zfs_strdup(hdl, path));
3535 zbookmark_compare(const void *a, const void *b)
3537 return (memcmp(a, b, sizeof (zbookmark_phys_t)));
3541 * Retrieve the persistent error log, uniquify the members, and return to the
3545 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3547 zfs_cmd_t zc = { 0 };
3549 zbookmark_phys_t *zb = NULL;
3553 * Retrieve the raw error list from the kernel. If the number of errors
3554 * has increased, allocate more space and continue until we get the
3557 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3561 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3562 count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL)
3564 zc.zc_nvlist_dst_size = count;
3565 (void) strcpy(zc.zc_name, zhp->zpool_name);
3567 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3569 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3570 if (errno == ENOMEM) {
3573 count = zc.zc_nvlist_dst_size;
3574 dst = zfs_alloc(zhp->zpool_hdl, count *
3575 sizeof (zbookmark_phys_t));
3578 zc.zc_nvlist_dst = (uintptr_t)dst;
3588 * Sort the resulting bookmarks. This is a little confusing due to the
3589 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3590 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3591 * _not_ copied as part of the process. So we point the start of our
3592 * array appropriate and decrement the total number of elements.
3594 zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
3595 zc.zc_nvlist_dst_size;
3596 count -= zc.zc_nvlist_dst_size;
3598 qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_compare);
3600 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3603 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3605 for (i = 0; i < count; i++) {
3608 /* ignoring zb_blkid and zb_level for now */
3609 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3610 zb[i-1].zb_object == zb[i].zb_object)
3613 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3615 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3616 zb[i].zb_objset) != 0) {
3620 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3621 zb[i].zb_object) != 0) {
3625 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3632 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3636 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3637 return (no_memory(zhp->zpool_hdl));
3641 * Upgrade a ZFS pool to the latest on-disk version.
3644 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3646 zfs_cmd_t zc = { 0 };
3647 libzfs_handle_t *hdl = zhp->zpool_hdl;
3649 (void) strcpy(zc.zc_name, zhp->zpool_name);
3650 zc.zc_cookie = new_version;
3652 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3653 return (zpool_standard_error_fmt(hdl, errno,
3654 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3660 zfs_save_arguments(int argc, char **argv, char *string, int len)
3662 (void) strlcpy(string, basename(argv[0]), len);
3663 for (int i = 1; i < argc; i++) {
3664 (void) strlcat(string, " ", len);
3665 (void) strlcat(string, argv[i], len);
3670 zpool_log_history(libzfs_handle_t *hdl, const char *message)
3672 zfs_cmd_t zc = { 0 };
3676 args = fnvlist_alloc();
3677 fnvlist_add_string(args, "message", message);
3678 err = zcmd_write_src_nvlist(hdl, &zc, args);
3680 err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
3682 zcmd_free_nvlists(&zc);
3687 * Perform ioctl to get some command history of a pool.
3689 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3690 * logical offset of the history buffer to start reading from.
3692 * Upon return, 'off' is the next logical offset to read from and
3693 * 'len' is the actual amount of bytes read into 'buf'.
3696 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3698 zfs_cmd_t zc = { 0 };
3699 libzfs_handle_t *hdl = zhp->zpool_hdl;
3701 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3703 zc.zc_history = (uint64_t)(uintptr_t)buf;
3704 zc.zc_history_len = *len;
3705 zc.zc_history_offset = *off;
3707 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3710 return (zfs_error_fmt(hdl, EZFS_PERM,
3711 dgettext(TEXT_DOMAIN,
3712 "cannot show history for pool '%s'"),
3715 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3716 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3717 "'%s'"), zhp->zpool_name));
3719 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3720 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3721 "'%s', pool must be upgraded"), zhp->zpool_name));
3723 return (zpool_standard_error_fmt(hdl, errno,
3724 dgettext(TEXT_DOMAIN,
3725 "cannot get history for '%s'"), zhp->zpool_name));
3729 *len = zc.zc_history_len;
3730 *off = zc.zc_history_offset;
3736 * Process the buffer of nvlists, unpacking and storing each nvlist record
3737 * into 'records'. 'leftover' is set to the number of bytes that weren't
3738 * processed as there wasn't a complete record.
3741 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3742 nvlist_t ***records, uint_t *numrecords)
3748 while (bytes_read > sizeof (reclen)) {
3750 /* get length of packed record (stored as little endian) */
3751 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3752 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3754 if (bytes_read < sizeof (reclen) + reclen)
3758 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3760 bytes_read -= sizeof (reclen) + reclen;
3761 buf += sizeof (reclen) + reclen;
3763 /* add record to nvlist array */
3765 if (ISP2(*numrecords + 1)) {
3766 *records = realloc(*records,
3767 *numrecords * 2 * sizeof (nvlist_t *));
3769 (*records)[*numrecords - 1] = nv;
3772 *leftover = bytes_read;
3776 /* from spa_history.c: spa_history_create_obj() */
3777 #define HIS_BUF_LEN_DEF (128 << 10)
3778 #define HIS_BUF_LEN_MAX (1 << 30)
3781 * Retrieve the command history of a pool.
3784 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3787 uint64_t bufsize = HIS_BUF_LEN_DEF;
3789 nvlist_t **records = NULL;
3790 uint_t numrecords = 0;
3793 if ((buf = malloc(bufsize)) == NULL)
3796 uint64_t bytes_read = bufsize;
3799 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3802 /* if nothing else was read in, we're at EOF, just return */
3803 if (bytes_read == 0)
3806 if ((err = zpool_history_unpack(buf, bytes_read,
3807 &leftover, &records, &numrecords)) != 0)
3812 * If the history block is too big, double the buffer
3813 * size and try again.
3815 if (leftover == bytes_read) {
3820 if ((bufsize >= HIS_BUF_LEN_MAX) ||
3821 ((buf = malloc(bufsize)) == NULL)) {
3832 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3833 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3834 records, numrecords) == 0);
3836 for (i = 0; i < numrecords; i++)
3837 nvlist_free(records[i]);
3844 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3845 char *pathname, size_t len)
3847 zfs_cmd_t zc = { 0 };
3848 boolean_t mounted = B_FALSE;
3849 char *mntpnt = NULL;
3850 char dsname[MAXNAMELEN];
3853 /* special case for the MOS */
3854 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
3858 /* get the dataset's name */
3859 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3861 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3862 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3863 /* just write out a path of two object numbers */
3864 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3868 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3870 /* find out if the dataset is mounted */
3871 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3873 /* get the corrupted object's path */
3874 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3876 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3879 (void) snprintf(pathname, len, "%s%s", mntpnt,
3882 (void) snprintf(pathname, len, "%s:%s",
3883 dsname, zc.zc_value);
3886 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
3893 * Read the EFI label from the config, if a label does not exist then
3894 * pass back the error to the caller. If the caller has passed a non-NULL
3895 * diskaddr argument then we set it to the starting address of the EFI
3899 read_efi_label(nvlist_t *config, diskaddr_t *sb)
3903 char diskname[MAXPATHLEN];
3906 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3909 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
3910 strrchr(path, '/'));
3911 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
3912 struct dk_gpt *vtoc;
3914 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3916 *sb = vtoc->efi_parts[0].p_start;
3925 * determine where a partition starts on a disk in the current
3929 find_start_block(nvlist_t *config)
3933 diskaddr_t sb = MAXOFFSET_T;
3936 if (nvlist_lookup_nvlist_array(config,
3937 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3938 if (nvlist_lookup_uint64(config,
3939 ZPOOL_CONFIG_WHOLE_DISK,
3940 &wholedisk) != 0 || !wholedisk) {
3941 return (MAXOFFSET_T);
3943 if (read_efi_label(config, &sb) < 0)
3948 for (c = 0; c < children; c++) {
3949 sb = find_start_block(child[c]);
3950 if (sb != MAXOFFSET_T) {
3954 return (MAXOFFSET_T);
3959 * Label an individual disk. The name provided is the short name,
3960 * stripped of any leading /dev path.
3963 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name)
3966 char path[MAXPATHLEN];
3967 struct dk_gpt *vtoc;
3969 size_t resv = EFI_MIN_RESV_SIZE;
3970 uint64_t slice_size;
3971 diskaddr_t start_block;
3974 /* prepare an error message just in case */
3975 (void) snprintf(errbuf, sizeof (errbuf),
3976 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3981 if (zpool_is_bootable(zhp)) {
3982 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3983 "EFI labeled devices are not supported on root "
3985 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3988 verify(nvlist_lookup_nvlist(zhp->zpool_config,
3989 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3991 if (zhp->zpool_start_block == 0)
3992 start_block = find_start_block(nvroot);
3994 start_block = zhp->zpool_start_block;
3995 zhp->zpool_start_block = start_block;
3998 start_block = NEW_START_BLOCK;
4001 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
4004 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
4006 * This shouldn't happen. We've long since verified that this
4007 * is a valid device.
4010 dgettext(TEXT_DOMAIN, "unable to open device"));
4011 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
4014 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
4016 * The only way this can fail is if we run out of memory, or we
4017 * were unable to read the disk's capacity
4019 if (errno == ENOMEM)
4020 (void) no_memory(hdl);
4023 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4024 "unable to read disk capacity"), name);
4026 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
4029 slice_size = vtoc->efi_last_u_lba + 1;
4030 slice_size -= EFI_MIN_RESV_SIZE;
4031 if (start_block == MAXOFFSET_T)
4032 start_block = NEW_START_BLOCK;
4033 slice_size -= start_block;
4035 vtoc->efi_parts[0].p_start = start_block;
4036 vtoc->efi_parts[0].p_size = slice_size;
4039 * Why we use V_USR: V_BACKUP confuses users, and is considered
4040 * disposable by some EFI utilities (since EFI doesn't have a backup
4041 * slice). V_UNASSIGNED is supposed to be used only for zero size
4042 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4043 * etc. were all pretty specific. V_USR is as close to reality as we
4044 * can get, in the absence of V_OTHER.
4046 vtoc->efi_parts[0].p_tag = V_USR;
4047 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
4049 vtoc->efi_parts[8].p_start = slice_size + start_block;
4050 vtoc->efi_parts[8].p_size = resv;
4051 vtoc->efi_parts[8].p_tag = V_RESERVED;
4053 if (efi_write(fd, vtoc) != 0) {
4055 * Some block drivers (like pcata) may not support EFI
4056 * GPT labels. Print out a helpful error message dir-
4057 * ecting the user to manually label the disk and give
4063 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4064 "try using fdisk(1M) and then provide a specific slice"));
4065 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4075 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
4081 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
4082 if (strcmp(type, VDEV_TYPE_FILE) == 0 ||
4083 strcmp(type, VDEV_TYPE_HOLE) == 0 ||
4084 strcmp(type, VDEV_TYPE_MISSING) == 0) {
4085 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4086 "vdev type '%s' is not supported"), type);
4087 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
4090 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
4091 &child, &children) == 0) {
4092 for (c = 0; c < children; c++) {
4093 if (!supported_dump_vdev_type(hdl, child[c], errbuf))
4101 * Check if this zvol is allowable for use as a dump device; zero if
4102 * it is, > 0 if it isn't, < 0 if it isn't a zvol.
4104 * Allowable storage configurations include mirrors, all raidz variants, and
4105 * pools with log, cache, and spare devices. Pools which are backed by files or
4106 * have missing/hole vdevs are not suitable.
4109 zvol_check_dump_config(char *arg)
4111 zpool_handle_t *zhp = NULL;
4112 nvlist_t *config, *nvroot;
4116 libzfs_handle_t *hdl;
4118 char poolname[ZPOOL_MAXNAMELEN];
4119 int pathlen = strlen(ZVOL_FULL_DEV_DIR);
4122 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
4126 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4127 "dump is not supported on device '%s'"), arg);
4129 if ((hdl = libzfs_init()) == NULL)
4131 libzfs_print_on_error(hdl, B_TRUE);
4133 volname = arg + pathlen;
4135 /* check the configuration of the pool */
4136 if ((p = strchr(volname, '/')) == NULL) {
4137 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4138 "malformed dataset name"));
4139 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4141 } else if (p - volname >= ZFS_MAXNAMELEN) {
4142 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4143 "dataset name is too long"));
4144 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
4147 (void) strncpy(poolname, volname, p - volname);
4148 poolname[p - volname] = '\0';
4151 if ((zhp = zpool_open(hdl, poolname)) == NULL) {
4152 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4153 "could not open pool '%s'"), poolname);
4154 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4157 config = zpool_get_config(zhp, NULL);
4158 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4160 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4161 "could not obtain vdev configuration for '%s'"), poolname);
4162 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4166 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4167 &top, &toplevels) == 0);
4169 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {