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.
26 #include <sys/types.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)
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)
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 * Map POOL STATE to printed strings.
208 zpool_pool_state_to_name(pool_state_t state)
211 case POOL_STATE_ACTIVE:
212 return (gettext("ACTIVE"));
213 case POOL_STATE_EXPORTED:
214 return (gettext("EXPORTED"));
215 case POOL_STATE_DESTROYED:
216 return (gettext("DESTROYED"));
217 case POOL_STATE_SPARE:
218 return (gettext("SPARE"));
219 case POOL_STATE_L2CACHE:
220 return (gettext("L2CACHE"));
221 case POOL_STATE_UNINITIALIZED:
222 return (gettext("UNINITIALIZED"));
223 case POOL_STATE_UNAVAIL:
224 return (gettext("UNAVAIL"));
225 case POOL_STATE_POTENTIALLY_ACTIVE:
226 return (gettext("POTENTIALLY_ACTIVE"));
229 return (gettext("UNKNOWN"));
233 * Get a zpool property value for 'prop' and return the value in
234 * a pre-allocated buffer.
237 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
238 zprop_source_t *srctype)
242 zprop_source_t src = ZPROP_SRC_NONE;
247 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
249 case ZPOOL_PROP_NAME:
250 (void) strlcpy(buf, zpool_get_name(zhp), len);
253 case ZPOOL_PROP_HEALTH:
254 (void) strlcpy(buf, "FAULTED", len);
257 case ZPOOL_PROP_GUID:
258 intval = zpool_get_prop_int(zhp, prop, &src);
259 (void) snprintf(buf, len, "%llu", intval);
262 case ZPOOL_PROP_ALTROOT:
263 case ZPOOL_PROP_CACHEFILE:
264 if (zhp->zpool_props != NULL ||
265 zpool_get_all_props(zhp) == 0) {
267 zpool_get_prop_string(zhp, prop, &src),
275 (void) strlcpy(buf, "-", len);
284 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
285 prop != ZPOOL_PROP_NAME)
288 switch (zpool_prop_get_type(prop)) {
289 case PROP_TYPE_STRING:
290 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
294 case PROP_TYPE_NUMBER:
295 intval = zpool_get_prop_int(zhp, prop, &src);
298 case ZPOOL_PROP_SIZE:
299 case ZPOOL_PROP_ALLOCATED:
300 case ZPOOL_PROP_FREE:
301 (void) zfs_nicenum(intval, buf, len);
304 case ZPOOL_PROP_CAPACITY:
305 (void) snprintf(buf, len, "%llu%%",
306 (u_longlong_t)intval);
309 case ZPOOL_PROP_DEDUPRATIO:
310 (void) snprintf(buf, len, "%llu.%02llux",
311 (u_longlong_t)(intval / 100),
312 (u_longlong_t)(intval % 100));
315 case ZPOOL_PROP_HEALTH:
316 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
317 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
318 verify(nvlist_lookup_uint64_array(nvroot,
319 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
322 (void) strlcpy(buf, zpool_state_to_name(intval,
326 (void) snprintf(buf, len, "%llu", intval);
330 case PROP_TYPE_INDEX:
331 intval = zpool_get_prop_int(zhp, prop, &src);
332 if (zpool_prop_index_to_string(prop, intval, &strval)
335 (void) strlcpy(buf, strval, len);
349 * Check if the bootfs name has the same pool name as it is set to.
350 * Assuming bootfs is a valid dataset name.
353 bootfs_name_valid(const char *pool, char *bootfs)
355 int len = strlen(pool);
357 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
360 if (strncmp(pool, bootfs, len) == 0 &&
361 (bootfs[len] == '/' || bootfs[len] == '\0'))
368 * Inspect the configuration to determine if any of the devices contain
372 pool_uses_efi(nvlist_t *config)
378 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
379 &child, &children) != 0)
380 return (read_efi_label(config, NULL) >= 0);
382 for (c = 0; c < children; c++) {
383 if (pool_uses_efi(child[c]))
391 pool_is_bootable(zpool_handle_t *zhp)
393 char bootfs[ZPOOL_MAXNAMELEN];
395 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
396 sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
397 sizeof (bootfs)) != 0);
402 * Given an nvlist of zpool properties to be set, validate that they are
403 * correct, and parse any numeric properties (index, boolean, etc) if they are
404 * specified as strings.
407 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
408 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
416 struct stat64 statbuf;
420 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
421 (void) no_memory(hdl);
426 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
427 const char *propname = nvpair_name(elem);
430 * Make sure this property is valid and applies to this type.
432 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
433 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
434 "invalid property '%s'"), propname);
435 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
439 if (zpool_prop_readonly(prop)) {
440 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
441 "is readonly"), propname);
442 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
446 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
447 &strval, &intval, errbuf) != 0)
451 * Perform additional checking for specific properties.
454 case ZPOOL_PROP_VERSION:
455 if (intval < version || intval > SPA_VERSION) {
456 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
457 "property '%s' number %d is invalid."),
459 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
464 case ZPOOL_PROP_BOOTFS:
465 if (flags.create || flags.import) {
466 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
467 "property '%s' cannot be set at creation "
468 "or import time"), propname);
469 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
473 if (version < SPA_VERSION_BOOTFS) {
474 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
475 "pool must be upgraded to support "
476 "'%s' property"), propname);
477 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
482 * bootfs property value has to be a dataset name and
483 * the dataset has to be in the same pool as it sets to.
485 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
488 "is an invalid name"), strval);
489 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
493 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
494 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
495 "could not open pool '%s'"), poolname);
496 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
499 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
500 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
504 * bootfs property cannot be set on a disk which has
507 if (pool_uses_efi(nvroot)) {
508 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
509 "property '%s' not supported on "
510 "EFI labeled devices"), propname);
511 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
519 case ZPOOL_PROP_ALTROOT:
520 if (!flags.create && !flags.import) {
521 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
522 "property '%s' can only be set during pool "
523 "creation or import"), propname);
524 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
528 if (strval[0] != '/') {
529 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
530 "bad alternate root '%s'"), strval);
531 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
536 case ZPOOL_PROP_CACHEFILE:
537 if (strval[0] == '\0')
540 if (strcmp(strval, "none") == 0)
543 if (strval[0] != '/') {
544 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
545 "property '%s' must be empty, an "
546 "absolute path, or 'none'"), propname);
547 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
551 slash = strrchr(strval, '/');
553 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
554 strcmp(slash, "/..") == 0) {
555 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
556 "'%s' is not a valid file"), strval);
557 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
563 if (strval[0] != '\0' &&
564 (stat64(strval, &statbuf) != 0 ||
565 !S_ISDIR(statbuf.st_mode))) {
566 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
567 "'%s' is not a valid directory"),
569 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
576 case ZPOOL_PROP_READONLY:
578 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
579 "property '%s' can only be set at "
580 "import time"), propname);
581 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
590 nvlist_free(retprops);
595 * Set zpool property : propname=propval.
598 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
600 zfs_cmd_t zc = { 0 };
603 nvlist_t *nvl = NULL;
606 prop_flags_t flags = { 0 };
608 (void) snprintf(errbuf, sizeof (errbuf),
609 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
612 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
613 return (no_memory(zhp->zpool_hdl));
615 if (nvlist_add_string(nvl, propname, propval) != 0) {
617 return (no_memory(zhp->zpool_hdl));
620 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
621 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
622 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
631 * Execute the corresponding ioctl() to set this property.
633 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
635 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
640 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
642 zcmd_free_nvlists(&zc);
646 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
648 (void) zpool_props_refresh(zhp);
654 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
656 libzfs_handle_t *hdl = zhp->zpool_hdl;
658 char buf[ZFS_MAXPROPLEN];
660 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
663 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
668 if (entry->pl_prop != ZPROP_INVAL &&
669 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
671 if (strlen(buf) > entry->pl_width)
672 entry->pl_width = strlen(buf);
681 * Don't start the slice at the default block of 34; many storage
682 * devices will use a stripe width of 128k, so start there instead.
684 #define NEW_START_BLOCK 256
687 * Validate the given pool name, optionally putting an extended error message in
691 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
697 ret = pool_namecheck(pool, &why, &what);
700 * The rules for reserved pool names were extended at a later point.
701 * But we need to support users with existing pools that may now be
702 * invalid. So we only check for this expanded set of names during a
703 * create (or import), and only in userland.
705 if (ret == 0 && !isopen &&
706 (strncmp(pool, "mirror", 6) == 0 ||
707 strncmp(pool, "raidz", 5) == 0 ||
708 strncmp(pool, "spare", 5) == 0 ||
709 strcmp(pool, "log") == 0)) {
712 dgettext(TEXT_DOMAIN, "name is reserved"));
720 case NAME_ERR_TOOLONG:
722 dgettext(TEXT_DOMAIN, "name is too long"));
725 case NAME_ERR_INVALCHAR:
727 dgettext(TEXT_DOMAIN, "invalid character "
728 "'%c' in pool name"), what);
731 case NAME_ERR_NOLETTER:
732 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
733 "name must begin with a letter"));
736 case NAME_ERR_RESERVED:
737 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
738 "name is reserved"));
741 case NAME_ERR_DISKLIKE:
742 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
743 "pool name is reserved"));
746 case NAME_ERR_LEADING_SLASH:
747 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
748 "leading slash in name"));
751 case NAME_ERR_EMPTY_COMPONENT:
752 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
753 "empty component in name"));
756 case NAME_ERR_TRAILING_SLASH:
757 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
758 "trailing slash in name"));
761 case NAME_ERR_MULTIPLE_AT:
762 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
763 "multiple '@' delimiters in name"));
775 * Open a handle to the given pool, even if the pool is currently in the FAULTED
779 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
785 * Make sure the pool name is valid.
787 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
788 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
789 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
794 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
797 zhp->zpool_hdl = hdl;
798 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
800 if (zpool_refresh_stats(zhp, &missing) != 0) {
806 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
807 (void) zfs_error_fmt(hdl, EZFS_NOENT,
808 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
817 * Like the above, but silent on error. Used when iterating over pools (because
818 * the configuration cache may be out of date).
821 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
826 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
829 zhp->zpool_hdl = hdl;
830 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
832 if (zpool_refresh_stats(zhp, &missing) != 0) {
848 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
852 zpool_open(libzfs_handle_t *hdl, const char *pool)
856 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
859 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
860 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
861 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
870 * Close the handle. Simply frees the memory associated with the handle.
873 zpool_close(zpool_handle_t *zhp)
875 if (zhp->zpool_config)
876 nvlist_free(zhp->zpool_config);
877 if (zhp->zpool_old_config)
878 nvlist_free(zhp->zpool_old_config);
879 if (zhp->zpool_props)
880 nvlist_free(zhp->zpool_props);
885 * Return the name of the pool.
888 zpool_get_name(zpool_handle_t *zhp)
890 return (zhp->zpool_name);
895 * Return the state of the pool (ACTIVE or UNAVAILABLE)
898 zpool_get_state(zpool_handle_t *zhp)
900 return (zhp->zpool_state);
904 * Create the named pool, using the provided vdev list. It is assumed
905 * that the consumer has already validated the contents of the nvlist, so we
906 * don't have to worry about error semantics.
909 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
910 nvlist_t *props, nvlist_t *fsprops)
912 zfs_cmd_t zc = { 0 };
913 nvlist_t *zc_fsprops = NULL;
914 nvlist_t *zc_props = NULL;
919 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
920 "cannot create '%s'"), pool);
922 if (!zpool_name_valid(hdl, B_FALSE, pool))
923 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
925 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
929 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
931 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
932 SPA_VERSION_1, flags, msg)) == NULL) {
941 zoned = ((nvlist_lookup_string(fsprops,
942 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
943 strcmp(zonestr, "on") == 0);
945 if ((zc_fsprops = zfs_valid_proplist(hdl,
946 ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
950 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
953 if (nvlist_add_nvlist(zc_props,
954 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
959 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
962 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
964 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
966 zcmd_free_nvlists(&zc);
967 nvlist_free(zc_props);
968 nvlist_free(zc_fsprops);
973 * This can happen if the user has specified the same
974 * device multiple times. We can't reliably detect this
975 * until we try to add it and see we already have a
978 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
979 "one or more vdevs refer to the same device"));
980 return (zfs_error(hdl, EZFS_BADDEV, msg));
984 * This occurs when one of the devices is below
985 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
986 * device was the problem device since there's no
987 * reliable way to determine device size from userland.
992 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
994 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
995 "one or more devices is less than the "
996 "minimum size (%s)"), buf);
998 return (zfs_error(hdl, EZFS_BADDEV, msg));
1001 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1002 "one or more devices is out of space"));
1003 return (zfs_error(hdl, EZFS_BADDEV, msg));
1006 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1007 "cache device must be a disk or disk slice"));
1008 return (zfs_error(hdl, EZFS_BADDEV, msg));
1011 return (zpool_standard_error(hdl, errno, msg));
1016 * If this is an alternate root pool, then we automatically set the
1017 * mountpoint of the root dataset to be '/'.
1019 if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
1023 verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
1024 verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1031 zcmd_free_nvlists(&zc);
1032 nvlist_free(zc_props);
1033 nvlist_free(zc_fsprops);
1038 * Destroy the given pool. It is up to the caller to ensure that there are no
1039 * datasets left in the pool.
1042 zpool_destroy(zpool_handle_t *zhp)
1044 zfs_cmd_t zc = { 0 };
1045 zfs_handle_t *zfp = NULL;
1046 libzfs_handle_t *hdl = zhp->zpool_hdl;
1049 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1050 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1053 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1055 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1056 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1057 "cannot destroy '%s'"), zhp->zpool_name);
1059 if (errno == EROFS) {
1060 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1061 "one or more devices is read only"));
1062 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1064 (void) zpool_standard_error(hdl, errno, msg);
1073 remove_mountpoint(zfp);
1081 * Add the given vdevs to the pool. The caller must have already performed the
1082 * necessary verification to ensure that the vdev specification is well-formed.
1085 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1087 zfs_cmd_t zc = { 0 };
1089 libzfs_handle_t *hdl = zhp->zpool_hdl;
1091 nvlist_t **spares, **l2cache;
1092 uint_t nspares, nl2cache;
1094 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1095 "cannot add to '%s'"), zhp->zpool_name);
1097 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1098 SPA_VERSION_SPARES &&
1099 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1100 &spares, &nspares) == 0) {
1101 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1102 "upgraded to add hot spares"));
1103 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1106 if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1107 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1110 for (s = 0; s < nspares; s++) {
1113 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1114 &path) == 0 && pool_uses_efi(spares[s])) {
1115 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1116 "device '%s' contains an EFI label and "
1117 "cannot be used on root pools."),
1118 zpool_vdev_name(hdl, NULL, spares[s],
1120 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1125 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1126 SPA_VERSION_L2CACHE &&
1127 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1128 &l2cache, &nl2cache) == 0) {
1129 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1130 "upgraded to add cache devices"));
1131 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1134 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1136 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1138 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1142 * This can happen if the user has specified the same
1143 * device multiple times. We can't reliably detect this
1144 * until we try to add it and see we already have a
1147 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1148 "one or more vdevs refer to the same device"));
1149 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1154 * This occurrs when one of the devices is below
1155 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1156 * device was the problem device since there's no
1157 * reliable way to determine device size from userland.
1162 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1164 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1165 "device is less than the minimum "
1168 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1172 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1173 "pool must be upgraded to add these vdevs"));
1174 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1178 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1179 "root pool can not have multiple vdevs"
1180 " or separate logs"));
1181 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1185 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1186 "cache device must be a disk or disk slice"));
1187 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1191 (void) zpool_standard_error(hdl, errno, msg);
1199 zcmd_free_nvlists(&zc);
1205 * Exports the pool from the system. The caller must ensure that there are no
1206 * mounted datasets in the pool.
1209 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1211 zfs_cmd_t zc = { 0 };
1214 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1215 "cannot export '%s'"), zhp->zpool_name);
1217 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1218 zc.zc_cookie = force;
1219 zc.zc_guid = hardforce;
1221 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1224 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1225 "use '-f' to override the following errors:\n"
1226 "'%s' has an active shared spare which could be"
1227 " used by other pools once '%s' is exported."),
1228 zhp->zpool_name, zhp->zpool_name);
1229 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1232 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1241 zpool_export(zpool_handle_t *zhp, boolean_t force)
1243 return (zpool_export_common(zhp, force, B_FALSE));
1247 zpool_export_force(zpool_handle_t *zhp)
1249 return (zpool_export_common(zhp, B_TRUE, B_TRUE));
1253 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1256 nvlist_t *nv = NULL;
1262 if (!hdl->libzfs_printerr || config == NULL)
1265 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0)
1268 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1270 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1272 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1273 strftime(timestr, 128, 0, &t) != 0) {
1275 (void) printf(dgettext(TEXT_DOMAIN,
1276 "Would be able to return %s "
1277 "to its state as of %s.\n"),
1280 (void) printf(dgettext(TEXT_DOMAIN,
1281 "Pool %s returned to its state as of %s.\n"),
1285 (void) printf(dgettext(TEXT_DOMAIN,
1286 "%s approximately %lld "),
1287 dryrun ? "Would discard" : "Discarded",
1289 (void) printf(dgettext(TEXT_DOMAIN,
1290 "minutes of transactions.\n"));
1291 } else if (loss > 0) {
1292 (void) printf(dgettext(TEXT_DOMAIN,
1293 "%s approximately %lld "),
1294 dryrun ? "Would discard" : "Discarded", loss);
1295 (void) printf(dgettext(TEXT_DOMAIN,
1296 "seconds of transactions.\n"));
1302 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1305 nvlist_t *nv = NULL;
1307 uint64_t edata = UINT64_MAX;
1312 if (!hdl->libzfs_printerr)
1316 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1318 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1320 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1321 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1322 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1325 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1326 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1329 (void) printf(dgettext(TEXT_DOMAIN,
1330 "Recovery is possible, but will result in some data loss.\n"));
1332 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1333 strftime(timestr, 128, 0, &t) != 0) {
1334 (void) printf(dgettext(TEXT_DOMAIN,
1335 "\tReturning the pool to its state as of %s\n"
1336 "\tshould correct the problem. "),
1339 (void) printf(dgettext(TEXT_DOMAIN,
1340 "\tReverting the pool to an earlier state "
1341 "should correct the problem.\n\t"));
1345 (void) printf(dgettext(TEXT_DOMAIN,
1346 "Approximately %lld minutes of data\n"
1347 "\tmust be discarded, irreversibly. "), (loss + 30) / 60);
1348 } else if (loss > 0) {
1349 (void) printf(dgettext(TEXT_DOMAIN,
1350 "Approximately %lld seconds of data\n"
1351 "\tmust be discarded, irreversibly. "), loss);
1353 if (edata != 0 && edata != UINT64_MAX) {
1355 (void) printf(dgettext(TEXT_DOMAIN,
1356 "After rewind, at least\n"
1357 "\tone persistent user-data error will remain. "));
1359 (void) printf(dgettext(TEXT_DOMAIN,
1360 "After rewind, several\n"
1361 "\tpersistent user-data errors will remain. "));
1364 (void) printf(dgettext(TEXT_DOMAIN,
1365 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1366 reason >= 0 ? "clear" : "import", name);
1368 (void) printf(dgettext(TEXT_DOMAIN,
1369 "A scrub of the pool\n"
1370 "\tis strongly recommended after recovery.\n"));
1374 (void) printf(dgettext(TEXT_DOMAIN,
1375 "Destroy and re-create the pool from\n\ta backup source.\n"));
1379 * zpool_import() is a contracted interface. Should be kept the same
1382 * Applications should use zpool_import_props() to import a pool with
1383 * new properties value to be set.
1386 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1389 nvlist_t *props = NULL;
1392 if (altroot != NULL) {
1393 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1394 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1395 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1399 if (nvlist_add_string(props,
1400 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1401 nvlist_add_string(props,
1402 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1404 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1405 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1410 ret = zpool_import_props(hdl, config, newname, props,
1418 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1424 uint64_t is_log = 0;
1426 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1430 (void) printf("\t%*s%s%s\n", indent, "", name,
1431 is_log ? " [log]" : "");
1433 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1434 &child, &children) != 0)
1437 for (c = 0; c < children; c++) {
1438 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
1439 print_vdev_tree(hdl, vname, child[c], indent + 2);
1445 * Import the given pool using the known configuration and a list of
1446 * properties to be set. The configuration should have come from
1447 * zpool_find_import(). The 'newname' parameters control whether the pool
1448 * is imported with a different name.
1451 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1452 nvlist_t *props, int flags)
1454 zfs_cmd_t zc = { 0 };
1455 zpool_rewind_policy_t policy;
1456 nvlist_t *nv = NULL;
1457 nvlist_t *nvinfo = NULL;
1458 nvlist_t *missing = NULL;
1465 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1468 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1469 "cannot import pool '%s'"), origname);
1471 if (newname != NULL) {
1472 if (!zpool_name_valid(hdl, B_FALSE, newname))
1473 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1474 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1476 thename = (char *)newname;
1483 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1485 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1488 if ((props = zpool_valid_proplist(hdl, origname,
1489 props, version, flags, errbuf)) == NULL) {
1491 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1497 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1499 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1502 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1506 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1511 zc.zc_cookie = flags;
1512 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1514 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1515 zcmd_free_nvlists(&zc);
1522 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1523 zpool_get_rewind_policy(config, &policy);
1529 * Dry-run failed, but we print out what success
1530 * looks like if we found a best txg
1532 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1533 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1539 if (newname == NULL)
1540 (void) snprintf(desc, sizeof (desc),
1541 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1544 (void) snprintf(desc, sizeof (desc),
1545 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1551 * Unsupported version.
1553 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1557 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1561 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1562 "one or more devices is read only"));
1563 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1567 if (nv && nvlist_lookup_nvlist(nv,
1568 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1569 nvlist_lookup_nvlist(nvinfo,
1570 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1571 (void) printf(dgettext(TEXT_DOMAIN,
1572 "The devices below are missing, use "
1573 "'-m' to import the pool anyway:\n"));
1574 print_vdev_tree(hdl, NULL, missing, 2);
1575 (void) printf("\n");
1577 (void) zpool_standard_error(hdl, error, desc);
1581 (void) zpool_standard_error(hdl, error, desc);
1585 (void) zpool_standard_error(hdl, error, desc);
1586 zpool_explain_recover(hdl,
1587 newname ? origname : thename, -error, nv);
1594 zpool_handle_t *zhp;
1597 * This should never fail, but play it safe anyway.
1599 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1601 else if (zhp != NULL)
1603 if (policy.zrp_request &
1604 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1605 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1606 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1612 zcmd_free_nvlists(&zc);
1622 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1624 zfs_cmd_t zc = { 0 };
1626 libzfs_handle_t *hdl = zhp->zpool_hdl;
1628 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1629 zc.zc_cookie = func;
1631 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
1632 (errno == ENOENT && func != POOL_SCAN_NONE))
1635 if (func == POOL_SCAN_SCRUB) {
1636 (void) snprintf(msg, sizeof (msg),
1637 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1638 } else if (func == POOL_SCAN_NONE) {
1639 (void) snprintf(msg, sizeof (msg),
1640 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1643 assert(!"unexpected result");
1646 if (errno == EBUSY) {
1648 pool_scan_stat_t *ps = NULL;
1651 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1652 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1653 (void) nvlist_lookup_uint64_array(nvroot,
1654 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1655 if (ps && ps->pss_func == POOL_SCAN_SCRUB)
1656 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1658 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1659 } else if (errno == ENOENT) {
1660 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1662 return (zpool_standard_error(hdl, errno, msg));
1667 * This provides a very minimal check whether a given string is likely a
1668 * c#t#d# style string. Users of this are expected to do their own
1669 * verification of the s# part.
1671 #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1]))
1674 * More elaborate version for ones which may start with "/dev/dsk/"
1678 ctd_check_path(char *str) {
1680 * If it starts with a slash, check the last component.
1682 if (str && str[0] == '/') {
1683 char *tmp = strrchr(str, '/');
1686 * If it ends in "/old", check the second-to-last
1687 * component of the string instead.
1689 if (tmp != str && strcmp(tmp, "/old") == 0) {
1690 for (tmp--; *tmp != '/'; tmp--)
1695 return (CTD_CHECK(str));
1699 * Find a vdev that matches the search criteria specified. We use the
1700 * the nvpair name to determine how we should look for the device.
1701 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1702 * spare; but FALSE if its an INUSE spare.
1705 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1706 boolean_t *l2cache, boolean_t *log)
1713 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1715 /* Nothing to look for */
1716 if (search == NULL || pair == NULL)
1719 /* Obtain the key we will use to search */
1720 srchkey = nvpair_name(pair);
1722 switch (nvpair_type(pair)) {
1723 case DATA_TYPE_UINT64:
1724 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1725 uint64_t srchval, theguid;
1727 verify(nvpair_value_uint64(pair, &srchval) == 0);
1728 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1730 if (theguid == srchval)
1735 case DATA_TYPE_STRING: {
1736 char *srchval, *val;
1738 verify(nvpair_value_string(pair, &srchval) == 0);
1739 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1743 * Search for the requested value. Special cases:
1745 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
1746 * "s0" or "s0/old". The "s0" part is hidden from the user,
1747 * but included in the string, so this matches around it.
1748 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1750 * Otherwise, all other searches are simple string compares.
1752 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
1753 ctd_check_path(val)) {
1754 uint64_t wholedisk = 0;
1756 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1759 int slen = strlen(srchval);
1760 int vlen = strlen(val);
1762 if (slen != vlen - 2)
1766 * make_leaf_vdev() should only set
1767 * wholedisk for ZPOOL_CONFIG_PATHs which
1768 * will include "/dev/dsk/", giving plenty of
1769 * room for the indices used next.
1774 * strings identical except trailing "s0"
1776 if (strcmp(&val[vlen - 2], "s0") == 0 &&
1777 strncmp(srchval, val, slen) == 0)
1781 * strings identical except trailing "s0/old"
1783 if (strcmp(&val[vlen - 6], "s0/old") == 0 &&
1784 strcmp(&srchval[slen - 4], "/old") == 0 &&
1785 strncmp(srchval, val, slen - 4) == 0)
1790 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
1791 char *type, *idx, *end, *p;
1792 uint64_t id, vdev_id;
1795 * Determine our vdev type, keeping in mind
1796 * that the srchval is composed of a type and
1797 * vdev id pair (i.e. mirror-4).
1799 if ((type = strdup(srchval)) == NULL)
1802 if ((p = strrchr(type, '-')) == NULL) {
1810 * If the types don't match then keep looking.
1812 if (strncmp(val, type, strlen(val)) != 0) {
1817 verify(strncmp(type, VDEV_TYPE_RAIDZ,
1818 strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1819 strncmp(type, VDEV_TYPE_MIRROR,
1820 strlen(VDEV_TYPE_MIRROR)) == 0);
1821 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
1825 vdev_id = strtoull(idx, &end, 10);
1832 * Now verify that we have the correct vdev id.
1841 if (strcmp(srchval, val) == 0)
1850 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1851 &child, &children) != 0)
1854 for (c = 0; c < children; c++) {
1855 if ((ret = vdev_to_nvlist_iter(child[c], search,
1856 avail_spare, l2cache, NULL)) != NULL) {
1858 * The 'is_log' value is only set for the toplevel
1859 * vdev, not the leaf vdevs. So we always lookup the
1860 * log device from the root of the vdev tree (where
1861 * 'log' is non-NULL).
1864 nvlist_lookup_uint64(child[c],
1865 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
1873 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1874 &child, &children) == 0) {
1875 for (c = 0; c < children; c++) {
1876 if ((ret = vdev_to_nvlist_iter(child[c], search,
1877 avail_spare, l2cache, NULL)) != NULL) {
1878 *avail_spare = B_TRUE;
1884 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1885 &child, &children) == 0) {
1886 for (c = 0; c < children; c++) {
1887 if ((ret = vdev_to_nvlist_iter(child[c], search,
1888 avail_spare, l2cache, NULL)) != NULL) {
1899 * Given a physical path (minus the "/devices" prefix), find the
1903 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
1904 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
1906 nvlist_t *search, *nvroot, *ret;
1908 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1909 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
1911 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1914 *avail_spare = B_FALSE;
1918 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1919 nvlist_free(search);
1925 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
1928 zpool_vdev_is_interior(const char *name)
1930 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1931 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
1937 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
1938 boolean_t *l2cache, boolean_t *log)
1940 char buf[MAXPATHLEN];
1942 nvlist_t *nvroot, *search, *ret;
1945 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1947 guid = strtoull(path, &end, 10);
1948 if (guid != 0 && *end == '\0') {
1949 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
1950 } else if (zpool_vdev_is_interior(path)) {
1951 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
1952 } else if (path[0] != '/') {
1953 (void) snprintf(buf, sizeof (buf), "%s%s", _PATH_DEV, path);
1954 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
1956 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
1959 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1962 *avail_spare = B_FALSE;
1966 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1967 nvlist_free(search);
1973 vdev_online(nvlist_t *nv)
1977 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
1978 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
1979 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
1986 * Helper function for zpool_get_physpaths().
1989 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
1990 size_t *bytes_written)
1992 size_t bytes_left, pos, rsz;
1996 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
1998 return (EZFS_NODEVICE);
2000 pos = *bytes_written;
2001 bytes_left = physpath_size - pos;
2002 format = (pos == 0) ? "%s" : " %s";
2004 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2005 *bytes_written += rsz;
2007 if (rsz >= bytes_left) {
2008 /* if physpath was not copied properly, clear it */
2009 if (bytes_left != 0) {
2012 return (EZFS_NOSPC);
2018 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2019 size_t *rsz, boolean_t is_spare)
2024 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2025 return (EZFS_INVALCONFIG);
2027 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2029 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2030 * For a spare vdev, we only want to boot from the active
2035 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2038 return (EZFS_INVALCONFIG);
2041 if (vdev_online(nv)) {
2042 if ((ret = vdev_get_one_physpath(nv, physpath,
2043 phypath_size, rsz)) != 0)
2046 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2047 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2048 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2053 if (nvlist_lookup_nvlist_array(nv,
2054 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2055 return (EZFS_INVALCONFIG);
2057 for (i = 0; i < count; i++) {
2058 ret = vdev_get_physpaths(child[i], physpath,
2059 phypath_size, rsz, is_spare);
2060 if (ret == EZFS_NOSPC)
2065 return (EZFS_POOL_INVALARG);
2069 * Get phys_path for a root pool config.
2070 * Return 0 on success; non-zero on failure.
2073 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2076 nvlist_t *vdev_root;
2083 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2085 return (EZFS_INVALCONFIG);
2087 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2088 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2089 &child, &count) != 0)
2090 return (EZFS_INVALCONFIG);
2093 * root pool can not have EFI labeled disks and can only have
2094 * a single top-level vdev.
2096 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
2097 pool_uses_efi(vdev_root))
2098 return (EZFS_POOL_INVALARG);
2100 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2103 /* No online devices */
2105 return (EZFS_NODEVICE);
2111 * Get phys_path for a root pool
2112 * Return 0 on success; non-zero on failure.
2115 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2117 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2122 * If the device has being dynamically expanded then we need to relabel
2123 * the disk to use the new unallocated space.
2126 zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2129 char path[MAXPATHLEN];
2132 int (*_efi_use_whole_disk)(int);
2134 if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2135 "efi_use_whole_disk")) == NULL)
2138 (void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name);
2140 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2141 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2142 "relabel '%s': unable to open device"), name);
2143 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2147 * It's possible that we might encounter an error if the device
2148 * does not have any unallocated space left. If so, we simply
2149 * ignore that error and continue on.
2151 error = _efi_use_whole_disk(fd);
2153 if (error && error != VT_ENOSPC) {
2154 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2155 "relabel '%s': unable to read disk capacity"), name);
2156 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2163 * Bring the specified vdev online. The 'flags' parameter is a set of the
2164 * ZFS_ONLINE_* flags.
2167 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2168 vdev_state_t *newstate)
2170 zfs_cmd_t zc = { 0 };
2173 boolean_t avail_spare, l2cache, islog;
2174 libzfs_handle_t *hdl = zhp->zpool_hdl;
2176 if (flags & ZFS_ONLINE_EXPAND) {
2177 (void) snprintf(msg, sizeof (msg),
2178 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2180 (void) snprintf(msg, sizeof (msg),
2181 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2184 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2185 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2187 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2189 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2192 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2194 if (flags & ZFS_ONLINE_EXPAND ||
2195 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2196 char *pathname = NULL;
2197 uint64_t wholedisk = 0;
2199 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2201 verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
2205 * XXX - L2ARC 1.0 devices can't support expansion.
2208 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2209 "cannot expand cache devices"));
2210 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2214 pathname += strlen(DISK_ROOT) + 1;
2215 (void) zpool_relabel_disk(hdl, pathname);
2219 zc.zc_cookie = VDEV_STATE_ONLINE;
2222 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2223 if (errno == EINVAL) {
2224 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2225 "from this pool into a new one. Use '%s' "
2226 "instead"), "zpool detach");
2227 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2229 return (zpool_standard_error(hdl, errno, msg));
2232 *newstate = zc.zc_cookie;
2237 * Take the specified vdev offline
2240 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2242 zfs_cmd_t zc = { 0 };
2245 boolean_t avail_spare, l2cache;
2246 libzfs_handle_t *hdl = zhp->zpool_hdl;
2248 (void) snprintf(msg, sizeof (msg),
2249 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2251 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2252 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2254 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2256 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2259 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2261 zc.zc_cookie = VDEV_STATE_OFFLINE;
2262 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2264 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2271 * There are no other replicas of this device.
2273 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2277 * The log device has unplayed logs
2279 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2282 return (zpool_standard_error(hdl, errno, msg));
2287 * Mark the given vdev faulted.
2290 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2292 zfs_cmd_t zc = { 0 };
2294 libzfs_handle_t *hdl = zhp->zpool_hdl;
2296 (void) snprintf(msg, sizeof (msg),
2297 dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2299 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2301 zc.zc_cookie = VDEV_STATE_FAULTED;
2304 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2311 * There are no other replicas of this device.
2313 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2316 return (zpool_standard_error(hdl, errno, msg));
2322 * Mark the given vdev degraded.
2325 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2327 zfs_cmd_t zc = { 0 };
2329 libzfs_handle_t *hdl = zhp->zpool_hdl;
2331 (void) snprintf(msg, sizeof (msg),
2332 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
2334 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2336 zc.zc_cookie = VDEV_STATE_DEGRADED;
2339 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2342 return (zpool_standard_error(hdl, errno, msg));
2346 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2350 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2356 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2358 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2361 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2362 children == 2 && child[which] == tgt)
2365 for (c = 0; c < children; c++)
2366 if (is_replacing_spare(child[c], tgt, which))
2374 * Attach new_disk (fully described by nvroot) to old_disk.
2375 * If 'replacing' is specified, the new disk will replace the old one.
2378 zpool_vdev_attach(zpool_handle_t *zhp,
2379 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2381 zfs_cmd_t zc = { 0 };
2385 boolean_t avail_spare, l2cache, islog;
2390 nvlist_t *config_root;
2391 libzfs_handle_t *hdl = zhp->zpool_hdl;
2392 boolean_t rootpool = pool_is_bootable(zhp);
2395 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2396 "cannot replace %s with %s"), old_disk, new_disk);
2398 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2399 "cannot attach %s to %s"), new_disk, old_disk);
2402 * If this is a root pool, make sure that we're not attaching an
2403 * EFI labeled device.
2405 if (rootpool && pool_uses_efi(nvroot)) {
2406 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2407 "EFI labeled devices are not supported on root pools."));
2408 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2411 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2412 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2414 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2417 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2420 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2422 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2423 zc.zc_cookie = replacing;
2425 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2426 &child, &children) != 0 || children != 1) {
2427 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2428 "new device must be a single disk"));
2429 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2432 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2433 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2435 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2439 * If the target is a hot spare that has been swapped in, we can only
2440 * replace it with another hot spare.
2443 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2444 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2445 NULL) == NULL || !avail_spare) &&
2446 is_replacing_spare(config_root, tgt, 1)) {
2447 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2448 "can only be replaced by another hot spare"));
2450 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2455 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2458 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2460 zcmd_free_nvlists(&zc);
2465 * XXX need a better way to prevent user from
2466 * booting up a half-baked vdev.
2468 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2469 "sure to wait until resilver is done "
2470 "before rebooting.\n"));
2471 (void) fprintf(stderr, "\n");
2472 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "If "
2473 "you boot from pool '%s', you may need to update\n"
2474 "boot code on newly attached disk '%s'.\n\n"
2475 "Assuming you use GPT partitioning and 'da0' is "
2476 "your new boot disk\n"
2477 "you may use the following command:\n\n"
2478 "\tgpart bootcode -b /boot/pmbr -p "
2479 "/boot/gptzfsboot -i 1 da0\n\n"),
2480 zhp->zpool_name, new_disk);
2488 * Can't attach to or replace this type of vdev.
2491 uint64_t version = zpool_get_prop_int(zhp,
2492 ZPOOL_PROP_VERSION, NULL);
2495 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2496 "cannot replace a log with a spare"));
2497 else if (version >= SPA_VERSION_MULTI_REPLACE)
2498 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2499 "already in replacing/spare config; wait "
2500 "for completion or use 'zpool detach'"));
2502 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2503 "cannot replace a replacing device"));
2505 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2506 "can only attach to mirrors and top-level "
2509 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2514 * The new device must be a single disk.
2516 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2517 "new device must be a single disk"));
2518 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2522 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2524 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2529 * The new device is too small.
2531 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2532 "device is too small"));
2533 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2538 * The new device has a different alignment requirement.
2540 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2541 "devices have different sector alignment"));
2542 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2547 * The resulting top-level vdev spec won't fit in the label.
2549 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2553 (void) zpool_standard_error(hdl, errno, msg);
2560 * Detach the specified device.
2563 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2565 zfs_cmd_t zc = { 0 };
2568 boolean_t avail_spare, l2cache;
2569 libzfs_handle_t *hdl = zhp->zpool_hdl;
2571 (void) snprintf(msg, sizeof (msg),
2572 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2574 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2575 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2577 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2580 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2583 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2585 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2587 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2594 * Can't detach from this type of vdev.
2596 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2597 "applicable to mirror and replacing vdevs"));
2598 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2603 * There are no other replicas of this device.
2605 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2609 (void) zpool_standard_error(hdl, errno, msg);
2616 * Find a mirror vdev in the source nvlist.
2618 * The mchild array contains a list of disks in one of the top-level mirrors
2619 * of the source pool. The schild array contains a list of disks that the
2620 * user specified on the command line. We loop over the mchild array to
2621 * see if any entry in the schild array matches.
2623 * If a disk in the mchild array is found in the schild array, we return
2624 * the index of that entry. Otherwise we return -1.
2627 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2628 nvlist_t **schild, uint_t schildren)
2632 for (mc = 0; mc < mchildren; mc++) {
2634 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2635 mchild[mc], B_FALSE);
2637 for (sc = 0; sc < schildren; sc++) {
2638 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2639 schild[sc], B_FALSE);
2640 boolean_t result = (strcmp(mpath, spath) == 0);
2656 * Split a mirror pool. If newroot points to null, then a new nvlist
2657 * is generated and it is the responsibility of the caller to free it.
2660 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2661 nvlist_t *props, splitflags_t flags)
2663 zfs_cmd_t zc = { 0 };
2665 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2666 nvlist_t **varray = NULL, *zc_props = NULL;
2667 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2668 libzfs_handle_t *hdl = zhp->zpool_hdl;
2670 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2673 (void) snprintf(msg, sizeof (msg),
2674 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2676 if (!zpool_name_valid(hdl, B_FALSE, newname))
2677 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2679 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2680 (void) fprintf(stderr, gettext("Internal error: unable to "
2681 "retrieve pool configuration\n"));
2685 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2687 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2690 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2691 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2692 props, vers, flags, msg)) == NULL)
2696 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2698 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2699 "Source pool is missing vdev tree"));
2701 nvlist_free(zc_props);
2705 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2708 if (*newroot == NULL ||
2709 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2710 &newchild, &newchildren) != 0)
2713 for (c = 0; c < children; c++) {
2714 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2716 nvlist_t **mchild, *vdev;
2721 * Unlike cache & spares, slogs are stored in the
2722 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2724 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2726 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2728 if (is_log || is_hole) {
2730 * Create a hole vdev and put it in the config.
2732 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2734 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2735 VDEV_TYPE_HOLE) != 0)
2737 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2742 varray[vcount++] = vdev;
2746 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
2748 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2749 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2750 "Source pool must be composed only of mirrors\n"));
2751 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2755 verify(nvlist_lookup_nvlist_array(child[c],
2756 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2758 /* find or add an entry for this top-level vdev */
2759 if (newchildren > 0 &&
2760 (entry = find_vdev_entry(zhp, mchild, mchildren,
2761 newchild, newchildren)) >= 0) {
2762 /* We found a disk that the user specified. */
2763 vdev = mchild[entry];
2766 /* User didn't specify a disk for this vdev. */
2767 vdev = mchild[mchildren - 1];
2770 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
2774 /* did we find every disk the user specified? */
2775 if (found != newchildren) {
2776 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
2777 "include at most one disk from each mirror"));
2778 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2782 /* Prepare the nvlist for populating. */
2783 if (*newroot == NULL) {
2784 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
2787 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
2788 VDEV_TYPE_ROOT) != 0)
2791 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
2794 /* Add all the children we found */
2795 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
2796 lastlog == 0 ? vcount : lastlog) != 0)
2800 * If we're just doing a dry run, exit now with success.
2803 memory_err = B_FALSE;
2808 /* now build up the config list & call the ioctl */
2809 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
2812 if (nvlist_add_nvlist(newconfig,
2813 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
2814 nvlist_add_string(newconfig,
2815 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
2816 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
2820 * The new pool is automatically part of the namespace unless we
2821 * explicitly export it.
2824 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
2825 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2826 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
2827 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
2829 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
2832 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
2833 retval = zpool_standard_error(hdl, errno, msg);
2838 memory_err = B_FALSE;
2841 if (varray != NULL) {
2844 for (v = 0; v < vcount; v++)
2845 nvlist_free(varray[v]);
2848 zcmd_free_nvlists(&zc);
2850 nvlist_free(zc_props);
2852 nvlist_free(newconfig);
2854 nvlist_free(*newroot);
2862 return (no_memory(hdl));
2868 * Remove the given device. Currently, this is supported only for hot spares
2869 * and level 2 cache devices.
2872 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
2874 zfs_cmd_t zc = { 0 };
2877 boolean_t avail_spare, l2cache, islog;
2878 libzfs_handle_t *hdl = zhp->zpool_hdl;
2881 (void) snprintf(msg, sizeof (msg),
2882 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
2884 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2885 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2887 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2889 * XXX - this should just go away.
2891 if (!avail_spare && !l2cache && !islog) {
2892 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2893 "only inactive hot spares, cache, top-level, "
2894 "or log devices can be removed"));
2895 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2898 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
2899 if (islog && version < SPA_VERSION_HOLES) {
2900 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2901 "pool must be upgrade to support log removal"));
2902 return (zfs_error(hdl, EZFS_BADVERSION, msg));
2905 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2907 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
2910 return (zpool_standard_error(hdl, errno, msg));
2914 * Clear the errors for the pool, or the particular device if specified.
2917 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
2919 zfs_cmd_t zc = { 0 };
2922 zpool_rewind_policy_t policy;
2923 boolean_t avail_spare, l2cache;
2924 libzfs_handle_t *hdl = zhp->zpool_hdl;
2925 nvlist_t *nvi = NULL;
2929 (void) snprintf(msg, sizeof (msg),
2930 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2933 (void) snprintf(msg, sizeof (msg),
2934 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2937 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2939 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
2940 &l2cache, NULL)) == 0)
2941 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2944 * Don't allow error clearing for hot spares. Do allow
2945 * error clearing for l2cache devices.
2948 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2950 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
2954 zpool_get_rewind_policy(rewindnvl, &policy);
2955 zc.zc_cookie = policy.zrp_request;
2957 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
2960 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
2963 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
2965 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
2966 zcmd_free_nvlists(&zc);
2971 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
2972 errno != EPERM && errno != EACCES)) {
2973 if (policy.zrp_request &
2974 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2975 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
2976 zpool_rewind_exclaim(hdl, zc.zc_name,
2977 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
2981 zcmd_free_nvlists(&zc);
2985 zcmd_free_nvlists(&zc);
2986 return (zpool_standard_error(hdl, errno, msg));
2990 * Similar to zpool_clear(), but takes a GUID (used by fmd).
2993 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
2995 zfs_cmd_t zc = { 0 };
2997 libzfs_handle_t *hdl = zhp->zpool_hdl;
2999 (void) snprintf(msg, sizeof (msg),
3000 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3003 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3005 zc.zc_cookie = ZPOOL_NO_REWIND;
3007 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3010 return (zpool_standard_error(hdl, errno, msg));
3014 * Convert from a devid string to a path.
3017 devid_to_path(char *devid_str)
3022 devid_nmlist_t *list = NULL;
3025 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3028 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3030 devid_str_free(minor);
3036 if ((path = strdup(list[0].devname)) == NULL)
3039 devid_free_nmlist(list);
3045 * Convert from a path to a devid string.
3048 path_to_devid(const char *path)
3054 if ((fd = open(path, O_RDONLY)) < 0)
3059 if (devid_get(fd, &devid) == 0) {
3060 if (devid_get_minor_name(fd, &minor) == 0)
3061 ret = devid_str_encode(devid, minor);
3063 devid_str_free(minor);
3072 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3073 * ignore any failure here, since a common case is for an unprivileged user to
3074 * type 'zpool status', and we'll display the correct information anyway.
3077 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3079 zfs_cmd_t zc = { 0 };
3081 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3082 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3083 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3086 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3090 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3091 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3092 * We also check if this is a whole disk, in which case we strip off the
3093 * trailing 's0' slice name.
3095 * This routine is also responsible for identifying when disks have been
3096 * reconfigured in a new location. The kernel will have opened the device by
3097 * devid, but the path will still refer to the old location. To catch this, we
3098 * first do a path -> devid translation (which is fast for the common case). If
3099 * the devid matches, we're done. If not, we do a reverse devid -> path
3100 * translation and issue the appropriate ioctl() to update the path of the vdev.
3101 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3105 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3116 have_stats = nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3117 (uint64_t **)&vs, &vsc) == 0;
3118 have_path = nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0;
3121 * If the device is not currently present, assume it will not
3122 * come back at the same device path. Display the device by GUID.
3124 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3125 have_path && have_stats && vs->vs_state <= VDEV_STATE_CANT_OPEN) {
3126 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3128 (void) snprintf(buf, sizeof (buf), "%llu",
3129 (u_longlong_t)value);
3131 } else if (have_path) {
3134 * If the device is dead (faulted, offline, etc) then don't
3135 * bother opening it. Otherwise we may be forcing the user to
3136 * open a misbehaving device, which can have undesirable
3139 if ((have_stats == 0 ||
3140 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3142 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3144 * Determine if the current path is correct.
3146 char *newdevid = path_to_devid(path);
3148 if (newdevid == NULL ||
3149 strcmp(devid, newdevid) != 0) {
3152 if ((newpath = devid_to_path(devid)) != NULL) {
3154 * Update the path appropriately.
3156 set_path(zhp, nv, newpath);
3157 if (nvlist_add_string(nv,
3158 ZPOOL_CONFIG_PATH, newpath) == 0)
3159 verify(nvlist_lookup_string(nv,
3167 devid_str_free(newdevid);
3171 if (strncmp(path, "/dev/dsk/", 9) == 0)
3174 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3175 &value) == 0 && value) {
3176 int pathlen = strlen(path);
3177 char *tmp = zfs_strdup(hdl, path);
3180 * If it starts with c#, and ends with "s0", chop
3181 * the "s0" off, or if it ends with "s0/old", remove
3182 * the "s0" from the middle.
3184 if (CTD_CHECK(tmp)) {
3185 if (strcmp(&tmp[pathlen - 2], "s0") == 0) {
3186 tmp[pathlen - 2] = '\0';
3187 } else if (pathlen > 6 &&
3188 strcmp(&tmp[pathlen - 6], "s0/old") == 0) {
3189 (void) strcpy(&tmp[pathlen - 6],
3196 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3197 path += sizeof(_PATH_DEV) - 1;
3200 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3203 * If it's a raidz device, we need to stick in the parity level.
3205 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3206 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3208 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3209 (u_longlong_t)value);
3214 * We identify each top-level vdev by using a <type-id>
3215 * naming convention.
3220 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3222 (void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3228 return (zfs_strdup(hdl, path));
3232 zbookmark_compare(const void *a, const void *b)
3234 return (memcmp(a, b, sizeof (zbookmark_t)));
3238 * Retrieve the persistent error log, uniquify the members, and return to the
3242 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3244 zfs_cmd_t zc = { 0 };
3246 zbookmark_t *zb = NULL;
3250 * Retrieve the raw error list from the kernel. If the number of errors
3251 * has increased, allocate more space and continue until we get the
3254 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3258 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3259 count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
3261 zc.zc_nvlist_dst_size = count;
3262 (void) strcpy(zc.zc_name, zhp->zpool_name);
3264 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3266 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3267 if (errno == ENOMEM) {
3268 count = zc.zc_nvlist_dst_size;
3269 if ((zc.zc_nvlist_dst = (uintptr_t)
3270 zfs_alloc(zhp->zpool_hdl, count *
3271 sizeof (zbookmark_t))) == (uintptr_t)NULL)
3282 * Sort the resulting bookmarks. This is a little confusing due to the
3283 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3284 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3285 * _not_ copied as part of the process. So we point the start of our
3286 * array appropriate and decrement the total number of elements.
3288 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
3289 zc.zc_nvlist_dst_size;
3290 count -= zc.zc_nvlist_dst_size;
3292 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
3294 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3297 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3299 for (i = 0; i < count; i++) {
3302 /* ignoring zb_blkid and zb_level for now */
3303 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3304 zb[i-1].zb_object == zb[i].zb_object)
3307 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3309 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3310 zb[i].zb_objset) != 0) {
3314 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3315 zb[i].zb_object) != 0) {
3319 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3326 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3330 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3331 return (no_memory(zhp->zpool_hdl));
3335 * Upgrade a ZFS pool to the latest on-disk version.
3338 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3340 zfs_cmd_t zc = { 0 };
3341 libzfs_handle_t *hdl = zhp->zpool_hdl;
3343 (void) strcpy(zc.zc_name, zhp->zpool_name);
3344 zc.zc_cookie = new_version;
3346 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3347 return (zpool_standard_error_fmt(hdl, errno,
3348 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3354 zpool_set_history_str(const char *subcommand, int argc, char **argv,
3359 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
3360 for (i = 1; i < argc; i++) {
3361 if (strlen(history_str) + 1 + strlen(argv[i]) >
3364 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
3365 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
3370 * Stage command history for logging.
3373 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
3375 if (history_str == NULL)
3378 if (strlen(history_str) > HIS_MAX_RECORD_LEN)
3381 if (hdl->libzfs_log_str != NULL)
3382 free(hdl->libzfs_log_str);
3384 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
3385 return (no_memory(hdl));
3391 * Perform ioctl to get some command history of a pool.
3393 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3394 * logical offset of the history buffer to start reading from.
3396 * Upon return, 'off' is the next logical offset to read from and
3397 * 'len' is the actual amount of bytes read into 'buf'.
3400 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3402 zfs_cmd_t zc = { 0 };
3403 libzfs_handle_t *hdl = zhp->zpool_hdl;
3405 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3407 zc.zc_history = (uint64_t)(uintptr_t)buf;
3408 zc.zc_history_len = *len;
3409 zc.zc_history_offset = *off;
3411 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3414 return (zfs_error_fmt(hdl, EZFS_PERM,
3415 dgettext(TEXT_DOMAIN,
3416 "cannot show history for pool '%s'"),
3419 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3420 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3421 "'%s'"), zhp->zpool_name));
3423 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3424 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3425 "'%s', pool must be upgraded"), zhp->zpool_name));
3427 return (zpool_standard_error_fmt(hdl, errno,
3428 dgettext(TEXT_DOMAIN,
3429 "cannot get history for '%s'"), zhp->zpool_name));
3433 *len = zc.zc_history_len;
3434 *off = zc.zc_history_offset;
3440 * Process the buffer of nvlists, unpacking and storing each nvlist record
3441 * into 'records'. 'leftover' is set to the number of bytes that weren't
3442 * processed as there wasn't a complete record.
3445 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3446 nvlist_t ***records, uint_t *numrecords)
3452 while (bytes_read > sizeof (reclen)) {
3454 /* get length of packed record (stored as little endian) */
3455 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3456 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3458 if (bytes_read < sizeof (reclen) + reclen)
3462 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3464 bytes_read -= sizeof (reclen) + reclen;
3465 buf += sizeof (reclen) + reclen;
3467 /* add record to nvlist array */
3469 if (ISP2(*numrecords + 1)) {
3470 *records = realloc(*records,
3471 *numrecords * 2 * sizeof (nvlist_t *));
3473 (*records)[*numrecords - 1] = nv;
3476 *leftover = bytes_read;
3480 #define HIS_BUF_LEN (128*1024)
3483 * Retrieve the command history of a pool.
3486 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3488 char buf[HIS_BUF_LEN];
3490 nvlist_t **records = NULL;
3491 uint_t numrecords = 0;
3495 uint64_t bytes_read = sizeof (buf);
3498 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3501 /* if nothing else was read in, we're at EOF, just return */
3505 if ((err = zpool_history_unpack(buf, bytes_read,
3506 &leftover, &records, &numrecords)) != 0)
3514 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3515 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3516 records, numrecords) == 0);
3518 for (i = 0; i < numrecords; i++)
3519 nvlist_free(records[i]);
3526 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3527 char *pathname, size_t len)
3529 zfs_cmd_t zc = { 0 };
3530 boolean_t mounted = B_FALSE;
3531 char *mntpnt = NULL;
3532 char dsname[MAXNAMELEN];
3535 /* special case for the MOS */
3536 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
3540 /* get the dataset's name */
3541 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3543 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3544 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3545 /* just write out a path of two object numbers */
3546 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3550 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3552 /* find out if the dataset is mounted */
3553 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3555 /* get the corrupted object's path */
3556 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3558 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3561 (void) snprintf(pathname, len, "%s%s", mntpnt,
3564 (void) snprintf(pathname, len, "%s:%s",
3565 dsname, zc.zc_value);
3568 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
3575 * Read the EFI label from the config, if a label does not exist then
3576 * pass back the error to the caller. If the caller has passed a non-NULL
3577 * diskaddr argument then we set it to the starting address of the EFI
3581 read_efi_label(nvlist_t *config, diskaddr_t *sb)
3585 char diskname[MAXPATHLEN];
3588 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3591 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
3592 strrchr(path, '/'));
3593 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
3594 struct dk_gpt *vtoc;
3596 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3598 *sb = vtoc->efi_parts[0].p_start;
3607 * determine where a partition starts on a disk in the current
3611 find_start_block(nvlist_t *config)
3615 diskaddr_t sb = MAXOFFSET_T;
3618 if (nvlist_lookup_nvlist_array(config,
3619 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3620 if (nvlist_lookup_uint64(config,
3621 ZPOOL_CONFIG_WHOLE_DISK,
3622 &wholedisk) != 0 || !wholedisk) {
3623 return (MAXOFFSET_T);
3625 if (read_efi_label(config, &sb) < 0)
3630 for (c = 0; c < children; c++) {
3631 sb = find_start_block(child[c]);
3632 if (sb != MAXOFFSET_T) {
3636 return (MAXOFFSET_T);
3641 * Label an individual disk. The name provided is the short name,
3642 * stripped of any leading /dev path.
3645 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name)
3648 char path[MAXPATHLEN];
3649 struct dk_gpt *vtoc;
3651 size_t resv = EFI_MIN_RESV_SIZE;
3652 uint64_t slice_size;
3653 diskaddr_t start_block;
3656 /* prepare an error message just in case */
3657 (void) snprintf(errbuf, sizeof (errbuf),
3658 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3663 if (pool_is_bootable(zhp)) {
3664 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3665 "EFI labeled devices are not supported on root "
3667 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3670 verify(nvlist_lookup_nvlist(zhp->zpool_config,
3671 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3673 if (zhp->zpool_start_block == 0)
3674 start_block = find_start_block(nvroot);
3676 start_block = zhp->zpool_start_block;
3677 zhp->zpool_start_block = start_block;
3680 start_block = NEW_START_BLOCK;
3683 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
3686 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
3688 * This shouldn't happen. We've long since verified that this
3689 * is a valid device.
3692 dgettext(TEXT_DOMAIN, "unable to open device"));
3693 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
3696 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
3698 * The only way this can fail is if we run out of memory, or we
3699 * were unable to read the disk's capacity
3701 if (errno == ENOMEM)
3702 (void) no_memory(hdl);
3705 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3706 "unable to read disk capacity"), name);
3708 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
3711 slice_size = vtoc->efi_last_u_lba + 1;
3712 slice_size -= EFI_MIN_RESV_SIZE;
3713 if (start_block == MAXOFFSET_T)
3714 start_block = NEW_START_BLOCK;
3715 slice_size -= start_block;
3717 vtoc->efi_parts[0].p_start = start_block;
3718 vtoc->efi_parts[0].p_size = slice_size;
3721 * Why we use V_USR: V_BACKUP confuses users, and is considered
3722 * disposable by some EFI utilities (since EFI doesn't have a backup
3723 * slice). V_UNASSIGNED is supposed to be used only for zero size
3724 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
3725 * etc. were all pretty specific. V_USR is as close to reality as we
3726 * can get, in the absence of V_OTHER.
3728 vtoc->efi_parts[0].p_tag = V_USR;
3729 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
3731 vtoc->efi_parts[8].p_start = slice_size + start_block;
3732 vtoc->efi_parts[8].p_size = resv;
3733 vtoc->efi_parts[8].p_tag = V_RESERVED;
3735 if (efi_write(fd, vtoc) != 0) {
3737 * Some block drivers (like pcata) may not support EFI
3738 * GPT labels. Print out a helpful error message dir-
3739 * ecting the user to manually label the disk and give
3745 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3746 "try using fdisk(1M) and then provide a specific slice"));
3747 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3757 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
3763 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
3764 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
3765 strcmp(type, VDEV_TYPE_FILE) == 0 ||
3766 strcmp(type, VDEV_TYPE_LOG) == 0 ||
3767 strcmp(type, VDEV_TYPE_HOLE) == 0 ||
3768 strcmp(type, VDEV_TYPE_MISSING) == 0) {
3769 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3770 "vdev type '%s' is not supported"), type);
3771 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
3774 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
3775 &child, &children) == 0) {
3776 for (c = 0; c < children; c++) {
3777 if (!supported_dump_vdev_type(hdl, child[c], errbuf))
3785 * check if this zvol is allowable for use as a dump device; zero if
3786 * it is, > 0 if it isn't, < 0 if it isn't a zvol
3789 zvol_check_dump_config(char *arg)
3791 zpool_handle_t *zhp = NULL;
3792 nvlist_t *config, *nvroot;
3796 libzfs_handle_t *hdl;
3798 char poolname[ZPOOL_MAXNAMELEN];
3799 int pathlen = strlen(ZVOL_FULL_DEV_DIR);
3802 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
3806 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3807 "dump is not supported on device '%s'"), arg);
3809 if ((hdl = libzfs_init()) == NULL)
3811 libzfs_print_on_error(hdl, B_TRUE);
3813 volname = arg + pathlen;
3815 /* check the configuration of the pool */
3816 if ((p = strchr(volname, '/')) == NULL) {
3817 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3818 "malformed dataset name"));
3819 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3821 } else if (p - volname >= ZFS_MAXNAMELEN) {
3822 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3823 "dataset name is too long"));
3824 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
3827 (void) strncpy(poolname, volname, p - volname);
3828 poolname[p - volname] = '\0';
3831 if ((zhp = zpool_open(hdl, poolname)) == NULL) {
3832 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3833 "could not open pool '%s'"), poolname);
3834 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
3837 config = zpool_get_config(zhp, NULL);
3838 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3840 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3841 "could not obtain vdev configuration for '%s'"), poolname);
3842 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
3846 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3847 &top, &toplevels) == 0);
3848 if (toplevels != 1) {
3849 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3850 "'%s' has multiple top level vdevs"), poolname);
3851 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
3855 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {