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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
39 #include <sys/efi_partition.h>
41 #include <sys/zfs_ioctl.h>
44 #include "zfs_namecheck.h"
46 #include "libzfs_impl.h"
47 #include "zfs_comutil.h"
49 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
51 typedef struct prop_flags {
52 int create:1; /* Validate property on creation */
53 int import:1; /* Validate property on import */
57 * ====================================================================
58 * zpool property functions
59 * ====================================================================
63 zpool_get_all_props(zpool_handle_t *zhp)
65 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
66 libzfs_handle_t *hdl = zhp->zpool_hdl;
68 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
70 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
73 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
74 if (errno == ENOMEM) {
75 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
76 zcmd_free_nvlists(&zc);
80 zcmd_free_nvlists(&zc);
85 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
86 zcmd_free_nvlists(&zc);
90 zcmd_free_nvlists(&zc);
96 zpool_props_refresh(zpool_handle_t *zhp)
100 old_props = zhp->zpool_props;
102 if (zpool_get_all_props(zhp) != 0)
105 nvlist_free(old_props);
110 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
116 zprop_source_t source;
118 nvl = zhp->zpool_props;
119 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
120 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
122 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
124 source = ZPROP_SRC_DEFAULT;
125 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
136 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
140 zprop_source_t source;
142 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
144 * zpool_get_all_props() has most likely failed because
145 * the pool is faulted, but if all we need is the top level
146 * vdev's guid then get it from the zhp config nvlist.
148 if ((prop == ZPOOL_PROP_GUID) &&
149 (nvlist_lookup_nvlist(zhp->zpool_config,
150 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
151 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
155 return (zpool_prop_default_numeric(prop));
158 nvl = zhp->zpool_props;
159 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
160 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
162 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
164 source = ZPROP_SRC_DEFAULT;
165 value = zpool_prop_default_numeric(prop);
175 * Map VDEV STATE to printed strings.
178 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
183 case VDEV_STATE_CLOSED:
184 case VDEV_STATE_OFFLINE:
185 return (gettext("OFFLINE"));
186 case VDEV_STATE_REMOVED:
187 return (gettext("REMOVED"));
188 case VDEV_STATE_CANT_OPEN:
189 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
190 return (gettext("FAULTED"));
191 else if (aux == VDEV_AUX_SPLIT_POOL)
192 return (gettext("SPLIT"));
194 return (gettext("UNAVAIL"));
195 case VDEV_STATE_FAULTED:
196 return (gettext("FAULTED"));
197 case VDEV_STATE_DEGRADED:
198 return (gettext("DEGRADED"));
199 case VDEV_STATE_HEALTHY:
200 return (gettext("ONLINE"));
203 return (gettext("UNKNOWN"));
207 * Get a zpool property value for 'prop' and return the value in
208 * a pre-allocated buffer.
211 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
212 zprop_source_t *srctype)
216 zprop_source_t src = ZPROP_SRC_NONE;
221 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
223 case ZPOOL_PROP_NAME:
224 (void) strlcpy(buf, zpool_get_name(zhp), len);
227 case ZPOOL_PROP_HEALTH:
228 (void) strlcpy(buf, "FAULTED", len);
231 case ZPOOL_PROP_GUID:
232 intval = zpool_get_prop_int(zhp, prop, &src);
233 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
236 case ZPOOL_PROP_ALTROOT:
237 case ZPOOL_PROP_CACHEFILE:
238 case ZPOOL_PROP_COMMENT:
239 if (zhp->zpool_props != NULL ||
240 zpool_get_all_props(zhp) == 0) {
242 zpool_get_prop_string(zhp, prop, &src),
250 (void) strlcpy(buf, "-", len);
259 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
260 prop != ZPOOL_PROP_NAME)
263 switch (zpool_prop_get_type(prop)) {
264 case PROP_TYPE_STRING:
265 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
269 case PROP_TYPE_NUMBER:
270 intval = zpool_get_prop_int(zhp, prop, &src);
273 case ZPOOL_PROP_SIZE:
274 case ZPOOL_PROP_ALLOCATED:
275 case ZPOOL_PROP_FREE:
276 case ZPOOL_PROP_EXPANDSZ:
277 case ZPOOL_PROP_ASHIFT:
278 (void) zfs_nicenum(intval, buf, len);
281 case ZPOOL_PROP_CAPACITY:
282 (void) snprintf(buf, len, "%llu%%",
283 (u_longlong_t)intval);
286 case ZPOOL_PROP_DEDUPRATIO:
287 (void) snprintf(buf, len, "%llu.%02llux",
288 (u_longlong_t)(intval / 100),
289 (u_longlong_t)(intval % 100));
292 case ZPOOL_PROP_HEALTH:
293 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
294 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
295 verify(nvlist_lookup_uint64_array(nvroot,
296 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
299 (void) strlcpy(buf, zpool_state_to_name(intval,
303 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
307 case PROP_TYPE_INDEX:
308 intval = zpool_get_prop_int(zhp, prop, &src);
309 if (zpool_prop_index_to_string(prop, intval, &strval)
312 (void) strlcpy(buf, strval, len);
326 * Check if the bootfs name has the same pool name as it is set to.
327 * Assuming bootfs is a valid dataset name.
330 bootfs_name_valid(const char *pool, char *bootfs)
332 int len = strlen(pool);
334 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
337 if (strncmp(pool, bootfs, len) == 0 &&
338 (bootfs[len] == '/' || bootfs[len] == '\0'))
344 #if defined(__sun__) || defined(__sun)
346 * Inspect the configuration to determine if any of the devices contain
350 pool_uses_efi(nvlist_t *config)
355 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
356 &child, &children) != 0)
357 return (read_efi_label(config, NULL) >= 0);
359 for (c = 0; c < children; c++) {
360 if (pool_uses_efi(child[c]))
368 zpool_is_bootable(zpool_handle_t *zhp)
370 char bootfs[ZPOOL_MAXNAMELEN];
372 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
373 sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
374 sizeof (bootfs)) != 0);
379 * Given an nvlist of zpool properties to be set, validate that they are
380 * correct, and parse any numeric properties (index, boolean, etc) if they are
381 * specified as strings.
384 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
385 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
393 struct stat64 statbuf;
397 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
398 (void) no_memory(hdl);
403 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
404 const char *propname = nvpair_name(elem);
407 * Make sure this property is valid and applies to this type.
409 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
410 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
411 "invalid property '%s'"), propname);
412 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
416 if (zpool_prop_readonly(prop)) {
417 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
418 "is readonly"), propname);
419 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
423 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
424 &strval, &intval, errbuf) != 0)
428 * Perform additional checking for specific properties.
433 case ZPOOL_PROP_VERSION:
434 if (intval < version || intval > SPA_VERSION) {
435 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
436 "property '%s' number %d is invalid."),
438 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
443 case ZPOOL_PROP_ASHIFT:
445 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
446 "property '%s' can only be set at "
447 "creation time"), propname);
448 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
452 if (intval != 0 && (intval < 9 || intval > 13)) {
453 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
454 "property '%s' number %d is invalid."),
456 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
461 case ZPOOL_PROP_BOOTFS:
462 if (flags.create || flags.import) {
463 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
464 "property '%s' cannot be set at creation "
465 "or import time"), propname);
466 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
470 if (version < SPA_VERSION_BOOTFS) {
471 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
472 "pool must be upgraded to support "
473 "'%s' property"), propname);
474 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
479 * bootfs property value has to be a dataset name and
480 * the dataset has to be in the same pool as it sets to.
482 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
484 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
485 "is an invalid name"), strval);
486 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
490 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
491 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
492 "could not open pool '%s'"), poolname);
493 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
496 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
497 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
499 #if defined(__sun__) || defined(__sun)
501 * bootfs property cannot be set on a disk which has
504 if (pool_uses_efi(nvroot)) {
505 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
506 "property '%s' not supported on "
507 "EFI labeled devices"), propname);
508 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
516 case ZPOOL_PROP_ALTROOT:
517 if (!flags.create && !flags.import) {
518 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
519 "property '%s' can only be set during pool "
520 "creation or import"), propname);
521 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
525 if (strval[0] != '/') {
526 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
527 "bad alternate root '%s'"), strval);
528 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
533 case ZPOOL_PROP_CACHEFILE:
534 if (strval[0] == '\0')
537 if (strcmp(strval, "none") == 0)
540 if (strval[0] != '/') {
541 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
542 "property '%s' must be empty, an "
543 "absolute path, or 'none'"), propname);
544 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
548 slash = strrchr(strval, '/');
550 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
551 strcmp(slash, "/..") == 0) {
552 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
553 "'%s' is not a valid file"), strval);
554 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
560 if (strval[0] != '\0' &&
561 (stat64(strval, &statbuf) != 0 ||
562 !S_ISDIR(statbuf.st_mode))) {
563 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
564 "'%s' is not a valid directory"),
566 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
573 case ZPOOL_PROP_COMMENT:
574 for (check = strval; *check != '\0'; check++) {
575 if (!isprint(*check)) {
577 dgettext(TEXT_DOMAIN,
578 "comment may only have printable "
580 (void) zfs_error(hdl, EZFS_BADPROP,
585 if (strlen(strval) > ZPROP_MAX_COMMENT) {
586 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
587 "comment must not exceed %d characters"),
589 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
593 case ZPOOL_PROP_READONLY:
595 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
596 "property '%s' can only be set at "
597 "import time"), propname);
598 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
607 nvlist_free(retprops);
612 * Set zpool property : propname=propval.
615 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
617 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
620 nvlist_t *nvl = NULL;
623 prop_flags_t flags = { 0 };
625 (void) snprintf(errbuf, sizeof (errbuf),
626 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
629 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
630 return (no_memory(zhp->zpool_hdl));
632 if (nvlist_add_string(nvl, propname, propval) != 0) {
634 return (no_memory(zhp->zpool_hdl));
637 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
638 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
639 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
648 * Execute the corresponding ioctl() to set this property.
650 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
652 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
657 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
659 zcmd_free_nvlists(&zc);
663 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
665 (void) zpool_props_refresh(zhp);
671 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
673 libzfs_handle_t *hdl = zhp->zpool_hdl;
675 char buf[ZFS_MAXPROPLEN];
677 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
680 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
685 if (entry->pl_prop != ZPROP_INVAL &&
686 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
688 if (strlen(buf) > entry->pl_width)
689 entry->pl_width = strlen(buf);
698 * Don't start the slice at the default block of 34; many storage
699 * devices will use a stripe width of 128k, other vendors prefer a 1m
700 * alignment. It is best to play it safe and ensure a 1m alignment
701 * given 512B blocks. When the block size is larger by a power of 2
702 * we will still be 1m aligned. Some devices are sensitive to the
703 * partition ending alignment as well.
705 #define NEW_START_BLOCK 2048
706 #define PARTITION_END_ALIGNMENT 2048
709 * Validate the given pool name, optionally putting an extended error message in
713 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
719 ret = pool_namecheck(pool, &why, &what);
722 * The rules for reserved pool names were extended at a later point.
723 * But we need to support users with existing pools that may now be
724 * invalid. So we only check for this expanded set of names during a
725 * create (or import), and only in userland.
727 if (ret == 0 && !isopen &&
728 (strncmp(pool, "mirror", 6) == 0 ||
729 strncmp(pool, "raidz", 5) == 0 ||
730 strncmp(pool, "spare", 5) == 0 ||
731 strcmp(pool, "log") == 0)) {
734 dgettext(TEXT_DOMAIN, "name is reserved"));
742 case NAME_ERR_TOOLONG:
744 dgettext(TEXT_DOMAIN, "name is too long"));
747 case NAME_ERR_INVALCHAR:
749 dgettext(TEXT_DOMAIN, "invalid character "
750 "'%c' in pool name"), what);
753 case NAME_ERR_NOLETTER:
754 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
755 "name must begin with a letter"));
758 case NAME_ERR_RESERVED:
759 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
760 "name is reserved"));
763 case NAME_ERR_DISKLIKE:
764 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
765 "pool name is reserved"));
768 case NAME_ERR_LEADING_SLASH:
769 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
770 "leading slash in name"));
773 case NAME_ERR_EMPTY_COMPONENT:
774 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
775 "empty component in name"));
778 case NAME_ERR_TRAILING_SLASH:
779 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
780 "trailing slash in name"));
783 case NAME_ERR_MULTIPLE_AT:
784 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
785 "multiple '@' delimiters in name"));
788 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
789 "permission set is missing '@'"));
800 * Open a handle to the given pool, even if the pool is currently in the FAULTED
804 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
810 * Make sure the pool name is valid.
812 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
813 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
814 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
819 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
822 zhp->zpool_hdl = hdl;
823 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
825 if (zpool_refresh_stats(zhp, &missing) != 0) {
831 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
832 (void) zfs_error_fmt(hdl, EZFS_NOENT,
833 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
842 * Like the above, but silent on error. Used when iterating over pools (because
843 * the configuration cache may be out of date).
846 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
851 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
854 zhp->zpool_hdl = hdl;
855 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
857 if (zpool_refresh_stats(zhp, &missing) != 0) {
873 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
877 zpool_open(libzfs_handle_t *hdl, const char *pool)
881 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
884 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
885 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
886 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
895 * Close the handle. Simply frees the memory associated with the handle.
898 zpool_close(zpool_handle_t *zhp)
900 if (zhp->zpool_config)
901 nvlist_free(zhp->zpool_config);
902 if (zhp->zpool_old_config)
903 nvlist_free(zhp->zpool_old_config);
904 if (zhp->zpool_props)
905 nvlist_free(zhp->zpool_props);
910 * Return the name of the pool.
913 zpool_get_name(zpool_handle_t *zhp)
915 return (zhp->zpool_name);
920 * Return the state of the pool (ACTIVE or UNAVAILABLE)
923 zpool_get_state(zpool_handle_t *zhp)
925 return (zhp->zpool_state);
929 * Create the named pool, using the provided vdev list. It is assumed
930 * that the consumer has already validated the contents of the nvlist, so we
931 * don't have to worry about error semantics.
934 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
935 nvlist_t *props, nvlist_t *fsprops)
937 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
938 nvlist_t *zc_fsprops = NULL;
939 nvlist_t *zc_props = NULL;
944 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
945 "cannot create '%s'"), pool);
947 if (!zpool_name_valid(hdl, B_FALSE, pool))
948 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
950 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
954 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
956 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
957 SPA_VERSION_1, flags, msg)) == NULL) {
966 zoned = ((nvlist_lookup_string(fsprops,
967 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
968 strcmp(zonestr, "on") == 0);
970 if ((zc_fsprops = zfs_valid_proplist(hdl,
971 ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
975 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
978 if (nvlist_add_nvlist(zc_props,
979 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
984 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
987 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
989 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
991 zcmd_free_nvlists(&zc);
992 nvlist_free(zc_props);
993 nvlist_free(zc_fsprops);
998 * This can happen if the user has specified the same
999 * device multiple times. We can't reliably detect this
1000 * until we try to add it and see we already have a
1001 * label. This can also happen under if the device is
1002 * part of an active md or lvm device.
1004 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1005 "one or more vdevs refer to the same device, or one of\n"
1006 "the devices is part of an active md or lvm device"));
1007 return (zfs_error(hdl, EZFS_BADDEV, msg));
1011 * This occurs when one of the devices is below
1012 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1013 * device was the problem device since there's no
1014 * reliable way to determine device size from userland.
1019 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1021 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1022 "one or more devices is less than the "
1023 "minimum size (%s)"), buf);
1025 return (zfs_error(hdl, EZFS_BADDEV, msg));
1028 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1029 "one or more devices is out of space"));
1030 return (zfs_error(hdl, EZFS_BADDEV, msg));
1033 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1034 "cache device must be a disk or disk slice"));
1035 return (zfs_error(hdl, EZFS_BADDEV, msg));
1038 return (zpool_standard_error(hdl, errno, msg));
1043 * If this is an alternate root pool, then we automatically set the
1044 * mountpoint of the root dataset to be '/'.
1046 if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
1050 verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
1051 verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1058 zcmd_free_nvlists(&zc);
1059 nvlist_free(zc_props);
1060 nvlist_free(zc_fsprops);
1065 * Destroy the given pool. It is up to the caller to ensure that there are no
1066 * datasets left in the pool.
1069 zpool_destroy(zpool_handle_t *zhp)
1071 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1072 zfs_handle_t *zfp = NULL;
1073 libzfs_handle_t *hdl = zhp->zpool_hdl;
1076 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1077 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1080 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1082 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1083 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1084 "cannot destroy '%s'"), zhp->zpool_name);
1086 if (errno == EROFS) {
1087 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1088 "one or more devices is read only"));
1089 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1091 (void) zpool_standard_error(hdl, errno, msg);
1100 remove_mountpoint(zfp);
1108 * Add the given vdevs to the pool. The caller must have already performed the
1109 * necessary verification to ensure that the vdev specification is well-formed.
1112 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1114 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1116 libzfs_handle_t *hdl = zhp->zpool_hdl;
1118 nvlist_t **spares, **l2cache;
1119 uint_t nspares, nl2cache;
1121 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1122 "cannot add to '%s'"), zhp->zpool_name);
1124 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1125 SPA_VERSION_SPARES &&
1126 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1127 &spares, &nspares) == 0) {
1128 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1129 "upgraded to add hot spares"));
1130 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1133 #if defined(__sun__) || defined(__sun)
1134 if (zpool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1135 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1138 for (s = 0; s < nspares; s++) {
1141 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1142 &path) == 0 && pool_uses_efi(spares[s])) {
1143 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1144 "device '%s' contains an EFI label and "
1145 "cannot be used on root pools."),
1146 zpool_vdev_name(hdl, NULL, spares[s],
1148 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1154 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1155 SPA_VERSION_L2CACHE &&
1156 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1157 &l2cache, &nl2cache) == 0) {
1158 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1159 "upgraded to add cache devices"));
1160 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1163 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1165 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1167 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1171 * This can happen if the user has specified the same
1172 * device multiple times. We can't reliably detect this
1173 * until we try to add it and see we already have a
1176 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1177 "one or more vdevs refer to the same device"));
1178 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1183 * This occurrs when one of the devices is below
1184 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1185 * device was the problem device since there's no
1186 * reliable way to determine device size from userland.
1191 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1193 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1194 "device is less than the minimum "
1197 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1201 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1202 "pool must be upgraded to add these vdevs"));
1203 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1207 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1208 "root pool can not have multiple vdevs"
1209 " or separate logs"));
1210 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1214 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1215 "cache device must be a disk or disk slice"));
1216 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1220 (void) zpool_standard_error(hdl, errno, msg);
1228 zcmd_free_nvlists(&zc);
1234 * Exports the pool from the system. The caller must ensure that there are no
1235 * mounted datasets in the pool.
1238 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1240 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1243 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1244 "cannot export '%s'"), zhp->zpool_name);
1246 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1247 zc.zc_cookie = force;
1248 zc.zc_guid = hardforce;
1250 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1253 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1254 "use '-f' to override the following errors:\n"
1255 "'%s' has an active shared spare which could be"
1256 " used by other pools once '%s' is exported."),
1257 zhp->zpool_name, zhp->zpool_name);
1258 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1261 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1270 zpool_export(zpool_handle_t *zhp, boolean_t force)
1272 return (zpool_export_common(zhp, force, B_FALSE));
1276 zpool_export_force(zpool_handle_t *zhp)
1278 return (zpool_export_common(zhp, B_TRUE, B_TRUE));
1282 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1285 nvlist_t *nv = NULL;
1291 if (!hdl->libzfs_printerr || config == NULL)
1294 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0)
1297 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1299 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1301 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1302 strftime(timestr, 128, "%c", &t) != 0) {
1304 (void) printf(dgettext(TEXT_DOMAIN,
1305 "Would be able to return %s "
1306 "to its state as of %s.\n"),
1309 (void) printf(dgettext(TEXT_DOMAIN,
1310 "Pool %s returned to its state as of %s.\n"),
1314 (void) printf(dgettext(TEXT_DOMAIN,
1315 "%s approximately %lld "),
1316 dryrun ? "Would discard" : "Discarded",
1317 ((longlong_t)loss + 30) / 60);
1318 (void) printf(dgettext(TEXT_DOMAIN,
1319 "minutes of transactions.\n"));
1320 } else if (loss > 0) {
1321 (void) printf(dgettext(TEXT_DOMAIN,
1322 "%s approximately %lld "),
1323 dryrun ? "Would discard" : "Discarded",
1325 (void) printf(dgettext(TEXT_DOMAIN,
1326 "seconds of transactions.\n"));
1332 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1335 nvlist_t *nv = NULL;
1337 uint64_t edata = UINT64_MAX;
1342 if (!hdl->libzfs_printerr)
1346 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1348 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1350 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1351 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1352 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1355 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1356 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1359 (void) printf(dgettext(TEXT_DOMAIN,
1360 "Recovery is possible, but will result in some data loss.\n"));
1362 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1363 strftime(timestr, 128, "%c", &t) != 0) {
1364 (void) printf(dgettext(TEXT_DOMAIN,
1365 "\tReturning the pool to its state as of %s\n"
1366 "\tshould correct the problem. "),
1369 (void) printf(dgettext(TEXT_DOMAIN,
1370 "\tReverting the pool to an earlier state "
1371 "should correct the problem.\n\t"));
1375 (void) printf(dgettext(TEXT_DOMAIN,
1376 "Approximately %lld minutes of data\n"
1377 "\tmust be discarded, irreversibly. "),
1378 ((longlong_t)loss + 30) / 60);
1379 } else if (loss > 0) {
1380 (void) printf(dgettext(TEXT_DOMAIN,
1381 "Approximately %lld seconds of data\n"
1382 "\tmust be discarded, irreversibly. "),
1385 if (edata != 0 && edata != UINT64_MAX) {
1387 (void) printf(dgettext(TEXT_DOMAIN,
1388 "After rewind, at least\n"
1389 "\tone persistent user-data error will remain. "));
1391 (void) printf(dgettext(TEXT_DOMAIN,
1392 "After rewind, several\n"
1393 "\tpersistent user-data errors will remain. "));
1396 (void) printf(dgettext(TEXT_DOMAIN,
1397 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1398 reason >= 0 ? "clear" : "import", name);
1400 (void) printf(dgettext(TEXT_DOMAIN,
1401 "A scrub of the pool\n"
1402 "\tis strongly recommended after recovery.\n"));
1406 (void) printf(dgettext(TEXT_DOMAIN,
1407 "Destroy and re-create the pool from\n\ta backup source.\n"));
1411 * zpool_import() is a contracted interface. Should be kept the same
1414 * Applications should use zpool_import_props() to import a pool with
1415 * new properties value to be set.
1418 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1421 nvlist_t *props = NULL;
1424 if (altroot != NULL) {
1425 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1426 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1427 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1431 if (nvlist_add_string(props,
1432 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1433 nvlist_add_string(props,
1434 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1436 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1437 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1442 ret = zpool_import_props(hdl, config, newname, props,
1450 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1456 uint64_t is_log = 0;
1458 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1462 (void) printf("\t%*s%s%s\n", indent, "", name,
1463 is_log ? " [log]" : "");
1465 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1466 &child, &children) != 0)
1469 for (c = 0; c < children; c++) {
1470 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
1471 print_vdev_tree(hdl, vname, child[c], indent + 2);
1477 * Import the given pool using the known configuration and a list of
1478 * properties to be set. The configuration should have come from
1479 * zpool_find_import(). The 'newname' parameters control whether the pool
1480 * is imported with a different name.
1483 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1484 nvlist_t *props, int flags)
1486 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1487 zpool_rewind_policy_t policy;
1488 nvlist_t *nv = NULL;
1489 nvlist_t *nvinfo = NULL;
1490 nvlist_t *missing = NULL;
1497 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1500 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1501 "cannot import pool '%s'"), origname);
1503 if (newname != NULL) {
1504 if (!zpool_name_valid(hdl, B_FALSE, newname))
1505 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1506 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1508 thename = (char *)newname;
1515 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1517 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1520 if ((props = zpool_valid_proplist(hdl, origname,
1521 props, version, flags, errbuf)) == NULL) {
1523 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1529 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1531 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1534 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1538 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1543 zc.zc_cookie = flags;
1544 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1546 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1547 zcmd_free_nvlists(&zc);
1554 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1555 zpool_get_rewind_policy(config, &policy);
1561 * Dry-run failed, but we print out what success
1562 * looks like if we found a best txg
1564 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1565 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1571 if (newname == NULL)
1572 (void) snprintf(desc, sizeof (desc),
1573 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1576 (void) snprintf(desc, sizeof (desc),
1577 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1583 * Unsupported version.
1585 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1589 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1593 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1594 "one or more devices is read only"));
1595 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1599 if (nv && nvlist_lookup_nvlist(nv,
1600 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1601 nvlist_lookup_nvlist(nvinfo,
1602 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1603 (void) printf(dgettext(TEXT_DOMAIN,
1604 "The devices below are missing, use "
1605 "'-m' to import the pool anyway:\n"));
1606 print_vdev_tree(hdl, NULL, missing, 2);
1607 (void) printf("\n");
1609 (void) zpool_standard_error(hdl, error, desc);
1613 (void) zpool_standard_error(hdl, error, desc);
1617 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1618 "one or more devices are already in use\n"));
1619 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1623 (void) zpool_standard_error(hdl, error, desc);
1624 zpool_explain_recover(hdl,
1625 newname ? origname : thename, -error, nv);
1632 zpool_handle_t *zhp;
1635 * This should never fail, but play it safe anyway.
1637 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1639 else if (zhp != NULL)
1641 if (policy.zrp_request &
1642 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1643 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1644 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1650 zcmd_free_nvlists(&zc);
1660 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1662 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1664 libzfs_handle_t *hdl = zhp->zpool_hdl;
1666 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1667 zc.zc_cookie = func;
1669 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
1670 (errno == ENOENT && func != POOL_SCAN_NONE))
1673 if (func == POOL_SCAN_SCRUB) {
1674 (void) snprintf(msg, sizeof (msg),
1675 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1676 } else if (func == POOL_SCAN_NONE) {
1677 (void) snprintf(msg, sizeof (msg),
1678 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1681 assert(!"unexpected result");
1684 if (errno == EBUSY) {
1686 pool_scan_stat_t *ps = NULL;
1689 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1690 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1691 (void) nvlist_lookup_uint64_array(nvroot,
1692 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1693 if (ps && ps->pss_func == POOL_SCAN_SCRUB)
1694 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1696 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1697 } else if (errno == ENOENT) {
1698 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1700 return (zpool_standard_error(hdl, errno, msg));
1705 * Find a vdev that matches the search criteria specified. We use the
1706 * the nvpair name to determine how we should look for the device.
1707 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1708 * spare; but FALSE if its an INUSE spare.
1711 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1712 boolean_t *l2cache, boolean_t *log)
1719 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1721 /* Nothing to look for */
1722 if (search == NULL || pair == NULL)
1725 /* Obtain the key we will use to search */
1726 srchkey = nvpair_name(pair);
1728 switch (nvpair_type(pair)) {
1729 case DATA_TYPE_UINT64:
1730 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1731 uint64_t srchval, theguid;
1733 verify(nvpair_value_uint64(pair, &srchval) == 0);
1734 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1736 if (theguid == srchval)
1741 case DATA_TYPE_STRING: {
1742 char *srchval, *val;
1744 verify(nvpair_value_string(pair, &srchval) == 0);
1745 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1749 * Search for the requested value. Special cases:
1751 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
1752 * "-part1", or "p1". The suffix is hidden from the user,
1753 * but included in the string, so this matches around it.
1754 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
1755 * is used to check all possible expanded paths.
1756 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1758 * Otherwise, all other searches are simple string compares.
1760 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) {
1761 uint64_t wholedisk = 0;
1763 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1765 if (zfs_strcmp_pathname(srchval, val, wholedisk) == 0)
1768 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
1769 char *type, *idx, *end, *p;
1770 uint64_t id, vdev_id;
1773 * Determine our vdev type, keeping in mind
1774 * that the srchval is composed of a type and
1775 * vdev id pair (i.e. mirror-4).
1777 if ((type = strdup(srchval)) == NULL)
1780 if ((p = strrchr(type, '-')) == NULL) {
1788 * If the types don't match then keep looking.
1790 if (strncmp(val, type, strlen(val)) != 0) {
1795 verify(strncmp(type, VDEV_TYPE_RAIDZ,
1796 strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1797 strncmp(type, VDEV_TYPE_MIRROR,
1798 strlen(VDEV_TYPE_MIRROR)) == 0);
1799 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
1803 vdev_id = strtoull(idx, &end, 10);
1810 * Now verify that we have the correct vdev id.
1819 if (strcmp(srchval, val) == 0)
1828 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1829 &child, &children) != 0)
1832 for (c = 0; c < children; c++) {
1833 if ((ret = vdev_to_nvlist_iter(child[c], search,
1834 avail_spare, l2cache, NULL)) != NULL) {
1836 * The 'is_log' value is only set for the toplevel
1837 * vdev, not the leaf vdevs. So we always lookup the
1838 * log device from the root of the vdev tree (where
1839 * 'log' is non-NULL).
1842 nvlist_lookup_uint64(child[c],
1843 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
1851 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1852 &child, &children) == 0) {
1853 for (c = 0; c < children; c++) {
1854 if ((ret = vdev_to_nvlist_iter(child[c], search,
1855 avail_spare, l2cache, NULL)) != NULL) {
1856 *avail_spare = B_TRUE;
1862 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1863 &child, &children) == 0) {
1864 for (c = 0; c < children; c++) {
1865 if ((ret = vdev_to_nvlist_iter(child[c], search,
1866 avail_spare, l2cache, NULL)) != NULL) {
1877 * Given a physical path (minus the "/devices" prefix), find the
1881 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
1882 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
1884 nvlist_t *search, *nvroot, *ret;
1886 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1887 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
1889 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1892 *avail_spare = B_FALSE;
1896 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1897 nvlist_free(search);
1903 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
1906 zpool_vdev_is_interior(const char *name)
1908 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1909 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
1915 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
1916 boolean_t *l2cache, boolean_t *log)
1919 nvlist_t *nvroot, *search, *ret;
1922 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1924 guid = strtoull(path, &end, 10);
1925 if (guid != 0 && *end == '\0') {
1926 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
1927 } else if (zpool_vdev_is_interior(path)) {
1928 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
1930 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
1933 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1936 *avail_spare = B_FALSE;
1940 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1941 nvlist_free(search);
1947 vdev_online(nvlist_t *nv)
1951 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
1952 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
1953 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
1960 * Helper function for zpool_get_physpaths().
1963 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
1964 size_t *bytes_written)
1966 size_t bytes_left, pos, rsz;
1970 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
1972 return (EZFS_NODEVICE);
1974 pos = *bytes_written;
1975 bytes_left = physpath_size - pos;
1976 format = (pos == 0) ? "%s" : " %s";
1978 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
1979 *bytes_written += rsz;
1981 if (rsz >= bytes_left) {
1982 /* if physpath was not copied properly, clear it */
1983 if (bytes_left != 0) {
1986 return (EZFS_NOSPC);
1992 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
1993 size_t *rsz, boolean_t is_spare)
1998 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
1999 return (EZFS_INVALCONFIG);
2001 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2003 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2004 * For a spare vdev, we only want to boot from the active
2009 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2012 return (EZFS_INVALCONFIG);
2015 if (vdev_online(nv)) {
2016 if ((ret = vdev_get_one_physpath(nv, physpath,
2017 phypath_size, rsz)) != 0)
2020 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2021 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2022 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2027 if (nvlist_lookup_nvlist_array(nv,
2028 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2029 return (EZFS_INVALCONFIG);
2031 for (i = 0; i < count; i++) {
2032 ret = vdev_get_physpaths(child[i], physpath,
2033 phypath_size, rsz, is_spare);
2034 if (ret == EZFS_NOSPC)
2039 return (EZFS_POOL_INVALARG);
2043 * Get phys_path for a root pool config.
2044 * Return 0 on success; non-zero on failure.
2047 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2050 nvlist_t *vdev_root;
2057 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2059 return (EZFS_INVALCONFIG);
2061 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2062 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2063 &child, &count) != 0)
2064 return (EZFS_INVALCONFIG);
2066 #if defined(__sun__) || defined(__sun)
2068 * root pool can not have EFI labeled disks and can only have
2069 * a single top-level vdev.
2071 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
2072 pool_uses_efi(vdev_root))
2073 return (EZFS_POOL_INVALARG);
2076 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2079 /* No online devices */
2081 return (EZFS_NODEVICE);
2087 * Get phys_path for a root pool
2088 * Return 0 on success; non-zero on failure.
2091 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2093 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2098 * If the device has being dynamically expanded then we need to relabel
2099 * the disk to use the new unallocated space.
2102 zpool_relabel_disk(libzfs_handle_t *hdl, const char *path, const char *msg)
2106 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
2107 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2108 "relabel '%s': unable to open device: %d"), path, errno);
2109 return (zfs_error(hdl, EZFS_OPENFAILED, msg));
2113 * It's possible that we might encounter an error if the device
2114 * does not have any unallocated space left. If so, we simply
2115 * ignore that error and continue on.
2117 * Also, we don't call efi_rescan() - that would just return EBUSY.
2118 * The module will do it for us in vdev_disk_open().
2120 error = efi_use_whole_disk(fd);
2122 if (error && error != VT_ENOSPC) {
2123 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2124 "relabel '%s': unable to read disk capacity"), path);
2125 return (zfs_error(hdl, EZFS_NOCAP, msg));
2131 * Bring the specified vdev online. The 'flags' parameter is a set of the
2132 * ZFS_ONLINE_* flags.
2135 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2136 vdev_state_t *newstate)
2138 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2141 boolean_t avail_spare, l2cache, islog;
2142 libzfs_handle_t *hdl = zhp->zpool_hdl;
2145 if (flags & ZFS_ONLINE_EXPAND) {
2146 (void) snprintf(msg, sizeof (msg),
2147 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2149 (void) snprintf(msg, sizeof (msg),
2150 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2153 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2154 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2156 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2158 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2161 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2163 if (flags & ZFS_ONLINE_EXPAND ||
2164 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2165 uint64_t wholedisk = 0;
2167 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2171 * XXX - L2ARC 1.0 devices can't support expansion.
2174 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2175 "cannot expand cache devices"));
2176 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2180 const char *fullpath = path;
2181 char buf[MAXPATHLEN];
2183 if (path[0] != '/') {
2184 error = zfs_resolve_shortname(path, buf,
2187 return (zfs_error(hdl, EZFS_NODEVICE,
2193 error = zpool_relabel_disk(hdl, fullpath, msg);
2199 zc.zc_cookie = VDEV_STATE_ONLINE;
2202 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2203 if (errno == EINVAL) {
2204 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2205 "from this pool into a new one. Use '%s' "
2206 "instead"), "zpool detach");
2207 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2209 return (zpool_standard_error(hdl, errno, msg));
2212 *newstate = zc.zc_cookie;
2217 * Take the specified vdev offline
2220 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2222 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2225 boolean_t avail_spare, l2cache;
2226 libzfs_handle_t *hdl = zhp->zpool_hdl;
2228 (void) snprintf(msg, sizeof (msg),
2229 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2231 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2232 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2234 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2236 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2239 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2241 zc.zc_cookie = VDEV_STATE_OFFLINE;
2242 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2244 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2251 * There are no other replicas of this device.
2253 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2257 * The log device has unplayed logs
2259 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2262 return (zpool_standard_error(hdl, errno, msg));
2267 * Mark the given vdev faulted.
2270 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2272 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2274 libzfs_handle_t *hdl = zhp->zpool_hdl;
2276 (void) snprintf(msg, sizeof (msg),
2277 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
2279 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2281 zc.zc_cookie = VDEV_STATE_FAULTED;
2284 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2291 * There are no other replicas of this device.
2293 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2296 return (zpool_standard_error(hdl, errno, msg));
2302 * Mark the given vdev degraded.
2305 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2307 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2309 libzfs_handle_t *hdl = zhp->zpool_hdl;
2311 (void) snprintf(msg, sizeof (msg),
2312 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid);
2314 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2316 zc.zc_cookie = VDEV_STATE_DEGRADED;
2319 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2322 return (zpool_standard_error(hdl, errno, msg));
2326 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2330 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2336 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2338 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2341 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2342 children == 2 && child[which] == tgt)
2345 for (c = 0; c < children; c++)
2346 if (is_replacing_spare(child[c], tgt, which))
2354 * Attach new_disk (fully described by nvroot) to old_disk.
2355 * If 'replacing' is specified, the new disk will replace the old one.
2358 zpool_vdev_attach(zpool_handle_t *zhp,
2359 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2361 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2365 boolean_t avail_spare, l2cache, islog;
2370 nvlist_t *config_root;
2371 libzfs_handle_t *hdl = zhp->zpool_hdl;
2372 boolean_t rootpool = zpool_is_bootable(zhp);
2375 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2376 "cannot replace %s with %s"), old_disk, new_disk);
2378 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2379 "cannot attach %s to %s"), new_disk, old_disk);
2381 #if defined(__sun__) || defined(__sun)
2383 * If this is a root pool, make sure that we're not attaching an
2384 * EFI labeled device.
2386 if (rootpool && pool_uses_efi(nvroot)) {
2387 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2388 "EFI labeled devices are not supported on root pools."));
2389 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2393 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2394 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2396 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2399 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2402 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2404 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2405 zc.zc_cookie = replacing;
2407 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2408 &child, &children) != 0 || children != 1) {
2409 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2410 "new device must be a single disk"));
2411 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2414 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2415 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2417 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2421 * If the target is a hot spare that has been swapped in, we can only
2422 * replace it with another hot spare.
2425 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2426 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2427 NULL) == NULL || !avail_spare) &&
2428 is_replacing_spare(config_root, tgt, 1)) {
2429 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2430 "can only be replaced by another hot spare"));
2432 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2437 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2440 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2442 zcmd_free_nvlists(&zc);
2447 * XXX need a better way to prevent user from
2448 * booting up a half-baked vdev.
2450 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2451 "sure to wait until resilver is done "
2452 "before rebooting.\n"));
2460 * Can't attach to or replace this type of vdev.
2463 uint64_t version = zpool_get_prop_int(zhp,
2464 ZPOOL_PROP_VERSION, NULL);
2467 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2468 "cannot replace a log with a spare"));
2469 else if (version >= SPA_VERSION_MULTI_REPLACE)
2470 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2471 "already in replacing/spare config; wait "
2472 "for completion or use 'zpool detach'"));
2474 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2475 "cannot replace a replacing device"));
2477 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2478 "can only attach to mirrors and top-level "
2481 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2486 * The new device must be a single disk.
2488 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2489 "new device must be a single disk"));
2490 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2494 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2496 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2501 * The new device is too small.
2503 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2504 "device is too small"));
2505 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2510 * The new device has a different alignment requirement.
2512 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2513 "devices have different sector alignment"));
2514 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2519 * The resulting top-level vdev spec won't fit in the label.
2521 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2525 (void) zpool_standard_error(hdl, errno, msg);
2532 * Detach the specified device.
2535 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2537 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2540 boolean_t avail_spare, l2cache;
2541 libzfs_handle_t *hdl = zhp->zpool_hdl;
2543 (void) snprintf(msg, sizeof (msg),
2544 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2546 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2547 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2549 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2552 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2555 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2557 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2559 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2566 * Can't detach from this type of vdev.
2568 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2569 "applicable to mirror and replacing vdevs"));
2570 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2575 * There are no other replicas of this device.
2577 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2581 (void) zpool_standard_error(hdl, errno, msg);
2588 * Find a mirror vdev in the source nvlist.
2590 * The mchild array contains a list of disks in one of the top-level mirrors
2591 * of the source pool. The schild array contains a list of disks that the
2592 * user specified on the command line. We loop over the mchild array to
2593 * see if any entry in the schild array matches.
2595 * If a disk in the mchild array is found in the schild array, we return
2596 * the index of that entry. Otherwise we return -1.
2599 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2600 nvlist_t **schild, uint_t schildren)
2604 for (mc = 0; mc < mchildren; mc++) {
2606 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2607 mchild[mc], B_FALSE);
2609 for (sc = 0; sc < schildren; sc++) {
2610 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2611 schild[sc], B_FALSE);
2612 boolean_t result = (strcmp(mpath, spath) == 0);
2628 * Split a mirror pool. If newroot points to null, then a new nvlist
2629 * is generated and it is the responsibility of the caller to free it.
2632 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2633 nvlist_t *props, splitflags_t flags)
2635 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2637 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2638 nvlist_t **varray = NULL, *zc_props = NULL;
2639 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2640 libzfs_handle_t *hdl = zhp->zpool_hdl;
2642 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2645 (void) snprintf(msg, sizeof (msg),
2646 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2648 if (!zpool_name_valid(hdl, B_FALSE, newname))
2649 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2651 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2652 (void) fprintf(stderr, gettext("Internal error: unable to "
2653 "retrieve pool configuration\n"));
2657 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2659 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2662 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2663 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2664 props, vers, flags, msg)) == NULL)
2668 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2670 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2671 "Source pool is missing vdev tree"));
2673 nvlist_free(zc_props);
2677 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2680 if (*newroot == NULL ||
2681 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2682 &newchild, &newchildren) != 0)
2685 for (c = 0; c < children; c++) {
2686 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2688 nvlist_t **mchild, *vdev;
2693 * Unlike cache & spares, slogs are stored in the
2694 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2696 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2698 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2700 if (is_log || is_hole) {
2702 * Create a hole vdev and put it in the config.
2704 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2706 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2707 VDEV_TYPE_HOLE) != 0)
2709 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2714 varray[vcount++] = vdev;
2718 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
2720 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2721 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2722 "Source pool must be composed only of mirrors\n"));
2723 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2727 verify(nvlist_lookup_nvlist_array(child[c],
2728 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2730 /* find or add an entry for this top-level vdev */
2731 if (newchildren > 0 &&
2732 (entry = find_vdev_entry(zhp, mchild, mchildren,
2733 newchild, newchildren)) >= 0) {
2734 /* We found a disk that the user specified. */
2735 vdev = mchild[entry];
2738 /* User didn't specify a disk for this vdev. */
2739 vdev = mchild[mchildren - 1];
2742 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
2746 /* did we find every disk the user specified? */
2747 if (found != newchildren) {
2748 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
2749 "include at most one disk from each mirror"));
2750 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2754 /* Prepare the nvlist for populating. */
2755 if (*newroot == NULL) {
2756 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
2759 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
2760 VDEV_TYPE_ROOT) != 0)
2763 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
2766 /* Add all the children we found */
2767 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
2768 lastlog == 0 ? vcount : lastlog) != 0)
2772 * If we're just doing a dry run, exit now with success.
2775 memory_err = B_FALSE;
2780 /* now build up the config list & call the ioctl */
2781 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
2784 if (nvlist_add_nvlist(newconfig,
2785 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
2786 nvlist_add_string(newconfig,
2787 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
2788 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
2792 * The new pool is automatically part of the namespace unless we
2793 * explicitly export it.
2796 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
2797 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2798 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
2799 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
2801 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
2804 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
2805 retval = zpool_standard_error(hdl, errno, msg);
2810 memory_err = B_FALSE;
2813 if (varray != NULL) {
2816 for (v = 0; v < vcount; v++)
2817 nvlist_free(varray[v]);
2820 zcmd_free_nvlists(&zc);
2822 nvlist_free(zc_props);
2824 nvlist_free(newconfig);
2826 nvlist_free(*newroot);
2834 return (no_memory(hdl));
2840 * Remove the given device. Currently, this is supported only for hot spares
2841 * and level 2 cache devices.
2844 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
2846 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2849 boolean_t avail_spare, l2cache, islog;
2850 libzfs_handle_t *hdl = zhp->zpool_hdl;
2853 (void) snprintf(msg, sizeof (msg),
2854 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
2856 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2857 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2859 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2861 * XXX - this should just go away.
2863 if (!avail_spare && !l2cache && !islog) {
2864 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2865 "only inactive hot spares, cache, top-level, "
2866 "or log devices can be removed"));
2867 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2870 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
2871 if (islog && version < SPA_VERSION_HOLES) {
2872 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2873 "pool must be upgrade to support log removal"));
2874 return (zfs_error(hdl, EZFS_BADVERSION, msg));
2877 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2879 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
2882 return (zpool_standard_error(hdl, errno, msg));
2886 * Clear the errors for the pool, or the particular device if specified.
2889 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
2891 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2894 zpool_rewind_policy_t policy;
2895 boolean_t avail_spare, l2cache;
2896 libzfs_handle_t *hdl = zhp->zpool_hdl;
2897 nvlist_t *nvi = NULL;
2901 (void) snprintf(msg, sizeof (msg),
2902 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2905 (void) snprintf(msg, sizeof (msg),
2906 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2909 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2911 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
2912 &l2cache, NULL)) == 0)
2913 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2916 * Don't allow error clearing for hot spares. Do allow
2917 * error clearing for l2cache devices.
2920 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2922 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
2926 zpool_get_rewind_policy(rewindnvl, &policy);
2927 zc.zc_cookie = policy.zrp_request;
2929 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
2932 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
2935 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
2937 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
2938 zcmd_free_nvlists(&zc);
2943 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
2944 errno != EPERM && errno != EACCES)) {
2945 if (policy.zrp_request &
2946 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2947 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
2948 zpool_rewind_exclaim(hdl, zc.zc_name,
2949 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
2953 zcmd_free_nvlists(&zc);
2957 zcmd_free_nvlists(&zc);
2958 return (zpool_standard_error(hdl, errno, msg));
2962 * Similar to zpool_clear(), but takes a GUID (used by fmd).
2965 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
2967 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2969 libzfs_handle_t *hdl = zhp->zpool_hdl;
2971 (void) snprintf(msg, sizeof (msg),
2972 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
2973 (u_longlong_t)guid);
2975 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2977 zc.zc_cookie = ZPOOL_NO_REWIND;
2979 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
2982 return (zpool_standard_error(hdl, errno, msg));
2986 * Change the GUID for a pool.
2989 zpool_reguid(zpool_handle_t *zhp)
2992 libzfs_handle_t *hdl = zhp->zpool_hdl;
2993 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2995 (void) snprintf(msg, sizeof (msg),
2996 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
2998 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2999 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3002 return (zpool_standard_error(hdl, errno, msg));
3009 zpool_reopen(zpool_handle_t *zhp)
3011 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3013 libzfs_handle_t *hdl = zhp->zpool_hdl;
3015 (void) snprintf(msg, sizeof (msg),
3016 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
3019 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3020 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
3022 return (zpool_standard_error(hdl, errno, msg));
3026 * Convert from a devid string to a path.
3029 devid_to_path(char *devid_str)
3034 devid_nmlist_t *list = NULL;
3037 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3040 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3042 devid_str_free(minor);
3048 if ((path = strdup(list[0].devname)) == NULL)
3051 devid_free_nmlist(list);
3057 * Convert from a path to a devid string.
3060 path_to_devid(const char *path)
3066 if ((fd = open(path, O_RDONLY)) < 0)
3071 if (devid_get(fd, &devid) == 0) {
3072 if (devid_get_minor_name(fd, &minor) == 0)
3073 ret = devid_str_encode(devid, minor);
3075 devid_str_free(minor);
3084 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3085 * ignore any failure here, since a common case is for an unprivileged user to
3086 * type 'zpool status', and we'll display the correct information anyway.
3089 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3091 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3093 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3094 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3095 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3098 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3102 * Remove partition suffix from a vdev path. Partition suffixes may take three
3103 * forms: "-partX", "pX", or "X", where X is a string of digits. The second
3104 * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
3105 * third case only occurs when preceded by a string matching the regular
3106 * expression "^[hs]d[a-z]+", i.e. a scsi or ide disk.
3109 strip_partition(libzfs_handle_t *hdl, char *path)
3111 char *tmp = zfs_strdup(hdl, path);
3112 char *part = NULL, *d = NULL;
3114 if ((part = strstr(tmp, "-part")) && part != tmp) {
3116 } else if ((part = strrchr(tmp, 'p')) &&
3117 part > tmp + 1 && isdigit(*(part-1))) {
3119 } else if ((tmp[0] == 'h' || tmp[0] == 's') && tmp[1] == 'd') {
3120 for (d = &tmp[2]; isalpha(*d); part = ++d);
3122 if (part && d && *d != '\0') {
3123 for (; isdigit(*d); d++);
3130 #define PATH_BUF_LEN 64
3133 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3134 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3135 * We also check if this is a whole disk, in which case we strip off the
3136 * trailing 's0' slice name.
3138 * This routine is also responsible for identifying when disks have been
3139 * reconfigured in a new location. The kernel will have opened the device by
3140 * devid, but the path will still refer to the old location. To catch this, we
3141 * first do a path -> devid translation (which is fast for the common case). If
3142 * the devid matches, we're done. If not, we do a reverse devid -> path
3143 * translation and issue the appropriate ioctl() to update the path of the vdev.
3144 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3148 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3151 char *path, *devid, *type;
3153 char buf[PATH_BUF_LEN];
3154 char tmpbuf[PATH_BUF_LEN];
3158 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
3160 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3162 (void) snprintf(buf, sizeof (buf), "%llu",
3163 (u_longlong_t)value);
3165 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3167 * If the device is dead (faulted, offline, etc) then don't
3168 * bother opening it. Otherwise we may be forcing the user to
3169 * open a misbehaving device, which can have undesirable
3172 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3173 (uint64_t **)&vs, &vsc) != 0 ||
3174 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3176 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3178 * Determine if the current path is correct.
3180 char *newdevid = path_to_devid(path);
3182 if (newdevid == NULL ||
3183 strcmp(devid, newdevid) != 0) {
3186 if ((newpath = devid_to_path(devid)) != NULL) {
3188 * Update the path appropriately.
3190 set_path(zhp, nv, newpath);
3191 if (nvlist_add_string(nv,
3192 ZPOOL_CONFIG_PATH, newpath) == 0)
3193 verify(nvlist_lookup_string(nv,
3201 devid_str_free(newdevid);
3205 * For a block device only use the name.
3207 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
3208 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
3209 path = strrchr(path, '/');
3214 * Remove the partition from the path it this is a whole disk.
3216 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3217 &value) == 0 && value) {
3218 return strip_partition(hdl, path);
3221 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3224 * If it's a raidz device, we need to stick in the parity level.
3226 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3228 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3230 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3231 (u_longlong_t)value);
3236 * We identify each top-level vdev by using a <type-id>
3237 * naming convention.
3242 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3244 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s-%llu",
3245 path, (u_longlong_t)id);
3250 return (zfs_strdup(hdl, path));
3254 zbookmark_compare(const void *a, const void *b)
3256 return (memcmp(a, b, sizeof (zbookmark_t)));
3260 * Retrieve the persistent error log, uniquify the members, and return to the
3264 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3266 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3268 zbookmark_t *zb = NULL;
3272 * Retrieve the raw error list from the kernel. If the number of errors
3273 * has increased, allocate more space and continue until we get the
3276 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3280 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3281 count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
3283 zc.zc_nvlist_dst_size = count;
3284 (void) strcpy(zc.zc_name, zhp->zpool_name);
3286 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3288 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3289 if (errno == ENOMEM) {
3290 count = zc.zc_nvlist_dst_size;
3291 if ((zc.zc_nvlist_dst = (uintptr_t)
3292 zfs_alloc(zhp->zpool_hdl, count *
3293 sizeof (zbookmark_t))) == (uintptr_t)NULL)
3304 * Sort the resulting bookmarks. This is a little confusing due to the
3305 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3306 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3307 * _not_ copied as part of the process. So we point the start of our
3308 * array appropriate and decrement the total number of elements.
3310 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
3311 zc.zc_nvlist_dst_size;
3312 count -= zc.zc_nvlist_dst_size;
3314 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
3316 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3319 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3321 for (i = 0; i < count; i++) {
3324 /* ignoring zb_blkid and zb_level for now */
3325 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3326 zb[i-1].zb_object == zb[i].zb_object)
3329 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3331 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3332 zb[i].zb_objset) != 0) {
3336 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3337 zb[i].zb_object) != 0) {
3341 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3348 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3352 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3353 return (no_memory(zhp->zpool_hdl));
3357 * Upgrade a ZFS pool to the latest on-disk version.
3360 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3362 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3363 libzfs_handle_t *hdl = zhp->zpool_hdl;
3365 (void) strcpy(zc.zc_name, zhp->zpool_name);
3366 zc.zc_cookie = new_version;
3368 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3369 return (zpool_standard_error_fmt(hdl, errno,
3370 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3376 zpool_set_history_str(const char *subcommand, int argc, char **argv,
3381 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
3382 for (i = 1; i < argc; i++) {
3383 if (strlen(history_str) + 1 + strlen(argv[i]) >
3386 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
3387 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
3392 * Stage command history for logging.
3395 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
3397 if (history_str == NULL)
3400 if (strlen(history_str) > HIS_MAX_RECORD_LEN)
3403 if (hdl->libzfs_log_str != NULL)
3404 free(hdl->libzfs_log_str);
3406 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
3407 return (no_memory(hdl));
3413 * Perform ioctl to get some command history of a pool.
3415 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3416 * logical offset of the history buffer to start reading from.
3418 * Upon return, 'off' is the next logical offset to read from and
3419 * 'len' is the actual amount of bytes read into 'buf'.
3422 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3424 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3425 libzfs_handle_t *hdl = zhp->zpool_hdl;
3427 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3429 zc.zc_history = (uint64_t)(uintptr_t)buf;
3430 zc.zc_history_len = *len;
3431 zc.zc_history_offset = *off;
3433 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3436 return (zfs_error_fmt(hdl, EZFS_PERM,
3437 dgettext(TEXT_DOMAIN,
3438 "cannot show history for pool '%s'"),
3441 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3442 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3443 "'%s'"), zhp->zpool_name));
3445 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3446 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3447 "'%s', pool must be upgraded"), zhp->zpool_name));
3449 return (zpool_standard_error_fmt(hdl, errno,
3450 dgettext(TEXT_DOMAIN,
3451 "cannot get history for '%s'"), zhp->zpool_name));
3455 *len = zc.zc_history_len;
3456 *off = zc.zc_history_offset;
3462 * Process the buffer of nvlists, unpacking and storing each nvlist record
3463 * into 'records'. 'leftover' is set to the number of bytes that weren't
3464 * processed as there wasn't a complete record.
3467 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3468 nvlist_t ***records, uint_t *numrecords)
3474 while (bytes_read > sizeof (reclen)) {
3476 /* get length of packed record (stored as little endian) */
3477 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3478 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3480 if (bytes_read < sizeof (reclen) + reclen)
3484 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3486 bytes_read -= sizeof (reclen) + reclen;
3487 buf += sizeof (reclen) + reclen;
3489 /* add record to nvlist array */
3491 if (ISP2(*numrecords + 1)) {
3492 *records = realloc(*records,
3493 *numrecords * 2 * sizeof (nvlist_t *));
3495 (*records)[*numrecords - 1] = nv;
3498 *leftover = bytes_read;
3502 #define HIS_BUF_LEN (128*1024)
3505 * Retrieve the command history of a pool.
3508 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3510 char buf[HIS_BUF_LEN];
3512 nvlist_t **records = NULL;
3513 uint_t numrecords = 0;
3517 uint64_t bytes_read = sizeof (buf);
3520 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3523 /* if nothing else was read in, we're at EOF, just return */
3527 if ((err = zpool_history_unpack(buf, bytes_read,
3528 &leftover, &records, &numrecords)) != 0)
3536 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3537 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3538 records, numrecords) == 0);
3540 for (i = 0; i < numrecords; i++)
3541 nvlist_free(records[i]);
3548 * Retrieve the next event. If there is a new event available 'nvp' will
3549 * contain a newly allocated nvlist and 'dropped' will be set to the number
3550 * of missed events since the last call to this function. When 'nvp' is
3551 * set to NULL it indicates no new events are available. In either case
3552 * the function returns 0 and it is up to the caller to free 'nvp'. In
3553 * the case of a fatal error the function will return a non-zero value.
3554 * When the function is called in blocking mode it will not return until
3555 * a new event is available.
3558 zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
3559 int *dropped, int block, int cleanup_fd)
3561 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3566 zc.zc_cleanup_fd = cleanup_fd;
3569 zc.zc_guid = ZEVENT_NONBLOCK;
3571 if (zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE) != 0)
3575 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
3578 error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
3579 dgettext(TEXT_DOMAIN, "zfs shutdown"));
3582 /* Blocking error case should not occur */
3584 error = zpool_standard_error_fmt(hdl, errno,
3585 dgettext(TEXT_DOMAIN, "cannot get event"));
3589 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3590 error = zfs_error_fmt(hdl, EZFS_NOMEM,
3591 dgettext(TEXT_DOMAIN, "cannot get event"));
3597 error = zpool_standard_error_fmt(hdl, errno,
3598 dgettext(TEXT_DOMAIN, "cannot get event"));
3603 error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
3607 *dropped = (int)zc.zc_cookie;
3609 zcmd_free_nvlists(&zc);
3618 zpool_events_clear(libzfs_handle_t *hdl, int *count)
3620 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3623 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3624 "cannot clear events"));
3626 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
3627 return (zpool_standard_error_fmt(hdl, errno, msg));
3630 *count = (int)zc.zc_cookie; /* # of events cleared */
3636 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3637 char *pathname, size_t len)
3639 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3640 boolean_t mounted = B_FALSE;
3641 char *mntpnt = NULL;
3642 char dsname[MAXNAMELEN];
3645 /* special case for the MOS */
3646 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", (longlong_t)obj);
3650 /* get the dataset's name */
3651 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3653 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3654 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3655 /* just write out a path of two object numbers */
3656 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3657 (longlong_t)dsobj, (longlong_t)obj);
3660 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3662 /* find out if the dataset is mounted */
3663 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3665 /* get the corrupted object's path */
3666 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3668 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3671 (void) snprintf(pathname, len, "%s%s", mntpnt,
3674 (void) snprintf(pathname, len, "%s:%s",
3675 dsname, zc.zc_value);
3678 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, (longlong_t)obj);
3684 * Read the EFI label from the config, if a label does not exist then
3685 * pass back the error to the caller. If the caller has passed a non-NULL
3686 * diskaddr argument then we set it to the starting address of the EFI
3690 read_efi_label(nvlist_t *config, diskaddr_t *sb)
3694 char diskname[MAXPATHLEN];
3697 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3700 (void) snprintf(diskname, sizeof (diskname), "%s%s", DISK_ROOT,
3701 strrchr(path, '/'));
3702 if ((fd = open(diskname, O_RDWR|O_DIRECT)) >= 0) {
3703 struct dk_gpt *vtoc;
3705 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3707 *sb = vtoc->efi_parts[0].p_start;
3716 * determine where a partition starts on a disk in the current
3720 find_start_block(nvlist_t *config)
3724 diskaddr_t sb = MAXOFFSET_T;
3727 if (nvlist_lookup_nvlist_array(config,
3728 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3729 if (nvlist_lookup_uint64(config,
3730 ZPOOL_CONFIG_WHOLE_DISK,
3731 &wholedisk) != 0 || !wholedisk) {
3732 return (MAXOFFSET_T);
3734 if (read_efi_label(config, &sb) < 0)
3739 for (c = 0; c < children; c++) {
3740 sb = find_start_block(child[c]);
3741 if (sb != MAXOFFSET_T) {
3745 return (MAXOFFSET_T);
3749 zpool_label_disk_wait(char *path, int timeout)
3751 struct stat64 statbuf;
3755 * Wait timeout miliseconds for a newly created device to be available
3756 * from the given path. There is a small window when a /dev/ device
3757 * will exist and the udev link will not, so we must wait for the
3758 * symlink. Depending on the udev rules this may take a few seconds.
3760 for (i = 0; i < timeout; i++) {
3764 if ((stat64(path, &statbuf) == 0) && (errno == 0))
3772 zpool_label_disk_check(char *path)
3774 struct dk_gpt *vtoc;
3777 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0)
3780 if ((err = efi_alloc_and_read(fd, &vtoc)) != 0) {
3785 if (vtoc->efi_flags & EFI_GPT_PRIMARY_CORRUPT) {
3797 * Label an individual disk. The name provided is the short name,
3798 * stripped of any leading /dev path.
3801 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
3803 char path[MAXPATHLEN];
3804 struct dk_gpt *vtoc;
3806 size_t resv = EFI_MIN_RESV_SIZE;
3807 uint64_t slice_size;
3808 diskaddr_t start_block;
3811 /* prepare an error message just in case */
3812 (void) snprintf(errbuf, sizeof (errbuf),
3813 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3818 #if defined(__sun__) || defined(__sun)
3819 if (zpool_is_bootable(zhp)) {
3820 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3821 "EFI labeled devices are not supported on root "
3823 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3827 verify(nvlist_lookup_nvlist(zhp->zpool_config,
3828 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3830 if (zhp->zpool_start_block == 0)
3831 start_block = find_start_block(nvroot);
3833 start_block = zhp->zpool_start_block;
3834 zhp->zpool_start_block = start_block;
3837 start_block = NEW_START_BLOCK;
3840 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
3842 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
3844 * This shouldn't happen. We've long since verified that this
3845 * is a valid device.
3847 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
3848 "label '%s': unable to open device: %d"), path, errno);
3849 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
3852 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
3854 * The only way this can fail is if we run out of memory, or we
3855 * were unable to read the disk's capacity
3857 if (errno == ENOMEM)
3858 (void) no_memory(hdl);
3861 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
3862 "label '%s': unable to read disk capacity"), path);
3864 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
3867 slice_size = vtoc->efi_last_u_lba + 1;
3868 slice_size -= EFI_MIN_RESV_SIZE;
3869 if (start_block == MAXOFFSET_T)
3870 start_block = NEW_START_BLOCK;
3871 slice_size -= start_block;
3872 slice_size = P2ALIGN(slice_size, PARTITION_END_ALIGNMENT);
3874 vtoc->efi_parts[0].p_start = start_block;
3875 vtoc->efi_parts[0].p_size = slice_size;
3878 * Why we use V_USR: V_BACKUP confuses users, and is considered
3879 * disposable by some EFI utilities (since EFI doesn't have a backup
3880 * slice). V_UNASSIGNED is supposed to be used only for zero size
3881 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
3882 * etc. were all pretty specific. V_USR is as close to reality as we
3883 * can get, in the absence of V_OTHER.
3885 vtoc->efi_parts[0].p_tag = V_USR;
3886 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
3888 vtoc->efi_parts[8].p_start = slice_size + start_block;
3889 vtoc->efi_parts[8].p_size = resv;
3890 vtoc->efi_parts[8].p_tag = V_RESERVED;
3892 if ((rval = efi_write(fd, vtoc)) != 0 || (rval = efi_rescan(fd)) != 0) {
3894 * Some block drivers (like pcata) may not support EFI
3895 * GPT labels. Print out a helpful error message dir-
3896 * ecting the user to manually label the disk and give
3902 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "try using "
3903 "parted(8) and then provide a specific slice: %d"), rval);
3904 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3910 /* Wait for the first expected partition to appear. */
3912 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
3913 (void) zfs_append_partition(path, MAXPATHLEN);
3915 rval = zpool_label_disk_wait(path, 3000);
3917 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "failed to "
3918 "detect device partitions on '%s': %d"), path, rval);
3919 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3922 /* We can't be to paranoid. Read the label back and verify it. */
3923 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
3924 rval = zpool_label_disk_check(path);
3926 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "freshly written "
3927 "EFI label on '%s' is damaged. Ensure\nthis device "
3928 "is not in in use, and is functioning properly: %d"),
3930 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));