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) 2012 by Delphix. All rights reserved.
39 #include <sys/efi_partition.h>
41 #include <sys/zfs_ioctl.h>
44 #include "zfs_namecheck.h"
46 #include "libzfs_impl.h"
47 #include "zfs_comutil.h"
48 #include "zfeature_common.h"
50 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
52 typedef struct prop_flags {
53 int create:1; /* Validate property on creation */
54 int import:1; /* Validate property on import */
58 * ====================================================================
59 * zpool property functions
60 * ====================================================================
64 zpool_get_all_props(zpool_handle_t *zhp)
66 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
67 libzfs_handle_t *hdl = zhp->zpool_hdl;
69 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
71 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
74 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
75 if (errno == ENOMEM) {
76 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
77 zcmd_free_nvlists(&zc);
81 zcmd_free_nvlists(&zc);
86 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
87 zcmd_free_nvlists(&zc);
91 zcmd_free_nvlists(&zc);
97 zpool_props_refresh(zpool_handle_t *zhp)
101 old_props = zhp->zpool_props;
103 if (zpool_get_all_props(zhp) != 0)
106 nvlist_free(old_props);
111 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
117 zprop_source_t source;
119 nvl = zhp->zpool_props;
120 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
121 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
123 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
125 source = ZPROP_SRC_DEFAULT;
126 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
137 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
141 zprop_source_t source;
143 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
145 * zpool_get_all_props() has most likely failed because
146 * the pool is faulted, but if all we need is the top level
147 * vdev's guid then get it from the zhp config nvlist.
149 if ((prop == ZPOOL_PROP_GUID) &&
150 (nvlist_lookup_nvlist(zhp->zpool_config,
151 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
152 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
156 return (zpool_prop_default_numeric(prop));
159 nvl = zhp->zpool_props;
160 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
161 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
163 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
165 source = ZPROP_SRC_DEFAULT;
166 value = zpool_prop_default_numeric(prop);
176 * Map VDEV STATE to printed strings.
179 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)
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 * API compatibility wrapper around zpool_get_prop_literal
241 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
242 zprop_source_t *srctype)
244 return zpool_get_prop_literal(zhp, prop, buf, len, srctype, B_FALSE);
248 * Get a zpool property value for 'prop' and return the value in
249 * a pre-allocated buffer.
252 zpool_get_prop_literal(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
253 zprop_source_t *srctype, boolean_t literal)
257 zprop_source_t src = ZPROP_SRC_NONE;
262 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
264 case ZPOOL_PROP_NAME:
265 (void) strlcpy(buf, zpool_get_name(zhp), len);
268 case ZPOOL_PROP_HEALTH:
269 (void) strlcpy(buf, "FAULTED", len);
272 case ZPOOL_PROP_GUID:
273 intval = zpool_get_prop_int(zhp, prop, &src);
274 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
277 case ZPOOL_PROP_ALTROOT:
278 case ZPOOL_PROP_CACHEFILE:
279 case ZPOOL_PROP_COMMENT:
280 if (zhp->zpool_props != NULL ||
281 zpool_get_all_props(zhp) == 0) {
283 zpool_get_prop_string(zhp, prop, &src),
291 (void) strlcpy(buf, "-", len);
300 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
301 prop != ZPOOL_PROP_NAME)
304 switch (zpool_prop_get_type(prop)) {
305 case PROP_TYPE_STRING:
306 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
310 case PROP_TYPE_NUMBER:
311 intval = zpool_get_prop_int(zhp, prop, &src);
314 case ZPOOL_PROP_SIZE:
315 case ZPOOL_PROP_ALLOCATED:
316 case ZPOOL_PROP_FREE:
317 case ZPOOL_PROP_FREEING:
318 case ZPOOL_PROP_EXPANDSZ:
319 case ZPOOL_PROP_ASHIFT:
321 (void) snprintf(buf, len, "%llu",
322 (u_longlong_t)intval);
324 (void) zfs_nicenum(intval, buf, len);
327 case ZPOOL_PROP_CAPACITY:
328 (void) snprintf(buf, len, "%llu%%",
329 (u_longlong_t)intval);
332 case ZPOOL_PROP_DEDUPRATIO:
333 (void) snprintf(buf, len, "%llu.%02llux",
334 (u_longlong_t)(intval / 100),
335 (u_longlong_t)(intval % 100));
338 case ZPOOL_PROP_HEALTH:
339 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
340 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
341 verify(nvlist_lookup_uint64_array(nvroot,
342 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
345 (void) strlcpy(buf, zpool_state_to_name(intval,
348 case ZPOOL_PROP_VERSION:
349 if (intval >= SPA_VERSION_FEATURES) {
350 (void) snprintf(buf, len, "-");
355 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
359 case PROP_TYPE_INDEX:
360 intval = zpool_get_prop_int(zhp, prop, &src);
361 if (zpool_prop_index_to_string(prop, intval, &strval)
364 (void) strlcpy(buf, strval, len);
378 * Check if the bootfs name has the same pool name as it is set to.
379 * Assuming bootfs is a valid dataset name.
382 bootfs_name_valid(const char *pool, char *bootfs)
384 int len = strlen(pool);
386 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
389 if (strncmp(pool, bootfs, len) == 0 &&
390 (bootfs[len] == '/' || bootfs[len] == '\0'))
396 #if defined(__sun__) || defined(__sun)
398 * Inspect the configuration to determine if any of the devices contain
402 pool_uses_efi(nvlist_t *config)
407 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
408 &child, &children) != 0)
409 return (read_efi_label(config, NULL) >= 0);
411 for (c = 0; c < children; c++) {
412 if (pool_uses_efi(child[c]))
420 zpool_is_bootable(zpool_handle_t *zhp)
422 char bootfs[ZPOOL_MAXNAMELEN];
424 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
425 sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
426 sizeof (bootfs)) != 0);
431 * Given an nvlist of zpool properties to be set, validate that they are
432 * correct, and parse any numeric properties (index, boolean, etc) if they are
433 * specified as strings.
436 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
437 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
445 struct stat64 statbuf;
449 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
450 (void) no_memory(hdl);
455 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
456 const char *propname = nvpair_name(elem);
458 prop = zpool_name_to_prop(propname);
459 if (prop == ZPROP_INVAL && zpool_prop_feature(propname)) {
461 zfeature_info_t *feature;
462 char *fname = strchr(propname, '@') + 1;
464 err = zfeature_lookup_name(fname, &feature);
466 ASSERT3U(err, ==, ENOENT);
467 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
468 "invalid feature '%s'"), fname);
469 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
473 if (nvpair_type(elem) != DATA_TYPE_STRING) {
474 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
475 "'%s' must be a string"), propname);
476 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
480 (void) nvpair_value_string(elem, &strval);
481 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) {
482 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
483 "property '%s' can only be set to "
484 "'enabled'"), propname);
485 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
489 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
490 (void) no_memory(hdl);
497 * Make sure this property is valid and applies to this type.
499 if (prop == ZPROP_INVAL) {
500 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
501 "invalid property '%s'"), propname);
502 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
506 if (zpool_prop_readonly(prop)) {
507 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
508 "is readonly"), propname);
509 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
513 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
514 &strval, &intval, errbuf) != 0)
518 * Perform additional checking for specific properties.
523 case ZPOOL_PROP_VERSION:
524 if (intval < version ||
525 !SPA_VERSION_IS_SUPPORTED(intval)) {
526 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
527 "property '%s' number %d is invalid."),
529 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
534 case ZPOOL_PROP_ASHIFT:
536 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
537 "property '%s' can only be set at "
538 "creation time"), propname);
539 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
543 if (intval != 0 && (intval < 9 || intval > 13)) {
544 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
545 "property '%s' number %d is invalid."),
547 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
552 case ZPOOL_PROP_BOOTFS:
553 if (flags.create || flags.import) {
554 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
555 "property '%s' cannot be set at creation "
556 "or import time"), propname);
557 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
561 if (version < SPA_VERSION_BOOTFS) {
562 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
563 "pool must be upgraded to support "
564 "'%s' property"), propname);
565 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
570 * bootfs property value has to be a dataset name and
571 * the dataset has to be in the same pool as it sets to.
573 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
575 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
576 "is an invalid name"), strval);
577 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
581 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
582 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
583 "could not open pool '%s'"), poolname);
584 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
587 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
588 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
590 #if defined(__sun__) || defined(__sun)
592 * bootfs property cannot be set on a disk which has
595 if (pool_uses_efi(nvroot)) {
596 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
597 "property '%s' not supported on "
598 "EFI labeled devices"), propname);
599 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
607 case ZPOOL_PROP_ALTROOT:
608 if (!flags.create && !flags.import) {
609 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
610 "property '%s' can only be set during pool "
611 "creation or import"), propname);
612 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
616 if (strval[0] != '/') {
617 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
618 "bad alternate root '%s'"), strval);
619 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
624 case ZPOOL_PROP_CACHEFILE:
625 if (strval[0] == '\0')
628 if (strcmp(strval, "none") == 0)
631 if (strval[0] != '/') {
632 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
633 "property '%s' must be empty, an "
634 "absolute path, or 'none'"), propname);
635 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
639 slash = strrchr(strval, '/');
641 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
642 strcmp(slash, "/..") == 0) {
643 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
644 "'%s' is not a valid file"), strval);
645 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
651 if (strval[0] != '\0' &&
652 (stat64(strval, &statbuf) != 0 ||
653 !S_ISDIR(statbuf.st_mode))) {
654 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
655 "'%s' is not a valid directory"),
657 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
664 case ZPOOL_PROP_COMMENT:
665 for (check = strval; *check != '\0'; check++) {
666 if (!isprint(*check)) {
668 dgettext(TEXT_DOMAIN,
669 "comment may only have printable "
671 (void) zfs_error(hdl, EZFS_BADPROP,
676 if (strlen(strval) > ZPROP_MAX_COMMENT) {
677 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
678 "comment must not exceed %d characters"),
680 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
684 case ZPOOL_PROP_READONLY:
686 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
687 "property '%s' can only be set at "
688 "import time"), propname);
689 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
698 nvlist_free(retprops);
703 * Set zpool property : propname=propval.
706 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
708 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
711 nvlist_t *nvl = NULL;
714 prop_flags_t flags = { 0 };
716 (void) snprintf(errbuf, sizeof (errbuf),
717 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
720 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
721 return (no_memory(zhp->zpool_hdl));
723 if (nvlist_add_string(nvl, propname, propval) != 0) {
725 return (no_memory(zhp->zpool_hdl));
728 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
729 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
730 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
739 * Execute the corresponding ioctl() to set this property.
741 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
743 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
748 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
750 zcmd_free_nvlists(&zc);
754 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
756 (void) zpool_props_refresh(zhp);
762 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
764 libzfs_handle_t *hdl = zhp->zpool_hdl;
766 char buf[ZFS_MAXPROPLEN];
767 nvlist_t *features = NULL;
770 boolean_t firstexpand = (NULL == *plp);
773 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
777 while (*last != NULL)
778 last = &(*last)->pl_next;
781 features = zpool_get_features(zhp);
783 if ((*plp)->pl_all && firstexpand) {
784 for (i = 0; i < SPA_FEATURES; i++) {
785 zprop_list_t *entry = zfs_alloc(hdl,
786 sizeof (zprop_list_t));
787 entry->pl_prop = ZPROP_INVAL;
788 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
789 spa_feature_table[i].fi_uname);
790 entry->pl_width = strlen(entry->pl_user_prop);
791 entry->pl_all = B_TRUE;
794 last = &entry->pl_next;
798 /* add any unsupported features */
799 for (nvp = nvlist_next_nvpair(features, NULL);
800 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
805 if (zfeature_is_supported(nvpair_name(nvp)))
808 propname = zfs_asprintf(hdl, "unsupported@%s",
812 * Before adding the property to the list make sure that no
813 * other pool already added the same property.
817 while (entry != NULL) {
818 if (entry->pl_user_prop != NULL &&
819 strcmp(propname, entry->pl_user_prop) == 0) {
823 entry = entry->pl_next;
830 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
831 entry->pl_prop = ZPROP_INVAL;
832 entry->pl_user_prop = propname;
833 entry->pl_width = strlen(entry->pl_user_prop);
834 entry->pl_all = B_TRUE;
837 last = &entry->pl_next;
840 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
845 if (entry->pl_prop != ZPROP_INVAL &&
846 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
848 if (strlen(buf) > entry->pl_width)
849 entry->pl_width = strlen(buf);
857 * Get the state for the given feature on the given ZFS pool.
860 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
864 boolean_t found = B_FALSE;
865 nvlist_t *features = zpool_get_features(zhp);
867 const char *feature = strchr(propname, '@') + 1;
869 supported = zpool_prop_feature(propname);
870 ASSERT(supported || zpool_prop_unsupported(propname));
873 * Convert from feature name to feature guid. This conversion is
874 * unecessary for unsupported@... properties because they already
881 ret = zfeature_lookup_name(feature, &fi);
883 (void) strlcpy(buf, "-", len);
886 feature = fi->fi_guid;
889 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
894 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
897 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
899 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
904 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
906 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
909 (void) strlcpy(buf, "-", len);
918 * Don't start the slice at the default block of 34; many storage
919 * devices will use a stripe width of 128k, other vendors prefer a 1m
920 * alignment. It is best to play it safe and ensure a 1m alignment
921 * given 512B blocks. When the block size is larger by a power of 2
922 * we will still be 1m aligned. Some devices are sensitive to the
923 * partition ending alignment as well.
925 #define NEW_START_BLOCK 2048
926 #define PARTITION_END_ALIGNMENT 2048
929 * Validate the given pool name, optionally putting an extended error message in
933 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
939 ret = pool_namecheck(pool, &why, &what);
942 * The rules for reserved pool names were extended at a later point.
943 * But we need to support users with existing pools that may now be
944 * invalid. So we only check for this expanded set of names during a
945 * create (or import), and only in userland.
947 if (ret == 0 && !isopen &&
948 (strncmp(pool, "mirror", 6) == 0 ||
949 strncmp(pool, "raidz", 5) == 0 ||
950 strncmp(pool, "spare", 5) == 0 ||
951 strcmp(pool, "log") == 0)) {
954 dgettext(TEXT_DOMAIN, "name is reserved"));
962 case NAME_ERR_TOOLONG:
964 dgettext(TEXT_DOMAIN, "name is too long"));
967 case NAME_ERR_INVALCHAR:
969 dgettext(TEXT_DOMAIN, "invalid character "
970 "'%c' in pool name"), what);
973 case NAME_ERR_NOLETTER:
974 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
975 "name must begin with a letter"));
978 case NAME_ERR_RESERVED:
979 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
980 "name is reserved"));
983 case NAME_ERR_DISKLIKE:
984 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
985 "pool name is reserved"));
988 case NAME_ERR_LEADING_SLASH:
989 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
990 "leading slash in name"));
993 case NAME_ERR_EMPTY_COMPONENT:
994 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
995 "empty component in name"));
998 case NAME_ERR_TRAILING_SLASH:
999 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1000 "trailing slash in name"));
1003 case NAME_ERR_MULTIPLE_AT:
1004 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1005 "multiple '@' delimiters in name"));
1007 case NAME_ERR_NO_AT:
1008 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1009 "permission set is missing '@'"));
1020 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1024 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
1026 zpool_handle_t *zhp;
1030 * Make sure the pool name is valid.
1032 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
1033 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1034 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1039 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1042 zhp->zpool_hdl = hdl;
1043 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1045 if (zpool_refresh_stats(zhp, &missing) != 0) {
1051 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1052 (void) zfs_error_fmt(hdl, EZFS_NOENT,
1053 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1062 * Like the above, but silent on error. Used when iterating over pools (because
1063 * the configuration cache may be out of date).
1066 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1068 zpool_handle_t *zhp;
1071 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1074 zhp->zpool_hdl = hdl;
1075 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1077 if (zpool_refresh_stats(zhp, &missing) != 0) {
1093 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1097 zpool_open(libzfs_handle_t *hdl, const char *pool)
1099 zpool_handle_t *zhp;
1101 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1104 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1105 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1106 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1115 * Close the handle. Simply frees the memory associated with the handle.
1118 zpool_close(zpool_handle_t *zhp)
1120 if (zhp->zpool_config)
1121 nvlist_free(zhp->zpool_config);
1122 if (zhp->zpool_old_config)
1123 nvlist_free(zhp->zpool_old_config);
1124 if (zhp->zpool_props)
1125 nvlist_free(zhp->zpool_props);
1130 * Return the name of the pool.
1133 zpool_get_name(zpool_handle_t *zhp)
1135 return (zhp->zpool_name);
1140 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1143 zpool_get_state(zpool_handle_t *zhp)
1145 return (zhp->zpool_state);
1149 * Create the named pool, using the provided vdev list. It is assumed
1150 * that the consumer has already validated the contents of the nvlist, so we
1151 * don't have to worry about error semantics.
1154 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1155 nvlist_t *props, nvlist_t *fsprops)
1157 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1158 nvlist_t *zc_fsprops = NULL;
1159 nvlist_t *zc_props = NULL;
1164 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1165 "cannot create '%s'"), pool);
1167 if (!zpool_name_valid(hdl, B_FALSE, pool))
1168 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1170 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1174 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1176 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1177 SPA_VERSION_1, flags, msg)) == NULL) {
1186 zoned = ((nvlist_lookup_string(fsprops,
1187 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1188 strcmp(zonestr, "on") == 0);
1190 if ((zc_fsprops = zfs_valid_proplist(hdl,
1191 ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
1195 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1198 if (nvlist_add_nvlist(zc_props,
1199 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1204 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1207 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1209 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1211 zcmd_free_nvlists(&zc);
1212 nvlist_free(zc_props);
1213 nvlist_free(zc_fsprops);
1218 * This can happen if the user has specified the same
1219 * device multiple times. We can't reliably detect this
1220 * until we try to add it and see we already have a
1221 * label. This can also happen under if the device is
1222 * part of an active md or lvm device.
1224 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1225 "one or more vdevs refer to the same device, or one of\n"
1226 "the devices is part of an active md or lvm device"));
1227 return (zfs_error(hdl, EZFS_BADDEV, msg));
1231 * This occurs when one of the devices is below
1232 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1233 * device was the problem device since there's no
1234 * reliable way to determine device size from userland.
1239 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1241 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1242 "one or more devices is less than the "
1243 "minimum size (%s)"), buf);
1245 return (zfs_error(hdl, EZFS_BADDEV, msg));
1248 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1249 "one or more devices is out of space"));
1250 return (zfs_error(hdl, EZFS_BADDEV, msg));
1253 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1254 "cache device must be a disk or disk slice"));
1255 return (zfs_error(hdl, EZFS_BADDEV, msg));
1258 return (zpool_standard_error(hdl, errno, msg));
1263 * If this is an alternate root pool, then we automatically set the
1264 * mountpoint of the root dataset to be '/'.
1266 if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
1270 verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
1271 verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1278 zcmd_free_nvlists(&zc);
1279 nvlist_free(zc_props);
1280 nvlist_free(zc_fsprops);
1285 * Destroy the given pool. It is up to the caller to ensure that there are no
1286 * datasets left in the pool.
1289 zpool_destroy(zpool_handle_t *zhp)
1291 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1292 zfs_handle_t *zfp = NULL;
1293 libzfs_handle_t *hdl = zhp->zpool_hdl;
1296 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1297 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1300 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1302 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1303 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1304 "cannot destroy '%s'"), zhp->zpool_name);
1306 if (errno == EROFS) {
1307 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1308 "one or more devices is read only"));
1309 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1311 (void) zpool_standard_error(hdl, errno, msg);
1320 remove_mountpoint(zfp);
1328 * Add the given vdevs to the pool. The caller must have already performed the
1329 * necessary verification to ensure that the vdev specification is well-formed.
1332 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1334 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1336 libzfs_handle_t *hdl = zhp->zpool_hdl;
1338 nvlist_t **spares, **l2cache;
1339 uint_t nspares, nl2cache;
1341 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1342 "cannot add to '%s'"), zhp->zpool_name);
1344 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1345 SPA_VERSION_SPARES &&
1346 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1347 &spares, &nspares) == 0) {
1348 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1349 "upgraded to add hot spares"));
1350 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1353 #if defined(__sun__) || defined(__sun)
1354 if (zpool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1355 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1358 for (s = 0; s < nspares; s++) {
1361 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1362 &path) == 0 && pool_uses_efi(spares[s])) {
1363 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1364 "device '%s' contains an EFI label and "
1365 "cannot be used on root pools."),
1366 zpool_vdev_name(hdl, NULL, spares[s],
1368 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1374 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1375 SPA_VERSION_L2CACHE &&
1376 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1377 &l2cache, &nl2cache) == 0) {
1378 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1379 "upgraded to add cache devices"));
1380 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1383 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1385 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1387 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1391 * This can happen if the user has specified the same
1392 * device multiple times. We can't reliably detect this
1393 * until we try to add it and see we already have a
1396 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1397 "one or more vdevs refer to the same device"));
1398 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1403 * This occurrs when one of the devices is below
1404 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1405 * device was the problem device since there's no
1406 * reliable way to determine device size from userland.
1411 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1413 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1414 "device is less than the minimum "
1417 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1421 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1422 "pool must be upgraded to add these vdevs"));
1423 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1427 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1428 "root pool can not have multiple vdevs"
1429 " or separate logs"));
1430 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1434 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1435 "cache device must be a disk or disk slice"));
1436 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1440 (void) zpool_standard_error(hdl, errno, msg);
1448 zcmd_free_nvlists(&zc);
1454 * Exports the pool from the system. The caller must ensure that there are no
1455 * mounted datasets in the pool.
1458 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1460 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1463 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1464 "cannot export '%s'"), zhp->zpool_name);
1466 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1467 zc.zc_cookie = force;
1468 zc.zc_guid = hardforce;
1470 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1473 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1474 "use '-f' to override the following errors:\n"
1475 "'%s' has an active shared spare which could be"
1476 " used by other pools once '%s' is exported."),
1477 zhp->zpool_name, zhp->zpool_name);
1478 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1481 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1490 zpool_export(zpool_handle_t *zhp, boolean_t force)
1492 return (zpool_export_common(zhp, force, B_FALSE));
1496 zpool_export_force(zpool_handle_t *zhp)
1498 return (zpool_export_common(zhp, B_TRUE, B_TRUE));
1502 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1505 nvlist_t *nv = NULL;
1511 if (!hdl->libzfs_printerr || config == NULL)
1514 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1515 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1519 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1521 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1523 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1524 strftime(timestr, 128, "%c", &t) != 0) {
1526 (void) printf(dgettext(TEXT_DOMAIN,
1527 "Would be able to return %s "
1528 "to its state as of %s.\n"),
1531 (void) printf(dgettext(TEXT_DOMAIN,
1532 "Pool %s returned to its state as of %s.\n"),
1536 (void) printf(dgettext(TEXT_DOMAIN,
1537 "%s approximately %lld "),
1538 dryrun ? "Would discard" : "Discarded",
1539 ((longlong_t)loss + 30) / 60);
1540 (void) printf(dgettext(TEXT_DOMAIN,
1541 "minutes of transactions.\n"));
1542 } else if (loss > 0) {
1543 (void) printf(dgettext(TEXT_DOMAIN,
1544 "%s approximately %lld "),
1545 dryrun ? "Would discard" : "Discarded",
1547 (void) printf(dgettext(TEXT_DOMAIN,
1548 "seconds of transactions.\n"));
1554 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1557 nvlist_t *nv = NULL;
1559 uint64_t edata = UINT64_MAX;
1564 if (!hdl->libzfs_printerr)
1568 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1570 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1572 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1573 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1574 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1575 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1578 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1579 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1582 (void) printf(dgettext(TEXT_DOMAIN,
1583 "Recovery is possible, but will result in some data loss.\n"));
1585 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1586 strftime(timestr, 128, "%c", &t) != 0) {
1587 (void) printf(dgettext(TEXT_DOMAIN,
1588 "\tReturning the pool to its state as of %s\n"
1589 "\tshould correct the problem. "),
1592 (void) printf(dgettext(TEXT_DOMAIN,
1593 "\tReverting the pool to an earlier state "
1594 "should correct the problem.\n\t"));
1598 (void) printf(dgettext(TEXT_DOMAIN,
1599 "Approximately %lld minutes of data\n"
1600 "\tmust be discarded, irreversibly. "),
1601 ((longlong_t)loss + 30) / 60);
1602 } else if (loss > 0) {
1603 (void) printf(dgettext(TEXT_DOMAIN,
1604 "Approximately %lld seconds of data\n"
1605 "\tmust be discarded, irreversibly. "),
1608 if (edata != 0 && edata != UINT64_MAX) {
1610 (void) printf(dgettext(TEXT_DOMAIN,
1611 "After rewind, at least\n"
1612 "\tone persistent user-data error will remain. "));
1614 (void) printf(dgettext(TEXT_DOMAIN,
1615 "After rewind, several\n"
1616 "\tpersistent user-data errors will remain. "));
1619 (void) printf(dgettext(TEXT_DOMAIN,
1620 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1621 reason >= 0 ? "clear" : "import", name);
1623 (void) printf(dgettext(TEXT_DOMAIN,
1624 "A scrub of the pool\n"
1625 "\tis strongly recommended after recovery.\n"));
1629 (void) printf(dgettext(TEXT_DOMAIN,
1630 "Destroy and re-create the pool from\n\ta backup source.\n"));
1634 * zpool_import() is a contracted interface. Should be kept the same
1637 * Applications should use zpool_import_props() to import a pool with
1638 * new properties value to be set.
1641 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1644 nvlist_t *props = NULL;
1647 if (altroot != NULL) {
1648 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1649 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1650 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1654 if (nvlist_add_string(props,
1655 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1656 nvlist_add_string(props,
1657 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1659 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1660 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1665 ret = zpool_import_props(hdl, config, newname, props,
1673 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1679 uint64_t is_log = 0;
1681 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1685 (void) printf("\t%*s%s%s\n", indent, "", name,
1686 is_log ? " [log]" : "");
1688 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1689 &child, &children) != 0)
1692 for (c = 0; c < children; c++) {
1693 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
1694 print_vdev_tree(hdl, vname, child[c], indent + 2);
1700 zpool_print_unsup_feat(nvlist_t *config)
1702 nvlist_t *nvinfo, *unsup_feat;
1705 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1707 verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1710 for (nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1711 nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1714 verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1715 verify(nvpair_value_string(nvp, &desc) == 0);
1717 if (strlen(desc) > 0)
1718 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1720 (void) printf("\t%s\n", nvpair_name(nvp));
1725 * Import the given pool using the known configuration and a list of
1726 * properties to be set. The configuration should have come from
1727 * zpool_find_import(). The 'newname' parameters control whether the pool
1728 * is imported with a different name.
1731 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1732 nvlist_t *props, int flags)
1734 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1735 zpool_rewind_policy_t policy;
1736 nvlist_t *nv = NULL;
1737 nvlist_t *nvinfo = NULL;
1738 nvlist_t *missing = NULL;
1745 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1748 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1749 "cannot import pool '%s'"), origname);
1751 if (newname != NULL) {
1752 if (!zpool_name_valid(hdl, B_FALSE, newname))
1753 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1754 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1756 thename = (char *)newname;
1763 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1765 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1768 if ((props = zpool_valid_proplist(hdl, origname,
1769 props, version, flags, errbuf)) == NULL) {
1771 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1777 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1779 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1782 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1786 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1791 zc.zc_cookie = flags;
1792 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1794 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1795 zcmd_free_nvlists(&zc);
1802 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1803 zpool_get_rewind_policy(config, &policy);
1809 * Dry-run failed, but we print out what success
1810 * looks like if we found a best txg
1812 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1813 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1819 if (newname == NULL)
1820 (void) snprintf(desc, sizeof (desc),
1821 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1824 (void) snprintf(desc, sizeof (desc),
1825 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1830 if (nv != NULL && nvlist_lookup_nvlist(nv,
1831 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1832 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1833 (void) printf(dgettext(TEXT_DOMAIN, "This "
1834 "pool uses the following feature(s) not "
1835 "supported by this system:\n"));
1836 zpool_print_unsup_feat(nv);
1837 if (nvlist_exists(nvinfo,
1838 ZPOOL_CONFIG_CAN_RDONLY)) {
1839 (void) printf(dgettext(TEXT_DOMAIN,
1840 "All unsupported features are only "
1841 "required for writing to the pool."
1842 "\nThe pool can be imported using "
1843 "'-o readonly=on'.\n"));
1847 * Unsupported version.
1849 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1853 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1857 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1858 "one or more devices is read only"));
1859 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1863 if (nv && nvlist_lookup_nvlist(nv,
1864 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1865 nvlist_lookup_nvlist(nvinfo,
1866 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1867 (void) printf(dgettext(TEXT_DOMAIN,
1868 "The devices below are missing, use "
1869 "'-m' to import the pool anyway:\n"));
1870 print_vdev_tree(hdl, NULL, missing, 2);
1871 (void) printf("\n");
1873 (void) zpool_standard_error(hdl, error, desc);
1877 (void) zpool_standard_error(hdl, error, desc);
1881 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1882 "one or more devices are already in use\n"));
1883 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1887 (void) zpool_standard_error(hdl, error, desc);
1888 zpool_explain_recover(hdl,
1889 newname ? origname : thename, -error, nv);
1896 zpool_handle_t *zhp;
1899 * This should never fail, but play it safe anyway.
1901 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1903 else if (zhp != NULL)
1905 if (policy.zrp_request &
1906 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1907 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1908 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1914 zcmd_free_nvlists(&zc);
1924 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1926 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1928 libzfs_handle_t *hdl = zhp->zpool_hdl;
1930 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1931 zc.zc_cookie = func;
1933 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
1934 (errno == ENOENT && func != POOL_SCAN_NONE))
1937 if (func == POOL_SCAN_SCRUB) {
1938 (void) snprintf(msg, sizeof (msg),
1939 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1940 } else if (func == POOL_SCAN_NONE) {
1941 (void) snprintf(msg, sizeof (msg),
1942 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1945 assert(!"unexpected result");
1948 if (errno == EBUSY) {
1950 pool_scan_stat_t *ps = NULL;
1953 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1954 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1955 (void) nvlist_lookup_uint64_array(nvroot,
1956 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1957 if (ps && ps->pss_func == POOL_SCAN_SCRUB)
1958 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1960 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1961 } else if (errno == ENOENT) {
1962 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1964 return (zpool_standard_error(hdl, errno, msg));
1969 * Find a vdev that matches the search criteria specified. We use the
1970 * the nvpair name to determine how we should look for the device.
1971 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1972 * spare; but FALSE if its an INUSE spare.
1975 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1976 boolean_t *l2cache, boolean_t *log)
1983 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1985 /* Nothing to look for */
1986 if (search == NULL || pair == NULL)
1989 /* Obtain the key we will use to search */
1990 srchkey = nvpair_name(pair);
1992 switch (nvpair_type(pair)) {
1993 case DATA_TYPE_UINT64:
1994 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1995 uint64_t srchval, theguid;
1997 verify(nvpair_value_uint64(pair, &srchval) == 0);
1998 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2000 if (theguid == srchval)
2005 case DATA_TYPE_STRING: {
2006 char *srchval, *val;
2008 verify(nvpair_value_string(pair, &srchval) == 0);
2009 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2013 * Search for the requested value. Special cases:
2015 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2016 * "-part1", or "p1". The suffix is hidden from the user,
2017 * but included in the string, so this matches around it.
2018 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2019 * is used to check all possible expanded paths.
2020 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2022 * Otherwise, all other searches are simple string compares.
2024 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) {
2025 uint64_t wholedisk = 0;
2027 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2029 if (zfs_strcmp_pathname(srchval, val, wholedisk) == 0)
2032 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2033 char *type, *idx, *end, *p;
2034 uint64_t id, vdev_id;
2037 * Determine our vdev type, keeping in mind
2038 * that the srchval is composed of a type and
2039 * vdev id pair (i.e. mirror-4).
2041 if ((type = strdup(srchval)) == NULL)
2044 if ((p = strrchr(type, '-')) == NULL) {
2052 * If the types don't match then keep looking.
2054 if (strncmp(val, type, strlen(val)) != 0) {
2059 verify(strncmp(type, VDEV_TYPE_RAIDZ,
2060 strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2061 strncmp(type, VDEV_TYPE_MIRROR,
2062 strlen(VDEV_TYPE_MIRROR)) == 0);
2063 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2067 vdev_id = strtoull(idx, &end, 10);
2074 * Now verify that we have the correct vdev id.
2083 if (strcmp(srchval, val) == 0)
2092 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2093 &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) {
2100 * The 'is_log' value is only set for the toplevel
2101 * vdev, not the leaf vdevs. So we always lookup the
2102 * log device from the root of the vdev tree (where
2103 * 'log' is non-NULL).
2106 nvlist_lookup_uint64(child[c],
2107 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2115 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2116 &child, &children) == 0) {
2117 for (c = 0; c < children; c++) {
2118 if ((ret = vdev_to_nvlist_iter(child[c], search,
2119 avail_spare, l2cache, NULL)) != NULL) {
2120 *avail_spare = B_TRUE;
2126 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2127 &child, &children) == 0) {
2128 for (c = 0; c < children; c++) {
2129 if ((ret = vdev_to_nvlist_iter(child[c], search,
2130 avail_spare, l2cache, NULL)) != NULL) {
2141 * Given a physical path (minus the "/devices" prefix), find the
2145 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2146 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2148 nvlist_t *search, *nvroot, *ret;
2150 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2151 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2153 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2156 *avail_spare = B_FALSE;
2160 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2161 nvlist_free(search);
2167 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2170 zpool_vdev_is_interior(const char *name)
2172 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2173 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2179 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2180 boolean_t *l2cache, boolean_t *log)
2183 nvlist_t *nvroot, *search, *ret;
2186 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2188 guid = strtoull(path, &end, 10);
2189 if (guid != 0 && *end == '\0') {
2190 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2191 } else if (zpool_vdev_is_interior(path)) {
2192 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2194 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2197 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2200 *avail_spare = B_FALSE;
2204 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2205 nvlist_free(search);
2211 vdev_online(nvlist_t *nv)
2215 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2216 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2217 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2224 * Helper function for zpool_get_physpaths().
2227 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2228 size_t *bytes_written)
2230 size_t bytes_left, pos, rsz;
2234 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2236 return (EZFS_NODEVICE);
2238 pos = *bytes_written;
2239 bytes_left = physpath_size - pos;
2240 format = (pos == 0) ? "%s" : " %s";
2242 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2243 *bytes_written += rsz;
2245 if (rsz >= bytes_left) {
2246 /* if physpath was not copied properly, clear it */
2247 if (bytes_left != 0) {
2250 return (EZFS_NOSPC);
2256 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2257 size_t *rsz, boolean_t is_spare)
2262 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2263 return (EZFS_INVALCONFIG);
2265 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2267 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2268 * For a spare vdev, we only want to boot from the active
2273 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2276 return (EZFS_INVALCONFIG);
2279 if (vdev_online(nv)) {
2280 if ((ret = vdev_get_one_physpath(nv, physpath,
2281 phypath_size, rsz)) != 0)
2284 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2285 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2286 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2291 if (nvlist_lookup_nvlist_array(nv,
2292 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2293 return (EZFS_INVALCONFIG);
2295 for (i = 0; i < count; i++) {
2296 ret = vdev_get_physpaths(child[i], physpath,
2297 phypath_size, rsz, is_spare);
2298 if (ret == EZFS_NOSPC)
2303 return (EZFS_POOL_INVALARG);
2307 * Get phys_path for a root pool config.
2308 * Return 0 on success; non-zero on failure.
2311 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2314 nvlist_t *vdev_root;
2321 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2323 return (EZFS_INVALCONFIG);
2325 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2326 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2327 &child, &count) != 0)
2328 return (EZFS_INVALCONFIG);
2330 #if defined(__sun__) || defined(__sun)
2332 * root pool can not have EFI labeled disks and can only have
2333 * a single top-level vdev.
2335 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
2336 pool_uses_efi(vdev_root))
2337 return (EZFS_POOL_INVALARG);
2340 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2343 /* No online devices */
2345 return (EZFS_NODEVICE);
2351 * Get phys_path for a root pool
2352 * Return 0 on success; non-zero on failure.
2355 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2357 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2362 * If the device has being dynamically expanded then we need to relabel
2363 * the disk to use the new unallocated space.
2366 zpool_relabel_disk(libzfs_handle_t *hdl, const char *path, const char *msg)
2370 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
2371 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2372 "relabel '%s': unable to open device: %d"), path, errno);
2373 return (zfs_error(hdl, EZFS_OPENFAILED, msg));
2377 * It's possible that we might encounter an error if the device
2378 * does not have any unallocated space left. If so, we simply
2379 * ignore that error and continue on.
2381 * Also, we don't call efi_rescan() - that would just return EBUSY.
2382 * The module will do it for us in vdev_disk_open().
2384 error = efi_use_whole_disk(fd);
2386 if (error && error != VT_ENOSPC) {
2387 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2388 "relabel '%s': unable to read disk capacity"), path);
2389 return (zfs_error(hdl, EZFS_NOCAP, msg));
2395 * Bring the specified vdev online. The 'flags' parameter is a set of the
2396 * ZFS_ONLINE_* flags.
2399 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2400 vdev_state_t *newstate)
2402 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2405 boolean_t avail_spare, l2cache, islog;
2406 libzfs_handle_t *hdl = zhp->zpool_hdl;
2409 if (flags & ZFS_ONLINE_EXPAND) {
2410 (void) snprintf(msg, sizeof (msg),
2411 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2413 (void) snprintf(msg, sizeof (msg),
2414 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2417 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2418 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2420 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2422 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2425 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2427 if (flags & ZFS_ONLINE_EXPAND ||
2428 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2429 uint64_t wholedisk = 0;
2431 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2435 * XXX - L2ARC 1.0 devices can't support expansion.
2438 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2439 "cannot expand cache devices"));
2440 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2444 const char *fullpath = path;
2445 char buf[MAXPATHLEN];
2447 if (path[0] != '/') {
2448 error = zfs_resolve_shortname(path, buf,
2451 return (zfs_error(hdl, EZFS_NODEVICE,
2457 error = zpool_relabel_disk(hdl, fullpath, msg);
2463 zc.zc_cookie = VDEV_STATE_ONLINE;
2466 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2467 if (errno == EINVAL) {
2468 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2469 "from this pool into a new one. Use '%s' "
2470 "instead"), "zpool detach");
2471 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2473 return (zpool_standard_error(hdl, errno, msg));
2476 *newstate = zc.zc_cookie;
2481 * Take the specified vdev offline
2484 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2486 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2489 boolean_t avail_spare, l2cache;
2490 libzfs_handle_t *hdl = zhp->zpool_hdl;
2492 (void) snprintf(msg, sizeof (msg),
2493 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2495 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2496 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2498 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2500 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2503 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2505 zc.zc_cookie = VDEV_STATE_OFFLINE;
2506 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2508 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2515 * There are no other replicas of this device.
2517 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2521 * The log device has unplayed logs
2523 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2526 return (zpool_standard_error(hdl, errno, msg));
2531 * Mark the given vdev faulted.
2534 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2536 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2538 libzfs_handle_t *hdl = zhp->zpool_hdl;
2540 (void) snprintf(msg, sizeof (msg),
2541 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
2543 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2545 zc.zc_cookie = VDEV_STATE_FAULTED;
2548 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2555 * There are no other replicas of this device.
2557 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2560 return (zpool_standard_error(hdl, errno, msg));
2566 * Mark the given vdev degraded.
2569 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2571 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2573 libzfs_handle_t *hdl = zhp->zpool_hdl;
2575 (void) snprintf(msg, sizeof (msg),
2576 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid);
2578 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2580 zc.zc_cookie = VDEV_STATE_DEGRADED;
2583 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2586 return (zpool_standard_error(hdl, errno, msg));
2590 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2594 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2600 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2602 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2605 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2606 children == 2 && child[which] == tgt)
2609 for (c = 0; c < children; c++)
2610 if (is_replacing_spare(child[c], tgt, which))
2618 * Attach new_disk (fully described by nvroot) to old_disk.
2619 * If 'replacing' is specified, the new disk will replace the old one.
2622 zpool_vdev_attach(zpool_handle_t *zhp,
2623 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2625 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2629 boolean_t avail_spare, l2cache, islog;
2634 nvlist_t *config_root;
2635 libzfs_handle_t *hdl = zhp->zpool_hdl;
2636 boolean_t rootpool = zpool_is_bootable(zhp);
2639 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2640 "cannot replace %s with %s"), old_disk, new_disk);
2642 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2643 "cannot attach %s to %s"), new_disk, old_disk);
2645 #if defined(__sun__) || defined(__sun)
2647 * If this is a root pool, make sure that we're not attaching an
2648 * EFI labeled device.
2650 if (rootpool && pool_uses_efi(nvroot)) {
2651 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2652 "EFI labeled devices are not supported on root pools."));
2653 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2657 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2658 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2660 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2663 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2666 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2668 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2669 zc.zc_cookie = replacing;
2671 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2672 &child, &children) != 0 || children != 1) {
2673 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2674 "new device must be a single disk"));
2675 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2678 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2679 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2681 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2685 * If the target is a hot spare that has been swapped in, we can only
2686 * replace it with another hot spare.
2689 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2690 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2691 NULL) == NULL || !avail_spare) &&
2692 is_replacing_spare(config_root, tgt, 1)) {
2693 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2694 "can only be replaced by another hot spare"));
2696 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2701 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2704 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2706 zcmd_free_nvlists(&zc);
2711 * XXX need a better way to prevent user from
2712 * booting up a half-baked vdev.
2714 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2715 "sure to wait until resilver is done "
2716 "before rebooting.\n"));
2724 * Can't attach to or replace this type of vdev.
2727 uint64_t version = zpool_get_prop_int(zhp,
2728 ZPOOL_PROP_VERSION, NULL);
2731 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2732 "cannot replace a log with a spare"));
2733 else if (version >= SPA_VERSION_MULTI_REPLACE)
2734 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2735 "already in replacing/spare config; wait "
2736 "for completion or use 'zpool detach'"));
2738 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2739 "cannot replace a replacing device"));
2741 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2742 "can only attach to mirrors and top-level "
2745 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2750 * The new device must be a single disk.
2752 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2753 "new device must be a single disk"));
2754 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2758 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2760 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2765 * The new device is too small.
2767 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2768 "device is too small"));
2769 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2774 * The new device has a different alignment requirement.
2776 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2777 "devices have different sector alignment"));
2778 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2783 * The resulting top-level vdev spec won't fit in the label.
2785 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2789 (void) zpool_standard_error(hdl, errno, msg);
2796 * Detach the specified device.
2799 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2801 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2804 boolean_t avail_spare, l2cache;
2805 libzfs_handle_t *hdl = zhp->zpool_hdl;
2807 (void) snprintf(msg, sizeof (msg),
2808 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2810 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2811 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2813 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2816 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2819 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2821 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2823 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2830 * Can't detach from this type of vdev.
2832 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2833 "applicable to mirror and replacing vdevs"));
2834 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2839 * There are no other replicas of this device.
2841 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2845 (void) zpool_standard_error(hdl, errno, msg);
2852 * Find a mirror vdev in the source nvlist.
2854 * The mchild array contains a list of disks in one of the top-level mirrors
2855 * of the source pool. The schild array contains a list of disks that the
2856 * user specified on the command line. We loop over the mchild array to
2857 * see if any entry in the schild array matches.
2859 * If a disk in the mchild array is found in the schild array, we return
2860 * the index of that entry. Otherwise we return -1.
2863 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2864 nvlist_t **schild, uint_t schildren)
2868 for (mc = 0; mc < mchildren; mc++) {
2870 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2871 mchild[mc], B_FALSE);
2873 for (sc = 0; sc < schildren; sc++) {
2874 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2875 schild[sc], B_FALSE);
2876 boolean_t result = (strcmp(mpath, spath) == 0);
2892 * Split a mirror pool. If newroot points to null, then a new nvlist
2893 * is generated and it is the responsibility of the caller to free it.
2896 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2897 nvlist_t *props, splitflags_t flags)
2899 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2901 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2902 nvlist_t **varray = NULL, *zc_props = NULL;
2903 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2904 libzfs_handle_t *hdl = zhp->zpool_hdl;
2906 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2909 (void) snprintf(msg, sizeof (msg),
2910 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2912 if (!zpool_name_valid(hdl, B_FALSE, newname))
2913 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2915 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2916 (void) fprintf(stderr, gettext("Internal error: unable to "
2917 "retrieve pool configuration\n"));
2921 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2923 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2926 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2927 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2928 props, vers, flags, msg)) == NULL)
2932 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2934 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2935 "Source pool is missing vdev tree"));
2937 nvlist_free(zc_props);
2941 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2944 if (*newroot == NULL ||
2945 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2946 &newchild, &newchildren) != 0)
2949 for (c = 0; c < children; c++) {
2950 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2952 nvlist_t **mchild, *vdev;
2957 * Unlike cache & spares, slogs are stored in the
2958 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2960 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2962 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2964 if (is_log || is_hole) {
2966 * Create a hole vdev and put it in the config.
2968 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2970 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2971 VDEV_TYPE_HOLE) != 0)
2973 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2978 varray[vcount++] = vdev;
2982 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
2984 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2985 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2986 "Source pool must be composed only of mirrors\n"));
2987 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2991 verify(nvlist_lookup_nvlist_array(child[c],
2992 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2994 /* find or add an entry for this top-level vdev */
2995 if (newchildren > 0 &&
2996 (entry = find_vdev_entry(zhp, mchild, mchildren,
2997 newchild, newchildren)) >= 0) {
2998 /* We found a disk that the user specified. */
2999 vdev = mchild[entry];
3002 /* User didn't specify a disk for this vdev. */
3003 vdev = mchild[mchildren - 1];
3006 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3010 /* did we find every disk the user specified? */
3011 if (found != newchildren) {
3012 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3013 "include at most one disk from each mirror"));
3014 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3018 /* Prepare the nvlist for populating. */
3019 if (*newroot == NULL) {
3020 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3023 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3024 VDEV_TYPE_ROOT) != 0)
3027 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3030 /* Add all the children we found */
3031 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3032 lastlog == 0 ? vcount : lastlog) != 0)
3036 * If we're just doing a dry run, exit now with success.
3039 memory_err = B_FALSE;
3044 /* now build up the config list & call the ioctl */
3045 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3048 if (nvlist_add_nvlist(newconfig,
3049 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3050 nvlist_add_string(newconfig,
3051 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3052 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3056 * The new pool is automatically part of the namespace unless we
3057 * explicitly export it.
3060 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3061 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3062 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3063 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3065 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3068 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3069 retval = zpool_standard_error(hdl, errno, msg);
3074 memory_err = B_FALSE;
3077 if (varray != NULL) {
3080 for (v = 0; v < vcount; v++)
3081 nvlist_free(varray[v]);
3084 zcmd_free_nvlists(&zc);
3086 nvlist_free(zc_props);
3088 nvlist_free(newconfig);
3090 nvlist_free(*newroot);
3098 return (no_memory(hdl));
3104 * Remove the given device. Currently, this is supported only for hot spares
3105 * and level 2 cache devices.
3108 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3110 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3113 boolean_t avail_spare, l2cache, islog;
3114 libzfs_handle_t *hdl = zhp->zpool_hdl;
3117 (void) snprintf(msg, sizeof (msg),
3118 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3120 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3121 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3123 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3125 * XXX - this should just go away.
3127 if (!avail_spare && !l2cache && !islog) {
3128 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3129 "only inactive hot spares, cache, top-level, "
3130 "or log devices can be removed"));
3131 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3134 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3135 if (islog && version < SPA_VERSION_HOLES) {
3136 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3137 "pool must be upgrade to support log removal"));
3138 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3141 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3143 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3146 return (zpool_standard_error(hdl, errno, msg));
3150 * Clear the errors for the pool, or the particular device if specified.
3153 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3155 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3158 zpool_rewind_policy_t policy;
3159 boolean_t avail_spare, l2cache;
3160 libzfs_handle_t *hdl = zhp->zpool_hdl;
3161 nvlist_t *nvi = NULL;
3165 (void) snprintf(msg, sizeof (msg),
3166 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3169 (void) snprintf(msg, sizeof (msg),
3170 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3173 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3175 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3176 &l2cache, NULL)) == 0)
3177 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3180 * Don't allow error clearing for hot spares. Do allow
3181 * error clearing for l2cache devices.
3184 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3186 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3190 zpool_get_rewind_policy(rewindnvl, &policy);
3191 zc.zc_cookie = policy.zrp_request;
3193 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3196 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3199 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3201 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3202 zcmd_free_nvlists(&zc);
3207 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
3208 errno != EPERM && errno != EACCES)) {
3209 if (policy.zrp_request &
3210 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3211 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3212 zpool_rewind_exclaim(hdl, zc.zc_name,
3213 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
3217 zcmd_free_nvlists(&zc);
3221 zcmd_free_nvlists(&zc);
3222 return (zpool_standard_error(hdl, errno, msg));
3226 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3229 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3231 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3233 libzfs_handle_t *hdl = zhp->zpool_hdl;
3235 (void) snprintf(msg, sizeof (msg),
3236 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3237 (u_longlong_t)guid);
3239 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3241 zc.zc_cookie = ZPOOL_NO_REWIND;
3243 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3246 return (zpool_standard_error(hdl, errno, msg));
3250 * Change the GUID for a pool.
3253 zpool_reguid(zpool_handle_t *zhp)
3256 libzfs_handle_t *hdl = zhp->zpool_hdl;
3257 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3259 (void) snprintf(msg, sizeof (msg),
3260 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3262 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3263 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3266 return (zpool_standard_error(hdl, errno, msg));
3273 zpool_reopen(zpool_handle_t *zhp)
3275 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3277 libzfs_handle_t *hdl = zhp->zpool_hdl;
3279 (void) snprintf(msg, sizeof (msg),
3280 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
3283 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3284 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
3286 return (zpool_standard_error(hdl, errno, msg));
3290 * Convert from a devid string to a path.
3293 devid_to_path(char *devid_str)
3298 devid_nmlist_t *list = NULL;
3301 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3304 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3306 devid_str_free(minor);
3312 if ((path = strdup(list[0].devname)) == NULL)
3315 devid_free_nmlist(list);
3321 * Convert from a path to a devid string.
3324 path_to_devid(const char *path)
3330 if ((fd = open(path, O_RDONLY)) < 0)
3335 if (devid_get(fd, &devid) == 0) {
3336 if (devid_get_minor_name(fd, &minor) == 0)
3337 ret = devid_str_encode(devid, minor);
3339 devid_str_free(minor);
3348 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3349 * ignore any failure here, since a common case is for an unprivileged user to
3350 * type 'zpool status', and we'll display the correct information anyway.
3353 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3355 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3357 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3358 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3359 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3362 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3366 * Remove partition suffix from a vdev path. Partition suffixes may take three
3367 * forms: "-partX", "pX", or "X", where X is a string of digits. The second
3368 * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
3369 * third case only occurs when preceded by a string matching the regular
3370 * expression "^[hs]d[a-z]+", i.e. a scsi or ide disk.
3373 strip_partition(libzfs_handle_t *hdl, char *path)
3375 char *tmp = zfs_strdup(hdl, path);
3376 char *part = NULL, *d = NULL;
3378 if ((part = strstr(tmp, "-part")) && part != tmp) {
3380 } else if ((part = strrchr(tmp, 'p')) &&
3381 part > tmp + 1 && isdigit(*(part-1))) {
3383 } else if ((tmp[0] == 'h' || tmp[0] == 's') && tmp[1] == 'd') {
3384 for (d = &tmp[2]; isalpha(*d); part = ++d);
3386 if (part && d && *d != '\0') {
3387 for (; isdigit(*d); d++);
3394 #define PATH_BUF_LEN 64
3397 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3398 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3399 * We also check if this is a whole disk, in which case we strip off the
3400 * trailing 's0' slice name.
3402 * This routine is also responsible for identifying when disks have been
3403 * reconfigured in a new location. The kernel will have opened the device by
3404 * devid, but the path will still refer to the old location. To catch this, we
3405 * first do a path -> devid translation (which is fast for the common case). If
3406 * the devid matches, we're done. If not, we do a reverse devid -> path
3407 * translation and issue the appropriate ioctl() to update the path of the vdev.
3408 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3412 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3415 char *path, *devid, *type;
3417 char buf[PATH_BUF_LEN];
3418 char tmpbuf[PATH_BUF_LEN];
3422 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
3424 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3426 (void) snprintf(buf, sizeof (buf), "%llu",
3427 (u_longlong_t)value);
3429 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3431 * If the device is dead (faulted, offline, etc) then don't
3432 * bother opening it. Otherwise we may be forcing the user to
3433 * open a misbehaving device, which can have undesirable
3436 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3437 (uint64_t **)&vs, &vsc) != 0 ||
3438 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3440 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3442 * Determine if the current path is correct.
3444 char *newdevid = path_to_devid(path);
3446 if (newdevid == NULL ||
3447 strcmp(devid, newdevid) != 0) {
3450 if ((newpath = devid_to_path(devid)) != NULL) {
3452 * Update the path appropriately.
3454 set_path(zhp, nv, newpath);
3455 if (nvlist_add_string(nv,
3456 ZPOOL_CONFIG_PATH, newpath) == 0)
3457 verify(nvlist_lookup_string(nv,
3465 devid_str_free(newdevid);
3469 * For a block device only use the name.
3471 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
3472 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
3473 path = strrchr(path, '/');
3478 * Remove the partition from the path it this is a whole disk.
3480 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3481 &value) == 0 && value) {
3482 return strip_partition(hdl, path);
3485 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3488 * If it's a raidz device, we need to stick in the parity level.
3490 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3492 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3494 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3495 (u_longlong_t)value);
3500 * We identify each top-level vdev by using a <type-id>
3501 * naming convention.
3506 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3508 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s-%llu",
3509 path, (u_longlong_t)id);
3514 return (zfs_strdup(hdl, path));
3518 zbookmark_compare(const void *a, const void *b)
3520 return (memcmp(a, b, sizeof (zbookmark_t)));
3524 * Retrieve the persistent error log, uniquify the members, and return to the
3528 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3530 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3532 zbookmark_t *zb = NULL;
3536 * Retrieve the raw error list from the kernel. If the number of errors
3537 * has increased, allocate more space and continue until we get the
3540 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3544 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3545 count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
3547 zc.zc_nvlist_dst_size = count;
3548 (void) strcpy(zc.zc_name, zhp->zpool_name);
3550 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3552 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3553 if (errno == ENOMEM) {
3554 count = zc.zc_nvlist_dst_size;
3555 if ((zc.zc_nvlist_dst = (uintptr_t)
3556 zfs_alloc(zhp->zpool_hdl, count *
3557 sizeof (zbookmark_t))) == (uintptr_t)NULL)
3568 * Sort the resulting bookmarks. This is a little confusing due to the
3569 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3570 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3571 * _not_ copied as part of the process. So we point the start of our
3572 * array appropriate and decrement the total number of elements.
3574 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
3575 zc.zc_nvlist_dst_size;
3576 count -= zc.zc_nvlist_dst_size;
3578 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
3580 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3583 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3585 for (i = 0; i < count; i++) {
3588 /* ignoring zb_blkid and zb_level for now */
3589 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3590 zb[i-1].zb_object == zb[i].zb_object)
3593 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3595 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3596 zb[i].zb_objset) != 0) {
3600 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3601 zb[i].zb_object) != 0) {
3605 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3612 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3616 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3617 return (no_memory(zhp->zpool_hdl));
3621 * Upgrade a ZFS pool to the latest on-disk version.
3624 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3626 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3627 libzfs_handle_t *hdl = zhp->zpool_hdl;
3629 (void) strcpy(zc.zc_name, zhp->zpool_name);
3630 zc.zc_cookie = new_version;
3632 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3633 return (zpool_standard_error_fmt(hdl, errno,
3634 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3640 zpool_set_history_str(const char *subcommand, int argc, char **argv,
3645 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
3646 for (i = 1; i < argc; i++) {
3647 if (strlen(history_str) + 1 + strlen(argv[i]) >
3650 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
3651 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
3656 * Stage command history for logging.
3659 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
3661 if (history_str == NULL)
3664 if (strlen(history_str) > HIS_MAX_RECORD_LEN)
3667 if (hdl->libzfs_log_str != NULL)
3668 free(hdl->libzfs_log_str);
3670 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
3671 return (no_memory(hdl));
3677 * Perform ioctl to get some command history of a pool.
3679 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3680 * logical offset of the history buffer to start reading from.
3682 * Upon return, 'off' is the next logical offset to read from and
3683 * 'len' is the actual amount of bytes read into 'buf'.
3686 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3688 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3689 libzfs_handle_t *hdl = zhp->zpool_hdl;
3691 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3693 zc.zc_history = (uint64_t)(uintptr_t)buf;
3694 zc.zc_history_len = *len;
3695 zc.zc_history_offset = *off;
3697 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3700 return (zfs_error_fmt(hdl, EZFS_PERM,
3701 dgettext(TEXT_DOMAIN,
3702 "cannot show history for pool '%s'"),
3705 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3706 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3707 "'%s'"), zhp->zpool_name));
3709 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3710 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3711 "'%s', pool must be upgraded"), zhp->zpool_name));
3713 return (zpool_standard_error_fmt(hdl, errno,
3714 dgettext(TEXT_DOMAIN,
3715 "cannot get history for '%s'"), zhp->zpool_name));
3719 *len = zc.zc_history_len;
3720 *off = zc.zc_history_offset;
3726 * Process the buffer of nvlists, unpacking and storing each nvlist record
3727 * into 'records'. 'leftover' is set to the number of bytes that weren't
3728 * processed as there wasn't a complete record.
3731 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3732 nvlist_t ***records, uint_t *numrecords)
3738 while (bytes_read > sizeof (reclen)) {
3740 /* get length of packed record (stored as little endian) */
3741 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3742 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3744 if (bytes_read < sizeof (reclen) + reclen)
3748 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3750 bytes_read -= sizeof (reclen) + reclen;
3751 buf += sizeof (reclen) + reclen;
3753 /* add record to nvlist array */
3755 if (ISP2(*numrecords + 1)) {
3756 *records = realloc(*records,
3757 *numrecords * 2 * sizeof (nvlist_t *));
3759 (*records)[*numrecords - 1] = nv;
3762 *leftover = bytes_read;
3766 #define HIS_BUF_LEN (128*1024)
3769 * Retrieve the command history of a pool.
3772 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3774 char buf[HIS_BUF_LEN];
3776 nvlist_t **records = NULL;
3777 uint_t numrecords = 0;
3781 uint64_t bytes_read = sizeof (buf);
3784 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3787 /* if nothing else was read in, we're at EOF, just return */
3791 if ((err = zpool_history_unpack(buf, bytes_read,
3792 &leftover, &records, &numrecords)) != 0)
3800 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3801 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3802 records, numrecords) == 0);
3804 for (i = 0; i < numrecords; i++)
3805 nvlist_free(records[i]);
3812 * Retrieve the next event. If there is a new event available 'nvp' will
3813 * contain a newly allocated nvlist and 'dropped' will be set to the number
3814 * of missed events since the last call to this function. When 'nvp' is
3815 * set to NULL it indicates no new events are available. In either case
3816 * the function returns 0 and it is up to the caller to free 'nvp'. In
3817 * the case of a fatal error the function will return a non-zero value.
3818 * When the function is called in blocking mode it will not return until
3819 * a new event is available.
3822 zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
3823 int *dropped, int block, int cleanup_fd)
3825 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3830 zc.zc_cleanup_fd = cleanup_fd;
3833 zc.zc_guid = ZEVENT_NONBLOCK;
3835 if (zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE) != 0)
3839 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
3842 error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
3843 dgettext(TEXT_DOMAIN, "zfs shutdown"));
3846 /* Blocking error case should not occur */
3848 error = zpool_standard_error_fmt(hdl, errno,
3849 dgettext(TEXT_DOMAIN, "cannot get event"));
3853 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3854 error = zfs_error_fmt(hdl, EZFS_NOMEM,
3855 dgettext(TEXT_DOMAIN, "cannot get event"));
3861 error = zpool_standard_error_fmt(hdl, errno,
3862 dgettext(TEXT_DOMAIN, "cannot get event"));
3867 error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
3871 *dropped = (int)zc.zc_cookie;
3873 zcmd_free_nvlists(&zc);
3882 zpool_events_clear(libzfs_handle_t *hdl, int *count)
3884 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3887 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3888 "cannot clear events"));
3890 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
3891 return (zpool_standard_error_fmt(hdl, errno, msg));
3894 *count = (int)zc.zc_cookie; /* # of events cleared */
3900 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3901 char *pathname, size_t len)
3903 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3904 boolean_t mounted = B_FALSE;
3905 char *mntpnt = NULL;
3906 char dsname[MAXNAMELEN];
3909 /* special case for the MOS */
3910 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", (longlong_t)obj);
3914 /* get the dataset's name */
3915 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3917 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3918 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3919 /* just write out a path of two object numbers */
3920 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3921 (longlong_t)dsobj, (longlong_t)obj);
3924 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3926 /* find out if the dataset is mounted */
3927 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3929 /* get the corrupted object's path */
3930 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3932 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3935 (void) snprintf(pathname, len, "%s%s", mntpnt,
3938 (void) snprintf(pathname, len, "%s:%s",
3939 dsname, zc.zc_value);
3942 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, (longlong_t)obj);
3948 * Read the EFI label from the config, if a label does not exist then
3949 * pass back the error to the caller. If the caller has passed a non-NULL
3950 * diskaddr argument then we set it to the starting address of the EFI
3954 read_efi_label(nvlist_t *config, diskaddr_t *sb)
3958 char diskname[MAXPATHLEN];
3961 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3964 (void) snprintf(diskname, sizeof (diskname), "%s%s", DISK_ROOT,
3965 strrchr(path, '/'));
3966 if ((fd = open(diskname, O_RDWR|O_DIRECT)) >= 0) {
3967 struct dk_gpt *vtoc;
3969 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3971 *sb = vtoc->efi_parts[0].p_start;
3980 * determine where a partition starts on a disk in the current
3984 find_start_block(nvlist_t *config)
3988 diskaddr_t sb = MAXOFFSET_T;
3991 if (nvlist_lookup_nvlist_array(config,
3992 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3993 if (nvlist_lookup_uint64(config,
3994 ZPOOL_CONFIG_WHOLE_DISK,
3995 &wholedisk) != 0 || !wholedisk) {
3996 return (MAXOFFSET_T);
3998 if (read_efi_label(config, &sb) < 0)
4003 for (c = 0; c < children; c++) {
4004 sb = find_start_block(child[c]);
4005 if (sb != MAXOFFSET_T) {
4009 return (MAXOFFSET_T);
4013 zpool_label_disk_wait(char *path, int timeout)
4015 struct stat64 statbuf;
4019 * Wait timeout miliseconds for a newly created device to be available
4020 * from the given path. There is a small window when a /dev/ device
4021 * will exist and the udev link will not, so we must wait for the
4022 * symlink. Depending on the udev rules this may take a few seconds.
4024 for (i = 0; i < timeout; i++) {
4028 if ((stat64(path, &statbuf) == 0) && (errno == 0))
4036 zpool_label_disk_check(char *path)
4038 struct dk_gpt *vtoc;
4041 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0)
4044 if ((err = efi_alloc_and_read(fd, &vtoc)) != 0) {
4049 if (vtoc->efi_flags & EFI_GPT_PRIMARY_CORRUPT) {
4061 * Label an individual disk. The name provided is the short name,
4062 * stripped of any leading /dev path.
4065 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
4067 char path[MAXPATHLEN];
4068 struct dk_gpt *vtoc;
4070 size_t resv = EFI_MIN_RESV_SIZE;
4071 uint64_t slice_size;
4072 diskaddr_t start_block;
4075 /* prepare an error message just in case */
4076 (void) snprintf(errbuf, sizeof (errbuf),
4077 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
4082 #if defined(__sun__) || defined(__sun)
4083 if (zpool_is_bootable(zhp)) {
4084 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4085 "EFI labeled devices are not supported on root "
4087 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
4091 verify(nvlist_lookup_nvlist(zhp->zpool_config,
4092 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4094 if (zhp->zpool_start_block == 0)
4095 start_block = find_start_block(nvroot);
4097 start_block = zhp->zpool_start_block;
4098 zhp->zpool_start_block = start_block;
4101 start_block = NEW_START_BLOCK;
4104 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
4106 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
4108 * This shouldn't happen. We've long since verified that this
4109 * is a valid device.
4111 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
4112 "label '%s': unable to open device: %d"), path, errno);
4113 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
4116 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
4118 * The only way this can fail is if we run out of memory, or we
4119 * were unable to read the disk's capacity
4121 if (errno == ENOMEM)
4122 (void) no_memory(hdl);
4125 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
4126 "label '%s': unable to read disk capacity"), path);
4128 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
4131 slice_size = vtoc->efi_last_u_lba + 1;
4132 slice_size -= EFI_MIN_RESV_SIZE;
4133 if (start_block == MAXOFFSET_T)
4134 start_block = NEW_START_BLOCK;
4135 slice_size -= start_block;
4136 slice_size = P2ALIGN(slice_size, PARTITION_END_ALIGNMENT);
4138 vtoc->efi_parts[0].p_start = start_block;
4139 vtoc->efi_parts[0].p_size = slice_size;
4142 * Why we use V_USR: V_BACKUP confuses users, and is considered
4143 * disposable by some EFI utilities (since EFI doesn't have a backup
4144 * slice). V_UNASSIGNED is supposed to be used only for zero size
4145 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4146 * etc. were all pretty specific. V_USR is as close to reality as we
4147 * can get, in the absence of V_OTHER.
4149 vtoc->efi_parts[0].p_tag = V_USR;
4150 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
4152 vtoc->efi_parts[8].p_start = slice_size + start_block;
4153 vtoc->efi_parts[8].p_size = resv;
4154 vtoc->efi_parts[8].p_tag = V_RESERVED;
4156 if ((rval = efi_write(fd, vtoc)) != 0 || (rval = efi_rescan(fd)) != 0) {
4158 * Some block drivers (like pcata) may not support EFI
4159 * GPT labels. Print out a helpful error message dir-
4160 * ecting the user to manually label the disk and give
4166 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "try using "
4167 "parted(8) and then provide a specific slice: %d"), rval);
4168 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4174 /* Wait for the first expected partition to appear. */
4176 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
4177 (void) zfs_append_partition(path, MAXPATHLEN);
4179 rval = zpool_label_disk_wait(path, 3000);
4181 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "failed to "
4182 "detect device partitions on '%s': %d"), path, rval);
4183 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4186 /* We can't be to paranoid. Read the label back and verify it. */
4187 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
4188 rval = zpool_label_disk_check(path);
4190 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "freshly written "
4191 "EFI label on '%s' is damaged. Ensure\nthis device "
4192 "is not in in use, and is functioning properly: %d"),
4194 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));