4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27 * Copyright (c) 2018 Datto Inc.
28 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
29 * Copyright (c) 2017, Intel Corporation.
30 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>
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, const 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 * Given an nvlist of zpool properties to be set, validate that they are
450 * correct, and parse any numeric properties (index, boolean, etc) if they are
451 * specified as strings.
454 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
455 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
463 struct stat64 statbuf;
466 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
467 (void) no_memory(hdl);
472 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
473 const char *propname = nvpair_name(elem);
475 prop = zpool_name_to_prop(propname);
476 if (prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname)) {
478 char *fname = strchr(propname, '@') + 1;
480 err = zfeature_lookup_name(fname, NULL);
482 ASSERT3U(err, ==, ENOENT);
483 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
484 "invalid feature '%s'"), fname);
485 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
489 if (nvpair_type(elem) != DATA_TYPE_STRING) {
490 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
491 "'%s' must be a string"), propname);
492 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
496 (void) nvpair_value_string(elem, &strval);
497 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0 &&
498 strcmp(strval, ZFS_FEATURE_DISABLED) != 0) {
499 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
500 "property '%s' can only be set to "
501 "'enabled' or 'disabled'"), propname);
502 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
507 strcmp(strval, ZFS_FEATURE_DISABLED) == 0) {
508 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
509 "property '%s' can only be set to "
510 "'disabled' at creation time"), propname);
511 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
515 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
516 (void) no_memory(hdl);
523 * Make sure this property is valid and applies to this type.
525 if (prop == ZPOOL_PROP_INVAL) {
526 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
527 "invalid property '%s'"), propname);
528 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
532 if (zpool_prop_readonly(prop)) {
533 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
534 "is readonly"), propname);
535 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
539 if (!flags.create && zpool_prop_setonce(prop)) {
540 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
541 "property '%s' can only be set at "
542 "creation time"), propname);
543 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
547 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
548 &strval, &intval, errbuf) != 0)
552 * Perform additional checking for specific properties.
555 case ZPOOL_PROP_VERSION:
556 if (intval < version ||
557 !SPA_VERSION_IS_SUPPORTED(intval)) {
558 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
559 "property '%s' number %d is invalid."),
561 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
566 case ZPOOL_PROP_ASHIFT:
568 (intval < ASHIFT_MIN || intval > ASHIFT_MAX)) {
569 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
570 "property '%s' number %d is invalid, only "
571 "values between %" PRId32 " and "
572 "%" PRId32 " are allowed."),
573 propname, intval, ASHIFT_MIN, ASHIFT_MAX);
574 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
579 case ZPOOL_PROP_BOOTFS:
580 if (flags.create || flags.import) {
581 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
582 "property '%s' cannot be set at creation "
583 "or import time"), propname);
584 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
588 if (version < SPA_VERSION_BOOTFS) {
589 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
590 "pool must be upgraded to support "
591 "'%s' property"), propname);
592 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
597 * bootfs property value has to be a dataset name and
598 * the dataset has to be in the same pool as it sets to.
600 if (!bootfs_name_valid(poolname, strval)) {
601 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
602 "is an invalid name"), strval);
603 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
607 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
608 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
609 "could not open pool '%s'"), poolname);
610 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
616 case ZPOOL_PROP_ALTROOT:
617 if (!flags.create && !flags.import) {
618 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
619 "property '%s' can only be set during pool "
620 "creation or import"), propname);
621 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
625 if (strval[0] != '/') {
626 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
627 "bad alternate root '%s'"), strval);
628 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
633 case ZPOOL_PROP_CACHEFILE:
634 if (strval[0] == '\0')
637 if (strcmp(strval, "none") == 0)
640 if (strval[0] != '/') {
641 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
642 "property '%s' must be empty, an "
643 "absolute path, or 'none'"), propname);
644 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
648 slash = strrchr(strval, '/');
650 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
651 strcmp(slash, "/..") == 0) {
652 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
653 "'%s' is not a valid file"), strval);
654 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
660 if (strval[0] != '\0' &&
661 (stat64(strval, &statbuf) != 0 ||
662 !S_ISDIR(statbuf.st_mode))) {
663 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
664 "'%s' is not a valid directory"),
666 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
673 case ZPOOL_PROP_COMMENT:
674 for (check = strval; *check != '\0'; check++) {
675 if (!isprint(*check)) {
677 dgettext(TEXT_DOMAIN,
678 "comment may only have printable "
680 (void) zfs_error(hdl, EZFS_BADPROP,
685 if (strlen(strval) > ZPROP_MAX_COMMENT) {
686 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
687 "comment must not exceed %d characters"),
689 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
693 case ZPOOL_PROP_READONLY:
695 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
696 "property '%s' can only be set at "
697 "import time"), propname);
698 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
702 case ZPOOL_PROP_MULTIHOST:
703 if (get_system_hostid() == 0) {
704 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
705 "requires a non-zero system hostid"));
706 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
710 case ZPOOL_PROP_DEDUPDITTO:
711 printf("Note: property '%s' no longer has "
712 "any effect\n", propname);
722 nvlist_free(retprops);
727 * Set zpool property : propname=propval.
730 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
732 zfs_cmd_t zc = {"\0"};
735 nvlist_t *nvl = NULL;
738 prop_flags_t flags = { 0 };
740 (void) snprintf(errbuf, sizeof (errbuf),
741 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
744 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
745 return (no_memory(zhp->zpool_hdl));
747 if (nvlist_add_string(nvl, propname, propval) != 0) {
749 return (no_memory(zhp->zpool_hdl));
752 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
753 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
754 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
763 * Execute the corresponding ioctl() to set this property.
765 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
767 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
772 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
774 zcmd_free_nvlists(&zc);
778 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
780 (void) zpool_props_refresh(zhp);
786 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
788 libzfs_handle_t *hdl = zhp->zpool_hdl;
790 char buf[ZFS_MAXPROPLEN];
791 nvlist_t *features = NULL;
794 boolean_t firstexpand = (NULL == *plp);
797 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
801 while (*last != NULL)
802 last = &(*last)->pl_next;
805 features = zpool_get_features(zhp);
807 if ((*plp)->pl_all && firstexpand) {
808 for (i = 0; i < SPA_FEATURES; i++) {
809 zprop_list_t *entry = zfs_alloc(hdl,
810 sizeof (zprop_list_t));
811 entry->pl_prop = ZPROP_INVAL;
812 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
813 spa_feature_table[i].fi_uname);
814 entry->pl_width = strlen(entry->pl_user_prop);
815 entry->pl_all = B_TRUE;
818 last = &entry->pl_next;
822 /* add any unsupported features */
823 for (nvp = nvlist_next_nvpair(features, NULL);
824 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
829 if (zfeature_is_supported(nvpair_name(nvp)))
832 propname = zfs_asprintf(hdl, "unsupported@%s",
836 * Before adding the property to the list make sure that no
837 * other pool already added the same property.
841 while (entry != NULL) {
842 if (entry->pl_user_prop != NULL &&
843 strcmp(propname, entry->pl_user_prop) == 0) {
847 entry = entry->pl_next;
854 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
855 entry->pl_prop = ZPROP_INVAL;
856 entry->pl_user_prop = propname;
857 entry->pl_width = strlen(entry->pl_user_prop);
858 entry->pl_all = B_TRUE;
861 last = &entry->pl_next;
864 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
869 if (entry->pl_prop != ZPROP_INVAL &&
870 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
871 NULL, B_FALSE) == 0) {
872 if (strlen(buf) > entry->pl_width)
873 entry->pl_width = strlen(buf);
881 * Get the state for the given feature on the given ZFS pool.
884 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
888 boolean_t found = B_FALSE;
889 nvlist_t *features = zpool_get_features(zhp);
891 const char *feature = strchr(propname, '@') + 1;
893 supported = zpool_prop_feature(propname);
894 ASSERT(supported || zpool_prop_unsupported(propname));
897 * Convert from feature name to feature guid. This conversion is
898 * unnecessary for unsupported@... properties because they already
905 ret = zfeature_lookup_name(feature, &fid);
907 (void) strlcpy(buf, "-", len);
910 feature = spa_feature_table[fid].fi_guid;
913 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
918 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
921 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
923 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
928 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
930 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
933 (void) strlcpy(buf, "-", len);
942 * Validate the given pool name, optionally putting an extended error message in
946 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
952 ret = pool_namecheck(pool, &why, &what);
955 * The rules for reserved pool names were extended at a later point.
956 * But we need to support users with existing pools that may now be
957 * invalid. So we only check for this expanded set of names during a
958 * create (or import), and only in userland.
960 if (ret == 0 && !isopen &&
961 (strncmp(pool, "mirror", 6) == 0 ||
962 strncmp(pool, "raidz", 5) == 0 ||
963 strncmp(pool, "spare", 5) == 0 ||
964 strcmp(pool, "log") == 0)) {
967 dgettext(TEXT_DOMAIN, "name is reserved"));
975 case NAME_ERR_TOOLONG:
977 dgettext(TEXT_DOMAIN, "name is too long"));
980 case NAME_ERR_INVALCHAR:
982 dgettext(TEXT_DOMAIN, "invalid character "
983 "'%c' in pool name"), what);
986 case NAME_ERR_NOLETTER:
987 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
988 "name must begin with a letter"));
991 case NAME_ERR_RESERVED:
992 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
993 "name is reserved"));
996 case NAME_ERR_DISKLIKE:
997 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
998 "pool name is reserved"));
1001 case NAME_ERR_LEADING_SLASH:
1002 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1003 "leading slash in name"));
1006 case NAME_ERR_EMPTY_COMPONENT:
1007 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1008 "empty component in name"));
1011 case NAME_ERR_TRAILING_SLASH:
1012 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1013 "trailing slash in name"));
1016 case NAME_ERR_MULTIPLE_DELIMITERS:
1017 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1018 "multiple '@' and/or '#' delimiters in "
1022 case NAME_ERR_NO_AT:
1023 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1024 "permission set is missing '@'"));
1028 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1029 "(%d) not defined"), why);
1040 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1044 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
1046 zpool_handle_t *zhp;
1050 * Make sure the pool name is valid.
1052 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
1053 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1054 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1059 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1062 zhp->zpool_hdl = hdl;
1063 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1065 if (zpool_refresh_stats(zhp, &missing) != 0) {
1071 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1072 (void) zfs_error_fmt(hdl, EZFS_NOENT,
1073 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1082 * Like the above, but silent on error. Used when iterating over pools (because
1083 * the configuration cache may be out of date).
1086 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1088 zpool_handle_t *zhp;
1091 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1094 zhp->zpool_hdl = hdl;
1095 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1097 if (zpool_refresh_stats(zhp, &missing) != 0) {
1113 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1117 zpool_open(libzfs_handle_t *hdl, const char *pool)
1119 zpool_handle_t *zhp;
1121 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1124 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1125 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1126 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1135 * Close the handle. Simply frees the memory associated with the handle.
1138 zpool_close(zpool_handle_t *zhp)
1140 nvlist_free(zhp->zpool_config);
1141 nvlist_free(zhp->zpool_old_config);
1142 nvlist_free(zhp->zpool_props);
1147 * Return the name of the pool.
1150 zpool_get_name(zpool_handle_t *zhp)
1152 return (zhp->zpool_name);
1157 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1160 zpool_get_state(zpool_handle_t *zhp)
1162 return (zhp->zpool_state);
1166 * Check if vdev list contains a special vdev
1169 zpool_has_special_vdev(nvlist_t *nvroot)
1174 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, &child,
1176 for (uint_t c = 0; c < children; c++) {
1179 if (nvlist_lookup_string(child[c],
1180 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias) == 0 &&
1181 strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0) {
1190 * Create the named pool, using the provided vdev list. It is assumed
1191 * that the consumer has already validated the contents of the nvlist, so we
1192 * don't have to worry about error semantics.
1195 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1196 nvlist_t *props, nvlist_t *fsprops)
1198 zfs_cmd_t zc = {"\0"};
1199 nvlist_t *zc_fsprops = NULL;
1200 nvlist_t *zc_props = NULL;
1201 nvlist_t *hidden_args = NULL;
1202 uint8_t *wkeydata = NULL;
1207 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1208 "cannot create '%s'"), pool);
1210 if (!zpool_name_valid(hdl, B_FALSE, pool))
1211 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1213 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1217 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1219 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1220 SPA_VERSION_1, flags, msg)) == NULL) {
1229 zoned = ((nvlist_lookup_string(fsprops,
1230 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1231 strcmp(zonestr, "on") == 0);
1233 if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
1234 fsprops, zoned, NULL, NULL, B_TRUE, msg)) == NULL) {
1238 if (nvlist_exists(zc_fsprops,
1239 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS)) &&
1240 !zpool_has_special_vdev(nvroot)) {
1241 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1242 "%s property requires a special vdev"),
1243 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS));
1244 (void) zfs_error(hdl, EZFS_BADPROP, msg);
1249 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1252 if (zfs_crypto_create(hdl, NULL, zc_fsprops, props, B_TRUE,
1253 &wkeydata, &wkeylen) != 0) {
1254 zfs_error(hdl, EZFS_CRYPTOFAILED, msg);
1257 if (nvlist_add_nvlist(zc_props,
1258 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1261 if (wkeydata != NULL) {
1262 if (nvlist_alloc(&hidden_args, NV_UNIQUE_NAME, 0) != 0)
1265 if (nvlist_add_uint8_array(hidden_args, "wkeydata",
1266 wkeydata, wkeylen) != 0)
1269 if (nvlist_add_nvlist(zc_props, ZPOOL_HIDDEN_ARGS,
1275 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1278 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1280 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1282 zcmd_free_nvlists(&zc);
1283 nvlist_free(zc_props);
1284 nvlist_free(zc_fsprops);
1285 nvlist_free(hidden_args);
1286 if (wkeydata != NULL)
1292 * This can happen if the user has specified the same
1293 * device multiple times. We can't reliably detect this
1294 * until we try to add it and see we already have a
1295 * label. This can also happen under if the device is
1296 * part of an active md or lvm device.
1298 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1299 "one or more vdevs refer to the same device, or "
1300 "one of\nthe devices is part of an active md or "
1302 return (zfs_error(hdl, EZFS_BADDEV, msg));
1306 * This happens if the record size is smaller or larger
1307 * than the allowed size range, or not a power of 2.
1309 * NOTE: although zfs_valid_proplist is called earlier,
1310 * this case may have slipped through since the
1311 * pool does not exist yet and it is therefore
1312 * impossible to read properties e.g. max blocksize
1315 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1316 "record size invalid"));
1317 return (zfs_error(hdl, EZFS_BADPROP, msg));
1321 * This occurs when one of the devices is below
1322 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1323 * device was the problem device since there's no
1324 * reliable way to determine device size from userland.
1329 zfs_nicebytes(SPA_MINDEVSIZE, buf,
1332 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1333 "one or more devices is less than the "
1334 "minimum size (%s)"), buf);
1336 return (zfs_error(hdl, EZFS_BADDEV, msg));
1339 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1340 "one or more devices is out of space"));
1341 return (zfs_error(hdl, EZFS_BADDEV, msg));
1344 return (zpool_standard_error(hdl, errno, msg));
1349 zcmd_free_nvlists(&zc);
1350 nvlist_free(zc_props);
1351 nvlist_free(zc_fsprops);
1352 nvlist_free(hidden_args);
1353 if (wkeydata != NULL)
1359 * Destroy the given pool. It is up to the caller to ensure that there are no
1360 * datasets left in the pool.
1363 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1365 zfs_cmd_t zc = {"\0"};
1366 zfs_handle_t *zfp = NULL;
1367 libzfs_handle_t *hdl = zhp->zpool_hdl;
1370 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1371 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1374 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1375 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1377 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1378 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1379 "cannot destroy '%s'"), zhp->zpool_name);
1381 if (errno == EROFS) {
1382 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1383 "one or more devices is read only"));
1384 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1386 (void) zpool_standard_error(hdl, errno, msg);
1395 remove_mountpoint(zfp);
1403 * Create a checkpoint in the given pool.
1406 zpool_checkpoint(zpool_handle_t *zhp)
1408 libzfs_handle_t *hdl = zhp->zpool_hdl;
1412 error = lzc_pool_checkpoint(zhp->zpool_name);
1414 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1415 "cannot checkpoint '%s'"), zhp->zpool_name);
1416 (void) zpool_standard_error(hdl, error, msg);
1424 * Discard the checkpoint from the given pool.
1427 zpool_discard_checkpoint(zpool_handle_t *zhp)
1429 libzfs_handle_t *hdl = zhp->zpool_hdl;
1433 error = lzc_pool_checkpoint_discard(zhp->zpool_name);
1435 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1436 "cannot discard checkpoint in '%s'"), zhp->zpool_name);
1437 (void) zpool_standard_error(hdl, error, msg);
1445 * Add the given vdevs to the pool. The caller must have already performed the
1446 * necessary verification to ensure that the vdev specification is well-formed.
1449 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1451 zfs_cmd_t zc = {"\0"};
1453 libzfs_handle_t *hdl = zhp->zpool_hdl;
1455 nvlist_t **spares, **l2cache;
1456 uint_t nspares, nl2cache;
1458 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1459 "cannot add to '%s'"), zhp->zpool_name);
1461 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1462 SPA_VERSION_SPARES &&
1463 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1464 &spares, &nspares) == 0) {
1465 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1466 "upgraded to add hot spares"));
1467 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1470 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1471 SPA_VERSION_L2CACHE &&
1472 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1473 &l2cache, &nl2cache) == 0) {
1474 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1475 "upgraded to add cache devices"));
1476 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1479 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1481 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1483 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1487 * This can happen if the user has specified the same
1488 * device multiple times. We can't reliably detect this
1489 * until we try to add it and see we already have a
1492 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1493 "one or more vdevs refer to the same device"));
1494 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1498 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1499 "invalid config; a pool with removing/removed "
1500 "vdevs does not support adding raidz vdevs"));
1501 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1506 * This occurs when one of the devices is below
1507 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1508 * device was the problem device since there's no
1509 * reliable way to determine device size from userland.
1514 zfs_nicebytes(SPA_MINDEVSIZE, buf,
1517 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1518 "device is less than the minimum "
1521 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1525 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1526 "pool must be upgraded to add these vdevs"));
1527 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1531 (void) zpool_standard_error(hdl, errno, msg);
1539 zcmd_free_nvlists(&zc);
1545 * Exports the pool from the system. The caller must ensure that there are no
1546 * mounted datasets in the pool.
1549 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1550 const char *log_str)
1552 zfs_cmd_t zc = {"\0"};
1555 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1556 "cannot export '%s'"), zhp->zpool_name);
1558 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1559 zc.zc_cookie = force;
1560 zc.zc_guid = hardforce;
1561 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1563 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1566 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1567 "use '-f' to override the following errors:\n"
1568 "'%s' has an active shared spare which could be"
1569 " used by other pools once '%s' is exported."),
1570 zhp->zpool_name, zhp->zpool_name);
1571 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1574 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1583 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1585 return (zpool_export_common(zhp, force, B_FALSE, log_str));
1589 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1591 return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1595 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1598 nvlist_t *nv = NULL;
1604 if (!hdl->libzfs_printerr || config == NULL)
1607 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1608 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1612 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1614 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1616 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1617 strftime(timestr, 128, "%c", &t) != 0) {
1619 (void) printf(dgettext(TEXT_DOMAIN,
1620 "Would be able to return %s "
1621 "to its state as of %s.\n"),
1624 (void) printf(dgettext(TEXT_DOMAIN,
1625 "Pool %s returned to its state as of %s.\n"),
1629 (void) printf(dgettext(TEXT_DOMAIN,
1630 "%s approximately %lld "),
1631 dryrun ? "Would discard" : "Discarded",
1632 ((longlong_t)loss + 30) / 60);
1633 (void) printf(dgettext(TEXT_DOMAIN,
1634 "minutes of transactions.\n"));
1635 } else if (loss > 0) {
1636 (void) printf(dgettext(TEXT_DOMAIN,
1637 "%s approximately %lld "),
1638 dryrun ? "Would discard" : "Discarded",
1640 (void) printf(dgettext(TEXT_DOMAIN,
1641 "seconds of transactions.\n"));
1647 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1650 nvlist_t *nv = NULL;
1652 uint64_t edata = UINT64_MAX;
1657 if (!hdl->libzfs_printerr)
1661 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1663 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1665 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1666 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1667 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1668 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1671 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1672 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1675 (void) printf(dgettext(TEXT_DOMAIN,
1676 "Recovery is possible, but will result in some data loss.\n"));
1678 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1679 strftime(timestr, 128, "%c", &t) != 0) {
1680 (void) printf(dgettext(TEXT_DOMAIN,
1681 "\tReturning the pool to its state as of %s\n"
1682 "\tshould correct the problem. "),
1685 (void) printf(dgettext(TEXT_DOMAIN,
1686 "\tReverting the pool to an earlier state "
1687 "should correct the problem.\n\t"));
1691 (void) printf(dgettext(TEXT_DOMAIN,
1692 "Approximately %lld minutes of data\n"
1693 "\tmust be discarded, irreversibly. "),
1694 ((longlong_t)loss + 30) / 60);
1695 } else if (loss > 0) {
1696 (void) printf(dgettext(TEXT_DOMAIN,
1697 "Approximately %lld seconds of data\n"
1698 "\tmust be discarded, irreversibly. "),
1701 if (edata != 0 && edata != UINT64_MAX) {
1703 (void) printf(dgettext(TEXT_DOMAIN,
1704 "After rewind, at least\n"
1705 "\tone persistent user-data error will remain. "));
1707 (void) printf(dgettext(TEXT_DOMAIN,
1708 "After rewind, several\n"
1709 "\tpersistent user-data errors will remain. "));
1712 (void) printf(dgettext(TEXT_DOMAIN,
1713 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1714 reason >= 0 ? "clear" : "import", name);
1716 (void) printf(dgettext(TEXT_DOMAIN,
1717 "A scrub of the pool\n"
1718 "\tis strongly recommended after recovery.\n"));
1722 (void) printf(dgettext(TEXT_DOMAIN,
1723 "Destroy and re-create the pool from\n\ta backup source.\n"));
1727 * zpool_import() is a contracted interface. Should be kept the same
1730 * Applications should use zpool_import_props() to import a pool with
1731 * new properties value to be set.
1734 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1737 nvlist_t *props = NULL;
1740 if (altroot != NULL) {
1741 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1742 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1743 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1747 if (nvlist_add_string(props,
1748 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1749 nvlist_add_string(props,
1750 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1752 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1753 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1758 ret = zpool_import_props(hdl, config, newname, props,
1765 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1771 uint64_t is_log = 0;
1773 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1777 (void) printf("\t%*s%s%s\n", indent, "", name,
1778 is_log ? " [log]" : "");
1780 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1781 &child, &children) != 0)
1784 for (c = 0; c < children; c++) {
1785 vname = zpool_vdev_name(hdl, NULL, child[c], VDEV_NAME_TYPE_ID);
1786 print_vdev_tree(hdl, vname, child[c], indent + 2);
1792 zpool_print_unsup_feat(nvlist_t *config)
1794 nvlist_t *nvinfo, *unsup_feat;
1797 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1799 verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1802 for (nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1803 nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1806 verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1807 verify(nvpair_value_string(nvp, &desc) == 0);
1809 if (strlen(desc) > 0)
1810 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1812 (void) printf("\t%s\n", nvpair_name(nvp));
1817 * Import the given pool using the known configuration and a list of
1818 * properties to be set. The configuration should have come from
1819 * zpool_find_import(). The 'newname' parameters control whether the pool
1820 * is imported with a different name.
1823 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1824 nvlist_t *props, int flags)
1826 zfs_cmd_t zc = {"\0"};
1827 zpool_load_policy_t policy;
1828 nvlist_t *nv = NULL;
1829 nvlist_t *nvinfo = NULL;
1830 nvlist_t *missing = NULL;
1837 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1840 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1841 "cannot import pool '%s'"), origname);
1843 if (newname != NULL) {
1844 if (!zpool_name_valid(hdl, B_FALSE, newname))
1845 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1846 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1848 thename = (char *)newname;
1853 if (props != NULL) {
1855 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1857 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1860 if ((props = zpool_valid_proplist(hdl, origname,
1861 props, version, flags, errbuf)) == NULL)
1863 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1870 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1872 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1875 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1876 zcmd_free_nvlists(&zc);
1879 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1880 zcmd_free_nvlists(&zc);
1884 zc.zc_cookie = flags;
1885 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1887 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1888 zcmd_free_nvlists(&zc);
1895 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1897 zcmd_free_nvlists(&zc);
1899 zpool_get_load_policy(config, &policy);
1906 * Dry-run failed, but we print out what success
1907 * looks like if we found a best txg
1909 if (policy.zlp_rewind & ZPOOL_TRY_REWIND) {
1910 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1916 if (newname == NULL)
1917 (void) snprintf(desc, sizeof (desc),
1918 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1921 (void) snprintf(desc, sizeof (desc),
1922 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1927 if (nv != NULL && nvlist_lookup_nvlist(nv,
1928 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1929 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1930 (void) printf(dgettext(TEXT_DOMAIN, "This "
1931 "pool uses the following feature(s) not "
1932 "supported by this system:\n"));
1933 zpool_print_unsup_feat(nv);
1934 if (nvlist_exists(nvinfo,
1935 ZPOOL_CONFIG_CAN_RDONLY)) {
1936 (void) printf(dgettext(TEXT_DOMAIN,
1937 "All unsupported features are only "
1938 "required for writing to the pool."
1939 "\nThe pool can be imported using "
1940 "'-o readonly=on'.\n"));
1944 * Unsupported version.
1946 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1950 if (nv != NULL && nvlist_lookup_nvlist(nv,
1951 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0) {
1952 char *hostname = "<unknown>";
1953 uint64_t hostid = 0;
1954 mmp_state_t mmp_state;
1956 mmp_state = fnvlist_lookup_uint64(nvinfo,
1957 ZPOOL_CONFIG_MMP_STATE);
1959 if (nvlist_exists(nvinfo,
1960 ZPOOL_CONFIG_MMP_HOSTNAME))
1961 hostname = fnvlist_lookup_string(nvinfo,
1962 ZPOOL_CONFIG_MMP_HOSTNAME);
1964 if (nvlist_exists(nvinfo,
1965 ZPOOL_CONFIG_MMP_HOSTID))
1966 hostid = fnvlist_lookup_uint64(nvinfo,
1967 ZPOOL_CONFIG_MMP_HOSTID);
1969 if (mmp_state == MMP_STATE_ACTIVE) {
1970 (void) snprintf(aux, sizeof (aux),
1971 dgettext(TEXT_DOMAIN, "pool is imp"
1972 "orted on host '%s' (hostid=%lx).\n"
1973 "Export the pool on the other "
1974 "system, then run 'zpool import'."),
1975 hostname, (unsigned long) hostid);
1976 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
1977 (void) snprintf(aux, sizeof (aux),
1978 dgettext(TEXT_DOMAIN, "pool has "
1979 "the multihost property on and "
1980 "the\nsystem's hostid is not set. "
1981 "Set a unique system hostid with "
1982 "the zgenhostid(8) command.\n"));
1985 (void) zfs_error_aux(hdl, aux);
1987 (void) zfs_error(hdl, EZFS_ACTIVE_POOL, desc);
1991 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1995 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1996 "one or more devices is read only"));
1997 (void) zfs_error(hdl, EZFS_BADDEV, desc);
2001 if (nv && nvlist_lookup_nvlist(nv,
2002 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
2003 nvlist_lookup_nvlist(nvinfo,
2004 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
2005 (void) printf(dgettext(TEXT_DOMAIN,
2006 "The devices below are missing or "
2007 "corrupted, use '-m' to import the pool "
2009 print_vdev_tree(hdl, NULL, missing, 2);
2010 (void) printf("\n");
2012 (void) zpool_standard_error(hdl, error, desc);
2016 (void) zpool_standard_error(hdl, error, desc);
2020 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2021 "one or more devices are already in use\n"));
2022 (void) zfs_error(hdl, EZFS_BADDEV, desc);
2025 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2026 "new name of at least one dataset is longer than "
2027 "the maximum allowable length"));
2028 (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
2031 (void) zpool_standard_error(hdl, error, desc);
2032 zpool_explain_recover(hdl,
2033 newname ? origname : thename, -error, nv);
2040 zpool_handle_t *zhp;
2043 * This should never fail, but play it safe anyway.
2045 if (zpool_open_silent(hdl, thename, &zhp) != 0)
2047 else if (zhp != NULL)
2049 if (policy.zlp_rewind &
2050 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2051 zpool_rewind_exclaim(hdl, newname ? origname : thename,
2052 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0), nv);
2062 * Translate vdev names to guids. If a vdev_path is determined to be
2063 * unsuitable then a vd_errlist is allocated and the vdev path and errno
2067 zpool_translate_vdev_guids(zpool_handle_t *zhp, nvlist_t *vds,
2068 nvlist_t *vdev_guids, nvlist_t *guids_to_paths, nvlist_t **vd_errlist)
2070 nvlist_t *errlist = NULL;
2073 for (nvpair_t *elem = nvlist_next_nvpair(vds, NULL); elem != NULL;
2074 elem = nvlist_next_nvpair(vds, elem)) {
2075 boolean_t spare, cache;
2077 char *vd_path = nvpair_name(elem);
2078 nvlist_t *tgt = zpool_find_vdev(zhp, vd_path, &spare, &cache,
2081 if ((tgt == NULL) || cache || spare) {
2082 if (errlist == NULL) {
2083 errlist = fnvlist_alloc();
2087 uint64_t err = (tgt == NULL) ? EZFS_NODEVICE :
2088 (spare ? EZFS_ISSPARE : EZFS_ISL2CACHE);
2089 fnvlist_add_int64(errlist, vd_path, err);
2093 uint64_t guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
2094 fnvlist_add_uint64(vdev_guids, vd_path, guid);
2096 char msg[MAXNAMELEN];
2097 (void) snprintf(msg, sizeof (msg), "%llu", (u_longlong_t)guid);
2098 fnvlist_add_string(guids_to_paths, msg, vd_path);
2102 verify(errlist != NULL);
2103 if (vd_errlist != NULL)
2104 *vd_errlist = errlist;
2106 fnvlist_free(errlist);
2113 xlate_init_err(int err)
2117 return (EZFS_NODEVICE);
2120 return (EZFS_BADDEV);
2122 return (EZFS_INITIALIZING);
2124 return (EZFS_NO_INITIALIZE);
2130 * Begin, suspend, or cancel the initialization (initializing of all free
2131 * blocks) for the given vdevs in the given pool.
2134 zpool_initialize_impl(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2135 nvlist_t *vds, boolean_t wait)
2139 nvlist_t *vdev_guids = fnvlist_alloc();
2140 nvlist_t *guids_to_paths = fnvlist_alloc();
2141 nvlist_t *vd_errlist = NULL;
2145 err = zpool_translate_vdev_guids(zhp, vds, vdev_guids,
2146 guids_to_paths, &vd_errlist);
2149 verify(vd_errlist != NULL);
2153 err = lzc_initialize(zhp->zpool_name, cmd_type,
2154 vdev_guids, &errlist);
2157 if (errlist != NULL) {
2158 vd_errlist = fnvlist_lookup_nvlist(errlist,
2159 ZPOOL_INITIALIZE_VDEVS);
2162 (void) zpool_standard_error(zhp->zpool_hdl, err,
2163 dgettext(TEXT_DOMAIN, "operation failed"));
2168 for (elem = nvlist_next_nvpair(vdev_guids, NULL); elem != NULL;
2169 elem = nvlist_next_nvpair(vdev_guids, elem)) {
2171 uint64_t guid = fnvpair_value_uint64(elem);
2173 err = lzc_wait_tag(zhp->zpool_name,
2174 ZPOOL_WAIT_INITIALIZE, guid, NULL);
2176 (void) zpool_standard_error_fmt(zhp->zpool_hdl,
2177 err, dgettext(TEXT_DOMAIN, "error "
2178 "waiting for '%s' to initialize"),
2188 for (elem = nvlist_next_nvpair(vd_errlist, NULL); elem != NULL;
2189 elem = nvlist_next_nvpair(vd_errlist, elem)) {
2190 int64_t vd_error = xlate_init_err(fnvpair_value_int64(elem));
2193 if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem),
2195 path = nvpair_name(elem);
2197 (void) zfs_error_fmt(zhp->zpool_hdl, vd_error,
2198 "cannot initialize '%s'", path);
2202 fnvlist_free(vdev_guids);
2203 fnvlist_free(guids_to_paths);
2205 if (vd_errlist != NULL)
2206 fnvlist_free(vd_errlist);
2208 return (err == 0 ? 0 : -1);
2212 zpool_initialize(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2215 return (zpool_initialize_impl(zhp, cmd_type, vds, B_FALSE));
2219 zpool_initialize_wait(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2222 return (zpool_initialize_impl(zhp, cmd_type, vds, B_TRUE));
2226 xlate_trim_err(int err)
2230 return (EZFS_NODEVICE);
2233 return (EZFS_BADDEV);
2235 return (EZFS_TRIMMING);
2237 return (EZFS_NO_TRIM);
2239 return (EZFS_TRIM_NOTSUP);
2245 zpool_trim_wait(zpool_handle_t *zhp, nvlist_t *vdev_guids)
2250 for (elem = nvlist_next_nvpair(vdev_guids, NULL); elem != NULL;
2251 elem = nvlist_next_nvpair(vdev_guids, elem)) {
2253 uint64_t guid = fnvpair_value_uint64(elem);
2255 err = lzc_wait_tag(zhp->zpool_name,
2256 ZPOOL_WAIT_TRIM, guid, NULL);
2258 (void) zpool_standard_error_fmt(zhp->zpool_hdl,
2259 err, dgettext(TEXT_DOMAIN, "error "
2260 "waiting to trim '%s'"), nvpair_name(elem));
2269 * Check errlist and report any errors, omitting ones which should be
2270 * suppressed. Returns B_TRUE if any errors were reported.
2273 check_trim_errs(zpool_handle_t *zhp, trimflags_t *trim_flags,
2274 nvlist_t *guids_to_paths, nvlist_t *vds, nvlist_t *errlist)
2277 boolean_t reported_errs = B_FALSE;
2279 int num_suppressed_errs = 0;
2281 for (elem = nvlist_next_nvpair(vds, NULL);
2282 elem != NULL; elem = nvlist_next_nvpair(vds, elem)) {
2286 for (elem = nvlist_next_nvpair(errlist, NULL);
2287 elem != NULL; elem = nvlist_next_nvpair(errlist, elem)) {
2288 int64_t vd_error = xlate_trim_err(fnvpair_value_int64(elem));
2292 * If only the pool was specified, and it was not a secure
2293 * trim then suppress warnings for individual vdevs which
2294 * do not support trimming.
2296 if (vd_error == EZFS_TRIM_NOTSUP &&
2297 trim_flags->fullpool &&
2298 !trim_flags->secure) {
2299 num_suppressed_errs++;
2303 reported_errs = B_TRUE;
2304 if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem),
2306 path = nvpair_name(elem);
2308 (void) zfs_error_fmt(zhp->zpool_hdl, vd_error,
2309 "cannot trim '%s'", path);
2312 if (num_suppressed_errs == num_vds) {
2313 (void) zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
2314 "no devices in pool support trim operations"));
2315 (void) (zfs_error(zhp->zpool_hdl, EZFS_TRIM_NOTSUP,
2316 dgettext(TEXT_DOMAIN, "cannot trim")));
2317 reported_errs = B_TRUE;
2320 return (reported_errs);
2324 * Begin, suspend, or cancel the TRIM (discarding of all free blocks) for
2325 * the given vdevs in the given pool.
2328 zpool_trim(zpool_handle_t *zhp, pool_trim_func_t cmd_type, nvlist_t *vds,
2329 trimflags_t *trim_flags)
2334 nvlist_t *vdev_guids = fnvlist_alloc();
2335 nvlist_t *guids_to_paths = fnvlist_alloc();
2336 nvlist_t *errlist = NULL;
2338 err = zpool_translate_vdev_guids(zhp, vds, vdev_guids,
2339 guids_to_paths, &errlist);
2341 check_trim_errs(zhp, trim_flags, guids_to_paths, vds, errlist);
2346 err = lzc_trim(zhp->zpool_name, cmd_type, trim_flags->rate,
2347 trim_flags->secure, vdev_guids, &errlist);
2349 nvlist_t *vd_errlist;
2350 if (errlist != NULL && nvlist_lookup_nvlist(errlist,
2351 ZPOOL_TRIM_VDEVS, &vd_errlist) == 0) {
2352 if (check_trim_errs(zhp, trim_flags, guids_to_paths,
2360 (void) snprintf(msg, sizeof (msg),
2361 dgettext(TEXT_DOMAIN, "operation failed"));
2362 zpool_standard_error(zhp->zpool_hdl, err, msg);
2369 if (trim_flags->wait)
2370 retval = zpool_trim_wait(zhp, vdev_guids);
2373 if (errlist != NULL)
2374 fnvlist_free(errlist);
2375 fnvlist_free(vdev_guids);
2376 fnvlist_free(guids_to_paths);
2384 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd)
2386 zfs_cmd_t zc = {"\0"};
2389 libzfs_handle_t *hdl = zhp->zpool_hdl;
2391 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2392 zc.zc_cookie = func;
2395 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0)
2400 /* ECANCELED on a scrub means we resumed a paused scrub */
2401 if (err == ECANCELED && func == POOL_SCAN_SCRUB &&
2402 cmd == POOL_SCRUB_NORMAL)
2405 if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL)
2408 if (func == POOL_SCAN_SCRUB) {
2409 if (cmd == POOL_SCRUB_PAUSE) {
2410 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2411 "cannot pause scrubbing %s"), zc.zc_name);
2413 assert(cmd == POOL_SCRUB_NORMAL);
2414 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2415 "cannot scrub %s"), zc.zc_name);
2417 } else if (func == POOL_SCAN_RESILVER) {
2418 assert(cmd == POOL_SCRUB_NORMAL);
2419 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2420 "cannot restart resilver on %s"), zc.zc_name);
2421 } else if (func == POOL_SCAN_NONE) {
2422 (void) snprintf(msg, sizeof (msg),
2423 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
2426 assert(!"unexpected result");
2431 pool_scan_stat_t *ps = NULL;
2434 verify(nvlist_lookup_nvlist(zhp->zpool_config,
2435 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2436 (void) nvlist_lookup_uint64_array(nvroot,
2437 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
2438 if (ps && ps->pss_func == POOL_SCAN_SCRUB &&
2439 ps->pss_state == DSS_SCANNING) {
2440 if (cmd == POOL_SCRUB_PAUSE)
2441 return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg));
2443 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
2445 return (zfs_error(hdl, EZFS_RESILVERING, msg));
2447 } else if (err == ENOENT) {
2448 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
2449 } else if (err == ENOTSUP && func == POOL_SCAN_RESILVER) {
2450 return (zfs_error(hdl, EZFS_NO_RESILVER_DEFER, msg));
2452 return (zpool_standard_error(hdl, err, msg));
2457 * Find a vdev that matches the search criteria specified. We use the
2458 * the nvpair name to determine how we should look for the device.
2459 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2460 * spare; but FALSE if its an INUSE spare.
2463 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
2464 boolean_t *l2cache, boolean_t *log)
2471 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
2473 /* Nothing to look for */
2474 if (search == NULL || pair == NULL)
2477 /* Obtain the key we will use to search */
2478 srchkey = nvpair_name(pair);
2480 switch (nvpair_type(pair)) {
2481 case DATA_TYPE_UINT64:
2482 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
2483 uint64_t srchval, theguid;
2485 verify(nvpair_value_uint64(pair, &srchval) == 0);
2486 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2488 if (theguid == srchval)
2493 case DATA_TYPE_STRING: {
2494 char *srchval, *val;
2496 verify(nvpair_value_string(pair, &srchval) == 0);
2497 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2501 * Search for the requested value. Special cases:
2503 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2504 * "-part1", or "p1". The suffix is hidden from the user,
2505 * but included in the string, so this matches around it.
2506 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2507 * is used to check all possible expanded paths.
2508 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2510 * Otherwise, all other searches are simple string compares.
2512 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) {
2513 uint64_t wholedisk = 0;
2515 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2517 if (zfs_strcmp_pathname(srchval, val, wholedisk) == 0)
2520 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2521 char *type, *idx, *end, *p;
2522 uint64_t id, vdev_id;
2525 * Determine our vdev type, keeping in mind
2526 * that the srchval is composed of a type and
2527 * vdev id pair (i.e. mirror-4).
2529 if ((type = strdup(srchval)) == NULL)
2532 if ((p = strrchr(type, '-')) == NULL) {
2540 * If the types don't match then keep looking.
2542 if (strncmp(val, type, strlen(val)) != 0) {
2547 verify(zpool_vdev_is_interior(type));
2548 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2552 vdev_id = strtoull(idx, &end, 10);
2559 * Now verify that we have the correct vdev id.
2568 if (strcmp(srchval, val) == 0)
2577 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2578 &child, &children) != 0)
2581 for (c = 0; c < children; c++) {
2582 if ((ret = vdev_to_nvlist_iter(child[c], search,
2583 avail_spare, l2cache, NULL)) != NULL) {
2585 * The 'is_log' value is only set for the toplevel
2586 * vdev, not the leaf vdevs. So we always lookup the
2587 * log device from the root of the vdev tree (where
2588 * 'log' is non-NULL).
2591 nvlist_lookup_uint64(child[c],
2592 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2600 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2601 &child, &children) == 0) {
2602 for (c = 0; c < children; c++) {
2603 if ((ret = vdev_to_nvlist_iter(child[c], search,
2604 avail_spare, l2cache, NULL)) != NULL) {
2605 *avail_spare = B_TRUE;
2611 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2612 &child, &children) == 0) {
2613 for (c = 0; c < children; c++) {
2614 if ((ret = vdev_to_nvlist_iter(child[c], search,
2615 avail_spare, l2cache, NULL)) != NULL) {
2626 * Given a physical path or guid, find the associated vdev.
2629 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2630 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2632 nvlist_t *search, *nvroot, *ret;
2636 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2638 guid = strtoull(ppath, &end, 0);
2639 if (guid != 0 && *end == '\0') {
2640 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2642 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH,
2646 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2649 *avail_spare = B_FALSE;
2653 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2654 nvlist_free(search);
2660 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2663 zpool_vdev_is_interior(const char *name)
2665 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2666 strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 ||
2668 VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 ||
2669 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2675 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2676 boolean_t *l2cache, boolean_t *log)
2679 nvlist_t *nvroot, *search, *ret;
2682 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2684 guid = strtoull(path, &end, 0);
2685 if (guid != 0 && *end == '\0') {
2686 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2687 } else if (zpool_vdev_is_interior(path)) {
2688 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2690 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2693 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2696 *avail_spare = B_FALSE;
2700 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2701 nvlist_free(search);
2707 vdev_is_online(nvlist_t *nv)
2711 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2712 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2713 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2720 * Helper function for zpool_get_physpaths().
2723 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2724 size_t *bytes_written)
2726 size_t bytes_left, pos, rsz;
2730 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2732 return (EZFS_NODEVICE);
2734 pos = *bytes_written;
2735 bytes_left = physpath_size - pos;
2736 format = (pos == 0) ? "%s" : " %s";
2738 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2739 *bytes_written += rsz;
2741 if (rsz >= bytes_left) {
2742 /* if physpath was not copied properly, clear it */
2743 if (bytes_left != 0) {
2746 return (EZFS_NOSPC);
2752 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2753 size_t *rsz, boolean_t is_spare)
2758 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2759 return (EZFS_INVALCONFIG);
2761 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2763 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2764 * For a spare vdev, we only want to boot from the active
2769 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2772 return (EZFS_INVALCONFIG);
2775 if (vdev_is_online(nv)) {
2776 if ((ret = vdev_get_one_physpath(nv, physpath,
2777 phypath_size, rsz)) != 0)
2780 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2781 strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
2782 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2783 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2788 if (nvlist_lookup_nvlist_array(nv,
2789 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2790 return (EZFS_INVALCONFIG);
2792 for (i = 0; i < count; i++) {
2793 ret = vdev_get_physpaths(child[i], physpath,
2794 phypath_size, rsz, is_spare);
2795 if (ret == EZFS_NOSPC)
2800 return (EZFS_POOL_INVALARG);
2804 * Get phys_path for a root pool config.
2805 * Return 0 on success; non-zero on failure.
2808 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2811 nvlist_t *vdev_root;
2818 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2820 return (EZFS_INVALCONFIG);
2822 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2823 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2824 &child, &count) != 0)
2825 return (EZFS_INVALCONFIG);
2828 * root pool can only have a single top-level vdev.
2830 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2831 return (EZFS_POOL_INVALARG);
2833 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2836 /* No online devices */
2838 return (EZFS_NODEVICE);
2844 * Get phys_path for a root pool
2845 * Return 0 on success; non-zero on failure.
2848 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2850 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2855 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
2857 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
2858 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
2862 zpool_vdev_path_to_guid_impl(zpool_handle_t *zhp, const char *path,
2863 boolean_t *is_spare, boolean_t *is_l2cache, boolean_t *is_log)
2866 boolean_t spare = B_FALSE, l2cache = B_FALSE, log = B_FALSE;
2869 if ((tgt = zpool_find_vdev(zhp, path, &spare, &l2cache,
2873 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &guid) == 0);
2874 if (is_spare != NULL)
2876 if (is_l2cache != NULL)
2877 *is_l2cache = l2cache;
2884 /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
2886 zpool_vdev_path_to_guid(zpool_handle_t *zhp, const char *path)
2888 return (zpool_vdev_path_to_guid_impl(zhp, path, NULL, NULL, NULL));
2892 * Bring the specified vdev online. The 'flags' parameter is a set of the
2893 * ZFS_ONLINE_* flags.
2896 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2897 vdev_state_t *newstate)
2899 zfs_cmd_t zc = {"\0"};
2903 boolean_t avail_spare, l2cache, islog;
2904 libzfs_handle_t *hdl = zhp->zpool_hdl;
2907 if (flags & ZFS_ONLINE_EXPAND) {
2908 (void) snprintf(msg, sizeof (msg),
2909 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2911 (void) snprintf(msg, sizeof (msg),
2912 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2915 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2916 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2918 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2920 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2923 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2925 if ((flags & ZFS_ONLINE_EXPAND ||
2926 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) &&
2927 nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) {
2928 uint64_t wholedisk = 0;
2930 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2934 * XXX - L2ARC 1.0 devices can't support expansion.
2937 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2938 "cannot expand cache devices"));
2939 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2943 const char *fullpath = path;
2944 char buf[MAXPATHLEN];
2946 if (path[0] != '/') {
2947 error = zfs_resolve_shortname(path, buf,
2950 return (zfs_error(hdl, EZFS_NODEVICE,
2956 error = zpool_relabel_disk(hdl, fullpath, msg);
2962 zc.zc_cookie = VDEV_STATE_ONLINE;
2965 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2966 if (errno == EINVAL) {
2967 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2968 "from this pool into a new one. Use '%s' "
2969 "instead"), "zpool detach");
2970 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2972 return (zpool_standard_error(hdl, errno, msg));
2975 *newstate = zc.zc_cookie;
2980 * Take the specified vdev offline
2983 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2985 zfs_cmd_t zc = {"\0"};
2988 boolean_t avail_spare, l2cache;
2989 libzfs_handle_t *hdl = zhp->zpool_hdl;
2991 (void) snprintf(msg, sizeof (msg),
2992 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2994 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2995 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2997 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2999 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3002 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3004 zc.zc_cookie = VDEV_STATE_OFFLINE;
3005 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
3007 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3014 * There are no other replicas of this device.
3016 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
3020 * The log device has unplayed logs
3022 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
3025 return (zpool_standard_error(hdl, errno, msg));
3030 * Mark the given vdev faulted.
3033 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
3035 zfs_cmd_t zc = {"\0"};
3037 libzfs_handle_t *hdl = zhp->zpool_hdl;
3039 (void) snprintf(msg, sizeof (msg),
3040 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
3042 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3044 zc.zc_cookie = VDEV_STATE_FAULTED;
3047 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3054 * There are no other replicas of this device.
3056 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
3059 return (zpool_standard_error(hdl, errno, msg));
3065 * Mark the given vdev degraded.
3068 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
3070 zfs_cmd_t zc = {"\0"};
3072 libzfs_handle_t *hdl = zhp->zpool_hdl;
3074 (void) snprintf(msg, sizeof (msg),
3075 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid);
3077 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3079 zc.zc_cookie = VDEV_STATE_DEGRADED;
3082 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3085 return (zpool_standard_error(hdl, errno, msg));
3089 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
3093 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
3099 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
3101 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
3104 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
3105 children == 2 && child[which] == tgt)
3108 for (c = 0; c < children; c++)
3109 if (is_replacing_spare(child[c], tgt, which))
3117 * Attach new_disk (fully described by nvroot) to old_disk.
3118 * If 'replacing' is specified, the new disk will replace the old one.
3121 zpool_vdev_attach(zpool_handle_t *zhp, const char *old_disk,
3122 const char *new_disk, nvlist_t *nvroot, int replacing, boolean_t rebuild)
3124 zfs_cmd_t zc = {"\0"};
3128 boolean_t avail_spare, l2cache, islog;
3133 nvlist_t *config_root;
3134 libzfs_handle_t *hdl = zhp->zpool_hdl;
3137 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3138 "cannot replace %s with %s"), old_disk, new_disk);
3140 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3141 "cannot attach %s to %s"), new_disk, old_disk);
3143 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3144 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
3146 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3149 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3152 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
3154 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3155 zc.zc_cookie = replacing;
3156 zc.zc_simple = rebuild;
3159 zfeature_lookup_guid("org.openzfs:device_rebuild", NULL) != 0) {
3160 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3161 "the loaded zfs module doesn't support device rebuilds"));
3162 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
3165 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3166 &child, &children) != 0 || children != 1) {
3167 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3168 "new device must be a single disk"));
3169 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
3172 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
3173 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
3175 if ((newname = zpool_vdev_name(NULL, NULL, child[0], 0)) == NULL)
3179 * If the target is a hot spare that has been swapped in, we can only
3180 * replace it with another hot spare.
3183 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
3184 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
3185 NULL) == NULL || !avail_spare) &&
3186 is_replacing_spare(config_root, tgt, 1)) {
3187 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3188 "can only be replaced by another hot spare"));
3190 return (zfs_error(hdl, EZFS_BADTARGET, msg));
3195 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
3198 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
3200 zcmd_free_nvlists(&zc);
3208 * Can't attach to or replace this type of vdev.
3211 uint64_t version = zpool_get_prop_int(zhp,
3212 ZPOOL_PROP_VERSION, NULL);
3215 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3216 "cannot replace a log with a spare"));
3217 } else if (rebuild) {
3218 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3219 "only mirror vdevs support sequential "
3221 } else if (version >= SPA_VERSION_MULTI_REPLACE) {
3222 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3223 "already in replacing/spare config; wait "
3224 "for completion or use 'zpool detach'"));
3226 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3227 "cannot replace a replacing device"));
3230 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3231 "can only attach to mirrors and top-level "
3234 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
3239 * The new device must be a single disk.
3241 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3242 "new device must be a single disk"));
3243 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3247 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy, "
3248 "or device removal is in progress"),
3250 (void) zfs_error(hdl, EZFS_BADDEV, msg);
3255 * The new device is too small.
3257 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3258 "device is too small"));
3259 (void) zfs_error(hdl, EZFS_BADDEV, msg);
3264 * The new device has a different optimal sector size.
3266 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3267 "new device has a different optimal sector size; use the "
3268 "option '-o ashift=N' to override the optimal size"));
3269 (void) zfs_error(hdl, EZFS_BADDEV, msg);
3274 * The resulting top-level vdev spec won't fit in the label.
3276 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
3280 (void) zpool_standard_error(hdl, errno, msg);
3287 * Detach the specified device.
3290 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
3292 zfs_cmd_t zc = {"\0"};
3295 boolean_t avail_spare, l2cache;
3296 libzfs_handle_t *hdl = zhp->zpool_hdl;
3298 (void) snprintf(msg, sizeof (msg),
3299 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
3301 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3302 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3304 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3307 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3310 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
3312 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3314 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
3321 * Can't detach from this type of vdev.
3323 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
3324 "applicable to mirror and replacing vdevs"));
3325 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
3330 * There are no other replicas of this device.
3332 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
3336 (void) zpool_standard_error(hdl, errno, msg);
3343 * Find a mirror vdev in the source nvlist.
3345 * The mchild array contains a list of disks in one of the top-level mirrors
3346 * of the source pool. The schild array contains a list of disks that the
3347 * user specified on the command line. We loop over the mchild array to
3348 * see if any entry in the schild array matches.
3350 * If a disk in the mchild array is found in the schild array, we return
3351 * the index of that entry. Otherwise we return -1.
3354 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
3355 nvlist_t **schild, uint_t schildren)
3359 for (mc = 0; mc < mchildren; mc++) {
3361 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3364 for (sc = 0; sc < schildren; sc++) {
3365 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3367 boolean_t result = (strcmp(mpath, spath) == 0);
3383 * Split a mirror pool. If newroot points to null, then a new nvlist
3384 * is generated and it is the responsibility of the caller to free it.
3387 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
3388 nvlist_t *props, splitflags_t flags)
3390 zfs_cmd_t zc = {"\0"};
3392 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
3393 nvlist_t **varray = NULL, *zc_props = NULL;
3394 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
3395 libzfs_handle_t *hdl = zhp->zpool_hdl;
3396 uint64_t vers, readonly = B_FALSE;
3397 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
3400 (void) snprintf(msg, sizeof (msg),
3401 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
3403 if (!zpool_name_valid(hdl, B_FALSE, newname))
3404 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
3406 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
3407 (void) fprintf(stderr, gettext("Internal error: unable to "
3408 "retrieve pool configuration\n"));
3412 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
3414 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
3417 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
3418 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
3419 props, vers, flags, msg)) == NULL)
3421 (void) nvlist_lookup_uint64(zc_props,
3422 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3424 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3425 "property %s can only be set at import time"),
3426 zpool_prop_to_name(ZPOOL_PROP_READONLY));
3431 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3433 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3434 "Source pool is missing vdev tree"));
3435 nvlist_free(zc_props);
3439 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
3442 if (*newroot == NULL ||
3443 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
3444 &newchild, &newchildren) != 0)
3447 for (c = 0; c < children; c++) {
3448 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
3450 nvlist_t **mchild, *vdev;
3455 * Unlike cache & spares, slogs are stored in the
3456 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3458 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3460 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
3462 if (is_log || is_hole) {
3464 * Create a hole vdev and put it in the config.
3466 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
3468 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
3469 VDEV_TYPE_HOLE) != 0)
3471 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
3476 varray[vcount++] = vdev;
3480 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
3483 if (strcmp(type, VDEV_TYPE_INDIRECT) == 0) {
3485 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3488 } else if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
3489 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3490 "Source pool must be composed only of mirrors\n"));
3491 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3495 verify(nvlist_lookup_nvlist_array(child[c],
3496 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3498 /* find or add an entry for this top-level vdev */
3499 if (newchildren > 0 &&
3500 (entry = find_vdev_entry(zhp, mchild, mchildren,
3501 newchild, newchildren)) >= 0) {
3502 /* We found a disk that the user specified. */
3503 vdev = mchild[entry];
3506 /* User didn't specify a disk for this vdev. */
3507 vdev = mchild[mchildren - 1];
3510 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3514 /* did we find every disk the user specified? */
3515 if (found != newchildren) {
3516 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3517 "include at most one disk from each mirror"));
3518 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3522 /* Prepare the nvlist for populating. */
3523 if (*newroot == NULL) {
3524 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3527 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3528 VDEV_TYPE_ROOT) != 0)
3531 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3534 /* Add all the children we found */
3535 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3536 lastlog == 0 ? vcount : lastlog) != 0)
3540 * If we're just doing a dry run, exit now with success.
3543 memory_err = B_FALSE;
3548 /* now build up the config list & call the ioctl */
3549 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3552 if (nvlist_add_nvlist(newconfig,
3553 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3554 nvlist_add_string(newconfig,
3555 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3556 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3560 * The new pool is automatically part of the namespace unless we
3561 * explicitly export it.
3564 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3565 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3566 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3567 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3569 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3572 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3573 retval = zpool_standard_error(hdl, errno, msg);
3578 memory_err = B_FALSE;
3581 if (varray != NULL) {
3584 for (v = 0; v < vcount; v++)
3585 nvlist_free(varray[v]);
3588 zcmd_free_nvlists(&zc);
3589 nvlist_free(zc_props);
3590 nvlist_free(newconfig);
3592 nvlist_free(*newroot);
3600 return (no_memory(hdl));
3606 * Remove the given device.
3609 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3611 zfs_cmd_t zc = {"\0"};
3614 boolean_t avail_spare, l2cache, islog;
3615 libzfs_handle_t *hdl = zhp->zpool_hdl;
3618 (void) snprintf(msg, sizeof (msg),
3619 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3621 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3622 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3624 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3626 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3627 if (islog && version < SPA_VERSION_HOLES) {
3628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3629 "pool must be upgraded to support log removal"));
3630 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3633 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3635 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3641 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3642 "invalid config; all top-level vdevs must "
3643 "have the same sector size and not be raidz."));
3644 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3649 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3650 "Mount encrypted datasets to replay logs."));
3652 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3653 "Pool busy; removal may already be in progress"));
3655 (void) zfs_error(hdl, EZFS_BUSY, msg);
3660 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3661 "Mount encrypted datasets to replay logs."));
3662 (void) zfs_error(hdl, EZFS_BUSY, msg);
3664 (void) zpool_standard_error(hdl, errno, msg);
3669 (void) zpool_standard_error(hdl, errno, msg);
3675 zpool_vdev_remove_cancel(zpool_handle_t *zhp)
3679 libzfs_handle_t *hdl = zhp->zpool_hdl;
3681 (void) snprintf(msg, sizeof (msg),
3682 dgettext(TEXT_DOMAIN, "cannot cancel removal"));
3684 bzero(&zc, sizeof (zc));
3685 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3688 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3691 return (zpool_standard_error(hdl, errno, msg));
3695 zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path,
3700 boolean_t avail_spare, l2cache, islog;
3701 libzfs_handle_t *hdl = zhp->zpool_hdl;
3703 (void) snprintf(msg, sizeof (msg),
3704 dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"),
3707 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3709 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3711 if (avail_spare || l2cache || islog) {
3716 if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) {
3717 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3718 "indirect size not available"));
3719 return (zfs_error(hdl, EINVAL, msg));
3725 * Clear the errors for the pool, or the particular device if specified.
3728 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3730 zfs_cmd_t zc = {"\0"};
3733 zpool_load_policy_t policy;
3734 boolean_t avail_spare, l2cache;
3735 libzfs_handle_t *hdl = zhp->zpool_hdl;
3736 nvlist_t *nvi = NULL;
3740 (void) snprintf(msg, sizeof (msg),
3741 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3744 (void) snprintf(msg, sizeof (msg),
3745 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3748 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3750 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3751 &l2cache, NULL)) == NULL)
3752 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3755 * Don't allow error clearing for hot spares. Do allow
3756 * error clearing for l2cache devices.
3759 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3761 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3765 zpool_get_load_policy(rewindnvl, &policy);
3766 zc.zc_cookie = policy.zlp_rewind;
3768 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3771 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3774 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3776 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3777 zcmd_free_nvlists(&zc);
3782 if (!error || ((policy.zlp_rewind & ZPOOL_TRY_REWIND) &&
3783 errno != EPERM && errno != EACCES)) {
3784 if (policy.zlp_rewind &
3785 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3786 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3787 zpool_rewind_exclaim(hdl, zc.zc_name,
3788 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0),
3792 zcmd_free_nvlists(&zc);
3796 zcmd_free_nvlists(&zc);
3797 return (zpool_standard_error(hdl, errno, msg));
3801 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3804 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3806 zfs_cmd_t zc = {"\0"};
3808 libzfs_handle_t *hdl = zhp->zpool_hdl;
3810 (void) snprintf(msg, sizeof (msg),
3811 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3812 (u_longlong_t)guid);
3814 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3816 zc.zc_cookie = ZPOOL_NO_REWIND;
3818 if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
3821 return (zpool_standard_error(hdl, errno, msg));
3825 * Change the GUID for a pool.
3828 zpool_reguid(zpool_handle_t *zhp)
3831 libzfs_handle_t *hdl = zhp->zpool_hdl;
3832 zfs_cmd_t zc = {"\0"};
3834 (void) snprintf(msg, sizeof (msg),
3835 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3837 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3838 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3841 return (zpool_standard_error(hdl, errno, msg));
3848 zpool_reopen_one(zpool_handle_t *zhp, void *data)
3850 libzfs_handle_t *hdl = zpool_get_handle(zhp);
3851 const char *pool_name = zpool_get_name(zhp);
3852 boolean_t *scrub_restart = data;
3855 error = lzc_reopen(pool_name, *scrub_restart);
3857 return (zpool_standard_error_fmt(hdl, error,
3858 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"), pool_name));
3864 /* call into libzfs_core to execute the sync IOCTL per pool */
3866 zpool_sync_one(zpool_handle_t *zhp, void *data)
3869 libzfs_handle_t *hdl = zpool_get_handle(zhp);
3870 const char *pool_name = zpool_get_name(zhp);
3871 boolean_t *force = data;
3872 nvlist_t *innvl = fnvlist_alloc();
3874 fnvlist_add_boolean_value(innvl, "force", *force);
3875 if ((ret = lzc_sync(pool_name, innvl, NULL)) != 0) {
3877 return (zpool_standard_error_fmt(hdl, ret,
3878 dgettext(TEXT_DOMAIN, "sync '%s' failed"), pool_name));
3885 #define PATH_BUF_LEN 64
3888 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3889 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3890 * We also check if this is a whole disk, in which case we strip off the
3891 * trailing 's0' slice name.
3893 * This routine is also responsible for identifying when disks have been
3894 * reconfigured in a new location. The kernel will have opened the device by
3895 * devid, but the path will still refer to the old location. To catch this, we
3896 * first do a path -> devid translation (which is fast for the common case). If
3897 * the devid matches, we're done. If not, we do a reverse devid -> path
3898 * translation and issue the appropriate ioctl() to update the path of the vdev.
3899 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3903 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3906 char *path, *type, *env;
3908 char buf[PATH_BUF_LEN];
3909 char tmpbuf[PATH_BUF_LEN];
3912 * vdev_name will be "root"/"root-0" for the root vdev, but it is the
3913 * zpool name that will be displayed to the user.
3915 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
3916 if (zhp != NULL && strcmp(type, "root") == 0)
3917 return (zfs_strdup(hdl, zpool_get_name(zhp)));
3919 env = getenv("ZPOOL_VDEV_NAME_PATH");
3920 if (env && (strtoul(env, NULL, 0) > 0 ||
3921 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3922 name_flags |= VDEV_NAME_PATH;
3924 env = getenv("ZPOOL_VDEV_NAME_GUID");
3925 if (env && (strtoul(env, NULL, 0) > 0 ||
3926 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3927 name_flags |= VDEV_NAME_GUID;
3929 env = getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
3930 if (env && (strtoul(env, NULL, 0) > 0 ||
3931 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3932 name_flags |= VDEV_NAME_FOLLOW_LINKS;
3934 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3935 name_flags & VDEV_NAME_GUID) {
3936 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value);
3937 (void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)value);
3939 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3940 if (name_flags & VDEV_NAME_FOLLOW_LINKS) {
3941 char *rp = realpath(path, NULL);
3943 strlcpy(buf, rp, sizeof (buf));
3950 * For a block device only use the name.
3952 if ((strcmp(type, VDEV_TYPE_DISK) == 0) &&
3953 !(name_flags & VDEV_NAME_PATH)) {
3954 path = zfs_strip_path(path);
3958 * Remove the partition from the path it this is a whole disk.
3960 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, &value)
3961 == 0 && value && !(name_flags & VDEV_NAME_PATH)) {
3962 return (zfs_strip_partition(path));
3968 * If it's a raidz device, we need to stick in the parity level.
3970 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3971 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3973 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3974 (u_longlong_t)value);
3979 * We identify each top-level vdev by using a <type-id>
3980 * naming convention.
3982 if (name_flags & VDEV_NAME_TYPE_ID) {
3984 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3986 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s-%llu",
3987 path, (u_longlong_t)id);
3992 return (zfs_strdup(hdl, path));
3996 zbookmark_mem_compare(const void *a, const void *b)
3998 return (memcmp(a, b, sizeof (zbookmark_phys_t)));
4002 * Retrieve the persistent error log, uniquify the members, and return to the
4006 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
4008 zfs_cmd_t zc = {"\0"};
4009 libzfs_handle_t *hdl = zhp->zpool_hdl;
4011 zbookmark_phys_t *zb = NULL;
4015 * Retrieve the raw error list from the kernel. If the number of errors
4016 * has increased, allocate more space and continue until we get the
4019 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
4023 zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
4024 count * sizeof (zbookmark_phys_t));
4025 zc.zc_nvlist_dst_size = count;
4026 (void) strcpy(zc.zc_name, zhp->zpool_name);
4028 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_ERROR_LOG,
4030 free((void *)(uintptr_t)zc.zc_nvlist_dst);
4031 if (errno == ENOMEM) {
4034 count = zc.zc_nvlist_dst_size;
4035 dst = zfs_alloc(zhp->zpool_hdl, count *
4036 sizeof (zbookmark_phys_t));
4037 zc.zc_nvlist_dst = (uintptr_t)dst;
4039 return (zpool_standard_error_fmt(hdl, errno,
4040 dgettext(TEXT_DOMAIN, "errors: List of "
4041 "errors unavailable")));
4049 * Sort the resulting bookmarks. This is a little confusing due to the
4050 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
4051 * to first, and 'zc_nvlist_dst_size' indicates the number of bookmarks
4052 * _not_ copied as part of the process. So we point the start of our
4053 * array appropriate and decrement the total number of elements.
4055 zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
4056 zc.zc_nvlist_dst_size;
4057 count -= zc.zc_nvlist_dst_size;
4059 qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
4061 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
4064 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
4066 for (i = 0; i < count; i++) {
4069 /* ignoring zb_blkid and zb_level for now */
4070 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
4071 zb[i-1].zb_object == zb[i].zb_object)
4074 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
4076 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
4077 zb[i].zb_objset) != 0) {
4081 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
4082 zb[i].zb_object) != 0) {
4086 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
4093 free((void *)(uintptr_t)zc.zc_nvlist_dst);
4097 free((void *)(uintptr_t)zc.zc_nvlist_dst);
4098 return (no_memory(zhp->zpool_hdl));
4102 * Upgrade a ZFS pool to the latest on-disk version.
4105 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
4107 zfs_cmd_t zc = {"\0"};
4108 libzfs_handle_t *hdl = zhp->zpool_hdl;
4110 (void) strcpy(zc.zc_name, zhp->zpool_name);
4111 zc.zc_cookie = new_version;
4113 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
4114 return (zpool_standard_error_fmt(hdl, errno,
4115 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
4121 zfs_save_arguments(int argc, char **argv, char *string, int len)
4125 (void) strlcpy(string, basename(argv[0]), len);
4126 for (i = 1; i < argc; i++) {
4127 (void) strlcat(string, " ", len);
4128 (void) strlcat(string, argv[i], len);
4133 zpool_log_history(libzfs_handle_t *hdl, const char *message)
4135 zfs_cmd_t zc = {"\0"};
4139 args = fnvlist_alloc();
4140 fnvlist_add_string(args, "message", message);
4141 err = zcmd_write_src_nvlist(hdl, &zc, args);
4143 err = zfs_ioctl(hdl, ZFS_IOC_LOG_HISTORY, &zc);
4145 zcmd_free_nvlists(&zc);
4150 * Perform ioctl to get some command history of a pool.
4152 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
4153 * logical offset of the history buffer to start reading from.
4155 * Upon return, 'off' is the next logical offset to read from and
4156 * 'len' is the actual amount of bytes read into 'buf'.
4159 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
4161 zfs_cmd_t zc = {"\0"};
4162 libzfs_handle_t *hdl = zhp->zpool_hdl;
4164 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4166 zc.zc_history = (uint64_t)(uintptr_t)buf;
4167 zc.zc_history_len = *len;
4168 zc.zc_history_offset = *off;
4170 if (zfs_ioctl(hdl, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
4173 return (zfs_error_fmt(hdl, EZFS_PERM,
4174 dgettext(TEXT_DOMAIN,
4175 "cannot show history for pool '%s'"),
4178 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
4179 dgettext(TEXT_DOMAIN, "cannot get history for pool "
4180 "'%s'"), zhp->zpool_name));
4182 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
4183 dgettext(TEXT_DOMAIN, "cannot get history for pool "
4184 "'%s', pool must be upgraded"), zhp->zpool_name));
4186 return (zpool_standard_error_fmt(hdl, errno,
4187 dgettext(TEXT_DOMAIN,
4188 "cannot get history for '%s'"), zhp->zpool_name));
4192 *len = zc.zc_history_len;
4193 *off = zc.zc_history_offset;
4199 * Retrieve the command history of a pool.
4202 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp, uint64_t *off,
4206 int buflen = 128 * 1024;
4207 nvlist_t **records = NULL;
4208 uint_t numrecords = 0;
4210 uint64_t start = *off;
4212 buf = malloc(buflen);
4215 /* process about 1MB a time */
4216 while (*off - start < 1024 * 1024) {
4217 uint64_t bytes_read = buflen;
4220 if ((err = get_history(zhp, buf, off, &bytes_read)) != 0)
4223 /* if nothing else was read in, we're at EOF, just return */
4229 if ((err = zpool_history_unpack(buf, bytes_read,
4230 &leftover, &records, &numrecords)) != 0)
4233 if (leftover == bytes_read) {
4235 * no progress made, because buffer is not big enough
4236 * to hold this record; resize and retry.
4240 buf = malloc(buflen);
4249 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
4250 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
4251 records, numrecords) == 0);
4253 for (i = 0; i < numrecords; i++)
4254 nvlist_free(records[i]);
4261 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
4262 * If there is a new event available 'nvp' will contain a newly allocated
4263 * nvlist and 'dropped' will be set to the number of missed events since
4264 * the last call to this function. When 'nvp' is set to NULL it indicates
4265 * no new events are available. In either case the function returns 0 and
4266 * it is up to the caller to free 'nvp'. In the case of a fatal error the
4267 * function will return a non-zero value. When the function is called in
4268 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
4269 * it will not return until a new event is available.
4272 zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
4273 int *dropped, unsigned flags, int zevent_fd)
4275 zfs_cmd_t zc = {"\0"};
4280 zc.zc_cleanup_fd = zevent_fd;
4282 if (flags & ZEVENT_NONBLOCK)
4283 zc.zc_guid = ZEVENT_NONBLOCK;
4285 if (zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE) != 0)
4289 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
4292 error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
4293 dgettext(TEXT_DOMAIN, "zfs shutdown"));
4296 /* Blocking error case should not occur */
4297 if (!(flags & ZEVENT_NONBLOCK))
4298 error = zpool_standard_error_fmt(hdl, errno,
4299 dgettext(TEXT_DOMAIN, "cannot get event"));
4303 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
4304 error = zfs_error_fmt(hdl, EZFS_NOMEM,
4305 dgettext(TEXT_DOMAIN, "cannot get event"));
4311 error = zpool_standard_error_fmt(hdl, errno,
4312 dgettext(TEXT_DOMAIN, "cannot get event"));
4317 error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
4321 *dropped = (int)zc.zc_cookie;
4323 zcmd_free_nvlists(&zc);
4332 zpool_events_clear(libzfs_handle_t *hdl, int *count)
4334 zfs_cmd_t zc = {"\0"};
4337 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
4338 "cannot clear events"));
4340 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
4341 return (zpool_standard_error_fmt(hdl, errno, msg));
4344 *count = (int)zc.zc_cookie; /* # of events cleared */
4350 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4351 * the passed zevent_fd file handle. On success zero is returned,
4352 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4355 zpool_events_seek(libzfs_handle_t *hdl, uint64_t eid, int zevent_fd)
4357 zfs_cmd_t zc = {"\0"};
4361 zc.zc_cleanup_fd = zevent_fd;
4363 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_SEEK, &zc) != 0) {
4366 error = zfs_error_fmt(hdl, EZFS_NOENT,
4367 dgettext(TEXT_DOMAIN, "cannot get event"));
4371 error = zfs_error_fmt(hdl, EZFS_NOMEM,
4372 dgettext(TEXT_DOMAIN, "cannot get event"));
4376 error = zpool_standard_error_fmt(hdl, errno,
4377 dgettext(TEXT_DOMAIN, "cannot get event"));
4386 zpool_obj_to_path_impl(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4387 char *pathname, size_t len, boolean_t always_unmounted)
4389 zfs_cmd_t zc = {"\0"};
4390 boolean_t mounted = B_FALSE;
4391 char *mntpnt = NULL;
4392 char dsname[ZFS_MAX_DATASET_NAME_LEN];
4395 /* special case for the MOS */
4396 (void) snprintf(pathname, len, "<metadata>:<0x%llx>",
4401 /* get the dataset's name */
4402 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4404 if (zfs_ioctl(zhp->zpool_hdl,
4405 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
4406 /* just write out a path of two object numbers */
4407 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
4408 (longlong_t)dsobj, (longlong_t)obj);
4411 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
4413 /* find out if the dataset is mounted */
4414 mounted = !always_unmounted && is_mounted(zhp->zpool_hdl, dsname,
4417 /* get the corrupted object's path */
4418 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
4420 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_OBJ_TO_PATH,
4423 (void) snprintf(pathname, len, "%s%s", mntpnt,
4426 (void) snprintf(pathname, len, "%s:%s",
4427 dsname, zc.zc_value);
4430 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname,
4437 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4438 char *pathname, size_t len)
4440 zpool_obj_to_path_impl(zhp, dsobj, obj, pathname, len, B_FALSE);
4444 zpool_obj_to_path_ds(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4445 char *pathname, size_t len)
4447 zpool_obj_to_path_impl(zhp, dsobj, obj, pathname, len, B_TRUE);
4450 * Wait while the specified activity is in progress in the pool.
4453 zpool_wait(zpool_handle_t *zhp, zpool_wait_activity_t activity)
4457 int error = zpool_wait_status(zhp, activity, &missing, NULL);
4460 (void) zpool_standard_error_fmt(zhp->zpool_hdl, ENOENT,
4461 dgettext(TEXT_DOMAIN, "error waiting in pool '%s'"),
4470 * Wait for the given activity and return the status of the wait (whether or not
4471 * any waiting was done) in the 'waited' parameter. Non-existent pools are
4472 * reported via the 'missing' parameter, rather than by printing an error
4473 * message. This is convenient when this function is called in a loop over a
4474 * long period of time (as it is, for example, by zpool's wait cmd). In that
4475 * scenario, a pool being exported or destroyed should be considered a normal
4476 * event, so we don't want to print an error when we find that the pool doesn't
4480 zpool_wait_status(zpool_handle_t *zhp, zpool_wait_activity_t activity,
4481 boolean_t *missing, boolean_t *waited)
4483 int error = lzc_wait(zhp->zpool_name, activity, waited);
4484 *missing = (error == ENOENT);
4489 (void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
4490 dgettext(TEXT_DOMAIN, "error waiting in pool '%s'"),
4498 zpool_set_bootenv(zpool_handle_t *zhp, const nvlist_t *envmap)
4500 int error = lzc_set_bootenv(zhp->zpool_name, envmap);
4502 (void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
4503 dgettext(TEXT_DOMAIN,
4504 "error setting bootenv in pool '%s'"), zhp->zpool_name);
4511 zpool_get_bootenv(zpool_handle_t *zhp, nvlist_t **nvlp)
4517 error = lzc_get_bootenv(zhp->zpool_name, &nvl);
4519 (void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
4520 dgettext(TEXT_DOMAIN,
4521 "error getting bootenv in pool '%s'"), zhp->zpool_name);