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.
35 #include <sys/efi_partition.h>
37 #include <sys/zfs_ioctl.h>
40 #include "zfs_namecheck.h"
42 #include "libzfs_impl.h"
43 #include "zfs_comutil.h"
45 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
47 #define DISK_ROOT "/dev/dsk"
48 #define RDISK_ROOT "/dev/rdsk"
49 #define BACKUP_SLICE "s2"
51 typedef struct prop_flags {
52 int create:1; /* Validate property on creation */
53 int import:1; /* Validate property on import */
57 * ====================================================================
58 * zpool property functions
59 * ====================================================================
63 zpool_get_all_props(zpool_handle_t *zhp)
65 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
66 libzfs_handle_t *hdl = zhp->zpool_hdl;
68 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
70 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
73 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
74 if (errno == ENOMEM) {
75 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
76 zcmd_free_nvlists(&zc);
80 zcmd_free_nvlists(&zc);
85 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
86 zcmd_free_nvlists(&zc);
90 zcmd_free_nvlists(&zc);
96 zpool_props_refresh(zpool_handle_t *zhp)
100 old_props = zhp->zpool_props;
102 if (zpool_get_all_props(zhp) != 0)
105 nvlist_free(old_props);
110 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
116 zprop_source_t source;
118 nvl = zhp->zpool_props;
119 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
120 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
122 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
124 source = ZPROP_SRC_DEFAULT;
125 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
136 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
140 zprop_source_t source;
142 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
144 * zpool_get_all_props() has most likely failed because
145 * the pool is faulted, but if all we need is the top level
146 * vdev's guid then get it from the zhp config nvlist.
148 if ((prop == ZPOOL_PROP_GUID) &&
149 (nvlist_lookup_nvlist(zhp->zpool_config,
150 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
151 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
155 return (zpool_prop_default_numeric(prop));
158 nvl = zhp->zpool_props;
159 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
160 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
162 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
164 source = ZPROP_SRC_DEFAULT;
165 value = zpool_prop_default_numeric(prop);
175 * Map VDEV STATE to printed strings.
178 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
183 case VDEV_STATE_CLOSED:
184 case VDEV_STATE_OFFLINE:
185 return (gettext("OFFLINE"));
186 case VDEV_STATE_REMOVED:
187 return (gettext("REMOVED"));
188 case VDEV_STATE_CANT_OPEN:
189 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
190 return (gettext("FAULTED"));
191 else if (aux == VDEV_AUX_SPLIT_POOL)
192 return (gettext("SPLIT"));
194 return (gettext("UNAVAIL"));
195 case VDEV_STATE_FAULTED:
196 return (gettext("FAULTED"));
197 case VDEV_STATE_DEGRADED:
198 return (gettext("DEGRADED"));
199 case VDEV_STATE_HEALTHY:
200 return (gettext("ONLINE"));
203 return (gettext("UNKNOWN"));
207 * Get a zpool property value for 'prop' and return the value in
208 * a pre-allocated buffer.
211 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
212 zprop_source_t *srctype)
216 zprop_source_t src = ZPROP_SRC_NONE;
221 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
223 case ZPOOL_PROP_NAME:
224 (void) strlcpy(buf, zpool_get_name(zhp), len);
227 case ZPOOL_PROP_HEALTH:
228 (void) strlcpy(buf, "FAULTED", len);
231 case ZPOOL_PROP_GUID:
232 intval = zpool_get_prop_int(zhp, prop, &src);
233 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
236 case ZPOOL_PROP_ALTROOT:
237 case ZPOOL_PROP_CACHEFILE:
238 if (zhp->zpool_props != NULL ||
239 zpool_get_all_props(zhp) == 0) {
241 zpool_get_prop_string(zhp, prop, &src),
249 (void) strlcpy(buf, "-", len);
258 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
259 prop != ZPOOL_PROP_NAME)
262 switch (zpool_prop_get_type(prop)) {
263 case PROP_TYPE_STRING:
264 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
268 case PROP_TYPE_NUMBER:
269 intval = zpool_get_prop_int(zhp, prop, &src);
272 case ZPOOL_PROP_SIZE:
273 case ZPOOL_PROP_ALLOCATED:
274 case ZPOOL_PROP_FREE:
275 (void) zfs_nicenum(intval, buf, len);
278 case ZPOOL_PROP_CAPACITY:
279 (void) snprintf(buf, len, "%llu%%",
280 (u_longlong_t)intval);
283 case ZPOOL_PROP_DEDUPRATIO:
284 (void) snprintf(buf, len, "%llu.%02llux",
285 (u_longlong_t)(intval / 100),
286 (u_longlong_t)(intval % 100));
289 case ZPOOL_PROP_HEALTH:
290 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
291 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
292 verify(nvlist_lookup_uint64_array(nvroot,
293 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
296 (void) strlcpy(buf, zpool_state_to_name(intval,
300 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
304 case PROP_TYPE_INDEX:
305 intval = zpool_get_prop_int(zhp, prop, &src);
306 if (zpool_prop_index_to_string(prop, intval, &strval)
309 (void) strlcpy(buf, strval, len);
323 * Check if the bootfs name has the same pool name as it is set to.
324 * Assuming bootfs is a valid dataset name.
327 bootfs_name_valid(const char *pool, char *bootfs)
329 int len = strlen(pool);
331 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
334 if (strncmp(pool, bootfs, len) == 0 &&
335 (bootfs[len] == '/' || bootfs[len] == '\0'))
342 * Inspect the configuration to determine if any of the devices contain
346 pool_uses_efi(nvlist_t *config)
351 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
352 &child, &children) != 0)
353 return (read_efi_label(config, NULL) >= 0);
355 for (c = 0; c < children; c++) {
356 if (pool_uses_efi(child[c]))
363 pool_is_bootable(zpool_handle_t *zhp)
365 char bootfs[ZPOOL_MAXNAMELEN];
367 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
368 sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
369 sizeof (bootfs)) != 0);
374 * Given an nvlist of zpool properties to be set, validate that they are
375 * correct, and parse any numeric properties (index, boolean, etc) if they are
376 * specified as strings.
379 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
380 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
388 struct stat64 statbuf;
392 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
393 (void) no_memory(hdl);
398 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
399 const char *propname = nvpair_name(elem);
402 * Make sure this property is valid and applies to this type.
404 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
405 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
406 "invalid property '%s'"), propname);
407 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
411 if (zpool_prop_readonly(prop)) {
412 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
413 "is readonly"), propname);
414 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
418 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
419 &strval, &intval, errbuf) != 0)
423 * Perform additional checking for specific properties.
428 case ZPOOL_PROP_VERSION:
429 if (intval < version || intval > SPA_VERSION) {
430 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
431 "property '%s' number %d is invalid."),
433 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
438 case ZPOOL_PROP_BOOTFS:
439 if (flags.create || flags.import) {
440 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
441 "property '%s' cannot be set at creation "
442 "or import time"), propname);
443 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
447 if (version < SPA_VERSION_BOOTFS) {
448 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
449 "pool must be upgraded to support "
450 "'%s' property"), propname);
451 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
456 * bootfs property value has to be a dataset name and
457 * the dataset has to be in the same pool as it sets to.
459 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
461 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
462 "is an invalid name"), strval);
463 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
467 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
468 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
469 "could not open pool '%s'"), poolname);
470 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
473 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
474 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
477 * bootfs property cannot be set on a disk which has
480 if (pool_uses_efi(nvroot)) {
481 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
482 "property '%s' not supported on "
483 "EFI labeled devices"), propname);
484 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
491 case ZPOOL_PROP_ALTROOT:
492 if (!flags.create && !flags.import) {
493 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
494 "property '%s' can only be set during pool "
495 "creation or import"), propname);
496 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
500 if (strval[0] != '/') {
501 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
502 "bad alternate root '%s'"), strval);
503 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
508 case ZPOOL_PROP_CACHEFILE:
509 if (strval[0] == '\0')
512 if (strcmp(strval, "none") == 0)
515 if (strval[0] != '/') {
516 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
517 "property '%s' must be empty, an "
518 "absolute path, or 'none'"), propname);
519 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
523 slash = strrchr(strval, '/');
525 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
526 strcmp(slash, "/..") == 0) {
527 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
528 "'%s' is not a valid file"), strval);
529 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
535 if (strval[0] != '\0' &&
536 (stat64(strval, &statbuf) != 0 ||
537 !S_ISDIR(statbuf.st_mode))) {
538 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
539 "'%s' is not a valid directory"),
541 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
548 case ZPOOL_PROP_READONLY:
550 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
551 "property '%s' can only be set at "
552 "import time"), propname);
553 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
562 nvlist_free(retprops);
567 * Set zpool property : propname=propval.
570 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
572 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
575 nvlist_t *nvl = NULL;
578 prop_flags_t flags = { 0 };
580 (void) snprintf(errbuf, sizeof (errbuf),
581 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
584 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
585 return (no_memory(zhp->zpool_hdl));
587 if (nvlist_add_string(nvl, propname, propval) != 0) {
589 return (no_memory(zhp->zpool_hdl));
592 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
593 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
594 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
603 * Execute the corresponding ioctl() to set this property.
605 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
607 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
612 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
614 zcmd_free_nvlists(&zc);
618 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
620 (void) zpool_props_refresh(zhp);
626 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
628 libzfs_handle_t *hdl = zhp->zpool_hdl;
630 char buf[ZFS_MAXPROPLEN];
632 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
635 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
640 if (entry->pl_prop != ZPROP_INVAL &&
641 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
643 if (strlen(buf) > entry->pl_width)
644 entry->pl_width = strlen(buf);
653 * Don't start the slice at the default block of 34; many storage
654 * devices will use a stripe width of 128k, so start there instead.
656 #define NEW_START_BLOCK 256
659 * Validate the given pool name, optionally putting an extended error message in
663 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
669 ret = pool_namecheck(pool, &why, &what);
672 * The rules for reserved pool names were extended at a later point.
673 * But we need to support users with existing pools that may now be
674 * invalid. So we only check for this expanded set of names during a
675 * create (or import), and only in userland.
677 if (ret == 0 && !isopen &&
678 (strncmp(pool, "mirror", 6) == 0 ||
679 strncmp(pool, "raidz", 5) == 0 ||
680 strncmp(pool, "spare", 5) == 0 ||
681 strcmp(pool, "log") == 0)) {
684 dgettext(TEXT_DOMAIN, "name is reserved"));
692 case NAME_ERR_TOOLONG:
694 dgettext(TEXT_DOMAIN, "name is too long"));
697 case NAME_ERR_INVALCHAR:
699 dgettext(TEXT_DOMAIN, "invalid character "
700 "'%c' in pool name"), what);
703 case NAME_ERR_NOLETTER:
704 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
705 "name must begin with a letter"));
708 case NAME_ERR_RESERVED:
709 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
710 "name is reserved"));
713 case NAME_ERR_DISKLIKE:
714 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
715 "pool name is reserved"));
718 case NAME_ERR_LEADING_SLASH:
719 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
720 "leading slash in name"));
723 case NAME_ERR_EMPTY_COMPONENT:
724 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
725 "empty component in name"));
728 case NAME_ERR_TRAILING_SLASH:
729 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
730 "trailing slash in name"));
733 case NAME_ERR_MULTIPLE_AT:
734 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
735 "multiple '@' delimiters in name"));
738 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
739 "permission set is missing '@'"));
750 * Open a handle to the given pool, even if the pool is currently in the FAULTED
754 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
760 * Make sure the pool name is valid.
762 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
763 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
764 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
769 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
772 zhp->zpool_hdl = hdl;
773 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
775 if (zpool_refresh_stats(zhp, &missing) != 0) {
781 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
782 (void) zfs_error_fmt(hdl, EZFS_NOENT,
783 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
792 * Like the above, but silent on error. Used when iterating over pools (because
793 * the configuration cache may be out of date).
796 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
801 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
804 zhp->zpool_hdl = hdl;
805 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
807 if (zpool_refresh_stats(zhp, &missing) != 0) {
823 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
827 zpool_open(libzfs_handle_t *hdl, const char *pool)
831 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
834 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
835 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
836 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
845 * Close the handle. Simply frees the memory associated with the handle.
848 zpool_close(zpool_handle_t *zhp)
850 if (zhp->zpool_config)
851 nvlist_free(zhp->zpool_config);
852 if (zhp->zpool_old_config)
853 nvlist_free(zhp->zpool_old_config);
854 if (zhp->zpool_props)
855 nvlist_free(zhp->zpool_props);
860 * Return the name of the pool.
863 zpool_get_name(zpool_handle_t *zhp)
865 return (zhp->zpool_name);
870 * Return the state of the pool (ACTIVE or UNAVAILABLE)
873 zpool_get_state(zpool_handle_t *zhp)
875 return (zhp->zpool_state);
879 * Create the named pool, using the provided vdev list. It is assumed
880 * that the consumer has already validated the contents of the nvlist, so we
881 * don't have to worry about error semantics.
884 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
885 nvlist_t *props, nvlist_t *fsprops)
887 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
888 nvlist_t *zc_fsprops = NULL;
889 nvlist_t *zc_props = NULL;
894 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
895 "cannot create '%s'"), pool);
897 if (!zpool_name_valid(hdl, B_FALSE, pool))
898 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
900 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
904 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
906 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
907 SPA_VERSION_1, flags, msg)) == NULL) {
916 zoned = ((nvlist_lookup_string(fsprops,
917 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
918 strcmp(zonestr, "on") == 0);
920 if ((zc_fsprops = zfs_valid_proplist(hdl,
921 ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
925 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
928 if (nvlist_add_nvlist(zc_props,
929 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
934 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
937 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
939 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
941 zcmd_free_nvlists(&zc);
942 nvlist_free(zc_props);
943 nvlist_free(zc_fsprops);
948 * This can happen if the user has specified the same
949 * device multiple times. We can't reliably detect this
950 * until we try to add it and see we already have a
953 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
954 "one or more vdevs refer to the same device"));
955 return (zfs_error(hdl, EZFS_BADDEV, msg));
959 * This occurs when one of the devices is below
960 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
961 * device was the problem device since there's no
962 * reliable way to determine device size from userland.
967 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
969 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
970 "one or more devices is less than the "
971 "minimum size (%s)"), buf);
973 return (zfs_error(hdl, EZFS_BADDEV, msg));
976 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
977 "one or more devices is out of space"));
978 return (zfs_error(hdl, EZFS_BADDEV, msg));
981 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
982 "cache device must be a disk or disk slice"));
983 return (zfs_error(hdl, EZFS_BADDEV, msg));
986 return (zpool_standard_error(hdl, errno, msg));
991 * If this is an alternate root pool, then we automatically set the
992 * mountpoint of the root dataset to be '/'.
994 if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
998 verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
999 verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1006 zcmd_free_nvlists(&zc);
1007 nvlist_free(zc_props);
1008 nvlist_free(zc_fsprops);
1013 * Destroy the given pool. It is up to the caller to ensure that there are no
1014 * datasets left in the pool.
1017 zpool_destroy(zpool_handle_t *zhp)
1019 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1020 zfs_handle_t *zfp = NULL;
1021 libzfs_handle_t *hdl = zhp->zpool_hdl;
1024 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1025 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1028 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1030 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1031 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1032 "cannot destroy '%s'"), zhp->zpool_name);
1034 if (errno == EROFS) {
1035 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1036 "one or more devices is read only"));
1037 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1039 (void) zpool_standard_error(hdl, errno, msg);
1048 remove_mountpoint(zfp);
1056 * Add the given vdevs to the pool. The caller must have already performed the
1057 * necessary verification to ensure that the vdev specification is well-formed.
1060 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1062 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1064 libzfs_handle_t *hdl = zhp->zpool_hdl;
1066 nvlist_t **spares, **l2cache;
1067 uint_t nspares, nl2cache;
1069 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1070 "cannot add to '%s'"), zhp->zpool_name);
1072 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1073 SPA_VERSION_SPARES &&
1074 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1075 &spares, &nspares) == 0) {
1076 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1077 "upgraded to add hot spares"));
1078 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1081 if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1082 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1085 for (s = 0; s < nspares; s++) {
1088 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1089 &path) == 0 && pool_uses_efi(spares[s])) {
1090 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1091 "device '%s' contains an EFI label and "
1092 "cannot be used on root pools."),
1093 zpool_vdev_name(hdl, NULL, spares[s],
1095 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1100 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1101 SPA_VERSION_L2CACHE &&
1102 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1103 &l2cache, &nl2cache) == 0) {
1104 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1105 "upgraded to add cache devices"));
1106 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1109 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1111 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1113 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1117 * This can happen if the user has specified the same
1118 * device multiple times. We can't reliably detect this
1119 * until we try to add it and see we already have a
1122 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1123 "one or more vdevs refer to the same device"));
1124 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1129 * This occurrs when one of the devices is below
1130 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1131 * device was the problem device since there's no
1132 * reliable way to determine device size from userland.
1137 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1139 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1140 "device is less than the minimum "
1143 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1147 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1148 "pool must be upgraded to add these vdevs"));
1149 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1153 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1154 "root pool can not have multiple vdevs"
1155 " or separate logs"));
1156 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1160 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1161 "cache device must be a disk or disk slice"));
1162 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1166 (void) zpool_standard_error(hdl, errno, msg);
1174 zcmd_free_nvlists(&zc);
1180 * Exports the pool from the system. The caller must ensure that there are no
1181 * mounted datasets in the pool.
1184 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1186 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1189 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1190 "cannot export '%s'"), zhp->zpool_name);
1192 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1193 zc.zc_cookie = force;
1194 zc.zc_guid = hardforce;
1196 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1199 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1200 "use '-f' to override the following errors:\n"
1201 "'%s' has an active shared spare which could be"
1202 " used by other pools once '%s' is exported."),
1203 zhp->zpool_name, zhp->zpool_name);
1204 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1207 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1216 zpool_export(zpool_handle_t *zhp, boolean_t force)
1218 return (zpool_export_common(zhp, force, B_FALSE));
1222 zpool_export_force(zpool_handle_t *zhp)
1224 return (zpool_export_common(zhp, B_TRUE, B_TRUE));
1228 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1231 nvlist_t *nv = NULL;
1237 if (!hdl->libzfs_printerr || config == NULL)
1240 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0)
1243 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1245 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1247 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1248 strftime(timestr, 128, "%c", &t) != 0) {
1250 (void) printf(dgettext(TEXT_DOMAIN,
1251 "Would be able to return %s "
1252 "to its state as of %s.\n"),
1255 (void) printf(dgettext(TEXT_DOMAIN,
1256 "Pool %s returned to its state as of %s.\n"),
1260 (void) printf(dgettext(TEXT_DOMAIN,
1261 "%s approximately %lld "),
1262 dryrun ? "Would discard" : "Discarded",
1263 ((longlong_t)loss + 30) / 60);
1264 (void) printf(dgettext(TEXT_DOMAIN,
1265 "minutes of transactions.\n"));
1266 } else if (loss > 0) {
1267 (void) printf(dgettext(TEXT_DOMAIN,
1268 "%s approximately %lld "),
1269 dryrun ? "Would discard" : "Discarded",
1271 (void) printf(dgettext(TEXT_DOMAIN,
1272 "seconds of transactions.\n"));
1278 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1281 nvlist_t *nv = NULL;
1283 uint64_t edata = UINT64_MAX;
1288 if (!hdl->libzfs_printerr)
1292 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1294 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1296 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1297 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1298 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1301 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1302 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1305 (void) printf(dgettext(TEXT_DOMAIN,
1306 "Recovery is possible, but will result in some data loss.\n"));
1308 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1309 strftime(timestr, 128, "%c", &t) != 0) {
1310 (void) printf(dgettext(TEXT_DOMAIN,
1311 "\tReturning the pool to its state as of %s\n"
1312 "\tshould correct the problem. "),
1315 (void) printf(dgettext(TEXT_DOMAIN,
1316 "\tReverting the pool to an earlier state "
1317 "should correct the problem.\n\t"));
1321 (void) printf(dgettext(TEXT_DOMAIN,
1322 "Approximately %lld minutes of data\n"
1323 "\tmust be discarded, irreversibly. "),
1324 ((longlong_t)loss + 30) / 60);
1325 } else if (loss > 0) {
1326 (void) printf(dgettext(TEXT_DOMAIN,
1327 "Approximately %lld seconds of data\n"
1328 "\tmust be discarded, irreversibly. "),
1331 if (edata != 0 && edata != UINT64_MAX) {
1333 (void) printf(dgettext(TEXT_DOMAIN,
1334 "After rewind, at least\n"
1335 "\tone persistent user-data error will remain. "));
1337 (void) printf(dgettext(TEXT_DOMAIN,
1338 "After rewind, several\n"
1339 "\tpersistent user-data errors will remain. "));
1342 (void) printf(dgettext(TEXT_DOMAIN,
1343 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1344 reason >= 0 ? "clear" : "import", name);
1346 (void) printf(dgettext(TEXT_DOMAIN,
1347 "A scrub of the pool\n"
1348 "\tis strongly recommended after recovery.\n"));
1352 (void) printf(dgettext(TEXT_DOMAIN,
1353 "Destroy and re-create the pool from\n\ta backup source.\n"));
1357 * zpool_import() is a contracted interface. Should be kept the same
1360 * Applications should use zpool_import_props() to import a pool with
1361 * new properties value to be set.
1364 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1367 nvlist_t *props = NULL;
1370 if (altroot != NULL) {
1371 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1372 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1373 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1377 if (nvlist_add_string(props,
1378 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1379 nvlist_add_string(props,
1380 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1382 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1383 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1388 ret = zpool_import_props(hdl, config, newname, props,
1396 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1402 uint64_t is_log = 0;
1404 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1408 (void) printf("\t%*s%s%s\n", indent, "", name,
1409 is_log ? " [log]" : "");
1411 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1412 &child, &children) != 0)
1415 for (c = 0; c < children; c++) {
1416 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
1417 print_vdev_tree(hdl, vname, child[c], indent + 2);
1423 * Import the given pool using the known configuration and a list of
1424 * properties to be set. The configuration should have come from
1425 * zpool_find_import(). The 'newname' parameters control whether the pool
1426 * is imported with a different name.
1429 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1430 nvlist_t *props, int flags)
1432 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1433 zpool_rewind_policy_t policy;
1434 nvlist_t *nv = NULL;
1435 nvlist_t *nvinfo = NULL;
1436 nvlist_t *missing = NULL;
1443 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1446 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1447 "cannot import pool '%s'"), origname);
1449 if (newname != NULL) {
1450 if (!zpool_name_valid(hdl, B_FALSE, newname))
1451 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1452 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1454 thename = (char *)newname;
1461 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1463 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1466 if ((props = zpool_valid_proplist(hdl, origname,
1467 props, version, flags, errbuf)) == NULL) {
1469 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1475 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1477 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1480 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1484 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1489 zc.zc_cookie = flags;
1490 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1492 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1493 zcmd_free_nvlists(&zc);
1500 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1501 zpool_get_rewind_policy(config, &policy);
1507 * Dry-run failed, but we print out what success
1508 * looks like if we found a best txg
1510 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1511 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1517 if (newname == NULL)
1518 (void) snprintf(desc, sizeof (desc),
1519 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1522 (void) snprintf(desc, sizeof (desc),
1523 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1529 * Unsupported version.
1531 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1535 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1539 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1540 "one or more devices is read only"));
1541 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1545 if (nv && nvlist_lookup_nvlist(nv,
1546 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1547 nvlist_lookup_nvlist(nvinfo,
1548 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1549 (void) printf(dgettext(TEXT_DOMAIN,
1550 "The devices below are missing, use "
1551 "'-m' to import the pool anyway:\n"));
1552 print_vdev_tree(hdl, NULL, missing, 2);
1553 (void) printf("\n");
1555 (void) zpool_standard_error(hdl, error, desc);
1559 (void) zpool_standard_error(hdl, error, desc);
1563 (void) zpool_standard_error(hdl, error, desc);
1564 zpool_explain_recover(hdl,
1565 newname ? origname : thename, -error, nv);
1572 zpool_handle_t *zhp;
1575 * This should never fail, but play it safe anyway.
1577 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1579 else if (zhp != NULL)
1581 if (policy.zrp_request &
1582 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1583 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1584 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1590 zcmd_free_nvlists(&zc);
1600 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1602 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1604 libzfs_handle_t *hdl = zhp->zpool_hdl;
1606 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1607 zc.zc_cookie = func;
1609 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
1610 (errno == ENOENT && func != POOL_SCAN_NONE))
1613 if (func == POOL_SCAN_SCRUB) {
1614 (void) snprintf(msg, sizeof (msg),
1615 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1616 } else if (func == POOL_SCAN_NONE) {
1617 (void) snprintf(msg, sizeof (msg),
1618 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1621 assert(!"unexpected result");
1624 if (errno == EBUSY) {
1626 pool_scan_stat_t *ps = NULL;
1629 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1630 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1631 (void) nvlist_lookup_uint64_array(nvroot,
1632 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1633 if (ps && ps->pss_func == POOL_SCAN_SCRUB)
1634 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1636 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1637 } else if (errno == ENOENT) {
1638 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1640 return (zpool_standard_error(hdl, errno, msg));
1645 * This provides a very minimal check whether a given string is likely a
1646 * c#t#d# style string. Users of this are expected to do their own
1647 * verification of the s# part.
1649 #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1]))
1652 * More elaborate version for ones which may start with "/dev/dsk/"
1656 ctd_check_path(char *str) {
1658 * If it starts with a slash, check the last component.
1660 if (str && str[0] == '/') {
1661 char *tmp = strrchr(str, '/');
1664 * If it ends in "/old", check the second-to-last
1665 * component of the string instead.
1667 if (tmp != str && strcmp(tmp, "/old") == 0) {
1668 for (tmp--; *tmp != '/'; tmp--)
1673 return (CTD_CHECK(str));
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
1724 * "s0" or "s0/old". The "s0" part is hidden from the user,
1725 * but included in the string, so this matches around it.
1726 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1728 * Otherwise, all other searches are simple string compares.
1730 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
1731 ctd_check_path(val)) {
1732 uint64_t wholedisk = 0;
1734 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1737 int slen = strlen(srchval);
1738 int vlen = strlen(val);
1740 if (slen != vlen - 2)
1744 * make_leaf_vdev() should only set
1745 * wholedisk for ZPOOL_CONFIG_PATHs which
1746 * will include "/dev/dsk/", giving plenty of
1747 * room for the indices used next.
1752 * strings identical except trailing "s0"
1754 if (strcmp(&val[vlen - 2], "s0") == 0 &&
1755 strncmp(srchval, val, slen) == 0)
1759 * strings identical except trailing "s0/old"
1761 if (strcmp(&val[vlen - 6], "s0/old") == 0 &&
1762 strcmp(&srchval[slen - 4], "/old") == 0 &&
1763 strncmp(srchval, val, slen - 4) == 0)
1768 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
1769 char *type, *idx, *end, *p;
1770 uint64_t id, vdev_id;
1773 * Determine our vdev type, keeping in mind
1774 * that the srchval is composed of a type and
1775 * vdev id pair (i.e. mirror-4).
1777 if ((type = strdup(srchval)) == NULL)
1780 if ((p = strrchr(type, '-')) == NULL) {
1788 * If the types don't match then keep looking.
1790 if (strncmp(val, type, strlen(val)) != 0) {
1795 verify(strncmp(type, VDEV_TYPE_RAIDZ,
1796 strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1797 strncmp(type, VDEV_TYPE_MIRROR,
1798 strlen(VDEV_TYPE_MIRROR)) == 0);
1799 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
1803 vdev_id = strtoull(idx, &end, 10);
1810 * Now verify that we have the correct vdev id.
1819 if (strcmp(srchval, val) == 0)
1828 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1829 &child, &children) != 0)
1832 for (c = 0; c < children; c++) {
1833 if ((ret = vdev_to_nvlist_iter(child[c], search,
1834 avail_spare, l2cache, NULL)) != NULL) {
1836 * The 'is_log' value is only set for the toplevel
1837 * vdev, not the leaf vdevs. So we always lookup the
1838 * log device from the root of the vdev tree (where
1839 * 'log' is non-NULL).
1842 nvlist_lookup_uint64(child[c],
1843 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
1851 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1852 &child, &children) == 0) {
1853 for (c = 0; c < children; c++) {
1854 if ((ret = vdev_to_nvlist_iter(child[c], search,
1855 avail_spare, l2cache, NULL)) != NULL) {
1856 *avail_spare = B_TRUE;
1862 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1863 &child, &children) == 0) {
1864 for (c = 0; c < children; c++) {
1865 if ((ret = vdev_to_nvlist_iter(child[c], search,
1866 avail_spare, l2cache, NULL)) != NULL) {
1877 * Given a physical path (minus the "/devices" prefix), find the
1881 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
1882 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
1884 nvlist_t *search, *nvroot, *ret;
1886 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1887 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
1889 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1892 *avail_spare = B_FALSE;
1896 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1897 nvlist_free(search);
1903 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
1906 zpool_vdev_is_interior(const char *name)
1908 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1909 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
1915 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
1916 boolean_t *l2cache, boolean_t *log)
1918 char buf[MAXPATHLEN];
1920 nvlist_t *nvroot, *search, *ret;
1923 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1925 guid = strtoull(path, &end, 10);
1926 if (guid != 0 && *end == '\0') {
1927 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
1928 } else if (zpool_vdev_is_interior(path)) {
1929 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
1930 } else if (path[0] != '/') {
1931 (void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
1932 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
1934 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
1937 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1940 *avail_spare = B_FALSE;
1944 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1945 nvlist_free(search);
1951 vdev_online(nvlist_t *nv)
1955 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
1956 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
1957 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
1964 * Helper function for zpool_get_physpaths().
1967 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
1968 size_t *bytes_written)
1970 size_t bytes_left, pos, rsz;
1974 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
1976 return (EZFS_NODEVICE);
1978 pos = *bytes_written;
1979 bytes_left = physpath_size - pos;
1980 format = (pos == 0) ? "%s" : " %s";
1982 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
1983 *bytes_written += rsz;
1985 if (rsz >= bytes_left) {
1986 /* if physpath was not copied properly, clear it */
1987 if (bytes_left != 0) {
1990 return (EZFS_NOSPC);
1996 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
1997 size_t *rsz, boolean_t is_spare)
2002 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2003 return (EZFS_INVALCONFIG);
2005 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2007 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2008 * For a spare vdev, we only want to boot from the active
2013 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2016 return (EZFS_INVALCONFIG);
2019 if (vdev_online(nv)) {
2020 if ((ret = vdev_get_one_physpath(nv, physpath,
2021 phypath_size, rsz)) != 0)
2024 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2025 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2026 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2031 if (nvlist_lookup_nvlist_array(nv,
2032 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2033 return (EZFS_INVALCONFIG);
2035 for (i = 0; i < count; i++) {
2036 ret = vdev_get_physpaths(child[i], physpath,
2037 phypath_size, rsz, is_spare);
2038 if (ret == EZFS_NOSPC)
2043 return (EZFS_POOL_INVALARG);
2047 * Get phys_path for a root pool config.
2048 * Return 0 on success; non-zero on failure.
2051 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2054 nvlist_t *vdev_root;
2061 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2063 return (EZFS_INVALCONFIG);
2065 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2066 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2067 &child, &count) != 0)
2068 return (EZFS_INVALCONFIG);
2071 * root pool can not have EFI labeled disks and can only have
2072 * a single top-level vdev.
2074 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
2075 pool_uses_efi(vdev_root))
2076 return (EZFS_POOL_INVALARG);
2078 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2081 /* No online devices */
2083 return (EZFS_NODEVICE);
2089 * Get phys_path for a root pool
2090 * Return 0 on success; non-zero on failure.
2093 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2095 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2100 * If the device has being dynamically expanded then we need to relabel
2101 * the disk to use the new unallocated space.
2104 zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2106 char path[MAXPATHLEN];
2109 int (*_efi_use_whole_disk)(int);
2111 if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2112 "efi_use_whole_disk")) == NULL)
2115 (void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name);
2117 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2118 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2119 "relabel '%s': unable to open device"), name);
2120 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2124 * It's possible that we might encounter an error if the device
2125 * does not have any unallocated space left. If so, we simply
2126 * ignore that error and continue on.
2128 error = _efi_use_whole_disk(fd);
2130 if (error && error != VT_ENOSPC) {
2131 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2132 "relabel '%s': unable to read disk capacity"), name);
2133 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2139 * Bring the specified vdev online. The 'flags' parameter is a set of the
2140 * ZFS_ONLINE_* flags.
2143 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2144 vdev_state_t *newstate)
2146 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2149 boolean_t avail_spare, l2cache, islog;
2150 libzfs_handle_t *hdl = zhp->zpool_hdl;
2152 if (flags & ZFS_ONLINE_EXPAND) {
2153 (void) snprintf(msg, sizeof (msg),
2154 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2156 (void) snprintf(msg, sizeof (msg),
2157 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2160 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2161 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2163 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2165 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2168 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2170 if (flags & ZFS_ONLINE_EXPAND ||
2171 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2172 char *pathname = NULL;
2173 uint64_t wholedisk = 0;
2175 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2177 verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
2181 * XXX - L2ARC 1.0 devices can't support expansion.
2184 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2185 "cannot expand cache devices"));
2186 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2190 pathname += strlen(DISK_ROOT) + 1;
2191 (void) zpool_relabel_disk(hdl, pathname);
2195 zc.zc_cookie = VDEV_STATE_ONLINE;
2198 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2199 if (errno == EINVAL) {
2200 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2201 "from this pool into a new one. Use '%s' "
2202 "instead"), "zpool detach");
2203 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2205 return (zpool_standard_error(hdl, errno, msg));
2208 *newstate = zc.zc_cookie;
2213 * Take the specified vdev offline
2216 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2218 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2221 boolean_t avail_spare, l2cache;
2222 libzfs_handle_t *hdl = zhp->zpool_hdl;
2224 (void) snprintf(msg, sizeof (msg),
2225 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2227 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2228 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2230 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2232 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2235 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2237 zc.zc_cookie = VDEV_STATE_OFFLINE;
2238 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2240 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2247 * There are no other replicas of this device.
2249 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2253 * The log device has unplayed logs
2255 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2258 return (zpool_standard_error(hdl, errno, msg));
2263 * Mark the given vdev faulted.
2266 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2268 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2270 libzfs_handle_t *hdl = zhp->zpool_hdl;
2272 (void) snprintf(msg, sizeof (msg),
2273 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
2275 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2277 zc.zc_cookie = VDEV_STATE_FAULTED;
2280 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2287 * There are no other replicas of this device.
2289 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2292 return (zpool_standard_error(hdl, errno, msg));
2298 * Mark the given vdev degraded.
2301 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2303 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2305 libzfs_handle_t *hdl = zhp->zpool_hdl;
2307 (void) snprintf(msg, sizeof (msg),
2308 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid);
2310 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2312 zc.zc_cookie = VDEV_STATE_DEGRADED;
2315 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2318 return (zpool_standard_error(hdl, errno, msg));
2322 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2326 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2332 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2334 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2337 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2338 children == 2 && child[which] == tgt)
2341 for (c = 0; c < children; c++)
2342 if (is_replacing_spare(child[c], tgt, which))
2350 * Attach new_disk (fully described by nvroot) to old_disk.
2351 * If 'replacing' is specified, the new disk will replace the old one.
2354 zpool_vdev_attach(zpool_handle_t *zhp,
2355 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2357 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2361 boolean_t avail_spare, l2cache, islog;
2366 nvlist_t *config_root;
2367 libzfs_handle_t *hdl = zhp->zpool_hdl;
2368 boolean_t rootpool = pool_is_bootable(zhp);
2371 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2372 "cannot replace %s with %s"), old_disk, new_disk);
2374 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2375 "cannot attach %s to %s"), new_disk, old_disk);
2378 * If this is a root pool, make sure that we're not attaching an
2379 * EFI labeled device.
2381 if (rootpool && pool_uses_efi(nvroot)) {
2382 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2383 "EFI labeled devices are not supported on root pools."));
2384 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2387 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2388 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2390 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2393 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2396 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2398 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2399 zc.zc_cookie = replacing;
2401 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2402 &child, &children) != 0 || children != 1) {
2403 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2404 "new device must be a single disk"));
2405 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2408 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2409 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2411 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2415 * If the target is a hot spare that has been swapped in, we can only
2416 * replace it with another hot spare.
2419 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2420 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2421 NULL) == NULL || !avail_spare) &&
2422 is_replacing_spare(config_root, tgt, 1)) {
2423 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2424 "can only be replaced by another hot spare"));
2426 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2431 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2434 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2436 zcmd_free_nvlists(&zc);
2441 * XXX need a better way to prevent user from
2442 * booting up a half-baked vdev.
2444 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2445 "sure to wait until resilver is done "
2446 "before rebooting.\n"));
2454 * Can't attach to or replace this type of vdev.
2457 uint64_t version = zpool_get_prop_int(zhp,
2458 ZPOOL_PROP_VERSION, NULL);
2461 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2462 "cannot replace a log with a spare"));
2463 else if (version >= SPA_VERSION_MULTI_REPLACE)
2464 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2465 "already in replacing/spare config; wait "
2466 "for completion or use 'zpool detach'"));
2468 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2469 "cannot replace a replacing device"));
2471 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2472 "can only attach to mirrors and top-level "
2475 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2480 * The new device must be a single disk.
2482 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2483 "new device must be a single disk"));
2484 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2488 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2490 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2495 * The new device is too small.
2497 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2498 "device is too small"));
2499 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2504 * The new device has a different alignment requirement.
2506 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2507 "devices have different sector alignment"));
2508 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2513 * The resulting top-level vdev spec won't fit in the label.
2515 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2519 (void) zpool_standard_error(hdl, errno, msg);
2526 * Detach the specified device.
2529 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2531 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2534 boolean_t avail_spare, l2cache;
2535 libzfs_handle_t *hdl = zhp->zpool_hdl;
2537 (void) snprintf(msg, sizeof (msg),
2538 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2540 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2541 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2543 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2546 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2549 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2551 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2553 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2560 * Can't detach from this type of vdev.
2562 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2563 "applicable to mirror and replacing vdevs"));
2564 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2569 * There are no other replicas of this device.
2571 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2575 (void) zpool_standard_error(hdl, errno, msg);
2582 * Find a mirror vdev in the source nvlist.
2584 * The mchild array contains a list of disks in one of the top-level mirrors
2585 * of the source pool. The schild array contains a list of disks that the
2586 * user specified on the command line. We loop over the mchild array to
2587 * see if any entry in the schild array matches.
2589 * If a disk in the mchild array is found in the schild array, we return
2590 * the index of that entry. Otherwise we return -1.
2593 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2594 nvlist_t **schild, uint_t schildren)
2598 for (mc = 0; mc < mchildren; mc++) {
2600 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2601 mchild[mc], B_FALSE);
2603 for (sc = 0; sc < schildren; sc++) {
2604 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2605 schild[sc], B_FALSE);
2606 boolean_t result = (strcmp(mpath, spath) == 0);
2622 * Split a mirror pool. If newroot points to null, then a new nvlist
2623 * is generated and it is the responsibility of the caller to free it.
2626 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2627 nvlist_t *props, splitflags_t flags)
2629 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2631 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2632 nvlist_t **varray = NULL, *zc_props = NULL;
2633 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2634 libzfs_handle_t *hdl = zhp->zpool_hdl;
2636 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2639 (void) snprintf(msg, sizeof (msg),
2640 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2642 if (!zpool_name_valid(hdl, B_FALSE, newname))
2643 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2645 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2646 (void) fprintf(stderr, gettext("Internal error: unable to "
2647 "retrieve pool configuration\n"));
2651 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2653 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2656 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2657 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2658 props, vers, flags, msg)) == NULL)
2662 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2664 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2665 "Source pool is missing vdev tree"));
2667 nvlist_free(zc_props);
2671 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2674 if (*newroot == NULL ||
2675 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2676 &newchild, &newchildren) != 0)
2679 for (c = 0; c < children; c++) {
2680 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2682 nvlist_t **mchild, *vdev;
2687 * Unlike cache & spares, slogs are stored in the
2688 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2690 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2692 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2694 if (is_log || is_hole) {
2696 * Create a hole vdev and put it in the config.
2698 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2700 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2701 VDEV_TYPE_HOLE) != 0)
2703 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2708 varray[vcount++] = vdev;
2712 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
2714 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2715 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2716 "Source pool must be composed only of mirrors\n"));
2717 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2721 verify(nvlist_lookup_nvlist_array(child[c],
2722 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2724 /* find or add an entry for this top-level vdev */
2725 if (newchildren > 0 &&
2726 (entry = find_vdev_entry(zhp, mchild, mchildren,
2727 newchild, newchildren)) >= 0) {
2728 /* We found a disk that the user specified. */
2729 vdev = mchild[entry];
2732 /* User didn't specify a disk for this vdev. */
2733 vdev = mchild[mchildren - 1];
2736 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
2740 /* did we find every disk the user specified? */
2741 if (found != newchildren) {
2742 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
2743 "include at most one disk from each mirror"));
2744 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2748 /* Prepare the nvlist for populating. */
2749 if (*newroot == NULL) {
2750 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
2753 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
2754 VDEV_TYPE_ROOT) != 0)
2757 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
2760 /* Add all the children we found */
2761 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
2762 lastlog == 0 ? vcount : lastlog) != 0)
2766 * If we're just doing a dry run, exit now with success.
2769 memory_err = B_FALSE;
2774 /* now build up the config list & call the ioctl */
2775 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
2778 if (nvlist_add_nvlist(newconfig,
2779 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
2780 nvlist_add_string(newconfig,
2781 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
2782 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
2786 * The new pool is automatically part of the namespace unless we
2787 * explicitly export it.
2790 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
2791 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2792 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
2793 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
2795 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
2798 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
2799 retval = zpool_standard_error(hdl, errno, msg);
2804 memory_err = B_FALSE;
2807 if (varray != NULL) {
2810 for (v = 0; v < vcount; v++)
2811 nvlist_free(varray[v]);
2814 zcmd_free_nvlists(&zc);
2816 nvlist_free(zc_props);
2818 nvlist_free(newconfig);
2820 nvlist_free(*newroot);
2828 return (no_memory(hdl));
2834 * Remove the given device. Currently, this is supported only for hot spares
2835 * and level 2 cache devices.
2838 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
2840 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2843 boolean_t avail_spare, l2cache, islog;
2844 libzfs_handle_t *hdl = zhp->zpool_hdl;
2847 (void) snprintf(msg, sizeof (msg),
2848 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
2850 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2851 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2853 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2855 * XXX - this should just go away.
2857 if (!avail_spare && !l2cache && !islog) {
2858 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2859 "only inactive hot spares, cache, top-level, "
2860 "or log devices can be removed"));
2861 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2864 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
2865 if (islog && version < SPA_VERSION_HOLES) {
2866 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2867 "pool must be upgrade to support log removal"));
2868 return (zfs_error(hdl, EZFS_BADVERSION, msg));
2871 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2873 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
2876 return (zpool_standard_error(hdl, errno, msg));
2880 * Clear the errors for the pool, or the particular device if specified.
2883 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
2885 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2888 zpool_rewind_policy_t policy;
2889 boolean_t avail_spare, l2cache;
2890 libzfs_handle_t *hdl = zhp->zpool_hdl;
2891 nvlist_t *nvi = NULL;
2895 (void) snprintf(msg, sizeof (msg),
2896 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2899 (void) snprintf(msg, sizeof (msg),
2900 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2903 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2905 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
2906 &l2cache, NULL)) == 0)
2907 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2910 * Don't allow error clearing for hot spares. Do allow
2911 * error clearing for l2cache devices.
2914 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2916 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
2920 zpool_get_rewind_policy(rewindnvl, &policy);
2921 zc.zc_cookie = policy.zrp_request;
2923 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
2926 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
2929 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
2931 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
2932 zcmd_free_nvlists(&zc);
2937 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
2938 errno != EPERM && errno != EACCES)) {
2939 if (policy.zrp_request &
2940 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2941 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
2942 zpool_rewind_exclaim(hdl, zc.zc_name,
2943 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
2947 zcmd_free_nvlists(&zc);
2951 zcmd_free_nvlists(&zc);
2952 return (zpool_standard_error(hdl, errno, msg));
2956 * Similar to zpool_clear(), but takes a GUID (used by fmd).
2959 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
2961 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2963 libzfs_handle_t *hdl = zhp->zpool_hdl;
2965 (void) snprintf(msg, sizeof (msg),
2966 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
2967 (u_longlong_t)guid);
2969 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2971 zc.zc_cookie = ZPOOL_NO_REWIND;
2973 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
2976 return (zpool_standard_error(hdl, errno, msg));
2980 * Convert from a devid string to a path.
2983 devid_to_path(char *devid_str)
2988 devid_nmlist_t *list = NULL;
2991 if (devid_str_decode(devid_str, &devid, &minor) != 0)
2994 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
2996 devid_str_free(minor);
3002 if ((path = strdup(list[0].devname)) == NULL)
3005 devid_free_nmlist(list);
3011 * Convert from a path to a devid string.
3014 path_to_devid(const char *path)
3020 if ((fd = open(path, O_RDONLY)) < 0)
3025 if (devid_get(fd, &devid) == 0) {
3026 if (devid_get_minor_name(fd, &minor) == 0)
3027 ret = devid_str_encode(devid, minor);
3029 devid_str_free(minor);
3038 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3039 * ignore any failure here, since a common case is for an unprivileged user to
3040 * type 'zpool status', and we'll display the correct information anyway.
3043 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3045 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3047 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3048 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3049 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3052 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3056 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3057 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3058 * We also check if this is a whole disk, in which case we strip off the
3059 * trailing 's0' slice name.
3061 * This routine is also responsible for identifying when disks have been
3062 * reconfigured in a new location. The kernel will have opened the device by
3063 * devid, but the path will still refer to the old location. To catch this, we
3064 * first do a path -> devid translation (which is fast for the common case). If
3065 * the devid matches, we're done. If not, we do a reverse devid -> path
3066 * translation and issue the appropriate ioctl() to update the path of the vdev.
3067 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3071 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3080 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
3082 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3084 (void) snprintf(buf, sizeof (buf), "%llu",
3085 (u_longlong_t)value);
3087 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3090 * If the device is dead (faulted, offline, etc) then don't
3091 * bother opening it. Otherwise we may be forcing the user to
3092 * open a misbehaving device, which can have undesirable
3095 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3096 (uint64_t **)&vs, &vsc) != 0 ||
3097 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3099 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3101 * Determine if the current path is correct.
3103 char *newdevid = path_to_devid(path);
3105 if (newdevid == NULL ||
3106 strcmp(devid, newdevid) != 0) {
3109 if ((newpath = devid_to_path(devid)) != NULL) {
3111 * Update the path appropriately.
3113 set_path(zhp, nv, newpath);
3114 if (nvlist_add_string(nv,
3115 ZPOOL_CONFIG_PATH, newpath) == 0)
3116 verify(nvlist_lookup_string(nv,
3124 devid_str_free(newdevid);
3127 if (strncmp(path, "/dev/dsk/", 9) == 0)
3130 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3131 &value) == 0 && value) {
3132 int pathlen = strlen(path);
3133 char *tmp = zfs_strdup(hdl, path);
3136 * If it starts with c#, and ends with "s0", chop
3137 * the "s0" off, or if it ends with "s0/old", remove
3138 * the "s0" from the middle.
3140 if (CTD_CHECK(tmp)) {
3141 if (strcmp(&tmp[pathlen - 2], "s0") == 0) {
3142 tmp[pathlen - 2] = '\0';
3143 } else if (pathlen > 6 &&
3144 strcmp(&tmp[pathlen - 6], "s0/old") == 0) {
3145 (void) strcpy(&tmp[pathlen - 6],
3152 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3155 * If it's a raidz device, we need to stick in the parity level.
3157 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3158 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3160 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3161 (u_longlong_t)value);
3166 * We identify each top-level vdev by using a <type-id>
3167 * naming convention.
3172 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3174 (void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3180 return (zfs_strdup(hdl, path));
3184 zbookmark_compare(const void *a, const void *b)
3186 return (memcmp(a, b, sizeof (zbookmark_t)));
3190 * Retrieve the persistent error log, uniquify the members, and return to the
3194 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3196 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3198 zbookmark_t *zb = NULL;
3202 * Retrieve the raw error list from the kernel. If the number of errors
3203 * has increased, allocate more space and continue until we get the
3206 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3210 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3211 count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
3213 zc.zc_nvlist_dst_size = count;
3214 (void) strcpy(zc.zc_name, zhp->zpool_name);
3216 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3218 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3219 if (errno == ENOMEM) {
3220 count = zc.zc_nvlist_dst_size;
3221 if ((zc.zc_nvlist_dst = (uintptr_t)
3222 zfs_alloc(zhp->zpool_hdl, count *
3223 sizeof (zbookmark_t))) == (uintptr_t)NULL)
3234 * Sort the resulting bookmarks. This is a little confusing due to the
3235 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3236 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3237 * _not_ copied as part of the process. So we point the start of our
3238 * array appropriate and decrement the total number of elements.
3240 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
3241 zc.zc_nvlist_dst_size;
3242 count -= zc.zc_nvlist_dst_size;
3244 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
3246 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3249 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3251 for (i = 0; i < count; i++) {
3254 /* ignoring zb_blkid and zb_level for now */
3255 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3256 zb[i-1].zb_object == zb[i].zb_object)
3259 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3261 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3262 zb[i].zb_objset) != 0) {
3266 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3267 zb[i].zb_object) != 0) {
3271 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3278 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3282 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3283 return (no_memory(zhp->zpool_hdl));
3287 * Upgrade a ZFS pool to the latest on-disk version.
3290 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3292 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3293 libzfs_handle_t *hdl = zhp->zpool_hdl;
3295 (void) strcpy(zc.zc_name, zhp->zpool_name);
3296 zc.zc_cookie = new_version;
3298 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3299 return (zpool_standard_error_fmt(hdl, errno,
3300 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3306 zpool_set_history_str(const char *subcommand, int argc, char **argv,
3311 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
3312 for (i = 1; i < argc; i++) {
3313 if (strlen(history_str) + 1 + strlen(argv[i]) >
3316 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
3317 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
3322 * Stage command history for logging.
3325 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
3327 if (history_str == NULL)
3330 if (strlen(history_str) > HIS_MAX_RECORD_LEN)
3333 if (hdl->libzfs_log_str != NULL)
3334 free(hdl->libzfs_log_str);
3336 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
3337 return (no_memory(hdl));
3343 * Perform ioctl to get some command history of a pool.
3345 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3346 * logical offset of the history buffer to start reading from.
3348 * Upon return, 'off' is the next logical offset to read from and
3349 * 'len' is the actual amount of bytes read into 'buf'.
3352 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3354 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3355 libzfs_handle_t *hdl = zhp->zpool_hdl;
3357 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3359 zc.zc_history = (uint64_t)(uintptr_t)buf;
3360 zc.zc_history_len = *len;
3361 zc.zc_history_offset = *off;
3363 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3366 return (zfs_error_fmt(hdl, EZFS_PERM,
3367 dgettext(TEXT_DOMAIN,
3368 "cannot show history for pool '%s'"),
3371 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3372 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3373 "'%s'"), zhp->zpool_name));
3375 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3376 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3377 "'%s', pool must be upgraded"), zhp->zpool_name));
3379 return (zpool_standard_error_fmt(hdl, errno,
3380 dgettext(TEXT_DOMAIN,
3381 "cannot get history for '%s'"), zhp->zpool_name));
3385 *len = zc.zc_history_len;
3386 *off = zc.zc_history_offset;
3392 * Process the buffer of nvlists, unpacking and storing each nvlist record
3393 * into 'records'. 'leftover' is set to the number of bytes that weren't
3394 * processed as there wasn't a complete record.
3397 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3398 nvlist_t ***records, uint_t *numrecords)
3404 while (bytes_read > sizeof (reclen)) {
3406 /* get length of packed record (stored as little endian) */
3407 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3408 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3410 if (bytes_read < sizeof (reclen) + reclen)
3414 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3416 bytes_read -= sizeof (reclen) + reclen;
3417 buf += sizeof (reclen) + reclen;
3419 /* add record to nvlist array */
3421 if (ISP2(*numrecords + 1)) {
3422 *records = realloc(*records,
3423 *numrecords * 2 * sizeof (nvlist_t *));
3425 (*records)[*numrecords - 1] = nv;
3428 *leftover = bytes_read;
3432 #define HIS_BUF_LEN (128*1024)
3435 * Retrieve the command history of a pool.
3438 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3440 char buf[HIS_BUF_LEN];
3442 nvlist_t **records = NULL;
3443 uint_t numrecords = 0;
3447 uint64_t bytes_read = sizeof (buf);
3450 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3453 /* if nothing else was read in, we're at EOF, just return */
3457 if ((err = zpool_history_unpack(buf, bytes_read,
3458 &leftover, &records, &numrecords)) != 0)
3466 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3467 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3468 records, numrecords) == 0);
3470 for (i = 0; i < numrecords; i++)
3471 nvlist_free(records[i]);
3478 * Retrieve the next event. If there is a new event available 'nvp' will
3479 * contain a newly allocated nvlist and 'dropped' will be set to the number
3480 * of missed events since the last call to this function. When 'nvp' is
3481 * set to NULL it indicates no new events are available. In either case
3482 * the function returns 0 and it is up to the caller to free 'nvp'. In
3483 * the case of a fatal error the function will return a non-zero value.
3484 * When the function is called in blocking mode it will not return until
3485 * a new event is available.
3488 zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
3489 int *dropped, int block, int cleanup_fd)
3491 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3496 zc.zc_cleanup_fd = cleanup_fd;
3499 zc.zc_guid = ZEVENT_NONBLOCK;
3501 if (zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE) != 0)
3505 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
3508 error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
3509 dgettext(TEXT_DOMAIN, "zfs shutdown"));
3512 /* Blocking error case should not occur */
3514 error = zpool_standard_error_fmt(hdl, errno,
3515 dgettext(TEXT_DOMAIN, "cannot get event"));
3519 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3520 error = zfs_error_fmt(hdl, EZFS_NOMEM,
3521 dgettext(TEXT_DOMAIN, "cannot get event"));
3527 error = zpool_standard_error_fmt(hdl, errno,
3528 dgettext(TEXT_DOMAIN, "cannot get event"));
3533 error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
3537 *dropped = (int)zc.zc_cookie;
3539 zcmd_free_nvlists(&zc);
3548 zpool_events_clear(libzfs_handle_t *hdl, int *count)
3550 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3553 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3554 "cannot clear events"));
3556 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
3557 return (zpool_standard_error_fmt(hdl, errno, msg));
3560 *count = (int)zc.zc_cookie; /* # of events cleared */
3566 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3567 char *pathname, size_t len)
3569 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3570 boolean_t mounted = B_FALSE;
3571 char *mntpnt = NULL;
3572 char dsname[MAXNAMELEN];
3575 /* special case for the MOS */
3576 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", (longlong_t)obj);
3580 /* get the dataset's name */
3581 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3583 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3584 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3585 /* just write out a path of two object numbers */
3586 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3587 (longlong_t)dsobj, (longlong_t)obj);
3590 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3592 /* find out if the dataset is mounted */
3593 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3595 /* get the corrupted object's path */
3596 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3598 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3601 (void) snprintf(pathname, len, "%s%s", mntpnt,
3604 (void) snprintf(pathname, len, "%s:%s",
3605 dsname, zc.zc_value);
3608 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, (longlong_t)obj);
3614 * Read the EFI label from the config, if a label does not exist then
3615 * pass back the error to the caller. If the caller has passed a non-NULL
3616 * diskaddr argument then we set it to the starting address of the EFI
3620 read_efi_label(nvlist_t *config, diskaddr_t *sb)
3624 char diskname[MAXPATHLEN];
3627 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3630 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
3631 strrchr(path, '/'));
3632 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
3633 struct dk_gpt *vtoc;
3635 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3637 *sb = vtoc->efi_parts[0].p_start;
3646 * determine where a partition starts on a disk in the current
3650 find_start_block(nvlist_t *config)
3654 diskaddr_t sb = MAXOFFSET_T;
3657 if (nvlist_lookup_nvlist_array(config,
3658 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3659 if (nvlist_lookup_uint64(config,
3660 ZPOOL_CONFIG_WHOLE_DISK,
3661 &wholedisk) != 0 || !wholedisk) {
3662 return (MAXOFFSET_T);
3664 if (read_efi_label(config, &sb) < 0)
3669 for (c = 0; c < children; c++) {
3670 sb = find_start_block(child[c]);
3671 if (sb != MAXOFFSET_T) {
3675 return (MAXOFFSET_T);
3679 * Label an individual disk. The name provided is the short name,
3680 * stripped of any leading /dev path.
3683 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
3685 char path[MAXPATHLEN];
3686 struct dk_gpt *vtoc;
3688 size_t resv = EFI_MIN_RESV_SIZE;
3689 uint64_t slice_size;
3690 diskaddr_t start_block;
3693 /* prepare an error message just in case */
3694 (void) snprintf(errbuf, sizeof (errbuf),
3695 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3700 if (pool_is_bootable(zhp)) {
3701 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3702 "EFI labeled devices are not supported on root "
3704 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3707 verify(nvlist_lookup_nvlist(zhp->zpool_config,
3708 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3710 if (zhp->zpool_start_block == 0)
3711 start_block = find_start_block(nvroot);
3713 start_block = zhp->zpool_start_block;
3714 zhp->zpool_start_block = start_block;
3717 start_block = NEW_START_BLOCK;
3720 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
3723 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
3725 * This shouldn't happen. We've long since verified that this
3726 * is a valid device.
3729 dgettext(TEXT_DOMAIN, "unable to open device"));
3730 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
3733 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
3735 * The only way this can fail is if we run out of memory, or we
3736 * were unable to read the disk's capacity
3738 if (errno == ENOMEM)
3739 (void) no_memory(hdl);
3742 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3743 "unable to read disk capacity"), name);
3745 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
3748 slice_size = vtoc->efi_last_u_lba + 1;
3749 slice_size -= EFI_MIN_RESV_SIZE;
3750 if (start_block == MAXOFFSET_T)
3751 start_block = NEW_START_BLOCK;
3752 slice_size -= start_block;
3754 vtoc->efi_parts[0].p_start = start_block;
3755 vtoc->efi_parts[0].p_size = slice_size;
3758 * Why we use V_USR: V_BACKUP confuses users, and is considered
3759 * disposable by some EFI utilities (since EFI doesn't have a backup
3760 * slice). V_UNASSIGNED is supposed to be used only for zero size
3761 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
3762 * etc. were all pretty specific. V_USR is as close to reality as we
3763 * can get, in the absence of V_OTHER.
3765 vtoc->efi_parts[0].p_tag = V_USR;
3766 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
3768 vtoc->efi_parts[8].p_start = slice_size + start_block;
3769 vtoc->efi_parts[8].p_size = resv;
3770 vtoc->efi_parts[8].p_tag = V_RESERVED;
3772 if (efi_write(fd, vtoc) != 0) {
3774 * Some block drivers (like pcata) may not support EFI
3775 * GPT labels. Print out a helpful error message dir-
3776 * ecting the user to manually label the disk and give
3782 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3783 "try using fdisk(1M) and then provide a specific slice"));
3784 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3793 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
3799 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
3800 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
3801 strcmp(type, VDEV_TYPE_FILE) == 0 ||
3802 strcmp(type, VDEV_TYPE_LOG) == 0 ||
3803 strcmp(type, VDEV_TYPE_HOLE) == 0 ||
3804 strcmp(type, VDEV_TYPE_MISSING) == 0) {
3805 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3806 "vdev type '%s' is not supported"), type);
3807 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
3810 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
3811 &child, &children) == 0) {
3812 for (c = 0; c < children; c++) {
3813 if (!supported_dump_vdev_type(hdl, child[c], errbuf))
3821 * check if this zvol is allowable for use as a dump device; zero if
3822 * it is, > 0 if it isn't, < 0 if it isn't a zvol
3825 zvol_check_dump_config(char *arg)
3827 zpool_handle_t *zhp = NULL;
3828 nvlist_t *config, *nvroot;
3832 libzfs_handle_t *hdl;
3834 char poolname[ZPOOL_MAXNAMELEN];
3835 int pathlen = strlen(ZVOL_FULL_DEV_DIR);
3838 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
3842 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3843 "dump is not supported on device '%s'"), arg);
3845 if ((hdl = libzfs_init()) == NULL)
3847 libzfs_print_on_error(hdl, B_TRUE);
3849 volname = arg + pathlen;
3851 /* check the configuration of the pool */
3852 if ((p = strchr(volname, '/')) == NULL) {
3853 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3854 "malformed dataset name"));
3855 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3857 } else if (p - volname >= ZFS_MAXNAMELEN) {
3858 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3859 "dataset name is too long"));
3860 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
3863 (void) strncpy(poolname, volname, p - volname);
3864 poolname[p - volname] = '\0';
3867 if ((zhp = zpool_open(hdl, poolname)) == NULL) {
3868 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3869 "could not open pool '%s'"), poolname);
3870 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
3873 config = zpool_get_config(zhp, NULL);
3874 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3876 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3877 "could not obtain vdev configuration for '%s'"), poolname);
3878 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
3882 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3883 &top, &toplevels) == 0);
3884 if (toplevels != 1) {
3885 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3886 "'%s' has multiple top level vdevs"), poolname);
3887 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
3891 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {