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) 2013 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:
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),
279 (void) strlcpy(buf, "-", len);
288 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
289 prop != ZPOOL_PROP_NAME)
292 switch (zpool_prop_get_type(prop)) {
293 case PROP_TYPE_STRING:
294 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
298 case PROP_TYPE_NUMBER:
299 intval = zpool_get_prop_int(zhp, prop, &src);
302 case ZPOOL_PROP_SIZE:
303 case ZPOOL_PROP_ALLOCATED:
304 case ZPOOL_PROP_FREE:
305 case ZPOOL_PROP_FREEING:
306 case ZPOOL_PROP_EXPANDSZ:
308 (void) snprintf(buf, len, "%llu",
309 (u_longlong_t)intval);
311 (void) zfs_nicenum(intval, buf, len);
315 case ZPOOL_PROP_CAPACITY:
317 (void) snprintf(buf, len, "%llu",
318 (u_longlong_t)intval);
320 (void) snprintf(buf, len, "%llu%%",
321 (u_longlong_t)intval);
325 case ZPOOL_PROP_DEDUPRATIO:
326 (void) snprintf(buf, len, "%llu.%02llux",
327 (u_longlong_t)(intval / 100),
328 (u_longlong_t)(intval % 100));
331 case ZPOOL_PROP_HEALTH:
332 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
333 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
334 verify(nvlist_lookup_uint64_array(nvroot,
335 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
338 (void) strlcpy(buf, zpool_state_to_name(intval,
341 case ZPOOL_PROP_VERSION:
342 if (intval >= SPA_VERSION_FEATURES) {
343 (void) snprintf(buf, len, "-");
348 (void) snprintf(buf, len, "%llu", intval);
352 case PROP_TYPE_INDEX:
353 intval = zpool_get_prop_int(zhp, prop, &src);
354 if (zpool_prop_index_to_string(prop, intval, &strval)
357 (void) strlcpy(buf, strval, len);
371 * Check if the bootfs name has the same pool name as it is set to.
372 * Assuming bootfs is a valid dataset name.
375 bootfs_name_valid(const char *pool, char *bootfs)
377 int len = strlen(pool);
379 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
382 if (strncmp(pool, bootfs, len) == 0 &&
383 (bootfs[len] == '/' || bootfs[len] == '\0'))
390 * Inspect the configuration to determine if any of the devices contain
394 pool_uses_efi(nvlist_t *config)
400 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
401 &child, &children) != 0)
402 return (read_efi_label(config, NULL) >= 0);
404 for (c = 0; c < children; c++) {
405 if (pool_uses_efi(child[c]))
413 zpool_is_bootable(zpool_handle_t *zhp)
415 char bootfs[ZPOOL_MAXNAMELEN];
417 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
418 sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-",
419 sizeof (bootfs)) != 0);
424 * Given an nvlist of zpool properties to be set, validate that they are
425 * correct, and parse any numeric properties (index, boolean, etc) if they are
426 * specified as strings.
429 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
430 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
438 struct stat64 statbuf;
442 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
443 (void) no_memory(hdl);
448 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
449 const char *propname = nvpair_name(elem);
451 prop = zpool_name_to_prop(propname);
452 if (prop == ZPROP_INVAL && zpool_prop_feature(propname)) {
454 char *fname = strchr(propname, '@') + 1;
456 err = zfeature_lookup_name(fname, NULL);
458 ASSERT3U(err, ==, ENOENT);
459 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
460 "invalid feature '%s'"), fname);
461 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
465 if (nvpair_type(elem) != DATA_TYPE_STRING) {
466 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
467 "'%s' must be a string"), propname);
468 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
472 (void) nvpair_value_string(elem, &strval);
473 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) {
474 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
475 "property '%s' can only be set to "
476 "'enabled'"), propname);
477 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
481 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
482 (void) no_memory(hdl);
489 * Make sure this property is valid and applies to this type.
491 if (prop == ZPROP_INVAL) {
492 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
493 "invalid property '%s'"), propname);
494 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
498 if (zpool_prop_readonly(prop)) {
499 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
500 "is readonly"), propname);
501 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
505 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
506 &strval, &intval, errbuf) != 0)
510 * Perform additional checking for specific properties.
513 case ZPOOL_PROP_VERSION:
514 if (intval < version ||
515 !SPA_VERSION_IS_SUPPORTED(intval)) {
516 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
517 "property '%s' number %d is invalid."),
519 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
524 case ZPOOL_PROP_BOOTFS:
525 if (flags.create || flags.import) {
526 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
527 "property '%s' cannot be set at creation "
528 "or import time"), propname);
529 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
533 if (version < SPA_VERSION_BOOTFS) {
534 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
535 "pool must be upgraded to support "
536 "'%s' property"), propname);
537 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
542 * bootfs property value has to be a dataset name and
543 * the dataset has to be in the same pool as it sets to.
545 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
547 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
548 "is an invalid name"), strval);
549 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
553 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
554 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
555 "could not open pool '%s'"), poolname);
556 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
559 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
560 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
564 * bootfs property cannot be set on a disk which has
567 if (pool_uses_efi(nvroot)) {
568 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
569 "property '%s' not supported on "
570 "EFI labeled devices"), propname);
571 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
579 case ZPOOL_PROP_ALTROOT:
580 if (!flags.create && !flags.import) {
581 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
582 "property '%s' can only be set during pool "
583 "creation or import"), propname);
584 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
588 if (strval[0] != '/') {
589 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
590 "bad alternate root '%s'"), strval);
591 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
596 case ZPOOL_PROP_CACHEFILE:
597 if (strval[0] == '\0')
600 if (strcmp(strval, "none") == 0)
603 if (strval[0] != '/') {
604 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
605 "property '%s' must be empty, an "
606 "absolute path, or 'none'"), propname);
607 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
611 slash = strrchr(strval, '/');
613 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
614 strcmp(slash, "/..") == 0) {
615 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
616 "'%s' is not a valid file"), strval);
617 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
623 if (strval[0] != '\0' &&
624 (stat64(strval, &statbuf) != 0 ||
625 !S_ISDIR(statbuf.st_mode))) {
626 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
627 "'%s' is not a valid directory"),
629 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
636 case ZPOOL_PROP_COMMENT:
637 for (check = strval; *check != '\0'; check++) {
638 if (!isprint(*check)) {
640 dgettext(TEXT_DOMAIN,
641 "comment may only have printable "
643 (void) zfs_error(hdl, EZFS_BADPROP,
648 if (strlen(strval) > ZPROP_MAX_COMMENT) {
649 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
650 "comment must not exceed %d characters"),
652 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
656 case ZPOOL_PROP_READONLY:
658 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
659 "property '%s' can only be set at "
660 "import time"), propname);
661 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
670 nvlist_free(retprops);
675 * Set zpool property : propname=propval.
678 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
680 zfs_cmd_t zc = { 0 };
683 nvlist_t *nvl = NULL;
686 prop_flags_t flags = { 0 };
688 (void) snprintf(errbuf, sizeof (errbuf),
689 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
692 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
693 return (no_memory(zhp->zpool_hdl));
695 if (nvlist_add_string(nvl, propname, propval) != 0) {
697 return (no_memory(zhp->zpool_hdl));
700 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
701 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
702 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
711 * Execute the corresponding ioctl() to set this property.
713 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
715 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
720 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
722 zcmd_free_nvlists(&zc);
726 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
728 (void) zpool_props_refresh(zhp);
734 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
736 libzfs_handle_t *hdl = zhp->zpool_hdl;
738 char buf[ZFS_MAXPROPLEN];
739 nvlist_t *features = NULL;
741 boolean_t firstexpand = (NULL == *plp);
743 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
747 while (*last != NULL)
748 last = &(*last)->pl_next;
751 features = zpool_get_features(zhp);
753 if ((*plp)->pl_all && firstexpand) {
754 for (int i = 0; i < SPA_FEATURES; i++) {
755 zprop_list_t *entry = zfs_alloc(hdl,
756 sizeof (zprop_list_t));
757 entry->pl_prop = ZPROP_INVAL;
758 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
759 spa_feature_table[i].fi_uname);
760 entry->pl_width = strlen(entry->pl_user_prop);
761 entry->pl_all = B_TRUE;
764 last = &entry->pl_next;
768 /* add any unsupported features */
769 for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL);
770 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
775 if (zfeature_is_supported(nvpair_name(nvp)))
778 propname = zfs_asprintf(hdl, "unsupported@%s",
782 * Before adding the property to the list make sure that no
783 * other pool already added the same property.
787 while (entry != NULL) {
788 if (entry->pl_user_prop != NULL &&
789 strcmp(propname, entry->pl_user_prop) == 0) {
793 entry = entry->pl_next;
800 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
801 entry->pl_prop = ZPROP_INVAL;
802 entry->pl_user_prop = propname;
803 entry->pl_width = strlen(entry->pl_user_prop);
804 entry->pl_all = B_TRUE;
807 last = &entry->pl_next;
810 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
815 if (entry->pl_prop != ZPROP_INVAL &&
816 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
817 NULL, B_FALSE) == 0) {
818 if (strlen(buf) > entry->pl_width)
819 entry->pl_width = strlen(buf);
827 * Get the state for the given feature on the given ZFS pool.
830 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
834 boolean_t found = B_FALSE;
835 nvlist_t *features = zpool_get_features(zhp);
837 const char *feature = strchr(propname, '@') + 1;
839 supported = zpool_prop_feature(propname);
840 ASSERT(supported || zpool_prop_unsupported(propname));
843 * Convert from feature name to feature guid. This conversion is
844 * unecessary for unsupported@... properties because they already
851 ret = zfeature_lookup_name(feature, &fid);
853 (void) strlcpy(buf, "-", len);
856 feature = spa_feature_table[fid].fi_guid;
859 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
864 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
867 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
869 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
874 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
876 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
879 (void) strlcpy(buf, "-", len);
888 * Don't start the slice at the default block of 34; many storage
889 * devices will use a stripe width of 128k, so start there instead.
891 #define NEW_START_BLOCK 256
894 * Validate the given pool name, optionally putting an extended error message in
898 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
904 ret = pool_namecheck(pool, &why, &what);
907 * The rules for reserved pool names were extended at a later point.
908 * But we need to support users with existing pools that may now be
909 * invalid. So we only check for this expanded set of names during a
910 * create (or import), and only in userland.
912 if (ret == 0 && !isopen &&
913 (strncmp(pool, "mirror", 6) == 0 ||
914 strncmp(pool, "raidz", 5) == 0 ||
915 strncmp(pool, "spare", 5) == 0 ||
916 strcmp(pool, "log") == 0)) {
919 dgettext(TEXT_DOMAIN, "name is reserved"));
927 case NAME_ERR_TOOLONG:
929 dgettext(TEXT_DOMAIN, "name is too long"));
932 case NAME_ERR_INVALCHAR:
934 dgettext(TEXT_DOMAIN, "invalid character "
935 "'%c' in pool name"), what);
938 case NAME_ERR_NOLETTER:
939 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
940 "name must begin with a letter"));
943 case NAME_ERR_RESERVED:
944 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
945 "name is reserved"));
948 case NAME_ERR_DISKLIKE:
949 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
950 "pool name is reserved"));
953 case NAME_ERR_LEADING_SLASH:
954 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
955 "leading slash in name"));
958 case NAME_ERR_EMPTY_COMPONENT:
959 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
960 "empty component in name"));
963 case NAME_ERR_TRAILING_SLASH:
964 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
965 "trailing slash in name"));
968 case NAME_ERR_MULTIPLE_AT:
969 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
970 "multiple '@' delimiters in name"));
982 * Open a handle to the given pool, even if the pool is currently in the FAULTED
986 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
992 * Make sure the pool name is valid.
994 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
995 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
996 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1001 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1004 zhp->zpool_hdl = hdl;
1005 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1007 if (zpool_refresh_stats(zhp, &missing) != 0) {
1013 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1014 (void) zfs_error_fmt(hdl, EZFS_NOENT,
1015 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1024 * Like the above, but silent on error. Used when iterating over pools (because
1025 * the configuration cache may be out of date).
1028 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1030 zpool_handle_t *zhp;
1033 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1036 zhp->zpool_hdl = hdl;
1037 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1039 if (zpool_refresh_stats(zhp, &missing) != 0) {
1055 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1059 zpool_open(libzfs_handle_t *hdl, const char *pool)
1061 zpool_handle_t *zhp;
1063 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1066 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1067 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1068 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1077 * Close the handle. Simply frees the memory associated with the handle.
1080 zpool_close(zpool_handle_t *zhp)
1082 if (zhp->zpool_config)
1083 nvlist_free(zhp->zpool_config);
1084 if (zhp->zpool_old_config)
1085 nvlist_free(zhp->zpool_old_config);
1086 if (zhp->zpool_props)
1087 nvlist_free(zhp->zpool_props);
1092 * Return the name of the pool.
1095 zpool_get_name(zpool_handle_t *zhp)
1097 return (zhp->zpool_name);
1102 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1105 zpool_get_state(zpool_handle_t *zhp)
1107 return (zhp->zpool_state);
1111 * Create the named pool, using the provided vdev list. It is assumed
1112 * that the consumer has already validated the contents of the nvlist, so we
1113 * don't have to worry about error semantics.
1116 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1117 nvlist_t *props, nvlist_t *fsprops)
1119 zfs_cmd_t zc = { 0 };
1120 nvlist_t *zc_fsprops = NULL;
1121 nvlist_t *zc_props = NULL;
1125 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1126 "cannot create '%s'"), pool);
1128 if (!zpool_name_valid(hdl, B_FALSE, pool))
1129 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1131 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1135 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1137 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1138 SPA_VERSION_1, flags, msg)) == NULL) {
1147 zoned = ((nvlist_lookup_string(fsprops,
1148 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1149 strcmp(zonestr, "on") == 0);
1151 if ((zc_fsprops = zfs_valid_proplist(hdl,
1152 ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
1156 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1159 if (nvlist_add_nvlist(zc_props,
1160 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1165 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1168 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1170 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1172 zcmd_free_nvlists(&zc);
1173 nvlist_free(zc_props);
1174 nvlist_free(zc_fsprops);
1179 * This can happen if the user has specified the same
1180 * device multiple times. We can't reliably detect this
1181 * until we try to add it and see we already have a
1184 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1185 "one or more vdevs refer to the same device"));
1186 return (zfs_error(hdl, EZFS_BADDEV, msg));
1190 * This occurs when one of the devices is below
1191 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1192 * device was the problem device since there's no
1193 * reliable way to determine device size from userland.
1198 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1200 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1201 "one or more devices is less than the "
1202 "minimum size (%s)"), buf);
1204 return (zfs_error(hdl, EZFS_BADDEV, msg));
1207 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1208 "one or more devices is out of space"));
1209 return (zfs_error(hdl, EZFS_BADDEV, msg));
1212 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1213 "cache device must be a disk or disk slice"));
1214 return (zfs_error(hdl, EZFS_BADDEV, msg));
1217 return (zpool_standard_error(hdl, errno, msg));
1222 zcmd_free_nvlists(&zc);
1223 nvlist_free(zc_props);
1224 nvlist_free(zc_fsprops);
1229 * Destroy the given pool. It is up to the caller to ensure that there are no
1230 * datasets left in the pool.
1233 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1235 zfs_cmd_t zc = { 0 };
1236 zfs_handle_t *zfp = NULL;
1237 libzfs_handle_t *hdl = zhp->zpool_hdl;
1240 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1241 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1244 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1245 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1247 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1248 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1249 "cannot destroy '%s'"), zhp->zpool_name);
1251 if (errno == EROFS) {
1252 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1253 "one or more devices is read only"));
1254 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1256 (void) zpool_standard_error(hdl, errno, msg);
1265 remove_mountpoint(zfp);
1273 * Add the given vdevs to the pool. The caller must have already performed the
1274 * necessary verification to ensure that the vdev specification is well-formed.
1277 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1279 zfs_cmd_t zc = { 0 };
1281 libzfs_handle_t *hdl = zhp->zpool_hdl;
1283 nvlist_t **spares, **l2cache;
1284 uint_t nspares, nl2cache;
1286 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1287 "cannot add to '%s'"), zhp->zpool_name);
1289 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1290 SPA_VERSION_SPARES &&
1291 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1292 &spares, &nspares) == 0) {
1293 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1294 "upgraded to add hot spares"));
1295 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1298 if (zpool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1299 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1302 for (s = 0; s < nspares; s++) {
1305 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1306 &path) == 0 && pool_uses_efi(spares[s])) {
1307 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1308 "device '%s' contains an EFI label and "
1309 "cannot be used on root pools."),
1310 zpool_vdev_name(hdl, NULL, spares[s],
1312 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1317 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1318 SPA_VERSION_L2CACHE &&
1319 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1320 &l2cache, &nl2cache) == 0) {
1321 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1322 "upgraded to add cache devices"));
1323 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1326 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1328 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1330 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1334 * This can happen if the user has specified the same
1335 * device multiple times. We can't reliably detect this
1336 * until we try to add it and see we already have a
1339 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1340 "one or more vdevs refer to the same device"));
1341 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1346 * This occurrs when one of the devices is below
1347 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1348 * device was the problem device since there's no
1349 * reliable way to determine device size from userland.
1354 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1356 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1357 "device is less than the minimum "
1360 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1364 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1365 "pool must be upgraded to add these vdevs"));
1366 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1370 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1371 "root pool can not have multiple vdevs"
1372 " or separate logs"));
1373 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1377 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1378 "cache device must be a disk or disk slice"));
1379 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1383 (void) zpool_standard_error(hdl, errno, msg);
1391 zcmd_free_nvlists(&zc);
1397 * Exports the pool from the system. The caller must ensure that there are no
1398 * mounted datasets in the pool.
1401 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1402 const char *log_str)
1404 zfs_cmd_t zc = { 0 };
1407 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1408 "cannot export '%s'"), zhp->zpool_name);
1410 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1411 zc.zc_cookie = force;
1412 zc.zc_guid = hardforce;
1413 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1415 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1418 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1419 "use '-f' to override the following errors:\n"
1420 "'%s' has an active shared spare which could be"
1421 " used by other pools once '%s' is exported."),
1422 zhp->zpool_name, zhp->zpool_name);
1423 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1426 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1435 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1437 return (zpool_export_common(zhp, force, B_FALSE, log_str));
1441 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1443 return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1447 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1450 nvlist_t *nv = NULL;
1456 if (!hdl->libzfs_printerr || config == NULL)
1459 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1460 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1464 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1466 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1468 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1469 strftime(timestr, 128, 0, &t) != 0) {
1471 (void) printf(dgettext(TEXT_DOMAIN,
1472 "Would be able to return %s "
1473 "to its state as of %s.\n"),
1476 (void) printf(dgettext(TEXT_DOMAIN,
1477 "Pool %s returned to its state as of %s.\n"),
1481 (void) printf(dgettext(TEXT_DOMAIN,
1482 "%s approximately %lld "),
1483 dryrun ? "Would discard" : "Discarded",
1485 (void) printf(dgettext(TEXT_DOMAIN,
1486 "minutes of transactions.\n"));
1487 } else if (loss > 0) {
1488 (void) printf(dgettext(TEXT_DOMAIN,
1489 "%s approximately %lld "),
1490 dryrun ? "Would discard" : "Discarded", loss);
1491 (void) printf(dgettext(TEXT_DOMAIN,
1492 "seconds of transactions.\n"));
1498 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1501 nvlist_t *nv = NULL;
1503 uint64_t edata = UINT64_MAX;
1508 if (!hdl->libzfs_printerr)
1512 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1514 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1516 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1517 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1518 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1519 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1522 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1523 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1526 (void) printf(dgettext(TEXT_DOMAIN,
1527 "Recovery is possible, but will result in some data loss.\n"));
1529 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1530 strftime(timestr, 128, 0, &t) != 0) {
1531 (void) printf(dgettext(TEXT_DOMAIN,
1532 "\tReturning the pool to its state as of %s\n"
1533 "\tshould correct the problem. "),
1536 (void) printf(dgettext(TEXT_DOMAIN,
1537 "\tReverting the pool to an earlier state "
1538 "should correct the problem.\n\t"));
1542 (void) printf(dgettext(TEXT_DOMAIN,
1543 "Approximately %lld minutes of data\n"
1544 "\tmust be discarded, irreversibly. "), (loss + 30) / 60);
1545 } else if (loss > 0) {
1546 (void) printf(dgettext(TEXT_DOMAIN,
1547 "Approximately %lld seconds of data\n"
1548 "\tmust be discarded, irreversibly. "), loss);
1550 if (edata != 0 && edata != UINT64_MAX) {
1552 (void) printf(dgettext(TEXT_DOMAIN,
1553 "After rewind, at least\n"
1554 "\tone persistent user-data error will remain. "));
1556 (void) printf(dgettext(TEXT_DOMAIN,
1557 "After rewind, several\n"
1558 "\tpersistent user-data errors will remain. "));
1561 (void) printf(dgettext(TEXT_DOMAIN,
1562 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1563 reason >= 0 ? "clear" : "import", name);
1565 (void) printf(dgettext(TEXT_DOMAIN,
1566 "A scrub of the pool\n"
1567 "\tis strongly recommended after recovery.\n"));
1571 (void) printf(dgettext(TEXT_DOMAIN,
1572 "Destroy and re-create the pool from\n\ta backup source.\n"));
1576 * zpool_import() is a contracted interface. Should be kept the same
1579 * Applications should use zpool_import_props() to import a pool with
1580 * new properties value to be set.
1583 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1586 nvlist_t *props = NULL;
1589 if (altroot != NULL) {
1590 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1591 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1592 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1596 if (nvlist_add_string(props,
1597 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1598 nvlist_add_string(props,
1599 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1601 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1602 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1607 ret = zpool_import_props(hdl, config, newname, props,
1615 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1621 uint64_t is_log = 0;
1623 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1627 (void) printf("\t%*s%s%s\n", indent, "", name,
1628 is_log ? " [log]" : "");
1630 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1631 &child, &children) != 0)
1634 for (c = 0; c < children; c++) {
1635 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
1636 print_vdev_tree(hdl, vname, child[c], indent + 2);
1642 zpool_print_unsup_feat(nvlist_t *config)
1644 nvlist_t *nvinfo, *unsup_feat;
1646 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1648 verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1651 for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1652 nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1655 verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1656 verify(nvpair_value_string(nvp, &desc) == 0);
1658 if (strlen(desc) > 0)
1659 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1661 (void) printf("\t%s\n", nvpair_name(nvp));
1666 * Import the given pool using the known configuration and a list of
1667 * properties to be set. The configuration should have come from
1668 * zpool_find_import(). The 'newname' parameters control whether the pool
1669 * is imported with a different name.
1672 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1673 nvlist_t *props, int flags)
1675 zfs_cmd_t zc = { 0 };
1676 zpool_rewind_policy_t policy;
1677 nvlist_t *nv = NULL;
1678 nvlist_t *nvinfo = NULL;
1679 nvlist_t *missing = NULL;
1686 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1689 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1690 "cannot import pool '%s'"), origname);
1692 if (newname != NULL) {
1693 if (!zpool_name_valid(hdl, B_FALSE, newname))
1694 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1695 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1697 thename = (char *)newname;
1702 if (props != NULL) {
1704 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1706 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1709 if ((props = zpool_valid_proplist(hdl, origname,
1710 props, version, flags, errbuf)) == NULL)
1712 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1719 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1721 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1724 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1725 zcmd_free_nvlists(&zc);
1728 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1729 zcmd_free_nvlists(&zc);
1733 zc.zc_cookie = flags;
1734 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1736 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1737 zcmd_free_nvlists(&zc);
1744 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1746 zcmd_free_nvlists(&zc);
1748 zpool_get_rewind_policy(config, &policy);
1754 * Dry-run failed, but we print out what success
1755 * looks like if we found a best txg
1757 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1758 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1764 if (newname == NULL)
1765 (void) snprintf(desc, sizeof (desc),
1766 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1769 (void) snprintf(desc, sizeof (desc),
1770 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1775 if (nv != NULL && nvlist_lookup_nvlist(nv,
1776 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1777 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1778 (void) printf(dgettext(TEXT_DOMAIN, "This "
1779 "pool uses the following feature(s) not "
1780 "supported by this system:\n"));
1781 zpool_print_unsup_feat(nv);
1782 if (nvlist_exists(nvinfo,
1783 ZPOOL_CONFIG_CAN_RDONLY)) {
1784 (void) printf(dgettext(TEXT_DOMAIN,
1785 "All unsupported features are only "
1786 "required for writing to the pool."
1787 "\nThe pool can be imported using "
1788 "'-o readonly=on'.\n"));
1792 * Unsupported version.
1794 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1798 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1802 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1803 "one or more devices is read only"));
1804 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1808 if (nv && nvlist_lookup_nvlist(nv,
1809 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1810 nvlist_lookup_nvlist(nvinfo,
1811 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1812 (void) printf(dgettext(TEXT_DOMAIN,
1813 "The devices below are missing, use "
1814 "'-m' to import the pool anyway:\n"));
1815 print_vdev_tree(hdl, NULL, missing, 2);
1816 (void) printf("\n");
1818 (void) zpool_standard_error(hdl, error, desc);
1822 (void) zpool_standard_error(hdl, error, desc);
1826 (void) zpool_standard_error(hdl, error, desc);
1827 zpool_explain_recover(hdl,
1828 newname ? origname : thename, -error, nv);
1835 zpool_handle_t *zhp;
1838 * This should never fail, but play it safe anyway.
1840 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1842 else if (zhp != NULL)
1844 if (policy.zrp_request &
1845 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1846 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1847 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1860 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1862 zfs_cmd_t zc = { 0 };
1864 libzfs_handle_t *hdl = zhp->zpool_hdl;
1866 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1867 zc.zc_cookie = func;
1869 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
1870 (errno == ENOENT && func != POOL_SCAN_NONE))
1873 if (func == POOL_SCAN_SCRUB) {
1874 (void) snprintf(msg, sizeof (msg),
1875 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1876 } else if (func == POOL_SCAN_NONE) {
1877 (void) snprintf(msg, sizeof (msg),
1878 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1881 assert(!"unexpected result");
1884 if (errno == EBUSY) {
1886 pool_scan_stat_t *ps = NULL;
1889 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1890 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1891 (void) nvlist_lookup_uint64_array(nvroot,
1892 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1893 if (ps && ps->pss_func == POOL_SCAN_SCRUB)
1894 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1896 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1897 } else if (errno == ENOENT) {
1898 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1900 return (zpool_standard_error(hdl, errno, msg));
1905 * This provides a very minimal check whether a given string is likely a
1906 * c#t#d# style string. Users of this are expected to do their own
1907 * verification of the s# part.
1909 #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1]))
1912 * More elaborate version for ones which may start with "/dev/dsk/"
1916 ctd_check_path(char *str) {
1918 * If it starts with a slash, check the last component.
1920 if (str && str[0] == '/') {
1921 char *tmp = strrchr(str, '/');
1924 * If it ends in "/old", check the second-to-last
1925 * component of the string instead.
1927 if (tmp != str && strcmp(tmp, "/old") == 0) {
1928 for (tmp--; *tmp != '/'; tmp--)
1933 return (CTD_CHECK(str));
1937 * Find a vdev that matches the search criteria specified. We use the
1938 * the nvpair name to determine how we should look for the device.
1939 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1940 * spare; but FALSE if its an INUSE spare.
1943 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1944 boolean_t *l2cache, boolean_t *log)
1951 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1953 /* Nothing to look for */
1954 if (search == NULL || pair == NULL)
1957 /* Obtain the key we will use to search */
1958 srchkey = nvpair_name(pair);
1960 switch (nvpair_type(pair)) {
1961 case DATA_TYPE_UINT64:
1962 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1963 uint64_t srchval, theguid;
1965 verify(nvpair_value_uint64(pair, &srchval) == 0);
1966 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1968 if (theguid == srchval)
1973 case DATA_TYPE_STRING: {
1974 char *srchval, *val;
1976 verify(nvpair_value_string(pair, &srchval) == 0);
1977 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1981 * Search for the requested value. Special cases:
1983 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
1984 * "s0" or "s0/old". The "s0" part is hidden from the user,
1985 * but included in the string, so this matches around it.
1986 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1988 * Otherwise, all other searches are simple string compares.
1990 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
1991 ctd_check_path(val)) {
1992 uint64_t wholedisk = 0;
1994 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1997 int slen = strlen(srchval);
1998 int vlen = strlen(val);
2000 if (slen != vlen - 2)
2004 * make_leaf_vdev() should only set
2005 * wholedisk for ZPOOL_CONFIG_PATHs which
2006 * will include "/dev/dsk/", giving plenty of
2007 * room for the indices used next.
2012 * strings identical except trailing "s0"
2014 if (strcmp(&val[vlen - 2], "s0") == 0 &&
2015 strncmp(srchval, val, slen) == 0)
2019 * strings identical except trailing "s0/old"
2021 if (strcmp(&val[vlen - 6], "s0/old") == 0 &&
2022 strcmp(&srchval[slen - 4], "/old") == 0 &&
2023 strncmp(srchval, val, slen - 4) == 0)
2028 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2029 char *type, *idx, *end, *p;
2030 uint64_t id, vdev_id;
2033 * Determine our vdev type, keeping in mind
2034 * that the srchval is composed of a type and
2035 * vdev id pair (i.e. mirror-4).
2037 if ((type = strdup(srchval)) == NULL)
2040 if ((p = strrchr(type, '-')) == NULL) {
2048 * If the types don't match then keep looking.
2050 if (strncmp(val, type, strlen(val)) != 0) {
2055 verify(strncmp(type, VDEV_TYPE_RAIDZ,
2056 strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2057 strncmp(type, VDEV_TYPE_MIRROR,
2058 strlen(VDEV_TYPE_MIRROR)) == 0);
2059 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2063 vdev_id = strtoull(idx, &end, 10);
2070 * Now verify that we have the correct vdev id.
2079 if (strcmp(srchval, val) == 0)
2088 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2089 &child, &children) != 0)
2092 for (c = 0; c < children; c++) {
2093 if ((ret = vdev_to_nvlist_iter(child[c], search,
2094 avail_spare, l2cache, NULL)) != NULL) {
2096 * The 'is_log' value is only set for the toplevel
2097 * vdev, not the leaf vdevs. So we always lookup the
2098 * log device from the root of the vdev tree (where
2099 * 'log' is non-NULL).
2102 nvlist_lookup_uint64(child[c],
2103 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2111 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2112 &child, &children) == 0) {
2113 for (c = 0; c < children; c++) {
2114 if ((ret = vdev_to_nvlist_iter(child[c], search,
2115 avail_spare, l2cache, NULL)) != NULL) {
2116 *avail_spare = B_TRUE;
2122 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2123 &child, &children) == 0) {
2124 for (c = 0; c < children; c++) {
2125 if ((ret = vdev_to_nvlist_iter(child[c], search,
2126 avail_spare, l2cache, NULL)) != NULL) {
2137 * Given a physical path (minus the "/devices" prefix), find the
2141 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2142 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2144 nvlist_t *search, *nvroot, *ret;
2146 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2147 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2149 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2152 *avail_spare = B_FALSE;
2156 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2157 nvlist_free(search);
2163 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2166 zpool_vdev_is_interior(const char *name)
2168 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2169 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2175 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2176 boolean_t *l2cache, boolean_t *log)
2178 char buf[MAXPATHLEN];
2180 nvlist_t *nvroot, *search, *ret;
2183 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2185 guid = strtoull(path, &end, 10);
2186 if (guid != 0 && *end == '\0') {
2187 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2188 } else if (zpool_vdev_is_interior(path)) {
2189 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2190 } else if (path[0] != '/') {
2191 (void) snprintf(buf, sizeof (buf), "%s%s", _PATH_DEV, path);
2192 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
2194 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2197 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2200 *avail_spare = B_FALSE;
2204 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2205 nvlist_free(search);
2211 vdev_online(nvlist_t *nv)
2215 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2216 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2217 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2224 * Helper function for zpool_get_physpaths().
2227 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2228 size_t *bytes_written)
2230 size_t bytes_left, pos, rsz;
2234 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2236 return (EZFS_NODEVICE);
2238 pos = *bytes_written;
2239 bytes_left = physpath_size - pos;
2240 format = (pos == 0) ? "%s" : " %s";
2242 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2243 *bytes_written += rsz;
2245 if (rsz >= bytes_left) {
2246 /* if physpath was not copied properly, clear it */
2247 if (bytes_left != 0) {
2250 return (EZFS_NOSPC);
2256 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2257 size_t *rsz, boolean_t is_spare)
2262 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2263 return (EZFS_INVALCONFIG);
2265 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2267 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2268 * For a spare vdev, we only want to boot from the active
2273 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2276 return (EZFS_INVALCONFIG);
2279 if (vdev_online(nv)) {
2280 if ((ret = vdev_get_one_physpath(nv, physpath,
2281 phypath_size, rsz)) != 0)
2284 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2285 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2286 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2291 if (nvlist_lookup_nvlist_array(nv,
2292 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2293 return (EZFS_INVALCONFIG);
2295 for (i = 0; i < count; i++) {
2296 ret = vdev_get_physpaths(child[i], physpath,
2297 phypath_size, rsz, is_spare);
2298 if (ret == EZFS_NOSPC)
2303 return (EZFS_POOL_INVALARG);
2307 * Get phys_path for a root pool config.
2308 * Return 0 on success; non-zero on failure.
2311 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2314 nvlist_t *vdev_root;
2321 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2323 return (EZFS_INVALCONFIG);
2325 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2326 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2327 &child, &count) != 0)
2328 return (EZFS_INVALCONFIG);
2331 * root pool can not have EFI labeled disks and can only have
2332 * a single top-level vdev.
2334 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
2335 pool_uses_efi(vdev_root))
2336 return (EZFS_POOL_INVALARG);
2338 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2341 /* No online devices */
2343 return (EZFS_NODEVICE);
2349 * Get phys_path for a root pool
2350 * Return 0 on success; non-zero on failure.
2353 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2355 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2360 * If the device has being dynamically expanded then we need to relabel
2361 * the disk to use the new unallocated space.
2364 zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2367 char path[MAXPATHLEN];
2370 int (*_efi_use_whole_disk)(int);
2372 if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2373 "efi_use_whole_disk")) == NULL)
2376 (void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name);
2378 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2379 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2380 "relabel '%s': unable to open device"), name);
2381 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2385 * It's possible that we might encounter an error if the device
2386 * does not have any unallocated space left. If so, we simply
2387 * ignore that error and continue on.
2389 error = _efi_use_whole_disk(fd);
2391 if (error && error != VT_ENOSPC) {
2392 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2393 "relabel '%s': unable to read disk capacity"), name);
2394 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2401 * Bring the specified vdev online. The 'flags' parameter is a set of the
2402 * ZFS_ONLINE_* flags.
2405 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2406 vdev_state_t *newstate)
2408 zfs_cmd_t zc = { 0 };
2411 boolean_t avail_spare, l2cache, islog;
2412 libzfs_handle_t *hdl = zhp->zpool_hdl;
2414 if (flags & ZFS_ONLINE_EXPAND) {
2415 (void) snprintf(msg, sizeof (msg),
2416 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2418 (void) snprintf(msg, sizeof (msg),
2419 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2422 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2423 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2425 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2427 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2430 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2432 if (flags & ZFS_ONLINE_EXPAND ||
2433 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2434 char *pathname = NULL;
2435 uint64_t wholedisk = 0;
2437 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2439 verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
2443 * XXX - L2ARC 1.0 devices can't support expansion.
2446 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2447 "cannot expand cache devices"));
2448 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2452 pathname += strlen(DISK_ROOT) + 1;
2453 (void) zpool_relabel_disk(hdl, pathname);
2457 zc.zc_cookie = VDEV_STATE_ONLINE;
2460 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2461 if (errno == EINVAL) {
2462 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2463 "from this pool into a new one. Use '%s' "
2464 "instead"), "zpool detach");
2465 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2467 return (zpool_standard_error(hdl, errno, msg));
2470 *newstate = zc.zc_cookie;
2475 * Take the specified vdev offline
2478 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2480 zfs_cmd_t zc = { 0 };
2483 boolean_t avail_spare, l2cache;
2484 libzfs_handle_t *hdl = zhp->zpool_hdl;
2486 (void) snprintf(msg, sizeof (msg),
2487 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2489 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2490 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2492 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2494 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2497 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2499 zc.zc_cookie = VDEV_STATE_OFFLINE;
2500 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2502 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2509 * There are no other replicas of this device.
2511 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2515 * The log device has unplayed logs
2517 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2520 return (zpool_standard_error(hdl, errno, msg));
2525 * Mark the given vdev faulted.
2528 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2530 zfs_cmd_t zc = { 0 };
2532 libzfs_handle_t *hdl = zhp->zpool_hdl;
2534 (void) snprintf(msg, sizeof (msg),
2535 dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2537 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2539 zc.zc_cookie = VDEV_STATE_FAULTED;
2542 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2549 * There are no other replicas of this device.
2551 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2554 return (zpool_standard_error(hdl, errno, msg));
2560 * Mark the given vdev degraded.
2563 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2565 zfs_cmd_t zc = { 0 };
2567 libzfs_handle_t *hdl = zhp->zpool_hdl;
2569 (void) snprintf(msg, sizeof (msg),
2570 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
2572 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2574 zc.zc_cookie = VDEV_STATE_DEGRADED;
2577 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2580 return (zpool_standard_error(hdl, errno, msg));
2584 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2588 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2594 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2596 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2599 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2600 children == 2 && child[which] == tgt)
2603 for (c = 0; c < children; c++)
2604 if (is_replacing_spare(child[c], tgt, which))
2612 * Attach new_disk (fully described by nvroot) to old_disk.
2613 * If 'replacing' is specified, the new disk will replace the old one.
2616 zpool_vdev_attach(zpool_handle_t *zhp,
2617 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2619 zfs_cmd_t zc = { 0 };
2623 boolean_t avail_spare, l2cache, islog;
2628 nvlist_t *config_root;
2629 libzfs_handle_t *hdl = zhp->zpool_hdl;
2630 boolean_t rootpool = zpool_is_bootable(zhp);
2633 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2634 "cannot replace %s with %s"), old_disk, new_disk);
2636 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2637 "cannot attach %s to %s"), new_disk, old_disk);
2640 * If this is a root pool, make sure that we're not attaching an
2641 * EFI labeled device.
2643 if (rootpool && pool_uses_efi(nvroot)) {
2644 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2645 "EFI labeled devices are not supported on root pools."));
2646 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2649 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2650 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2652 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2655 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2658 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2660 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2661 zc.zc_cookie = replacing;
2663 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2664 &child, &children) != 0 || children != 1) {
2665 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2666 "new device must be a single disk"));
2667 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2670 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2671 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2673 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2677 * If the target is a hot spare that has been swapped in, we can only
2678 * replace it with another hot spare.
2681 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2682 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2683 NULL) == NULL || !avail_spare) &&
2684 is_replacing_spare(config_root, tgt, 1)) {
2685 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2686 "can only be replaced by another hot spare"));
2688 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2693 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2696 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2698 zcmd_free_nvlists(&zc);
2703 * XXX need a better way to prevent user from
2704 * booting up a half-baked vdev.
2706 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2707 "sure to wait until resilver is done "
2708 "before rebooting.\n"));
2709 (void) fprintf(stderr, "\n");
2710 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "If "
2711 "you boot from pool '%s', you may need to update\n"
2712 "boot code on newly attached disk '%s'.\n\n"
2713 "Assuming you use GPT partitioning and 'da0' is "
2714 "your new boot disk\n"
2715 "you may use the following command:\n\n"
2716 "\tgpart bootcode -b /boot/pmbr -p "
2717 "/boot/gptzfsboot -i 1 da0\n\n"),
2718 zhp->zpool_name, new_disk);
2726 * Can't attach to or replace this type of vdev.
2729 uint64_t version = zpool_get_prop_int(zhp,
2730 ZPOOL_PROP_VERSION, NULL);
2733 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2734 "cannot replace a log with a spare"));
2735 else if (version >= SPA_VERSION_MULTI_REPLACE)
2736 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2737 "already in replacing/spare config; wait "
2738 "for completion or use 'zpool detach'"));
2740 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2741 "cannot replace a replacing device"));
2743 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2744 "can only attach to mirrors and top-level "
2747 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2752 * The new device must be a single disk.
2754 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2755 "new device must be a single disk"));
2756 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2760 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2762 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2767 * The new device is too small.
2769 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2770 "device is too small"));
2771 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2776 * The new device has a different alignment requirement.
2778 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2779 "devices have different sector alignment"));
2780 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2785 * The resulting top-level vdev spec won't fit in the label.
2787 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2791 (void) zpool_standard_error(hdl, errno, msg);
2798 * Detach the specified device.
2801 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2803 zfs_cmd_t zc = { 0 };
2806 boolean_t avail_spare, l2cache;
2807 libzfs_handle_t *hdl = zhp->zpool_hdl;
2809 (void) snprintf(msg, sizeof (msg),
2810 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2812 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2813 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2815 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2818 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2821 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2823 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2825 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2832 * Can't detach from this type of vdev.
2834 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2835 "applicable to mirror and replacing vdevs"));
2836 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2841 * There are no other replicas of this device.
2843 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2847 (void) zpool_standard_error(hdl, errno, msg);
2854 * Find a mirror vdev in the source nvlist.
2856 * The mchild array contains a list of disks in one of the top-level mirrors
2857 * of the source pool. The schild array contains a list of disks that the
2858 * user specified on the command line. We loop over the mchild array to
2859 * see if any entry in the schild array matches.
2861 * If a disk in the mchild array is found in the schild array, we return
2862 * the index of that entry. Otherwise we return -1.
2865 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2866 nvlist_t **schild, uint_t schildren)
2870 for (mc = 0; mc < mchildren; mc++) {
2872 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2873 mchild[mc], B_FALSE);
2875 for (sc = 0; sc < schildren; sc++) {
2876 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2877 schild[sc], B_FALSE);
2878 boolean_t result = (strcmp(mpath, spath) == 0);
2894 * Split a mirror pool. If newroot points to null, then a new nvlist
2895 * is generated and it is the responsibility of the caller to free it.
2898 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2899 nvlist_t *props, splitflags_t flags)
2901 zfs_cmd_t zc = { 0 };
2903 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2904 nvlist_t **varray = NULL, *zc_props = NULL;
2905 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2906 libzfs_handle_t *hdl = zhp->zpool_hdl;
2908 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2911 (void) snprintf(msg, sizeof (msg),
2912 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2914 if (!zpool_name_valid(hdl, B_FALSE, newname))
2915 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2917 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2918 (void) fprintf(stderr, gettext("Internal error: unable to "
2919 "retrieve pool configuration\n"));
2923 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2925 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2928 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2929 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2930 props, vers, flags, msg)) == NULL)
2934 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2936 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2937 "Source pool is missing vdev tree"));
2939 nvlist_free(zc_props);
2943 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2946 if (*newroot == NULL ||
2947 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2948 &newchild, &newchildren) != 0)
2951 for (c = 0; c < children; c++) {
2952 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2954 nvlist_t **mchild, *vdev;
2959 * Unlike cache & spares, slogs are stored in the
2960 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2962 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2964 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2966 if (is_log || is_hole) {
2968 * Create a hole vdev and put it in the config.
2970 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2972 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2973 VDEV_TYPE_HOLE) != 0)
2975 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2980 varray[vcount++] = vdev;
2984 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
2986 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2987 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2988 "Source pool must be composed only of mirrors\n"));
2989 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2993 verify(nvlist_lookup_nvlist_array(child[c],
2994 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2996 /* find or add an entry for this top-level vdev */
2997 if (newchildren > 0 &&
2998 (entry = find_vdev_entry(zhp, mchild, mchildren,
2999 newchild, newchildren)) >= 0) {
3000 /* We found a disk that the user specified. */
3001 vdev = mchild[entry];
3004 /* User didn't specify a disk for this vdev. */
3005 vdev = mchild[mchildren - 1];
3008 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3012 /* did we find every disk the user specified? */
3013 if (found != newchildren) {
3014 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3015 "include at most one disk from each mirror"));
3016 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3020 /* Prepare the nvlist for populating. */
3021 if (*newroot == NULL) {
3022 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3025 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3026 VDEV_TYPE_ROOT) != 0)
3029 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3032 /* Add all the children we found */
3033 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3034 lastlog == 0 ? vcount : lastlog) != 0)
3038 * If we're just doing a dry run, exit now with success.
3041 memory_err = B_FALSE;
3046 /* now build up the config list & call the ioctl */
3047 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3050 if (nvlist_add_nvlist(newconfig,
3051 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3052 nvlist_add_string(newconfig,
3053 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3054 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3058 * The new pool is automatically part of the namespace unless we
3059 * explicitly export it.
3062 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3063 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3064 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3065 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3067 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3070 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3071 retval = zpool_standard_error(hdl, errno, msg);
3076 memory_err = B_FALSE;
3079 if (varray != NULL) {
3082 for (v = 0; v < vcount; v++)
3083 nvlist_free(varray[v]);
3086 zcmd_free_nvlists(&zc);
3088 nvlist_free(zc_props);
3090 nvlist_free(newconfig);
3092 nvlist_free(*newroot);
3100 return (no_memory(hdl));
3106 * Remove the given device. Currently, this is supported only for hot spares
3107 * and level 2 cache devices.
3110 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3112 zfs_cmd_t zc = { 0 };
3115 boolean_t avail_spare, l2cache, islog;
3116 libzfs_handle_t *hdl = zhp->zpool_hdl;
3119 (void) snprintf(msg, sizeof (msg),
3120 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3122 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3123 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3125 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3127 * XXX - this should just go away.
3129 if (!avail_spare && !l2cache && !islog) {
3130 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3131 "only inactive hot spares, cache, top-level, "
3132 "or log devices can be removed"));
3133 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3136 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3137 if (islog && version < SPA_VERSION_HOLES) {
3138 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3139 "pool must be upgrade to support log removal"));
3140 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3143 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3145 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3148 return (zpool_standard_error(hdl, errno, msg));
3152 * Clear the errors for the pool, or the particular device if specified.
3155 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3157 zfs_cmd_t zc = { 0 };
3160 zpool_rewind_policy_t policy;
3161 boolean_t avail_spare, l2cache;
3162 libzfs_handle_t *hdl = zhp->zpool_hdl;
3163 nvlist_t *nvi = NULL;
3167 (void) snprintf(msg, sizeof (msg),
3168 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3171 (void) snprintf(msg, sizeof (msg),
3172 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3175 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3177 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3178 &l2cache, NULL)) == 0)
3179 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3182 * Don't allow error clearing for hot spares. Do allow
3183 * error clearing for l2cache devices.
3186 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3188 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3192 zpool_get_rewind_policy(rewindnvl, &policy);
3193 zc.zc_cookie = policy.zrp_request;
3195 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3198 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3201 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3203 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3204 zcmd_free_nvlists(&zc);
3209 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
3210 errno != EPERM && errno != EACCES)) {
3211 if (policy.zrp_request &
3212 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3213 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3214 zpool_rewind_exclaim(hdl, zc.zc_name,
3215 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
3219 zcmd_free_nvlists(&zc);
3223 zcmd_free_nvlists(&zc);
3224 return (zpool_standard_error(hdl, errno, msg));
3228 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3231 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3233 zfs_cmd_t zc = { 0 };
3235 libzfs_handle_t *hdl = zhp->zpool_hdl;
3237 (void) snprintf(msg, sizeof (msg),
3238 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3241 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3243 zc.zc_cookie = ZPOOL_NO_REWIND;
3245 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3248 return (zpool_standard_error(hdl, errno, msg));
3252 * Change the GUID for a pool.
3255 zpool_reguid(zpool_handle_t *zhp)
3258 libzfs_handle_t *hdl = zhp->zpool_hdl;
3259 zfs_cmd_t zc = { 0 };
3261 (void) snprintf(msg, sizeof (msg),
3262 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3264 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3265 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3268 return (zpool_standard_error(hdl, errno, msg));
3275 zpool_reopen(zpool_handle_t *zhp)
3277 zfs_cmd_t zc = { 0 };
3279 libzfs_handle_t *hdl = zhp->zpool_hdl;
3281 (void) snprintf(msg, sizeof (msg),
3282 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
3285 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3286 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
3288 return (zpool_standard_error(hdl, errno, msg));
3292 * Convert from a devid string to a path.
3295 devid_to_path(char *devid_str)
3300 devid_nmlist_t *list = NULL;
3303 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3306 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3308 devid_str_free(minor);
3315 * In a case the strdup() fails, we will just return NULL below.
3317 path = strdup(list[0].devname);
3319 devid_free_nmlist(list);
3325 * Convert from a path to a devid string.
3328 path_to_devid(const char *path)
3334 if ((fd = open(path, O_RDONLY)) < 0)
3339 if (devid_get(fd, &devid) == 0) {
3340 if (devid_get_minor_name(fd, &minor) == 0)
3341 ret = devid_str_encode(devid, minor);
3343 devid_str_free(minor);
3352 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3353 * ignore any failure here, since a common case is for an unprivileged user to
3354 * type 'zpool status', and we'll display the correct information anyway.
3357 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3359 zfs_cmd_t zc = { 0 };
3361 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3362 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3363 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3366 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3370 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3371 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3372 * We also check if this is a whole disk, in which case we strip off the
3373 * trailing 's0' slice name.
3375 * This routine is also responsible for identifying when disks have been
3376 * reconfigured in a new location. The kernel will have opened the device by
3377 * devid, but the path will still refer to the old location. To catch this, we
3378 * first do a path -> devid translation (which is fast for the common case). If
3379 * the devid matches, we're done. If not, we do a reverse devid -> path
3380 * translation and issue the appropriate ioctl() to update the path of the vdev.
3381 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3385 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3396 have_stats = nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3397 (uint64_t **)&vs, &vsc) == 0;
3398 have_path = nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0;
3401 * If the device is not currently present, assume it will not
3402 * come back at the same device path. Display the device by GUID.
3404 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3405 have_path && have_stats && vs->vs_state <= VDEV_STATE_CANT_OPEN) {
3406 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3408 (void) snprintf(buf, sizeof (buf), "%llu",
3409 (u_longlong_t)value);
3411 } else if (have_path) {
3414 * If the device is dead (faulted, offline, etc) then don't
3415 * bother opening it. Otherwise we may be forcing the user to
3416 * open a misbehaving device, which can have undesirable
3419 if ((have_stats == 0 ||
3420 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3422 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3424 * Determine if the current path is correct.
3426 char *newdevid = path_to_devid(path);
3428 if (newdevid == NULL ||
3429 strcmp(devid, newdevid) != 0) {
3432 if ((newpath = devid_to_path(devid)) != NULL) {
3434 * Update the path appropriately.
3436 set_path(zhp, nv, newpath);
3437 if (nvlist_add_string(nv,
3438 ZPOOL_CONFIG_PATH, newpath) == 0)
3439 verify(nvlist_lookup_string(nv,
3447 devid_str_free(newdevid);
3451 if (strncmp(path, "/dev/dsk/", 9) == 0)
3454 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3455 &value) == 0 && value) {
3456 int pathlen = strlen(path);
3457 char *tmp = zfs_strdup(hdl, path);
3460 * If it starts with c#, and ends with "s0", chop
3461 * the "s0" off, or if it ends with "s0/old", remove
3462 * the "s0" from the middle.
3464 if (CTD_CHECK(tmp)) {
3465 if (strcmp(&tmp[pathlen - 2], "s0") == 0) {
3466 tmp[pathlen - 2] = '\0';
3467 } else if (pathlen > 6 &&
3468 strcmp(&tmp[pathlen - 6], "s0/old") == 0) {
3469 (void) strcpy(&tmp[pathlen - 6],
3476 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3477 path += sizeof(_PATH_DEV) - 1;
3480 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3483 * If it's a raidz device, we need to stick in the parity level.
3485 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3486 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3488 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3489 (u_longlong_t)value);
3494 * We identify each top-level vdev by using a <type-id>
3495 * naming convention.
3500 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3502 (void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3508 return (zfs_strdup(hdl, path));
3512 zbookmark_compare(const void *a, const void *b)
3514 return (memcmp(a, b, sizeof (zbookmark_t)));
3518 * Retrieve the persistent error log, uniquify the members, and return to the
3522 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3524 zfs_cmd_t zc = { 0 };
3526 zbookmark_t *zb = NULL;
3530 * Retrieve the raw error list from the kernel. If the number of errors
3531 * has increased, allocate more space and continue until we get the
3534 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3538 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3539 count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
3541 zc.zc_nvlist_dst_size = count;
3542 (void) strcpy(zc.zc_name, zhp->zpool_name);
3544 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3546 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3547 if (errno == ENOMEM) {
3548 count = zc.zc_nvlist_dst_size;
3549 if ((zc.zc_nvlist_dst = (uintptr_t)
3550 zfs_alloc(zhp->zpool_hdl, count *
3551 sizeof (zbookmark_t))) == (uintptr_t)NULL)
3562 * Sort the resulting bookmarks. This is a little confusing due to the
3563 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3564 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3565 * _not_ copied as part of the process. So we point the start of our
3566 * array appropriate and decrement the total number of elements.
3568 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
3569 zc.zc_nvlist_dst_size;
3570 count -= zc.zc_nvlist_dst_size;
3572 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
3574 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3577 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3579 for (i = 0; i < count; i++) {
3582 /* ignoring zb_blkid and zb_level for now */
3583 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3584 zb[i-1].zb_object == zb[i].zb_object)
3587 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3589 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3590 zb[i].zb_objset) != 0) {
3594 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3595 zb[i].zb_object) != 0) {
3599 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3606 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3610 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3611 return (no_memory(zhp->zpool_hdl));
3615 * Upgrade a ZFS pool to the latest on-disk version.
3618 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3620 zfs_cmd_t zc = { 0 };
3621 libzfs_handle_t *hdl = zhp->zpool_hdl;
3623 (void) strcpy(zc.zc_name, zhp->zpool_name);
3624 zc.zc_cookie = new_version;
3626 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3627 return (zpool_standard_error_fmt(hdl, errno,
3628 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3634 zfs_save_arguments(int argc, char **argv, char *string, int len)
3636 (void) strlcpy(string, basename(argv[0]), len);
3637 for (int i = 1; i < argc; i++) {
3638 (void) strlcat(string, " ", len);
3639 (void) strlcat(string, argv[i], len);
3644 zpool_log_history(libzfs_handle_t *hdl, const char *message)
3646 zfs_cmd_t zc = { 0 };
3650 args = fnvlist_alloc();
3651 fnvlist_add_string(args, "message", message);
3652 err = zcmd_write_src_nvlist(hdl, &zc, args);
3654 err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
3656 zcmd_free_nvlists(&zc);
3661 * Perform ioctl to get some command history of a pool.
3663 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3664 * logical offset of the history buffer to start reading from.
3666 * Upon return, 'off' is the next logical offset to read from and
3667 * 'len' is the actual amount of bytes read into 'buf'.
3670 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3672 zfs_cmd_t zc = { 0 };
3673 libzfs_handle_t *hdl = zhp->zpool_hdl;
3675 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3677 zc.zc_history = (uint64_t)(uintptr_t)buf;
3678 zc.zc_history_len = *len;
3679 zc.zc_history_offset = *off;
3681 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3684 return (zfs_error_fmt(hdl, EZFS_PERM,
3685 dgettext(TEXT_DOMAIN,
3686 "cannot show history for pool '%s'"),
3689 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3690 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3691 "'%s'"), zhp->zpool_name));
3693 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3694 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3695 "'%s', pool must be upgraded"), zhp->zpool_name));
3697 return (zpool_standard_error_fmt(hdl, errno,
3698 dgettext(TEXT_DOMAIN,
3699 "cannot get history for '%s'"), zhp->zpool_name));
3703 *len = zc.zc_history_len;
3704 *off = zc.zc_history_offset;
3710 * Process the buffer of nvlists, unpacking and storing each nvlist record
3711 * into 'records'. 'leftover' is set to the number of bytes that weren't
3712 * processed as there wasn't a complete record.
3715 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3716 nvlist_t ***records, uint_t *numrecords)
3722 while (bytes_read > sizeof (reclen)) {
3724 /* get length of packed record (stored as little endian) */
3725 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3726 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3728 if (bytes_read < sizeof (reclen) + reclen)
3732 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3734 bytes_read -= sizeof (reclen) + reclen;
3735 buf += sizeof (reclen) + reclen;
3737 /* add record to nvlist array */
3739 if (ISP2(*numrecords + 1)) {
3740 *records = realloc(*records,
3741 *numrecords * 2 * sizeof (nvlist_t *));
3743 (*records)[*numrecords - 1] = nv;
3746 *leftover = bytes_read;
3750 /* from spa_history.c: spa_history_create_obj() */
3751 #define HIS_BUF_LEN_DEF (128 << 10)
3752 #define HIS_BUF_LEN_MAX (1 << 30)
3755 * Retrieve the command history of a pool.
3758 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3761 uint64_t bufsize = HIS_BUF_LEN_DEF;
3763 nvlist_t **records = NULL;
3764 uint_t numrecords = 0;
3767 if ((buf = malloc(bufsize)) == NULL)
3770 uint64_t bytes_read = bufsize;
3773 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3776 /* if nothing else was read in, we're at EOF, just return */
3777 if (bytes_read == 0)
3780 if ((err = zpool_history_unpack(buf, bytes_read,
3781 &leftover, &records, &numrecords)) != 0)
3786 * If the history block is too big, double the buffer
3787 * size and try again.
3789 if (leftover == bytes_read) {
3794 if ((bufsize >= HIS_BUF_LEN_MAX) ||
3795 ((buf = malloc(bufsize)) == NULL)) {
3806 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3807 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3808 records, numrecords) == 0);
3810 for (i = 0; i < numrecords; i++)
3811 nvlist_free(records[i]);
3818 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3819 char *pathname, size_t len)
3821 zfs_cmd_t zc = { 0 };
3822 boolean_t mounted = B_FALSE;
3823 char *mntpnt = NULL;
3824 char dsname[MAXNAMELEN];
3827 /* special case for the MOS */
3828 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
3832 /* get the dataset's name */
3833 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3835 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3836 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3837 /* just write out a path of two object numbers */
3838 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3842 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3844 /* find out if the dataset is mounted */
3845 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3847 /* get the corrupted object's path */
3848 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3850 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3853 (void) snprintf(pathname, len, "%s%s", mntpnt,
3856 (void) snprintf(pathname, len, "%s:%s",
3857 dsname, zc.zc_value);
3860 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
3867 * Read the EFI label from the config, if a label does not exist then
3868 * pass back the error to the caller. If the caller has passed a non-NULL
3869 * diskaddr argument then we set it to the starting address of the EFI
3873 read_efi_label(nvlist_t *config, diskaddr_t *sb)
3877 char diskname[MAXPATHLEN];
3880 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3883 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
3884 strrchr(path, '/'));
3885 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
3886 struct dk_gpt *vtoc;
3888 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3890 *sb = vtoc->efi_parts[0].p_start;
3899 * determine where a partition starts on a disk in the current
3903 find_start_block(nvlist_t *config)
3907 diskaddr_t sb = MAXOFFSET_T;
3910 if (nvlist_lookup_nvlist_array(config,
3911 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3912 if (nvlist_lookup_uint64(config,
3913 ZPOOL_CONFIG_WHOLE_DISK,
3914 &wholedisk) != 0 || !wholedisk) {
3915 return (MAXOFFSET_T);
3917 if (read_efi_label(config, &sb) < 0)
3922 for (c = 0; c < children; c++) {
3923 sb = find_start_block(child[c]);
3924 if (sb != MAXOFFSET_T) {
3928 return (MAXOFFSET_T);
3933 * Label an individual disk. The name provided is the short name,
3934 * stripped of any leading /dev path.
3937 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name)
3940 char path[MAXPATHLEN];
3941 struct dk_gpt *vtoc;
3943 size_t resv = EFI_MIN_RESV_SIZE;
3944 uint64_t slice_size;
3945 diskaddr_t start_block;
3948 /* prepare an error message just in case */
3949 (void) snprintf(errbuf, sizeof (errbuf),
3950 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3955 if (zpool_is_bootable(zhp)) {
3956 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3957 "EFI labeled devices are not supported on root "
3959 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3962 verify(nvlist_lookup_nvlist(zhp->zpool_config,
3963 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3965 if (zhp->zpool_start_block == 0)
3966 start_block = find_start_block(nvroot);
3968 start_block = zhp->zpool_start_block;
3969 zhp->zpool_start_block = start_block;
3972 start_block = NEW_START_BLOCK;
3975 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
3978 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
3980 * This shouldn't happen. We've long since verified that this
3981 * is a valid device.
3984 dgettext(TEXT_DOMAIN, "unable to open device"));
3985 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
3988 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
3990 * The only way this can fail is if we run out of memory, or we
3991 * were unable to read the disk's capacity
3993 if (errno == ENOMEM)
3994 (void) no_memory(hdl);
3997 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3998 "unable to read disk capacity"), name);
4000 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
4003 slice_size = vtoc->efi_last_u_lba + 1;
4004 slice_size -= EFI_MIN_RESV_SIZE;
4005 if (start_block == MAXOFFSET_T)
4006 start_block = NEW_START_BLOCK;
4007 slice_size -= start_block;
4009 vtoc->efi_parts[0].p_start = start_block;
4010 vtoc->efi_parts[0].p_size = slice_size;
4013 * Why we use V_USR: V_BACKUP confuses users, and is considered
4014 * disposable by some EFI utilities (since EFI doesn't have a backup
4015 * slice). V_UNASSIGNED is supposed to be used only for zero size
4016 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4017 * etc. were all pretty specific. V_USR is as close to reality as we
4018 * can get, in the absence of V_OTHER.
4020 vtoc->efi_parts[0].p_tag = V_USR;
4021 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
4023 vtoc->efi_parts[8].p_start = slice_size + start_block;
4024 vtoc->efi_parts[8].p_size = resv;
4025 vtoc->efi_parts[8].p_tag = V_RESERVED;
4027 if (efi_write(fd, vtoc) != 0) {
4029 * Some block drivers (like pcata) may not support EFI
4030 * GPT labels. Print out a helpful error message dir-
4031 * ecting the user to manually label the disk and give
4037 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4038 "try using fdisk(1M) and then provide a specific slice"));
4039 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4049 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
4055 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
4056 if (strcmp(type, VDEV_TYPE_FILE) == 0 ||
4057 strcmp(type, VDEV_TYPE_HOLE) == 0 ||
4058 strcmp(type, VDEV_TYPE_MISSING) == 0) {
4059 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4060 "vdev type '%s' is not supported"), type);
4061 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
4064 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
4065 &child, &children) == 0) {
4066 for (c = 0; c < children; c++) {
4067 if (!supported_dump_vdev_type(hdl, child[c], errbuf))
4075 * Check if this zvol is allowable for use as a dump device; zero if
4076 * it is, > 0 if it isn't, < 0 if it isn't a zvol.
4078 * Allowable storage configurations include mirrors, all raidz variants, and
4079 * pools with log, cache, and spare devices. Pools which are backed by files or
4080 * have missing/hole vdevs are not suitable.
4083 zvol_check_dump_config(char *arg)
4085 zpool_handle_t *zhp = NULL;
4086 nvlist_t *config, *nvroot;
4090 libzfs_handle_t *hdl;
4092 char poolname[ZPOOL_MAXNAMELEN];
4093 int pathlen = strlen(ZVOL_FULL_DEV_DIR);
4096 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
4100 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4101 "dump is not supported on device '%s'"), arg);
4103 if ((hdl = libzfs_init()) == NULL)
4105 libzfs_print_on_error(hdl, B_TRUE);
4107 volname = arg + pathlen;
4109 /* check the configuration of the pool */
4110 if ((p = strchr(volname, '/')) == NULL) {
4111 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4112 "malformed dataset name"));
4113 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4115 } else if (p - volname >= ZFS_MAXNAMELEN) {
4116 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4117 "dataset name is too long"));
4118 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
4121 (void) strncpy(poolname, volname, p - volname);
4122 poolname[p - volname] = '\0';
4125 if ((zhp = zpool_open(hdl, poolname)) == NULL) {
4126 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4127 "could not open pool '%s'"), poolname);
4128 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4131 config = zpool_get_config(zhp, NULL);
4132 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4134 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4135 "could not obtain vdev configuration for '%s'"), poolname);
4136 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4140 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4141 &top, &toplevels) == 0);
4143 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {