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 (void) zfs_nicenum(intval, buf, len);
276 case ZPOOL_PROP_CAPACITY:
277 (void) snprintf(buf, len, "%llu%%",
278 (u_longlong_t)intval);
281 case ZPOOL_PROP_DEDUPRATIO:
282 (void) snprintf(buf, len, "%llu.%02llux",
283 (u_longlong_t)(intval / 100),
284 (u_longlong_t)(intval % 100));
287 case ZPOOL_PROP_HEALTH:
288 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
289 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
290 verify(nvlist_lookup_uint64_array(nvroot,
291 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
294 (void) strlcpy(buf, zpool_state_to_name(intval,
298 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
302 case PROP_TYPE_INDEX:
303 intval = zpool_get_prop_int(zhp, prop, &src);
304 if (zpool_prop_index_to_string(prop, intval, &strval)
307 (void) strlcpy(buf, strval, len);
321 * Check if the bootfs name has the same pool name as it is set to.
322 * Assuming bootfs is a valid dataset name.
325 bootfs_name_valid(const char *pool, char *bootfs)
327 int len = strlen(pool);
329 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
332 if (strncmp(pool, bootfs, len) == 0 &&
333 (bootfs[len] == '/' || bootfs[len] == '\0'))
340 * Inspect the configuration to determine if any of the devices contain
344 pool_uses_efi(nvlist_t *config)
349 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
350 &child, &children) != 0)
351 return (read_efi_label(config, NULL) >= 0);
353 for (c = 0; c < children; c++) {
354 if (pool_uses_efi(child[c]))
361 pool_is_bootable(zpool_handle_t *zhp)
363 char bootfs[ZPOOL_MAXNAMELEN];
365 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
366 sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
367 sizeof (bootfs)) != 0);
372 * Given an nvlist of zpool properties to be set, validate that they are
373 * correct, and parse any numeric properties (index, boolean, etc) if they are
374 * specified as strings.
377 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
378 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
386 struct stat64 statbuf;
390 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
391 (void) no_memory(hdl);
396 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
397 const char *propname = nvpair_name(elem);
400 * Make sure this property is valid and applies to this type.
402 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
403 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
404 "invalid property '%s'"), propname);
405 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
409 if (zpool_prop_readonly(prop)) {
410 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
411 "is readonly"), propname);
412 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
416 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
417 &strval, &intval, errbuf) != 0)
421 * Perform additional checking for specific properties.
426 case ZPOOL_PROP_VERSION:
427 if (intval < version || intval > SPA_VERSION) {
428 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
429 "property '%s' number %d is invalid."),
431 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
436 case ZPOOL_PROP_BOOTFS:
437 if (flags.create || flags.import) {
438 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
439 "property '%s' cannot be set at creation "
440 "or import time"), propname);
441 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
445 if (version < SPA_VERSION_BOOTFS) {
446 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
447 "pool must be upgraded to support "
448 "'%s' property"), propname);
449 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
454 * bootfs property value has to be a dataset name and
455 * the dataset has to be in the same pool as it sets to.
457 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
459 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
460 "is an invalid name"), strval);
461 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
465 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
466 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
467 "could not open pool '%s'"), poolname);
468 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
471 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
472 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
475 * bootfs property cannot be set on a disk which has
478 if (pool_uses_efi(nvroot)) {
479 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
480 "property '%s' not supported on "
481 "EFI labeled devices"), propname);
482 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
489 case ZPOOL_PROP_ALTROOT:
490 if (!flags.create && !flags.import) {
491 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
492 "property '%s' can only be set during pool "
493 "creation or import"), propname);
494 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
498 if (strval[0] != '/') {
499 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
500 "bad alternate root '%s'"), strval);
501 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
506 case ZPOOL_PROP_CACHEFILE:
507 if (strval[0] == '\0')
510 if (strcmp(strval, "none") == 0)
513 if (strval[0] != '/') {
514 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
515 "property '%s' must be empty, an "
516 "absolute path, or 'none'"), propname);
517 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
521 slash = strrchr(strval, '/');
523 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
524 strcmp(slash, "/..") == 0) {
525 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
526 "'%s' is not a valid file"), strval);
527 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
533 if (strval[0] != '\0' &&
534 (stat64(strval, &statbuf) != 0 ||
535 !S_ISDIR(statbuf.st_mode))) {
536 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
537 "'%s' is not a valid directory"),
539 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
546 case ZPOOL_PROP_READONLY:
548 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
549 "property '%s' can only be set at "
550 "import time"), propname);
551 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
560 nvlist_free(retprops);
565 * Set zpool property : propname=propval.
568 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
570 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
573 nvlist_t *nvl = NULL;
576 prop_flags_t flags = { 0 };
578 (void) snprintf(errbuf, sizeof (errbuf),
579 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
582 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
583 return (no_memory(zhp->zpool_hdl));
585 if (nvlist_add_string(nvl, propname, propval) != 0) {
587 return (no_memory(zhp->zpool_hdl));
590 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
591 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
592 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
601 * Execute the corresponding ioctl() to set this property.
603 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
605 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
610 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
612 zcmd_free_nvlists(&zc);
616 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
618 (void) zpool_props_refresh(zhp);
624 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
626 libzfs_handle_t *hdl = zhp->zpool_hdl;
628 char buf[ZFS_MAXPROPLEN];
630 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
633 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
638 if (entry->pl_prop != ZPROP_INVAL &&
639 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
641 if (strlen(buf) > entry->pl_width)
642 entry->pl_width = strlen(buf);
651 * Don't start the slice at the default block of 34; many storage
652 * devices will use a stripe width of 128k, other vendors prefer a 1m
653 * alignment. It is best to play it safe and ensure a 1m alignment
654 * give 512b blocks. When the block size is larger by a power of 2
655 * we will still be 1m aligned.
657 #define NEW_START_BLOCK 2048
660 * Validate the given pool name, optionally putting an extended error message in
664 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
670 ret = pool_namecheck(pool, &why, &what);
673 * The rules for reserved pool names were extended at a later point.
674 * But we need to support users with existing pools that may now be
675 * invalid. So we only check for this expanded set of names during a
676 * create (or import), and only in userland.
678 if (ret == 0 && !isopen &&
679 (strncmp(pool, "mirror", 6) == 0 ||
680 strncmp(pool, "raidz", 5) == 0 ||
681 strncmp(pool, "spare", 5) == 0 ||
682 strcmp(pool, "log") == 0)) {
685 dgettext(TEXT_DOMAIN, "name is reserved"));
693 case NAME_ERR_TOOLONG:
695 dgettext(TEXT_DOMAIN, "name is too long"));
698 case NAME_ERR_INVALCHAR:
700 dgettext(TEXT_DOMAIN, "invalid character "
701 "'%c' in pool name"), what);
704 case NAME_ERR_NOLETTER:
705 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
706 "name must begin with a letter"));
709 case NAME_ERR_RESERVED:
710 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
711 "name is reserved"));
714 case NAME_ERR_DISKLIKE:
715 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
716 "pool name is reserved"));
719 case NAME_ERR_LEADING_SLASH:
720 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
721 "leading slash in name"));
724 case NAME_ERR_EMPTY_COMPONENT:
725 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
726 "empty component in name"));
729 case NAME_ERR_TRAILING_SLASH:
730 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
731 "trailing slash in name"));
734 case NAME_ERR_MULTIPLE_AT:
735 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
736 "multiple '@' delimiters in name"));
739 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
740 "permission set is missing '@'"));
751 * Open a handle to the given pool, even if the pool is currently in the FAULTED
755 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
761 * Make sure the pool name is valid.
763 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
764 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
765 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
770 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
773 zhp->zpool_hdl = hdl;
774 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
776 if (zpool_refresh_stats(zhp, &missing) != 0) {
782 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
783 (void) zfs_error_fmt(hdl, EZFS_NOENT,
784 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
793 * Like the above, but silent on error. Used when iterating over pools (because
794 * the configuration cache may be out of date).
797 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
802 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
805 zhp->zpool_hdl = hdl;
806 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
808 if (zpool_refresh_stats(zhp, &missing) != 0) {
824 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
828 zpool_open(libzfs_handle_t *hdl, const char *pool)
832 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
835 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
836 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
837 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
846 * Close the handle. Simply frees the memory associated with the handle.
849 zpool_close(zpool_handle_t *zhp)
851 if (zhp->zpool_config)
852 nvlist_free(zhp->zpool_config);
853 if (zhp->zpool_old_config)
854 nvlist_free(zhp->zpool_old_config);
855 if (zhp->zpool_props)
856 nvlist_free(zhp->zpool_props);
861 * Return the name of the pool.
864 zpool_get_name(zpool_handle_t *zhp)
866 return (zhp->zpool_name);
871 * Return the state of the pool (ACTIVE or UNAVAILABLE)
874 zpool_get_state(zpool_handle_t *zhp)
876 return (zhp->zpool_state);
880 * Create the named pool, using the provided vdev list. It is assumed
881 * that the consumer has already validated the contents of the nvlist, so we
882 * don't have to worry about error semantics.
885 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
886 nvlist_t *props, nvlist_t *fsprops)
888 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
889 nvlist_t *zc_fsprops = NULL;
890 nvlist_t *zc_props = NULL;
895 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
896 "cannot create '%s'"), pool);
898 if (!zpool_name_valid(hdl, B_FALSE, pool))
899 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
901 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
905 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
907 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
908 SPA_VERSION_1, flags, msg)) == NULL) {
917 zoned = ((nvlist_lookup_string(fsprops,
918 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
919 strcmp(zonestr, "on") == 0);
921 if ((zc_fsprops = zfs_valid_proplist(hdl,
922 ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
926 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
929 if (nvlist_add_nvlist(zc_props,
930 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
935 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
938 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
940 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
942 zcmd_free_nvlists(&zc);
943 nvlist_free(zc_props);
944 nvlist_free(zc_fsprops);
949 * This can happen if the user has specified the same
950 * device multiple times. We can't reliably detect this
951 * until we try to add it and see we already have a
952 * label. This can also happen under if the device is
953 * part of an active md or lvm device.
955 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
956 "one or more vdevs refer to the same device, or one of\n"
957 "the devices is part of an active md or lvm device"));
958 return (zfs_error(hdl, EZFS_BADDEV, msg));
962 * This occurs when one of the devices is below
963 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
964 * device was the problem device since there's no
965 * reliable way to determine device size from userland.
970 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
972 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
973 "one or more devices is less than the "
974 "minimum size (%s)"), buf);
976 return (zfs_error(hdl, EZFS_BADDEV, msg));
979 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
980 "one or more devices is out of space"));
981 return (zfs_error(hdl, EZFS_BADDEV, msg));
984 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
985 "cache device must be a disk or disk slice"));
986 return (zfs_error(hdl, EZFS_BADDEV, msg));
989 return (zpool_standard_error(hdl, errno, msg));
994 * If this is an alternate root pool, then we automatically set the
995 * mountpoint of the root dataset to be '/'.
997 if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
1001 verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
1002 verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1009 zcmd_free_nvlists(&zc);
1010 nvlist_free(zc_props);
1011 nvlist_free(zc_fsprops);
1016 * Destroy the given pool. It is up to the caller to ensure that there are no
1017 * datasets left in the pool.
1020 zpool_destroy(zpool_handle_t *zhp)
1022 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1023 zfs_handle_t *zfp = NULL;
1024 libzfs_handle_t *hdl = zhp->zpool_hdl;
1027 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1028 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1031 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1033 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1034 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1035 "cannot destroy '%s'"), zhp->zpool_name);
1037 if (errno == EROFS) {
1038 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1039 "one or more devices is read only"));
1040 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1042 (void) zpool_standard_error(hdl, errno, msg);
1051 remove_mountpoint(zfp);
1059 * Add the given vdevs to the pool. The caller must have already performed the
1060 * necessary verification to ensure that the vdev specification is well-formed.
1063 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1065 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1067 libzfs_handle_t *hdl = zhp->zpool_hdl;
1069 nvlist_t **spares, **l2cache;
1070 uint_t nspares, nl2cache;
1072 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1073 "cannot add to '%s'"), zhp->zpool_name);
1075 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1076 SPA_VERSION_SPARES &&
1077 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1078 &spares, &nspares) == 0) {
1079 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1080 "upgraded to add hot spares"));
1081 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1084 if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1085 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1088 for (s = 0; s < nspares; s++) {
1091 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1092 &path) == 0 && pool_uses_efi(spares[s])) {
1093 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1094 "device '%s' contains an EFI label and "
1095 "cannot be used on root pools."),
1096 zpool_vdev_name(hdl, NULL, spares[s],
1098 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1103 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1104 SPA_VERSION_L2CACHE &&
1105 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1106 &l2cache, &nl2cache) == 0) {
1107 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1108 "upgraded to add cache devices"));
1109 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1112 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1114 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1116 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1120 * This can happen if the user has specified the same
1121 * device multiple times. We can't reliably detect this
1122 * until we try to add it and see we already have a
1125 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1126 "one or more vdevs refer to the same device"));
1127 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1132 * This occurrs when one of the devices is below
1133 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1134 * device was the problem device since there's no
1135 * reliable way to determine device size from userland.
1140 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1142 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1143 "device is less than the minimum "
1146 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1150 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1151 "pool must be upgraded to add these vdevs"));
1152 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1156 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1157 "root pool can not have multiple vdevs"
1158 " or separate logs"));
1159 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1163 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1164 "cache device must be a disk or disk slice"));
1165 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1169 (void) zpool_standard_error(hdl, errno, msg);
1177 zcmd_free_nvlists(&zc);
1183 * Exports the pool from the system. The caller must ensure that there are no
1184 * mounted datasets in the pool.
1187 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1189 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1192 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1193 "cannot export '%s'"), zhp->zpool_name);
1195 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1196 zc.zc_cookie = force;
1197 zc.zc_guid = hardforce;
1199 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1202 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1203 "use '-f' to override the following errors:\n"
1204 "'%s' has an active shared spare which could be"
1205 " used by other pools once '%s' is exported."),
1206 zhp->zpool_name, zhp->zpool_name);
1207 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1210 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1219 zpool_export(zpool_handle_t *zhp, boolean_t force)
1221 return (zpool_export_common(zhp, force, B_FALSE));
1225 zpool_export_force(zpool_handle_t *zhp)
1227 return (zpool_export_common(zhp, B_TRUE, B_TRUE));
1231 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1234 nvlist_t *nv = NULL;
1240 if (!hdl->libzfs_printerr || config == NULL)
1243 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0)
1246 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1248 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1250 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1251 strftime(timestr, 128, "%c", &t) != 0) {
1253 (void) printf(dgettext(TEXT_DOMAIN,
1254 "Would be able to return %s "
1255 "to its state as of %s.\n"),
1258 (void) printf(dgettext(TEXT_DOMAIN,
1259 "Pool %s returned to its state as of %s.\n"),
1263 (void) printf(dgettext(TEXT_DOMAIN,
1264 "%s approximately %lld "),
1265 dryrun ? "Would discard" : "Discarded",
1266 ((longlong_t)loss + 30) / 60);
1267 (void) printf(dgettext(TEXT_DOMAIN,
1268 "minutes of transactions.\n"));
1269 } else if (loss > 0) {
1270 (void) printf(dgettext(TEXT_DOMAIN,
1271 "%s approximately %lld "),
1272 dryrun ? "Would discard" : "Discarded",
1274 (void) printf(dgettext(TEXT_DOMAIN,
1275 "seconds of transactions.\n"));
1281 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1284 nvlist_t *nv = NULL;
1286 uint64_t edata = UINT64_MAX;
1291 if (!hdl->libzfs_printerr)
1295 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1297 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1299 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1300 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1301 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1304 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1305 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1308 (void) printf(dgettext(TEXT_DOMAIN,
1309 "Recovery is possible, but will result in some data loss.\n"));
1311 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1312 strftime(timestr, 128, "%c", &t) != 0) {
1313 (void) printf(dgettext(TEXT_DOMAIN,
1314 "\tReturning the pool to its state as of %s\n"
1315 "\tshould correct the problem. "),
1318 (void) printf(dgettext(TEXT_DOMAIN,
1319 "\tReverting the pool to an earlier state "
1320 "should correct the problem.\n\t"));
1324 (void) printf(dgettext(TEXT_DOMAIN,
1325 "Approximately %lld minutes of data\n"
1326 "\tmust be discarded, irreversibly. "),
1327 ((longlong_t)loss + 30) / 60);
1328 } else if (loss > 0) {
1329 (void) printf(dgettext(TEXT_DOMAIN,
1330 "Approximately %lld seconds of data\n"
1331 "\tmust be discarded, irreversibly. "),
1334 if (edata != 0 && edata != UINT64_MAX) {
1336 (void) printf(dgettext(TEXT_DOMAIN,
1337 "After rewind, at least\n"
1338 "\tone persistent user-data error will remain. "));
1340 (void) printf(dgettext(TEXT_DOMAIN,
1341 "After rewind, several\n"
1342 "\tpersistent user-data errors will remain. "));
1345 (void) printf(dgettext(TEXT_DOMAIN,
1346 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1347 reason >= 0 ? "clear" : "import", name);
1349 (void) printf(dgettext(TEXT_DOMAIN,
1350 "A scrub of the pool\n"
1351 "\tis strongly recommended after recovery.\n"));
1355 (void) printf(dgettext(TEXT_DOMAIN,
1356 "Destroy and re-create the pool from\n\ta backup source.\n"));
1360 * zpool_import() is a contracted interface. Should be kept the same
1363 * Applications should use zpool_import_props() to import a pool with
1364 * new properties value to be set.
1367 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1370 nvlist_t *props = NULL;
1373 if (altroot != NULL) {
1374 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1375 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1376 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1380 if (nvlist_add_string(props,
1381 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1382 nvlist_add_string(props,
1383 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1385 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1386 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1391 ret = zpool_import_props(hdl, config, newname, props,
1399 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1405 uint64_t is_log = 0;
1407 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1411 (void) printf("\t%*s%s%s\n", indent, "", name,
1412 is_log ? " [log]" : "");
1414 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1415 &child, &children) != 0)
1418 for (c = 0; c < children; c++) {
1419 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
1420 print_vdev_tree(hdl, vname, child[c], indent + 2);
1426 * Import the given pool using the known configuration and a list of
1427 * properties to be set. The configuration should have come from
1428 * zpool_find_import(). The 'newname' parameters control whether the pool
1429 * is imported with a different name.
1432 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1433 nvlist_t *props, int flags)
1435 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1436 zpool_rewind_policy_t policy;
1437 nvlist_t *nv = NULL;
1438 nvlist_t *nvinfo = NULL;
1439 nvlist_t *missing = NULL;
1446 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1449 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1450 "cannot import pool '%s'"), origname);
1452 if (newname != NULL) {
1453 if (!zpool_name_valid(hdl, B_FALSE, newname))
1454 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1455 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1457 thename = (char *)newname;
1464 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1466 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1469 if ((props = zpool_valid_proplist(hdl, origname,
1470 props, version, flags, errbuf)) == NULL) {
1472 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1478 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1480 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1483 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1487 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1492 zc.zc_cookie = flags;
1493 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1495 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1496 zcmd_free_nvlists(&zc);
1503 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1504 zpool_get_rewind_policy(config, &policy);
1510 * Dry-run failed, but we print out what success
1511 * looks like if we found a best txg
1513 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1514 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1520 if (newname == NULL)
1521 (void) snprintf(desc, sizeof (desc),
1522 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1525 (void) snprintf(desc, sizeof (desc),
1526 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1532 * Unsupported version.
1534 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1538 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1542 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1543 "one or more devices is read only"));
1544 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1548 if (nv && nvlist_lookup_nvlist(nv,
1549 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1550 nvlist_lookup_nvlist(nvinfo,
1551 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1552 (void) printf(dgettext(TEXT_DOMAIN,
1553 "The devices below are missing, use "
1554 "'-m' to import the pool anyway:\n"));
1555 print_vdev_tree(hdl, NULL, missing, 2);
1556 (void) printf("\n");
1558 (void) zpool_standard_error(hdl, error, desc);
1562 (void) zpool_standard_error(hdl, error, desc);
1566 (void) zpool_standard_error(hdl, error, desc);
1567 zpool_explain_recover(hdl,
1568 newname ? origname : thename, -error, nv);
1575 zpool_handle_t *zhp;
1578 * This should never fail, but play it safe anyway.
1580 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1582 else if (zhp != NULL)
1584 if (policy.zrp_request &
1585 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1586 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1587 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1593 zcmd_free_nvlists(&zc);
1603 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1605 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1607 libzfs_handle_t *hdl = zhp->zpool_hdl;
1609 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1610 zc.zc_cookie = func;
1612 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
1613 (errno == ENOENT && func != POOL_SCAN_NONE))
1616 if (func == POOL_SCAN_SCRUB) {
1617 (void) snprintf(msg, sizeof (msg),
1618 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1619 } else if (func == POOL_SCAN_NONE) {
1620 (void) snprintf(msg, sizeof (msg),
1621 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1624 assert(!"unexpected result");
1627 if (errno == EBUSY) {
1629 pool_scan_stat_t *ps = NULL;
1632 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1633 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1634 (void) nvlist_lookup_uint64_array(nvroot,
1635 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1636 if (ps && ps->pss_func == POOL_SCAN_SCRUB)
1637 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1639 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1640 } else if (errno == ENOENT) {
1641 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1643 return (zpool_standard_error(hdl, errno, msg));
1648 * This provides a very minimal check whether a given string is likely a
1649 * c#t#d# style string. Users of this are expected to do their own
1650 * verification of the s# part.
1652 #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1]))
1655 * More elaborate version for ones which may start with "/dev/dsk/"
1659 ctd_check_path(char *str) {
1661 * If it starts with a slash, check the last component.
1663 if (str && str[0] == '/') {
1664 char *tmp = strrchr(str, '/');
1667 * If it ends in "/old", check the second-to-last
1668 * component of the string instead.
1670 if (tmp != str && strcmp(tmp, "/old") == 0) {
1671 for (tmp--; *tmp != '/'; tmp--)
1676 return (CTD_CHECK(str));
1680 * Find a vdev that matches the search criteria specified. We use the
1681 * the nvpair name to determine how we should look for the device.
1682 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1683 * spare; but FALSE if its an INUSE spare.
1686 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1687 boolean_t *l2cache, boolean_t *log)
1694 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1696 /* Nothing to look for */
1697 if (search == NULL || pair == NULL)
1700 /* Obtain the key we will use to search */
1701 srchkey = nvpair_name(pair);
1703 switch (nvpair_type(pair)) {
1704 case DATA_TYPE_UINT64:
1705 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1706 uint64_t srchval, theguid;
1708 verify(nvpair_value_uint64(pair, &srchval) == 0);
1709 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1711 if (theguid == srchval)
1716 case DATA_TYPE_STRING: {
1717 char *srchval, *val;
1719 verify(nvpair_value_string(pair, &srchval) == 0);
1720 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1724 * Search for the requested value. Special cases:
1726 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
1727 * "s0" or "s0/old". The "s0" part is hidden from the user,
1728 * but included in the string, so this matches around it.
1729 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1731 * Otherwise, all other searches are simple string compares.
1733 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
1734 ctd_check_path(val)) {
1735 uint64_t wholedisk = 0;
1737 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1740 int slen = strlen(srchval);
1741 int vlen = strlen(val);
1743 if (slen != vlen - 2)
1747 * make_leaf_vdev() should only set
1748 * wholedisk for ZPOOL_CONFIG_PATHs which
1749 * will include "/dev/dsk/", giving plenty of
1750 * room for the indices used next.
1755 * strings identical except trailing "s0"
1757 if (strcmp(&val[vlen - 2], "s0") == 0 &&
1758 strncmp(srchval, val, slen) == 0)
1762 * strings identical except trailing "s0/old"
1764 if (strcmp(&val[vlen - 6], "s0/old") == 0 &&
1765 strcmp(&srchval[slen - 4], "/old") == 0 &&
1766 strncmp(srchval, val, slen - 4) == 0)
1771 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
1772 char *type, *idx, *end, *p;
1773 uint64_t id, vdev_id;
1776 * Determine our vdev type, keeping in mind
1777 * that the srchval is composed of a type and
1778 * vdev id pair (i.e. mirror-4).
1780 if ((type = strdup(srchval)) == NULL)
1783 if ((p = strrchr(type, '-')) == NULL) {
1791 * If the types don't match then keep looking.
1793 if (strncmp(val, type, strlen(val)) != 0) {
1798 verify(strncmp(type, VDEV_TYPE_RAIDZ,
1799 strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1800 strncmp(type, VDEV_TYPE_MIRROR,
1801 strlen(VDEV_TYPE_MIRROR)) == 0);
1802 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
1806 vdev_id = strtoull(idx, &end, 10);
1813 * Now verify that we have the correct vdev id.
1822 if (strcmp(srchval, val) == 0)
1831 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1832 &child, &children) != 0)
1835 for (c = 0; c < children; c++) {
1836 if ((ret = vdev_to_nvlist_iter(child[c], search,
1837 avail_spare, l2cache, NULL)) != NULL) {
1839 * The 'is_log' value is only set for the toplevel
1840 * vdev, not the leaf vdevs. So we always lookup the
1841 * log device from the root of the vdev tree (where
1842 * 'log' is non-NULL).
1845 nvlist_lookup_uint64(child[c],
1846 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
1854 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1855 &child, &children) == 0) {
1856 for (c = 0; c < children; c++) {
1857 if ((ret = vdev_to_nvlist_iter(child[c], search,
1858 avail_spare, l2cache, NULL)) != NULL) {
1859 *avail_spare = B_TRUE;
1865 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1866 &child, &children) == 0) {
1867 for (c = 0; c < children; c++) {
1868 if ((ret = vdev_to_nvlist_iter(child[c], search,
1869 avail_spare, l2cache, NULL)) != NULL) {
1880 * Given a physical path (minus the "/devices" prefix), find the
1884 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
1885 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
1887 nvlist_t *search, *nvroot, *ret;
1889 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1890 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
1892 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1895 *avail_spare = B_FALSE;
1899 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1900 nvlist_free(search);
1906 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
1909 zpool_vdev_is_interior(const char *name)
1911 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1912 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
1918 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
1919 boolean_t *l2cache, boolean_t *log)
1921 char buf[MAXPATHLEN];
1923 nvlist_t *nvroot, *search, *ret;
1926 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1928 guid = strtoull(path, &end, 10);
1929 if (guid != 0 && *end == '\0') {
1930 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
1931 } else if (zpool_vdev_is_interior(path)) {
1932 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
1933 } else if (path[0] != '/') {
1934 (void) snprintf(buf, sizeof (buf), "%s/%s", DISK_ROOT, path);
1935 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
1937 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
1940 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1943 *avail_spare = B_FALSE;
1947 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1948 nvlist_free(search);
1954 vdev_online(nvlist_t *nv)
1958 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
1959 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
1960 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
1967 * Helper function for zpool_get_physpaths().
1970 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
1971 size_t *bytes_written)
1973 size_t bytes_left, pos, rsz;
1977 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
1979 return (EZFS_NODEVICE);
1981 pos = *bytes_written;
1982 bytes_left = physpath_size - pos;
1983 format = (pos == 0) ? "%s" : " %s";
1985 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
1986 *bytes_written += rsz;
1988 if (rsz >= bytes_left) {
1989 /* if physpath was not copied properly, clear it */
1990 if (bytes_left != 0) {
1993 return (EZFS_NOSPC);
1999 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2000 size_t *rsz, boolean_t is_spare)
2005 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2006 return (EZFS_INVALCONFIG);
2008 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2010 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2011 * For a spare vdev, we only want to boot from the active
2016 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2019 return (EZFS_INVALCONFIG);
2022 if (vdev_online(nv)) {
2023 if ((ret = vdev_get_one_physpath(nv, physpath,
2024 phypath_size, rsz)) != 0)
2027 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2028 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2029 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2034 if (nvlist_lookup_nvlist_array(nv,
2035 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2036 return (EZFS_INVALCONFIG);
2038 for (i = 0; i < count; i++) {
2039 ret = vdev_get_physpaths(child[i], physpath,
2040 phypath_size, rsz, is_spare);
2041 if (ret == EZFS_NOSPC)
2046 return (EZFS_POOL_INVALARG);
2050 * Get phys_path for a root pool config.
2051 * Return 0 on success; non-zero on failure.
2054 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2057 nvlist_t *vdev_root;
2064 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2066 return (EZFS_INVALCONFIG);
2068 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2069 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2070 &child, &count) != 0)
2071 return (EZFS_INVALCONFIG);
2074 * root pool can not have EFI labeled disks and can only have
2075 * a single top-level vdev.
2077 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
2078 pool_uses_efi(vdev_root))
2079 return (EZFS_POOL_INVALARG);
2081 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2084 /* No online devices */
2086 return (EZFS_NODEVICE);
2092 * Get phys_path for a root pool
2093 * Return 0 on success; non-zero on failure.
2096 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2098 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2103 * If the device has being dynamically expanded then we need to relabel
2104 * the disk to use the new unallocated space.
2107 zpool_relabel_disk(libzfs_handle_t *hdl, const char *path)
2112 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
2113 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2114 "relabel '%s': unable to open device"), path);
2115 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2119 * It's possible that we might encounter an error if the device
2120 * does not have any unallocated space left. If so, we simply
2121 * ignore that error and continue on.
2123 error = efi_use_whole_disk(fd);
2125 if (error && error != VT_ENOSPC) {
2126 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2127 "relabel '%s': unable to read disk capacity"), path);
2128 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2134 * Bring the specified vdev online. The 'flags' parameter is a set of the
2135 * ZFS_ONLINE_* flags.
2138 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2139 vdev_state_t *newstate)
2141 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2144 boolean_t avail_spare, l2cache, islog;
2145 libzfs_handle_t *hdl = zhp->zpool_hdl;
2147 if (flags & ZFS_ONLINE_EXPAND) {
2148 (void) snprintf(msg, sizeof (msg),
2149 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2151 (void) snprintf(msg, sizeof (msg),
2152 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2155 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2156 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2158 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2160 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2163 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2165 if (flags & ZFS_ONLINE_EXPAND ||
2166 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2167 char *pathname = NULL;
2168 uint64_t wholedisk = 0;
2170 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2172 verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
2176 * XXX - L2ARC 1.0 devices can't support expansion.
2179 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2180 "cannot expand cache devices"));
2181 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2185 pathname += strlen(DISK_ROOT) + 1;
2186 (void) zpool_relabel_disk(hdl, pathname);
2190 zc.zc_cookie = VDEV_STATE_ONLINE;
2193 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2194 if (errno == EINVAL) {
2195 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2196 "from this pool into a new one. Use '%s' "
2197 "instead"), "zpool detach");
2198 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2200 return (zpool_standard_error(hdl, errno, msg));
2203 *newstate = zc.zc_cookie;
2208 * Take the specified vdev offline
2211 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2213 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2216 boolean_t avail_spare, l2cache;
2217 libzfs_handle_t *hdl = zhp->zpool_hdl;
2219 (void) snprintf(msg, sizeof (msg),
2220 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2222 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2223 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2225 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2227 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2230 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2232 zc.zc_cookie = VDEV_STATE_OFFLINE;
2233 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2235 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2242 * There are no other replicas of this device.
2244 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2248 * The log device has unplayed logs
2250 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2253 return (zpool_standard_error(hdl, errno, msg));
2258 * Mark the given vdev faulted.
2261 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2263 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2265 libzfs_handle_t *hdl = zhp->zpool_hdl;
2267 (void) snprintf(msg, sizeof (msg),
2268 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
2270 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2272 zc.zc_cookie = VDEV_STATE_FAULTED;
2275 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2282 * There are no other replicas of this device.
2284 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2287 return (zpool_standard_error(hdl, errno, msg));
2293 * Mark the given vdev degraded.
2296 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2298 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2300 libzfs_handle_t *hdl = zhp->zpool_hdl;
2302 (void) snprintf(msg, sizeof (msg),
2303 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid);
2305 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2307 zc.zc_cookie = VDEV_STATE_DEGRADED;
2310 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2313 return (zpool_standard_error(hdl, errno, msg));
2317 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2321 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2327 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2329 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2332 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2333 children == 2 && child[which] == tgt)
2336 for (c = 0; c < children; c++)
2337 if (is_replacing_spare(child[c], tgt, which))
2345 * Attach new_disk (fully described by nvroot) to old_disk.
2346 * If 'replacing' is specified, the new disk will replace the old one.
2349 zpool_vdev_attach(zpool_handle_t *zhp,
2350 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2352 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2356 boolean_t avail_spare, l2cache, islog;
2361 nvlist_t *config_root;
2362 libzfs_handle_t *hdl = zhp->zpool_hdl;
2363 boolean_t rootpool = pool_is_bootable(zhp);
2366 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2367 "cannot replace %s with %s"), old_disk, new_disk);
2369 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2370 "cannot attach %s to %s"), new_disk, old_disk);
2373 * If this is a root pool, make sure that we're not attaching an
2374 * EFI labeled device.
2376 if (rootpool && pool_uses_efi(nvroot)) {
2377 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2378 "EFI labeled devices are not supported on root pools."));
2379 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2382 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2383 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2385 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2388 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2391 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2393 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2394 zc.zc_cookie = replacing;
2396 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2397 &child, &children) != 0 || children != 1) {
2398 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2399 "new device must be a single disk"));
2400 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2403 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2404 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2406 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2410 * If the target is a hot spare that has been swapped in, we can only
2411 * replace it with another hot spare.
2414 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2415 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2416 NULL) == NULL || !avail_spare) &&
2417 is_replacing_spare(config_root, tgt, 1)) {
2418 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2419 "can only be replaced by another hot spare"));
2421 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2426 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2429 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2431 zcmd_free_nvlists(&zc);
2436 * XXX need a better way to prevent user from
2437 * booting up a half-baked vdev.
2439 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2440 "sure to wait until resilver is done "
2441 "before rebooting.\n"));
2449 * Can't attach to or replace this type of vdev.
2452 uint64_t version = zpool_get_prop_int(zhp,
2453 ZPOOL_PROP_VERSION, NULL);
2456 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2457 "cannot replace a log with a spare"));
2458 else if (version >= SPA_VERSION_MULTI_REPLACE)
2459 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2460 "already in replacing/spare config; wait "
2461 "for completion or use 'zpool detach'"));
2463 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2464 "cannot replace a replacing device"));
2466 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2467 "can only attach to mirrors and top-level "
2470 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2475 * The new device must be a single disk.
2477 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2478 "new device must be a single disk"));
2479 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2483 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2485 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2490 * The new device is too small.
2492 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2493 "device is too small"));
2494 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2499 * The new device has a different alignment requirement.
2501 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2502 "devices have different sector alignment"));
2503 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2508 * The resulting top-level vdev spec won't fit in the label.
2510 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2514 (void) zpool_standard_error(hdl, errno, msg);
2521 * Detach the specified device.
2524 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2526 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2529 boolean_t avail_spare, l2cache;
2530 libzfs_handle_t *hdl = zhp->zpool_hdl;
2532 (void) snprintf(msg, sizeof (msg),
2533 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2535 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2536 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2538 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2541 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2544 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2546 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2548 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2555 * Can't detach from this type of vdev.
2557 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2558 "applicable to mirror and replacing vdevs"));
2559 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2564 * There are no other replicas of this device.
2566 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2570 (void) zpool_standard_error(hdl, errno, msg);
2577 * Find a mirror vdev in the source nvlist.
2579 * The mchild array contains a list of disks in one of the top-level mirrors
2580 * of the source pool. The schild array contains a list of disks that the
2581 * user specified on the command line. We loop over the mchild array to
2582 * see if any entry in the schild array matches.
2584 * If a disk in the mchild array is found in the schild array, we return
2585 * the index of that entry. Otherwise we return -1.
2588 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2589 nvlist_t **schild, uint_t schildren)
2593 for (mc = 0; mc < mchildren; mc++) {
2595 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2596 mchild[mc], B_FALSE);
2598 for (sc = 0; sc < schildren; sc++) {
2599 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2600 schild[sc], B_FALSE);
2601 boolean_t result = (strcmp(mpath, spath) == 0);
2617 * Split a mirror pool. If newroot points to null, then a new nvlist
2618 * is generated and it is the responsibility of the caller to free it.
2621 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2622 nvlist_t *props, splitflags_t flags)
2624 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2626 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2627 nvlist_t **varray = NULL, *zc_props = NULL;
2628 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2629 libzfs_handle_t *hdl = zhp->zpool_hdl;
2631 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2634 (void) snprintf(msg, sizeof (msg),
2635 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2637 if (!zpool_name_valid(hdl, B_FALSE, newname))
2638 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2640 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2641 (void) fprintf(stderr, gettext("Internal error: unable to "
2642 "retrieve pool configuration\n"));
2646 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2648 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2651 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2652 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2653 props, vers, flags, msg)) == NULL)
2657 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2659 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2660 "Source pool is missing vdev tree"));
2662 nvlist_free(zc_props);
2666 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2669 if (*newroot == NULL ||
2670 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2671 &newchild, &newchildren) != 0)
2674 for (c = 0; c < children; c++) {
2675 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2677 nvlist_t **mchild, *vdev;
2682 * Unlike cache & spares, slogs are stored in the
2683 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2685 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2687 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2689 if (is_log || is_hole) {
2691 * Create a hole vdev and put it in the config.
2693 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2695 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2696 VDEV_TYPE_HOLE) != 0)
2698 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2703 varray[vcount++] = vdev;
2707 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
2709 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2710 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2711 "Source pool must be composed only of mirrors\n"));
2712 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2716 verify(nvlist_lookup_nvlist_array(child[c],
2717 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2719 /* find or add an entry for this top-level vdev */
2720 if (newchildren > 0 &&
2721 (entry = find_vdev_entry(zhp, mchild, mchildren,
2722 newchild, newchildren)) >= 0) {
2723 /* We found a disk that the user specified. */
2724 vdev = mchild[entry];
2727 /* User didn't specify a disk for this vdev. */
2728 vdev = mchild[mchildren - 1];
2731 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
2735 /* did we find every disk the user specified? */
2736 if (found != newchildren) {
2737 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
2738 "include at most one disk from each mirror"));
2739 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2743 /* Prepare the nvlist for populating. */
2744 if (*newroot == NULL) {
2745 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
2748 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
2749 VDEV_TYPE_ROOT) != 0)
2752 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
2755 /* Add all the children we found */
2756 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
2757 lastlog == 0 ? vcount : lastlog) != 0)
2761 * If we're just doing a dry run, exit now with success.
2764 memory_err = B_FALSE;
2769 /* now build up the config list & call the ioctl */
2770 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
2773 if (nvlist_add_nvlist(newconfig,
2774 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
2775 nvlist_add_string(newconfig,
2776 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
2777 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
2781 * The new pool is automatically part of the namespace unless we
2782 * explicitly export it.
2785 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
2786 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2787 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
2788 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
2790 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
2793 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
2794 retval = zpool_standard_error(hdl, errno, msg);
2799 memory_err = B_FALSE;
2802 if (varray != NULL) {
2805 for (v = 0; v < vcount; v++)
2806 nvlist_free(varray[v]);
2809 zcmd_free_nvlists(&zc);
2811 nvlist_free(zc_props);
2813 nvlist_free(newconfig);
2815 nvlist_free(*newroot);
2823 return (no_memory(hdl));
2829 * Remove the given device. Currently, this is supported only for hot spares
2830 * and level 2 cache devices.
2833 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
2835 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2838 boolean_t avail_spare, l2cache, islog;
2839 libzfs_handle_t *hdl = zhp->zpool_hdl;
2842 (void) snprintf(msg, sizeof (msg),
2843 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
2845 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2846 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2848 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2850 * XXX - this should just go away.
2852 if (!avail_spare && !l2cache && !islog) {
2853 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2854 "only inactive hot spares, cache, top-level, "
2855 "or log devices can be removed"));
2856 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2859 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
2860 if (islog && version < SPA_VERSION_HOLES) {
2861 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2862 "pool must be upgrade to support log removal"));
2863 return (zfs_error(hdl, EZFS_BADVERSION, msg));
2866 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2868 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
2871 return (zpool_standard_error(hdl, errno, msg));
2875 * Clear the errors for the pool, or the particular device if specified.
2878 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
2880 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2883 zpool_rewind_policy_t policy;
2884 boolean_t avail_spare, l2cache;
2885 libzfs_handle_t *hdl = zhp->zpool_hdl;
2886 nvlist_t *nvi = NULL;
2890 (void) snprintf(msg, sizeof (msg),
2891 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2894 (void) snprintf(msg, sizeof (msg),
2895 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2898 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2900 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
2901 &l2cache, NULL)) == 0)
2902 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2905 * Don't allow error clearing for hot spares. Do allow
2906 * error clearing for l2cache devices.
2909 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2911 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
2915 zpool_get_rewind_policy(rewindnvl, &policy);
2916 zc.zc_cookie = policy.zrp_request;
2918 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
2921 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
2924 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
2926 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
2927 zcmd_free_nvlists(&zc);
2932 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
2933 errno != EPERM && errno != EACCES)) {
2934 if (policy.zrp_request &
2935 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2936 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
2937 zpool_rewind_exclaim(hdl, zc.zc_name,
2938 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
2942 zcmd_free_nvlists(&zc);
2946 zcmd_free_nvlists(&zc);
2947 return (zpool_standard_error(hdl, errno, msg));
2951 * Similar to zpool_clear(), but takes a GUID (used by fmd).
2954 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
2956 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2958 libzfs_handle_t *hdl = zhp->zpool_hdl;
2960 (void) snprintf(msg, sizeof (msg),
2961 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
2962 (u_longlong_t)guid);
2964 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2966 zc.zc_cookie = ZPOOL_NO_REWIND;
2968 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
2971 return (zpool_standard_error(hdl, errno, msg));
2975 * Convert from a devid string to a path.
2978 devid_to_path(char *devid_str)
2983 devid_nmlist_t *list = NULL;
2986 if (devid_str_decode(devid_str, &devid, &minor) != 0)
2989 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
2991 devid_str_free(minor);
2997 if ((path = strdup(list[0].devname)) == NULL)
3000 devid_free_nmlist(list);
3006 * Convert from a path to a devid string.
3009 path_to_devid(const char *path)
3015 if ((fd = open(path, O_RDONLY)) < 0)
3020 if (devid_get(fd, &devid) == 0) {
3021 if (devid_get_minor_name(fd, &minor) == 0)
3022 ret = devid_str_encode(devid, minor);
3024 devid_str_free(minor);
3033 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3034 * ignore any failure here, since a common case is for an unprivileged user to
3035 * type 'zpool status', and we'll display the correct information anyway.
3038 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3040 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3042 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3043 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3044 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3047 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3051 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3052 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3053 * We also check if this is a whole disk, in which case we strip off the
3054 * trailing 's0' slice name.
3056 * This routine is also responsible for identifying when disks have been
3057 * reconfigured in a new location. The kernel will have opened the device by
3058 * devid, but the path will still refer to the old location. To catch this, we
3059 * first do a path -> devid translation (which is fast for the common case). If
3060 * the devid matches, we're done. If not, we do a reverse devid -> path
3061 * translation and issue the appropriate ioctl() to update the path of the vdev.
3062 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3066 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3069 char *path, *devid, *type;
3075 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
3077 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3079 (void) snprintf(buf, sizeof (buf), "%llu",
3080 (u_longlong_t)value);
3082 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3084 * If the device is dead (faulted, offline, etc) then don't
3085 * bother opening it. Otherwise we may be forcing the user to
3086 * open a misbehaving device, which can have undesirable
3089 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3090 (uint64_t **)&vs, &vsc) != 0 ||
3091 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3093 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3095 * Determine if the current path is correct.
3097 char *newdevid = path_to_devid(path);
3099 if (newdevid == NULL ||
3100 strcmp(devid, newdevid) != 0) {
3103 if ((newpath = devid_to_path(devid)) != NULL) {
3105 * Update the path appropriately.
3107 set_path(zhp, nv, newpath);
3108 if (nvlist_add_string(nv,
3109 ZPOOL_CONFIG_PATH, newpath) == 0)
3110 verify(nvlist_lookup_string(nv,
3118 devid_str_free(newdevid);
3122 * For a block device only use the name.
3124 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
3125 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
3126 path = strrchr(path, '/');
3130 #if defined(__sun__) || defined(__sun)
3132 * The following code strips the slice from the device path.
3134 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3135 &value) == 0 && value) {
3136 int pathlen = strlen(path);
3137 char *tmp = zfs_strdup(hdl, path);
3140 * If it starts with c#, and ends with "s0", chop
3141 * the "s0" off, or if it ends with "s0/old", remove
3142 * the "s0" from the middle.
3144 if (CTD_CHECK(tmp)) {
3145 if (strcmp(&tmp[pathlen - 2], "s0") == 0) {
3146 tmp[pathlen - 2] = '\0';
3147 } else if (pathlen > 6 &&
3148 strcmp(&tmp[pathlen - 6], "s0/old") == 0) {
3149 (void) strcpy(&tmp[pathlen - 6],
3157 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3160 * If it's a raidz device, we need to stick in the parity level.
3162 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3163 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3165 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3166 (u_longlong_t)value);
3171 * We identify each top-level vdev by using a <type-id>
3172 * naming convention.
3177 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3179 (void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3185 return (zfs_strdup(hdl, path));
3189 zbookmark_compare(const void *a, const void *b)
3191 return (memcmp(a, b, sizeof (zbookmark_t)));
3195 * Retrieve the persistent error log, uniquify the members, and return to the
3199 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3201 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3203 zbookmark_t *zb = NULL;
3207 * Retrieve the raw error list from the kernel. If the number of errors
3208 * has increased, allocate more space and continue until we get the
3211 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3215 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3216 count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
3218 zc.zc_nvlist_dst_size = count;
3219 (void) strcpy(zc.zc_name, zhp->zpool_name);
3221 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3223 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3224 if (errno == ENOMEM) {
3225 count = zc.zc_nvlist_dst_size;
3226 if ((zc.zc_nvlist_dst = (uintptr_t)
3227 zfs_alloc(zhp->zpool_hdl, count *
3228 sizeof (zbookmark_t))) == (uintptr_t)NULL)
3239 * Sort the resulting bookmarks. This is a little confusing due to the
3240 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3241 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3242 * _not_ copied as part of the process. So we point the start of our
3243 * array appropriate and decrement the total number of elements.
3245 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
3246 zc.zc_nvlist_dst_size;
3247 count -= zc.zc_nvlist_dst_size;
3249 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
3251 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3254 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3256 for (i = 0; i < count; i++) {
3259 /* ignoring zb_blkid and zb_level for now */
3260 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3261 zb[i-1].zb_object == zb[i].zb_object)
3264 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3266 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3267 zb[i].zb_objset) != 0) {
3271 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3272 zb[i].zb_object) != 0) {
3276 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3283 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3287 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3288 return (no_memory(zhp->zpool_hdl));
3292 * Upgrade a ZFS pool to the latest on-disk version.
3295 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3297 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3298 libzfs_handle_t *hdl = zhp->zpool_hdl;
3300 (void) strcpy(zc.zc_name, zhp->zpool_name);
3301 zc.zc_cookie = new_version;
3303 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3304 return (zpool_standard_error_fmt(hdl, errno,
3305 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3311 zpool_set_history_str(const char *subcommand, int argc, char **argv,
3316 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
3317 for (i = 1; i < argc; i++) {
3318 if (strlen(history_str) + 1 + strlen(argv[i]) >
3321 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
3322 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
3327 * Stage command history for logging.
3330 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
3332 if (history_str == NULL)
3335 if (strlen(history_str) > HIS_MAX_RECORD_LEN)
3338 if (hdl->libzfs_log_str != NULL)
3339 free(hdl->libzfs_log_str);
3341 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
3342 return (no_memory(hdl));
3348 * Perform ioctl to get some command history of a pool.
3350 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3351 * logical offset of the history buffer to start reading from.
3353 * Upon return, 'off' is the next logical offset to read from and
3354 * 'len' is the actual amount of bytes read into 'buf'.
3357 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3359 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3360 libzfs_handle_t *hdl = zhp->zpool_hdl;
3362 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3364 zc.zc_history = (uint64_t)(uintptr_t)buf;
3365 zc.zc_history_len = *len;
3366 zc.zc_history_offset = *off;
3368 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3371 return (zfs_error_fmt(hdl, EZFS_PERM,
3372 dgettext(TEXT_DOMAIN,
3373 "cannot show history for pool '%s'"),
3376 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3377 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3378 "'%s'"), zhp->zpool_name));
3380 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3381 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3382 "'%s', pool must be upgraded"), zhp->zpool_name));
3384 return (zpool_standard_error_fmt(hdl, errno,
3385 dgettext(TEXT_DOMAIN,
3386 "cannot get history for '%s'"), zhp->zpool_name));
3390 *len = zc.zc_history_len;
3391 *off = zc.zc_history_offset;
3397 * Process the buffer of nvlists, unpacking and storing each nvlist record
3398 * into 'records'. 'leftover' is set to the number of bytes that weren't
3399 * processed as there wasn't a complete record.
3402 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3403 nvlist_t ***records, uint_t *numrecords)
3409 while (bytes_read > sizeof (reclen)) {
3411 /* get length of packed record (stored as little endian) */
3412 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3413 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3415 if (bytes_read < sizeof (reclen) + reclen)
3419 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3421 bytes_read -= sizeof (reclen) + reclen;
3422 buf += sizeof (reclen) + reclen;
3424 /* add record to nvlist array */
3426 if (ISP2(*numrecords + 1)) {
3427 *records = realloc(*records,
3428 *numrecords * 2 * sizeof (nvlist_t *));
3430 (*records)[*numrecords - 1] = nv;
3433 *leftover = bytes_read;
3437 #define HIS_BUF_LEN (128*1024)
3440 * Retrieve the command history of a pool.
3443 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3445 char buf[HIS_BUF_LEN];
3447 nvlist_t **records = NULL;
3448 uint_t numrecords = 0;
3452 uint64_t bytes_read = sizeof (buf);
3455 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3458 /* if nothing else was read in, we're at EOF, just return */
3462 if ((err = zpool_history_unpack(buf, bytes_read,
3463 &leftover, &records, &numrecords)) != 0)
3471 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3472 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3473 records, numrecords) == 0);
3475 for (i = 0; i < numrecords; i++)
3476 nvlist_free(records[i]);
3483 * Retrieve the next event. If there is a new event available 'nvp' will
3484 * contain a newly allocated nvlist and 'dropped' will be set to the number
3485 * of missed events since the last call to this function. When 'nvp' is
3486 * set to NULL it indicates no new events are available. In either case
3487 * the function returns 0 and it is up to the caller to free 'nvp'. In
3488 * the case of a fatal error the function will return a non-zero value.
3489 * When the function is called in blocking mode it will not return until
3490 * a new event is available.
3493 zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
3494 int *dropped, int block, int cleanup_fd)
3496 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3501 zc.zc_cleanup_fd = cleanup_fd;
3504 zc.zc_guid = ZEVENT_NONBLOCK;
3506 if (zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE) != 0)
3510 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
3513 error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
3514 dgettext(TEXT_DOMAIN, "zfs shutdown"));
3517 /* Blocking error case should not occur */
3519 error = zpool_standard_error_fmt(hdl, errno,
3520 dgettext(TEXT_DOMAIN, "cannot get event"));
3524 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3525 error = zfs_error_fmt(hdl, EZFS_NOMEM,
3526 dgettext(TEXT_DOMAIN, "cannot get event"));
3532 error = zpool_standard_error_fmt(hdl, errno,
3533 dgettext(TEXT_DOMAIN, "cannot get event"));
3538 error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
3542 *dropped = (int)zc.zc_cookie;
3544 zcmd_free_nvlists(&zc);
3553 zpool_events_clear(libzfs_handle_t *hdl, int *count)
3555 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3558 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3559 "cannot clear events"));
3561 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
3562 return (zpool_standard_error_fmt(hdl, errno, msg));
3565 *count = (int)zc.zc_cookie; /* # of events cleared */
3571 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3572 char *pathname, size_t len)
3574 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3575 boolean_t mounted = B_FALSE;
3576 char *mntpnt = NULL;
3577 char dsname[MAXNAMELEN];
3580 /* special case for the MOS */
3581 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", (longlong_t)obj);
3585 /* get the dataset's name */
3586 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3588 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3589 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3590 /* just write out a path of two object numbers */
3591 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3592 (longlong_t)dsobj, (longlong_t)obj);
3595 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3597 /* find out if the dataset is mounted */
3598 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3600 /* get the corrupted object's path */
3601 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3603 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3606 (void) snprintf(pathname, len, "%s%s", mntpnt,
3609 (void) snprintf(pathname, len, "%s:%s",
3610 dsname, zc.zc_value);
3613 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, (longlong_t)obj);
3619 * Read the EFI label from the config, if a label does not exist then
3620 * pass back the error to the caller. If the caller has passed a non-NULL
3621 * diskaddr argument then we set it to the starting address of the EFI
3625 read_efi_label(nvlist_t *config, diskaddr_t *sb)
3629 char diskname[MAXPATHLEN];
3632 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3635 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
3636 strrchr(path, '/'));
3637 if ((fd = open(diskname, O_RDWR|O_DIRECT)) >= 0) {
3638 struct dk_gpt *vtoc;
3640 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3642 *sb = vtoc->efi_parts[0].p_start;
3651 * determine where a partition starts on a disk in the current
3655 find_start_block(nvlist_t *config)
3659 diskaddr_t sb = MAXOFFSET_T;
3662 if (nvlist_lookup_nvlist_array(config,
3663 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3664 if (nvlist_lookup_uint64(config,
3665 ZPOOL_CONFIG_WHOLE_DISK,
3666 &wholedisk) != 0 || !wholedisk) {
3667 return (MAXOFFSET_T);
3669 if (read_efi_label(config, &sb) < 0)
3674 for (c = 0; c < children; c++) {
3675 sb = find_start_block(child[c]);
3676 if (sb != MAXOFFSET_T) {
3680 return (MAXOFFSET_T);
3684 zpool_label_disk_wait(char *path, int timeout)
3686 struct stat64 statbuf;
3690 * Wait timeout miliseconds for a newly created device to be available
3691 * from the given path. There is a small window when a /dev/ device
3692 * will exist and the udev link will not, so we must wait for the
3693 * symlink. Depending on the udev rules this may take a few seconds.
3695 for (i = 0; i < timeout; i++) {
3699 if ((stat64(path, &statbuf) == 0) && (errno == 0))
3707 zpool_label_disk_check(char *path)
3709 struct dk_gpt *vtoc;
3712 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0)
3715 if ((err = efi_alloc_and_read(fd, &vtoc)) != 0) {
3720 if (vtoc->efi_flags & EFI_GPT_PRIMARY_CORRUPT) {
3732 * Label an individual disk. The name provided is the short name,
3733 * stripped of any leading /dev path.
3736 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
3738 char path[MAXPATHLEN];
3739 struct dk_gpt *vtoc;
3741 size_t resv = EFI_MIN_RESV_SIZE;
3742 uint64_t slice_size;
3743 diskaddr_t start_block;
3746 /* prepare an error message just in case */
3747 (void) snprintf(errbuf, sizeof (errbuf),
3748 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3753 if (pool_is_bootable(zhp)) {
3754 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3755 "EFI labeled devices are not supported on root "
3757 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3760 verify(nvlist_lookup_nvlist(zhp->zpool_config,
3761 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3763 if (zhp->zpool_start_block == 0)
3764 start_block = find_start_block(nvroot);
3766 start_block = zhp->zpool_start_block;
3767 zhp->zpool_start_block = start_block;
3770 start_block = NEW_START_BLOCK;
3773 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
3776 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
3778 * This shouldn't happen. We've long since verified that this
3779 * is a valid device.
3781 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3782 "unable to open device '%s': %d"), path, errno);
3783 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
3786 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
3788 * The only way this can fail is if we run out of memory, or we
3789 * were unable to read the disk's capacity
3791 if (errno == ENOMEM)
3792 (void) no_memory(hdl);
3795 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3796 "unable to read disk capacity"), name);
3798 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
3801 slice_size = vtoc->efi_last_u_lba + 1;
3802 slice_size -= EFI_MIN_RESV_SIZE;
3803 if (start_block == MAXOFFSET_T)
3804 start_block = NEW_START_BLOCK;
3805 slice_size -= start_block;
3807 vtoc->efi_parts[0].p_start = start_block;
3808 vtoc->efi_parts[0].p_size = slice_size;
3811 * Why we use V_USR: V_BACKUP confuses users, and is considered
3812 * disposable by some EFI utilities (since EFI doesn't have a backup
3813 * slice). V_UNASSIGNED is supposed to be used only for zero size
3814 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
3815 * etc. were all pretty specific. V_USR is as close to reality as we
3816 * can get, in the absence of V_OTHER.
3818 vtoc->efi_parts[0].p_tag = V_USR;
3819 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
3821 vtoc->efi_parts[8].p_start = slice_size + start_block;
3822 vtoc->efi_parts[8].p_size = resv;
3823 vtoc->efi_parts[8].p_tag = V_RESERVED;
3825 if ((rval = efi_write(fd, vtoc)) != 0) {
3827 * Some block drivers (like pcata) may not support EFI
3828 * GPT labels. Print out a helpful error message dir-
3829 * ecting the user to manually label the disk and give
3835 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "try using "
3836 "parted(8) and then provide a specific slice: %d"), rval);
3837 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3843 /* Wait for the first expected slice to appear. */
3844 (void) snprintf(path, sizeof (path), "%s/%s%s%s", DISK_ROOT, name,
3845 isdigit(name[strlen(name)-1]) ? "p" : "", FIRST_SLICE);
3846 rval = zpool_label_disk_wait(path, 3000);
3848 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "failed to "
3849 "detect device partitions on '%s': %d"), path, rval);
3850 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3853 /* We can't be to paranoid. Read the label back and verify it. */
3854 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
3855 rval = zpool_label_disk_check(path);
3857 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "freshly written "
3858 "EFI label on '%s' is damaged. Ensure\nthis device "
3859 "is not in in use, and is functioning properly: %d"),
3861 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));