4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012, 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:
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_LEAKED:
307 case ZPOOL_PROP_EXPANDSZ:
309 (void) snprintf(buf, len, "%llu",
310 (u_longlong_t)intval);
312 (void) zfs_nicenum(intval, buf, len);
316 case ZPOOL_PROP_CAPACITY:
318 (void) snprintf(buf, len, "%llu",
319 (u_longlong_t)intval);
321 (void) snprintf(buf, len, "%llu%%",
322 (u_longlong_t)intval);
325 case ZPOOL_PROP_FRAGMENTATION:
326 if (intval == UINT64_MAX) {
327 (void) strlcpy(buf, "-", len);
329 (void) snprintf(buf, len, "%llu%%",
330 (u_longlong_t)intval);
334 case ZPOOL_PROP_DEDUPRATIO:
335 (void) snprintf(buf, len, "%llu.%02llux",
336 (u_longlong_t)(intval / 100),
337 (u_longlong_t)(intval % 100));
340 case ZPOOL_PROP_HEALTH:
341 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
342 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
343 verify(nvlist_lookup_uint64_array(nvroot,
344 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
347 (void) strlcpy(buf, zpool_state_to_name(intval,
350 case ZPOOL_PROP_VERSION:
351 if (intval >= SPA_VERSION_FEATURES) {
352 (void) snprintf(buf, len, "-");
357 (void) snprintf(buf, len, "%llu", intval);
361 case PROP_TYPE_INDEX:
362 intval = zpool_get_prop_int(zhp, prop, &src);
363 if (zpool_prop_index_to_string(prop, intval, &strval)
366 (void) strlcpy(buf, strval, len);
380 * Check if the bootfs name has the same pool name as it is set to.
381 * Assuming bootfs is a valid dataset name.
384 bootfs_name_valid(const char *pool, char *bootfs)
386 int len = strlen(pool);
388 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
391 if (strncmp(pool, bootfs, len) == 0 &&
392 (bootfs[len] == '/' || bootfs[len] == '\0'))
399 * Inspect the configuration to determine if any of the devices contain
403 pool_uses_efi(nvlist_t *config)
409 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
410 &child, &children) != 0)
411 return (read_efi_label(config, NULL) >= 0);
413 for (c = 0; c < children; c++) {
414 if (pool_uses_efi(child[c]))
422 zpool_is_bootable(zpool_handle_t *zhp)
424 char bootfs[ZPOOL_MAXNAMELEN];
426 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
427 sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-",
428 sizeof (bootfs)) != 0);
433 * Given an nvlist of zpool properties to be set, validate that they are
434 * correct, and parse any numeric properties (index, boolean, etc) if they are
435 * specified as strings.
438 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
439 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
447 struct stat64 statbuf;
451 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
452 (void) no_memory(hdl);
457 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
458 const char *propname = nvpair_name(elem);
460 prop = zpool_name_to_prop(propname);
461 if (prop == ZPROP_INVAL && zpool_prop_feature(propname)) {
463 char *fname = strchr(propname, '@') + 1;
465 err = zfeature_lookup_name(fname, NULL);
467 ASSERT3U(err, ==, ENOENT);
468 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
469 "invalid feature '%s'"), fname);
470 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
474 if (nvpair_type(elem) != DATA_TYPE_STRING) {
475 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
476 "'%s' must be a string"), propname);
477 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
481 (void) nvpair_value_string(elem, &strval);
482 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) {
483 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
484 "property '%s' can only be set to "
485 "'enabled'"), propname);
486 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
490 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
491 (void) no_memory(hdl);
498 * Make sure this property is valid and applies to this type.
500 if (prop == ZPROP_INVAL) {
501 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
502 "invalid property '%s'"), propname);
503 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
507 if (zpool_prop_readonly(prop)) {
508 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
509 "is readonly"), propname);
510 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
514 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
515 &strval, &intval, errbuf) != 0)
519 * Perform additional checking for specific properties.
522 case ZPOOL_PROP_VERSION:
523 if (intval < version ||
524 !SPA_VERSION_IS_SUPPORTED(intval)) {
525 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
526 "property '%s' number %d is invalid."),
528 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
533 case ZPOOL_PROP_BOOTFS:
534 if (flags.create || flags.import) {
535 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
536 "property '%s' cannot be set at creation "
537 "or import time"), propname);
538 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
542 if (version < SPA_VERSION_BOOTFS) {
543 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
544 "pool must be upgraded to support "
545 "'%s' property"), propname);
546 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
551 * bootfs property value has to be a dataset name and
552 * the dataset has to be in the same pool as it sets to.
554 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
556 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
557 "is an invalid name"), strval);
558 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
562 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
563 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
564 "could not open pool '%s'"), poolname);
565 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
568 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
569 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
573 * bootfs property cannot be set on a disk which has
576 if (pool_uses_efi(nvroot)) {
577 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
578 "property '%s' not supported on "
579 "EFI labeled devices"), propname);
580 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
588 case ZPOOL_PROP_ALTROOT:
589 if (!flags.create && !flags.import) {
590 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
591 "property '%s' can only be set during pool "
592 "creation or import"), propname);
593 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
597 if (strval[0] != '/') {
598 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
599 "bad alternate root '%s'"), strval);
600 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
605 case ZPOOL_PROP_CACHEFILE:
606 if (strval[0] == '\0')
609 if (strcmp(strval, "none") == 0)
612 if (strval[0] != '/') {
613 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
614 "property '%s' must be empty, an "
615 "absolute path, or 'none'"), propname);
616 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
620 slash = strrchr(strval, '/');
622 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
623 strcmp(slash, "/..") == 0) {
624 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
625 "'%s' is not a valid file"), strval);
626 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
632 if (strval[0] != '\0' &&
633 (stat64(strval, &statbuf) != 0 ||
634 !S_ISDIR(statbuf.st_mode))) {
635 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
636 "'%s' is not a valid directory"),
638 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
645 case ZPOOL_PROP_COMMENT:
646 for (check = strval; *check != '\0'; check++) {
647 if (!isprint(*check)) {
649 dgettext(TEXT_DOMAIN,
650 "comment may only have printable "
652 (void) zfs_error(hdl, EZFS_BADPROP,
657 if (strlen(strval) > ZPROP_MAX_COMMENT) {
658 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
659 "comment must not exceed %d characters"),
661 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
665 case ZPOOL_PROP_READONLY:
667 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
668 "property '%s' can only be set at "
669 "import time"), propname);
670 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
679 nvlist_free(retprops);
684 * Set zpool property : propname=propval.
687 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
689 zfs_cmd_t zc = { 0 };
692 nvlist_t *nvl = NULL;
695 prop_flags_t flags = { 0 };
697 (void) snprintf(errbuf, sizeof (errbuf),
698 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
701 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
702 return (no_memory(zhp->zpool_hdl));
704 if (nvlist_add_string(nvl, propname, propval) != 0) {
706 return (no_memory(zhp->zpool_hdl));
709 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
710 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
711 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
720 * Execute the corresponding ioctl() to set this property.
722 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
724 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
729 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
731 zcmd_free_nvlists(&zc);
735 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
737 (void) zpool_props_refresh(zhp);
743 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
745 libzfs_handle_t *hdl = zhp->zpool_hdl;
747 char buf[ZFS_MAXPROPLEN];
748 nvlist_t *features = NULL;
750 boolean_t firstexpand = (NULL == *plp);
752 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
756 while (*last != NULL)
757 last = &(*last)->pl_next;
760 features = zpool_get_features(zhp);
762 if ((*plp)->pl_all && firstexpand) {
763 for (int i = 0; i < SPA_FEATURES; i++) {
764 zprop_list_t *entry = zfs_alloc(hdl,
765 sizeof (zprop_list_t));
766 entry->pl_prop = ZPROP_INVAL;
767 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
768 spa_feature_table[i].fi_uname);
769 entry->pl_width = strlen(entry->pl_user_prop);
770 entry->pl_all = B_TRUE;
773 last = &entry->pl_next;
777 /* add any unsupported features */
778 for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL);
779 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
784 if (zfeature_is_supported(nvpair_name(nvp)))
787 propname = zfs_asprintf(hdl, "unsupported@%s",
791 * Before adding the property to the list make sure that no
792 * other pool already added the same property.
796 while (entry != NULL) {
797 if (entry->pl_user_prop != NULL &&
798 strcmp(propname, entry->pl_user_prop) == 0) {
802 entry = entry->pl_next;
809 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
810 entry->pl_prop = ZPROP_INVAL;
811 entry->pl_user_prop = propname;
812 entry->pl_width = strlen(entry->pl_user_prop);
813 entry->pl_all = B_TRUE;
816 last = &entry->pl_next;
819 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
824 if (entry->pl_prop != ZPROP_INVAL &&
825 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
826 NULL, B_FALSE) == 0) {
827 if (strlen(buf) > entry->pl_width)
828 entry->pl_width = strlen(buf);
836 * Get the state for the given feature on the given ZFS pool.
839 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
843 boolean_t found = B_FALSE;
844 nvlist_t *features = zpool_get_features(zhp);
846 const char *feature = strchr(propname, '@') + 1;
848 supported = zpool_prop_feature(propname);
849 ASSERT(supported || zpool_prop_unsupported(propname));
852 * Convert from feature name to feature guid. This conversion is
853 * unecessary for unsupported@... properties because they already
860 ret = zfeature_lookup_name(feature, &fid);
862 (void) strlcpy(buf, "-", len);
865 feature = spa_feature_table[fid].fi_guid;
868 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
873 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
876 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
878 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
883 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
885 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
888 (void) strlcpy(buf, "-", len);
897 * Don't start the slice at the default block of 34; many storage
898 * devices will use a stripe width of 128k, so start there instead.
900 #define NEW_START_BLOCK 256
903 * Validate the given pool name, optionally putting an extended error message in
907 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
913 ret = pool_namecheck(pool, &why, &what);
916 * The rules for reserved pool names were extended at a later point.
917 * But we need to support users with existing pools that may now be
918 * invalid. So we only check for this expanded set of names during a
919 * create (or import), and only in userland.
921 if (ret == 0 && !isopen &&
922 (strncmp(pool, "mirror", 6) == 0 ||
923 strncmp(pool, "raidz", 5) == 0 ||
924 strncmp(pool, "spare", 5) == 0 ||
925 strcmp(pool, "log") == 0)) {
928 dgettext(TEXT_DOMAIN, "name is reserved"));
936 case NAME_ERR_TOOLONG:
938 dgettext(TEXT_DOMAIN, "name is too long"));
941 case NAME_ERR_INVALCHAR:
943 dgettext(TEXT_DOMAIN, "invalid character "
944 "'%c' in pool name"), what);
947 case NAME_ERR_NOLETTER:
948 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
949 "name must begin with a letter"));
952 case NAME_ERR_RESERVED:
953 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
954 "name is reserved"));
957 case NAME_ERR_DISKLIKE:
958 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
959 "pool name is reserved"));
962 case NAME_ERR_LEADING_SLASH:
963 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
964 "leading slash in name"));
967 case NAME_ERR_EMPTY_COMPONENT:
968 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
969 "empty component in name"));
972 case NAME_ERR_TRAILING_SLASH:
973 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
974 "trailing slash in name"));
977 case NAME_ERR_MULTIPLE_AT:
978 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
979 "multiple '@' delimiters in name"));
991 * Open a handle to the given pool, even if the pool is currently in the FAULTED
995 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
1001 * Make sure the pool name is valid.
1003 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
1004 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1005 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1010 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1013 zhp->zpool_hdl = hdl;
1014 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1016 if (zpool_refresh_stats(zhp, &missing) != 0) {
1022 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1023 (void) zfs_error_fmt(hdl, EZFS_NOENT,
1024 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1033 * Like the above, but silent on error. Used when iterating over pools (because
1034 * the configuration cache may be out of date).
1037 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1039 zpool_handle_t *zhp;
1042 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1045 zhp->zpool_hdl = hdl;
1046 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1048 if (zpool_refresh_stats(zhp, &missing) != 0) {
1064 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1068 zpool_open(libzfs_handle_t *hdl, const char *pool)
1070 zpool_handle_t *zhp;
1072 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1075 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1076 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1077 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1086 * Close the handle. Simply frees the memory associated with the handle.
1089 zpool_close(zpool_handle_t *zhp)
1091 if (zhp->zpool_config)
1092 nvlist_free(zhp->zpool_config);
1093 if (zhp->zpool_old_config)
1094 nvlist_free(zhp->zpool_old_config);
1095 if (zhp->zpool_props)
1096 nvlist_free(zhp->zpool_props);
1101 * Return the name of the pool.
1104 zpool_get_name(zpool_handle_t *zhp)
1106 return (zhp->zpool_name);
1111 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1114 zpool_get_state(zpool_handle_t *zhp)
1116 return (zhp->zpool_state);
1120 * Create the named pool, using the provided vdev list. It is assumed
1121 * that the consumer has already validated the contents of the nvlist, so we
1122 * don't have to worry about error semantics.
1125 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1126 nvlist_t *props, nvlist_t *fsprops)
1128 zfs_cmd_t zc = { 0 };
1129 nvlist_t *zc_fsprops = NULL;
1130 nvlist_t *zc_props = NULL;
1134 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1135 "cannot create '%s'"), pool);
1137 if (!zpool_name_valid(hdl, B_FALSE, pool))
1138 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1140 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1144 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1146 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1147 SPA_VERSION_1, flags, msg)) == NULL) {
1156 zoned = ((nvlist_lookup_string(fsprops,
1157 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1158 strcmp(zonestr, "on") == 0);
1160 if ((zc_fsprops = zfs_valid_proplist(hdl,
1161 ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
1165 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1168 if (nvlist_add_nvlist(zc_props,
1169 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1174 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1177 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1179 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1181 zcmd_free_nvlists(&zc);
1182 nvlist_free(zc_props);
1183 nvlist_free(zc_fsprops);
1188 * This can happen if the user has specified the same
1189 * device multiple times. We can't reliably detect this
1190 * until we try to add it and see we already have a
1193 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1194 "one or more vdevs refer to the same device"));
1195 return (zfs_error(hdl, EZFS_BADDEV, msg));
1199 * This occurs when one of the devices is below
1200 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1201 * device was the problem device since there's no
1202 * reliable way to determine device size from userland.
1207 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1209 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1210 "one or more devices is less than the "
1211 "minimum size (%s)"), buf);
1213 return (zfs_error(hdl, EZFS_BADDEV, msg));
1216 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1217 "one or more devices is out of space"));
1218 return (zfs_error(hdl, EZFS_BADDEV, msg));
1221 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1222 "cache device must be a disk or disk slice"));
1223 return (zfs_error(hdl, EZFS_BADDEV, msg));
1226 return (zpool_standard_error(hdl, errno, msg));
1231 zcmd_free_nvlists(&zc);
1232 nvlist_free(zc_props);
1233 nvlist_free(zc_fsprops);
1238 * Destroy the given pool. It is up to the caller to ensure that there are no
1239 * datasets left in the pool.
1242 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1244 zfs_cmd_t zc = { 0 };
1245 zfs_handle_t *zfp = NULL;
1246 libzfs_handle_t *hdl = zhp->zpool_hdl;
1249 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1250 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1253 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1254 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1256 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1257 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1258 "cannot destroy '%s'"), zhp->zpool_name);
1260 if (errno == EROFS) {
1261 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1262 "one or more devices is read only"));
1263 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1265 (void) zpool_standard_error(hdl, errno, msg);
1274 remove_mountpoint(zfp);
1282 * Add the given vdevs to the pool. The caller must have already performed the
1283 * necessary verification to ensure that the vdev specification is well-formed.
1286 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1288 zfs_cmd_t zc = { 0 };
1290 libzfs_handle_t *hdl = zhp->zpool_hdl;
1292 nvlist_t **spares, **l2cache;
1293 uint_t nspares, nl2cache;
1295 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1296 "cannot add to '%s'"), zhp->zpool_name);
1298 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1299 SPA_VERSION_SPARES &&
1300 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1301 &spares, &nspares) == 0) {
1302 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1303 "upgraded to add hot spares"));
1304 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1307 if (zpool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1308 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1311 for (s = 0; s < nspares; s++) {
1314 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1315 &path) == 0 && pool_uses_efi(spares[s])) {
1316 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1317 "device '%s' contains an EFI label and "
1318 "cannot be used on root pools."),
1319 zpool_vdev_name(hdl, NULL, spares[s],
1321 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1326 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1327 SPA_VERSION_L2CACHE &&
1328 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1329 &l2cache, &nl2cache) == 0) {
1330 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1331 "upgraded to add cache devices"));
1332 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1335 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1337 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1339 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1343 * This can happen if the user has specified the same
1344 * device multiple times. We can't reliably detect this
1345 * until we try to add it and see we already have a
1348 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1349 "one or more vdevs refer to the same device"));
1350 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1355 * This occurrs when one of the devices is below
1356 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1357 * device was the problem device since there's no
1358 * reliable way to determine device size from userland.
1363 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1365 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1366 "device is less than the minimum "
1369 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1373 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1374 "pool must be upgraded to add these vdevs"));
1375 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1379 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1380 "root pool can not have multiple vdevs"
1381 " or separate logs"));
1382 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1386 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1387 "cache device must be a disk or disk slice"));
1388 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1392 (void) zpool_standard_error(hdl, errno, msg);
1400 zcmd_free_nvlists(&zc);
1406 * Exports the pool from the system. The caller must ensure that there are no
1407 * mounted datasets in the pool.
1410 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1411 const char *log_str)
1413 zfs_cmd_t zc = { 0 };
1416 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1417 "cannot export '%s'"), zhp->zpool_name);
1419 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1420 zc.zc_cookie = force;
1421 zc.zc_guid = hardforce;
1422 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1424 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1427 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1428 "use '-f' to override the following errors:\n"
1429 "'%s' has an active shared spare which could be"
1430 " used by other pools once '%s' is exported."),
1431 zhp->zpool_name, zhp->zpool_name);
1432 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1435 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1444 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1446 return (zpool_export_common(zhp, force, B_FALSE, log_str));
1450 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1452 return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1456 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1459 nvlist_t *nv = NULL;
1465 if (!hdl->libzfs_printerr || config == NULL)
1468 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1469 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1473 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1475 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1477 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1478 strftime(timestr, 128, 0, &t) != 0) {
1480 (void) printf(dgettext(TEXT_DOMAIN,
1481 "Would be able to return %s "
1482 "to its state as of %s.\n"),
1485 (void) printf(dgettext(TEXT_DOMAIN,
1486 "Pool %s returned to its state as of %s.\n"),
1490 (void) printf(dgettext(TEXT_DOMAIN,
1491 "%s approximately %lld "),
1492 dryrun ? "Would discard" : "Discarded",
1494 (void) printf(dgettext(TEXT_DOMAIN,
1495 "minutes of transactions.\n"));
1496 } else if (loss > 0) {
1497 (void) printf(dgettext(TEXT_DOMAIN,
1498 "%s approximately %lld "),
1499 dryrun ? "Would discard" : "Discarded", loss);
1500 (void) printf(dgettext(TEXT_DOMAIN,
1501 "seconds of transactions.\n"));
1507 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1510 nvlist_t *nv = NULL;
1512 uint64_t edata = UINT64_MAX;
1517 if (!hdl->libzfs_printerr)
1521 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1523 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1525 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1526 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1527 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1528 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1531 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1532 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1535 (void) printf(dgettext(TEXT_DOMAIN,
1536 "Recovery is possible, but will result in some data loss.\n"));
1538 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1539 strftime(timestr, 128, 0, &t) != 0) {
1540 (void) printf(dgettext(TEXT_DOMAIN,
1541 "\tReturning the pool to its state as of %s\n"
1542 "\tshould correct the problem. "),
1545 (void) printf(dgettext(TEXT_DOMAIN,
1546 "\tReverting the pool to an earlier state "
1547 "should correct the problem.\n\t"));
1551 (void) printf(dgettext(TEXT_DOMAIN,
1552 "Approximately %lld minutes of data\n"
1553 "\tmust be discarded, irreversibly. "), (loss + 30) / 60);
1554 } else if (loss > 0) {
1555 (void) printf(dgettext(TEXT_DOMAIN,
1556 "Approximately %lld seconds of data\n"
1557 "\tmust be discarded, irreversibly. "), loss);
1559 if (edata != 0 && edata != UINT64_MAX) {
1561 (void) printf(dgettext(TEXT_DOMAIN,
1562 "After rewind, at least\n"
1563 "\tone persistent user-data error will remain. "));
1565 (void) printf(dgettext(TEXT_DOMAIN,
1566 "After rewind, several\n"
1567 "\tpersistent user-data errors will remain. "));
1570 (void) printf(dgettext(TEXT_DOMAIN,
1571 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1572 reason >= 0 ? "clear" : "import", name);
1574 (void) printf(dgettext(TEXT_DOMAIN,
1575 "A scrub of the pool\n"
1576 "\tis strongly recommended after recovery.\n"));
1580 (void) printf(dgettext(TEXT_DOMAIN,
1581 "Destroy and re-create the pool from\n\ta backup source.\n"));
1585 * zpool_import() is a contracted interface. Should be kept the same
1588 * Applications should use zpool_import_props() to import a pool with
1589 * new properties value to be set.
1592 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1595 nvlist_t *props = NULL;
1598 if (altroot != NULL) {
1599 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1600 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1601 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1605 if (nvlist_add_string(props,
1606 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1607 nvlist_add_string(props,
1608 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1610 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1611 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1616 ret = zpool_import_props(hdl, config, newname, props,
1624 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1630 uint64_t is_log = 0;
1632 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1636 (void) printf("\t%*s%s%s\n", indent, "", name,
1637 is_log ? " [log]" : "");
1639 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1640 &child, &children) != 0)
1643 for (c = 0; c < children; c++) {
1644 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
1645 print_vdev_tree(hdl, vname, child[c], indent + 2);
1651 zpool_print_unsup_feat(nvlist_t *config)
1653 nvlist_t *nvinfo, *unsup_feat;
1655 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1657 verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1660 for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1661 nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1664 verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1665 verify(nvpair_value_string(nvp, &desc) == 0);
1667 if (strlen(desc) > 0)
1668 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1670 (void) printf("\t%s\n", nvpair_name(nvp));
1675 * Import the given pool using the known configuration and a list of
1676 * properties to be set. The configuration should have come from
1677 * zpool_find_import(). The 'newname' parameters control whether the pool
1678 * is imported with a different name.
1681 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1682 nvlist_t *props, int flags)
1684 zfs_cmd_t zc = { 0 };
1685 zpool_rewind_policy_t policy;
1686 nvlist_t *nv = NULL;
1687 nvlist_t *nvinfo = NULL;
1688 nvlist_t *missing = NULL;
1695 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1698 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1699 "cannot import pool '%s'"), origname);
1701 if (newname != NULL) {
1702 if (!zpool_name_valid(hdl, B_FALSE, newname))
1703 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1704 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1706 thename = (char *)newname;
1713 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1715 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1718 if ((props = zpool_valid_proplist(hdl, origname,
1719 props, version, flags, errbuf)) == NULL) {
1721 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1727 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1729 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1732 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1736 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1741 zc.zc_cookie = flags;
1742 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1744 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1745 zcmd_free_nvlists(&zc);
1752 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1753 zpool_get_rewind_policy(config, &policy);
1759 * Dry-run failed, but we print out what success
1760 * looks like if we found a best txg
1762 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1763 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1769 if (newname == NULL)
1770 (void) snprintf(desc, sizeof (desc),
1771 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1774 (void) snprintf(desc, sizeof (desc),
1775 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1780 if (nv != NULL && nvlist_lookup_nvlist(nv,
1781 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1782 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1783 (void) printf(dgettext(TEXT_DOMAIN, "This "
1784 "pool uses the following feature(s) not "
1785 "supported by this system:\n"));
1786 zpool_print_unsup_feat(nv);
1787 if (nvlist_exists(nvinfo,
1788 ZPOOL_CONFIG_CAN_RDONLY)) {
1789 (void) printf(dgettext(TEXT_DOMAIN,
1790 "All unsupported features are only "
1791 "required for writing to the pool."
1792 "\nThe pool can be imported using "
1793 "'-o readonly=on'.\n"));
1797 * Unsupported version.
1799 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1803 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1807 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1808 "one or more devices is read only"));
1809 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1813 if (nv && nvlist_lookup_nvlist(nv,
1814 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1815 nvlist_lookup_nvlist(nvinfo,
1816 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1817 (void) printf(dgettext(TEXT_DOMAIN,
1818 "The devices below are missing, use "
1819 "'-m' to import the pool anyway:\n"));
1820 print_vdev_tree(hdl, NULL, missing, 2);
1821 (void) printf("\n");
1823 (void) zpool_standard_error(hdl, error, desc);
1827 (void) zpool_standard_error(hdl, error, desc);
1831 (void) zpool_standard_error(hdl, error, desc);
1832 zpool_explain_recover(hdl,
1833 newname ? origname : thename, -error, nv);
1840 zpool_handle_t *zhp;
1843 * This should never fail, but play it safe anyway.
1845 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1847 else if (zhp != NULL)
1849 if (policy.zrp_request &
1850 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1851 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1852 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1858 zcmd_free_nvlists(&zc);
1868 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1870 zfs_cmd_t zc = { 0 };
1872 libzfs_handle_t *hdl = zhp->zpool_hdl;
1874 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1875 zc.zc_cookie = func;
1877 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
1878 (errno == ENOENT && func != POOL_SCAN_NONE))
1881 if (func == POOL_SCAN_SCRUB) {
1882 (void) snprintf(msg, sizeof (msg),
1883 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1884 } else if (func == POOL_SCAN_NONE) {
1885 (void) snprintf(msg, sizeof (msg),
1886 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1889 assert(!"unexpected result");
1892 if (errno == EBUSY) {
1894 pool_scan_stat_t *ps = NULL;
1897 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1898 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1899 (void) nvlist_lookup_uint64_array(nvroot,
1900 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1901 if (ps && ps->pss_func == POOL_SCAN_SCRUB)
1902 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1904 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1905 } else if (errno == ENOENT) {
1906 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1908 return (zpool_standard_error(hdl, errno, msg));
1913 * This provides a very minimal check whether a given string is likely a
1914 * c#t#d# style string. Users of this are expected to do their own
1915 * verification of the s# part.
1917 #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1]))
1920 * More elaborate version for ones which may start with "/dev/dsk/"
1924 ctd_check_path(char *str) {
1926 * If it starts with a slash, check the last component.
1928 if (str && str[0] == '/') {
1929 char *tmp = strrchr(str, '/');
1932 * If it ends in "/old", check the second-to-last
1933 * component of the string instead.
1935 if (tmp != str && strcmp(tmp, "/old") == 0) {
1936 for (tmp--; *tmp != '/'; tmp--)
1941 return (CTD_CHECK(str));
1945 * Find a vdev that matches the search criteria specified. We use the
1946 * the nvpair name to determine how we should look for the device.
1947 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1948 * spare; but FALSE if its an INUSE spare.
1951 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1952 boolean_t *l2cache, boolean_t *log)
1959 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1961 /* Nothing to look for */
1962 if (search == NULL || pair == NULL)
1965 /* Obtain the key we will use to search */
1966 srchkey = nvpair_name(pair);
1968 switch (nvpair_type(pair)) {
1969 case DATA_TYPE_UINT64:
1970 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1971 uint64_t srchval, theguid;
1973 verify(nvpair_value_uint64(pair, &srchval) == 0);
1974 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1976 if (theguid == srchval)
1981 case DATA_TYPE_STRING: {
1982 char *srchval, *val;
1984 verify(nvpair_value_string(pair, &srchval) == 0);
1985 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1989 * Search for the requested value. Special cases:
1991 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
1992 * "s0" or "s0/old". The "s0" part is hidden from the user,
1993 * but included in the string, so this matches around it.
1994 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1996 * Otherwise, all other searches are simple string compares.
1998 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
1999 ctd_check_path(val)) {
2000 uint64_t wholedisk = 0;
2002 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2005 int slen = strlen(srchval);
2006 int vlen = strlen(val);
2008 if (slen != vlen - 2)
2012 * make_leaf_vdev() should only set
2013 * wholedisk for ZPOOL_CONFIG_PATHs which
2014 * will include "/dev/dsk/", giving plenty of
2015 * room for the indices used next.
2020 * strings identical except trailing "s0"
2022 if (strcmp(&val[vlen - 2], "s0") == 0 &&
2023 strncmp(srchval, val, slen) == 0)
2027 * strings identical except trailing "s0/old"
2029 if (strcmp(&val[vlen - 6], "s0/old") == 0 &&
2030 strcmp(&srchval[slen - 4], "/old") == 0 &&
2031 strncmp(srchval, val, slen - 4) == 0)
2036 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2037 char *type, *idx, *end, *p;
2038 uint64_t id, vdev_id;
2041 * Determine our vdev type, keeping in mind
2042 * that the srchval is composed of a type and
2043 * vdev id pair (i.e. mirror-4).
2045 if ((type = strdup(srchval)) == NULL)
2048 if ((p = strrchr(type, '-')) == NULL) {
2056 * If the types don't match then keep looking.
2058 if (strncmp(val, type, strlen(val)) != 0) {
2063 verify(strncmp(type, VDEV_TYPE_RAIDZ,
2064 strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2065 strncmp(type, VDEV_TYPE_MIRROR,
2066 strlen(VDEV_TYPE_MIRROR)) == 0);
2067 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2071 vdev_id = strtoull(idx, &end, 10);
2078 * Now verify that we have the correct vdev id.
2087 if (strcmp(srchval, val) == 0)
2096 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2097 &child, &children) != 0)
2100 for (c = 0; c < children; c++) {
2101 if ((ret = vdev_to_nvlist_iter(child[c], search,
2102 avail_spare, l2cache, NULL)) != NULL) {
2104 * The 'is_log' value is only set for the toplevel
2105 * vdev, not the leaf vdevs. So we always lookup the
2106 * log device from the root of the vdev tree (where
2107 * 'log' is non-NULL).
2110 nvlist_lookup_uint64(child[c],
2111 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2119 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2120 &child, &children) == 0) {
2121 for (c = 0; c < children; c++) {
2122 if ((ret = vdev_to_nvlist_iter(child[c], search,
2123 avail_spare, l2cache, NULL)) != NULL) {
2124 *avail_spare = B_TRUE;
2130 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2131 &child, &children) == 0) {
2132 for (c = 0; c < children; c++) {
2133 if ((ret = vdev_to_nvlist_iter(child[c], search,
2134 avail_spare, l2cache, NULL)) != NULL) {
2145 * Given a physical path (minus the "/devices" prefix), find the
2149 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2150 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2152 nvlist_t *search, *nvroot, *ret;
2154 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2155 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2157 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2160 *avail_spare = B_FALSE;
2164 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2165 nvlist_free(search);
2171 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2174 zpool_vdev_is_interior(const char *name)
2176 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2177 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2183 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2184 boolean_t *l2cache, boolean_t *log)
2186 char buf[MAXPATHLEN];
2188 nvlist_t *nvroot, *search, *ret;
2191 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2193 guid = strtoull(path, &end, 10);
2194 if (guid != 0 && *end == '\0') {
2195 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2196 } else if (zpool_vdev_is_interior(path)) {
2197 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2198 } else if (path[0] != '/') {
2199 (void) snprintf(buf, sizeof (buf), "%s%s", _PATH_DEV, path);
2200 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
2202 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2205 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2208 *avail_spare = B_FALSE;
2212 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2213 nvlist_free(search);
2219 vdev_online(nvlist_t *nv)
2223 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2224 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2225 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2232 * Helper function for zpool_get_physpaths().
2235 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2236 size_t *bytes_written)
2238 size_t bytes_left, pos, rsz;
2242 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2244 return (EZFS_NODEVICE);
2246 pos = *bytes_written;
2247 bytes_left = physpath_size - pos;
2248 format = (pos == 0) ? "%s" : " %s";
2250 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2251 *bytes_written += rsz;
2253 if (rsz >= bytes_left) {
2254 /* if physpath was not copied properly, clear it */
2255 if (bytes_left != 0) {
2258 return (EZFS_NOSPC);
2264 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2265 size_t *rsz, boolean_t is_spare)
2270 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2271 return (EZFS_INVALCONFIG);
2273 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2275 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2276 * For a spare vdev, we only want to boot from the active
2281 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2284 return (EZFS_INVALCONFIG);
2287 if (vdev_online(nv)) {
2288 if ((ret = vdev_get_one_physpath(nv, physpath,
2289 phypath_size, rsz)) != 0)
2292 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2293 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2294 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2299 if (nvlist_lookup_nvlist_array(nv,
2300 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2301 return (EZFS_INVALCONFIG);
2303 for (i = 0; i < count; i++) {
2304 ret = vdev_get_physpaths(child[i], physpath,
2305 phypath_size, rsz, is_spare);
2306 if (ret == EZFS_NOSPC)
2311 return (EZFS_POOL_INVALARG);
2315 * Get phys_path for a root pool config.
2316 * Return 0 on success; non-zero on failure.
2319 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2322 nvlist_t *vdev_root;
2329 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2331 return (EZFS_INVALCONFIG);
2333 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2334 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2335 &child, &count) != 0)
2336 return (EZFS_INVALCONFIG);
2339 * root pool can not have EFI labeled disks and can only have
2340 * a single top-level vdev.
2342 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
2343 pool_uses_efi(vdev_root))
2344 return (EZFS_POOL_INVALARG);
2346 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2349 /* No online devices */
2351 return (EZFS_NODEVICE);
2357 * Get phys_path for a root pool
2358 * Return 0 on success; non-zero on failure.
2361 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2363 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2368 * If the device has being dynamically expanded then we need to relabel
2369 * the disk to use the new unallocated space.
2372 zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2375 char path[MAXPATHLEN];
2378 int (*_efi_use_whole_disk)(int);
2380 if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2381 "efi_use_whole_disk")) == NULL)
2384 (void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name);
2386 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2387 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2388 "relabel '%s': unable to open device"), name);
2389 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2393 * It's possible that we might encounter an error if the device
2394 * does not have any unallocated space left. If so, we simply
2395 * ignore that error and continue on.
2397 error = _efi_use_whole_disk(fd);
2399 if (error && error != VT_ENOSPC) {
2400 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2401 "relabel '%s': unable to read disk capacity"), name);
2402 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2409 * Bring the specified vdev online. The 'flags' parameter is a set of the
2410 * ZFS_ONLINE_* flags.
2413 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2414 vdev_state_t *newstate)
2416 zfs_cmd_t zc = { 0 };
2419 boolean_t avail_spare, l2cache, islog;
2420 libzfs_handle_t *hdl = zhp->zpool_hdl;
2422 if (flags & ZFS_ONLINE_EXPAND) {
2423 (void) snprintf(msg, sizeof (msg),
2424 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2426 (void) snprintf(msg, sizeof (msg),
2427 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2430 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2431 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2433 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2435 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2438 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2440 if (flags & ZFS_ONLINE_EXPAND ||
2441 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2442 char *pathname = NULL;
2443 uint64_t wholedisk = 0;
2445 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2447 verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
2451 * XXX - L2ARC 1.0 devices can't support expansion.
2454 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2455 "cannot expand cache devices"));
2456 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2460 pathname += strlen(DISK_ROOT) + 1;
2461 (void) zpool_relabel_disk(hdl, pathname);
2465 zc.zc_cookie = VDEV_STATE_ONLINE;
2468 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2469 if (errno == EINVAL) {
2470 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2471 "from this pool into a new one. Use '%s' "
2472 "instead"), "zpool detach");
2473 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2475 return (zpool_standard_error(hdl, errno, msg));
2478 *newstate = zc.zc_cookie;
2483 * Take the specified vdev offline
2486 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2488 zfs_cmd_t zc = { 0 };
2491 boolean_t avail_spare, l2cache;
2492 libzfs_handle_t *hdl = zhp->zpool_hdl;
2494 (void) snprintf(msg, sizeof (msg),
2495 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2497 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2498 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2500 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2502 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2505 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2507 zc.zc_cookie = VDEV_STATE_OFFLINE;
2508 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2510 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2517 * There are no other replicas of this device.
2519 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2523 * The log device has unplayed logs
2525 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2528 return (zpool_standard_error(hdl, errno, msg));
2533 * Mark the given vdev faulted.
2536 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2538 zfs_cmd_t zc = { 0 };
2540 libzfs_handle_t *hdl = zhp->zpool_hdl;
2542 (void) snprintf(msg, sizeof (msg),
2543 dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2545 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2547 zc.zc_cookie = VDEV_STATE_FAULTED;
2550 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2557 * There are no other replicas of this device.
2559 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2562 return (zpool_standard_error(hdl, errno, msg));
2568 * Mark the given vdev degraded.
2571 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2573 zfs_cmd_t zc = { 0 };
2575 libzfs_handle_t *hdl = zhp->zpool_hdl;
2577 (void) snprintf(msg, sizeof (msg),
2578 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
2580 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2582 zc.zc_cookie = VDEV_STATE_DEGRADED;
2585 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2588 return (zpool_standard_error(hdl, errno, msg));
2592 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2596 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2602 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2604 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2607 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2608 children == 2 && child[which] == tgt)
2611 for (c = 0; c < children; c++)
2612 if (is_replacing_spare(child[c], tgt, which))
2620 * Attach new_disk (fully described by nvroot) to old_disk.
2621 * If 'replacing' is specified, the new disk will replace the old one.
2624 zpool_vdev_attach(zpool_handle_t *zhp,
2625 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2627 zfs_cmd_t zc = { 0 };
2631 boolean_t avail_spare, l2cache, islog;
2636 nvlist_t *config_root;
2637 libzfs_handle_t *hdl = zhp->zpool_hdl;
2638 boolean_t rootpool = zpool_is_bootable(zhp);
2641 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2642 "cannot replace %s with %s"), old_disk, new_disk);
2644 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2645 "cannot attach %s to %s"), new_disk, old_disk);
2648 * If this is a root pool, make sure that we're not attaching an
2649 * EFI labeled device.
2651 if (rootpool && pool_uses_efi(nvroot)) {
2652 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2653 "EFI labeled devices are not supported on root pools."));
2654 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2657 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2658 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2660 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2663 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2666 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2668 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2669 zc.zc_cookie = replacing;
2671 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2672 &child, &children) != 0 || children != 1) {
2673 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2674 "new device must be a single disk"));
2675 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2678 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2679 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2681 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2685 * If the target is a hot spare that has been swapped in, we can only
2686 * replace it with another hot spare.
2689 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2690 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2691 NULL) == NULL || !avail_spare) &&
2692 is_replacing_spare(config_root, tgt, 1)) {
2693 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2694 "can only be replaced by another hot spare"));
2696 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2701 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2704 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2706 zcmd_free_nvlists(&zc);
2711 * XXX need a better way to prevent user from
2712 * booting up a half-baked vdev.
2714 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2715 "sure to wait until resilver is done "
2716 "before rebooting.\n"));
2717 (void) fprintf(stderr, "\n");
2718 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "If "
2719 "you boot from pool '%s', you may need to update\n"
2720 "boot code on newly attached disk '%s'.\n\n"
2721 "Assuming you use GPT partitioning and 'da0' is "
2722 "your new boot disk\n"
2723 "you may use the following command:\n\n"
2724 "\tgpart bootcode -b /boot/pmbr -p "
2725 "/boot/gptzfsboot -i 1 da0\n\n"),
2726 zhp->zpool_name, new_disk);
2734 * Can't attach to or replace this type of vdev.
2737 uint64_t version = zpool_get_prop_int(zhp,
2738 ZPOOL_PROP_VERSION, NULL);
2741 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2742 "cannot replace a log with a spare"));
2743 else if (version >= SPA_VERSION_MULTI_REPLACE)
2744 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2745 "already in replacing/spare config; wait "
2746 "for completion or use 'zpool detach'"));
2748 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2749 "cannot replace a replacing device"));
2751 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2752 "can only attach to mirrors and top-level "
2755 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2760 * The new device must be a single disk.
2762 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2763 "new device must be a single disk"));
2764 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2768 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2770 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2775 * The new device is too small.
2777 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2778 "device is too small"));
2779 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2784 * The new device has a different alignment requirement.
2786 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2787 "devices have different sector alignment"));
2788 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2793 * The resulting top-level vdev spec won't fit in the label.
2795 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2799 (void) zpool_standard_error(hdl, errno, msg);
2806 * Detach the specified device.
2809 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2811 zfs_cmd_t zc = { 0 };
2814 boolean_t avail_spare, l2cache;
2815 libzfs_handle_t *hdl = zhp->zpool_hdl;
2817 (void) snprintf(msg, sizeof (msg),
2818 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2820 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2821 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2823 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2826 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2829 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2831 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2833 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2840 * Can't detach from this type of vdev.
2842 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2843 "applicable to mirror and replacing vdevs"));
2844 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2849 * There are no other replicas of this device.
2851 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2855 (void) zpool_standard_error(hdl, errno, msg);
2862 * Find a mirror vdev in the source nvlist.
2864 * The mchild array contains a list of disks in one of the top-level mirrors
2865 * of the source pool. The schild array contains a list of disks that the
2866 * user specified on the command line. We loop over the mchild array to
2867 * see if any entry in the schild array matches.
2869 * If a disk in the mchild array is found in the schild array, we return
2870 * the index of that entry. Otherwise we return -1.
2873 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2874 nvlist_t **schild, uint_t schildren)
2878 for (mc = 0; mc < mchildren; mc++) {
2880 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2881 mchild[mc], B_FALSE);
2883 for (sc = 0; sc < schildren; sc++) {
2884 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2885 schild[sc], B_FALSE);
2886 boolean_t result = (strcmp(mpath, spath) == 0);
2902 * Split a mirror pool. If newroot points to null, then a new nvlist
2903 * is generated and it is the responsibility of the caller to free it.
2906 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2907 nvlist_t *props, splitflags_t flags)
2909 zfs_cmd_t zc = { 0 };
2911 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2912 nvlist_t **varray = NULL, *zc_props = NULL;
2913 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2914 libzfs_handle_t *hdl = zhp->zpool_hdl;
2916 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2919 (void) snprintf(msg, sizeof (msg),
2920 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2922 if (!zpool_name_valid(hdl, B_FALSE, newname))
2923 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2925 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2926 (void) fprintf(stderr, gettext("Internal error: unable to "
2927 "retrieve pool configuration\n"));
2931 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2933 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2936 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2937 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2938 props, vers, flags, msg)) == NULL)
2942 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2944 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2945 "Source pool is missing vdev tree"));
2947 nvlist_free(zc_props);
2951 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2954 if (*newroot == NULL ||
2955 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2956 &newchild, &newchildren) != 0)
2959 for (c = 0; c < children; c++) {
2960 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2962 nvlist_t **mchild, *vdev;
2967 * Unlike cache & spares, slogs are stored in the
2968 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2970 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2972 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2974 if (is_log || is_hole) {
2976 * Create a hole vdev and put it in the config.
2978 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2980 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2981 VDEV_TYPE_HOLE) != 0)
2983 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2988 varray[vcount++] = vdev;
2992 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
2994 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2995 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2996 "Source pool must be composed only of mirrors\n"));
2997 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3001 verify(nvlist_lookup_nvlist_array(child[c],
3002 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3004 /* find or add an entry for this top-level vdev */
3005 if (newchildren > 0 &&
3006 (entry = find_vdev_entry(zhp, mchild, mchildren,
3007 newchild, newchildren)) >= 0) {
3008 /* We found a disk that the user specified. */
3009 vdev = mchild[entry];
3012 /* User didn't specify a disk for this vdev. */
3013 vdev = mchild[mchildren - 1];
3016 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3020 /* did we find every disk the user specified? */
3021 if (found != newchildren) {
3022 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3023 "include at most one disk from each mirror"));
3024 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3028 /* Prepare the nvlist for populating. */
3029 if (*newroot == NULL) {
3030 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3033 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3034 VDEV_TYPE_ROOT) != 0)
3037 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3040 /* Add all the children we found */
3041 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3042 lastlog == 0 ? vcount : lastlog) != 0)
3046 * If we're just doing a dry run, exit now with success.
3049 memory_err = B_FALSE;
3054 /* now build up the config list & call the ioctl */
3055 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3058 if (nvlist_add_nvlist(newconfig,
3059 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3060 nvlist_add_string(newconfig,
3061 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3062 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3066 * The new pool is automatically part of the namespace unless we
3067 * explicitly export it.
3070 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3071 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3072 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3073 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3075 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3078 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3079 retval = zpool_standard_error(hdl, errno, msg);
3084 memory_err = B_FALSE;
3087 if (varray != NULL) {
3090 for (v = 0; v < vcount; v++)
3091 nvlist_free(varray[v]);
3094 zcmd_free_nvlists(&zc);
3096 nvlist_free(zc_props);
3098 nvlist_free(newconfig);
3100 nvlist_free(*newroot);
3108 return (no_memory(hdl));
3114 * Remove the given device. Currently, this is supported only for hot spares
3115 * and level 2 cache devices.
3118 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3120 zfs_cmd_t zc = { 0 };
3123 boolean_t avail_spare, l2cache, islog;
3124 libzfs_handle_t *hdl = zhp->zpool_hdl;
3127 (void) snprintf(msg, sizeof (msg),
3128 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3130 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3131 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3133 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3135 * XXX - this should just go away.
3137 if (!avail_spare && !l2cache && !islog) {
3138 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3139 "only inactive hot spares, cache, top-level, "
3140 "or log devices can be removed"));
3141 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3144 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3145 if (islog && version < SPA_VERSION_HOLES) {
3146 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3147 "pool must be upgrade to support log removal"));
3148 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3151 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3153 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3156 return (zpool_standard_error(hdl, errno, msg));
3160 * Clear the errors for the pool, or the particular device if specified.
3163 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3165 zfs_cmd_t zc = { 0 };
3168 zpool_rewind_policy_t policy;
3169 boolean_t avail_spare, l2cache;
3170 libzfs_handle_t *hdl = zhp->zpool_hdl;
3171 nvlist_t *nvi = NULL;
3175 (void) snprintf(msg, sizeof (msg),
3176 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3179 (void) snprintf(msg, sizeof (msg),
3180 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3183 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3185 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3186 &l2cache, NULL)) == 0)
3187 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3190 * Don't allow error clearing for hot spares. Do allow
3191 * error clearing for l2cache devices.
3194 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3196 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3200 zpool_get_rewind_policy(rewindnvl, &policy);
3201 zc.zc_cookie = policy.zrp_request;
3203 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3206 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3209 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3211 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3212 zcmd_free_nvlists(&zc);
3217 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
3218 errno != EPERM && errno != EACCES)) {
3219 if (policy.zrp_request &
3220 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3221 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3222 zpool_rewind_exclaim(hdl, zc.zc_name,
3223 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
3227 zcmd_free_nvlists(&zc);
3231 zcmd_free_nvlists(&zc);
3232 return (zpool_standard_error(hdl, errno, msg));
3236 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3239 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3241 zfs_cmd_t zc = { 0 };
3243 libzfs_handle_t *hdl = zhp->zpool_hdl;
3245 (void) snprintf(msg, sizeof (msg),
3246 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3249 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3251 zc.zc_cookie = ZPOOL_NO_REWIND;
3253 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3256 return (zpool_standard_error(hdl, errno, msg));
3260 * Change the GUID for a pool.
3263 zpool_reguid(zpool_handle_t *zhp)
3266 libzfs_handle_t *hdl = zhp->zpool_hdl;
3267 zfs_cmd_t zc = { 0 };
3269 (void) snprintf(msg, sizeof (msg),
3270 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3272 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3273 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3276 return (zpool_standard_error(hdl, errno, msg));
3283 zpool_reopen(zpool_handle_t *zhp)
3285 zfs_cmd_t zc = { 0 };
3287 libzfs_handle_t *hdl = zhp->zpool_hdl;
3289 (void) snprintf(msg, sizeof (msg),
3290 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
3293 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3294 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
3296 return (zpool_standard_error(hdl, errno, msg));
3300 * Convert from a devid string to a path.
3303 devid_to_path(char *devid_str)
3308 devid_nmlist_t *list = NULL;
3311 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3314 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3316 devid_str_free(minor);
3322 if ((path = strdup(list[0].devname)) == NULL)
3325 devid_free_nmlist(list);
3331 * Convert from a path to a devid string.
3334 path_to_devid(const char *path)
3341 if ((fd = open(path, O_RDONLY)) < 0)
3346 if (devid_get(fd, &devid) == 0) {
3347 if (devid_get_minor_name(fd, &minor) == 0)
3348 ret = devid_str_encode(devid, minor);
3350 devid_str_free(minor);
3362 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3363 * ignore any failure here, since a common case is for an unprivileged user to
3364 * type 'zpool status', and we'll display the correct information anyway.
3367 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3369 zfs_cmd_t zc = { 0 };
3371 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3372 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3373 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3376 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3380 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3381 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3382 * We also check if this is a whole disk, in which case we strip off the
3383 * trailing 's0' slice name.
3385 * This routine is also responsible for identifying when disks have been
3386 * reconfigured in a new location. The kernel will have opened the device by
3387 * devid, but the path will still refer to the old location. To catch this, we
3388 * first do a path -> devid translation (which is fast for the common case). If
3389 * the devid matches, we're done. If not, we do a reverse devid -> path
3390 * translation and issue the appropriate ioctl() to update the path of the vdev.
3391 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3395 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3406 have_stats = nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3407 (uint64_t **)&vs, &vsc) == 0;
3408 have_path = nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0;
3411 * If the device is not currently present, assume it will not
3412 * come back at the same device path. Display the device by GUID.
3414 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3415 have_path && have_stats && vs->vs_state <= VDEV_STATE_CANT_OPEN) {
3416 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3418 (void) snprintf(buf, sizeof (buf), "%llu",
3419 (u_longlong_t)value);
3421 } else if (have_path) {
3424 * If the device is dead (faulted, offline, etc) then don't
3425 * bother opening it. Otherwise we may be forcing the user to
3426 * open a misbehaving device, which can have undesirable
3429 if ((have_stats == 0 ||
3430 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3432 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3434 * Determine if the current path is correct.
3436 char *newdevid = path_to_devid(path);
3438 if (newdevid == NULL ||
3439 strcmp(devid, newdevid) != 0) {
3442 if ((newpath = devid_to_path(devid)) != NULL) {
3444 * Update the path appropriately.
3446 set_path(zhp, nv, newpath);
3447 if (nvlist_add_string(nv,
3448 ZPOOL_CONFIG_PATH, newpath) == 0)
3449 verify(nvlist_lookup_string(nv,
3457 devid_str_free(newdevid);
3461 if (strncmp(path, "/dev/dsk/", 9) == 0)
3464 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3465 &value) == 0 && value) {
3466 int pathlen = strlen(path);
3467 char *tmp = zfs_strdup(hdl, path);
3470 * If it starts with c#, and ends with "s0", chop
3471 * the "s0" off, or if it ends with "s0/old", remove
3472 * the "s0" from the middle.
3474 if (CTD_CHECK(tmp)) {
3475 if (strcmp(&tmp[pathlen - 2], "s0") == 0) {
3476 tmp[pathlen - 2] = '\0';
3477 } else if (pathlen > 6 &&
3478 strcmp(&tmp[pathlen - 6], "s0/old") == 0) {
3479 (void) strcpy(&tmp[pathlen - 6],
3486 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3487 path += sizeof(_PATH_DEV) - 1;
3490 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3493 * If it's a raidz device, we need to stick in the parity level.
3495 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3496 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3498 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3499 (u_longlong_t)value);
3504 * We identify each top-level vdev by using a <type-id>
3505 * naming convention.
3510 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3512 (void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3518 return (zfs_strdup(hdl, path));
3522 zbookmark_compare(const void *a, const void *b)
3524 return (memcmp(a, b, sizeof (zbookmark_phys_t)));
3528 * Retrieve the persistent error log, uniquify the members, and return to the
3532 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3534 zfs_cmd_t zc = { 0 };
3536 zbookmark_phys_t *zb = NULL;
3540 * Retrieve the raw error list from the kernel. If the number of errors
3541 * has increased, allocate more space and continue until we get the
3544 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3548 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3549 count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL)
3551 zc.zc_nvlist_dst_size = count;
3552 (void) strcpy(zc.zc_name, zhp->zpool_name);
3554 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3556 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3557 if (errno == ENOMEM) {
3560 count = zc.zc_nvlist_dst_size;
3561 dst = zfs_alloc(zhp->zpool_hdl, count *
3562 sizeof (zbookmark_phys_t));
3565 zc.zc_nvlist_dst = (uintptr_t)dst;
3575 * Sort the resulting bookmarks. This is a little confusing due to the
3576 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3577 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3578 * _not_ copied as part of the process. So we point the start of our
3579 * array appropriate and decrement the total number of elements.
3581 zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
3582 zc.zc_nvlist_dst_size;
3583 count -= zc.zc_nvlist_dst_size;
3585 qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_compare);
3587 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3590 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3592 for (i = 0; i < count; i++) {
3595 /* ignoring zb_blkid and zb_level for now */
3596 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3597 zb[i-1].zb_object == zb[i].zb_object)
3600 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3602 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3603 zb[i].zb_objset) != 0) {
3607 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3608 zb[i].zb_object) != 0) {
3612 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3619 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3623 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3624 return (no_memory(zhp->zpool_hdl));
3628 * Upgrade a ZFS pool to the latest on-disk version.
3631 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3633 zfs_cmd_t zc = { 0 };
3634 libzfs_handle_t *hdl = zhp->zpool_hdl;
3636 (void) strcpy(zc.zc_name, zhp->zpool_name);
3637 zc.zc_cookie = new_version;
3639 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3640 return (zpool_standard_error_fmt(hdl, errno,
3641 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3647 zfs_save_arguments(int argc, char **argv, char *string, int len)
3649 (void) strlcpy(string, basename(argv[0]), len);
3650 for (int i = 1; i < argc; i++) {
3651 (void) strlcat(string, " ", len);
3652 (void) strlcat(string, argv[i], len);
3657 zpool_log_history(libzfs_handle_t *hdl, const char *message)
3659 zfs_cmd_t zc = { 0 };
3663 args = fnvlist_alloc();
3664 fnvlist_add_string(args, "message", message);
3665 err = zcmd_write_src_nvlist(hdl, &zc, args);
3667 err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
3669 zcmd_free_nvlists(&zc);
3674 * Perform ioctl to get some command history of a pool.
3676 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3677 * logical offset of the history buffer to start reading from.
3679 * Upon return, 'off' is the next logical offset to read from and
3680 * 'len' is the actual amount of bytes read into 'buf'.
3683 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3685 zfs_cmd_t zc = { 0 };
3686 libzfs_handle_t *hdl = zhp->zpool_hdl;
3688 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3690 zc.zc_history = (uint64_t)(uintptr_t)buf;
3691 zc.zc_history_len = *len;
3692 zc.zc_history_offset = *off;
3694 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3697 return (zfs_error_fmt(hdl, EZFS_PERM,
3698 dgettext(TEXT_DOMAIN,
3699 "cannot show history for pool '%s'"),
3702 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3703 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3704 "'%s'"), zhp->zpool_name));
3706 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3707 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3708 "'%s', pool must be upgraded"), zhp->zpool_name));
3710 return (zpool_standard_error_fmt(hdl, errno,
3711 dgettext(TEXT_DOMAIN,
3712 "cannot get history for '%s'"), zhp->zpool_name));
3716 *len = zc.zc_history_len;
3717 *off = zc.zc_history_offset;
3723 * Process the buffer of nvlists, unpacking and storing each nvlist record
3724 * into 'records'. 'leftover' is set to the number of bytes that weren't
3725 * processed as there wasn't a complete record.
3728 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3729 nvlist_t ***records, uint_t *numrecords)
3735 while (bytes_read > sizeof (reclen)) {
3737 /* get length of packed record (stored as little endian) */
3738 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3739 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3741 if (bytes_read < sizeof (reclen) + reclen)
3745 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3747 bytes_read -= sizeof (reclen) + reclen;
3748 buf += sizeof (reclen) + reclen;
3750 /* add record to nvlist array */
3752 if (ISP2(*numrecords + 1)) {
3753 *records = realloc(*records,
3754 *numrecords * 2 * sizeof (nvlist_t *));
3756 (*records)[*numrecords - 1] = nv;
3759 *leftover = bytes_read;
3763 /* from spa_history.c: spa_history_create_obj() */
3764 #define HIS_BUF_LEN_DEF (128 << 10)
3765 #define HIS_BUF_LEN_MAX (1 << 30)
3768 * Retrieve the command history of a pool.
3771 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3774 uint64_t bufsize = HIS_BUF_LEN_DEF;
3776 nvlist_t **records = NULL;
3777 uint_t numrecords = 0;
3780 if ((buf = malloc(bufsize)) == NULL)
3783 uint64_t bytes_read = bufsize;
3786 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3789 /* if nothing else was read in, we're at EOF, just return */
3790 if (bytes_read == 0)
3793 if ((err = zpool_history_unpack(buf, bytes_read,
3794 &leftover, &records, &numrecords)) != 0)
3799 * If the history block is too big, double the buffer
3800 * size and try again.
3802 if (leftover == bytes_read) {
3807 if ((bufsize >= HIS_BUF_LEN_MAX) ||
3808 ((buf = malloc(bufsize)) == NULL)) {
3819 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3820 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3821 records, numrecords) == 0);
3823 for (i = 0; i < numrecords; i++)
3824 nvlist_free(records[i]);
3831 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3832 char *pathname, size_t len)
3834 zfs_cmd_t zc = { 0 };
3835 boolean_t mounted = B_FALSE;
3836 char *mntpnt = NULL;
3837 char dsname[MAXNAMELEN];
3840 /* special case for the MOS */
3841 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
3845 /* get the dataset's name */
3846 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3848 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3849 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3850 /* just write out a path of two object numbers */
3851 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3855 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3857 /* find out if the dataset is mounted */
3858 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3860 /* get the corrupted object's path */
3861 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3863 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3866 (void) snprintf(pathname, len, "%s%s", mntpnt,
3869 (void) snprintf(pathname, len, "%s:%s",
3870 dsname, zc.zc_value);
3873 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
3880 * Read the EFI label from the config, if a label does not exist then
3881 * pass back the error to the caller. If the caller has passed a non-NULL
3882 * diskaddr argument then we set it to the starting address of the EFI
3886 read_efi_label(nvlist_t *config, diskaddr_t *sb)
3890 char diskname[MAXPATHLEN];
3893 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3896 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
3897 strrchr(path, '/'));
3898 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
3899 struct dk_gpt *vtoc;
3901 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3903 *sb = vtoc->efi_parts[0].p_start;
3912 * determine where a partition starts on a disk in the current
3916 find_start_block(nvlist_t *config)
3920 diskaddr_t sb = MAXOFFSET_T;
3923 if (nvlist_lookup_nvlist_array(config,
3924 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3925 if (nvlist_lookup_uint64(config,
3926 ZPOOL_CONFIG_WHOLE_DISK,
3927 &wholedisk) != 0 || !wholedisk) {
3928 return (MAXOFFSET_T);
3930 if (read_efi_label(config, &sb) < 0)
3935 for (c = 0; c < children; c++) {
3936 sb = find_start_block(child[c]);
3937 if (sb != MAXOFFSET_T) {
3941 return (MAXOFFSET_T);
3946 * Label an individual disk. The name provided is the short name,
3947 * stripped of any leading /dev path.
3950 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name)
3953 char path[MAXPATHLEN];
3954 struct dk_gpt *vtoc;
3956 size_t resv = EFI_MIN_RESV_SIZE;
3957 uint64_t slice_size;
3958 diskaddr_t start_block;
3961 /* prepare an error message just in case */
3962 (void) snprintf(errbuf, sizeof (errbuf),
3963 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3968 if (zpool_is_bootable(zhp)) {
3969 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3970 "EFI labeled devices are not supported on root "
3972 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3975 verify(nvlist_lookup_nvlist(zhp->zpool_config,
3976 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3978 if (zhp->zpool_start_block == 0)
3979 start_block = find_start_block(nvroot);
3981 start_block = zhp->zpool_start_block;
3982 zhp->zpool_start_block = start_block;
3985 start_block = NEW_START_BLOCK;
3988 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
3991 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
3993 * This shouldn't happen. We've long since verified that this
3994 * is a valid device.
3997 dgettext(TEXT_DOMAIN, "unable to open device"));
3998 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
4001 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
4003 * The only way this can fail is if we run out of memory, or we
4004 * were unable to read the disk's capacity
4006 if (errno == ENOMEM)
4007 (void) no_memory(hdl);
4010 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4011 "unable to read disk capacity"), name);
4013 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
4016 slice_size = vtoc->efi_last_u_lba + 1;
4017 slice_size -= EFI_MIN_RESV_SIZE;
4018 if (start_block == MAXOFFSET_T)
4019 start_block = NEW_START_BLOCK;
4020 slice_size -= start_block;
4022 vtoc->efi_parts[0].p_start = start_block;
4023 vtoc->efi_parts[0].p_size = slice_size;
4026 * Why we use V_USR: V_BACKUP confuses users, and is considered
4027 * disposable by some EFI utilities (since EFI doesn't have a backup
4028 * slice). V_UNASSIGNED is supposed to be used only for zero size
4029 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4030 * etc. were all pretty specific. V_USR is as close to reality as we
4031 * can get, in the absence of V_OTHER.
4033 vtoc->efi_parts[0].p_tag = V_USR;
4034 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
4036 vtoc->efi_parts[8].p_start = slice_size + start_block;
4037 vtoc->efi_parts[8].p_size = resv;
4038 vtoc->efi_parts[8].p_tag = V_RESERVED;
4040 if (efi_write(fd, vtoc) != 0) {
4042 * Some block drivers (like pcata) may not support EFI
4043 * GPT labels. Print out a helpful error message dir-
4044 * ecting the user to manually label the disk and give
4050 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4051 "try using fdisk(1M) and then provide a specific slice"));
4052 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4062 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
4068 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
4069 if (strcmp(type, VDEV_TYPE_FILE) == 0 ||
4070 strcmp(type, VDEV_TYPE_HOLE) == 0 ||
4071 strcmp(type, VDEV_TYPE_MISSING) == 0) {
4072 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4073 "vdev type '%s' is not supported"), type);
4074 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
4077 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
4078 &child, &children) == 0) {
4079 for (c = 0; c < children; c++) {
4080 if (!supported_dump_vdev_type(hdl, child[c], errbuf))
4088 * Check if this zvol is allowable for use as a dump device; zero if
4089 * it is, > 0 if it isn't, < 0 if it isn't a zvol.
4091 * Allowable storage configurations include mirrors, all raidz variants, and
4092 * pools with log, cache, and spare devices. Pools which are backed by files or
4093 * have missing/hole vdevs are not suitable.
4096 zvol_check_dump_config(char *arg)
4098 zpool_handle_t *zhp = NULL;
4099 nvlist_t *config, *nvroot;
4103 libzfs_handle_t *hdl;
4105 char poolname[ZPOOL_MAXNAMELEN];
4106 int pathlen = strlen(ZVOL_FULL_DEV_DIR);
4109 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
4113 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4114 "dump is not supported on device '%s'"), arg);
4116 if ((hdl = libzfs_init()) == NULL)
4118 libzfs_print_on_error(hdl, B_TRUE);
4120 volname = arg + pathlen;
4122 /* check the configuration of the pool */
4123 if ((p = strchr(volname, '/')) == NULL) {
4124 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4125 "malformed dataset name"));
4126 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4128 } else if (p - volname >= ZFS_MAXNAMELEN) {
4129 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4130 "dataset name is too long"));
4131 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
4134 (void) strncpy(poolname, volname, p - volname);
4135 poolname[p - volname] = '\0';
4138 if ((zhp = zpool_open(hdl, poolname)) == NULL) {
4139 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4140 "could not open pool '%s'"), poolname);
4141 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4144 config = zpool_get_config(zhp, NULL);
4145 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4147 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4148 "could not obtain vdev configuration for '%s'"), poolname);
4149 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4153 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4154 &top, &toplevels) == 0);
4156 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {