4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27 * Copyright (c) 2018 Datto Inc.
28 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
29 * Copyright (c) 2017, Intel Corporation.
30 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>
42 #include <sys/efi_partition.h>
43 #include <sys/systeminfo.h>
45 #include <sys/zfs_ioctl.h>
46 #include <sys/vdev_disk.h>
50 #include "zfs_namecheck.h"
52 #include "libzfs_impl.h"
53 #include "zfs_comutil.h"
54 #include "zfeature_common.h"
56 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
57 static boolean_t zpool_vdev_is_interior(const char *name);
59 typedef struct prop_flags {
60 int create:1; /* Validate property on creation */
61 int import:1; /* Validate property on import */
65 * ====================================================================
66 * zpool property functions
67 * ====================================================================
71 zpool_get_all_props(zpool_handle_t *zhp)
73 zfs_cmd_t zc = {"\0"};
74 libzfs_handle_t *hdl = zhp->zpool_hdl;
76 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
78 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
81 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
82 if (errno == ENOMEM) {
83 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
84 zcmd_free_nvlists(&zc);
88 zcmd_free_nvlists(&zc);
93 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
94 zcmd_free_nvlists(&zc);
98 zcmd_free_nvlists(&zc);
104 zpool_props_refresh(zpool_handle_t *zhp)
108 old_props = zhp->zpool_props;
110 if (zpool_get_all_props(zhp) != 0)
113 nvlist_free(old_props);
118 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
124 zprop_source_t source;
126 nvl = zhp->zpool_props;
127 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
128 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
130 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
132 source = ZPROP_SRC_DEFAULT;
133 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
144 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
148 zprop_source_t source;
150 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
152 * zpool_get_all_props() has most likely failed because
153 * the pool is faulted, but if all we need is the top level
154 * vdev's guid then get it from the zhp config nvlist.
156 if ((prop == ZPOOL_PROP_GUID) &&
157 (nvlist_lookup_nvlist(zhp->zpool_config,
158 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
159 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
163 return (zpool_prop_default_numeric(prop));
166 nvl = zhp->zpool_props;
167 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
168 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
170 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
172 source = ZPROP_SRC_DEFAULT;
173 value = zpool_prop_default_numeric(prop);
183 * Map VDEV STATE to printed strings.
186 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
189 case VDEV_STATE_CLOSED:
190 case VDEV_STATE_OFFLINE:
191 return (gettext("OFFLINE"));
192 case VDEV_STATE_REMOVED:
193 return (gettext("REMOVED"));
194 case VDEV_STATE_CANT_OPEN:
195 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
196 return (gettext("FAULTED"));
197 else if (aux == VDEV_AUX_SPLIT_POOL)
198 return (gettext("SPLIT"));
200 return (gettext("UNAVAIL"));
201 case VDEV_STATE_FAULTED:
202 return (gettext("FAULTED"));
203 case VDEV_STATE_DEGRADED:
204 return (gettext("DEGRADED"));
205 case VDEV_STATE_HEALTHY:
206 return (gettext("ONLINE"));
212 return (gettext("UNKNOWN"));
216 * Map POOL STATE to printed strings.
219 zpool_pool_state_to_name(pool_state_t state)
224 case POOL_STATE_ACTIVE:
225 return (gettext("ACTIVE"));
226 case POOL_STATE_EXPORTED:
227 return (gettext("EXPORTED"));
228 case POOL_STATE_DESTROYED:
229 return (gettext("DESTROYED"));
230 case POOL_STATE_SPARE:
231 return (gettext("SPARE"));
232 case POOL_STATE_L2CACHE:
233 return (gettext("L2CACHE"));
234 case POOL_STATE_UNINITIALIZED:
235 return (gettext("UNINITIALIZED"));
236 case POOL_STATE_UNAVAIL:
237 return (gettext("UNAVAIL"));
238 case POOL_STATE_POTENTIALLY_ACTIVE:
239 return (gettext("POTENTIALLY_ACTIVE"));
242 return (gettext("UNKNOWN"));
246 * Given a pool handle, return the pool health string ("ONLINE", "DEGRADED",
250 zpool_get_state_str(zpool_handle_t *zhp)
252 zpool_errata_t errata;
253 zpool_status_t status;
259 status = zpool_get_status(zhp, NULL, &errata);
261 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
262 str = gettext("FAULTED");
263 } else if (status == ZPOOL_STATUS_IO_FAILURE_WAIT ||
264 status == ZPOOL_STATUS_IO_FAILURE_MMP) {
265 str = gettext("SUSPENDED");
267 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
268 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
269 verify(nvlist_lookup_uint64_array(nvroot,
270 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
272 str = zpool_state_to_name(vs->vs_state, vs->vs_aux);
278 * Get a zpool property value for 'prop' and return the value in
279 * a pre-allocated buffer.
282 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf,
283 size_t len, zprop_source_t *srctype, boolean_t literal)
287 zprop_source_t src = ZPROP_SRC_NONE;
289 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
291 case ZPOOL_PROP_NAME:
292 (void) strlcpy(buf, zpool_get_name(zhp), len);
295 case ZPOOL_PROP_HEALTH:
296 (void) strlcpy(buf, zpool_get_state_str(zhp), len);
299 case ZPOOL_PROP_GUID:
300 intval = zpool_get_prop_int(zhp, prop, &src);
301 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
304 case ZPOOL_PROP_ALTROOT:
305 case ZPOOL_PROP_CACHEFILE:
306 case ZPOOL_PROP_COMMENT:
307 if (zhp->zpool_props != NULL ||
308 zpool_get_all_props(zhp) == 0) {
310 zpool_get_prop_string(zhp, prop, &src),
316 (void) strlcpy(buf, "-", len);
325 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
326 prop != ZPOOL_PROP_NAME)
329 switch (zpool_prop_get_type(prop)) {
330 case PROP_TYPE_STRING:
331 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
335 case PROP_TYPE_NUMBER:
336 intval = zpool_get_prop_int(zhp, prop, &src);
339 case ZPOOL_PROP_SIZE:
340 case ZPOOL_PROP_ALLOCATED:
341 case ZPOOL_PROP_FREE:
342 case ZPOOL_PROP_FREEING:
343 case ZPOOL_PROP_LEAKED:
344 case ZPOOL_PROP_ASHIFT:
346 (void) snprintf(buf, len, "%llu",
347 (u_longlong_t)intval);
349 (void) zfs_nicenum(intval, buf, len);
352 case ZPOOL_PROP_EXPANDSZ:
353 case ZPOOL_PROP_CHECKPOINT:
355 (void) strlcpy(buf, "-", len);
356 } else if (literal) {
357 (void) snprintf(buf, len, "%llu",
358 (u_longlong_t)intval);
360 (void) zfs_nicebytes(intval, buf, len);
364 case ZPOOL_PROP_CAPACITY:
366 (void) snprintf(buf, len, "%llu",
367 (u_longlong_t)intval);
369 (void) snprintf(buf, len, "%llu%%",
370 (u_longlong_t)intval);
374 case ZPOOL_PROP_FRAGMENTATION:
375 if (intval == UINT64_MAX) {
376 (void) strlcpy(buf, "-", len);
377 } else if (literal) {
378 (void) snprintf(buf, len, "%llu",
379 (u_longlong_t)intval);
381 (void) snprintf(buf, len, "%llu%%",
382 (u_longlong_t)intval);
386 case ZPOOL_PROP_DEDUPRATIO:
388 (void) snprintf(buf, len, "%llu.%02llu",
389 (u_longlong_t)(intval / 100),
390 (u_longlong_t)(intval % 100));
392 (void) snprintf(buf, len, "%llu.%02llux",
393 (u_longlong_t)(intval / 100),
394 (u_longlong_t)(intval % 100));
397 case ZPOOL_PROP_HEALTH:
398 (void) strlcpy(buf, zpool_get_state_str(zhp), len);
400 case ZPOOL_PROP_VERSION:
401 if (intval >= SPA_VERSION_FEATURES) {
402 (void) snprintf(buf, len, "-");
407 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
411 case PROP_TYPE_INDEX:
412 intval = zpool_get_prop_int(zhp, prop, &src);
413 if (zpool_prop_index_to_string(prop, intval, &strval)
416 (void) strlcpy(buf, strval, len);
430 * Check if the bootfs name has the same pool name as it is set to.
431 * Assuming bootfs is a valid dataset name.
434 bootfs_name_valid(const char *pool, char *bootfs)
436 int len = strlen(pool);
437 if (bootfs[0] == '\0')
440 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
443 if (strncmp(pool, bootfs, len) == 0 &&
444 (bootfs[len] == '/' || bootfs[len] == '\0'))
451 zpool_is_bootable(zpool_handle_t *zhp)
453 char bootfs[ZFS_MAX_DATASET_NAME_LEN];
455 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
456 sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-",
457 sizeof (bootfs)) != 0);
462 * Given an nvlist of zpool properties to be set, validate that they are
463 * correct, and parse any numeric properties (index, boolean, etc) if they are
464 * specified as strings.
467 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
468 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
476 struct stat64 statbuf;
479 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
480 (void) no_memory(hdl);
485 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
486 const char *propname = nvpair_name(elem);
488 prop = zpool_name_to_prop(propname);
489 if (prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname)) {
491 char *fname = strchr(propname, '@') + 1;
493 err = zfeature_lookup_name(fname, NULL);
495 ASSERT3U(err, ==, ENOENT);
496 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
497 "invalid feature '%s'"), fname);
498 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
502 if (nvpair_type(elem) != DATA_TYPE_STRING) {
503 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
504 "'%s' must be a string"), propname);
505 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
509 (void) nvpair_value_string(elem, &strval);
510 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0 &&
511 strcmp(strval, ZFS_FEATURE_DISABLED) != 0) {
512 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
513 "property '%s' can only be set to "
514 "'enabled' or 'disabled'"), propname);
515 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
520 strcmp(strval, ZFS_FEATURE_DISABLED) == 0) {
521 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
522 "property '%s' can only be set to "
523 "'disabled' at creation time"), propname);
524 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
528 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
529 (void) no_memory(hdl);
536 * Make sure this property is valid and applies to this type.
538 if (prop == ZPOOL_PROP_INVAL) {
539 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
540 "invalid property '%s'"), propname);
541 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
545 if (zpool_prop_readonly(prop)) {
546 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
547 "is readonly"), propname);
548 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
552 if (!flags.create && zpool_prop_setonce(prop)) {
553 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
554 "property '%s' can only be set at "
555 "creation time"), propname);
556 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
560 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
561 &strval, &intval, errbuf) != 0)
565 * Perform additional checking for specific properties.
568 case ZPOOL_PROP_VERSION:
569 if (intval < version ||
570 !SPA_VERSION_IS_SUPPORTED(intval)) {
571 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
572 "property '%s' number %d is invalid."),
574 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
579 case ZPOOL_PROP_ASHIFT:
581 (intval < ASHIFT_MIN || intval > ASHIFT_MAX)) {
582 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
583 "property '%s' number %d is invalid, only "
584 "values between %" PRId32 " and "
585 "%" PRId32 " are allowed."),
586 propname, intval, ASHIFT_MIN, ASHIFT_MAX);
587 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
592 case ZPOOL_PROP_BOOTFS:
593 if (flags.create || flags.import) {
594 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
595 "property '%s' cannot be set at creation "
596 "or import time"), propname);
597 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
601 if (version < SPA_VERSION_BOOTFS) {
602 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
603 "pool must be upgraded to support "
604 "'%s' property"), propname);
605 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
610 * bootfs property value has to be a dataset name and
611 * the dataset has to be in the same pool as it sets to.
613 if (!bootfs_name_valid(poolname, strval)) {
614 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
615 "is an invalid name"), strval);
616 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
620 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
621 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
622 "could not open pool '%s'"), poolname);
623 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
629 case ZPOOL_PROP_ALTROOT:
630 if (!flags.create && !flags.import) {
631 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
632 "property '%s' can only be set during pool "
633 "creation or import"), propname);
634 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
638 if (strval[0] != '/') {
639 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
640 "bad alternate root '%s'"), strval);
641 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
646 case ZPOOL_PROP_CACHEFILE:
647 if (strval[0] == '\0')
650 if (strcmp(strval, "none") == 0)
653 if (strval[0] != '/') {
654 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
655 "property '%s' must be empty, an "
656 "absolute path, or 'none'"), propname);
657 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
661 slash = strrchr(strval, '/');
663 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
664 strcmp(slash, "/..") == 0) {
665 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
666 "'%s' is not a valid file"), strval);
667 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
673 if (strval[0] != '\0' &&
674 (stat64(strval, &statbuf) != 0 ||
675 !S_ISDIR(statbuf.st_mode))) {
676 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
677 "'%s' is not a valid directory"),
679 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
686 case ZPOOL_PROP_COMMENT:
687 for (check = strval; *check != '\0'; check++) {
688 if (!isprint(*check)) {
690 dgettext(TEXT_DOMAIN,
691 "comment may only have printable "
693 (void) zfs_error(hdl, EZFS_BADPROP,
698 if (strlen(strval) > ZPROP_MAX_COMMENT) {
699 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
700 "comment must not exceed %d characters"),
702 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
706 case ZPOOL_PROP_READONLY:
708 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
709 "property '%s' can only be set at "
710 "import time"), propname);
711 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
715 case ZPOOL_PROP_MULTIHOST:
716 if (get_system_hostid() == 0) {
717 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
718 "requires a non-zero system hostid"));
719 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
723 case ZPOOL_PROP_DEDUPDITTO:
724 printf("Note: property '%s' no longer has "
725 "any effect\n", propname);
729 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
730 "property '%s'(%d) not defined"), propname, prop);
737 nvlist_free(retprops);
742 * Set zpool property : propname=propval.
745 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
747 zfs_cmd_t zc = {"\0"};
750 nvlist_t *nvl = NULL;
753 prop_flags_t flags = { 0 };
755 (void) snprintf(errbuf, sizeof (errbuf),
756 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
759 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
760 return (no_memory(zhp->zpool_hdl));
762 if (nvlist_add_string(nvl, propname, propval) != 0) {
764 return (no_memory(zhp->zpool_hdl));
767 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
768 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
769 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
778 * Execute the corresponding ioctl() to set this property.
780 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
782 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
787 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
789 zcmd_free_nvlists(&zc);
793 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
795 (void) zpool_props_refresh(zhp);
801 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
803 libzfs_handle_t *hdl = zhp->zpool_hdl;
805 char buf[ZFS_MAXPROPLEN];
806 nvlist_t *features = NULL;
809 boolean_t firstexpand = (NULL == *plp);
812 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
816 while (*last != NULL)
817 last = &(*last)->pl_next;
820 features = zpool_get_features(zhp);
822 if ((*plp)->pl_all && firstexpand) {
823 for (i = 0; i < SPA_FEATURES; i++) {
824 zprop_list_t *entry = zfs_alloc(hdl,
825 sizeof (zprop_list_t));
826 entry->pl_prop = ZPROP_INVAL;
827 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
828 spa_feature_table[i].fi_uname);
829 entry->pl_width = strlen(entry->pl_user_prop);
830 entry->pl_all = B_TRUE;
833 last = &entry->pl_next;
837 /* add any unsupported features */
838 for (nvp = nvlist_next_nvpair(features, NULL);
839 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
844 if (zfeature_is_supported(nvpair_name(nvp)))
847 propname = zfs_asprintf(hdl, "unsupported@%s",
851 * Before adding the property to the list make sure that no
852 * other pool already added the same property.
856 while (entry != NULL) {
857 if (entry->pl_user_prop != NULL &&
858 strcmp(propname, entry->pl_user_prop) == 0) {
862 entry = entry->pl_next;
869 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
870 entry->pl_prop = ZPROP_INVAL;
871 entry->pl_user_prop = propname;
872 entry->pl_width = strlen(entry->pl_user_prop);
873 entry->pl_all = B_TRUE;
876 last = &entry->pl_next;
879 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
884 if (entry->pl_prop != ZPROP_INVAL &&
885 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
886 NULL, B_FALSE) == 0) {
887 if (strlen(buf) > entry->pl_width)
888 entry->pl_width = strlen(buf);
896 * Get the state for the given feature on the given ZFS pool.
899 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
903 boolean_t found = B_FALSE;
904 nvlist_t *features = zpool_get_features(zhp);
906 const char *feature = strchr(propname, '@') + 1;
908 supported = zpool_prop_feature(propname);
909 ASSERT(supported || zpool_prop_unsupported(propname));
912 * Convert from feature name to feature guid. This conversion is
913 * unnecessary for unsupported@... properties because they already
920 ret = zfeature_lookup_name(feature, &fid);
922 (void) strlcpy(buf, "-", len);
925 feature = spa_feature_table[fid].fi_guid;
928 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
933 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
936 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
938 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
943 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
945 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
948 (void) strlcpy(buf, "-", len);
957 * Validate the given pool name, optionally putting an extended error message in
961 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
967 ret = pool_namecheck(pool, &why, &what);
970 * The rules for reserved pool names were extended at a later point.
971 * But we need to support users with existing pools that may now be
972 * invalid. So we only check for this expanded set of names during a
973 * create (or import), and only in userland.
975 if (ret == 0 && !isopen &&
976 (strncmp(pool, "mirror", 6) == 0 ||
977 strncmp(pool, "raidz", 5) == 0 ||
978 strncmp(pool, "spare", 5) == 0 ||
979 strcmp(pool, "log") == 0)) {
982 dgettext(TEXT_DOMAIN, "name is reserved"));
990 case NAME_ERR_TOOLONG:
992 dgettext(TEXT_DOMAIN, "name is too long"));
995 case NAME_ERR_INVALCHAR:
997 dgettext(TEXT_DOMAIN, "invalid character "
998 "'%c' in pool name"), what);
1001 case NAME_ERR_NOLETTER:
1002 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1003 "name must begin with a letter"));
1006 case NAME_ERR_RESERVED:
1007 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1008 "name is reserved"));
1011 case NAME_ERR_DISKLIKE:
1012 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1013 "pool name is reserved"));
1016 case NAME_ERR_LEADING_SLASH:
1017 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1018 "leading slash in name"));
1021 case NAME_ERR_EMPTY_COMPONENT:
1022 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1023 "empty component in name"));
1026 case NAME_ERR_TRAILING_SLASH:
1027 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1028 "trailing slash in name"));
1031 case NAME_ERR_MULTIPLE_DELIMITERS:
1032 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1033 "multiple '@' and/or '#' delimiters in "
1037 case NAME_ERR_NO_AT:
1038 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1039 "permission set is missing '@'"));
1043 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1044 "(%d) not defined"), why);
1055 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1059 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
1061 zpool_handle_t *zhp;
1065 * Make sure the pool name is valid.
1067 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
1068 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1069 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1074 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1077 zhp->zpool_hdl = hdl;
1078 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1080 if (zpool_refresh_stats(zhp, &missing) != 0) {
1086 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1087 (void) zfs_error_fmt(hdl, EZFS_NOENT,
1088 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1097 * Like the above, but silent on error. Used when iterating over pools (because
1098 * the configuration cache may be out of date).
1101 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1103 zpool_handle_t *zhp;
1106 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1109 zhp->zpool_hdl = hdl;
1110 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1112 if (zpool_refresh_stats(zhp, &missing) != 0) {
1128 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1132 zpool_open(libzfs_handle_t *hdl, const char *pool)
1134 zpool_handle_t *zhp;
1136 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1139 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1140 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1141 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1150 * Close the handle. Simply frees the memory associated with the handle.
1153 zpool_close(zpool_handle_t *zhp)
1155 nvlist_free(zhp->zpool_config);
1156 nvlist_free(zhp->zpool_old_config);
1157 nvlist_free(zhp->zpool_props);
1162 * Return the name of the pool.
1165 zpool_get_name(zpool_handle_t *zhp)
1167 return (zhp->zpool_name);
1172 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1175 zpool_get_state(zpool_handle_t *zhp)
1177 return (zhp->zpool_state);
1181 * Check if vdev list contains a special vdev
1184 zpool_has_special_vdev(nvlist_t *nvroot)
1189 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, &child,
1191 for (uint_t c = 0; c < children; c++) {
1194 if (nvlist_lookup_string(child[c],
1195 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias) == 0 &&
1196 strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0) {
1205 * Create the named pool, using the provided vdev list. It is assumed
1206 * that the consumer has already validated the contents of the nvlist, so we
1207 * don't have to worry about error semantics.
1210 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1211 nvlist_t *props, nvlist_t *fsprops)
1213 zfs_cmd_t zc = {"\0"};
1214 nvlist_t *zc_fsprops = NULL;
1215 nvlist_t *zc_props = NULL;
1216 nvlist_t *hidden_args = NULL;
1217 uint8_t *wkeydata = NULL;
1222 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1223 "cannot create '%s'"), pool);
1225 if (!zpool_name_valid(hdl, B_FALSE, pool))
1226 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1228 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1232 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1234 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1235 SPA_VERSION_1, flags, msg)) == NULL) {
1244 zoned = ((nvlist_lookup_string(fsprops,
1245 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1246 strcmp(zonestr, "on") == 0);
1248 if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
1249 fsprops, zoned, NULL, NULL, B_TRUE, msg)) == NULL) {
1253 if (nvlist_exists(zc_fsprops,
1254 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS)) &&
1255 !zpool_has_special_vdev(nvroot)) {
1256 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1257 "%s property requires a special vdev"),
1258 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS));
1259 (void) zfs_error(hdl, EZFS_BADPROP, msg);
1264 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1267 if (zfs_crypto_create(hdl, NULL, zc_fsprops, props, B_TRUE,
1268 &wkeydata, &wkeylen) != 0) {
1269 zfs_error(hdl, EZFS_CRYPTOFAILED, msg);
1272 if (nvlist_add_nvlist(zc_props,
1273 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1276 if (wkeydata != NULL) {
1277 if (nvlist_alloc(&hidden_args, NV_UNIQUE_NAME, 0) != 0)
1280 if (nvlist_add_uint8_array(hidden_args, "wkeydata",
1281 wkeydata, wkeylen) != 0)
1284 if (nvlist_add_nvlist(zc_props, ZPOOL_HIDDEN_ARGS,
1290 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1293 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1295 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1297 zcmd_free_nvlists(&zc);
1298 nvlist_free(zc_props);
1299 nvlist_free(zc_fsprops);
1300 nvlist_free(hidden_args);
1301 if (wkeydata != NULL)
1307 * This can happen if the user has specified the same
1308 * device multiple times. We can't reliably detect this
1309 * until we try to add it and see we already have a
1310 * label. This can also happen under if the device is
1311 * part of an active md or lvm device.
1313 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1314 "one or more vdevs refer to the same device, or "
1315 "one of\nthe devices is part of an active md or "
1317 return (zfs_error(hdl, EZFS_BADDEV, msg));
1321 * This happens if the record size is smaller or larger
1322 * than the allowed size range, or not a power of 2.
1324 * NOTE: although zfs_valid_proplist is called earlier,
1325 * this case may have slipped through since the
1326 * pool does not exist yet and it is therefore
1327 * impossible to read properties e.g. max blocksize
1330 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1331 "record size invalid"));
1332 return (zfs_error(hdl, EZFS_BADPROP, msg));
1336 * This occurs when one of the devices is below
1337 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1338 * device was the problem device since there's no
1339 * reliable way to determine device size from userland.
1344 zfs_nicebytes(SPA_MINDEVSIZE, buf,
1347 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1348 "one or more devices is less than the "
1349 "minimum size (%s)"), buf);
1351 return (zfs_error(hdl, EZFS_BADDEV, msg));
1354 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1355 "one or more devices is out of space"));
1356 return (zfs_error(hdl, EZFS_BADDEV, msg));
1359 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1360 "cache device must be a disk or disk slice"));
1361 return (zfs_error(hdl, EZFS_BADDEV, msg));
1364 return (zpool_standard_error(hdl, errno, msg));
1369 zcmd_free_nvlists(&zc);
1370 nvlist_free(zc_props);
1371 nvlist_free(zc_fsprops);
1372 nvlist_free(hidden_args);
1373 if (wkeydata != NULL)
1379 * Destroy the given pool. It is up to the caller to ensure that there are no
1380 * datasets left in the pool.
1383 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1385 zfs_cmd_t zc = {"\0"};
1386 zfs_handle_t *zfp = NULL;
1387 libzfs_handle_t *hdl = zhp->zpool_hdl;
1390 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1391 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1394 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1395 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1397 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1398 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1399 "cannot destroy '%s'"), zhp->zpool_name);
1401 if (errno == EROFS) {
1402 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1403 "one or more devices is read only"));
1404 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1406 (void) zpool_standard_error(hdl, errno, msg);
1415 remove_mountpoint(zfp);
1423 * Create a checkpoint in the given pool.
1426 zpool_checkpoint(zpool_handle_t *zhp)
1428 libzfs_handle_t *hdl = zhp->zpool_hdl;
1432 error = lzc_pool_checkpoint(zhp->zpool_name);
1434 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1435 "cannot checkpoint '%s'"), zhp->zpool_name);
1436 (void) zpool_standard_error(hdl, error, msg);
1444 * Discard the checkpoint from the given pool.
1447 zpool_discard_checkpoint(zpool_handle_t *zhp)
1449 libzfs_handle_t *hdl = zhp->zpool_hdl;
1453 error = lzc_pool_checkpoint_discard(zhp->zpool_name);
1455 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1456 "cannot discard checkpoint in '%s'"), zhp->zpool_name);
1457 (void) zpool_standard_error(hdl, error, msg);
1465 * Add the given vdevs to the pool. The caller must have already performed the
1466 * necessary verification to ensure that the vdev specification is well-formed.
1469 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1471 zfs_cmd_t zc = {"\0"};
1473 libzfs_handle_t *hdl = zhp->zpool_hdl;
1475 nvlist_t **spares, **l2cache;
1476 uint_t nspares, nl2cache;
1478 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1479 "cannot add to '%s'"), zhp->zpool_name);
1481 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1482 SPA_VERSION_SPARES &&
1483 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1484 &spares, &nspares) == 0) {
1485 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1486 "upgraded to add hot spares"));
1487 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1490 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1491 SPA_VERSION_L2CACHE &&
1492 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1493 &l2cache, &nl2cache) == 0) {
1494 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1495 "upgraded to add cache devices"));
1496 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1499 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1501 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1503 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1507 * This can happen if the user has specified the same
1508 * device multiple times. We can't reliably detect this
1509 * until we try to add it and see we already have a
1512 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1513 "one or more vdevs refer to the same device"));
1514 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1518 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1519 "invalid config; a pool with removing/removed "
1520 "vdevs does not support adding raidz vdevs"));
1521 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1526 * This occurs when one of the devices is below
1527 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1528 * device was the problem device since there's no
1529 * reliable way to determine device size from userland.
1534 zfs_nicebytes(SPA_MINDEVSIZE, buf,
1537 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1538 "device is less than the minimum "
1541 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1545 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1546 "pool must be upgraded to add these vdevs"));
1547 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1551 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1552 "cache device must be a disk or disk slice"));
1553 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1557 (void) zpool_standard_error(hdl, errno, msg);
1565 zcmd_free_nvlists(&zc);
1571 * Exports the pool from the system. The caller must ensure that there are no
1572 * mounted datasets in the pool.
1575 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1576 const char *log_str)
1578 zfs_cmd_t zc = {"\0"};
1581 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1582 "cannot export '%s'"), zhp->zpool_name);
1584 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1585 zc.zc_cookie = force;
1586 zc.zc_guid = hardforce;
1587 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1589 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1592 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1593 "use '-f' to override the following errors:\n"
1594 "'%s' has an active shared spare which could be"
1595 " used by other pools once '%s' is exported."),
1596 zhp->zpool_name, zhp->zpool_name);
1597 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1600 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1609 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1611 return (zpool_export_common(zhp, force, B_FALSE, log_str));
1615 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1617 return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1621 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1624 nvlist_t *nv = NULL;
1630 if (!hdl->libzfs_printerr || config == NULL)
1633 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1634 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1638 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1640 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1642 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1643 strftime(timestr, 128, "%c", &t) != 0) {
1645 (void) printf(dgettext(TEXT_DOMAIN,
1646 "Would be able to return %s "
1647 "to its state as of %s.\n"),
1650 (void) printf(dgettext(TEXT_DOMAIN,
1651 "Pool %s returned to its state as of %s.\n"),
1655 (void) printf(dgettext(TEXT_DOMAIN,
1656 "%s approximately %lld "),
1657 dryrun ? "Would discard" : "Discarded",
1658 ((longlong_t)loss + 30) / 60);
1659 (void) printf(dgettext(TEXT_DOMAIN,
1660 "minutes of transactions.\n"));
1661 } else if (loss > 0) {
1662 (void) printf(dgettext(TEXT_DOMAIN,
1663 "%s approximately %lld "),
1664 dryrun ? "Would discard" : "Discarded",
1666 (void) printf(dgettext(TEXT_DOMAIN,
1667 "seconds of transactions.\n"));
1673 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1676 nvlist_t *nv = NULL;
1678 uint64_t edata = UINT64_MAX;
1683 if (!hdl->libzfs_printerr)
1687 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1689 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1691 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1692 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1693 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1694 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1697 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1698 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1701 (void) printf(dgettext(TEXT_DOMAIN,
1702 "Recovery is possible, but will result in some data loss.\n"));
1704 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1705 strftime(timestr, 128, "%c", &t) != 0) {
1706 (void) printf(dgettext(TEXT_DOMAIN,
1707 "\tReturning the pool to its state as of %s\n"
1708 "\tshould correct the problem. "),
1711 (void) printf(dgettext(TEXT_DOMAIN,
1712 "\tReverting the pool to an earlier state "
1713 "should correct the problem.\n\t"));
1717 (void) printf(dgettext(TEXT_DOMAIN,
1718 "Approximately %lld minutes of data\n"
1719 "\tmust be discarded, irreversibly. "),
1720 ((longlong_t)loss + 30) / 60);
1721 } else if (loss > 0) {
1722 (void) printf(dgettext(TEXT_DOMAIN,
1723 "Approximately %lld seconds of data\n"
1724 "\tmust be discarded, irreversibly. "),
1727 if (edata != 0 && edata != UINT64_MAX) {
1729 (void) printf(dgettext(TEXT_DOMAIN,
1730 "After rewind, at least\n"
1731 "\tone persistent user-data error will remain. "));
1733 (void) printf(dgettext(TEXT_DOMAIN,
1734 "After rewind, several\n"
1735 "\tpersistent user-data errors will remain. "));
1738 (void) printf(dgettext(TEXT_DOMAIN,
1739 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1740 reason >= 0 ? "clear" : "import", name);
1742 (void) printf(dgettext(TEXT_DOMAIN,
1743 "A scrub of the pool\n"
1744 "\tis strongly recommended after recovery.\n"));
1748 (void) printf(dgettext(TEXT_DOMAIN,
1749 "Destroy and re-create the pool from\n\ta backup source.\n"));
1753 * zpool_import() is a contracted interface. Should be kept the same
1756 * Applications should use zpool_import_props() to import a pool with
1757 * new properties value to be set.
1760 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1763 nvlist_t *props = NULL;
1766 if (altroot != NULL) {
1767 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1768 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1769 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1773 if (nvlist_add_string(props,
1774 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1775 nvlist_add_string(props,
1776 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1778 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1779 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1784 ret = zpool_import_props(hdl, config, newname, props,
1791 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1797 uint64_t is_log = 0;
1799 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1803 (void) printf("\t%*s%s%s\n", indent, "", name,
1804 is_log ? " [log]" : "");
1806 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1807 &child, &children) != 0)
1810 for (c = 0; c < children; c++) {
1811 vname = zpool_vdev_name(hdl, NULL, child[c], VDEV_NAME_TYPE_ID);
1812 print_vdev_tree(hdl, vname, child[c], indent + 2);
1818 zpool_print_unsup_feat(nvlist_t *config)
1820 nvlist_t *nvinfo, *unsup_feat;
1823 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1825 verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1828 for (nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1829 nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1832 verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1833 verify(nvpair_value_string(nvp, &desc) == 0);
1835 if (strlen(desc) > 0)
1836 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1838 (void) printf("\t%s\n", nvpair_name(nvp));
1843 * Import the given pool using the known configuration and a list of
1844 * properties to be set. The configuration should have come from
1845 * zpool_find_import(). The 'newname' parameters control whether the pool
1846 * is imported with a different name.
1849 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1850 nvlist_t *props, int flags)
1852 zfs_cmd_t zc = {"\0"};
1853 zpool_load_policy_t policy;
1854 nvlist_t *nv = NULL;
1855 nvlist_t *nvinfo = NULL;
1856 nvlist_t *missing = NULL;
1863 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1866 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1867 "cannot import pool '%s'"), origname);
1869 if (newname != NULL) {
1870 if (!zpool_name_valid(hdl, B_FALSE, newname))
1871 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1872 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1874 thename = (char *)newname;
1879 if (props != NULL) {
1881 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1883 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1886 if ((props = zpool_valid_proplist(hdl, origname,
1887 props, version, flags, errbuf)) == NULL)
1889 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1896 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1898 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1901 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1902 zcmd_free_nvlists(&zc);
1905 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1906 zcmd_free_nvlists(&zc);
1910 zc.zc_cookie = flags;
1911 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1913 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1914 zcmd_free_nvlists(&zc);
1921 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1923 zcmd_free_nvlists(&zc);
1925 zpool_get_load_policy(config, &policy);
1932 * Dry-run failed, but we print out what success
1933 * looks like if we found a best txg
1935 if (policy.zlp_rewind & ZPOOL_TRY_REWIND) {
1936 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1942 if (newname == NULL)
1943 (void) snprintf(desc, sizeof (desc),
1944 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1947 (void) snprintf(desc, sizeof (desc),
1948 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1953 if (nv != NULL && nvlist_lookup_nvlist(nv,
1954 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1955 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1956 (void) printf(dgettext(TEXT_DOMAIN, "This "
1957 "pool uses the following feature(s) not "
1958 "supported by this system:\n"));
1959 zpool_print_unsup_feat(nv);
1960 if (nvlist_exists(nvinfo,
1961 ZPOOL_CONFIG_CAN_RDONLY)) {
1962 (void) printf(dgettext(TEXT_DOMAIN,
1963 "All unsupported features are only "
1964 "required for writing to the pool."
1965 "\nThe pool can be imported using "
1966 "'-o readonly=on'.\n"));
1970 * Unsupported version.
1972 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1976 if (nv != NULL && nvlist_lookup_nvlist(nv,
1977 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0) {
1978 char *hostname = "<unknown>";
1979 uint64_t hostid = 0;
1980 mmp_state_t mmp_state;
1982 mmp_state = fnvlist_lookup_uint64(nvinfo,
1983 ZPOOL_CONFIG_MMP_STATE);
1985 if (nvlist_exists(nvinfo,
1986 ZPOOL_CONFIG_MMP_HOSTNAME))
1987 hostname = fnvlist_lookup_string(nvinfo,
1988 ZPOOL_CONFIG_MMP_HOSTNAME);
1990 if (nvlist_exists(nvinfo,
1991 ZPOOL_CONFIG_MMP_HOSTID))
1992 hostid = fnvlist_lookup_uint64(nvinfo,
1993 ZPOOL_CONFIG_MMP_HOSTID);
1995 if (mmp_state == MMP_STATE_ACTIVE) {
1996 (void) snprintf(aux, sizeof (aux),
1997 dgettext(TEXT_DOMAIN, "pool is imp"
1998 "orted on host '%s' (hostid=%lx).\n"
1999 "Export the pool on the other "
2000 "system, then run 'zpool import'."),
2001 hostname, (unsigned long) hostid);
2002 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
2003 (void) snprintf(aux, sizeof (aux),
2004 dgettext(TEXT_DOMAIN, "pool has "
2005 "the multihost property on and "
2006 "the\nsystem's hostid is not set. "
2007 "Set a unique system hostid with "
2008 "the zgenhostid(8) command.\n"));
2011 (void) zfs_error_aux(hdl, aux);
2013 (void) zfs_error(hdl, EZFS_ACTIVE_POOL, desc);
2017 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
2021 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2022 "one or more devices is read only"));
2023 (void) zfs_error(hdl, EZFS_BADDEV, desc);
2027 if (nv && nvlist_lookup_nvlist(nv,
2028 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
2029 nvlist_lookup_nvlist(nvinfo,
2030 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
2031 (void) printf(dgettext(TEXT_DOMAIN,
2032 "The devices below are missing or "
2033 "corrupted, use '-m' to import the pool "
2035 print_vdev_tree(hdl, NULL, missing, 2);
2036 (void) printf("\n");
2038 (void) zpool_standard_error(hdl, error, desc);
2042 (void) zpool_standard_error(hdl, error, desc);
2046 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2047 "one or more devices are already in use\n"));
2048 (void) zfs_error(hdl, EZFS_BADDEV, desc);
2051 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2052 "new name of at least one dataset is longer than "
2053 "the maximum allowable length"));
2054 (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
2057 (void) zpool_standard_error(hdl, error, desc);
2058 zpool_explain_recover(hdl,
2059 newname ? origname : thename, -error, nv);
2066 zpool_handle_t *zhp;
2069 * This should never fail, but play it safe anyway.
2071 if (zpool_open_silent(hdl, thename, &zhp) != 0)
2073 else if (zhp != NULL)
2075 if (policy.zlp_rewind &
2076 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2077 zpool_rewind_exclaim(hdl, newname ? origname : thename,
2078 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0), nv);
2088 * Translate vdev names to guids. If a vdev_path is determined to be
2089 * unsuitable then a vd_errlist is allocated and the vdev path and errno
2093 zpool_translate_vdev_guids(zpool_handle_t *zhp, nvlist_t *vds,
2094 nvlist_t *vdev_guids, nvlist_t *guids_to_paths, nvlist_t **vd_errlist)
2096 nvlist_t *errlist = NULL;
2099 for (nvpair_t *elem = nvlist_next_nvpair(vds, NULL); elem != NULL;
2100 elem = nvlist_next_nvpair(vds, elem)) {
2101 boolean_t spare, cache;
2103 char *vd_path = nvpair_name(elem);
2104 nvlist_t *tgt = zpool_find_vdev(zhp, vd_path, &spare, &cache,
2107 if ((tgt == NULL) || cache || spare) {
2108 if (errlist == NULL) {
2109 errlist = fnvlist_alloc();
2113 uint64_t err = (tgt == NULL) ? EZFS_NODEVICE :
2114 (spare ? EZFS_ISSPARE : EZFS_ISL2CACHE);
2115 fnvlist_add_int64(errlist, vd_path, err);
2119 uint64_t guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
2120 fnvlist_add_uint64(vdev_guids, vd_path, guid);
2122 char msg[MAXNAMELEN];
2123 (void) snprintf(msg, sizeof (msg), "%llu", (u_longlong_t)guid);
2124 fnvlist_add_string(guids_to_paths, msg, vd_path);
2128 verify(errlist != NULL);
2129 if (vd_errlist != NULL)
2130 *vd_errlist = errlist;
2132 fnvlist_free(errlist);
2139 xlate_init_err(int err)
2143 return (EZFS_NODEVICE);
2146 return (EZFS_BADDEV);
2148 return (EZFS_INITIALIZING);
2150 return (EZFS_NO_INITIALIZE);
2156 * Begin, suspend, or cancel the initialization (initializing of all free
2157 * blocks) for the given vdevs in the given pool.
2160 zpool_initialize_impl(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2161 nvlist_t *vds, boolean_t wait)
2165 nvlist_t *vdev_guids = fnvlist_alloc();
2166 nvlist_t *guids_to_paths = fnvlist_alloc();
2167 nvlist_t *vd_errlist = NULL;
2171 err = zpool_translate_vdev_guids(zhp, vds, vdev_guids,
2172 guids_to_paths, &vd_errlist);
2175 verify(vd_errlist != NULL);
2179 err = lzc_initialize(zhp->zpool_name, cmd_type,
2180 vdev_guids, &errlist);
2183 if (errlist != NULL) {
2184 vd_errlist = fnvlist_lookup_nvlist(errlist,
2185 ZPOOL_INITIALIZE_VDEVS);
2188 (void) zpool_standard_error(zhp->zpool_hdl, err,
2189 dgettext(TEXT_DOMAIN, "operation failed"));
2194 for (elem = nvlist_next_nvpair(vdev_guids, NULL); elem != NULL;
2195 elem = nvlist_next_nvpair(vdev_guids, elem)) {
2197 uint64_t guid = fnvpair_value_uint64(elem);
2199 err = lzc_wait_tag(zhp->zpool_name,
2200 ZPOOL_WAIT_INITIALIZE, guid, NULL);
2202 (void) zpool_standard_error_fmt(zhp->zpool_hdl,
2203 err, dgettext(TEXT_DOMAIN, "error "
2204 "waiting for '%s' to initialize"),
2214 for (elem = nvlist_next_nvpair(vd_errlist, NULL); elem != NULL;
2215 elem = nvlist_next_nvpair(vd_errlist, elem)) {
2216 int64_t vd_error = xlate_init_err(fnvpair_value_int64(elem));
2219 if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem),
2221 path = nvpair_name(elem);
2223 (void) zfs_error_fmt(zhp->zpool_hdl, vd_error,
2224 "cannot initialize '%s'", path);
2228 fnvlist_free(vdev_guids);
2229 fnvlist_free(guids_to_paths);
2231 if (vd_errlist != NULL)
2232 fnvlist_free(vd_errlist);
2234 return (err == 0 ? 0 : -1);
2238 zpool_initialize(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2241 return (zpool_initialize_impl(zhp, cmd_type, vds, B_FALSE));
2245 zpool_initialize_wait(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2248 return (zpool_initialize_impl(zhp, cmd_type, vds, B_TRUE));
2252 xlate_trim_err(int err)
2256 return (EZFS_NODEVICE);
2259 return (EZFS_BADDEV);
2261 return (EZFS_TRIMMING);
2263 return (EZFS_NO_TRIM);
2265 return (EZFS_TRIM_NOTSUP);
2271 * Begin, suspend, or cancel the TRIM (discarding of all free blocks) for
2272 * the given vdevs in the given pool.
2275 zpool_trim(zpool_handle_t *zhp, pool_trim_func_t cmd_type, nvlist_t *vds,
2276 trimflags_t *trim_flags)
2281 nvlist_t *vdev_guids = fnvlist_alloc();
2282 nvlist_t *guids_to_paths = fnvlist_alloc();
2283 nvlist_t *vd_errlist = NULL;
2287 err = zpool_translate_vdev_guids(zhp, vds, vdev_guids,
2288 guids_to_paths, &vd_errlist);
2290 err = lzc_trim(zhp->zpool_name, cmd_type, trim_flags->rate,
2291 trim_flags->secure, vdev_guids, &errlist);
2293 fnvlist_free(vdev_guids);
2294 fnvlist_free(guids_to_paths);
2298 if (errlist != NULL) {
2299 vd_errlist = fnvlist_lookup_nvlist(errlist,
2303 (void) snprintf(msg, sizeof (msg),
2304 dgettext(TEXT_DOMAIN, "operation failed"));
2306 verify(vd_errlist != NULL);
2309 for (elem = nvlist_next_nvpair(vd_errlist, NULL);
2310 elem != NULL; elem = nvlist_next_nvpair(vd_errlist, elem)) {
2311 int64_t vd_error = xlate_trim_err(fnvpair_value_int64(elem));
2315 * If only the pool was specified, and it was not a secure
2316 * trim then suppress warnings for individual vdevs which
2317 * do not support trimming.
2319 if (vd_error == EZFS_TRIM_NOTSUP &&
2320 trim_flags->fullpool &&
2321 !trim_flags->secure) {
2325 if (nvlist_lookup_string(guids_to_paths, nvpair_name(elem),
2327 path = nvpair_name(elem);
2329 (void) zfs_error_fmt(zhp->zpool_hdl, vd_error,
2330 "cannot trim '%s'", path);
2333 fnvlist_free(vdev_guids);
2334 fnvlist_free(guids_to_paths);
2336 if (vd_errlist != NULL) {
2337 fnvlist_free(vd_errlist);
2341 return (zpool_standard_error(zhp->zpool_hdl, err, msg));
2348 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd)
2350 zfs_cmd_t zc = {"\0"};
2353 libzfs_handle_t *hdl = zhp->zpool_hdl;
2355 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2356 zc.zc_cookie = func;
2359 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0)
2364 /* ECANCELED on a scrub means we resumed a paused scrub */
2365 if (err == ECANCELED && func == POOL_SCAN_SCRUB &&
2366 cmd == POOL_SCRUB_NORMAL)
2369 if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL)
2372 if (func == POOL_SCAN_SCRUB) {
2373 if (cmd == POOL_SCRUB_PAUSE) {
2374 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2375 "cannot pause scrubbing %s"), zc.zc_name);
2377 assert(cmd == POOL_SCRUB_NORMAL);
2378 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2379 "cannot scrub %s"), zc.zc_name);
2381 } else if (func == POOL_SCAN_RESILVER) {
2382 assert(cmd == POOL_SCRUB_NORMAL);
2383 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2384 "cannot restart resilver on %s"), zc.zc_name);
2385 } else if (func == POOL_SCAN_NONE) {
2386 (void) snprintf(msg, sizeof (msg),
2387 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
2390 assert(!"unexpected result");
2395 pool_scan_stat_t *ps = NULL;
2398 verify(nvlist_lookup_nvlist(zhp->zpool_config,
2399 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2400 (void) nvlist_lookup_uint64_array(nvroot,
2401 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
2402 if (ps && ps->pss_func == POOL_SCAN_SCRUB) {
2403 if (cmd == POOL_SCRUB_PAUSE)
2404 return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg));
2406 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
2408 return (zfs_error(hdl, EZFS_RESILVERING, msg));
2410 } else if (err == ENOENT) {
2411 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
2412 } else if (err == ENOTSUP && func == POOL_SCAN_RESILVER) {
2413 return (zfs_error(hdl, EZFS_NO_RESILVER_DEFER, msg));
2415 return (zpool_standard_error(hdl, err, msg));
2420 * Find a vdev that matches the search criteria specified. We use the
2421 * the nvpair name to determine how we should look for the device.
2422 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2423 * spare; but FALSE if its an INUSE spare.
2426 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
2427 boolean_t *l2cache, boolean_t *log)
2434 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
2436 /* Nothing to look for */
2437 if (search == NULL || pair == NULL)
2440 /* Obtain the key we will use to search */
2441 srchkey = nvpair_name(pair);
2443 switch (nvpair_type(pair)) {
2444 case DATA_TYPE_UINT64:
2445 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
2446 uint64_t srchval, theguid;
2448 verify(nvpair_value_uint64(pair, &srchval) == 0);
2449 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2451 if (theguid == srchval)
2456 case DATA_TYPE_STRING: {
2457 char *srchval, *val;
2459 verify(nvpair_value_string(pair, &srchval) == 0);
2460 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2464 * Search for the requested value. Special cases:
2466 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2467 * "-part1", or "p1". The suffix is hidden from the user,
2468 * but included in the string, so this matches around it.
2469 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2470 * is used to check all possible expanded paths.
2471 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2473 * Otherwise, all other searches are simple string compares.
2475 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) {
2476 uint64_t wholedisk = 0;
2478 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2480 if (zfs_strcmp_pathname(srchval, val, wholedisk) == 0)
2483 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2484 char *type, *idx, *end, *p;
2485 uint64_t id, vdev_id;
2488 * Determine our vdev type, keeping in mind
2489 * that the srchval is composed of a type and
2490 * vdev id pair (i.e. mirror-4).
2492 if ((type = strdup(srchval)) == NULL)
2495 if ((p = strrchr(type, '-')) == NULL) {
2503 * If the types don't match then keep looking.
2505 if (strncmp(val, type, strlen(val)) != 0) {
2510 verify(zpool_vdev_is_interior(type));
2511 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2515 vdev_id = strtoull(idx, &end, 10);
2522 * Now verify that we have the correct vdev id.
2531 if (strcmp(srchval, val) == 0)
2540 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2541 &child, &children) != 0)
2544 for (c = 0; c < children; c++) {
2545 if ((ret = vdev_to_nvlist_iter(child[c], search,
2546 avail_spare, l2cache, NULL)) != NULL) {
2548 * The 'is_log' value is only set for the toplevel
2549 * vdev, not the leaf vdevs. So we always lookup the
2550 * log device from the root of the vdev tree (where
2551 * 'log' is non-NULL).
2554 nvlist_lookup_uint64(child[c],
2555 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2563 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2564 &child, &children) == 0) {
2565 for (c = 0; c < children; c++) {
2566 if ((ret = vdev_to_nvlist_iter(child[c], search,
2567 avail_spare, l2cache, NULL)) != NULL) {
2568 *avail_spare = B_TRUE;
2574 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2575 &child, &children) == 0) {
2576 for (c = 0; c < children; c++) {
2577 if ((ret = vdev_to_nvlist_iter(child[c], search,
2578 avail_spare, l2cache, NULL)) != NULL) {
2589 * Given a physical path or guid, find the associated vdev.
2592 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2593 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2595 nvlist_t *search, *nvroot, *ret;
2599 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2601 guid = strtoull(ppath, &end, 0);
2602 if (guid != 0 && *end == '\0') {
2603 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2605 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH,
2609 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2612 *avail_spare = B_FALSE;
2616 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2617 nvlist_free(search);
2623 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2626 zpool_vdev_is_interior(const char *name)
2628 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2629 strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 ||
2631 VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 ||
2632 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2638 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2639 boolean_t *l2cache, boolean_t *log)
2642 nvlist_t *nvroot, *search, *ret;
2645 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2647 guid = strtoull(path, &end, 0);
2648 if (guid != 0 && *end == '\0') {
2649 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2650 } else if (zpool_vdev_is_interior(path)) {
2651 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2653 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2656 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2659 *avail_spare = B_FALSE;
2663 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2664 nvlist_free(search);
2670 vdev_is_online(nvlist_t *nv)
2674 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2675 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2676 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2683 * Helper function for zpool_get_physpaths().
2686 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2687 size_t *bytes_written)
2689 size_t bytes_left, pos, rsz;
2693 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2695 return (EZFS_NODEVICE);
2697 pos = *bytes_written;
2698 bytes_left = physpath_size - pos;
2699 format = (pos == 0) ? "%s" : " %s";
2701 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2702 *bytes_written += rsz;
2704 if (rsz >= bytes_left) {
2705 /* if physpath was not copied properly, clear it */
2706 if (bytes_left != 0) {
2709 return (EZFS_NOSPC);
2715 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2716 size_t *rsz, boolean_t is_spare)
2721 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2722 return (EZFS_INVALCONFIG);
2724 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2726 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2727 * For a spare vdev, we only want to boot from the active
2732 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2735 return (EZFS_INVALCONFIG);
2738 if (vdev_is_online(nv)) {
2739 if ((ret = vdev_get_one_physpath(nv, physpath,
2740 phypath_size, rsz)) != 0)
2743 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2744 strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
2745 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2746 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2751 if (nvlist_lookup_nvlist_array(nv,
2752 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2753 return (EZFS_INVALCONFIG);
2755 for (i = 0; i < count; i++) {
2756 ret = vdev_get_physpaths(child[i], physpath,
2757 phypath_size, rsz, is_spare);
2758 if (ret == EZFS_NOSPC)
2763 return (EZFS_POOL_INVALARG);
2767 * Get phys_path for a root pool config.
2768 * Return 0 on success; non-zero on failure.
2771 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2774 nvlist_t *vdev_root;
2781 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2783 return (EZFS_INVALCONFIG);
2785 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2786 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2787 &child, &count) != 0)
2788 return (EZFS_INVALCONFIG);
2791 * root pool can only have a single top-level vdev.
2793 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2794 return (EZFS_POOL_INVALARG);
2796 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2799 /* No online devices */
2801 return (EZFS_NODEVICE);
2807 * Get phys_path for a root pool
2808 * Return 0 on success; non-zero on failure.
2811 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2813 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2818 * If the device has being dynamically expanded then we need to relabel
2819 * the disk to use the new unallocated space.
2822 zpool_relabel_disk(libzfs_handle_t *hdl, const char *path, const char *msg)
2826 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
2827 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2828 "relabel '%s': unable to open device: %d"), path, errno);
2829 return (zfs_error(hdl, EZFS_OPENFAILED, msg));
2833 * It's possible that we might encounter an error if the device
2834 * does not have any unallocated space left. If so, we simply
2835 * ignore that error and continue on.
2837 * Also, we don't call efi_rescan() - that would just return EBUSY.
2838 * The module will do it for us in vdev_disk_open().
2840 error = efi_use_whole_disk(fd);
2842 /* Flush the buffers to disk and invalidate the page cache. */
2844 (void) ioctl(fd, BLKFLSBUF);
2847 if (error && error != VT_ENOSPC) {
2848 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2849 "relabel '%s': unable to read disk capacity"), path);
2850 return (zfs_error(hdl, EZFS_NOCAP, msg));
2857 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
2859 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
2860 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
2864 zpool_vdev_path_to_guid_impl(zpool_handle_t *zhp, const char *path,
2865 boolean_t *is_spare, boolean_t *is_l2cache, boolean_t *is_log)
2868 boolean_t spare = B_FALSE, l2cache = B_FALSE, log = B_FALSE;
2871 if ((tgt = zpool_find_vdev(zhp, path, &spare, &l2cache,
2875 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &guid) == 0);
2876 if (is_spare != NULL)
2878 if (is_l2cache != NULL)
2879 *is_l2cache = l2cache;
2886 /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
2888 zpool_vdev_path_to_guid(zpool_handle_t *zhp, const char *path)
2890 return (zpool_vdev_path_to_guid_impl(zhp, path, NULL, NULL, NULL));
2894 * Bring the specified vdev online. The 'flags' parameter is a set of the
2895 * ZFS_ONLINE_* flags.
2898 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2899 vdev_state_t *newstate)
2901 zfs_cmd_t zc = {"\0"};
2905 boolean_t avail_spare, l2cache, islog;
2906 libzfs_handle_t *hdl = zhp->zpool_hdl;
2909 if (flags & ZFS_ONLINE_EXPAND) {
2910 (void) snprintf(msg, sizeof (msg),
2911 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2913 (void) snprintf(msg, sizeof (msg),
2914 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2917 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2918 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2920 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2922 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2925 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2927 if ((flags & ZFS_ONLINE_EXPAND ||
2928 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) &&
2929 nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) {
2930 uint64_t wholedisk = 0;
2932 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2936 * XXX - L2ARC 1.0 devices can't support expansion.
2939 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2940 "cannot expand cache devices"));
2941 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2945 const char *fullpath = path;
2946 char buf[MAXPATHLEN];
2948 if (path[0] != '/') {
2949 error = zfs_resolve_shortname(path, buf,
2952 return (zfs_error(hdl, EZFS_NODEVICE,
2958 error = zpool_relabel_disk(hdl, fullpath, msg);
2964 zc.zc_cookie = VDEV_STATE_ONLINE;
2967 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2968 if (errno == EINVAL) {
2969 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2970 "from this pool into a new one. Use '%s' "
2971 "instead"), "zpool detach");
2972 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2974 return (zpool_standard_error(hdl, errno, msg));
2977 *newstate = zc.zc_cookie;
2982 * Take the specified vdev offline
2985 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2987 zfs_cmd_t zc = {"\0"};
2990 boolean_t avail_spare, l2cache;
2991 libzfs_handle_t *hdl = zhp->zpool_hdl;
2993 (void) snprintf(msg, sizeof (msg),
2994 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2996 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2997 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2999 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3001 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3004 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3006 zc.zc_cookie = VDEV_STATE_OFFLINE;
3007 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
3009 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3016 * There are no other replicas of this device.
3018 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
3022 * The log device has unplayed logs
3024 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
3027 return (zpool_standard_error(hdl, errno, msg));
3032 * Mark the given vdev faulted.
3035 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
3037 zfs_cmd_t zc = {"\0"};
3039 libzfs_handle_t *hdl = zhp->zpool_hdl;
3041 (void) snprintf(msg, sizeof (msg),
3042 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
3044 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3046 zc.zc_cookie = VDEV_STATE_FAULTED;
3049 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3056 * There are no other replicas of this device.
3058 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
3061 return (zpool_standard_error(hdl, errno, msg));
3067 * Mark the given vdev degraded.
3070 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
3072 zfs_cmd_t zc = {"\0"};
3074 libzfs_handle_t *hdl = zhp->zpool_hdl;
3076 (void) snprintf(msg, sizeof (msg),
3077 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid);
3079 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3081 zc.zc_cookie = VDEV_STATE_DEGRADED;
3084 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
3087 return (zpool_standard_error(hdl, errno, msg));
3091 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
3095 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
3101 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
3103 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
3106 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
3107 children == 2 && child[which] == tgt)
3110 for (c = 0; c < children; c++)
3111 if (is_replacing_spare(child[c], tgt, which))
3119 * Attach new_disk (fully described by nvroot) to old_disk.
3120 * If 'replacing' is specified, the new disk will replace the old one.
3123 zpool_vdev_attach(zpool_handle_t *zhp,
3124 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
3126 zfs_cmd_t zc = {"\0"};
3130 boolean_t avail_spare, l2cache, islog;
3135 nvlist_t *config_root;
3136 libzfs_handle_t *hdl = zhp->zpool_hdl;
3137 boolean_t rootpool = zpool_is_bootable(zhp);
3140 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3141 "cannot replace %s with %s"), old_disk, new_disk);
3143 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3144 "cannot attach %s to %s"), new_disk, old_disk);
3146 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3147 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
3149 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3152 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3155 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
3157 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3158 zc.zc_cookie = replacing;
3160 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3161 &child, &children) != 0 || children != 1) {
3162 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3163 "new device must be a single disk"));
3164 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
3167 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
3168 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
3170 if ((newname = zpool_vdev_name(NULL, NULL, child[0], 0)) == NULL)
3174 * If the target is a hot spare that has been swapped in, we can only
3175 * replace it with another hot spare.
3178 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
3179 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
3180 NULL) == NULL || !avail_spare) &&
3181 is_replacing_spare(config_root, tgt, 1)) {
3182 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3183 "can only be replaced by another hot spare"));
3185 return (zfs_error(hdl, EZFS_BADTARGET, msg));
3190 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
3193 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
3195 zcmd_free_nvlists(&zc);
3200 * XXX need a better way to prevent user from
3201 * booting up a half-baked vdev.
3203 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
3204 "sure to wait until resilver is done "
3205 "before rebooting.\n"));
3213 * Can't attach to or replace this type of vdev.
3216 uint64_t version = zpool_get_prop_int(zhp,
3217 ZPOOL_PROP_VERSION, NULL);
3220 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3221 "cannot replace a log with a spare"));
3222 else if (version >= SPA_VERSION_MULTI_REPLACE)
3223 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3224 "already in replacing/spare config; wait "
3225 "for completion or use 'zpool detach'"));
3227 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3228 "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)
3482 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
3483 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3484 "Source pool must be composed only of mirrors\n"));
3485 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3489 verify(nvlist_lookup_nvlist_array(child[c],
3490 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3492 /* find or add an entry for this top-level vdev */
3493 if (newchildren > 0 &&
3494 (entry = find_vdev_entry(zhp, mchild, mchildren,
3495 newchild, newchildren)) >= 0) {
3496 /* We found a disk that the user specified. */
3497 vdev = mchild[entry];
3500 /* User didn't specify a disk for this vdev. */
3501 vdev = mchild[mchildren - 1];
3504 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3508 /* did we find every disk the user specified? */
3509 if (found != newchildren) {
3510 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3511 "include at most one disk from each mirror"));
3512 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3516 /* Prepare the nvlist for populating. */
3517 if (*newroot == NULL) {
3518 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3521 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3522 VDEV_TYPE_ROOT) != 0)
3525 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3528 /* Add all the children we found */
3529 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3530 lastlog == 0 ? vcount : lastlog) != 0)
3534 * If we're just doing a dry run, exit now with success.
3537 memory_err = B_FALSE;
3542 /* now build up the config list & call the ioctl */
3543 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3546 if (nvlist_add_nvlist(newconfig,
3547 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3548 nvlist_add_string(newconfig,
3549 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3550 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3554 * The new pool is automatically part of the namespace unless we
3555 * explicitly export it.
3558 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3559 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3560 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3561 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3563 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3566 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3567 retval = zpool_standard_error(hdl, errno, msg);
3572 memory_err = B_FALSE;
3575 if (varray != NULL) {
3578 for (v = 0; v < vcount; v++)
3579 nvlist_free(varray[v]);
3582 zcmd_free_nvlists(&zc);
3583 nvlist_free(zc_props);
3584 nvlist_free(newconfig);
3586 nvlist_free(*newroot);
3594 return (no_memory(hdl));
3600 * Remove the given device.
3603 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3605 zfs_cmd_t zc = {"\0"};
3608 boolean_t avail_spare, l2cache, islog;
3609 libzfs_handle_t *hdl = zhp->zpool_hdl;
3612 (void) snprintf(msg, sizeof (msg),
3613 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3615 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3616 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3618 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3620 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3621 if (islog && version < SPA_VERSION_HOLES) {
3622 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3623 "pool must be upgraded to support log removal"));
3624 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3627 if (!islog && !avail_spare && !l2cache && zpool_is_bootable(zhp)) {
3628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3629 "root pool can not have removed devices, "
3630 "because GRUB does not understand them"));
3631 return (zfs_error(hdl, EINVAL, msg));
3634 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3636 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3642 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3643 "invalid config; all top-level vdevs must "
3644 "have the same sector size and not be raidz."));
3645 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3650 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3651 "Mount encrypted datasets to replay logs."));
3653 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3654 "Pool busy; removal may already be in progress"));
3656 (void) zfs_error(hdl, EZFS_BUSY, msg);
3661 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3662 "Mount encrypted datasets to replay logs."));
3663 (void) zfs_error(hdl, EZFS_BUSY, msg);
3665 (void) zpool_standard_error(hdl, errno, msg);
3670 (void) zpool_standard_error(hdl, errno, msg);
3676 zpool_vdev_remove_cancel(zpool_handle_t *zhp)
3680 libzfs_handle_t *hdl = zhp->zpool_hdl;
3682 (void) snprintf(msg, sizeof (msg),
3683 dgettext(TEXT_DOMAIN, "cannot cancel removal"));
3685 bzero(&zc, sizeof (zc));
3686 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3689 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3692 return (zpool_standard_error(hdl, errno, msg));
3696 zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path,
3701 boolean_t avail_spare, l2cache, islog;
3702 libzfs_handle_t *hdl = zhp->zpool_hdl;
3704 (void) snprintf(msg, sizeof (msg),
3705 dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"),
3708 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3710 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3712 if (avail_spare || l2cache || islog) {
3717 if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) {
3718 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3719 "indirect size not available"));
3720 return (zfs_error(hdl, EINVAL, msg));
3726 * Clear the errors for the pool, or the particular device if specified.
3729 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3731 zfs_cmd_t zc = {"\0"};
3734 zpool_load_policy_t policy;
3735 boolean_t avail_spare, l2cache;
3736 libzfs_handle_t *hdl = zhp->zpool_hdl;
3737 nvlist_t *nvi = NULL;
3741 (void) snprintf(msg, sizeof (msg),
3742 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3745 (void) snprintf(msg, sizeof (msg),
3746 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3749 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3751 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3752 &l2cache, NULL)) == NULL)
3753 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3756 * Don't allow error clearing for hot spares. Do allow
3757 * error clearing for l2cache devices.
3760 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3762 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3766 zpool_get_load_policy(rewindnvl, &policy);
3767 zc.zc_cookie = policy.zlp_rewind;
3769 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3772 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3775 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3777 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3778 zcmd_free_nvlists(&zc);
3783 if (!error || ((policy.zlp_rewind & ZPOOL_TRY_REWIND) &&
3784 errno != EPERM && errno != EACCES)) {
3785 if (policy.zlp_rewind &
3786 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3787 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3788 zpool_rewind_exclaim(hdl, zc.zc_name,
3789 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0),
3793 zcmd_free_nvlists(&zc);
3797 zcmd_free_nvlists(&zc);
3798 return (zpool_standard_error(hdl, errno, msg));
3802 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3805 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3807 zfs_cmd_t zc = {"\0"};
3809 libzfs_handle_t *hdl = zhp->zpool_hdl;
3811 (void) snprintf(msg, sizeof (msg),
3812 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3813 (u_longlong_t)guid);
3815 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3817 zc.zc_cookie = ZPOOL_NO_REWIND;
3819 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3822 return (zpool_standard_error(hdl, errno, msg));
3826 * Change the GUID for a pool.
3829 zpool_reguid(zpool_handle_t *zhp)
3832 libzfs_handle_t *hdl = zhp->zpool_hdl;
3833 zfs_cmd_t zc = {"\0"};
3835 (void) snprintf(msg, sizeof (msg),
3836 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3838 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3839 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3842 return (zpool_standard_error(hdl, errno, msg));
3849 zpool_reopen_one(zpool_handle_t *zhp, void *data)
3851 libzfs_handle_t *hdl = zpool_get_handle(zhp);
3852 const char *pool_name = zpool_get_name(zhp);
3853 boolean_t *scrub_restart = data;
3856 error = lzc_reopen(pool_name, *scrub_restart);
3858 return (zpool_standard_error_fmt(hdl, error,
3859 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"), pool_name));
3865 /* call into libzfs_core to execute the sync IOCTL per pool */
3867 zpool_sync_one(zpool_handle_t *zhp, void *data)
3870 libzfs_handle_t *hdl = zpool_get_handle(zhp);
3871 const char *pool_name = zpool_get_name(zhp);
3872 boolean_t *force = data;
3873 nvlist_t *innvl = fnvlist_alloc();
3875 fnvlist_add_boolean_value(innvl, "force", *force);
3876 if ((ret = lzc_sync(pool_name, innvl, NULL)) != 0) {
3878 return (zpool_standard_error_fmt(hdl, ret,
3879 dgettext(TEXT_DOMAIN, "sync '%s' failed"), pool_name));
3886 #define PATH_BUF_LEN 64
3889 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3890 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3891 * We also check if this is a whole disk, in which case we strip off the
3892 * trailing 's0' slice name.
3894 * This routine is also responsible for identifying when disks have been
3895 * reconfigured in a new location. The kernel will have opened the device by
3896 * devid, but the path will still refer to the old location. To catch this, we
3897 * first do a path -> devid translation (which is fast for the common case). If
3898 * the devid matches, we're done. If not, we do a reverse devid -> path
3899 * translation and issue the appropriate ioctl() to update the path of the vdev.
3900 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3904 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3907 char *path, *type, *env;
3909 char buf[PATH_BUF_LEN];
3910 char tmpbuf[PATH_BUF_LEN];
3913 * vdev_name will be "root"/"root-0" for the root vdev, but it is the
3914 * zpool name that will be displayed to the user.
3916 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
3917 if (zhp != NULL && strcmp(type, "root") == 0)
3918 return (zfs_strdup(hdl, zpool_get_name(zhp)));
3920 env = getenv("ZPOOL_VDEV_NAME_PATH");
3921 if (env && (strtoul(env, NULL, 0) > 0 ||
3922 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3923 name_flags |= VDEV_NAME_PATH;
3925 env = getenv("ZPOOL_VDEV_NAME_GUID");
3926 if (env && (strtoul(env, NULL, 0) > 0 ||
3927 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3928 name_flags |= VDEV_NAME_GUID;
3930 env = getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
3931 if (env && (strtoul(env, NULL, 0) > 0 ||
3932 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3933 name_flags |= VDEV_NAME_FOLLOW_LINKS;
3935 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3936 name_flags & VDEV_NAME_GUID) {
3937 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value);
3938 (void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)value);
3940 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3941 if (name_flags & VDEV_NAME_FOLLOW_LINKS) {
3942 char *rp = realpath(path, NULL);
3944 strlcpy(buf, rp, sizeof (buf));
3951 * For a block device only use the name.
3953 if ((strcmp(type, VDEV_TYPE_DISK) == 0) &&
3954 !(name_flags & VDEV_NAME_PATH)) {
3955 path = strrchr(path, '/');
3960 * Remove the partition from the path it this is a whole disk.
3962 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, &value)
3963 == 0 && value && !(name_flags & VDEV_NAME_PATH)) {
3964 return (zfs_strip_partition(path));
3970 * If it's a raidz device, we need to stick in the parity level.
3972 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3973 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3975 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3976 (u_longlong_t)value);
3981 * We identify each top-level vdev by using a <type-id>
3982 * naming convention.
3984 if (name_flags & VDEV_NAME_TYPE_ID) {
3986 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3988 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s-%llu",
3989 path, (u_longlong_t)id);
3994 return (zfs_strdup(hdl, path));
3998 zbookmark_mem_compare(const void *a, const void *b)
4000 return (memcmp(a, b, sizeof (zbookmark_phys_t)));
4004 * Retrieve the persistent error log, uniquify the members, and return to the
4008 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
4010 zfs_cmd_t zc = {"\0"};
4011 libzfs_handle_t *hdl = zhp->zpool_hdl;
4013 zbookmark_phys_t *zb = NULL;
4017 * Retrieve the raw error list from the kernel. If the number of errors
4018 * has increased, allocate more space and continue until we get the
4021 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
4025 zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
4026 count * sizeof (zbookmark_phys_t));
4027 zc.zc_nvlist_dst_size = count;
4028 (void) strcpy(zc.zc_name, zhp->zpool_name);
4030 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
4032 free((void *)(uintptr_t)zc.zc_nvlist_dst);
4033 if (errno == ENOMEM) {
4036 count = zc.zc_nvlist_dst_size;
4037 dst = zfs_alloc(zhp->zpool_hdl, count *
4038 sizeof (zbookmark_phys_t));
4039 zc.zc_nvlist_dst = (uintptr_t)dst;
4041 return (zpool_standard_error_fmt(hdl, errno,
4042 dgettext(TEXT_DOMAIN, "errors: List of "
4043 "errors unavailable")));
4051 * Sort the resulting bookmarks. This is a little confusing due to the
4052 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
4053 * to first, and 'zc_nvlist_dst_size' indicates the number of bookmarks
4054 * _not_ copied as part of the process. So we point the start of our
4055 * array appropriate and decrement the total number of elements.
4057 zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
4058 zc.zc_nvlist_dst_size;
4059 count -= zc.zc_nvlist_dst_size;
4061 qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
4063 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
4066 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
4068 for (i = 0; i < count; i++) {
4071 /* ignoring zb_blkid and zb_level for now */
4072 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
4073 zb[i-1].zb_object == zb[i].zb_object)
4076 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
4078 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
4079 zb[i].zb_objset) != 0) {
4083 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
4084 zb[i].zb_object) != 0) {
4088 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
4095 free((void *)(uintptr_t)zc.zc_nvlist_dst);
4099 free((void *)(uintptr_t)zc.zc_nvlist_dst);
4100 return (no_memory(zhp->zpool_hdl));
4104 * Upgrade a ZFS pool to the latest on-disk version.
4107 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
4109 zfs_cmd_t zc = {"\0"};
4110 libzfs_handle_t *hdl = zhp->zpool_hdl;
4112 (void) strcpy(zc.zc_name, zhp->zpool_name);
4113 zc.zc_cookie = new_version;
4115 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
4116 return (zpool_standard_error_fmt(hdl, errno,
4117 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
4123 zfs_save_arguments(int argc, char **argv, char *string, int len)
4127 (void) strlcpy(string, basename(argv[0]), len);
4128 for (i = 1; i < argc; i++) {
4129 (void) strlcat(string, " ", len);
4130 (void) strlcat(string, argv[i], len);
4135 zpool_log_history(libzfs_handle_t *hdl, const char *message)
4137 zfs_cmd_t zc = {"\0"};
4141 args = fnvlist_alloc();
4142 fnvlist_add_string(args, "message", message);
4143 err = zcmd_write_src_nvlist(hdl, &zc, args);
4145 err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
4147 zcmd_free_nvlists(&zc);
4152 * Perform ioctl to get some command history of a pool.
4154 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
4155 * logical offset of the history buffer to start reading from.
4157 * Upon return, 'off' is the next logical offset to read from and
4158 * 'len' is the actual amount of bytes read into 'buf'.
4161 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
4163 zfs_cmd_t zc = {"\0"};
4164 libzfs_handle_t *hdl = zhp->zpool_hdl;
4166 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4168 zc.zc_history = (uint64_t)(uintptr_t)buf;
4169 zc.zc_history_len = *len;
4170 zc.zc_history_offset = *off;
4172 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
4175 return (zfs_error_fmt(hdl, EZFS_PERM,
4176 dgettext(TEXT_DOMAIN,
4177 "cannot show history for pool '%s'"),
4180 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
4181 dgettext(TEXT_DOMAIN, "cannot get history for pool "
4182 "'%s'"), zhp->zpool_name));
4184 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
4185 dgettext(TEXT_DOMAIN, "cannot get history for pool "
4186 "'%s', pool must be upgraded"), zhp->zpool_name));
4188 return (zpool_standard_error_fmt(hdl, errno,
4189 dgettext(TEXT_DOMAIN,
4190 "cannot get history for '%s'"), zhp->zpool_name));
4194 *len = zc.zc_history_len;
4195 *off = zc.zc_history_offset;
4201 * Retrieve the command history of a pool.
4204 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
4207 int buflen = 128 * 1024;
4209 nvlist_t **records = NULL;
4210 uint_t numrecords = 0;
4213 buf = malloc(buflen);
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 */
4227 if ((err = zpool_history_unpack(buf, bytes_read,
4228 &leftover, &records, &numrecords)) != 0)
4231 if (leftover == bytes_read) {
4233 * no progress made, because buffer is not big enough
4234 * to hold this record; resize and retry.
4238 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(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4387 char *pathname, size_t len)
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 (ioctl(zhp->zpool_hdl->libzfs_fd,
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 = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
4416 /* get the corrupted object's path */
4417 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
4419 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
4422 (void) snprintf(pathname, len, "%s%s", mntpnt,
4425 (void) snprintf(pathname, len, "%s:%s",
4426 dsname, zc.zc_value);
4429 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname,
4436 * Read the EFI label from the config, if a label does not exist then
4437 * pass back the error to the caller. If the caller has passed a non-NULL
4438 * diskaddr argument then we set it to the starting address of the EFI
4442 read_efi_label(nvlist_t *config, diskaddr_t *sb)
4446 char diskname[MAXPATHLEN];
4449 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
4452 (void) snprintf(diskname, sizeof (diskname), "%s%s", DISK_ROOT,
4453 strrchr(path, '/'));
4454 if ((fd = open(diskname, O_RDONLY|O_DIRECT)) >= 0) {
4455 struct dk_gpt *vtoc;
4457 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
4459 *sb = vtoc->efi_parts[0].p_start;
4468 * determine where a partition starts on a disk in the current
4472 find_start_block(nvlist_t *config)
4476 diskaddr_t sb = MAXOFFSET_T;
4479 if (nvlist_lookup_nvlist_array(config,
4480 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
4481 if (nvlist_lookup_uint64(config,
4482 ZPOOL_CONFIG_WHOLE_DISK,
4483 &wholedisk) != 0 || !wholedisk) {
4484 return (MAXOFFSET_T);
4486 if (read_efi_label(config, &sb) < 0)
4491 for (c = 0; c < children; c++) {
4492 sb = find_start_block(child[c]);
4493 if (sb != MAXOFFSET_T) {
4497 return (MAXOFFSET_T);
4501 zpool_label_disk_check(char *path)
4503 struct dk_gpt *vtoc;
4506 if ((fd = open(path, O_RDONLY|O_DIRECT)) < 0)
4509 if ((err = efi_alloc_and_read(fd, &vtoc)) != 0) {
4514 if (vtoc->efi_flags & EFI_GPT_PRIMARY_CORRUPT) {
4526 * Generate a unique partition name for the ZFS member. Partitions must
4527 * have unique names to ensure udev will be able to create symlinks under
4528 * /dev/disk/by-partlabel/ for all pool members. The partition names are
4529 * of the form <pool>-<unique-id>.
4532 zpool_label_name(char *label_name, int label_size)
4537 fd = open("/dev/urandom", O_RDONLY);
4539 if (read(fd, &id, sizeof (id)) != sizeof (id))
4546 id = (((uint64_t)rand()) << 32) | (uint64_t)rand();
4548 snprintf(label_name, label_size, "zfs-%016llx", (u_longlong_t)id);
4552 * Label an individual disk. The name provided is the short name,
4553 * stripped of any leading /dev path.
4556 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
4558 char path[MAXPATHLEN];
4559 struct dk_gpt *vtoc;
4561 size_t resv = EFI_MIN_RESV_SIZE;
4562 uint64_t slice_size;
4563 diskaddr_t start_block;
4566 /* prepare an error message just in case */
4567 (void) snprintf(errbuf, sizeof (errbuf),
4568 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
4573 verify(nvlist_lookup_nvlist(zhp->zpool_config,
4574 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4576 if (zhp->zpool_start_block == 0)
4577 start_block = find_start_block(nvroot);
4579 start_block = zhp->zpool_start_block;
4580 zhp->zpool_start_block = start_block;
4583 start_block = NEW_START_BLOCK;
4586 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
4588 if ((fd = open(path, O_RDWR|O_DIRECT|O_EXCL)) < 0) {
4590 * This shouldn't happen. We've long since verified that this
4591 * is a valid device.
4593 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
4594 "label '%s': unable to open device: %d"), path, errno);
4595 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
4598 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
4600 * The only way this can fail is if we run out of memory, or we
4601 * were unable to read the disk's capacity
4603 if (errno == ENOMEM)
4604 (void) no_memory(hdl);
4607 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
4608 "label '%s': unable to read disk capacity"), path);
4610 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
4613 slice_size = vtoc->efi_last_u_lba + 1;
4614 slice_size -= EFI_MIN_RESV_SIZE;
4615 if (start_block == MAXOFFSET_T)
4616 start_block = NEW_START_BLOCK;
4617 slice_size -= start_block;
4618 slice_size = P2ALIGN(slice_size, PARTITION_END_ALIGNMENT);
4620 vtoc->efi_parts[0].p_start = start_block;
4621 vtoc->efi_parts[0].p_size = slice_size;
4624 * Why we use V_USR: V_BACKUP confuses users, and is considered
4625 * disposable by some EFI utilities (since EFI doesn't have a backup
4626 * slice). V_UNASSIGNED is supposed to be used only for zero size
4627 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4628 * etc. were all pretty specific. V_USR is as close to reality as we
4629 * can get, in the absence of V_OTHER.
4631 vtoc->efi_parts[0].p_tag = V_USR;
4632 zpool_label_name(vtoc->efi_parts[0].p_name, EFI_PART_NAME_LEN);
4634 vtoc->efi_parts[8].p_start = slice_size + start_block;
4635 vtoc->efi_parts[8].p_size = resv;
4636 vtoc->efi_parts[8].p_tag = V_RESERVED;
4638 rval = efi_write(fd, vtoc);
4640 /* Flush the buffers to disk and invalidate the page cache. */
4642 (void) ioctl(fd, BLKFLSBUF);
4645 rval = efi_rescan(fd);
4648 * Some block drivers (like pcata) may not support EFI GPT labels.
4649 * Print out a helpful error message directing the user to manually
4650 * label the disk and give a specific slice.
4656 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "try using "
4657 "parted(8) and then provide a specific slice: %d"), rval);
4658 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4664 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
4665 (void) zfs_append_partition(path, MAXPATHLEN);
4667 /* Wait to udev to signal use the device has settled. */
4668 rval = zpool_label_disk_wait(path, DISK_LABEL_WAIT);
4670 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "failed to "
4671 "detect device partitions on '%s': %d"), path, rval);
4672 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4675 /* We can't be to paranoid. Read the label back and verify it. */
4676 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
4677 rval = zpool_label_disk_check(path);
4679 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "freshly written "
4680 "EFI label on '%s' is damaged. Ensure\nthis device "
4681 "is not in use, and is functioning properly: %d"),
4683 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4690 * Wait while the specified activity is in progress in the pool.
4693 zpool_wait(zpool_handle_t *zhp, zpool_wait_activity_t activity)
4697 int error = zpool_wait_status(zhp, activity, &missing, NULL);
4700 (void) zpool_standard_error_fmt(zhp->zpool_hdl, ENOENT,
4701 dgettext(TEXT_DOMAIN, "error waiting in pool '%s'"),
4710 * Wait for the given activity and return the status of the wait (whether or not
4711 * any waiting was done) in the 'waited' parameter. Non-existent pools are
4712 * reported via the 'missing' parameter, rather than by printing an error
4713 * message. This is convenient when this function is called in a loop over a
4714 * long period of time (as it is, for example, by zpool's wait cmd). In that
4715 * scenario, a pool being exported or destroyed should be considered a normal
4716 * event, so we don't want to print an error when we find that the pool doesn't
4720 zpool_wait_status(zpool_handle_t *zhp, zpool_wait_activity_t activity,
4721 boolean_t *missing, boolean_t *waited)
4723 int error = lzc_wait(zhp->zpool_name, activity, waited);
4724 *missing = (error == ENOENT);
4729 (void) zpool_standard_error_fmt(zhp->zpool_hdl, error,
4730 dgettext(TEXT_DOMAIN, "error waiting in pool '%s'"),