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.
37 #include <sys/efi_partition.h>
39 #include <sys/zfs_ioctl.h>
42 #include "zfs_namecheck.h"
44 #include "libzfs_impl.h"
45 #include "zfs_comutil.h"
47 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
49 typedef struct prop_flags {
50 int create:1; /* Validate property on creation */
51 int import:1; /* Validate property on import */
55 * ====================================================================
56 * zpool property functions
57 * ====================================================================
61 zpool_get_all_props(zpool_handle_t *zhp)
63 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
64 libzfs_handle_t *hdl = zhp->zpool_hdl;
66 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
68 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
71 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
72 if (errno == ENOMEM) {
73 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
74 zcmd_free_nvlists(&zc);
78 zcmd_free_nvlists(&zc);
83 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
84 zcmd_free_nvlists(&zc);
88 zcmd_free_nvlists(&zc);
94 zpool_props_refresh(zpool_handle_t *zhp)
98 old_props = zhp->zpool_props;
100 if (zpool_get_all_props(zhp) != 0)
103 nvlist_free(old_props);
108 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
114 zprop_source_t source;
116 nvl = zhp->zpool_props;
117 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
118 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
120 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
122 source = ZPROP_SRC_DEFAULT;
123 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
134 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
138 zprop_source_t source;
140 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
142 * zpool_get_all_props() has most likely failed because
143 * the pool is faulted, but if all we need is the top level
144 * vdev's guid then get it from the zhp config nvlist.
146 if ((prop == ZPOOL_PROP_GUID) &&
147 (nvlist_lookup_nvlist(zhp->zpool_config,
148 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
149 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
153 return (zpool_prop_default_numeric(prop));
156 nvl = zhp->zpool_props;
157 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
158 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
160 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
162 source = ZPROP_SRC_DEFAULT;
163 value = zpool_prop_default_numeric(prop);
173 * Map VDEV STATE to printed strings.
176 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
181 case VDEV_STATE_CLOSED:
182 case VDEV_STATE_OFFLINE:
183 return (gettext("OFFLINE"));
184 case VDEV_STATE_REMOVED:
185 return (gettext("REMOVED"));
186 case VDEV_STATE_CANT_OPEN:
187 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
188 return (gettext("FAULTED"));
189 else if (aux == VDEV_AUX_SPLIT_POOL)
190 return (gettext("SPLIT"));
192 return (gettext("UNAVAIL"));
193 case VDEV_STATE_FAULTED:
194 return (gettext("FAULTED"));
195 case VDEV_STATE_DEGRADED:
196 return (gettext("DEGRADED"));
197 case VDEV_STATE_HEALTHY:
198 return (gettext("ONLINE"));
201 return (gettext("UNKNOWN"));
205 * Get a zpool property value for 'prop' and return the value in
206 * a pre-allocated buffer.
209 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
210 zprop_source_t *srctype)
214 zprop_source_t src = ZPROP_SRC_NONE;
219 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
221 case ZPOOL_PROP_NAME:
222 (void) strlcpy(buf, zpool_get_name(zhp), len);
225 case ZPOOL_PROP_HEALTH:
226 (void) strlcpy(buf, "FAULTED", len);
229 case ZPOOL_PROP_GUID:
230 intval = zpool_get_prop_int(zhp, prop, &src);
231 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
234 case ZPOOL_PROP_ALTROOT:
235 case ZPOOL_PROP_CACHEFILE:
236 if (zhp->zpool_props != NULL ||
237 zpool_get_all_props(zhp) == 0) {
239 zpool_get_prop_string(zhp, prop, &src),
247 (void) strlcpy(buf, "-", len);
256 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
257 prop != ZPOOL_PROP_NAME)
260 switch (zpool_prop_get_type(prop)) {
261 case PROP_TYPE_STRING:
262 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
266 case PROP_TYPE_NUMBER:
267 intval = zpool_get_prop_int(zhp, prop, &src);
270 case ZPOOL_PROP_SIZE:
271 case ZPOOL_PROP_ALLOCATED:
272 case ZPOOL_PROP_FREE:
273 case ZPOOL_PROP_ASHIFT:
274 (void) zfs_nicenum(intval, buf, len);
277 case ZPOOL_PROP_CAPACITY:
278 (void) snprintf(buf, len, "%llu%%",
279 (u_longlong_t)intval);
282 case ZPOOL_PROP_DEDUPRATIO:
283 (void) snprintf(buf, len, "%llu.%02llux",
284 (u_longlong_t)(intval / 100),
285 (u_longlong_t)(intval % 100));
288 case ZPOOL_PROP_HEALTH:
289 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
290 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
291 verify(nvlist_lookup_uint64_array(nvroot,
292 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
295 (void) strlcpy(buf, zpool_state_to_name(intval,
299 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
303 case PROP_TYPE_INDEX:
304 intval = zpool_get_prop_int(zhp, prop, &src);
305 if (zpool_prop_index_to_string(prop, intval, &strval)
308 (void) strlcpy(buf, strval, len);
322 * Check if the bootfs name has the same pool name as it is set to.
323 * Assuming bootfs is a valid dataset name.
326 bootfs_name_valid(const char *pool, char *bootfs)
328 int len = strlen(pool);
330 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
333 if (strncmp(pool, bootfs, len) == 0 &&
334 (bootfs[len] == '/' || bootfs[len] == '\0'))
341 * Inspect the configuration to determine if any of the devices contain
345 pool_uses_efi(nvlist_t *config)
350 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
351 &child, &children) != 0)
352 return (read_efi_label(config, NULL) >= 0);
354 for (c = 0; c < children; c++) {
355 if (pool_uses_efi(child[c]))
362 pool_is_bootable(zpool_handle_t *zhp)
364 char bootfs[ZPOOL_MAXNAMELEN];
366 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
367 sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
368 sizeof (bootfs)) != 0);
373 * Given an nvlist of zpool properties to be set, validate that they are
374 * correct, and parse any numeric properties (index, boolean, etc) if they are
375 * specified as strings.
378 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
379 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
387 struct stat64 statbuf;
391 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
392 (void) no_memory(hdl);
397 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
398 const char *propname = nvpair_name(elem);
401 * Make sure this property is valid and applies to this type.
403 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
404 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
405 "invalid property '%s'"), propname);
406 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
410 if (zpool_prop_readonly(prop)) {
411 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
412 "is readonly"), propname);
413 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
417 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
418 &strval, &intval, errbuf) != 0)
422 * Perform additional checking for specific properties.
427 case ZPOOL_PROP_VERSION:
428 if (intval < version || intval > SPA_VERSION) {
429 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
430 "property '%s' number %d is invalid."),
432 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
437 case ZPOOL_PROP_ASHIFT:
439 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
440 "property '%s' can only be set at "
441 "creation time"), propname);
442 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
446 if (intval != 0 && (intval < 9 || intval > 13)) {
447 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
448 "property '%s' number %d is invalid."),
450 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
455 case ZPOOL_PROP_BOOTFS:
456 if (flags.create || flags.import) {
457 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
458 "property '%s' cannot be set at creation "
459 "or import time"), propname);
460 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
464 if (version < SPA_VERSION_BOOTFS) {
465 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
466 "pool must be upgraded to support "
467 "'%s' property"), propname);
468 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
473 * bootfs property value has to be a dataset name and
474 * the dataset has to be in the same pool as it sets to.
476 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
478 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
479 "is an invalid name"), strval);
480 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
484 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
485 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
486 "could not open pool '%s'"), poolname);
487 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
490 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
491 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
493 #if defined(__sun__) || defined(__sun)
495 * bootfs property cannot be set on a disk which has
498 if (pool_uses_efi(nvroot)) {
499 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
500 "property '%s' not supported on "
501 "EFI labeled devices"), propname);
502 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
510 case ZPOOL_PROP_ALTROOT:
511 if (!flags.create && !flags.import) {
512 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
513 "property '%s' can only be set during pool "
514 "creation or import"), propname);
515 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
519 if (strval[0] != '/') {
520 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
521 "bad alternate root '%s'"), strval);
522 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
527 case ZPOOL_PROP_CACHEFILE:
528 if (strval[0] == '\0')
531 if (strcmp(strval, "none") == 0)
534 if (strval[0] != '/') {
535 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
536 "property '%s' must be empty, an "
537 "absolute path, or 'none'"), propname);
538 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
542 slash = strrchr(strval, '/');
544 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
545 strcmp(slash, "/..") == 0) {
546 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
547 "'%s' is not a valid file"), strval);
548 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
554 if (strval[0] != '\0' &&
555 (stat64(strval, &statbuf) != 0 ||
556 !S_ISDIR(statbuf.st_mode))) {
557 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
558 "'%s' is not a valid directory"),
560 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
567 case ZPOOL_PROP_READONLY:
569 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
570 "property '%s' can only be set at "
571 "import time"), propname);
572 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
581 nvlist_free(retprops);
586 * Set zpool property : propname=propval.
589 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
591 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
594 nvlist_t *nvl = NULL;
597 prop_flags_t flags = { 0 };
599 (void) snprintf(errbuf, sizeof (errbuf),
600 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
603 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
604 return (no_memory(zhp->zpool_hdl));
606 if (nvlist_add_string(nvl, propname, propval) != 0) {
608 return (no_memory(zhp->zpool_hdl));
611 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
612 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
613 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
622 * Execute the corresponding ioctl() to set this property.
624 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
626 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
631 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
633 zcmd_free_nvlists(&zc);
637 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
639 (void) zpool_props_refresh(zhp);
645 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
647 libzfs_handle_t *hdl = zhp->zpool_hdl;
649 char buf[ZFS_MAXPROPLEN];
651 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
654 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
659 if (entry->pl_prop != ZPROP_INVAL &&
660 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
662 if (strlen(buf) > entry->pl_width)
663 entry->pl_width = strlen(buf);
672 * Don't start the slice at the default block of 34; many storage
673 * devices will use a stripe width of 128k, other vendors prefer a 1m
674 * alignment. It is best to play it safe and ensure a 1m alignment
675 * given 512B blocks. When the block size is larger by a power of 2
676 * we will still be 1m aligned. Some devices are sensitive to the
677 * partition ending alignment as well.
679 #define NEW_START_BLOCK 2048
680 #define PARTITION_END_ALIGNMENT 2048
683 * Validate the given pool name, optionally putting an extended error message in
687 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
693 ret = pool_namecheck(pool, &why, &what);
696 * The rules for reserved pool names were extended at a later point.
697 * But we need to support users with existing pools that may now be
698 * invalid. So we only check for this expanded set of names during a
699 * create (or import), and only in userland.
701 if (ret == 0 && !isopen &&
702 (strncmp(pool, "mirror", 6) == 0 ||
703 strncmp(pool, "raidz", 5) == 0 ||
704 strncmp(pool, "spare", 5) == 0 ||
705 strcmp(pool, "log") == 0)) {
708 dgettext(TEXT_DOMAIN, "name is reserved"));
716 case NAME_ERR_TOOLONG:
718 dgettext(TEXT_DOMAIN, "name is too long"));
721 case NAME_ERR_INVALCHAR:
723 dgettext(TEXT_DOMAIN, "invalid character "
724 "'%c' in pool name"), what);
727 case NAME_ERR_NOLETTER:
728 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
729 "name must begin with a letter"));
732 case NAME_ERR_RESERVED:
733 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
734 "name is reserved"));
737 case NAME_ERR_DISKLIKE:
738 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
739 "pool name is reserved"));
742 case NAME_ERR_LEADING_SLASH:
743 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
744 "leading slash in name"));
747 case NAME_ERR_EMPTY_COMPONENT:
748 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
749 "empty component in name"));
752 case NAME_ERR_TRAILING_SLASH:
753 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
754 "trailing slash in name"));
757 case NAME_ERR_MULTIPLE_AT:
758 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
759 "multiple '@' delimiters in name"));
762 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
763 "permission set is missing '@'"));
774 * Open a handle to the given pool, even if the pool is currently in the FAULTED
778 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
784 * Make sure the pool name is valid.
786 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
787 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
788 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
793 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
796 zhp->zpool_hdl = hdl;
797 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
799 if (zpool_refresh_stats(zhp, &missing) != 0) {
805 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
806 (void) zfs_error_fmt(hdl, EZFS_NOENT,
807 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
816 * Like the above, but silent on error. Used when iterating over pools (because
817 * the configuration cache may be out of date).
820 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
825 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
828 zhp->zpool_hdl = hdl;
829 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
831 if (zpool_refresh_stats(zhp, &missing) != 0) {
847 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
851 zpool_open(libzfs_handle_t *hdl, const char *pool)
855 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
858 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
859 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
860 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
869 * Close the handle. Simply frees the memory associated with the handle.
872 zpool_close(zpool_handle_t *zhp)
874 if (zhp->zpool_config)
875 nvlist_free(zhp->zpool_config);
876 if (zhp->zpool_old_config)
877 nvlist_free(zhp->zpool_old_config);
878 if (zhp->zpool_props)
879 nvlist_free(zhp->zpool_props);
884 * Return the name of the pool.
887 zpool_get_name(zpool_handle_t *zhp)
889 return (zhp->zpool_name);
894 * Return the state of the pool (ACTIVE or UNAVAILABLE)
897 zpool_get_state(zpool_handle_t *zhp)
899 return (zhp->zpool_state);
903 * Create the named pool, using the provided vdev list. It is assumed
904 * that the consumer has already validated the contents of the nvlist, so we
905 * don't have to worry about error semantics.
908 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
909 nvlist_t *props, nvlist_t *fsprops)
911 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
912 nvlist_t *zc_fsprops = NULL;
913 nvlist_t *zc_props = NULL;
918 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
919 "cannot create '%s'"), pool);
921 if (!zpool_name_valid(hdl, B_FALSE, pool))
922 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
924 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
928 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
930 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
931 SPA_VERSION_1, flags, msg)) == NULL) {
940 zoned = ((nvlist_lookup_string(fsprops,
941 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
942 strcmp(zonestr, "on") == 0);
944 if ((zc_fsprops = zfs_valid_proplist(hdl,
945 ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
949 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
952 if (nvlist_add_nvlist(zc_props,
953 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
958 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
961 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
963 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
965 zcmd_free_nvlists(&zc);
966 nvlist_free(zc_props);
967 nvlist_free(zc_fsprops);
972 * This can happen if the user has specified the same
973 * device multiple times. We can't reliably detect this
974 * until we try to add it and see we already have a
975 * label. This can also happen under if the device is
976 * part of an active md or lvm device.
978 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
979 "one or more vdevs refer to the same device, or one of\n"
980 "the devices is part of an active md or lvm device"));
981 return (zfs_error(hdl, EZFS_BADDEV, msg));
985 * This occurs when one of the devices is below
986 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
987 * device was the problem device since there's no
988 * reliable way to determine device size from userland.
993 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
995 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
996 "one or more devices is less than the "
997 "minimum size (%s)"), buf);
999 return (zfs_error(hdl, EZFS_BADDEV, msg));
1002 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1003 "one or more devices is out of space"));
1004 return (zfs_error(hdl, EZFS_BADDEV, msg));
1007 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1008 "cache device must be a disk or disk slice"));
1009 return (zfs_error(hdl, EZFS_BADDEV, msg));
1012 return (zpool_standard_error(hdl, errno, msg));
1017 * If this is an alternate root pool, then we automatically set the
1018 * mountpoint of the root dataset to be '/'.
1020 if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
1024 verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
1025 verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1032 zcmd_free_nvlists(&zc);
1033 nvlist_free(zc_props);
1034 nvlist_free(zc_fsprops);
1039 * Destroy the given pool. It is up to the caller to ensure that there are no
1040 * datasets left in the pool.
1043 zpool_destroy(zpool_handle_t *zhp)
1045 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1046 zfs_handle_t *zfp = NULL;
1047 libzfs_handle_t *hdl = zhp->zpool_hdl;
1050 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1051 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1054 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1056 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1057 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1058 "cannot destroy '%s'"), zhp->zpool_name);
1060 if (errno == EROFS) {
1061 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1062 "one or more devices is read only"));
1063 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1065 (void) zpool_standard_error(hdl, errno, msg);
1074 remove_mountpoint(zfp);
1082 * Add the given vdevs to the pool. The caller must have already performed the
1083 * necessary verification to ensure that the vdev specification is well-formed.
1086 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1088 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1090 libzfs_handle_t *hdl = zhp->zpool_hdl;
1092 nvlist_t **spares, **l2cache;
1093 uint_t nspares, nl2cache;
1095 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1096 "cannot add to '%s'"), zhp->zpool_name);
1098 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1099 SPA_VERSION_SPARES &&
1100 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1101 &spares, &nspares) == 0) {
1102 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1103 "upgraded to add hot spares"));
1104 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1107 if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1108 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1111 for (s = 0; s < nspares; s++) {
1114 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1115 &path) == 0 && pool_uses_efi(spares[s])) {
1116 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1117 "device '%s' contains an EFI label and "
1118 "cannot be used on root pools."),
1119 zpool_vdev_name(hdl, NULL, spares[s],
1121 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1126 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1127 SPA_VERSION_L2CACHE &&
1128 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1129 &l2cache, &nl2cache) == 0) {
1130 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1131 "upgraded to add cache devices"));
1132 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1135 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1137 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1139 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1143 * This can happen if the user has specified the same
1144 * device multiple times. We can't reliably detect this
1145 * until we try to add it and see we already have a
1148 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1149 "one or more vdevs refer to the same device"));
1150 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1155 * This occurrs when one of the devices is below
1156 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1157 * device was the problem device since there's no
1158 * reliable way to determine device size from userland.
1163 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1165 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1166 "device is less than the minimum "
1169 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1173 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1174 "pool must be upgraded to add these vdevs"));
1175 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1179 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1180 "root pool can not have multiple vdevs"
1181 " or separate logs"));
1182 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1186 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1187 "cache device must be a disk or disk slice"));
1188 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1192 (void) zpool_standard_error(hdl, errno, msg);
1200 zcmd_free_nvlists(&zc);
1206 * Exports the pool from the system. The caller must ensure that there are no
1207 * mounted datasets in the pool.
1210 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1212 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1215 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1216 "cannot export '%s'"), zhp->zpool_name);
1218 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1219 zc.zc_cookie = force;
1220 zc.zc_guid = hardforce;
1222 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1225 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1226 "use '-f' to override the following errors:\n"
1227 "'%s' has an active shared spare which could be"
1228 " used by other pools once '%s' is exported."),
1229 zhp->zpool_name, zhp->zpool_name);
1230 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1233 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1242 zpool_export(zpool_handle_t *zhp, boolean_t force)
1244 return (zpool_export_common(zhp, force, B_FALSE));
1248 zpool_export_force(zpool_handle_t *zhp)
1250 return (zpool_export_common(zhp, B_TRUE, B_TRUE));
1254 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1257 nvlist_t *nv = NULL;
1263 if (!hdl->libzfs_printerr || config == NULL)
1266 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0)
1269 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1271 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1273 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1274 strftime(timestr, 128, "%c", &t) != 0) {
1276 (void) printf(dgettext(TEXT_DOMAIN,
1277 "Would be able to return %s "
1278 "to its state as of %s.\n"),
1281 (void) printf(dgettext(TEXT_DOMAIN,
1282 "Pool %s returned to its state as of %s.\n"),
1286 (void) printf(dgettext(TEXT_DOMAIN,
1287 "%s approximately %lld "),
1288 dryrun ? "Would discard" : "Discarded",
1289 ((longlong_t)loss + 30) / 60);
1290 (void) printf(dgettext(TEXT_DOMAIN,
1291 "minutes of transactions.\n"));
1292 } else if (loss > 0) {
1293 (void) printf(dgettext(TEXT_DOMAIN,
1294 "%s approximately %lld "),
1295 dryrun ? "Would discard" : "Discarded",
1297 (void) printf(dgettext(TEXT_DOMAIN,
1298 "seconds of transactions.\n"));
1304 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1307 nvlist_t *nv = NULL;
1309 uint64_t edata = UINT64_MAX;
1314 if (!hdl->libzfs_printerr)
1318 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1320 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1322 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1323 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1324 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1327 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1328 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1331 (void) printf(dgettext(TEXT_DOMAIN,
1332 "Recovery is possible, but will result in some data loss.\n"));
1334 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1335 strftime(timestr, 128, "%c", &t) != 0) {
1336 (void) printf(dgettext(TEXT_DOMAIN,
1337 "\tReturning the pool to its state as of %s\n"
1338 "\tshould correct the problem. "),
1341 (void) printf(dgettext(TEXT_DOMAIN,
1342 "\tReverting the pool to an earlier state "
1343 "should correct the problem.\n\t"));
1347 (void) printf(dgettext(TEXT_DOMAIN,
1348 "Approximately %lld minutes of data\n"
1349 "\tmust be discarded, irreversibly. "),
1350 ((longlong_t)loss + 30) / 60);
1351 } else if (loss > 0) {
1352 (void) printf(dgettext(TEXT_DOMAIN,
1353 "Approximately %lld seconds of data\n"
1354 "\tmust be discarded, irreversibly. "),
1357 if (edata != 0 && edata != UINT64_MAX) {
1359 (void) printf(dgettext(TEXT_DOMAIN,
1360 "After rewind, at least\n"
1361 "\tone persistent user-data error will remain. "));
1363 (void) printf(dgettext(TEXT_DOMAIN,
1364 "After rewind, several\n"
1365 "\tpersistent user-data errors will remain. "));
1368 (void) printf(dgettext(TEXT_DOMAIN,
1369 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1370 reason >= 0 ? "clear" : "import", name);
1372 (void) printf(dgettext(TEXT_DOMAIN,
1373 "A scrub of the pool\n"
1374 "\tis strongly recommended after recovery.\n"));
1378 (void) printf(dgettext(TEXT_DOMAIN,
1379 "Destroy and re-create the pool from\n\ta backup source.\n"));
1383 * zpool_import() is a contracted interface. Should be kept the same
1386 * Applications should use zpool_import_props() to import a pool with
1387 * new properties value to be set.
1390 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1393 nvlist_t *props = NULL;
1396 if (altroot != NULL) {
1397 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1398 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1399 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1403 if (nvlist_add_string(props,
1404 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1405 nvlist_add_string(props,
1406 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1408 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1409 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1414 ret = zpool_import_props(hdl, config, newname, props,
1422 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1428 uint64_t is_log = 0;
1430 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1434 (void) printf("\t%*s%s%s\n", indent, "", name,
1435 is_log ? " [log]" : "");
1437 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1438 &child, &children) != 0)
1441 for (c = 0; c < children; c++) {
1442 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
1443 print_vdev_tree(hdl, vname, child[c], indent + 2);
1449 * Import the given pool using the known configuration and a list of
1450 * properties to be set. The configuration should have come from
1451 * zpool_find_import(). The 'newname' parameters control whether the pool
1452 * is imported with a different name.
1455 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1456 nvlist_t *props, int flags)
1458 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1459 zpool_rewind_policy_t policy;
1460 nvlist_t *nv = NULL;
1461 nvlist_t *nvinfo = NULL;
1462 nvlist_t *missing = NULL;
1469 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1472 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1473 "cannot import pool '%s'"), origname);
1475 if (newname != NULL) {
1476 if (!zpool_name_valid(hdl, B_FALSE, newname))
1477 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1478 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1480 thename = (char *)newname;
1487 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1489 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1492 if ((props = zpool_valid_proplist(hdl, origname,
1493 props, version, flags, errbuf)) == NULL) {
1495 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1501 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1503 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1506 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1510 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1515 zc.zc_cookie = flags;
1516 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1518 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1519 zcmd_free_nvlists(&zc);
1526 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1527 zpool_get_rewind_policy(config, &policy);
1533 * Dry-run failed, but we print out what success
1534 * looks like if we found a best txg
1536 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1537 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1543 if (newname == NULL)
1544 (void) snprintf(desc, sizeof (desc),
1545 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1548 (void) snprintf(desc, sizeof (desc),
1549 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1555 * Unsupported version.
1557 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1561 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1565 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1566 "one or more devices is read only"));
1567 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1571 if (nv && nvlist_lookup_nvlist(nv,
1572 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1573 nvlist_lookup_nvlist(nvinfo,
1574 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1575 (void) printf(dgettext(TEXT_DOMAIN,
1576 "The devices below are missing, use "
1577 "'-m' to import the pool anyway:\n"));
1578 print_vdev_tree(hdl, NULL, missing, 2);
1579 (void) printf("\n");
1581 (void) zpool_standard_error(hdl, error, desc);
1585 (void) zpool_standard_error(hdl, error, desc);
1589 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1590 "one or more devices are already in use\n"));
1591 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1595 (void) zpool_standard_error(hdl, error, desc);
1596 zpool_explain_recover(hdl,
1597 newname ? origname : thename, -error, nv);
1604 zpool_handle_t *zhp;
1607 * This should never fail, but play it safe anyway.
1609 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1611 else if (zhp != NULL)
1613 if (policy.zrp_request &
1614 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1615 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1616 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1622 zcmd_free_nvlists(&zc);
1632 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1634 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1636 libzfs_handle_t *hdl = zhp->zpool_hdl;
1638 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1639 zc.zc_cookie = func;
1641 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
1642 (errno == ENOENT && func != POOL_SCAN_NONE))
1645 if (func == POOL_SCAN_SCRUB) {
1646 (void) snprintf(msg, sizeof (msg),
1647 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1648 } else if (func == POOL_SCAN_NONE) {
1649 (void) snprintf(msg, sizeof (msg),
1650 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1653 assert(!"unexpected result");
1656 if (errno == EBUSY) {
1658 pool_scan_stat_t *ps = NULL;
1661 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1662 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1663 (void) nvlist_lookup_uint64_array(nvroot,
1664 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1665 if (ps && ps->pss_func == POOL_SCAN_SCRUB)
1666 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1668 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1669 } else if (errno == ENOENT) {
1670 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1672 return (zpool_standard_error(hdl, errno, msg));
1677 * Find a vdev that matches the search criteria specified. We use the
1678 * the nvpair name to determine how we should look for the device.
1679 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1680 * spare; but FALSE if its an INUSE spare.
1683 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1684 boolean_t *l2cache, boolean_t *log)
1691 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1693 /* Nothing to look for */
1694 if (search == NULL || pair == NULL)
1697 /* Obtain the key we will use to search */
1698 srchkey = nvpair_name(pair);
1700 switch (nvpair_type(pair)) {
1701 case DATA_TYPE_UINT64:
1702 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1703 uint64_t srchval, theguid;
1705 verify(nvpair_value_uint64(pair, &srchval) == 0);
1706 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1708 if (theguid == srchval)
1713 case DATA_TYPE_STRING: {
1714 char *srchval, *val;
1716 verify(nvpair_value_string(pair, &srchval) == 0);
1717 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1721 * Search for the requested value. Special cases:
1723 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in with a
1724 * partition suffix "1", "-part1", or "p1". The suffix is hidden
1725 * from the user, but included in the string, so this matches around
1727 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1729 * Otherwise, all other searches are simple string compares.
1731 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) {
1732 uint64_t wholedisk = 0;
1734 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1737 char buf[MAXPATHLEN];
1739 zfs_append_partition(srchval, buf, sizeof (buf));
1740 if (strcmp(val, buf) == 0)
1745 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
1746 char *type, *idx, *end, *p;
1747 uint64_t id, vdev_id;
1750 * Determine our vdev type, keeping in mind
1751 * that the srchval is composed of a type and
1752 * vdev id pair (i.e. mirror-4).
1754 if ((type = strdup(srchval)) == NULL)
1757 if ((p = strrchr(type, '-')) == NULL) {
1765 * If the types don't match then keep looking.
1767 if (strncmp(val, type, strlen(val)) != 0) {
1772 verify(strncmp(type, VDEV_TYPE_RAIDZ,
1773 strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1774 strncmp(type, VDEV_TYPE_MIRROR,
1775 strlen(VDEV_TYPE_MIRROR)) == 0);
1776 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
1780 vdev_id = strtoull(idx, &end, 10);
1787 * Now verify that we have the correct vdev id.
1796 if (strcmp(srchval, val) == 0)
1805 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1806 &child, &children) != 0)
1809 for (c = 0; c < children; c++) {
1810 if ((ret = vdev_to_nvlist_iter(child[c], search,
1811 avail_spare, l2cache, NULL)) != NULL) {
1813 * The 'is_log' value is only set for the toplevel
1814 * vdev, not the leaf vdevs. So we always lookup the
1815 * log device from the root of the vdev tree (where
1816 * 'log' is non-NULL).
1819 nvlist_lookup_uint64(child[c],
1820 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
1828 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1829 &child, &children) == 0) {
1830 for (c = 0; c < children; c++) {
1831 if ((ret = vdev_to_nvlist_iter(child[c], search,
1832 avail_spare, l2cache, NULL)) != NULL) {
1833 *avail_spare = B_TRUE;
1839 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1840 &child, &children) == 0) {
1841 for (c = 0; c < children; c++) {
1842 if ((ret = vdev_to_nvlist_iter(child[c], search,
1843 avail_spare, l2cache, NULL)) != NULL) {
1854 * Given a physical path (minus the "/devices" prefix), find the
1858 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
1859 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
1861 nvlist_t *search, *nvroot, *ret;
1863 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1864 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
1866 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1869 *avail_spare = B_FALSE;
1873 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1874 nvlist_free(search);
1880 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
1883 zpool_vdev_is_interior(const char *name)
1885 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1886 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
1892 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
1893 boolean_t *l2cache, boolean_t *log)
1895 char buf[MAXPATHLEN];
1897 nvlist_t *nvroot, *search, *ret;
1900 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1902 guid = strtoull(path, &end, 10);
1903 if (guid != 0 && *end == '\0') {
1904 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
1905 } else if (zpool_vdev_is_interior(path)) {
1906 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
1907 } else if (path[0] != '/') {
1908 if (zfs_resolve_shortname(path, buf, sizeof (buf)) < 0) {
1909 nvlist_free(search);
1912 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
1914 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
1917 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1920 *avail_spare = B_FALSE;
1924 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1925 nvlist_free(search);
1931 vdev_online(nvlist_t *nv)
1935 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
1936 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
1937 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
1944 * Helper function for zpool_get_physpaths().
1947 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
1948 size_t *bytes_written)
1950 size_t bytes_left, pos, rsz;
1954 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
1956 return (EZFS_NODEVICE);
1958 pos = *bytes_written;
1959 bytes_left = physpath_size - pos;
1960 format = (pos == 0) ? "%s" : " %s";
1962 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
1963 *bytes_written += rsz;
1965 if (rsz >= bytes_left) {
1966 /* if physpath was not copied properly, clear it */
1967 if (bytes_left != 0) {
1970 return (EZFS_NOSPC);
1976 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
1977 size_t *rsz, boolean_t is_spare)
1982 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
1983 return (EZFS_INVALCONFIG);
1985 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
1987 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
1988 * For a spare vdev, we only want to boot from the active
1993 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
1996 return (EZFS_INVALCONFIG);
1999 if (vdev_online(nv)) {
2000 if ((ret = vdev_get_one_physpath(nv, physpath,
2001 phypath_size, rsz)) != 0)
2004 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2005 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2006 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2011 if (nvlist_lookup_nvlist_array(nv,
2012 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2013 return (EZFS_INVALCONFIG);
2015 for (i = 0; i < count; i++) {
2016 ret = vdev_get_physpaths(child[i], physpath,
2017 phypath_size, rsz, is_spare);
2018 if (ret == EZFS_NOSPC)
2023 return (EZFS_POOL_INVALARG);
2027 * Get phys_path for a root pool config.
2028 * Return 0 on success; non-zero on failure.
2031 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2034 nvlist_t *vdev_root;
2041 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2043 return (EZFS_INVALCONFIG);
2045 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2046 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2047 &child, &count) != 0)
2048 return (EZFS_INVALCONFIG);
2051 * root pool can not have EFI labeled disks and can only have
2052 * a single top-level vdev.
2054 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
2055 pool_uses_efi(vdev_root))
2056 return (EZFS_POOL_INVALARG);
2058 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2061 /* No online devices */
2063 return (EZFS_NODEVICE);
2069 * Get phys_path for a root pool
2070 * Return 0 on success; non-zero on failure.
2073 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2075 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2080 * If the device has being dynamically expanded then we need to relabel
2081 * the disk to use the new unallocated space.
2084 zpool_relabel_disk(libzfs_handle_t *hdl, const char *path)
2089 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
2090 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2091 "relabel '%s': unable to open device: %d"), path, errno);
2092 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2096 * It's possible that we might encounter an error if the device
2097 * does not have any unallocated space left. If so, we simply
2098 * ignore that error and continue on.
2100 error = efi_use_whole_disk(fd);
2102 if (error && error != VT_ENOSPC) {
2103 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2104 "relabel '%s': unable to read disk capacity"), path);
2105 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2111 * Bring the specified vdev online. The 'flags' parameter is a set of the
2112 * ZFS_ONLINE_* flags.
2115 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2116 vdev_state_t *newstate)
2118 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2121 boolean_t avail_spare, l2cache, islog;
2122 libzfs_handle_t *hdl = zhp->zpool_hdl;
2124 if (flags & ZFS_ONLINE_EXPAND) {
2125 (void) snprintf(msg, sizeof (msg),
2126 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2128 (void) snprintf(msg, sizeof (msg),
2129 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2132 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2133 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2135 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2137 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2140 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2142 if (flags & ZFS_ONLINE_EXPAND ||
2143 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2144 char *pathname = NULL;
2145 uint64_t wholedisk = 0;
2147 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2149 verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
2153 * XXX - L2ARC 1.0 devices can't support expansion.
2156 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2157 "cannot expand cache devices"));
2158 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2162 pathname += strlen(DISK_ROOT) + 1;
2163 (void) zpool_relabel_disk(hdl, pathname);
2167 zc.zc_cookie = VDEV_STATE_ONLINE;
2170 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2171 if (errno == EINVAL) {
2172 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2173 "from this pool into a new one. Use '%s' "
2174 "instead"), "zpool detach");
2175 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2177 return (zpool_standard_error(hdl, errno, msg));
2180 *newstate = zc.zc_cookie;
2185 * Take the specified vdev offline
2188 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2190 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2193 boolean_t avail_spare, l2cache;
2194 libzfs_handle_t *hdl = zhp->zpool_hdl;
2196 (void) snprintf(msg, sizeof (msg),
2197 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2199 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2200 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2202 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2204 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2207 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2209 zc.zc_cookie = VDEV_STATE_OFFLINE;
2210 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2212 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2219 * There are no other replicas of this device.
2221 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2225 * The log device has unplayed logs
2227 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2230 return (zpool_standard_error(hdl, errno, msg));
2235 * Mark the given vdev faulted.
2238 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2240 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2242 libzfs_handle_t *hdl = zhp->zpool_hdl;
2244 (void) snprintf(msg, sizeof (msg),
2245 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
2247 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2249 zc.zc_cookie = VDEV_STATE_FAULTED;
2252 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2259 * There are no other replicas of this device.
2261 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2264 return (zpool_standard_error(hdl, errno, msg));
2270 * Mark the given vdev degraded.
2273 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2275 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2277 libzfs_handle_t *hdl = zhp->zpool_hdl;
2279 (void) snprintf(msg, sizeof (msg),
2280 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid);
2282 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2284 zc.zc_cookie = VDEV_STATE_DEGRADED;
2287 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2290 return (zpool_standard_error(hdl, errno, msg));
2294 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2298 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2304 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2306 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2309 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2310 children == 2 && child[which] == tgt)
2313 for (c = 0; c < children; c++)
2314 if (is_replacing_spare(child[c], tgt, which))
2322 * Attach new_disk (fully described by nvroot) to old_disk.
2323 * If 'replacing' is specified, the new disk will replace the old one.
2326 zpool_vdev_attach(zpool_handle_t *zhp,
2327 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2329 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2333 boolean_t avail_spare, l2cache, islog;
2338 nvlist_t *config_root;
2339 libzfs_handle_t *hdl = zhp->zpool_hdl;
2340 boolean_t rootpool = pool_is_bootable(zhp);
2343 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2344 "cannot replace %s with %s"), old_disk, new_disk);
2346 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2347 "cannot attach %s to %s"), new_disk, old_disk);
2350 * If this is a root pool, make sure that we're not attaching an
2351 * EFI labeled device.
2353 if (rootpool && pool_uses_efi(nvroot)) {
2354 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2355 "EFI labeled devices are not supported on root pools."));
2356 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2359 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2360 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2362 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2365 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2368 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2370 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2371 zc.zc_cookie = replacing;
2373 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2374 &child, &children) != 0 || children != 1) {
2375 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2376 "new device must be a single disk"));
2377 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2380 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2381 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2383 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2387 * If the target is a hot spare that has been swapped in, we can only
2388 * replace it with another hot spare.
2391 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2392 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2393 NULL) == NULL || !avail_spare) &&
2394 is_replacing_spare(config_root, tgt, 1)) {
2395 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2396 "can only be replaced by another hot spare"));
2398 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2403 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2406 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2408 zcmd_free_nvlists(&zc);
2413 * XXX need a better way to prevent user from
2414 * booting up a half-baked vdev.
2416 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2417 "sure to wait until resilver is done "
2418 "before rebooting.\n"));
2426 * Can't attach to or replace this type of vdev.
2429 uint64_t version = zpool_get_prop_int(zhp,
2430 ZPOOL_PROP_VERSION, NULL);
2433 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2434 "cannot replace a log with a spare"));
2435 else if (version >= SPA_VERSION_MULTI_REPLACE)
2436 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2437 "already in replacing/spare config; wait "
2438 "for completion or use 'zpool detach'"));
2440 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2441 "cannot replace a replacing device"));
2443 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2444 "can only attach to mirrors and top-level "
2447 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2452 * The new device must be a single disk.
2454 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2455 "new device must be a single disk"));
2456 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2460 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2462 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2467 * The new device is too small.
2469 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2470 "device is too small"));
2471 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2476 * The new device has a different alignment requirement.
2478 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2479 "devices have different sector alignment"));
2480 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2485 * The resulting top-level vdev spec won't fit in the label.
2487 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2491 (void) zpool_standard_error(hdl, errno, msg);
2498 * Detach the specified device.
2501 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2503 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2506 boolean_t avail_spare, l2cache;
2507 libzfs_handle_t *hdl = zhp->zpool_hdl;
2509 (void) snprintf(msg, sizeof (msg),
2510 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2512 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2513 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2515 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2518 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2521 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2523 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2525 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2532 * Can't detach from this type of vdev.
2534 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2535 "applicable to mirror and replacing vdevs"));
2536 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2541 * There are no other replicas of this device.
2543 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2547 (void) zpool_standard_error(hdl, errno, msg);
2554 * Find a mirror vdev in the source nvlist.
2556 * The mchild array contains a list of disks in one of the top-level mirrors
2557 * of the source pool. The schild array contains a list of disks that the
2558 * user specified on the command line. We loop over the mchild array to
2559 * see if any entry in the schild array matches.
2561 * If a disk in the mchild array is found in the schild array, we return
2562 * the index of that entry. Otherwise we return -1.
2565 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2566 nvlist_t **schild, uint_t schildren)
2570 for (mc = 0; mc < mchildren; mc++) {
2572 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2573 mchild[mc], B_FALSE);
2575 for (sc = 0; sc < schildren; sc++) {
2576 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2577 schild[sc], B_FALSE);
2578 boolean_t result = (strcmp(mpath, spath) == 0);
2594 * Split a mirror pool. If newroot points to null, then a new nvlist
2595 * is generated and it is the responsibility of the caller to free it.
2598 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2599 nvlist_t *props, splitflags_t flags)
2601 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2603 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2604 nvlist_t **varray = NULL, *zc_props = NULL;
2605 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2606 libzfs_handle_t *hdl = zhp->zpool_hdl;
2608 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2611 (void) snprintf(msg, sizeof (msg),
2612 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2614 if (!zpool_name_valid(hdl, B_FALSE, newname))
2615 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2617 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2618 (void) fprintf(stderr, gettext("Internal error: unable to "
2619 "retrieve pool configuration\n"));
2623 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2625 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2628 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2629 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2630 props, vers, flags, msg)) == NULL)
2634 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2636 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2637 "Source pool is missing vdev tree"));
2639 nvlist_free(zc_props);
2643 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2646 if (*newroot == NULL ||
2647 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2648 &newchild, &newchildren) != 0)
2651 for (c = 0; c < children; c++) {
2652 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2654 nvlist_t **mchild, *vdev;
2659 * Unlike cache & spares, slogs are stored in the
2660 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2662 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2664 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2666 if (is_log || is_hole) {
2668 * Create a hole vdev and put it in the config.
2670 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2672 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2673 VDEV_TYPE_HOLE) != 0)
2675 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2680 varray[vcount++] = vdev;
2684 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
2686 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2687 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2688 "Source pool must be composed only of mirrors\n"));
2689 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2693 verify(nvlist_lookup_nvlist_array(child[c],
2694 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2696 /* find or add an entry for this top-level vdev */
2697 if (newchildren > 0 &&
2698 (entry = find_vdev_entry(zhp, mchild, mchildren,
2699 newchild, newchildren)) >= 0) {
2700 /* We found a disk that the user specified. */
2701 vdev = mchild[entry];
2704 /* User didn't specify a disk for this vdev. */
2705 vdev = mchild[mchildren - 1];
2708 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
2712 /* did we find every disk the user specified? */
2713 if (found != newchildren) {
2714 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
2715 "include at most one disk from each mirror"));
2716 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2720 /* Prepare the nvlist for populating. */
2721 if (*newroot == NULL) {
2722 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
2725 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
2726 VDEV_TYPE_ROOT) != 0)
2729 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
2732 /* Add all the children we found */
2733 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
2734 lastlog == 0 ? vcount : lastlog) != 0)
2738 * If we're just doing a dry run, exit now with success.
2741 memory_err = B_FALSE;
2746 /* now build up the config list & call the ioctl */
2747 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
2750 if (nvlist_add_nvlist(newconfig,
2751 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
2752 nvlist_add_string(newconfig,
2753 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
2754 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
2758 * The new pool is automatically part of the namespace unless we
2759 * explicitly export it.
2762 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
2763 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2764 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
2765 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
2767 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
2770 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
2771 retval = zpool_standard_error(hdl, errno, msg);
2776 memory_err = B_FALSE;
2779 if (varray != NULL) {
2782 for (v = 0; v < vcount; v++)
2783 nvlist_free(varray[v]);
2786 zcmd_free_nvlists(&zc);
2788 nvlist_free(zc_props);
2790 nvlist_free(newconfig);
2792 nvlist_free(*newroot);
2800 return (no_memory(hdl));
2806 * Remove the given device. Currently, this is supported only for hot spares
2807 * and level 2 cache devices.
2810 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
2812 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2815 boolean_t avail_spare, l2cache, islog;
2816 libzfs_handle_t *hdl = zhp->zpool_hdl;
2819 (void) snprintf(msg, sizeof (msg),
2820 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
2822 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2823 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2825 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2827 * XXX - this should just go away.
2829 if (!avail_spare && !l2cache && !islog) {
2830 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2831 "only inactive hot spares, cache, top-level, "
2832 "or log devices can be removed"));
2833 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2836 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
2837 if (islog && version < SPA_VERSION_HOLES) {
2838 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2839 "pool must be upgrade to support log removal"));
2840 return (zfs_error(hdl, EZFS_BADVERSION, msg));
2843 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2845 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
2848 return (zpool_standard_error(hdl, errno, msg));
2852 * Clear the errors for the pool, or the particular device if specified.
2855 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
2857 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2860 zpool_rewind_policy_t policy;
2861 boolean_t avail_spare, l2cache;
2862 libzfs_handle_t *hdl = zhp->zpool_hdl;
2863 nvlist_t *nvi = NULL;
2867 (void) snprintf(msg, sizeof (msg),
2868 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2871 (void) snprintf(msg, sizeof (msg),
2872 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2875 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2877 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
2878 &l2cache, NULL)) == 0)
2879 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2882 * Don't allow error clearing for hot spares. Do allow
2883 * error clearing for l2cache devices.
2886 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2888 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
2892 zpool_get_rewind_policy(rewindnvl, &policy);
2893 zc.zc_cookie = policy.zrp_request;
2895 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
2898 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
2901 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
2903 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
2904 zcmd_free_nvlists(&zc);
2909 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
2910 errno != EPERM && errno != EACCES)) {
2911 if (policy.zrp_request &
2912 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2913 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
2914 zpool_rewind_exclaim(hdl, zc.zc_name,
2915 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
2919 zcmd_free_nvlists(&zc);
2923 zcmd_free_nvlists(&zc);
2924 return (zpool_standard_error(hdl, errno, msg));
2928 * Similar to zpool_clear(), but takes a GUID (used by fmd).
2931 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
2933 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2935 libzfs_handle_t *hdl = zhp->zpool_hdl;
2937 (void) snprintf(msg, sizeof (msg),
2938 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
2939 (u_longlong_t)guid);
2941 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2943 zc.zc_cookie = ZPOOL_NO_REWIND;
2945 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
2948 return (zpool_standard_error(hdl, errno, msg));
2952 * Convert from a devid string to a path.
2955 devid_to_path(char *devid_str)
2960 devid_nmlist_t *list = NULL;
2963 if (devid_str_decode(devid_str, &devid, &minor) != 0)
2966 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
2968 devid_str_free(minor);
2974 if ((path = strdup(list[0].devname)) == NULL)
2977 devid_free_nmlist(list);
2983 * Convert from a path to a devid string.
2986 path_to_devid(const char *path)
2992 if ((fd = open(path, O_RDONLY)) < 0)
2997 if (devid_get(fd, &devid) == 0) {
2998 if (devid_get_minor_name(fd, &minor) == 0)
2999 ret = devid_str_encode(devid, minor);
3001 devid_str_free(minor);
3010 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3011 * ignore any failure here, since a common case is for an unprivileged user to
3012 * type 'zpool status', and we'll display the correct information anyway.
3015 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3017 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3019 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3020 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3021 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3024 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3028 * Remove partition suffix from a vdev path. Partition suffixes may take three
3029 * forms: "-partX", "pX", or "X", where X is a string of digits. The second
3030 * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
3031 * third case only occurs when preceded by a string matching the regular
3032 * expression "^[hs]d[a-z]+", i.e. a scsi or ide disk.
3035 strip_partition(libzfs_handle_t *hdl, char *path)
3037 char *tmp = zfs_strdup(hdl, path);
3038 char *part = NULL, *d = NULL;
3040 if ((part = strstr(tmp, "-part")) && part != tmp) {
3042 } else if ((part = strrchr(tmp, 'p')) &&
3043 part > tmp + 1 && isdigit(*(part-1))) {
3045 } else if ((tmp[0] == 'h' || tmp[0] == 's') && tmp[1] == 'd') {
3046 for (d = &tmp[2]; isalpha(*d); part = ++d);
3048 if (part && d && *d != '\0') {
3049 for (; isdigit(*d); d++);
3056 #define PATH_BUF_LEN 64
3059 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3060 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3061 * We also check if this is a whole disk, in which case we strip off the
3062 * trailing 's0' slice name.
3064 * This routine is also responsible for identifying when disks have been
3065 * reconfigured in a new location. The kernel will have opened the device by
3066 * devid, but the path will still refer to the old location. To catch this, we
3067 * first do a path -> devid translation (which is fast for the common case). If
3068 * the devid matches, we're done. If not, we do a reverse devid -> path
3069 * translation and issue the appropriate ioctl() to update the path of the vdev.
3070 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3074 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3077 char *path, *devid, *type;
3079 char buf[PATH_BUF_LEN];
3083 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
3085 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3087 (void) snprintf(buf, sizeof (buf), "%llu",
3088 (u_longlong_t)value);
3090 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3092 * If the device is dead (faulted, offline, etc) then don't
3093 * bother opening it. Otherwise we may be forcing the user to
3094 * open a misbehaving device, which can have undesirable
3097 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3098 (uint64_t **)&vs, &vsc) != 0 ||
3099 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3101 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3103 * Determine if the current path is correct.
3105 char *newdevid = path_to_devid(path);
3107 if (newdevid == NULL ||
3108 strcmp(devid, newdevid) != 0) {
3111 if ((newpath = devid_to_path(devid)) != NULL) {
3113 * Update the path appropriately.
3115 set_path(zhp, nv, newpath);
3116 if (nvlist_add_string(nv,
3117 ZPOOL_CONFIG_PATH, newpath) == 0)
3118 verify(nvlist_lookup_string(nv,
3126 devid_str_free(newdevid);
3130 * For a block device only use the name.
3132 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
3133 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
3134 path = strrchr(path, '/');
3139 * Remove the partition from the path it this is a whole disk.
3141 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3142 &value) == 0 && value) {
3143 return strip_partition(hdl, path);
3146 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3149 * If it's a raidz device, we need to stick in the parity level.
3151 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3152 char tmpbuf[PATH_BUF_LEN];
3154 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3156 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s%llu", path,
3157 (u_longlong_t)value);
3162 * We identify each top-level vdev by using a <type-id>
3163 * naming convention.
3168 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3170 (void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3176 return (zfs_strdup(hdl, path));
3180 zbookmark_compare(const void *a, const void *b)
3182 return (memcmp(a, b, sizeof (zbookmark_t)));
3186 * Retrieve the persistent error log, uniquify the members, and return to the
3190 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3192 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3194 zbookmark_t *zb = NULL;
3198 * Retrieve the raw error list from the kernel. If the number of errors
3199 * has increased, allocate more space and continue until we get the
3202 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3206 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3207 count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
3209 zc.zc_nvlist_dst_size = count;
3210 (void) strcpy(zc.zc_name, zhp->zpool_name);
3212 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3214 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3215 if (errno == ENOMEM) {
3216 count = zc.zc_nvlist_dst_size;
3217 if ((zc.zc_nvlist_dst = (uintptr_t)
3218 zfs_alloc(zhp->zpool_hdl, count *
3219 sizeof (zbookmark_t))) == (uintptr_t)NULL)
3230 * Sort the resulting bookmarks. This is a little confusing due to the
3231 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3232 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3233 * _not_ copied as part of the process. So we point the start of our
3234 * array appropriate and decrement the total number of elements.
3236 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
3237 zc.zc_nvlist_dst_size;
3238 count -= zc.zc_nvlist_dst_size;
3240 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
3242 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3245 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3247 for (i = 0; i < count; i++) {
3250 /* ignoring zb_blkid and zb_level for now */
3251 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3252 zb[i-1].zb_object == zb[i].zb_object)
3255 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3257 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3258 zb[i].zb_objset) != 0) {
3262 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3263 zb[i].zb_object) != 0) {
3267 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3274 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3278 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3279 return (no_memory(zhp->zpool_hdl));
3283 * Upgrade a ZFS pool to the latest on-disk version.
3286 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3288 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3289 libzfs_handle_t *hdl = zhp->zpool_hdl;
3291 (void) strcpy(zc.zc_name, zhp->zpool_name);
3292 zc.zc_cookie = new_version;
3294 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3295 return (zpool_standard_error_fmt(hdl, errno,
3296 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3302 zpool_set_history_str(const char *subcommand, int argc, char **argv,
3307 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
3308 for (i = 1; i < argc; i++) {
3309 if (strlen(history_str) + 1 + strlen(argv[i]) >
3312 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
3313 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
3318 * Stage command history for logging.
3321 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
3323 if (history_str == NULL)
3326 if (strlen(history_str) > HIS_MAX_RECORD_LEN)
3329 if (hdl->libzfs_log_str != NULL)
3330 free(hdl->libzfs_log_str);
3332 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
3333 return (no_memory(hdl));
3339 * Perform ioctl to get some command history of a pool.
3341 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3342 * logical offset of the history buffer to start reading from.
3344 * Upon return, 'off' is the next logical offset to read from and
3345 * 'len' is the actual amount of bytes read into 'buf'.
3348 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3350 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3351 libzfs_handle_t *hdl = zhp->zpool_hdl;
3353 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3355 zc.zc_history = (uint64_t)(uintptr_t)buf;
3356 zc.zc_history_len = *len;
3357 zc.zc_history_offset = *off;
3359 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3362 return (zfs_error_fmt(hdl, EZFS_PERM,
3363 dgettext(TEXT_DOMAIN,
3364 "cannot show history for pool '%s'"),
3367 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3368 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3369 "'%s'"), zhp->zpool_name));
3371 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3372 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3373 "'%s', pool must be upgraded"), zhp->zpool_name));
3375 return (zpool_standard_error_fmt(hdl, errno,
3376 dgettext(TEXT_DOMAIN,
3377 "cannot get history for '%s'"), zhp->zpool_name));
3381 *len = zc.zc_history_len;
3382 *off = zc.zc_history_offset;
3388 * Process the buffer of nvlists, unpacking and storing each nvlist record
3389 * into 'records'. 'leftover' is set to the number of bytes that weren't
3390 * processed as there wasn't a complete record.
3393 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3394 nvlist_t ***records, uint_t *numrecords)
3400 while (bytes_read > sizeof (reclen)) {
3402 /* get length of packed record (stored as little endian) */
3403 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3404 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3406 if (bytes_read < sizeof (reclen) + reclen)
3410 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3412 bytes_read -= sizeof (reclen) + reclen;
3413 buf += sizeof (reclen) + reclen;
3415 /* add record to nvlist array */
3417 if (ISP2(*numrecords + 1)) {
3418 *records = realloc(*records,
3419 *numrecords * 2 * sizeof (nvlist_t *));
3421 (*records)[*numrecords - 1] = nv;
3424 *leftover = bytes_read;
3428 #define HIS_BUF_LEN (128*1024)
3431 * Retrieve the command history of a pool.
3434 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3436 char buf[HIS_BUF_LEN];
3438 nvlist_t **records = NULL;
3439 uint_t numrecords = 0;
3443 uint64_t bytes_read = sizeof (buf);
3446 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3449 /* if nothing else was read in, we're at EOF, just return */
3453 if ((err = zpool_history_unpack(buf, bytes_read,
3454 &leftover, &records, &numrecords)) != 0)
3462 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3463 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3464 records, numrecords) == 0);
3466 for (i = 0; i < numrecords; i++)
3467 nvlist_free(records[i]);
3474 * Retrieve the next event. If there is a new event available 'nvp' will
3475 * contain a newly allocated nvlist and 'dropped' will be set to the number
3476 * of missed events since the last call to this function. When 'nvp' is
3477 * set to NULL it indicates no new events are available. In either case
3478 * the function returns 0 and it is up to the caller to free 'nvp'. In
3479 * the case of a fatal error the function will return a non-zero value.
3480 * When the function is called in blocking mode it will not return until
3481 * a new event is available.
3484 zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
3485 int *dropped, int block, int cleanup_fd)
3487 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3492 zc.zc_cleanup_fd = cleanup_fd;
3495 zc.zc_guid = ZEVENT_NONBLOCK;
3497 if (zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE) != 0)
3501 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
3504 error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
3505 dgettext(TEXT_DOMAIN, "zfs shutdown"));
3508 /* Blocking error case should not occur */
3510 error = zpool_standard_error_fmt(hdl, errno,
3511 dgettext(TEXT_DOMAIN, "cannot get event"));
3515 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3516 error = zfs_error_fmt(hdl, EZFS_NOMEM,
3517 dgettext(TEXT_DOMAIN, "cannot get event"));
3523 error = zpool_standard_error_fmt(hdl, errno,
3524 dgettext(TEXT_DOMAIN, "cannot get event"));
3529 error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
3533 *dropped = (int)zc.zc_cookie;
3535 zcmd_free_nvlists(&zc);
3544 zpool_events_clear(libzfs_handle_t *hdl, int *count)
3546 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3549 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3550 "cannot clear events"));
3552 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
3553 return (zpool_standard_error_fmt(hdl, errno, msg));
3556 *count = (int)zc.zc_cookie; /* # of events cleared */
3562 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3563 char *pathname, size_t len)
3565 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3566 boolean_t mounted = B_FALSE;
3567 char *mntpnt = NULL;
3568 char dsname[MAXNAMELEN];
3571 /* special case for the MOS */
3572 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", (longlong_t)obj);
3576 /* get the dataset's name */
3577 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3579 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3580 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3581 /* just write out a path of two object numbers */
3582 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3583 (longlong_t)dsobj, (longlong_t)obj);
3586 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3588 /* find out if the dataset is mounted */
3589 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3591 /* get the corrupted object's path */
3592 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3594 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3597 (void) snprintf(pathname, len, "%s%s", mntpnt,
3600 (void) snprintf(pathname, len, "%s:%s",
3601 dsname, zc.zc_value);
3604 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, (longlong_t)obj);
3610 * Read the EFI label from the config, if a label does not exist then
3611 * pass back the error to the caller. If the caller has passed a non-NULL
3612 * diskaddr argument then we set it to the starting address of the EFI
3616 read_efi_label(nvlist_t *config, diskaddr_t *sb)
3620 char diskname[MAXPATHLEN];
3623 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3626 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
3627 strrchr(path, '/'));
3628 if ((fd = open(diskname, O_RDWR|O_DIRECT)) >= 0) {
3629 struct dk_gpt *vtoc;
3631 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3633 *sb = vtoc->efi_parts[0].p_start;
3642 * determine where a partition starts on a disk in the current
3646 find_start_block(nvlist_t *config)
3650 diskaddr_t sb = MAXOFFSET_T;
3653 if (nvlist_lookup_nvlist_array(config,
3654 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3655 if (nvlist_lookup_uint64(config,
3656 ZPOOL_CONFIG_WHOLE_DISK,
3657 &wholedisk) != 0 || !wholedisk) {
3658 return (MAXOFFSET_T);
3660 if (read_efi_label(config, &sb) < 0)
3665 for (c = 0; c < children; c++) {
3666 sb = find_start_block(child[c]);
3667 if (sb != MAXOFFSET_T) {
3671 return (MAXOFFSET_T);
3675 zpool_label_disk_wait(char *path, int timeout)
3677 struct stat64 statbuf;
3681 * Wait timeout miliseconds for a newly created device to be available
3682 * from the given path. There is a small window when a /dev/ device
3683 * will exist and the udev link will not, so we must wait for the
3684 * symlink. Depending on the udev rules this may take a few seconds.
3686 for (i = 0; i < timeout; i++) {
3690 if ((stat64(path, &statbuf) == 0) && (errno == 0))
3698 zpool_label_disk_check(char *path)
3700 struct dk_gpt *vtoc;
3703 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0)
3706 if ((err = efi_alloc_and_read(fd, &vtoc)) != 0) {
3711 if (vtoc->efi_flags & EFI_GPT_PRIMARY_CORRUPT) {
3723 * Label an individual disk. The name provided is the short name,
3724 * stripped of any leading /dev path.
3727 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
3729 char path[MAXPATHLEN];
3730 struct dk_gpt *vtoc;
3732 size_t resv = EFI_MIN_RESV_SIZE;
3733 uint64_t slice_size;
3734 diskaddr_t start_block;
3737 /* prepare an error message just in case */
3738 (void) snprintf(errbuf, sizeof (errbuf),
3739 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3744 if (pool_is_bootable(zhp)) {
3745 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3746 "EFI labeled devices are not supported on root "
3748 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3751 verify(nvlist_lookup_nvlist(zhp->zpool_config,
3752 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3754 if (zhp->zpool_start_block == 0)
3755 start_block = find_start_block(nvroot);
3757 start_block = zhp->zpool_start_block;
3758 zhp->zpool_start_block = start_block;
3761 start_block = NEW_START_BLOCK;
3764 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
3767 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
3769 * This shouldn't happen. We've long since verified that this
3770 * is a valid device.
3772 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
3773 "label '%s': unable to open device: %d"), path, errno);
3774 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
3777 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
3779 * The only way this can fail is if we run out of memory, or we
3780 * were unable to read the disk's capacity
3782 if (errno == ENOMEM)
3783 (void) no_memory(hdl);
3786 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
3787 "label '%s': unable to read disk capacity"), path);
3789 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
3792 slice_size = vtoc->efi_last_u_lba + 1;
3793 slice_size -= EFI_MIN_RESV_SIZE;
3794 if (start_block == MAXOFFSET_T)
3795 start_block = NEW_START_BLOCK;
3796 slice_size -= start_block;
3797 slice_size = P2ALIGN(slice_size, PARTITION_END_ALIGNMENT);
3799 vtoc->efi_parts[0].p_start = start_block;
3800 vtoc->efi_parts[0].p_size = slice_size;
3803 * Why we use V_USR: V_BACKUP confuses users, and is considered
3804 * disposable by some EFI utilities (since EFI doesn't have a backup
3805 * slice). V_UNASSIGNED is supposed to be used only for zero size
3806 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
3807 * etc. were all pretty specific. V_USR is as close to reality as we
3808 * can get, in the absence of V_OTHER.
3810 vtoc->efi_parts[0].p_tag = V_USR;
3811 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
3813 vtoc->efi_parts[8].p_start = slice_size + start_block;
3814 vtoc->efi_parts[8].p_size = resv;
3815 vtoc->efi_parts[8].p_tag = V_RESERVED;
3817 if ((rval = efi_write(fd, vtoc)) != 0) {
3819 * Some block drivers (like pcata) may not support EFI
3820 * GPT labels. Print out a helpful error message dir-
3821 * ecting the user to manually label the disk and give
3827 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "try using "
3828 "parted(8) and then provide a specific slice: %d"), rval);
3829 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3835 /* Wait for the first expected slice to appear. */
3836 (void) snprintf(path, sizeof (path), "%s/%s%s%s", DISK_ROOT, name,
3837 isdigit(name[strlen(name)-1]) ? "p" : "", FIRST_SLICE);
3838 rval = zpool_label_disk_wait(path, 3000);
3840 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "failed to "
3841 "detect device partitions on '%s': %d"), path, rval);
3842 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3845 /* We can't be to paranoid. Read the label back and verify it. */
3846 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
3847 rval = zpool_label_disk_check(path);
3849 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "freshly written "
3850 "EFI label on '%s' is damaged. Ensure\nthis device "
3851 "is not in in use, and is functioning properly: %d"),
3853 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));