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 or "
1813 "corrupted, use '-m' to import the pool "
1815 print_vdev_tree(hdl, NULL, missing, 2);
1816 (void) printf("\n");
1818 (void) zpool_standard_error(hdl, error, desc);
1822 (void) zpool_standard_error(hdl, error, desc);
1825 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1826 "new name of at least one dataset is longer than "
1827 "the maximum allowable length"));
1828 (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
1831 (void) zpool_standard_error(hdl, error, desc);
1832 zpool_explain_recover(hdl,
1833 newname ? origname : thename, -error, nv);
1840 zpool_handle_t *zhp;
1843 * This should never fail, but play it safe anyway.
1845 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1847 else if (zhp != NULL)
1849 if (policy.zrp_request &
1850 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1851 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1852 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1865 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd)
1867 zfs_cmd_t zc = { 0 };
1870 libzfs_handle_t *hdl = zhp->zpool_hdl;
1872 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1873 zc.zc_cookie = func;
1876 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0)
1881 /* ECANCELED on a scrub means we resumed a paused scrub */
1882 if (err == ECANCELED && func == POOL_SCAN_SCRUB &&
1883 cmd == POOL_SCRUB_NORMAL)
1886 if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL)
1889 if (func == POOL_SCAN_SCRUB) {
1890 if (cmd == POOL_SCRUB_PAUSE) {
1891 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1892 "cannot pause scrubbing %s"), zc.zc_name);
1894 assert(cmd == POOL_SCRUB_NORMAL);
1895 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1896 "cannot scrub %s"), zc.zc_name);
1898 } else if (func == POOL_SCAN_NONE) {
1899 (void) snprintf(msg, sizeof (msg),
1900 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1903 assert(!"unexpected result");
1908 pool_scan_stat_t *ps = NULL;
1911 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1912 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1913 (void) nvlist_lookup_uint64_array(nvroot,
1914 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1915 if (ps && ps->pss_func == POOL_SCAN_SCRUB) {
1916 if (cmd == POOL_SCRUB_PAUSE)
1917 return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg));
1919 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1921 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1923 } else if (err == ENOENT) {
1924 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1926 return (zpool_standard_error(hdl, err, msg));
1932 * This provides a very minimal check whether a given string is likely a
1933 * c#t#d# style string. Users of this are expected to do their own
1934 * verification of the s# part.
1936 #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1]))
1939 * More elaborate version for ones which may start with "/dev/dsk/"
1943 ctd_check_path(char *str)
1946 * If it starts with a slash, check the last component.
1948 if (str && str[0] == '/') {
1949 char *tmp = strrchr(str, '/');
1952 * If it ends in "/old", check the second-to-last
1953 * component of the string instead.
1955 if (tmp != str && strcmp(tmp, "/old") == 0) {
1956 for (tmp--; *tmp != '/'; tmp--)
1961 return (CTD_CHECK(str));
1966 * Find a vdev that matches the search criteria specified. We use the
1967 * the nvpair name to determine how we should look for the device.
1968 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1969 * spare; but FALSE if its an INUSE spare.
1972 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1973 boolean_t *l2cache, boolean_t *log)
1980 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1982 /* Nothing to look for */
1983 if (search == NULL || pair == NULL)
1986 /* Obtain the key we will use to search */
1987 srchkey = nvpair_name(pair);
1989 switch (nvpair_type(pair)) {
1990 case DATA_TYPE_UINT64:
1991 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1992 uint64_t srchval, theguid;
1994 verify(nvpair_value_uint64(pair, &srchval) == 0);
1995 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1997 if (theguid == srchval)
2002 case DATA_TYPE_STRING: {
2003 char *srchval, *val;
2005 verify(nvpair_value_string(pair, &srchval) == 0);
2006 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2010 * Search for the requested value. Special cases:
2012 * - ZPOOL_CONFIG_PATH for whole disk entries. To support
2013 * UEFI boot, these end in "s0" or "s0/old" or "s1" or
2014 * "s1/old". The "s0" or "s1" part is hidden from the user,
2015 * but included in the string, so this matches around it.
2016 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2018 * Otherwise, all other searches are simple string compares.
2021 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
2022 ctd_check_path(val)) {
2023 uint64_t wholedisk = 0;
2025 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2028 int slen = strlen(srchval);
2029 int vlen = strlen(val);
2031 if (slen != vlen - 2)
2035 * make_leaf_vdev() should only set
2036 * wholedisk for ZPOOL_CONFIG_PATHs which
2037 * will include "/dev/dsk/", giving plenty of
2038 * room for the indices used next.
2043 * strings identical except trailing "s0"
2045 if ((strcmp(&val[vlen - 2], "s0") == 0 ||
2046 strcmp(&val[vlen - 2], "s1") == 0) &&
2047 strncmp(srchval, val, slen) == 0)
2051 * strings identical except trailing "s0/old"
2053 if ((strcmp(&val[vlen - 6], "s0/old") == 0 ||
2054 strcmp(&val[vlen - 6], "s1/old") == 0) &&
2055 strcmp(&srchval[slen - 4], "/old") == 0 &&
2056 strncmp(srchval, val, slen - 4) == 0)
2061 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2063 if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2065 char *type, *idx, *end, *p;
2066 uint64_t id, vdev_id;
2069 * Determine our vdev type, keeping in mind
2070 * that the srchval is composed of a type and
2071 * vdev id pair (i.e. mirror-4).
2073 if ((type = strdup(srchval)) == NULL)
2076 if ((p = strrchr(type, '-')) == NULL) {
2084 * If the types don't match then keep looking.
2086 if (strncmp(val, type, strlen(val)) != 0) {
2091 verify(zpool_vdev_is_interior(type));
2092 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2096 vdev_id = strtoull(idx, &end, 10);
2103 * Now verify that we have the correct vdev id.
2112 if (strcmp(srchval, val) == 0)
2121 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2122 &child, &children) != 0)
2125 for (c = 0; c < children; c++) {
2126 if ((ret = vdev_to_nvlist_iter(child[c], search,
2127 avail_spare, l2cache, NULL)) != NULL) {
2129 * The 'is_log' value is only set for the toplevel
2130 * vdev, not the leaf vdevs. So we always lookup the
2131 * log device from the root of the vdev tree (where
2132 * 'log' is non-NULL).
2135 nvlist_lookup_uint64(child[c],
2136 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2144 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2145 &child, &children) == 0) {
2146 for (c = 0; c < children; c++) {
2147 if ((ret = vdev_to_nvlist_iter(child[c], search,
2148 avail_spare, l2cache, NULL)) != NULL) {
2149 *avail_spare = B_TRUE;
2155 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2156 &child, &children) == 0) {
2157 for (c = 0; c < children; c++) {
2158 if ((ret = vdev_to_nvlist_iter(child[c], search,
2159 avail_spare, l2cache, NULL)) != NULL) {
2170 * Given a physical path (minus the "/devices" prefix), find the
2174 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2175 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2177 nvlist_t *search, *nvroot, *ret;
2179 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2180 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2182 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2185 *avail_spare = B_FALSE;
2189 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2190 nvlist_free(search);
2196 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2199 zpool_vdev_is_interior(const char *name)
2201 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2202 strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 ||
2204 VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 ||
2205 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2211 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2212 boolean_t *l2cache, boolean_t *log)
2214 char buf[MAXPATHLEN];
2216 nvlist_t *nvroot, *search, *ret;
2219 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2221 guid = strtoull(path, &end, 10);
2222 if (guid != 0 && *end == '\0') {
2223 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2224 } else if (zpool_vdev_is_interior(path)) {
2225 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2226 } else if (path[0] != '/') {
2227 (void) snprintf(buf, sizeof (buf), "%s%s", _PATH_DEV, path);
2228 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
2230 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2233 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2236 *avail_spare = B_FALSE;
2240 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2241 nvlist_free(search);
2247 vdev_online(nvlist_t *nv)
2251 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2252 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2253 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2260 * Helper function for zpool_get_physpaths().
2263 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2264 size_t *bytes_written)
2266 size_t bytes_left, pos, rsz;
2270 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2272 return (EZFS_NODEVICE);
2274 pos = *bytes_written;
2275 bytes_left = physpath_size - pos;
2276 format = (pos == 0) ? "%s" : " %s";
2278 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2279 *bytes_written += rsz;
2281 if (rsz >= bytes_left) {
2282 /* if physpath was not copied properly, clear it */
2283 if (bytes_left != 0) {
2286 return (EZFS_NOSPC);
2292 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2293 size_t *rsz, boolean_t is_spare)
2298 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2299 return (EZFS_INVALCONFIG);
2301 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2303 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2304 * For a spare vdev, we only want to boot from the active
2309 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2312 return (EZFS_INVALCONFIG);
2315 if (vdev_online(nv)) {
2316 if ((ret = vdev_get_one_physpath(nv, physpath,
2317 phypath_size, rsz)) != 0)
2320 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2321 strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
2322 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2323 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2328 if (nvlist_lookup_nvlist_array(nv,
2329 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2330 return (EZFS_INVALCONFIG);
2332 for (i = 0; i < count; i++) {
2333 ret = vdev_get_physpaths(child[i], physpath,
2334 phypath_size, rsz, is_spare);
2335 if (ret == EZFS_NOSPC)
2340 return (EZFS_POOL_INVALARG);
2344 * Get phys_path for a root pool config.
2345 * Return 0 on success; non-zero on failure.
2348 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2351 nvlist_t *vdev_root;
2358 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2360 return (EZFS_INVALCONFIG);
2362 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2363 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2364 &child, &count) != 0)
2365 return (EZFS_INVALCONFIG);
2368 * root pool can only have a single top-level vdev.
2370 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2371 return (EZFS_POOL_INVALARG);
2373 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2376 /* No online devices */
2378 return (EZFS_NODEVICE);
2384 * Get phys_path for a root pool
2385 * Return 0 on success; non-zero on failure.
2388 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2390 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2395 * If the device has being dynamically expanded then we need to relabel
2396 * the disk to use the new unallocated space.
2399 zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2402 char path[MAXPATHLEN];
2405 int (*_efi_use_whole_disk)(int);
2407 if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2408 "efi_use_whole_disk")) == NULL)
2411 (void) snprintf(path, sizeof (path), "%s/%s", ZFS_RDISK_ROOT, name);
2413 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2414 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2415 "relabel '%s': unable to open device"), name);
2416 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2420 * It's possible that we might encounter an error if the device
2421 * does not have any unallocated space left. If so, we simply
2422 * ignore that error and continue on.
2424 error = _efi_use_whole_disk(fd);
2426 if (error && error != VT_ENOSPC) {
2427 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2428 "relabel '%s': unable to read disk capacity"), name);
2429 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2431 #endif /* illumos */
2436 * Bring the specified vdev online. The 'flags' parameter is a set of the
2437 * ZFS_ONLINE_* flags.
2440 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2441 vdev_state_t *newstate)
2443 zfs_cmd_t zc = { 0 };
2447 boolean_t avail_spare, l2cache, islog;
2448 libzfs_handle_t *hdl = zhp->zpool_hdl;
2450 if (flags & ZFS_ONLINE_EXPAND) {
2451 (void) snprintf(msg, sizeof (msg),
2452 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2454 (void) snprintf(msg, sizeof (msg),
2455 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2458 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2459 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2461 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2463 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2466 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2468 if ((flags & ZFS_ONLINE_EXPAND ||
2469 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) &&
2470 nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) {
2471 uint64_t wholedisk = 0;
2473 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2477 * XXX - L2ARC 1.0 devices can't support expansion.
2480 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2481 "cannot expand cache devices"));
2482 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2486 pathname += strlen(ZFS_DISK_ROOT) + 1;
2487 (void) zpool_relabel_disk(hdl, pathname);
2491 zc.zc_cookie = VDEV_STATE_ONLINE;
2494 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2495 if (errno == EINVAL) {
2496 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2497 "from this pool into a new one. Use '%s' "
2498 "instead"), "zpool detach");
2499 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2501 return (zpool_standard_error(hdl, errno, msg));
2504 *newstate = zc.zc_cookie;
2509 * Take the specified vdev offline
2512 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2514 zfs_cmd_t zc = { 0 };
2517 boolean_t avail_spare, l2cache;
2518 libzfs_handle_t *hdl = zhp->zpool_hdl;
2520 (void) snprintf(msg, sizeof (msg),
2521 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2523 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2524 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2526 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2528 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2531 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2533 zc.zc_cookie = VDEV_STATE_OFFLINE;
2534 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2536 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2543 * There are no other replicas of this device.
2545 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2549 * The log device has unplayed logs
2551 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2554 return (zpool_standard_error(hdl, errno, msg));
2559 * Mark the given vdev faulted.
2562 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2564 zfs_cmd_t zc = { 0 };
2566 libzfs_handle_t *hdl = zhp->zpool_hdl;
2568 (void) snprintf(msg, sizeof (msg),
2569 dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2571 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2573 zc.zc_cookie = VDEV_STATE_FAULTED;
2576 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2583 * There are no other replicas of this device.
2585 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2588 return (zpool_standard_error(hdl, errno, msg));
2594 * Mark the given vdev degraded.
2597 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2599 zfs_cmd_t zc = { 0 };
2601 libzfs_handle_t *hdl = zhp->zpool_hdl;
2603 (void) snprintf(msg, sizeof (msg),
2604 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
2606 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2608 zc.zc_cookie = VDEV_STATE_DEGRADED;
2611 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2614 return (zpool_standard_error(hdl, errno, msg));
2618 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2622 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2628 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2630 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2633 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2634 children == 2 && child[which] == tgt)
2637 for (c = 0; c < children; c++)
2638 if (is_replacing_spare(child[c], tgt, which))
2646 * Attach new_disk (fully described by nvroot) to old_disk.
2647 * If 'replacing' is specified, the new disk will replace the old one.
2650 zpool_vdev_attach(zpool_handle_t *zhp,
2651 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2653 zfs_cmd_t zc = { 0 };
2657 boolean_t avail_spare, l2cache, islog;
2662 nvlist_t *config_root;
2663 libzfs_handle_t *hdl = zhp->zpool_hdl;
2664 boolean_t rootpool = zpool_is_bootable(zhp);
2667 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2668 "cannot replace %s with %s"), old_disk, new_disk);
2670 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2671 "cannot attach %s to %s"), new_disk, old_disk);
2673 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2674 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2676 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2679 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2682 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2684 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2685 zc.zc_cookie = replacing;
2687 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2688 &child, &children) != 0 || children != 1) {
2689 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2690 "new device must be a single disk"));
2691 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2694 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2695 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2697 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2701 * If the target is a hot spare that has been swapped in, we can only
2702 * replace it with another hot spare.
2705 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2706 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2707 NULL) == NULL || !avail_spare) &&
2708 is_replacing_spare(config_root, tgt, 1)) {
2709 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2710 "can only be replaced by another hot spare"));
2712 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2717 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2720 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2722 zcmd_free_nvlists(&zc);
2727 * XXX need a better way to prevent user from
2728 * booting up a half-baked vdev.
2730 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2731 "sure to wait until resilver is done "
2732 "before rebooting.\n"));
2733 (void) fprintf(stderr, "\n");
2734 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "If "
2735 "you boot from pool '%s', you may need to update\n"
2736 "boot code on newly attached disk '%s'.\n\n"
2737 "Assuming you use GPT partitioning and 'da0' is "
2738 "your new boot disk\n"
2739 "you may use the following command:\n\n"
2740 "\tgpart bootcode -b /boot/pmbr -p "
2741 "/boot/gptzfsboot -i 1 da0\n\n"),
2742 zhp->zpool_name, new_disk);
2750 * Can't attach to or replace this type of vdev.
2753 uint64_t version = zpool_get_prop_int(zhp,
2754 ZPOOL_PROP_VERSION, NULL);
2757 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2758 "cannot replace a log with a spare"));
2759 else if (version >= SPA_VERSION_MULTI_REPLACE)
2760 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2761 "already in replacing/spare config; wait "
2762 "for completion or use 'zpool detach'"));
2764 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2765 "cannot replace a replacing device"));
2767 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2768 "can only attach to mirrors and top-level "
2771 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2776 * The new device must be a single disk.
2778 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2779 "new device must be a single disk"));
2780 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2784 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy, "
2785 "or pool has removing/removed vdevs"),
2787 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2792 * The new device is too small.
2794 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2795 "device is too small"));
2796 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2801 * The new device has a different alignment requirement.
2803 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2804 "devices have different sector alignment"));
2805 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2810 * The resulting top-level vdev spec won't fit in the label.
2812 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2816 (void) zpool_standard_error(hdl, errno, msg);
2823 * Detach the specified device.
2826 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2828 zfs_cmd_t zc = { 0 };
2831 boolean_t avail_spare, l2cache;
2832 libzfs_handle_t *hdl = zhp->zpool_hdl;
2834 (void) snprintf(msg, sizeof (msg),
2835 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2837 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2838 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2840 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2843 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2846 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2848 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2850 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2857 * Can't detach from this type of vdev.
2859 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2860 "applicable to mirror and replacing vdevs"));
2861 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2866 * There are no other replicas of this device.
2868 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2872 (void) zpool_standard_error(hdl, errno, msg);
2879 * Find a mirror vdev in the source nvlist.
2881 * The mchild array contains a list of disks in one of the top-level mirrors
2882 * of the source pool. The schild array contains a list of disks that the
2883 * user specified on the command line. We loop over the mchild array to
2884 * see if any entry in the schild array matches.
2886 * If a disk in the mchild array is found in the schild array, we return
2887 * the index of that entry. Otherwise we return -1.
2890 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2891 nvlist_t **schild, uint_t schildren)
2895 for (mc = 0; mc < mchildren; mc++) {
2897 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2898 mchild[mc], B_FALSE);
2900 for (sc = 0; sc < schildren; sc++) {
2901 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2902 schild[sc], B_FALSE);
2903 boolean_t result = (strcmp(mpath, spath) == 0);
2919 * Split a mirror pool. If newroot points to null, then a new nvlist
2920 * is generated and it is the responsibility of the caller to free it.
2923 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2924 nvlist_t *props, splitflags_t flags)
2926 zfs_cmd_t zc = { 0 };
2928 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2929 nvlist_t **varray = NULL, *zc_props = NULL;
2930 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2931 libzfs_handle_t *hdl = zhp->zpool_hdl;
2933 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2936 (void) snprintf(msg, sizeof (msg),
2937 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2939 if (!zpool_name_valid(hdl, B_FALSE, newname))
2940 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2942 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2943 (void) fprintf(stderr, gettext("Internal error: unable to "
2944 "retrieve pool configuration\n"));
2948 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2950 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2953 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2954 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2955 props, vers, flags, msg)) == NULL)
2959 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2961 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2962 "Source pool is missing vdev tree"));
2963 nvlist_free(zc_props);
2967 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2970 if (*newroot == NULL ||
2971 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2972 &newchild, &newchildren) != 0)
2975 for (c = 0; c < children; c++) {
2976 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2978 nvlist_t **mchild, *vdev;
2983 * Unlike cache & spares, slogs are stored in the
2984 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2986 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2988 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2990 if (is_log || is_hole) {
2992 * Create a hole vdev and put it in the config.
2994 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2996 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2997 VDEV_TYPE_HOLE) != 0)
2999 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
3004 varray[vcount++] = vdev;
3008 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
3010 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
3011 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3012 "Source pool must be composed only of mirrors\n"));
3013 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3017 verify(nvlist_lookup_nvlist_array(child[c],
3018 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3020 /* find or add an entry for this top-level vdev */
3021 if (newchildren > 0 &&
3022 (entry = find_vdev_entry(zhp, mchild, mchildren,
3023 newchild, newchildren)) >= 0) {
3024 /* We found a disk that the user specified. */
3025 vdev = mchild[entry];
3028 /* User didn't specify a disk for this vdev. */
3029 vdev = mchild[mchildren - 1];
3032 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3036 /* did we find every disk the user specified? */
3037 if (found != newchildren) {
3038 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3039 "include at most one disk from each mirror"));
3040 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3044 /* Prepare the nvlist for populating. */
3045 if (*newroot == NULL) {
3046 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3049 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3050 VDEV_TYPE_ROOT) != 0)
3053 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3056 /* Add all the children we found */
3057 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3058 lastlog == 0 ? vcount : lastlog) != 0)
3062 * If we're just doing a dry run, exit now with success.
3065 memory_err = B_FALSE;
3070 /* now build up the config list & call the ioctl */
3071 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3074 if (nvlist_add_nvlist(newconfig,
3075 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3076 nvlist_add_string(newconfig,
3077 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3078 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3082 * The new pool is automatically part of the namespace unless we
3083 * explicitly export it.
3086 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3087 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3088 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3089 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3091 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3094 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3095 retval = zpool_standard_error(hdl, errno, msg);
3100 memory_err = B_FALSE;
3103 if (varray != NULL) {
3106 for (v = 0; v < vcount; v++)
3107 nvlist_free(varray[v]);
3110 zcmd_free_nvlists(&zc);
3111 nvlist_free(zc_props);
3112 nvlist_free(newconfig);
3114 nvlist_free(*newroot);
3122 return (no_memory(hdl));
3128 * Remove the given device.
3131 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3133 zfs_cmd_t zc = { 0 };
3136 boolean_t avail_spare, l2cache, islog;
3137 libzfs_handle_t *hdl = zhp->zpool_hdl;
3140 (void) snprintf(msg, sizeof (msg),
3141 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3143 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3144 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3146 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3148 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3149 if (islog && version < SPA_VERSION_HOLES) {
3150 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3151 "pool must be upgraded to support log removal"));
3152 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3155 if (!islog && !avail_spare && !l2cache && zpool_is_bootable(zhp)) {
3156 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3157 "root pool can not have removed devices, "
3158 "because GRUB does not understand them"));
3159 return (zfs_error(hdl, EINVAL, msg));
3162 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3164 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3170 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3171 "invalid config; all top-level vdevs must "
3172 "have the same sector size and not be raidz."));
3173 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3177 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3178 "Pool busy; removal may already be in progress"));
3179 (void) zfs_error(hdl, EZFS_BUSY, msg);
3183 (void) zpool_standard_error(hdl, errno, msg);
3189 zpool_vdev_remove_cancel(zpool_handle_t *zhp)
3191 zfs_cmd_t zc = { 0 };
3193 libzfs_handle_t *hdl = zhp->zpool_hdl;
3195 (void) snprintf(msg, sizeof (msg),
3196 dgettext(TEXT_DOMAIN, "cannot cancel removal"));
3198 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3201 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3204 return (zpool_standard_error(hdl, errno, msg));
3208 zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path,
3213 boolean_t avail_spare, l2cache, islog;
3214 libzfs_handle_t *hdl = zhp->zpool_hdl;
3216 (void) snprintf(msg, sizeof (msg),
3217 dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"),
3220 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3222 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3224 if (avail_spare || l2cache || islog) {
3229 if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) {
3230 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3231 "indirect size not available"));
3232 return (zfs_error(hdl, EINVAL, msg));
3238 * Clear the errors for the pool, or the particular device if specified.
3241 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3243 zfs_cmd_t zc = { 0 };
3246 zpool_rewind_policy_t policy;
3247 boolean_t avail_spare, l2cache;
3248 libzfs_handle_t *hdl = zhp->zpool_hdl;
3249 nvlist_t *nvi = NULL;
3253 (void) snprintf(msg, sizeof (msg),
3254 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3257 (void) snprintf(msg, sizeof (msg),
3258 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3261 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3263 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3264 &l2cache, NULL)) == NULL)
3265 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3268 * Don't allow error clearing for hot spares. Do allow
3269 * error clearing for l2cache devices.
3272 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3274 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3278 zpool_get_rewind_policy(rewindnvl, &policy);
3279 zc.zc_cookie = policy.zrp_request;
3281 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3284 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3287 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3289 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3290 zcmd_free_nvlists(&zc);
3295 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
3296 errno != EPERM && errno != EACCES)) {
3297 if (policy.zrp_request &
3298 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3299 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3300 zpool_rewind_exclaim(hdl, zc.zc_name,
3301 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
3305 zcmd_free_nvlists(&zc);
3309 zcmd_free_nvlists(&zc);
3310 return (zpool_standard_error(hdl, errno, msg));
3314 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3317 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3319 zfs_cmd_t zc = { 0 };
3321 libzfs_handle_t *hdl = zhp->zpool_hdl;
3323 (void) snprintf(msg, sizeof (msg),
3324 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3327 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3329 zc.zc_cookie = ZPOOL_NO_REWIND;
3331 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3334 return (zpool_standard_error(hdl, errno, msg));
3338 * Change the GUID for a pool.
3341 zpool_reguid(zpool_handle_t *zhp)
3344 libzfs_handle_t *hdl = zhp->zpool_hdl;
3345 zfs_cmd_t zc = { 0 };
3347 (void) snprintf(msg, sizeof (msg),
3348 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3350 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3351 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3354 return (zpool_standard_error(hdl, errno, msg));
3361 zpool_reopen(zpool_handle_t *zhp)
3363 zfs_cmd_t zc = { 0 };
3365 libzfs_handle_t *hdl = zhp->zpool_hdl;
3367 (void) snprintf(msg, sizeof (msg),
3368 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
3371 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3372 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
3374 return (zpool_standard_error(hdl, errno, msg));
3378 * Convert from a devid string to a path.
3381 devid_to_path(char *devid_str)
3386 devid_nmlist_t *list = NULL;
3389 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3392 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3394 devid_str_free(minor);
3401 * In a case the strdup() fails, we will just return NULL below.
3403 path = strdup(list[0].devname);
3405 devid_free_nmlist(list);
3411 * Convert from a path to a devid string.
3414 path_to_devid(const char *path)
3421 if ((fd = open(path, O_RDONLY)) < 0)
3426 if (devid_get(fd, &devid) == 0) {
3427 if (devid_get_minor_name(fd, &minor) == 0)
3428 ret = devid_str_encode(devid, minor);
3430 devid_str_free(minor);
3442 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3443 * ignore any failure here, since a common case is for an unprivileged user to
3444 * type 'zpool status', and we'll display the correct information anyway.
3447 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3449 zfs_cmd_t zc = { 0 };
3451 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3452 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3453 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3456 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3460 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3461 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3462 * We also check if this is a whole disk, in which case we strip off the
3463 * trailing 's0' slice name.
3465 * This routine is also responsible for identifying when disks have been
3466 * reconfigured in a new location. The kernel will have opened the device by
3467 * devid, but the path will still refer to the old location. To catch this, we
3468 * first do a path -> devid translation (which is fast for the common case). If
3469 * the devid matches, we're done. If not, we do a reverse devid -> path
3470 * translation and issue the appropriate ioctl() to update the path of the vdev.
3471 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3475 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3486 have_stats = nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3487 (uint64_t **)&vs, &vsc) == 0;
3488 have_path = nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0;
3491 * If the device is not currently present, assume it will not
3492 * come back at the same device path. Display the device by GUID.
3494 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3495 have_path && have_stats && vs->vs_state <= VDEV_STATE_CANT_OPEN) {
3496 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3498 (void) snprintf(buf, sizeof (buf), "%llu",
3499 (u_longlong_t)value);
3501 } else if (have_path) {
3504 * If the device is dead (faulted, offline, etc) then don't
3505 * bother opening it. Otherwise we may be forcing the user to
3506 * open a misbehaving device, which can have undesirable
3509 if ((have_stats == 0 ||
3510 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3512 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3514 * Determine if the current path is correct.
3516 char *newdevid = path_to_devid(path);
3518 if (newdevid == NULL ||
3519 strcmp(devid, newdevid) != 0) {
3522 if ((newpath = devid_to_path(devid)) != NULL) {
3524 * Update the path appropriately.
3526 set_path(zhp, nv, newpath);
3527 if (nvlist_add_string(nv,
3528 ZPOOL_CONFIG_PATH, newpath) == 0)
3529 verify(nvlist_lookup_string(nv,
3537 devid_str_free(newdevid);
3541 if (strncmp(path, ZFS_DISK_ROOTD, strlen(ZFS_DISK_ROOTD)) == 0)
3542 path += strlen(ZFS_DISK_ROOTD);
3544 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3545 &value) == 0 && value) {
3546 int pathlen = strlen(path);
3547 char *tmp = zfs_strdup(hdl, path);
3550 * If it starts with c#, and ends with "s0" or "s1",
3551 * chop the slice off, or if it ends with "s0/old" or
3552 * "s1/old", remove the slice from the middle.
3554 if (CTD_CHECK(tmp)) {
3555 if (strcmp(&tmp[pathlen - 2], "s0") == 0 ||
3556 strcmp(&tmp[pathlen - 2], "s1") == 0) {
3557 tmp[pathlen - 2] = '\0';
3558 } else if (pathlen > 6 &&
3559 (strcmp(&tmp[pathlen - 6], "s0/old") == 0 ||
3560 strcmp(&tmp[pathlen - 6], "s1/old") == 0)) {
3561 (void) strcpy(&tmp[pathlen - 6],
3567 #else /* !illumos */
3568 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3569 path += sizeof(_PATH_DEV) - 1;
3570 #endif /* illumos */
3572 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3575 * If it's a raidz device, we need to stick in the parity level.
3577 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3578 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3580 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3581 (u_longlong_t)value);
3586 * We identify each top-level vdev by using a <type-id>
3587 * naming convention.
3592 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3594 (void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3600 return (zfs_strdup(hdl, path));
3604 zbookmark_mem_compare(const void *a, const void *b)
3606 return (memcmp(a, b, sizeof (zbookmark_phys_t)));
3610 * Retrieve the persistent error log, uniquify the members, and return to the
3614 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3616 zfs_cmd_t zc = { 0 };
3618 zbookmark_phys_t *zb = NULL;
3622 * Retrieve the raw error list from the kernel. If the number of errors
3623 * has increased, allocate more space and continue until we get the
3626 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3630 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3631 count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL)
3633 zc.zc_nvlist_dst_size = count;
3634 (void) strcpy(zc.zc_name, zhp->zpool_name);
3636 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3638 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3639 if (errno == ENOMEM) {
3642 count = zc.zc_nvlist_dst_size;
3643 dst = zfs_alloc(zhp->zpool_hdl, count *
3644 sizeof (zbookmark_phys_t));
3647 zc.zc_nvlist_dst = (uintptr_t)dst;
3657 * Sort the resulting bookmarks. This is a little confusing due to the
3658 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3659 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3660 * _not_ copied as part of the process. So we point the start of our
3661 * array appropriate and decrement the total number of elements.
3663 zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
3664 zc.zc_nvlist_dst_size;
3665 count -= zc.zc_nvlist_dst_size;
3667 qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
3669 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3672 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3674 for (i = 0; i < count; i++) {
3677 /* ignoring zb_blkid and zb_level for now */
3678 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3679 zb[i-1].zb_object == zb[i].zb_object)
3682 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3684 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3685 zb[i].zb_objset) != 0) {
3689 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3690 zb[i].zb_object) != 0) {
3694 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3701 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3705 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3706 return (no_memory(zhp->zpool_hdl));
3710 * Upgrade a ZFS pool to the latest on-disk version.
3713 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3715 zfs_cmd_t zc = { 0 };
3716 libzfs_handle_t *hdl = zhp->zpool_hdl;
3718 (void) strcpy(zc.zc_name, zhp->zpool_name);
3719 zc.zc_cookie = new_version;
3721 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3722 return (zpool_standard_error_fmt(hdl, errno,
3723 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3729 zfs_save_arguments(int argc, char **argv, char *string, int len)
3731 (void) strlcpy(string, basename(argv[0]), len);
3732 for (int i = 1; i < argc; i++) {
3733 (void) strlcat(string, " ", len);
3734 (void) strlcat(string, argv[i], len);
3739 zpool_log_history(libzfs_handle_t *hdl, const char *message)
3741 zfs_cmd_t zc = { 0 };
3745 args = fnvlist_alloc();
3746 fnvlist_add_string(args, "message", message);
3747 err = zcmd_write_src_nvlist(hdl, &zc, args);
3749 err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
3751 zcmd_free_nvlists(&zc);
3756 * Perform ioctl to get some command history of a pool.
3758 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3759 * logical offset of the history buffer to start reading from.
3761 * Upon return, 'off' is the next logical offset to read from and
3762 * 'len' is the actual amount of bytes read into 'buf'.
3765 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3767 zfs_cmd_t zc = { 0 };
3768 libzfs_handle_t *hdl = zhp->zpool_hdl;
3770 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3772 zc.zc_history = (uint64_t)(uintptr_t)buf;
3773 zc.zc_history_len = *len;
3774 zc.zc_history_offset = *off;
3776 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3779 return (zfs_error_fmt(hdl, EZFS_PERM,
3780 dgettext(TEXT_DOMAIN,
3781 "cannot show history for pool '%s'"),
3784 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3785 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3786 "'%s'"), zhp->zpool_name));
3788 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3789 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3790 "'%s', pool must be upgraded"), zhp->zpool_name));
3792 return (zpool_standard_error_fmt(hdl, errno,
3793 dgettext(TEXT_DOMAIN,
3794 "cannot get history for '%s'"), zhp->zpool_name));
3798 *len = zc.zc_history_len;
3799 *off = zc.zc_history_offset;
3805 * Process the buffer of nvlists, unpacking and storing each nvlist record
3806 * into 'records'. 'leftover' is set to the number of bytes that weren't
3807 * processed as there wasn't a complete record.
3810 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3811 nvlist_t ***records, uint_t *numrecords)
3817 while (bytes_read > sizeof (reclen)) {
3819 /* get length of packed record (stored as little endian) */
3820 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3821 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3823 if (bytes_read < sizeof (reclen) + reclen)
3827 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3829 bytes_read -= sizeof (reclen) + reclen;
3830 buf += sizeof (reclen) + reclen;
3832 /* add record to nvlist array */
3834 if (ISP2(*numrecords + 1)) {
3835 *records = realloc(*records,
3836 *numrecords * 2 * sizeof (nvlist_t *));
3838 (*records)[*numrecords - 1] = nv;
3841 *leftover = bytes_read;
3845 /* from spa_history.c: spa_history_create_obj() */
3846 #define HIS_BUF_LEN_DEF (128 << 10)
3847 #define HIS_BUF_LEN_MAX (1 << 30)
3850 * Retrieve the command history of a pool.
3853 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3856 uint64_t buflen = HIS_BUF_LEN_DEF;
3858 nvlist_t **records = NULL;
3859 uint_t numrecords = 0;
3862 buf = malloc(buflen);
3866 uint64_t bytes_read = buflen;
3869 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3872 /* if nothing else was read in, we're at EOF, just return */
3873 if (bytes_read == 0)
3876 if ((err = zpool_history_unpack(buf, bytes_read,
3877 &leftover, &records, &numrecords)) != 0)
3880 if (leftover == bytes_read) {
3882 * no progress made, because buffer is not big enough
3883 * to hold this record; resize and retry.
3888 if ((buflen >= HIS_BUF_LEN_MAX) ||
3889 ((buf = malloc(buflen)) == NULL)) {
3901 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3902 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3903 records, numrecords) == 0);
3905 for (i = 0; i < numrecords; i++)
3906 nvlist_free(records[i]);
3913 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3914 char *pathname, size_t len)
3916 zfs_cmd_t zc = { 0 };
3917 boolean_t mounted = B_FALSE;
3918 char *mntpnt = NULL;
3919 char dsname[ZFS_MAX_DATASET_NAME_LEN];
3922 /* special case for the MOS */
3923 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
3927 /* get the dataset's name */
3928 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3930 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3931 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3932 /* just write out a path of two object numbers */
3933 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3937 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3939 /* find out if the dataset is mounted */
3940 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3942 /* get the corrupted object's path */
3943 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3945 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3948 (void) snprintf(pathname, len, "%s%s", mntpnt,
3951 (void) snprintf(pathname, len, "%s:%s",
3952 dsname, zc.zc_value);
3955 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
3962 * Read the EFI label from the config, if a label does not exist then
3963 * pass back the error to the caller. If the caller has passed a non-NULL
3964 * diskaddr argument then we set it to the starting address of the EFI
3965 * partition. If the caller has passed a non-NULL boolean argument, then
3966 * we set it to indicate if the disk does have efi system partition.
3969 read_efi_label(nvlist_t *config, diskaddr_t *sb, boolean_t *system)
3973 char diskname[MAXPATHLEN];
3974 boolean_t boot = B_FALSE;
3978 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3981 (void) snprintf(diskname, sizeof (diskname), "%s%s", ZFS_RDISK_ROOT,
3982 strrchr(path, '/'));
3983 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
3984 struct dk_gpt *vtoc;
3986 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3987 for (slice = 0; slice < vtoc->efi_nparts; slice++) {
3988 if (vtoc->efi_parts[slice].p_tag == V_SYSTEM)
3990 if (vtoc->efi_parts[slice].p_tag == V_USR)
3993 if (sb != NULL && vtoc->efi_parts[slice].p_tag == V_USR)
3994 *sb = vtoc->efi_parts[slice].p_start;
4005 * determine where a partition starts on a disk in the current
4009 find_start_block(nvlist_t *config)
4013 diskaddr_t sb = MAXOFFSET_T;
4016 if (nvlist_lookup_nvlist_array(config,
4017 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
4018 if (nvlist_lookup_uint64(config,
4019 ZPOOL_CONFIG_WHOLE_DISK,
4020 &wholedisk) != 0 || !wholedisk) {
4021 return (MAXOFFSET_T);
4023 if (read_efi_label(config, &sb, NULL) < 0)
4028 for (c = 0; c < children; c++) {
4029 sb = find_start_block(child[c]);
4030 if (sb != MAXOFFSET_T) {
4034 return (MAXOFFSET_T);
4036 #endif /* illumos */
4039 * Label an individual disk. The name provided is the short name,
4040 * stripped of any leading /dev path.
4043 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name,
4044 zpool_boot_label_t boot_type, uint64_t boot_size, int *slice)
4047 char path[MAXPATHLEN];
4048 struct dk_gpt *vtoc;
4050 size_t resv = EFI_MIN_RESV_SIZE;
4051 uint64_t slice_size;
4052 diskaddr_t start_block;
4055 /* prepare an error message just in case */
4056 (void) snprintf(errbuf, sizeof (errbuf),
4057 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
4062 verify(nvlist_lookup_nvlist(zhp->zpool_config,
4063 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4065 if (zhp->zpool_start_block == 0)
4066 start_block = find_start_block(nvroot);
4068 start_block = zhp->zpool_start_block;
4069 zhp->zpool_start_block = start_block;
4072 start_block = NEW_START_BLOCK;
4075 (void) snprintf(path, sizeof (path), "%s/%s%s", ZFS_RDISK_ROOT, name,
4078 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
4080 * This shouldn't happen. We've long since verified that this
4081 * is a valid device.
4084 dgettext(TEXT_DOMAIN, "unable to open device"));
4085 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
4088 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
4090 * The only way this can fail is if we run out of memory, or we
4091 * were unable to read the disk's capacity
4093 if (errno == ENOMEM)
4094 (void) no_memory(hdl);
4097 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4098 "unable to read disk capacity"), name);
4100 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
4104 * Why we use V_USR: V_BACKUP confuses users, and is considered
4105 * disposable by some EFI utilities (since EFI doesn't have a backup
4106 * slice). V_UNASSIGNED is supposed to be used only for zero size
4107 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4108 * etc. were all pretty specific. V_USR is as close to reality as we
4109 * can get, in the absence of V_OTHER.
4111 /* first fix the partition start block */
4112 if (start_block == MAXOFFSET_T)
4113 start_block = NEW_START_BLOCK;
4116 * EFI System partition is using slice 0.
4117 * ZFS is on slice 1 and slice 8 is reserved.
4118 * We assume the GPT partition table without system
4119 * partition has zfs p_start == NEW_START_BLOCK.
4120 * If start_block != NEW_START_BLOCK, it means we have
4121 * system partition. Correct solution would be to query/cache vtoc
4122 * from existing vdev member.
4124 if (boot_type == ZPOOL_CREATE_BOOT_LABEL) {
4125 if (boot_size % vtoc->efi_lbasize != 0) {
4126 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4127 "boot partition size must be a multiple of %d"),
4131 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4134 * System partition size checks.
4135 * Note the 1MB is quite arbitrary value, since we
4136 * are creating dedicated pool, it should be enough
4137 * to hold fat + efi bootloader. May need to be
4138 * adjusted if the bootloader size will grow.
4140 if (boot_size < 1024 * 1024) {
4142 zfs_nicenum(boot_size, buf, sizeof (buf));
4143 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4144 "Specified size %s for EFI System partition is too "
4145 "small, the minimum size is 1MB."), buf);
4148 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4150 /* 33MB is tested with mkfs -F pcfs */
4151 if (hdl->libzfs_printerr &&
4152 ((vtoc->efi_lbasize == 512 &&
4153 boot_size < 33 * 1024 * 1024) ||
4154 (vtoc->efi_lbasize == 4096 &&
4155 boot_size < 256 * 1024 * 1024))) {
4157 zfs_nicenum(boot_size, buf, sizeof (buf));
4158 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
4159 "Warning: EFI System partition size %s is "
4160 "not allowing to create FAT32 file\nsystem, which "
4161 "may result in unbootable system.\n"), buf);
4163 /* Adjust zfs partition start by size of system partition. */
4164 start_block += boot_size / vtoc->efi_lbasize;
4167 if (start_block == NEW_START_BLOCK) {
4169 * Use default layout.
4170 * ZFS is on slice 0 and slice 8 is reserved.
4172 slice_size = vtoc->efi_last_u_lba + 1;
4173 slice_size -= EFI_MIN_RESV_SIZE;
4174 slice_size -= start_block;
4178 vtoc->efi_parts[0].p_start = start_block;
4179 vtoc->efi_parts[0].p_size = slice_size;
4181 vtoc->efi_parts[0].p_tag = V_USR;
4182 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
4184 vtoc->efi_parts[8].p_start = slice_size + start_block;
4185 vtoc->efi_parts[8].p_size = resv;
4186 vtoc->efi_parts[8].p_tag = V_RESERVED;
4188 slice_size = start_block - NEW_START_BLOCK;
4189 vtoc->efi_parts[0].p_start = NEW_START_BLOCK;
4190 vtoc->efi_parts[0].p_size = slice_size;
4191 vtoc->efi_parts[0].p_tag = V_SYSTEM;
4192 (void) strcpy(vtoc->efi_parts[0].p_name, "loader");
4195 /* prepare slice 1 */
4196 slice_size = vtoc->efi_last_u_lba + 1 - slice_size;
4198 slice_size -= NEW_START_BLOCK;
4199 vtoc->efi_parts[1].p_start = start_block;
4200 vtoc->efi_parts[1].p_size = slice_size;
4201 vtoc->efi_parts[1].p_tag = V_USR;
4202 (void) strcpy(vtoc->efi_parts[1].p_name, "zfs");
4204 vtoc->efi_parts[8].p_start = slice_size + start_block;
4205 vtoc->efi_parts[8].p_size = resv;
4206 vtoc->efi_parts[8].p_tag = V_RESERVED;
4209 if (efi_write(fd, vtoc) != 0) {
4211 * Some block drivers (like pcata) may not support EFI
4212 * GPT labels. Print out a helpful error message dir-
4213 * ecting the user to manually label the disk and give
4219 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4220 "try using fdisk(1M) and then provide a specific slice"));
4221 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4226 #endif /* illumos */
4231 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
4237 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
4238 if (strcmp(type, VDEV_TYPE_FILE) == 0 ||
4239 strcmp(type, VDEV_TYPE_HOLE) == 0 ||
4240 strcmp(type, VDEV_TYPE_MISSING) == 0) {
4241 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4242 "vdev type '%s' is not supported"), type);
4243 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
4246 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
4247 &child, &children) == 0) {
4248 for (c = 0; c < children; c++) {
4249 if (!supported_dump_vdev_type(hdl, child[c], errbuf))
4257 * Check if this zvol is allowable for use as a dump device; zero if
4258 * it is, > 0 if it isn't, < 0 if it isn't a zvol.
4260 * Allowable storage configurations include mirrors, all raidz variants, and
4261 * pools with log, cache, and spare devices. Pools which are backed by files or
4262 * have missing/hole vdevs are not suitable.
4265 zvol_check_dump_config(char *arg)
4267 zpool_handle_t *zhp = NULL;
4268 nvlist_t *config, *nvroot;
4272 libzfs_handle_t *hdl;
4274 char poolname[ZFS_MAX_DATASET_NAME_LEN];
4275 int pathlen = strlen(ZVOL_FULL_DEV_DIR);
4278 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
4282 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4283 "dump is not supported on device '%s'"), arg);
4285 if ((hdl = libzfs_init()) == NULL)
4287 libzfs_print_on_error(hdl, B_TRUE);
4289 volname = arg + pathlen;
4291 /* check the configuration of the pool */
4292 if ((p = strchr(volname, '/')) == NULL) {
4293 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4294 "malformed dataset name"));
4295 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4297 } else if (p - volname >= ZFS_MAX_DATASET_NAME_LEN) {
4298 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4299 "dataset name is too long"));
4300 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
4303 (void) strncpy(poolname, volname, p - volname);
4304 poolname[p - volname] = '\0';
4307 if ((zhp = zpool_open(hdl, poolname)) == NULL) {
4308 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4309 "could not open pool '%s'"), poolname);
4310 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4313 config = zpool_get_config(zhp, NULL);
4314 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4316 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4317 "could not obtain vdev configuration for '%s'"), poolname);
4318 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4322 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4323 &top, &toplevels) == 0);
4325 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
4338 zpool_nextboot(libzfs_handle_t *hdl, uint64_t pool_guid, uint64_t dev_guid,
4339 const char *command)
4341 zfs_cmd_t zc = { 0 };
4347 args = fnvlist_alloc();
4348 fnvlist_add_uint64(args, ZPOOL_CONFIG_POOL_GUID, pool_guid);
4349 fnvlist_add_uint64(args, ZPOOL_CONFIG_GUID, dev_guid);
4350 fnvlist_add_string(args, "command", command);
4351 error = zcmd_write_src_nvlist(hdl, &zc, args);
4353 error = ioctl(hdl->libzfs_fd, ZFS_IOC_NEXTBOOT, &zc);
4354 zcmd_free_nvlists(&zc);