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, 2018 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>
42 #include <sys/efi_partition.h>
43 #include <sys/systeminfo.h>
44 #include <sys/zfs_ioctl.h>
45 #include <sys/vdev_disk.h>
49 #include "zfs_namecheck.h"
51 #include "libzfs_impl.h"
52 #include "zfs_comutil.h"
53 #include "zfeature_common.h"
55 static boolean_t zpool_vdev_is_interior(const char *name);
57 typedef struct prop_flags {
58 int create:1; /* Validate property on creation */
59 int import:1; /* Validate property on import */
63 * ====================================================================
64 * zpool property functions
65 * ====================================================================
69 zpool_get_all_props(zpool_handle_t *zhp)
71 zfs_cmd_t zc = {"\0"};
72 libzfs_handle_t *hdl = zhp->zpool_hdl;
74 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
76 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
79 while (zfs_ioctl(hdl, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
80 if (errno == ENOMEM) {
81 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
82 zcmd_free_nvlists(&zc);
86 zcmd_free_nvlists(&zc);
91 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
92 zcmd_free_nvlists(&zc);
96 zcmd_free_nvlists(&zc);
102 zpool_props_refresh(zpool_handle_t *zhp)
106 old_props = zhp->zpool_props;
108 if (zpool_get_all_props(zhp) != 0)
111 nvlist_free(old_props);
116 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
122 zprop_source_t source;
124 nvl = zhp->zpool_props;
125 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
126 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
128 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
130 source = ZPROP_SRC_DEFAULT;
131 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
142 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
146 zprop_source_t source;
148 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
150 * zpool_get_all_props() has most likely failed because
151 * the pool is faulted, but if all we need is the top level
152 * vdev's guid then get it from the zhp config nvlist.
154 if ((prop == ZPOOL_PROP_GUID) &&
155 (nvlist_lookup_nvlist(zhp->zpool_config,
156 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
157 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
161 return (zpool_prop_default_numeric(prop));
164 nvl = zhp->zpool_props;
165 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
166 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
168 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
170 source = ZPROP_SRC_DEFAULT;
171 value = zpool_prop_default_numeric(prop);
181 * Map VDEV STATE to printed strings.
184 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
187 case VDEV_STATE_CLOSED:
188 case VDEV_STATE_OFFLINE:
189 return (gettext("OFFLINE"));
190 case VDEV_STATE_REMOVED:
191 return (gettext("REMOVED"));
192 case VDEV_STATE_CANT_OPEN:
193 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
194 return (gettext("FAULTED"));
195 else if (aux == VDEV_AUX_SPLIT_POOL)
196 return (gettext("SPLIT"));
198 return (gettext("UNAVAIL"));
199 case VDEV_STATE_FAULTED:
200 return (gettext("FAULTED"));
201 case VDEV_STATE_DEGRADED:
202 return (gettext("DEGRADED"));
203 case VDEV_STATE_HEALTHY:
204 return (gettext("ONLINE"));
210 return (gettext("UNKNOWN"));
214 * Map POOL STATE to printed strings.
217 zpool_pool_state_to_name(pool_state_t state)
222 case POOL_STATE_ACTIVE:
223 return (gettext("ACTIVE"));
224 case POOL_STATE_EXPORTED:
225 return (gettext("EXPORTED"));
226 case POOL_STATE_DESTROYED:
227 return (gettext("DESTROYED"));
228 case POOL_STATE_SPARE:
229 return (gettext("SPARE"));
230 case POOL_STATE_L2CACHE:
231 return (gettext("L2CACHE"));
232 case POOL_STATE_UNINITIALIZED:
233 return (gettext("UNINITIALIZED"));
234 case POOL_STATE_UNAVAIL:
235 return (gettext("UNAVAIL"));
236 case POOL_STATE_POTENTIALLY_ACTIVE:
237 return (gettext("POTENTIALLY_ACTIVE"));
240 return (gettext("UNKNOWN"));
244 * Given a pool handle, return the pool health string ("ONLINE", "DEGRADED",
248 zpool_get_state_str(zpool_handle_t *zhp)
250 zpool_errata_t errata;
251 zpool_status_t status;
257 status = zpool_get_status(zhp, NULL, &errata);
259 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
260 str = gettext("FAULTED");
261 } else if (status == ZPOOL_STATUS_IO_FAILURE_WAIT ||
262 status == ZPOOL_STATUS_IO_FAILURE_MMP) {
263 str = gettext("SUSPENDED");
265 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
266 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
267 verify(nvlist_lookup_uint64_array(nvroot,
268 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
270 str = zpool_state_to_name(vs->vs_state, vs->vs_aux);
276 * Get a zpool property value for 'prop' and return the value in
277 * a pre-allocated buffer.
280 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf,
281 size_t len, zprop_source_t *srctype, boolean_t literal)
285 zprop_source_t src = ZPROP_SRC_NONE;
287 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
289 case ZPOOL_PROP_NAME:
290 (void) strlcpy(buf, zpool_get_name(zhp), len);
293 case ZPOOL_PROP_HEALTH:
294 (void) strlcpy(buf, zpool_get_state_str(zhp), len);
297 case ZPOOL_PROP_GUID:
298 intval = zpool_get_prop_int(zhp, prop, &src);
299 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
302 case ZPOOL_PROP_ALTROOT:
303 case ZPOOL_PROP_CACHEFILE:
304 case ZPOOL_PROP_COMMENT:
305 if (zhp->zpool_props != NULL ||
306 zpool_get_all_props(zhp) == 0) {
308 zpool_get_prop_string(zhp, prop, &src),
314 (void) strlcpy(buf, "-", len);
323 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
324 prop != ZPOOL_PROP_NAME)
327 switch (zpool_prop_get_type(prop)) {
328 case PROP_TYPE_STRING:
329 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
333 case PROP_TYPE_NUMBER:
334 intval = zpool_get_prop_int(zhp, prop, &src);
337 case ZPOOL_PROP_SIZE:
338 case ZPOOL_PROP_ALLOCATED:
339 case ZPOOL_PROP_FREE:
340 case ZPOOL_PROP_FREEING:
341 case ZPOOL_PROP_LEAKED:
342 case ZPOOL_PROP_ASHIFT:
344 (void) snprintf(buf, len, "%llu",
345 (u_longlong_t)intval);
347 (void) zfs_nicenum(intval, buf, len);
350 case ZPOOL_PROP_EXPANDSZ:
351 case ZPOOL_PROP_CHECKPOINT:
353 (void) strlcpy(buf, "-", len);
354 } else if (literal) {
355 (void) snprintf(buf, len, "%llu",
356 (u_longlong_t)intval);
358 (void) zfs_nicebytes(intval, buf, len);
362 case ZPOOL_PROP_CAPACITY:
364 (void) snprintf(buf, len, "%llu",
365 (u_longlong_t)intval);
367 (void) snprintf(buf, len, "%llu%%",
368 (u_longlong_t)intval);
372 case ZPOOL_PROP_FRAGMENTATION:
373 if (intval == UINT64_MAX) {
374 (void) strlcpy(buf, "-", len);
375 } else if (literal) {
376 (void) snprintf(buf, len, "%llu",
377 (u_longlong_t)intval);
379 (void) snprintf(buf, len, "%llu%%",
380 (u_longlong_t)intval);
384 case ZPOOL_PROP_DEDUPRATIO:
386 (void) snprintf(buf, len, "%llu.%02llu",
387 (u_longlong_t)(intval / 100),
388 (u_longlong_t)(intval % 100));
390 (void) snprintf(buf, len, "%llu.%02llux",
391 (u_longlong_t)(intval / 100),
392 (u_longlong_t)(intval % 100));
395 case ZPOOL_PROP_HEALTH:
396 (void) strlcpy(buf, zpool_get_state_str(zhp), len);
398 case ZPOOL_PROP_VERSION:
399 if (intval >= SPA_VERSION_FEATURES) {
400 (void) snprintf(buf, len, "-");
405 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
409 case PROP_TYPE_INDEX:
410 intval = zpool_get_prop_int(zhp, prop, &src);
411 if (zpool_prop_index_to_string(prop, intval, &strval)
414 (void) strlcpy(buf, strval, len);
428 * Check if the bootfs name has the same pool name as it is set to.
429 * Assuming bootfs is a valid dataset name.
432 bootfs_name_valid(const char *pool, char *bootfs)
434 int len = strlen(pool);
435 if (bootfs[0] == '\0')
438 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
441 if (strncmp(pool, bootfs, len) == 0 &&
442 (bootfs[len] == '/' || bootfs[len] == '\0'))
449 zpool_is_bootable(zpool_handle_t *zhp)
451 char bootfs[ZFS_MAX_DATASET_NAME_LEN];
453 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
454 sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-",
455 sizeof (bootfs)) != 0);
460 * Given an nvlist of zpool properties to be set, validate that they are
461 * correct, and parse any numeric properties (index, boolean, etc) if they are
462 * specified as strings.
465 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
466 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
474 struct stat64 statbuf;
477 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
478 (void) no_memory(hdl);
483 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
484 const char *propname = nvpair_name(elem);
486 prop = zpool_name_to_prop(propname);
487 if (prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname)) {
489 char *fname = strchr(propname, '@') + 1;
491 err = zfeature_lookup_name(fname, NULL);
493 ASSERT3U(err, ==, ENOENT);
494 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
495 "invalid feature '%s'"), fname);
496 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
500 if (nvpair_type(elem) != DATA_TYPE_STRING) {
501 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
502 "'%s' must be a string"), propname);
503 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
507 (void) nvpair_value_string(elem, &strval);
508 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0 &&
509 strcmp(strval, ZFS_FEATURE_DISABLED) != 0) {
510 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
511 "property '%s' can only be set to "
512 "'enabled' or 'disabled'"), propname);
513 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
518 strcmp(strval, ZFS_FEATURE_DISABLED) == 0) {
519 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
520 "property '%s' can only be set to "
521 "'disabled' at creation time"), propname);
522 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
526 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
527 (void) no_memory(hdl);
534 * Make sure this property is valid and applies to this type.
536 if (prop == ZPOOL_PROP_INVAL) {
537 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
538 "invalid property '%s'"), propname);
539 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
543 if (zpool_prop_readonly(prop)) {
544 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
545 "is readonly"), propname);
546 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
550 if (!flags.create && zpool_prop_setonce(prop)) {
551 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
552 "property '%s' can only be set at "
553 "creation time"), propname);
554 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
558 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
559 &strval, &intval, errbuf) != 0)
563 * Perform additional checking for specific properties.
566 case ZPOOL_PROP_VERSION:
567 if (intval < version ||
568 !SPA_VERSION_IS_SUPPORTED(intval)) {
569 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
570 "property '%s' number %d is invalid."),
572 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
577 case ZPOOL_PROP_ASHIFT:
579 (intval < ASHIFT_MIN || intval > ASHIFT_MAX)) {
580 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
581 "property '%s' number %d is invalid, only "
582 "values between %" PRId32 " and "
583 "%" PRId32 " are allowed."),
584 propname, intval, ASHIFT_MIN, ASHIFT_MAX);
585 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
590 case ZPOOL_PROP_BOOTFS:
591 if (flags.create || flags.import) {
592 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
593 "property '%s' cannot be set at creation "
594 "or import time"), propname);
595 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
599 if (version < SPA_VERSION_BOOTFS) {
600 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
601 "pool must be upgraded to support "
602 "'%s' property"), propname);
603 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
608 * bootfs property value has to be a dataset name and
609 * the dataset has to be in the same pool as it sets to.
611 if (!bootfs_name_valid(poolname, strval)) {
612 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
613 "is an invalid name"), strval);
614 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
618 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
619 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
620 "could not open pool '%s'"), poolname);
621 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
627 case ZPOOL_PROP_ALTROOT:
628 if (!flags.create && !flags.import) {
629 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
630 "property '%s' can only be set during pool "
631 "creation or import"), propname);
632 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
636 if (strval[0] != '/') {
637 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
638 "bad alternate root '%s'"), strval);
639 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
644 case ZPOOL_PROP_CACHEFILE:
645 if (strval[0] == '\0')
648 if (strcmp(strval, "none") == 0)
651 if (strval[0] != '/') {
652 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
653 "property '%s' must be empty, an "
654 "absolute path, or 'none'"), propname);
655 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
659 slash = strrchr(strval, '/');
661 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
662 strcmp(slash, "/..") == 0) {
663 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
664 "'%s' is not a valid file"), strval);
665 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
671 if (strval[0] != '\0' &&
672 (stat64(strval, &statbuf) != 0 ||
673 !S_ISDIR(statbuf.st_mode))) {
674 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
675 "'%s' is not a valid directory"),
677 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
684 case ZPOOL_PROP_COMMENT:
685 for (check = strval; *check != '\0'; check++) {
686 if (!isprint(*check)) {
688 dgettext(TEXT_DOMAIN,
689 "comment may only have printable "
691 (void) zfs_error(hdl, EZFS_BADPROP,
696 if (strlen(strval) > ZPROP_MAX_COMMENT) {
697 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
698 "comment must not exceed %d characters"),
700 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
704 case ZPOOL_PROP_READONLY:
706 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
707 "property '%s' can only be set at "
708 "import time"), propname);
709 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
713 case ZPOOL_PROP_MULTIHOST:
714 if (get_system_hostid() == 0) {
715 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
716 "requires a non-zero system hostid"));
717 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
721 case ZPOOL_PROP_DEDUPDITTO:
722 printf("Note: property '%s' no longer has "
723 "any effect\n", propname);
727 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
728 "property '%s'(%d) not defined"), propname, prop);
735 nvlist_free(retprops);
740 * Set zpool property : propname=propval.
743 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
745 zfs_cmd_t zc = {"\0"};
748 nvlist_t *nvl = NULL;
751 prop_flags_t flags = { 0 };
753 (void) snprintf(errbuf, sizeof (errbuf),
754 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
757 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
758 return (no_memory(zhp->zpool_hdl));
760 if (nvlist_add_string(nvl, propname, propval) != 0) {
762 return (no_memory(zhp->zpool_hdl));
765 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
766 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
767 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
776 * Execute the corresponding ioctl() to set this property.
778 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
780 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
785 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
787 zcmd_free_nvlists(&zc);
791 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
793 (void) zpool_props_refresh(zhp);
799 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
801 libzfs_handle_t *hdl = zhp->zpool_hdl;
803 char buf[ZFS_MAXPROPLEN];
804 nvlist_t *features = NULL;
807 boolean_t firstexpand = (NULL == *plp);
810 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
814 while (*last != NULL)
815 last = &(*last)->pl_next;
818 features = zpool_get_features(zhp);
820 if ((*plp)->pl_all && firstexpand) {
821 for (i = 0; i < SPA_FEATURES; i++) {
822 zprop_list_t *entry = zfs_alloc(hdl,
823 sizeof (zprop_list_t));
824 entry->pl_prop = ZPROP_INVAL;
825 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
826 spa_feature_table[i].fi_uname);
827 entry->pl_width = strlen(entry->pl_user_prop);
828 entry->pl_all = B_TRUE;
831 last = &entry->pl_next;
835 /* add any unsupported features */
836 for (nvp = nvlist_next_nvpair(features, NULL);
837 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
842 if (zfeature_is_supported(nvpair_name(nvp)))
845 propname = zfs_asprintf(hdl, "unsupported@%s",
849 * Before adding the property to the list make sure that no
850 * other pool already added the same property.
854 while (entry != NULL) {
855 if (entry->pl_user_prop != NULL &&
856 strcmp(propname, entry->pl_user_prop) == 0) {
860 entry = entry->pl_next;
867 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
868 entry->pl_prop = ZPROP_INVAL;
869 entry->pl_user_prop = propname;
870 entry->pl_width = strlen(entry->pl_user_prop);
871 entry->pl_all = B_TRUE;
874 last = &entry->pl_next;
877 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
882 if (entry->pl_prop != ZPROP_INVAL &&
883 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
884 NULL, B_FALSE) == 0) {
885 if (strlen(buf) > entry->pl_width)
886 entry->pl_width = strlen(buf);
894 * Get the state for the given feature on the given ZFS pool.
897 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
901 boolean_t found = B_FALSE;
902 nvlist_t *features = zpool_get_features(zhp);
904 const char *feature = strchr(propname, '@') + 1;
906 supported = zpool_prop_feature(propname);
907 ASSERT(supported || zpool_prop_unsupported(propname));
910 * Convert from feature name to feature guid. This conversion is
911 * unnecessary for unsupported@... properties because they already
918 ret = zfeature_lookup_name(feature, &fid);
920 (void) strlcpy(buf, "-", len);
923 feature = spa_feature_table[fid].fi_guid;
926 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
931 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
934 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
936 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
941 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
943 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
946 (void) strlcpy(buf, "-", len);
955 * Validate the given pool name, optionally putting an extended error message in
959 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
965 ret = pool_namecheck(pool, &why, &what);
968 * The rules for reserved pool names were extended at a later point.
969 * But we need to support users with existing pools that may now be
970 * invalid. So we only check for this expanded set of names during a
971 * create (or import), and only in userland.
973 if (ret == 0 && !isopen &&
974 (strncmp(pool, "mirror", 6) == 0 ||
975 strncmp(pool, "raidz", 5) == 0 ||
976 strncmp(pool, "spare", 5) == 0 ||
977 strcmp(pool, "log") == 0)) {
980 dgettext(TEXT_DOMAIN, "name is reserved"));
988 case NAME_ERR_TOOLONG:
990 dgettext(TEXT_DOMAIN, "name is too long"));
993 case NAME_ERR_INVALCHAR:
995 dgettext(TEXT_DOMAIN, "invalid character "
996 "'%c' in pool name"), what);
999 case NAME_ERR_NOLETTER:
1000 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1001 "name must begin with a letter"));
1004 case NAME_ERR_RESERVED:
1005 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1006 "name is reserved"));
1009 case NAME_ERR_DISKLIKE:
1010 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1011 "pool name is reserved"));
1014 case NAME_ERR_LEADING_SLASH:
1015 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1016 "leading slash in name"));
1019 case NAME_ERR_EMPTY_COMPONENT:
1020 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1021 "empty component in name"));
1024 case NAME_ERR_TRAILING_SLASH:
1025 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1026 "trailing slash in name"));
1029 case NAME_ERR_MULTIPLE_DELIMITERS:
1030 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1031 "multiple '@' and/or '#' delimiters in "
1035 case NAME_ERR_NO_AT:
1036 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1037 "permission set is missing '@'"));
1041 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1042 "(%d) not defined"), why);
1053 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1057 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
1059 zpool_handle_t *zhp;
1063 * Make sure the pool name is valid.
1065 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
1066 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1067 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1072 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1075 zhp->zpool_hdl = hdl;
1076 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1078 if (zpool_refresh_stats(zhp, &missing) != 0) {
1084 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1085 (void) zfs_error_fmt(hdl, EZFS_NOENT,
1086 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1095 * Like the above, but silent on error. Used when iterating over pools (because
1096 * the configuration cache may be out of date).
1099 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1101 zpool_handle_t *zhp;
1104 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1107 zhp->zpool_hdl = hdl;
1108 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1110 if (zpool_refresh_stats(zhp, &missing) != 0) {
1126 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1130 zpool_open(libzfs_handle_t *hdl, const char *pool)
1132 zpool_handle_t *zhp;
1134 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1137 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1138 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1139 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1148 * Close the handle. Simply frees the memory associated with the handle.
1151 zpool_close(zpool_handle_t *zhp)
1153 nvlist_free(zhp->zpool_config);
1154 nvlist_free(zhp->zpool_old_config);
1155 nvlist_free(zhp->zpool_props);
1160 * Return the name of the pool.
1163 zpool_get_name(zpool_handle_t *zhp)
1165 return (zhp->zpool_name);
1170 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1173 zpool_get_state(zpool_handle_t *zhp)
1175 return (zhp->zpool_state);
1179 * Check if vdev list contains a special vdev
1182 zpool_has_special_vdev(nvlist_t *nvroot)
1187 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, &child,
1189 for (uint_t c = 0; c < children; c++) {
1192 if (nvlist_lookup_string(child[c],
1193 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias) == 0 &&
1194 strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0) {
1203 * Create the named pool, using the provided vdev list. It is assumed
1204 * that the consumer has already validated the contents of the nvlist, so we
1205 * don't have to worry about error semantics.
1208 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1209 nvlist_t *props, nvlist_t *fsprops)
1211 zfs_cmd_t zc = {"\0"};
1212 nvlist_t *zc_fsprops = NULL;
1213 nvlist_t *zc_props = NULL;
1214 nvlist_t *hidden_args = NULL;
1215 uint8_t *wkeydata = NULL;
1220 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1221 "cannot create '%s'"), pool);
1223 if (!zpool_name_valid(hdl, B_FALSE, pool))
1224 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1226 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1230 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1232 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1233 SPA_VERSION_1, flags, msg)) == NULL) {
1242 zoned = ((nvlist_lookup_string(fsprops,
1243 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1244 strcmp(zonestr, "on") == 0);
1246 if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
1247 fsprops, zoned, NULL, NULL, B_TRUE, msg)) == NULL) {
1251 if (nvlist_exists(zc_fsprops,
1252 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS)) &&
1253 !zpool_has_special_vdev(nvroot)) {
1254 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1255 "%s property requires a special vdev"),
1256 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS));
1257 (void) zfs_error(hdl, EZFS_BADPROP, msg);
1262 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1265 if (zfs_crypto_create(hdl, NULL, zc_fsprops, props, B_TRUE,
1266 &wkeydata, &wkeylen) != 0) {
1267 zfs_error(hdl, EZFS_CRYPTOFAILED, msg);
1270 if (nvlist_add_nvlist(zc_props,
1271 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1274 if (wkeydata != NULL) {
1275 if (nvlist_alloc(&hidden_args, NV_UNIQUE_NAME, 0) != 0)
1278 if (nvlist_add_uint8_array(hidden_args, "wkeydata",
1279 wkeydata, wkeylen) != 0)
1282 if (nvlist_add_nvlist(zc_props, ZPOOL_HIDDEN_ARGS,
1288 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1291 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1293 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1295 zcmd_free_nvlists(&zc);
1296 nvlist_free(zc_props);
1297 nvlist_free(zc_fsprops);
1298 nvlist_free(hidden_args);
1299 if (wkeydata != NULL)
1305 * This can happen if the user has specified the same
1306 * device multiple times. We can't reliably detect this
1307 * until we try to add it and see we already have a
1308 * label. This can also happen under if the device is
1309 * part of an active md or lvm device.
1311 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1312 "one or more vdevs refer to the same device, or "
1313 "one of\nthe devices is part of an active md or "
1315 return (zfs_error(hdl, EZFS_BADDEV, msg));
1319 * This happens if the record size is smaller or larger
1320 * than the allowed size range, or not a power of 2.
1322 * NOTE: although zfs_valid_proplist is called earlier,
1323 * this case may have slipped through since the
1324 * pool does not exist yet and it is therefore
1325 * impossible to read properties e.g. max blocksize
1328 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1329 "record size invalid"));
1330 return (zfs_error(hdl, EZFS_BADPROP, msg));
1334 * This occurs when one of the devices is below
1335 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1336 * device was the problem device since there's no
1337 * reliable way to determine device size from userland.
1342 zfs_nicebytes(SPA_MINDEVSIZE, buf,
1345 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1346 "one or more devices is less than the "
1347 "minimum size (%s)"), buf);
1349 return (zfs_error(hdl, EZFS_BADDEV, msg));
1352 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1353 "one or more devices is out of space"));
1354 return (zfs_error(hdl, EZFS_BADDEV, msg));
1357 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1358 "cache device must be a disk or disk slice"));
1359 return (zfs_error(hdl, EZFS_BADDEV, msg));
1362 return (zpool_standard_error(hdl, errno, msg));
1367 zcmd_free_nvlists(&zc);
1368 nvlist_free(zc_props);
1369 nvlist_free(zc_fsprops);
1370 nvlist_free(hidden_args);
1371 if (wkeydata != NULL)
1377 * Destroy the given pool. It is up to the caller to ensure that there are no
1378 * datasets left in the pool.
1381 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1383 zfs_cmd_t zc = {"\0"};
1384 zfs_handle_t *zfp = NULL;
1385 libzfs_handle_t *hdl = zhp->zpool_hdl;
1388 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1389 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1392 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1393 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1395 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1396 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1397 "cannot destroy '%s'"), zhp->zpool_name);
1399 if (errno == EROFS) {
1400 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1401 "one or more devices is read only"));
1402 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1404 (void) zpool_standard_error(hdl, errno, msg);
1413 remove_mountpoint(zfp);
1421 * Create a checkpoint in the given pool.
1424 zpool_checkpoint(zpool_handle_t *zhp)
1426 libzfs_handle_t *hdl = zhp->zpool_hdl;
1430 error = lzc_pool_checkpoint(zhp->zpool_name);
1432 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1433 "cannot checkpoint '%s'"), zhp->zpool_name);
1434 (void) zpool_standard_error(hdl, error, msg);
1442 * Discard the checkpoint from the given pool.
1445 zpool_discard_checkpoint(zpool_handle_t *zhp)
1447 libzfs_handle_t *hdl = zhp->zpool_hdl;
1451 error = lzc_pool_checkpoint_discard(zhp->zpool_name);
1453 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1454 "cannot discard checkpoint in '%s'"), zhp->zpool_name);
1455 (void) zpool_standard_error(hdl, error, msg);
1463 * Add the given vdevs to the pool. The caller must have already performed the
1464 * necessary verification to ensure that the vdev specification is well-formed.
1467 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1469 zfs_cmd_t zc = {"\0"};
1471 libzfs_handle_t *hdl = zhp->zpool_hdl;
1473 nvlist_t **spares, **l2cache;
1474 uint_t nspares, nl2cache;
1476 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1477 "cannot add to '%s'"), zhp->zpool_name);
1479 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1480 SPA_VERSION_SPARES &&
1481 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1482 &spares, &nspares) == 0) {
1483 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1484 "upgraded to add hot spares"));
1485 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1488 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1489 SPA_VERSION_L2CACHE &&
1490 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1491 &l2cache, &nl2cache) == 0) {
1492 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1493 "upgraded to add cache devices"));
1494 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1497 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1499 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1501 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1505 * This can happen if the user has specified the same
1506 * device multiple times. We can't reliably detect this
1507 * until we try to add it and see we already have a
1510 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1511 "one or more vdevs refer to the same device"));
1512 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1516 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1517 "invalid config; a pool with removing/removed "
1518 "vdevs does not support adding raidz vdevs"));
1519 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1524 * This occurs when one of the devices is below
1525 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1526 * device was the problem device since there's no
1527 * reliable way to determine device size from userland.
1532 zfs_nicebytes(SPA_MINDEVSIZE, buf,
1535 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1536 "device is less than the minimum "
1539 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1543 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1544 "pool must be upgraded to add these vdevs"));
1545 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1549 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1550 "cache device must be a disk or disk slice"));
1551 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1555 (void) zpool_standard_error(hdl, errno, msg);
1563 zcmd_free_nvlists(&zc);
1569 * Exports the pool from the system. The caller must ensure that there are no
1570 * mounted datasets in the pool.
1573 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1574 const char *log_str)
1576 zfs_cmd_t zc = {"\0"};
1579 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1580 "cannot export '%s'"), zhp->zpool_name);
1582 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1583 zc.zc_cookie = force;
1584 zc.zc_guid = hardforce;
1585 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1587 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1590 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1591 "use '-f' to override the following errors:\n"
1592 "'%s' has an active shared spare which could be"
1593 " used by other pools once '%s' is exported."),
1594 zhp->zpool_name, zhp->zpool_name);
1595 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1598 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1607 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1609 return (zpool_export_common(zhp, force, B_FALSE, log_str));
1613 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1615 return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1619 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1622 nvlist_t *nv = NULL;
1628 if (!hdl->libzfs_printerr || config == NULL)
1631 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1632 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1636 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1638 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1640 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1641 strftime(timestr, 128, "%c", &t) != 0) {
1643 (void) printf(dgettext(TEXT_DOMAIN,
1644 "Would be able to return %s "
1645 "to its state as of %s.\n"),
1648 (void) printf(dgettext(TEXT_DOMAIN,
1649 "Pool %s returned to its state as of %s.\n"),
1653 (void) printf(dgettext(TEXT_DOMAIN,
1654 "%s approximately %lld "),
1655 dryrun ? "Would discard" : "Discarded",
1656 ((longlong_t)loss + 30) / 60);
1657 (void) printf(dgettext(TEXT_DOMAIN,
1658 "minutes of transactions.\n"));
1659 } else if (loss > 0) {
1660 (void) printf(dgettext(TEXT_DOMAIN,
1661 "%s approximately %lld "),
1662 dryrun ? "Would discard" : "Discarded",
1664 (void) printf(dgettext(TEXT_DOMAIN,
1665 "seconds of transactions.\n"));
1671 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1674 nvlist_t *nv = NULL;
1676 uint64_t edata = UINT64_MAX;
1681 if (!hdl->libzfs_printerr)
1685 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1687 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1689 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1690 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1691 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1692 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1695 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1696 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1699 (void) printf(dgettext(TEXT_DOMAIN,
1700 "Recovery is possible, but will result in some data loss.\n"));
1702 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1703 strftime(timestr, 128, "%c", &t) != 0) {
1704 (void) printf(dgettext(TEXT_DOMAIN,
1705 "\tReturning the pool to its state as of %s\n"
1706 "\tshould correct the problem. "),
1709 (void) printf(dgettext(TEXT_DOMAIN,
1710 "\tReverting the pool to an earlier state "
1711 "should correct the problem.\n\t"));
1715 (void) printf(dgettext(TEXT_DOMAIN,
1716 "Approximately %lld minutes of data\n"
1717 "\tmust be discarded, irreversibly. "),
1718 ((longlong_t)loss + 30) / 60);
1719 } else if (loss > 0) {
1720 (void) printf(dgettext(TEXT_DOMAIN,
1721 "Approximately %lld seconds of data\n"
1722 "\tmust be discarded, irreversibly. "),
1725 if (edata != 0 && edata != UINT64_MAX) {
1727 (void) printf(dgettext(TEXT_DOMAIN,
1728 "After rewind, at least\n"
1729 "\tone persistent user-data error will remain. "));
1731 (void) printf(dgettext(TEXT_DOMAIN,
1732 "After rewind, several\n"
1733 "\tpersistent user-data errors will remain. "));
1736 (void) printf(dgettext(TEXT_DOMAIN,
1737 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1738 reason >= 0 ? "clear" : "import", name);
1740 (void) printf(dgettext(TEXT_DOMAIN,
1741 "A scrub of the pool\n"
1742 "\tis strongly recommended after recovery.\n"));
1746 (void) printf(dgettext(TEXT_DOMAIN,
1747 "Destroy and re-create the pool from\n\ta backup source.\n"));
1751 * zpool_import() is a contracted interface. Should be kept the same
1754 * Applications should use zpool_import_props() to import a pool with
1755 * new properties value to be set.
1758 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1761 nvlist_t *props = NULL;
1764 if (altroot != NULL) {
1765 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1766 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1767 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1771 if (nvlist_add_string(props,
1772 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1773 nvlist_add_string(props,
1774 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1776 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1777 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1782 ret = zpool_import_props(hdl, config, newname, props,
1789 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1795 uint64_t is_log = 0;
1797 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1801 (void) printf("\t%*s%s%s\n", indent, "", name,
1802 is_log ? " [log]" : "");
1804 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1805 &child, &children) != 0)
1808 for (c = 0; c < children; c++) {
1809 vname = zpool_vdev_name(hdl, NULL, child[c], VDEV_NAME_TYPE_ID);
1810 print_vdev_tree(hdl, vname, child[c], indent + 2);
1816 zpool_print_unsup_feat(nvlist_t *config)
1818 nvlist_t *nvinfo, *unsup_feat;
1821 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1823 verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1826 for (nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1827 nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1830 verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1831 verify(nvpair_value_string(nvp, &desc) == 0);
1833 if (strlen(desc) > 0)
1834 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1836 (void) printf("\t%s\n", nvpair_name(nvp));
1841 * Import the given pool using the known configuration and a list of
1842 * properties to be set. The configuration should have come from
1843 * zpool_find_import(). The 'newname' parameters control whether the pool
1844 * is imported with a different name.
1847 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1848 nvlist_t *props, int flags)
1850 zfs_cmd_t zc = {"\0"};
1851 zpool_load_policy_t policy;
1852 nvlist_t *nv = NULL;
1853 nvlist_t *nvinfo = NULL;
1854 nvlist_t *missing = NULL;
1861 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1864 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1865 "cannot import pool '%s'"), origname);
1867 if (newname != NULL) {
1868 if (!zpool_name_valid(hdl, B_FALSE, newname))
1869 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1870 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1872 thename = (char *)newname;
1877 if (props != NULL) {
1879 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1881 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1884 if ((props = zpool_valid_proplist(hdl, origname,
1885 props, version, flags, errbuf)) == NULL)
1887 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1894 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1896 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1899 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1900 zcmd_free_nvlists(&zc);
1903 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1904 zcmd_free_nvlists(&zc);
1908 zc.zc_cookie = flags;
1909 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1911 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1912 zcmd_free_nvlists(&zc);
1919 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1921 zcmd_free_nvlists(&zc);
1923 zpool_get_load_policy(config, &policy);
1930 * Dry-run failed, but we print out what success
1931 * looks like if we found a best txg
1933 if (policy.zlp_rewind & ZPOOL_TRY_REWIND) {
1934 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1940 if (newname == NULL)
1941 (void) snprintf(desc, sizeof (desc),
1942 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1945 (void) snprintf(desc, sizeof (desc),
1946 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1951 if (nv != NULL && nvlist_lookup_nvlist(nv,
1952 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1953 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1954 (void) printf(dgettext(TEXT_DOMAIN, "This "
1955 "pool uses the following feature(s) not "
1956 "supported by this system:\n"));
1957 zpool_print_unsup_feat(nv);
1958 if (nvlist_exists(nvinfo,
1959 ZPOOL_CONFIG_CAN_RDONLY)) {
1960 (void) printf(dgettext(TEXT_DOMAIN,
1961 "All unsupported features are only "
1962 "required for writing to the pool."
1963 "\nThe pool can be imported using "
1964 "'-o readonly=on'.\n"));
1968 * Unsupported version.
1970 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1974 if (nv != NULL && nvlist_lookup_nvlist(nv,
1975 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0) {
1976 char *hostname = "<unknown>";
1977 uint64_t hostid = 0;
1978 mmp_state_t mmp_state;
1980 mmp_state = fnvlist_lookup_uint64(nvinfo,
1981 ZPOOL_CONFIG_MMP_STATE);
1983 if (nvlist_exists(nvinfo,
1984 ZPOOL_CONFIG_MMP_HOSTNAME))
1985 hostname = fnvlist_lookup_string(nvinfo,
1986 ZPOOL_CONFIG_MMP_HOSTNAME);
1988 if (nvlist_exists(nvinfo,
1989 ZPOOL_CONFIG_MMP_HOSTID))
1990 hostid = fnvlist_lookup_uint64(nvinfo,
1991 ZPOOL_CONFIG_MMP_HOSTID);
1993 if (mmp_state == MMP_STATE_ACTIVE) {
1994 (void) snprintf(aux, sizeof (aux),
1995 dgettext(TEXT_DOMAIN, "pool is imp"
1996 "orted on host '%s' (hostid=%lx).\n"
1997 "Export the pool on the other "
1998 "system, then run 'zpool import'."),
1999 hostname, (unsigned long) hostid);
2000 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
2001 (void) snprintf(aux, sizeof (aux),
2002 dgettext(TEXT_DOMAIN, "pool has "
2003 "the multihost property on and "
2004 "the\nsystem's hostid is not set. "
2005 "Set a unique system hostid with "
2006 "the zgenhostid(8) command.\n"));
2009 (void) zfs_error_aux(hdl, aux);
2011 (void) zfs_error(hdl, EZFS_ACTIVE_POOL, desc);
2015 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
2019 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2020 "one or more devices is read only"));
2021 (void) zfs_error(hdl, EZFS_BADDEV, desc);
2025 if (nv && nvlist_lookup_nvlist(nv,
2026 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
2027 nvlist_lookup_nvlist(nvinfo,
2028 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
2029 (void) printf(dgettext(TEXT_DOMAIN,
2030 "The devices below are missing or "
2031 "corrupted, use '-m' to import the pool "
2033 print_vdev_tree(hdl, NULL, missing, 2);
2034 (void) printf("\n");
2036 (void) zpool_standard_error(hdl, error, desc);
2040 (void) zpool_standard_error(hdl, error, desc);
2044 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2045 "one or more devices are already in use\n"));
2046 (void) zfs_error(hdl, EZFS_BADDEV, desc);
2049 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2050 "new name of at least one dataset is longer than "
2051 "the maximum allowable length"));
2052 (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
2055 (void) zpool_standard_error(hdl, error, desc);
2056 zpool_explain_recover(hdl,
2057 newname ? origname : thename, -error, nv);
2064 zpool_handle_t *zhp;
2067 * This should never fail, but play it safe anyway.
2069 if (zpool_open_silent(hdl, thename, &zhp) != 0)
2071 else if (zhp != NULL)
2073 if (policy.zlp_rewind &
2074 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2075 zpool_rewind_exclaim(hdl, newname ? origname : thename,
2076 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0), nv);
2086 * Translate vdev names to guids. If a vdev_path is determined to be
2087 * unsuitable then a vd_errlist is allocated and the vdev path and errno
2091 zpool_translate_vdev_guids(zpool_handle_t *zhp, nvlist_t *vds,
2092 nvlist_t *vdev_guids, nvlist_t *guids_to_paths, nvlist_t **vd_errlist)
2094 nvlist_t *errlist = NULL;
2097 for (nvpair_t *elem = nvlist_next_nvpair(vds, NULL); elem != NULL;
2098 elem = nvlist_next_nvpair(vds, elem)) {
2099 boolean_t spare, cache;
2101 char *vd_path = nvpair_name(elem);
2102 nvlist_t *tgt = zpool_find_vdev(zhp, vd_path, &spare, &cache,
2105 if ((tgt == NULL) || cache || spare) {
2106 if (errlist == NULL) {
2107 errlist = fnvlist_alloc();
2111 uint64_t err = (tgt == NULL) ? EZFS_NODEVICE :
2112 (spare ? EZFS_ISSPARE : EZFS_ISL2CACHE);
2113 fnvlist_add_int64(errlist, vd_path, err);
2117 uint64_t guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
2118 fnvlist_add_uint64(vdev_guids, vd_path, guid);
2120 char msg[MAXNAMELEN];
2121 (void) snprintf(msg, sizeof (msg), "%llu", (u_longlong_t)guid);
2122 fnvlist_add_string(guids_to_paths, msg, vd_path);
2126 verify(errlist != NULL);
2127 if (vd_errlist != NULL)
2128 *vd_errlist = errlist;
2130 fnvlist_free(errlist);
2137 xlate_init_err(int err)
2141 return (EZFS_NODEVICE);
2144 return (EZFS_BADDEV);
2146 return (EZFS_INITIALIZING);
2148 return (EZFS_NO_INITIALIZE);
2154 * Begin, suspend, or cancel the initialization (initializing of all free
2155 * blocks) for the given vdevs in the given pool.
2158 zpool_initialize_impl(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2159 nvlist_t *vds, boolean_t wait)
2163 nvlist_t *vdev_guids = fnvlist_alloc();
2164 nvlist_t *guids_to_paths = fnvlist_alloc();
2165 nvlist_t *vd_errlist = NULL;
2169 err = zpool_translate_vdev_guids(zhp, vds, vdev_guids,
2170 guids_to_paths, &vd_errlist);
2173 verify(vd_errlist != NULL);
2177 err = lzc_initialize(zhp->zpool_name, cmd_type,
2178 vdev_guids, &errlist);
2181 if (errlist != NULL) {
2182 vd_errlist = fnvlist_lookup_nvlist(errlist,
2183 ZPOOL_INITIALIZE_VDEVS);
2186 (void) zpool_standard_error(zhp->zpool_hdl, err,
2187 dgettext(TEXT_DOMAIN, "operation failed"));
2192 for (elem = nvlist_next_nvpair(vdev_guids, NULL); elem != NULL;
2193 elem = nvlist_next_nvpair(vdev_guids, elem)) {
2195 uint64_t guid = fnvpair_value_uint64(elem);
2197 err = lzc_wait_tag(zhp->zpool_name,
2198 ZPOOL_WAIT_INITIALIZE, guid, NULL);
2200 (void) zpool_standard_error_fmt(zhp->zpool_hdl,
2201 err, dgettext(TEXT_DOMAIN, "error "
2202 "waiting for '%s' to initialize"),
2212 for (elem = nvlist_next_nvpair(vd_errlist, NULL); elem != NULL;
2213 elem = nvlist_next_nvpair(vd_errlist, elem)) {
2214 int64_t vd_error = xlate_init_err(fnvpair_value_int64(elem));
2217 if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem),
2219 path = nvpair_name(elem);
2221 (void) zfs_error_fmt(zhp->zpool_hdl, vd_error,
2222 "cannot initialize '%s'", path);
2226 fnvlist_free(vdev_guids);
2227 fnvlist_free(guids_to_paths);
2229 if (vd_errlist != NULL)
2230 fnvlist_free(vd_errlist);
2232 return (err == 0 ? 0 : -1);
2236 zpool_initialize(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2239 return (zpool_initialize_impl(zhp, cmd_type, vds, B_FALSE));
2243 zpool_initialize_wait(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2246 return (zpool_initialize_impl(zhp, cmd_type, vds, B_TRUE));
2250 xlate_trim_err(int err)
2254 return (EZFS_NODEVICE);
2257 return (EZFS_BADDEV);
2259 return (EZFS_TRIMMING);
2261 return (EZFS_NO_TRIM);
2263 return (EZFS_TRIM_NOTSUP);
2269 * Begin, suspend, or cancel the TRIM (discarding of all free blocks) for
2270 * the given vdevs in the given pool.
2273 zpool_trim(zpool_handle_t *zhp, pool_trim_func_t cmd_type, nvlist_t *vds,
2274 trimflags_t *trim_flags)
2279 nvlist_t *vdev_guids = fnvlist_alloc();
2280 nvlist_t *guids_to_paths = fnvlist_alloc();
2281 nvlist_t *vd_errlist = NULL;
2285 err = zpool_translate_vdev_guids(zhp, vds, vdev_guids,
2286 guids_to_paths, &vd_errlist);
2288 err = lzc_trim(zhp->zpool_name, cmd_type, trim_flags->rate,
2289 trim_flags->secure, vdev_guids, &errlist);
2291 fnvlist_free(vdev_guids);
2292 fnvlist_free(guids_to_paths);
2296 if (errlist != NULL) {
2297 vd_errlist = fnvlist_lookup_nvlist(errlist,
2301 (void) snprintf(msg, sizeof (msg),
2302 dgettext(TEXT_DOMAIN, "operation failed"));
2304 verify(vd_errlist != NULL);
2307 for (elem = nvlist_next_nvpair(vd_errlist, NULL);
2308 elem != NULL; elem = nvlist_next_nvpair(vd_errlist, elem)) {
2309 int64_t vd_error = xlate_trim_err(fnvpair_value_int64(elem));
2313 * If only the pool was specified, and it was not a secure
2314 * trim then suppress warnings for individual vdevs which
2315 * do not support trimming.
2317 if (vd_error == EZFS_TRIM_NOTSUP &&
2318 trim_flags->fullpool &&
2319 !trim_flags->secure) {
2323 if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem),
2325 path = nvpair_name(elem);
2327 (void) zfs_error_fmt(zhp->zpool_hdl, vd_error,
2328 "cannot trim '%s'", path);
2331 fnvlist_free(vdev_guids);
2332 fnvlist_free(guids_to_paths);
2334 if (vd_errlist != NULL) {
2335 fnvlist_free(vd_errlist);
2339 return (zpool_standard_error(zhp->zpool_hdl, err, msg));
2346 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd)
2348 zfs_cmd_t zc = {"\0"};
2351 libzfs_handle_t *hdl = zhp->zpool_hdl;
2353 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2354 zc.zc_cookie = func;
2357 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0)
2362 /* ECANCELED on a scrub means we resumed a paused scrub */
2363 if (err == ECANCELED && func == POOL_SCAN_SCRUB &&
2364 cmd == POOL_SCRUB_NORMAL)
2367 if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL)
2370 if (func == POOL_SCAN_SCRUB) {
2371 if (cmd == POOL_SCRUB_PAUSE) {
2372 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2373 "cannot pause scrubbing %s"), zc.zc_name);
2375 assert(cmd == POOL_SCRUB_NORMAL);
2376 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2377 "cannot scrub %s"), zc.zc_name);
2379 } else if (func == POOL_SCAN_RESILVER) {
2380 assert(cmd == POOL_SCRUB_NORMAL);
2381 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2382 "cannot restart resilver on %s"), zc.zc_name);
2383 } else if (func == POOL_SCAN_NONE) {
2384 (void) snprintf(msg, sizeof (msg),
2385 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
2388 assert(!"unexpected result");
2393 pool_scan_stat_t *ps = NULL;
2396 verify(nvlist_lookup_nvlist(zhp->zpool_config,
2397 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2398 (void) nvlist_lookup_uint64_array(nvroot,
2399 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
2400 if (ps && ps->pss_func == POOL_SCAN_SCRUB) {
2401 if (cmd == POOL_SCRUB_PAUSE)
2402 return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg));
2404 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
2406 return (zfs_error(hdl, EZFS_RESILVERING, msg));
2408 } else if (err == ENOENT) {
2409 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
2410 } else if (err == ENOTSUP && func == POOL_SCAN_RESILVER) {
2411 return (zfs_error(hdl, EZFS_NO_RESILVER_DEFER, msg));
2413 return (zpool_standard_error(hdl, err, msg));
2418 * Find a vdev that matches the search criteria specified. We use the
2419 * the nvpair name to determine how we should look for the device.
2420 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2421 * spare; but FALSE if its an INUSE spare.
2424 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
2425 boolean_t *l2cache, boolean_t *log)
2432 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
2434 /* Nothing to look for */
2435 if (search == NULL || pair == NULL)
2438 /* Obtain the key we will use to search */
2439 srchkey = nvpair_name(pair);
2441 switch (nvpair_type(pair)) {
2442 case DATA_TYPE_UINT64:
2443 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
2444 uint64_t srchval, theguid;
2446 verify(nvpair_value_uint64(pair, &srchval) == 0);
2447 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2449 if (theguid == srchval)
2454 case DATA_TYPE_STRING: {
2455 char *srchval, *val;
2457 verify(nvpair_value_string(pair, &srchval) == 0);
2458 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2462 * Search for the requested value. Special cases:
2464 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2465 * "-part1", or "p1". The suffix is hidden from the user,
2466 * but included in the string, so this matches around it.
2467 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2468 * is used to check all possible expanded paths.
2469 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2471 * Otherwise, all other searches are simple string compares.
2473 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) {
2474 uint64_t wholedisk = 0;
2476 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2478 if (zfs_strcmp_pathname(srchval, val, wholedisk) == 0)
2481 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2482 char *type, *idx, *end, *p;
2483 uint64_t id, vdev_id;
2486 * Determine our vdev type, keeping in mind
2487 * that the srchval is composed of a type and
2488 * vdev id pair (i.e. mirror-4).
2490 if ((type = strdup(srchval)) == NULL)
2493 if ((p = strrchr(type, '-')) == NULL) {
2501 * If the types don't match then keep looking.
2503 if (strncmp(val, type, strlen(val)) != 0) {
2508 verify(zpool_vdev_is_interior(type));
2509 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2513 vdev_id = strtoull(idx, &end, 10);
2520 * Now verify that we have the correct vdev id.
2529 if (strcmp(srchval, val) == 0)
2538 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2539 &child, &children) != 0)
2542 for (c = 0; c < children; c++) {
2543 if ((ret = vdev_to_nvlist_iter(child[c], search,
2544 avail_spare, l2cache, NULL)) != NULL) {
2546 * The 'is_log' value is only set for the toplevel
2547 * vdev, not the leaf vdevs. So we always lookup the
2548 * log device from the root of the vdev tree (where
2549 * 'log' is non-NULL).
2552 nvlist_lookup_uint64(child[c],
2553 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2561 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2562 &child, &children) == 0) {
2563 for (c = 0; c < children; c++) {
2564 if ((ret = vdev_to_nvlist_iter(child[c], search,
2565 avail_spare, l2cache, NULL)) != NULL) {
2566 *avail_spare = B_TRUE;
2572 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2573 &child, &children) == 0) {
2574 for (c = 0; c < children; c++) {
2575 if ((ret = vdev_to_nvlist_iter(child[c], search,
2576 avail_spare, l2cache, NULL)) != NULL) {
2587 * Given a physical path or guid, find the associated vdev.
2590 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2591 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2593 nvlist_t *search, *nvroot, *ret;
2597 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2599 guid = strtoull(ppath, &end, 0);
2600 if (guid != 0 && *end == '\0') {
2601 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2603 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH,
2607 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2610 *avail_spare = B_FALSE;
2614 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2615 nvlist_free(search);
2621 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2624 zpool_vdev_is_interior(const char *name)
2626 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2627 strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 ||
2629 VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 ||
2630 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2636 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2637 boolean_t *l2cache, boolean_t *log)
2640 nvlist_t *nvroot, *search, *ret;
2643 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2645 guid = strtoull(path, &end, 0);
2646 if (guid != 0 && *end == '\0') {
2647 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2648 } else if (zpool_vdev_is_interior(path)) {
2649 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2651 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2654 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2657 *avail_spare = B_FALSE;
2661 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2662 nvlist_free(search);
2668 vdev_is_online(nvlist_t *nv)
2672 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2673 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2674 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2681 * Helper function for zpool_get_physpaths().
2684 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2685 size_t *bytes_written)
2687 size_t bytes_left, pos, rsz;
2691 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2693 return (EZFS_NODEVICE);
2695 pos = *bytes_written;
2696 bytes_left = physpath_size - pos;
2697 format = (pos == 0) ? "%s" : " %s";
2699 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2700 *bytes_written += rsz;
2702 if (rsz >= bytes_left) {
2703 /* if physpath was not copied properly, clear it */
2704 if (bytes_left != 0) {
2707 return (EZFS_NOSPC);
2713 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2714 size_t *rsz, boolean_t is_spare)
2719 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2720 return (EZFS_INVALCONFIG);
2722 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2724 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2725 * For a spare vdev, we only want to boot from the active
2730 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2733 return (EZFS_INVALCONFIG);
2736 if (vdev_is_online(nv)) {
2737 if ((ret = vdev_get_one_physpath(nv, physpath,
2738 phypath_size, rsz)) != 0)
2741 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2742 strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
2743 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2744 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2749 if (nvlist_lookup_nvlist_array(nv,
2750 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2751 return (EZFS_INVALCONFIG);
2753 for (i = 0; i < count; i++) {
2754 ret = vdev_get_physpaths(child[i], physpath,
2755 phypath_size, rsz, is_spare);
2756 if (ret == EZFS_NOSPC)
2761 return (EZFS_POOL_INVALARG);
2765 * Get phys_path for a root pool config.
2766 * Return 0 on success; non-zero on failure.
2769 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2772 nvlist_t *vdev_root;
2779 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2781 return (EZFS_INVALCONFIG);
2783 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2784 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2785 &child, &count) != 0)
2786 return (EZFS_INVALCONFIG);
2789 * root pool can only have a single top-level vdev.
2791 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2792 return (EZFS_POOL_INVALARG);
2794 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2797 /* No online devices */
2799 return (EZFS_NODEVICE);
2805 * Get phys_path for a root pool
2806 * Return 0 on success; non-zero on failure.
2809 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2811 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2816 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
2818 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
2819 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
2823 zpool_vdev_path_to_guid_impl(zpool_handle_t *zhp, const char *path,
2824 boolean_t *is_spare, boolean_t *is_l2cache, boolean_t *is_log)
2827 boolean_t spare = B_FALSE, l2cache = B_FALSE, log = B_FALSE;
2830 if ((tgt = zpool_find_vdev(zhp, path, &spare, &l2cache,
2834 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &guid) == 0);
2835 if (is_spare != NULL)
2837 if (is_l2cache != NULL)
2838 *is_l2cache = l2cache;
2845 /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
2847 zpool_vdev_path_to_guid(zpool_handle_t *zhp, const char *path)
2849 return (zpool_vdev_path_to_guid_impl(zhp, path, NULL, NULL, NULL));
2853 * Bring the specified vdev online. The 'flags' parameter is a set of the
2854 * ZFS_ONLINE_* flags.
2857 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2858 vdev_state_t *newstate)
2860 zfs_cmd_t zc = {"\0"};
2864 boolean_t avail_spare, l2cache, islog;
2865 libzfs_handle_t *hdl = zhp->zpool_hdl;
2868 if (flags & ZFS_ONLINE_EXPAND) {
2869 (void) snprintf(msg, sizeof (msg),
2870 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2872 (void) snprintf(msg, sizeof (msg),
2873 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2876 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2877 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2879 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2881 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2884 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2886 if ((flags & ZFS_ONLINE_EXPAND ||
2887 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) &&
2888 nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) {
2889 uint64_t wholedisk = 0;
2891 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2895 * XXX - L2ARC 1.0 devices can't support expansion.
2898 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2899 "cannot expand cache devices"));
2900 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2904 const char *fullpath = path;
2905 char buf[MAXPATHLEN];
2907 if (path[0] != '/') {
2908 error = zfs_resolve_shortname(path, buf,
2911 return (zfs_error(hdl, EZFS_NODEVICE,
2917 error = zpool_relabel_disk(hdl, fullpath, msg);
2923 zc.zc_cookie = VDEV_STATE_ONLINE;
2926 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2927 if (errno == EINVAL) {
2928 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2929 "from this pool into a new one. Use '%s' "
2930 "instead"), "zpool detach");
2931 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2933 return (zpool_standard_error(hdl, errno, msg));
2936 *newstate = zc.zc_cookie;
2941 * Take the specified vdev offline
2944 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2946 zfs_cmd_t zc = {"\0"};
2949 boolean_t avail_spare, l2cache;
2950 libzfs_handle_t *hdl = zhp->zpool_hdl;
2952 (void) snprintf(msg, sizeof (msg),
2953 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2955 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2956 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2958 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2960 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2963 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2965 zc.zc_cookie = VDEV_STATE_OFFLINE;
2966 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2968 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2975 * There are no other replicas of this device.
2977 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2981 * The log device has unplayed logs
2983 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2986 return (zpool_standard_error(hdl, errno, msg));
2991 * Mark the given vdev faulted.
2994 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2996 zfs_cmd_t zc = {"\0"};
2998 libzfs_handle_t *hdl = zhp->zpool_hdl;
3000 (void) snprintf(msg, sizeof (msg),
3001 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
3003 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3005 zc.zc_cookie = VDEV_STATE_FAULTED;
3008 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3015 * There are no other replicas of this device.
3017 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
3020 return (zpool_standard_error(hdl, errno, msg));
3026 * Mark the given vdev degraded.
3029 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
3031 zfs_cmd_t zc = {"\0"};
3033 libzfs_handle_t *hdl = zhp->zpool_hdl;
3035 (void) snprintf(msg, sizeof (msg),
3036 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid);
3038 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3040 zc.zc_cookie = VDEV_STATE_DEGRADED;
3043 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3046 return (zpool_standard_error(hdl, errno, msg));
3050 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
3054 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
3060 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
3062 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
3065 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
3066 children == 2 && child[which] == tgt)
3069 for (c = 0; c < children; c++)
3070 if (is_replacing_spare(child[c], tgt, which))
3078 * Attach new_disk (fully described by nvroot) to old_disk.
3079 * If 'replacing' is specified, the new disk will replace the old one.
3082 zpool_vdev_attach(zpool_handle_t *zhp,
3083 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
3085 zfs_cmd_t zc = {"\0"};
3089 boolean_t avail_spare, l2cache, islog;
3094 nvlist_t *config_root;
3095 libzfs_handle_t *hdl = zhp->zpool_hdl;
3096 boolean_t rootpool = zpool_is_bootable(zhp);
3099 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3100 "cannot replace %s with %s"), old_disk, new_disk);
3102 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3103 "cannot attach %s to %s"), new_disk, old_disk);
3105 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3106 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
3108 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3111 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3114 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
3116 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3117 zc.zc_cookie = replacing;
3119 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3120 &child, &children) != 0 || children != 1) {
3121 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3122 "new device must be a single disk"));
3123 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
3126 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
3127 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
3129 if ((newname = zpool_vdev_name(NULL, NULL, child[0], 0)) == NULL)
3133 * If the target is a hot spare that has been swapped in, we can only
3134 * replace it with another hot spare.
3137 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
3138 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
3139 NULL) == NULL || !avail_spare) &&
3140 is_replacing_spare(config_root, tgt, 1)) {
3141 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3142 "can only be replaced by another hot spare"));
3144 return (zfs_error(hdl, EZFS_BADTARGET, msg));
3149 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
3152 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
3154 zcmd_free_nvlists(&zc);
3159 * XXX need a better way to prevent user from
3160 * booting up a half-baked vdev.
3162 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
3163 "sure to wait until resilver is done "
3164 "before rebooting.\n"));
3172 * Can't attach to or replace this type of vdev.
3175 uint64_t version = zpool_get_prop_int(zhp,
3176 ZPOOL_PROP_VERSION, NULL);
3179 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3180 "cannot replace a log with a spare"));
3181 else if (version >= SPA_VERSION_MULTI_REPLACE)
3182 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3183 "already in replacing/spare config; wait "
3184 "for completion or use 'zpool detach'"));
3186 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3187 "cannot replace a replacing device"));
3189 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3190 "can only attach to mirrors and top-level "
3193 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
3198 * The new device must be a single disk.
3200 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3201 "new device must be a single disk"));
3202 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3206 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy, "
3207 "or device removal is in progress"),
3209 (void) zfs_error(hdl, EZFS_BADDEV, msg);
3214 * The new device is too small.
3216 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3217 "device is too small"));
3218 (void) zfs_error(hdl, EZFS_BADDEV, msg);
3223 * The new device has a different optimal sector size.
3225 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3226 "new device has a different optimal sector size; use the "
3227 "option '-o ashift=N' to override the optimal size"));
3228 (void) zfs_error(hdl, EZFS_BADDEV, msg);
3233 * The resulting top-level vdev spec won't fit in the label.
3235 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
3239 (void) zpool_standard_error(hdl, errno, msg);
3246 * Detach the specified device.
3249 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
3251 zfs_cmd_t zc = {"\0"};
3254 boolean_t avail_spare, l2cache;
3255 libzfs_handle_t *hdl = zhp->zpool_hdl;
3257 (void) snprintf(msg, sizeof (msg),
3258 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
3260 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3261 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3263 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3266 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3269 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
3271 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3273 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
3280 * Can't detach from this type of vdev.
3282 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
3283 "applicable to mirror and replacing vdevs"));
3284 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
3289 * There are no other replicas of this device.
3291 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
3295 (void) zpool_standard_error(hdl, errno, msg);
3302 * Find a mirror vdev in the source nvlist.
3304 * The mchild array contains a list of disks in one of the top-level mirrors
3305 * of the source pool. The schild array contains a list of disks that the
3306 * user specified on the command line. We loop over the mchild array to
3307 * see if any entry in the schild array matches.
3309 * If a disk in the mchild array is found in the schild array, we return
3310 * the index of that entry. Otherwise we return -1.
3313 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
3314 nvlist_t **schild, uint_t schildren)
3318 for (mc = 0; mc < mchildren; mc++) {
3320 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3323 for (sc = 0; sc < schildren; sc++) {
3324 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3326 boolean_t result = (strcmp(mpath, spath) == 0);
3342 * Split a mirror pool. If newroot points to null, then a new nvlist
3343 * is generated and it is the responsibility of the caller to free it.
3346 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
3347 nvlist_t *props, splitflags_t flags)
3349 zfs_cmd_t zc = {"\0"};
3351 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
3352 nvlist_t **varray = NULL, *zc_props = NULL;
3353 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
3354 libzfs_handle_t *hdl = zhp->zpool_hdl;
3355 uint64_t vers, readonly = B_FALSE;
3356 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
3359 (void) snprintf(msg, sizeof (msg),
3360 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
3362 if (!zpool_name_valid(hdl, B_FALSE, newname))
3363 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
3365 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
3366 (void) fprintf(stderr, gettext("Internal error: unable to "
3367 "retrieve pool configuration\n"));
3371 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
3373 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
3376 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
3377 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
3378 props, vers, flags, msg)) == NULL)
3380 (void) nvlist_lookup_uint64(zc_props,
3381 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3383 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3384 "property %s can only be set at import time"),
3385 zpool_prop_to_name(ZPOOL_PROP_READONLY));
3390 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3392 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3393 "Source pool is missing vdev tree"));
3394 nvlist_free(zc_props);
3398 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
3401 if (*newroot == NULL ||
3402 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
3403 &newchild, &newchildren) != 0)
3406 for (c = 0; c < children; c++) {
3407 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
3409 nvlist_t **mchild, *vdev;
3414 * Unlike cache & spares, slogs are stored in the
3415 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3417 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3419 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
3421 if (is_log || is_hole) {
3423 * Create a hole vdev and put it in the config.
3425 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
3427 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
3428 VDEV_TYPE_HOLE) != 0)
3430 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
3435 varray[vcount++] = vdev;
3439 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
3441 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
3442 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3443 "Source pool must be composed only of mirrors\n"));
3444 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3448 verify(nvlist_lookup_nvlist_array(child[c],
3449 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3451 /* find or add an entry for this top-level vdev */
3452 if (newchildren > 0 &&
3453 (entry = find_vdev_entry(zhp, mchild, mchildren,
3454 newchild, newchildren)) >= 0) {
3455 /* We found a disk that the user specified. */
3456 vdev = mchild[entry];
3459 /* User didn't specify a disk for this vdev. */
3460 vdev = mchild[mchildren - 1];
3463 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3467 /* did we find every disk the user specified? */
3468 if (found != newchildren) {
3469 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3470 "include at most one disk from each mirror"));
3471 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3475 /* Prepare the nvlist for populating. */
3476 if (*newroot == NULL) {
3477 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3480 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3481 VDEV_TYPE_ROOT) != 0)
3484 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3487 /* Add all the children we found */
3488 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3489 lastlog == 0 ? vcount : lastlog) != 0)
3493 * If we're just doing a dry run, exit now with success.
3496 memory_err = B_FALSE;
3501 /* now build up the config list & call the ioctl */
3502 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3505 if (nvlist_add_nvlist(newconfig,
3506 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3507 nvlist_add_string(newconfig,
3508 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3509 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3513 * The new pool is automatically part of the namespace unless we
3514 * explicitly export it.
3517 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3518 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3519 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3520 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3522 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3525 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3526 retval = zpool_standard_error(hdl, errno, msg);
3531 memory_err = B_FALSE;
3534 if (varray != NULL) {
3537 for (v = 0; v < vcount; v++)
3538 nvlist_free(varray[v]);
3541 zcmd_free_nvlists(&zc);
3542 nvlist_free(zc_props);
3543 nvlist_free(newconfig);
3545 nvlist_free(*newroot);
3553 return (no_memory(hdl));
3559 * Remove the given device.
3562 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3564 zfs_cmd_t zc = {"\0"};
3567 boolean_t avail_spare, l2cache, islog;
3568 libzfs_handle_t *hdl = zhp->zpool_hdl;
3571 (void) snprintf(msg, sizeof (msg),
3572 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3574 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3575 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3577 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3579 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3580 if (islog && version < SPA_VERSION_HOLES) {
3581 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3582 "pool must be upgraded to support log removal"));
3583 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3586 if (!islog && !avail_spare && !l2cache && zpool_is_bootable(zhp)) {
3587 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3588 "root pool can not have removed devices, "
3589 "because GRUB does not understand them"));
3590 return (zfs_error(hdl, EINVAL, msg));
3593 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3595 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3601 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3602 "invalid config; all top-level vdevs must "
3603 "have the same sector size and not be raidz."));
3604 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3609 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3610 "Mount encrypted datasets to replay logs."));
3612 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3613 "Pool busy; removal may already be in progress"));
3615 (void) zfs_error(hdl, EZFS_BUSY, msg);
3620 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3621 "Mount encrypted datasets to replay logs."));
3622 (void) zfs_error(hdl, EZFS_BUSY, msg);
3624 (void) zpool_standard_error(hdl, errno, msg);
3629 (void) zpool_standard_error(hdl, errno, msg);
3635 zpool_vdev_remove_cancel(zpool_handle_t *zhp)
3639 libzfs_handle_t *hdl = zhp->zpool_hdl;
3641 (void) snprintf(msg, sizeof (msg),
3642 dgettext(TEXT_DOMAIN, "cannot cancel removal"));
3644 bzero(&zc, sizeof (zc));
3645 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3648 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3651 return (zpool_standard_error(hdl, errno, msg));
3655 zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path,
3660 boolean_t avail_spare, l2cache, islog;
3661 libzfs_handle_t *hdl = zhp->zpool_hdl;
3663 (void) snprintf(msg, sizeof (msg),
3664 dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"),
3667 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3669 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3671 if (avail_spare || l2cache || islog) {
3676 if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) {
3677 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3678 "indirect size not available"));
3679 return (zfs_error(hdl, EINVAL, msg));
3685 * Clear the errors for the pool, or the particular device if specified.
3688 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3690 zfs_cmd_t zc = {"\0"};
3693 zpool_load_policy_t policy;
3694 boolean_t avail_spare, l2cache;
3695 libzfs_handle_t *hdl = zhp->zpool_hdl;
3696 nvlist_t *nvi = NULL;
3700 (void) snprintf(msg, sizeof (msg),
3701 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3704 (void) snprintf(msg, sizeof (msg),
3705 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3708 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3710 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3711 &l2cache, NULL)) == NULL)
3712 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3715 * Don't allow error clearing for hot spares. Do allow
3716 * error clearing for l2cache devices.
3719 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3721 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3725 zpool_get_load_policy(rewindnvl, &policy);
3726 zc.zc_cookie = policy.zlp_rewind;
3728 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3731 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3734 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3736 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3737 zcmd_free_nvlists(&zc);
3742 if (!error || ((policy.zlp_rewind & ZPOOL_TRY_REWIND) &&
3743 errno != EPERM && errno != EACCES)) {
3744 if (policy.zlp_rewind &
3745 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3746 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3747 zpool_rewind_exclaim(hdl, zc.zc_name,
3748 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0),
3752 zcmd_free_nvlists(&zc);
3756 zcmd_free_nvlists(&zc);
3757 return (zpool_standard_error(hdl, errno, msg));
3761 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3764 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3766 zfs_cmd_t zc = {"\0"};
3768 libzfs_handle_t *hdl = zhp->zpool_hdl;
3770 (void) snprintf(msg, sizeof (msg),
3771 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3772 (u_longlong_t)guid);
3774 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3776 zc.zc_cookie = ZPOOL_NO_REWIND;
3778 if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
3781 return (zpool_standard_error(hdl, errno, msg));
3785 * Change the GUID for a pool.
3788 zpool_reguid(zpool_handle_t *zhp)
3791 libzfs_handle_t *hdl = zhp->zpool_hdl;
3792 zfs_cmd_t zc = {"\0"};
3794 (void) snprintf(msg, sizeof (msg),
3795 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3797 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3798 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3801 return (zpool_standard_error(hdl, errno, msg));
3808 zpool_reopen_one(zpool_handle_t *zhp, void *data)
3810 libzfs_handle_t *hdl = zpool_get_handle(zhp);
3811 const char *pool_name = zpool_get_name(zhp);
3812 boolean_t *scrub_restart = data;
3815 error = lzc_reopen(pool_name, *scrub_restart);
3817 return (zpool_standard_error_fmt(hdl, error,
3818 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"), pool_name));
3824 /* call into libzfs_core to execute the sync IOCTL per pool */
3826 zpool_sync_one(zpool_handle_t *zhp, void *data)
3829 libzfs_handle_t *hdl = zpool_get_handle(zhp);
3830 const char *pool_name = zpool_get_name(zhp);
3831 boolean_t *force = data;
3832 nvlist_t *innvl = fnvlist_alloc();
3834 fnvlist_add_boolean_value(innvl, "force", *force);
3835 if ((ret = lzc_sync(pool_name, innvl, NULL)) != 0) {
3837 return (zpool_standard_error_fmt(hdl, ret,
3838 dgettext(TEXT_DOMAIN, "sync '%s' failed"), pool_name));
3845 #define PATH_BUF_LEN 64
3848 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3849 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3850 * We also check if this is a whole disk, in which case we strip off the
3851 * trailing 's0' slice name.
3853 * This routine is also responsible for identifying when disks have been
3854 * reconfigured in a new location. The kernel will have opened the device by
3855 * devid, but the path will still refer to the old location. To catch this, we
3856 * first do a path -> devid translation (which is fast for the common case). If
3857 * the devid matches, we're done. If not, we do a reverse devid -> path
3858 * translation and issue the appropriate ioctl() to update the path of the vdev.
3859 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3863 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3866 char *path, *type, *env;
3868 char buf[PATH_BUF_LEN];
3869 char tmpbuf[PATH_BUF_LEN];
3872 * vdev_name will be "root"/"root-0" for the root vdev, but it is the
3873 * zpool name that will be displayed to the user.
3875 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
3876 if (zhp != NULL && strcmp(type, "root") == 0)
3877 return (zfs_strdup(hdl, zpool_get_name(zhp)));
3879 env = getenv("ZPOOL_VDEV_NAME_PATH");
3880 if (env && (strtoul(env, NULL, 0) > 0 ||
3881 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3882 name_flags |= VDEV_NAME_PATH;
3884 env = getenv("ZPOOL_VDEV_NAME_GUID");
3885 if (env && (strtoul(env, NULL, 0) > 0 ||
3886 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3887 name_flags |= VDEV_NAME_GUID;
3889 env = getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
3890 if (env && (strtoul(env, NULL, 0) > 0 ||
3891 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3892 name_flags |= VDEV_NAME_FOLLOW_LINKS;
3894 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3895 name_flags & VDEV_NAME_GUID) {
3896 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value);
3897 (void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)value);
3899 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3900 if (name_flags & VDEV_NAME_FOLLOW_LINKS) {
3901 char *rp = realpath(path, NULL);
3903 strlcpy(buf, rp, sizeof (buf));
3910 * For a block device only use the name.
3912 if ((strcmp(type, VDEV_TYPE_DISK) == 0) &&
3913 !(name_flags & VDEV_NAME_PATH)) {
3914 path = strrchr(path, '/');
3919 * Remove the partition from the path it this is a whole disk.
3921 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, &value)
3922 == 0 && value && !(name_flags & VDEV_NAME_PATH)) {
3923 return (zfs_strip_partition(path));
3929 * If it's a raidz device, we need to stick in the parity level.
3931 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3932 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3934 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3935 (u_longlong_t)value);
3940 * We identify each top-level vdev by using a <type-id>
3941 * naming convention.
3943 if (name_flags & VDEV_NAME_TYPE_ID) {
3945 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3947 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s-%llu",
3948 path, (u_longlong_t)id);
3953 return (zfs_strdup(hdl, path));
3957 zbookmark_mem_compare(const void *a, const void *b)
3959 return (memcmp(a, b, sizeof (zbookmark_phys_t)));
3963 * Retrieve the persistent error log, uniquify the members, and return to the
3967 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3969 zfs_cmd_t zc = {"\0"};
3970 libzfs_handle_t *hdl = zhp->zpool_hdl;
3972 zbookmark_phys_t *zb = NULL;
3976 * Retrieve the raw error list from the kernel. If the number of errors
3977 * has increased, allocate more space and continue until we get the
3980 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3984 zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3985 count * sizeof (zbookmark_phys_t));
3986 zc.zc_nvlist_dst_size = count;
3987 (void) strcpy(zc.zc_name, zhp->zpool_name);
3989 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_ERROR_LOG,
3991 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3992 if (errno == ENOMEM) {
3995 count = zc.zc_nvlist_dst_size;
3996 dst = zfs_alloc(zhp->zpool_hdl, count *
3997 sizeof (zbookmark_phys_t));
3998 zc.zc_nvlist_dst = (uintptr_t)dst;
4000 return (zpool_standard_error_fmt(hdl, errno,
4001 dgettext(TEXT_DOMAIN, "errors: List of "
4002 "errors unavailable")));
4010 * Sort the resulting bookmarks. This is a little confusing due to the
4011 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
4012 * to first, and 'zc_nvlist_dst_size' indicates the number of bookmarks
4013 * _not_ copied as part of the process. So we point the start of our
4014 * array appropriate and decrement the total number of elements.
4016 zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
4017 zc.zc_nvlist_dst_size;
4018 count -= zc.zc_nvlist_dst_size;
4020 qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
4022 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
4025 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
4027 for (i = 0; i < count; i++) {
4030 /* ignoring zb_blkid and zb_level for now */
4031 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
4032 zb[i-1].zb_object == zb[i].zb_object)
4035 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
4037 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
4038 zb[i].zb_objset) != 0) {
4042 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
4043 zb[i].zb_object) != 0) {
4047 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
4054 free((void *)(uintptr_t)zc.zc_nvlist_dst);
4058 free((void *)(uintptr_t)zc.zc_nvlist_dst);
4059 return (no_memory(zhp->zpool_hdl));
4063 * Upgrade a ZFS pool to the latest on-disk version.
4066 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
4068 zfs_cmd_t zc = {"\0"};
4069 libzfs_handle_t *hdl = zhp->zpool_hdl;
4071 (void) strcpy(zc.zc_name, zhp->zpool_name);
4072 zc.zc_cookie = new_version;
4074 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
4075 return (zpool_standard_error_fmt(hdl, errno,
4076 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
4082 zfs_save_arguments(int argc, char **argv, char *string, int len)
4086 (void) strlcpy(string, basename(argv[0]), len);
4087 for (i = 1; i < argc; i++) {
4088 (void) strlcat(string, " ", len);
4089 (void) strlcat(string, argv[i], len);
4094 zpool_log_history(libzfs_handle_t *hdl, const char *message)
4096 zfs_cmd_t zc = {"\0"};
4100 args = fnvlist_alloc();
4101 fnvlist_add_string(args, "message", message);
4102 err = zcmd_write_src_nvlist(hdl, &zc, args);
4104 err = zfs_ioctl(hdl, ZFS_IOC_LOG_HISTORY, &zc);
4106 zcmd_free_nvlists(&zc);
4111 * Perform ioctl to get some command history of a pool.
4113 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
4114 * logical offset of the history buffer to start reading from.
4116 * Upon return, 'off' is the next logical offset to read from and
4117 * 'len' is the actual amount of bytes read into 'buf'.
4120 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
4122 zfs_cmd_t zc = {"\0"};
4123 libzfs_handle_t *hdl = zhp->zpool_hdl;
4125 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4127 zc.zc_history = (uint64_t)(uintptr_t)buf;
4128 zc.zc_history_len = *len;
4129 zc.zc_history_offset = *off;
4131 if (zfs_ioctl(hdl, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
4134 return (zfs_error_fmt(hdl, EZFS_PERM,
4135 dgettext(TEXT_DOMAIN,
4136 "cannot show history for pool '%s'"),
4139 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
4140 dgettext(TEXT_DOMAIN, "cannot get history for pool "
4141 "'%s'"), zhp->zpool_name));
4143 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
4144 dgettext(TEXT_DOMAIN, "cannot get history for pool "
4145 "'%s', pool must be upgraded"), zhp->zpool_name));
4147 return (zpool_standard_error_fmt(hdl, errno,
4148 dgettext(TEXT_DOMAIN,
4149 "cannot get history for '%s'"), zhp->zpool_name));
4153 *len = zc.zc_history_len;
4154 *off = zc.zc_history_offset;
4160 * Retrieve the command history of a pool.
4163 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
4166 int buflen = 128 * 1024;
4168 nvlist_t **records = NULL;
4169 uint_t numrecords = 0;
4172 buf = malloc(buflen);
4176 uint64_t bytes_read = buflen;
4179 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
4182 /* if nothing else was read in, we're at EOF, just return */
4186 if ((err = zpool_history_unpack(buf, bytes_read,
4187 &leftover, &records, &numrecords)) != 0)
4190 if (leftover == bytes_read) {
4192 * no progress made, because buffer is not big enough
4193 * to hold this record; resize and retry.
4197 buf = malloc(buflen);
4208 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
4209 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
4210 records, numrecords) == 0);
4212 for (i = 0; i < numrecords; i++)
4213 nvlist_free(records[i]);
4220 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
4221 * If there is a new event available 'nvp' will contain a newly allocated
4222 * nvlist and 'dropped' will be set to the number of missed events since
4223 * the last call to this function. When 'nvp' is set to NULL it indicates
4224 * no new events are available. In either case the function returns 0 and
4225 * it is up to the caller to free 'nvp'. In the case of a fatal error the
4226 * function will return a non-zero value. When the function is called in
4227 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
4228 * it will not return until a new event is available.
4231 zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
4232 int *dropped, unsigned flags, int zevent_fd)
4234 zfs_cmd_t zc = {"\0"};
4239 zc.zc_cleanup_fd = zevent_fd;
4241 if (flags & ZEVENT_NONBLOCK)
4242 zc.zc_guid = ZEVENT_NONBLOCK;
4244 if (zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE) != 0)
4248 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
4251 error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
4252 dgettext(TEXT_DOMAIN, "zfs shutdown"));
4255 /* Blocking error case should not occur */
4256 if (!(flags & ZEVENT_NONBLOCK))
4257 error = zpool_standard_error_fmt(hdl, errno,
4258 dgettext(TEXT_DOMAIN, "cannot get event"));
4262 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
4263 error = zfs_error_fmt(hdl, EZFS_NOMEM,
4264 dgettext(TEXT_DOMAIN, "cannot get event"));
4270 error = zpool_standard_error_fmt(hdl, errno,
4271 dgettext(TEXT_DOMAIN, "cannot get event"));
4276 error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
4280 *dropped = (int)zc.zc_cookie;
4282 zcmd_free_nvlists(&zc);
4291 zpool_events_clear(libzfs_handle_t *hdl, int *count)
4293 zfs_cmd_t zc = {"\0"};
4296 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
4297 "cannot clear events"));
4299 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
4300 return (zpool_standard_error_fmt(hdl, errno, msg));
4303 *count = (int)zc.zc_cookie; /* # of events cleared */
4309 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4310 * the passed zevent_fd file handle. On success zero is returned,
4311 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4314 zpool_events_seek(libzfs_handle_t *hdl, uint64_t eid, int zevent_fd)
4316 zfs_cmd_t zc = {"\0"};
4320 zc.zc_cleanup_fd = zevent_fd;
4322 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_SEEK, &zc) != 0) {
4325 error = zfs_error_fmt(hdl, EZFS_NOENT,
4326 dgettext(TEXT_DOMAIN, "cannot get event"));
4330 error = zfs_error_fmt(hdl, EZFS_NOMEM,
4331 dgettext(TEXT_DOMAIN, "cannot get event"));
4335 error = zpool_standard_error_fmt(hdl, errno,
4336 dgettext(TEXT_DOMAIN, "cannot get event"));
4345 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4346 char *pathname, size_t len)
4348 zfs_cmd_t zc = {"\0"};
4349 boolean_t mounted = B_FALSE;
4350 char *mntpnt = NULL;
4351 char dsname[ZFS_MAX_DATASET_NAME_LEN];
4354 /* special case for the MOS */
4355 (void) snprintf(pathname, len, "<metadata>:<0x%llx>",
4360 /* get the dataset's name */
4361 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4363 if (zfs_ioctl(zhp->zpool_hdl,
4364 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
4365 /* just write out a path of two object numbers */
4366 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
4367 (longlong_t)dsobj, (longlong_t)obj);
4370 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
4372 /* find out if the dataset is mounted */
4373 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
4375 /* get the corrupted object's path */
4376 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
4378 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_OBJ_TO_PATH,
4381 (void) snprintf(pathname, len, "%s%s", mntpnt,
4384 (void) snprintf(pathname, len, "%s:%s",
4385 dsname, zc.zc_value);
4388 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname,
4395 * Wait while the specified activity is in progress in the pool.
4398 zpool_wait(zpool_handle_t *zhp, zpool_wait_activity_t activity)
4402 int error = zpool_wait_status(zhp, activity, &missing, NULL);
4405 (void) zpool_standard_error_fmt(zhp->zpool_hdl, ENOENT,
4406 dgettext(TEXT_DOMAIN, "error waiting in pool '%s'"),
4415 * Wait for the given activity and return the status of the wait (whether or not
4416 * any waiting was done) in the 'waited' parameter. Non-existent pools are
4417 * reported via the 'missing' parameter, rather than by printing an error
4418 * message. This is convenient when this function is called in a loop over a
4419 * long period of time (as it is, for example, by zpool's wait cmd). In that
4420 * scenario, a pool being exported or destroyed should be considered a normal
4421 * event, so we don't want to print an error when we find that the pool doesn't
4425 zpool_wait_status(zpool_handle_t *zhp, zpool_wait_activity_t activity,
4426 boolean_t *missing, boolean_t *waited)
4428 int error = lzc_wait(zhp->zpool_name, activity, waited);
4429 *missing = (error == ENOENT);
4434 (void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
4435 dgettext(TEXT_DOMAIN, "error waiting in pool '%s'"),