4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27 * Copyright (c) 2018 Datto Inc.
28 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
29 * Copyright (c) 2017, Intel Corporation.
30 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>
31 * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
32 * Copyright (c) 2021, Klara Inc.
44 #include <sys/efi_partition.h>
45 #include <sys/systeminfo.h>
46 #include <sys/zfs_ioctl.h>
47 #include <sys/zfs_sysfs.h>
48 #include <sys/vdev_disk.h>
49 #include <sys/types.h>
54 #include "zfs_namecheck.h"
56 #include "libzfs_impl.h"
57 #include "zfs_comutil.h"
58 #include "zfeature_common.h"
60 static boolean_t zpool_vdev_is_interior(const char *name);
62 typedef struct prop_flags {
63 int create:1; /* Validate property on creation */
64 int import:1; /* Validate property on import */
65 int vdevprop:1; /* Validate property as a VDEV property */
69 * ====================================================================
70 * zpool property functions
71 * ====================================================================
75 zpool_get_all_props(zpool_handle_t *zhp)
77 zfs_cmd_t zc = {"\0"};
78 libzfs_handle_t *hdl = zhp->zpool_hdl;
80 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
82 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
85 while (zfs_ioctl(hdl, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
86 if (errno == ENOMEM) {
87 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
88 zcmd_free_nvlists(&zc);
92 zcmd_free_nvlists(&zc);
97 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
98 zcmd_free_nvlists(&zc);
102 zcmd_free_nvlists(&zc);
108 zpool_props_refresh(zpool_handle_t *zhp)
112 old_props = zhp->zpool_props;
114 if (zpool_get_all_props(zhp) != 0)
117 nvlist_free(old_props);
122 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
128 zprop_source_t source;
130 nvl = zhp->zpool_props;
131 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
132 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
134 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
136 source = ZPROP_SRC_DEFAULT;
137 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
148 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
152 zprop_source_t source;
154 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
156 * zpool_get_all_props() has most likely failed because
157 * the pool is faulted, but if all we need is the top level
158 * vdev's guid then get it from the zhp config nvlist.
160 if ((prop == ZPOOL_PROP_GUID) &&
161 (nvlist_lookup_nvlist(zhp->zpool_config,
162 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
163 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
167 return (zpool_prop_default_numeric(prop));
170 nvl = zhp->zpool_props;
171 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
172 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
174 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
176 source = ZPROP_SRC_DEFAULT;
177 value = zpool_prop_default_numeric(prop);
187 * Map VDEV STATE to printed strings.
190 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
193 case VDEV_STATE_CLOSED:
194 case VDEV_STATE_OFFLINE:
195 return (gettext("OFFLINE"));
196 case VDEV_STATE_REMOVED:
197 return (gettext("REMOVED"));
198 case VDEV_STATE_CANT_OPEN:
199 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
200 return (gettext("FAULTED"));
201 else if (aux == VDEV_AUX_SPLIT_POOL)
202 return (gettext("SPLIT"));
204 return (gettext("UNAVAIL"));
205 case VDEV_STATE_FAULTED:
206 return (gettext("FAULTED"));
207 case VDEV_STATE_DEGRADED:
208 return (gettext("DEGRADED"));
209 case VDEV_STATE_HEALTHY:
210 return (gettext("ONLINE"));
216 return (gettext("UNKNOWN"));
220 * Map POOL STATE to printed strings.
223 zpool_pool_state_to_name(pool_state_t state)
228 case POOL_STATE_ACTIVE:
229 return (gettext("ACTIVE"));
230 case POOL_STATE_EXPORTED:
231 return (gettext("EXPORTED"));
232 case POOL_STATE_DESTROYED:
233 return (gettext("DESTROYED"));
234 case POOL_STATE_SPARE:
235 return (gettext("SPARE"));
236 case POOL_STATE_L2CACHE:
237 return (gettext("L2CACHE"));
238 case POOL_STATE_UNINITIALIZED:
239 return (gettext("UNINITIALIZED"));
240 case POOL_STATE_UNAVAIL:
241 return (gettext("UNAVAIL"));
242 case POOL_STATE_POTENTIALLY_ACTIVE:
243 return (gettext("POTENTIALLY_ACTIVE"));
246 return (gettext("UNKNOWN"));
250 * Given a pool handle, return the pool health string ("ONLINE", "DEGRADED",
254 zpool_get_state_str(zpool_handle_t *zhp)
256 zpool_errata_t errata;
257 zpool_status_t status;
263 status = zpool_get_status(zhp, NULL, &errata);
265 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
266 str = gettext("FAULTED");
267 } else if (status == ZPOOL_STATUS_IO_FAILURE_WAIT ||
268 status == ZPOOL_STATUS_IO_FAILURE_MMP) {
269 str = gettext("SUSPENDED");
271 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
272 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
273 verify(nvlist_lookup_uint64_array(nvroot,
274 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
276 str = zpool_state_to_name(vs->vs_state, vs->vs_aux);
282 * Get a zpool property value for 'prop' and return the value in
283 * a pre-allocated buffer.
286 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf,
287 size_t len, zprop_source_t *srctype, boolean_t literal)
291 zprop_source_t src = ZPROP_SRC_NONE;
293 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
295 case ZPOOL_PROP_NAME:
296 (void) strlcpy(buf, zpool_get_name(zhp), len);
299 case ZPOOL_PROP_HEALTH:
300 (void) strlcpy(buf, zpool_get_state_str(zhp), len);
303 case ZPOOL_PROP_GUID:
304 intval = zpool_get_prop_int(zhp, prop, &src);
305 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
308 case ZPOOL_PROP_ALTROOT:
309 case ZPOOL_PROP_CACHEFILE:
310 case ZPOOL_PROP_COMMENT:
311 case ZPOOL_PROP_COMPATIBILITY:
312 if (zhp->zpool_props != NULL ||
313 zpool_get_all_props(zhp) == 0) {
315 zpool_get_prop_string(zhp, prop, &src),
321 (void) strlcpy(buf, "-", len);
330 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
331 prop != ZPOOL_PROP_NAME)
334 switch (zpool_prop_get_type(prop)) {
335 case PROP_TYPE_STRING:
336 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
340 case PROP_TYPE_NUMBER:
341 intval = zpool_get_prop_int(zhp, prop, &src);
344 case ZPOOL_PROP_SIZE:
345 case ZPOOL_PROP_ALLOCATED:
346 case ZPOOL_PROP_FREE:
347 case ZPOOL_PROP_FREEING:
348 case ZPOOL_PROP_LEAKED:
349 case ZPOOL_PROP_ASHIFT:
351 (void) snprintf(buf, len, "%llu",
352 (u_longlong_t)intval);
354 (void) zfs_nicenum(intval, buf, len);
357 case ZPOOL_PROP_EXPANDSZ:
358 case ZPOOL_PROP_CHECKPOINT:
360 (void) strlcpy(buf, "-", len);
361 } else if (literal) {
362 (void) snprintf(buf, len, "%llu",
363 (u_longlong_t)intval);
365 (void) zfs_nicebytes(intval, buf, len);
369 case ZPOOL_PROP_CAPACITY:
371 (void) snprintf(buf, len, "%llu",
372 (u_longlong_t)intval);
374 (void) snprintf(buf, len, "%llu%%",
375 (u_longlong_t)intval);
379 case ZPOOL_PROP_FRAGMENTATION:
380 if (intval == UINT64_MAX) {
381 (void) strlcpy(buf, "-", len);
382 } else if (literal) {
383 (void) snprintf(buf, len, "%llu",
384 (u_longlong_t)intval);
386 (void) snprintf(buf, len, "%llu%%",
387 (u_longlong_t)intval);
391 case ZPOOL_PROP_DEDUPRATIO:
393 (void) snprintf(buf, len, "%llu.%02llu",
394 (u_longlong_t)(intval / 100),
395 (u_longlong_t)(intval % 100));
397 (void) snprintf(buf, len, "%llu.%02llux",
398 (u_longlong_t)(intval / 100),
399 (u_longlong_t)(intval % 100));
402 case ZPOOL_PROP_HEALTH:
403 (void) strlcpy(buf, zpool_get_state_str(zhp), len);
405 case ZPOOL_PROP_VERSION:
406 if (intval >= SPA_VERSION_FEATURES) {
407 (void) snprintf(buf, len, "-");
412 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
416 case PROP_TYPE_INDEX:
417 intval = zpool_get_prop_int(zhp, prop, &src);
418 if (zpool_prop_index_to_string(prop, intval, &strval)
421 (void) strlcpy(buf, strval, len);
435 * Check if the bootfs name has the same pool name as it is set to.
436 * Assuming bootfs is a valid dataset name.
439 bootfs_name_valid(const char *pool, const char *bootfs)
441 int len = strlen(pool);
442 if (bootfs[0] == '\0')
445 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
448 if (strncmp(pool, bootfs, len) == 0 &&
449 (bootfs[len] == '/' || bootfs[len] == '\0'))
456 * Given an nvlist of zpool properties to be set, validate that they are
457 * correct, and parse any numeric properties (index, boolean, etc) if they are
458 * specified as strings.
461 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
462 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
470 struct stat64 statbuf;
474 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
475 (void) no_memory(hdl);
480 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
481 const char *propname = nvpair_name(elem);
483 if (flags.vdevprop && zpool_prop_vdev(propname)) {
484 vdev_prop_t vprop = vdev_name_to_prop(propname);
486 if (vdev_prop_readonly(vprop)) {
487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
488 "is readonly"), propname);
489 (void) zfs_error(hdl, EZFS_PROPREADONLY,
494 if (zprop_parse_value(hdl, elem, vprop, ZFS_TYPE_VDEV,
495 retprops, &strval, &intval, errbuf) != 0)
499 } else if (flags.vdevprop && vdev_prop_user(propname)) {
500 if (nvlist_add_nvpair(retprops, elem) != 0) {
501 (void) no_memory(hdl);
505 } else if (flags.vdevprop) {
506 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
507 "invalid property: '%s'"), propname);
508 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
512 prop = zpool_name_to_prop(propname);
513 if (prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname)) {
515 char *fname = strchr(propname, '@') + 1;
517 err = zfeature_lookup_name(fname, NULL);
519 ASSERT3U(err, ==, ENOENT);
520 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
521 "feature '%s' unsupported by kernel"),
523 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
527 if (nvpair_type(elem) != DATA_TYPE_STRING) {
528 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
529 "'%s' must be a string"), propname);
530 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
534 (void) nvpair_value_string(elem, &strval);
535 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0 &&
536 strcmp(strval, ZFS_FEATURE_DISABLED) != 0) {
537 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
538 "property '%s' can only be set to "
539 "'enabled' or 'disabled'"), propname);
540 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
545 strcmp(strval, ZFS_FEATURE_DISABLED) == 0) {
546 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
547 "property '%s' can only be set to "
548 "'disabled' at creation time"), propname);
549 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
553 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
554 (void) no_memory(hdl);
561 * Make sure this property is valid and applies to this type.
563 if (prop == ZPOOL_PROP_INVAL) {
564 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
565 "invalid property '%s'"), propname);
566 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
570 if (zpool_prop_readonly(prop)) {
571 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
572 "is readonly"), propname);
573 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
577 if (!flags.create && zpool_prop_setonce(prop)) {
578 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
579 "property '%s' can only be set at "
580 "creation time"), propname);
581 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
585 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
586 &strval, &intval, errbuf) != 0)
590 * Perform additional checking for specific properties.
593 case ZPOOL_PROP_VERSION:
594 if (intval < version ||
595 !SPA_VERSION_IS_SUPPORTED(intval)) {
596 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
597 "property '%s' number %llu is invalid."),
598 propname, (unsigned long long)intval);
599 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
604 case ZPOOL_PROP_ASHIFT:
606 (intval < ASHIFT_MIN || intval > ASHIFT_MAX)) {
607 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
608 "property '%s' number %llu is invalid, "
609 "only values between %" PRId32 " and %"
610 PRId32 " are allowed."),
611 propname, (unsigned long long)intval,
612 ASHIFT_MIN, ASHIFT_MAX);
613 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
618 case ZPOOL_PROP_BOOTFS:
619 if (flags.create || flags.import) {
620 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
621 "property '%s' cannot be set at creation "
622 "or import time"), propname);
623 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
627 if (version < SPA_VERSION_BOOTFS) {
628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
629 "pool must be upgraded to support "
630 "'%s' property"), propname);
631 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
636 * bootfs property value has to be a dataset name and
637 * the dataset has to be in the same pool as it sets to.
639 if (!bootfs_name_valid(poolname, strval)) {
640 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
641 "is an invalid name"), strval);
642 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
646 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
647 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
648 "could not open pool '%s'"), poolname);
649 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
655 case ZPOOL_PROP_ALTROOT:
656 if (!flags.create && !flags.import) {
657 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
658 "property '%s' can only be set during pool "
659 "creation or import"), propname);
660 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
664 if (strval[0] != '/') {
665 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
666 "bad alternate root '%s'"), strval);
667 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
672 case ZPOOL_PROP_CACHEFILE:
673 if (strval[0] == '\0')
676 if (strcmp(strval, "none") == 0)
679 if (strval[0] != '/') {
680 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
681 "property '%s' must be empty, an "
682 "absolute path, or 'none'"), propname);
683 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
687 slash = strrchr(strval, '/');
689 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
690 strcmp(slash, "/..") == 0) {
691 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
692 "'%s' is not a valid file"), strval);
693 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
699 if (strval[0] != '\0' &&
700 (stat64(strval, &statbuf) != 0 ||
701 !S_ISDIR(statbuf.st_mode))) {
702 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
703 "'%s' is not a valid directory"),
705 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
712 case ZPOOL_PROP_COMPATIBILITY:
713 switch (zpool_load_compat(strval, NULL, report, 1024)) {
714 case ZPOOL_COMPATIBILITY_OK:
715 case ZPOOL_COMPATIBILITY_WARNTOKEN:
717 case ZPOOL_COMPATIBILITY_BADFILE:
718 case ZPOOL_COMPATIBILITY_BADTOKEN:
719 case ZPOOL_COMPATIBILITY_NOFILES:
720 zfs_error_aux(hdl, "%s", report);
721 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
726 case ZPOOL_PROP_COMMENT:
727 for (check = strval; *check != '\0'; check++) {
728 if (!isprint(*check)) {
730 dgettext(TEXT_DOMAIN,
731 "comment may only have printable "
733 (void) zfs_error(hdl, EZFS_BADPROP,
738 if (strlen(strval) > ZPROP_MAX_COMMENT) {
739 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
740 "comment must not exceed %d characters"),
742 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
746 case ZPOOL_PROP_READONLY:
748 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
749 "property '%s' can only be set at "
750 "import time"), propname);
751 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
755 case ZPOOL_PROP_MULTIHOST:
756 if (get_system_hostid() == 0) {
757 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
758 "requires a non-zero system hostid"));
759 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
763 case ZPOOL_PROP_DEDUPDITTO:
764 printf("Note: property '%s' no longer has "
765 "any effect\n", propname);
775 nvlist_free(retprops);
780 * Set zpool property : propname=propval.
783 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
785 zfs_cmd_t zc = {"\0"};
788 nvlist_t *nvl = NULL;
791 prop_flags_t flags = { 0 };
793 (void) snprintf(errbuf, sizeof (errbuf),
794 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
797 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
798 return (no_memory(zhp->zpool_hdl));
800 if (nvlist_add_string(nvl, propname, propval) != 0) {
802 return (no_memory(zhp->zpool_hdl));
805 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
806 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
807 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
816 * Execute the corresponding ioctl() to set this property.
818 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
820 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
825 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
827 zcmd_free_nvlists(&zc);
831 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
833 (void) zpool_props_refresh(zhp);
839 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp,
840 zfs_type_t type, boolean_t literal)
842 libzfs_handle_t *hdl = zhp->zpool_hdl;
844 char buf[ZFS_MAXPROPLEN];
845 nvlist_t *features = NULL;
848 boolean_t firstexpand = (NULL == *plp);
851 if (zprop_expand_list(hdl, plp, type) != 0)
854 if (type == ZFS_TYPE_VDEV)
858 while (*last != NULL)
859 last = &(*last)->pl_next;
862 features = zpool_get_features(zhp);
864 if ((*plp)->pl_all && firstexpand) {
865 for (i = 0; i < SPA_FEATURES; i++) {
866 zprop_list_t *entry = zfs_alloc(hdl,
867 sizeof (zprop_list_t));
868 entry->pl_prop = ZPROP_INVAL;
869 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
870 spa_feature_table[i].fi_uname);
871 entry->pl_width = strlen(entry->pl_user_prop);
872 entry->pl_all = B_TRUE;
875 last = &entry->pl_next;
879 /* add any unsupported features */
880 for (nvp = nvlist_next_nvpair(features, NULL);
881 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
886 if (zfeature_is_supported(nvpair_name(nvp)))
889 propname = zfs_asprintf(hdl, "unsupported@%s",
893 * Before adding the property to the list make sure that no
894 * other pool already added the same property.
898 while (entry != NULL) {
899 if (entry->pl_user_prop != NULL &&
900 strcmp(propname, entry->pl_user_prop) == 0) {
904 entry = entry->pl_next;
911 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
912 entry->pl_prop = ZPROP_INVAL;
913 entry->pl_user_prop = propname;
914 entry->pl_width = strlen(entry->pl_user_prop);
915 entry->pl_all = B_TRUE;
918 last = &entry->pl_next;
921 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
922 if (entry->pl_fixed && !literal)
925 if (entry->pl_prop != ZPROP_INVAL &&
926 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
927 NULL, literal) == 0) {
928 if (strlen(buf) > entry->pl_width)
929 entry->pl_width = strlen(buf);
937 vdev_expand_proplist(zpool_handle_t *zhp, const char *vdevname,
941 char buf[ZFS_MAXPROPLEN];
944 nvpair_t *elem = NULL;
945 nvlist_t *vprops = NULL;
946 nvlist_t *propval = NULL;
947 const char *propname;
951 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
955 if (zpool_get_vdev_prop(zhp, vdevname, entry->pl_prop,
956 entry->pl_user_prop, buf, sizeof (buf), NULL,
958 if (strlen(buf) > entry->pl_width)
959 entry->pl_width = strlen(buf);
961 if (entry->pl_prop == VDEV_PROP_NAME &&
962 strlen(vdevname) > entry->pl_width)
963 entry->pl_width = strlen(vdevname);
966 /* Handle the all properties case */
968 if (*last != NULL && (*last)->pl_all == B_TRUE) {
969 while (*last != NULL)
970 last = &(*last)->pl_next;
972 err = zpool_get_all_vdev_props(zhp, vdevname, &vprops);
976 while ((elem = nvlist_next_nvpair(vprops, elem)) != NULL) {
977 propname = nvpair_name(elem);
979 /* Skip properties that are not user defined */
980 if ((prop = vdev_name_to_prop(propname)) !=
984 if (nvpair_value_nvlist(elem, &propval) != 0)
987 verify(nvlist_lookup_string(propval, ZPROP_VALUE,
990 if ((entry = zfs_alloc(zhp->zpool_hdl,
991 sizeof (zprop_list_t))) == NULL)
994 entry->pl_prop = prop;
995 entry->pl_user_prop = zfs_strdup(zhp->zpool_hdl,
997 entry->pl_width = strlen(strval);
998 entry->pl_all = B_TRUE;
1000 last = &entry->pl_next;
1008 * Get the state for the given feature on the given ZFS pool.
1011 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
1015 boolean_t found = B_FALSE;
1016 nvlist_t *features = zpool_get_features(zhp);
1017 boolean_t supported;
1018 const char *feature = strchr(propname, '@') + 1;
1020 supported = zpool_prop_feature(propname);
1021 ASSERT(supported || zpool_prop_unsupported(propname));
1024 * Convert from feature name to feature guid. This conversion is
1025 * unnecessary for unsupported@... properties because they already
1032 ret = zfeature_lookup_name(feature, &fid);
1034 (void) strlcpy(buf, "-", len);
1037 feature = spa_feature_table[fid].fi_guid;
1040 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
1045 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
1048 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
1050 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
1054 if (refcount == 0) {
1055 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
1057 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
1060 (void) strlcpy(buf, "-", len);
1069 * Validate the given pool name, optionally putting an extended error message in
1073 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
1075 namecheck_err_t why;
1079 ret = pool_namecheck(pool, &why, &what);
1082 * The rules for reserved pool names were extended at a later point.
1083 * But we need to support users with existing pools that may now be
1084 * invalid. So we only check for this expanded set of names during a
1085 * create (or import), and only in userland.
1087 if (ret == 0 && !isopen &&
1088 (strncmp(pool, "mirror", 6) == 0 ||
1089 strncmp(pool, "raidz", 5) == 0 ||
1090 strncmp(pool, "draid", 5) == 0 ||
1091 strncmp(pool, "spare", 5) == 0 ||
1092 strcmp(pool, "log") == 0)) {
1095 dgettext(TEXT_DOMAIN, "name is reserved"));
1103 case NAME_ERR_TOOLONG:
1105 dgettext(TEXT_DOMAIN, "name is too long"));
1108 case NAME_ERR_INVALCHAR:
1110 dgettext(TEXT_DOMAIN, "invalid character "
1111 "'%c' in pool name"), what);
1114 case NAME_ERR_NOLETTER:
1115 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1116 "name must begin with a letter"));
1119 case NAME_ERR_RESERVED:
1120 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1121 "name is reserved"));
1124 case NAME_ERR_DISKLIKE:
1125 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1126 "pool name is reserved"));
1129 case NAME_ERR_LEADING_SLASH:
1130 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1131 "leading slash in name"));
1134 case NAME_ERR_EMPTY_COMPONENT:
1135 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1136 "empty component in name"));
1139 case NAME_ERR_TRAILING_SLASH:
1140 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1141 "trailing slash in name"));
1144 case NAME_ERR_MULTIPLE_DELIMITERS:
1145 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1146 "multiple '@' and/or '#' delimiters in "
1150 case NAME_ERR_NO_AT:
1151 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1152 "permission set is missing '@'"));
1156 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1157 "(%d) not defined"), why);
1168 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1172 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
1174 zpool_handle_t *zhp;
1178 * Make sure the pool name is valid.
1180 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
1181 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1182 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1187 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1190 zhp->zpool_hdl = hdl;
1191 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1193 if (zpool_refresh_stats(zhp, &missing) != 0) {
1199 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1200 (void) zfs_error_fmt(hdl, EZFS_NOENT,
1201 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1210 * Like the above, but silent on error. Used when iterating over pools (because
1211 * the configuration cache may be out of date).
1214 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1216 zpool_handle_t *zhp;
1219 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1222 zhp->zpool_hdl = hdl;
1223 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1225 if (zpool_refresh_stats(zhp, &missing) != 0) {
1241 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1245 zpool_open(libzfs_handle_t *hdl, const char *pool)
1247 zpool_handle_t *zhp;
1249 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1252 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1253 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1254 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1263 * Close the handle. Simply frees the memory associated with the handle.
1266 zpool_close(zpool_handle_t *zhp)
1268 nvlist_free(zhp->zpool_config);
1269 nvlist_free(zhp->zpool_old_config);
1270 nvlist_free(zhp->zpool_props);
1275 * Return the name of the pool.
1278 zpool_get_name(zpool_handle_t *zhp)
1280 return (zhp->zpool_name);
1285 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1288 zpool_get_state(zpool_handle_t *zhp)
1290 return (zhp->zpool_state);
1294 * Check if vdev list contains a special vdev
1297 zpool_has_special_vdev(nvlist_t *nvroot)
1302 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, &child,
1304 for (uint_t c = 0; c < children; c++) {
1307 if (nvlist_lookup_string(child[c],
1308 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias) == 0 &&
1309 strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0) {
1318 * Check if vdev list contains a dRAID vdev
1321 zpool_has_draid_vdev(nvlist_t *nvroot)
1326 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1327 &child, &children) == 0) {
1328 for (uint_t c = 0; c < children; c++) {
1331 if (nvlist_lookup_string(child[c],
1332 ZPOOL_CONFIG_TYPE, &type) == 0 &&
1333 strcmp(type, VDEV_TYPE_DRAID) == 0) {
1342 * Output a dRAID top-level vdev name in to the provided buffer.
1345 zpool_draid_name(char *name, int len, uint64_t data, uint64_t parity,
1346 uint64_t spares, uint64_t children)
1348 snprintf(name, len, "%s%llu:%llud:%lluc:%llus",
1349 VDEV_TYPE_DRAID, (u_longlong_t)parity, (u_longlong_t)data,
1350 (u_longlong_t)children, (u_longlong_t)spares);
1356 * Return B_TRUE if the provided name is a dRAID spare name.
1359 zpool_is_draid_spare(const char *name)
1361 uint64_t spare_id, parity, vdev_id;
1363 if (sscanf(name, VDEV_TYPE_DRAID "%llu-%llu-%llu",
1364 (u_longlong_t *)&parity, (u_longlong_t *)&vdev_id,
1365 (u_longlong_t *)&spare_id) == 3) {
1373 * Create the named pool, using the provided vdev list. It is assumed
1374 * that the consumer has already validated the contents of the nvlist, so we
1375 * don't have to worry about error semantics.
1378 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1379 nvlist_t *props, nvlist_t *fsprops)
1381 zfs_cmd_t zc = {"\0"};
1382 nvlist_t *zc_fsprops = NULL;
1383 nvlist_t *zc_props = NULL;
1384 nvlist_t *hidden_args = NULL;
1385 uint8_t *wkeydata = NULL;
1390 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1391 "cannot create '%s'"), pool);
1393 if (!zpool_name_valid(hdl, B_FALSE, pool))
1394 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1396 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1400 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1402 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1403 SPA_VERSION_1, flags, msg)) == NULL) {
1412 zoned = ((nvlist_lookup_string(fsprops,
1413 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1414 strcmp(zonestr, "on") == 0);
1416 if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
1417 fsprops, zoned, NULL, NULL, B_TRUE, msg)) == NULL) {
1421 if (nvlist_exists(zc_fsprops,
1422 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS)) &&
1423 !zpool_has_special_vdev(nvroot)) {
1424 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1425 "%s property requires a special vdev"),
1426 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS));
1427 (void) zfs_error(hdl, EZFS_BADPROP, msg);
1432 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1435 if (zfs_crypto_create(hdl, NULL, zc_fsprops, props, B_TRUE,
1436 &wkeydata, &wkeylen) != 0) {
1437 zfs_error(hdl, EZFS_CRYPTOFAILED, msg);
1440 if (nvlist_add_nvlist(zc_props,
1441 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1444 if (wkeydata != NULL) {
1445 if (nvlist_alloc(&hidden_args, NV_UNIQUE_NAME, 0) != 0)
1448 if (nvlist_add_uint8_array(hidden_args, "wkeydata",
1449 wkeydata, wkeylen) != 0)
1452 if (nvlist_add_nvlist(zc_props, ZPOOL_HIDDEN_ARGS,
1458 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1461 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1463 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1465 zcmd_free_nvlists(&zc);
1466 nvlist_free(zc_props);
1467 nvlist_free(zc_fsprops);
1468 nvlist_free(hidden_args);
1469 if (wkeydata != NULL)
1475 * This can happen if the user has specified the same
1476 * device multiple times. We can't reliably detect this
1477 * until we try to add it and see we already have a
1478 * label. This can also happen under if the device is
1479 * part of an active md or lvm device.
1481 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1482 "one or more vdevs refer to the same device, or "
1483 "one of\nthe devices is part of an active md or "
1485 return (zfs_error(hdl, EZFS_BADDEV, msg));
1489 * This happens if the record size is smaller or larger
1490 * than the allowed size range, or not a power of 2.
1492 * NOTE: although zfs_valid_proplist is called earlier,
1493 * this case may have slipped through since the
1494 * pool does not exist yet and it is therefore
1495 * impossible to read properties e.g. max blocksize
1498 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1499 "record size invalid"));
1500 return (zfs_error(hdl, EZFS_BADPROP, msg));
1504 * This occurs when one of the devices is below
1505 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1506 * device was the problem device since there's no
1507 * reliable way to determine device size from userland.
1512 zfs_nicebytes(SPA_MINDEVSIZE, buf,
1515 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1516 "one or more devices is less than the "
1517 "minimum size (%s)"), buf);
1519 return (zfs_error(hdl, EZFS_BADDEV, msg));
1522 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1523 "one or more devices is out of space"));
1524 return (zfs_error(hdl, EZFS_BADDEV, msg));
1527 if (zpool_has_draid_vdev(nvroot) &&
1528 zfeature_lookup_name("draid", NULL) != 0) {
1529 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1530 "dRAID vdevs are unsupported by the "
1532 return (zfs_error(hdl, EZFS_BADDEV, msg));
1534 return (zpool_standard_error(hdl, errno, msg));
1538 return (zpool_standard_error(hdl, errno, msg));
1543 zcmd_free_nvlists(&zc);
1544 nvlist_free(zc_props);
1545 nvlist_free(zc_fsprops);
1546 nvlist_free(hidden_args);
1547 if (wkeydata != NULL)
1553 * Destroy the given pool. It is up to the caller to ensure that there are no
1554 * datasets left in the pool.
1557 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1559 zfs_cmd_t zc = {"\0"};
1560 zfs_handle_t *zfp = NULL;
1561 libzfs_handle_t *hdl = zhp->zpool_hdl;
1564 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1565 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1568 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1569 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1571 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1572 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1573 "cannot destroy '%s'"), zhp->zpool_name);
1575 if (errno == EROFS) {
1576 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1577 "one or more devices is read only"));
1578 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1580 (void) zpool_standard_error(hdl, errno, msg);
1589 remove_mountpoint(zfp);
1597 * Create a checkpoint in the given pool.
1600 zpool_checkpoint(zpool_handle_t *zhp)
1602 libzfs_handle_t *hdl = zhp->zpool_hdl;
1606 error = lzc_pool_checkpoint(zhp->zpool_name);
1608 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1609 "cannot checkpoint '%s'"), zhp->zpool_name);
1610 (void) zpool_standard_error(hdl, error, msg);
1618 * Discard the checkpoint from the given pool.
1621 zpool_discard_checkpoint(zpool_handle_t *zhp)
1623 libzfs_handle_t *hdl = zhp->zpool_hdl;
1627 error = lzc_pool_checkpoint_discard(zhp->zpool_name);
1629 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1630 "cannot discard checkpoint in '%s'"), zhp->zpool_name);
1631 (void) zpool_standard_error(hdl, error, msg);
1639 * Add the given vdevs to the pool. The caller must have already performed the
1640 * necessary verification to ensure that the vdev specification is well-formed.
1643 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1645 zfs_cmd_t zc = {"\0"};
1647 libzfs_handle_t *hdl = zhp->zpool_hdl;
1649 nvlist_t **spares, **l2cache;
1650 uint_t nspares, nl2cache;
1652 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1653 "cannot add to '%s'"), zhp->zpool_name);
1655 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1656 SPA_VERSION_SPARES &&
1657 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1658 &spares, &nspares) == 0) {
1659 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1660 "upgraded to add hot spares"));
1661 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1664 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1665 SPA_VERSION_L2CACHE &&
1666 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1667 &l2cache, &nl2cache) == 0) {
1668 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1669 "upgraded to add cache devices"));
1670 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1673 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1675 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1677 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1681 * This can happen if the user has specified the same
1682 * device multiple times. We can't reliably detect this
1683 * until we try to add it and see we already have a
1686 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1687 "one or more vdevs refer to the same device"));
1688 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1693 if (zpool_has_draid_vdev(nvroot) &&
1694 zfeature_lookup_name("draid", NULL) != 0) {
1695 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1696 "dRAID vdevs are unsupported by the "
1699 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1700 "invalid config; a pool with removing/"
1701 "removed vdevs does not support adding "
1702 "raidz or dRAID vdevs"));
1705 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1710 * This occurs when one of the devices is below
1711 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1712 * device was the problem device since there's no
1713 * reliable way to determine device size from userland.
1718 zfs_nicebytes(SPA_MINDEVSIZE, buf,
1721 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1722 "device is less than the minimum "
1725 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1729 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1730 "pool must be upgraded to add these vdevs"));
1731 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1735 (void) zpool_standard_error(hdl, errno, msg);
1743 zcmd_free_nvlists(&zc);
1749 * Exports the pool from the system. The caller must ensure that there are no
1750 * mounted datasets in the pool.
1753 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1754 const char *log_str)
1756 zfs_cmd_t zc = {"\0"};
1758 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1759 zc.zc_cookie = force;
1760 zc.zc_guid = hardforce;
1761 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1763 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1766 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1767 "use '-f' to override the following errors:\n"
1768 "'%s' has an active shared spare which could be"
1769 " used by other pools once '%s' is exported."),
1770 zhp->zpool_name, zhp->zpool_name);
1771 return (zfs_error_fmt(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1772 dgettext(TEXT_DOMAIN, "cannot export '%s'"),
1775 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1776 dgettext(TEXT_DOMAIN, "cannot export '%s'"),
1785 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1787 return (zpool_export_common(zhp, force, B_FALSE, log_str));
1791 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1793 return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1797 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1800 nvlist_t *nv = NULL;
1806 if (!hdl->libzfs_printerr || config == NULL)
1809 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1810 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1814 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1816 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1818 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1819 strftime(timestr, 128, "%c", &t) != 0) {
1821 (void) printf(dgettext(TEXT_DOMAIN,
1822 "Would be able to return %s "
1823 "to its state as of %s.\n"),
1826 (void) printf(dgettext(TEXT_DOMAIN,
1827 "Pool %s returned to its state as of %s.\n"),
1831 (void) printf(dgettext(TEXT_DOMAIN,
1832 "%s approximately %lld "),
1833 dryrun ? "Would discard" : "Discarded",
1834 ((longlong_t)loss + 30) / 60);
1835 (void) printf(dgettext(TEXT_DOMAIN,
1836 "minutes of transactions.\n"));
1837 } else if (loss > 0) {
1838 (void) printf(dgettext(TEXT_DOMAIN,
1839 "%s approximately %lld "),
1840 dryrun ? "Would discard" : "Discarded",
1842 (void) printf(dgettext(TEXT_DOMAIN,
1843 "seconds of transactions.\n"));
1849 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1852 nvlist_t *nv = NULL;
1854 uint64_t edata = UINT64_MAX;
1859 if (!hdl->libzfs_printerr)
1863 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1865 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1867 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1868 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1869 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1870 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1873 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1874 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1877 (void) printf(dgettext(TEXT_DOMAIN,
1878 "Recovery is possible, but will result in some data loss.\n"));
1880 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1881 strftime(timestr, 128, "%c", &t) != 0) {
1882 (void) printf(dgettext(TEXT_DOMAIN,
1883 "\tReturning the pool to its state as of %s\n"
1884 "\tshould correct the problem. "),
1887 (void) printf(dgettext(TEXT_DOMAIN,
1888 "\tReverting the pool to an earlier state "
1889 "should correct the problem.\n\t"));
1893 (void) printf(dgettext(TEXT_DOMAIN,
1894 "Approximately %lld minutes of data\n"
1895 "\tmust be discarded, irreversibly. "),
1896 ((longlong_t)loss + 30) / 60);
1897 } else if (loss > 0) {
1898 (void) printf(dgettext(TEXT_DOMAIN,
1899 "Approximately %lld seconds of data\n"
1900 "\tmust be discarded, irreversibly. "),
1903 if (edata != 0 && edata != UINT64_MAX) {
1905 (void) printf(dgettext(TEXT_DOMAIN,
1906 "After rewind, at least\n"
1907 "\tone persistent user-data error will remain. "));
1909 (void) printf(dgettext(TEXT_DOMAIN,
1910 "After rewind, several\n"
1911 "\tpersistent user-data errors will remain. "));
1914 (void) printf(dgettext(TEXT_DOMAIN,
1915 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1916 reason >= 0 ? "clear" : "import", name);
1918 (void) printf(dgettext(TEXT_DOMAIN,
1919 "A scrub of the pool\n"
1920 "\tis strongly recommended after recovery.\n"));
1924 (void) printf(dgettext(TEXT_DOMAIN,
1925 "Destroy and re-create the pool from\n\ta backup source.\n"));
1929 * zpool_import() is a contracted interface. Should be kept the same
1932 * Applications should use zpool_import_props() to import a pool with
1933 * new properties value to be set.
1936 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1939 nvlist_t *props = NULL;
1942 if (altroot != NULL) {
1943 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1944 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1945 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1949 if (nvlist_add_string(props,
1950 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1951 nvlist_add_string(props,
1952 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1954 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1955 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1960 ret = zpool_import_props(hdl, config, newname, props,
1967 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1973 uint64_t is_log = 0;
1975 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1979 (void) printf("\t%*s%s%s\n", indent, "", name,
1980 is_log ? " [log]" : "");
1982 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1983 &child, &children) != 0)
1986 for (c = 0; c < children; c++) {
1987 vname = zpool_vdev_name(hdl, NULL, child[c], VDEV_NAME_TYPE_ID);
1988 print_vdev_tree(hdl, vname, child[c], indent + 2);
1994 zpool_print_unsup_feat(nvlist_t *config)
1996 nvlist_t *nvinfo, *unsup_feat;
1999 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
2001 verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
2004 for (nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
2005 nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
2008 verify(nvpair_type(nvp) == DATA_TYPE_STRING);
2009 verify(nvpair_value_string(nvp, &desc) == 0);
2011 if (strlen(desc) > 0)
2012 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
2014 (void) printf("\t%s\n", nvpair_name(nvp));
2019 * Import the given pool using the known configuration and a list of
2020 * properties to be set. The configuration should have come from
2021 * zpool_find_import(). The 'newname' parameters control whether the pool
2022 * is imported with a different name.
2025 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
2026 nvlist_t *props, int flags)
2028 zfs_cmd_t zc = {"\0"};
2029 zpool_load_policy_t policy;
2030 nvlist_t *nv = NULL;
2031 nvlist_t *nvinfo = NULL;
2032 nvlist_t *missing = NULL;
2039 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
2042 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2043 "cannot import pool '%s'"), origname);
2045 if (newname != NULL) {
2046 if (!zpool_name_valid(hdl, B_FALSE, newname))
2047 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
2048 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
2050 thename = (char *)newname;
2055 if (props != NULL) {
2057 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2059 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
2062 if ((props = zpool_valid_proplist(hdl, origname,
2063 props, version, flags, errbuf)) == NULL)
2065 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
2072 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
2074 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
2077 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
2078 zcmd_free_nvlists(&zc);
2081 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
2082 zcmd_free_nvlists(&zc);
2086 zc.zc_cookie = flags;
2087 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
2089 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
2090 zcmd_free_nvlists(&zc);
2097 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
2099 zcmd_free_nvlists(&zc);
2101 zpool_get_load_policy(config, &policy);
2108 * Dry-run failed, but we print out what success
2109 * looks like if we found a best txg
2111 if (policy.zlp_rewind & ZPOOL_TRY_REWIND) {
2112 zpool_rewind_exclaim(hdl, newname ? origname : thename,
2118 if (newname == NULL)
2119 (void) snprintf(desc, sizeof (desc),
2120 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
2123 (void) snprintf(desc, sizeof (desc),
2124 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
2129 if (nv != NULL && nvlist_lookup_nvlist(nv,
2130 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
2131 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
2132 (void) printf(dgettext(TEXT_DOMAIN, "This "
2133 "pool uses the following feature(s) not "
2134 "supported by this system:\n"));
2135 zpool_print_unsup_feat(nv);
2136 if (nvlist_exists(nvinfo,
2137 ZPOOL_CONFIG_CAN_RDONLY)) {
2138 (void) printf(dgettext(TEXT_DOMAIN,
2139 "All unsupported features are only "
2140 "required for writing to the pool."
2141 "\nThe pool can be imported using "
2142 "'-o readonly=on'.\n"));
2146 * Unsupported version.
2148 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
2152 if (nv != NULL && nvlist_lookup_nvlist(nv,
2153 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0) {
2154 char *hostname = "<unknown>";
2155 uint64_t hostid = 0;
2156 mmp_state_t mmp_state;
2158 mmp_state = fnvlist_lookup_uint64(nvinfo,
2159 ZPOOL_CONFIG_MMP_STATE);
2161 if (nvlist_exists(nvinfo,
2162 ZPOOL_CONFIG_MMP_HOSTNAME))
2163 hostname = fnvlist_lookup_string(nvinfo,
2164 ZPOOL_CONFIG_MMP_HOSTNAME);
2166 if (nvlist_exists(nvinfo,
2167 ZPOOL_CONFIG_MMP_HOSTID))
2168 hostid = fnvlist_lookup_uint64(nvinfo,
2169 ZPOOL_CONFIG_MMP_HOSTID);
2171 if (mmp_state == MMP_STATE_ACTIVE) {
2172 (void) snprintf(aux, sizeof (aux),
2173 dgettext(TEXT_DOMAIN, "pool is imp"
2174 "orted on host '%s' (hostid=%lx).\n"
2175 "Export the pool on the other "
2176 "system, then run 'zpool import'."),
2177 hostname, (unsigned long) hostid);
2178 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
2179 (void) snprintf(aux, sizeof (aux),
2180 dgettext(TEXT_DOMAIN, "pool has "
2181 "the multihost property on and "
2182 "the\nsystem's hostid is not set. "
2183 "Set a unique system hostid with "
2184 "the zgenhostid(8) command.\n"));
2187 (void) zfs_error_aux(hdl, "%s", aux);
2189 (void) zfs_error(hdl, EZFS_ACTIVE_POOL, desc);
2193 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
2197 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2198 "one or more devices is read only"));
2199 (void) zfs_error(hdl, EZFS_BADDEV, desc);
2203 if (nv && nvlist_lookup_nvlist(nv,
2204 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
2205 nvlist_lookup_nvlist(nvinfo,
2206 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
2207 (void) printf(dgettext(TEXT_DOMAIN,
2208 "The devices below are missing or "
2209 "corrupted, use '-m' to import the pool "
2211 print_vdev_tree(hdl, NULL, missing, 2);
2212 (void) printf("\n");
2214 (void) zpool_standard_error(hdl, error, desc);
2218 (void) zpool_standard_error(hdl, error, desc);
2222 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2223 "one or more devices are already in use\n"));
2224 (void) zfs_error(hdl, EZFS_BADDEV, desc);
2227 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2228 "new name of at least one dataset is longer than "
2229 "the maximum allowable length"));
2230 (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
2233 (void) zpool_standard_error(hdl, error, desc);
2234 zpool_explain_recover(hdl,
2235 newname ? origname : thename, -error, nv);
2242 zpool_handle_t *zhp;
2245 * This should never fail, but play it safe anyway.
2247 if (zpool_open_silent(hdl, thename, &zhp) != 0)
2249 else if (zhp != NULL)
2251 if (policy.zlp_rewind &
2252 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2253 zpool_rewind_exclaim(hdl, newname ? origname : thename,
2254 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0), nv);
2264 * Translate vdev names to guids. If a vdev_path is determined to be
2265 * unsuitable then a vd_errlist is allocated and the vdev path and errno
2269 zpool_translate_vdev_guids(zpool_handle_t *zhp, nvlist_t *vds,
2270 nvlist_t *vdev_guids, nvlist_t *guids_to_paths, nvlist_t **vd_errlist)
2272 nvlist_t *errlist = NULL;
2275 for (nvpair_t *elem = nvlist_next_nvpair(vds, NULL); elem != NULL;
2276 elem = nvlist_next_nvpair(vds, elem)) {
2277 boolean_t spare, cache;
2279 char *vd_path = nvpair_name(elem);
2280 nvlist_t *tgt = zpool_find_vdev(zhp, vd_path, &spare, &cache,
2283 if ((tgt == NULL) || cache || spare) {
2284 if (errlist == NULL) {
2285 errlist = fnvlist_alloc();
2289 uint64_t err = (tgt == NULL) ? EZFS_NODEVICE :
2290 (spare ? EZFS_ISSPARE : EZFS_ISL2CACHE);
2291 fnvlist_add_int64(errlist, vd_path, err);
2295 uint64_t guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
2296 fnvlist_add_uint64(vdev_guids, vd_path, guid);
2298 char msg[MAXNAMELEN];
2299 (void) snprintf(msg, sizeof (msg), "%llu", (u_longlong_t)guid);
2300 fnvlist_add_string(guids_to_paths, msg, vd_path);
2304 verify(errlist != NULL);
2305 if (vd_errlist != NULL)
2306 *vd_errlist = errlist;
2308 fnvlist_free(errlist);
2315 xlate_init_err(int err)
2319 return (EZFS_NODEVICE);
2322 return (EZFS_BADDEV);
2324 return (EZFS_INITIALIZING);
2326 return (EZFS_NO_INITIALIZE);
2332 * Begin, suspend, or cancel the initialization (initializing of all free
2333 * blocks) for the given vdevs in the given pool.
2336 zpool_initialize_impl(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2337 nvlist_t *vds, boolean_t wait)
2341 nvlist_t *vdev_guids = fnvlist_alloc();
2342 nvlist_t *guids_to_paths = fnvlist_alloc();
2343 nvlist_t *vd_errlist = NULL;
2347 err = zpool_translate_vdev_guids(zhp, vds, vdev_guids,
2348 guids_to_paths, &vd_errlist);
2351 verify(vd_errlist != NULL);
2355 err = lzc_initialize(zhp->zpool_name, cmd_type,
2356 vdev_guids, &errlist);
2359 if (errlist != NULL) {
2360 vd_errlist = fnvlist_lookup_nvlist(errlist,
2361 ZPOOL_INITIALIZE_VDEVS);
2364 (void) zpool_standard_error(zhp->zpool_hdl, err,
2365 dgettext(TEXT_DOMAIN, "operation failed"));
2370 for (elem = nvlist_next_nvpair(vdev_guids, NULL); elem != NULL;
2371 elem = nvlist_next_nvpair(vdev_guids, elem)) {
2373 uint64_t guid = fnvpair_value_uint64(elem);
2375 err = lzc_wait_tag(zhp->zpool_name,
2376 ZPOOL_WAIT_INITIALIZE, guid, NULL);
2378 (void) zpool_standard_error_fmt(zhp->zpool_hdl,
2379 err, dgettext(TEXT_DOMAIN, "error "
2380 "waiting for '%s' to initialize"),
2390 for (elem = nvlist_next_nvpair(vd_errlist, NULL); elem != NULL;
2391 elem = nvlist_next_nvpair(vd_errlist, elem)) {
2392 int64_t vd_error = xlate_init_err(fnvpair_value_int64(elem));
2395 if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem),
2397 path = nvpair_name(elem);
2399 (void) zfs_error_fmt(zhp->zpool_hdl, vd_error,
2400 "cannot initialize '%s'", path);
2404 fnvlist_free(vdev_guids);
2405 fnvlist_free(guids_to_paths);
2407 if (vd_errlist != NULL)
2408 fnvlist_free(vd_errlist);
2410 return (err == 0 ? 0 : -1);
2414 zpool_initialize(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2417 return (zpool_initialize_impl(zhp, cmd_type, vds, B_FALSE));
2421 zpool_initialize_wait(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2424 return (zpool_initialize_impl(zhp, cmd_type, vds, B_TRUE));
2428 xlate_trim_err(int err)
2432 return (EZFS_NODEVICE);
2435 return (EZFS_BADDEV);
2437 return (EZFS_TRIMMING);
2439 return (EZFS_NO_TRIM);
2441 return (EZFS_TRIM_NOTSUP);
2447 zpool_trim_wait(zpool_handle_t *zhp, nvlist_t *vdev_guids)
2452 for (elem = nvlist_next_nvpair(vdev_guids, NULL); elem != NULL;
2453 elem = nvlist_next_nvpair(vdev_guids, elem)) {
2455 uint64_t guid = fnvpair_value_uint64(elem);
2457 err = lzc_wait_tag(zhp->zpool_name,
2458 ZPOOL_WAIT_TRIM, guid, NULL);
2460 (void) zpool_standard_error_fmt(zhp->zpool_hdl,
2461 err, dgettext(TEXT_DOMAIN, "error "
2462 "waiting to trim '%s'"), nvpair_name(elem));
2471 * Check errlist and report any errors, omitting ones which should be
2472 * suppressed. Returns B_TRUE if any errors were reported.
2475 check_trim_errs(zpool_handle_t *zhp, trimflags_t *trim_flags,
2476 nvlist_t *guids_to_paths, nvlist_t *vds, nvlist_t *errlist)
2479 boolean_t reported_errs = B_FALSE;
2481 int num_suppressed_errs = 0;
2483 for (elem = nvlist_next_nvpair(vds, NULL);
2484 elem != NULL; elem = nvlist_next_nvpair(vds, elem)) {
2488 for (elem = nvlist_next_nvpair(errlist, NULL);
2489 elem != NULL; elem = nvlist_next_nvpair(errlist, elem)) {
2490 int64_t vd_error = xlate_trim_err(fnvpair_value_int64(elem));
2494 * If only the pool was specified, and it was not a secure
2495 * trim then suppress warnings for individual vdevs which
2496 * do not support trimming.
2498 if (vd_error == EZFS_TRIM_NOTSUP &&
2499 trim_flags->fullpool &&
2500 !trim_flags->secure) {
2501 num_suppressed_errs++;
2505 reported_errs = B_TRUE;
2506 if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem),
2508 path = nvpair_name(elem);
2510 (void) zfs_error_fmt(zhp->zpool_hdl, vd_error,
2511 "cannot trim '%s'", path);
2514 if (num_suppressed_errs == num_vds) {
2515 (void) zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
2516 "no devices in pool support trim operations"));
2517 (void) (zfs_error(zhp->zpool_hdl, EZFS_TRIM_NOTSUP,
2518 dgettext(TEXT_DOMAIN, "cannot trim")));
2519 reported_errs = B_TRUE;
2522 return (reported_errs);
2526 * Begin, suspend, or cancel the TRIM (discarding of all free blocks) for
2527 * the given vdevs in the given pool.
2530 zpool_trim(zpool_handle_t *zhp, pool_trim_func_t cmd_type, nvlist_t *vds,
2531 trimflags_t *trim_flags)
2536 nvlist_t *vdev_guids = fnvlist_alloc();
2537 nvlist_t *guids_to_paths = fnvlist_alloc();
2538 nvlist_t *errlist = NULL;
2540 err = zpool_translate_vdev_guids(zhp, vds, vdev_guids,
2541 guids_to_paths, &errlist);
2543 check_trim_errs(zhp, trim_flags, guids_to_paths, vds, errlist);
2548 err = lzc_trim(zhp->zpool_name, cmd_type, trim_flags->rate,
2549 trim_flags->secure, vdev_guids, &errlist);
2551 nvlist_t *vd_errlist;
2552 if (errlist != NULL && nvlist_lookup_nvlist(errlist,
2553 ZPOOL_TRIM_VDEVS, &vd_errlist) == 0) {
2554 if (check_trim_errs(zhp, trim_flags, guids_to_paths,
2562 (void) snprintf(msg, sizeof (msg),
2563 dgettext(TEXT_DOMAIN, "operation failed"));
2564 zpool_standard_error(zhp->zpool_hdl, err, msg);
2571 if (trim_flags->wait)
2572 retval = zpool_trim_wait(zhp, vdev_guids);
2575 if (errlist != NULL)
2576 fnvlist_free(errlist);
2577 fnvlist_free(vdev_guids);
2578 fnvlist_free(guids_to_paths);
2586 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd)
2588 zfs_cmd_t zc = {"\0"};
2591 libzfs_handle_t *hdl = zhp->zpool_hdl;
2593 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2594 zc.zc_cookie = func;
2597 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0)
2602 /* ECANCELED on a scrub means we resumed a paused scrub */
2603 if (err == ECANCELED && func == POOL_SCAN_SCRUB &&
2604 cmd == POOL_SCRUB_NORMAL)
2607 if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL)
2610 if (func == POOL_SCAN_SCRUB) {
2611 if (cmd == POOL_SCRUB_PAUSE) {
2612 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2613 "cannot pause scrubbing %s"), zc.zc_name);
2615 assert(cmd == POOL_SCRUB_NORMAL);
2616 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2617 "cannot scrub %s"), zc.zc_name);
2619 } else if (func == POOL_SCAN_RESILVER) {
2620 assert(cmd == POOL_SCRUB_NORMAL);
2621 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2622 "cannot restart resilver on %s"), zc.zc_name);
2623 } else if (func == POOL_SCAN_NONE) {
2624 (void) snprintf(msg, sizeof (msg),
2625 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
2628 assert(!"unexpected result");
2633 pool_scan_stat_t *ps = NULL;
2636 verify(nvlist_lookup_nvlist(zhp->zpool_config,
2637 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2638 (void) nvlist_lookup_uint64_array(nvroot,
2639 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
2640 if (ps && ps->pss_func == POOL_SCAN_SCRUB &&
2641 ps->pss_state == DSS_SCANNING) {
2642 if (cmd == POOL_SCRUB_PAUSE)
2643 return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg));
2645 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
2647 return (zfs_error(hdl, EZFS_RESILVERING, msg));
2649 } else if (err == ENOENT) {
2650 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
2651 } else if (err == ENOTSUP && func == POOL_SCAN_RESILVER) {
2652 return (zfs_error(hdl, EZFS_NO_RESILVER_DEFER, msg));
2654 return (zpool_standard_error(hdl, err, msg));
2659 * Find a vdev that matches the search criteria specified. We use the
2660 * the nvpair name to determine how we should look for the device.
2661 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2662 * spare; but FALSE if its an INUSE spare.
2665 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
2666 boolean_t *l2cache, boolean_t *log)
2673 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
2675 /* Nothing to look for */
2676 if (search == NULL || pair == NULL)
2679 /* Obtain the key we will use to search */
2680 srchkey = nvpair_name(pair);
2682 switch (nvpair_type(pair)) {
2683 case DATA_TYPE_UINT64:
2684 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
2685 uint64_t srchval, theguid;
2687 verify(nvpair_value_uint64(pair, &srchval) == 0);
2688 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2690 if (theguid == srchval)
2695 case DATA_TYPE_STRING: {
2696 char *srchval, *val;
2698 verify(nvpair_value_string(pair, &srchval) == 0);
2699 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2703 * Search for the requested value. Special cases:
2705 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2706 * "-part1", or "p1". The suffix is hidden from the user,
2707 * but included in the string, so this matches around it.
2708 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2709 * is used to check all possible expanded paths.
2710 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2712 * Otherwise, all other searches are simple string compares.
2714 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) {
2715 uint64_t wholedisk = 0;
2717 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2719 if (zfs_strcmp_pathname(srchval, val, wholedisk) == 0)
2722 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2723 char *type, *idx, *end, *p;
2724 uint64_t id, vdev_id;
2727 * Determine our vdev type, keeping in mind
2728 * that the srchval is composed of a type and
2729 * vdev id pair (i.e. mirror-4).
2731 if ((type = strdup(srchval)) == NULL)
2734 if ((p = strrchr(type, '-')) == NULL) {
2742 * If the types don't match then keep looking.
2744 if (strncmp(val, type, strlen(val)) != 0) {
2749 verify(zpool_vdev_is_interior(type));
2750 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2754 vdev_id = strtoull(idx, &end, 10);
2757 * If we are looking for a raidz and a parity is
2758 * specified, make sure it matches.
2760 int rzlen = strlen(VDEV_TYPE_RAIDZ);
2761 assert(rzlen == strlen(VDEV_TYPE_DRAID));
2762 int typlen = strlen(type);
2763 if ((strncmp(type, VDEV_TYPE_RAIDZ, rzlen) == 0 ||
2764 strncmp(type, VDEV_TYPE_DRAID, rzlen) == 0) &&
2766 uint64_t vdev_parity;
2767 int parity = *(type + rzlen) - '0';
2769 if (parity <= 0 || parity > 3 ||
2770 (typlen - rzlen) != 1) {
2772 * Nonsense parity specified, can
2778 verify(nvlist_lookup_uint64(nv,
2779 ZPOOL_CONFIG_NPARITY, &vdev_parity) == 0);
2780 if ((int)vdev_parity != parity) {
2791 * Now verify that we have the correct vdev id.
2800 if (strcmp(srchval, val) == 0)
2809 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2810 &child, &children) != 0)
2813 for (c = 0; c < children; c++) {
2814 if ((ret = vdev_to_nvlist_iter(child[c], search,
2815 avail_spare, l2cache, NULL)) != NULL) {
2817 * The 'is_log' value is only set for the toplevel
2818 * vdev, not the leaf vdevs. So we always lookup the
2819 * log device from the root of the vdev tree (where
2820 * 'log' is non-NULL).
2823 nvlist_lookup_uint64(child[c],
2824 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2832 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2833 &child, &children) == 0) {
2834 for (c = 0; c < children; c++) {
2835 if ((ret = vdev_to_nvlist_iter(child[c], search,
2836 avail_spare, l2cache, NULL)) != NULL) {
2837 *avail_spare = B_TRUE;
2843 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2844 &child, &children) == 0) {
2845 for (c = 0; c < children; c++) {
2846 if ((ret = vdev_to_nvlist_iter(child[c], search,
2847 avail_spare, l2cache, NULL)) != NULL) {
2858 * Given a physical path or guid, find the associated vdev.
2861 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2862 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2864 nvlist_t *search, *nvroot, *ret;
2868 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2870 guid = strtoull(ppath, &end, 0);
2871 if (guid != 0 && *end == '\0') {
2872 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2874 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH,
2878 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2881 *avail_spare = B_FALSE;
2885 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2886 nvlist_free(search);
2892 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2895 zpool_vdev_is_interior(const char *name)
2897 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2898 strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 ||
2900 VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 ||
2901 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2904 if (strncmp(name, VDEV_TYPE_DRAID, strlen(VDEV_TYPE_DRAID)) == 0 &&
2905 !zpool_is_draid_spare(name))
2912 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2913 boolean_t *l2cache, boolean_t *log)
2916 nvlist_t *nvroot, *search, *ret;
2919 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2921 guid = strtoull(path, &end, 0);
2922 if (guid != 0 && *end == '\0') {
2923 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2924 } else if (zpool_vdev_is_interior(path)) {
2925 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2927 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2930 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2933 *avail_spare = B_FALSE;
2937 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2938 nvlist_free(search);
2944 vdev_is_online(nvlist_t *nv)
2948 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2949 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2950 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2957 * Helper function for zpool_get_physpaths().
2960 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2961 size_t *bytes_written)
2963 size_t bytes_left, pos, rsz;
2967 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2969 return (EZFS_NODEVICE);
2971 pos = *bytes_written;
2972 bytes_left = physpath_size - pos;
2973 format = (pos == 0) ? "%s" : " %s";
2975 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2976 *bytes_written += rsz;
2978 if (rsz >= bytes_left) {
2979 /* if physpath was not copied properly, clear it */
2980 if (bytes_left != 0) {
2983 return (EZFS_NOSPC);
2989 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2990 size_t *rsz, boolean_t is_spare)
2995 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2996 return (EZFS_INVALCONFIG);
2998 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
3000 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
3001 * For a spare vdev, we only want to boot from the active
3006 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
3009 return (EZFS_INVALCONFIG);
3012 if (vdev_is_online(nv)) {
3013 if ((ret = vdev_get_one_physpath(nv, physpath,
3014 phypath_size, rsz)) != 0)
3017 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
3018 strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
3019 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
3020 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
3025 if (nvlist_lookup_nvlist_array(nv,
3026 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
3027 return (EZFS_INVALCONFIG);
3029 for (i = 0; i < count; i++) {
3030 ret = vdev_get_physpaths(child[i], physpath,
3031 phypath_size, rsz, is_spare);
3032 if (ret == EZFS_NOSPC)
3037 return (EZFS_POOL_INVALARG);
3041 * Get phys_path for a root pool config.
3042 * Return 0 on success; non-zero on failure.
3045 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
3048 nvlist_t *vdev_root;
3055 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3057 return (EZFS_INVALCONFIG);
3059 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
3060 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
3061 &child, &count) != 0)
3062 return (EZFS_INVALCONFIG);
3065 * root pool can only have a single top-level vdev.
3067 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
3068 return (EZFS_POOL_INVALARG);
3070 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
3073 /* No online devices */
3075 return (EZFS_NODEVICE);
3081 * Get phys_path for a root pool
3082 * Return 0 on success; non-zero on failure.
3085 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
3087 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
3092 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
3094 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
3095 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
3099 zpool_vdev_path_to_guid_impl(zpool_handle_t *zhp, const char *path,
3100 boolean_t *is_spare, boolean_t *is_l2cache, boolean_t *is_log)
3103 boolean_t spare = B_FALSE, l2cache = B_FALSE, log = B_FALSE;
3106 if ((tgt = zpool_find_vdev(zhp, path, &spare, &l2cache,
3110 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &guid) == 0);
3111 if (is_spare != NULL)
3113 if (is_l2cache != NULL)
3114 *is_l2cache = l2cache;
3121 /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
3123 zpool_vdev_path_to_guid(zpool_handle_t *zhp, const char *path)
3125 return (zpool_vdev_path_to_guid_impl(zhp, path, NULL, NULL, NULL));
3129 * Bring the specified vdev online. The 'flags' parameter is a set of the
3130 * ZFS_ONLINE_* flags.
3133 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
3134 vdev_state_t *newstate)
3136 zfs_cmd_t zc = {"\0"};
3140 boolean_t avail_spare, l2cache, islog;
3141 libzfs_handle_t *hdl = zhp->zpool_hdl;
3144 if (flags & ZFS_ONLINE_EXPAND) {
3145 (void) snprintf(msg, sizeof (msg),
3146 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
3148 (void) snprintf(msg, sizeof (msg),
3149 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
3152 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3153 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3155 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3157 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3160 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3162 if ((flags & ZFS_ONLINE_EXPAND ||
3163 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) &&
3164 nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) {
3165 uint64_t wholedisk = 0;
3167 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
3171 * XXX - L2ARC 1.0 devices can't support expansion.
3174 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3175 "cannot expand cache devices"));
3176 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
3180 const char *fullpath = path;
3181 char buf[MAXPATHLEN];
3183 if (path[0] != '/') {
3184 error = zfs_resolve_shortname(path, buf,
3187 return (zfs_error(hdl, EZFS_NODEVICE,
3193 error = zpool_relabel_disk(hdl, fullpath, msg);
3199 zc.zc_cookie = VDEV_STATE_ONLINE;
3202 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
3203 if (errno == EINVAL) {
3204 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
3205 "from this pool into a new one. Use '%s' "
3206 "instead"), "zpool detach");
3207 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
3209 return (zpool_standard_error(hdl, errno, msg));
3212 *newstate = zc.zc_cookie;
3217 * Take the specified vdev offline
3220 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
3222 zfs_cmd_t zc = {"\0"};
3225 boolean_t avail_spare, l2cache;
3226 libzfs_handle_t *hdl = zhp->zpool_hdl;
3228 (void) snprintf(msg, sizeof (msg),
3229 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
3231 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3232 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3234 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3236 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3239 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3241 zc.zc_cookie = VDEV_STATE_OFFLINE;
3242 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
3244 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3251 * There are no other replicas of this device.
3253 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
3257 * The log device has unplayed logs
3259 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
3262 return (zpool_standard_error(hdl, errno, msg));
3267 * Mark the given vdev faulted.
3270 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
3272 zfs_cmd_t zc = {"\0"};
3274 libzfs_handle_t *hdl = zhp->zpool_hdl;
3276 (void) snprintf(msg, sizeof (msg),
3277 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
3279 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3281 zc.zc_cookie = VDEV_STATE_FAULTED;
3284 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3291 * There are no other replicas of this device.
3293 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
3296 return (zpool_standard_error(hdl, errno, msg));
3302 * Mark the given vdev degraded.
3305 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
3307 zfs_cmd_t zc = {"\0"};
3309 libzfs_handle_t *hdl = zhp->zpool_hdl;
3311 (void) snprintf(msg, sizeof (msg),
3312 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid);
3314 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3316 zc.zc_cookie = VDEV_STATE_DEGRADED;
3319 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3322 return (zpool_standard_error(hdl, errno, msg));
3326 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
3330 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
3336 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
3338 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
3341 if ((strcmp(type, VDEV_TYPE_SPARE) == 0 ||
3342 strcmp(type, VDEV_TYPE_DRAID_SPARE) == 0) &&
3343 children == 2 && child[which] == tgt)
3346 for (c = 0; c < children; c++)
3347 if (is_replacing_spare(child[c], tgt, which))
3355 * Attach new_disk (fully described by nvroot) to old_disk.
3356 * If 'replacing' is specified, the new disk will replace the old one.
3359 zpool_vdev_attach(zpool_handle_t *zhp, const char *old_disk,
3360 const char *new_disk, nvlist_t *nvroot, int replacing, boolean_t rebuild)
3362 zfs_cmd_t zc = {"\0"};
3366 boolean_t avail_spare, l2cache, islog;
3371 nvlist_t *config_root;
3372 libzfs_handle_t *hdl = zhp->zpool_hdl;
3375 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3376 "cannot replace %s with %s"), old_disk, new_disk);
3378 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3379 "cannot attach %s to %s"), new_disk, old_disk);
3381 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3382 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
3384 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3387 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3390 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
3392 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3393 zc.zc_cookie = replacing;
3394 zc.zc_simple = rebuild;
3397 zfeature_lookup_guid("org.openzfs:device_rebuild", NULL) != 0) {
3398 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3399 "the loaded zfs module doesn't support device rebuilds"));
3400 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
3403 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3404 &child, &children) != 0 || children != 1) {
3405 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3406 "new device must be a single disk"));
3407 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
3410 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
3411 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
3413 if ((newname = zpool_vdev_name(NULL, NULL, child[0], 0)) == NULL)
3417 * If the target is a hot spare that has been swapped in, we can only
3418 * replace it with another hot spare.
3421 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
3422 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
3423 NULL) == NULL || !avail_spare) &&
3424 is_replacing_spare(config_root, tgt, 1)) {
3425 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3426 "can only be replaced by another hot spare"));
3428 return (zfs_error(hdl, EZFS_BADTARGET, msg));
3433 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
3436 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
3438 zcmd_free_nvlists(&zc);
3446 * Can't attach to or replace this type of vdev.
3449 uint64_t version = zpool_get_prop_int(zhp,
3450 ZPOOL_PROP_VERSION, NULL);
3453 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3454 "cannot replace a log with a spare"));
3455 } else if (rebuild) {
3456 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3457 "only mirror and dRAID vdevs support "
3458 "sequential reconstruction"));
3459 } else if (zpool_is_draid_spare(new_disk)) {
3460 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3461 "dRAID spares can only replace child "
3462 "devices in their parent's dRAID vdev"));
3463 } else if (version >= SPA_VERSION_MULTI_REPLACE) {
3464 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3465 "already in replacing/spare config; wait "
3466 "for completion or use 'zpool detach'"));
3468 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3469 "cannot replace a replacing device"));
3472 char status[64] = {0};
3473 zpool_prop_get_feature(zhp,
3474 "feature@device_rebuild", status, 63);
3476 strncmp(status, ZFS_FEATURE_DISABLED, 64) == 0) {
3477 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3478 "device_rebuild feature must be enabled "
3479 "in order to use sequential "
3482 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3483 "can only attach to mirrors and top-level "
3487 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
3492 * The new device must be a single disk.
3494 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3495 "new device must be a single disk"));
3496 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3500 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy, "
3501 "or device removal is in progress"),
3503 (void) zfs_error(hdl, EZFS_BADDEV, msg);
3508 * The new device is too small.
3510 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3511 "device is too small"));
3512 (void) zfs_error(hdl, EZFS_BADDEV, msg);
3517 * The new device has a different optimal sector size.
3519 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3520 "new device has a different optimal sector size; use the "
3521 "option '-o ashift=N' to override the optimal size"));
3522 (void) zfs_error(hdl, EZFS_BADDEV, msg);
3527 * The resulting top-level vdev spec won't fit in the label.
3529 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
3533 (void) zpool_standard_error(hdl, errno, msg);
3540 * Detach the specified device.
3543 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
3545 zfs_cmd_t zc = {"\0"};
3548 boolean_t avail_spare, l2cache;
3549 libzfs_handle_t *hdl = zhp->zpool_hdl;
3551 (void) snprintf(msg, sizeof (msg),
3552 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
3554 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3555 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3557 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3560 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3563 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
3565 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3567 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
3574 * Can't detach from this type of vdev.
3576 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
3577 "applicable to mirror and replacing vdevs"));
3578 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
3583 * There are no other replicas of this device.
3585 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
3589 (void) zpool_standard_error(hdl, errno, msg);
3596 * Find a mirror vdev in the source nvlist.
3598 * The mchild array contains a list of disks in one of the top-level mirrors
3599 * of the source pool. The schild array contains a list of disks that the
3600 * user specified on the command line. We loop over the mchild array to
3601 * see if any entry in the schild array matches.
3603 * If a disk in the mchild array is found in the schild array, we return
3604 * the index of that entry. Otherwise we return -1.
3607 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
3608 nvlist_t **schild, uint_t schildren)
3612 for (mc = 0; mc < mchildren; mc++) {
3614 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3617 for (sc = 0; sc < schildren; sc++) {
3618 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3620 boolean_t result = (strcmp(mpath, spath) == 0);
3636 * Split a mirror pool. If newroot points to null, then a new nvlist
3637 * is generated and it is the responsibility of the caller to free it.
3640 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
3641 nvlist_t *props, splitflags_t flags)
3643 zfs_cmd_t zc = {"\0"};
3644 char msg[1024], *bias;
3645 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
3646 nvlist_t **varray = NULL, *zc_props = NULL;
3647 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
3648 libzfs_handle_t *hdl = zhp->zpool_hdl;
3649 uint64_t vers, readonly = B_FALSE;
3650 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
3653 (void) snprintf(msg, sizeof (msg),
3654 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
3656 if (!zpool_name_valid(hdl, B_FALSE, newname))
3657 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
3659 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
3660 (void) fprintf(stderr, gettext("Internal error: unable to "
3661 "retrieve pool configuration\n"));
3665 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
3667 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
3670 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
3671 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
3672 props, vers, flags, msg)) == NULL)
3674 (void) nvlist_lookup_uint64(zc_props,
3675 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3677 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3678 "property %s can only be set at import time"),
3679 zpool_prop_to_name(ZPOOL_PROP_READONLY));
3684 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3686 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3687 "Source pool is missing vdev tree"));
3688 nvlist_free(zc_props);
3692 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
3695 if (*newroot == NULL ||
3696 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
3697 &newchild, &newchildren) != 0)
3700 for (c = 0; c < children; c++) {
3701 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
3702 boolean_t is_special = B_FALSE, is_dedup = B_FALSE;
3704 nvlist_t **mchild, *vdev;
3709 * Unlike cache & spares, slogs are stored in the
3710 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3712 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3714 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
3716 if (is_log || is_hole) {
3718 * Create a hole vdev and put it in the config.
3720 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
3722 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
3723 VDEV_TYPE_HOLE) != 0)
3725 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
3730 varray[vcount++] = vdev;
3734 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
3737 if (strcmp(type, VDEV_TYPE_INDIRECT) == 0) {
3739 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3742 } else if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
3743 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3744 "Source pool must be composed only of mirrors\n"));
3745 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3749 if (nvlist_lookup_string(child[c],
3750 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias) == 0) {
3751 if (strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0)
3752 is_special = B_TRUE;
3753 else if (strcmp(bias, VDEV_ALLOC_BIAS_DEDUP) == 0)
3756 verify(nvlist_lookup_nvlist_array(child[c],
3757 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3759 /* find or add an entry for this top-level vdev */
3760 if (newchildren > 0 &&
3761 (entry = find_vdev_entry(zhp, mchild, mchildren,
3762 newchild, newchildren)) >= 0) {
3763 /* We found a disk that the user specified. */
3764 vdev = mchild[entry];
3767 /* User didn't specify a disk for this vdev. */
3768 vdev = mchild[mchildren - 1];
3771 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3774 if (flags.dryrun != 0) {
3775 if (is_dedup == B_TRUE) {
3776 if (nvlist_add_string(varray[vcount - 1],
3777 ZPOOL_CONFIG_ALLOCATION_BIAS,
3778 VDEV_ALLOC_BIAS_DEDUP) != 0)
3780 } else if (is_special == B_TRUE) {
3781 if (nvlist_add_string(varray[vcount - 1],
3782 ZPOOL_CONFIG_ALLOCATION_BIAS,
3783 VDEV_ALLOC_BIAS_SPECIAL) != 0)
3789 /* did we find every disk the user specified? */
3790 if (found != newchildren) {
3791 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3792 "include at most one disk from each mirror"));
3793 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3797 /* Prepare the nvlist for populating. */
3798 if (*newroot == NULL) {
3799 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3802 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3803 VDEV_TYPE_ROOT) != 0)
3806 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3809 /* Add all the children we found */
3810 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3811 lastlog == 0 ? vcount : lastlog) != 0)
3815 * If we're just doing a dry run, exit now with success.
3818 memory_err = B_FALSE;
3823 /* now build up the config list & call the ioctl */
3824 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3827 if (nvlist_add_nvlist(newconfig,
3828 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3829 nvlist_add_string(newconfig,
3830 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3831 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3835 * The new pool is automatically part of the namespace unless we
3836 * explicitly export it.
3839 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3840 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3841 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3842 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3844 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3847 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3848 retval = zpool_standard_error(hdl, errno, msg);
3853 memory_err = B_FALSE;
3856 if (varray != NULL) {
3859 for (v = 0; v < vcount; v++)
3860 nvlist_free(varray[v]);
3863 zcmd_free_nvlists(&zc);
3864 nvlist_free(zc_props);
3865 nvlist_free(newconfig);
3867 nvlist_free(*newroot);
3875 return (no_memory(hdl));
3881 * Remove the given device.
3884 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3886 zfs_cmd_t zc = {"\0"};
3889 boolean_t avail_spare, l2cache, islog;
3890 libzfs_handle_t *hdl = zhp->zpool_hdl;
3893 (void) snprintf(msg, sizeof (msg),
3894 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3896 if (zpool_is_draid_spare(path)) {
3897 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3898 "dRAID spares cannot be removed"));
3899 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3902 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3903 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3905 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3907 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3908 if (islog && version < SPA_VERSION_HOLES) {
3909 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3910 "pool must be upgraded to support log removal"));
3911 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3914 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3916 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3922 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3923 "invalid config; all top-level vdevs must "
3924 "have the same sector size and not be raidz."));
3925 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3930 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3931 "Mount encrypted datasets to replay logs."));
3933 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3934 "Pool busy; removal may already be in progress"));
3936 (void) zfs_error(hdl, EZFS_BUSY, msg);
3941 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3942 "Mount encrypted datasets to replay logs."));
3943 (void) zfs_error(hdl, EZFS_BUSY, msg);
3945 (void) zpool_standard_error(hdl, errno, msg);
3950 (void) zpool_standard_error(hdl, errno, msg);
3956 zpool_vdev_remove_cancel(zpool_handle_t *zhp)
3960 libzfs_handle_t *hdl = zhp->zpool_hdl;
3962 (void) snprintf(msg, sizeof (msg),
3963 dgettext(TEXT_DOMAIN, "cannot cancel removal"));
3965 bzero(&zc, sizeof (zc));
3966 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3969 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3972 return (zpool_standard_error(hdl, errno, msg));
3976 zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path,
3981 boolean_t avail_spare, l2cache, islog;
3982 libzfs_handle_t *hdl = zhp->zpool_hdl;
3984 (void) snprintf(msg, sizeof (msg),
3985 dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"),
3988 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3990 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3992 if (avail_spare || l2cache || islog) {
3997 if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) {
3998 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3999 "indirect size not available"));
4000 return (zfs_error(hdl, EINVAL, msg));
4006 * Clear the errors for the pool, or the particular device if specified.
4009 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
4011 zfs_cmd_t zc = {"\0"};
4014 zpool_load_policy_t policy;
4015 boolean_t avail_spare, l2cache;
4016 libzfs_handle_t *hdl = zhp->zpool_hdl;
4017 nvlist_t *nvi = NULL;
4021 (void) snprintf(msg, sizeof (msg),
4022 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
4025 (void) snprintf(msg, sizeof (msg),
4026 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
4029 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4031 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
4032 &l2cache, NULL)) == NULL)
4033 return (zfs_error(hdl, EZFS_NODEVICE, msg));
4036 * Don't allow error clearing for hot spares. Do allow
4037 * error clearing for l2cache devices.
4040 return (zfs_error(hdl, EZFS_ISSPARE, msg));
4042 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
4046 zpool_get_load_policy(rewindnvl, &policy);
4047 zc.zc_cookie = policy.zlp_rewind;
4049 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
4052 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
4055 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
4057 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
4058 zcmd_free_nvlists(&zc);
4063 if (!error || ((policy.zlp_rewind & ZPOOL_TRY_REWIND) &&
4064 errno != EPERM && errno != EACCES)) {
4065 if (policy.zlp_rewind &
4066 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
4067 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
4068 zpool_rewind_exclaim(hdl, zc.zc_name,
4069 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0),
4073 zcmd_free_nvlists(&zc);
4077 zcmd_free_nvlists(&zc);
4078 return (zpool_standard_error(hdl, errno, msg));
4082 * Similar to zpool_clear(), but takes a GUID (used by fmd).
4085 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
4087 zfs_cmd_t zc = {"\0"};
4089 libzfs_handle_t *hdl = zhp->zpool_hdl;
4091 (void) snprintf(msg, sizeof (msg),
4092 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
4093 (u_longlong_t)guid);
4095 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4097 zc.zc_cookie = ZPOOL_NO_REWIND;
4099 if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
4102 return (zpool_standard_error(hdl, errno, msg));
4106 * Change the GUID for a pool.
4109 zpool_reguid(zpool_handle_t *zhp)
4112 libzfs_handle_t *hdl = zhp->zpool_hdl;
4113 zfs_cmd_t zc = {"\0"};
4115 (void) snprintf(msg, sizeof (msg),
4116 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
4118 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4119 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
4122 return (zpool_standard_error(hdl, errno, msg));
4129 zpool_reopen_one(zpool_handle_t *zhp, void *data)
4131 libzfs_handle_t *hdl = zpool_get_handle(zhp);
4132 const char *pool_name = zpool_get_name(zhp);
4133 boolean_t *scrub_restart = data;
4136 error = lzc_reopen(pool_name, *scrub_restart);
4138 return (zpool_standard_error_fmt(hdl, error,
4139 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"), pool_name));
4145 /* call into libzfs_core to execute the sync IOCTL per pool */
4147 zpool_sync_one(zpool_handle_t *zhp, void *data)
4150 libzfs_handle_t *hdl = zpool_get_handle(zhp);
4151 const char *pool_name = zpool_get_name(zhp);
4152 boolean_t *force = data;
4153 nvlist_t *innvl = fnvlist_alloc();
4155 fnvlist_add_boolean_value(innvl, "force", *force);
4156 if ((ret = lzc_sync(pool_name, innvl, NULL)) != 0) {
4158 return (zpool_standard_error_fmt(hdl, ret,
4159 dgettext(TEXT_DOMAIN, "sync '%s' failed"), pool_name));
4166 #define PATH_BUF_LEN 64
4169 * Given a vdev, return the name to display in iostat. If the vdev has a path,
4170 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
4171 * We also check if this is a whole disk, in which case we strip off the
4172 * trailing 's0' slice name.
4174 * This routine is also responsible for identifying when disks have been
4175 * reconfigured in a new location. The kernel will have opened the device by
4176 * devid, but the path will still refer to the old location. To catch this, we
4177 * first do a path -> devid translation (which is fast for the common case). If
4178 * the devid matches, we're done. If not, we do a reverse devid -> path
4179 * translation and issue the appropriate ioctl() to update the path of the vdev.
4180 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
4184 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
4187 char *path, *type, *env;
4189 char buf[PATH_BUF_LEN];
4190 char tmpbuf[PATH_BUF_LEN];
4193 * vdev_name will be "root"/"root-0" for the root vdev, but it is the
4194 * zpool name that will be displayed to the user.
4196 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
4197 if (zhp != NULL && strcmp(type, "root") == 0)
4198 return (zfs_strdup(hdl, zpool_get_name(zhp)));
4200 env = getenv("ZPOOL_VDEV_NAME_PATH");
4201 if (env && (strtoul(env, NULL, 0) > 0 ||
4202 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
4203 name_flags |= VDEV_NAME_PATH;
4205 env = getenv("ZPOOL_VDEV_NAME_GUID");
4206 if (env && (strtoul(env, NULL, 0) > 0 ||
4207 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
4208 name_flags |= VDEV_NAME_GUID;
4210 env = getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
4211 if (env && (strtoul(env, NULL, 0) > 0 ||
4212 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
4213 name_flags |= VDEV_NAME_FOLLOW_LINKS;
4215 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
4216 name_flags & VDEV_NAME_GUID) {
4217 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value);
4218 (void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)value);
4220 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
4221 if (name_flags & VDEV_NAME_FOLLOW_LINKS) {
4222 char *rp = realpath(path, NULL);
4224 strlcpy(buf, rp, sizeof (buf));
4231 * For a block device only use the name.
4233 if ((strcmp(type, VDEV_TYPE_DISK) == 0) &&
4234 !(name_flags & VDEV_NAME_PATH)) {
4235 path = zfs_strip_path(path);
4239 * Remove the partition from the path if this is a whole disk.
4241 if (strcmp(type, VDEV_TYPE_DRAID_SPARE) != 0 &&
4242 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, &value)
4243 == 0 && value && !(name_flags & VDEV_NAME_PATH)) {
4244 return (zfs_strip_partition(path));
4250 * If it's a raidz device, we need to stick in the parity level.
4252 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
4253 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
4255 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
4256 (u_longlong_t)value);
4261 * If it's a dRAID device, we add parity, groups, and spares.
4263 if (strcmp(path, VDEV_TYPE_DRAID) == 0) {
4264 uint64_t ndata, nparity, nspares;
4268 verify(nvlist_lookup_nvlist_array(nv,
4269 ZPOOL_CONFIG_CHILDREN, &child, &children) == 0);
4270 verify(nvlist_lookup_uint64(nv,
4271 ZPOOL_CONFIG_NPARITY, &nparity) == 0);
4272 verify(nvlist_lookup_uint64(nv,
4273 ZPOOL_CONFIG_DRAID_NDATA, &ndata) == 0);
4274 verify(nvlist_lookup_uint64(nv,
4275 ZPOOL_CONFIG_DRAID_NSPARES, &nspares) == 0);
4277 path = zpool_draid_name(buf, sizeof (buf), ndata,
4278 nparity, nspares, children);
4282 * We identify each top-level vdev by using a <type-id>
4283 * naming convention.
4285 if (name_flags & VDEV_NAME_TYPE_ID) {
4287 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
4289 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s-%llu",
4290 path, (u_longlong_t)id);
4295 return (zfs_strdup(hdl, path));
4299 zbookmark_mem_compare(const void *a, const void *b)
4301 return (memcmp(a, b, sizeof (zbookmark_phys_t)));
4305 * Retrieve the persistent error log, uniquify the members, and return to the
4309 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
4311 zfs_cmd_t zc = {"\0"};
4312 libzfs_handle_t *hdl = zhp->zpool_hdl;
4314 zbookmark_phys_t *zb = NULL;
4318 * Retrieve the raw error list from the kernel. If the number of errors
4319 * has increased, allocate more space and continue until we get the
4322 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
4326 zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
4327 count * sizeof (zbookmark_phys_t));
4328 zc.zc_nvlist_dst_size = count;
4329 (void) strcpy(zc.zc_name, zhp->zpool_name);
4331 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_ERROR_LOG,
4333 free((void *)(uintptr_t)zc.zc_nvlist_dst);
4334 if (errno == ENOMEM) {
4337 count = zc.zc_nvlist_dst_size;
4338 dst = zfs_alloc(zhp->zpool_hdl, count *
4339 sizeof (zbookmark_phys_t));
4340 zc.zc_nvlist_dst = (uintptr_t)dst;
4342 return (zpool_standard_error_fmt(hdl, errno,
4343 dgettext(TEXT_DOMAIN, "errors: List of "
4344 "errors unavailable")));
4352 * Sort the resulting bookmarks. This is a little confusing due to the
4353 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
4354 * to first, and 'zc_nvlist_dst_size' indicates the number of bookmarks
4355 * _not_ copied as part of the process. So we point the start of our
4356 * array appropriate and decrement the total number of elements.
4358 zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
4359 zc.zc_nvlist_dst_size;
4360 count -= zc.zc_nvlist_dst_size;
4362 qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
4364 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
4367 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
4369 for (i = 0; i < count; i++) {
4372 /* ignoring zb_blkid and zb_level for now */
4373 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
4374 zb[i-1].zb_object == zb[i].zb_object)
4377 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
4379 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
4380 zb[i].zb_objset) != 0) {
4384 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
4385 zb[i].zb_object) != 0) {
4389 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
4396 free((void *)(uintptr_t)zc.zc_nvlist_dst);
4400 free((void *)(uintptr_t)zc.zc_nvlist_dst);
4401 return (no_memory(zhp->zpool_hdl));
4405 * Upgrade a ZFS pool to the latest on-disk version.
4408 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
4410 zfs_cmd_t zc = {"\0"};
4411 libzfs_handle_t *hdl = zhp->zpool_hdl;
4413 (void) strcpy(zc.zc_name, zhp->zpool_name);
4414 zc.zc_cookie = new_version;
4416 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
4417 return (zpool_standard_error_fmt(hdl, errno,
4418 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
4424 zfs_save_arguments(int argc, char **argv, char *string, int len)
4428 (void) strlcpy(string, zfs_basename(argv[0]), len);
4429 for (i = 1; i < argc; i++) {
4430 (void) strlcat(string, " ", len);
4431 (void) strlcat(string, argv[i], len);
4436 zpool_log_history(libzfs_handle_t *hdl, const char *message)
4438 zfs_cmd_t zc = {"\0"};
4442 args = fnvlist_alloc();
4443 fnvlist_add_string(args, "message", message);
4444 err = zcmd_write_src_nvlist(hdl, &zc, args);
4446 err = zfs_ioctl(hdl, ZFS_IOC_LOG_HISTORY, &zc);
4448 zcmd_free_nvlists(&zc);
4453 * Perform ioctl to get some command history of a pool.
4455 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
4456 * logical offset of the history buffer to start reading from.
4458 * Upon return, 'off' is the next logical offset to read from and
4459 * 'len' is the actual amount of bytes read into 'buf'.
4462 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
4464 zfs_cmd_t zc = {"\0"};
4465 libzfs_handle_t *hdl = zhp->zpool_hdl;
4467 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4469 zc.zc_history = (uint64_t)(uintptr_t)buf;
4470 zc.zc_history_len = *len;
4471 zc.zc_history_offset = *off;
4473 if (zfs_ioctl(hdl, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
4476 return (zfs_error_fmt(hdl, EZFS_PERM,
4477 dgettext(TEXT_DOMAIN,
4478 "cannot show history for pool '%s'"),
4481 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
4482 dgettext(TEXT_DOMAIN, "cannot get history for pool "
4483 "'%s'"), zhp->zpool_name));
4485 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
4486 dgettext(TEXT_DOMAIN, "cannot get history for pool "
4487 "'%s', pool must be upgraded"), zhp->zpool_name));
4489 return (zpool_standard_error_fmt(hdl, errno,
4490 dgettext(TEXT_DOMAIN,
4491 "cannot get history for '%s'"), zhp->zpool_name));
4495 *len = zc.zc_history_len;
4496 *off = zc.zc_history_offset;
4502 * Retrieve the command history of a pool.
4505 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp, uint64_t *off,
4509 int buflen = 128 * 1024;
4510 nvlist_t **records = NULL;
4511 uint_t numrecords = 0;
4513 uint64_t start = *off;
4515 buf = malloc(buflen);
4518 /* process about 1MB a time */
4519 while (*off - start < 1024 * 1024) {
4520 uint64_t bytes_read = buflen;
4523 if ((err = get_history(zhp, buf, off, &bytes_read)) != 0)
4526 /* if nothing else was read in, we're at EOF, just return */
4532 if ((err = zpool_history_unpack(buf, bytes_read,
4533 &leftover, &records, &numrecords)) != 0)
4536 if (leftover == bytes_read) {
4538 * no progress made, because buffer is not big enough
4539 * to hold this record; resize and retry.
4543 buf = malloc(buflen);
4552 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
4553 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
4554 records, numrecords) == 0);
4556 for (i = 0; i < numrecords; i++)
4557 nvlist_free(records[i]);
4564 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
4565 * If there is a new event available 'nvp' will contain a newly allocated
4566 * nvlist and 'dropped' will be set to the number of missed events since
4567 * the last call to this function. When 'nvp' is set to NULL it indicates
4568 * no new events are available. In either case the function returns 0 and
4569 * it is up to the caller to free 'nvp'. In the case of a fatal error the
4570 * function will return a non-zero value. When the function is called in
4571 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
4572 * it will not return until a new event is available.
4575 zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
4576 int *dropped, unsigned flags, int zevent_fd)
4578 zfs_cmd_t zc = {"\0"};
4583 zc.zc_cleanup_fd = zevent_fd;
4585 if (flags & ZEVENT_NONBLOCK)
4586 zc.zc_guid = ZEVENT_NONBLOCK;
4588 if (zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE) != 0)
4592 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
4595 error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
4596 dgettext(TEXT_DOMAIN, "zfs shutdown"));
4599 /* Blocking error case should not occur */
4600 if (!(flags & ZEVENT_NONBLOCK))
4601 error = zpool_standard_error_fmt(hdl, errno,
4602 dgettext(TEXT_DOMAIN, "cannot get event"));
4606 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
4607 error = zfs_error_fmt(hdl, EZFS_NOMEM,
4608 dgettext(TEXT_DOMAIN, "cannot get event"));
4614 error = zpool_standard_error_fmt(hdl, errno,
4615 dgettext(TEXT_DOMAIN, "cannot get event"));
4620 error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
4624 *dropped = (int)zc.zc_cookie;
4626 zcmd_free_nvlists(&zc);
4635 zpool_events_clear(libzfs_handle_t *hdl, int *count)
4637 zfs_cmd_t zc = {"\0"};
4639 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
4640 return (zpool_standard_error(hdl, errno,
4641 dgettext(TEXT_DOMAIN, "cannot clear events")));
4644 *count = (int)zc.zc_cookie; /* # of events cleared */
4650 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4651 * the passed zevent_fd file handle. On success zero is returned,
4652 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4655 zpool_events_seek(libzfs_handle_t *hdl, uint64_t eid, int zevent_fd)
4657 zfs_cmd_t zc = {"\0"};
4661 zc.zc_cleanup_fd = zevent_fd;
4663 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_SEEK, &zc) != 0) {
4666 error = zfs_error_fmt(hdl, EZFS_NOENT,
4667 dgettext(TEXT_DOMAIN, "cannot get event"));
4671 error = zfs_error_fmt(hdl, EZFS_NOMEM,
4672 dgettext(TEXT_DOMAIN, "cannot get event"));
4676 error = zpool_standard_error_fmt(hdl, errno,
4677 dgettext(TEXT_DOMAIN, "cannot get event"));
4686 zpool_obj_to_path_impl(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4687 char *pathname, size_t len, boolean_t always_unmounted)
4689 zfs_cmd_t zc = {"\0"};
4690 boolean_t mounted = B_FALSE;
4691 char *mntpnt = NULL;
4692 char dsname[ZFS_MAX_DATASET_NAME_LEN];
4695 /* special case for the MOS */
4696 (void) snprintf(pathname, len, "<metadata>:<0x%llx>",
4701 /* get the dataset's name */
4702 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4704 if (zfs_ioctl(zhp->zpool_hdl,
4705 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
4706 /* just write out a path of two object numbers */
4707 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
4708 (longlong_t)dsobj, (longlong_t)obj);
4711 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
4713 /* find out if the dataset is mounted */
4714 mounted = !always_unmounted && is_mounted(zhp->zpool_hdl, dsname,
4717 /* get the corrupted object's path */
4718 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
4720 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_OBJ_TO_PATH,
4723 (void) snprintf(pathname, len, "%s%s", mntpnt,
4726 (void) snprintf(pathname, len, "%s:%s",
4727 dsname, zc.zc_value);
4730 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname,
4737 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4738 char *pathname, size_t len)
4740 zpool_obj_to_path_impl(zhp, dsobj, obj, pathname, len, B_FALSE);
4744 zpool_obj_to_path_ds(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4745 char *pathname, size_t len)
4747 zpool_obj_to_path_impl(zhp, dsobj, obj, pathname, len, B_TRUE);
4750 * Wait while the specified activity is in progress in the pool.
4753 zpool_wait(zpool_handle_t *zhp, zpool_wait_activity_t activity)
4757 int error = zpool_wait_status(zhp, activity, &missing, NULL);
4760 (void) zpool_standard_error_fmt(zhp->zpool_hdl, ENOENT,
4761 dgettext(TEXT_DOMAIN, "error waiting in pool '%s'"),
4770 * Wait for the given activity and return the status of the wait (whether or not
4771 * any waiting was done) in the 'waited' parameter. Non-existent pools are
4772 * reported via the 'missing' parameter, rather than by printing an error
4773 * message. This is convenient when this function is called in a loop over a
4774 * long period of time (as it is, for example, by zpool's wait cmd). In that
4775 * scenario, a pool being exported or destroyed should be considered a normal
4776 * event, so we don't want to print an error when we find that the pool doesn't
4780 zpool_wait_status(zpool_handle_t *zhp, zpool_wait_activity_t activity,
4781 boolean_t *missing, boolean_t *waited)
4783 int error = lzc_wait(zhp->zpool_name, activity, waited);
4784 *missing = (error == ENOENT);
4789 (void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
4790 dgettext(TEXT_DOMAIN, "error waiting in pool '%s'"),
4798 zpool_set_bootenv(zpool_handle_t *zhp, const nvlist_t *envmap)
4800 int error = lzc_set_bootenv(zhp->zpool_name, envmap);
4802 (void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
4803 dgettext(TEXT_DOMAIN,
4804 "error setting bootenv in pool '%s'"), zhp->zpool_name);
4811 zpool_get_bootenv(zpool_handle_t *zhp, nvlist_t **nvlp)
4817 error = lzc_get_bootenv(zhp->zpool_name, &nvl);
4819 (void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
4820 dgettext(TEXT_DOMAIN,
4821 "error getting bootenv in pool '%s'"), zhp->zpool_name);
4830 * Attempt to read and parse feature file(s) (from "compatibility" property).
4831 * Files contain zpool feature names, comma or whitespace-separated.
4832 * Comments (# character to next newline) are discarded.
4835 * compatibility : string containing feature filenames
4836 * features : either NULL or pointer to array of boolean
4837 * report : either NULL or pointer to string buffer
4838 * rlen : length of "report" buffer
4840 * compatibility is NULL (unset), "", "off", "legacy", or list of
4841 * comma-separated filenames. filenames should either be absolute,
4843 * 1) ZPOOL_SYSCONF_COMPAT_D (eg: /etc/zfs/compatibility.d) or
4844 * 2) ZPOOL_DATA_COMPAT_D (eg: /usr/share/zfs/compatibility.d).
4845 * (Unset), "" or "off" => enable all features
4846 * "legacy" => disable all features
4848 * Any feature names read from files which match unames in spa_feature_table
4849 * will have the corresponding boolean set in the features array (if non-NULL).
4850 * If more than one feature set specified, only features present in *all* of
4853 * "report" if not NULL will be populated with a suitable status message.
4856 * ZPOOL_COMPATIBILITY_OK : files read and parsed ok
4857 * ZPOOL_COMPATIBILITY_BADFILE : file too big or not a text file
4858 * ZPOOL_COMPATIBILITY_BADTOKEN : SYSCONF file contains invalid feature name
4859 * ZPOOL_COMPATIBILITY_WARNTOKEN : DATA file contains invalid feature name
4860 * ZPOOL_COMPATIBILITY_NOFILES : no feature files found
4862 zpool_compat_status_t
4863 zpool_load_compat(const char *compat, boolean_t *features, char *report,
4866 int sdirfd, ddirfd, featfd;
4870 char *file, *line, *word;
4872 char l_compat[ZFS_MAXPROPLEN];
4874 boolean_t ret_nofiles = B_TRUE;
4875 boolean_t ret_badfile = B_FALSE;
4876 boolean_t ret_badtoken = B_FALSE;
4877 boolean_t ret_warntoken = B_FALSE;
4879 /* special cases (unset), "" and "off" => enable all features */
4880 if (compat == NULL || compat[0] == '\0' ||
4881 strcmp(compat, ZPOOL_COMPAT_OFF) == 0) {
4882 if (features != NULL)
4883 for (uint_t i = 0; i < SPA_FEATURES; i++)
4884 features[i] = B_TRUE;
4886 strlcpy(report, gettext("all features enabled"), rlen);
4887 return (ZPOOL_COMPATIBILITY_OK);
4890 /* Final special case "legacy" => disable all features */
4891 if (strcmp(compat, ZPOOL_COMPAT_LEGACY) == 0) {
4892 if (features != NULL)
4893 for (uint_t i = 0; i < SPA_FEATURES; i++)
4894 features[i] = B_FALSE;
4896 strlcpy(report, gettext("all features disabled"), rlen);
4897 return (ZPOOL_COMPATIBILITY_OK);
4901 * Start with all true; will be ANDed with results from each file
4903 if (features != NULL)
4904 for (uint_t i = 0; i < SPA_FEATURES; i++)
4905 features[i] = B_TRUE;
4907 char err_badfile[1024] = "";
4908 char err_badtoken[1024] = "";
4911 * We ignore errors from the directory open()
4912 * as they're only needed if the filename is relative
4913 * which will be checked during the openat().
4916 /* O_PATH safer than O_RDONLY if system allows it */
4918 #define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_PATH)
4920 #define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_RDONLY)
4923 sdirfd = open(ZPOOL_SYSCONF_COMPAT_D, ZC_DIR_FLAGS);
4924 ddirfd = open(ZPOOL_DATA_COMPAT_D, ZC_DIR_FLAGS);
4926 (void) strlcpy(l_compat, compat, ZFS_MAXPROPLEN);
4928 for (file = strtok_r(l_compat, ",", &ps);
4930 file = strtok_r(NULL, ",", &ps)) {
4932 boolean_t l_features[SPA_FEATURES];
4934 enum { Z_SYSCONF, Z_DATA } source;
4936 /* try sysconfdir first, then datadir */
4938 if ((featfd = openat(sdirfd, file, O_RDONLY | O_CLOEXEC)) < 0) {
4939 featfd = openat(ddirfd, file, O_RDONLY | O_CLOEXEC);
4943 /* File readable and correct size? */
4945 fstat(featfd, &fs) < 0 ||
4947 fs.st_size > ZPOOL_COMPAT_MAXSIZE) {
4948 (void) close(featfd);
4949 strlcat(err_badfile, file, ZFS_MAXPROPLEN);
4950 strlcat(err_badfile, " ", ZFS_MAXPROPLEN);
4951 ret_badfile = B_TRUE;
4955 /* Prefault the file if system allows */
4956 #if defined(MAP_POPULATE)
4957 #define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_POPULATE)
4958 #elif defined(MAP_PREFAULT_READ)
4959 #define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_PREFAULT_READ)
4961 #define ZC_MMAP_FLAGS (MAP_PRIVATE)
4964 /* private mmap() so we can strtok safely */
4965 fc = (char *)mmap(NULL, fs.st_size, PROT_READ | PROT_WRITE,
4966 ZC_MMAP_FLAGS, featfd, 0);
4967 (void) close(featfd);
4969 /* map ok, and last character == newline? */
4970 if (fc == MAP_FAILED || fc[fs.st_size - 1] != '\n') {
4971 (void) munmap((void *) fc, fs.st_size);
4972 strlcat(err_badfile, file, ZFS_MAXPROPLEN);
4973 strlcat(err_badfile, " ", ZFS_MAXPROPLEN);
4974 ret_badfile = B_TRUE;
4978 ret_nofiles = B_FALSE;
4980 for (uint_t i = 0; i < SPA_FEATURES; i++)
4981 l_features[i] = B_FALSE;
4983 /* replace final newline with NULL to ensure string ends */
4984 fc[fs.st_size - 1] = '\0';
4986 for (line = strtok_r(fc, "\n", &ls);
4988 line = strtok_r(NULL, "\n", &ls)) {
4989 /* discard comments */
4990 char *r = strchr(line, '#');
4994 for (word = strtok_r(line, ", \t", &ws);
4996 word = strtok_r(NULL, ", \t", &ws)) {
4997 /* Find matching feature name */
4999 for (f = 0; f < SPA_FEATURES; f++) {
5000 zfeature_info_t *fi =
5001 &spa_feature_table[f];
5002 if (strcmp(word, fi->fi_uname) == 0) {
5003 l_features[f] = B_TRUE;
5007 if (f < SPA_FEATURES)
5010 /* found an unrecognized word */
5011 /* lightly sanitize it */
5012 if (strlen(word) > 32)
5014 for (char *c = word; *c != '\0'; c++)
5018 strlcat(err_badtoken, word, ZFS_MAXPROPLEN);
5019 strlcat(err_badtoken, " ", ZFS_MAXPROPLEN);
5020 if (source == Z_SYSCONF)
5021 ret_badtoken = B_TRUE;
5023 ret_warntoken = B_TRUE;
5026 (void) munmap((void *) fc, fs.st_size);
5028 if (features != NULL)
5029 for (uint_t i = 0; i < SPA_FEATURES; i++)
5030 features[i] &= l_features[i];
5032 (void) close(sdirfd);
5033 (void) close(ddirfd);
5035 /* Return the most serious error */
5038 snprintf(report, rlen, gettext("could not read/"
5039 "parse feature file(s): %s"), err_badfile);
5040 return (ZPOOL_COMPATIBILITY_BADFILE);
5045 gettext("no valid compatibility files specified"),
5047 return (ZPOOL_COMPATIBILITY_NOFILES);
5051 snprintf(report, rlen, gettext("invalid feature "
5052 "name(s) in local compatibility files: %s"),
5054 return (ZPOOL_COMPATIBILITY_BADTOKEN);
5056 if (ret_warntoken) {
5058 snprintf(report, rlen, gettext("unrecognized feature "
5059 "name(s) in distribution compatibility files: %s"),
5061 return (ZPOOL_COMPATIBILITY_WARNTOKEN);
5064 strlcpy(report, gettext("compatibility set ok"), rlen);
5065 return (ZPOOL_COMPATIBILITY_OK);
5069 zpool_vdev_guid(zpool_handle_t *zhp, const char *vdevname, uint64_t *vdev_guid)
5072 boolean_t avail_spare, l2cache;
5074 verify(zhp != NULL);
5075 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
5077 (void) snprintf(errbuf, sizeof (errbuf),
5078 dgettext(TEXT_DOMAIN, "pool is in an unavailable state"));
5079 return (zfs_error(zhp->zpool_hdl, EZFS_POOLUNAVAIL, errbuf));
5082 if ((tgt = zpool_find_vdev(zhp, vdevname, &avail_spare, &l2cache,
5085 (void) snprintf(errbuf, sizeof (errbuf),
5086 dgettext(TEXT_DOMAIN, "can not find %s in %s"),
5087 vdevname, zhp->zpool_name);
5088 return (zfs_error(zhp->zpool_hdl, EZFS_NODEVICE, errbuf));
5091 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, vdev_guid) == 0);
5096 * Get a vdev property value for 'prop' and return the value in
5097 * a pre-allocated buffer.
5100 zpool_get_vdev_prop_value(nvlist_t *nvprop, vdev_prop_t prop, char *prop_name,
5101 char *buf, size_t len, zprop_source_t *srctype, boolean_t literal)
5106 zprop_source_t src = ZPROP_SRC_NONE;
5108 if (prop == VDEV_PROP_USER) {
5109 /* user property, prop_name must contain the property name */
5110 assert(prop_name != NULL);
5111 if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) {
5112 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE,
5115 verify(nvlist_lookup_string(nv, ZPROP_VALUE,
5118 /* user prop not found */
5121 (void) strlcpy(buf, strval, len);
5127 if (prop_name == NULL)
5128 prop_name = (char *)vdev_prop_to_name(prop);
5130 switch (vdev_prop_get_type(prop)) {
5131 case PROP_TYPE_STRING:
5132 if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) {
5133 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE,
5136 verify(nvlist_lookup_string(nv, ZPROP_VALUE,
5139 src = ZPROP_SRC_DEFAULT;
5140 if ((strval = (char *)vdev_prop_default_string(prop))
5144 (void) strlcpy(buf, strval, len);
5147 case PROP_TYPE_NUMBER:
5148 if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) {
5149 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE,
5152 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE,
5155 src = ZPROP_SRC_DEFAULT;
5156 intval = vdev_prop_default_numeric(prop);
5160 case VDEV_PROP_ASIZE:
5161 case VDEV_PROP_PSIZE:
5162 case VDEV_PROP_SIZE:
5163 case VDEV_PROP_ALLOCATED:
5164 case VDEV_PROP_FREE:
5165 case VDEV_PROP_READ_ERRORS:
5166 case VDEV_PROP_WRITE_ERRORS:
5167 case VDEV_PROP_CHECKSUM_ERRORS:
5168 case VDEV_PROP_INITIALIZE_ERRORS:
5169 case VDEV_PROP_OPS_NULL:
5170 case VDEV_PROP_OPS_READ:
5171 case VDEV_PROP_OPS_WRITE:
5172 case VDEV_PROP_OPS_FREE:
5173 case VDEV_PROP_OPS_CLAIM:
5174 case VDEV_PROP_OPS_TRIM:
5175 case VDEV_PROP_BYTES_NULL:
5176 case VDEV_PROP_BYTES_READ:
5177 case VDEV_PROP_BYTES_WRITE:
5178 case VDEV_PROP_BYTES_FREE:
5179 case VDEV_PROP_BYTES_CLAIM:
5180 case VDEV_PROP_BYTES_TRIM:
5182 (void) snprintf(buf, len, "%llu",
5183 (u_longlong_t)intval);
5185 (void) zfs_nicenum(intval, buf, len);
5188 case VDEV_PROP_EXPANDSZ:
5190 (void) strlcpy(buf, "-", len);
5191 } else if (literal) {
5192 (void) snprintf(buf, len, "%llu",
5193 (u_longlong_t)intval);
5195 (void) zfs_nicenum(intval, buf, len);
5198 case VDEV_PROP_CAPACITY:
5200 (void) snprintf(buf, len, "%llu",
5201 (u_longlong_t)intval);
5203 (void) snprintf(buf, len, "%llu%%",
5204 (u_longlong_t)intval);
5207 case VDEV_PROP_FRAGMENTATION:
5208 if (intval == UINT64_MAX) {
5209 (void) strlcpy(buf, "-", len);
5211 (void) snprintf(buf, len, "%llu%%",
5212 (u_longlong_t)intval);
5215 case VDEV_PROP_STATE:
5217 (void) snprintf(buf, len, "%llu",
5218 (u_longlong_t)intval);
5220 (void) strlcpy(buf, zpool_state_to_name(intval,
5221 VDEV_AUX_NONE), len);
5225 (void) snprintf(buf, len, "%llu",
5226 (u_longlong_t)intval);
5230 case PROP_TYPE_INDEX:
5231 if (nvlist_lookup_nvlist(nvprop, prop_name, &nv) == 0) {
5232 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE,
5235 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE,
5238 src = ZPROP_SRC_DEFAULT;
5239 intval = vdev_prop_default_numeric(prop);
5241 if (vdev_prop_index_to_string(prop, intval,
5242 (const char **)&strval) != 0)
5244 (void) strlcpy(buf, strval, len);
5258 * Get a vdev property value for 'prop_name' and return the value in
5259 * a pre-allocated buffer.
5262 zpool_get_vdev_prop(zpool_handle_t *zhp, const char *vdevname, vdev_prop_t prop,
5263 char *prop_name, char *buf, size_t len, zprop_source_t *srctype,
5266 nvlist_t *reqnvl, *reqprops;
5267 nvlist_t *retprops = NULL;
5271 if ((ret = zpool_vdev_guid(zhp, vdevname, &vdev_guid)) != 0)
5274 if (nvlist_alloc(&reqnvl, NV_UNIQUE_NAME, 0) != 0)
5275 return (no_memory(zhp->zpool_hdl));
5276 if (nvlist_alloc(&reqprops, NV_UNIQUE_NAME, 0) != 0)
5277 return (no_memory(zhp->zpool_hdl));
5279 fnvlist_add_uint64(reqnvl, ZPOOL_VDEV_PROPS_GET_VDEV, vdev_guid);
5281 if (prop != VDEV_PROP_USER) {
5282 /* prop_name overrides prop value */
5283 if (prop_name != NULL)
5284 prop = vdev_name_to_prop(prop_name);
5286 prop_name = (char *)vdev_prop_to_name(prop);
5287 assert(prop < VDEV_NUM_PROPS);
5290 assert(prop_name != NULL);
5291 if (nvlist_add_uint64(reqprops, prop_name, prop) != 0) {
5292 nvlist_free(reqnvl);
5293 nvlist_free(reqprops);
5294 return (no_memory(zhp->zpool_hdl));
5297 fnvlist_add_nvlist(reqnvl, ZPOOL_VDEV_PROPS_GET_PROPS, reqprops);
5299 ret = lzc_get_vdev_prop(zhp->zpool_name, reqnvl, &retprops);
5302 ret = zpool_get_vdev_prop_value(retprops, prop, prop_name, buf,
5303 len, srctype, literal);
5306 (void) snprintf(errbuf, sizeof (errbuf),
5307 dgettext(TEXT_DOMAIN, "cannot get vdev property %s from"
5308 " %s in %s"), prop_name, vdevname, zhp->zpool_name);
5309 (void) zpool_standard_error(zhp->zpool_hdl, ret, errbuf);
5312 nvlist_free(reqnvl);
5313 nvlist_free(reqprops);
5314 nvlist_free(retprops);
5320 * Get all vdev properties
5323 zpool_get_all_vdev_props(zpool_handle_t *zhp, const char *vdevname,
5326 nvlist_t *nvl = NULL;
5330 if ((ret = zpool_vdev_guid(zhp, vdevname, &vdev_guid)) != 0)
5333 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5334 return (no_memory(zhp->zpool_hdl));
5336 fnvlist_add_uint64(nvl, ZPOOL_VDEV_PROPS_GET_VDEV, vdev_guid);
5338 ret = lzc_get_vdev_prop(zhp->zpool_name, nvl, outnvl);
5344 (void) snprintf(errbuf, sizeof (errbuf),
5345 dgettext(TEXT_DOMAIN, "cannot get vdev properties for"
5346 " %s in %s"), vdevname, zhp->zpool_name);
5347 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
5357 zpool_set_vdev_prop(zpool_handle_t *zhp, const char *vdevname,
5358 const char *propname, const char *propval)
5362 nvlist_t *nvl = NULL;
5363 nvlist_t *outnvl = NULL;
5365 nvlist_t *realprops;
5366 prop_flags_t flags = { 0 };
5370 if ((ret = zpool_vdev_guid(zhp, vdevname, &vdev_guid)) != 0)
5373 vprop = vdev_name_to_prop(propname);
5375 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
5376 return (no_memory(zhp->zpool_hdl));
5377 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0)
5378 return (no_memory(zhp->zpool_hdl));
5380 fnvlist_add_uint64(nvl, ZPOOL_VDEV_PROPS_SET_VDEV, vdev_guid);
5382 if (nvlist_add_string(props, propname, propval) != 0) {
5384 return (no_memory(zhp->zpool_hdl));
5388 (void) snprintf(errbuf, sizeof (errbuf),
5389 dgettext(TEXT_DOMAIN, "cannot set property %s for %s on %s"),
5390 propname, vdevname, zhp->zpool_name);
5393 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
5394 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
5395 zhp->zpool_name, props, version, flags, errbuf)) == NULL) {
5404 fnvlist_add_nvlist(nvl, ZPOOL_VDEV_PROPS_SET_PROPS, props);
5406 ret = lzc_set_vdev_prop(zhp->zpool_name, nvl, &outnvl);
5410 nvlist_free(outnvl);
5413 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);