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 2015 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2011, 2014 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, boolean_t literal)
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:
260 zpool_pool_state_to_name(POOL_STATE_UNAVAIL), len);
263 case ZPOOL_PROP_GUID:
264 intval = zpool_get_prop_int(zhp, prop, &src);
265 (void) snprintf(buf, len, "%llu", intval);
268 case ZPOOL_PROP_ALTROOT:
269 case ZPOOL_PROP_CACHEFILE:
270 case ZPOOL_PROP_COMMENT:
271 if (zhp->zpool_props != NULL ||
272 zpool_get_all_props(zhp) == 0) {
274 zpool_get_prop_string(zhp, prop, &src),
280 (void) strlcpy(buf, "-", len);
289 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
290 prop != ZPOOL_PROP_NAME)
293 switch (zpool_prop_get_type(prop)) {
294 case PROP_TYPE_STRING:
295 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
299 case PROP_TYPE_NUMBER:
300 intval = zpool_get_prop_int(zhp, prop, &src);
303 case ZPOOL_PROP_SIZE:
304 case ZPOOL_PROP_ALLOCATED:
305 case ZPOOL_PROP_FREE:
306 case ZPOOL_PROP_FREEING:
307 case ZPOOL_PROP_LEAKED:
309 (void) snprintf(buf, len, "%llu",
310 (u_longlong_t)intval);
312 (void) zfs_nicenum(intval, buf, len);
315 case ZPOOL_PROP_EXPANDSZ:
317 (void) strlcpy(buf, "-", len);
318 } else if (literal) {
319 (void) snprintf(buf, len, "%llu",
320 (u_longlong_t)intval);
322 (void) zfs_nicenum(intval, buf, len);
325 case ZPOOL_PROP_CAPACITY:
327 (void) snprintf(buf, len, "%llu",
328 (u_longlong_t)intval);
330 (void) snprintf(buf, len, "%llu%%",
331 (u_longlong_t)intval);
334 case ZPOOL_PROP_FRAGMENTATION:
335 if (intval == UINT64_MAX) {
336 (void) strlcpy(buf, "-", len);
338 (void) snprintf(buf, len, "%llu%%",
339 (u_longlong_t)intval);
342 case ZPOOL_PROP_DEDUPRATIO:
343 (void) snprintf(buf, len, "%llu.%02llux",
344 (u_longlong_t)(intval / 100),
345 (u_longlong_t)(intval % 100));
347 case ZPOOL_PROP_HEALTH:
348 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
349 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
350 verify(nvlist_lookup_uint64_array(nvroot,
351 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
354 (void) strlcpy(buf, zpool_state_to_name(intval,
357 case ZPOOL_PROP_VERSION:
358 if (intval >= SPA_VERSION_FEATURES) {
359 (void) snprintf(buf, len, "-");
364 (void) snprintf(buf, len, "%llu", intval);
368 case PROP_TYPE_INDEX:
369 intval = zpool_get_prop_int(zhp, prop, &src);
370 if (zpool_prop_index_to_string(prop, intval, &strval)
373 (void) strlcpy(buf, strval, len);
387 * Check if the bootfs name has the same pool name as it is set to.
388 * Assuming bootfs is a valid dataset name.
391 bootfs_name_valid(const char *pool, char *bootfs)
393 int len = strlen(pool);
395 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
398 if (strncmp(pool, bootfs, len) == 0 &&
399 (bootfs[len] == '/' || bootfs[len] == '\0'))
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, B_FALSE) == 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;
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 char *fname = strchr(propname, '@') + 1;
448 err = zfeature_lookup_name(fname, NULL);
450 ASSERT3U(err, ==, ENOENT);
451 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
452 "invalid feature '%s'"), fname);
453 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
457 if (nvpair_type(elem) != DATA_TYPE_STRING) {
458 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
459 "'%s' must be a string"), propname);
460 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
464 (void) nvpair_value_string(elem, &strval);
465 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) {
466 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
467 "property '%s' can only be set to "
468 "'enabled'"), propname);
469 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
473 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
474 (void) no_memory(hdl);
481 * Make sure this property is valid and applies to this type.
483 if (prop == ZPROP_INVAL) {
484 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
485 "invalid property '%s'"), propname);
486 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
490 if (zpool_prop_readonly(prop)) {
491 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
492 "is readonly"), propname);
493 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
497 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
498 &strval, &intval, errbuf) != 0)
502 * Perform additional checking for specific properties.
505 case ZPOOL_PROP_VERSION:
506 if (intval < version ||
507 !SPA_VERSION_IS_SUPPORTED(intval)) {
508 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
509 "property '%s' number %d is invalid."),
511 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
516 case ZPOOL_PROP_BOOTFS:
517 if (flags.create || flags.import) {
518 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
519 "property '%s' cannot be set at creation "
520 "or import time"), propname);
521 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
525 if (version < SPA_VERSION_BOOTFS) {
526 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
527 "pool must be upgraded to support "
528 "'%s' property"), propname);
529 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
534 * bootfs property value has to be a dataset name and
535 * the dataset has to be in the same pool as it sets to.
537 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
539 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
540 "is an invalid name"), strval);
541 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
545 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
546 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
547 "could not open pool '%s'"), poolname);
548 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
554 case ZPOOL_PROP_ALTROOT:
555 if (!flags.create && !flags.import) {
556 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
557 "property '%s' can only be set during pool "
558 "creation or import"), propname);
559 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
563 if (strval[0] != '/') {
564 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
565 "bad alternate root '%s'"), strval);
566 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
571 case ZPOOL_PROP_CACHEFILE:
572 if (strval[0] == '\0')
575 if (strcmp(strval, "none") == 0)
578 if (strval[0] != '/') {
579 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
580 "property '%s' must be empty, an "
581 "absolute path, or 'none'"), propname);
582 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
586 slash = strrchr(strval, '/');
588 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
589 strcmp(slash, "/..") == 0) {
590 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
591 "'%s' is not a valid file"), strval);
592 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
598 if (strval[0] != '\0' &&
599 (stat64(strval, &statbuf) != 0 ||
600 !S_ISDIR(statbuf.st_mode))) {
601 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
602 "'%s' is not a valid directory"),
604 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
611 case ZPOOL_PROP_COMMENT:
612 for (check = strval; *check != '\0'; check++) {
613 if (!isprint(*check)) {
615 dgettext(TEXT_DOMAIN,
616 "comment may only have printable "
618 (void) zfs_error(hdl, EZFS_BADPROP,
623 if (strlen(strval) > ZPROP_MAX_COMMENT) {
624 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
625 "comment must not exceed %d characters"),
627 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
631 case ZPOOL_PROP_READONLY:
633 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
634 "property '%s' can only be set at "
635 "import time"), propname);
636 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
645 nvlist_free(retprops);
650 * Set zpool property : propname=propval.
653 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
655 zfs_cmd_t zc = { 0 };
658 nvlist_t *nvl = NULL;
661 prop_flags_t flags = { 0 };
663 (void) snprintf(errbuf, sizeof (errbuf),
664 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
667 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
668 return (no_memory(zhp->zpool_hdl));
670 if (nvlist_add_string(nvl, propname, propval) != 0) {
672 return (no_memory(zhp->zpool_hdl));
675 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
676 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
677 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
686 * Execute the corresponding ioctl() to set this property.
688 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
690 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
695 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
697 zcmd_free_nvlists(&zc);
701 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
703 (void) zpool_props_refresh(zhp);
709 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
711 libzfs_handle_t *hdl = zhp->zpool_hdl;
713 char buf[ZFS_MAXPROPLEN];
714 nvlist_t *features = NULL;
716 boolean_t firstexpand = (NULL == *plp);
718 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
722 while (*last != NULL)
723 last = &(*last)->pl_next;
726 features = zpool_get_features(zhp);
728 if ((*plp)->pl_all && firstexpand) {
729 for (int i = 0; i < SPA_FEATURES; i++) {
730 zprop_list_t *entry = zfs_alloc(hdl,
731 sizeof (zprop_list_t));
732 entry->pl_prop = ZPROP_INVAL;
733 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
734 spa_feature_table[i].fi_uname);
735 entry->pl_width = strlen(entry->pl_user_prop);
736 entry->pl_all = B_TRUE;
739 last = &entry->pl_next;
743 /* add any unsupported features */
744 for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL);
745 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
750 if (zfeature_is_supported(nvpair_name(nvp)))
753 propname = zfs_asprintf(hdl, "unsupported@%s",
757 * Before adding the property to the list make sure that no
758 * other pool already added the same property.
762 while (entry != NULL) {
763 if (entry->pl_user_prop != NULL &&
764 strcmp(propname, entry->pl_user_prop) == 0) {
768 entry = entry->pl_next;
775 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
776 entry->pl_prop = ZPROP_INVAL;
777 entry->pl_user_prop = propname;
778 entry->pl_width = strlen(entry->pl_user_prop);
779 entry->pl_all = B_TRUE;
782 last = &entry->pl_next;
785 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
790 if (entry->pl_prop != ZPROP_INVAL &&
791 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
792 NULL, B_FALSE) == 0) {
793 if (strlen(buf) > entry->pl_width)
794 entry->pl_width = strlen(buf);
802 * Get the state for the given feature on the given ZFS pool.
805 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
809 boolean_t found = B_FALSE;
810 nvlist_t *features = zpool_get_features(zhp);
812 const char *feature = strchr(propname, '@') + 1;
814 supported = zpool_prop_feature(propname);
815 ASSERT(supported || zpool_prop_unsupported(propname));
818 * Convert from feature name to feature guid. This conversion is
819 * unecessary for unsupported@... properties because they already
826 ret = zfeature_lookup_name(feature, &fid);
828 (void) strlcpy(buf, "-", len);
831 feature = spa_feature_table[fid].fi_guid;
834 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
839 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
842 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
844 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
849 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
851 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
854 (void) strlcpy(buf, "-", len);
863 * Don't start the slice at the default block of 34; many storage
864 * devices will use a stripe width of 128k, so start there instead.
866 #define NEW_START_BLOCK 256
869 * Validate the given pool name, optionally putting an extended error message in
873 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
879 ret = pool_namecheck(pool, &why, &what);
882 * The rules for reserved pool names were extended at a later point.
883 * But we need to support users with existing pools that may now be
884 * invalid. So we only check for this expanded set of names during a
885 * create (or import), and only in userland.
887 if (ret == 0 && !isopen &&
888 (strncmp(pool, "mirror", 6) == 0 ||
889 strncmp(pool, "raidz", 5) == 0 ||
890 strncmp(pool, "spare", 5) == 0 ||
891 strcmp(pool, "log") == 0)) {
894 dgettext(TEXT_DOMAIN, "name is reserved"));
902 case NAME_ERR_TOOLONG:
904 dgettext(TEXT_DOMAIN, "name is too long"));
907 case NAME_ERR_INVALCHAR:
909 dgettext(TEXT_DOMAIN, "invalid character "
910 "'%c' in pool name"), what);
913 case NAME_ERR_NOLETTER:
914 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
915 "name must begin with a letter"));
918 case NAME_ERR_RESERVED:
919 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
920 "name is reserved"));
923 case NAME_ERR_DISKLIKE:
924 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
925 "pool name is reserved"));
928 case NAME_ERR_LEADING_SLASH:
929 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
930 "leading slash in name"));
933 case NAME_ERR_EMPTY_COMPONENT:
934 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
935 "empty component in name"));
938 case NAME_ERR_TRAILING_SLASH:
939 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
940 "trailing slash in name"));
943 case NAME_ERR_MULTIPLE_AT:
944 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
945 "multiple '@' delimiters in name"));
957 * Open a handle to the given pool, even if the pool is currently in the FAULTED
961 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
967 * Make sure the pool name is valid.
969 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
970 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
971 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
976 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
979 zhp->zpool_hdl = hdl;
980 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
982 if (zpool_refresh_stats(zhp, &missing) != 0) {
988 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
989 (void) zfs_error_fmt(hdl, EZFS_NOENT,
990 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
999 * Like the above, but silent on error. Used when iterating over pools (because
1000 * the configuration cache may be out of date).
1003 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1005 zpool_handle_t *zhp;
1008 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1011 zhp->zpool_hdl = hdl;
1012 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1014 if (zpool_refresh_stats(zhp, &missing) != 0) {
1030 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1034 zpool_open(libzfs_handle_t *hdl, const char *pool)
1036 zpool_handle_t *zhp;
1038 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1041 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1042 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1043 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1052 * Close the handle. Simply frees the memory associated with the handle.
1055 zpool_close(zpool_handle_t *zhp)
1057 if (zhp->zpool_config)
1058 nvlist_free(zhp->zpool_config);
1059 if (zhp->zpool_old_config)
1060 nvlist_free(zhp->zpool_old_config);
1061 if (zhp->zpool_props)
1062 nvlist_free(zhp->zpool_props);
1067 * Return the name of the pool.
1070 zpool_get_name(zpool_handle_t *zhp)
1072 return (zhp->zpool_name);
1077 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1080 zpool_get_state(zpool_handle_t *zhp)
1082 return (zhp->zpool_state);
1086 * Create the named pool, using the provided vdev list. It is assumed
1087 * that the consumer has already validated the contents of the nvlist, so we
1088 * don't have to worry about error semantics.
1091 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1092 nvlist_t *props, nvlist_t *fsprops)
1094 zfs_cmd_t zc = { 0 };
1095 nvlist_t *zc_fsprops = NULL;
1096 nvlist_t *zc_props = NULL;
1100 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1101 "cannot create '%s'"), pool);
1103 if (!zpool_name_valid(hdl, B_FALSE, pool))
1104 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1106 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1110 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1112 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1113 SPA_VERSION_1, flags, msg)) == NULL) {
1122 zoned = ((nvlist_lookup_string(fsprops,
1123 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1124 strcmp(zonestr, "on") == 0);
1126 if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
1127 fsprops, zoned, NULL, NULL, msg)) == NULL) {
1131 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1134 if (nvlist_add_nvlist(zc_props,
1135 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1140 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1143 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1145 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1147 zcmd_free_nvlists(&zc);
1148 nvlist_free(zc_props);
1149 nvlist_free(zc_fsprops);
1154 * This can happen if the user has specified the same
1155 * device multiple times. We can't reliably detect this
1156 * until we try to add it and see we already have a
1159 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1160 "one or more vdevs refer to the same device"));
1161 return (zfs_error(hdl, EZFS_BADDEV, msg));
1165 * This happens if the record size is smaller or larger
1166 * than the allowed size range, or not a power of 2.
1168 * NOTE: although zfs_valid_proplist is called earlier,
1169 * this case may have slipped through since the
1170 * pool does not exist yet and it is therefore
1171 * impossible to read properties e.g. max blocksize
1174 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1175 "record size invalid"));
1176 return (zfs_error(hdl, EZFS_BADPROP, msg));
1180 * This occurs when one of the devices is below
1181 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1182 * device was the problem device since there's no
1183 * reliable way to determine device size from userland.
1188 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1190 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1191 "one or more devices is less than the "
1192 "minimum size (%s)"), buf);
1194 return (zfs_error(hdl, EZFS_BADDEV, msg));
1197 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1198 "one or more devices is out of space"));
1199 return (zfs_error(hdl, EZFS_BADDEV, msg));
1202 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1203 "cache device must be a disk or disk slice"));
1204 return (zfs_error(hdl, EZFS_BADDEV, msg));
1207 return (zpool_standard_error(hdl, errno, msg));
1212 zcmd_free_nvlists(&zc);
1213 nvlist_free(zc_props);
1214 nvlist_free(zc_fsprops);
1219 * Destroy the given pool. It is up to the caller to ensure that there are no
1220 * datasets left in the pool.
1223 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1225 zfs_cmd_t zc = { 0 };
1226 zfs_handle_t *zfp = NULL;
1227 libzfs_handle_t *hdl = zhp->zpool_hdl;
1230 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1231 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1234 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1235 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1237 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1238 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1239 "cannot destroy '%s'"), zhp->zpool_name);
1241 if (errno == EROFS) {
1242 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1243 "one or more devices is read only"));
1244 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1246 (void) zpool_standard_error(hdl, errno, msg);
1255 remove_mountpoint(zfp);
1263 * Add the given vdevs to the pool. The caller must have already performed the
1264 * necessary verification to ensure that the vdev specification is well-formed.
1267 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1269 zfs_cmd_t zc = { 0 };
1271 libzfs_handle_t *hdl = zhp->zpool_hdl;
1273 nvlist_t **spares, **l2cache;
1274 uint_t nspares, nl2cache;
1276 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1277 "cannot add to '%s'"), zhp->zpool_name);
1279 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1280 SPA_VERSION_SPARES &&
1281 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1282 &spares, &nspares) == 0) {
1283 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1284 "upgraded to add hot spares"));
1285 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1288 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1289 SPA_VERSION_L2CACHE &&
1290 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1291 &l2cache, &nl2cache) == 0) {
1292 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1293 "upgraded to add cache devices"));
1294 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1297 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1299 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1301 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1305 * This can happen if the user has specified the same
1306 * device multiple times. We can't reliably detect this
1307 * until we try to add it and see we already have a
1310 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1311 "one or more vdevs refer to the same device"));
1312 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1317 * This occurrs when one of the devices is below
1318 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1319 * device was the problem device since there's no
1320 * reliable way to determine device size from userland.
1325 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1327 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1328 "device is less than the minimum "
1331 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1335 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1336 "pool must be upgraded to add these vdevs"));
1337 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1341 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1342 "root pool can not have multiple vdevs"
1343 " or separate logs"));
1344 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1348 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1349 "cache device must be a disk or disk slice"));
1350 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1354 (void) zpool_standard_error(hdl, errno, msg);
1362 zcmd_free_nvlists(&zc);
1368 * Exports the pool from the system. The caller must ensure that there are no
1369 * mounted datasets in the pool.
1372 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1373 const char *log_str)
1375 zfs_cmd_t zc = { 0 };
1378 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1379 "cannot export '%s'"), zhp->zpool_name);
1381 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1382 zc.zc_cookie = force;
1383 zc.zc_guid = hardforce;
1384 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1386 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1389 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1390 "use '-f' to override the following errors:\n"
1391 "'%s' has an active shared spare which could be"
1392 " used by other pools once '%s' is exported."),
1393 zhp->zpool_name, zhp->zpool_name);
1394 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1397 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1406 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1408 return (zpool_export_common(zhp, force, B_FALSE, log_str));
1412 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1414 return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1418 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1421 nvlist_t *nv = NULL;
1427 if (!hdl->libzfs_printerr || config == NULL)
1430 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1431 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1435 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1437 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1439 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1440 strftime(timestr, 128, 0, &t) != 0) {
1442 (void) printf(dgettext(TEXT_DOMAIN,
1443 "Would be able to return %s "
1444 "to its state as of %s.\n"),
1447 (void) printf(dgettext(TEXT_DOMAIN,
1448 "Pool %s returned to its state as of %s.\n"),
1452 (void) printf(dgettext(TEXT_DOMAIN,
1453 "%s approximately %lld "),
1454 dryrun ? "Would discard" : "Discarded",
1456 (void) printf(dgettext(TEXT_DOMAIN,
1457 "minutes of transactions.\n"));
1458 } else if (loss > 0) {
1459 (void) printf(dgettext(TEXT_DOMAIN,
1460 "%s approximately %lld "),
1461 dryrun ? "Would discard" : "Discarded", loss);
1462 (void) printf(dgettext(TEXT_DOMAIN,
1463 "seconds of transactions.\n"));
1469 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1472 nvlist_t *nv = NULL;
1474 uint64_t edata = UINT64_MAX;
1479 if (!hdl->libzfs_printerr)
1483 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1485 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1487 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1488 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1489 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1490 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1493 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1494 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1497 (void) printf(dgettext(TEXT_DOMAIN,
1498 "Recovery is possible, but will result in some data loss.\n"));
1500 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1501 strftime(timestr, 128, 0, &t) != 0) {
1502 (void) printf(dgettext(TEXT_DOMAIN,
1503 "\tReturning the pool to its state as of %s\n"
1504 "\tshould correct the problem. "),
1507 (void) printf(dgettext(TEXT_DOMAIN,
1508 "\tReverting the pool to an earlier state "
1509 "should correct the problem.\n\t"));
1513 (void) printf(dgettext(TEXT_DOMAIN,
1514 "Approximately %lld minutes of data\n"
1515 "\tmust be discarded, irreversibly. "), (loss + 30) / 60);
1516 } else if (loss > 0) {
1517 (void) printf(dgettext(TEXT_DOMAIN,
1518 "Approximately %lld seconds of data\n"
1519 "\tmust be discarded, irreversibly. "), loss);
1521 if (edata != 0 && edata != UINT64_MAX) {
1523 (void) printf(dgettext(TEXT_DOMAIN,
1524 "After rewind, at least\n"
1525 "\tone persistent user-data error will remain. "));
1527 (void) printf(dgettext(TEXT_DOMAIN,
1528 "After rewind, several\n"
1529 "\tpersistent user-data errors will remain. "));
1532 (void) printf(dgettext(TEXT_DOMAIN,
1533 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1534 reason >= 0 ? "clear" : "import", name);
1536 (void) printf(dgettext(TEXT_DOMAIN,
1537 "A scrub of the pool\n"
1538 "\tis strongly recommended after recovery.\n"));
1542 (void) printf(dgettext(TEXT_DOMAIN,
1543 "Destroy and re-create the pool from\n\ta backup source.\n"));
1547 * zpool_import() is a contracted interface. Should be kept the same
1550 * Applications should use zpool_import_props() to import a pool with
1551 * new properties value to be set.
1554 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1557 nvlist_t *props = NULL;
1560 if (altroot != NULL) {
1561 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1562 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1563 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1567 if (nvlist_add_string(props,
1568 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1569 nvlist_add_string(props,
1570 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1572 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1573 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1578 ret = zpool_import_props(hdl, config, newname, props,
1586 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1592 uint64_t is_log = 0;
1594 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1598 (void) printf("\t%*s%s%s\n", indent, "", name,
1599 is_log ? " [log]" : "");
1601 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1602 &child, &children) != 0)
1605 for (c = 0; c < children; c++) {
1606 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
1607 print_vdev_tree(hdl, vname, child[c], indent + 2);
1613 zpool_print_unsup_feat(nvlist_t *config)
1615 nvlist_t *nvinfo, *unsup_feat;
1617 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1619 verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1622 for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1623 nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1626 verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1627 verify(nvpair_value_string(nvp, &desc) == 0);
1629 if (strlen(desc) > 0)
1630 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1632 (void) printf("\t%s\n", nvpair_name(nvp));
1637 * Import the given pool using the known configuration and a list of
1638 * properties to be set. The configuration should have come from
1639 * zpool_find_import(). The 'newname' parameters control whether the pool
1640 * is imported with a different name.
1643 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1644 nvlist_t *props, int flags)
1646 zfs_cmd_t zc = { 0 };
1647 zpool_rewind_policy_t policy;
1648 nvlist_t *nv = NULL;
1649 nvlist_t *nvinfo = NULL;
1650 nvlist_t *missing = NULL;
1657 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1660 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1661 "cannot import pool '%s'"), origname);
1663 if (newname != NULL) {
1664 if (!zpool_name_valid(hdl, B_FALSE, newname))
1665 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1666 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1668 thename = (char *)newname;
1673 if (props != NULL) {
1675 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1677 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1680 if ((props = zpool_valid_proplist(hdl, origname,
1681 props, version, flags, errbuf)) == NULL)
1683 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1690 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1692 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1695 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1696 zcmd_free_nvlists(&zc);
1699 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1700 zcmd_free_nvlists(&zc);
1704 zc.zc_cookie = flags;
1705 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1707 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1708 zcmd_free_nvlists(&zc);
1715 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1717 zcmd_free_nvlists(&zc);
1719 zpool_get_rewind_policy(config, &policy);
1725 * Dry-run failed, but we print out what success
1726 * looks like if we found a best txg
1728 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1729 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1735 if (newname == NULL)
1736 (void) snprintf(desc, sizeof (desc),
1737 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1740 (void) snprintf(desc, sizeof (desc),
1741 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1746 if (nv != NULL && nvlist_lookup_nvlist(nv,
1747 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1748 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1749 (void) printf(dgettext(TEXT_DOMAIN, "This "
1750 "pool uses the following feature(s) not "
1751 "supported by this system:\n"));
1752 zpool_print_unsup_feat(nv);
1753 if (nvlist_exists(nvinfo,
1754 ZPOOL_CONFIG_CAN_RDONLY)) {
1755 (void) printf(dgettext(TEXT_DOMAIN,
1756 "All unsupported features are only "
1757 "required for writing to the pool."
1758 "\nThe pool can be imported using "
1759 "'-o readonly=on'.\n"));
1763 * Unsupported version.
1765 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1769 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1773 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1774 "one or more devices is read only"));
1775 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1779 if (nv && nvlist_lookup_nvlist(nv,
1780 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1781 nvlist_lookup_nvlist(nvinfo,
1782 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1783 (void) printf(dgettext(TEXT_DOMAIN,
1784 "The devices below are missing, use "
1785 "'-m' to import the pool anyway:\n"));
1786 print_vdev_tree(hdl, NULL, missing, 2);
1787 (void) printf("\n");
1789 (void) zpool_standard_error(hdl, error, desc);
1793 (void) zpool_standard_error(hdl, error, desc);
1797 (void) zpool_standard_error(hdl, error, desc);
1798 zpool_explain_recover(hdl,
1799 newname ? origname : thename, -error, nv);
1806 zpool_handle_t *zhp;
1809 * This should never fail, but play it safe anyway.
1811 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1813 else if (zhp != NULL)
1815 if (policy.zrp_request &
1816 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1817 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1818 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1831 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1833 zfs_cmd_t zc = { 0 };
1835 libzfs_handle_t *hdl = zhp->zpool_hdl;
1837 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1838 zc.zc_cookie = func;
1840 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
1841 (errno == ENOENT && func != POOL_SCAN_NONE))
1844 if (func == POOL_SCAN_SCRUB) {
1845 (void) snprintf(msg, sizeof (msg),
1846 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1847 } else if (func == POOL_SCAN_NONE) {
1848 (void) snprintf(msg, sizeof (msg),
1849 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1852 assert(!"unexpected result");
1855 if (errno == EBUSY) {
1857 pool_scan_stat_t *ps = NULL;
1860 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1861 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1862 (void) nvlist_lookup_uint64_array(nvroot,
1863 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1864 if (ps && ps->pss_func == POOL_SCAN_SCRUB)
1865 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1867 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1868 } else if (errno == ENOENT) {
1869 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1871 return (zpool_standard_error(hdl, errno, msg));
1877 * This provides a very minimal check whether a given string is likely a
1878 * c#t#d# style string. Users of this are expected to do their own
1879 * verification of the s# part.
1881 #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1]))
1884 * More elaborate version for ones which may start with "/dev/dsk/"
1888 ctd_check_path(char *str) {
1890 * If it starts with a slash, check the last component.
1892 if (str && str[0] == '/') {
1893 char *tmp = strrchr(str, '/');
1896 * If it ends in "/old", check the second-to-last
1897 * component of the string instead.
1899 if (tmp != str && strcmp(tmp, "/old") == 0) {
1900 for (tmp--; *tmp != '/'; tmp--)
1905 return (CTD_CHECK(str));
1910 * Find a vdev that matches the search criteria specified. We use the
1911 * the nvpair name to determine how we should look for the device.
1912 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1913 * spare; but FALSE if its an INUSE spare.
1916 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1917 boolean_t *l2cache, boolean_t *log)
1924 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1926 /* Nothing to look for */
1927 if (search == NULL || pair == NULL)
1930 /* Obtain the key we will use to search */
1931 srchkey = nvpair_name(pair);
1933 switch (nvpair_type(pair)) {
1934 case DATA_TYPE_UINT64:
1935 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1936 uint64_t srchval, theguid;
1938 verify(nvpair_value_uint64(pair, &srchval) == 0);
1939 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1941 if (theguid == srchval)
1946 case DATA_TYPE_STRING: {
1947 char *srchval, *val;
1949 verify(nvpair_value_string(pair, &srchval) == 0);
1950 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1954 * Search for the requested value. Special cases:
1956 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
1957 * "s0" or "s0/old". The "s0" part is hidden from the user,
1958 * but included in the string, so this matches around it.
1959 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1961 * Otherwise, all other searches are simple string compares.
1964 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
1965 ctd_check_path(val)) {
1966 uint64_t wholedisk = 0;
1968 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1971 int slen = strlen(srchval);
1972 int vlen = strlen(val);
1974 if (slen != vlen - 2)
1978 * make_leaf_vdev() should only set
1979 * wholedisk for ZPOOL_CONFIG_PATHs which
1980 * will include "/dev/dsk/", giving plenty of
1981 * room for the indices used next.
1986 * strings identical except trailing "s0"
1988 if (strcmp(&val[vlen - 2], "s0") == 0 &&
1989 strncmp(srchval, val, slen) == 0)
1993 * strings identical except trailing "s0/old"
1995 if (strcmp(&val[vlen - 6], "s0/old") == 0 &&
1996 strcmp(&srchval[slen - 4], "/old") == 0 &&
1997 strncmp(srchval, val, slen - 4) == 0)
2002 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2004 if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2006 char *type, *idx, *end, *p;
2007 uint64_t id, vdev_id;
2010 * Determine our vdev type, keeping in mind
2011 * that the srchval is composed of a type and
2012 * vdev id pair (i.e. mirror-4).
2014 if ((type = strdup(srchval)) == NULL)
2017 if ((p = strrchr(type, '-')) == NULL) {
2025 * If the types don't match then keep looking.
2027 if (strncmp(val, type, strlen(val)) != 0) {
2032 verify(strncmp(type, VDEV_TYPE_RAIDZ,
2033 strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2034 strncmp(type, VDEV_TYPE_MIRROR,
2035 strlen(VDEV_TYPE_MIRROR)) == 0);
2036 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2040 vdev_id = strtoull(idx, &end, 10);
2047 * Now verify that we have the correct vdev id.
2056 if (strcmp(srchval, val) == 0)
2065 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2066 &child, &children) != 0)
2069 for (c = 0; c < children; c++) {
2070 if ((ret = vdev_to_nvlist_iter(child[c], search,
2071 avail_spare, l2cache, NULL)) != NULL) {
2073 * The 'is_log' value is only set for the toplevel
2074 * vdev, not the leaf vdevs. So we always lookup the
2075 * log device from the root of the vdev tree (where
2076 * 'log' is non-NULL).
2079 nvlist_lookup_uint64(child[c],
2080 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2088 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2089 &child, &children) == 0) {
2090 for (c = 0; c < children; c++) {
2091 if ((ret = vdev_to_nvlist_iter(child[c], search,
2092 avail_spare, l2cache, NULL)) != NULL) {
2093 *avail_spare = B_TRUE;
2099 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2100 &child, &children) == 0) {
2101 for (c = 0; c < children; c++) {
2102 if ((ret = vdev_to_nvlist_iter(child[c], search,
2103 avail_spare, l2cache, NULL)) != NULL) {
2114 * Given a physical path (minus the "/devices" prefix), find the
2118 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2119 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2121 nvlist_t *search, *nvroot, *ret;
2123 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2124 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2126 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2129 *avail_spare = B_FALSE;
2133 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2134 nvlist_free(search);
2140 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2143 zpool_vdev_is_interior(const char *name)
2145 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2146 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2152 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2153 boolean_t *l2cache, boolean_t *log)
2155 char buf[MAXPATHLEN];
2157 nvlist_t *nvroot, *search, *ret;
2160 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2162 guid = strtoull(path, &end, 10);
2163 if (guid != 0 && *end == '\0') {
2164 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2165 } else if (zpool_vdev_is_interior(path)) {
2166 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2167 } else if (path[0] != '/') {
2168 (void) snprintf(buf, sizeof (buf), "%s%s", _PATH_DEV, path);
2169 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
2171 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2174 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2177 *avail_spare = B_FALSE;
2181 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2182 nvlist_free(search);
2188 vdev_online(nvlist_t *nv)
2192 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2193 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2194 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2201 * Helper function for zpool_get_physpaths().
2204 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2205 size_t *bytes_written)
2207 size_t bytes_left, pos, rsz;
2211 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2213 return (EZFS_NODEVICE);
2215 pos = *bytes_written;
2216 bytes_left = physpath_size - pos;
2217 format = (pos == 0) ? "%s" : " %s";
2219 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2220 *bytes_written += rsz;
2222 if (rsz >= bytes_left) {
2223 /* if physpath was not copied properly, clear it */
2224 if (bytes_left != 0) {
2227 return (EZFS_NOSPC);
2233 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2234 size_t *rsz, boolean_t is_spare)
2239 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2240 return (EZFS_INVALCONFIG);
2242 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2244 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2245 * For a spare vdev, we only want to boot from the active
2250 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2253 return (EZFS_INVALCONFIG);
2256 if (vdev_online(nv)) {
2257 if ((ret = vdev_get_one_physpath(nv, physpath,
2258 phypath_size, rsz)) != 0)
2261 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2262 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2263 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2268 if (nvlist_lookup_nvlist_array(nv,
2269 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2270 return (EZFS_INVALCONFIG);
2272 for (i = 0; i < count; i++) {
2273 ret = vdev_get_physpaths(child[i], physpath,
2274 phypath_size, rsz, is_spare);
2275 if (ret == EZFS_NOSPC)
2280 return (EZFS_POOL_INVALARG);
2284 * Get phys_path for a root pool config.
2285 * Return 0 on success; non-zero on failure.
2288 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2291 nvlist_t *vdev_root;
2298 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2300 return (EZFS_INVALCONFIG);
2302 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2303 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2304 &child, &count) != 0)
2305 return (EZFS_INVALCONFIG);
2308 * root pool can only have a single top-level vdev.
2310 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2311 return (EZFS_POOL_INVALARG);
2313 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2316 /* No online devices */
2318 return (EZFS_NODEVICE);
2324 * Get phys_path for a root pool
2325 * Return 0 on success; non-zero on failure.
2328 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2330 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2335 * If the device has being dynamically expanded then we need to relabel
2336 * the disk to use the new unallocated space.
2339 zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2342 char path[MAXPATHLEN];
2345 int (*_efi_use_whole_disk)(int);
2347 if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2348 "efi_use_whole_disk")) == NULL)
2351 (void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name);
2353 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2354 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2355 "relabel '%s': unable to open device"), name);
2356 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2360 * It's possible that we might encounter an error if the device
2361 * does not have any unallocated space left. If so, we simply
2362 * ignore that error and continue on.
2364 error = _efi_use_whole_disk(fd);
2366 if (error && error != VT_ENOSPC) {
2367 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2368 "relabel '%s': unable to read disk capacity"), name);
2369 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2376 * Bring the specified vdev online. The 'flags' parameter is a set of the
2377 * ZFS_ONLINE_* flags.
2380 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2381 vdev_state_t *newstate)
2383 zfs_cmd_t zc = { 0 };
2386 boolean_t avail_spare, l2cache, islog;
2387 libzfs_handle_t *hdl = zhp->zpool_hdl;
2389 if (flags & ZFS_ONLINE_EXPAND) {
2390 (void) snprintf(msg, sizeof (msg),
2391 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2393 (void) snprintf(msg, sizeof (msg),
2394 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2397 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2398 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2400 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2402 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2405 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2407 if (flags & ZFS_ONLINE_EXPAND ||
2408 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2409 char *pathname = NULL;
2410 uint64_t wholedisk = 0;
2412 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2414 verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
2418 * XXX - L2ARC 1.0 devices can't support expansion.
2421 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2422 "cannot expand cache devices"));
2423 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2427 pathname += strlen(DISK_ROOT) + 1;
2428 (void) zpool_relabel_disk(hdl, pathname);
2432 zc.zc_cookie = VDEV_STATE_ONLINE;
2435 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2436 if (errno == EINVAL) {
2437 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2438 "from this pool into a new one. Use '%s' "
2439 "instead"), "zpool detach");
2440 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2442 return (zpool_standard_error(hdl, errno, msg));
2445 *newstate = zc.zc_cookie;
2450 * Take the specified vdev offline
2453 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2455 zfs_cmd_t zc = { 0 };
2458 boolean_t avail_spare, l2cache;
2459 libzfs_handle_t *hdl = zhp->zpool_hdl;
2461 (void) snprintf(msg, sizeof (msg),
2462 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2464 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2465 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2467 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2469 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2472 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2474 zc.zc_cookie = VDEV_STATE_OFFLINE;
2475 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2477 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2484 * There are no other replicas of this device.
2486 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2490 * The log device has unplayed logs
2492 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2495 return (zpool_standard_error(hdl, errno, msg));
2500 * Mark the given vdev faulted.
2503 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2505 zfs_cmd_t zc = { 0 };
2507 libzfs_handle_t *hdl = zhp->zpool_hdl;
2509 (void) snprintf(msg, sizeof (msg),
2510 dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2512 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2514 zc.zc_cookie = VDEV_STATE_FAULTED;
2517 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2524 * There are no other replicas of this device.
2526 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2529 return (zpool_standard_error(hdl, errno, msg));
2535 * Mark the given vdev degraded.
2538 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2540 zfs_cmd_t zc = { 0 };
2542 libzfs_handle_t *hdl = zhp->zpool_hdl;
2544 (void) snprintf(msg, sizeof (msg),
2545 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
2547 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2549 zc.zc_cookie = VDEV_STATE_DEGRADED;
2552 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2555 return (zpool_standard_error(hdl, errno, msg));
2559 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2563 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2569 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2571 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2574 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2575 children == 2 && child[which] == tgt)
2578 for (c = 0; c < children; c++)
2579 if (is_replacing_spare(child[c], tgt, which))
2587 * Attach new_disk (fully described by nvroot) to old_disk.
2588 * If 'replacing' is specified, the new disk will replace the old one.
2591 zpool_vdev_attach(zpool_handle_t *zhp,
2592 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2594 zfs_cmd_t zc = { 0 };
2598 boolean_t avail_spare, l2cache, islog;
2603 nvlist_t *config_root;
2604 libzfs_handle_t *hdl = zhp->zpool_hdl;
2605 boolean_t rootpool = zpool_is_bootable(zhp);
2608 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2609 "cannot replace %s with %s"), old_disk, new_disk);
2611 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2612 "cannot attach %s to %s"), new_disk, old_disk);
2614 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2615 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2617 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2620 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2623 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2625 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2626 zc.zc_cookie = replacing;
2628 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2629 &child, &children) != 0 || children != 1) {
2630 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2631 "new device must be a single disk"));
2632 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2635 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2636 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2638 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2642 * If the target is a hot spare that has been swapped in, we can only
2643 * replace it with another hot spare.
2646 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2647 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2648 NULL) == NULL || !avail_spare) &&
2649 is_replacing_spare(config_root, tgt, 1)) {
2650 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2651 "can only be replaced by another hot spare"));
2653 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2658 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2661 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2663 zcmd_free_nvlists(&zc);
2668 * XXX need a better way to prevent user from
2669 * booting up a half-baked vdev.
2671 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2672 "sure to wait until resilver is done "
2673 "before rebooting.\n"));
2674 (void) fprintf(stderr, "\n");
2675 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "If "
2676 "you boot from pool '%s', you may need to update\n"
2677 "boot code on newly attached disk '%s'.\n\n"
2678 "Assuming you use GPT partitioning and 'da0' is "
2679 "your new boot disk\n"
2680 "you may use the following command:\n\n"
2681 "\tgpart bootcode -b /boot/pmbr -p "
2682 "/boot/gptzfsboot -i 1 da0\n\n"),
2683 zhp->zpool_name, new_disk);
2691 * Can't attach to or replace this type of vdev.
2694 uint64_t version = zpool_get_prop_int(zhp,
2695 ZPOOL_PROP_VERSION, NULL);
2698 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2699 "cannot replace a log with a spare"));
2700 else if (version >= SPA_VERSION_MULTI_REPLACE)
2701 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2702 "already in replacing/spare config; wait "
2703 "for completion or use 'zpool detach'"));
2705 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2706 "cannot replace a replacing device"));
2708 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2709 "can only attach to mirrors and top-level "
2712 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2717 * The new device must be a single disk.
2719 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2720 "new device must be a single disk"));
2721 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2725 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2727 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2732 * The new device is too small.
2734 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2735 "device is too small"));
2736 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2741 * The new device has a different alignment requirement.
2743 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2744 "devices have different sector alignment"));
2745 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2750 * The resulting top-level vdev spec won't fit in the label.
2752 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2756 (void) zpool_standard_error(hdl, errno, msg);
2763 * Detach the specified device.
2766 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2768 zfs_cmd_t zc = { 0 };
2771 boolean_t avail_spare, l2cache;
2772 libzfs_handle_t *hdl = zhp->zpool_hdl;
2774 (void) snprintf(msg, sizeof (msg),
2775 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2777 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2778 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2780 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2783 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2786 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2788 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2790 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2797 * Can't detach from this type of vdev.
2799 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2800 "applicable to mirror and replacing vdevs"));
2801 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2806 * There are no other replicas of this device.
2808 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2812 (void) zpool_standard_error(hdl, errno, msg);
2819 * Find a mirror vdev in the source nvlist.
2821 * The mchild array contains a list of disks in one of the top-level mirrors
2822 * of the source pool. The schild array contains a list of disks that the
2823 * user specified on the command line. We loop over the mchild array to
2824 * see if any entry in the schild array matches.
2826 * If a disk in the mchild array is found in the schild array, we return
2827 * the index of that entry. Otherwise we return -1.
2830 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2831 nvlist_t **schild, uint_t schildren)
2835 for (mc = 0; mc < mchildren; mc++) {
2837 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2838 mchild[mc], B_FALSE);
2840 for (sc = 0; sc < schildren; sc++) {
2841 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2842 schild[sc], B_FALSE);
2843 boolean_t result = (strcmp(mpath, spath) == 0);
2859 * Split a mirror pool. If newroot points to null, then a new nvlist
2860 * is generated and it is the responsibility of the caller to free it.
2863 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2864 nvlist_t *props, splitflags_t flags)
2866 zfs_cmd_t zc = { 0 };
2868 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2869 nvlist_t **varray = NULL, *zc_props = NULL;
2870 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2871 libzfs_handle_t *hdl = zhp->zpool_hdl;
2873 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2876 (void) snprintf(msg, sizeof (msg),
2877 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2879 if (!zpool_name_valid(hdl, B_FALSE, newname))
2880 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2882 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2883 (void) fprintf(stderr, gettext("Internal error: unable to "
2884 "retrieve pool configuration\n"));
2888 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2890 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2893 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2894 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2895 props, vers, flags, msg)) == NULL)
2899 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2901 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2902 "Source pool is missing vdev tree"));
2904 nvlist_free(zc_props);
2908 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2911 if (*newroot == NULL ||
2912 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2913 &newchild, &newchildren) != 0)
2916 for (c = 0; c < children; c++) {
2917 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2919 nvlist_t **mchild, *vdev;
2924 * Unlike cache & spares, slogs are stored in the
2925 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2927 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2929 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2931 if (is_log || is_hole) {
2933 * Create a hole vdev and put it in the config.
2935 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2937 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2938 VDEV_TYPE_HOLE) != 0)
2940 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2945 varray[vcount++] = vdev;
2949 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
2951 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2952 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2953 "Source pool must be composed only of mirrors\n"));
2954 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2958 verify(nvlist_lookup_nvlist_array(child[c],
2959 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2961 /* find or add an entry for this top-level vdev */
2962 if (newchildren > 0 &&
2963 (entry = find_vdev_entry(zhp, mchild, mchildren,
2964 newchild, newchildren)) >= 0) {
2965 /* We found a disk that the user specified. */
2966 vdev = mchild[entry];
2969 /* User didn't specify a disk for this vdev. */
2970 vdev = mchild[mchildren - 1];
2973 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
2977 /* did we find every disk the user specified? */
2978 if (found != newchildren) {
2979 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
2980 "include at most one disk from each mirror"));
2981 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2985 /* Prepare the nvlist for populating. */
2986 if (*newroot == NULL) {
2987 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
2990 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
2991 VDEV_TYPE_ROOT) != 0)
2994 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
2997 /* Add all the children we found */
2998 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
2999 lastlog == 0 ? vcount : lastlog) != 0)
3003 * If we're just doing a dry run, exit now with success.
3006 memory_err = B_FALSE;
3011 /* now build up the config list & call the ioctl */
3012 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3015 if (nvlist_add_nvlist(newconfig,
3016 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3017 nvlist_add_string(newconfig,
3018 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3019 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3023 * The new pool is automatically part of the namespace unless we
3024 * explicitly export it.
3027 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3028 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3029 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3030 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3032 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3035 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3036 retval = zpool_standard_error(hdl, errno, msg);
3041 memory_err = B_FALSE;
3044 if (varray != NULL) {
3047 for (v = 0; v < vcount; v++)
3048 nvlist_free(varray[v]);
3051 zcmd_free_nvlists(&zc);
3053 nvlist_free(zc_props);
3055 nvlist_free(newconfig);
3057 nvlist_free(*newroot);
3065 return (no_memory(hdl));
3071 * Remove the given device. Currently, this is supported only for hot spares
3072 * and level 2 cache devices.
3075 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3077 zfs_cmd_t zc = { 0 };
3080 boolean_t avail_spare, l2cache, islog;
3081 libzfs_handle_t *hdl = zhp->zpool_hdl;
3084 (void) snprintf(msg, sizeof (msg),
3085 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3087 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3088 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3090 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3092 * XXX - this should just go away.
3094 if (!avail_spare && !l2cache && !islog) {
3095 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3096 "only inactive hot spares, cache, top-level, "
3097 "or log devices can be removed"));
3098 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3101 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3102 if (islog && version < SPA_VERSION_HOLES) {
3103 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3104 "pool must be upgrade to support log removal"));
3105 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3108 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3110 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3113 return (zpool_standard_error(hdl, errno, msg));
3117 * Clear the errors for the pool, or the particular device if specified.
3120 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3122 zfs_cmd_t zc = { 0 };
3125 zpool_rewind_policy_t policy;
3126 boolean_t avail_spare, l2cache;
3127 libzfs_handle_t *hdl = zhp->zpool_hdl;
3128 nvlist_t *nvi = NULL;
3132 (void) snprintf(msg, sizeof (msg),
3133 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3136 (void) snprintf(msg, sizeof (msg),
3137 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3140 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3142 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3143 &l2cache, NULL)) == 0)
3144 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3147 * Don't allow error clearing for hot spares. Do allow
3148 * error clearing for l2cache devices.
3151 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3153 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3157 zpool_get_rewind_policy(rewindnvl, &policy);
3158 zc.zc_cookie = policy.zrp_request;
3160 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3163 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3166 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3168 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3169 zcmd_free_nvlists(&zc);
3174 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
3175 errno != EPERM && errno != EACCES)) {
3176 if (policy.zrp_request &
3177 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3178 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3179 zpool_rewind_exclaim(hdl, zc.zc_name,
3180 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
3184 zcmd_free_nvlists(&zc);
3188 zcmd_free_nvlists(&zc);
3189 return (zpool_standard_error(hdl, errno, msg));
3193 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3196 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3198 zfs_cmd_t zc = { 0 };
3200 libzfs_handle_t *hdl = zhp->zpool_hdl;
3202 (void) snprintf(msg, sizeof (msg),
3203 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3206 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3208 zc.zc_cookie = ZPOOL_NO_REWIND;
3210 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3213 return (zpool_standard_error(hdl, errno, msg));
3217 * Change the GUID for a pool.
3220 zpool_reguid(zpool_handle_t *zhp)
3223 libzfs_handle_t *hdl = zhp->zpool_hdl;
3224 zfs_cmd_t zc = { 0 };
3226 (void) snprintf(msg, sizeof (msg),
3227 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3229 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3230 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3233 return (zpool_standard_error(hdl, errno, msg));
3240 zpool_reopen(zpool_handle_t *zhp)
3242 zfs_cmd_t zc = { 0 };
3244 libzfs_handle_t *hdl = zhp->zpool_hdl;
3246 (void) snprintf(msg, sizeof (msg),
3247 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
3250 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3251 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
3253 return (zpool_standard_error(hdl, errno, msg));
3257 * Convert from a devid string to a path.
3260 devid_to_path(char *devid_str)
3265 devid_nmlist_t *list = NULL;
3268 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3271 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3273 devid_str_free(minor);
3280 * In a case the strdup() fails, we will just return NULL below.
3282 path = strdup(list[0].devname);
3284 devid_free_nmlist(list);
3290 * Convert from a path to a devid string.
3293 path_to_devid(const char *path)
3300 if ((fd = open(path, O_RDONLY)) < 0)
3305 if (devid_get(fd, &devid) == 0) {
3306 if (devid_get_minor_name(fd, &minor) == 0)
3307 ret = devid_str_encode(devid, minor);
3309 devid_str_free(minor);
3321 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3322 * ignore any failure here, since a common case is for an unprivileged user to
3323 * type 'zpool status', and we'll display the correct information anyway.
3326 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3328 zfs_cmd_t zc = { 0 };
3330 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3331 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3332 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3335 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3339 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3340 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3341 * We also check if this is a whole disk, in which case we strip off the
3342 * trailing 's0' slice name.
3344 * This routine is also responsible for identifying when disks have been
3345 * reconfigured in a new location. The kernel will have opened the device by
3346 * devid, but the path will still refer to the old location. To catch this, we
3347 * first do a path -> devid translation (which is fast for the common case). If
3348 * the devid matches, we're done. If not, we do a reverse devid -> path
3349 * translation and issue the appropriate ioctl() to update the path of the vdev.
3350 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3354 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3365 have_stats = nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3366 (uint64_t **)&vs, &vsc) == 0;
3367 have_path = nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0;
3370 * If the device is not currently present, assume it will not
3371 * come back at the same device path. Display the device by GUID.
3373 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3374 have_path && have_stats && vs->vs_state <= VDEV_STATE_CANT_OPEN) {
3375 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3377 (void) snprintf(buf, sizeof (buf), "%llu",
3378 (u_longlong_t)value);
3380 } else if (have_path) {
3383 * If the device is dead (faulted, offline, etc) then don't
3384 * bother opening it. Otherwise we may be forcing the user to
3385 * open a misbehaving device, which can have undesirable
3388 if ((have_stats == 0 ||
3389 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3391 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3393 * Determine if the current path is correct.
3395 char *newdevid = path_to_devid(path);
3397 if (newdevid == NULL ||
3398 strcmp(devid, newdevid) != 0) {
3401 if ((newpath = devid_to_path(devid)) != NULL) {
3403 * Update the path appropriately.
3405 set_path(zhp, nv, newpath);
3406 if (nvlist_add_string(nv,
3407 ZPOOL_CONFIG_PATH, newpath) == 0)
3408 verify(nvlist_lookup_string(nv,
3416 devid_str_free(newdevid);
3420 if (strncmp(path, "/dev/dsk/", 9) == 0)
3423 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3424 &value) == 0 && value) {
3425 int pathlen = strlen(path);
3426 char *tmp = zfs_strdup(hdl, path);
3429 * If it starts with c#, and ends with "s0", chop
3430 * the "s0" off, or if it ends with "s0/old", remove
3431 * the "s0" from the middle.
3433 if (CTD_CHECK(tmp)) {
3434 if (strcmp(&tmp[pathlen - 2], "s0") == 0) {
3435 tmp[pathlen - 2] = '\0';
3436 } else if (pathlen > 6 &&
3437 strcmp(&tmp[pathlen - 6], "s0/old") == 0) {
3438 (void) strcpy(&tmp[pathlen - 6],
3445 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3446 path += sizeof(_PATH_DEV) - 1;
3449 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3452 * If it's a raidz device, we need to stick in the parity level.
3454 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3455 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3457 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3458 (u_longlong_t)value);
3463 * We identify each top-level vdev by using a <type-id>
3464 * naming convention.
3469 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3471 (void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3477 return (zfs_strdup(hdl, path));
3481 zbookmark_mem_compare(const void *a, const void *b)
3483 return (memcmp(a, b, sizeof (zbookmark_phys_t)));
3487 * Retrieve the persistent error log, uniquify the members, and return to the
3491 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3493 zfs_cmd_t zc = { 0 };
3495 zbookmark_phys_t *zb = NULL;
3499 * Retrieve the raw error list from the kernel. If the number of errors
3500 * has increased, allocate more space and continue until we get the
3503 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3507 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3508 count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL)
3510 zc.zc_nvlist_dst_size = count;
3511 (void) strcpy(zc.zc_name, zhp->zpool_name);
3513 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3515 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3516 if (errno == ENOMEM) {
3519 count = zc.zc_nvlist_dst_size;
3520 dst = zfs_alloc(zhp->zpool_hdl, count *
3521 sizeof (zbookmark_phys_t));
3524 zc.zc_nvlist_dst = (uintptr_t)dst;
3534 * Sort the resulting bookmarks. This is a little confusing due to the
3535 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3536 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3537 * _not_ copied as part of the process. So we point the start of our
3538 * array appropriate and decrement the total number of elements.
3540 zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
3541 zc.zc_nvlist_dst_size;
3542 count -= zc.zc_nvlist_dst_size;
3544 qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
3546 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3549 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3551 for (i = 0; i < count; i++) {
3554 /* ignoring zb_blkid and zb_level for now */
3555 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3556 zb[i-1].zb_object == zb[i].zb_object)
3559 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3561 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3562 zb[i].zb_objset) != 0) {
3566 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3567 zb[i].zb_object) != 0) {
3571 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3578 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3582 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3583 return (no_memory(zhp->zpool_hdl));
3587 * Upgrade a ZFS pool to the latest on-disk version.
3590 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3592 zfs_cmd_t zc = { 0 };
3593 libzfs_handle_t *hdl = zhp->zpool_hdl;
3595 (void) strcpy(zc.zc_name, zhp->zpool_name);
3596 zc.zc_cookie = new_version;
3598 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3599 return (zpool_standard_error_fmt(hdl, errno,
3600 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3606 zfs_save_arguments(int argc, char **argv, char *string, int len)
3608 (void) strlcpy(string, basename(argv[0]), len);
3609 for (int i = 1; i < argc; i++) {
3610 (void) strlcat(string, " ", len);
3611 (void) strlcat(string, argv[i], len);
3616 zpool_log_history(libzfs_handle_t *hdl, const char *message)
3618 zfs_cmd_t zc = { 0 };
3622 args = fnvlist_alloc();
3623 fnvlist_add_string(args, "message", message);
3624 err = zcmd_write_src_nvlist(hdl, &zc, args);
3626 err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
3628 zcmd_free_nvlists(&zc);
3633 * Perform ioctl to get some command history of a pool.
3635 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3636 * logical offset of the history buffer to start reading from.
3638 * Upon return, 'off' is the next logical offset to read from and
3639 * 'len' is the actual amount of bytes read into 'buf'.
3642 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3644 zfs_cmd_t zc = { 0 };
3645 libzfs_handle_t *hdl = zhp->zpool_hdl;
3647 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3649 zc.zc_history = (uint64_t)(uintptr_t)buf;
3650 zc.zc_history_len = *len;
3651 zc.zc_history_offset = *off;
3653 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3656 return (zfs_error_fmt(hdl, EZFS_PERM,
3657 dgettext(TEXT_DOMAIN,
3658 "cannot show history for pool '%s'"),
3661 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3662 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3663 "'%s'"), zhp->zpool_name));
3665 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3666 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3667 "'%s', pool must be upgraded"), zhp->zpool_name));
3669 return (zpool_standard_error_fmt(hdl, errno,
3670 dgettext(TEXT_DOMAIN,
3671 "cannot get history for '%s'"), zhp->zpool_name));
3675 *len = zc.zc_history_len;
3676 *off = zc.zc_history_offset;
3682 * Process the buffer of nvlists, unpacking and storing each nvlist record
3683 * into 'records'. 'leftover' is set to the number of bytes that weren't
3684 * processed as there wasn't a complete record.
3687 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3688 nvlist_t ***records, uint_t *numrecords)
3694 while (bytes_read > sizeof (reclen)) {
3696 /* get length of packed record (stored as little endian) */
3697 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3698 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3700 if (bytes_read < sizeof (reclen) + reclen)
3704 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3706 bytes_read -= sizeof (reclen) + reclen;
3707 buf += sizeof (reclen) + reclen;
3709 /* add record to nvlist array */
3711 if (ISP2(*numrecords + 1)) {
3712 *records = realloc(*records,
3713 *numrecords * 2 * sizeof (nvlist_t *));
3715 (*records)[*numrecords - 1] = nv;
3718 *leftover = bytes_read;
3722 /* from spa_history.c: spa_history_create_obj() */
3723 #define HIS_BUF_LEN_DEF (128 << 10)
3724 #define HIS_BUF_LEN_MAX (1 << 30)
3727 * Retrieve the command history of a pool.
3730 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3733 uint64_t buflen = HIS_BUF_LEN_DEF;
3735 nvlist_t **records = NULL;
3736 uint_t numrecords = 0;
3739 buf = malloc(buflen);
3743 uint64_t bytes_read = buflen;
3746 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3749 /* if nothing else was read in, we're at EOF, just return */
3750 if (bytes_read == 0)
3753 if ((err = zpool_history_unpack(buf, bytes_read,
3754 &leftover, &records, &numrecords)) != 0)
3757 if (leftover == bytes_read) {
3759 * no progress made, because buffer is not big enough
3760 * to hold this record; resize and retry.
3765 if ((buflen >= HIS_BUF_LEN_MAX) ||
3766 ((buf = malloc(buflen)) == NULL)) {
3778 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3779 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3780 records, numrecords) == 0);
3782 for (i = 0; i < numrecords; i++)
3783 nvlist_free(records[i]);
3790 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3791 char *pathname, size_t len)
3793 zfs_cmd_t zc = { 0 };
3794 boolean_t mounted = B_FALSE;
3795 char *mntpnt = NULL;
3796 char dsname[MAXNAMELEN];
3799 /* special case for the MOS */
3800 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
3804 /* get the dataset's name */
3805 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3807 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3808 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3809 /* just write out a path of two object numbers */
3810 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3814 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3816 /* find out if the dataset is mounted */
3817 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3819 /* get the corrupted object's path */
3820 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3822 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3825 (void) snprintf(pathname, len, "%s%s", mntpnt,
3828 (void) snprintf(pathname, len, "%s:%s",
3829 dsname, zc.zc_value);
3832 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
3839 * Read the EFI label from the config, if a label does not exist then
3840 * pass back the error to the caller. If the caller has passed a non-NULL
3841 * diskaddr argument then we set it to the starting address of the EFI
3845 read_efi_label(nvlist_t *config, diskaddr_t *sb)
3849 char diskname[MAXPATHLEN];
3852 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3855 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
3856 strrchr(path, '/'));
3857 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
3858 struct dk_gpt *vtoc;
3860 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3862 *sb = vtoc->efi_parts[0].p_start;
3871 * determine where a partition starts on a disk in the current
3875 find_start_block(nvlist_t *config)
3879 diskaddr_t sb = MAXOFFSET_T;
3882 if (nvlist_lookup_nvlist_array(config,
3883 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3884 if (nvlist_lookup_uint64(config,
3885 ZPOOL_CONFIG_WHOLE_DISK,
3886 &wholedisk) != 0 || !wholedisk) {
3887 return (MAXOFFSET_T);
3889 if (read_efi_label(config, &sb) < 0)
3894 for (c = 0; c < children; c++) {
3895 sb = find_start_block(child[c]);
3896 if (sb != MAXOFFSET_T) {
3900 return (MAXOFFSET_T);
3905 * Label an individual disk. The name provided is the short name,
3906 * stripped of any leading /dev path.
3909 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name)
3912 char path[MAXPATHLEN];
3913 struct dk_gpt *vtoc;
3915 size_t resv = EFI_MIN_RESV_SIZE;
3916 uint64_t slice_size;
3917 diskaddr_t start_block;
3920 /* prepare an error message just in case */
3921 (void) snprintf(errbuf, sizeof (errbuf),
3922 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3927 verify(nvlist_lookup_nvlist(zhp->zpool_config,
3928 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3930 if (zhp->zpool_start_block == 0)
3931 start_block = find_start_block(nvroot);
3933 start_block = zhp->zpool_start_block;
3934 zhp->zpool_start_block = start_block;
3937 start_block = NEW_START_BLOCK;
3940 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
3943 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
3945 * This shouldn't happen. We've long since verified that this
3946 * is a valid device.
3949 dgettext(TEXT_DOMAIN, "unable to open device"));
3950 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
3953 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
3955 * The only way this can fail is if we run out of memory, or we
3956 * were unable to read the disk's capacity
3958 if (errno == ENOMEM)
3959 (void) no_memory(hdl);
3962 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3963 "unable to read disk capacity"), name);
3965 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
3968 slice_size = vtoc->efi_last_u_lba + 1;
3969 slice_size -= EFI_MIN_RESV_SIZE;
3970 if (start_block == MAXOFFSET_T)
3971 start_block = NEW_START_BLOCK;
3972 slice_size -= start_block;
3974 vtoc->efi_parts[0].p_start = start_block;
3975 vtoc->efi_parts[0].p_size = slice_size;
3978 * Why we use V_USR: V_BACKUP confuses users, and is considered
3979 * disposable by some EFI utilities (since EFI doesn't have a backup
3980 * slice). V_UNASSIGNED is supposed to be used only for zero size
3981 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
3982 * etc. were all pretty specific. V_USR is as close to reality as we
3983 * can get, in the absence of V_OTHER.
3985 vtoc->efi_parts[0].p_tag = V_USR;
3986 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
3988 vtoc->efi_parts[8].p_start = slice_size + start_block;
3989 vtoc->efi_parts[8].p_size = resv;
3990 vtoc->efi_parts[8].p_tag = V_RESERVED;
3992 if (efi_write(fd, vtoc) != 0) {
3994 * Some block drivers (like pcata) may not support EFI
3995 * GPT labels. Print out a helpful error message dir-
3996 * ecting the user to manually label the disk and give
4002 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4003 "try using fdisk(1M) and then provide a specific slice"));
4004 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4014 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
4020 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
4021 if (strcmp(type, VDEV_TYPE_FILE) == 0 ||
4022 strcmp(type, VDEV_TYPE_HOLE) == 0 ||
4023 strcmp(type, VDEV_TYPE_MISSING) == 0) {
4024 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4025 "vdev type '%s' is not supported"), type);
4026 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
4029 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
4030 &child, &children) == 0) {
4031 for (c = 0; c < children; c++) {
4032 if (!supported_dump_vdev_type(hdl, child[c], errbuf))
4040 * Check if this zvol is allowable for use as a dump device; zero if
4041 * it is, > 0 if it isn't, < 0 if it isn't a zvol.
4043 * Allowable storage configurations include mirrors, all raidz variants, and
4044 * pools with log, cache, and spare devices. Pools which are backed by files or
4045 * have missing/hole vdevs are not suitable.
4048 zvol_check_dump_config(char *arg)
4050 zpool_handle_t *zhp = NULL;
4051 nvlist_t *config, *nvroot;
4055 libzfs_handle_t *hdl;
4057 char poolname[ZPOOL_MAXNAMELEN];
4058 int pathlen = strlen(ZVOL_FULL_DEV_DIR);
4061 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
4065 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4066 "dump is not supported on device '%s'"), arg);
4068 if ((hdl = libzfs_init()) == NULL)
4070 libzfs_print_on_error(hdl, B_TRUE);
4072 volname = arg + pathlen;
4074 /* check the configuration of the pool */
4075 if ((p = strchr(volname, '/')) == NULL) {
4076 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4077 "malformed dataset name"));
4078 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4080 } else if (p - volname >= ZFS_MAXNAMELEN) {
4081 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4082 "dataset name is too long"));
4083 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
4086 (void) strncpy(poolname, volname, p - volname);
4087 poolname[p - volname] = '\0';
4090 if ((zhp = zpool_open(hdl, poolname)) == NULL) {
4091 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4092 "could not open pool '%s'"), poolname);
4093 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4096 config = zpool_get_config(zhp, NULL);
4097 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4099 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4100 "could not obtain vdev configuration for '%s'"), poolname);
4101 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4105 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4106 &top, &toplevels) == 0);
4108 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {