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>
43 #include <sys/efi_partition.h>
44 #include <sys/systeminfo.h>
45 #include <sys/zfs_ioctl.h>
46 #include <sys/zfs_sysfs.h>
47 #include <sys/vdev_disk.h>
48 #include <sys/types.h>
53 #include "zfs_namecheck.h"
55 #include "libzfs_impl.h"
56 #include "zfs_comutil.h"
57 #include "zfeature_common.h"
59 static boolean_t zpool_vdev_is_interior(const char *name);
61 typedef struct prop_flags {
62 int create:1; /* Validate property on creation */
63 int import:1; /* Validate property on import */
67 * ====================================================================
68 * zpool property functions
69 * ====================================================================
73 zpool_get_all_props(zpool_handle_t *zhp)
75 zfs_cmd_t zc = {"\0"};
76 libzfs_handle_t *hdl = zhp->zpool_hdl;
78 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
80 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
83 while (zfs_ioctl(hdl, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
84 if (errno == ENOMEM) {
85 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
86 zcmd_free_nvlists(&zc);
90 zcmd_free_nvlists(&zc);
95 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
96 zcmd_free_nvlists(&zc);
100 zcmd_free_nvlists(&zc);
106 zpool_props_refresh(zpool_handle_t *zhp)
110 old_props = zhp->zpool_props;
112 if (zpool_get_all_props(zhp) != 0)
115 nvlist_free(old_props);
120 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
126 zprop_source_t source;
128 nvl = zhp->zpool_props;
129 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
130 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
132 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
134 source = ZPROP_SRC_DEFAULT;
135 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
146 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
150 zprop_source_t source;
152 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
154 * zpool_get_all_props() has most likely failed because
155 * the pool is faulted, but if all we need is the top level
156 * vdev's guid then get it from the zhp config nvlist.
158 if ((prop == ZPOOL_PROP_GUID) &&
159 (nvlist_lookup_nvlist(zhp->zpool_config,
160 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
161 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
165 return (zpool_prop_default_numeric(prop));
168 nvl = zhp->zpool_props;
169 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
170 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
172 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
174 source = ZPROP_SRC_DEFAULT;
175 value = zpool_prop_default_numeric(prop);
185 * Map VDEV STATE to printed strings.
188 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
191 case VDEV_STATE_CLOSED:
192 case VDEV_STATE_OFFLINE:
193 return (gettext("OFFLINE"));
194 case VDEV_STATE_REMOVED:
195 return (gettext("REMOVED"));
196 case VDEV_STATE_CANT_OPEN:
197 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
198 return (gettext("FAULTED"));
199 else if (aux == VDEV_AUX_SPLIT_POOL)
200 return (gettext("SPLIT"));
202 return (gettext("UNAVAIL"));
203 case VDEV_STATE_FAULTED:
204 return (gettext("FAULTED"));
205 case VDEV_STATE_DEGRADED:
206 return (gettext("DEGRADED"));
207 case VDEV_STATE_HEALTHY:
208 return (gettext("ONLINE"));
214 return (gettext("UNKNOWN"));
218 * Map POOL STATE to printed strings.
221 zpool_pool_state_to_name(pool_state_t state)
226 case POOL_STATE_ACTIVE:
227 return (gettext("ACTIVE"));
228 case POOL_STATE_EXPORTED:
229 return (gettext("EXPORTED"));
230 case POOL_STATE_DESTROYED:
231 return (gettext("DESTROYED"));
232 case POOL_STATE_SPARE:
233 return (gettext("SPARE"));
234 case POOL_STATE_L2CACHE:
235 return (gettext("L2CACHE"));
236 case POOL_STATE_UNINITIALIZED:
237 return (gettext("UNINITIALIZED"));
238 case POOL_STATE_UNAVAIL:
239 return (gettext("UNAVAIL"));
240 case POOL_STATE_POTENTIALLY_ACTIVE:
241 return (gettext("POTENTIALLY_ACTIVE"));
244 return (gettext("UNKNOWN"));
248 * Given a pool handle, return the pool health string ("ONLINE", "DEGRADED",
252 zpool_get_state_str(zpool_handle_t *zhp)
254 zpool_errata_t errata;
255 zpool_status_t status;
261 status = zpool_get_status(zhp, NULL, &errata);
263 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
264 str = gettext("FAULTED");
265 } else if (status == ZPOOL_STATUS_IO_FAILURE_WAIT ||
266 status == ZPOOL_STATUS_IO_FAILURE_MMP) {
267 str = gettext("SUSPENDED");
269 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
270 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
271 verify(nvlist_lookup_uint64_array(nvroot,
272 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
274 str = zpool_state_to_name(vs->vs_state, vs->vs_aux);
280 * Get a zpool property value for 'prop' and return the value in
281 * a pre-allocated buffer.
284 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf,
285 size_t len, zprop_source_t *srctype, boolean_t literal)
289 zprop_source_t src = ZPROP_SRC_NONE;
291 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
293 case ZPOOL_PROP_NAME:
294 (void) strlcpy(buf, zpool_get_name(zhp), len);
297 case ZPOOL_PROP_HEALTH:
298 (void) strlcpy(buf, zpool_get_state_str(zhp), len);
301 case ZPOOL_PROP_GUID:
302 intval = zpool_get_prop_int(zhp, prop, &src);
303 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
306 case ZPOOL_PROP_ALTROOT:
307 case ZPOOL_PROP_CACHEFILE:
308 case ZPOOL_PROP_COMMENT:
309 case ZPOOL_PROP_COMPATIBILITY:
310 if (zhp->zpool_props != NULL ||
311 zpool_get_all_props(zhp) == 0) {
313 zpool_get_prop_string(zhp, prop, &src),
319 (void) strlcpy(buf, "-", len);
328 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
329 prop != ZPOOL_PROP_NAME)
332 switch (zpool_prop_get_type(prop)) {
333 case PROP_TYPE_STRING:
334 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
338 case PROP_TYPE_NUMBER:
339 intval = zpool_get_prop_int(zhp, prop, &src);
342 case ZPOOL_PROP_SIZE:
343 case ZPOOL_PROP_ALLOCATED:
344 case ZPOOL_PROP_FREE:
345 case ZPOOL_PROP_FREEING:
346 case ZPOOL_PROP_LEAKED:
347 case ZPOOL_PROP_ASHIFT:
349 (void) snprintf(buf, len, "%llu",
350 (u_longlong_t)intval);
352 (void) zfs_nicenum(intval, buf, len);
355 case ZPOOL_PROP_EXPANDSZ:
356 case ZPOOL_PROP_CHECKPOINT:
358 (void) strlcpy(buf, "-", len);
359 } else if (literal) {
360 (void) snprintf(buf, len, "%llu",
361 (u_longlong_t)intval);
363 (void) zfs_nicebytes(intval, buf, len);
367 case ZPOOL_PROP_CAPACITY:
369 (void) snprintf(buf, len, "%llu",
370 (u_longlong_t)intval);
372 (void) snprintf(buf, len, "%llu%%",
373 (u_longlong_t)intval);
377 case ZPOOL_PROP_FRAGMENTATION:
378 if (intval == UINT64_MAX) {
379 (void) strlcpy(buf, "-", len);
380 } else if (literal) {
381 (void) snprintf(buf, len, "%llu",
382 (u_longlong_t)intval);
384 (void) snprintf(buf, len, "%llu%%",
385 (u_longlong_t)intval);
389 case ZPOOL_PROP_DEDUPRATIO:
391 (void) snprintf(buf, len, "%llu.%02llu",
392 (u_longlong_t)(intval / 100),
393 (u_longlong_t)(intval % 100));
395 (void) snprintf(buf, len, "%llu.%02llux",
396 (u_longlong_t)(intval / 100),
397 (u_longlong_t)(intval % 100));
400 case ZPOOL_PROP_HEALTH:
401 (void) strlcpy(buf, zpool_get_state_str(zhp), len);
403 case ZPOOL_PROP_VERSION:
404 if (intval >= SPA_VERSION_FEATURES) {
405 (void) snprintf(buf, len, "-");
410 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
414 case PROP_TYPE_INDEX:
415 intval = zpool_get_prop_int(zhp, prop, &src);
416 if (zpool_prop_index_to_string(prop, intval, &strval)
419 (void) strlcpy(buf, strval, len);
433 * Check if the bootfs name has the same pool name as it is set to.
434 * Assuming bootfs is a valid dataset name.
437 bootfs_name_valid(const char *pool, const char *bootfs)
439 int len = strlen(pool);
440 if (bootfs[0] == '\0')
443 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
446 if (strncmp(pool, bootfs, len) == 0 &&
447 (bootfs[len] == '/' || bootfs[len] == '\0'))
454 * Given an nvlist of zpool properties to be set, validate that they are
455 * correct, and parse any numeric properties (index, boolean, etc) if they are
456 * specified as strings.
459 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
460 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
468 struct stat64 statbuf;
472 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
473 (void) no_memory(hdl);
478 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
479 const char *propname = nvpair_name(elem);
481 prop = zpool_name_to_prop(propname);
482 if (prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname)) {
484 char *fname = strchr(propname, '@') + 1;
486 err = zfeature_lookup_name(fname, NULL);
488 ASSERT3U(err, ==, ENOENT);
489 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
490 "feature '%s' unsupported by kernel"),
492 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
496 if (nvpair_type(elem) != DATA_TYPE_STRING) {
497 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
498 "'%s' must be a string"), propname);
499 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
503 (void) nvpair_value_string(elem, &strval);
504 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0 &&
505 strcmp(strval, ZFS_FEATURE_DISABLED) != 0) {
506 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
507 "property '%s' can only be set to "
508 "'enabled' or 'disabled'"), propname);
509 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
514 strcmp(strval, ZFS_FEATURE_DISABLED) == 0) {
515 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
516 "property '%s' can only be set to "
517 "'disabled' at creation time"), propname);
518 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
522 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
523 (void) no_memory(hdl);
530 * Make sure this property is valid and applies to this type.
532 if (prop == ZPOOL_PROP_INVAL) {
533 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
534 "invalid property '%s'"), propname);
535 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
539 if (zpool_prop_readonly(prop)) {
540 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
541 "is readonly"), propname);
542 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
546 if (!flags.create && zpool_prop_setonce(prop)) {
547 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
548 "property '%s' can only be set at "
549 "creation time"), propname);
550 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
554 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
555 &strval, &intval, errbuf) != 0)
559 * Perform additional checking for specific properties.
562 case ZPOOL_PROP_VERSION:
563 if (intval < version ||
564 !SPA_VERSION_IS_SUPPORTED(intval)) {
565 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
566 "property '%s' number %llu is invalid."),
567 propname, (unsigned long long)intval);
568 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
573 case ZPOOL_PROP_ASHIFT:
575 (intval < ASHIFT_MIN || intval > ASHIFT_MAX)) {
576 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
577 "property '%s' number %llu is invalid, "
578 "only values between %" PRId32 " and %"
579 PRId32 " are allowed."),
580 propname, (unsigned long long)intval,
581 ASHIFT_MIN, ASHIFT_MAX);
582 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
587 case ZPOOL_PROP_BOOTFS:
588 if (flags.create || flags.import) {
589 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
590 "property '%s' cannot be set at creation "
591 "or import time"), propname);
592 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
596 if (version < SPA_VERSION_BOOTFS) {
597 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
598 "pool must be upgraded to support "
599 "'%s' property"), propname);
600 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
605 * bootfs property value has to be a dataset name and
606 * the dataset has to be in the same pool as it sets to.
608 if (!bootfs_name_valid(poolname, strval)) {
609 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
610 "is an invalid name"), strval);
611 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
615 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
616 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
617 "could not open pool '%s'"), poolname);
618 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
624 case ZPOOL_PROP_ALTROOT:
625 if (!flags.create && !flags.import) {
626 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
627 "property '%s' can only be set during pool "
628 "creation or import"), propname);
629 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
633 if (strval[0] != '/') {
634 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
635 "bad alternate root '%s'"), strval);
636 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
641 case ZPOOL_PROP_CACHEFILE:
642 if (strval[0] == '\0')
645 if (strcmp(strval, "none") == 0)
648 if (strval[0] != '/') {
649 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
650 "property '%s' must be empty, an "
651 "absolute path, or 'none'"), propname);
652 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
656 slash = strrchr(strval, '/');
658 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
659 strcmp(slash, "/..") == 0) {
660 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
661 "'%s' is not a valid file"), strval);
662 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
668 if (strval[0] != '\0' &&
669 (stat64(strval, &statbuf) != 0 ||
670 !S_ISDIR(statbuf.st_mode))) {
671 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
672 "'%s' is not a valid directory"),
674 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
681 case ZPOOL_PROP_COMPATIBILITY:
682 switch (zpool_load_compat(strval, NULL, report, 1024)) {
683 case ZPOOL_COMPATIBILITY_OK:
684 case ZPOOL_COMPATIBILITY_WARNTOKEN:
686 case ZPOOL_COMPATIBILITY_BADFILE:
687 case ZPOOL_COMPATIBILITY_BADTOKEN:
688 case ZPOOL_COMPATIBILITY_NOFILES:
689 zfs_error_aux(hdl, "%s", report);
690 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
695 case ZPOOL_PROP_COMMENT:
696 for (check = strval; *check != '\0'; check++) {
697 if (!isprint(*check)) {
699 dgettext(TEXT_DOMAIN,
700 "comment may only have printable "
702 (void) zfs_error(hdl, EZFS_BADPROP,
707 if (strlen(strval) > ZPROP_MAX_COMMENT) {
708 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
709 "comment must not exceed %d characters"),
711 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
715 case ZPOOL_PROP_READONLY:
717 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
718 "property '%s' can only be set at "
719 "import time"), propname);
720 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
724 case ZPOOL_PROP_MULTIHOST:
725 if (get_system_hostid() == 0) {
726 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
727 "requires a non-zero system hostid"));
728 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
732 case ZPOOL_PROP_DEDUPDITTO:
733 printf("Note: property '%s' no longer has "
734 "any effect\n", propname);
744 nvlist_free(retprops);
749 * Set zpool property : propname=propval.
752 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
754 zfs_cmd_t zc = {"\0"};
757 nvlist_t *nvl = NULL;
760 prop_flags_t flags = { 0 };
762 (void) snprintf(errbuf, sizeof (errbuf),
763 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
766 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
767 return (no_memory(zhp->zpool_hdl));
769 if (nvlist_add_string(nvl, propname, propval) != 0) {
771 return (no_memory(zhp->zpool_hdl));
774 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
775 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
776 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
785 * Execute the corresponding ioctl() to set this property.
787 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
789 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
794 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
796 zcmd_free_nvlists(&zc);
800 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
802 (void) zpool_props_refresh(zhp);
808 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp,
811 libzfs_handle_t *hdl = zhp->zpool_hdl;
813 char buf[ZFS_MAXPROPLEN];
814 nvlist_t *features = NULL;
817 boolean_t firstexpand = (NULL == *plp);
820 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
824 while (*last != NULL)
825 last = &(*last)->pl_next;
828 features = zpool_get_features(zhp);
830 if ((*plp)->pl_all && firstexpand) {
831 for (i = 0; i < SPA_FEATURES; i++) {
832 zprop_list_t *entry = zfs_alloc(hdl,
833 sizeof (zprop_list_t));
834 entry->pl_prop = ZPROP_INVAL;
835 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
836 spa_feature_table[i].fi_uname);
837 entry->pl_width = strlen(entry->pl_user_prop);
838 entry->pl_all = B_TRUE;
841 last = &entry->pl_next;
845 /* add any unsupported features */
846 for (nvp = nvlist_next_nvpair(features, NULL);
847 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
852 if (zfeature_is_supported(nvpair_name(nvp)))
855 propname = zfs_asprintf(hdl, "unsupported@%s",
859 * Before adding the property to the list make sure that no
860 * other pool already added the same property.
864 while (entry != NULL) {
865 if (entry->pl_user_prop != NULL &&
866 strcmp(propname, entry->pl_user_prop) == 0) {
870 entry = entry->pl_next;
877 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
878 entry->pl_prop = ZPROP_INVAL;
879 entry->pl_user_prop = propname;
880 entry->pl_width = strlen(entry->pl_user_prop);
881 entry->pl_all = B_TRUE;
884 last = &entry->pl_next;
887 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
888 if (entry->pl_fixed && !literal)
891 if (entry->pl_prop != ZPROP_INVAL &&
892 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
893 NULL, literal) == 0) {
894 if (strlen(buf) > entry->pl_width)
895 entry->pl_width = strlen(buf);
903 * Get the state for the given feature on the given ZFS pool.
906 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
910 boolean_t found = B_FALSE;
911 nvlist_t *features = zpool_get_features(zhp);
913 const char *feature = strchr(propname, '@') + 1;
915 supported = zpool_prop_feature(propname);
916 ASSERT(supported || zpool_prop_unsupported(propname));
919 * Convert from feature name to feature guid. This conversion is
920 * unnecessary for unsupported@... properties because they already
927 ret = zfeature_lookup_name(feature, &fid);
929 (void) strlcpy(buf, "-", len);
932 feature = spa_feature_table[fid].fi_guid;
935 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
940 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
943 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
945 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
950 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
952 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
955 (void) strlcpy(buf, "-", len);
964 * Validate the given pool name, optionally putting an extended error message in
968 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
974 ret = pool_namecheck(pool, &why, &what);
977 * The rules for reserved pool names were extended at a later point.
978 * But we need to support users with existing pools that may now be
979 * invalid. So we only check for this expanded set of names during a
980 * create (or import), and only in userland.
982 if (ret == 0 && !isopen &&
983 (strncmp(pool, "mirror", 6) == 0 ||
984 strncmp(pool, "raidz", 5) == 0 ||
985 strncmp(pool, "draid", 5) == 0 ||
986 strncmp(pool, "spare", 5) == 0 ||
987 strcmp(pool, "log") == 0)) {
990 dgettext(TEXT_DOMAIN, "name is reserved"));
998 case NAME_ERR_TOOLONG:
1000 dgettext(TEXT_DOMAIN, "name is too long"));
1003 case NAME_ERR_INVALCHAR:
1005 dgettext(TEXT_DOMAIN, "invalid character "
1006 "'%c' in pool name"), what);
1009 case NAME_ERR_NOLETTER:
1010 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1011 "name must begin with a letter"));
1014 case NAME_ERR_RESERVED:
1015 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1016 "name is reserved"));
1019 case NAME_ERR_DISKLIKE:
1020 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1021 "pool name is reserved"));
1024 case NAME_ERR_LEADING_SLASH:
1025 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1026 "leading slash in name"));
1029 case NAME_ERR_EMPTY_COMPONENT:
1030 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1031 "empty component in name"));
1034 case NAME_ERR_TRAILING_SLASH:
1035 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1036 "trailing slash in name"));
1039 case NAME_ERR_MULTIPLE_DELIMITERS:
1040 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1041 "multiple '@' and/or '#' delimiters in "
1045 case NAME_ERR_NO_AT:
1046 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1047 "permission set is missing '@'"));
1051 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1052 "(%d) not defined"), why);
1063 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1067 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
1069 zpool_handle_t *zhp;
1073 * Make sure the pool name is valid.
1075 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
1076 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1077 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1082 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1085 zhp->zpool_hdl = hdl;
1086 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1088 if (zpool_refresh_stats(zhp, &missing) != 0) {
1094 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1095 (void) zfs_error_fmt(hdl, EZFS_NOENT,
1096 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1105 * Like the above, but silent on error. Used when iterating over pools (because
1106 * the configuration cache may be out of date).
1109 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1111 zpool_handle_t *zhp;
1114 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1117 zhp->zpool_hdl = hdl;
1118 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1120 if (zpool_refresh_stats(zhp, &missing) != 0) {
1136 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1140 zpool_open(libzfs_handle_t *hdl, const char *pool)
1142 zpool_handle_t *zhp;
1144 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1147 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1148 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1149 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1158 * Close the handle. Simply frees the memory associated with the handle.
1161 zpool_close(zpool_handle_t *zhp)
1163 nvlist_free(zhp->zpool_config);
1164 nvlist_free(zhp->zpool_old_config);
1165 nvlist_free(zhp->zpool_props);
1170 * Return the name of the pool.
1173 zpool_get_name(zpool_handle_t *zhp)
1175 return (zhp->zpool_name);
1180 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1183 zpool_get_state(zpool_handle_t *zhp)
1185 return (zhp->zpool_state);
1189 * Check if vdev list contains a special vdev
1192 zpool_has_special_vdev(nvlist_t *nvroot)
1197 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, &child,
1199 for (uint_t c = 0; c < children; c++) {
1202 if (nvlist_lookup_string(child[c],
1203 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias) == 0 &&
1204 strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0) {
1213 * Check if vdev list contains a dRAID vdev
1216 zpool_has_draid_vdev(nvlist_t *nvroot)
1221 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1222 &child, &children) == 0) {
1223 for (uint_t c = 0; c < children; c++) {
1226 if (nvlist_lookup_string(child[c],
1227 ZPOOL_CONFIG_TYPE, &type) == 0 &&
1228 strcmp(type, VDEV_TYPE_DRAID) == 0) {
1237 * Output a dRAID top-level vdev name in to the provided buffer.
1240 zpool_draid_name(char *name, int len, uint64_t data, uint64_t parity,
1241 uint64_t spares, uint64_t children)
1243 snprintf(name, len, "%s%llu:%llud:%lluc:%llus",
1244 VDEV_TYPE_DRAID, (u_longlong_t)parity, (u_longlong_t)data,
1245 (u_longlong_t)children, (u_longlong_t)spares);
1251 * Return B_TRUE if the provided name is a dRAID spare name.
1254 zpool_is_draid_spare(const char *name)
1256 uint64_t spare_id, parity, vdev_id;
1258 if (sscanf(name, VDEV_TYPE_DRAID "%llu-%llu-%llu",
1259 (u_longlong_t *)&parity, (u_longlong_t *)&vdev_id,
1260 (u_longlong_t *)&spare_id) == 3) {
1268 * Create the named pool, using the provided vdev list. It is assumed
1269 * that the consumer has already validated the contents of the nvlist, so we
1270 * don't have to worry about error semantics.
1273 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1274 nvlist_t *props, nvlist_t *fsprops)
1276 zfs_cmd_t zc = {"\0"};
1277 nvlist_t *zc_fsprops = NULL;
1278 nvlist_t *zc_props = NULL;
1279 nvlist_t *hidden_args = NULL;
1280 uint8_t *wkeydata = NULL;
1285 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1286 "cannot create '%s'"), pool);
1288 if (!zpool_name_valid(hdl, B_FALSE, pool))
1289 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1291 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1295 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1297 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1298 SPA_VERSION_1, flags, msg)) == NULL) {
1307 zoned = ((nvlist_lookup_string(fsprops,
1308 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1309 strcmp(zonestr, "on") == 0);
1311 if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
1312 fsprops, zoned, NULL, NULL, B_TRUE, msg)) == NULL) {
1316 if (nvlist_exists(zc_fsprops,
1317 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS)) &&
1318 !zpool_has_special_vdev(nvroot)) {
1319 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1320 "%s property requires a special vdev"),
1321 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS));
1322 (void) zfs_error(hdl, EZFS_BADPROP, msg);
1327 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1330 if (zfs_crypto_create(hdl, NULL, zc_fsprops, props, B_TRUE,
1331 &wkeydata, &wkeylen) != 0) {
1332 zfs_error(hdl, EZFS_CRYPTOFAILED, msg);
1335 if (nvlist_add_nvlist(zc_props,
1336 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1339 if (wkeydata != NULL) {
1340 if (nvlist_alloc(&hidden_args, NV_UNIQUE_NAME, 0) != 0)
1343 if (nvlist_add_uint8_array(hidden_args, "wkeydata",
1344 wkeydata, wkeylen) != 0)
1347 if (nvlist_add_nvlist(zc_props, ZPOOL_HIDDEN_ARGS,
1353 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1356 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1358 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1360 zcmd_free_nvlists(&zc);
1361 nvlist_free(zc_props);
1362 nvlist_free(zc_fsprops);
1363 nvlist_free(hidden_args);
1364 if (wkeydata != NULL)
1370 * This can happen if the user has specified the same
1371 * device multiple times. We can't reliably detect this
1372 * until we try to add it and see we already have a
1373 * label. This can also happen under if the device is
1374 * part of an active md or lvm device.
1376 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1377 "one or more vdevs refer to the same device, or "
1378 "one of\nthe devices is part of an active md or "
1380 return (zfs_error(hdl, EZFS_BADDEV, msg));
1384 * This happens if the record size is smaller or larger
1385 * than the allowed size range, or not a power of 2.
1387 * NOTE: although zfs_valid_proplist is called earlier,
1388 * this case may have slipped through since the
1389 * pool does not exist yet and it is therefore
1390 * impossible to read properties e.g. max blocksize
1393 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1394 "record size invalid"));
1395 return (zfs_error(hdl, EZFS_BADPROP, msg));
1399 * This occurs when one of the devices is below
1400 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1401 * device was the problem device since there's no
1402 * reliable way to determine device size from userland.
1407 zfs_nicebytes(SPA_MINDEVSIZE, buf,
1410 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1411 "one or more devices is less than the "
1412 "minimum size (%s)"), buf);
1414 return (zfs_error(hdl, EZFS_BADDEV, msg));
1417 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1418 "one or more devices is out of space"));
1419 return (zfs_error(hdl, EZFS_BADDEV, msg));
1422 if (zpool_has_draid_vdev(nvroot) &&
1423 zfeature_lookup_name("draid", NULL) != 0) {
1424 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1425 "dRAID vdevs are unsupported by the "
1427 return (zfs_error(hdl, EZFS_BADDEV, msg));
1429 return (zpool_standard_error(hdl, errno, msg));
1433 return (zpool_standard_error(hdl, errno, msg));
1438 zcmd_free_nvlists(&zc);
1439 nvlist_free(zc_props);
1440 nvlist_free(zc_fsprops);
1441 nvlist_free(hidden_args);
1442 if (wkeydata != NULL)
1448 * Destroy the given pool. It is up to the caller to ensure that there are no
1449 * datasets left in the pool.
1452 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1454 zfs_cmd_t zc = {"\0"};
1455 zfs_handle_t *zfp = NULL;
1456 libzfs_handle_t *hdl = zhp->zpool_hdl;
1459 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1460 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1463 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1464 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1466 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1467 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1468 "cannot destroy '%s'"), zhp->zpool_name);
1470 if (errno == EROFS) {
1471 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1472 "one or more devices is read only"));
1473 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1475 (void) zpool_standard_error(hdl, errno, msg);
1484 remove_mountpoint(zfp);
1492 * Create a checkpoint in the given pool.
1495 zpool_checkpoint(zpool_handle_t *zhp)
1497 libzfs_handle_t *hdl = zhp->zpool_hdl;
1501 error = lzc_pool_checkpoint(zhp->zpool_name);
1503 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1504 "cannot checkpoint '%s'"), zhp->zpool_name);
1505 (void) zpool_standard_error(hdl, error, msg);
1513 * Discard the checkpoint from the given pool.
1516 zpool_discard_checkpoint(zpool_handle_t *zhp)
1518 libzfs_handle_t *hdl = zhp->zpool_hdl;
1522 error = lzc_pool_checkpoint_discard(zhp->zpool_name);
1524 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1525 "cannot discard checkpoint in '%s'"), zhp->zpool_name);
1526 (void) zpool_standard_error(hdl, error, msg);
1534 * Add the given vdevs to the pool. The caller must have already performed the
1535 * necessary verification to ensure that the vdev specification is well-formed.
1538 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1540 zfs_cmd_t zc = {"\0"};
1542 libzfs_handle_t *hdl = zhp->zpool_hdl;
1544 nvlist_t **spares, **l2cache;
1545 uint_t nspares, nl2cache;
1547 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1548 "cannot add to '%s'"), zhp->zpool_name);
1550 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1551 SPA_VERSION_SPARES &&
1552 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1553 &spares, &nspares) == 0) {
1554 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1555 "upgraded to add hot spares"));
1556 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1559 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1560 SPA_VERSION_L2CACHE &&
1561 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1562 &l2cache, &nl2cache) == 0) {
1563 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1564 "upgraded to add cache devices"));
1565 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1568 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1570 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1572 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1576 * This can happen if the user has specified the same
1577 * device multiple times. We can't reliably detect this
1578 * until we try to add it and see we already have a
1581 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1582 "one or more vdevs refer to the same device"));
1583 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1588 if (zpool_has_draid_vdev(nvroot) &&
1589 zfeature_lookup_name("draid", NULL) != 0) {
1590 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1591 "dRAID vdevs are unsupported by the "
1594 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1595 "invalid config; a pool with removing/"
1596 "removed vdevs does not support adding "
1597 "raidz or dRAID vdevs"));
1600 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1605 * This occurs when one of the devices is below
1606 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1607 * device was the problem device since there's no
1608 * reliable way to determine device size from userland.
1613 zfs_nicebytes(SPA_MINDEVSIZE, buf,
1616 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1617 "device is less than the minimum "
1620 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1624 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1625 "pool must be upgraded to add these vdevs"));
1626 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1630 (void) zpool_standard_error(hdl, errno, msg);
1638 zcmd_free_nvlists(&zc);
1644 * Exports the pool from the system. The caller must ensure that there are no
1645 * mounted datasets in the pool.
1648 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1649 const char *log_str)
1651 zfs_cmd_t zc = {"\0"};
1653 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1654 zc.zc_cookie = force;
1655 zc.zc_guid = hardforce;
1656 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1658 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1661 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1662 "use '-f' to override the following errors:\n"
1663 "'%s' has an active shared spare which could be"
1664 " used by other pools once '%s' is exported."),
1665 zhp->zpool_name, zhp->zpool_name);
1666 return (zfs_error_fmt(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1667 dgettext(TEXT_DOMAIN, "cannot export '%s'"),
1670 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1671 dgettext(TEXT_DOMAIN, "cannot export '%s'"),
1680 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1682 return (zpool_export_common(zhp, force, B_FALSE, log_str));
1686 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1688 return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1692 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1695 nvlist_t *nv = NULL;
1701 if (!hdl->libzfs_printerr || config == NULL)
1704 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1705 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1709 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1711 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1713 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1714 strftime(timestr, 128, "%c", &t) != 0) {
1716 (void) printf(dgettext(TEXT_DOMAIN,
1717 "Would be able to return %s "
1718 "to its state as of %s.\n"),
1721 (void) printf(dgettext(TEXT_DOMAIN,
1722 "Pool %s returned to its state as of %s.\n"),
1726 (void) printf(dgettext(TEXT_DOMAIN,
1727 "%s approximately %lld "),
1728 dryrun ? "Would discard" : "Discarded",
1729 ((longlong_t)loss + 30) / 60);
1730 (void) printf(dgettext(TEXT_DOMAIN,
1731 "minutes of transactions.\n"));
1732 } else if (loss > 0) {
1733 (void) printf(dgettext(TEXT_DOMAIN,
1734 "%s approximately %lld "),
1735 dryrun ? "Would discard" : "Discarded",
1737 (void) printf(dgettext(TEXT_DOMAIN,
1738 "seconds of transactions.\n"));
1744 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1747 nvlist_t *nv = NULL;
1749 uint64_t edata = UINT64_MAX;
1754 if (!hdl->libzfs_printerr)
1758 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1760 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1762 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1763 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1764 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1765 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1768 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1769 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1772 (void) printf(dgettext(TEXT_DOMAIN,
1773 "Recovery is possible, but will result in some data loss.\n"));
1775 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1776 strftime(timestr, 128, "%c", &t) != 0) {
1777 (void) printf(dgettext(TEXT_DOMAIN,
1778 "\tReturning the pool to its state as of %s\n"
1779 "\tshould correct the problem. "),
1782 (void) printf(dgettext(TEXT_DOMAIN,
1783 "\tReverting the pool to an earlier state "
1784 "should correct the problem.\n\t"));
1788 (void) printf(dgettext(TEXT_DOMAIN,
1789 "Approximately %lld minutes of data\n"
1790 "\tmust be discarded, irreversibly. "),
1791 ((longlong_t)loss + 30) / 60);
1792 } else if (loss > 0) {
1793 (void) printf(dgettext(TEXT_DOMAIN,
1794 "Approximately %lld seconds of data\n"
1795 "\tmust be discarded, irreversibly. "),
1798 if (edata != 0 && edata != UINT64_MAX) {
1800 (void) printf(dgettext(TEXT_DOMAIN,
1801 "After rewind, at least\n"
1802 "\tone persistent user-data error will remain. "));
1804 (void) printf(dgettext(TEXT_DOMAIN,
1805 "After rewind, several\n"
1806 "\tpersistent user-data errors will remain. "));
1809 (void) printf(dgettext(TEXT_DOMAIN,
1810 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1811 reason >= 0 ? "clear" : "import", name);
1813 (void) printf(dgettext(TEXT_DOMAIN,
1814 "A scrub of the pool\n"
1815 "\tis strongly recommended after recovery.\n"));
1819 (void) printf(dgettext(TEXT_DOMAIN,
1820 "Destroy and re-create the pool from\n\ta backup source.\n"));
1824 * zpool_import() is a contracted interface. Should be kept the same
1827 * Applications should use zpool_import_props() to import a pool with
1828 * new properties value to be set.
1831 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1834 nvlist_t *props = NULL;
1837 if (altroot != NULL) {
1838 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1839 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1840 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1844 if (nvlist_add_string(props,
1845 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1846 nvlist_add_string(props,
1847 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1849 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1850 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1855 ret = zpool_import_props(hdl, config, newname, props,
1862 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1868 uint64_t is_log = 0;
1870 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1874 (void) printf("\t%*s%s%s\n", indent, "", name,
1875 is_log ? " [log]" : "");
1877 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1878 &child, &children) != 0)
1881 for (c = 0; c < children; c++) {
1882 vname = zpool_vdev_name(hdl, NULL, child[c], VDEV_NAME_TYPE_ID);
1883 print_vdev_tree(hdl, vname, child[c], indent + 2);
1889 zpool_print_unsup_feat(nvlist_t *config)
1891 nvlist_t *nvinfo, *unsup_feat;
1894 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1896 verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1899 for (nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1900 nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1903 verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1904 verify(nvpair_value_string(nvp, &desc) == 0);
1906 if (strlen(desc) > 0)
1907 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1909 (void) printf("\t%s\n", nvpair_name(nvp));
1914 * Import the given pool using the known configuration and a list of
1915 * properties to be set. The configuration should have come from
1916 * zpool_find_import(). The 'newname' parameters control whether the pool
1917 * is imported with a different name.
1920 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1921 nvlist_t *props, int flags)
1923 zfs_cmd_t zc = {"\0"};
1924 zpool_load_policy_t policy;
1925 nvlist_t *nv = NULL;
1926 nvlist_t *nvinfo = NULL;
1927 nvlist_t *missing = NULL;
1934 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1937 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1938 "cannot import pool '%s'"), origname);
1940 if (newname != NULL) {
1941 if (!zpool_name_valid(hdl, B_FALSE, newname))
1942 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1943 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1945 thename = (char *)newname;
1950 if (props != NULL) {
1952 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1954 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1957 if ((props = zpool_valid_proplist(hdl, origname,
1958 props, version, flags, errbuf)) == NULL)
1960 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1967 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1969 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1972 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1973 zcmd_free_nvlists(&zc);
1976 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1977 zcmd_free_nvlists(&zc);
1981 zc.zc_cookie = flags;
1982 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1984 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1985 zcmd_free_nvlists(&zc);
1992 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1994 zcmd_free_nvlists(&zc);
1996 zpool_get_load_policy(config, &policy);
2003 * Dry-run failed, but we print out what success
2004 * looks like if we found a best txg
2006 if (policy.zlp_rewind & ZPOOL_TRY_REWIND) {
2007 zpool_rewind_exclaim(hdl, newname ? origname : thename,
2013 if (newname == NULL)
2014 (void) snprintf(desc, sizeof (desc),
2015 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
2018 (void) snprintf(desc, sizeof (desc),
2019 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
2024 if (nv != NULL && nvlist_lookup_nvlist(nv,
2025 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
2026 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
2027 (void) printf(dgettext(TEXT_DOMAIN, "This "
2028 "pool uses the following feature(s) not "
2029 "supported by this system:\n"));
2030 zpool_print_unsup_feat(nv);
2031 if (nvlist_exists(nvinfo,
2032 ZPOOL_CONFIG_CAN_RDONLY)) {
2033 (void) printf(dgettext(TEXT_DOMAIN,
2034 "All unsupported features are only "
2035 "required for writing to the pool."
2036 "\nThe pool can be imported using "
2037 "'-o readonly=on'.\n"));
2041 * Unsupported version.
2043 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
2047 if (nv != NULL && nvlist_lookup_nvlist(nv,
2048 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0) {
2049 char *hostname = "<unknown>";
2050 uint64_t hostid = 0;
2051 mmp_state_t mmp_state;
2053 mmp_state = fnvlist_lookup_uint64(nvinfo,
2054 ZPOOL_CONFIG_MMP_STATE);
2056 if (nvlist_exists(nvinfo,
2057 ZPOOL_CONFIG_MMP_HOSTNAME))
2058 hostname = fnvlist_lookup_string(nvinfo,
2059 ZPOOL_CONFIG_MMP_HOSTNAME);
2061 if (nvlist_exists(nvinfo,
2062 ZPOOL_CONFIG_MMP_HOSTID))
2063 hostid = fnvlist_lookup_uint64(nvinfo,
2064 ZPOOL_CONFIG_MMP_HOSTID);
2066 if (mmp_state == MMP_STATE_ACTIVE) {
2067 (void) snprintf(aux, sizeof (aux),
2068 dgettext(TEXT_DOMAIN, "pool is imp"
2069 "orted on host '%s' (hostid=%lx).\n"
2070 "Export the pool on the other "
2071 "system, then run 'zpool import'."),
2072 hostname, (unsigned long) hostid);
2073 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
2074 (void) snprintf(aux, sizeof (aux),
2075 dgettext(TEXT_DOMAIN, "pool has "
2076 "the multihost property on and "
2077 "the\nsystem's hostid is not set. "
2078 "Set a unique system hostid with "
2079 "the zgenhostid(8) command.\n"));
2082 (void) zfs_error_aux(hdl, "%s", aux);
2084 (void) zfs_error(hdl, EZFS_ACTIVE_POOL, desc);
2088 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
2092 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2093 "one or more devices is read only"));
2094 (void) zfs_error(hdl, EZFS_BADDEV, desc);
2098 if (nv && nvlist_lookup_nvlist(nv,
2099 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
2100 nvlist_lookup_nvlist(nvinfo,
2101 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
2102 (void) printf(dgettext(TEXT_DOMAIN,
2103 "The devices below are missing or "
2104 "corrupted, use '-m' to import the pool "
2106 print_vdev_tree(hdl, NULL, missing, 2);
2107 (void) printf("\n");
2109 (void) zpool_standard_error(hdl, error, desc);
2113 (void) zpool_standard_error(hdl, error, desc);
2117 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2118 "one or more devices are already in use\n"));
2119 (void) zfs_error(hdl, EZFS_BADDEV, desc);
2122 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2123 "new name of at least one dataset is longer than "
2124 "the maximum allowable length"));
2125 (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
2128 (void) zpool_standard_error(hdl, error, desc);
2129 zpool_explain_recover(hdl,
2130 newname ? origname : thename, -error, nv);
2137 zpool_handle_t *zhp;
2140 * This should never fail, but play it safe anyway.
2142 if (zpool_open_silent(hdl, thename, &zhp) != 0)
2144 else if (zhp != NULL)
2146 if (policy.zlp_rewind &
2147 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2148 zpool_rewind_exclaim(hdl, newname ? origname : thename,
2149 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0), nv);
2159 * Translate vdev names to guids. If a vdev_path is determined to be
2160 * unsuitable then a vd_errlist is allocated and the vdev path and errno
2164 zpool_translate_vdev_guids(zpool_handle_t *zhp, nvlist_t *vds,
2165 nvlist_t *vdev_guids, nvlist_t *guids_to_paths, nvlist_t **vd_errlist)
2167 nvlist_t *errlist = NULL;
2170 for (nvpair_t *elem = nvlist_next_nvpair(vds, NULL); elem != NULL;
2171 elem = nvlist_next_nvpair(vds, elem)) {
2172 boolean_t spare, cache;
2174 char *vd_path = nvpair_name(elem);
2175 nvlist_t *tgt = zpool_find_vdev(zhp, vd_path, &spare, &cache,
2178 if ((tgt == NULL) || cache || spare) {
2179 if (errlist == NULL) {
2180 errlist = fnvlist_alloc();
2184 uint64_t err = (tgt == NULL) ? EZFS_NODEVICE :
2185 (spare ? EZFS_ISSPARE : EZFS_ISL2CACHE);
2186 fnvlist_add_int64(errlist, vd_path, err);
2190 uint64_t guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
2191 fnvlist_add_uint64(vdev_guids, vd_path, guid);
2193 char msg[MAXNAMELEN];
2194 (void) snprintf(msg, sizeof (msg), "%llu", (u_longlong_t)guid);
2195 fnvlist_add_string(guids_to_paths, msg, vd_path);
2199 verify(errlist != NULL);
2200 if (vd_errlist != NULL)
2201 *vd_errlist = errlist;
2203 fnvlist_free(errlist);
2210 xlate_init_err(int err)
2214 return (EZFS_NODEVICE);
2217 return (EZFS_BADDEV);
2219 return (EZFS_INITIALIZING);
2221 return (EZFS_NO_INITIALIZE);
2227 * Begin, suspend, or cancel the initialization (initializing of all free
2228 * blocks) for the given vdevs in the given pool.
2231 zpool_initialize_impl(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2232 nvlist_t *vds, boolean_t wait)
2236 nvlist_t *vdev_guids = fnvlist_alloc();
2237 nvlist_t *guids_to_paths = fnvlist_alloc();
2238 nvlist_t *vd_errlist = NULL;
2242 err = zpool_translate_vdev_guids(zhp, vds, vdev_guids,
2243 guids_to_paths, &vd_errlist);
2246 verify(vd_errlist != NULL);
2250 err = lzc_initialize(zhp->zpool_name, cmd_type,
2251 vdev_guids, &errlist);
2254 if (errlist != NULL) {
2255 vd_errlist = fnvlist_lookup_nvlist(errlist,
2256 ZPOOL_INITIALIZE_VDEVS);
2259 (void) zpool_standard_error(zhp->zpool_hdl, err,
2260 dgettext(TEXT_DOMAIN, "operation failed"));
2265 for (elem = nvlist_next_nvpair(vdev_guids, NULL); elem != NULL;
2266 elem = nvlist_next_nvpair(vdev_guids, elem)) {
2268 uint64_t guid = fnvpair_value_uint64(elem);
2270 err = lzc_wait_tag(zhp->zpool_name,
2271 ZPOOL_WAIT_INITIALIZE, guid, NULL);
2273 (void) zpool_standard_error_fmt(zhp->zpool_hdl,
2274 err, dgettext(TEXT_DOMAIN, "error "
2275 "waiting for '%s' to initialize"),
2285 for (elem = nvlist_next_nvpair(vd_errlist, NULL); elem != NULL;
2286 elem = nvlist_next_nvpair(vd_errlist, elem)) {
2287 int64_t vd_error = xlate_init_err(fnvpair_value_int64(elem));
2290 if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem),
2292 path = nvpair_name(elem);
2294 (void) zfs_error_fmt(zhp->zpool_hdl, vd_error,
2295 "cannot initialize '%s'", path);
2299 fnvlist_free(vdev_guids);
2300 fnvlist_free(guids_to_paths);
2302 if (vd_errlist != NULL)
2303 fnvlist_free(vd_errlist);
2305 return (err == 0 ? 0 : -1);
2309 zpool_initialize(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2312 return (zpool_initialize_impl(zhp, cmd_type, vds, B_FALSE));
2316 zpool_initialize_wait(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2319 return (zpool_initialize_impl(zhp, cmd_type, vds, B_TRUE));
2323 xlate_trim_err(int err)
2327 return (EZFS_NODEVICE);
2330 return (EZFS_BADDEV);
2332 return (EZFS_TRIMMING);
2334 return (EZFS_NO_TRIM);
2336 return (EZFS_TRIM_NOTSUP);
2342 zpool_trim_wait(zpool_handle_t *zhp, nvlist_t *vdev_guids)
2347 for (elem = nvlist_next_nvpair(vdev_guids, NULL); elem != NULL;
2348 elem = nvlist_next_nvpair(vdev_guids, elem)) {
2350 uint64_t guid = fnvpair_value_uint64(elem);
2352 err = lzc_wait_tag(zhp->zpool_name,
2353 ZPOOL_WAIT_TRIM, guid, NULL);
2355 (void) zpool_standard_error_fmt(zhp->zpool_hdl,
2356 err, dgettext(TEXT_DOMAIN, "error "
2357 "waiting to trim '%s'"), nvpair_name(elem));
2366 * Check errlist and report any errors, omitting ones which should be
2367 * suppressed. Returns B_TRUE if any errors were reported.
2370 check_trim_errs(zpool_handle_t *zhp, trimflags_t *trim_flags,
2371 nvlist_t *guids_to_paths, nvlist_t *vds, nvlist_t *errlist)
2374 boolean_t reported_errs = B_FALSE;
2376 int num_suppressed_errs = 0;
2378 for (elem = nvlist_next_nvpair(vds, NULL);
2379 elem != NULL; elem = nvlist_next_nvpair(vds, elem)) {
2383 for (elem = nvlist_next_nvpair(errlist, NULL);
2384 elem != NULL; elem = nvlist_next_nvpair(errlist, elem)) {
2385 int64_t vd_error = xlate_trim_err(fnvpair_value_int64(elem));
2389 * If only the pool was specified, and it was not a secure
2390 * trim then suppress warnings for individual vdevs which
2391 * do not support trimming.
2393 if (vd_error == EZFS_TRIM_NOTSUP &&
2394 trim_flags->fullpool &&
2395 !trim_flags->secure) {
2396 num_suppressed_errs++;
2400 reported_errs = B_TRUE;
2401 if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem),
2403 path = nvpair_name(elem);
2405 (void) zfs_error_fmt(zhp->zpool_hdl, vd_error,
2406 "cannot trim '%s'", path);
2409 if (num_suppressed_errs == num_vds) {
2410 (void) zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
2411 "no devices in pool support trim operations"));
2412 (void) (zfs_error(zhp->zpool_hdl, EZFS_TRIM_NOTSUP,
2413 dgettext(TEXT_DOMAIN, "cannot trim")));
2414 reported_errs = B_TRUE;
2417 return (reported_errs);
2421 * Begin, suspend, or cancel the TRIM (discarding of all free blocks) for
2422 * the given vdevs in the given pool.
2425 zpool_trim(zpool_handle_t *zhp, pool_trim_func_t cmd_type, nvlist_t *vds,
2426 trimflags_t *trim_flags)
2431 nvlist_t *vdev_guids = fnvlist_alloc();
2432 nvlist_t *guids_to_paths = fnvlist_alloc();
2433 nvlist_t *errlist = NULL;
2435 err = zpool_translate_vdev_guids(zhp, vds, vdev_guids,
2436 guids_to_paths, &errlist);
2438 check_trim_errs(zhp, trim_flags, guids_to_paths, vds, errlist);
2443 err = lzc_trim(zhp->zpool_name, cmd_type, trim_flags->rate,
2444 trim_flags->secure, vdev_guids, &errlist);
2446 nvlist_t *vd_errlist;
2447 if (errlist != NULL && nvlist_lookup_nvlist(errlist,
2448 ZPOOL_TRIM_VDEVS, &vd_errlist) == 0) {
2449 if (check_trim_errs(zhp, trim_flags, guids_to_paths,
2457 (void) snprintf(msg, sizeof (msg),
2458 dgettext(TEXT_DOMAIN, "operation failed"));
2459 zpool_standard_error(zhp->zpool_hdl, err, msg);
2466 if (trim_flags->wait)
2467 retval = zpool_trim_wait(zhp, vdev_guids);
2470 if (errlist != NULL)
2471 fnvlist_free(errlist);
2472 fnvlist_free(vdev_guids);
2473 fnvlist_free(guids_to_paths);
2481 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd)
2483 zfs_cmd_t zc = {"\0"};
2486 libzfs_handle_t *hdl = zhp->zpool_hdl;
2488 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2489 zc.zc_cookie = func;
2492 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0)
2497 /* ECANCELED on a scrub means we resumed a paused scrub */
2498 if (err == ECANCELED && func == POOL_SCAN_SCRUB &&
2499 cmd == POOL_SCRUB_NORMAL)
2502 if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL)
2505 if (func == POOL_SCAN_SCRUB) {
2506 if (cmd == POOL_SCRUB_PAUSE) {
2507 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2508 "cannot pause scrubbing %s"), zc.zc_name);
2510 assert(cmd == POOL_SCRUB_NORMAL);
2511 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2512 "cannot scrub %s"), zc.zc_name);
2514 } else if (func == POOL_SCAN_RESILVER) {
2515 assert(cmd == POOL_SCRUB_NORMAL);
2516 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2517 "cannot restart resilver on %s"), zc.zc_name);
2518 } else if (func == POOL_SCAN_NONE) {
2519 (void) snprintf(msg, sizeof (msg),
2520 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
2523 assert(!"unexpected result");
2528 pool_scan_stat_t *ps = NULL;
2531 verify(nvlist_lookup_nvlist(zhp->zpool_config,
2532 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2533 (void) nvlist_lookup_uint64_array(nvroot,
2534 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
2535 if (ps && ps->pss_func == POOL_SCAN_SCRUB &&
2536 ps->pss_state == DSS_SCANNING) {
2537 if (cmd == POOL_SCRUB_PAUSE)
2538 return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg));
2540 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
2542 return (zfs_error(hdl, EZFS_RESILVERING, msg));
2544 } else if (err == ENOENT) {
2545 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
2546 } else if (err == ENOTSUP && func == POOL_SCAN_RESILVER) {
2547 return (zfs_error(hdl, EZFS_NO_RESILVER_DEFER, msg));
2549 return (zpool_standard_error(hdl, err, msg));
2554 * Find a vdev that matches the search criteria specified. We use the
2555 * the nvpair name to determine how we should look for the device.
2556 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2557 * spare; but FALSE if its an INUSE spare.
2560 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
2561 boolean_t *l2cache, boolean_t *log)
2568 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
2570 /* Nothing to look for */
2571 if (search == NULL || pair == NULL)
2574 /* Obtain the key we will use to search */
2575 srchkey = nvpair_name(pair);
2577 switch (nvpair_type(pair)) {
2578 case DATA_TYPE_UINT64:
2579 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
2580 uint64_t srchval, theguid;
2582 verify(nvpair_value_uint64(pair, &srchval) == 0);
2583 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2585 if (theguid == srchval)
2590 case DATA_TYPE_STRING: {
2591 char *srchval, *val;
2593 verify(nvpair_value_string(pair, &srchval) == 0);
2594 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2598 * Search for the requested value. Special cases:
2600 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2601 * "-part1", or "p1". The suffix is hidden from the user,
2602 * but included in the string, so this matches around it.
2603 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2604 * is used to check all possible expanded paths.
2605 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2607 * Otherwise, all other searches are simple string compares.
2609 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) {
2610 uint64_t wholedisk = 0;
2612 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2614 if (zfs_strcmp_pathname(srchval, val, wholedisk) == 0)
2617 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2618 char *type, *idx, *end, *p;
2619 uint64_t id, vdev_id;
2622 * Determine our vdev type, keeping in mind
2623 * that the srchval is composed of a type and
2624 * vdev id pair (i.e. mirror-4).
2626 if ((type = strdup(srchval)) == NULL)
2629 if ((p = strrchr(type, '-')) == NULL) {
2637 * If the types don't match then keep looking.
2639 if (strncmp(val, type, strlen(val)) != 0) {
2644 verify(zpool_vdev_is_interior(type));
2645 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2649 vdev_id = strtoull(idx, &end, 10);
2652 * If we are looking for a raidz and a parity is
2653 * specified, make sure it matches.
2655 int rzlen = strlen(VDEV_TYPE_RAIDZ);
2656 assert(rzlen == strlen(VDEV_TYPE_DRAID));
2657 int typlen = strlen(type);
2658 if ((strncmp(type, VDEV_TYPE_RAIDZ, rzlen) == 0 ||
2659 strncmp(type, VDEV_TYPE_DRAID, rzlen) == 0) &&
2661 uint64_t vdev_parity;
2662 int parity = *(type + rzlen) - '0';
2664 if (parity <= 0 || parity > 3 ||
2665 (typlen - rzlen) != 1) {
2667 * Nonsense parity specified, can
2673 verify(nvlist_lookup_uint64(nv,
2674 ZPOOL_CONFIG_NPARITY, &vdev_parity) == 0);
2675 if ((int)vdev_parity != parity) {
2686 * Now verify that we have the correct vdev id.
2695 if (strcmp(srchval, val) == 0)
2704 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2705 &child, &children) != 0)
2708 for (c = 0; c < children; c++) {
2709 if ((ret = vdev_to_nvlist_iter(child[c], search,
2710 avail_spare, l2cache, NULL)) != NULL) {
2712 * The 'is_log' value is only set for the toplevel
2713 * vdev, not the leaf vdevs. So we always lookup the
2714 * log device from the root of the vdev tree (where
2715 * 'log' is non-NULL).
2718 nvlist_lookup_uint64(child[c],
2719 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2727 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2728 &child, &children) == 0) {
2729 for (c = 0; c < children; c++) {
2730 if ((ret = vdev_to_nvlist_iter(child[c], search,
2731 avail_spare, l2cache, NULL)) != NULL) {
2732 *avail_spare = B_TRUE;
2738 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2739 &child, &children) == 0) {
2740 for (c = 0; c < children; c++) {
2741 if ((ret = vdev_to_nvlist_iter(child[c], search,
2742 avail_spare, l2cache, NULL)) != NULL) {
2753 * Given a physical path or guid, find the associated vdev.
2756 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2757 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2759 nvlist_t *search, *nvroot, *ret;
2763 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2765 guid = strtoull(ppath, &end, 0);
2766 if (guid != 0 && *end == '\0') {
2767 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2769 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH,
2773 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2776 *avail_spare = B_FALSE;
2780 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2781 nvlist_free(search);
2787 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2790 zpool_vdev_is_interior(const char *name)
2792 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2793 strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 ||
2795 VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 ||
2796 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2799 if (strncmp(name, VDEV_TYPE_DRAID, strlen(VDEV_TYPE_DRAID)) == 0 &&
2800 !zpool_is_draid_spare(name))
2807 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2808 boolean_t *l2cache, boolean_t *log)
2811 nvlist_t *nvroot, *search, *ret;
2814 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2816 guid = strtoull(path, &end, 0);
2817 if (guid != 0 && *end == '\0') {
2818 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2819 } else if (zpool_vdev_is_interior(path)) {
2820 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2822 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2825 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2828 *avail_spare = B_FALSE;
2832 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2833 nvlist_free(search);
2839 vdev_is_online(nvlist_t *nv)
2843 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2844 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2845 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2852 * Helper function for zpool_get_physpaths().
2855 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2856 size_t *bytes_written)
2858 size_t bytes_left, pos, rsz;
2862 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2864 return (EZFS_NODEVICE);
2866 pos = *bytes_written;
2867 bytes_left = physpath_size - pos;
2868 format = (pos == 0) ? "%s" : " %s";
2870 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2871 *bytes_written += rsz;
2873 if (rsz >= bytes_left) {
2874 /* if physpath was not copied properly, clear it */
2875 if (bytes_left != 0) {
2878 return (EZFS_NOSPC);
2884 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2885 size_t *rsz, boolean_t is_spare)
2890 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2891 return (EZFS_INVALCONFIG);
2893 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2895 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2896 * For a spare vdev, we only want to boot from the active
2901 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2904 return (EZFS_INVALCONFIG);
2907 if (vdev_is_online(nv)) {
2908 if ((ret = vdev_get_one_physpath(nv, physpath,
2909 phypath_size, rsz)) != 0)
2912 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2913 strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
2914 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2915 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2920 if (nvlist_lookup_nvlist_array(nv,
2921 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2922 return (EZFS_INVALCONFIG);
2924 for (i = 0; i < count; i++) {
2925 ret = vdev_get_physpaths(child[i], physpath,
2926 phypath_size, rsz, is_spare);
2927 if (ret == EZFS_NOSPC)
2932 return (EZFS_POOL_INVALARG);
2936 * Get phys_path for a root pool config.
2937 * Return 0 on success; non-zero on failure.
2940 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2943 nvlist_t *vdev_root;
2950 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2952 return (EZFS_INVALCONFIG);
2954 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2955 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2956 &child, &count) != 0)
2957 return (EZFS_INVALCONFIG);
2960 * root pool can only have a single top-level vdev.
2962 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2963 return (EZFS_POOL_INVALARG);
2965 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2968 /* No online devices */
2970 return (EZFS_NODEVICE);
2976 * Get phys_path for a root pool
2977 * Return 0 on success; non-zero on failure.
2980 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2982 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2987 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
2989 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
2990 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
2994 zpool_vdev_path_to_guid_impl(zpool_handle_t *zhp, const char *path,
2995 boolean_t *is_spare, boolean_t *is_l2cache, boolean_t *is_log)
2998 boolean_t spare = B_FALSE, l2cache = B_FALSE, log = B_FALSE;
3001 if ((tgt = zpool_find_vdev(zhp, path, &spare, &l2cache,
3005 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &guid) == 0);
3006 if (is_spare != NULL)
3008 if (is_l2cache != NULL)
3009 *is_l2cache = l2cache;
3016 /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
3018 zpool_vdev_path_to_guid(zpool_handle_t *zhp, const char *path)
3020 return (zpool_vdev_path_to_guid_impl(zhp, path, NULL, NULL, NULL));
3024 * Bring the specified vdev online. The 'flags' parameter is a set of the
3025 * ZFS_ONLINE_* flags.
3028 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
3029 vdev_state_t *newstate)
3031 zfs_cmd_t zc = {"\0"};
3035 boolean_t avail_spare, l2cache, islog;
3036 libzfs_handle_t *hdl = zhp->zpool_hdl;
3039 if (flags & ZFS_ONLINE_EXPAND) {
3040 (void) snprintf(msg, sizeof (msg),
3041 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
3043 (void) snprintf(msg, sizeof (msg),
3044 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
3047 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3048 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3050 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3052 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3055 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3057 if ((flags & ZFS_ONLINE_EXPAND ||
3058 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) &&
3059 nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) {
3060 uint64_t wholedisk = 0;
3062 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
3066 * XXX - L2ARC 1.0 devices can't support expansion.
3069 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3070 "cannot expand cache devices"));
3071 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
3075 const char *fullpath = path;
3076 char buf[MAXPATHLEN];
3078 if (path[0] != '/') {
3079 error = zfs_resolve_shortname(path, buf,
3082 return (zfs_error(hdl, EZFS_NODEVICE,
3088 error = zpool_relabel_disk(hdl, fullpath, msg);
3094 zc.zc_cookie = VDEV_STATE_ONLINE;
3097 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
3098 if (errno == EINVAL) {
3099 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
3100 "from this pool into a new one. Use '%s' "
3101 "instead"), "zpool detach");
3102 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
3104 return (zpool_standard_error(hdl, errno, msg));
3107 *newstate = zc.zc_cookie;
3112 * Take the specified vdev offline
3115 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
3117 zfs_cmd_t zc = {"\0"};
3120 boolean_t avail_spare, l2cache;
3121 libzfs_handle_t *hdl = zhp->zpool_hdl;
3123 (void) snprintf(msg, sizeof (msg),
3124 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
3126 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3127 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3129 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3131 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3134 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3136 zc.zc_cookie = VDEV_STATE_OFFLINE;
3137 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
3139 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3146 * There are no other replicas of this device.
3148 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
3152 * The log device has unplayed logs
3154 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
3157 return (zpool_standard_error(hdl, errno, msg));
3162 * Mark the given vdev faulted.
3165 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
3167 zfs_cmd_t zc = {"\0"};
3169 libzfs_handle_t *hdl = zhp->zpool_hdl;
3171 (void) snprintf(msg, sizeof (msg),
3172 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
3174 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3176 zc.zc_cookie = VDEV_STATE_FAULTED;
3179 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3186 * There are no other replicas of this device.
3188 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
3191 return (zpool_standard_error(hdl, errno, msg));
3197 * Mark the given vdev degraded.
3200 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
3202 zfs_cmd_t zc = {"\0"};
3204 libzfs_handle_t *hdl = zhp->zpool_hdl;
3206 (void) snprintf(msg, sizeof (msg),
3207 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid);
3209 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3211 zc.zc_cookie = VDEV_STATE_DEGRADED;
3214 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3217 return (zpool_standard_error(hdl, errno, msg));
3221 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
3225 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
3231 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
3233 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
3236 if ((strcmp(type, VDEV_TYPE_SPARE) == 0 ||
3237 strcmp(type, VDEV_TYPE_DRAID_SPARE) == 0) &&
3238 children == 2 && child[which] == tgt)
3241 for (c = 0; c < children; c++)
3242 if (is_replacing_spare(child[c], tgt, which))
3250 * Attach new_disk (fully described by nvroot) to old_disk.
3251 * If 'replacing' is specified, the new disk will replace the old one.
3254 zpool_vdev_attach(zpool_handle_t *zhp, const char *old_disk,
3255 const char *new_disk, nvlist_t *nvroot, int replacing, boolean_t rebuild)
3257 zfs_cmd_t zc = {"\0"};
3261 boolean_t avail_spare, l2cache, islog;
3266 nvlist_t *config_root;
3267 libzfs_handle_t *hdl = zhp->zpool_hdl;
3270 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3271 "cannot replace %s with %s"), old_disk, new_disk);
3273 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3274 "cannot attach %s to %s"), new_disk, old_disk);
3276 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3277 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
3279 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3282 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3285 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
3287 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3288 zc.zc_cookie = replacing;
3289 zc.zc_simple = rebuild;
3292 zfeature_lookup_guid("org.openzfs:device_rebuild", NULL) != 0) {
3293 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3294 "the loaded zfs module doesn't support device rebuilds"));
3295 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
3298 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3299 &child, &children) != 0 || children != 1) {
3300 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3301 "new device must be a single disk"));
3302 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
3305 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
3306 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
3308 if ((newname = zpool_vdev_name(NULL, NULL, child[0], 0)) == NULL)
3312 * If the target is a hot spare that has been swapped in, we can only
3313 * replace it with another hot spare.
3316 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
3317 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
3318 NULL) == NULL || !avail_spare) &&
3319 is_replacing_spare(config_root, tgt, 1)) {
3320 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3321 "can only be replaced by another hot spare"));
3323 return (zfs_error(hdl, EZFS_BADTARGET, msg));
3328 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
3331 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
3333 zcmd_free_nvlists(&zc);
3341 * Can't attach to or replace this type of vdev.
3344 uint64_t version = zpool_get_prop_int(zhp,
3345 ZPOOL_PROP_VERSION, NULL);
3348 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3349 "cannot replace a log with a spare"));
3350 } else if (rebuild) {
3351 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3352 "only mirror and dRAID vdevs support "
3353 "sequential reconstruction"));
3354 } else if (zpool_is_draid_spare(new_disk)) {
3355 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3356 "dRAID spares can only replace child "
3357 "devices in their parent's dRAID vdev"));
3358 } else if (version >= SPA_VERSION_MULTI_REPLACE) {
3359 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3360 "already in replacing/spare config; wait "
3361 "for completion or use 'zpool detach'"));
3363 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3364 "cannot replace a replacing device"));
3367 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3368 "can only attach to mirrors and top-level "
3371 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
3376 * The new device must be a single disk.
3378 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3379 "new device must be a single disk"));
3380 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3384 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy, "
3385 "or device removal is in progress"),
3387 (void) zfs_error(hdl, EZFS_BADDEV, msg);
3392 * The new device is too small.
3394 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3395 "device is too small"));
3396 (void) zfs_error(hdl, EZFS_BADDEV, msg);
3401 * The new device has a different optimal sector size.
3403 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3404 "new device has a different optimal sector size; use the "
3405 "option '-o ashift=N' to override the optimal size"));
3406 (void) zfs_error(hdl, EZFS_BADDEV, msg);
3411 * The resulting top-level vdev spec won't fit in the label.
3413 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
3417 (void) zpool_standard_error(hdl, errno, msg);
3424 * Detach the specified device.
3427 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
3429 zfs_cmd_t zc = {"\0"};
3432 boolean_t avail_spare, l2cache;
3433 libzfs_handle_t *hdl = zhp->zpool_hdl;
3435 (void) snprintf(msg, sizeof (msg),
3436 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
3438 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3439 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3441 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3444 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3447 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
3449 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3451 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
3458 * Can't detach from this type of vdev.
3460 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
3461 "applicable to mirror and replacing vdevs"));
3462 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
3467 * There are no other replicas of this device.
3469 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
3473 (void) zpool_standard_error(hdl, errno, msg);
3480 * Find a mirror vdev in the source nvlist.
3482 * The mchild array contains a list of disks in one of the top-level mirrors
3483 * of the source pool. The schild array contains a list of disks that the
3484 * user specified on the command line. We loop over the mchild array to
3485 * see if any entry in the schild array matches.
3487 * If a disk in the mchild array is found in the schild array, we return
3488 * the index of that entry. Otherwise we return -1.
3491 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
3492 nvlist_t **schild, uint_t schildren)
3496 for (mc = 0; mc < mchildren; mc++) {
3498 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3501 for (sc = 0; sc < schildren; sc++) {
3502 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3504 boolean_t result = (strcmp(mpath, spath) == 0);
3520 * Split a mirror pool. If newroot points to null, then a new nvlist
3521 * is generated and it is the responsibility of the caller to free it.
3524 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
3525 nvlist_t *props, splitflags_t flags)
3527 zfs_cmd_t zc = {"\0"};
3528 char msg[1024], *bias;
3529 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
3530 nvlist_t **varray = NULL, *zc_props = NULL;
3531 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
3532 libzfs_handle_t *hdl = zhp->zpool_hdl;
3533 uint64_t vers, readonly = B_FALSE;
3534 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
3537 (void) snprintf(msg, sizeof (msg),
3538 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
3540 if (!zpool_name_valid(hdl, B_FALSE, newname))
3541 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
3543 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
3544 (void) fprintf(stderr, gettext("Internal error: unable to "
3545 "retrieve pool configuration\n"));
3549 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
3551 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
3554 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
3555 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
3556 props, vers, flags, msg)) == NULL)
3558 (void) nvlist_lookup_uint64(zc_props,
3559 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3561 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3562 "property %s can only be set at import time"),
3563 zpool_prop_to_name(ZPOOL_PROP_READONLY));
3568 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3570 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3571 "Source pool is missing vdev tree"));
3572 nvlist_free(zc_props);
3576 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
3579 if (*newroot == NULL ||
3580 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
3581 &newchild, &newchildren) != 0)
3584 for (c = 0; c < children; c++) {
3585 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
3586 boolean_t is_special = B_FALSE, is_dedup = B_FALSE;
3588 nvlist_t **mchild, *vdev;
3593 * Unlike cache & spares, slogs are stored in the
3594 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3596 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3598 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
3600 if (is_log || is_hole) {
3602 * Create a hole vdev and put it in the config.
3604 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
3606 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
3607 VDEV_TYPE_HOLE) != 0)
3609 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
3614 varray[vcount++] = vdev;
3618 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
3621 if (strcmp(type, VDEV_TYPE_INDIRECT) == 0) {
3623 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3626 } else if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
3627 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3628 "Source pool must be composed only of mirrors\n"));
3629 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3633 if (nvlist_lookup_string(child[c],
3634 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias) == 0) {
3635 if (strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0)
3636 is_special = B_TRUE;
3637 else if (strcmp(bias, VDEV_ALLOC_BIAS_DEDUP) == 0)
3640 verify(nvlist_lookup_nvlist_array(child[c],
3641 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3643 /* find or add an entry for this top-level vdev */
3644 if (newchildren > 0 &&
3645 (entry = find_vdev_entry(zhp, mchild, mchildren,
3646 newchild, newchildren)) >= 0) {
3647 /* We found a disk that the user specified. */
3648 vdev = mchild[entry];
3651 /* User didn't specify a disk for this vdev. */
3652 vdev = mchild[mchildren - 1];
3655 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3658 if (flags.dryrun != 0) {
3659 if (is_dedup == B_TRUE) {
3660 if (nvlist_add_string(varray[vcount - 1],
3661 ZPOOL_CONFIG_ALLOCATION_BIAS,
3662 VDEV_ALLOC_BIAS_DEDUP) != 0)
3664 } else if (is_special == B_TRUE) {
3665 if (nvlist_add_string(varray[vcount - 1],
3666 ZPOOL_CONFIG_ALLOCATION_BIAS,
3667 VDEV_ALLOC_BIAS_SPECIAL) != 0)
3673 /* did we find every disk the user specified? */
3674 if (found != newchildren) {
3675 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3676 "include at most one disk from each mirror"));
3677 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3681 /* Prepare the nvlist for populating. */
3682 if (*newroot == NULL) {
3683 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3686 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3687 VDEV_TYPE_ROOT) != 0)
3690 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3693 /* Add all the children we found */
3694 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3695 lastlog == 0 ? vcount : lastlog) != 0)
3699 * If we're just doing a dry run, exit now with success.
3702 memory_err = B_FALSE;
3707 /* now build up the config list & call the ioctl */
3708 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3711 if (nvlist_add_nvlist(newconfig,
3712 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3713 nvlist_add_string(newconfig,
3714 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3715 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3719 * The new pool is automatically part of the namespace unless we
3720 * explicitly export it.
3723 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3724 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3725 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3726 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3728 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3731 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3732 retval = zpool_standard_error(hdl, errno, msg);
3737 memory_err = B_FALSE;
3740 if (varray != NULL) {
3743 for (v = 0; v < vcount; v++)
3744 nvlist_free(varray[v]);
3747 zcmd_free_nvlists(&zc);
3748 nvlist_free(zc_props);
3749 nvlist_free(newconfig);
3751 nvlist_free(*newroot);
3759 return (no_memory(hdl));
3765 * Remove the given device.
3768 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3770 zfs_cmd_t zc = {"\0"};
3773 boolean_t avail_spare, l2cache, islog;
3774 libzfs_handle_t *hdl = zhp->zpool_hdl;
3777 (void) snprintf(msg, sizeof (msg),
3778 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3780 if (zpool_is_draid_spare(path)) {
3781 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3782 "dRAID spares cannot be removed"));
3783 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3786 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3787 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3789 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3791 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3792 if (islog && version < SPA_VERSION_HOLES) {
3793 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3794 "pool must be upgraded to support log removal"));
3795 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3798 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3800 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3806 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3807 "invalid config; all top-level vdevs must "
3808 "have the same sector size and not be raidz."));
3809 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3814 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3815 "Mount encrypted datasets to replay logs."));
3817 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3818 "Pool busy; removal may already be in progress"));
3820 (void) zfs_error(hdl, EZFS_BUSY, msg);
3825 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3826 "Mount encrypted datasets to replay logs."));
3827 (void) zfs_error(hdl, EZFS_BUSY, msg);
3829 (void) zpool_standard_error(hdl, errno, msg);
3834 (void) zpool_standard_error(hdl, errno, msg);
3840 zpool_vdev_remove_cancel(zpool_handle_t *zhp)
3844 libzfs_handle_t *hdl = zhp->zpool_hdl;
3846 (void) snprintf(msg, sizeof (msg),
3847 dgettext(TEXT_DOMAIN, "cannot cancel removal"));
3849 bzero(&zc, sizeof (zc));
3850 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3853 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3856 return (zpool_standard_error(hdl, errno, msg));
3860 zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path,
3865 boolean_t avail_spare, l2cache, islog;
3866 libzfs_handle_t *hdl = zhp->zpool_hdl;
3868 (void) snprintf(msg, sizeof (msg),
3869 dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"),
3872 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3874 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3876 if (avail_spare || l2cache || islog) {
3881 if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) {
3882 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3883 "indirect size not available"));
3884 return (zfs_error(hdl, EINVAL, msg));
3890 * Clear the errors for the pool, or the particular device if specified.
3893 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3895 zfs_cmd_t zc = {"\0"};
3898 zpool_load_policy_t policy;
3899 boolean_t avail_spare, l2cache;
3900 libzfs_handle_t *hdl = zhp->zpool_hdl;
3901 nvlist_t *nvi = NULL;
3905 (void) snprintf(msg, sizeof (msg),
3906 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3909 (void) snprintf(msg, sizeof (msg),
3910 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3913 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3915 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3916 &l2cache, NULL)) == NULL)
3917 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3920 * Don't allow error clearing for hot spares. Do allow
3921 * error clearing for l2cache devices.
3924 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3926 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3930 zpool_get_load_policy(rewindnvl, &policy);
3931 zc.zc_cookie = policy.zlp_rewind;
3933 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3936 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3939 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3941 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3942 zcmd_free_nvlists(&zc);
3947 if (!error || ((policy.zlp_rewind & ZPOOL_TRY_REWIND) &&
3948 errno != EPERM && errno != EACCES)) {
3949 if (policy.zlp_rewind &
3950 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3951 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3952 zpool_rewind_exclaim(hdl, zc.zc_name,
3953 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0),
3957 zcmd_free_nvlists(&zc);
3961 zcmd_free_nvlists(&zc);
3962 return (zpool_standard_error(hdl, errno, msg));
3966 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3969 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3971 zfs_cmd_t zc = {"\0"};
3973 libzfs_handle_t *hdl = zhp->zpool_hdl;
3975 (void) snprintf(msg, sizeof (msg),
3976 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3977 (u_longlong_t)guid);
3979 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3981 zc.zc_cookie = ZPOOL_NO_REWIND;
3983 if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
3986 return (zpool_standard_error(hdl, errno, msg));
3990 * Change the GUID for a pool.
3993 zpool_reguid(zpool_handle_t *zhp)
3996 libzfs_handle_t *hdl = zhp->zpool_hdl;
3997 zfs_cmd_t zc = {"\0"};
3999 (void) snprintf(msg, sizeof (msg),
4000 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
4002 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4003 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
4006 return (zpool_standard_error(hdl, errno, msg));
4013 zpool_reopen_one(zpool_handle_t *zhp, void *data)
4015 libzfs_handle_t *hdl = zpool_get_handle(zhp);
4016 const char *pool_name = zpool_get_name(zhp);
4017 boolean_t *scrub_restart = data;
4020 error = lzc_reopen(pool_name, *scrub_restart);
4022 return (zpool_standard_error_fmt(hdl, error,
4023 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"), pool_name));
4029 /* call into libzfs_core to execute the sync IOCTL per pool */
4031 zpool_sync_one(zpool_handle_t *zhp, void *data)
4034 libzfs_handle_t *hdl = zpool_get_handle(zhp);
4035 const char *pool_name = zpool_get_name(zhp);
4036 boolean_t *force = data;
4037 nvlist_t *innvl = fnvlist_alloc();
4039 fnvlist_add_boolean_value(innvl, "force", *force);
4040 if ((ret = lzc_sync(pool_name, innvl, NULL)) != 0) {
4042 return (zpool_standard_error_fmt(hdl, ret,
4043 dgettext(TEXT_DOMAIN, "sync '%s' failed"), pool_name));
4050 #define PATH_BUF_LEN 64
4053 * Given a vdev, return the name to display in iostat. If the vdev has a path,
4054 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
4055 * We also check if this is a whole disk, in which case we strip off the
4056 * trailing 's0' slice name.
4058 * This routine is also responsible for identifying when disks have been
4059 * reconfigured in a new location. The kernel will have opened the device by
4060 * devid, but the path will still refer to the old location. To catch this, we
4061 * first do a path -> devid translation (which is fast for the common case). If
4062 * the devid matches, we're done. If not, we do a reverse devid -> path
4063 * translation and issue the appropriate ioctl() to update the path of the vdev.
4064 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
4068 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
4071 char *path, *type, *env;
4073 char buf[PATH_BUF_LEN];
4074 char tmpbuf[PATH_BUF_LEN];
4077 * vdev_name will be "root"/"root-0" for the root vdev, but it is the
4078 * zpool name that will be displayed to the user.
4080 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
4081 if (zhp != NULL && strcmp(type, "root") == 0)
4082 return (zfs_strdup(hdl, zpool_get_name(zhp)));
4084 env = getenv("ZPOOL_VDEV_NAME_PATH");
4085 if (env && (strtoul(env, NULL, 0) > 0 ||
4086 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
4087 name_flags |= VDEV_NAME_PATH;
4089 env = getenv("ZPOOL_VDEV_NAME_GUID");
4090 if (env && (strtoul(env, NULL, 0) > 0 ||
4091 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
4092 name_flags |= VDEV_NAME_GUID;
4094 env = getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
4095 if (env && (strtoul(env, NULL, 0) > 0 ||
4096 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
4097 name_flags |= VDEV_NAME_FOLLOW_LINKS;
4099 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
4100 name_flags & VDEV_NAME_GUID) {
4101 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value);
4102 (void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)value);
4104 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
4105 if (name_flags & VDEV_NAME_FOLLOW_LINKS) {
4106 char *rp = realpath(path, NULL);
4108 strlcpy(buf, rp, sizeof (buf));
4115 * For a block device only use the name.
4117 if ((strcmp(type, VDEV_TYPE_DISK) == 0) &&
4118 !(name_flags & VDEV_NAME_PATH)) {
4119 path = zfs_strip_path(path);
4123 * Remove the partition from the path if this is a whole disk.
4125 if (strcmp(type, VDEV_TYPE_DRAID_SPARE) != 0 &&
4126 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, &value)
4127 == 0 && value && !(name_flags & VDEV_NAME_PATH)) {
4128 return (zfs_strip_partition(path));
4134 * If it's a raidz device, we need to stick in the parity level.
4136 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
4137 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
4139 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
4140 (u_longlong_t)value);
4145 * If it's a dRAID device, we add parity, groups, and spares.
4147 if (strcmp(path, VDEV_TYPE_DRAID) == 0) {
4148 uint64_t ndata, nparity, nspares;
4152 verify(nvlist_lookup_nvlist_array(nv,
4153 ZPOOL_CONFIG_CHILDREN, &child, &children) == 0);
4154 verify(nvlist_lookup_uint64(nv,
4155 ZPOOL_CONFIG_NPARITY, &nparity) == 0);
4156 verify(nvlist_lookup_uint64(nv,
4157 ZPOOL_CONFIG_DRAID_NDATA, &ndata) == 0);
4158 verify(nvlist_lookup_uint64(nv,
4159 ZPOOL_CONFIG_DRAID_NSPARES, &nspares) == 0);
4161 path = zpool_draid_name(buf, sizeof (buf), ndata,
4162 nparity, nspares, children);
4166 * We identify each top-level vdev by using a <type-id>
4167 * naming convention.
4169 if (name_flags & VDEV_NAME_TYPE_ID) {
4171 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
4173 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s-%llu",
4174 path, (u_longlong_t)id);
4179 return (zfs_strdup(hdl, path));
4183 zbookmark_mem_compare(const void *a, const void *b)
4185 return (memcmp(a, b, sizeof (zbookmark_phys_t)));
4189 * Retrieve the persistent error log, uniquify the members, and return to the
4193 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
4195 zfs_cmd_t zc = {"\0"};
4196 libzfs_handle_t *hdl = zhp->zpool_hdl;
4198 zbookmark_phys_t *zb = NULL;
4202 * Retrieve the raw error list from the kernel. If the number of errors
4203 * has increased, allocate more space and continue until we get the
4206 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
4210 zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
4211 count * sizeof (zbookmark_phys_t));
4212 zc.zc_nvlist_dst_size = count;
4213 (void) strcpy(zc.zc_name, zhp->zpool_name);
4215 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_ERROR_LOG,
4217 free((void *)(uintptr_t)zc.zc_nvlist_dst);
4218 if (errno == ENOMEM) {
4221 count = zc.zc_nvlist_dst_size;
4222 dst = zfs_alloc(zhp->zpool_hdl, count *
4223 sizeof (zbookmark_phys_t));
4224 zc.zc_nvlist_dst = (uintptr_t)dst;
4226 return (zpool_standard_error_fmt(hdl, errno,
4227 dgettext(TEXT_DOMAIN, "errors: List of "
4228 "errors unavailable")));
4236 * Sort the resulting bookmarks. This is a little confusing due to the
4237 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
4238 * to first, and 'zc_nvlist_dst_size' indicates the number of bookmarks
4239 * _not_ copied as part of the process. So we point the start of our
4240 * array appropriate and decrement the total number of elements.
4242 zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
4243 zc.zc_nvlist_dst_size;
4244 count -= zc.zc_nvlist_dst_size;
4246 qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
4248 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
4251 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
4253 for (i = 0; i < count; i++) {
4256 /* ignoring zb_blkid and zb_level for now */
4257 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
4258 zb[i-1].zb_object == zb[i].zb_object)
4261 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
4263 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
4264 zb[i].zb_objset) != 0) {
4268 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
4269 zb[i].zb_object) != 0) {
4273 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
4280 free((void *)(uintptr_t)zc.zc_nvlist_dst);
4284 free((void *)(uintptr_t)zc.zc_nvlist_dst);
4285 return (no_memory(zhp->zpool_hdl));
4289 * Upgrade a ZFS pool to the latest on-disk version.
4292 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
4294 zfs_cmd_t zc = {"\0"};
4295 libzfs_handle_t *hdl = zhp->zpool_hdl;
4297 (void) strcpy(zc.zc_name, zhp->zpool_name);
4298 zc.zc_cookie = new_version;
4300 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
4301 return (zpool_standard_error_fmt(hdl, errno,
4302 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
4308 zfs_save_arguments(int argc, char **argv, char *string, int len)
4312 (void) strlcpy(string, basename(argv[0]), len);
4313 for (i = 1; i < argc; i++) {
4314 (void) strlcat(string, " ", len);
4315 (void) strlcat(string, argv[i], len);
4320 zpool_log_history(libzfs_handle_t *hdl, const char *message)
4322 zfs_cmd_t zc = {"\0"};
4326 args = fnvlist_alloc();
4327 fnvlist_add_string(args, "message", message);
4328 err = zcmd_write_src_nvlist(hdl, &zc, args);
4330 err = zfs_ioctl(hdl, ZFS_IOC_LOG_HISTORY, &zc);
4332 zcmd_free_nvlists(&zc);
4337 * Perform ioctl to get some command history of a pool.
4339 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
4340 * logical offset of the history buffer to start reading from.
4342 * Upon return, 'off' is the next logical offset to read from and
4343 * 'len' is the actual amount of bytes read into 'buf'.
4346 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
4348 zfs_cmd_t zc = {"\0"};
4349 libzfs_handle_t *hdl = zhp->zpool_hdl;
4351 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4353 zc.zc_history = (uint64_t)(uintptr_t)buf;
4354 zc.zc_history_len = *len;
4355 zc.zc_history_offset = *off;
4357 if (zfs_ioctl(hdl, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
4360 return (zfs_error_fmt(hdl, EZFS_PERM,
4361 dgettext(TEXT_DOMAIN,
4362 "cannot show history for pool '%s'"),
4365 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
4366 dgettext(TEXT_DOMAIN, "cannot get history for pool "
4367 "'%s'"), zhp->zpool_name));
4369 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
4370 dgettext(TEXT_DOMAIN, "cannot get history for pool "
4371 "'%s', pool must be upgraded"), zhp->zpool_name));
4373 return (zpool_standard_error_fmt(hdl, errno,
4374 dgettext(TEXT_DOMAIN,
4375 "cannot get history for '%s'"), zhp->zpool_name));
4379 *len = zc.zc_history_len;
4380 *off = zc.zc_history_offset;
4386 * Retrieve the command history of a pool.
4389 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp, uint64_t *off,
4393 int buflen = 128 * 1024;
4394 nvlist_t **records = NULL;
4395 uint_t numrecords = 0;
4397 uint64_t start = *off;
4399 buf = malloc(buflen);
4402 /* process about 1MB a time */
4403 while (*off - start < 1024 * 1024) {
4404 uint64_t bytes_read = buflen;
4407 if ((err = get_history(zhp, buf, off, &bytes_read)) != 0)
4410 /* if nothing else was read in, we're at EOF, just return */
4416 if ((err = zpool_history_unpack(buf, bytes_read,
4417 &leftover, &records, &numrecords)) != 0)
4420 if (leftover == bytes_read) {
4422 * no progress made, because buffer is not big enough
4423 * to hold this record; resize and retry.
4427 buf = malloc(buflen);
4436 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
4437 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
4438 records, numrecords) == 0);
4440 for (i = 0; i < numrecords; i++)
4441 nvlist_free(records[i]);
4448 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
4449 * If there is a new event available 'nvp' will contain a newly allocated
4450 * nvlist and 'dropped' will be set to the number of missed events since
4451 * the last call to this function. When 'nvp' is set to NULL it indicates
4452 * no new events are available. In either case the function returns 0 and
4453 * it is up to the caller to free 'nvp'. In the case of a fatal error the
4454 * function will return a non-zero value. When the function is called in
4455 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
4456 * it will not return until a new event is available.
4459 zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
4460 int *dropped, unsigned flags, int zevent_fd)
4462 zfs_cmd_t zc = {"\0"};
4467 zc.zc_cleanup_fd = zevent_fd;
4469 if (flags & ZEVENT_NONBLOCK)
4470 zc.zc_guid = ZEVENT_NONBLOCK;
4472 if (zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE) != 0)
4476 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
4479 error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
4480 dgettext(TEXT_DOMAIN, "zfs shutdown"));
4483 /* Blocking error case should not occur */
4484 if (!(flags & ZEVENT_NONBLOCK))
4485 error = zpool_standard_error_fmt(hdl, errno,
4486 dgettext(TEXT_DOMAIN, "cannot get event"));
4490 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
4491 error = zfs_error_fmt(hdl, EZFS_NOMEM,
4492 dgettext(TEXT_DOMAIN, "cannot get event"));
4498 error = zpool_standard_error_fmt(hdl, errno,
4499 dgettext(TEXT_DOMAIN, "cannot get event"));
4504 error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
4508 *dropped = (int)zc.zc_cookie;
4510 zcmd_free_nvlists(&zc);
4519 zpool_events_clear(libzfs_handle_t *hdl, int *count)
4521 zfs_cmd_t zc = {"\0"};
4523 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
4524 return (zpool_standard_error(hdl, errno,
4525 dgettext(TEXT_DOMAIN, "cannot clear events")));
4528 *count = (int)zc.zc_cookie; /* # of events cleared */
4534 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4535 * the passed zevent_fd file handle. On success zero is returned,
4536 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4539 zpool_events_seek(libzfs_handle_t *hdl, uint64_t eid, int zevent_fd)
4541 zfs_cmd_t zc = {"\0"};
4545 zc.zc_cleanup_fd = zevent_fd;
4547 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_SEEK, &zc) != 0) {
4550 error = zfs_error_fmt(hdl, EZFS_NOENT,
4551 dgettext(TEXT_DOMAIN, "cannot get event"));
4555 error = zfs_error_fmt(hdl, EZFS_NOMEM,
4556 dgettext(TEXT_DOMAIN, "cannot get event"));
4560 error = zpool_standard_error_fmt(hdl, errno,
4561 dgettext(TEXT_DOMAIN, "cannot get event"));
4570 zpool_obj_to_path_impl(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4571 char *pathname, size_t len, boolean_t always_unmounted)
4573 zfs_cmd_t zc = {"\0"};
4574 boolean_t mounted = B_FALSE;
4575 char *mntpnt = NULL;
4576 char dsname[ZFS_MAX_DATASET_NAME_LEN];
4579 /* special case for the MOS */
4580 (void) snprintf(pathname, len, "<metadata>:<0x%llx>",
4585 /* get the dataset's name */
4586 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4588 if (zfs_ioctl(zhp->zpool_hdl,
4589 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
4590 /* just write out a path of two object numbers */
4591 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
4592 (longlong_t)dsobj, (longlong_t)obj);
4595 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
4597 /* find out if the dataset is mounted */
4598 mounted = !always_unmounted && is_mounted(zhp->zpool_hdl, dsname,
4601 /* get the corrupted object's path */
4602 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
4604 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_OBJ_TO_PATH,
4607 (void) snprintf(pathname, len, "%s%s", mntpnt,
4610 (void) snprintf(pathname, len, "%s:%s",
4611 dsname, zc.zc_value);
4614 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname,
4621 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4622 char *pathname, size_t len)
4624 zpool_obj_to_path_impl(zhp, dsobj, obj, pathname, len, B_FALSE);
4628 zpool_obj_to_path_ds(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4629 char *pathname, size_t len)
4631 zpool_obj_to_path_impl(zhp, dsobj, obj, pathname, len, B_TRUE);
4634 * Wait while the specified activity is in progress in the pool.
4637 zpool_wait(zpool_handle_t *zhp, zpool_wait_activity_t activity)
4641 int error = zpool_wait_status(zhp, activity, &missing, NULL);
4644 (void) zpool_standard_error_fmt(zhp->zpool_hdl, ENOENT,
4645 dgettext(TEXT_DOMAIN, "error waiting in pool '%s'"),
4654 * Wait for the given activity and return the status of the wait (whether or not
4655 * any waiting was done) in the 'waited' parameter. Non-existent pools are
4656 * reported via the 'missing' parameter, rather than by printing an error
4657 * message. This is convenient when this function is called in a loop over a
4658 * long period of time (as it is, for example, by zpool's wait cmd). In that
4659 * scenario, a pool being exported or destroyed should be considered a normal
4660 * event, so we don't want to print an error when we find that the pool doesn't
4664 zpool_wait_status(zpool_handle_t *zhp, zpool_wait_activity_t activity,
4665 boolean_t *missing, boolean_t *waited)
4667 int error = lzc_wait(zhp->zpool_name, activity, waited);
4668 *missing = (error == ENOENT);
4673 (void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
4674 dgettext(TEXT_DOMAIN, "error waiting in pool '%s'"),
4682 zpool_set_bootenv(zpool_handle_t *zhp, const nvlist_t *envmap)
4684 int error = lzc_set_bootenv(zhp->zpool_name, envmap);
4686 (void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
4687 dgettext(TEXT_DOMAIN,
4688 "error setting bootenv in pool '%s'"), zhp->zpool_name);
4695 zpool_get_bootenv(zpool_handle_t *zhp, nvlist_t **nvlp)
4701 error = lzc_get_bootenv(zhp->zpool_name, &nvl);
4703 (void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
4704 dgettext(TEXT_DOMAIN,
4705 "error getting bootenv in pool '%s'"), zhp->zpool_name);
4714 * Attempt to read and parse feature file(s) (from "compatibility" property).
4715 * Files contain zpool feature names, comma or whitespace-separated.
4716 * Comments (# character to next newline) are discarded.
4719 * compatibility : string containing feature filenames
4720 * features : either NULL or pointer to array of boolean
4721 * report : either NULL or pointer to string buffer
4722 * rlen : length of "report" buffer
4724 * compatibility is NULL (unset), "", "off", "legacy", or list of
4725 * comma-separated filenames. filenames should either be absolute,
4727 * 1) ZPOOL_SYSCONF_COMPAT_D (eg: /etc/zfs/compatibility.d) or
4728 * 2) ZPOOL_DATA_COMPAT_D (eg: /usr/share/zfs/compatibility.d).
4729 * (Unset), "" or "off" => enable all features
4730 * "legacy" => disable all features
4732 * Any feature names read from files which match unames in spa_feature_table
4733 * will have the corresponding boolean set in the features array (if non-NULL).
4734 * If more than one feature set specified, only features present in *all* of
4737 * "report" if not NULL will be populated with a suitable status message.
4740 * ZPOOL_COMPATIBILITY_OK : files read and parsed ok
4741 * ZPOOL_COMPATIBILITY_BADFILE : file too big or not a text file
4742 * ZPOOL_COMPATIBILITY_BADTOKEN : SYSCONF file contains invalid feature name
4743 * ZPOOL_COMPATIBILITY_WARNTOKEN : DATA file contains invalid feature name
4744 * ZPOOL_COMPATIBILITY_NOFILES : no feature files found
4746 zpool_compat_status_t
4747 zpool_load_compat(const char *compat, boolean_t *features, char *report,
4750 int sdirfd, ddirfd, featfd;
4754 char *file, *line, *word;
4756 char l_compat[ZFS_MAXPROPLEN];
4758 boolean_t ret_nofiles = B_TRUE;
4759 boolean_t ret_badfile = B_FALSE;
4760 boolean_t ret_badtoken = B_FALSE;
4761 boolean_t ret_warntoken = B_FALSE;
4763 /* special cases (unset), "" and "off" => enable all features */
4764 if (compat == NULL || compat[0] == '\0' ||
4765 strcmp(compat, ZPOOL_COMPAT_OFF) == 0) {
4766 if (features != NULL)
4767 for (uint_t i = 0; i < SPA_FEATURES; i++)
4768 features[i] = B_TRUE;
4770 strlcpy(report, gettext("all features enabled"), rlen);
4771 return (ZPOOL_COMPATIBILITY_OK);
4774 /* Final special case "legacy" => disable all features */
4775 if (strcmp(compat, ZPOOL_COMPAT_LEGACY) == 0) {
4776 if (features != NULL)
4777 for (uint_t i = 0; i < SPA_FEATURES; i++)
4778 features[i] = B_FALSE;
4780 strlcpy(report, gettext("all features disabled"), rlen);
4781 return (ZPOOL_COMPATIBILITY_OK);
4785 * Start with all true; will be ANDed with results from each file
4787 if (features != NULL)
4788 for (uint_t i = 0; i < SPA_FEATURES; i++)
4789 features[i] = B_TRUE;
4791 char err_badfile[1024] = "";
4792 char err_badtoken[1024] = "";
4795 * We ignore errors from the directory open()
4796 * as they're only needed if the filename is relative
4797 * which will be checked during the openat().
4800 /* O_PATH safer than O_RDONLY if system allows it */
4802 #define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_PATH)
4804 #define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_RDONLY)
4807 sdirfd = open(ZPOOL_SYSCONF_COMPAT_D, ZC_DIR_FLAGS);
4808 ddirfd = open(ZPOOL_DATA_COMPAT_D, ZC_DIR_FLAGS);
4810 (void) strlcpy(l_compat, compat, ZFS_MAXPROPLEN);
4812 for (file = strtok_r(l_compat, ",", &ps);
4814 file = strtok_r(NULL, ",", &ps)) {
4816 boolean_t l_features[SPA_FEATURES];
4818 enum { Z_SYSCONF, Z_DATA } source;
4820 /* try sysconfdir first, then datadir */
4822 if ((featfd = openat(sdirfd, file, O_RDONLY | O_CLOEXEC)) < 0) {
4823 featfd = openat(ddirfd, file, O_RDONLY | O_CLOEXEC);
4827 /* File readable and correct size? */
4829 fstat(featfd, &fs) < 0 ||
4831 fs.st_size > ZPOOL_COMPAT_MAXSIZE) {
4832 (void) close(featfd);
4833 strlcat(err_badfile, file, ZFS_MAXPROPLEN);
4834 strlcat(err_badfile, " ", ZFS_MAXPROPLEN);
4835 ret_badfile = B_TRUE;
4839 /* Prefault the file if system allows */
4840 #if defined(MAP_POPULATE)
4841 #define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_POPULATE)
4842 #elif defined(MAP_PREFAULT_READ)
4843 #define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_PREFAULT_READ)
4845 #define ZC_MMAP_FLAGS (MAP_PRIVATE)
4848 /* private mmap() so we can strtok safely */
4849 fc = (char *)mmap(NULL, fs.st_size, PROT_READ | PROT_WRITE,
4850 ZC_MMAP_FLAGS, featfd, 0);
4851 (void) close(featfd);
4853 /* map ok, and last character == newline? */
4854 if (fc == MAP_FAILED || fc[fs.st_size - 1] != '\n') {
4855 (void) munmap((void *) fc, fs.st_size);
4856 strlcat(err_badfile, file, ZFS_MAXPROPLEN);
4857 strlcat(err_badfile, " ", ZFS_MAXPROPLEN);
4858 ret_badfile = B_TRUE;
4862 ret_nofiles = B_FALSE;
4864 for (uint_t i = 0; i < SPA_FEATURES; i++)
4865 l_features[i] = B_FALSE;
4867 /* replace final newline with NULL to ensure string ends */
4868 fc[fs.st_size - 1] = '\0';
4870 for (line = strtok_r(fc, "\n", &ls);
4872 line = strtok_r(NULL, "\n", &ls)) {
4873 /* discard comments */
4874 *(strchrnul(line, '#')) = '\0';
4876 for (word = strtok_r(line, ", \t", &ws);
4878 word = strtok_r(NULL, ", \t", &ws)) {
4879 /* Find matching feature name */
4881 for (f = 0; f < SPA_FEATURES; f++) {
4882 zfeature_info_t *fi =
4883 &spa_feature_table[f];
4884 if (strcmp(word, fi->fi_uname) == 0) {
4885 l_features[f] = B_TRUE;
4889 if (f < SPA_FEATURES)
4892 /* found an unrecognized word */
4893 /* lightly sanitize it */
4894 if (strlen(word) > 32)
4896 for (char *c = word; *c != '\0'; c++)
4900 strlcat(err_badtoken, word, ZFS_MAXPROPLEN);
4901 strlcat(err_badtoken, " ", ZFS_MAXPROPLEN);
4902 if (source == Z_SYSCONF)
4903 ret_badtoken = B_TRUE;
4905 ret_warntoken = B_TRUE;
4908 (void) munmap((void *) fc, fs.st_size);
4910 if (features != NULL)
4911 for (uint_t i = 0; i < SPA_FEATURES; i++)
4912 features[i] &= l_features[i];
4914 (void) close(sdirfd);
4915 (void) close(ddirfd);
4917 /* Return the most serious error */
4920 snprintf(report, rlen, gettext("could not read/"
4921 "parse feature file(s): %s"), err_badfile);
4922 return (ZPOOL_COMPATIBILITY_BADFILE);
4927 gettext("no valid compatibility files specified"),
4929 return (ZPOOL_COMPATIBILITY_NOFILES);
4933 snprintf(report, rlen, gettext("invalid feature "
4934 "name(s) in local compatibility files: %s"),
4936 return (ZPOOL_COMPATIBILITY_BADTOKEN);
4938 if (ret_warntoken) {
4940 snprintf(report, rlen, gettext("unrecognized feature "
4941 "name(s) in distribution compatibility files: %s"),
4943 return (ZPOOL_COMPATIBILITY_WARNTOKEN);
4946 strlcpy(report, gettext("compatibility set ok"), rlen);
4947 return (ZPOOL_COMPATIBILITY_OK);