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.
44 #include <sys/efi_partition.h>
45 #include <sys/systeminfo.h>
47 #include <sys/zfs_ioctl.h>
48 #include <sys/vdev_disk.h>
51 #include "zfs_namecheck.h"
53 #include "libzfs_impl.h"
54 #include "zfs_comutil.h"
55 #include "zfeature_common.h"
57 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
58 static boolean_t zpool_vdev_is_interior(const char *name);
60 typedef struct prop_flags {
61 int create:1; /* Validate property on creation */
62 int import:1; /* Validate property on import */
66 * ====================================================================
67 * zpool property functions
68 * ====================================================================
72 zpool_get_all_props(zpool_handle_t *zhp)
74 zfs_cmd_t zc = {"\0"};
75 libzfs_handle_t *hdl = zhp->zpool_hdl;
77 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
79 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
82 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
83 if (errno == ENOMEM) {
84 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
85 zcmd_free_nvlists(&zc);
89 zcmd_free_nvlists(&zc);
94 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
95 zcmd_free_nvlists(&zc);
99 zcmd_free_nvlists(&zc);
105 zpool_props_refresh(zpool_handle_t *zhp)
109 old_props = zhp->zpool_props;
111 if (zpool_get_all_props(zhp) != 0)
114 nvlist_free(old_props);
119 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
125 zprop_source_t source;
127 nvl = zhp->zpool_props;
128 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
129 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
131 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
133 source = ZPROP_SRC_DEFAULT;
134 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
145 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
149 zprop_source_t source;
151 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
153 * zpool_get_all_props() has most likely failed because
154 * the pool is faulted, but if all we need is the top level
155 * vdev's guid then get it from the zhp config nvlist.
157 if ((prop == ZPOOL_PROP_GUID) &&
158 (nvlist_lookup_nvlist(zhp->zpool_config,
159 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
160 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
164 return (zpool_prop_default_numeric(prop));
167 nvl = zhp->zpool_props;
168 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
169 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
171 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
173 source = ZPROP_SRC_DEFAULT;
174 value = zpool_prop_default_numeric(prop);
184 * Map VDEV STATE to printed strings.
187 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
190 case VDEV_STATE_CLOSED:
191 case VDEV_STATE_OFFLINE:
192 return (gettext("OFFLINE"));
193 case VDEV_STATE_REMOVED:
194 return (gettext("REMOVED"));
195 case VDEV_STATE_CANT_OPEN:
196 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
197 return (gettext("FAULTED"));
198 else if (aux == VDEV_AUX_SPLIT_POOL)
199 return (gettext("SPLIT"));
201 return (gettext("UNAVAIL"));
202 case VDEV_STATE_FAULTED:
203 return (gettext("FAULTED"));
204 case VDEV_STATE_DEGRADED:
205 return (gettext("DEGRADED"));
206 case VDEV_STATE_HEALTHY:
207 return (gettext("ONLINE"));
213 return (gettext("UNKNOWN"));
217 * Map POOL STATE to printed strings.
220 zpool_pool_state_to_name(pool_state_t state)
225 case POOL_STATE_ACTIVE:
226 return (gettext("ACTIVE"));
227 case POOL_STATE_EXPORTED:
228 return (gettext("EXPORTED"));
229 case POOL_STATE_DESTROYED:
230 return (gettext("DESTROYED"));
231 case POOL_STATE_SPARE:
232 return (gettext("SPARE"));
233 case POOL_STATE_L2CACHE:
234 return (gettext("L2CACHE"));
235 case POOL_STATE_UNINITIALIZED:
236 return (gettext("UNINITIALIZED"));
237 case POOL_STATE_UNAVAIL:
238 return (gettext("UNAVAIL"));
239 case POOL_STATE_POTENTIALLY_ACTIVE:
240 return (gettext("POTENTIALLY_ACTIVE"));
243 return (gettext("UNKNOWN"));
247 * Given a pool handle, return the pool health string ("ONLINE", "DEGRADED",
251 zpool_get_state_str(zpool_handle_t *zhp)
253 zpool_errata_t errata;
254 zpool_status_t status;
260 status = zpool_get_status(zhp, NULL, &errata);
262 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
263 str = gettext("FAULTED");
264 } else if (status == ZPOOL_STATUS_IO_FAILURE_WAIT ||
265 status == ZPOOL_STATUS_IO_FAILURE_MMP) {
266 str = gettext("SUSPENDED");
268 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
269 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
270 verify(nvlist_lookup_uint64_array(nvroot,
271 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
273 str = zpool_state_to_name(vs->vs_state, vs->vs_aux);
279 * Get a zpool property value for 'prop' and return the value in
280 * a pre-allocated buffer.
283 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf,
284 size_t len, zprop_source_t *srctype, boolean_t literal)
288 zprop_source_t src = ZPROP_SRC_NONE;
290 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
292 case ZPOOL_PROP_NAME:
293 (void) strlcpy(buf, zpool_get_name(zhp), len);
296 case ZPOOL_PROP_HEALTH:
297 (void) strlcpy(buf, zpool_get_state_str(zhp), len);
300 case ZPOOL_PROP_GUID:
301 intval = zpool_get_prop_int(zhp, prop, &src);
302 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
305 case ZPOOL_PROP_ALTROOT:
306 case ZPOOL_PROP_CACHEFILE:
307 case ZPOOL_PROP_COMMENT:
308 if (zhp->zpool_props != NULL ||
309 zpool_get_all_props(zhp) == 0) {
311 zpool_get_prop_string(zhp, prop, &src),
317 (void) strlcpy(buf, "-", len);
326 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
327 prop != ZPOOL_PROP_NAME)
330 switch (zpool_prop_get_type(prop)) {
331 case PROP_TYPE_STRING:
332 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
336 case PROP_TYPE_NUMBER:
337 intval = zpool_get_prop_int(zhp, prop, &src);
340 case ZPOOL_PROP_SIZE:
341 case ZPOOL_PROP_ALLOCATED:
342 case ZPOOL_PROP_FREE:
343 case ZPOOL_PROP_FREEING:
344 case ZPOOL_PROP_LEAKED:
345 case ZPOOL_PROP_ASHIFT:
347 (void) snprintf(buf, len, "%llu",
348 (u_longlong_t)intval);
350 (void) zfs_nicenum(intval, buf, len);
353 case ZPOOL_PROP_EXPANDSZ:
354 case ZPOOL_PROP_CHECKPOINT:
356 (void) strlcpy(buf, "-", len);
357 } else if (literal) {
358 (void) snprintf(buf, len, "%llu",
359 (u_longlong_t)intval);
361 (void) zfs_nicebytes(intval, buf, len);
365 case ZPOOL_PROP_CAPACITY:
367 (void) snprintf(buf, len, "%llu",
368 (u_longlong_t)intval);
370 (void) snprintf(buf, len, "%llu%%",
371 (u_longlong_t)intval);
375 case ZPOOL_PROP_FRAGMENTATION:
376 if (intval == UINT64_MAX) {
377 (void) strlcpy(buf, "-", len);
378 } else if (literal) {
379 (void) snprintf(buf, len, "%llu",
380 (u_longlong_t)intval);
382 (void) snprintf(buf, len, "%llu%%",
383 (u_longlong_t)intval);
387 case ZPOOL_PROP_DEDUPRATIO:
389 (void) snprintf(buf, len, "%llu.%02llu",
390 (u_longlong_t)(intval / 100),
391 (u_longlong_t)(intval % 100));
393 (void) snprintf(buf, len, "%llu.%02llux",
394 (u_longlong_t)(intval / 100),
395 (u_longlong_t)(intval % 100));
398 case ZPOOL_PROP_HEALTH:
399 (void) strlcpy(buf, zpool_get_state_str(zhp), len);
401 case ZPOOL_PROP_VERSION:
402 if (intval >= SPA_VERSION_FEATURES) {
403 (void) snprintf(buf, len, "-");
408 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
412 case PROP_TYPE_INDEX:
413 intval = zpool_get_prop_int(zhp, prop, &src);
414 if (zpool_prop_index_to_string(prop, intval, &strval)
417 (void) strlcpy(buf, strval, len);
431 * Check if the bootfs name has the same pool name as it is set to.
432 * Assuming bootfs is a valid dataset name.
435 bootfs_name_valid(const char *pool, char *bootfs)
437 int len = strlen(pool);
438 if (bootfs[0] == '\0')
441 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
444 if (strncmp(pool, bootfs, len) == 0 &&
445 (bootfs[len] == '/' || bootfs[len] == '\0'))
452 zpool_is_bootable(zpool_handle_t *zhp)
454 char bootfs[ZFS_MAX_DATASET_NAME_LEN];
456 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
457 sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-",
458 sizeof (bootfs)) != 0);
463 * Given an nvlist of zpool properties to be set, validate that they are
464 * correct, and parse any numeric properties (index, boolean, etc) if they are
465 * specified as strings.
468 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
469 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
477 struct stat64 statbuf;
480 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
481 (void) no_memory(hdl);
486 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
487 const char *propname = nvpair_name(elem);
489 prop = zpool_name_to_prop(propname);
490 if (prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname)) {
492 char *fname = strchr(propname, '@') + 1;
494 err = zfeature_lookup_name(fname, NULL);
496 ASSERT3U(err, ==, ENOENT);
497 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
498 "invalid feature '%s'"), fname);
499 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
503 if (nvpair_type(elem) != DATA_TYPE_STRING) {
504 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
505 "'%s' must be a string"), propname);
506 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
510 (void) nvpair_value_string(elem, &strval);
511 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0 &&
512 strcmp(strval, ZFS_FEATURE_DISABLED) != 0) {
513 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
514 "property '%s' can only be set to "
515 "'enabled' or 'disabled'"), propname);
516 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
521 strcmp(strval, ZFS_FEATURE_DISABLED) == 0) {
522 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
523 "property '%s' can only be set to "
524 "'disabled' at creation time"), propname);
525 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
529 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
530 (void) no_memory(hdl);
537 * Make sure this property is valid and applies to this type.
539 if (prop == ZPOOL_PROP_INVAL) {
540 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
541 "invalid property '%s'"), propname);
542 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
546 if (zpool_prop_readonly(prop)) {
547 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
548 "is readonly"), propname);
549 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
553 if (!flags.create && zpool_prop_setonce(prop)) {
554 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
555 "property '%s' can only be set at "
556 "creation time"), propname);
557 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
561 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
562 &strval, &intval, errbuf) != 0)
566 * Perform additional checking for specific properties.
569 case ZPOOL_PROP_VERSION:
570 if (intval < version ||
571 !SPA_VERSION_IS_SUPPORTED(intval)) {
572 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
573 "property '%s' number %d is invalid."),
575 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
580 case ZPOOL_PROP_ASHIFT:
582 (intval < ASHIFT_MIN || intval > ASHIFT_MAX)) {
583 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
584 "invalid '%s=%d' property: only values "
585 "between %" PRId32 " and %" PRId32 " "
587 propname, intval, ASHIFT_MIN, ASHIFT_MAX);
588 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
593 case ZPOOL_PROP_BOOTFS:
594 if (flags.create || flags.import) {
595 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
596 "property '%s' cannot be set at creation "
597 "or import time"), propname);
598 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
602 if (version < SPA_VERSION_BOOTFS) {
603 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
604 "pool must be upgraded to support "
605 "'%s' property"), propname);
606 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
611 * bootfs property value has to be a dataset name and
612 * the dataset has to be in the same pool as it sets to.
614 if (!bootfs_name_valid(poolname, strval)) {
615 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
616 "is an invalid name"), strval);
617 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
621 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
622 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
623 "could not open pool '%s'"), poolname);
624 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
630 case ZPOOL_PROP_ALTROOT:
631 if (!flags.create && !flags.import) {
632 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
633 "property '%s' can only be set during pool "
634 "creation or import"), propname);
635 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
639 if (strval[0] != '/') {
640 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
641 "bad alternate root '%s'"), strval);
642 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
647 case ZPOOL_PROP_CACHEFILE:
648 if (strval[0] == '\0')
651 if (strcmp(strval, "none") == 0)
654 if (strval[0] != '/') {
655 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
656 "property '%s' must be empty, an "
657 "absolute path, or 'none'"), propname);
658 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
662 slash = strrchr(strval, '/');
664 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
665 strcmp(slash, "/..") == 0) {
666 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
667 "'%s' is not a valid file"), strval);
668 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
674 if (strval[0] != '\0' &&
675 (stat64(strval, &statbuf) != 0 ||
676 !S_ISDIR(statbuf.st_mode))) {
677 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
678 "'%s' is not a valid directory"),
680 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
687 case ZPOOL_PROP_COMMENT:
688 for (check = strval; *check != '\0'; check++) {
689 if (!isprint(*check)) {
691 dgettext(TEXT_DOMAIN,
692 "comment may only have printable "
694 (void) zfs_error(hdl, EZFS_BADPROP,
699 if (strlen(strval) > ZPROP_MAX_COMMENT) {
700 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
701 "comment must not exceed %d characters"),
703 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
707 case ZPOOL_PROP_READONLY:
709 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
710 "property '%s' can only be set at "
711 "import time"), propname);
712 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
716 case ZPOOL_PROP_MULTIHOST:
717 if (get_system_hostid() == 0) {
718 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
719 "requires a non-zero system hostid"));
720 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
726 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
727 "property '%s'(%d) not defined"), propname, prop);
734 nvlist_free(retprops);
739 * Set zpool property : propname=propval.
742 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
744 zfs_cmd_t zc = {"\0"};
747 nvlist_t *nvl = NULL;
750 prop_flags_t flags = { 0 };
752 (void) snprintf(errbuf, sizeof (errbuf),
753 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
756 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
757 return (no_memory(zhp->zpool_hdl));
759 if (nvlist_add_string(nvl, propname, propval) != 0) {
761 return (no_memory(zhp->zpool_hdl));
764 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
765 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
766 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
775 * Execute the corresponding ioctl() to set this property.
777 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
779 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
784 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
786 zcmd_free_nvlists(&zc);
790 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
792 (void) zpool_props_refresh(zhp);
798 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
800 libzfs_handle_t *hdl = zhp->zpool_hdl;
802 char buf[ZFS_MAXPROPLEN];
803 nvlist_t *features = NULL;
806 boolean_t firstexpand = (NULL == *plp);
809 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
813 while (*last != NULL)
814 last = &(*last)->pl_next;
817 features = zpool_get_features(zhp);
819 if ((*plp)->pl_all && firstexpand) {
820 for (i = 0; i < SPA_FEATURES; i++) {
821 zprop_list_t *entry = zfs_alloc(hdl,
822 sizeof (zprop_list_t));
823 entry->pl_prop = ZPROP_INVAL;
824 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
825 spa_feature_table[i].fi_uname);
826 entry->pl_width = strlen(entry->pl_user_prop);
827 entry->pl_all = B_TRUE;
830 last = &entry->pl_next;
834 /* add any unsupported features */
835 for (nvp = nvlist_next_nvpair(features, NULL);
836 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
841 if (zfeature_is_supported(nvpair_name(nvp)))
844 propname = zfs_asprintf(hdl, "unsupported@%s",
848 * Before adding the property to the list make sure that no
849 * other pool already added the same property.
853 while (entry != NULL) {
854 if (entry->pl_user_prop != NULL &&
855 strcmp(propname, entry->pl_user_prop) == 0) {
859 entry = entry->pl_next;
866 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
867 entry->pl_prop = ZPROP_INVAL;
868 entry->pl_user_prop = propname;
869 entry->pl_width = strlen(entry->pl_user_prop);
870 entry->pl_all = B_TRUE;
873 last = &entry->pl_next;
876 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
881 if (entry->pl_prop != ZPROP_INVAL &&
882 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
883 NULL, B_FALSE) == 0) {
884 if (strlen(buf) > entry->pl_width)
885 entry->pl_width = strlen(buf);
893 * Get the state for the given feature on the given ZFS pool.
896 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
900 boolean_t found = B_FALSE;
901 nvlist_t *features = zpool_get_features(zhp);
903 const char *feature = strchr(propname, '@') + 1;
905 supported = zpool_prop_feature(propname);
906 ASSERT(supported || zpool_prop_unsupported(propname));
909 * Convert from feature name to feature guid. This conversion is
910 * unnecessary for unsupported@... properties because they already
917 ret = zfeature_lookup_name(feature, &fid);
919 (void) strlcpy(buf, "-", len);
922 feature = spa_feature_table[fid].fi_guid;
925 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
930 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
933 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
935 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
940 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
942 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
945 (void) strlcpy(buf, "-", len);
954 * Validate the given pool name, optionally putting an extended error message in
958 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
964 ret = pool_namecheck(pool, &why, &what);
967 * The rules for reserved pool names were extended at a later point.
968 * But we need to support users with existing pools that may now be
969 * invalid. So we only check for this expanded set of names during a
970 * create (or import), and only in userland.
972 if (ret == 0 && !isopen &&
973 (strncmp(pool, "mirror", 6) == 0 ||
974 strncmp(pool, "raidz", 5) == 0 ||
975 strncmp(pool, "spare", 5) == 0 ||
976 strcmp(pool, "log") == 0)) {
979 dgettext(TEXT_DOMAIN, "name is reserved"));
987 case NAME_ERR_TOOLONG:
989 dgettext(TEXT_DOMAIN, "name is too long"));
992 case NAME_ERR_INVALCHAR:
994 dgettext(TEXT_DOMAIN, "invalid character "
995 "'%c' in pool name"), what);
998 case NAME_ERR_NOLETTER:
999 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1000 "name must begin with a letter"));
1003 case NAME_ERR_RESERVED:
1004 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1005 "name is reserved"));
1008 case NAME_ERR_DISKLIKE:
1009 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1010 "pool name is reserved"));
1013 case NAME_ERR_LEADING_SLASH:
1014 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1015 "leading slash in name"));
1018 case NAME_ERR_EMPTY_COMPONENT:
1019 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1020 "empty component in name"));
1023 case NAME_ERR_TRAILING_SLASH:
1024 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1025 "trailing slash in name"));
1028 case NAME_ERR_MULTIPLE_DELIMITERS:
1029 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1030 "multiple '@' and/or '#' delimiters in "
1034 case NAME_ERR_NO_AT:
1035 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1036 "permission set is missing '@'"));
1040 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1041 "(%d) not defined"), why);
1052 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1056 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
1058 zpool_handle_t *zhp;
1062 * Make sure the pool name is valid.
1064 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
1065 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1066 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1071 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1074 zhp->zpool_hdl = hdl;
1075 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1077 if (zpool_refresh_stats(zhp, &missing) != 0) {
1083 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1084 (void) zfs_error_fmt(hdl, EZFS_NOENT,
1085 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1094 * Like the above, but silent on error. Used when iterating over pools (because
1095 * the configuration cache may be out of date).
1098 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1100 zpool_handle_t *zhp;
1103 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1106 zhp->zpool_hdl = hdl;
1107 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1109 if (zpool_refresh_stats(zhp, &missing) != 0) {
1125 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1129 zpool_open(libzfs_handle_t *hdl, const char *pool)
1131 zpool_handle_t *zhp;
1133 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1136 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1137 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1138 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1147 * Close the handle. Simply frees the memory associated with the handle.
1150 zpool_close(zpool_handle_t *zhp)
1152 nvlist_free(zhp->zpool_config);
1153 nvlist_free(zhp->zpool_old_config);
1154 nvlist_free(zhp->zpool_props);
1159 * Return the name of the pool.
1162 zpool_get_name(zpool_handle_t *zhp)
1164 return (zhp->zpool_name);
1169 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1172 zpool_get_state(zpool_handle_t *zhp)
1174 return (zhp->zpool_state);
1178 * Check if vdev list contains a special vdev
1181 zpool_has_special_vdev(nvlist_t *nvroot)
1186 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, &child,
1188 for (uint_t c = 0; c < children; c++) {
1191 if (nvlist_lookup_string(child[c],
1192 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias) == 0 &&
1193 strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0) {
1202 * Create the named pool, using the provided vdev list. It is assumed
1203 * that the consumer has already validated the contents of the nvlist, so we
1204 * don't have to worry about error semantics.
1207 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1208 nvlist_t *props, nvlist_t *fsprops)
1210 zfs_cmd_t zc = {"\0"};
1211 nvlist_t *zc_fsprops = NULL;
1212 nvlist_t *zc_props = NULL;
1213 nvlist_t *hidden_args = NULL;
1214 uint8_t *wkeydata = NULL;
1219 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1220 "cannot create '%s'"), pool);
1222 if (!zpool_name_valid(hdl, B_FALSE, pool))
1223 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1225 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1229 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1231 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1232 SPA_VERSION_1, flags, msg)) == NULL) {
1241 zoned = ((nvlist_lookup_string(fsprops,
1242 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1243 strcmp(zonestr, "on") == 0);
1245 if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
1246 fsprops, zoned, NULL, NULL, B_TRUE, msg)) == NULL) {
1250 if (nvlist_exists(zc_fsprops,
1251 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS)) &&
1252 !zpool_has_special_vdev(nvroot)) {
1253 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1254 "%s property requires a special vdev"),
1255 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS));
1256 (void) zfs_error(hdl, EZFS_BADPROP, msg);
1261 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1264 if (zfs_crypto_create(hdl, NULL, zc_fsprops, props, B_TRUE,
1265 &wkeydata, &wkeylen) != 0) {
1266 zfs_error(hdl, EZFS_CRYPTOFAILED, msg);
1269 if (nvlist_add_nvlist(zc_props,
1270 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1273 if (wkeydata != NULL) {
1274 if (nvlist_alloc(&hidden_args, NV_UNIQUE_NAME, 0) != 0)
1277 if (nvlist_add_uint8_array(hidden_args, "wkeydata",
1278 wkeydata, wkeylen) != 0)
1281 if (nvlist_add_nvlist(zc_props, ZPOOL_HIDDEN_ARGS,
1287 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1290 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1292 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1294 zcmd_free_nvlists(&zc);
1295 nvlist_free(zc_props);
1296 nvlist_free(zc_fsprops);
1297 nvlist_free(hidden_args);
1298 if (wkeydata != NULL)
1304 * This can happen if the user has specified the same
1305 * device multiple times. We can't reliably detect this
1306 * until we try to add it and see we already have a
1307 * label. This can also happen under if the device is
1308 * part of an active md or lvm device.
1310 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1311 "one or more vdevs refer to the same device, or "
1312 "one of\nthe devices is part of an active md or "
1314 return (zfs_error(hdl, EZFS_BADDEV, msg));
1318 * This happens if the record size is smaller or larger
1319 * than the allowed size range, or not a power of 2.
1321 * NOTE: although zfs_valid_proplist is called earlier,
1322 * this case may have slipped through since the
1323 * pool does not exist yet and it is therefore
1324 * impossible to read properties e.g. max blocksize
1327 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1328 "record size invalid"));
1329 return (zfs_error(hdl, EZFS_BADPROP, msg));
1333 * This occurs when one of the devices is below
1334 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1335 * device was the problem device since there's no
1336 * reliable way to determine device size from userland.
1341 zfs_nicebytes(SPA_MINDEVSIZE, buf,
1344 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1345 "one or more devices is less than the "
1346 "minimum size (%s)"), buf);
1348 return (zfs_error(hdl, EZFS_BADDEV, msg));
1351 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1352 "one or more devices is out of space"));
1353 return (zfs_error(hdl, EZFS_BADDEV, msg));
1356 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1357 "cache device must be a disk or disk slice"));
1358 return (zfs_error(hdl, EZFS_BADDEV, msg));
1361 return (zpool_standard_error(hdl, errno, msg));
1366 zcmd_free_nvlists(&zc);
1367 nvlist_free(zc_props);
1368 nvlist_free(zc_fsprops);
1369 nvlist_free(hidden_args);
1370 if (wkeydata != NULL)
1376 * Destroy the given pool. It is up to the caller to ensure that there are no
1377 * datasets left in the pool.
1380 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1382 zfs_cmd_t zc = {"\0"};
1383 zfs_handle_t *zfp = NULL;
1384 libzfs_handle_t *hdl = zhp->zpool_hdl;
1387 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1388 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1391 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1392 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1394 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1395 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1396 "cannot destroy '%s'"), zhp->zpool_name);
1398 if (errno == EROFS) {
1399 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1400 "one or more devices is read only"));
1401 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1403 (void) zpool_standard_error(hdl, errno, msg);
1412 remove_mountpoint(zfp);
1420 * Create a checkpoint in the given pool.
1423 zpool_checkpoint(zpool_handle_t *zhp)
1425 libzfs_handle_t *hdl = zhp->zpool_hdl;
1429 error = lzc_pool_checkpoint(zhp->zpool_name);
1431 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1432 "cannot checkpoint '%s'"), zhp->zpool_name);
1433 (void) zpool_standard_error(hdl, error, msg);
1441 * Discard the checkpoint from the given pool.
1444 zpool_discard_checkpoint(zpool_handle_t *zhp)
1446 libzfs_handle_t *hdl = zhp->zpool_hdl;
1450 error = lzc_pool_checkpoint_discard(zhp->zpool_name);
1452 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1453 "cannot discard checkpoint in '%s'"), zhp->zpool_name);
1454 (void) zpool_standard_error(hdl, error, msg);
1462 * Add the given vdevs to the pool. The caller must have already performed the
1463 * necessary verification to ensure that the vdev specification is well-formed.
1466 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1468 zfs_cmd_t zc = {"\0"};
1470 libzfs_handle_t *hdl = zhp->zpool_hdl;
1472 nvlist_t **spares, **l2cache;
1473 uint_t nspares, nl2cache;
1475 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1476 "cannot add to '%s'"), zhp->zpool_name);
1478 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1479 SPA_VERSION_SPARES &&
1480 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1481 &spares, &nspares) == 0) {
1482 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1483 "upgraded to add hot spares"));
1484 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1487 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1488 SPA_VERSION_L2CACHE &&
1489 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1490 &l2cache, &nl2cache) == 0) {
1491 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1492 "upgraded to add cache devices"));
1493 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1496 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1498 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1500 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1504 * This can happen if the user has specified the same
1505 * device multiple times. We can't reliably detect this
1506 * until we try to add it and see we already have a
1509 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1510 "one or more vdevs refer to the same device"));
1511 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1515 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1516 "invalid config; a pool with removing/removed "
1517 "vdevs does not support adding raidz vdevs"));
1518 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1523 * This occurrs when one of the devices is below
1524 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1525 * device was the problem device since there's no
1526 * reliable way to determine device size from userland.
1531 zfs_nicebytes(SPA_MINDEVSIZE, buf,
1534 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1535 "device is less than the minimum "
1538 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1542 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1543 "pool must be upgraded to add these vdevs"));
1544 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1548 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1549 "cache device must be a disk or disk slice"));
1550 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1554 (void) zpool_standard_error(hdl, errno, msg);
1562 zcmd_free_nvlists(&zc);
1568 * Exports the pool from the system. The caller must ensure that there are no
1569 * mounted datasets in the pool.
1572 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1573 const char *log_str)
1575 zfs_cmd_t zc = {"\0"};
1578 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1579 "cannot export '%s'"), zhp->zpool_name);
1581 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1582 zc.zc_cookie = force;
1583 zc.zc_guid = hardforce;
1584 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1586 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1589 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1590 "use '-f' to override the following errors:\n"
1591 "'%s' has an active shared spare which could be"
1592 " used by other pools once '%s' is exported."),
1593 zhp->zpool_name, zhp->zpool_name);
1594 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1597 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1606 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1608 return (zpool_export_common(zhp, force, B_FALSE, log_str));
1612 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1614 return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1618 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1621 nvlist_t *nv = NULL;
1627 if (!hdl->libzfs_printerr || config == NULL)
1630 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1631 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1635 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1637 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1639 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1640 strftime(timestr, 128, "%c", &t) != 0) {
1642 (void) printf(dgettext(TEXT_DOMAIN,
1643 "Would be able to return %s "
1644 "to its state as of %s.\n"),
1647 (void) printf(dgettext(TEXT_DOMAIN,
1648 "Pool %s returned to its state as of %s.\n"),
1652 (void) printf(dgettext(TEXT_DOMAIN,
1653 "%s approximately %lld "),
1654 dryrun ? "Would discard" : "Discarded",
1655 ((longlong_t)loss + 30) / 60);
1656 (void) printf(dgettext(TEXT_DOMAIN,
1657 "minutes of transactions.\n"));
1658 } else if (loss > 0) {
1659 (void) printf(dgettext(TEXT_DOMAIN,
1660 "%s approximately %lld "),
1661 dryrun ? "Would discard" : "Discarded",
1663 (void) printf(dgettext(TEXT_DOMAIN,
1664 "seconds of transactions.\n"));
1670 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1673 nvlist_t *nv = NULL;
1675 uint64_t edata = UINT64_MAX;
1680 if (!hdl->libzfs_printerr)
1684 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1686 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1688 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1689 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1690 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1691 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1694 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1695 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1698 (void) printf(dgettext(TEXT_DOMAIN,
1699 "Recovery is possible, but will result in some data loss.\n"));
1701 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1702 strftime(timestr, 128, "%c", &t) != 0) {
1703 (void) printf(dgettext(TEXT_DOMAIN,
1704 "\tReturning the pool to its state as of %s\n"
1705 "\tshould correct the problem. "),
1708 (void) printf(dgettext(TEXT_DOMAIN,
1709 "\tReverting the pool to an earlier state "
1710 "should correct the problem.\n\t"));
1714 (void) printf(dgettext(TEXT_DOMAIN,
1715 "Approximately %lld minutes of data\n"
1716 "\tmust be discarded, irreversibly. "),
1717 ((longlong_t)loss + 30) / 60);
1718 } else if (loss > 0) {
1719 (void) printf(dgettext(TEXT_DOMAIN,
1720 "Approximately %lld seconds of data\n"
1721 "\tmust be discarded, irreversibly. "),
1724 if (edata != 0 && edata != UINT64_MAX) {
1726 (void) printf(dgettext(TEXT_DOMAIN,
1727 "After rewind, at least\n"
1728 "\tone persistent user-data error will remain. "));
1730 (void) printf(dgettext(TEXT_DOMAIN,
1731 "After rewind, several\n"
1732 "\tpersistent user-data errors will remain. "));
1735 (void) printf(dgettext(TEXT_DOMAIN,
1736 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1737 reason >= 0 ? "clear" : "import", name);
1739 (void) printf(dgettext(TEXT_DOMAIN,
1740 "A scrub of the pool\n"
1741 "\tis strongly recommended after recovery.\n"));
1745 (void) printf(dgettext(TEXT_DOMAIN,
1746 "Destroy and re-create the pool from\n\ta backup source.\n"));
1750 * zpool_import() is a contracted interface. Should be kept the same
1753 * Applications should use zpool_import_props() to import a pool with
1754 * new properties value to be set.
1757 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1760 nvlist_t *props = NULL;
1763 if (altroot != NULL) {
1764 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1765 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1766 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1770 if (nvlist_add_string(props,
1771 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1772 nvlist_add_string(props,
1773 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1775 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1776 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1781 ret = zpool_import_props(hdl, config, newname, props,
1788 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1794 uint64_t is_log = 0;
1796 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1800 (void) printf("\t%*s%s%s\n", indent, "", name,
1801 is_log ? " [log]" : "");
1803 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1804 &child, &children) != 0)
1807 for (c = 0; c < children; c++) {
1808 vname = zpool_vdev_name(hdl, NULL, child[c], VDEV_NAME_TYPE_ID);
1809 print_vdev_tree(hdl, vname, child[c], indent + 2);
1815 zpool_print_unsup_feat(nvlist_t *config)
1817 nvlist_t *nvinfo, *unsup_feat;
1820 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1822 verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1825 for (nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1826 nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1829 verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1830 verify(nvpair_value_string(nvp, &desc) == 0);
1832 if (strlen(desc) > 0)
1833 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1835 (void) printf("\t%s\n", nvpair_name(nvp));
1840 * Import the given pool using the known configuration and a list of
1841 * properties to be set. The configuration should have come from
1842 * zpool_find_import(). The 'newname' parameters control whether the pool
1843 * is imported with a different name.
1846 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1847 nvlist_t *props, int flags)
1849 zfs_cmd_t zc = {"\0"};
1850 zpool_load_policy_t policy;
1851 nvlist_t *nv = NULL;
1852 nvlist_t *nvinfo = NULL;
1853 nvlist_t *missing = NULL;
1860 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1863 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1864 "cannot import pool '%s'"), origname);
1866 if (newname != NULL) {
1867 if (!zpool_name_valid(hdl, B_FALSE, newname))
1868 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1869 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1871 thename = (char *)newname;
1876 if (props != NULL) {
1878 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1880 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1883 if ((props = zpool_valid_proplist(hdl, origname,
1884 props, version, flags, errbuf)) == NULL)
1886 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1893 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1895 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1898 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1899 zcmd_free_nvlists(&zc);
1902 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1903 zcmd_free_nvlists(&zc);
1907 zc.zc_cookie = flags;
1908 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1910 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1911 zcmd_free_nvlists(&zc);
1918 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1920 zcmd_free_nvlists(&zc);
1922 zpool_get_load_policy(config, &policy);
1929 * Dry-run failed, but we print out what success
1930 * looks like if we found a best txg
1932 if (policy.zlp_rewind & ZPOOL_TRY_REWIND) {
1933 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1939 if (newname == NULL)
1940 (void) snprintf(desc, sizeof (desc),
1941 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1944 (void) snprintf(desc, sizeof (desc),
1945 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1950 if (nv != NULL && nvlist_lookup_nvlist(nv,
1951 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1952 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1953 (void) printf(dgettext(TEXT_DOMAIN, "This "
1954 "pool uses the following feature(s) not "
1955 "supported by this system:\n"));
1956 zpool_print_unsup_feat(nv);
1957 if (nvlist_exists(nvinfo,
1958 ZPOOL_CONFIG_CAN_RDONLY)) {
1959 (void) printf(dgettext(TEXT_DOMAIN,
1960 "All unsupported features are only "
1961 "required for writing to the pool."
1962 "\nThe pool can be imported using "
1963 "'-o readonly=on'.\n"));
1967 * Unsupported version.
1969 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1973 if (nv != NULL && nvlist_lookup_nvlist(nv,
1974 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0) {
1975 char *hostname = "<unknown>";
1976 uint64_t hostid = 0;
1977 mmp_state_t mmp_state;
1979 mmp_state = fnvlist_lookup_uint64(nvinfo,
1980 ZPOOL_CONFIG_MMP_STATE);
1982 if (nvlist_exists(nvinfo,
1983 ZPOOL_CONFIG_MMP_HOSTNAME))
1984 hostname = fnvlist_lookup_string(nvinfo,
1985 ZPOOL_CONFIG_MMP_HOSTNAME);
1987 if (nvlist_exists(nvinfo,
1988 ZPOOL_CONFIG_MMP_HOSTID))
1989 hostid = fnvlist_lookup_uint64(nvinfo,
1990 ZPOOL_CONFIG_MMP_HOSTID);
1992 if (mmp_state == MMP_STATE_ACTIVE) {
1993 (void) snprintf(aux, sizeof (aux),
1994 dgettext(TEXT_DOMAIN, "pool is imp"
1995 "orted on host '%s' (hostid=%lx).\n"
1996 "Export the pool on the other "
1997 "system, then run 'zpool import'."),
1998 hostname, (unsigned long) hostid);
1999 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
2000 (void) snprintf(aux, sizeof (aux),
2001 dgettext(TEXT_DOMAIN, "pool has "
2002 "the multihost property on and "
2003 "the\nsystem's hostid is not set. "
2004 "Set a unique system hostid with "
2005 "the zgenhostid(8) command.\n"));
2008 (void) zfs_error_aux(hdl, aux);
2010 (void) zfs_error(hdl, EZFS_ACTIVE_POOL, desc);
2014 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
2018 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2019 "one or more devices is read only"));
2020 (void) zfs_error(hdl, EZFS_BADDEV, desc);
2024 if (nv && nvlist_lookup_nvlist(nv,
2025 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
2026 nvlist_lookup_nvlist(nvinfo,
2027 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
2028 (void) printf(dgettext(TEXT_DOMAIN,
2029 "The devices below are missing or "
2030 "corrupted, use '-m' to import the pool "
2032 print_vdev_tree(hdl, NULL, missing, 2);
2033 (void) printf("\n");
2035 (void) zpool_standard_error(hdl, error, desc);
2039 (void) zpool_standard_error(hdl, error, desc);
2043 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2044 "one or more devices are already in use\n"));
2045 (void) zfs_error(hdl, EZFS_BADDEV, desc);
2048 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2049 "new name of at least one dataset is longer than "
2050 "the maximum allowable length"));
2051 (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
2054 (void) zpool_standard_error(hdl, error, desc);
2055 zpool_explain_recover(hdl,
2056 newname ? origname : thename, -error, nv);
2063 zpool_handle_t *zhp;
2066 * This should never fail, but play it safe anyway.
2068 if (zpool_open_silent(hdl, thename, &zhp) != 0)
2070 else if (zhp != NULL)
2072 if (policy.zlp_rewind &
2073 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2074 zpool_rewind_exclaim(hdl, newname ? origname : thename,
2075 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0), nv);
2088 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd)
2090 zfs_cmd_t zc = {"\0"};
2093 libzfs_handle_t *hdl = zhp->zpool_hdl;
2095 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2096 zc.zc_cookie = func;
2099 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0)
2104 /* ECANCELED on a scrub means we resumed a paused scrub */
2105 if (err == ECANCELED && func == POOL_SCAN_SCRUB &&
2106 cmd == POOL_SCRUB_NORMAL)
2109 if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL)
2112 if (func == POOL_SCAN_SCRUB) {
2113 if (cmd == POOL_SCRUB_PAUSE) {
2114 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2115 "cannot pause scrubbing %s"), zc.zc_name);
2117 assert(cmd == POOL_SCRUB_NORMAL);
2118 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2119 "cannot scrub %s"), zc.zc_name);
2121 } else if (func == POOL_SCAN_NONE) {
2122 (void) snprintf(msg, sizeof (msg),
2123 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
2126 assert(!"unexpected result");
2131 pool_scan_stat_t *ps = NULL;
2134 verify(nvlist_lookup_nvlist(zhp->zpool_config,
2135 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2136 (void) nvlist_lookup_uint64_array(nvroot,
2137 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
2138 if (ps && ps->pss_func == POOL_SCAN_SCRUB) {
2139 if (cmd == POOL_SCRUB_PAUSE)
2140 return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg));
2142 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
2144 return (zfs_error(hdl, EZFS_RESILVERING, msg));
2146 } else if (err == ENOENT) {
2147 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
2149 return (zpool_standard_error(hdl, err, msg));
2154 * Find a vdev that matches the search criteria specified. We use the
2155 * the nvpair name to determine how we should look for the device.
2156 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2157 * spare; but FALSE if its an INUSE spare.
2160 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
2161 boolean_t *l2cache, boolean_t *log)
2168 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
2170 /* Nothing to look for */
2171 if (search == NULL || pair == NULL)
2174 /* Obtain the key we will use to search */
2175 srchkey = nvpair_name(pair);
2177 switch (nvpair_type(pair)) {
2178 case DATA_TYPE_UINT64:
2179 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
2180 uint64_t srchval, theguid;
2182 verify(nvpair_value_uint64(pair, &srchval) == 0);
2183 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2185 if (theguid == srchval)
2190 case DATA_TYPE_STRING: {
2191 char *srchval, *val;
2193 verify(nvpair_value_string(pair, &srchval) == 0);
2194 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2198 * Search for the requested value. Special cases:
2200 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2201 * "-part1", or "p1". The suffix is hidden from the user,
2202 * but included in the string, so this matches around it.
2203 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2204 * is used to check all possible expanded paths.
2205 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2207 * Otherwise, all other searches are simple string compares.
2209 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) {
2210 uint64_t wholedisk = 0;
2212 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2214 if (zfs_strcmp_pathname(srchval, val, wholedisk) == 0)
2217 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2218 char *type, *idx, *end, *p;
2219 uint64_t id, vdev_id;
2222 * Determine our vdev type, keeping in mind
2223 * that the srchval is composed of a type and
2224 * vdev id pair (i.e. mirror-4).
2226 if ((type = strdup(srchval)) == NULL)
2229 if ((p = strrchr(type, '-')) == NULL) {
2237 * If the types don't match then keep looking.
2239 if (strncmp(val, type, strlen(val)) != 0) {
2244 verify(zpool_vdev_is_interior(type));
2245 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2249 vdev_id = strtoull(idx, &end, 10);
2256 * Now verify that we have the correct vdev id.
2265 if (strcmp(srchval, val) == 0)
2274 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2275 &child, &children) != 0)
2278 for (c = 0; c < children; c++) {
2279 if ((ret = vdev_to_nvlist_iter(child[c], search,
2280 avail_spare, l2cache, NULL)) != NULL) {
2282 * The 'is_log' value is only set for the toplevel
2283 * vdev, not the leaf vdevs. So we always lookup the
2284 * log device from the root of the vdev tree (where
2285 * 'log' is non-NULL).
2288 nvlist_lookup_uint64(child[c],
2289 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2297 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2298 &child, &children) == 0) {
2299 for (c = 0; c < children; c++) {
2300 if ((ret = vdev_to_nvlist_iter(child[c], search,
2301 avail_spare, l2cache, NULL)) != NULL) {
2302 *avail_spare = B_TRUE;
2308 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2309 &child, &children) == 0) {
2310 for (c = 0; c < children; c++) {
2311 if ((ret = vdev_to_nvlist_iter(child[c], search,
2312 avail_spare, l2cache, NULL)) != NULL) {
2323 * Given a physical path or guid, find the associated vdev.
2326 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2327 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2329 nvlist_t *search, *nvroot, *ret;
2333 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2335 guid = strtoull(ppath, &end, 0);
2336 if (guid != 0 && *end == '\0') {
2337 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2339 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH,
2343 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2346 *avail_spare = B_FALSE;
2350 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2351 nvlist_free(search);
2357 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2360 zpool_vdev_is_interior(const char *name)
2362 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2363 strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 ||
2365 VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 ||
2366 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2372 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2373 boolean_t *l2cache, boolean_t *log)
2376 nvlist_t *nvroot, *search, *ret;
2379 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2381 guid = strtoull(path, &end, 0);
2382 if (guid != 0 && *end == '\0') {
2383 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2384 } else if (zpool_vdev_is_interior(path)) {
2385 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2387 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2390 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2393 *avail_spare = B_FALSE;
2397 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2398 nvlist_free(search);
2404 vdev_is_online(nvlist_t *nv)
2408 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2409 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2410 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2417 * Helper function for zpool_get_physpaths().
2420 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2421 size_t *bytes_written)
2423 size_t bytes_left, pos, rsz;
2427 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2429 return (EZFS_NODEVICE);
2431 pos = *bytes_written;
2432 bytes_left = physpath_size - pos;
2433 format = (pos == 0) ? "%s" : " %s";
2435 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2436 *bytes_written += rsz;
2438 if (rsz >= bytes_left) {
2439 /* if physpath was not copied properly, clear it */
2440 if (bytes_left != 0) {
2443 return (EZFS_NOSPC);
2449 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2450 size_t *rsz, boolean_t is_spare)
2455 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2456 return (EZFS_INVALCONFIG);
2458 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2460 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2461 * For a spare vdev, we only want to boot from the active
2466 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2469 return (EZFS_INVALCONFIG);
2472 if (vdev_is_online(nv)) {
2473 if ((ret = vdev_get_one_physpath(nv, physpath,
2474 phypath_size, rsz)) != 0)
2477 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2478 strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
2479 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2480 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2485 if (nvlist_lookup_nvlist_array(nv,
2486 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2487 return (EZFS_INVALCONFIG);
2489 for (i = 0; i < count; i++) {
2490 ret = vdev_get_physpaths(child[i], physpath,
2491 phypath_size, rsz, is_spare);
2492 if (ret == EZFS_NOSPC)
2497 return (EZFS_POOL_INVALARG);
2501 * Get phys_path for a root pool config.
2502 * Return 0 on success; non-zero on failure.
2505 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2508 nvlist_t *vdev_root;
2515 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2517 return (EZFS_INVALCONFIG);
2519 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2520 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2521 &child, &count) != 0)
2522 return (EZFS_INVALCONFIG);
2525 * root pool can only have a single top-level vdev.
2527 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2528 return (EZFS_POOL_INVALARG);
2530 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2533 /* No online devices */
2535 return (EZFS_NODEVICE);
2541 * Get phys_path for a root pool
2542 * Return 0 on success; non-zero on failure.
2545 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2547 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2552 * If the device has being dynamically expanded then we need to relabel
2553 * the disk to use the new unallocated space.
2556 zpool_relabel_disk(libzfs_handle_t *hdl, const char *path, const char *msg)
2560 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
2561 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2562 "relabel '%s': unable to open device: %d"), path, errno);
2563 return (zfs_error(hdl, EZFS_OPENFAILED, msg));
2567 * It's possible that we might encounter an error if the device
2568 * does not have any unallocated space left. If so, we simply
2569 * ignore that error and continue on.
2571 * Also, we don't call efi_rescan() - that would just return EBUSY.
2572 * The module will do it for us in vdev_disk_open().
2574 error = efi_use_whole_disk(fd);
2576 /* Flush the buffers to disk and invalidate the page cache. */
2578 (void) ioctl(fd, BLKFLSBUF);
2581 if (error && error != VT_ENOSPC) {
2582 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2583 "relabel '%s': unable to read disk capacity"), path);
2584 return (zfs_error(hdl, EZFS_NOCAP, msg));
2591 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
2593 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
2594 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
2598 zpool_vdev_path_to_guid_impl(zpool_handle_t *zhp, const char *path,
2599 boolean_t *is_spare, boolean_t *is_l2cache, boolean_t *is_log)
2602 boolean_t spare = B_FALSE, l2cache = B_FALSE, log = B_FALSE;
2605 if ((tgt = zpool_find_vdev(zhp, path, &spare, &l2cache,
2609 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &guid) == 0);
2610 if (is_spare != NULL)
2612 if (is_l2cache != NULL)
2613 *is_l2cache = l2cache;
2620 /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
2622 zpool_vdev_path_to_guid(zpool_handle_t *zhp, const char *path)
2624 return (zpool_vdev_path_to_guid_impl(zhp, path, NULL, NULL, NULL));
2628 * Bring the specified vdev online. The 'flags' parameter is a set of the
2629 * ZFS_ONLINE_* flags.
2632 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2633 vdev_state_t *newstate)
2635 zfs_cmd_t zc = {"\0"};
2639 boolean_t avail_spare, l2cache, islog;
2640 libzfs_handle_t *hdl = zhp->zpool_hdl;
2643 if (flags & ZFS_ONLINE_EXPAND) {
2644 (void) snprintf(msg, sizeof (msg),
2645 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2647 (void) snprintf(msg, sizeof (msg),
2648 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2651 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2652 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2654 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2656 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2659 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2661 if ((flags & ZFS_ONLINE_EXPAND ||
2662 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) &&
2663 nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) {
2664 uint64_t wholedisk = 0;
2666 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2670 * XXX - L2ARC 1.0 devices can't support expansion.
2673 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2674 "cannot expand cache devices"));
2675 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2679 const char *fullpath = path;
2680 char buf[MAXPATHLEN];
2682 if (path[0] != '/') {
2683 error = zfs_resolve_shortname(path, buf,
2686 return (zfs_error(hdl, EZFS_NODEVICE,
2692 error = zpool_relabel_disk(hdl, fullpath, msg);
2698 zc.zc_cookie = VDEV_STATE_ONLINE;
2701 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2702 if (errno == EINVAL) {
2703 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2704 "from this pool into a new one. Use '%s' "
2705 "instead"), "zpool detach");
2706 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2708 return (zpool_standard_error(hdl, errno, msg));
2711 *newstate = zc.zc_cookie;
2716 * Take the specified vdev offline
2719 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2721 zfs_cmd_t zc = {"\0"};
2724 boolean_t avail_spare, l2cache;
2725 libzfs_handle_t *hdl = zhp->zpool_hdl;
2727 (void) snprintf(msg, sizeof (msg),
2728 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2730 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2731 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2733 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2735 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2738 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2740 zc.zc_cookie = VDEV_STATE_OFFLINE;
2741 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2743 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2750 * There are no other replicas of this device.
2752 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2756 * The log device has unplayed logs
2758 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2761 return (zpool_standard_error(hdl, errno, msg));
2766 * Mark the given vdev faulted.
2769 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2771 zfs_cmd_t zc = {"\0"};
2773 libzfs_handle_t *hdl = zhp->zpool_hdl;
2775 (void) snprintf(msg, sizeof (msg),
2776 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
2778 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2780 zc.zc_cookie = VDEV_STATE_FAULTED;
2783 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2790 * There are no other replicas of this device.
2792 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2795 return (zpool_standard_error(hdl, errno, msg));
2801 * Mark the given vdev degraded.
2804 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2806 zfs_cmd_t zc = {"\0"};
2808 libzfs_handle_t *hdl = zhp->zpool_hdl;
2810 (void) snprintf(msg, sizeof (msg),
2811 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid);
2813 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2815 zc.zc_cookie = VDEV_STATE_DEGRADED;
2818 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2821 return (zpool_standard_error(hdl, errno, msg));
2825 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2829 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2835 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2837 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2840 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2841 children == 2 && child[which] == tgt)
2844 for (c = 0; c < children; c++)
2845 if (is_replacing_spare(child[c], tgt, which))
2853 * Attach new_disk (fully described by nvroot) to old_disk.
2854 * If 'replacing' is specified, the new disk will replace the old one.
2857 zpool_vdev_attach(zpool_handle_t *zhp,
2858 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2860 zfs_cmd_t zc = {"\0"};
2864 boolean_t avail_spare, l2cache, islog;
2869 nvlist_t *config_root;
2870 libzfs_handle_t *hdl = zhp->zpool_hdl;
2871 boolean_t rootpool = zpool_is_bootable(zhp);
2874 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2875 "cannot replace %s with %s"), old_disk, new_disk);
2877 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2878 "cannot attach %s to %s"), new_disk, old_disk);
2880 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2881 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2883 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2886 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2889 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2891 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2892 zc.zc_cookie = replacing;
2894 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2895 &child, &children) != 0 || children != 1) {
2896 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2897 "new device must be a single disk"));
2898 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2901 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2902 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2904 if ((newname = zpool_vdev_name(NULL, NULL, child[0], 0)) == NULL)
2908 * If the target is a hot spare that has been swapped in, we can only
2909 * replace it with another hot spare.
2912 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2913 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2914 NULL) == NULL || !avail_spare) &&
2915 is_replacing_spare(config_root, tgt, 1)) {
2916 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2917 "can only be replaced by another hot spare"));
2919 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2924 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2927 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2929 zcmd_free_nvlists(&zc);
2934 * XXX need a better way to prevent user from
2935 * booting up a half-baked vdev.
2937 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2938 "sure to wait until resilver is done "
2939 "before rebooting.\n"));
2947 * Can't attach to or replace this type of vdev.
2950 uint64_t version = zpool_get_prop_int(zhp,
2951 ZPOOL_PROP_VERSION, NULL);
2954 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2955 "cannot replace a log with a spare"));
2956 else if (version >= SPA_VERSION_MULTI_REPLACE)
2957 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2958 "already in replacing/spare config; wait "
2959 "for completion or use 'zpool detach'"));
2961 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2962 "cannot replace a replacing device"));
2964 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2965 "can only attach to mirrors and top-level "
2968 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2973 * The new device must be a single disk.
2975 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2976 "new device must be a single disk"));
2977 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2981 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy, "
2982 "or device removal is in progress"),
2984 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2989 * The new device is too small.
2991 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2992 "device is too small"));
2993 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2998 * The new device has a different optimal sector size.
3000 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3001 "new device has a different optimal sector size; use the "
3002 "option '-o ashift=N' to override the optimal size"));
3003 (void) zfs_error(hdl, EZFS_BADDEV, msg);
3008 * The resulting top-level vdev spec won't fit in the label.
3010 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
3014 (void) zpool_standard_error(hdl, errno, msg);
3021 * Detach the specified device.
3024 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
3026 zfs_cmd_t zc = {"\0"};
3029 boolean_t avail_spare, l2cache;
3030 libzfs_handle_t *hdl = zhp->zpool_hdl;
3032 (void) snprintf(msg, sizeof (msg),
3033 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
3035 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3036 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3038 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3041 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3044 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
3046 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3048 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
3055 * Can't detach from this type of vdev.
3057 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
3058 "applicable to mirror and replacing vdevs"));
3059 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
3064 * There are no other replicas of this device.
3066 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
3070 (void) zpool_standard_error(hdl, errno, msg);
3077 * Find a mirror vdev in the source nvlist.
3079 * The mchild array contains a list of disks in one of the top-level mirrors
3080 * of the source pool. The schild array contains a list of disks that the
3081 * user specified on the command line. We loop over the mchild array to
3082 * see if any entry in the schild array matches.
3084 * If a disk in the mchild array is found in the schild array, we return
3085 * the index of that entry. Otherwise we return -1.
3088 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
3089 nvlist_t **schild, uint_t schildren)
3093 for (mc = 0; mc < mchildren; mc++) {
3095 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3098 for (sc = 0; sc < schildren; sc++) {
3099 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3101 boolean_t result = (strcmp(mpath, spath) == 0);
3117 * Split a mirror pool. If newroot points to null, then a new nvlist
3118 * is generated and it is the responsibility of the caller to free it.
3121 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
3122 nvlist_t *props, splitflags_t flags)
3124 zfs_cmd_t zc = {"\0"};
3126 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
3127 nvlist_t **varray = NULL, *zc_props = NULL;
3128 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
3129 libzfs_handle_t *hdl = zhp->zpool_hdl;
3130 uint64_t vers, readonly = B_FALSE;
3131 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
3134 (void) snprintf(msg, sizeof (msg),
3135 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
3137 if (!zpool_name_valid(hdl, B_FALSE, newname))
3138 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
3140 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
3141 (void) fprintf(stderr, gettext("Internal error: unable to "
3142 "retrieve pool configuration\n"));
3146 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
3148 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
3151 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
3152 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
3153 props, vers, flags, msg)) == NULL)
3155 (void) nvlist_lookup_uint64(zc_props,
3156 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3158 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3159 "property %s can only be set at import time"),
3160 zpool_prop_to_name(ZPOOL_PROP_READONLY));
3165 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3167 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3168 "Source pool is missing vdev tree"));
3169 nvlist_free(zc_props);
3173 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
3176 if (*newroot == NULL ||
3177 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
3178 &newchild, &newchildren) != 0)
3181 for (c = 0; c < children; c++) {
3182 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
3184 nvlist_t **mchild, *vdev;
3189 * Unlike cache & spares, slogs are stored in the
3190 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3192 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3194 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
3196 if (is_log || is_hole) {
3198 * Create a hole vdev and put it in the config.
3200 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
3202 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
3203 VDEV_TYPE_HOLE) != 0)
3205 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
3210 varray[vcount++] = vdev;
3214 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
3216 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
3217 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3218 "Source pool must be composed only of mirrors\n"));
3219 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3223 verify(nvlist_lookup_nvlist_array(child[c],
3224 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3226 /* find or add an entry for this top-level vdev */
3227 if (newchildren > 0 &&
3228 (entry = find_vdev_entry(zhp, mchild, mchildren,
3229 newchild, newchildren)) >= 0) {
3230 /* We found a disk that the user specified. */
3231 vdev = mchild[entry];
3234 /* User didn't specify a disk for this vdev. */
3235 vdev = mchild[mchildren - 1];
3238 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3242 /* did we find every disk the user specified? */
3243 if (found != newchildren) {
3244 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3245 "include at most one disk from each mirror"));
3246 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3250 /* Prepare the nvlist for populating. */
3251 if (*newroot == NULL) {
3252 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3255 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3256 VDEV_TYPE_ROOT) != 0)
3259 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3262 /* Add all the children we found */
3263 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3264 lastlog == 0 ? vcount : lastlog) != 0)
3268 * If we're just doing a dry run, exit now with success.
3271 memory_err = B_FALSE;
3276 /* now build up the config list & call the ioctl */
3277 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3280 if (nvlist_add_nvlist(newconfig,
3281 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3282 nvlist_add_string(newconfig,
3283 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3284 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3288 * The new pool is automatically part of the namespace unless we
3289 * explicitly export it.
3292 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3293 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3294 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3295 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3297 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3300 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3301 retval = zpool_standard_error(hdl, errno, msg);
3306 memory_err = B_FALSE;
3309 if (varray != NULL) {
3312 for (v = 0; v < vcount; v++)
3313 nvlist_free(varray[v]);
3316 zcmd_free_nvlists(&zc);
3317 nvlist_free(zc_props);
3318 nvlist_free(newconfig);
3320 nvlist_free(*newroot);
3328 return (no_memory(hdl));
3334 * Remove the given device.
3337 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3339 zfs_cmd_t zc = {"\0"};
3342 boolean_t avail_spare, l2cache, islog;
3343 libzfs_handle_t *hdl = zhp->zpool_hdl;
3346 (void) snprintf(msg, sizeof (msg),
3347 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3349 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3350 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3352 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3354 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3355 if (islog && version < SPA_VERSION_HOLES) {
3356 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3357 "pool must be upgraded to support log removal"));
3358 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3361 if (!islog && !avail_spare && !l2cache && zpool_is_bootable(zhp)) {
3362 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3363 "root pool can not have removed devices, "
3364 "because GRUB does not understand them"));
3365 return (zfs_error(hdl, EINVAL, msg));
3368 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3370 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3376 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3377 "invalid config; all top-level vdevs must "
3378 "have the same sector size and not be raidz."));
3379 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3384 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3385 "Mount encrypted datasets to replay logs."));
3387 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3388 "Pool busy; removal may already be in progress"));
3390 (void) zfs_error(hdl, EZFS_BUSY, msg);
3395 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3396 "Mount encrypted datasets to replay logs."));
3397 (void) zfs_error(hdl, EZFS_BUSY, msg);
3399 (void) zpool_standard_error(hdl, errno, msg);
3404 (void) zpool_standard_error(hdl, errno, msg);
3410 zpool_vdev_remove_cancel(zpool_handle_t *zhp)
3414 libzfs_handle_t *hdl = zhp->zpool_hdl;
3416 (void) snprintf(msg, sizeof (msg),
3417 dgettext(TEXT_DOMAIN, "cannot cancel removal"));
3419 bzero(&zc, sizeof (zc));
3420 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3423 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3426 return (zpool_standard_error(hdl, errno, msg));
3430 zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path,
3435 boolean_t avail_spare, l2cache, islog;
3436 libzfs_handle_t *hdl = zhp->zpool_hdl;
3438 (void) snprintf(msg, sizeof (msg),
3439 dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"),
3442 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3444 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3446 if (avail_spare || l2cache || islog) {
3451 if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) {
3452 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3453 "indirect size not available"));
3454 return (zfs_error(hdl, EINVAL, msg));
3460 * Clear the errors for the pool, or the particular device if specified.
3463 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3465 zfs_cmd_t zc = {"\0"};
3468 zpool_load_policy_t policy;
3469 boolean_t avail_spare, l2cache;
3470 libzfs_handle_t *hdl = zhp->zpool_hdl;
3471 nvlist_t *nvi = NULL;
3475 (void) snprintf(msg, sizeof (msg),
3476 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3479 (void) snprintf(msg, sizeof (msg),
3480 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3483 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3485 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3486 &l2cache, NULL)) == NULL)
3487 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3490 * Don't allow error clearing for hot spares. Do allow
3491 * error clearing for l2cache devices.
3494 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3496 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3500 zpool_get_load_policy(rewindnvl, &policy);
3501 zc.zc_cookie = policy.zlp_rewind;
3503 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3506 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3509 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3511 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3512 zcmd_free_nvlists(&zc);
3517 if (!error || ((policy.zlp_rewind & ZPOOL_TRY_REWIND) &&
3518 errno != EPERM && errno != EACCES)) {
3519 if (policy.zlp_rewind &
3520 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3521 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3522 zpool_rewind_exclaim(hdl, zc.zc_name,
3523 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0),
3527 zcmd_free_nvlists(&zc);
3531 zcmd_free_nvlists(&zc);
3532 return (zpool_standard_error(hdl, errno, msg));
3536 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3539 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3541 zfs_cmd_t zc = {"\0"};
3543 libzfs_handle_t *hdl = zhp->zpool_hdl;
3545 (void) snprintf(msg, sizeof (msg),
3546 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3547 (u_longlong_t)guid);
3549 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3551 zc.zc_cookie = ZPOOL_NO_REWIND;
3553 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3556 return (zpool_standard_error(hdl, errno, msg));
3560 * Change the GUID for a pool.
3563 zpool_reguid(zpool_handle_t *zhp)
3566 libzfs_handle_t *hdl = zhp->zpool_hdl;
3567 zfs_cmd_t zc = {"\0"};
3569 (void) snprintf(msg, sizeof (msg),
3570 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3572 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3573 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3576 return (zpool_standard_error(hdl, errno, msg));
3583 zpool_reopen_one(zpool_handle_t *zhp, void *data)
3585 libzfs_handle_t *hdl = zpool_get_handle(zhp);
3586 const char *pool_name = zpool_get_name(zhp);
3587 boolean_t *scrub_restart = data;
3590 error = lzc_reopen(pool_name, *scrub_restart);
3592 return (zpool_standard_error_fmt(hdl, error,
3593 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"), pool_name));
3599 /* call into libzfs_core to execute the sync IOCTL per pool */
3601 zpool_sync_one(zpool_handle_t *zhp, void *data)
3604 libzfs_handle_t *hdl = zpool_get_handle(zhp);
3605 const char *pool_name = zpool_get_name(zhp);
3606 boolean_t *force = data;
3607 nvlist_t *innvl = fnvlist_alloc();
3609 fnvlist_add_boolean_value(innvl, "force", *force);
3610 if ((ret = lzc_sync(pool_name, innvl, NULL)) != 0) {
3612 return (zpool_standard_error_fmt(hdl, ret,
3613 dgettext(TEXT_DOMAIN, "sync '%s' failed"), pool_name));
3620 #if defined(__sun__) || defined(__sun)
3622 * Convert from a devid string to a path.
3625 devid_to_path(char *devid_str)
3630 devid_nmlist_t *list = NULL;
3633 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3636 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3638 devid_str_free(minor);
3645 * In a case the strdup() fails, we will just return NULL below.
3647 path = strdup(list[0].devname);
3649 devid_free_nmlist(list);
3655 * Convert from a path to a devid string.
3658 path_to_devid(const char *path)
3664 if ((fd = open(path, O_RDONLY)) < 0)
3669 if (devid_get(fd, &devid) == 0) {
3670 if (devid_get_minor_name(fd, &minor) == 0)
3671 ret = devid_str_encode(devid, minor);
3673 devid_str_free(minor);
3682 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3683 * ignore any failure here, since a common case is for an unprivileged user to
3684 * type 'zpool status', and we'll display the correct information anyway.
3687 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3689 zfs_cmd_t zc = {"\0"};
3691 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3692 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3693 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3696 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3701 * Remove partition suffix from a vdev path. Partition suffixes may take three
3702 * forms: "-partX", "pX", or "X", where X is a string of digits. The second
3703 * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
3704 * third case only occurs when preceded by a string matching the regular
3705 * expression "^([hsv]|xv)d[a-z]+", i.e. a scsi, ide, virtio or xen disk.
3707 * caller must free the returned string
3710 zfs_strip_partition(char *path)
3712 char *tmp = strdup(path);
3713 char *part = NULL, *d = NULL;
3717 if ((part = strstr(tmp, "-part")) && part != tmp) {
3719 } else if ((part = strrchr(tmp, 'p')) &&
3720 part > tmp + 1 && isdigit(*(part-1))) {
3722 } else if ((tmp[0] == 'h' || tmp[0] == 's' || tmp[0] == 'v') &&
3724 for (d = &tmp[2]; isalpha(*d); part = ++d) { }
3725 } else if (strncmp("xvd", tmp, 3) == 0) {
3726 for (d = &tmp[3]; isalpha(*d); part = ++d) { }
3728 if (part && d && *d != '\0') {
3729 for (; isdigit(*d); d++) { }
3738 * Same as zfs_strip_partition, but allows "/dev/" to be in the pathname
3743 * Returned string must be freed.
3746 zfs_strip_partition_path(char *path)
3748 char *newpath = strdup(path);
3755 /* Point to "sda1" part of "/dev/sda1" */
3756 sd_offset = strrchr(newpath, '/') + 1;
3758 /* Get our new name "sda" */
3759 new_sd = zfs_strip_partition(sd_offset);
3765 /* Paste the "sda" where "sda1" was */
3766 strlcpy(sd_offset, new_sd, strlen(sd_offset) + 1);
3768 /* Free temporary "sda" */
3774 #define PATH_BUF_LEN 64
3777 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3778 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3779 * We also check if this is a whole disk, in which case we strip off the
3780 * trailing 's0' slice name.
3782 * This routine is also responsible for identifying when disks have been
3783 * reconfigured in a new location. The kernel will have opened the device by
3784 * devid, but the path will still refer to the old location. To catch this, we
3785 * first do a path -> devid translation (which is fast for the common case). If
3786 * the devid matches, we're done. If not, we do a reverse devid -> path
3787 * translation and issue the appropriate ioctl() to update the path of the vdev.
3788 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3792 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3795 char *path, *type, *env;
3797 char buf[PATH_BUF_LEN];
3798 char tmpbuf[PATH_BUF_LEN];
3801 * vdev_name will be "root"/"root-0" for the root vdev, but it is the
3802 * zpool name that will be displayed to the user.
3804 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
3805 if (zhp != NULL && strcmp(type, "root") == 0)
3806 return (zfs_strdup(hdl, zpool_get_name(zhp)));
3808 env = getenv("ZPOOL_VDEV_NAME_PATH");
3809 if (env && (strtoul(env, NULL, 0) > 0 ||
3810 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3811 name_flags |= VDEV_NAME_PATH;
3813 env = getenv("ZPOOL_VDEV_NAME_GUID");
3814 if (env && (strtoul(env, NULL, 0) > 0 ||
3815 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3816 name_flags |= VDEV_NAME_GUID;
3818 env = getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
3819 if (env && (strtoul(env, NULL, 0) > 0 ||
3820 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3821 name_flags |= VDEV_NAME_FOLLOW_LINKS;
3823 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3824 name_flags & VDEV_NAME_GUID) {
3825 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value);
3826 (void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)value);
3828 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3829 #if defined(__sun__) || defined(__sun)
3831 * Live VDEV path updates to a kernel VDEV during a
3832 * zpool_vdev_name lookup are not supported on Linux.
3839 * If the device is dead (faulted, offline, etc) then don't
3840 * bother opening it. Otherwise we may be forcing the user to
3841 * open a misbehaving device, which can have undesirable
3844 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3845 (uint64_t **)&vs, &vsc) != 0 ||
3846 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3848 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3850 * Determine if the current path is correct.
3852 char *newdevid = path_to_devid(path);
3854 if (newdevid == NULL ||
3855 strcmp(devid, newdevid) != 0) {
3858 if ((newpath = devid_to_path(devid)) != NULL) {
3860 * Update the path appropriately.
3862 set_path(zhp, nv, newpath);
3863 if (nvlist_add_string(nv,
3864 ZPOOL_CONFIG_PATH, newpath) == 0)
3865 verify(nvlist_lookup_string(nv,
3873 devid_str_free(newdevid);
3877 if (name_flags & VDEV_NAME_FOLLOW_LINKS) {
3878 char *rp = realpath(path, NULL);
3880 strlcpy(buf, rp, sizeof (buf));
3887 * For a block device only use the name.
3889 if ((strcmp(type, VDEV_TYPE_DISK) == 0) &&
3890 !(name_flags & VDEV_NAME_PATH)) {
3891 path = strrchr(path, '/');
3896 * Remove the partition from the path it this is a whole disk.
3898 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, &value)
3899 == 0 && value && !(name_flags & VDEV_NAME_PATH)) {
3900 return (zfs_strip_partition(path));
3906 * If it's a raidz device, we need to stick in the parity level.
3908 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3909 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3911 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3912 (u_longlong_t)value);
3917 * We identify each top-level vdev by using a <type-id>
3918 * naming convention.
3920 if (name_flags & VDEV_NAME_TYPE_ID) {
3922 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3924 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s-%llu",
3925 path, (u_longlong_t)id);
3930 return (zfs_strdup(hdl, path));
3934 zbookmark_mem_compare(const void *a, const void *b)
3936 return (memcmp(a, b, sizeof (zbookmark_phys_t)));
3940 * Retrieve the persistent error log, uniquify the members, and return to the
3944 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3946 zfs_cmd_t zc = {"\0"};
3947 libzfs_handle_t *hdl = zhp->zpool_hdl;
3949 zbookmark_phys_t *zb = NULL;
3953 * Retrieve the raw error list from the kernel. If the number of errors
3954 * has increased, allocate more space and continue until we get the
3957 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3961 zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3962 count * sizeof (zbookmark_phys_t));
3963 zc.zc_nvlist_dst_size = count;
3964 (void) strcpy(zc.zc_name, zhp->zpool_name);
3966 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3968 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3969 if (errno == ENOMEM) {
3972 count = zc.zc_nvlist_dst_size;
3973 dst = zfs_alloc(zhp->zpool_hdl, count *
3974 sizeof (zbookmark_phys_t));
3975 zc.zc_nvlist_dst = (uintptr_t)dst;
3977 return (zpool_standard_error_fmt(hdl, errno,
3978 dgettext(TEXT_DOMAIN, "errors: List of "
3979 "errors unavailable")));
3987 * Sort the resulting bookmarks. This is a little confusing due to the
3988 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3989 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3990 * _not_ copied as part of the process. So we point the start of our
3991 * array appropriate and decrement the total number of elements.
3993 zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
3994 zc.zc_nvlist_dst_size;
3995 count -= zc.zc_nvlist_dst_size;
3997 qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
3999 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
4002 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
4004 for (i = 0; i < count; i++) {
4007 /* ignoring zb_blkid and zb_level for now */
4008 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
4009 zb[i-1].zb_object == zb[i].zb_object)
4012 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
4014 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
4015 zb[i].zb_objset) != 0) {
4019 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
4020 zb[i].zb_object) != 0) {
4024 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
4031 free((void *)(uintptr_t)zc.zc_nvlist_dst);
4035 free((void *)(uintptr_t)zc.zc_nvlist_dst);
4036 return (no_memory(zhp->zpool_hdl));
4040 * Upgrade a ZFS pool to the latest on-disk version.
4043 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
4045 zfs_cmd_t zc = {"\0"};
4046 libzfs_handle_t *hdl = zhp->zpool_hdl;
4048 (void) strcpy(zc.zc_name, zhp->zpool_name);
4049 zc.zc_cookie = new_version;
4051 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
4052 return (zpool_standard_error_fmt(hdl, errno,
4053 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
4059 zfs_save_arguments(int argc, char **argv, char *string, int len)
4063 (void) strlcpy(string, basename(argv[0]), len);
4064 for (i = 1; i < argc; i++) {
4065 (void) strlcat(string, " ", len);
4066 (void) strlcat(string, argv[i], len);
4071 zpool_log_history(libzfs_handle_t *hdl, const char *message)
4073 zfs_cmd_t zc = {"\0"};
4077 args = fnvlist_alloc();
4078 fnvlist_add_string(args, "message", message);
4079 err = zcmd_write_src_nvlist(hdl, &zc, args);
4081 err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
4083 zcmd_free_nvlists(&zc);
4088 * Perform ioctl to get some command history of a pool.
4090 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
4091 * logical offset of the history buffer to start reading from.
4093 * Upon return, 'off' is the next logical offset to read from and
4094 * 'len' is the actual amount of bytes read into 'buf'.
4097 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
4099 zfs_cmd_t zc = {"\0"};
4100 libzfs_handle_t *hdl = zhp->zpool_hdl;
4102 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4104 zc.zc_history = (uint64_t)(uintptr_t)buf;
4105 zc.zc_history_len = *len;
4106 zc.zc_history_offset = *off;
4108 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
4111 return (zfs_error_fmt(hdl, EZFS_PERM,
4112 dgettext(TEXT_DOMAIN,
4113 "cannot show history for pool '%s'"),
4116 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
4117 dgettext(TEXT_DOMAIN, "cannot get history for pool "
4118 "'%s'"), zhp->zpool_name));
4120 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
4121 dgettext(TEXT_DOMAIN, "cannot get history for pool "
4122 "'%s', pool must be upgraded"), zhp->zpool_name));
4124 return (zpool_standard_error_fmt(hdl, errno,
4125 dgettext(TEXT_DOMAIN,
4126 "cannot get history for '%s'"), zhp->zpool_name));
4130 *len = zc.zc_history_len;
4131 *off = zc.zc_history_offset;
4137 * Process the buffer of nvlists, unpacking and storing each nvlist record
4138 * into 'records'. 'leftover' is set to the number of bytes that weren't
4139 * processed as there wasn't a complete record.
4142 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
4143 nvlist_t ***records, uint_t *numrecords)
4150 while (bytes_read > sizeof (reclen)) {
4152 /* get length of packed record (stored as little endian) */
4153 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
4154 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
4156 if (bytes_read < sizeof (reclen) + reclen)
4160 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
4162 bytes_read -= sizeof (reclen) + reclen;
4163 buf += sizeof (reclen) + reclen;
4165 /* add record to nvlist array */
4167 if (ISP2(*numrecords + 1)) {
4168 tmp = realloc(*records,
4169 *numrecords * 2 * sizeof (nvlist_t *));
4177 (*records)[*numrecords - 1] = nv;
4180 *leftover = bytes_read;
4185 * Retrieve the command history of a pool.
4188 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
4191 int buflen = 128 * 1024;
4193 nvlist_t **records = NULL;
4194 uint_t numrecords = 0;
4197 buf = malloc(buflen);
4201 uint64_t bytes_read = buflen;
4204 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
4207 /* if nothing else was read in, we're at EOF, just return */
4211 if ((err = zpool_history_unpack(buf, bytes_read,
4212 &leftover, &records, &numrecords)) != 0)
4215 if (leftover == bytes_read) {
4217 * no progress made, because buffer is not big enough
4218 * to hold this record; resize and retry.
4222 buf = malloc(buflen);
4233 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
4234 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
4235 records, numrecords) == 0);
4237 for (i = 0; i < numrecords; i++)
4238 nvlist_free(records[i]);
4245 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
4246 * If there is a new event available 'nvp' will contain a newly allocated
4247 * nvlist and 'dropped' will be set to the number of missed events since
4248 * the last call to this function. When 'nvp' is set to NULL it indicates
4249 * no new events are available. In either case the function returns 0 and
4250 * it is up to the caller to free 'nvp'. In the case of a fatal error the
4251 * function will return a non-zero value. When the function is called in
4252 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
4253 * it will not return until a new event is available.
4256 zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
4257 int *dropped, unsigned flags, int zevent_fd)
4259 zfs_cmd_t zc = {"\0"};
4264 zc.zc_cleanup_fd = zevent_fd;
4266 if (flags & ZEVENT_NONBLOCK)
4267 zc.zc_guid = ZEVENT_NONBLOCK;
4269 if (zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE) != 0)
4273 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
4276 error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
4277 dgettext(TEXT_DOMAIN, "zfs shutdown"));
4280 /* Blocking error case should not occur */
4281 if (!(flags & ZEVENT_NONBLOCK))
4282 error = zpool_standard_error_fmt(hdl, errno,
4283 dgettext(TEXT_DOMAIN, "cannot get event"));
4287 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
4288 error = zfs_error_fmt(hdl, EZFS_NOMEM,
4289 dgettext(TEXT_DOMAIN, "cannot get event"));
4295 error = zpool_standard_error_fmt(hdl, errno,
4296 dgettext(TEXT_DOMAIN, "cannot get event"));
4301 error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
4305 *dropped = (int)zc.zc_cookie;
4307 zcmd_free_nvlists(&zc);
4316 zpool_events_clear(libzfs_handle_t *hdl, int *count)
4318 zfs_cmd_t zc = {"\0"};
4321 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
4322 "cannot clear events"));
4324 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
4325 return (zpool_standard_error_fmt(hdl, errno, msg));
4328 *count = (int)zc.zc_cookie; /* # of events cleared */
4334 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4335 * the passed zevent_fd file handle. On success zero is returned,
4336 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4339 zpool_events_seek(libzfs_handle_t *hdl, uint64_t eid, int zevent_fd)
4341 zfs_cmd_t zc = {"\0"};
4345 zc.zc_cleanup_fd = zevent_fd;
4347 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_SEEK, &zc) != 0) {
4350 error = zfs_error_fmt(hdl, EZFS_NOENT,
4351 dgettext(TEXT_DOMAIN, "cannot get event"));
4355 error = zfs_error_fmt(hdl, EZFS_NOMEM,
4356 dgettext(TEXT_DOMAIN, "cannot get event"));
4360 error = zpool_standard_error_fmt(hdl, errno,
4361 dgettext(TEXT_DOMAIN, "cannot get event"));
4370 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4371 char *pathname, size_t len)
4373 zfs_cmd_t zc = {"\0"};
4374 boolean_t mounted = B_FALSE;
4375 char *mntpnt = NULL;
4376 char dsname[ZFS_MAX_DATASET_NAME_LEN];
4379 /* special case for the MOS */
4380 (void) snprintf(pathname, len, "<metadata>:<0x%llx>",
4385 /* get the dataset's name */
4386 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4388 if (ioctl(zhp->zpool_hdl->libzfs_fd,
4389 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
4390 /* just write out a path of two object numbers */
4391 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
4392 (longlong_t)dsobj, (longlong_t)obj);
4395 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
4397 /* find out if the dataset is mounted */
4398 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
4400 /* get the corrupted object's path */
4401 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
4403 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
4406 (void) snprintf(pathname, len, "%s%s", mntpnt,
4409 (void) snprintf(pathname, len, "%s:%s",
4410 dsname, zc.zc_value);
4413 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname,
4420 * Read the EFI label from the config, if a label does not exist then
4421 * pass back the error to the caller. If the caller has passed a non-NULL
4422 * diskaddr argument then we set it to the starting address of the EFI
4426 read_efi_label(nvlist_t *config, diskaddr_t *sb)
4430 char diskname[MAXPATHLEN];
4433 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
4436 (void) snprintf(diskname, sizeof (diskname), "%s%s", DISK_ROOT,
4437 strrchr(path, '/'));
4438 if ((fd = open(diskname, O_RDONLY|O_DIRECT)) >= 0) {
4439 struct dk_gpt *vtoc;
4441 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
4443 *sb = vtoc->efi_parts[0].p_start;
4452 * determine where a partition starts on a disk in the current
4456 find_start_block(nvlist_t *config)
4460 diskaddr_t sb = MAXOFFSET_T;
4463 if (nvlist_lookup_nvlist_array(config,
4464 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
4465 if (nvlist_lookup_uint64(config,
4466 ZPOOL_CONFIG_WHOLE_DISK,
4467 &wholedisk) != 0 || !wholedisk) {
4468 return (MAXOFFSET_T);
4470 if (read_efi_label(config, &sb) < 0)
4475 for (c = 0; c < children; c++) {
4476 sb = find_start_block(child[c]);
4477 if (sb != MAXOFFSET_T) {
4481 return (MAXOFFSET_T);
4485 zpool_label_disk_check(char *path)
4487 struct dk_gpt *vtoc;
4490 if ((fd = open(path, O_RDONLY|O_DIRECT)) < 0)
4493 if ((err = efi_alloc_and_read(fd, &vtoc)) != 0) {
4498 if (vtoc->efi_flags & EFI_GPT_PRIMARY_CORRUPT) {
4510 * Generate a unique partition name for the ZFS member. Partitions must
4511 * have unique names to ensure udev will be able to create symlinks under
4512 * /dev/disk/by-partlabel/ for all pool members. The partition names are
4513 * of the form <pool>-<unique-id>.
4516 zpool_label_name(char *label_name, int label_size)
4521 fd = open("/dev/urandom", O_RDONLY);
4523 if (read(fd, &id, sizeof (id)) != sizeof (id))
4530 id = (((uint64_t)rand()) << 32) | (uint64_t)rand();
4532 snprintf(label_name, label_size, "zfs-%016llx", (u_longlong_t)id);
4536 * Label an individual disk. The name provided is the short name,
4537 * stripped of any leading /dev path.
4540 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
4542 char path[MAXPATHLEN];
4543 struct dk_gpt *vtoc;
4545 size_t resv = EFI_MIN_RESV_SIZE;
4546 uint64_t slice_size;
4547 diskaddr_t start_block;
4550 /* prepare an error message just in case */
4551 (void) snprintf(errbuf, sizeof (errbuf),
4552 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
4557 verify(nvlist_lookup_nvlist(zhp->zpool_config,
4558 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4560 if (zhp->zpool_start_block == 0)
4561 start_block = find_start_block(nvroot);
4563 start_block = zhp->zpool_start_block;
4564 zhp->zpool_start_block = start_block;
4567 start_block = NEW_START_BLOCK;
4570 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
4572 if ((fd = open(path, O_RDWR|O_DIRECT|O_EXCL)) < 0) {
4574 * This shouldn't happen. We've long since verified that this
4575 * is a valid device.
4577 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
4578 "label '%s': unable to open device: %d"), path, errno);
4579 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
4582 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
4584 * The only way this can fail is if we run out of memory, or we
4585 * were unable to read the disk's capacity
4587 if (errno == ENOMEM)
4588 (void) no_memory(hdl);
4591 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
4592 "label '%s': unable to read disk capacity"), path);
4594 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
4597 slice_size = vtoc->efi_last_u_lba + 1;
4598 slice_size -= EFI_MIN_RESV_SIZE;
4599 if (start_block == MAXOFFSET_T)
4600 start_block = NEW_START_BLOCK;
4601 slice_size -= start_block;
4602 slice_size = P2ALIGN(slice_size, PARTITION_END_ALIGNMENT);
4604 vtoc->efi_parts[0].p_start = start_block;
4605 vtoc->efi_parts[0].p_size = slice_size;
4608 * Why we use V_USR: V_BACKUP confuses users, and is considered
4609 * disposable by some EFI utilities (since EFI doesn't have a backup
4610 * slice). V_UNASSIGNED is supposed to be used only for zero size
4611 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4612 * etc. were all pretty specific. V_USR is as close to reality as we
4613 * can get, in the absence of V_OTHER.
4615 vtoc->efi_parts[0].p_tag = V_USR;
4616 zpool_label_name(vtoc->efi_parts[0].p_name, EFI_PART_NAME_LEN);
4618 vtoc->efi_parts[8].p_start = slice_size + start_block;
4619 vtoc->efi_parts[8].p_size = resv;
4620 vtoc->efi_parts[8].p_tag = V_RESERVED;
4622 rval = efi_write(fd, vtoc);
4624 /* Flush the buffers to disk and invalidate the page cache. */
4626 (void) ioctl(fd, BLKFLSBUF);
4629 rval = efi_rescan(fd);
4632 * Some block drivers (like pcata) may not support EFI GPT labels.
4633 * Print out a helpful error message directing the user to manually
4634 * label the disk and give a specific slice.
4640 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "try using "
4641 "parted(8) and then provide a specific slice: %d"), rval);
4642 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4648 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
4649 (void) zfs_append_partition(path, MAXPATHLEN);
4651 /* Wait to udev to signal use the device has settled. */
4652 rval = zpool_label_disk_wait(path, DISK_LABEL_WAIT);
4654 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "failed to "
4655 "detect device partitions on '%s': %d"), path, rval);
4656 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4659 /* We can't be to paranoid. Read the label back and verify it. */
4660 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
4661 rval = zpool_label_disk_check(path);
4663 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "freshly written "
4664 "EFI label on '%s' is damaged. Ensure\nthis device "
4665 "is not in in use, and is functioning properly: %d"),
4667 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4674 * Allocate and return the underlying device name for a device mapper device.
4675 * If a device mapper device maps to multiple devices, return the first device.
4677 * For example, dm_name = "/dev/dm-0" could return "/dev/sda". Symlinks to a
4678 * DM device (like /dev/disk/by-vdev/A0) are also allowed.
4680 * Returns device name, or NULL on error or no match. If dm_name is not a DM
4681 * device then return NULL.
4683 * NOTE: The returned name string must be *freed*.
4686 dm_get_underlying_path(char *dm_name)
4696 if (dm_name == NULL)
4699 /* dm name may be a symlink (like /dev/disk/by-vdev/A0) */
4700 realp = realpath(dm_name, NULL);
4705 * If they preface 'dev' with a path (like "/dev") then strip it off.
4706 * We just want the 'dm-N' part.
4708 tmp = strrchr(realp, '/');
4710 dev_str = tmp + 1; /* +1 since we want the chr after '/' */
4714 size = asprintf(&tmp, "/sys/block/%s/slaves/", dev_str);
4715 if (size == -1 || !tmp)
4722 /* Return first sd* entry in /sys/block/dm-N/slaves/ */
4723 while ((ep = readdir(dp))) {
4724 if (ep->d_type != DT_DIR) { /* skip "." and ".." dirs */
4725 size = asprintf(&path, "/dev/%s", ep->d_name);
4739 * Return 1 if device is a device mapper or multipath device.
4743 zfs_dev_is_dm(char *dev_name)
4747 tmp = dm_get_underlying_path(dev_name);
4756 * By "whole disk" we mean an entire physical disk (something we can
4757 * label, toggle the write cache on, etc.) as opposed to the full
4758 * capacity of a pseudo-device such as lofi or did. We act as if we
4759 * are labeling the disk, which should be a pretty good test of whether
4760 * it's a viable device or not. Returns B_TRUE if it is and B_FALSE if
4764 zfs_dev_is_whole_disk(char *dev_name)
4766 struct dk_gpt *label;
4769 if ((fd = open(dev_name, O_RDONLY | O_DIRECT)) < 0)
4772 if (efi_alloc_and_init(fd, EFI_NUMPAR, &label) != 0) {
4784 * Lookup the underlying device for a device name
4786 * Often you'll have a symlink to a device, a partition device,
4787 * or a multipath device, and want to look up the underlying device.
4788 * This function returns the underlying device name. If the device
4789 * name is already the underlying device, then just return the same
4790 * name. If the device is a DM device with multiple underlying devices
4791 * then return the first one.
4795 * 1. /dev/disk/by-id/ata-QEMU_HARDDISK_QM00001 -> ../../sda
4796 * dev_name: /dev/disk/by-id/ata-QEMU_HARDDISK_QM00001
4799 * 2. /dev/mapper/mpatha (made up of /dev/sda and /dev/sdb)
4800 * dev_name: /dev/mapper/mpatha
4801 * returns: /dev/sda (first device)
4803 * 3. /dev/sda (already the underlying device)
4804 * dev_name: /dev/sda
4807 * 4. /dev/dm-3 (mapped to /dev/sda)
4808 * dev_name: /dev/dm-3
4811 * 5. /dev/disk/by-id/scsi-0QEMU_drive-scsi0-0-0-0-part9 -> ../../sdb9
4812 * dev_name: /dev/disk/by-id/scsi-0QEMU_drive-scsi0-0-0-0-part9
4815 * 6. /dev/disk/by-uuid/5df030cf-3cd9-46e4-8e99-3ccb462a4e9a -> ../dev/sda2
4816 * dev_name: /dev/disk/by-uuid/5df030cf-3cd9-46e4-8e99-3ccb462a4e9a
4819 * Returns underlying device name, or NULL on error or no match.
4821 * NOTE: The returned name string must be *freed*.
4824 zfs_get_underlying_path(char *dev_name)
4829 if (dev_name == NULL)
4832 tmp = dm_get_underlying_path(dev_name);
4834 /* dev_name not a DM device, so just un-symlinkize it */
4836 tmp = realpath(dev_name, NULL);
4839 name = zfs_strip_partition_path(tmp);
4847 * Given a dev name like "sda", return the full enclosure sysfs path to
4848 * the disk. You can also pass in the name with "/dev" prepended
4849 * to it (like /dev/sda).
4851 * For example, disk "sda" in enclosure slot 1:
4853 * returns: "/sys/class/enclosure/1:0:3:0/Slot 1"
4855 * 'dev' must be a non-devicemapper device.
4857 * Returned string must be freed.
4860 zfs_get_enclosure_sysfs_path(char *dev_name)
4864 char buf[MAXPATHLEN];
4872 if (dev_name == NULL)
4875 /* If they preface 'dev' with a path (like "/dev") then strip it off */
4876 tmp1 = strrchr(dev_name, '/');
4878 dev_name = tmp1 + 1; /* +1 since we want the chr after '/' */
4880 tmpsize = asprintf(&tmp1, "/sys/block/%s/device", dev_name);
4881 if (tmpsize == -1 || tmp1 == NULL) {
4888 tmp1 = NULL; /* To make free() at the end a NOP */
4893 * Look though all sysfs entries in /sys/block/<dev>/device for
4894 * the enclosure symlink.
4896 while ((ep = readdir(dp))) {
4897 /* Ignore everything that's not our enclosure_device link */
4898 if (strstr(ep->d_name, "enclosure_device") == NULL)
4901 if (asprintf(&tmp2, "%s/%s", tmp1, ep->d_name) == -1 ||
4905 size = readlink(tmp2, buf, sizeof (buf));
4907 /* Did readlink fail or crop the link name? */
4908 if (size == -1 || size >= sizeof (buf)) {
4910 tmp2 = NULL; /* To make free() at the end a NOP */
4915 * We got a valid link. readlink() doesn't terminate strings
4916 * so we have to do it.
4921 * Our link will look like:
4923 * "../../../../port-11:1:2/..STUFF../enclosure/1:0:3:0/SLOT 1"
4925 * We want to grab the "enclosure/1:0:3:0/SLOT 1" part
4927 tmp3 = strstr(buf, "enclosure");
4931 if (asprintf(&path, "/sys/class/%s", tmp3) == -1) {
4932 /* If asprintf() fails, 'path' is undefined */