4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011 by Delphix. All rights reserved.
28 #include <sys/types.h>
39 #include <sys/zfs_ioctl.h>
42 #include "zfs_namecheck.h"
44 #include "libzfs_impl.h"
45 #include "zfs_comutil.h"
47 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
49 #define DISK_ROOT "/dev/dsk"
50 #define RDISK_ROOT "/dev/rdsk"
51 #define BACKUP_SLICE "s2"
53 typedef struct prop_flags {
54 int create:1; /* Validate property on creation */
55 int import:1; /* Validate property on import */
59 * ====================================================================
60 * zpool property functions
61 * ====================================================================
65 zpool_get_all_props(zpool_handle_t *zhp)
68 libzfs_handle_t *hdl = zhp->zpool_hdl;
70 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
72 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
75 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
76 if (errno == ENOMEM) {
77 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
78 zcmd_free_nvlists(&zc);
82 zcmd_free_nvlists(&zc);
87 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
88 zcmd_free_nvlists(&zc);
92 zcmd_free_nvlists(&zc);
98 zpool_props_refresh(zpool_handle_t *zhp)
102 old_props = zhp->zpool_props;
104 if (zpool_get_all_props(zhp) != 0)
107 nvlist_free(old_props);
112 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
118 zprop_source_t source;
120 nvl = zhp->zpool_props;
121 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
122 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
124 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
126 source = ZPROP_SRC_DEFAULT;
127 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
138 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
142 zprop_source_t source;
144 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
146 * zpool_get_all_props() has most likely failed because
147 * the pool is faulted, but if all we need is the top level
148 * vdev's guid then get it from the zhp config nvlist.
150 if ((prop == ZPOOL_PROP_GUID) &&
151 (nvlist_lookup_nvlist(zhp->zpool_config,
152 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
153 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
157 return (zpool_prop_default_numeric(prop));
160 nvl = zhp->zpool_props;
161 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
162 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
164 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
166 source = ZPROP_SRC_DEFAULT;
167 value = zpool_prop_default_numeric(prop);
177 * Map VDEV STATE to printed strings.
180 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 * Map POOL STATE to printed strings.
210 zpool_pool_state_to_name(pool_state_t state)
213 case POOL_STATE_ACTIVE:
214 return (gettext("ACTIVE"));
215 case POOL_STATE_EXPORTED:
216 return (gettext("EXPORTED"));
217 case POOL_STATE_DESTROYED:
218 return (gettext("DESTROYED"));
219 case POOL_STATE_SPARE:
220 return (gettext("SPARE"));
221 case POOL_STATE_L2CACHE:
222 return (gettext("L2CACHE"));
223 case POOL_STATE_UNINITIALIZED:
224 return (gettext("UNINITIALIZED"));
225 case POOL_STATE_UNAVAIL:
226 return (gettext("UNAVAIL"));
227 case POOL_STATE_POTENTIALLY_ACTIVE:
228 return (gettext("POTENTIALLY_ACTIVE"));
231 return (gettext("UNKNOWN"));
235 * Get a zpool property value for 'prop' and return the value in
236 * a pre-allocated buffer.
239 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
240 zprop_source_t *srctype)
244 zprop_source_t src = ZPROP_SRC_NONE;
249 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
251 case ZPOOL_PROP_NAME:
252 (void) strlcpy(buf, zpool_get_name(zhp), len);
255 case ZPOOL_PROP_HEALTH:
256 (void) strlcpy(buf, "FAULTED", len);
259 case ZPOOL_PROP_GUID:
260 intval = zpool_get_prop_int(zhp, prop, &src);
261 (void) snprintf(buf, len, "%llu", intval);
264 case ZPOOL_PROP_ALTROOT:
265 case ZPOOL_PROP_CACHEFILE:
266 case ZPOOL_PROP_COMMENT:
267 if (zhp->zpool_props != NULL ||
268 zpool_get_all_props(zhp) == 0) {
270 zpool_get_prop_string(zhp, prop, &src),
278 (void) strlcpy(buf, "-", len);
287 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
288 prop != ZPOOL_PROP_NAME)
291 switch (zpool_prop_get_type(prop)) {
292 case PROP_TYPE_STRING:
293 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
297 case PROP_TYPE_NUMBER:
298 intval = zpool_get_prop_int(zhp, prop, &src);
301 case ZPOOL_PROP_SIZE:
302 case ZPOOL_PROP_ALLOCATED:
303 case ZPOOL_PROP_FREE:
304 (void) zfs_nicenum(intval, buf, len);
307 case ZPOOL_PROP_CAPACITY:
308 (void) snprintf(buf, len, "%llu%%",
309 (u_longlong_t)intval);
312 case ZPOOL_PROP_DEDUPRATIO:
313 (void) snprintf(buf, len, "%llu.%02llux",
314 (u_longlong_t)(intval / 100),
315 (u_longlong_t)(intval % 100));
318 case ZPOOL_PROP_HEALTH:
319 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
320 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
321 verify(nvlist_lookup_uint64_array(nvroot,
322 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
325 (void) strlcpy(buf, zpool_state_to_name(intval,
329 (void) snprintf(buf, len, "%llu", intval);
333 case PROP_TYPE_INDEX:
334 intval = zpool_get_prop_int(zhp, prop, &src);
335 if (zpool_prop_index_to_string(prop, intval, &strval)
338 (void) strlcpy(buf, strval, len);
352 * Check if the bootfs name has the same pool name as it is set to.
353 * Assuming bootfs is a valid dataset name.
356 bootfs_name_valid(const char *pool, char *bootfs)
358 int len = strlen(pool);
360 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
363 if (strncmp(pool, bootfs, len) == 0 &&
364 (bootfs[len] == '/' || bootfs[len] == '\0'))
371 * Inspect the configuration to determine if any of the devices contain
375 pool_uses_efi(nvlist_t *config)
381 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
382 &child, &children) != 0)
383 return (read_efi_label(config, NULL) >= 0);
385 for (c = 0; c < children; c++) {
386 if (pool_uses_efi(child[c]))
394 pool_is_bootable(zpool_handle_t *zhp)
396 char bootfs[ZPOOL_MAXNAMELEN];
398 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
399 sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
400 sizeof (bootfs)) != 0);
405 * Given an nvlist of zpool properties to be set, validate that they are
406 * correct, and parse any numeric properties (index, boolean, etc) if they are
407 * specified as strings.
410 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
411 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
419 struct stat64 statbuf;
423 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
424 (void) no_memory(hdl);
429 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
430 const char *propname = nvpair_name(elem);
433 * Make sure this property is valid and applies to this type.
435 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
436 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
437 "invalid property '%s'"), propname);
438 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
442 if (zpool_prop_readonly(prop)) {
443 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
444 "is readonly"), propname);
445 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
449 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
450 &strval, &intval, errbuf) != 0)
454 * Perform additional checking for specific properties.
457 case ZPOOL_PROP_VERSION:
458 if (intval < version || intval > SPA_VERSION) {
459 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
460 "property '%s' number %d is invalid."),
462 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
467 case ZPOOL_PROP_BOOTFS:
468 if (flags.create || flags.import) {
469 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
470 "property '%s' cannot be set at creation "
471 "or import time"), propname);
472 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
476 if (version < SPA_VERSION_BOOTFS) {
477 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
478 "pool must be upgraded to support "
479 "'%s' property"), propname);
480 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
485 * bootfs property value has to be a dataset name and
486 * the dataset has to be in the same pool as it sets to.
488 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
490 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
491 "is an invalid name"), strval);
492 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
496 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
497 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
498 "could not open pool '%s'"), poolname);
499 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
502 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
503 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
507 * bootfs property cannot be set on a disk which has
510 if (pool_uses_efi(nvroot)) {
511 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
512 "property '%s' not supported on "
513 "EFI labeled devices"), propname);
514 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
522 case ZPOOL_PROP_ALTROOT:
523 if (!flags.create && !flags.import) {
524 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
525 "property '%s' can only be set during pool "
526 "creation or import"), propname);
527 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
531 if (strval[0] != '/') {
532 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
533 "bad alternate root '%s'"), strval);
534 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
539 case ZPOOL_PROP_CACHEFILE:
540 if (strval[0] == '\0')
543 if (strcmp(strval, "none") == 0)
546 if (strval[0] != '/') {
547 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
548 "property '%s' must be empty, an "
549 "absolute path, or 'none'"), propname);
550 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
554 slash = strrchr(strval, '/');
556 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
557 strcmp(slash, "/..") == 0) {
558 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
559 "'%s' is not a valid file"), strval);
560 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
566 if (strval[0] != '\0' &&
567 (stat64(strval, &statbuf) != 0 ||
568 !S_ISDIR(statbuf.st_mode))) {
569 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
570 "'%s' is not a valid directory"),
572 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
579 case ZPOOL_PROP_COMMENT:
580 for (check = strval; *check != '\0'; check++) {
581 if (!isprint(*check)) {
583 dgettext(TEXT_DOMAIN,
584 "comment may only have printable "
586 (void) zfs_error(hdl, EZFS_BADPROP,
591 if (strlen(strval) > ZPROP_MAX_COMMENT) {
592 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
593 "comment must not exceed %d characters"),
595 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
599 case ZPOOL_PROP_READONLY:
601 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
602 "property '%s' can only be set at "
603 "import time"), propname);
604 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
613 nvlist_free(retprops);
618 * Set zpool property : propname=propval.
621 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
623 zfs_cmd_t zc = { 0 };
626 nvlist_t *nvl = NULL;
629 prop_flags_t flags = { 0 };
631 (void) snprintf(errbuf, sizeof (errbuf),
632 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
635 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
636 return (no_memory(zhp->zpool_hdl));
638 if (nvlist_add_string(nvl, propname, propval) != 0) {
640 return (no_memory(zhp->zpool_hdl));
643 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
644 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
645 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
654 * Execute the corresponding ioctl() to set this property.
656 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
658 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
663 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
665 zcmd_free_nvlists(&zc);
669 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
671 (void) zpool_props_refresh(zhp);
677 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
679 libzfs_handle_t *hdl = zhp->zpool_hdl;
681 char buf[ZFS_MAXPROPLEN];
683 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
686 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
691 if (entry->pl_prop != ZPROP_INVAL &&
692 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
694 if (strlen(buf) > entry->pl_width)
695 entry->pl_width = strlen(buf);
704 * Don't start the slice at the default block of 34; many storage
705 * devices will use a stripe width of 128k, so start there instead.
707 #define NEW_START_BLOCK 256
710 * Validate the given pool name, optionally putting an extended error message in
714 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
720 ret = pool_namecheck(pool, &why, &what);
723 * The rules for reserved pool names were extended at a later point.
724 * But we need to support users with existing pools that may now be
725 * invalid. So we only check for this expanded set of names during a
726 * create (or import), and only in userland.
728 if (ret == 0 && !isopen &&
729 (strncmp(pool, "mirror", 6) == 0 ||
730 strncmp(pool, "raidz", 5) == 0 ||
731 strncmp(pool, "spare", 5) == 0 ||
732 strcmp(pool, "log") == 0)) {
735 dgettext(TEXT_DOMAIN, "name is reserved"));
743 case NAME_ERR_TOOLONG:
745 dgettext(TEXT_DOMAIN, "name is too long"));
748 case NAME_ERR_INVALCHAR:
750 dgettext(TEXT_DOMAIN, "invalid character "
751 "'%c' in pool name"), what);
754 case NAME_ERR_NOLETTER:
755 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
756 "name must begin with a letter"));
759 case NAME_ERR_RESERVED:
760 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
761 "name is reserved"));
764 case NAME_ERR_DISKLIKE:
765 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
766 "pool name is reserved"));
769 case NAME_ERR_LEADING_SLASH:
770 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
771 "leading slash in name"));
774 case NAME_ERR_EMPTY_COMPONENT:
775 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
776 "empty component in name"));
779 case NAME_ERR_TRAILING_SLASH:
780 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
781 "trailing slash in name"));
784 case NAME_ERR_MULTIPLE_AT:
785 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
786 "multiple '@' delimiters in name"));
798 * Open a handle to the given pool, even if the pool is currently in the FAULTED
802 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
808 * Make sure the pool name is valid.
810 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
811 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
812 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
817 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
820 zhp->zpool_hdl = hdl;
821 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
823 if (zpool_refresh_stats(zhp, &missing) != 0) {
829 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
830 (void) zfs_error_fmt(hdl, EZFS_NOENT,
831 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
840 * Like the above, but silent on error. Used when iterating over pools (because
841 * the configuration cache may be out of date).
844 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
849 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
852 zhp->zpool_hdl = hdl;
853 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
855 if (zpool_refresh_stats(zhp, &missing) != 0) {
871 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
875 zpool_open(libzfs_handle_t *hdl, const char *pool)
879 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
882 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
883 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
884 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
893 * Close the handle. Simply frees the memory associated with the handle.
896 zpool_close(zpool_handle_t *zhp)
898 if (zhp->zpool_config)
899 nvlist_free(zhp->zpool_config);
900 if (zhp->zpool_old_config)
901 nvlist_free(zhp->zpool_old_config);
902 if (zhp->zpool_props)
903 nvlist_free(zhp->zpool_props);
908 * Return the name of the pool.
911 zpool_get_name(zpool_handle_t *zhp)
913 return (zhp->zpool_name);
918 * Return the state of the pool (ACTIVE or UNAVAILABLE)
921 zpool_get_state(zpool_handle_t *zhp)
923 return (zhp->zpool_state);
927 * Create the named pool, using the provided vdev list. It is assumed
928 * that the consumer has already validated the contents of the nvlist, so we
929 * don't have to worry about error semantics.
932 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
933 nvlist_t *props, nvlist_t *fsprops)
935 zfs_cmd_t zc = { 0 };
936 nvlist_t *zc_fsprops = NULL;
937 nvlist_t *zc_props = NULL;
942 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
943 "cannot create '%s'"), pool);
945 if (!zpool_name_valid(hdl, B_FALSE, pool))
946 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
948 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
952 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
954 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
955 SPA_VERSION_1, flags, msg)) == NULL) {
964 zoned = ((nvlist_lookup_string(fsprops,
965 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
966 strcmp(zonestr, "on") == 0);
968 if ((zc_fsprops = zfs_valid_proplist(hdl,
969 ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
973 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
976 if (nvlist_add_nvlist(zc_props,
977 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
982 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
985 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
987 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
989 zcmd_free_nvlists(&zc);
990 nvlist_free(zc_props);
991 nvlist_free(zc_fsprops);
996 * This can happen if the user has specified the same
997 * device multiple times. We can't reliably detect this
998 * until we try to add it and see we already have a
1001 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1002 "one or more vdevs refer to the same device"));
1003 return (zfs_error(hdl, EZFS_BADDEV, msg));
1007 * This occurs when one of the devices is below
1008 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1009 * device was the problem device since there's no
1010 * reliable way to determine device size from userland.
1015 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1017 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1018 "one or more devices is less than the "
1019 "minimum size (%s)"), buf);
1021 return (zfs_error(hdl, EZFS_BADDEV, msg));
1024 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1025 "one or more devices is out of space"));
1026 return (zfs_error(hdl, EZFS_BADDEV, msg));
1029 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1030 "cache device must be a disk or disk slice"));
1031 return (zfs_error(hdl, EZFS_BADDEV, msg));
1034 return (zpool_standard_error(hdl, errno, msg));
1039 * If this is an alternate root pool, then we automatically set the
1040 * mountpoint of the root dataset to be '/'.
1042 if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
1046 verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
1047 verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1054 zcmd_free_nvlists(&zc);
1055 nvlist_free(zc_props);
1056 nvlist_free(zc_fsprops);
1061 * Destroy the given pool. It is up to the caller to ensure that there are no
1062 * datasets left in the pool.
1065 zpool_destroy(zpool_handle_t *zhp)
1067 zfs_cmd_t zc = { 0 };
1068 zfs_handle_t *zfp = NULL;
1069 libzfs_handle_t *hdl = zhp->zpool_hdl;
1072 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1073 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1076 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1078 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1079 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1080 "cannot destroy '%s'"), zhp->zpool_name);
1082 if (errno == EROFS) {
1083 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1084 "one or more devices is read only"));
1085 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1087 (void) zpool_standard_error(hdl, errno, msg);
1096 remove_mountpoint(zfp);
1104 * Add the given vdevs to the pool. The caller must have already performed the
1105 * necessary verification to ensure that the vdev specification is well-formed.
1108 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1110 zfs_cmd_t zc = { 0 };
1112 libzfs_handle_t *hdl = zhp->zpool_hdl;
1114 nvlist_t **spares, **l2cache;
1115 uint_t nspares, nl2cache;
1117 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1118 "cannot add to '%s'"), zhp->zpool_name);
1120 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1121 SPA_VERSION_SPARES &&
1122 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1123 &spares, &nspares) == 0) {
1124 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1125 "upgraded to add hot spares"));
1126 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1129 if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1130 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1133 for (s = 0; s < nspares; s++) {
1136 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1137 &path) == 0 && pool_uses_efi(spares[s])) {
1138 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1139 "device '%s' contains an EFI label and "
1140 "cannot be used on root pools."),
1141 zpool_vdev_name(hdl, NULL, spares[s],
1143 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1148 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1149 SPA_VERSION_L2CACHE &&
1150 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1151 &l2cache, &nl2cache) == 0) {
1152 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1153 "upgraded to add cache devices"));
1154 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1157 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1159 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1161 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1165 * This can happen if the user has specified the same
1166 * device multiple times. We can't reliably detect this
1167 * until we try to add it and see we already have a
1170 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1171 "one or more vdevs refer to the same device"));
1172 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1177 * This occurrs when one of the devices is below
1178 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1179 * device was the problem device since there's no
1180 * reliable way to determine device size from userland.
1185 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1187 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1188 "device is less than the minimum "
1191 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1195 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1196 "pool must be upgraded to add these vdevs"));
1197 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1201 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1202 "root pool can not have multiple vdevs"
1203 " or separate logs"));
1204 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1208 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1209 "cache device must be a disk or disk slice"));
1210 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1214 (void) zpool_standard_error(hdl, errno, msg);
1222 zcmd_free_nvlists(&zc);
1228 * Exports the pool from the system. The caller must ensure that there are no
1229 * mounted datasets in the pool.
1232 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1234 zfs_cmd_t zc = { 0 };
1237 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1238 "cannot export '%s'"), zhp->zpool_name);
1240 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1241 zc.zc_cookie = force;
1242 zc.zc_guid = hardforce;
1244 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1247 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1248 "use '-f' to override the following errors:\n"
1249 "'%s' has an active shared spare which could be"
1250 " used by other pools once '%s' is exported."),
1251 zhp->zpool_name, zhp->zpool_name);
1252 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1255 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1264 zpool_export(zpool_handle_t *zhp, boolean_t force)
1266 return (zpool_export_common(zhp, force, B_FALSE));
1270 zpool_export_force(zpool_handle_t *zhp)
1272 return (zpool_export_common(zhp, B_TRUE, B_TRUE));
1276 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1279 nvlist_t *nv = NULL;
1285 if (!hdl->libzfs_printerr || config == NULL)
1288 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0)
1291 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1293 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1295 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1296 strftime(timestr, 128, 0, &t) != 0) {
1298 (void) printf(dgettext(TEXT_DOMAIN,
1299 "Would be able to return %s "
1300 "to its state as of %s.\n"),
1303 (void) printf(dgettext(TEXT_DOMAIN,
1304 "Pool %s returned to its state as of %s.\n"),
1308 (void) printf(dgettext(TEXT_DOMAIN,
1309 "%s approximately %lld "),
1310 dryrun ? "Would discard" : "Discarded",
1312 (void) printf(dgettext(TEXT_DOMAIN,
1313 "minutes of transactions.\n"));
1314 } else if (loss > 0) {
1315 (void) printf(dgettext(TEXT_DOMAIN,
1316 "%s approximately %lld "),
1317 dryrun ? "Would discard" : "Discarded", loss);
1318 (void) printf(dgettext(TEXT_DOMAIN,
1319 "seconds of transactions.\n"));
1325 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1328 nvlist_t *nv = NULL;
1330 uint64_t edata = UINT64_MAX;
1335 if (!hdl->libzfs_printerr)
1339 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1341 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1343 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1344 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1345 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1348 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1349 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1352 (void) printf(dgettext(TEXT_DOMAIN,
1353 "Recovery is possible, but will result in some data loss.\n"));
1355 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1356 strftime(timestr, 128, 0, &t) != 0) {
1357 (void) printf(dgettext(TEXT_DOMAIN,
1358 "\tReturning the pool to its state as of %s\n"
1359 "\tshould correct the problem. "),
1362 (void) printf(dgettext(TEXT_DOMAIN,
1363 "\tReverting the pool to an earlier state "
1364 "should correct the problem.\n\t"));
1368 (void) printf(dgettext(TEXT_DOMAIN,
1369 "Approximately %lld minutes of data\n"
1370 "\tmust be discarded, irreversibly. "), (loss + 30) / 60);
1371 } else if (loss > 0) {
1372 (void) printf(dgettext(TEXT_DOMAIN,
1373 "Approximately %lld seconds of data\n"
1374 "\tmust be discarded, irreversibly. "), loss);
1376 if (edata != 0 && edata != UINT64_MAX) {
1378 (void) printf(dgettext(TEXT_DOMAIN,
1379 "After rewind, at least\n"
1380 "\tone persistent user-data error will remain. "));
1382 (void) printf(dgettext(TEXT_DOMAIN,
1383 "After rewind, several\n"
1384 "\tpersistent user-data errors will remain. "));
1387 (void) printf(dgettext(TEXT_DOMAIN,
1388 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1389 reason >= 0 ? "clear" : "import", name);
1391 (void) printf(dgettext(TEXT_DOMAIN,
1392 "A scrub of the pool\n"
1393 "\tis strongly recommended after recovery.\n"));
1397 (void) printf(dgettext(TEXT_DOMAIN,
1398 "Destroy and re-create the pool from\n\ta backup source.\n"));
1402 * zpool_import() is a contracted interface. Should be kept the same
1405 * Applications should use zpool_import_props() to import a pool with
1406 * new properties value to be set.
1409 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1412 nvlist_t *props = NULL;
1415 if (altroot != NULL) {
1416 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1417 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1418 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1422 if (nvlist_add_string(props,
1423 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1424 nvlist_add_string(props,
1425 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1427 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1428 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1433 ret = zpool_import_props(hdl, config, newname, props,
1441 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1447 uint64_t is_log = 0;
1449 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1453 (void) printf("\t%*s%s%s\n", indent, "", name,
1454 is_log ? " [log]" : "");
1456 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1457 &child, &children) != 0)
1460 for (c = 0; c < children; c++) {
1461 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
1462 print_vdev_tree(hdl, vname, child[c], indent + 2);
1468 * Import the given pool using the known configuration and a list of
1469 * properties to be set. The configuration should have come from
1470 * zpool_find_import(). The 'newname' parameters control whether the pool
1471 * is imported with a different name.
1474 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1475 nvlist_t *props, int flags)
1477 zfs_cmd_t zc = { 0 };
1478 zpool_rewind_policy_t policy;
1479 nvlist_t *nv = NULL;
1480 nvlist_t *nvinfo = NULL;
1481 nvlist_t *missing = NULL;
1488 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1491 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1492 "cannot import pool '%s'"), origname);
1494 if (newname != NULL) {
1495 if (!zpool_name_valid(hdl, B_FALSE, newname))
1496 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1497 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1499 thename = (char *)newname;
1506 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1508 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1511 if ((props = zpool_valid_proplist(hdl, origname,
1512 props, version, flags, errbuf)) == NULL) {
1514 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1520 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1522 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1525 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1529 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1534 zc.zc_cookie = flags;
1535 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1537 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1538 zcmd_free_nvlists(&zc);
1545 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1546 zpool_get_rewind_policy(config, &policy);
1552 * Dry-run failed, but we print out what success
1553 * looks like if we found a best txg
1555 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1556 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1562 if (newname == NULL)
1563 (void) snprintf(desc, sizeof (desc),
1564 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1567 (void) snprintf(desc, sizeof (desc),
1568 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1574 * Unsupported version.
1576 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1580 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1584 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1585 "one or more devices is read only"));
1586 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1590 if (nv && nvlist_lookup_nvlist(nv,
1591 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1592 nvlist_lookup_nvlist(nvinfo,
1593 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1594 (void) printf(dgettext(TEXT_DOMAIN,
1595 "The devices below are missing, use "
1596 "'-m' to import the pool anyway:\n"));
1597 print_vdev_tree(hdl, NULL, missing, 2);
1598 (void) printf("\n");
1600 (void) zpool_standard_error(hdl, error, desc);
1604 (void) zpool_standard_error(hdl, error, desc);
1608 (void) zpool_standard_error(hdl, error, desc);
1609 zpool_explain_recover(hdl,
1610 newname ? origname : thename, -error, nv);
1617 zpool_handle_t *zhp;
1620 * This should never fail, but play it safe anyway.
1622 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1624 else if (zhp != NULL)
1626 if (policy.zrp_request &
1627 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1628 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1629 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1635 zcmd_free_nvlists(&zc);
1645 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1647 zfs_cmd_t zc = { 0 };
1649 libzfs_handle_t *hdl = zhp->zpool_hdl;
1651 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1652 zc.zc_cookie = func;
1654 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
1655 (errno == ENOENT && func != POOL_SCAN_NONE))
1658 if (func == POOL_SCAN_SCRUB) {
1659 (void) snprintf(msg, sizeof (msg),
1660 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1661 } else if (func == POOL_SCAN_NONE) {
1662 (void) snprintf(msg, sizeof (msg),
1663 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1666 assert(!"unexpected result");
1669 if (errno == EBUSY) {
1671 pool_scan_stat_t *ps = NULL;
1674 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1675 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1676 (void) nvlist_lookup_uint64_array(nvroot,
1677 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1678 if (ps && ps->pss_func == POOL_SCAN_SCRUB)
1679 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1681 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1682 } else if (errno == ENOENT) {
1683 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1685 return (zpool_standard_error(hdl, errno, msg));
1690 * This provides a very minimal check whether a given string is likely a
1691 * c#t#d# style string. Users of this are expected to do their own
1692 * verification of the s# part.
1694 #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1]))
1697 * More elaborate version for ones which may start with "/dev/dsk/"
1701 ctd_check_path(char *str) {
1703 * If it starts with a slash, check the last component.
1705 if (str && str[0] == '/') {
1706 char *tmp = strrchr(str, '/');
1709 * If it ends in "/old", check the second-to-last
1710 * component of the string instead.
1712 if (tmp != str && strcmp(tmp, "/old") == 0) {
1713 for (tmp--; *tmp != '/'; tmp--)
1718 return (CTD_CHECK(str));
1722 * Find a vdev that matches the search criteria specified. We use the
1723 * the nvpair name to determine how we should look for the device.
1724 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1725 * spare; but FALSE if its an INUSE spare.
1728 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1729 boolean_t *l2cache, boolean_t *log)
1736 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1738 /* Nothing to look for */
1739 if (search == NULL || pair == NULL)
1742 /* Obtain the key we will use to search */
1743 srchkey = nvpair_name(pair);
1745 switch (nvpair_type(pair)) {
1746 case DATA_TYPE_UINT64:
1747 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1748 uint64_t srchval, theguid;
1750 verify(nvpair_value_uint64(pair, &srchval) == 0);
1751 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1753 if (theguid == srchval)
1758 case DATA_TYPE_STRING: {
1759 char *srchval, *val;
1761 verify(nvpair_value_string(pair, &srchval) == 0);
1762 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1766 * Search for the requested value. Special cases:
1768 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
1769 * "s0" or "s0/old". The "s0" part is hidden from the user,
1770 * but included in the string, so this matches around it.
1771 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1773 * Otherwise, all other searches are simple string compares.
1775 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
1776 ctd_check_path(val)) {
1777 uint64_t wholedisk = 0;
1779 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1782 int slen = strlen(srchval);
1783 int vlen = strlen(val);
1785 if (slen != vlen - 2)
1789 * make_leaf_vdev() should only set
1790 * wholedisk for ZPOOL_CONFIG_PATHs which
1791 * will include "/dev/dsk/", giving plenty of
1792 * room for the indices used next.
1797 * strings identical except trailing "s0"
1799 if (strcmp(&val[vlen - 2], "s0") == 0 &&
1800 strncmp(srchval, val, slen) == 0)
1804 * strings identical except trailing "s0/old"
1806 if (strcmp(&val[vlen - 6], "s0/old") == 0 &&
1807 strcmp(&srchval[slen - 4], "/old") == 0 &&
1808 strncmp(srchval, val, slen - 4) == 0)
1813 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
1814 char *type, *idx, *end, *p;
1815 uint64_t id, vdev_id;
1818 * Determine our vdev type, keeping in mind
1819 * that the srchval is composed of a type and
1820 * vdev id pair (i.e. mirror-4).
1822 if ((type = strdup(srchval)) == NULL)
1825 if ((p = strrchr(type, '-')) == NULL) {
1833 * If the types don't match then keep looking.
1835 if (strncmp(val, type, strlen(val)) != 0) {
1840 verify(strncmp(type, VDEV_TYPE_RAIDZ,
1841 strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1842 strncmp(type, VDEV_TYPE_MIRROR,
1843 strlen(VDEV_TYPE_MIRROR)) == 0);
1844 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
1848 vdev_id = strtoull(idx, &end, 10);
1855 * Now verify that we have the correct vdev id.
1864 if (strcmp(srchval, val) == 0)
1873 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1874 &child, &children) != 0)
1877 for (c = 0; c < children; c++) {
1878 if ((ret = vdev_to_nvlist_iter(child[c], search,
1879 avail_spare, l2cache, NULL)) != NULL) {
1881 * The 'is_log' value is only set for the toplevel
1882 * vdev, not the leaf vdevs. So we always lookup the
1883 * log device from the root of the vdev tree (where
1884 * 'log' is non-NULL).
1887 nvlist_lookup_uint64(child[c],
1888 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
1896 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1897 &child, &children) == 0) {
1898 for (c = 0; c < children; c++) {
1899 if ((ret = vdev_to_nvlist_iter(child[c], search,
1900 avail_spare, l2cache, NULL)) != NULL) {
1901 *avail_spare = B_TRUE;
1907 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1908 &child, &children) == 0) {
1909 for (c = 0; c < children; c++) {
1910 if ((ret = vdev_to_nvlist_iter(child[c], search,
1911 avail_spare, l2cache, NULL)) != NULL) {
1922 * Given a physical path (minus the "/devices" prefix), find the
1926 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
1927 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
1929 nvlist_t *search, *nvroot, *ret;
1931 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1932 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
1934 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1937 *avail_spare = B_FALSE;
1941 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1942 nvlist_free(search);
1948 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
1951 zpool_vdev_is_interior(const char *name)
1953 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1954 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
1960 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
1961 boolean_t *l2cache, boolean_t *log)
1963 char buf[MAXPATHLEN];
1965 nvlist_t *nvroot, *search, *ret;
1968 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1970 guid = strtoull(path, &end, 10);
1971 if (guid != 0 && *end == '\0') {
1972 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
1973 } else if (zpool_vdev_is_interior(path)) {
1974 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
1975 } else if (path[0] != '/') {
1976 (void) snprintf(buf, sizeof (buf), "%s%s", _PATH_DEV, path);
1977 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
1979 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
1982 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1985 *avail_spare = B_FALSE;
1989 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1990 nvlist_free(search);
1996 vdev_online(nvlist_t *nv)
2000 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2001 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2002 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2009 * Helper function for zpool_get_physpaths().
2012 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2013 size_t *bytes_written)
2015 size_t bytes_left, pos, rsz;
2019 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2021 return (EZFS_NODEVICE);
2023 pos = *bytes_written;
2024 bytes_left = physpath_size - pos;
2025 format = (pos == 0) ? "%s" : " %s";
2027 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2028 *bytes_written += rsz;
2030 if (rsz >= bytes_left) {
2031 /* if physpath was not copied properly, clear it */
2032 if (bytes_left != 0) {
2035 return (EZFS_NOSPC);
2041 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2042 size_t *rsz, boolean_t is_spare)
2047 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2048 return (EZFS_INVALCONFIG);
2050 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2052 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2053 * For a spare vdev, we only want to boot from the active
2058 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2061 return (EZFS_INVALCONFIG);
2064 if (vdev_online(nv)) {
2065 if ((ret = vdev_get_one_physpath(nv, physpath,
2066 phypath_size, rsz)) != 0)
2069 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2070 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2071 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2076 if (nvlist_lookup_nvlist_array(nv,
2077 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2078 return (EZFS_INVALCONFIG);
2080 for (i = 0; i < count; i++) {
2081 ret = vdev_get_physpaths(child[i], physpath,
2082 phypath_size, rsz, is_spare);
2083 if (ret == EZFS_NOSPC)
2088 return (EZFS_POOL_INVALARG);
2092 * Get phys_path for a root pool config.
2093 * Return 0 on success; non-zero on failure.
2096 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2099 nvlist_t *vdev_root;
2106 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2108 return (EZFS_INVALCONFIG);
2110 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2111 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2112 &child, &count) != 0)
2113 return (EZFS_INVALCONFIG);
2116 * root pool can not have EFI labeled disks and can only have
2117 * a single top-level vdev.
2119 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
2120 pool_uses_efi(vdev_root))
2121 return (EZFS_POOL_INVALARG);
2123 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2126 /* No online devices */
2128 return (EZFS_NODEVICE);
2134 * Get phys_path for a root pool
2135 * Return 0 on success; non-zero on failure.
2138 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2140 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2145 * If the device has being dynamically expanded then we need to relabel
2146 * the disk to use the new unallocated space.
2149 zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2152 char path[MAXPATHLEN];
2155 int (*_efi_use_whole_disk)(int);
2157 if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2158 "efi_use_whole_disk")) == NULL)
2161 (void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name);
2163 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2164 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2165 "relabel '%s': unable to open device"), name);
2166 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2170 * It's possible that we might encounter an error if the device
2171 * does not have any unallocated space left. If so, we simply
2172 * ignore that error and continue on.
2174 error = _efi_use_whole_disk(fd);
2176 if (error && error != VT_ENOSPC) {
2177 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2178 "relabel '%s': unable to read disk capacity"), name);
2179 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2186 * Bring the specified vdev online. The 'flags' parameter is a set of the
2187 * ZFS_ONLINE_* flags.
2190 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2191 vdev_state_t *newstate)
2193 zfs_cmd_t zc = { 0 };
2196 boolean_t avail_spare, l2cache, islog;
2197 libzfs_handle_t *hdl = zhp->zpool_hdl;
2199 if (flags & ZFS_ONLINE_EXPAND) {
2200 (void) snprintf(msg, sizeof (msg),
2201 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2203 (void) snprintf(msg, sizeof (msg),
2204 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2207 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2208 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2210 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2212 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2215 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2217 if (flags & ZFS_ONLINE_EXPAND ||
2218 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2219 char *pathname = NULL;
2220 uint64_t wholedisk = 0;
2222 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2224 verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
2228 * XXX - L2ARC 1.0 devices can't support expansion.
2231 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2232 "cannot expand cache devices"));
2233 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2237 pathname += strlen(DISK_ROOT) + 1;
2238 (void) zpool_relabel_disk(hdl, pathname);
2242 zc.zc_cookie = VDEV_STATE_ONLINE;
2245 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2246 if (errno == EINVAL) {
2247 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2248 "from this pool into a new one. Use '%s' "
2249 "instead"), "zpool detach");
2250 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2252 return (zpool_standard_error(hdl, errno, msg));
2255 *newstate = zc.zc_cookie;
2260 * Take the specified vdev offline
2263 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2265 zfs_cmd_t zc = { 0 };
2268 boolean_t avail_spare, l2cache;
2269 libzfs_handle_t *hdl = zhp->zpool_hdl;
2271 (void) snprintf(msg, sizeof (msg),
2272 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2274 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2275 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2277 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2279 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2282 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2284 zc.zc_cookie = VDEV_STATE_OFFLINE;
2285 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2287 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2294 * There are no other replicas of this device.
2296 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2300 * The log device has unplayed logs
2302 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2305 return (zpool_standard_error(hdl, errno, msg));
2310 * Mark the given vdev faulted.
2313 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2315 zfs_cmd_t zc = { 0 };
2317 libzfs_handle_t *hdl = zhp->zpool_hdl;
2319 (void) snprintf(msg, sizeof (msg),
2320 dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2322 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2324 zc.zc_cookie = VDEV_STATE_FAULTED;
2327 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2334 * There are no other replicas of this device.
2336 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2339 return (zpool_standard_error(hdl, errno, msg));
2345 * Mark the given vdev degraded.
2348 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2350 zfs_cmd_t zc = { 0 };
2352 libzfs_handle_t *hdl = zhp->zpool_hdl;
2354 (void) snprintf(msg, sizeof (msg),
2355 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
2357 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2359 zc.zc_cookie = VDEV_STATE_DEGRADED;
2362 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2365 return (zpool_standard_error(hdl, errno, msg));
2369 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2373 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2379 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2381 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2384 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2385 children == 2 && child[which] == tgt)
2388 for (c = 0; c < children; c++)
2389 if (is_replacing_spare(child[c], tgt, which))
2397 * Attach new_disk (fully described by nvroot) to old_disk.
2398 * If 'replacing' is specified, the new disk will replace the old one.
2401 zpool_vdev_attach(zpool_handle_t *zhp,
2402 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2404 zfs_cmd_t zc = { 0 };
2408 boolean_t avail_spare, l2cache, islog;
2413 nvlist_t *config_root;
2414 libzfs_handle_t *hdl = zhp->zpool_hdl;
2415 boolean_t rootpool = pool_is_bootable(zhp);
2418 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2419 "cannot replace %s with %s"), old_disk, new_disk);
2421 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2422 "cannot attach %s to %s"), new_disk, old_disk);
2425 * If this is a root pool, make sure that we're not attaching an
2426 * EFI labeled device.
2428 if (rootpool && pool_uses_efi(nvroot)) {
2429 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2430 "EFI labeled devices are not supported on root pools."));
2431 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2434 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2435 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2437 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2440 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2443 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2445 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2446 zc.zc_cookie = replacing;
2448 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2449 &child, &children) != 0 || children != 1) {
2450 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2451 "new device must be a single disk"));
2452 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2455 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2456 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2458 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2462 * If the target is a hot spare that has been swapped in, we can only
2463 * replace it with another hot spare.
2466 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2467 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2468 NULL) == NULL || !avail_spare) &&
2469 is_replacing_spare(config_root, tgt, 1)) {
2470 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2471 "can only be replaced by another hot spare"));
2473 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2478 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2481 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2483 zcmd_free_nvlists(&zc);
2488 * XXX need a better way to prevent user from
2489 * booting up a half-baked vdev.
2491 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2492 "sure to wait until resilver is done "
2493 "before rebooting.\n"));
2494 (void) fprintf(stderr, "\n");
2495 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "If "
2496 "you boot from pool '%s', you may need to update\n"
2497 "boot code on newly attached disk '%s'.\n\n"
2498 "Assuming you use GPT partitioning and 'da0' is "
2499 "your new boot disk\n"
2500 "you may use the following command:\n\n"
2501 "\tgpart bootcode -b /boot/pmbr -p "
2502 "/boot/gptzfsboot -i 1 da0\n\n"),
2503 zhp->zpool_name, new_disk);
2511 * Can't attach to or replace this type of vdev.
2514 uint64_t version = zpool_get_prop_int(zhp,
2515 ZPOOL_PROP_VERSION, NULL);
2518 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2519 "cannot replace a log with a spare"));
2520 else if (version >= SPA_VERSION_MULTI_REPLACE)
2521 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2522 "already in replacing/spare config; wait "
2523 "for completion or use 'zpool detach'"));
2525 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2526 "cannot replace a replacing device"));
2528 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2529 "can only attach to mirrors and top-level "
2532 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2537 * The new device must be a single disk.
2539 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2540 "new device must be a single disk"));
2541 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2545 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2547 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2552 * The new device is too small.
2554 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2555 "device is too small"));
2556 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2561 * The new device has a different alignment requirement.
2563 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2564 "devices have different sector alignment"));
2565 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2570 * The resulting top-level vdev spec won't fit in the label.
2572 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2576 (void) zpool_standard_error(hdl, errno, msg);
2583 * Detach the specified device.
2586 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2588 zfs_cmd_t zc = { 0 };
2591 boolean_t avail_spare, l2cache;
2592 libzfs_handle_t *hdl = zhp->zpool_hdl;
2594 (void) snprintf(msg, sizeof (msg),
2595 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2597 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2598 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2600 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2603 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2606 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2608 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2610 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2617 * Can't detach from this type of vdev.
2619 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2620 "applicable to mirror and replacing vdevs"));
2621 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2626 * There are no other replicas of this device.
2628 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2632 (void) zpool_standard_error(hdl, errno, msg);
2639 * Find a mirror vdev in the source nvlist.
2641 * The mchild array contains a list of disks in one of the top-level mirrors
2642 * of the source pool. The schild array contains a list of disks that the
2643 * user specified on the command line. We loop over the mchild array to
2644 * see if any entry in the schild array matches.
2646 * If a disk in the mchild array is found in the schild array, we return
2647 * the index of that entry. Otherwise we return -1.
2650 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2651 nvlist_t **schild, uint_t schildren)
2655 for (mc = 0; mc < mchildren; mc++) {
2657 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2658 mchild[mc], B_FALSE);
2660 for (sc = 0; sc < schildren; sc++) {
2661 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2662 schild[sc], B_FALSE);
2663 boolean_t result = (strcmp(mpath, spath) == 0);
2679 * Split a mirror pool. If newroot points to null, then a new nvlist
2680 * is generated and it is the responsibility of the caller to free it.
2683 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2684 nvlist_t *props, splitflags_t flags)
2686 zfs_cmd_t zc = { 0 };
2688 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2689 nvlist_t **varray = NULL, *zc_props = NULL;
2690 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2691 libzfs_handle_t *hdl = zhp->zpool_hdl;
2693 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2696 (void) snprintf(msg, sizeof (msg),
2697 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2699 if (!zpool_name_valid(hdl, B_FALSE, newname))
2700 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2702 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2703 (void) fprintf(stderr, gettext("Internal error: unable to "
2704 "retrieve pool configuration\n"));
2708 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2710 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2713 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2714 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2715 props, vers, flags, msg)) == NULL)
2719 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2721 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2722 "Source pool is missing vdev tree"));
2724 nvlist_free(zc_props);
2728 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2731 if (*newroot == NULL ||
2732 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2733 &newchild, &newchildren) != 0)
2736 for (c = 0; c < children; c++) {
2737 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2739 nvlist_t **mchild, *vdev;
2744 * Unlike cache & spares, slogs are stored in the
2745 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2747 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2749 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2751 if (is_log || is_hole) {
2753 * Create a hole vdev and put it in the config.
2755 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2757 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2758 VDEV_TYPE_HOLE) != 0)
2760 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2765 varray[vcount++] = vdev;
2769 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
2771 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2772 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2773 "Source pool must be composed only of mirrors\n"));
2774 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2778 verify(nvlist_lookup_nvlist_array(child[c],
2779 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2781 /* find or add an entry for this top-level vdev */
2782 if (newchildren > 0 &&
2783 (entry = find_vdev_entry(zhp, mchild, mchildren,
2784 newchild, newchildren)) >= 0) {
2785 /* We found a disk that the user specified. */
2786 vdev = mchild[entry];
2789 /* User didn't specify a disk for this vdev. */
2790 vdev = mchild[mchildren - 1];
2793 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
2797 /* did we find every disk the user specified? */
2798 if (found != newchildren) {
2799 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
2800 "include at most one disk from each mirror"));
2801 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2805 /* Prepare the nvlist for populating. */
2806 if (*newroot == NULL) {
2807 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
2810 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
2811 VDEV_TYPE_ROOT) != 0)
2814 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
2817 /* Add all the children we found */
2818 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
2819 lastlog == 0 ? vcount : lastlog) != 0)
2823 * If we're just doing a dry run, exit now with success.
2826 memory_err = B_FALSE;
2831 /* now build up the config list & call the ioctl */
2832 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
2835 if (nvlist_add_nvlist(newconfig,
2836 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
2837 nvlist_add_string(newconfig,
2838 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
2839 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
2843 * The new pool is automatically part of the namespace unless we
2844 * explicitly export it.
2847 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
2848 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2849 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
2850 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
2852 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
2855 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
2856 retval = zpool_standard_error(hdl, errno, msg);
2861 memory_err = B_FALSE;
2864 if (varray != NULL) {
2867 for (v = 0; v < vcount; v++)
2868 nvlist_free(varray[v]);
2871 zcmd_free_nvlists(&zc);
2873 nvlist_free(zc_props);
2875 nvlist_free(newconfig);
2877 nvlist_free(*newroot);
2885 return (no_memory(hdl));
2891 * Remove the given device. Currently, this is supported only for hot spares
2892 * and level 2 cache devices.
2895 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
2897 zfs_cmd_t zc = { 0 };
2900 boolean_t avail_spare, l2cache, islog;
2901 libzfs_handle_t *hdl = zhp->zpool_hdl;
2904 (void) snprintf(msg, sizeof (msg),
2905 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
2907 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2908 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2910 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2912 * XXX - this should just go away.
2914 if (!avail_spare && !l2cache && !islog) {
2915 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2916 "only inactive hot spares, cache, top-level, "
2917 "or log devices can be removed"));
2918 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2921 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
2922 if (islog && version < SPA_VERSION_HOLES) {
2923 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2924 "pool must be upgrade to support log removal"));
2925 return (zfs_error(hdl, EZFS_BADVERSION, msg));
2928 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2930 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
2933 return (zpool_standard_error(hdl, errno, msg));
2937 * Clear the errors for the pool, or the particular device if specified.
2940 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
2942 zfs_cmd_t zc = { 0 };
2945 zpool_rewind_policy_t policy;
2946 boolean_t avail_spare, l2cache;
2947 libzfs_handle_t *hdl = zhp->zpool_hdl;
2948 nvlist_t *nvi = NULL;
2952 (void) snprintf(msg, sizeof (msg),
2953 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2956 (void) snprintf(msg, sizeof (msg),
2957 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2960 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2962 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
2963 &l2cache, NULL)) == 0)
2964 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2967 * Don't allow error clearing for hot spares. Do allow
2968 * error clearing for l2cache devices.
2971 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2973 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
2977 zpool_get_rewind_policy(rewindnvl, &policy);
2978 zc.zc_cookie = policy.zrp_request;
2980 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
2983 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
2986 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
2988 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
2989 zcmd_free_nvlists(&zc);
2994 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
2995 errno != EPERM && errno != EACCES)) {
2996 if (policy.zrp_request &
2997 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2998 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
2999 zpool_rewind_exclaim(hdl, zc.zc_name,
3000 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
3004 zcmd_free_nvlists(&zc);
3008 zcmd_free_nvlists(&zc);
3009 return (zpool_standard_error(hdl, errno, msg));
3013 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3016 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3018 zfs_cmd_t zc = { 0 };
3020 libzfs_handle_t *hdl = zhp->zpool_hdl;
3022 (void) snprintf(msg, sizeof (msg),
3023 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3026 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3028 zc.zc_cookie = ZPOOL_NO_REWIND;
3030 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3033 return (zpool_standard_error(hdl, errno, msg));
3037 * Change the GUID for a pool.
3040 zpool_reguid(zpool_handle_t *zhp)
3043 libzfs_handle_t *hdl = zhp->zpool_hdl;
3044 zfs_cmd_t zc = { 0 };
3046 (void) snprintf(msg, sizeof (msg),
3047 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3049 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3050 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3053 return (zpool_standard_error(hdl, errno, msg));
3057 * Convert from a devid string to a path.
3060 devid_to_path(char *devid_str)
3065 devid_nmlist_t *list = NULL;
3068 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3071 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3073 devid_str_free(minor);
3079 if ((path = strdup(list[0].devname)) == NULL)
3082 devid_free_nmlist(list);
3088 * Convert from a path to a devid string.
3091 path_to_devid(const char *path)
3097 if ((fd = open(path, O_RDONLY)) < 0)
3102 if (devid_get(fd, &devid) == 0) {
3103 if (devid_get_minor_name(fd, &minor) == 0)
3104 ret = devid_str_encode(devid, minor);
3106 devid_str_free(minor);
3115 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3116 * ignore any failure here, since a common case is for an unprivileged user to
3117 * type 'zpool status', and we'll display the correct information anyway.
3120 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3122 zfs_cmd_t zc = { 0 };
3124 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3125 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3126 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3129 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3133 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3134 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3135 * We also check if this is a whole disk, in which case we strip off the
3136 * trailing 's0' slice name.
3138 * This routine is also responsible for identifying when disks have been
3139 * reconfigured in a new location. The kernel will have opened the device by
3140 * devid, but the path will still refer to the old location. To catch this, we
3141 * first do a path -> devid translation (which is fast for the common case). If
3142 * the devid matches, we're done. If not, we do a reverse devid -> path
3143 * translation and issue the appropriate ioctl() to update the path of the vdev.
3144 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3148 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3159 have_stats = nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3160 (uint64_t **)&vs, &vsc) == 0;
3161 have_path = nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0;
3164 * If the device is not currently present, assume it will not
3165 * come back at the same device path. Display the device by GUID.
3167 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3168 have_path && have_stats && vs->vs_state <= VDEV_STATE_CANT_OPEN) {
3169 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3171 (void) snprintf(buf, sizeof (buf), "%llu",
3172 (u_longlong_t)value);
3174 } else if (have_path) {
3177 * If the device is dead (faulted, offline, etc) then don't
3178 * bother opening it. Otherwise we may be forcing the user to
3179 * open a misbehaving device, which can have undesirable
3182 if ((have_stats == 0 ||
3183 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3185 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3187 * Determine if the current path is correct.
3189 char *newdevid = path_to_devid(path);
3191 if (newdevid == NULL ||
3192 strcmp(devid, newdevid) != 0) {
3195 if ((newpath = devid_to_path(devid)) != NULL) {
3197 * Update the path appropriately.
3199 set_path(zhp, nv, newpath);
3200 if (nvlist_add_string(nv,
3201 ZPOOL_CONFIG_PATH, newpath) == 0)
3202 verify(nvlist_lookup_string(nv,
3210 devid_str_free(newdevid);
3214 if (strncmp(path, "/dev/dsk/", 9) == 0)
3217 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3218 &value) == 0 && value) {
3219 int pathlen = strlen(path);
3220 char *tmp = zfs_strdup(hdl, path);
3223 * If it starts with c#, and ends with "s0", chop
3224 * the "s0" off, or if it ends with "s0/old", remove
3225 * the "s0" from the middle.
3227 if (CTD_CHECK(tmp)) {
3228 if (strcmp(&tmp[pathlen - 2], "s0") == 0) {
3229 tmp[pathlen - 2] = '\0';
3230 } else if (pathlen > 6 &&
3231 strcmp(&tmp[pathlen - 6], "s0/old") == 0) {
3232 (void) strcpy(&tmp[pathlen - 6],
3239 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3240 path += sizeof(_PATH_DEV) - 1;
3243 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3246 * If it's a raidz device, we need to stick in the parity level.
3248 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3249 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3251 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3252 (u_longlong_t)value);
3257 * We identify each top-level vdev by using a <type-id>
3258 * naming convention.
3263 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3265 (void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3271 return (zfs_strdup(hdl, path));
3275 zbookmark_compare(const void *a, const void *b)
3277 return (memcmp(a, b, sizeof (zbookmark_t)));
3281 * Retrieve the persistent error log, uniquify the members, and return to the
3285 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3287 zfs_cmd_t zc = { 0 };
3289 zbookmark_t *zb = NULL;
3293 * Retrieve the raw error list from the kernel. If the number of errors
3294 * has increased, allocate more space and continue until we get the
3297 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3301 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3302 count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
3304 zc.zc_nvlist_dst_size = count;
3305 (void) strcpy(zc.zc_name, zhp->zpool_name);
3307 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3309 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3310 if (errno == ENOMEM) {
3311 count = zc.zc_nvlist_dst_size;
3312 if ((zc.zc_nvlist_dst = (uintptr_t)
3313 zfs_alloc(zhp->zpool_hdl, count *
3314 sizeof (zbookmark_t))) == (uintptr_t)NULL)
3325 * Sort the resulting bookmarks. This is a little confusing due to the
3326 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3327 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3328 * _not_ copied as part of the process. So we point the start of our
3329 * array appropriate and decrement the total number of elements.
3331 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
3332 zc.zc_nvlist_dst_size;
3333 count -= zc.zc_nvlist_dst_size;
3335 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
3337 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3340 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3342 for (i = 0; i < count; i++) {
3345 /* ignoring zb_blkid and zb_level for now */
3346 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3347 zb[i-1].zb_object == zb[i].zb_object)
3350 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3352 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3353 zb[i].zb_objset) != 0) {
3357 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3358 zb[i].zb_object) != 0) {
3362 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3369 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3373 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3374 return (no_memory(zhp->zpool_hdl));
3378 * Upgrade a ZFS pool to the latest on-disk version.
3381 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3383 zfs_cmd_t zc = { 0 };
3384 libzfs_handle_t *hdl = zhp->zpool_hdl;
3386 (void) strcpy(zc.zc_name, zhp->zpool_name);
3387 zc.zc_cookie = new_version;
3389 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3390 return (zpool_standard_error_fmt(hdl, errno,
3391 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3397 zpool_set_history_str(const char *subcommand, int argc, char **argv,
3402 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
3403 for (i = 1; i < argc; i++) {
3404 if (strlen(history_str) + 1 + strlen(argv[i]) >
3407 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
3408 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
3413 * Stage command history for logging.
3416 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
3418 if (history_str == NULL)
3421 if (strlen(history_str) > HIS_MAX_RECORD_LEN)
3424 if (hdl->libzfs_log_str != NULL)
3425 free(hdl->libzfs_log_str);
3427 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
3428 return (no_memory(hdl));
3434 * Perform ioctl to get some command history of a pool.
3436 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3437 * logical offset of the history buffer to start reading from.
3439 * Upon return, 'off' is the next logical offset to read from and
3440 * 'len' is the actual amount of bytes read into 'buf'.
3443 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3445 zfs_cmd_t zc = { 0 };
3446 libzfs_handle_t *hdl = zhp->zpool_hdl;
3448 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3450 zc.zc_history = (uint64_t)(uintptr_t)buf;
3451 zc.zc_history_len = *len;
3452 zc.zc_history_offset = *off;
3454 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3457 return (zfs_error_fmt(hdl, EZFS_PERM,
3458 dgettext(TEXT_DOMAIN,
3459 "cannot show history for pool '%s'"),
3462 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3463 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3464 "'%s'"), zhp->zpool_name));
3466 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3467 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3468 "'%s', pool must be upgraded"), zhp->zpool_name));
3470 return (zpool_standard_error_fmt(hdl, errno,
3471 dgettext(TEXT_DOMAIN,
3472 "cannot get history for '%s'"), zhp->zpool_name));
3476 *len = zc.zc_history_len;
3477 *off = zc.zc_history_offset;
3483 * Process the buffer of nvlists, unpacking and storing each nvlist record
3484 * into 'records'. 'leftover' is set to the number of bytes that weren't
3485 * processed as there wasn't a complete record.
3488 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3489 nvlist_t ***records, uint_t *numrecords)
3495 while (bytes_read > sizeof (reclen)) {
3497 /* get length of packed record (stored as little endian) */
3498 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3499 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3501 if (bytes_read < sizeof (reclen) + reclen)
3505 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3507 bytes_read -= sizeof (reclen) + reclen;
3508 buf += sizeof (reclen) + reclen;
3510 /* add record to nvlist array */
3512 if (ISP2(*numrecords + 1)) {
3513 *records = realloc(*records,
3514 *numrecords * 2 * sizeof (nvlist_t *));
3516 (*records)[*numrecords - 1] = nv;
3519 *leftover = bytes_read;
3523 #define HIS_BUF_LEN (128*1024)
3526 * Retrieve the command history of a pool.
3529 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3531 char buf[HIS_BUF_LEN];
3533 nvlist_t **records = NULL;
3534 uint_t numrecords = 0;
3538 uint64_t bytes_read = sizeof (buf);
3541 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3544 /* if nothing else was read in, we're at EOF, just return */
3548 if ((err = zpool_history_unpack(buf, bytes_read,
3549 &leftover, &records, &numrecords)) != 0)
3557 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3558 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3559 records, numrecords) == 0);
3561 for (i = 0; i < numrecords; i++)
3562 nvlist_free(records[i]);
3569 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3570 char *pathname, size_t len)
3572 zfs_cmd_t zc = { 0 };
3573 boolean_t mounted = B_FALSE;
3574 char *mntpnt = NULL;
3575 char dsname[MAXNAMELEN];
3578 /* special case for the MOS */
3579 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
3583 /* get the dataset's name */
3584 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3586 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3587 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3588 /* just write out a path of two object numbers */
3589 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3593 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3595 /* find out if the dataset is mounted */
3596 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3598 /* get the corrupted object's path */
3599 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3601 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3604 (void) snprintf(pathname, len, "%s%s", mntpnt,
3607 (void) snprintf(pathname, len, "%s:%s",
3608 dsname, zc.zc_value);
3611 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
3618 * Read the EFI label from the config, if a label does not exist then
3619 * pass back the error to the caller. If the caller has passed a non-NULL
3620 * diskaddr argument then we set it to the starting address of the EFI
3624 read_efi_label(nvlist_t *config, diskaddr_t *sb)
3628 char diskname[MAXPATHLEN];
3631 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3634 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
3635 strrchr(path, '/'));
3636 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
3637 struct dk_gpt *vtoc;
3639 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3641 *sb = vtoc->efi_parts[0].p_start;
3650 * determine where a partition starts on a disk in the current
3654 find_start_block(nvlist_t *config)
3658 diskaddr_t sb = MAXOFFSET_T;
3661 if (nvlist_lookup_nvlist_array(config,
3662 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3663 if (nvlist_lookup_uint64(config,
3664 ZPOOL_CONFIG_WHOLE_DISK,
3665 &wholedisk) != 0 || !wholedisk) {
3666 return (MAXOFFSET_T);
3668 if (read_efi_label(config, &sb) < 0)
3673 for (c = 0; c < children; c++) {
3674 sb = find_start_block(child[c]);
3675 if (sb != MAXOFFSET_T) {
3679 return (MAXOFFSET_T);
3684 * Label an individual disk. The name provided is the short name,
3685 * stripped of any leading /dev path.
3688 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name)
3691 char path[MAXPATHLEN];
3692 struct dk_gpt *vtoc;
3694 size_t resv = EFI_MIN_RESV_SIZE;
3695 uint64_t slice_size;
3696 diskaddr_t start_block;
3699 /* prepare an error message just in case */
3700 (void) snprintf(errbuf, sizeof (errbuf),
3701 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3706 if (pool_is_bootable(zhp)) {
3707 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3708 "EFI labeled devices are not supported on root "
3710 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3713 verify(nvlist_lookup_nvlist(zhp->zpool_config,
3714 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3716 if (zhp->zpool_start_block == 0)
3717 start_block = find_start_block(nvroot);
3719 start_block = zhp->zpool_start_block;
3720 zhp->zpool_start_block = start_block;
3723 start_block = NEW_START_BLOCK;
3726 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
3729 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
3731 * This shouldn't happen. We've long since verified that this
3732 * is a valid device.
3735 dgettext(TEXT_DOMAIN, "unable to open device"));
3736 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
3739 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
3741 * The only way this can fail is if we run out of memory, or we
3742 * were unable to read the disk's capacity
3744 if (errno == ENOMEM)
3745 (void) no_memory(hdl);
3748 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3749 "unable to read disk capacity"), name);
3751 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
3754 slice_size = vtoc->efi_last_u_lba + 1;
3755 slice_size -= EFI_MIN_RESV_SIZE;
3756 if (start_block == MAXOFFSET_T)
3757 start_block = NEW_START_BLOCK;
3758 slice_size -= start_block;
3760 vtoc->efi_parts[0].p_start = start_block;
3761 vtoc->efi_parts[0].p_size = slice_size;
3764 * Why we use V_USR: V_BACKUP confuses users, and is considered
3765 * disposable by some EFI utilities (since EFI doesn't have a backup
3766 * slice). V_UNASSIGNED is supposed to be used only for zero size
3767 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
3768 * etc. were all pretty specific. V_USR is as close to reality as we
3769 * can get, in the absence of V_OTHER.
3771 vtoc->efi_parts[0].p_tag = V_USR;
3772 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
3774 vtoc->efi_parts[8].p_start = slice_size + start_block;
3775 vtoc->efi_parts[8].p_size = resv;
3776 vtoc->efi_parts[8].p_tag = V_RESERVED;
3778 if (efi_write(fd, vtoc) != 0) {
3780 * Some block drivers (like pcata) may not support EFI
3781 * GPT labels. Print out a helpful error message dir-
3782 * ecting the user to manually label the disk and give
3788 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3789 "try using fdisk(1M) and then provide a specific slice"));
3790 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3800 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
3806 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
3807 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
3808 strcmp(type, VDEV_TYPE_FILE) == 0 ||
3809 strcmp(type, VDEV_TYPE_LOG) == 0 ||
3810 strcmp(type, VDEV_TYPE_HOLE) == 0 ||
3811 strcmp(type, VDEV_TYPE_MISSING) == 0) {
3812 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3813 "vdev type '%s' is not supported"), type);
3814 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
3817 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
3818 &child, &children) == 0) {
3819 for (c = 0; c < children; c++) {
3820 if (!supported_dump_vdev_type(hdl, child[c], errbuf))
3828 * check if this zvol is allowable for use as a dump device; zero if
3829 * it is, > 0 if it isn't, < 0 if it isn't a zvol
3832 zvol_check_dump_config(char *arg)
3834 zpool_handle_t *zhp = NULL;
3835 nvlist_t *config, *nvroot;
3839 libzfs_handle_t *hdl;
3841 char poolname[ZPOOL_MAXNAMELEN];
3842 int pathlen = strlen(ZVOL_FULL_DEV_DIR);
3845 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
3849 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3850 "dump is not supported on device '%s'"), arg);
3852 if ((hdl = libzfs_init()) == NULL)
3854 libzfs_print_on_error(hdl, B_TRUE);
3856 volname = arg + pathlen;
3858 /* check the configuration of the pool */
3859 if ((p = strchr(volname, '/')) == NULL) {
3860 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3861 "malformed dataset name"));
3862 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3864 } else if (p - volname >= ZFS_MAXNAMELEN) {
3865 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3866 "dataset name is too long"));
3867 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
3870 (void) strncpy(poolname, volname, p - volname);
3871 poolname[p - volname] = '\0';
3874 if ((zhp = zpool_open(hdl, poolname)) == NULL) {
3875 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3876 "could not open pool '%s'"), poolname);
3877 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
3880 config = zpool_get_config(zhp, NULL);
3881 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3883 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3884 "could not obtain vdev configuration for '%s'"), poolname);
3885 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
3889 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3890 &top, &toplevels) == 0);
3891 if (toplevels != 1) {
3892 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3893 "'%s' has multiple top level vdevs"), poolname);
3894 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
3898 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {