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.
26 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
29 #include <sys/types.h>
41 #include <sys/zfs_ioctl.h>
44 #include "zfs_namecheck.h"
46 #include "libzfs_impl.h"
47 #include "zfs_comutil.h"
48 #include "zfeature_common.h"
50 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
52 #define DISK_ROOT "/dev/dsk"
53 #define RDISK_ROOT "/dev/rdsk"
54 #define BACKUP_SLICE "s2"
56 typedef struct prop_flags {
57 int create:1; /* Validate property on creation */
58 int import:1; /* Validate property on import */
62 * ====================================================================
63 * zpool property functions
64 * ====================================================================
68 zpool_get_all_props(zpool_handle_t *zhp)
71 libzfs_handle_t *hdl = zhp->zpool_hdl;
73 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
75 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
78 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
79 if (errno == ENOMEM) {
80 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
81 zcmd_free_nvlists(&zc);
85 zcmd_free_nvlists(&zc);
90 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
91 zcmd_free_nvlists(&zc);
95 zcmd_free_nvlists(&zc);
101 zpool_props_refresh(zpool_handle_t *zhp)
105 old_props = zhp->zpool_props;
107 if (zpool_get_all_props(zhp) != 0)
110 nvlist_free(old_props);
115 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
121 zprop_source_t source;
123 nvl = zhp->zpool_props;
124 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
125 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
127 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
129 source = ZPROP_SRC_DEFAULT;
130 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
141 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
145 zprop_source_t source;
147 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
149 * zpool_get_all_props() has most likely failed because
150 * the pool is faulted, but if all we need is the top level
151 * vdev's guid then get it from the zhp config nvlist.
153 if ((prop == ZPOOL_PROP_GUID) &&
154 (nvlist_lookup_nvlist(zhp->zpool_config,
155 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
156 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
160 return (zpool_prop_default_numeric(prop));
163 nvl = zhp->zpool_props;
164 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
165 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
167 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
169 source = ZPROP_SRC_DEFAULT;
170 value = zpool_prop_default_numeric(prop);
180 * Map VDEV STATE to printed strings.
183 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
186 case VDEV_STATE_CLOSED:
187 case VDEV_STATE_OFFLINE:
188 return (gettext("OFFLINE"));
189 case VDEV_STATE_REMOVED:
190 return (gettext("REMOVED"));
191 case VDEV_STATE_CANT_OPEN:
192 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
193 return (gettext("FAULTED"));
194 else if (aux == VDEV_AUX_SPLIT_POOL)
195 return (gettext("SPLIT"));
197 return (gettext("UNAVAIL"));
198 case VDEV_STATE_FAULTED:
199 return (gettext("FAULTED"));
200 case VDEV_STATE_DEGRADED:
201 return (gettext("DEGRADED"));
202 case VDEV_STATE_HEALTHY:
203 return (gettext("ONLINE"));
206 return (gettext("UNKNOWN"));
210 * Map POOL STATE to printed strings.
213 zpool_pool_state_to_name(pool_state_t state)
216 case POOL_STATE_ACTIVE:
217 return (gettext("ACTIVE"));
218 case POOL_STATE_EXPORTED:
219 return (gettext("EXPORTED"));
220 case POOL_STATE_DESTROYED:
221 return (gettext("DESTROYED"));
222 case POOL_STATE_SPARE:
223 return (gettext("SPARE"));
224 case POOL_STATE_L2CACHE:
225 return (gettext("L2CACHE"));
226 case POOL_STATE_UNINITIALIZED:
227 return (gettext("UNINITIALIZED"));
228 case POOL_STATE_UNAVAIL:
229 return (gettext("UNAVAIL"));
230 case POOL_STATE_POTENTIALLY_ACTIVE:
231 return (gettext("POTENTIALLY_ACTIVE"));
234 return (gettext("UNKNOWN"));
238 * Get a zpool property value for 'prop' and return the value in
239 * a pre-allocated buffer.
242 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
243 zprop_source_t *srctype)
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", 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 (void) zfs_nicenum(intval, buf, len);
312 case ZPOOL_PROP_CAPACITY:
313 (void) snprintf(buf, len, "%llu%%",
314 (u_longlong_t)intval);
317 case ZPOOL_PROP_DEDUPRATIO:
318 (void) snprintf(buf, len, "%llu.%02llux",
319 (u_longlong_t)(intval / 100),
320 (u_longlong_t)(intval % 100));
323 case ZPOOL_PROP_HEALTH:
324 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
325 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
326 verify(nvlist_lookup_uint64_array(nvroot,
327 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
330 (void) strlcpy(buf, zpool_state_to_name(intval,
333 case ZPOOL_PROP_VERSION:
334 if (intval >= SPA_VERSION_FEATURES) {
335 (void) snprintf(buf, len, "-");
340 (void) snprintf(buf, len, "%llu", intval);
344 case PROP_TYPE_INDEX:
345 intval = zpool_get_prop_int(zhp, prop, &src);
346 if (zpool_prop_index_to_string(prop, intval, &strval)
349 (void) strlcpy(buf, strval, len);
363 * Check if the bootfs name has the same pool name as it is set to.
364 * Assuming bootfs is a valid dataset name.
367 bootfs_name_valid(const char *pool, char *bootfs)
369 int len = strlen(pool);
371 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
374 if (strncmp(pool, bootfs, len) == 0 &&
375 (bootfs[len] == '/' || bootfs[len] == '\0'))
382 * Inspect the configuration to determine if any of the devices contain
386 pool_uses_efi(nvlist_t *config)
392 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
393 &child, &children) != 0)
394 return (read_efi_label(config, NULL) >= 0);
396 for (c = 0; c < children; c++) {
397 if (pool_uses_efi(child[c]))
405 zpool_is_bootable(zpool_handle_t *zhp)
407 char bootfs[ZPOOL_MAXNAMELEN];
409 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
410 sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
411 sizeof (bootfs)) != 0);
416 * Given an nvlist of zpool properties to be set, validate that they are
417 * correct, and parse any numeric properties (index, boolean, etc) if they are
418 * specified as strings.
421 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
422 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
430 struct stat64 statbuf;
434 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
435 (void) no_memory(hdl);
440 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
441 const char *propname = nvpair_name(elem);
443 prop = zpool_name_to_prop(propname);
444 if (prop == ZPROP_INVAL && zpool_prop_feature(propname)) {
446 zfeature_info_t *feature;
447 char *fname = strchr(propname, '@') + 1;
449 err = zfeature_lookup_name(fname, &feature);
451 ASSERT3U(err, ==, ENOENT);
452 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
453 "invalid feature '%s'"), fname);
454 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
458 if (nvpair_type(elem) != DATA_TYPE_STRING) {
459 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
460 "'%s' must be a string"), propname);
461 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
465 (void) nvpair_value_string(elem, &strval);
466 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) {
467 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
468 "property '%s' can only be set to "
469 "'enabled'"), propname);
470 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
474 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
475 (void) no_memory(hdl);
482 * Make sure this property is valid and applies to this type.
484 if (prop == ZPROP_INVAL) {
485 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
486 "invalid property '%s'"), propname);
487 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
491 if (zpool_prop_readonly(prop)) {
492 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
493 "is readonly"), propname);
494 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
498 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
499 &strval, &intval, errbuf) != 0)
503 * Perform additional checking for specific properties.
506 case ZPOOL_PROP_VERSION:
507 if (intval < version ||
508 !SPA_VERSION_IS_SUPPORTED(intval)) {
509 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
510 "property '%s' number %d is invalid."),
512 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
517 case ZPOOL_PROP_BOOTFS:
518 if (flags.create || flags.import) {
519 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
520 "property '%s' cannot be set at creation "
521 "or import time"), propname);
522 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
526 if (version < SPA_VERSION_BOOTFS) {
527 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
528 "pool must be upgraded to support "
529 "'%s' property"), propname);
530 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
535 * bootfs property value has to be a dataset name and
536 * the dataset has to be in the same pool as it sets to.
538 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
540 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
541 "is an invalid name"), strval);
542 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
546 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
547 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
548 "could not open pool '%s'"), poolname);
549 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
552 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
553 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
557 * bootfs property cannot be set on a disk which has
560 if (pool_uses_efi(nvroot)) {
561 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
562 "property '%s' not supported on "
563 "EFI labeled devices"), propname);
564 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
572 case ZPOOL_PROP_ALTROOT:
573 if (!flags.create && !flags.import) {
574 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
575 "property '%s' can only be set during pool "
576 "creation or import"), propname);
577 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
581 if (strval[0] != '/') {
582 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
583 "bad alternate root '%s'"), strval);
584 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
589 case ZPOOL_PROP_CACHEFILE:
590 if (strval[0] == '\0')
593 if (strcmp(strval, "none") == 0)
596 if (strval[0] != '/') {
597 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
598 "property '%s' must be empty, an "
599 "absolute path, or 'none'"), propname);
600 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
604 slash = strrchr(strval, '/');
606 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
607 strcmp(slash, "/..") == 0) {
608 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
609 "'%s' is not a valid file"), strval);
610 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
616 if (strval[0] != '\0' &&
617 (stat64(strval, &statbuf) != 0 ||
618 !S_ISDIR(statbuf.st_mode))) {
619 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
620 "'%s' is not a valid directory"),
622 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
629 case ZPOOL_PROP_COMMENT:
630 for (check = strval; *check != '\0'; check++) {
631 if (!isprint(*check)) {
633 dgettext(TEXT_DOMAIN,
634 "comment may only have printable "
636 (void) zfs_error(hdl, EZFS_BADPROP,
641 if (strlen(strval) > ZPROP_MAX_COMMENT) {
642 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
643 "comment must not exceed %d characters"),
645 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
649 case ZPOOL_PROP_READONLY:
651 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
652 "property '%s' can only be set at "
653 "import time"), propname);
654 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
663 nvlist_free(retprops);
668 * Set zpool property : propname=propval.
671 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
673 zfs_cmd_t zc = { 0 };
676 nvlist_t *nvl = NULL;
679 prop_flags_t flags = { 0 };
681 (void) snprintf(errbuf, sizeof (errbuf),
682 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
685 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
686 return (no_memory(zhp->zpool_hdl));
688 if (nvlist_add_string(nvl, propname, propval) != 0) {
690 return (no_memory(zhp->zpool_hdl));
693 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
694 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
695 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
704 * Execute the corresponding ioctl() to set this property.
706 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
708 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
713 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
715 zcmd_free_nvlists(&zc);
719 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
721 (void) zpool_props_refresh(zhp);
727 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
729 libzfs_handle_t *hdl = zhp->zpool_hdl;
731 char buf[ZFS_MAXPROPLEN];
732 nvlist_t *features = NULL;
734 boolean_t firstexpand = (NULL == *plp);
736 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
740 while (*last != NULL)
741 last = &(*last)->pl_next;
744 features = zpool_get_features(zhp);
746 if ((*plp)->pl_all && firstexpand) {
747 for (int i = 0; i < SPA_FEATURES; i++) {
748 zprop_list_t *entry = zfs_alloc(hdl,
749 sizeof (zprop_list_t));
750 entry->pl_prop = ZPROP_INVAL;
751 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
752 spa_feature_table[i].fi_uname);
753 entry->pl_width = strlen(entry->pl_user_prop);
754 entry->pl_all = B_TRUE;
757 last = &entry->pl_next;
761 /* add any unsupported features */
762 for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL);
763 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
768 if (zfeature_is_supported(nvpair_name(nvp)))
771 propname = zfs_asprintf(hdl, "unsupported@%s",
775 * Before adding the property to the list make sure that no
776 * other pool already added the same property.
780 while (entry != NULL) {
781 if (entry->pl_user_prop != NULL &&
782 strcmp(propname, entry->pl_user_prop) == 0) {
786 entry = entry->pl_next;
793 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
794 entry->pl_prop = ZPROP_INVAL;
795 entry->pl_user_prop = propname;
796 entry->pl_width = strlen(entry->pl_user_prop);
797 entry->pl_all = B_TRUE;
800 last = &entry->pl_next;
803 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
808 if (entry->pl_prop != ZPROP_INVAL &&
809 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
811 if (strlen(buf) > entry->pl_width)
812 entry->pl_width = strlen(buf);
820 * Get the state for the given feature on the given ZFS pool.
823 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
827 boolean_t found = B_FALSE;
828 nvlist_t *features = zpool_get_features(zhp);
830 const char *feature = strchr(propname, '@') + 1;
832 supported = zpool_prop_feature(propname);
833 ASSERT(supported || zpool_prop_unsupported(propname));
836 * Convert from feature name to feature guid. This conversion is
837 * unecessary for unsupported@... properties because they already
844 ret = zfeature_lookup_name(feature, &fi);
846 (void) strlcpy(buf, "-", len);
849 feature = fi->fi_guid;
852 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
857 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
860 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
862 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
867 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
869 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
872 (void) strlcpy(buf, "-", len);
881 * Don't start the slice at the default block of 34; many storage
882 * devices will use a stripe width of 128k, so start there instead.
884 #define NEW_START_BLOCK 256
887 * Validate the given pool name, optionally putting an extended error message in
891 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
897 ret = pool_namecheck(pool, &why, &what);
900 * The rules for reserved pool names were extended at a later point.
901 * But we need to support users with existing pools that may now be
902 * invalid. So we only check for this expanded set of names during a
903 * create (or import), and only in userland.
905 if (ret == 0 && !isopen &&
906 (strncmp(pool, "mirror", 6) == 0 ||
907 strncmp(pool, "raidz", 5) == 0 ||
908 strncmp(pool, "spare", 5) == 0 ||
909 strcmp(pool, "log") == 0)) {
912 dgettext(TEXT_DOMAIN, "name is reserved"));
920 case NAME_ERR_TOOLONG:
922 dgettext(TEXT_DOMAIN, "name is too long"));
925 case NAME_ERR_INVALCHAR:
927 dgettext(TEXT_DOMAIN, "invalid character "
928 "'%c' in pool name"), what);
931 case NAME_ERR_NOLETTER:
932 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
933 "name must begin with a letter"));
936 case NAME_ERR_RESERVED:
937 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
938 "name is reserved"));
941 case NAME_ERR_DISKLIKE:
942 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
943 "pool name is reserved"));
946 case NAME_ERR_LEADING_SLASH:
947 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
948 "leading slash in name"));
951 case NAME_ERR_EMPTY_COMPONENT:
952 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
953 "empty component in name"));
956 case NAME_ERR_TRAILING_SLASH:
957 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
958 "trailing slash in name"));
961 case NAME_ERR_MULTIPLE_AT:
962 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
963 "multiple '@' delimiters in name"));
975 * Open a handle to the given pool, even if the pool is currently in the FAULTED
979 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
985 * Make sure the pool name is valid.
987 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
988 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
989 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
994 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
997 zhp->zpool_hdl = hdl;
998 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1000 if (zpool_refresh_stats(zhp, &missing) != 0) {
1006 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1007 (void) zfs_error_fmt(hdl, EZFS_NOENT,
1008 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1017 * Like the above, but silent on error. Used when iterating over pools (because
1018 * the configuration cache may be out of date).
1021 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1023 zpool_handle_t *zhp;
1026 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1029 zhp->zpool_hdl = hdl;
1030 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1032 if (zpool_refresh_stats(zhp, &missing) != 0) {
1048 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1052 zpool_open(libzfs_handle_t *hdl, const char *pool)
1054 zpool_handle_t *zhp;
1056 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1059 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1060 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1061 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1070 * Close the handle. Simply frees the memory associated with the handle.
1073 zpool_close(zpool_handle_t *zhp)
1075 if (zhp->zpool_config)
1076 nvlist_free(zhp->zpool_config);
1077 if (zhp->zpool_old_config)
1078 nvlist_free(zhp->zpool_old_config);
1079 if (zhp->zpool_props)
1080 nvlist_free(zhp->zpool_props);
1085 * Return the name of the pool.
1088 zpool_get_name(zpool_handle_t *zhp)
1090 return (zhp->zpool_name);
1095 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1098 zpool_get_state(zpool_handle_t *zhp)
1100 return (zhp->zpool_state);
1104 * Create the named pool, using the provided vdev list. It is assumed
1105 * that the consumer has already validated the contents of the nvlist, so we
1106 * don't have to worry about error semantics.
1109 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1110 nvlist_t *props, nvlist_t *fsprops)
1112 zfs_cmd_t zc = { 0 };
1113 nvlist_t *zc_fsprops = NULL;
1114 nvlist_t *zc_props = NULL;
1118 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1119 "cannot create '%s'"), pool);
1121 if (!zpool_name_valid(hdl, B_FALSE, pool))
1122 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1124 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1128 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1130 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1131 SPA_VERSION_1, flags, msg)) == NULL) {
1140 zoned = ((nvlist_lookup_string(fsprops,
1141 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1142 strcmp(zonestr, "on") == 0);
1144 if ((zc_fsprops = zfs_valid_proplist(hdl,
1145 ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
1149 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1152 if (nvlist_add_nvlist(zc_props,
1153 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1158 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1161 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1163 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1165 zcmd_free_nvlists(&zc);
1166 nvlist_free(zc_props);
1167 nvlist_free(zc_fsprops);
1172 * This can happen if the user has specified the same
1173 * device multiple times. We can't reliably detect this
1174 * until we try to add it and see we already have a
1177 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1178 "one or more vdevs refer to the same device"));
1179 return (zfs_error(hdl, EZFS_BADDEV, msg));
1183 * This occurs when one of the devices is below
1184 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1185 * device was the problem device since there's no
1186 * reliable way to determine device size from userland.
1191 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1193 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1194 "one or more devices is less than the "
1195 "minimum size (%s)"), buf);
1197 return (zfs_error(hdl, EZFS_BADDEV, msg));
1200 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1201 "one or more devices is out of space"));
1202 return (zfs_error(hdl, EZFS_BADDEV, msg));
1205 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1206 "cache device must be a disk or disk slice"));
1207 return (zfs_error(hdl, EZFS_BADDEV, msg));
1210 return (zpool_standard_error(hdl, errno, msg));
1215 zcmd_free_nvlists(&zc);
1216 nvlist_free(zc_props);
1217 nvlist_free(zc_fsprops);
1222 * Destroy the given pool. It is up to the caller to ensure that there are no
1223 * datasets left in the pool.
1226 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1228 zfs_cmd_t zc = { 0 };
1229 zfs_handle_t *zfp = NULL;
1230 libzfs_handle_t *hdl = zhp->zpool_hdl;
1233 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1234 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1237 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1238 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1240 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1241 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1242 "cannot destroy '%s'"), zhp->zpool_name);
1244 if (errno == EROFS) {
1245 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1246 "one or more devices is read only"));
1247 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1249 (void) zpool_standard_error(hdl, errno, msg);
1258 remove_mountpoint(zfp);
1266 * Add the given vdevs to the pool. The caller must have already performed the
1267 * necessary verification to ensure that the vdev specification is well-formed.
1270 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1272 zfs_cmd_t zc = { 0 };
1274 libzfs_handle_t *hdl = zhp->zpool_hdl;
1276 nvlist_t **spares, **l2cache;
1277 uint_t nspares, nl2cache;
1279 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1280 "cannot add to '%s'"), zhp->zpool_name);
1282 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1283 SPA_VERSION_SPARES &&
1284 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1285 &spares, &nspares) == 0) {
1286 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1287 "upgraded to add hot spares"));
1288 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1291 if (zpool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1292 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1295 for (s = 0; s < nspares; s++) {
1298 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1299 &path) == 0 && pool_uses_efi(spares[s])) {
1300 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1301 "device '%s' contains an EFI label and "
1302 "cannot be used on root pools."),
1303 zpool_vdev_name(hdl, NULL, spares[s],
1305 return (zfs_error(hdl, EZFS_POOL_NOTSUP, 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);
1339 * This occurrs when one of the devices is below
1340 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1341 * device was the problem device since there's no
1342 * reliable way to determine device size from userland.
1347 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1349 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1350 "device is less than the minimum "
1353 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1357 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1358 "pool must be upgraded to add these vdevs"));
1359 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1363 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1364 "root pool can not have multiple vdevs"
1365 " or separate logs"));
1366 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1370 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1371 "cache device must be a disk or disk slice"));
1372 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1376 (void) zpool_standard_error(hdl, errno, msg);
1384 zcmd_free_nvlists(&zc);
1390 * Exports the pool from the system. The caller must ensure that there are no
1391 * mounted datasets in the pool.
1394 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1395 const char *log_str)
1397 zfs_cmd_t zc = { 0 };
1400 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1401 "cannot export '%s'"), zhp->zpool_name);
1403 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1404 zc.zc_cookie = force;
1405 zc.zc_guid = hardforce;
1406 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1408 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1411 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1412 "use '-f' to override the following errors:\n"
1413 "'%s' has an active shared spare which could be"
1414 " used by other pools once '%s' is exported."),
1415 zhp->zpool_name, zhp->zpool_name);
1416 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1419 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1428 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1430 return (zpool_export_common(zhp, force, B_FALSE, log_str));
1434 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1436 return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1440 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1443 nvlist_t *nv = NULL;
1449 if (!hdl->libzfs_printerr || config == NULL)
1452 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1453 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1457 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1459 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1461 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1462 strftime(timestr, 128, 0, &t) != 0) {
1464 (void) printf(dgettext(TEXT_DOMAIN,
1465 "Would be able to return %s "
1466 "to its state as of %s.\n"),
1469 (void) printf(dgettext(TEXT_DOMAIN,
1470 "Pool %s returned to its state as of %s.\n"),
1474 (void) printf(dgettext(TEXT_DOMAIN,
1475 "%s approximately %lld "),
1476 dryrun ? "Would discard" : "Discarded",
1478 (void) printf(dgettext(TEXT_DOMAIN,
1479 "minutes of transactions.\n"));
1480 } else if (loss > 0) {
1481 (void) printf(dgettext(TEXT_DOMAIN,
1482 "%s approximately %lld "),
1483 dryrun ? "Would discard" : "Discarded", loss);
1484 (void) printf(dgettext(TEXT_DOMAIN,
1485 "seconds of transactions.\n"));
1491 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1494 nvlist_t *nv = NULL;
1496 uint64_t edata = UINT64_MAX;
1501 if (!hdl->libzfs_printerr)
1505 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1507 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1509 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1510 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1511 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1512 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1515 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1516 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1519 (void) printf(dgettext(TEXT_DOMAIN,
1520 "Recovery is possible, but will result in some data loss.\n"));
1522 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1523 strftime(timestr, 128, 0, &t) != 0) {
1524 (void) printf(dgettext(TEXT_DOMAIN,
1525 "\tReturning the pool to its state as of %s\n"
1526 "\tshould correct the problem. "),
1529 (void) printf(dgettext(TEXT_DOMAIN,
1530 "\tReverting the pool to an earlier state "
1531 "should correct the problem.\n\t"));
1535 (void) printf(dgettext(TEXT_DOMAIN,
1536 "Approximately %lld minutes of data\n"
1537 "\tmust be discarded, irreversibly. "), (loss + 30) / 60);
1538 } else if (loss > 0) {
1539 (void) printf(dgettext(TEXT_DOMAIN,
1540 "Approximately %lld seconds of data\n"
1541 "\tmust be discarded, irreversibly. "), loss);
1543 if (edata != 0 && edata != UINT64_MAX) {
1545 (void) printf(dgettext(TEXT_DOMAIN,
1546 "After rewind, at least\n"
1547 "\tone persistent user-data error will remain. "));
1549 (void) printf(dgettext(TEXT_DOMAIN,
1550 "After rewind, several\n"
1551 "\tpersistent user-data errors will remain. "));
1554 (void) printf(dgettext(TEXT_DOMAIN,
1555 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1556 reason >= 0 ? "clear" : "import", name);
1558 (void) printf(dgettext(TEXT_DOMAIN,
1559 "A scrub of the pool\n"
1560 "\tis strongly recommended after recovery.\n"));
1564 (void) printf(dgettext(TEXT_DOMAIN,
1565 "Destroy and re-create the pool from\n\ta backup source.\n"));
1569 * zpool_import() is a contracted interface. Should be kept the same
1572 * Applications should use zpool_import_props() to import a pool with
1573 * new properties value to be set.
1576 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1579 nvlist_t *props = NULL;
1582 if (altroot != NULL) {
1583 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1584 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1585 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1589 if (nvlist_add_string(props,
1590 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1591 nvlist_add_string(props,
1592 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1594 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1595 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1600 ret = zpool_import_props(hdl, config, newname, props,
1608 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1614 uint64_t is_log = 0;
1616 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1620 (void) printf("\t%*s%s%s\n", indent, "", name,
1621 is_log ? " [log]" : "");
1623 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1624 &child, &children) != 0)
1627 for (c = 0; c < children; c++) {
1628 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
1629 print_vdev_tree(hdl, vname, child[c], indent + 2);
1635 zpool_print_unsup_feat(nvlist_t *config)
1637 nvlist_t *nvinfo, *unsup_feat;
1639 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1641 verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1644 for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1645 nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1648 verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1649 verify(nvpair_value_string(nvp, &desc) == 0);
1651 if (strlen(desc) > 0)
1652 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1654 (void) printf("\t%s\n", nvpair_name(nvp));
1659 * Import the given pool using the known configuration and a list of
1660 * properties to be set. The configuration should have come from
1661 * zpool_find_import(). The 'newname' parameters control whether the pool
1662 * is imported with a different name.
1665 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1666 nvlist_t *props, int flags)
1668 zfs_cmd_t zc = { 0 };
1669 zpool_rewind_policy_t policy;
1670 nvlist_t *nv = NULL;
1671 nvlist_t *nvinfo = NULL;
1672 nvlist_t *missing = NULL;
1679 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1682 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1683 "cannot import pool '%s'"), origname);
1685 if (newname != NULL) {
1686 if (!zpool_name_valid(hdl, B_FALSE, newname))
1687 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1688 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1690 thename = (char *)newname;
1697 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1699 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1702 if ((props = zpool_valid_proplist(hdl, origname,
1703 props, version, flags, errbuf)) == NULL) {
1705 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1711 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1713 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1716 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1720 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1725 zc.zc_cookie = flags;
1726 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1728 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1729 zcmd_free_nvlists(&zc);
1736 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1737 zpool_get_rewind_policy(config, &policy);
1743 * Dry-run failed, but we print out what success
1744 * looks like if we found a best txg
1746 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1747 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1753 if (newname == NULL)
1754 (void) snprintf(desc, sizeof (desc),
1755 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1758 (void) snprintf(desc, sizeof (desc),
1759 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1764 if (nv != NULL && nvlist_lookup_nvlist(nv,
1765 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1766 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1767 (void) printf(dgettext(TEXT_DOMAIN, "This "
1768 "pool uses the following feature(s) not "
1769 "supported by this system:\n"));
1770 zpool_print_unsup_feat(nv);
1771 if (nvlist_exists(nvinfo,
1772 ZPOOL_CONFIG_CAN_RDONLY)) {
1773 (void) printf(dgettext(TEXT_DOMAIN,
1774 "All unsupported features are only "
1775 "required for writing to the pool."
1776 "\nThe pool can be imported using "
1777 "'-o readonly=on'.\n"));
1781 * Unsupported version.
1783 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1787 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1791 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1792 "one or more devices is read only"));
1793 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1797 if (nv && nvlist_lookup_nvlist(nv,
1798 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1799 nvlist_lookup_nvlist(nvinfo,
1800 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1801 (void) printf(dgettext(TEXT_DOMAIN,
1802 "The devices below are missing, use "
1803 "'-m' to import the pool anyway:\n"));
1804 print_vdev_tree(hdl, NULL, missing, 2);
1805 (void) printf("\n");
1807 (void) zpool_standard_error(hdl, error, desc);
1811 (void) zpool_standard_error(hdl, error, desc);
1815 (void) zpool_standard_error(hdl, error, desc);
1816 zpool_explain_recover(hdl,
1817 newname ? origname : thename, -error, nv);
1824 zpool_handle_t *zhp;
1827 * This should never fail, but play it safe anyway.
1829 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1831 else if (zhp != NULL)
1833 if (policy.zrp_request &
1834 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1835 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1836 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1842 zcmd_free_nvlists(&zc);
1852 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1854 zfs_cmd_t zc = { 0 };
1856 libzfs_handle_t *hdl = zhp->zpool_hdl;
1858 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1859 zc.zc_cookie = func;
1861 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
1862 (errno == ENOENT && func != POOL_SCAN_NONE))
1865 if (func == POOL_SCAN_SCRUB) {
1866 (void) snprintf(msg, sizeof (msg),
1867 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1868 } else if (func == POOL_SCAN_NONE) {
1869 (void) snprintf(msg, sizeof (msg),
1870 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1873 assert(!"unexpected result");
1876 if (errno == EBUSY) {
1878 pool_scan_stat_t *ps = NULL;
1881 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1882 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1883 (void) nvlist_lookup_uint64_array(nvroot,
1884 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1885 if (ps && ps->pss_func == POOL_SCAN_SCRUB)
1886 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1888 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1889 } else if (errno == ENOENT) {
1890 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1892 return (zpool_standard_error(hdl, errno, msg));
1897 * This provides a very minimal check whether a given string is likely a
1898 * c#t#d# style string. Users of this are expected to do their own
1899 * verification of the s# part.
1901 #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1]))
1904 * More elaborate version for ones which may start with "/dev/dsk/"
1908 ctd_check_path(char *str) {
1910 * If it starts with a slash, check the last component.
1912 if (str && str[0] == '/') {
1913 char *tmp = strrchr(str, '/');
1916 * If it ends in "/old", check the second-to-last
1917 * component of the string instead.
1919 if (tmp != str && strcmp(tmp, "/old") == 0) {
1920 for (tmp--; *tmp != '/'; tmp--)
1925 return (CTD_CHECK(str));
1929 * Find a vdev that matches the search criteria specified. We use the
1930 * the nvpair name to determine how we should look for the device.
1931 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1932 * spare; but FALSE if its an INUSE spare.
1935 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1936 boolean_t *l2cache, boolean_t *log)
1943 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1945 /* Nothing to look for */
1946 if (search == NULL || pair == NULL)
1949 /* Obtain the key we will use to search */
1950 srchkey = nvpair_name(pair);
1952 switch (nvpair_type(pair)) {
1953 case DATA_TYPE_UINT64:
1954 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1955 uint64_t srchval, theguid;
1957 verify(nvpair_value_uint64(pair, &srchval) == 0);
1958 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1960 if (theguid == srchval)
1965 case DATA_TYPE_STRING: {
1966 char *srchval, *val;
1968 verify(nvpair_value_string(pair, &srchval) == 0);
1969 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1973 * Search for the requested value. Special cases:
1975 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
1976 * "s0" or "s0/old". The "s0" part is hidden from the user,
1977 * but included in the string, so this matches around it.
1978 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1980 * Otherwise, all other searches are simple string compares.
1982 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
1983 ctd_check_path(val)) {
1984 uint64_t wholedisk = 0;
1986 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1989 int slen = strlen(srchval);
1990 int vlen = strlen(val);
1992 if (slen != vlen - 2)
1996 * make_leaf_vdev() should only set
1997 * wholedisk for ZPOOL_CONFIG_PATHs which
1998 * will include "/dev/dsk/", giving plenty of
1999 * room for the indices used next.
2004 * strings identical except trailing "s0"
2006 if (strcmp(&val[vlen - 2], "s0") == 0 &&
2007 strncmp(srchval, val, slen) == 0)
2011 * strings identical except trailing "s0/old"
2013 if (strcmp(&val[vlen - 6], "s0/old") == 0 &&
2014 strcmp(&srchval[slen - 4], "/old") == 0 &&
2015 strncmp(srchval, val, slen - 4) == 0)
2020 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2021 char *type, *idx, *end, *p;
2022 uint64_t id, vdev_id;
2025 * Determine our vdev type, keeping in mind
2026 * that the srchval is composed of a type and
2027 * vdev id pair (i.e. mirror-4).
2029 if ((type = strdup(srchval)) == NULL)
2032 if ((p = strrchr(type, '-')) == NULL) {
2040 * If the types don't match then keep looking.
2042 if (strncmp(val, type, strlen(val)) != 0) {
2047 verify(strncmp(type, VDEV_TYPE_RAIDZ,
2048 strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2049 strncmp(type, VDEV_TYPE_MIRROR,
2050 strlen(VDEV_TYPE_MIRROR)) == 0);
2051 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2055 vdev_id = strtoull(idx, &end, 10);
2062 * Now verify that we have the correct vdev id.
2071 if (strcmp(srchval, val) == 0)
2080 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2081 &child, &children) != 0)
2084 for (c = 0; c < children; c++) {
2085 if ((ret = vdev_to_nvlist_iter(child[c], search,
2086 avail_spare, l2cache, NULL)) != NULL) {
2088 * The 'is_log' value is only set for the toplevel
2089 * vdev, not the leaf vdevs. So we always lookup the
2090 * log device from the root of the vdev tree (where
2091 * 'log' is non-NULL).
2094 nvlist_lookup_uint64(child[c],
2095 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2103 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2104 &child, &children) == 0) {
2105 for (c = 0; c < children; c++) {
2106 if ((ret = vdev_to_nvlist_iter(child[c], search,
2107 avail_spare, l2cache, NULL)) != NULL) {
2108 *avail_spare = B_TRUE;
2114 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2115 &child, &children) == 0) {
2116 for (c = 0; c < children; c++) {
2117 if ((ret = vdev_to_nvlist_iter(child[c], search,
2118 avail_spare, l2cache, NULL)) != NULL) {
2129 * Given a physical path (minus the "/devices" prefix), find the
2133 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2134 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2136 nvlist_t *search, *nvroot, *ret;
2138 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2139 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2141 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2144 *avail_spare = B_FALSE;
2148 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2149 nvlist_free(search);
2155 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2158 zpool_vdev_is_interior(const char *name)
2160 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2161 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2167 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2168 boolean_t *l2cache, boolean_t *log)
2170 char buf[MAXPATHLEN];
2172 nvlist_t *nvroot, *search, *ret;
2175 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2177 guid = strtoull(path, &end, 10);
2178 if (guid != 0 && *end == '\0') {
2179 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2180 } else if (zpool_vdev_is_interior(path)) {
2181 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2182 } else if (path[0] != '/') {
2183 (void) snprintf(buf, sizeof (buf), "%s%s", _PATH_DEV, path);
2184 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
2186 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2189 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2192 *avail_spare = B_FALSE;
2196 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2197 nvlist_free(search);
2203 vdev_online(nvlist_t *nv)
2207 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2208 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2209 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2216 * Helper function for zpool_get_physpaths().
2219 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2220 size_t *bytes_written)
2222 size_t bytes_left, pos, rsz;
2226 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2228 return (EZFS_NODEVICE);
2230 pos = *bytes_written;
2231 bytes_left = physpath_size - pos;
2232 format = (pos == 0) ? "%s" : " %s";
2234 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2235 *bytes_written += rsz;
2237 if (rsz >= bytes_left) {
2238 /* if physpath was not copied properly, clear it */
2239 if (bytes_left != 0) {
2242 return (EZFS_NOSPC);
2248 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2249 size_t *rsz, boolean_t is_spare)
2254 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2255 return (EZFS_INVALCONFIG);
2257 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2259 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2260 * For a spare vdev, we only want to boot from the active
2265 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2268 return (EZFS_INVALCONFIG);
2271 if (vdev_online(nv)) {
2272 if ((ret = vdev_get_one_physpath(nv, physpath,
2273 phypath_size, rsz)) != 0)
2276 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2277 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2278 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2283 if (nvlist_lookup_nvlist_array(nv,
2284 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2285 return (EZFS_INVALCONFIG);
2287 for (i = 0; i < count; i++) {
2288 ret = vdev_get_physpaths(child[i], physpath,
2289 phypath_size, rsz, is_spare);
2290 if (ret == EZFS_NOSPC)
2295 return (EZFS_POOL_INVALARG);
2299 * Get phys_path for a root pool config.
2300 * Return 0 on success; non-zero on failure.
2303 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2306 nvlist_t *vdev_root;
2313 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2315 return (EZFS_INVALCONFIG);
2317 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2318 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2319 &child, &count) != 0)
2320 return (EZFS_INVALCONFIG);
2323 * root pool can not have EFI labeled disks and can only have
2324 * a single top-level vdev.
2326 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
2327 pool_uses_efi(vdev_root))
2328 return (EZFS_POOL_INVALARG);
2330 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2333 /* No online devices */
2335 return (EZFS_NODEVICE);
2341 * Get phys_path for a root pool
2342 * Return 0 on success; non-zero on failure.
2345 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2347 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2352 * If the device has being dynamically expanded then we need to relabel
2353 * the disk to use the new unallocated space.
2356 zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2359 char path[MAXPATHLEN];
2362 int (*_efi_use_whole_disk)(int);
2364 if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2365 "efi_use_whole_disk")) == NULL)
2368 (void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name);
2370 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2371 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2372 "relabel '%s': unable to open device"), name);
2373 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2377 * It's possible that we might encounter an error if the device
2378 * does not have any unallocated space left. If so, we simply
2379 * ignore that error and continue on.
2381 error = _efi_use_whole_disk(fd);
2383 if (error && error != VT_ENOSPC) {
2384 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2385 "relabel '%s': unable to read disk capacity"), name);
2386 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2393 * Bring the specified vdev online. The 'flags' parameter is a set of the
2394 * ZFS_ONLINE_* flags.
2397 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2398 vdev_state_t *newstate)
2400 zfs_cmd_t zc = { 0 };
2403 boolean_t avail_spare, l2cache, islog;
2404 libzfs_handle_t *hdl = zhp->zpool_hdl;
2406 if (flags & ZFS_ONLINE_EXPAND) {
2407 (void) snprintf(msg, sizeof (msg),
2408 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2410 (void) snprintf(msg, sizeof (msg),
2411 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2414 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2415 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2417 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2419 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2422 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2424 if (flags & ZFS_ONLINE_EXPAND ||
2425 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2426 char *pathname = NULL;
2427 uint64_t wholedisk = 0;
2429 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2431 verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
2435 * XXX - L2ARC 1.0 devices can't support expansion.
2438 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2439 "cannot expand cache devices"));
2440 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2444 pathname += strlen(DISK_ROOT) + 1;
2445 (void) zpool_relabel_disk(hdl, pathname);
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 };
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 };
2524 libzfs_handle_t *hdl = zhp->zpool_hdl;
2526 (void) snprintf(msg, sizeof (msg),
2527 dgettext(TEXT_DOMAIN, "cannot fault %llu"), 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 };
2559 libzfs_handle_t *hdl = zhp->zpool_hdl;
2561 (void) snprintf(msg, sizeof (msg),
2562 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), 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 };
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);
2632 * If this is a root pool, make sure that we're not attaching an
2633 * EFI labeled device.
2635 if (rootpool && pool_uses_efi(nvroot)) {
2636 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2637 "EFI labeled devices are not supported on root pools."));
2638 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2641 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2642 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2644 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2647 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2650 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2652 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2653 zc.zc_cookie = replacing;
2655 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2656 &child, &children) != 0 || children != 1) {
2657 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2658 "new device must be a single disk"));
2659 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2662 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2663 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2665 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2669 * If the target is a hot spare that has been swapped in, we can only
2670 * replace it with another hot spare.
2673 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2674 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2675 NULL) == NULL || !avail_spare) &&
2676 is_replacing_spare(config_root, tgt, 1)) {
2677 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2678 "can only be replaced by another hot spare"));
2680 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2685 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2688 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2690 zcmd_free_nvlists(&zc);
2695 * XXX need a better way to prevent user from
2696 * booting up a half-baked vdev.
2698 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2699 "sure to wait until resilver is done "
2700 "before rebooting.\n"));
2701 (void) fprintf(stderr, "\n");
2702 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "If "
2703 "you boot from pool '%s', you may need to update\n"
2704 "boot code on newly attached disk '%s'.\n\n"
2705 "Assuming you use GPT partitioning and 'da0' is "
2706 "your new boot disk\n"
2707 "you may use the following command:\n\n"
2708 "\tgpart bootcode -b /boot/pmbr -p "
2709 "/boot/gptzfsboot -i 1 da0\n\n"),
2710 zhp->zpool_name, new_disk);
2718 * Can't attach to or replace this type of vdev.
2721 uint64_t version = zpool_get_prop_int(zhp,
2722 ZPOOL_PROP_VERSION, NULL);
2725 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2726 "cannot replace a log with a spare"));
2727 else if (version >= SPA_VERSION_MULTI_REPLACE)
2728 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2729 "already in replacing/spare config; wait "
2730 "for completion or use 'zpool detach'"));
2732 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2733 "cannot replace a replacing device"));
2735 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2736 "can only attach to mirrors and top-level "
2739 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2744 * The new device must be a single disk.
2746 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2747 "new device must be a single disk"));
2748 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2752 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2754 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2759 * The new device is too small.
2761 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2762 "device is too small"));
2763 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2768 * The new device has a different alignment requirement.
2770 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2771 "devices have different sector alignment"));
2772 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2777 * The resulting top-level vdev spec won't fit in the label.
2779 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2783 (void) zpool_standard_error(hdl, errno, msg);
2790 * Detach the specified device.
2793 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2795 zfs_cmd_t zc = { 0 };
2798 boolean_t avail_spare, l2cache;
2799 libzfs_handle_t *hdl = zhp->zpool_hdl;
2801 (void) snprintf(msg, sizeof (msg),
2802 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2804 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2805 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2807 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2810 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2813 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2815 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2817 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2824 * Can't detach from this type of vdev.
2826 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2827 "applicable to mirror and replacing vdevs"));
2828 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2833 * There are no other replicas of this device.
2835 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2839 (void) zpool_standard_error(hdl, errno, msg);
2846 * Find a mirror vdev in the source nvlist.
2848 * The mchild array contains a list of disks in one of the top-level mirrors
2849 * of the source pool. The schild array contains a list of disks that the
2850 * user specified on the command line. We loop over the mchild array to
2851 * see if any entry in the schild array matches.
2853 * If a disk in the mchild array is found in the schild array, we return
2854 * the index of that entry. Otherwise we return -1.
2857 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2858 nvlist_t **schild, uint_t schildren)
2862 for (mc = 0; mc < mchildren; mc++) {
2864 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2865 mchild[mc], B_FALSE);
2867 for (sc = 0; sc < schildren; sc++) {
2868 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2869 schild[sc], B_FALSE);
2870 boolean_t result = (strcmp(mpath, spath) == 0);
2886 * Split a mirror pool. If newroot points to null, then a new nvlist
2887 * is generated and it is the responsibility of the caller to free it.
2890 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2891 nvlist_t *props, splitflags_t flags)
2893 zfs_cmd_t zc = { 0 };
2895 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2896 nvlist_t **varray = NULL, *zc_props = NULL;
2897 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2898 libzfs_handle_t *hdl = zhp->zpool_hdl;
2900 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2903 (void) snprintf(msg, sizeof (msg),
2904 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2906 if (!zpool_name_valid(hdl, B_FALSE, newname))
2907 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2909 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2910 (void) fprintf(stderr, gettext("Internal error: unable to "
2911 "retrieve pool configuration\n"));
2915 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2917 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2920 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2921 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2922 props, vers, flags, msg)) == NULL)
2926 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2928 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2929 "Source pool is missing vdev tree"));
2931 nvlist_free(zc_props);
2935 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2938 if (*newroot == NULL ||
2939 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2940 &newchild, &newchildren) != 0)
2943 for (c = 0; c < children; c++) {
2944 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2946 nvlist_t **mchild, *vdev;
2951 * Unlike cache & spares, slogs are stored in the
2952 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2954 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2956 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2958 if (is_log || is_hole) {
2960 * Create a hole vdev and put it in the config.
2962 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2964 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2965 VDEV_TYPE_HOLE) != 0)
2967 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2972 varray[vcount++] = vdev;
2976 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
2978 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2979 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2980 "Source pool must be composed only of mirrors\n"));
2981 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2985 verify(nvlist_lookup_nvlist_array(child[c],
2986 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2988 /* find or add an entry for this top-level vdev */
2989 if (newchildren > 0 &&
2990 (entry = find_vdev_entry(zhp, mchild, mchildren,
2991 newchild, newchildren)) >= 0) {
2992 /* We found a disk that the user specified. */
2993 vdev = mchild[entry];
2996 /* User didn't specify a disk for this vdev. */
2997 vdev = mchild[mchildren - 1];
3000 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3004 /* did we find every disk the user specified? */
3005 if (found != newchildren) {
3006 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3007 "include at most one disk from each mirror"));
3008 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3012 /* Prepare the nvlist for populating. */
3013 if (*newroot == NULL) {
3014 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3017 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3018 VDEV_TYPE_ROOT) != 0)
3021 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3024 /* Add all the children we found */
3025 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3026 lastlog == 0 ? vcount : lastlog) != 0)
3030 * If we're just doing a dry run, exit now with success.
3033 memory_err = B_FALSE;
3038 /* now build up the config list & call the ioctl */
3039 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3042 if (nvlist_add_nvlist(newconfig,
3043 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3044 nvlist_add_string(newconfig,
3045 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3046 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3050 * The new pool is automatically part of the namespace unless we
3051 * explicitly export it.
3054 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3055 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3056 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3057 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3059 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3062 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3063 retval = zpool_standard_error(hdl, errno, msg);
3068 memory_err = B_FALSE;
3071 if (varray != NULL) {
3074 for (v = 0; v < vcount; v++)
3075 nvlist_free(varray[v]);
3078 zcmd_free_nvlists(&zc);
3080 nvlist_free(zc_props);
3082 nvlist_free(newconfig);
3084 nvlist_free(*newroot);
3092 return (no_memory(hdl));
3098 * Remove the given device. Currently, this is supported only for hot spares
3099 * and level 2 cache devices.
3102 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3104 zfs_cmd_t zc = { 0 };
3107 boolean_t avail_spare, l2cache, islog;
3108 libzfs_handle_t *hdl = zhp->zpool_hdl;
3111 (void) snprintf(msg, sizeof (msg),
3112 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3114 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3115 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3117 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3119 * XXX - this should just go away.
3121 if (!avail_spare && !l2cache && !islog) {
3122 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3123 "only inactive hot spares, cache, top-level, "
3124 "or log devices can be removed"));
3125 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3128 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3129 if (islog && version < SPA_VERSION_HOLES) {
3130 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3131 "pool must be upgrade to support log removal"));
3132 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3135 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3137 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3140 return (zpool_standard_error(hdl, errno, msg));
3144 * Clear the errors for the pool, or the particular device if specified.
3147 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3149 zfs_cmd_t zc = { 0 };
3152 zpool_rewind_policy_t policy;
3153 boolean_t avail_spare, l2cache;
3154 libzfs_handle_t *hdl = zhp->zpool_hdl;
3155 nvlist_t *nvi = NULL;
3159 (void) snprintf(msg, sizeof (msg),
3160 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3163 (void) snprintf(msg, sizeof (msg),
3164 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3167 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3169 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3170 &l2cache, NULL)) == 0)
3171 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3174 * Don't allow error clearing for hot spares. Do allow
3175 * error clearing for l2cache devices.
3178 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3180 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3184 zpool_get_rewind_policy(rewindnvl, &policy);
3185 zc.zc_cookie = policy.zrp_request;
3187 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3190 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3193 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3195 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3196 zcmd_free_nvlists(&zc);
3201 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
3202 errno != EPERM && errno != EACCES)) {
3203 if (policy.zrp_request &
3204 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3205 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3206 zpool_rewind_exclaim(hdl, zc.zc_name,
3207 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
3211 zcmd_free_nvlists(&zc);
3215 zcmd_free_nvlists(&zc);
3216 return (zpool_standard_error(hdl, errno, msg));
3220 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3223 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3225 zfs_cmd_t zc = { 0 };
3227 libzfs_handle_t *hdl = zhp->zpool_hdl;
3229 (void) snprintf(msg, sizeof (msg),
3230 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3233 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3235 zc.zc_cookie = ZPOOL_NO_REWIND;
3237 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3240 return (zpool_standard_error(hdl, errno, msg));
3244 * Change the GUID for a pool.
3247 zpool_reguid(zpool_handle_t *zhp)
3250 libzfs_handle_t *hdl = zhp->zpool_hdl;
3251 zfs_cmd_t zc = { 0 };
3253 (void) snprintf(msg, sizeof (msg),
3254 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3256 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3257 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3260 return (zpool_standard_error(hdl, errno, msg));
3267 zpool_reopen(zpool_handle_t *zhp)
3269 zfs_cmd_t zc = { 0 };
3271 libzfs_handle_t *hdl = zhp->zpool_hdl;
3273 (void) snprintf(msg, sizeof (msg),
3274 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
3277 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3278 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
3280 return (zpool_standard_error(hdl, errno, msg));
3284 * Convert from a devid string to a path.
3287 devid_to_path(char *devid_str)
3292 devid_nmlist_t *list = NULL;
3295 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3298 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3300 devid_str_free(minor);
3306 if ((path = strdup(list[0].devname)) == NULL)
3309 devid_free_nmlist(list);
3315 * Convert from a path to a devid string.
3318 path_to_devid(const char *path)
3324 if ((fd = open(path, O_RDONLY)) < 0)
3329 if (devid_get(fd, &devid) == 0) {
3330 if (devid_get_minor_name(fd, &minor) == 0)
3331 ret = devid_str_encode(devid, minor);
3333 devid_str_free(minor);
3342 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3343 * ignore any failure here, since a common case is for an unprivileged user to
3344 * type 'zpool status', and we'll display the correct information anyway.
3347 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3349 zfs_cmd_t zc = { 0 };
3351 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3352 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3353 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3356 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3360 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3361 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3362 * We also check if this is a whole disk, in which case we strip off the
3363 * trailing 's0' slice name.
3365 * This routine is also responsible for identifying when disks have been
3366 * reconfigured in a new location. The kernel will have opened the device by
3367 * devid, but the path will still refer to the old location. To catch this, we
3368 * first do a path -> devid translation (which is fast for the common case). If
3369 * the devid matches, we're done. If not, we do a reverse devid -> path
3370 * translation and issue the appropriate ioctl() to update the path of the vdev.
3371 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3375 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3386 have_stats = nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3387 (uint64_t **)&vs, &vsc) == 0;
3388 have_path = nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0;
3391 * If the device is not currently present, assume it will not
3392 * come back at the same device path. Display the device by GUID.
3394 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3395 have_path && have_stats && vs->vs_state <= VDEV_STATE_CANT_OPEN) {
3396 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3398 (void) snprintf(buf, sizeof (buf), "%llu",
3399 (u_longlong_t)value);
3401 } else if (have_path) {
3404 * If the device is dead (faulted, offline, etc) then don't
3405 * bother opening it. Otherwise we may be forcing the user to
3406 * open a misbehaving device, which can have undesirable
3409 if ((have_stats == 0 ||
3410 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3412 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3414 * Determine if the current path is correct.
3416 char *newdevid = path_to_devid(path);
3418 if (newdevid == NULL ||
3419 strcmp(devid, newdevid) != 0) {
3422 if ((newpath = devid_to_path(devid)) != NULL) {
3424 * Update the path appropriately.
3426 set_path(zhp, nv, newpath);
3427 if (nvlist_add_string(nv,
3428 ZPOOL_CONFIG_PATH, newpath) == 0)
3429 verify(nvlist_lookup_string(nv,
3437 devid_str_free(newdevid);
3441 if (strncmp(path, "/dev/dsk/", 9) == 0)
3444 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3445 &value) == 0 && value) {
3446 int pathlen = strlen(path);
3447 char *tmp = zfs_strdup(hdl, path);
3450 * If it starts with c#, and ends with "s0", chop
3451 * the "s0" off, or if it ends with "s0/old", remove
3452 * the "s0" from the middle.
3454 if (CTD_CHECK(tmp)) {
3455 if (strcmp(&tmp[pathlen - 2], "s0") == 0) {
3456 tmp[pathlen - 2] = '\0';
3457 } else if (pathlen > 6 &&
3458 strcmp(&tmp[pathlen - 6], "s0/old") == 0) {
3459 (void) strcpy(&tmp[pathlen - 6],
3466 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3467 path += sizeof(_PATH_DEV) - 1;
3470 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3473 * If it's a raidz device, we need to stick in the parity level.
3475 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3476 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3478 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3479 (u_longlong_t)value);
3484 * We identify each top-level vdev by using a <type-id>
3485 * naming convention.
3490 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3492 (void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3498 return (zfs_strdup(hdl, path));
3502 zbookmark_compare(const void *a, const void *b)
3504 return (memcmp(a, b, sizeof (zbookmark_t)));
3508 * Retrieve the persistent error log, uniquify the members, and return to the
3512 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3514 zfs_cmd_t zc = { 0 };
3516 zbookmark_t *zb = NULL;
3520 * Retrieve the raw error list from the kernel. If the number of errors
3521 * has increased, allocate more space and continue until we get the
3524 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3528 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3529 count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
3531 zc.zc_nvlist_dst_size = count;
3532 (void) strcpy(zc.zc_name, zhp->zpool_name);
3534 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3536 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3537 if (errno == ENOMEM) {
3538 count = zc.zc_nvlist_dst_size;
3539 if ((zc.zc_nvlist_dst = (uintptr_t)
3540 zfs_alloc(zhp->zpool_hdl, count *
3541 sizeof (zbookmark_t))) == (uintptr_t)NULL)
3552 * Sort the resulting bookmarks. This is a little confusing due to the
3553 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3554 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3555 * _not_ copied as part of the process. So we point the start of our
3556 * array appropriate and decrement the total number of elements.
3558 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
3559 zc.zc_nvlist_dst_size;
3560 count -= zc.zc_nvlist_dst_size;
3562 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
3564 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3567 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3569 for (i = 0; i < count; i++) {
3572 /* ignoring zb_blkid and zb_level for now */
3573 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3574 zb[i-1].zb_object == zb[i].zb_object)
3577 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3579 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3580 zb[i].zb_objset) != 0) {
3584 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3585 zb[i].zb_object) != 0) {
3589 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3596 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3600 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3601 return (no_memory(zhp->zpool_hdl));
3605 * Upgrade a ZFS pool to the latest on-disk version.
3608 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3610 zfs_cmd_t zc = { 0 };
3611 libzfs_handle_t *hdl = zhp->zpool_hdl;
3613 (void) strcpy(zc.zc_name, zhp->zpool_name);
3614 zc.zc_cookie = new_version;
3616 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3617 return (zpool_standard_error_fmt(hdl, errno,
3618 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3624 zfs_save_arguments(int argc, char **argv, char *string, int len)
3626 (void) strlcpy(string, basename(argv[0]), len);
3627 for (int i = 1; i < argc; i++) {
3628 (void) strlcat(string, " ", len);
3629 (void) strlcat(string, argv[i], len);
3634 zpool_log_history(libzfs_handle_t *hdl, const char *message)
3636 zfs_cmd_t zc = { 0 };
3640 args = fnvlist_alloc();
3641 fnvlist_add_string(args, "message", message);
3642 err = zcmd_write_src_nvlist(hdl, &zc, args);
3644 err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
3646 zcmd_free_nvlists(&zc);
3651 * Perform ioctl to get some command history of a pool.
3653 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3654 * logical offset of the history buffer to start reading from.
3656 * Upon return, 'off' is the next logical offset to read from and
3657 * 'len' is the actual amount of bytes read into 'buf'.
3660 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3662 zfs_cmd_t zc = { 0 };
3663 libzfs_handle_t *hdl = zhp->zpool_hdl;
3665 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3667 zc.zc_history = (uint64_t)(uintptr_t)buf;
3668 zc.zc_history_len = *len;
3669 zc.zc_history_offset = *off;
3671 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3674 return (zfs_error_fmt(hdl, EZFS_PERM,
3675 dgettext(TEXT_DOMAIN,
3676 "cannot show history for pool '%s'"),
3679 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3680 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3681 "'%s'"), zhp->zpool_name));
3683 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3684 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3685 "'%s', pool must be upgraded"), zhp->zpool_name));
3687 return (zpool_standard_error_fmt(hdl, errno,
3688 dgettext(TEXT_DOMAIN,
3689 "cannot get history for '%s'"), zhp->zpool_name));
3693 *len = zc.zc_history_len;
3694 *off = zc.zc_history_offset;
3700 * Process the buffer of nvlists, unpacking and storing each nvlist record
3701 * into 'records'. 'leftover' is set to the number of bytes that weren't
3702 * processed as there wasn't a complete record.
3705 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3706 nvlist_t ***records, uint_t *numrecords)
3712 while (bytes_read > sizeof (reclen)) {
3714 /* get length of packed record (stored as little endian) */
3715 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3716 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3718 if (bytes_read < sizeof (reclen) + reclen)
3722 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3724 bytes_read -= sizeof (reclen) + reclen;
3725 buf += sizeof (reclen) + reclen;
3727 /* add record to nvlist array */
3729 if (ISP2(*numrecords + 1)) {
3730 *records = realloc(*records,
3731 *numrecords * 2 * sizeof (nvlist_t *));
3733 (*records)[*numrecords - 1] = nv;
3736 *leftover = bytes_read;
3740 #define HIS_BUF_LEN (128*1024)
3743 * Retrieve the command history of a pool.
3746 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3748 char buf[HIS_BUF_LEN];
3750 nvlist_t **records = NULL;
3751 uint_t numrecords = 0;
3755 uint64_t bytes_read = sizeof (buf);
3758 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3761 /* if nothing else was read in, we're at EOF, just return */
3765 if ((err = zpool_history_unpack(buf, bytes_read,
3766 &leftover, &records, &numrecords)) != 0)
3774 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3775 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3776 records, numrecords) == 0);
3778 for (i = 0; i < numrecords; i++)
3779 nvlist_free(records[i]);
3786 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3787 char *pathname, size_t len)
3789 zfs_cmd_t zc = { 0 };
3790 boolean_t mounted = B_FALSE;
3791 char *mntpnt = NULL;
3792 char dsname[MAXNAMELEN];
3795 /* special case for the MOS */
3796 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
3800 /* get the dataset's name */
3801 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3803 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3804 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3805 /* just write out a path of two object numbers */
3806 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3810 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3812 /* find out if the dataset is mounted */
3813 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3815 /* get the corrupted object's path */
3816 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3818 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3821 (void) snprintf(pathname, len, "%s%s", mntpnt,
3824 (void) snprintf(pathname, len, "%s:%s",
3825 dsname, zc.zc_value);
3828 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
3835 * Read the EFI label from the config, if a label does not exist then
3836 * pass back the error to the caller. If the caller has passed a non-NULL
3837 * diskaddr argument then we set it to the starting address of the EFI
3841 read_efi_label(nvlist_t *config, diskaddr_t *sb)
3845 char diskname[MAXPATHLEN];
3848 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3851 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
3852 strrchr(path, '/'));
3853 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
3854 struct dk_gpt *vtoc;
3856 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3858 *sb = vtoc->efi_parts[0].p_start;
3867 * determine where a partition starts on a disk in the current
3871 find_start_block(nvlist_t *config)
3875 diskaddr_t sb = MAXOFFSET_T;
3878 if (nvlist_lookup_nvlist_array(config,
3879 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3880 if (nvlist_lookup_uint64(config,
3881 ZPOOL_CONFIG_WHOLE_DISK,
3882 &wholedisk) != 0 || !wholedisk) {
3883 return (MAXOFFSET_T);
3885 if (read_efi_label(config, &sb) < 0)
3890 for (c = 0; c < children; c++) {
3891 sb = find_start_block(child[c]);
3892 if (sb != MAXOFFSET_T) {
3896 return (MAXOFFSET_T);
3901 * Label an individual disk. The name provided is the short name,
3902 * stripped of any leading /dev path.
3905 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name)
3908 char path[MAXPATHLEN];
3909 struct dk_gpt *vtoc;
3911 size_t resv = EFI_MIN_RESV_SIZE;
3912 uint64_t slice_size;
3913 diskaddr_t start_block;
3916 /* prepare an error message just in case */
3917 (void) snprintf(errbuf, sizeof (errbuf),
3918 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3923 if (zpool_is_bootable(zhp)) {
3924 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3925 "EFI labeled devices are not supported on root "
3927 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3930 verify(nvlist_lookup_nvlist(zhp->zpool_config,
3931 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3933 if (zhp->zpool_start_block == 0)
3934 start_block = find_start_block(nvroot);
3936 start_block = zhp->zpool_start_block;
3937 zhp->zpool_start_block = start_block;
3940 start_block = NEW_START_BLOCK;
3943 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
3946 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
3948 * This shouldn't happen. We've long since verified that this
3949 * is a valid device.
3952 dgettext(TEXT_DOMAIN, "unable to open device"));
3953 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
3956 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
3958 * The only way this can fail is if we run out of memory, or we
3959 * were unable to read the disk's capacity
3961 if (errno == ENOMEM)
3962 (void) no_memory(hdl);
3965 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3966 "unable to read disk capacity"), name);
3968 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
3971 slice_size = vtoc->efi_last_u_lba + 1;
3972 slice_size -= EFI_MIN_RESV_SIZE;
3973 if (start_block == MAXOFFSET_T)
3974 start_block = NEW_START_BLOCK;
3975 slice_size -= start_block;
3977 vtoc->efi_parts[0].p_start = start_block;
3978 vtoc->efi_parts[0].p_size = slice_size;
3981 * Why we use V_USR: V_BACKUP confuses users, and is considered
3982 * disposable by some EFI utilities (since EFI doesn't have a backup
3983 * slice). V_UNASSIGNED is supposed to be used only for zero size
3984 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
3985 * etc. were all pretty specific. V_USR is as close to reality as we
3986 * can get, in the absence of V_OTHER.
3988 vtoc->efi_parts[0].p_tag = V_USR;
3989 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
3991 vtoc->efi_parts[8].p_start = slice_size + start_block;
3992 vtoc->efi_parts[8].p_size = resv;
3993 vtoc->efi_parts[8].p_tag = V_RESERVED;
3995 if (efi_write(fd, vtoc) != 0) {
3997 * Some block drivers (like pcata) may not support EFI
3998 * GPT labels. Print out a helpful error message dir-
3999 * ecting the user to manually label the disk and give
4005 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4006 "try using fdisk(1M) and then provide a specific slice"));
4007 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4017 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
4023 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
4024 if (strcmp(type, VDEV_TYPE_FILE) == 0 ||
4025 strcmp(type, VDEV_TYPE_HOLE) == 0 ||
4026 strcmp(type, VDEV_TYPE_MISSING) == 0) {
4027 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4028 "vdev type '%s' is not supported"), type);
4029 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
4032 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
4033 &child, &children) == 0) {
4034 for (c = 0; c < children; c++) {
4035 if (!supported_dump_vdev_type(hdl, child[c], errbuf))
4043 * Check if this zvol is allowable for use as a dump device; zero if
4044 * it is, > 0 if it isn't, < 0 if it isn't a zvol.
4046 * Allowable storage configurations include mirrors, all raidz variants, and
4047 * pools with log, cache, and spare devices. Pools which are backed by files or
4048 * have missing/hole vdevs are not suitable.
4051 zvol_check_dump_config(char *arg)
4053 zpool_handle_t *zhp = NULL;
4054 nvlist_t *config, *nvroot;
4058 libzfs_handle_t *hdl;
4060 char poolname[ZPOOL_MAXNAMELEN];
4061 int pathlen = strlen(ZVOL_FULL_DEV_DIR);
4064 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
4068 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4069 "dump is not supported on device '%s'"), arg);
4071 if ((hdl = libzfs_init()) == NULL)
4073 libzfs_print_on_error(hdl, B_TRUE);
4075 volname = arg + pathlen;
4077 /* check the configuration of the pool */
4078 if ((p = strchr(volname, '/')) == NULL) {
4079 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4080 "malformed dataset name"));
4081 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4083 } else if (p - volname >= ZFS_MAXNAMELEN) {
4084 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4085 "dataset name is too long"));
4086 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
4089 (void) strncpy(poolname, volname, p - volname);
4090 poolname[p - volname] = '\0';
4093 if ((zhp = zpool_open(hdl, poolname)) == NULL) {
4094 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4095 "could not open pool '%s'"), poolname);
4096 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4099 config = zpool_get_config(zhp, NULL);
4100 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4102 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4103 "could not obtain vdev configuration for '%s'"), poolname);
4104 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4108 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4109 &top, &toplevels) == 0);
4111 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {