4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
25 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
26 * Copyright 2016 Nexenta Systems, Inc.
27 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
28 * Copyright (c) 2017 Datto Inc.
31 #include <sys/types.h>
43 #include <sys/zfs_ioctl.h>
46 #include "zfs_namecheck.h"
48 #include "libzfs_impl.h"
49 #include "zfs_comutil.h"
50 #include "zfeature_common.h"
52 static int read_efi_label(nvlist_t *, diskaddr_t *, boolean_t *);
53 static boolean_t zpool_vdev_is_interior(const char *name);
55 #define BACKUP_SLICE "s2"
57 typedef struct prop_flags {
58 int create:1; /* Validate property on creation */
59 int import:1; /* Validate property on import */
63 * ====================================================================
64 * zpool property functions
65 * ====================================================================
69 zpool_get_all_props(zpool_handle_t *zhp)
72 libzfs_handle_t *hdl = zhp->zpool_hdl;
74 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
76 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
79 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
80 if (errno == ENOMEM) {
81 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
82 zcmd_free_nvlists(&zc);
86 zcmd_free_nvlists(&zc);
91 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
92 zcmd_free_nvlists(&zc);
96 zcmd_free_nvlists(&zc);
102 zpool_props_refresh(zpool_handle_t *zhp)
106 old_props = zhp->zpool_props;
108 if (zpool_get_all_props(zhp) != 0)
111 nvlist_free(old_props);
116 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
122 zprop_source_t source;
124 nvl = zhp->zpool_props;
125 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
126 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
128 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
130 source = ZPROP_SRC_DEFAULT;
131 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
142 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
146 zprop_source_t source;
148 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
150 * zpool_get_all_props() has most likely failed because
151 * the pool is faulted, but if all we need is the top level
152 * vdev's guid then get it from the zhp config nvlist.
154 if ((prop == ZPOOL_PROP_GUID) &&
155 (nvlist_lookup_nvlist(zhp->zpool_config,
156 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
157 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
161 return (zpool_prop_default_numeric(prop));
164 nvl = zhp->zpool_props;
165 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
166 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
168 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
170 source = ZPROP_SRC_DEFAULT;
171 value = zpool_prop_default_numeric(prop);
181 * Map VDEV STATE to printed strings.
184 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
187 case VDEV_STATE_CLOSED:
188 case VDEV_STATE_OFFLINE:
189 return (gettext("OFFLINE"));
190 case VDEV_STATE_REMOVED:
191 return (gettext("REMOVED"));
192 case VDEV_STATE_CANT_OPEN:
193 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
194 return (gettext("FAULTED"));
195 else if (aux == VDEV_AUX_SPLIT_POOL)
196 return (gettext("SPLIT"));
198 return (gettext("UNAVAIL"));
199 case VDEV_STATE_FAULTED:
200 return (gettext("FAULTED"));
201 case VDEV_STATE_DEGRADED:
202 return (gettext("DEGRADED"));
203 case VDEV_STATE_HEALTHY:
204 return (gettext("ONLINE"));
210 return (gettext("UNKNOWN"));
214 * Map POOL STATE to printed strings.
217 zpool_pool_state_to_name(pool_state_t state)
220 case POOL_STATE_ACTIVE:
221 return (gettext("ACTIVE"));
222 case POOL_STATE_EXPORTED:
223 return (gettext("EXPORTED"));
224 case POOL_STATE_DESTROYED:
225 return (gettext("DESTROYED"));
226 case POOL_STATE_SPARE:
227 return (gettext("SPARE"));
228 case POOL_STATE_L2CACHE:
229 return (gettext("L2CACHE"));
230 case POOL_STATE_UNINITIALIZED:
231 return (gettext("UNINITIALIZED"));
232 case POOL_STATE_UNAVAIL:
233 return (gettext("UNAVAIL"));
234 case POOL_STATE_POTENTIALLY_ACTIVE:
235 return (gettext("POTENTIALLY_ACTIVE"));
238 return (gettext("UNKNOWN"));
242 * Get a zpool property value for 'prop' and return the value in
243 * a pre-allocated buffer.
246 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
247 zprop_source_t *srctype, boolean_t literal)
251 zprop_source_t src = ZPROP_SRC_NONE;
256 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
258 case ZPOOL_PROP_NAME:
259 (void) strlcpy(buf, zpool_get_name(zhp), len);
262 case ZPOOL_PROP_HEALTH:
264 zpool_pool_state_to_name(POOL_STATE_UNAVAIL), len);
267 case ZPOOL_PROP_GUID:
268 intval = zpool_get_prop_int(zhp, prop, &src);
269 (void) snprintf(buf, len, "%llu", intval);
272 case ZPOOL_PROP_ALTROOT:
273 case ZPOOL_PROP_CACHEFILE:
274 case ZPOOL_PROP_COMMENT:
275 if (zhp->zpool_props != NULL ||
276 zpool_get_all_props(zhp) == 0) {
278 zpool_get_prop_string(zhp, prop, &src),
284 (void) strlcpy(buf, "-", len);
293 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
294 prop != ZPOOL_PROP_NAME)
297 switch (zpool_prop_get_type(prop)) {
298 case PROP_TYPE_STRING:
299 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
303 case PROP_TYPE_NUMBER:
304 intval = zpool_get_prop_int(zhp, prop, &src);
307 case ZPOOL_PROP_SIZE:
308 case ZPOOL_PROP_ALLOCATED:
309 case ZPOOL_PROP_FREE:
310 case ZPOOL_PROP_FREEING:
311 case ZPOOL_PROP_LEAKED:
313 (void) snprintf(buf, len, "%llu",
314 (u_longlong_t)intval);
316 (void) zfs_nicenum(intval, buf, len);
319 case ZPOOL_PROP_BOOTSIZE:
320 case ZPOOL_PROP_EXPANDSZ:
322 (void) strlcpy(buf, "-", len);
323 } else if (literal) {
324 (void) snprintf(buf, len, "%llu",
325 (u_longlong_t)intval);
327 (void) zfs_nicenum(intval, buf, len);
330 case ZPOOL_PROP_CAPACITY:
332 (void) snprintf(buf, len, "%llu",
333 (u_longlong_t)intval);
335 (void) snprintf(buf, len, "%llu%%",
336 (u_longlong_t)intval);
339 case ZPOOL_PROP_FRAGMENTATION:
340 if (intval == UINT64_MAX) {
341 (void) strlcpy(buf, "-", len);
343 (void) snprintf(buf, len, "%llu%%",
344 (u_longlong_t)intval);
347 case ZPOOL_PROP_DEDUPRATIO:
348 (void) snprintf(buf, len, "%llu.%02llux",
349 (u_longlong_t)(intval / 100),
350 (u_longlong_t)(intval % 100));
352 case ZPOOL_PROP_HEALTH:
353 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
354 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
355 verify(nvlist_lookup_uint64_array(nvroot,
356 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
359 (void) strlcpy(buf, zpool_state_to_name(intval,
362 case ZPOOL_PROP_VERSION:
363 if (intval >= SPA_VERSION_FEATURES) {
364 (void) snprintf(buf, len, "-");
369 (void) snprintf(buf, len, "%llu", intval);
373 case PROP_TYPE_INDEX:
374 intval = zpool_get_prop_int(zhp, prop, &src);
375 if (zpool_prop_index_to_string(prop, intval, &strval)
378 (void) strlcpy(buf, strval, len);
392 * Check if the bootfs name has the same pool name as it is set to.
393 * Assuming bootfs is a valid dataset name.
396 bootfs_name_valid(const char *pool, char *bootfs)
398 int len = strlen(pool);
400 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
403 if (strncmp(pool, bootfs, len) == 0 &&
404 (bootfs[len] == '/' || bootfs[len] == '\0'))
411 zpool_is_bootable(zpool_handle_t *zhp)
413 char bootfs[ZFS_MAX_DATASET_NAME_LEN];
415 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
416 sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-",
417 sizeof (bootfs)) != 0);
422 * Given an nvlist of zpool properties to be set, validate that they are
423 * correct, and parse any numeric properties (index, boolean, etc) if they are
424 * specified as strings.
427 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
428 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
436 struct stat64 statbuf;
439 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
440 (void) no_memory(hdl);
445 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
446 const char *propname = nvpair_name(elem);
448 prop = zpool_name_to_prop(propname);
449 if (prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname)) {
451 char *fname = strchr(propname, '@') + 1;
453 err = zfeature_lookup_name(fname, NULL);
455 ASSERT3U(err, ==, ENOENT);
456 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
457 "invalid feature '%s'"), fname);
458 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
462 if (nvpair_type(elem) != DATA_TYPE_STRING) {
463 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
464 "'%s' must be a string"), propname);
465 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
469 (void) nvpair_value_string(elem, &strval);
470 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) {
471 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
472 "property '%s' can only be set to "
473 "'enabled'"), propname);
474 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
478 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
479 (void) no_memory(hdl);
486 * Make sure this property is valid and applies to this type.
488 if (prop == ZPOOL_PROP_INVAL) {
489 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
490 "invalid property '%s'"), propname);
491 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
495 if (zpool_prop_readonly(prop)) {
496 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
497 "is readonly"), propname);
498 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
502 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
503 &strval, &intval, errbuf) != 0)
507 * Perform additional checking for specific properties.
510 case ZPOOL_PROP_VERSION:
511 if (intval < version ||
512 !SPA_VERSION_IS_SUPPORTED(intval)) {
513 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
514 "property '%s' number %d is invalid."),
516 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
521 case ZPOOL_PROP_BOOTSIZE:
523 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
524 "property '%s' can only be set during pool "
525 "creation"), propname);
526 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
531 case ZPOOL_PROP_BOOTFS:
532 if (flags.create || flags.import) {
533 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
534 "property '%s' cannot be set at creation "
535 "or import time"), propname);
536 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
540 if (version < SPA_VERSION_BOOTFS) {
541 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
542 "pool must be upgraded to support "
543 "'%s' property"), propname);
544 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
549 * bootfs property value has to be a dataset name and
550 * the dataset has to be in the same pool as it sets to.
552 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
554 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
555 "is an invalid name"), strval);
556 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
560 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
561 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
562 "could not open pool '%s'"), poolname);
563 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
569 case ZPOOL_PROP_ALTROOT:
570 if (!flags.create && !flags.import) {
571 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
572 "property '%s' can only be set during pool "
573 "creation or import"), propname);
574 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
578 if (strval[0] != '/') {
579 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
580 "bad alternate root '%s'"), strval);
581 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
586 case ZPOOL_PROP_CACHEFILE:
587 if (strval[0] == '\0')
590 if (strcmp(strval, "none") == 0)
593 if (strval[0] != '/') {
594 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
595 "property '%s' must be empty, an "
596 "absolute path, or 'none'"), propname);
597 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
601 slash = strrchr(strval, '/');
603 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
604 strcmp(slash, "/..") == 0) {
605 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
606 "'%s' is not a valid file"), strval);
607 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
613 if (strval[0] != '\0' &&
614 (stat64(strval, &statbuf) != 0 ||
615 !S_ISDIR(statbuf.st_mode))) {
616 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
617 "'%s' is not a valid directory"),
619 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
626 case ZPOOL_PROP_COMMENT:
627 for (check = strval; *check != '\0'; check++) {
628 if (!isprint(*check)) {
630 dgettext(TEXT_DOMAIN,
631 "comment may only have printable "
633 (void) zfs_error(hdl, EZFS_BADPROP,
638 if (strlen(strval) > ZPROP_MAX_COMMENT) {
639 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
640 "comment must not exceed %d characters"),
642 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
646 case ZPOOL_PROP_READONLY:
648 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
649 "property '%s' can only be set at "
650 "import time"), propname);
651 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
657 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
658 "property '%s'(%d) not defined"), propname, prop);
665 nvlist_free(retprops);
670 * Set zpool property : propname=propval.
673 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
675 zfs_cmd_t zc = { 0 };
678 nvlist_t *nvl = NULL;
681 prop_flags_t flags = { 0 };
683 (void) snprintf(errbuf, sizeof (errbuf),
684 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
687 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
688 return (no_memory(zhp->zpool_hdl));
690 if (nvlist_add_string(nvl, propname, propval) != 0) {
692 return (no_memory(zhp->zpool_hdl));
695 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
696 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
697 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
706 * Execute the corresponding ioctl() to set this property.
708 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
710 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
715 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
717 zcmd_free_nvlists(&zc);
721 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
723 (void) zpool_props_refresh(zhp);
729 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
731 libzfs_handle_t *hdl = zhp->zpool_hdl;
733 char buf[ZFS_MAXPROPLEN];
734 nvlist_t *features = NULL;
736 boolean_t firstexpand = (NULL == *plp);
738 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
742 while (*last != NULL)
743 last = &(*last)->pl_next;
746 features = zpool_get_features(zhp);
748 if ((*plp)->pl_all && firstexpand) {
749 for (int i = 0; i < SPA_FEATURES; i++) {
750 zprop_list_t *entry = zfs_alloc(hdl,
751 sizeof (zprop_list_t));
752 entry->pl_prop = ZPROP_INVAL;
753 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
754 spa_feature_table[i].fi_uname);
755 entry->pl_width = strlen(entry->pl_user_prop);
756 entry->pl_all = B_TRUE;
759 last = &entry->pl_next;
763 /* add any unsupported features */
764 for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL);
765 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
770 if (zfeature_is_supported(nvpair_name(nvp)))
773 propname = zfs_asprintf(hdl, "unsupported@%s",
777 * Before adding the property to the list make sure that no
778 * other pool already added the same property.
782 while (entry != NULL) {
783 if (entry->pl_user_prop != NULL &&
784 strcmp(propname, entry->pl_user_prop) == 0) {
788 entry = entry->pl_next;
795 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
796 entry->pl_prop = ZPROP_INVAL;
797 entry->pl_user_prop = propname;
798 entry->pl_width = strlen(entry->pl_user_prop);
799 entry->pl_all = B_TRUE;
802 last = &entry->pl_next;
805 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
810 if (entry->pl_prop != ZPROP_INVAL &&
811 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
812 NULL, B_FALSE) == 0) {
813 if (strlen(buf) > entry->pl_width)
814 entry->pl_width = strlen(buf);
822 * Get the state for the given feature on the given ZFS pool.
825 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
829 boolean_t found = B_FALSE;
830 nvlist_t *features = zpool_get_features(zhp);
832 const char *feature = strchr(propname, '@') + 1;
834 supported = zpool_prop_feature(propname);
835 ASSERT(supported || zpool_prop_unsupported(propname));
838 * Convert from feature name to feature guid. This conversion is
839 * unecessary for unsupported@... properties because they already
846 ret = zfeature_lookup_name(feature, &fid);
848 (void) strlcpy(buf, "-", len);
851 feature = spa_feature_table[fid].fi_guid;
854 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
859 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
862 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
864 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
869 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
871 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
874 (void) strlcpy(buf, "-", len);
883 * Don't start the slice at the default block of 34; many storage
884 * devices will use a stripe width of 128k, so start there instead.
886 #define NEW_START_BLOCK 256
889 * Validate the given pool name, optionally putting an extended error message in
893 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
899 ret = pool_namecheck(pool, &why, &what);
902 * The rules for reserved pool names were extended at a later point.
903 * But we need to support users with existing pools that may now be
904 * invalid. So we only check for this expanded set of names during a
905 * create (or import), and only in userland.
907 if (ret == 0 && !isopen &&
908 (strncmp(pool, "mirror", 6) == 0 ||
909 strncmp(pool, "raidz", 5) == 0 ||
910 strncmp(pool, "spare", 5) == 0 ||
911 strcmp(pool, "log") == 0)) {
914 dgettext(TEXT_DOMAIN, "name is reserved"));
922 case NAME_ERR_TOOLONG:
924 dgettext(TEXT_DOMAIN, "name is too long"));
927 case NAME_ERR_INVALCHAR:
929 dgettext(TEXT_DOMAIN, "invalid character "
930 "'%c' in pool name"), what);
933 case NAME_ERR_NOLETTER:
934 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
935 "name must begin with a letter"));
938 case NAME_ERR_RESERVED:
939 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
940 "name is reserved"));
943 case NAME_ERR_DISKLIKE:
944 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
945 "pool name is reserved"));
948 case NAME_ERR_LEADING_SLASH:
949 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
950 "leading slash in name"));
953 case NAME_ERR_EMPTY_COMPONENT:
954 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
955 "empty component in name"));
958 case NAME_ERR_TRAILING_SLASH:
959 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
960 "trailing slash in name"));
963 case NAME_ERR_MULTIPLE_DELIMITERS:
964 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
965 "multiple '@' and/or '#' delimiters in "
970 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
971 "(%d) not defined"), why);
982 * Open a handle to the given pool, even if the pool is currently in the FAULTED
986 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
992 * Make sure the pool name is valid.
994 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
995 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
996 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1001 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1004 zhp->zpool_hdl = hdl;
1005 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1007 if (zpool_refresh_stats(zhp, &missing) != 0) {
1013 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1014 (void) zfs_error_fmt(hdl, EZFS_NOENT,
1015 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1024 * Like the above, but silent on error. Used when iterating over pools (because
1025 * the configuration cache may be out of date).
1028 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1030 zpool_handle_t *zhp;
1033 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1036 zhp->zpool_hdl = hdl;
1037 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1039 if (zpool_refresh_stats(zhp, &missing) != 0) {
1055 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1059 zpool_open(libzfs_handle_t *hdl, const char *pool)
1061 zpool_handle_t *zhp;
1063 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1066 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1067 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1068 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1077 * Close the handle. Simply frees the memory associated with the handle.
1080 zpool_close(zpool_handle_t *zhp)
1082 nvlist_free(zhp->zpool_config);
1083 nvlist_free(zhp->zpool_old_config);
1084 nvlist_free(zhp->zpool_props);
1089 * Return the name of the pool.
1092 zpool_get_name(zpool_handle_t *zhp)
1094 return (zhp->zpool_name);
1099 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1102 zpool_get_state(zpool_handle_t *zhp)
1104 return (zhp->zpool_state);
1108 * Create the named pool, using the provided vdev list. It is assumed
1109 * that the consumer has already validated the contents of the nvlist, so we
1110 * don't have to worry about error semantics.
1113 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1114 nvlist_t *props, nvlist_t *fsprops)
1116 zfs_cmd_t zc = { 0 };
1117 nvlist_t *zc_fsprops = NULL;
1118 nvlist_t *zc_props = NULL;
1122 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1123 "cannot create '%s'"), pool);
1125 if (!zpool_name_valid(hdl, B_FALSE, pool))
1126 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1128 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1132 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1134 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1135 SPA_VERSION_1, flags, msg)) == NULL) {
1144 zoned = ((nvlist_lookup_string(fsprops,
1145 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1146 strcmp(zonestr, "on") == 0);
1148 if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
1149 fsprops, zoned, NULL, NULL, msg)) == NULL) {
1153 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1156 if (nvlist_add_nvlist(zc_props,
1157 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1162 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1165 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1167 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1169 zcmd_free_nvlists(&zc);
1170 nvlist_free(zc_props);
1171 nvlist_free(zc_fsprops);
1176 * This can happen if the user has specified the same
1177 * device multiple times. We can't reliably detect this
1178 * until we try to add it and see we already have a
1181 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1182 "one or more vdevs refer to the same device"));
1183 return (zfs_error(hdl, EZFS_BADDEV, msg));
1187 * This happens if the record size is smaller or larger
1188 * than the allowed size range, or not a power of 2.
1190 * NOTE: although zfs_valid_proplist is called earlier,
1191 * this case may have slipped through since the
1192 * pool does not exist yet and it is therefore
1193 * impossible to read properties e.g. max blocksize
1196 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1197 "record size invalid"));
1198 return (zfs_error(hdl, EZFS_BADPROP, msg));
1202 * This occurs when one of the devices is below
1203 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1204 * device was the problem device since there's no
1205 * reliable way to determine device size from userland.
1210 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1212 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1213 "one or more devices is less than the "
1214 "minimum size (%s)"), buf);
1216 return (zfs_error(hdl, EZFS_BADDEV, msg));
1219 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1220 "one or more devices is out of space"));
1221 return (zfs_error(hdl, EZFS_BADDEV, msg));
1224 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1225 "cache device must be a disk or disk slice"));
1226 return (zfs_error(hdl, EZFS_BADDEV, msg));
1229 return (zpool_standard_error(hdl, errno, msg));
1234 zcmd_free_nvlists(&zc);
1235 nvlist_free(zc_props);
1236 nvlist_free(zc_fsprops);
1241 * Destroy the given pool. It is up to the caller to ensure that there are no
1242 * datasets left in the pool.
1245 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1247 zfs_cmd_t zc = { 0 };
1248 zfs_handle_t *zfp = NULL;
1249 libzfs_handle_t *hdl = zhp->zpool_hdl;
1252 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1253 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1256 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1257 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1259 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1260 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1261 "cannot destroy '%s'"), zhp->zpool_name);
1263 if (errno == EROFS) {
1264 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1265 "one or more devices is read only"));
1266 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1268 (void) zpool_standard_error(hdl, errno, msg);
1277 remove_mountpoint(zfp);
1285 * Add the given vdevs to the pool. The caller must have already performed the
1286 * necessary verification to ensure that the vdev specification is well-formed.
1289 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1291 zfs_cmd_t zc = { 0 };
1293 libzfs_handle_t *hdl = zhp->zpool_hdl;
1295 nvlist_t **spares, **l2cache;
1296 uint_t nspares, nl2cache;
1298 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1299 "cannot add to '%s'"), zhp->zpool_name);
1301 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1302 SPA_VERSION_SPARES &&
1303 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1304 &spares, &nspares) == 0) {
1305 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1306 "upgraded to add hot spares"));
1307 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1310 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1311 SPA_VERSION_L2CACHE &&
1312 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1313 &l2cache, &nl2cache) == 0) {
1314 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1315 "upgraded to add cache devices"));
1316 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1319 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1321 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1323 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1327 * This can happen if the user has specified the same
1328 * device multiple times. We can't reliably detect this
1329 * until we try to add it and see we already have a
1332 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1333 "one or more vdevs refer to the same device"));
1334 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1338 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1339 "invalid config; a pool with removing/removed "
1340 "vdevs does not support adding raidz vdevs"));
1341 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1346 * This occurrs when one of the devices is below
1347 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1348 * device was the problem device since there's no
1349 * reliable way to determine device size from userland.
1354 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1356 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1357 "device is less than the minimum "
1360 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1364 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1365 "pool must be upgraded to add these vdevs"));
1366 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1370 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1371 "root pool can not have multiple vdevs"
1372 " or separate logs"));
1373 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1377 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1378 "cache device must be a disk or disk slice"));
1379 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1383 (void) zpool_standard_error(hdl, errno, msg);
1391 zcmd_free_nvlists(&zc);
1397 * Exports the pool from the system. The caller must ensure that there are no
1398 * mounted datasets in the pool.
1401 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1402 const char *log_str)
1404 zfs_cmd_t zc = { 0 };
1407 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1408 "cannot export '%s'"), zhp->zpool_name);
1410 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1411 zc.zc_cookie = force;
1412 zc.zc_guid = hardforce;
1413 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1415 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1418 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1419 "use '-f' to override the following errors:\n"
1420 "'%s' has an active shared spare which could be"
1421 " used by other pools once '%s' is exported."),
1422 zhp->zpool_name, zhp->zpool_name);
1423 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1426 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1435 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1437 return (zpool_export_common(zhp, force, B_FALSE, log_str));
1441 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1443 return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1447 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1450 nvlist_t *nv = NULL;
1456 if (!hdl->libzfs_printerr || config == NULL)
1459 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1460 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1464 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1466 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1468 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1469 strftime(timestr, 128, 0, &t) != 0) {
1471 (void) printf(dgettext(TEXT_DOMAIN,
1472 "Would be able to return %s "
1473 "to its state as of %s.\n"),
1476 (void) printf(dgettext(TEXT_DOMAIN,
1477 "Pool %s returned to its state as of %s.\n"),
1481 (void) printf(dgettext(TEXT_DOMAIN,
1482 "%s approximately %lld "),
1483 dryrun ? "Would discard" : "Discarded",
1485 (void) printf(dgettext(TEXT_DOMAIN,
1486 "minutes of transactions.\n"));
1487 } else if (loss > 0) {
1488 (void) printf(dgettext(TEXT_DOMAIN,
1489 "%s approximately %lld "),
1490 dryrun ? "Would discard" : "Discarded", loss);
1491 (void) printf(dgettext(TEXT_DOMAIN,
1492 "seconds of transactions.\n"));
1498 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1501 nvlist_t *nv = NULL;
1503 uint64_t edata = UINT64_MAX;
1508 if (!hdl->libzfs_printerr)
1512 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1514 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1516 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1517 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1518 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1519 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1522 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1523 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1526 (void) printf(dgettext(TEXT_DOMAIN,
1527 "Recovery is possible, but will result in some data loss.\n"));
1529 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1530 strftime(timestr, 128, 0, &t) != 0) {
1531 (void) printf(dgettext(TEXT_DOMAIN,
1532 "\tReturning the pool to its state as of %s\n"
1533 "\tshould correct the problem. "),
1536 (void) printf(dgettext(TEXT_DOMAIN,
1537 "\tReverting the pool to an earlier state "
1538 "should correct the problem.\n\t"));
1542 (void) printf(dgettext(TEXT_DOMAIN,
1543 "Approximately %lld minutes of data\n"
1544 "\tmust be discarded, irreversibly. "), (loss + 30) / 60);
1545 } else if (loss > 0) {
1546 (void) printf(dgettext(TEXT_DOMAIN,
1547 "Approximately %lld seconds of data\n"
1548 "\tmust be discarded, irreversibly. "), loss);
1550 if (edata != 0 && edata != UINT64_MAX) {
1552 (void) printf(dgettext(TEXT_DOMAIN,
1553 "After rewind, at least\n"
1554 "\tone persistent user-data error will remain. "));
1556 (void) printf(dgettext(TEXT_DOMAIN,
1557 "After rewind, several\n"
1558 "\tpersistent user-data errors will remain. "));
1561 (void) printf(dgettext(TEXT_DOMAIN,
1562 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1563 reason >= 0 ? "clear" : "import", name);
1565 (void) printf(dgettext(TEXT_DOMAIN,
1566 "A scrub of the pool\n"
1567 "\tis strongly recommended after recovery.\n"));
1571 (void) printf(dgettext(TEXT_DOMAIN,
1572 "Destroy and re-create the pool from\n\ta backup source.\n"));
1576 * zpool_import() is a contracted interface. Should be kept the same
1579 * Applications should use zpool_import_props() to import a pool with
1580 * new properties value to be set.
1583 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1586 nvlist_t *props = NULL;
1589 if (altroot != NULL) {
1590 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1591 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1592 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1596 if (nvlist_add_string(props,
1597 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1598 nvlist_add_string(props,
1599 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1601 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1602 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1607 ret = zpool_import_props(hdl, config, newname, props,
1614 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1620 uint64_t is_log = 0;
1622 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1626 (void) printf("\t%*s%s%s\n", indent, "", name,
1627 is_log ? " [log]" : "");
1629 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1630 &child, &children) != 0)
1633 for (c = 0; c < children; c++) {
1634 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
1635 print_vdev_tree(hdl, vname, child[c], indent + 2);
1641 zpool_print_unsup_feat(nvlist_t *config)
1643 nvlist_t *nvinfo, *unsup_feat;
1645 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1647 verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1650 for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1651 nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1654 verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1655 verify(nvpair_value_string(nvp, &desc) == 0);
1657 if (strlen(desc) > 0)
1658 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1660 (void) printf("\t%s\n", nvpair_name(nvp));
1665 * Import the given pool using the known configuration and a list of
1666 * properties to be set. The configuration should have come from
1667 * zpool_find_import(). The 'newname' parameters control whether the pool
1668 * is imported with a different name.
1671 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1672 nvlist_t *props, int flags)
1674 zfs_cmd_t zc = { 0 };
1675 zpool_rewind_policy_t policy;
1676 nvlist_t *nv = NULL;
1677 nvlist_t *nvinfo = NULL;
1678 nvlist_t *missing = NULL;
1685 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1688 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1689 "cannot import pool '%s'"), origname);
1691 if (newname != NULL) {
1692 if (!zpool_name_valid(hdl, B_FALSE, newname))
1693 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1694 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1696 thename = (char *)newname;
1701 if (props != NULL) {
1703 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1705 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1708 if ((props = zpool_valid_proplist(hdl, origname,
1709 props, version, flags, errbuf)) == NULL)
1711 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1718 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1720 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1723 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1724 zcmd_free_nvlists(&zc);
1727 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1728 zcmd_free_nvlists(&zc);
1732 zc.zc_cookie = flags;
1733 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1735 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1736 zcmd_free_nvlists(&zc);
1743 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1745 zcmd_free_nvlists(&zc);
1747 zpool_get_rewind_policy(config, &policy);
1753 * Dry-run failed, but we print out what success
1754 * looks like if we found a best txg
1756 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1757 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1763 if (newname == NULL)
1764 (void) snprintf(desc, sizeof (desc),
1765 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1768 (void) snprintf(desc, sizeof (desc),
1769 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1774 if (nv != NULL && nvlist_lookup_nvlist(nv,
1775 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1776 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1777 (void) printf(dgettext(TEXT_DOMAIN, "This "
1778 "pool uses the following feature(s) not "
1779 "supported by this system:\n"));
1780 zpool_print_unsup_feat(nv);
1781 if (nvlist_exists(nvinfo,
1782 ZPOOL_CONFIG_CAN_RDONLY)) {
1783 (void) printf(dgettext(TEXT_DOMAIN,
1784 "All unsupported features are only "
1785 "required for writing to the pool."
1786 "\nThe pool can be imported using "
1787 "'-o readonly=on'.\n"));
1791 * Unsupported version.
1793 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1797 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1801 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1802 "one or more devices is read only"));
1803 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1807 if (nv && nvlist_lookup_nvlist(nv,
1808 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1809 nvlist_lookup_nvlist(nvinfo,
1810 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1811 (void) printf(dgettext(TEXT_DOMAIN,
1812 "The devices below are missing, use "
1813 "'-m' to import the pool anyway:\n"));
1814 print_vdev_tree(hdl, NULL, missing, 2);
1815 (void) printf("\n");
1817 (void) zpool_standard_error(hdl, error, desc);
1821 (void) zpool_standard_error(hdl, error, desc);
1824 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1825 "new name of at least one dataset is longer than "
1826 "the maximum allowable length"));
1827 (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
1830 (void) zpool_standard_error(hdl, error, desc);
1831 zpool_explain_recover(hdl,
1832 newname ? origname : thename, -error, nv);
1839 zpool_handle_t *zhp;
1842 * This should never fail, but play it safe anyway.
1844 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1846 else if (zhp != NULL)
1848 if (policy.zrp_request &
1849 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1850 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1851 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1864 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd)
1866 zfs_cmd_t zc = { 0 };
1869 libzfs_handle_t *hdl = zhp->zpool_hdl;
1871 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1872 zc.zc_cookie = func;
1875 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0)
1880 /* ECANCELED on a scrub means we resumed a paused scrub */
1881 if (err == ECANCELED && func == POOL_SCAN_SCRUB &&
1882 cmd == POOL_SCRUB_NORMAL)
1885 if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL)
1888 if (func == POOL_SCAN_SCRUB) {
1889 if (cmd == POOL_SCRUB_PAUSE) {
1890 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1891 "cannot pause scrubbing %s"), zc.zc_name);
1893 assert(cmd == POOL_SCRUB_NORMAL);
1894 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1895 "cannot scrub %s"), zc.zc_name);
1897 } else if (func == POOL_SCAN_NONE) {
1898 (void) snprintf(msg, sizeof (msg),
1899 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1902 assert(!"unexpected result");
1907 pool_scan_stat_t *ps = NULL;
1910 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1911 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1912 (void) nvlist_lookup_uint64_array(nvroot,
1913 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1914 if (ps && ps->pss_func == POOL_SCAN_SCRUB) {
1915 if (cmd == POOL_SCRUB_PAUSE)
1916 return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg));
1918 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1920 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1922 } else if (err == ENOENT) {
1923 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1925 return (zpool_standard_error(hdl, err, msg));
1931 * This provides a very minimal check whether a given string is likely a
1932 * c#t#d# style string. Users of this are expected to do their own
1933 * verification of the s# part.
1935 #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1]))
1938 * More elaborate version for ones which may start with "/dev/dsk/"
1942 ctd_check_path(char *str)
1945 * If it starts with a slash, check the last component.
1947 if (str && str[0] == '/') {
1948 char *tmp = strrchr(str, '/');
1951 * If it ends in "/old", check the second-to-last
1952 * component of the string instead.
1954 if (tmp != str && strcmp(tmp, "/old") == 0) {
1955 for (tmp--; *tmp != '/'; tmp--)
1960 return (CTD_CHECK(str));
1965 * Find a vdev that matches the search criteria specified. We use the
1966 * the nvpair name to determine how we should look for the device.
1967 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1968 * spare; but FALSE if its an INUSE spare.
1971 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1972 boolean_t *l2cache, boolean_t *log)
1979 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1981 /* Nothing to look for */
1982 if (search == NULL || pair == NULL)
1985 /* Obtain the key we will use to search */
1986 srchkey = nvpair_name(pair);
1988 switch (nvpair_type(pair)) {
1989 case DATA_TYPE_UINT64:
1990 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1991 uint64_t srchval, theguid;
1993 verify(nvpair_value_uint64(pair, &srchval) == 0);
1994 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1996 if (theguid == srchval)
2001 case DATA_TYPE_STRING: {
2002 char *srchval, *val;
2004 verify(nvpair_value_string(pair, &srchval) == 0);
2005 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2009 * Search for the requested value. Special cases:
2011 * - ZPOOL_CONFIG_PATH for whole disk entries. To support
2012 * UEFI boot, these end in "s0" or "s0/old" or "s1" or
2013 * "s1/old". The "s0" or "s1" part is hidden from the user,
2014 * but included in the string, so this matches around it.
2015 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2017 * Otherwise, all other searches are simple string compares.
2020 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
2021 ctd_check_path(val)) {
2022 uint64_t wholedisk = 0;
2024 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2027 int slen = strlen(srchval);
2028 int vlen = strlen(val);
2030 if (slen != vlen - 2)
2034 * make_leaf_vdev() should only set
2035 * wholedisk for ZPOOL_CONFIG_PATHs which
2036 * will include "/dev/dsk/", giving plenty of
2037 * room for the indices used next.
2042 * strings identical except trailing "s0"
2044 if ((strcmp(&val[vlen - 2], "s0") == 0 ||
2045 strcmp(&val[vlen - 2], "s1") == 0) &&
2046 strncmp(srchval, val, slen) == 0)
2050 * strings identical except trailing "s0/old"
2052 if ((strcmp(&val[vlen - 6], "s0/old") == 0 ||
2053 strcmp(&val[vlen - 6], "s1/old") == 0) &&
2054 strcmp(&srchval[slen - 4], "/old") == 0 &&
2055 strncmp(srchval, val, slen - 4) == 0)
2060 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2062 if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2064 char *type, *idx, *end, *p;
2065 uint64_t id, vdev_id;
2068 * Determine our vdev type, keeping in mind
2069 * that the srchval is composed of a type and
2070 * vdev id pair (i.e. mirror-4).
2072 if ((type = strdup(srchval)) == NULL)
2075 if ((p = strrchr(type, '-')) == NULL) {
2083 * If the types don't match then keep looking.
2085 if (strncmp(val, type, strlen(val)) != 0) {
2090 verify(zpool_vdev_is_interior(type));
2091 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2095 vdev_id = strtoull(idx, &end, 10);
2102 * Now verify that we have the correct vdev id.
2111 if (strcmp(srchval, val) == 0)
2120 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2121 &child, &children) != 0)
2124 for (c = 0; c < children; c++) {
2125 if ((ret = vdev_to_nvlist_iter(child[c], search,
2126 avail_spare, l2cache, NULL)) != NULL) {
2128 * The 'is_log' value is only set for the toplevel
2129 * vdev, not the leaf vdevs. So we always lookup the
2130 * log device from the root of the vdev tree (where
2131 * 'log' is non-NULL).
2134 nvlist_lookup_uint64(child[c],
2135 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2143 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
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) {
2148 *avail_spare = B_TRUE;
2154 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2155 &child, &children) == 0) {
2156 for (c = 0; c < children; c++) {
2157 if ((ret = vdev_to_nvlist_iter(child[c], search,
2158 avail_spare, l2cache, NULL)) != NULL) {
2169 * Given a physical path (minus the "/devices" prefix), find the
2173 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2174 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2176 nvlist_t *search, *nvroot, *ret;
2178 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2179 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2181 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2184 *avail_spare = B_FALSE;
2188 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2189 nvlist_free(search);
2195 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2198 zpool_vdev_is_interior(const char *name)
2200 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2201 strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 ||
2203 VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 ||
2204 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2210 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2211 boolean_t *l2cache, boolean_t *log)
2213 char buf[MAXPATHLEN];
2215 nvlist_t *nvroot, *search, *ret;
2218 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2220 guid = strtoull(path, &end, 10);
2221 if (guid != 0 && *end == '\0') {
2222 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2223 } else if (zpool_vdev_is_interior(path)) {
2224 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2225 } else if (path[0] != '/') {
2226 (void) snprintf(buf, sizeof (buf), "%s%s", _PATH_DEV, path);
2227 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
2229 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2232 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2235 *avail_spare = B_FALSE;
2239 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2240 nvlist_free(search);
2246 vdev_online(nvlist_t *nv)
2250 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2251 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2252 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2259 * Helper function for zpool_get_physpaths().
2262 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2263 size_t *bytes_written)
2265 size_t bytes_left, pos, rsz;
2269 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2271 return (EZFS_NODEVICE);
2273 pos = *bytes_written;
2274 bytes_left = physpath_size - pos;
2275 format = (pos == 0) ? "%s" : " %s";
2277 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2278 *bytes_written += rsz;
2280 if (rsz >= bytes_left) {
2281 /* if physpath was not copied properly, clear it */
2282 if (bytes_left != 0) {
2285 return (EZFS_NOSPC);
2291 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2292 size_t *rsz, boolean_t is_spare)
2297 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2298 return (EZFS_INVALCONFIG);
2300 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2302 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2303 * For a spare vdev, we only want to boot from the active
2308 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2311 return (EZFS_INVALCONFIG);
2314 if (vdev_online(nv)) {
2315 if ((ret = vdev_get_one_physpath(nv, physpath,
2316 phypath_size, rsz)) != 0)
2319 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2320 strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
2321 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2322 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2327 if (nvlist_lookup_nvlist_array(nv,
2328 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2329 return (EZFS_INVALCONFIG);
2331 for (i = 0; i < count; i++) {
2332 ret = vdev_get_physpaths(child[i], physpath,
2333 phypath_size, rsz, is_spare);
2334 if (ret == EZFS_NOSPC)
2339 return (EZFS_POOL_INVALARG);
2343 * Get phys_path for a root pool config.
2344 * Return 0 on success; non-zero on failure.
2347 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2350 nvlist_t *vdev_root;
2357 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2359 return (EZFS_INVALCONFIG);
2361 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2362 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2363 &child, &count) != 0)
2364 return (EZFS_INVALCONFIG);
2367 * root pool can only have a single top-level vdev.
2369 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2370 return (EZFS_POOL_INVALARG);
2372 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2375 /* No online devices */
2377 return (EZFS_NODEVICE);
2383 * Get phys_path for a root pool
2384 * Return 0 on success; non-zero on failure.
2387 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2389 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2394 * If the device has being dynamically expanded then we need to relabel
2395 * the disk to use the new unallocated space.
2398 zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2401 char path[MAXPATHLEN];
2404 int (*_efi_use_whole_disk)(int);
2406 if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2407 "efi_use_whole_disk")) == NULL)
2410 (void) snprintf(path, sizeof (path), "%s/%s", ZFS_RDISK_ROOT, name);
2412 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2413 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2414 "relabel '%s': unable to open device"), name);
2415 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2419 * It's possible that we might encounter an error if the device
2420 * does not have any unallocated space left. If so, we simply
2421 * ignore that error and continue on.
2423 error = _efi_use_whole_disk(fd);
2425 if (error && error != VT_ENOSPC) {
2426 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2427 "relabel '%s': unable to read disk capacity"), name);
2428 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2430 #endif /* illumos */
2435 * Bring the specified vdev online. The 'flags' parameter is a set of the
2436 * ZFS_ONLINE_* flags.
2439 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2440 vdev_state_t *newstate)
2442 zfs_cmd_t zc = { 0 };
2446 boolean_t avail_spare, l2cache, islog;
2447 libzfs_handle_t *hdl = zhp->zpool_hdl;
2449 if (flags & ZFS_ONLINE_EXPAND) {
2450 (void) snprintf(msg, sizeof (msg),
2451 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2453 (void) snprintf(msg, sizeof (msg),
2454 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2457 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2458 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2460 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2462 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2465 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2467 if ((flags & ZFS_ONLINE_EXPAND ||
2468 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) &&
2469 nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) {
2470 uint64_t wholedisk = 0;
2472 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2476 * XXX - L2ARC 1.0 devices can't support expansion.
2479 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2480 "cannot expand cache devices"));
2481 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2485 pathname += strlen(ZFS_DISK_ROOT) + 1;
2486 (void) zpool_relabel_disk(hdl, pathname);
2490 zc.zc_cookie = VDEV_STATE_ONLINE;
2493 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2494 if (errno == EINVAL) {
2495 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2496 "from this pool into a new one. Use '%s' "
2497 "instead"), "zpool detach");
2498 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2500 return (zpool_standard_error(hdl, errno, msg));
2503 *newstate = zc.zc_cookie;
2508 * Take the specified vdev offline
2511 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2513 zfs_cmd_t zc = { 0 };
2516 boolean_t avail_spare, l2cache;
2517 libzfs_handle_t *hdl = zhp->zpool_hdl;
2519 (void) snprintf(msg, sizeof (msg),
2520 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2522 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2523 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2525 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2527 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2530 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2532 zc.zc_cookie = VDEV_STATE_OFFLINE;
2533 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2535 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2542 * There are no other replicas of this device.
2544 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2548 * The log device has unplayed logs
2550 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2553 return (zpool_standard_error(hdl, errno, msg));
2558 * Mark the given vdev faulted.
2561 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2563 zfs_cmd_t zc = { 0 };
2565 libzfs_handle_t *hdl = zhp->zpool_hdl;
2567 (void) snprintf(msg, sizeof (msg),
2568 dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2570 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2572 zc.zc_cookie = VDEV_STATE_FAULTED;
2575 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2582 * There are no other replicas of this device.
2584 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2587 return (zpool_standard_error(hdl, errno, msg));
2593 * Mark the given vdev degraded.
2596 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2598 zfs_cmd_t zc = { 0 };
2600 libzfs_handle_t *hdl = zhp->zpool_hdl;
2602 (void) snprintf(msg, sizeof (msg),
2603 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
2605 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2607 zc.zc_cookie = VDEV_STATE_DEGRADED;
2610 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2613 return (zpool_standard_error(hdl, errno, msg));
2617 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2621 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2627 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2629 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2632 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2633 children == 2 && child[which] == tgt)
2636 for (c = 0; c < children; c++)
2637 if (is_replacing_spare(child[c], tgt, which))
2645 * Attach new_disk (fully described by nvroot) to old_disk.
2646 * If 'replacing' is specified, the new disk will replace the old one.
2649 zpool_vdev_attach(zpool_handle_t *zhp,
2650 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2652 zfs_cmd_t zc = { 0 };
2656 boolean_t avail_spare, l2cache, islog;
2661 nvlist_t *config_root;
2662 libzfs_handle_t *hdl = zhp->zpool_hdl;
2663 boolean_t rootpool = zpool_is_bootable(zhp);
2666 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2667 "cannot replace %s with %s"), old_disk, new_disk);
2669 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2670 "cannot attach %s to %s"), new_disk, old_disk);
2672 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2673 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2675 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2678 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2681 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2683 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2684 zc.zc_cookie = replacing;
2686 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2687 &child, &children) != 0 || children != 1) {
2688 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2689 "new device must be a single disk"));
2690 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2693 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2694 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2696 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2700 * If the target is a hot spare that has been swapped in, we can only
2701 * replace it with another hot spare.
2704 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2705 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2706 NULL) == NULL || !avail_spare) &&
2707 is_replacing_spare(config_root, tgt, 1)) {
2708 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2709 "can only be replaced by another hot spare"));
2711 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2716 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2719 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2721 zcmd_free_nvlists(&zc);
2726 * XXX need a better way to prevent user from
2727 * booting up a half-baked vdev.
2729 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2730 "sure to wait until resilver is done "
2731 "before rebooting.\n"));
2732 (void) fprintf(stderr, "\n");
2733 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "If "
2734 "you boot from pool '%s', you may need to update\n"
2735 "boot code on newly attached disk '%s'.\n\n"
2736 "Assuming you use GPT partitioning and 'da0' is "
2737 "your new boot disk\n"
2738 "you may use the following command:\n\n"
2739 "\tgpart bootcode -b /boot/pmbr -p "
2740 "/boot/gptzfsboot -i 1 da0\n\n"),
2741 zhp->zpool_name, new_disk);
2749 * Can't attach to or replace this type of vdev.
2752 uint64_t version = zpool_get_prop_int(zhp,
2753 ZPOOL_PROP_VERSION, NULL);
2756 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2757 "cannot replace a log with a spare"));
2758 else if (version >= SPA_VERSION_MULTI_REPLACE)
2759 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2760 "already in replacing/spare config; wait "
2761 "for completion or use 'zpool detach'"));
2763 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2764 "cannot replace a replacing device"));
2766 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2767 "can only attach to mirrors and top-level "
2770 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2775 * The new device must be a single disk.
2777 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2778 "new device must be a single disk"));
2779 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2783 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy, "
2784 "or pool has removing/removed vdevs"),
2786 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2791 * The new device is too small.
2793 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2794 "device is too small"));
2795 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2800 * The new device has a different alignment requirement.
2802 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2803 "devices have different sector alignment"));
2804 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2809 * The resulting top-level vdev spec won't fit in the label.
2811 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2815 (void) zpool_standard_error(hdl, errno, msg);
2822 * Detach the specified device.
2825 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2827 zfs_cmd_t zc = { 0 };
2830 boolean_t avail_spare, l2cache;
2831 libzfs_handle_t *hdl = zhp->zpool_hdl;
2833 (void) snprintf(msg, sizeof (msg),
2834 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2836 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2837 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2839 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2842 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2845 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2847 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2849 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2856 * Can't detach from this type of vdev.
2858 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2859 "applicable to mirror and replacing vdevs"));
2860 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2865 * There are no other replicas of this device.
2867 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2871 (void) zpool_standard_error(hdl, errno, msg);
2878 * Find a mirror vdev in the source nvlist.
2880 * The mchild array contains a list of disks in one of the top-level mirrors
2881 * of the source pool. The schild array contains a list of disks that the
2882 * user specified on the command line. We loop over the mchild array to
2883 * see if any entry in the schild array matches.
2885 * If a disk in the mchild array is found in the schild array, we return
2886 * the index of that entry. Otherwise we return -1.
2889 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2890 nvlist_t **schild, uint_t schildren)
2894 for (mc = 0; mc < mchildren; mc++) {
2896 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2897 mchild[mc], B_FALSE);
2899 for (sc = 0; sc < schildren; sc++) {
2900 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2901 schild[sc], B_FALSE);
2902 boolean_t result = (strcmp(mpath, spath) == 0);
2918 * Split a mirror pool. If newroot points to null, then a new nvlist
2919 * is generated and it is the responsibility of the caller to free it.
2922 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2923 nvlist_t *props, splitflags_t flags)
2925 zfs_cmd_t zc = { 0 };
2927 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2928 nvlist_t **varray = NULL, *zc_props = NULL;
2929 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2930 libzfs_handle_t *hdl = zhp->zpool_hdl;
2932 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2935 (void) snprintf(msg, sizeof (msg),
2936 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2938 if (!zpool_name_valid(hdl, B_FALSE, newname))
2939 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2941 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2942 (void) fprintf(stderr, gettext("Internal error: unable to "
2943 "retrieve pool configuration\n"));
2947 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2949 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2952 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2953 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2954 props, vers, flags, msg)) == NULL)
2958 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2960 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2961 "Source pool is missing vdev tree"));
2962 nvlist_free(zc_props);
2966 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2969 if (*newroot == NULL ||
2970 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2971 &newchild, &newchildren) != 0)
2974 for (c = 0; c < children; c++) {
2975 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2977 nvlist_t **mchild, *vdev;
2982 * Unlike cache & spares, slogs are stored in the
2983 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2985 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2987 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2989 if (is_log || is_hole) {
2991 * Create a hole vdev and put it in the config.
2993 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2995 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2996 VDEV_TYPE_HOLE) != 0)
2998 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
3003 varray[vcount++] = vdev;
3007 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
3009 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
3010 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3011 "Source pool must be composed only of mirrors\n"));
3012 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3016 verify(nvlist_lookup_nvlist_array(child[c],
3017 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3019 /* find or add an entry for this top-level vdev */
3020 if (newchildren > 0 &&
3021 (entry = find_vdev_entry(zhp, mchild, mchildren,
3022 newchild, newchildren)) >= 0) {
3023 /* We found a disk that the user specified. */
3024 vdev = mchild[entry];
3027 /* User didn't specify a disk for this vdev. */
3028 vdev = mchild[mchildren - 1];
3031 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3035 /* did we find every disk the user specified? */
3036 if (found != newchildren) {
3037 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3038 "include at most one disk from each mirror"));
3039 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3043 /* Prepare the nvlist for populating. */
3044 if (*newroot == NULL) {
3045 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3048 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3049 VDEV_TYPE_ROOT) != 0)
3052 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3055 /* Add all the children we found */
3056 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3057 lastlog == 0 ? vcount : lastlog) != 0)
3061 * If we're just doing a dry run, exit now with success.
3064 memory_err = B_FALSE;
3069 /* now build up the config list & call the ioctl */
3070 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3073 if (nvlist_add_nvlist(newconfig,
3074 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3075 nvlist_add_string(newconfig,
3076 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3077 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3081 * The new pool is automatically part of the namespace unless we
3082 * explicitly export it.
3085 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3086 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3087 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3088 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3090 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3093 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3094 retval = zpool_standard_error(hdl, errno, msg);
3099 memory_err = B_FALSE;
3102 if (varray != NULL) {
3105 for (v = 0; v < vcount; v++)
3106 nvlist_free(varray[v]);
3109 zcmd_free_nvlists(&zc);
3110 nvlist_free(zc_props);
3111 nvlist_free(newconfig);
3113 nvlist_free(*newroot);
3121 return (no_memory(hdl));
3127 * Remove the given device.
3130 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3132 zfs_cmd_t zc = { 0 };
3135 boolean_t avail_spare, l2cache, islog;
3136 libzfs_handle_t *hdl = zhp->zpool_hdl;
3139 (void) snprintf(msg, sizeof (msg),
3140 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3142 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3143 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3145 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3147 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3148 if (islog && version < SPA_VERSION_HOLES) {
3149 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3150 "pool must be upgraded to support log removal"));
3151 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3154 if (!islog && !avail_spare && !l2cache && zpool_is_bootable(zhp)) {
3155 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3156 "root pool can not have removed devices, "
3157 "because GRUB does not understand them"));
3158 return (zfs_error(hdl, EINVAL, msg));
3161 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3163 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3169 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3170 "invalid config; all top-level vdevs must "
3171 "have the same sector size and not be raidz."));
3172 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3176 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3177 "Pool busy; removal may already be in progress"));
3178 (void) zfs_error(hdl, EZFS_BUSY, msg);
3182 (void) zpool_standard_error(hdl, errno, msg);
3188 zpool_vdev_remove_cancel(zpool_handle_t *zhp)
3190 zfs_cmd_t zc = { 0 };
3192 libzfs_handle_t *hdl = zhp->zpool_hdl;
3194 (void) snprintf(msg, sizeof (msg),
3195 dgettext(TEXT_DOMAIN, "cannot cancel removal"));
3197 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3200 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3203 return (zpool_standard_error(hdl, errno, msg));
3207 zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path,
3212 boolean_t avail_spare, l2cache, islog;
3213 libzfs_handle_t *hdl = zhp->zpool_hdl;
3215 (void) snprintf(msg, sizeof (msg),
3216 dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"),
3219 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3221 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3223 if (avail_spare || l2cache || islog) {
3228 if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) {
3229 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3230 "indirect size not available"));
3231 return (zfs_error(hdl, EINVAL, msg));
3237 * Clear the errors for the pool, or the particular device if specified.
3240 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3242 zfs_cmd_t zc = { 0 };
3245 zpool_rewind_policy_t policy;
3246 boolean_t avail_spare, l2cache;
3247 libzfs_handle_t *hdl = zhp->zpool_hdl;
3248 nvlist_t *nvi = NULL;
3252 (void) snprintf(msg, sizeof (msg),
3253 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3256 (void) snprintf(msg, sizeof (msg),
3257 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3260 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3262 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3263 &l2cache, NULL)) == NULL)
3264 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3267 * Don't allow error clearing for hot spares. Do allow
3268 * error clearing for l2cache devices.
3271 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3273 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3277 zpool_get_rewind_policy(rewindnvl, &policy);
3278 zc.zc_cookie = policy.zrp_request;
3280 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3283 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3286 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3288 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3289 zcmd_free_nvlists(&zc);
3294 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
3295 errno != EPERM && errno != EACCES)) {
3296 if (policy.zrp_request &
3297 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3298 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3299 zpool_rewind_exclaim(hdl, zc.zc_name,
3300 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
3304 zcmd_free_nvlists(&zc);
3308 zcmd_free_nvlists(&zc);
3309 return (zpool_standard_error(hdl, errno, msg));
3313 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3316 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3318 zfs_cmd_t zc = { 0 };
3320 libzfs_handle_t *hdl = zhp->zpool_hdl;
3322 (void) snprintf(msg, sizeof (msg),
3323 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3326 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3328 zc.zc_cookie = ZPOOL_NO_REWIND;
3330 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3333 return (zpool_standard_error(hdl, errno, msg));
3337 * Change the GUID for a pool.
3340 zpool_reguid(zpool_handle_t *zhp)
3343 libzfs_handle_t *hdl = zhp->zpool_hdl;
3344 zfs_cmd_t zc = { 0 };
3346 (void) snprintf(msg, sizeof (msg),
3347 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3349 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3350 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3353 return (zpool_standard_error(hdl, errno, msg));
3360 zpool_reopen(zpool_handle_t *zhp)
3362 zfs_cmd_t zc = { 0 };
3364 libzfs_handle_t *hdl = zhp->zpool_hdl;
3366 (void) snprintf(msg, sizeof (msg),
3367 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
3370 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3371 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
3373 return (zpool_standard_error(hdl, errno, msg));
3377 * Convert from a devid string to a path.
3380 devid_to_path(char *devid_str)
3385 devid_nmlist_t *list = NULL;
3388 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3391 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3393 devid_str_free(minor);
3400 * In a case the strdup() fails, we will just return NULL below.
3402 path = strdup(list[0].devname);
3404 devid_free_nmlist(list);
3410 * Convert from a path to a devid string.
3413 path_to_devid(const char *path)
3420 if ((fd = open(path, O_RDONLY)) < 0)
3425 if (devid_get(fd, &devid) == 0) {
3426 if (devid_get_minor_name(fd, &minor) == 0)
3427 ret = devid_str_encode(devid, minor);
3429 devid_str_free(minor);
3441 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3442 * ignore any failure here, since a common case is for an unprivileged user to
3443 * type 'zpool status', and we'll display the correct information anyway.
3446 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3448 zfs_cmd_t zc = { 0 };
3450 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3451 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3452 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3455 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3459 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3460 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3461 * We also check if this is a whole disk, in which case we strip off the
3462 * trailing 's0' slice name.
3464 * This routine is also responsible for identifying when disks have been
3465 * reconfigured in a new location. The kernel will have opened the device by
3466 * devid, but the path will still refer to the old location. To catch this, we
3467 * first do a path -> devid translation (which is fast for the common case). If
3468 * the devid matches, we're done. If not, we do a reverse devid -> path
3469 * translation and issue the appropriate ioctl() to update the path of the vdev.
3470 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3474 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3485 have_stats = nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3486 (uint64_t **)&vs, &vsc) == 0;
3487 have_path = nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0;
3490 * If the device is not currently present, assume it will not
3491 * come back at the same device path. Display the device by GUID.
3493 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3494 have_path && have_stats && vs->vs_state <= VDEV_STATE_CANT_OPEN) {
3495 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3497 (void) snprintf(buf, sizeof (buf), "%llu",
3498 (u_longlong_t)value);
3500 } else if (have_path) {
3503 * If the device is dead (faulted, offline, etc) then don't
3504 * bother opening it. Otherwise we may be forcing the user to
3505 * open a misbehaving device, which can have undesirable
3508 if ((have_stats == 0 ||
3509 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3511 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3513 * Determine if the current path is correct.
3515 char *newdevid = path_to_devid(path);
3517 if (newdevid == NULL ||
3518 strcmp(devid, newdevid) != 0) {
3521 if ((newpath = devid_to_path(devid)) != NULL) {
3523 * Update the path appropriately.
3525 set_path(zhp, nv, newpath);
3526 if (nvlist_add_string(nv,
3527 ZPOOL_CONFIG_PATH, newpath) == 0)
3528 verify(nvlist_lookup_string(nv,
3536 devid_str_free(newdevid);
3540 if (strncmp(path, ZFS_DISK_ROOTD, strlen(ZFS_DISK_ROOTD)) == 0)
3541 path += strlen(ZFS_DISK_ROOTD);
3543 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3544 &value) == 0 && value) {
3545 int pathlen = strlen(path);
3546 char *tmp = zfs_strdup(hdl, path);
3549 * If it starts with c#, and ends with "s0" or "s1",
3550 * chop the slice off, or if it ends with "s0/old" or
3551 * "s1/old", remove the slice from the middle.
3553 if (CTD_CHECK(tmp)) {
3554 if (strcmp(&tmp[pathlen - 2], "s0") == 0 ||
3555 strcmp(&tmp[pathlen - 2], "s1") == 0) {
3556 tmp[pathlen - 2] = '\0';
3557 } else if (pathlen > 6 &&
3558 (strcmp(&tmp[pathlen - 6], "s0/old") == 0 ||
3559 strcmp(&tmp[pathlen - 6], "s1/old") == 0)) {
3560 (void) strcpy(&tmp[pathlen - 6],
3566 #else /* !illumos */
3567 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3568 path += sizeof(_PATH_DEV) - 1;
3569 #endif /* illumos */
3571 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3574 * If it's a raidz device, we need to stick in the parity level.
3576 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3577 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3579 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3580 (u_longlong_t)value);
3585 * We identify each top-level vdev by using a <type-id>
3586 * naming convention.
3591 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3593 (void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3599 return (zfs_strdup(hdl, path));
3603 zbookmark_mem_compare(const void *a, const void *b)
3605 return (memcmp(a, b, sizeof (zbookmark_phys_t)));
3609 * Retrieve the persistent error log, uniquify the members, and return to the
3613 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3615 zfs_cmd_t zc = { 0 };
3617 zbookmark_phys_t *zb = NULL;
3621 * Retrieve the raw error list from the kernel. If the number of errors
3622 * has increased, allocate more space and continue until we get the
3625 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3629 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3630 count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL)
3632 zc.zc_nvlist_dst_size = count;
3633 (void) strcpy(zc.zc_name, zhp->zpool_name);
3635 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3637 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3638 if (errno == ENOMEM) {
3641 count = zc.zc_nvlist_dst_size;
3642 dst = zfs_alloc(zhp->zpool_hdl, count *
3643 sizeof (zbookmark_phys_t));
3646 zc.zc_nvlist_dst = (uintptr_t)dst;
3656 * Sort the resulting bookmarks. This is a little confusing due to the
3657 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3658 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3659 * _not_ copied as part of the process. So we point the start of our
3660 * array appropriate and decrement the total number of elements.
3662 zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
3663 zc.zc_nvlist_dst_size;
3664 count -= zc.zc_nvlist_dst_size;
3666 qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
3668 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3671 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3673 for (i = 0; i < count; i++) {
3676 /* ignoring zb_blkid and zb_level for now */
3677 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3678 zb[i-1].zb_object == zb[i].zb_object)
3681 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3683 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3684 zb[i].zb_objset) != 0) {
3688 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3689 zb[i].zb_object) != 0) {
3693 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3700 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3704 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3705 return (no_memory(zhp->zpool_hdl));
3709 * Upgrade a ZFS pool to the latest on-disk version.
3712 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3714 zfs_cmd_t zc = { 0 };
3715 libzfs_handle_t *hdl = zhp->zpool_hdl;
3717 (void) strcpy(zc.zc_name, zhp->zpool_name);
3718 zc.zc_cookie = new_version;
3720 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3721 return (zpool_standard_error_fmt(hdl, errno,
3722 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3728 zfs_save_arguments(int argc, char **argv, char *string, int len)
3730 (void) strlcpy(string, basename(argv[0]), len);
3731 for (int i = 1; i < argc; i++) {
3732 (void) strlcat(string, " ", len);
3733 (void) strlcat(string, argv[i], len);
3738 zpool_log_history(libzfs_handle_t *hdl, const char *message)
3740 zfs_cmd_t zc = { 0 };
3744 args = fnvlist_alloc();
3745 fnvlist_add_string(args, "message", message);
3746 err = zcmd_write_src_nvlist(hdl, &zc, args);
3748 err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
3750 zcmd_free_nvlists(&zc);
3755 * Perform ioctl to get some command history of a pool.
3757 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3758 * logical offset of the history buffer to start reading from.
3760 * Upon return, 'off' is the next logical offset to read from and
3761 * 'len' is the actual amount of bytes read into 'buf'.
3764 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3766 zfs_cmd_t zc = { 0 };
3767 libzfs_handle_t *hdl = zhp->zpool_hdl;
3769 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3771 zc.zc_history = (uint64_t)(uintptr_t)buf;
3772 zc.zc_history_len = *len;
3773 zc.zc_history_offset = *off;
3775 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3778 return (zfs_error_fmt(hdl, EZFS_PERM,
3779 dgettext(TEXT_DOMAIN,
3780 "cannot show history for pool '%s'"),
3783 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3784 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3785 "'%s'"), zhp->zpool_name));
3787 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3788 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3789 "'%s', pool must be upgraded"), zhp->zpool_name));
3791 return (zpool_standard_error_fmt(hdl, errno,
3792 dgettext(TEXT_DOMAIN,
3793 "cannot get history for '%s'"), zhp->zpool_name));
3797 *len = zc.zc_history_len;
3798 *off = zc.zc_history_offset;
3804 * Process the buffer of nvlists, unpacking and storing each nvlist record
3805 * into 'records'. 'leftover' is set to the number of bytes that weren't
3806 * processed as there wasn't a complete record.
3809 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3810 nvlist_t ***records, uint_t *numrecords)
3816 while (bytes_read > sizeof (reclen)) {
3818 /* get length of packed record (stored as little endian) */
3819 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3820 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3822 if (bytes_read < sizeof (reclen) + reclen)
3826 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3828 bytes_read -= sizeof (reclen) + reclen;
3829 buf += sizeof (reclen) + reclen;
3831 /* add record to nvlist array */
3833 if (ISP2(*numrecords + 1)) {
3834 *records = realloc(*records,
3835 *numrecords * 2 * sizeof (nvlist_t *));
3837 (*records)[*numrecords - 1] = nv;
3840 *leftover = bytes_read;
3844 /* from spa_history.c: spa_history_create_obj() */
3845 #define HIS_BUF_LEN_DEF (128 << 10)
3846 #define HIS_BUF_LEN_MAX (1 << 30)
3849 * Retrieve the command history of a pool.
3852 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3855 uint64_t buflen = HIS_BUF_LEN_DEF;
3857 nvlist_t **records = NULL;
3858 uint_t numrecords = 0;
3861 buf = malloc(buflen);
3865 uint64_t bytes_read = buflen;
3868 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3871 /* if nothing else was read in, we're at EOF, just return */
3872 if (bytes_read == 0)
3875 if ((err = zpool_history_unpack(buf, bytes_read,
3876 &leftover, &records, &numrecords)) != 0)
3879 if (leftover == bytes_read) {
3881 * no progress made, because buffer is not big enough
3882 * to hold this record; resize and retry.
3887 if ((buflen >= HIS_BUF_LEN_MAX) ||
3888 ((buf = malloc(buflen)) == NULL)) {
3900 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3901 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3902 records, numrecords) == 0);
3904 for (i = 0; i < numrecords; i++)
3905 nvlist_free(records[i]);
3912 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3913 char *pathname, size_t len)
3915 zfs_cmd_t zc = { 0 };
3916 boolean_t mounted = B_FALSE;
3917 char *mntpnt = NULL;
3918 char dsname[ZFS_MAX_DATASET_NAME_LEN];
3921 /* special case for the MOS */
3922 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
3926 /* get the dataset's name */
3927 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3929 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3930 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3931 /* just write out a path of two object numbers */
3932 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3936 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3938 /* find out if the dataset is mounted */
3939 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3941 /* get the corrupted object's path */
3942 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3944 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3947 (void) snprintf(pathname, len, "%s%s", mntpnt,
3950 (void) snprintf(pathname, len, "%s:%s",
3951 dsname, zc.zc_value);
3954 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
3961 * Read the EFI label from the config, if a label does not exist then
3962 * pass back the error to the caller. If the caller has passed a non-NULL
3963 * diskaddr argument then we set it to the starting address of the EFI
3964 * partition. If the caller has passed a non-NULL boolean argument, then
3965 * we set it to indicate if the disk does have efi system partition.
3968 read_efi_label(nvlist_t *config, diskaddr_t *sb, boolean_t *system)
3972 char diskname[MAXPATHLEN];
3973 boolean_t boot = B_FALSE;
3977 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3980 (void) snprintf(diskname, sizeof (diskname), "%s%s", ZFS_RDISK_ROOT,
3981 strrchr(path, '/'));
3982 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
3983 struct dk_gpt *vtoc;
3985 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3986 for (slice = 0; slice < vtoc->efi_nparts; slice++) {
3987 if (vtoc->efi_parts[slice].p_tag == V_SYSTEM)
3989 if (vtoc->efi_parts[slice].p_tag == V_USR)
3992 if (sb != NULL && vtoc->efi_parts[slice].p_tag == V_USR)
3993 *sb = vtoc->efi_parts[slice].p_start;
4004 * determine where a partition starts on a disk in the current
4008 find_start_block(nvlist_t *config)
4012 diskaddr_t sb = MAXOFFSET_T;
4015 if (nvlist_lookup_nvlist_array(config,
4016 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
4017 if (nvlist_lookup_uint64(config,
4018 ZPOOL_CONFIG_WHOLE_DISK,
4019 &wholedisk) != 0 || !wholedisk) {
4020 return (MAXOFFSET_T);
4022 if (read_efi_label(config, &sb, NULL) < 0)
4027 for (c = 0; c < children; c++) {
4028 sb = find_start_block(child[c]);
4029 if (sb != MAXOFFSET_T) {
4033 return (MAXOFFSET_T);
4035 #endif /* illumos */
4038 * Label an individual disk. The name provided is the short name,
4039 * stripped of any leading /dev path.
4042 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name,
4043 zpool_boot_label_t boot_type, uint64_t boot_size, int *slice)
4046 char path[MAXPATHLEN];
4047 struct dk_gpt *vtoc;
4049 size_t resv = EFI_MIN_RESV_SIZE;
4050 uint64_t slice_size;
4051 diskaddr_t start_block;
4054 /* prepare an error message just in case */
4055 (void) snprintf(errbuf, sizeof (errbuf),
4056 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
4061 verify(nvlist_lookup_nvlist(zhp->zpool_config,
4062 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4064 if (zhp->zpool_start_block == 0)
4065 start_block = find_start_block(nvroot);
4067 start_block = zhp->zpool_start_block;
4068 zhp->zpool_start_block = start_block;
4071 start_block = NEW_START_BLOCK;
4074 (void) snprintf(path, sizeof (path), "%s/%s%s", ZFS_RDISK_ROOT, name,
4077 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
4079 * This shouldn't happen. We've long since verified that this
4080 * is a valid device.
4083 dgettext(TEXT_DOMAIN, "unable to open device"));
4084 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
4087 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
4089 * The only way this can fail is if we run out of memory, or we
4090 * were unable to read the disk's capacity
4092 if (errno == ENOMEM)
4093 (void) no_memory(hdl);
4096 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4097 "unable to read disk capacity"), name);
4099 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
4103 * Why we use V_USR: V_BACKUP confuses users, and is considered
4104 * disposable by some EFI utilities (since EFI doesn't have a backup
4105 * slice). V_UNASSIGNED is supposed to be used only for zero size
4106 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4107 * etc. were all pretty specific. V_USR is as close to reality as we
4108 * can get, in the absence of V_OTHER.
4110 /* first fix the partition start block */
4111 if (start_block == MAXOFFSET_T)
4112 start_block = NEW_START_BLOCK;
4115 * EFI System partition is using slice 0.
4116 * ZFS is on slice 1 and slice 8 is reserved.
4117 * We assume the GPT partition table without system
4118 * partition has zfs p_start == NEW_START_BLOCK.
4119 * If start_block != NEW_START_BLOCK, it means we have
4120 * system partition. Correct solution would be to query/cache vtoc
4121 * from existing vdev member.
4123 if (boot_type == ZPOOL_CREATE_BOOT_LABEL) {
4124 if (boot_size % vtoc->efi_lbasize != 0) {
4125 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4126 "boot partition size must be a multiple of %d"),
4130 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4133 * System partition size checks.
4134 * Note the 1MB is quite arbitrary value, since we
4135 * are creating dedicated pool, it should be enough
4136 * to hold fat + efi bootloader. May need to be
4137 * adjusted if the bootloader size will grow.
4139 if (boot_size < 1024 * 1024) {
4141 zfs_nicenum(boot_size, buf, sizeof (buf));
4142 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4143 "Specified size %s for EFI System partition is too "
4144 "small, the minimum size is 1MB."), buf);
4147 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4149 /* 33MB is tested with mkfs -F pcfs */
4150 if (hdl->libzfs_printerr &&
4151 ((vtoc->efi_lbasize == 512 &&
4152 boot_size < 33 * 1024 * 1024) ||
4153 (vtoc->efi_lbasize == 4096 &&
4154 boot_size < 256 * 1024 * 1024))) {
4156 zfs_nicenum(boot_size, buf, sizeof (buf));
4157 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
4158 "Warning: EFI System partition size %s is "
4159 "not allowing to create FAT32 file\nsystem, which "
4160 "may result in unbootable system.\n"), buf);
4162 /* Adjust zfs partition start by size of system partition. */
4163 start_block += boot_size / vtoc->efi_lbasize;
4166 if (start_block == NEW_START_BLOCK) {
4168 * Use default layout.
4169 * ZFS is on slice 0 and slice 8 is reserved.
4171 slice_size = vtoc->efi_last_u_lba + 1;
4172 slice_size -= EFI_MIN_RESV_SIZE;
4173 slice_size -= start_block;
4177 vtoc->efi_parts[0].p_start = start_block;
4178 vtoc->efi_parts[0].p_size = slice_size;
4180 vtoc->efi_parts[0].p_tag = V_USR;
4181 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
4183 vtoc->efi_parts[8].p_start = slice_size + start_block;
4184 vtoc->efi_parts[8].p_size = resv;
4185 vtoc->efi_parts[8].p_tag = V_RESERVED;
4187 slice_size = start_block - NEW_START_BLOCK;
4188 vtoc->efi_parts[0].p_start = NEW_START_BLOCK;
4189 vtoc->efi_parts[0].p_size = slice_size;
4190 vtoc->efi_parts[0].p_tag = V_SYSTEM;
4191 (void) strcpy(vtoc->efi_parts[0].p_name, "loader");
4194 /* prepare slice 1 */
4195 slice_size = vtoc->efi_last_u_lba + 1 - slice_size;
4197 slice_size -= NEW_START_BLOCK;
4198 vtoc->efi_parts[1].p_start = start_block;
4199 vtoc->efi_parts[1].p_size = slice_size;
4200 vtoc->efi_parts[1].p_tag = V_USR;
4201 (void) strcpy(vtoc->efi_parts[1].p_name, "zfs");
4203 vtoc->efi_parts[8].p_start = slice_size + start_block;
4204 vtoc->efi_parts[8].p_size = resv;
4205 vtoc->efi_parts[8].p_tag = V_RESERVED;
4208 if (efi_write(fd, vtoc) != 0) {
4210 * Some block drivers (like pcata) may not support EFI
4211 * GPT labels. Print out a helpful error message dir-
4212 * ecting the user to manually label the disk and give
4218 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4219 "try using fdisk(1M) and then provide a specific slice"));
4220 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4225 #endif /* illumos */
4230 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
4236 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
4237 if (strcmp(type, VDEV_TYPE_FILE) == 0 ||
4238 strcmp(type, VDEV_TYPE_HOLE) == 0 ||
4239 strcmp(type, VDEV_TYPE_MISSING) == 0) {
4240 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4241 "vdev type '%s' is not supported"), type);
4242 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
4245 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
4246 &child, &children) == 0) {
4247 for (c = 0; c < children; c++) {
4248 if (!supported_dump_vdev_type(hdl, child[c], errbuf))
4256 * Check if this zvol is allowable for use as a dump device; zero if
4257 * it is, > 0 if it isn't, < 0 if it isn't a zvol.
4259 * Allowable storage configurations include mirrors, all raidz variants, and
4260 * pools with log, cache, and spare devices. Pools which are backed by files or
4261 * have missing/hole vdevs are not suitable.
4264 zvol_check_dump_config(char *arg)
4266 zpool_handle_t *zhp = NULL;
4267 nvlist_t *config, *nvroot;
4271 libzfs_handle_t *hdl;
4273 char poolname[ZFS_MAX_DATASET_NAME_LEN];
4274 int pathlen = strlen(ZVOL_FULL_DEV_DIR);
4277 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
4281 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4282 "dump is not supported on device '%s'"), arg);
4284 if ((hdl = libzfs_init()) == NULL)
4286 libzfs_print_on_error(hdl, B_TRUE);
4288 volname = arg + pathlen;
4290 /* check the configuration of the pool */
4291 if ((p = strchr(volname, '/')) == NULL) {
4292 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4293 "malformed dataset name"));
4294 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4296 } else if (p - volname >= ZFS_MAX_DATASET_NAME_LEN) {
4297 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4298 "dataset name is too long"));
4299 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
4302 (void) strncpy(poolname, volname, p - volname);
4303 poolname[p - volname] = '\0';
4306 if ((zhp = zpool_open(hdl, poolname)) == NULL) {
4307 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4308 "could not open pool '%s'"), poolname);
4309 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4312 config = zpool_get_config(zhp, NULL);
4313 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4315 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4316 "could not obtain vdev configuration for '%s'"), poolname);
4317 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4321 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4322 &top, &toplevels) == 0);
4324 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
4337 zpool_nextboot(libzfs_handle_t *hdl, uint64_t pool_guid, uint64_t dev_guid,
4338 const char *command)
4340 zfs_cmd_t zc = { 0 };
4346 args = fnvlist_alloc();
4347 fnvlist_add_uint64(args, ZPOOL_CONFIG_POOL_GUID, pool_guid);
4348 fnvlist_add_uint64(args, ZPOOL_CONFIG_GUID, dev_guid);
4349 fnvlist_add_string(args, "command", command);
4350 error = zcmd_write_src_nvlist(hdl, &zc, args);
4352 error = ioctl(hdl->libzfs_fd, ZFS_IOC_NEXTBOOT, &zc);
4353 zcmd_free_nvlists(&zc);