4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
39 #include <sys/efi_partition.h>
41 #include <sys/zfs_ioctl.h>
44 #include "zfs_namecheck.h"
46 #include "libzfs_impl.h"
47 #include "zfs_comutil.h"
48 #include "zfeature_common.h"
50 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
52 typedef struct prop_flags {
53 int create:1; /* Validate property on creation */
54 int import:1; /* Validate property on import */
58 * ====================================================================
59 * zpool property functions
60 * ====================================================================
64 zpool_get_all_props(zpool_handle_t *zhp)
66 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
67 libzfs_handle_t *hdl = zhp->zpool_hdl;
69 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
71 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
74 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
75 if (errno == ENOMEM) {
76 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
77 zcmd_free_nvlists(&zc);
81 zcmd_free_nvlists(&zc);
86 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
87 zcmd_free_nvlists(&zc);
91 zcmd_free_nvlists(&zc);
97 zpool_props_refresh(zpool_handle_t *zhp)
101 old_props = zhp->zpool_props;
103 if (zpool_get_all_props(zhp) != 0)
106 nvlist_free(old_props);
111 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
117 zprop_source_t source;
119 nvl = zhp->zpool_props;
120 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
121 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
123 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
125 source = ZPROP_SRC_DEFAULT;
126 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
137 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
141 zprop_source_t source;
143 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
145 * zpool_get_all_props() has most likely failed because
146 * the pool is faulted, but if all we need is the top level
147 * vdev's guid then get it from the zhp config nvlist.
149 if ((prop == ZPOOL_PROP_GUID) &&
150 (nvlist_lookup_nvlist(zhp->zpool_config,
151 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
152 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
156 return (zpool_prop_default_numeric(prop));
159 nvl = zhp->zpool_props;
160 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
161 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
163 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
165 source = ZPROP_SRC_DEFAULT;
166 value = zpool_prop_default_numeric(prop);
176 * Map VDEV STATE to printed strings.
179 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
184 case VDEV_STATE_CLOSED:
185 case VDEV_STATE_OFFLINE:
186 return (gettext("OFFLINE"));
187 case VDEV_STATE_REMOVED:
188 return (gettext("REMOVED"));
189 case VDEV_STATE_CANT_OPEN:
190 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
191 return (gettext("FAULTED"));
192 else if (aux == VDEV_AUX_SPLIT_POOL)
193 return (gettext("SPLIT"));
195 return (gettext("UNAVAIL"));
196 case VDEV_STATE_FAULTED:
197 return (gettext("FAULTED"));
198 case VDEV_STATE_DEGRADED:
199 return (gettext("DEGRADED"));
200 case VDEV_STATE_HEALTHY:
201 return (gettext("ONLINE"));
204 return (gettext("UNKNOWN"));
208 * Map POOL STATE to printed strings.
211 zpool_pool_state_to_name(pool_state_t state)
216 case POOL_STATE_ACTIVE:
217 return (gettext("ACTIVE"));
218 case POOL_STATE_EXPORTED:
219 return (gettext("EXPORTED"));
220 case POOL_STATE_DESTROYED:
221 return (gettext("DESTROYED"));
222 case POOL_STATE_SPARE:
223 return (gettext("SPARE"));
224 case POOL_STATE_L2CACHE:
225 return (gettext("L2CACHE"));
226 case POOL_STATE_UNINITIALIZED:
227 return (gettext("UNINITIALIZED"));
228 case POOL_STATE_UNAVAIL:
229 return (gettext("UNAVAIL"));
230 case POOL_STATE_POTENTIALLY_ACTIVE:
231 return (gettext("POTENTIALLY_ACTIVE"));
234 return (gettext("UNKNOWN"));
238 * Get a zpool property value for 'prop' and return the value in
239 * a pre-allocated buffer.
242 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
243 zprop_source_t *srctype)
247 zprop_source_t src = ZPROP_SRC_NONE;
252 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
254 case ZPOOL_PROP_NAME:
255 (void) strlcpy(buf, zpool_get_name(zhp), len);
258 case ZPOOL_PROP_HEALTH:
259 (void) strlcpy(buf, "FAULTED", len);
262 case ZPOOL_PROP_GUID:
263 intval = zpool_get_prop_int(zhp, prop, &src);
264 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
267 case ZPOOL_PROP_ALTROOT:
268 case ZPOOL_PROP_CACHEFILE:
269 case ZPOOL_PROP_COMMENT:
270 if (zhp->zpool_props != NULL ||
271 zpool_get_all_props(zhp) == 0) {
273 zpool_get_prop_string(zhp, prop, &src),
281 (void) strlcpy(buf, "-", len);
290 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
291 prop != ZPOOL_PROP_NAME)
294 switch (zpool_prop_get_type(prop)) {
295 case PROP_TYPE_STRING:
296 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
300 case PROP_TYPE_NUMBER:
301 intval = zpool_get_prop_int(zhp, prop, &src);
304 case ZPOOL_PROP_SIZE:
305 case ZPOOL_PROP_ALLOCATED:
306 case ZPOOL_PROP_FREE:
307 case ZPOOL_PROP_FREEING:
308 case ZPOOL_PROP_EXPANDSZ:
309 case ZPOOL_PROP_ASHIFT:
310 (void) zfs_nicenum(intval, buf, len);
313 case ZPOOL_PROP_CAPACITY:
314 (void) snprintf(buf, len, "%llu%%",
315 (u_longlong_t)intval);
318 case ZPOOL_PROP_DEDUPRATIO:
319 (void) snprintf(buf, len, "%llu.%02llux",
320 (u_longlong_t)(intval / 100),
321 (u_longlong_t)(intval % 100));
324 case ZPOOL_PROP_HEALTH:
325 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
326 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
327 verify(nvlist_lookup_uint64_array(nvroot,
328 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
331 (void) strlcpy(buf, zpool_state_to_name(intval,
334 case ZPOOL_PROP_VERSION:
335 if (intval >= SPA_VERSION_FEATURES) {
336 (void) snprintf(buf, len, "-");
341 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
345 case PROP_TYPE_INDEX:
346 intval = zpool_get_prop_int(zhp, prop, &src);
347 if (zpool_prop_index_to_string(prop, intval, &strval)
350 (void) strlcpy(buf, strval, len);
364 * Check if the bootfs name has the same pool name as it is set to.
365 * Assuming bootfs is a valid dataset name.
368 bootfs_name_valid(const char *pool, char *bootfs)
370 int len = strlen(pool);
372 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
375 if (strncmp(pool, bootfs, len) == 0 &&
376 (bootfs[len] == '/' || bootfs[len] == '\0'))
382 #if defined(__sun__) || defined(__sun)
384 * Inspect the configuration to determine if any of the devices contain
388 pool_uses_efi(nvlist_t *config)
393 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
394 &child, &children) != 0)
395 return (read_efi_label(config, NULL) >= 0);
397 for (c = 0; c < children; c++) {
398 if (pool_uses_efi(child[c]))
406 zpool_is_bootable(zpool_handle_t *zhp)
408 char bootfs[ZPOOL_MAXNAMELEN];
410 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
411 sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
412 sizeof (bootfs)) != 0);
417 * Given an nvlist of zpool properties to be set, validate that they are
418 * correct, and parse any numeric properties (index, boolean, etc) if they are
419 * specified as strings.
422 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
423 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
431 struct stat64 statbuf;
435 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
436 (void) no_memory(hdl);
441 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
442 const char *propname = nvpair_name(elem);
444 prop = zpool_name_to_prop(propname);
445 if (prop == ZPROP_INVAL && zpool_prop_feature(propname)) {
447 zfeature_info_t *feature;
448 char *fname = strchr(propname, '@') + 1;
450 err = zfeature_lookup_name(fname, &feature);
452 ASSERT3U(err, ==, ENOENT);
453 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
454 "invalid feature '%s'"), fname);
455 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
459 if (nvpair_type(elem) != DATA_TYPE_STRING) {
460 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
461 "'%s' must be a string"), propname);
462 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
466 (void) nvpair_value_string(elem, &strval);
467 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) {
468 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
469 "property '%s' can only be set to "
470 "'enabled'"), propname);
471 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
475 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
476 (void) no_memory(hdl);
483 * Make sure this property is valid and applies to this type.
485 if (prop == ZPROP_INVAL) {
486 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
487 "invalid property '%s'"), propname);
488 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
492 if (zpool_prop_readonly(prop)) {
493 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
494 "is readonly"), propname);
495 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
499 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
500 &strval, &intval, errbuf) != 0)
504 * Perform additional checking for specific properties.
509 case ZPOOL_PROP_VERSION:
510 if (intval < version ||
511 !SPA_VERSION_IS_SUPPORTED(intval)) {
512 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
513 "property '%s' number %d is invalid."),
515 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
520 case ZPOOL_PROP_ASHIFT:
522 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
523 "property '%s' can only be set at "
524 "creation time"), propname);
525 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
529 if (intval != 0 && (intval < 9 || intval > 13)) {
530 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
531 "property '%s' number %d is invalid."),
533 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
538 case ZPOOL_PROP_BOOTFS:
539 if (flags.create || flags.import) {
540 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
541 "property '%s' cannot be set at creation "
542 "or import time"), propname);
543 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
547 if (version < SPA_VERSION_BOOTFS) {
548 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
549 "pool must be upgraded to support "
550 "'%s' property"), propname);
551 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
556 * bootfs property value has to be a dataset name and
557 * the dataset has to be in the same pool as it sets to.
559 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
561 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
562 "is an invalid name"), strval);
563 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
567 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
568 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
569 "could not open pool '%s'"), poolname);
570 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
573 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
574 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
576 #if defined(__sun__) || defined(__sun)
578 * bootfs property cannot be set on a disk which has
581 if (pool_uses_efi(nvroot)) {
582 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
583 "property '%s' not supported on "
584 "EFI labeled devices"), propname);
585 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
593 case ZPOOL_PROP_ALTROOT:
594 if (!flags.create && !flags.import) {
595 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
596 "property '%s' can only be set during pool "
597 "creation or import"), propname);
598 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
602 if (strval[0] != '/') {
603 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
604 "bad alternate root '%s'"), strval);
605 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
610 case ZPOOL_PROP_CACHEFILE:
611 if (strval[0] == '\0')
614 if (strcmp(strval, "none") == 0)
617 if (strval[0] != '/') {
618 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
619 "property '%s' must be empty, an "
620 "absolute path, or 'none'"), propname);
621 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
625 slash = strrchr(strval, '/');
627 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
628 strcmp(slash, "/..") == 0) {
629 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
630 "'%s' is not a valid file"), strval);
631 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
637 if (strval[0] != '\0' &&
638 (stat64(strval, &statbuf) != 0 ||
639 !S_ISDIR(statbuf.st_mode))) {
640 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
641 "'%s' is not a valid directory"),
643 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
650 case ZPOOL_PROP_COMMENT:
651 for (check = strval; *check != '\0'; check++) {
652 if (!isprint(*check)) {
654 dgettext(TEXT_DOMAIN,
655 "comment may only have printable "
657 (void) zfs_error(hdl, EZFS_BADPROP,
662 if (strlen(strval) > ZPROP_MAX_COMMENT) {
663 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
664 "comment must not exceed %d characters"),
666 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
670 case ZPOOL_PROP_READONLY:
672 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
673 "property '%s' can only be set at "
674 "import time"), propname);
675 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
684 nvlist_free(retprops);
689 * Set zpool property : propname=propval.
692 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
694 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
697 nvlist_t *nvl = NULL;
700 prop_flags_t flags = { 0 };
702 (void) snprintf(errbuf, sizeof (errbuf),
703 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
706 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
707 return (no_memory(zhp->zpool_hdl));
709 if (nvlist_add_string(nvl, propname, propval) != 0) {
711 return (no_memory(zhp->zpool_hdl));
714 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
715 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
716 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
725 * Execute the corresponding ioctl() to set this property.
727 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
729 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
734 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
736 zcmd_free_nvlists(&zc);
740 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
742 (void) zpool_props_refresh(zhp);
748 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
750 libzfs_handle_t *hdl = zhp->zpool_hdl;
752 char buf[ZFS_MAXPROPLEN];
753 nvlist_t *features = NULL;
756 boolean_t firstexpand = (NULL == *plp);
759 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
763 while (*last != NULL)
764 last = &(*last)->pl_next;
767 features = zpool_get_features(zhp);
769 if ((*plp)->pl_all && firstexpand) {
770 for (i = 0; i < SPA_FEATURES; i++) {
771 zprop_list_t *entry = zfs_alloc(hdl,
772 sizeof (zprop_list_t));
773 entry->pl_prop = ZPROP_INVAL;
774 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
775 spa_feature_table[i].fi_uname);
776 entry->pl_width = strlen(entry->pl_user_prop);
777 entry->pl_all = B_TRUE;
780 last = &entry->pl_next;
784 /* add any unsupported features */
785 for (nvp = nvlist_next_nvpair(features, NULL);
786 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
791 if (zfeature_is_supported(nvpair_name(nvp)))
794 propname = zfs_asprintf(hdl, "unsupported@%s",
798 * Before adding the property to the list make sure that no
799 * other pool already added the same property.
803 while (entry != NULL) {
804 if (entry->pl_user_prop != NULL &&
805 strcmp(propname, entry->pl_user_prop) == 0) {
809 entry = entry->pl_next;
816 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
817 entry->pl_prop = ZPROP_INVAL;
818 entry->pl_user_prop = propname;
819 entry->pl_width = strlen(entry->pl_user_prop);
820 entry->pl_all = B_TRUE;
823 last = &entry->pl_next;
826 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
831 if (entry->pl_prop != ZPROP_INVAL &&
832 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
834 if (strlen(buf) > entry->pl_width)
835 entry->pl_width = strlen(buf);
843 * Get the state for the given feature on the given ZFS pool.
846 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
850 boolean_t found = B_FALSE;
851 nvlist_t *features = zpool_get_features(zhp);
853 const char *feature = strchr(propname, '@') + 1;
855 supported = zpool_prop_feature(propname);
856 ASSERT(supported || zpool_prop_unsupported(propname));
859 * Convert from feature name to feature guid. This conversion is
860 * unecessary for unsupported@... properties because they already
867 ret = zfeature_lookup_name(feature, &fi);
869 (void) strlcpy(buf, "-", len);
872 feature = fi->fi_guid;
875 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
880 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
883 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
885 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
890 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
892 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
895 (void) strlcpy(buf, "-", len);
904 * Don't start the slice at the default block of 34; many storage
905 * devices will use a stripe width of 128k, other vendors prefer a 1m
906 * alignment. It is best to play it safe and ensure a 1m alignment
907 * given 512B blocks. When the block size is larger by a power of 2
908 * we will still be 1m aligned. Some devices are sensitive to the
909 * partition ending alignment as well.
911 #define NEW_START_BLOCK 2048
912 #define PARTITION_END_ALIGNMENT 2048
915 * Validate the given pool name, optionally putting an extended error message in
919 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
925 ret = pool_namecheck(pool, &why, &what);
928 * The rules for reserved pool names were extended at a later point.
929 * But we need to support users with existing pools that may now be
930 * invalid. So we only check for this expanded set of names during a
931 * create (or import), and only in userland.
933 if (ret == 0 && !isopen &&
934 (strncmp(pool, "mirror", 6) == 0 ||
935 strncmp(pool, "raidz", 5) == 0 ||
936 strncmp(pool, "spare", 5) == 0 ||
937 strcmp(pool, "log") == 0)) {
940 dgettext(TEXT_DOMAIN, "name is reserved"));
948 case NAME_ERR_TOOLONG:
950 dgettext(TEXT_DOMAIN, "name is too long"));
953 case NAME_ERR_INVALCHAR:
955 dgettext(TEXT_DOMAIN, "invalid character "
956 "'%c' in pool name"), what);
959 case NAME_ERR_NOLETTER:
960 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
961 "name must begin with a letter"));
964 case NAME_ERR_RESERVED:
965 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
966 "name is reserved"));
969 case NAME_ERR_DISKLIKE:
970 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
971 "pool name is reserved"));
974 case NAME_ERR_LEADING_SLASH:
975 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
976 "leading slash in name"));
979 case NAME_ERR_EMPTY_COMPONENT:
980 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
981 "empty component in name"));
984 case NAME_ERR_TRAILING_SLASH:
985 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
986 "trailing slash in name"));
989 case NAME_ERR_MULTIPLE_AT:
990 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
991 "multiple '@' delimiters in name"));
994 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
995 "permission set is missing '@'"));
1006 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1010 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
1012 zpool_handle_t *zhp;
1016 * Make sure the pool name is valid.
1018 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
1019 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1020 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1025 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1028 zhp->zpool_hdl = hdl;
1029 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1031 if (zpool_refresh_stats(zhp, &missing) != 0) {
1037 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1038 (void) zfs_error_fmt(hdl, EZFS_NOENT,
1039 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1048 * Like the above, but silent on error. Used when iterating over pools (because
1049 * the configuration cache may be out of date).
1052 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1054 zpool_handle_t *zhp;
1057 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1060 zhp->zpool_hdl = hdl;
1061 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1063 if (zpool_refresh_stats(zhp, &missing) != 0) {
1079 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1083 zpool_open(libzfs_handle_t *hdl, const char *pool)
1085 zpool_handle_t *zhp;
1087 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1090 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1091 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1092 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1101 * Close the handle. Simply frees the memory associated with the handle.
1104 zpool_close(zpool_handle_t *zhp)
1106 if (zhp->zpool_config)
1107 nvlist_free(zhp->zpool_config);
1108 if (zhp->zpool_old_config)
1109 nvlist_free(zhp->zpool_old_config);
1110 if (zhp->zpool_props)
1111 nvlist_free(zhp->zpool_props);
1116 * Return the name of the pool.
1119 zpool_get_name(zpool_handle_t *zhp)
1121 return (zhp->zpool_name);
1126 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1129 zpool_get_state(zpool_handle_t *zhp)
1131 return (zhp->zpool_state);
1135 * Create the named pool, using the provided vdev list. It is assumed
1136 * that the consumer has already validated the contents of the nvlist, so we
1137 * don't have to worry about error semantics.
1140 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1141 nvlist_t *props, nvlist_t *fsprops)
1143 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1144 nvlist_t *zc_fsprops = NULL;
1145 nvlist_t *zc_props = NULL;
1150 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1151 "cannot create '%s'"), pool);
1153 if (!zpool_name_valid(hdl, B_FALSE, pool))
1154 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1156 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1160 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1162 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1163 SPA_VERSION_1, flags, msg)) == NULL) {
1172 zoned = ((nvlist_lookup_string(fsprops,
1173 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1174 strcmp(zonestr, "on") == 0);
1176 if ((zc_fsprops = zfs_valid_proplist(hdl,
1177 ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
1181 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1184 if (nvlist_add_nvlist(zc_props,
1185 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1190 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1193 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1195 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1197 zcmd_free_nvlists(&zc);
1198 nvlist_free(zc_props);
1199 nvlist_free(zc_fsprops);
1204 * This can happen if the user has specified the same
1205 * device multiple times. We can't reliably detect this
1206 * until we try to add it and see we already have a
1207 * label. This can also happen under if the device is
1208 * part of an active md or lvm device.
1210 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1211 "one or more vdevs refer to the same device, or one of\n"
1212 "the devices is part of an active md or lvm device"));
1213 return (zfs_error(hdl, EZFS_BADDEV, msg));
1217 * This occurs when one of the devices is below
1218 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1219 * device was the problem device since there's no
1220 * reliable way to determine device size from userland.
1225 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1227 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1228 "one or more devices is less than the "
1229 "minimum size (%s)"), buf);
1231 return (zfs_error(hdl, EZFS_BADDEV, msg));
1234 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1235 "one or more devices is out of space"));
1236 return (zfs_error(hdl, EZFS_BADDEV, msg));
1239 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1240 "cache device must be a disk or disk slice"));
1241 return (zfs_error(hdl, EZFS_BADDEV, msg));
1244 return (zpool_standard_error(hdl, errno, msg));
1249 * If this is an alternate root pool, then we automatically set the
1250 * mountpoint of the root dataset to be '/'.
1252 if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
1256 verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
1257 verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1264 zcmd_free_nvlists(&zc);
1265 nvlist_free(zc_props);
1266 nvlist_free(zc_fsprops);
1271 * Destroy the given pool. It is up to the caller to ensure that there are no
1272 * datasets left in the pool.
1275 zpool_destroy(zpool_handle_t *zhp)
1277 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1278 zfs_handle_t *zfp = NULL;
1279 libzfs_handle_t *hdl = zhp->zpool_hdl;
1282 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1283 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1286 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1288 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1289 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1290 "cannot destroy '%s'"), zhp->zpool_name);
1292 if (errno == EROFS) {
1293 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1294 "one or more devices is read only"));
1295 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1297 (void) zpool_standard_error(hdl, errno, msg);
1306 remove_mountpoint(zfp);
1314 * Add the given vdevs to the pool. The caller must have already performed the
1315 * necessary verification to ensure that the vdev specification is well-formed.
1318 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1320 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1322 libzfs_handle_t *hdl = zhp->zpool_hdl;
1324 nvlist_t **spares, **l2cache;
1325 uint_t nspares, nl2cache;
1327 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1328 "cannot add to '%s'"), zhp->zpool_name);
1330 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1331 SPA_VERSION_SPARES &&
1332 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1333 &spares, &nspares) == 0) {
1334 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1335 "upgraded to add hot spares"));
1336 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1339 #if defined(__sun__) || defined(__sun)
1340 if (zpool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1341 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1344 for (s = 0; s < nspares; s++) {
1347 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1348 &path) == 0 && pool_uses_efi(spares[s])) {
1349 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1350 "device '%s' contains an EFI label and "
1351 "cannot be used on root pools."),
1352 zpool_vdev_name(hdl, NULL, spares[s],
1354 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1360 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1361 SPA_VERSION_L2CACHE &&
1362 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1363 &l2cache, &nl2cache) == 0) {
1364 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1365 "upgraded to add cache devices"));
1366 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1369 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1371 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1373 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1377 * This can happen if the user has specified the same
1378 * device multiple times. We can't reliably detect this
1379 * until we try to add it and see we already have a
1382 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1383 "one or more vdevs refer to the same device"));
1384 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1389 * This occurrs when one of the devices is below
1390 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1391 * device was the problem device since there's no
1392 * reliable way to determine device size from userland.
1397 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1399 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1400 "device is less than the minimum "
1403 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1407 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1408 "pool must be upgraded to add these vdevs"));
1409 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1413 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1414 "root pool can not have multiple vdevs"
1415 " or separate logs"));
1416 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1420 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1421 "cache device must be a disk or disk slice"));
1422 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1426 (void) zpool_standard_error(hdl, errno, msg);
1434 zcmd_free_nvlists(&zc);
1440 * Exports the pool from the system. The caller must ensure that there are no
1441 * mounted datasets in the pool.
1444 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1446 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1449 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1450 "cannot export '%s'"), zhp->zpool_name);
1452 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1453 zc.zc_cookie = force;
1454 zc.zc_guid = hardforce;
1456 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1459 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1460 "use '-f' to override the following errors:\n"
1461 "'%s' has an active shared spare which could be"
1462 " used by other pools once '%s' is exported."),
1463 zhp->zpool_name, zhp->zpool_name);
1464 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1467 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1476 zpool_export(zpool_handle_t *zhp, boolean_t force)
1478 return (zpool_export_common(zhp, force, B_FALSE));
1482 zpool_export_force(zpool_handle_t *zhp)
1484 return (zpool_export_common(zhp, B_TRUE, B_TRUE));
1488 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1491 nvlist_t *nv = NULL;
1497 if (!hdl->libzfs_printerr || config == NULL)
1500 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1501 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1505 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1507 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1509 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1510 strftime(timestr, 128, "%c", &t) != 0) {
1512 (void) printf(dgettext(TEXT_DOMAIN,
1513 "Would be able to return %s "
1514 "to its state as of %s.\n"),
1517 (void) printf(dgettext(TEXT_DOMAIN,
1518 "Pool %s returned to its state as of %s.\n"),
1522 (void) printf(dgettext(TEXT_DOMAIN,
1523 "%s approximately %lld "),
1524 dryrun ? "Would discard" : "Discarded",
1525 ((longlong_t)loss + 30) / 60);
1526 (void) printf(dgettext(TEXT_DOMAIN,
1527 "minutes of transactions.\n"));
1528 } else if (loss > 0) {
1529 (void) printf(dgettext(TEXT_DOMAIN,
1530 "%s approximately %lld "),
1531 dryrun ? "Would discard" : "Discarded",
1533 (void) printf(dgettext(TEXT_DOMAIN,
1534 "seconds of transactions.\n"));
1540 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1543 nvlist_t *nv = NULL;
1545 uint64_t edata = UINT64_MAX;
1550 if (!hdl->libzfs_printerr)
1554 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1556 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1558 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1559 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1560 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1561 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1564 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1565 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1568 (void) printf(dgettext(TEXT_DOMAIN,
1569 "Recovery is possible, but will result in some data loss.\n"));
1571 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1572 strftime(timestr, 128, "%c", &t) != 0) {
1573 (void) printf(dgettext(TEXT_DOMAIN,
1574 "\tReturning the pool to its state as of %s\n"
1575 "\tshould correct the problem. "),
1578 (void) printf(dgettext(TEXT_DOMAIN,
1579 "\tReverting the pool to an earlier state "
1580 "should correct the problem.\n\t"));
1584 (void) printf(dgettext(TEXT_DOMAIN,
1585 "Approximately %lld minutes of data\n"
1586 "\tmust be discarded, irreversibly. "),
1587 ((longlong_t)loss + 30) / 60);
1588 } else if (loss > 0) {
1589 (void) printf(dgettext(TEXT_DOMAIN,
1590 "Approximately %lld seconds of data\n"
1591 "\tmust be discarded, irreversibly. "),
1594 if (edata != 0 && edata != UINT64_MAX) {
1596 (void) printf(dgettext(TEXT_DOMAIN,
1597 "After rewind, at least\n"
1598 "\tone persistent user-data error will remain. "));
1600 (void) printf(dgettext(TEXT_DOMAIN,
1601 "After rewind, several\n"
1602 "\tpersistent user-data errors will remain. "));
1605 (void) printf(dgettext(TEXT_DOMAIN,
1606 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1607 reason >= 0 ? "clear" : "import", name);
1609 (void) printf(dgettext(TEXT_DOMAIN,
1610 "A scrub of the pool\n"
1611 "\tis strongly recommended after recovery.\n"));
1615 (void) printf(dgettext(TEXT_DOMAIN,
1616 "Destroy and re-create the pool from\n\ta backup source.\n"));
1620 * zpool_import() is a contracted interface. Should be kept the same
1623 * Applications should use zpool_import_props() to import a pool with
1624 * new properties value to be set.
1627 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1630 nvlist_t *props = NULL;
1633 if (altroot != NULL) {
1634 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1635 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1636 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1640 if (nvlist_add_string(props,
1641 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1642 nvlist_add_string(props,
1643 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1645 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1646 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1651 ret = zpool_import_props(hdl, config, newname, props,
1659 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1665 uint64_t is_log = 0;
1667 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1671 (void) printf("\t%*s%s%s\n", indent, "", name,
1672 is_log ? " [log]" : "");
1674 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1675 &child, &children) != 0)
1678 for (c = 0; c < children; c++) {
1679 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
1680 print_vdev_tree(hdl, vname, child[c], indent + 2);
1686 zpool_print_unsup_feat(nvlist_t *config)
1688 nvlist_t *nvinfo, *unsup_feat;
1691 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1693 verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1696 for (nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1697 nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1700 verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1701 verify(nvpair_value_string(nvp, &desc) == 0);
1703 if (strlen(desc) > 0)
1704 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1706 (void) printf("\t%s\n", nvpair_name(nvp));
1711 * Import the given pool using the known configuration and a list of
1712 * properties to be set. The configuration should have come from
1713 * zpool_find_import(). The 'newname' parameters control whether the pool
1714 * is imported with a different name.
1717 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1718 nvlist_t *props, int flags)
1720 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1721 zpool_rewind_policy_t policy;
1722 nvlist_t *nv = NULL;
1723 nvlist_t *nvinfo = NULL;
1724 nvlist_t *missing = NULL;
1731 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1734 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1735 "cannot import pool '%s'"), origname);
1737 if (newname != NULL) {
1738 if (!zpool_name_valid(hdl, B_FALSE, newname))
1739 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1740 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1742 thename = (char *)newname;
1749 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1751 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1754 if ((props = zpool_valid_proplist(hdl, origname,
1755 props, version, flags, errbuf)) == NULL) {
1757 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1763 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1765 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1768 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1772 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1777 zc.zc_cookie = flags;
1778 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1780 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1781 zcmd_free_nvlists(&zc);
1788 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1789 zpool_get_rewind_policy(config, &policy);
1795 * Dry-run failed, but we print out what success
1796 * looks like if we found a best txg
1798 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1799 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1805 if (newname == NULL)
1806 (void) snprintf(desc, sizeof (desc),
1807 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1810 (void) snprintf(desc, sizeof (desc),
1811 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1816 if (nv != NULL && nvlist_lookup_nvlist(nv,
1817 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1818 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1819 (void) printf(dgettext(TEXT_DOMAIN, "This "
1820 "pool uses the following feature(s) not "
1821 "supported by this system:\n"));
1822 zpool_print_unsup_feat(nv);
1823 if (nvlist_exists(nvinfo,
1824 ZPOOL_CONFIG_CAN_RDONLY)) {
1825 (void) printf(dgettext(TEXT_DOMAIN,
1826 "All unsupported features are only "
1827 "required for writing to the pool."
1828 "\nThe pool can be imported using "
1829 "'-o readonly=on'.\n"));
1833 * Unsupported version.
1835 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1839 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1843 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1844 "one or more devices is read only"));
1845 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1849 if (nv && nvlist_lookup_nvlist(nv,
1850 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1851 nvlist_lookup_nvlist(nvinfo,
1852 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1853 (void) printf(dgettext(TEXT_DOMAIN,
1854 "The devices below are missing, use "
1855 "'-m' to import the pool anyway:\n"));
1856 print_vdev_tree(hdl, NULL, missing, 2);
1857 (void) printf("\n");
1859 (void) zpool_standard_error(hdl, error, desc);
1863 (void) zpool_standard_error(hdl, error, desc);
1867 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1868 "one or more devices are already in use\n"));
1869 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1873 (void) zpool_standard_error(hdl, error, desc);
1874 zpool_explain_recover(hdl,
1875 newname ? origname : thename, -error, nv);
1882 zpool_handle_t *zhp;
1885 * This should never fail, but play it safe anyway.
1887 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1889 else if (zhp != NULL)
1891 if (policy.zrp_request &
1892 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1893 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1894 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1900 zcmd_free_nvlists(&zc);
1910 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1912 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1914 libzfs_handle_t *hdl = zhp->zpool_hdl;
1916 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1917 zc.zc_cookie = func;
1919 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
1920 (errno == ENOENT && func != POOL_SCAN_NONE))
1923 if (func == POOL_SCAN_SCRUB) {
1924 (void) snprintf(msg, sizeof (msg),
1925 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1926 } else if (func == POOL_SCAN_NONE) {
1927 (void) snprintf(msg, sizeof (msg),
1928 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1931 assert(!"unexpected result");
1934 if (errno == EBUSY) {
1936 pool_scan_stat_t *ps = NULL;
1939 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1940 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1941 (void) nvlist_lookup_uint64_array(nvroot,
1942 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1943 if (ps && ps->pss_func == POOL_SCAN_SCRUB)
1944 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1946 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1947 } else if (errno == ENOENT) {
1948 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1950 return (zpool_standard_error(hdl, errno, msg));
1955 * Find a vdev that matches the search criteria specified. We use the
1956 * the nvpair name to determine how we should look for the device.
1957 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1958 * spare; but FALSE if its an INUSE spare.
1961 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1962 boolean_t *l2cache, boolean_t *log)
1969 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1971 /* Nothing to look for */
1972 if (search == NULL || pair == NULL)
1975 /* Obtain the key we will use to search */
1976 srchkey = nvpair_name(pair);
1978 switch (nvpair_type(pair)) {
1979 case DATA_TYPE_UINT64:
1980 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1981 uint64_t srchval, theguid;
1983 verify(nvpair_value_uint64(pair, &srchval) == 0);
1984 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1986 if (theguid == srchval)
1991 case DATA_TYPE_STRING: {
1992 char *srchval, *val;
1994 verify(nvpair_value_string(pair, &srchval) == 0);
1995 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1999 * Search for the requested value. Special cases:
2001 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2002 * "-part1", or "p1". The suffix is hidden from the user,
2003 * but included in the string, so this matches around it.
2004 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2005 * is used to check all possible expanded paths.
2006 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2008 * Otherwise, all other searches are simple string compares.
2010 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) {
2011 uint64_t wholedisk = 0;
2013 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2015 if (zfs_strcmp_pathname(srchval, val, wholedisk) == 0)
2018 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2019 char *type, *idx, *end, *p;
2020 uint64_t id, vdev_id;
2023 * Determine our vdev type, keeping in mind
2024 * that the srchval is composed of a type and
2025 * vdev id pair (i.e. mirror-4).
2027 if ((type = strdup(srchval)) == NULL)
2030 if ((p = strrchr(type, '-')) == NULL) {
2038 * If the types don't match then keep looking.
2040 if (strncmp(val, type, strlen(val)) != 0) {
2045 verify(strncmp(type, VDEV_TYPE_RAIDZ,
2046 strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2047 strncmp(type, VDEV_TYPE_MIRROR,
2048 strlen(VDEV_TYPE_MIRROR)) == 0);
2049 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2053 vdev_id = strtoull(idx, &end, 10);
2060 * Now verify that we have the correct vdev id.
2069 if (strcmp(srchval, val) == 0)
2078 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2079 &child, &children) != 0)
2082 for (c = 0; c < children; c++) {
2083 if ((ret = vdev_to_nvlist_iter(child[c], search,
2084 avail_spare, l2cache, NULL)) != NULL) {
2086 * The 'is_log' value is only set for the toplevel
2087 * vdev, not the leaf vdevs. So we always lookup the
2088 * log device from the root of the vdev tree (where
2089 * 'log' is non-NULL).
2092 nvlist_lookup_uint64(child[c],
2093 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2101 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2102 &child, &children) == 0) {
2103 for (c = 0; c < children; c++) {
2104 if ((ret = vdev_to_nvlist_iter(child[c], search,
2105 avail_spare, l2cache, NULL)) != NULL) {
2106 *avail_spare = B_TRUE;
2112 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2113 &child, &children) == 0) {
2114 for (c = 0; c < children; c++) {
2115 if ((ret = vdev_to_nvlist_iter(child[c], search,
2116 avail_spare, l2cache, NULL)) != NULL) {
2127 * Given a physical path (minus the "/devices" prefix), find the
2131 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2132 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2134 nvlist_t *search, *nvroot, *ret;
2136 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2137 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2139 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2142 *avail_spare = B_FALSE;
2146 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2147 nvlist_free(search);
2153 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2156 zpool_vdev_is_interior(const char *name)
2158 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2159 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2165 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2166 boolean_t *l2cache, boolean_t *log)
2169 nvlist_t *nvroot, *search, *ret;
2172 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2174 guid = strtoull(path, &end, 10);
2175 if (guid != 0 && *end == '\0') {
2176 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2177 } else if (zpool_vdev_is_interior(path)) {
2178 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2180 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2183 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2186 *avail_spare = B_FALSE;
2190 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2191 nvlist_free(search);
2197 vdev_online(nvlist_t *nv)
2201 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2202 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2203 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2210 * Helper function for zpool_get_physpaths().
2213 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2214 size_t *bytes_written)
2216 size_t bytes_left, pos, rsz;
2220 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2222 return (EZFS_NODEVICE);
2224 pos = *bytes_written;
2225 bytes_left = physpath_size - pos;
2226 format = (pos == 0) ? "%s" : " %s";
2228 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2229 *bytes_written += rsz;
2231 if (rsz >= bytes_left) {
2232 /* if physpath was not copied properly, clear it */
2233 if (bytes_left != 0) {
2236 return (EZFS_NOSPC);
2242 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2243 size_t *rsz, boolean_t is_spare)
2248 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2249 return (EZFS_INVALCONFIG);
2251 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2253 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2254 * For a spare vdev, we only want to boot from the active
2259 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2262 return (EZFS_INVALCONFIG);
2265 if (vdev_online(nv)) {
2266 if ((ret = vdev_get_one_physpath(nv, physpath,
2267 phypath_size, rsz)) != 0)
2270 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2271 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2272 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2277 if (nvlist_lookup_nvlist_array(nv,
2278 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2279 return (EZFS_INVALCONFIG);
2281 for (i = 0; i < count; i++) {
2282 ret = vdev_get_physpaths(child[i], physpath,
2283 phypath_size, rsz, is_spare);
2284 if (ret == EZFS_NOSPC)
2289 return (EZFS_POOL_INVALARG);
2293 * Get phys_path for a root pool config.
2294 * Return 0 on success; non-zero on failure.
2297 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2300 nvlist_t *vdev_root;
2307 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2309 return (EZFS_INVALCONFIG);
2311 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2312 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2313 &child, &count) != 0)
2314 return (EZFS_INVALCONFIG);
2316 #if defined(__sun__) || defined(__sun)
2318 * root pool can not have EFI labeled disks and can only have
2319 * a single top-level vdev.
2321 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
2322 pool_uses_efi(vdev_root))
2323 return (EZFS_POOL_INVALARG);
2326 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2329 /* No online devices */
2331 return (EZFS_NODEVICE);
2337 * Get phys_path for a root pool
2338 * Return 0 on success; non-zero on failure.
2341 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2343 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2348 * If the device has being dynamically expanded then we need to relabel
2349 * the disk to use the new unallocated space.
2352 zpool_relabel_disk(libzfs_handle_t *hdl, const char *path, const char *msg)
2356 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
2357 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2358 "relabel '%s': unable to open device: %d"), path, errno);
2359 return (zfs_error(hdl, EZFS_OPENFAILED, msg));
2363 * It's possible that we might encounter an error if the device
2364 * does not have any unallocated space left. If so, we simply
2365 * ignore that error and continue on.
2367 * Also, we don't call efi_rescan() - that would just return EBUSY.
2368 * The module will do it for us in vdev_disk_open().
2370 error = efi_use_whole_disk(fd);
2372 if (error && error != VT_ENOSPC) {
2373 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2374 "relabel '%s': unable to read disk capacity"), path);
2375 return (zfs_error(hdl, EZFS_NOCAP, msg));
2381 * Bring the specified vdev online. The 'flags' parameter is a set of the
2382 * ZFS_ONLINE_* flags.
2385 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2386 vdev_state_t *newstate)
2388 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2391 boolean_t avail_spare, l2cache, islog;
2392 libzfs_handle_t *hdl = zhp->zpool_hdl;
2395 if (flags & ZFS_ONLINE_EXPAND) {
2396 (void) snprintf(msg, sizeof (msg),
2397 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2399 (void) snprintf(msg, sizeof (msg),
2400 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2403 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2404 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2406 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2408 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2411 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2413 if (flags & ZFS_ONLINE_EXPAND ||
2414 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2415 uint64_t wholedisk = 0;
2417 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2421 * XXX - L2ARC 1.0 devices can't support expansion.
2424 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2425 "cannot expand cache devices"));
2426 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2430 const char *fullpath = path;
2431 char buf[MAXPATHLEN];
2433 if (path[0] != '/') {
2434 error = zfs_resolve_shortname(path, buf,
2437 return (zfs_error(hdl, EZFS_NODEVICE,
2443 error = zpool_relabel_disk(hdl, fullpath, msg);
2449 zc.zc_cookie = VDEV_STATE_ONLINE;
2452 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2453 if (errno == EINVAL) {
2454 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2455 "from this pool into a new one. Use '%s' "
2456 "instead"), "zpool detach");
2457 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2459 return (zpool_standard_error(hdl, errno, msg));
2462 *newstate = zc.zc_cookie;
2467 * Take the specified vdev offline
2470 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2472 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2475 boolean_t avail_spare, l2cache;
2476 libzfs_handle_t *hdl = zhp->zpool_hdl;
2478 (void) snprintf(msg, sizeof (msg),
2479 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2481 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2482 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2484 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2486 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2489 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2491 zc.zc_cookie = VDEV_STATE_OFFLINE;
2492 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2494 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2501 * There are no other replicas of this device.
2503 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2507 * The log device has unplayed logs
2509 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2512 return (zpool_standard_error(hdl, errno, msg));
2517 * Mark the given vdev faulted.
2520 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2522 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2524 libzfs_handle_t *hdl = zhp->zpool_hdl;
2526 (void) snprintf(msg, sizeof (msg),
2527 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
2529 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2531 zc.zc_cookie = VDEV_STATE_FAULTED;
2534 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2541 * There are no other replicas of this device.
2543 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2546 return (zpool_standard_error(hdl, errno, msg));
2552 * Mark the given vdev degraded.
2555 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2557 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2559 libzfs_handle_t *hdl = zhp->zpool_hdl;
2561 (void) snprintf(msg, sizeof (msg),
2562 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid);
2564 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2566 zc.zc_cookie = VDEV_STATE_DEGRADED;
2569 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2572 return (zpool_standard_error(hdl, errno, msg));
2576 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2580 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2586 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2588 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2591 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2592 children == 2 && child[which] == tgt)
2595 for (c = 0; c < children; c++)
2596 if (is_replacing_spare(child[c], tgt, which))
2604 * Attach new_disk (fully described by nvroot) to old_disk.
2605 * If 'replacing' is specified, the new disk will replace the old one.
2608 zpool_vdev_attach(zpool_handle_t *zhp,
2609 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2611 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2615 boolean_t avail_spare, l2cache, islog;
2620 nvlist_t *config_root;
2621 libzfs_handle_t *hdl = zhp->zpool_hdl;
2622 boolean_t rootpool = zpool_is_bootable(zhp);
2625 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2626 "cannot replace %s with %s"), old_disk, new_disk);
2628 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2629 "cannot attach %s to %s"), new_disk, old_disk);
2631 #if defined(__sun__) || defined(__sun)
2633 * If this is a root pool, make sure that we're not attaching an
2634 * EFI labeled device.
2636 if (rootpool && pool_uses_efi(nvroot)) {
2637 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2638 "EFI labeled devices are not supported on root pools."));
2639 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2643 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2644 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2646 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2649 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2652 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2654 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2655 zc.zc_cookie = replacing;
2657 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2658 &child, &children) != 0 || children != 1) {
2659 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2660 "new device must be a single disk"));
2661 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2664 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2665 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2667 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2671 * If the target is a hot spare that has been swapped in, we can only
2672 * replace it with another hot spare.
2675 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2676 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2677 NULL) == NULL || !avail_spare) &&
2678 is_replacing_spare(config_root, tgt, 1)) {
2679 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2680 "can only be replaced by another hot spare"));
2682 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2687 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2690 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2692 zcmd_free_nvlists(&zc);
2697 * XXX need a better way to prevent user from
2698 * booting up a half-baked vdev.
2700 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2701 "sure to wait until resilver is done "
2702 "before rebooting.\n"));
2710 * Can't attach to or replace this type of vdev.
2713 uint64_t version = zpool_get_prop_int(zhp,
2714 ZPOOL_PROP_VERSION, NULL);
2717 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2718 "cannot replace a log with a spare"));
2719 else if (version >= SPA_VERSION_MULTI_REPLACE)
2720 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2721 "already in replacing/spare config; wait "
2722 "for completion or use 'zpool detach'"));
2724 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2725 "cannot replace a replacing device"));
2727 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2728 "can only attach to mirrors and top-level "
2731 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2736 * The new device must be a single disk.
2738 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2739 "new device must be a single disk"));
2740 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2744 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2746 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2751 * The new device is too small.
2753 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2754 "device is too small"));
2755 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2760 * The new device has a different alignment requirement.
2762 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2763 "devices have different sector alignment"));
2764 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2769 * The resulting top-level vdev spec won't fit in the label.
2771 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2775 (void) zpool_standard_error(hdl, errno, msg);
2782 * Detach the specified device.
2785 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2787 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2790 boolean_t avail_spare, l2cache;
2791 libzfs_handle_t *hdl = zhp->zpool_hdl;
2793 (void) snprintf(msg, sizeof (msg),
2794 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2796 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2797 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2799 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2802 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2805 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2807 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2809 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2816 * Can't detach from this type of vdev.
2818 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2819 "applicable to mirror and replacing vdevs"));
2820 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2825 * There are no other replicas of this device.
2827 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2831 (void) zpool_standard_error(hdl, errno, msg);
2838 * Find a mirror vdev in the source nvlist.
2840 * The mchild array contains a list of disks in one of the top-level mirrors
2841 * of the source pool. The schild array contains a list of disks that the
2842 * user specified on the command line. We loop over the mchild array to
2843 * see if any entry in the schild array matches.
2845 * If a disk in the mchild array is found in the schild array, we return
2846 * the index of that entry. Otherwise we return -1.
2849 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2850 nvlist_t **schild, uint_t schildren)
2854 for (mc = 0; mc < mchildren; mc++) {
2856 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2857 mchild[mc], B_FALSE);
2859 for (sc = 0; sc < schildren; sc++) {
2860 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2861 schild[sc], B_FALSE);
2862 boolean_t result = (strcmp(mpath, spath) == 0);
2878 * Split a mirror pool. If newroot points to null, then a new nvlist
2879 * is generated and it is the responsibility of the caller to free it.
2882 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2883 nvlist_t *props, splitflags_t flags)
2885 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2887 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2888 nvlist_t **varray = NULL, *zc_props = NULL;
2889 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2890 libzfs_handle_t *hdl = zhp->zpool_hdl;
2892 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2895 (void) snprintf(msg, sizeof (msg),
2896 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2898 if (!zpool_name_valid(hdl, B_FALSE, newname))
2899 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2901 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2902 (void) fprintf(stderr, gettext("Internal error: unable to "
2903 "retrieve pool configuration\n"));
2907 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2909 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2912 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2913 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2914 props, vers, flags, msg)) == NULL)
2918 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2920 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2921 "Source pool is missing vdev tree"));
2923 nvlist_free(zc_props);
2927 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2930 if (*newroot == NULL ||
2931 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2932 &newchild, &newchildren) != 0)
2935 for (c = 0; c < children; c++) {
2936 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2938 nvlist_t **mchild, *vdev;
2943 * Unlike cache & spares, slogs are stored in the
2944 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2946 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2948 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2950 if (is_log || is_hole) {
2952 * Create a hole vdev and put it in the config.
2954 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2956 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2957 VDEV_TYPE_HOLE) != 0)
2959 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2964 varray[vcount++] = vdev;
2968 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
2970 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2971 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2972 "Source pool must be composed only of mirrors\n"));
2973 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2977 verify(nvlist_lookup_nvlist_array(child[c],
2978 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2980 /* find or add an entry for this top-level vdev */
2981 if (newchildren > 0 &&
2982 (entry = find_vdev_entry(zhp, mchild, mchildren,
2983 newchild, newchildren)) >= 0) {
2984 /* We found a disk that the user specified. */
2985 vdev = mchild[entry];
2988 /* User didn't specify a disk for this vdev. */
2989 vdev = mchild[mchildren - 1];
2992 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
2996 /* did we find every disk the user specified? */
2997 if (found != newchildren) {
2998 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
2999 "include at most one disk from each mirror"));
3000 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3004 /* Prepare the nvlist for populating. */
3005 if (*newroot == NULL) {
3006 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3009 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3010 VDEV_TYPE_ROOT) != 0)
3013 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3016 /* Add all the children we found */
3017 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3018 lastlog == 0 ? vcount : lastlog) != 0)
3022 * If we're just doing a dry run, exit now with success.
3025 memory_err = B_FALSE;
3030 /* now build up the config list & call the ioctl */
3031 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3034 if (nvlist_add_nvlist(newconfig,
3035 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3036 nvlist_add_string(newconfig,
3037 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3038 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3042 * The new pool is automatically part of the namespace unless we
3043 * explicitly export it.
3046 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3047 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3048 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3049 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3051 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3054 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3055 retval = zpool_standard_error(hdl, errno, msg);
3060 memory_err = B_FALSE;
3063 if (varray != NULL) {
3066 for (v = 0; v < vcount; v++)
3067 nvlist_free(varray[v]);
3070 zcmd_free_nvlists(&zc);
3072 nvlist_free(zc_props);
3074 nvlist_free(newconfig);
3076 nvlist_free(*newroot);
3084 return (no_memory(hdl));
3090 * Remove the given device. Currently, this is supported only for hot spares
3091 * and level 2 cache devices.
3094 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3096 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3099 boolean_t avail_spare, l2cache, islog;
3100 libzfs_handle_t *hdl = zhp->zpool_hdl;
3103 (void) snprintf(msg, sizeof (msg),
3104 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3106 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3107 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3109 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3111 * XXX - this should just go away.
3113 if (!avail_spare && !l2cache && !islog) {
3114 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3115 "only inactive hot spares, cache, top-level, "
3116 "or log devices can be removed"));
3117 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3120 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3121 if (islog && version < SPA_VERSION_HOLES) {
3122 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3123 "pool must be upgrade to support log removal"));
3124 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3127 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3129 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3132 return (zpool_standard_error(hdl, errno, msg));
3136 * Clear the errors for the pool, or the particular device if specified.
3139 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3141 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3144 zpool_rewind_policy_t policy;
3145 boolean_t avail_spare, l2cache;
3146 libzfs_handle_t *hdl = zhp->zpool_hdl;
3147 nvlist_t *nvi = NULL;
3151 (void) snprintf(msg, sizeof (msg),
3152 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3155 (void) snprintf(msg, sizeof (msg),
3156 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3159 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3161 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3162 &l2cache, NULL)) == 0)
3163 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3166 * Don't allow error clearing for hot spares. Do allow
3167 * error clearing for l2cache devices.
3170 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3172 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3176 zpool_get_rewind_policy(rewindnvl, &policy);
3177 zc.zc_cookie = policy.zrp_request;
3179 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3182 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3185 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3187 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3188 zcmd_free_nvlists(&zc);
3193 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
3194 errno != EPERM && errno != EACCES)) {
3195 if (policy.zrp_request &
3196 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3197 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3198 zpool_rewind_exclaim(hdl, zc.zc_name,
3199 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
3203 zcmd_free_nvlists(&zc);
3207 zcmd_free_nvlists(&zc);
3208 return (zpool_standard_error(hdl, errno, msg));
3212 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3215 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3217 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3219 libzfs_handle_t *hdl = zhp->zpool_hdl;
3221 (void) snprintf(msg, sizeof (msg),
3222 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3223 (u_longlong_t)guid);
3225 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3227 zc.zc_cookie = ZPOOL_NO_REWIND;
3229 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3232 return (zpool_standard_error(hdl, errno, msg));
3236 * Change the GUID for a pool.
3239 zpool_reguid(zpool_handle_t *zhp)
3242 libzfs_handle_t *hdl = zhp->zpool_hdl;
3243 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3245 (void) snprintf(msg, sizeof (msg),
3246 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3248 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3249 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3252 return (zpool_standard_error(hdl, errno, msg));
3259 zpool_reopen(zpool_handle_t *zhp)
3261 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3263 libzfs_handle_t *hdl = zhp->zpool_hdl;
3265 (void) snprintf(msg, sizeof (msg),
3266 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
3269 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3270 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
3272 return (zpool_standard_error(hdl, errno, msg));
3276 * Convert from a devid string to a path.
3279 devid_to_path(char *devid_str)
3284 devid_nmlist_t *list = NULL;
3287 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3290 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3292 devid_str_free(minor);
3298 if ((path = strdup(list[0].devname)) == NULL)
3301 devid_free_nmlist(list);
3307 * Convert from a path to a devid string.
3310 path_to_devid(const char *path)
3316 if ((fd = open(path, O_RDONLY)) < 0)
3321 if (devid_get(fd, &devid) == 0) {
3322 if (devid_get_minor_name(fd, &minor) == 0)
3323 ret = devid_str_encode(devid, minor);
3325 devid_str_free(minor);
3334 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3335 * ignore any failure here, since a common case is for an unprivileged user to
3336 * type 'zpool status', and we'll display the correct information anyway.
3339 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3341 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3343 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3344 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3345 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3348 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3352 * Remove partition suffix from a vdev path. Partition suffixes may take three
3353 * forms: "-partX", "pX", or "X", where X is a string of digits. The second
3354 * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
3355 * third case only occurs when preceded by a string matching the regular
3356 * expression "^[hs]d[a-z]+", i.e. a scsi or ide disk.
3359 strip_partition(libzfs_handle_t *hdl, char *path)
3361 char *tmp = zfs_strdup(hdl, path);
3362 char *part = NULL, *d = NULL;
3364 if ((part = strstr(tmp, "-part")) && part != tmp) {
3366 } else if ((part = strrchr(tmp, 'p')) &&
3367 part > tmp + 1 && isdigit(*(part-1))) {
3369 } else if ((tmp[0] == 'h' || tmp[0] == 's') && tmp[1] == 'd') {
3370 for (d = &tmp[2]; isalpha(*d); part = ++d);
3372 if (part && d && *d != '\0') {
3373 for (; isdigit(*d); d++);
3380 #define PATH_BUF_LEN 64
3383 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3384 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3385 * We also check if this is a whole disk, in which case we strip off the
3386 * trailing 's0' slice name.
3388 * This routine is also responsible for identifying when disks have been
3389 * reconfigured in a new location. The kernel will have opened the device by
3390 * devid, but the path will still refer to the old location. To catch this, we
3391 * first do a path -> devid translation (which is fast for the common case). If
3392 * the devid matches, we're done. If not, we do a reverse devid -> path
3393 * translation and issue the appropriate ioctl() to update the path of the vdev.
3394 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3398 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3401 char *path, *devid, *type;
3403 char buf[PATH_BUF_LEN];
3404 char tmpbuf[PATH_BUF_LEN];
3408 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
3410 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3412 (void) snprintf(buf, sizeof (buf), "%llu",
3413 (u_longlong_t)value);
3415 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3417 * If the device is dead (faulted, offline, etc) then don't
3418 * bother opening it. Otherwise we may be forcing the user to
3419 * open a misbehaving device, which can have undesirable
3422 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3423 (uint64_t **)&vs, &vsc) != 0 ||
3424 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3426 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3428 * Determine if the current path is correct.
3430 char *newdevid = path_to_devid(path);
3432 if (newdevid == NULL ||
3433 strcmp(devid, newdevid) != 0) {
3436 if ((newpath = devid_to_path(devid)) != NULL) {
3438 * Update the path appropriately.
3440 set_path(zhp, nv, newpath);
3441 if (nvlist_add_string(nv,
3442 ZPOOL_CONFIG_PATH, newpath) == 0)
3443 verify(nvlist_lookup_string(nv,
3451 devid_str_free(newdevid);
3455 * For a block device only use the name.
3457 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
3458 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
3459 path = strrchr(path, '/');
3464 * Remove the partition from the path it this is a whole disk.
3466 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3467 &value) == 0 && value) {
3468 return strip_partition(hdl, path);
3471 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3474 * If it's a raidz device, we need to stick in the parity level.
3476 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3478 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3480 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3481 (u_longlong_t)value);
3486 * We identify each top-level vdev by using a <type-id>
3487 * naming convention.
3492 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3494 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s-%llu",
3495 path, (u_longlong_t)id);
3500 return (zfs_strdup(hdl, path));
3504 zbookmark_compare(const void *a, const void *b)
3506 return (memcmp(a, b, sizeof (zbookmark_t)));
3510 * Retrieve the persistent error log, uniquify the members, and return to the
3514 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3516 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3518 zbookmark_t *zb = NULL;
3522 * Retrieve the raw error list from the kernel. If the number of errors
3523 * has increased, allocate more space and continue until we get the
3526 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3530 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3531 count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
3533 zc.zc_nvlist_dst_size = count;
3534 (void) strcpy(zc.zc_name, zhp->zpool_name);
3536 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3538 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3539 if (errno == ENOMEM) {
3540 count = zc.zc_nvlist_dst_size;
3541 if ((zc.zc_nvlist_dst = (uintptr_t)
3542 zfs_alloc(zhp->zpool_hdl, count *
3543 sizeof (zbookmark_t))) == (uintptr_t)NULL)
3554 * Sort the resulting bookmarks. This is a little confusing due to the
3555 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3556 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3557 * _not_ copied as part of the process. So we point the start of our
3558 * array appropriate and decrement the total number of elements.
3560 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
3561 zc.zc_nvlist_dst_size;
3562 count -= zc.zc_nvlist_dst_size;
3564 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
3566 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3569 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3571 for (i = 0; i < count; i++) {
3574 /* ignoring zb_blkid and zb_level for now */
3575 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3576 zb[i-1].zb_object == zb[i].zb_object)
3579 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3581 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3582 zb[i].zb_objset) != 0) {
3586 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3587 zb[i].zb_object) != 0) {
3591 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3598 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3602 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3603 return (no_memory(zhp->zpool_hdl));
3607 * Upgrade a ZFS pool to the latest on-disk version.
3610 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3612 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3613 libzfs_handle_t *hdl = zhp->zpool_hdl;
3615 (void) strcpy(zc.zc_name, zhp->zpool_name);
3616 zc.zc_cookie = new_version;
3618 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3619 return (zpool_standard_error_fmt(hdl, errno,
3620 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3626 zpool_set_history_str(const char *subcommand, int argc, char **argv,
3631 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
3632 for (i = 1; i < argc; i++) {
3633 if (strlen(history_str) + 1 + strlen(argv[i]) >
3636 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
3637 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
3642 * Stage command history for logging.
3645 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
3647 if (history_str == NULL)
3650 if (strlen(history_str) > HIS_MAX_RECORD_LEN)
3653 if (hdl->libzfs_log_str != NULL)
3654 free(hdl->libzfs_log_str);
3656 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
3657 return (no_memory(hdl));
3663 * Perform ioctl to get some command history of a pool.
3665 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3666 * logical offset of the history buffer to start reading from.
3668 * Upon return, 'off' is the next logical offset to read from and
3669 * 'len' is the actual amount of bytes read into 'buf'.
3672 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3674 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3675 libzfs_handle_t *hdl = zhp->zpool_hdl;
3677 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3679 zc.zc_history = (uint64_t)(uintptr_t)buf;
3680 zc.zc_history_len = *len;
3681 zc.zc_history_offset = *off;
3683 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3686 return (zfs_error_fmt(hdl, EZFS_PERM,
3687 dgettext(TEXT_DOMAIN,
3688 "cannot show history for pool '%s'"),
3691 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3692 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3693 "'%s'"), zhp->zpool_name));
3695 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3696 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3697 "'%s', pool must be upgraded"), zhp->zpool_name));
3699 return (zpool_standard_error_fmt(hdl, errno,
3700 dgettext(TEXT_DOMAIN,
3701 "cannot get history for '%s'"), zhp->zpool_name));
3705 *len = zc.zc_history_len;
3706 *off = zc.zc_history_offset;
3712 * Process the buffer of nvlists, unpacking and storing each nvlist record
3713 * into 'records'. 'leftover' is set to the number of bytes that weren't
3714 * processed as there wasn't a complete record.
3717 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3718 nvlist_t ***records, uint_t *numrecords)
3724 while (bytes_read > sizeof (reclen)) {
3726 /* get length of packed record (stored as little endian) */
3727 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3728 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3730 if (bytes_read < sizeof (reclen) + reclen)
3734 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3736 bytes_read -= sizeof (reclen) + reclen;
3737 buf += sizeof (reclen) + reclen;
3739 /* add record to nvlist array */
3741 if (ISP2(*numrecords + 1)) {
3742 *records = realloc(*records,
3743 *numrecords * 2 * sizeof (nvlist_t *));
3745 (*records)[*numrecords - 1] = nv;
3748 *leftover = bytes_read;
3752 #define HIS_BUF_LEN (128*1024)
3755 * Retrieve the command history of a pool.
3758 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3760 char buf[HIS_BUF_LEN];
3762 nvlist_t **records = NULL;
3763 uint_t numrecords = 0;
3767 uint64_t bytes_read = sizeof (buf);
3770 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3773 /* if nothing else was read in, we're at EOF, just return */
3777 if ((err = zpool_history_unpack(buf, bytes_read,
3778 &leftover, &records, &numrecords)) != 0)
3786 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3787 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3788 records, numrecords) == 0);
3790 for (i = 0; i < numrecords; i++)
3791 nvlist_free(records[i]);
3798 * Retrieve the next event. If there is a new event available 'nvp' will
3799 * contain a newly allocated nvlist and 'dropped' will be set to the number
3800 * of missed events since the last call to this function. When 'nvp' is
3801 * set to NULL it indicates no new events are available. In either case
3802 * the function returns 0 and it is up to the caller to free 'nvp'. In
3803 * the case of a fatal error the function will return a non-zero value.
3804 * When the function is called in blocking mode it will not return until
3805 * a new event is available.
3808 zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
3809 int *dropped, int block, int cleanup_fd)
3811 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3816 zc.zc_cleanup_fd = cleanup_fd;
3819 zc.zc_guid = ZEVENT_NONBLOCK;
3821 if (zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE) != 0)
3825 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
3828 error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
3829 dgettext(TEXT_DOMAIN, "zfs shutdown"));
3832 /* Blocking error case should not occur */
3834 error = zpool_standard_error_fmt(hdl, errno,
3835 dgettext(TEXT_DOMAIN, "cannot get event"));
3839 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3840 error = zfs_error_fmt(hdl, EZFS_NOMEM,
3841 dgettext(TEXT_DOMAIN, "cannot get event"));
3847 error = zpool_standard_error_fmt(hdl, errno,
3848 dgettext(TEXT_DOMAIN, "cannot get event"));
3853 error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
3857 *dropped = (int)zc.zc_cookie;
3859 zcmd_free_nvlists(&zc);
3868 zpool_events_clear(libzfs_handle_t *hdl, int *count)
3870 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3873 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3874 "cannot clear events"));
3876 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
3877 return (zpool_standard_error_fmt(hdl, errno, msg));
3880 *count = (int)zc.zc_cookie; /* # of events cleared */
3886 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3887 char *pathname, size_t len)
3889 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3890 boolean_t mounted = B_FALSE;
3891 char *mntpnt = NULL;
3892 char dsname[MAXNAMELEN];
3895 /* special case for the MOS */
3896 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", (longlong_t)obj);
3900 /* get the dataset's name */
3901 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3903 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3904 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3905 /* just write out a path of two object numbers */
3906 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3907 (longlong_t)dsobj, (longlong_t)obj);
3910 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3912 /* find out if the dataset is mounted */
3913 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3915 /* get the corrupted object's path */
3916 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3918 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3921 (void) snprintf(pathname, len, "%s%s", mntpnt,
3924 (void) snprintf(pathname, len, "%s:%s",
3925 dsname, zc.zc_value);
3928 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, (longlong_t)obj);
3934 * Read the EFI label from the config, if a label does not exist then
3935 * pass back the error to the caller. If the caller has passed a non-NULL
3936 * diskaddr argument then we set it to the starting address of the EFI
3940 read_efi_label(nvlist_t *config, diskaddr_t *sb)
3944 char diskname[MAXPATHLEN];
3947 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3950 (void) snprintf(diskname, sizeof (diskname), "%s%s", DISK_ROOT,
3951 strrchr(path, '/'));
3952 if ((fd = open(diskname, O_RDWR|O_DIRECT)) >= 0) {
3953 struct dk_gpt *vtoc;
3955 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3957 *sb = vtoc->efi_parts[0].p_start;
3966 * determine where a partition starts on a disk in the current
3970 find_start_block(nvlist_t *config)
3974 diskaddr_t sb = MAXOFFSET_T;
3977 if (nvlist_lookup_nvlist_array(config,
3978 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3979 if (nvlist_lookup_uint64(config,
3980 ZPOOL_CONFIG_WHOLE_DISK,
3981 &wholedisk) != 0 || !wholedisk) {
3982 return (MAXOFFSET_T);
3984 if (read_efi_label(config, &sb) < 0)
3989 for (c = 0; c < children; c++) {
3990 sb = find_start_block(child[c]);
3991 if (sb != MAXOFFSET_T) {
3995 return (MAXOFFSET_T);
3999 zpool_label_disk_wait(char *path, int timeout)
4001 struct stat64 statbuf;
4005 * Wait timeout miliseconds for a newly created device to be available
4006 * from the given path. There is a small window when a /dev/ device
4007 * will exist and the udev link will not, so we must wait for the
4008 * symlink. Depending on the udev rules this may take a few seconds.
4010 for (i = 0; i < timeout; i++) {
4014 if ((stat64(path, &statbuf) == 0) && (errno == 0))
4022 zpool_label_disk_check(char *path)
4024 struct dk_gpt *vtoc;
4027 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0)
4030 if ((err = efi_alloc_and_read(fd, &vtoc)) != 0) {
4035 if (vtoc->efi_flags & EFI_GPT_PRIMARY_CORRUPT) {
4047 * Label an individual disk. The name provided is the short name,
4048 * stripped of any leading /dev path.
4051 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
4053 char path[MAXPATHLEN];
4054 struct dk_gpt *vtoc;
4056 size_t resv = EFI_MIN_RESV_SIZE;
4057 uint64_t slice_size;
4058 diskaddr_t start_block;
4061 /* prepare an error message just in case */
4062 (void) snprintf(errbuf, sizeof (errbuf),
4063 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
4068 #if defined(__sun__) || defined(__sun)
4069 if (zpool_is_bootable(zhp)) {
4070 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4071 "EFI labeled devices are not supported on root "
4073 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
4077 verify(nvlist_lookup_nvlist(zhp->zpool_config,
4078 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4080 if (zhp->zpool_start_block == 0)
4081 start_block = find_start_block(nvroot);
4083 start_block = zhp->zpool_start_block;
4084 zhp->zpool_start_block = start_block;
4087 start_block = NEW_START_BLOCK;
4090 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
4092 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
4094 * This shouldn't happen. We've long since verified that this
4095 * is a valid device.
4097 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
4098 "label '%s': unable to open device: %d"), path, errno);
4099 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
4102 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
4104 * The only way this can fail is if we run out of memory, or we
4105 * were unable to read the disk's capacity
4107 if (errno == ENOMEM)
4108 (void) no_memory(hdl);
4111 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
4112 "label '%s': unable to read disk capacity"), path);
4114 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
4117 slice_size = vtoc->efi_last_u_lba + 1;
4118 slice_size -= EFI_MIN_RESV_SIZE;
4119 if (start_block == MAXOFFSET_T)
4120 start_block = NEW_START_BLOCK;
4121 slice_size -= start_block;
4122 slice_size = P2ALIGN(slice_size, PARTITION_END_ALIGNMENT);
4124 vtoc->efi_parts[0].p_start = start_block;
4125 vtoc->efi_parts[0].p_size = slice_size;
4128 * Why we use V_USR: V_BACKUP confuses users, and is considered
4129 * disposable by some EFI utilities (since EFI doesn't have a backup
4130 * slice). V_UNASSIGNED is supposed to be used only for zero size
4131 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4132 * etc. were all pretty specific. V_USR is as close to reality as we
4133 * can get, in the absence of V_OTHER.
4135 vtoc->efi_parts[0].p_tag = V_USR;
4136 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
4138 vtoc->efi_parts[8].p_start = slice_size + start_block;
4139 vtoc->efi_parts[8].p_size = resv;
4140 vtoc->efi_parts[8].p_tag = V_RESERVED;
4142 if ((rval = efi_write(fd, vtoc)) != 0 || (rval = efi_rescan(fd)) != 0) {
4144 * Some block drivers (like pcata) may not support EFI
4145 * GPT labels. Print out a helpful error message dir-
4146 * ecting the user to manually label the disk and give
4152 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "try using "
4153 "parted(8) and then provide a specific slice: %d"), rval);
4154 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4160 /* Wait for the first expected partition to appear. */
4162 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
4163 (void) zfs_append_partition(path, MAXPATHLEN);
4165 rval = zpool_label_disk_wait(path, 3000);
4167 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "failed to "
4168 "detect device partitions on '%s': %d"), path, rval);
4169 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4172 /* We can't be to paranoid. Read the label back and verify it. */
4173 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
4174 rval = zpool_label_disk_check(path);
4176 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "freshly written "
4177 "EFI label on '%s' is damaged. Ensure\nthis device "
4178 "is not in in use, and is functioning properly: %d"),
4180 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));