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) 2012 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 case ZPOOL_PROP_EXPANDSZ:
305 (void) zfs_nicenum(intval, buf, len);
308 case ZPOOL_PROP_CAPACITY:
309 (void) snprintf(buf, len, "%llu%%",
310 (u_longlong_t)intval);
313 case ZPOOL_PROP_DEDUPRATIO:
314 (void) snprintf(buf, len, "%llu.%02llux",
315 (u_longlong_t)(intval / 100),
316 (u_longlong_t)(intval % 100));
319 case ZPOOL_PROP_HEALTH:
320 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
321 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
322 verify(nvlist_lookup_uint64_array(nvroot,
323 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
326 (void) strlcpy(buf, zpool_state_to_name(intval,
330 (void) snprintf(buf, len, "%llu", intval);
334 case PROP_TYPE_INDEX:
335 intval = zpool_get_prop_int(zhp, prop, &src);
336 if (zpool_prop_index_to_string(prop, intval, &strval)
339 (void) strlcpy(buf, strval, len);
353 * Check if the bootfs name has the same pool name as it is set to.
354 * Assuming bootfs is a valid dataset name.
357 bootfs_name_valid(const char *pool, char *bootfs)
359 int len = strlen(pool);
361 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
364 if (strncmp(pool, bootfs, len) == 0 &&
365 (bootfs[len] == '/' || bootfs[len] == '\0'))
372 * Inspect the configuration to determine if any of the devices contain
376 pool_uses_efi(nvlist_t *config)
382 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
383 &child, &children) != 0)
384 return (read_efi_label(config, NULL) >= 0);
386 for (c = 0; c < children; c++) {
387 if (pool_uses_efi(child[c]))
395 zpool_is_bootable(zpool_handle_t *zhp)
397 char bootfs[ZPOOL_MAXNAMELEN];
399 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
400 sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
401 sizeof (bootfs)) != 0);
406 * Given an nvlist of zpool properties to be set, validate that they are
407 * correct, and parse any numeric properties (index, boolean, etc) if they are
408 * specified as strings.
411 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
412 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
420 struct stat64 statbuf;
424 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
425 (void) no_memory(hdl);
430 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
431 const char *propname = nvpair_name(elem);
434 * Make sure this property is valid and applies to this type.
436 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
437 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
438 "invalid property '%s'"), propname);
439 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
443 if (zpool_prop_readonly(prop)) {
444 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
445 "is readonly"), propname);
446 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
450 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
451 &strval, &intval, errbuf) != 0)
455 * Perform additional checking for specific properties.
458 case ZPOOL_PROP_VERSION:
459 if (intval < version || intval > SPA_VERSION) {
460 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
461 "property '%s' number %d is invalid."),
463 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
468 case ZPOOL_PROP_BOOTFS:
469 if (flags.create || flags.import) {
470 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
471 "property '%s' cannot be set at creation "
472 "or import time"), propname);
473 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
477 if (version < SPA_VERSION_BOOTFS) {
478 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
479 "pool must be upgraded to support "
480 "'%s' property"), propname);
481 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
486 * bootfs property value has to be a dataset name and
487 * the dataset has to be in the same pool as it sets to.
489 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
491 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
492 "is an invalid name"), strval);
493 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
497 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
498 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
499 "could not open pool '%s'"), poolname);
500 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
503 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
504 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
508 * bootfs property cannot be set on a disk which has
511 if (pool_uses_efi(nvroot)) {
512 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
513 "property '%s' not supported on "
514 "EFI labeled devices"), propname);
515 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
523 case ZPOOL_PROP_ALTROOT:
524 if (!flags.create && !flags.import) {
525 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
526 "property '%s' can only be set during pool "
527 "creation or import"), propname);
528 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
532 if (strval[0] != '/') {
533 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
534 "bad alternate root '%s'"), strval);
535 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
540 case ZPOOL_PROP_CACHEFILE:
541 if (strval[0] == '\0')
544 if (strcmp(strval, "none") == 0)
547 if (strval[0] != '/') {
548 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
549 "property '%s' must be empty, an "
550 "absolute path, or 'none'"), propname);
551 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
555 slash = strrchr(strval, '/');
557 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
558 strcmp(slash, "/..") == 0) {
559 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
560 "'%s' is not a valid file"), strval);
561 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
567 if (strval[0] != '\0' &&
568 (stat64(strval, &statbuf) != 0 ||
569 !S_ISDIR(statbuf.st_mode))) {
570 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
571 "'%s' is not a valid directory"),
573 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
580 case ZPOOL_PROP_COMMENT:
581 for (check = strval; *check != '\0'; check++) {
582 if (!isprint(*check)) {
584 dgettext(TEXT_DOMAIN,
585 "comment may only have printable "
587 (void) zfs_error(hdl, EZFS_BADPROP,
592 if (strlen(strval) > ZPROP_MAX_COMMENT) {
593 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
594 "comment must not exceed %d characters"),
596 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
600 case ZPOOL_PROP_READONLY:
602 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
603 "property '%s' can only be set at "
604 "import time"), propname);
605 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
614 nvlist_free(retprops);
619 * Set zpool property : propname=propval.
622 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
624 zfs_cmd_t zc = { 0 };
627 nvlist_t *nvl = NULL;
630 prop_flags_t flags = { 0 };
632 (void) snprintf(errbuf, sizeof (errbuf),
633 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
636 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
637 return (no_memory(zhp->zpool_hdl));
639 if (nvlist_add_string(nvl, propname, propval) != 0) {
641 return (no_memory(zhp->zpool_hdl));
644 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
645 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
646 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
655 * Execute the corresponding ioctl() to set this property.
657 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
659 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
664 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
666 zcmd_free_nvlists(&zc);
670 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
672 (void) zpool_props_refresh(zhp);
678 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
680 libzfs_handle_t *hdl = zhp->zpool_hdl;
682 char buf[ZFS_MAXPROPLEN];
684 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
687 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
692 if (entry->pl_prop != ZPROP_INVAL &&
693 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
695 if (strlen(buf) > entry->pl_width)
696 entry->pl_width = strlen(buf);
705 * Don't start the slice at the default block of 34; many storage
706 * devices will use a stripe width of 128k, so start there instead.
708 #define NEW_START_BLOCK 256
711 * Validate the given pool name, optionally putting an extended error message in
715 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
721 ret = pool_namecheck(pool, &why, &what);
724 * The rules for reserved pool names were extended at a later point.
725 * But we need to support users with existing pools that may now be
726 * invalid. So we only check for this expanded set of names during a
727 * create (or import), and only in userland.
729 if (ret == 0 && !isopen &&
730 (strncmp(pool, "mirror", 6) == 0 ||
731 strncmp(pool, "raidz", 5) == 0 ||
732 strncmp(pool, "spare", 5) == 0 ||
733 strcmp(pool, "log") == 0)) {
736 dgettext(TEXT_DOMAIN, "name is reserved"));
744 case NAME_ERR_TOOLONG:
746 dgettext(TEXT_DOMAIN, "name is too long"));
749 case NAME_ERR_INVALCHAR:
751 dgettext(TEXT_DOMAIN, "invalid character "
752 "'%c' in pool name"), what);
755 case NAME_ERR_NOLETTER:
756 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
757 "name must begin with a letter"));
760 case NAME_ERR_RESERVED:
761 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
762 "name is reserved"));
765 case NAME_ERR_DISKLIKE:
766 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
767 "pool name is reserved"));
770 case NAME_ERR_LEADING_SLASH:
771 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
772 "leading slash in name"));
775 case NAME_ERR_EMPTY_COMPONENT:
776 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
777 "empty component in name"));
780 case NAME_ERR_TRAILING_SLASH:
781 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
782 "trailing slash in name"));
785 case NAME_ERR_MULTIPLE_AT:
786 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
787 "multiple '@' delimiters in name"));
799 * Open a handle to the given pool, even if the pool is currently in the FAULTED
803 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
809 * Make sure the pool name is valid.
811 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
812 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
813 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
818 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
821 zhp->zpool_hdl = hdl;
822 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
824 if (zpool_refresh_stats(zhp, &missing) != 0) {
830 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
831 (void) zfs_error_fmt(hdl, EZFS_NOENT,
832 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
841 * Like the above, but silent on error. Used when iterating over pools (because
842 * the configuration cache may be out of date).
845 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
850 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
853 zhp->zpool_hdl = hdl;
854 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
856 if (zpool_refresh_stats(zhp, &missing) != 0) {
872 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
876 zpool_open(libzfs_handle_t *hdl, const char *pool)
880 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
883 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
884 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
885 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
894 * Close the handle. Simply frees the memory associated with the handle.
897 zpool_close(zpool_handle_t *zhp)
899 if (zhp->zpool_config)
900 nvlist_free(zhp->zpool_config);
901 if (zhp->zpool_old_config)
902 nvlist_free(zhp->zpool_old_config);
903 if (zhp->zpool_props)
904 nvlist_free(zhp->zpool_props);
909 * Return the name of the pool.
912 zpool_get_name(zpool_handle_t *zhp)
914 return (zhp->zpool_name);
919 * Return the state of the pool (ACTIVE or UNAVAILABLE)
922 zpool_get_state(zpool_handle_t *zhp)
924 return (zhp->zpool_state);
928 * Create the named pool, using the provided vdev list. It is assumed
929 * that the consumer has already validated the contents of the nvlist, so we
930 * don't have to worry about error semantics.
933 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
934 nvlist_t *props, nvlist_t *fsprops)
936 zfs_cmd_t zc = { 0 };
937 nvlist_t *zc_fsprops = NULL;
938 nvlist_t *zc_props = NULL;
943 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
944 "cannot create '%s'"), pool);
946 if (!zpool_name_valid(hdl, B_FALSE, pool))
947 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
949 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
953 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
955 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
956 SPA_VERSION_1, flags, msg)) == NULL) {
965 zoned = ((nvlist_lookup_string(fsprops,
966 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
967 strcmp(zonestr, "on") == 0);
969 if ((zc_fsprops = zfs_valid_proplist(hdl,
970 ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
974 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
977 if (nvlist_add_nvlist(zc_props,
978 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
983 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
986 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
988 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
990 zcmd_free_nvlists(&zc);
991 nvlist_free(zc_props);
992 nvlist_free(zc_fsprops);
997 * This can happen if the user has specified the same
998 * device multiple times. We can't reliably detect this
999 * until we try to add it and see we already have a
1002 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1003 "one or more vdevs refer to the same device"));
1004 return (zfs_error(hdl, EZFS_BADDEV, msg));
1008 * This occurs when one of the devices is below
1009 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1010 * device was the problem device since there's no
1011 * reliable way to determine device size from userland.
1016 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1018 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1019 "one or more devices is less than the "
1020 "minimum size (%s)"), buf);
1022 return (zfs_error(hdl, EZFS_BADDEV, msg));
1025 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1026 "one or more devices is out of space"));
1027 return (zfs_error(hdl, EZFS_BADDEV, msg));
1030 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1031 "cache device must be a disk or disk slice"));
1032 return (zfs_error(hdl, EZFS_BADDEV, msg));
1035 return (zpool_standard_error(hdl, errno, msg));
1040 * If this is an alternate root pool, then we automatically set the
1041 * mountpoint of the root dataset to be '/'.
1043 if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
1047 verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
1048 verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1055 zcmd_free_nvlists(&zc);
1056 nvlist_free(zc_props);
1057 nvlist_free(zc_fsprops);
1062 * Destroy the given pool. It is up to the caller to ensure that there are no
1063 * datasets left in the pool.
1066 zpool_destroy(zpool_handle_t *zhp)
1068 zfs_cmd_t zc = { 0 };
1069 zfs_handle_t *zfp = NULL;
1070 libzfs_handle_t *hdl = zhp->zpool_hdl;
1073 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1074 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1077 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1079 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1080 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1081 "cannot destroy '%s'"), zhp->zpool_name);
1083 if (errno == EROFS) {
1084 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1085 "one or more devices is read only"));
1086 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1088 (void) zpool_standard_error(hdl, errno, msg);
1097 remove_mountpoint(zfp);
1105 * Add the given vdevs to the pool. The caller must have already performed the
1106 * necessary verification to ensure that the vdev specification is well-formed.
1109 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1111 zfs_cmd_t zc = { 0 };
1113 libzfs_handle_t *hdl = zhp->zpool_hdl;
1115 nvlist_t **spares, **l2cache;
1116 uint_t nspares, nl2cache;
1118 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1119 "cannot add to '%s'"), zhp->zpool_name);
1121 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1122 SPA_VERSION_SPARES &&
1123 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1124 &spares, &nspares) == 0) {
1125 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1126 "upgraded to add hot spares"));
1127 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1130 if (zpool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1131 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1134 for (s = 0; s < nspares; s++) {
1137 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1138 &path) == 0 && pool_uses_efi(spares[s])) {
1139 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1140 "device '%s' contains an EFI label and "
1141 "cannot be used on root pools."),
1142 zpool_vdev_name(hdl, NULL, spares[s],
1144 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1149 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1150 SPA_VERSION_L2CACHE &&
1151 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1152 &l2cache, &nl2cache) == 0) {
1153 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1154 "upgraded to add cache devices"));
1155 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1158 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1160 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1162 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1166 * This can happen if the user has specified the same
1167 * device multiple times. We can't reliably detect this
1168 * until we try to add it and see we already have a
1171 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1172 "one or more vdevs refer to the same device"));
1173 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1178 * This occurrs when one of the devices is below
1179 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1180 * device was the problem device since there's no
1181 * reliable way to determine device size from userland.
1186 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1188 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1189 "device is less than the minimum "
1192 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1196 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1197 "pool must be upgraded to add these vdevs"));
1198 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1202 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1203 "root pool can not have multiple vdevs"
1204 " or separate logs"));
1205 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1209 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1210 "cache device must be a disk or disk slice"));
1211 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1215 (void) zpool_standard_error(hdl, errno, msg);
1223 zcmd_free_nvlists(&zc);
1229 * Exports the pool from the system. The caller must ensure that there are no
1230 * mounted datasets in the pool.
1233 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1235 zfs_cmd_t zc = { 0 };
1238 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1239 "cannot export '%s'"), zhp->zpool_name);
1241 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1242 zc.zc_cookie = force;
1243 zc.zc_guid = hardforce;
1245 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1248 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1249 "use '-f' to override the following errors:\n"
1250 "'%s' has an active shared spare which could be"
1251 " used by other pools once '%s' is exported."),
1252 zhp->zpool_name, zhp->zpool_name);
1253 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1256 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1265 zpool_export(zpool_handle_t *zhp, boolean_t force)
1267 return (zpool_export_common(zhp, force, B_FALSE));
1271 zpool_export_force(zpool_handle_t *zhp)
1273 return (zpool_export_common(zhp, B_TRUE, B_TRUE));
1277 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1280 nvlist_t *nv = NULL;
1286 if (!hdl->libzfs_printerr || config == NULL)
1289 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0)
1292 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1294 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1296 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1297 strftime(timestr, 128, 0, &t) != 0) {
1299 (void) printf(dgettext(TEXT_DOMAIN,
1300 "Would be able to return %s "
1301 "to its state as of %s.\n"),
1304 (void) printf(dgettext(TEXT_DOMAIN,
1305 "Pool %s returned to its state as of %s.\n"),
1309 (void) printf(dgettext(TEXT_DOMAIN,
1310 "%s approximately %lld "),
1311 dryrun ? "Would discard" : "Discarded",
1313 (void) printf(dgettext(TEXT_DOMAIN,
1314 "minutes of transactions.\n"));
1315 } else if (loss > 0) {
1316 (void) printf(dgettext(TEXT_DOMAIN,
1317 "%s approximately %lld "),
1318 dryrun ? "Would discard" : "Discarded", loss);
1319 (void) printf(dgettext(TEXT_DOMAIN,
1320 "seconds of transactions.\n"));
1326 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1329 nvlist_t *nv = NULL;
1331 uint64_t edata = UINT64_MAX;
1336 if (!hdl->libzfs_printerr)
1340 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1342 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1344 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1345 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1346 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1349 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1350 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1353 (void) printf(dgettext(TEXT_DOMAIN,
1354 "Recovery is possible, but will result in some data loss.\n"));
1356 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1357 strftime(timestr, 128, 0, &t) != 0) {
1358 (void) printf(dgettext(TEXT_DOMAIN,
1359 "\tReturning the pool to its state as of %s\n"
1360 "\tshould correct the problem. "),
1363 (void) printf(dgettext(TEXT_DOMAIN,
1364 "\tReverting the pool to an earlier state "
1365 "should correct the problem.\n\t"));
1369 (void) printf(dgettext(TEXT_DOMAIN,
1370 "Approximately %lld minutes of data\n"
1371 "\tmust be discarded, irreversibly. "), (loss + 30) / 60);
1372 } else if (loss > 0) {
1373 (void) printf(dgettext(TEXT_DOMAIN,
1374 "Approximately %lld seconds of data\n"
1375 "\tmust be discarded, irreversibly. "), loss);
1377 if (edata != 0 && edata != UINT64_MAX) {
1379 (void) printf(dgettext(TEXT_DOMAIN,
1380 "After rewind, at least\n"
1381 "\tone persistent user-data error will remain. "));
1383 (void) printf(dgettext(TEXT_DOMAIN,
1384 "After rewind, several\n"
1385 "\tpersistent user-data errors will remain. "));
1388 (void) printf(dgettext(TEXT_DOMAIN,
1389 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1390 reason >= 0 ? "clear" : "import", name);
1392 (void) printf(dgettext(TEXT_DOMAIN,
1393 "A scrub of the pool\n"
1394 "\tis strongly recommended after recovery.\n"));
1398 (void) printf(dgettext(TEXT_DOMAIN,
1399 "Destroy and re-create the pool from\n\ta backup source.\n"));
1403 * zpool_import() is a contracted interface. Should be kept the same
1406 * Applications should use zpool_import_props() to import a pool with
1407 * new properties value to be set.
1410 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1413 nvlist_t *props = NULL;
1416 if (altroot != NULL) {
1417 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1418 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1419 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1423 if (nvlist_add_string(props,
1424 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1425 nvlist_add_string(props,
1426 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1428 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1429 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1434 ret = zpool_import_props(hdl, config, newname, props,
1442 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1448 uint64_t is_log = 0;
1450 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1454 (void) printf("\t%*s%s%s\n", indent, "", name,
1455 is_log ? " [log]" : "");
1457 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1458 &child, &children) != 0)
1461 for (c = 0; c < children; c++) {
1462 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
1463 print_vdev_tree(hdl, vname, child[c], indent + 2);
1469 * Import the given pool using the known configuration and a list of
1470 * properties to be set. The configuration should have come from
1471 * zpool_find_import(). The 'newname' parameters control whether the pool
1472 * is imported with a different name.
1475 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1476 nvlist_t *props, int flags)
1478 zfs_cmd_t zc = { 0 };
1479 zpool_rewind_policy_t policy;
1480 nvlist_t *nv = NULL;
1481 nvlist_t *nvinfo = NULL;
1482 nvlist_t *missing = NULL;
1489 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1492 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1493 "cannot import pool '%s'"), origname);
1495 if (newname != NULL) {
1496 if (!zpool_name_valid(hdl, B_FALSE, newname))
1497 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1498 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1500 thename = (char *)newname;
1507 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1509 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1512 if ((props = zpool_valid_proplist(hdl, origname,
1513 props, version, flags, errbuf)) == NULL) {
1515 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1521 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1523 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1526 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1530 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1535 zc.zc_cookie = flags;
1536 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1538 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1539 zcmd_free_nvlists(&zc);
1546 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1547 zpool_get_rewind_policy(config, &policy);
1553 * Dry-run failed, but we print out what success
1554 * looks like if we found a best txg
1556 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1557 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1563 if (newname == NULL)
1564 (void) snprintf(desc, sizeof (desc),
1565 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1568 (void) snprintf(desc, sizeof (desc),
1569 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1575 * Unsupported version.
1577 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1581 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1585 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1586 "one or more devices is read only"));
1587 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1591 if (nv && nvlist_lookup_nvlist(nv,
1592 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1593 nvlist_lookup_nvlist(nvinfo,
1594 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1595 (void) printf(dgettext(TEXT_DOMAIN,
1596 "The devices below are missing, use "
1597 "'-m' to import the pool anyway:\n"));
1598 print_vdev_tree(hdl, NULL, missing, 2);
1599 (void) printf("\n");
1601 (void) zpool_standard_error(hdl, error, desc);
1605 (void) zpool_standard_error(hdl, error, desc);
1609 (void) zpool_standard_error(hdl, error, desc);
1610 zpool_explain_recover(hdl,
1611 newname ? origname : thename, -error, nv);
1618 zpool_handle_t *zhp;
1621 * This should never fail, but play it safe anyway.
1623 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1625 else if (zhp != NULL)
1627 if (policy.zrp_request &
1628 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1629 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1630 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1636 zcmd_free_nvlists(&zc);
1646 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1648 zfs_cmd_t zc = { 0 };
1650 libzfs_handle_t *hdl = zhp->zpool_hdl;
1652 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1653 zc.zc_cookie = func;
1655 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
1656 (errno == ENOENT && func != POOL_SCAN_NONE))
1659 if (func == POOL_SCAN_SCRUB) {
1660 (void) snprintf(msg, sizeof (msg),
1661 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1662 } else if (func == POOL_SCAN_NONE) {
1663 (void) snprintf(msg, sizeof (msg),
1664 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1667 assert(!"unexpected result");
1670 if (errno == EBUSY) {
1672 pool_scan_stat_t *ps = NULL;
1675 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1676 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1677 (void) nvlist_lookup_uint64_array(nvroot,
1678 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1679 if (ps && ps->pss_func == POOL_SCAN_SCRUB)
1680 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1682 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1683 } else if (errno == ENOENT) {
1684 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1686 return (zpool_standard_error(hdl, errno, msg));
1691 * This provides a very minimal check whether a given string is likely a
1692 * c#t#d# style string. Users of this are expected to do their own
1693 * verification of the s# part.
1695 #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1]))
1698 * More elaborate version for ones which may start with "/dev/dsk/"
1702 ctd_check_path(char *str) {
1704 * If it starts with a slash, check the last component.
1706 if (str && str[0] == '/') {
1707 char *tmp = strrchr(str, '/');
1710 * If it ends in "/old", check the second-to-last
1711 * component of the string instead.
1713 if (tmp != str && strcmp(tmp, "/old") == 0) {
1714 for (tmp--; *tmp != '/'; tmp--)
1719 return (CTD_CHECK(str));
1723 * Find a vdev that matches the search criteria specified. We use the
1724 * the nvpair name to determine how we should look for the device.
1725 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1726 * spare; but FALSE if its an INUSE spare.
1729 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1730 boolean_t *l2cache, boolean_t *log)
1737 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1739 /* Nothing to look for */
1740 if (search == NULL || pair == NULL)
1743 /* Obtain the key we will use to search */
1744 srchkey = nvpair_name(pair);
1746 switch (nvpair_type(pair)) {
1747 case DATA_TYPE_UINT64:
1748 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1749 uint64_t srchval, theguid;
1751 verify(nvpair_value_uint64(pair, &srchval) == 0);
1752 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1754 if (theguid == srchval)
1759 case DATA_TYPE_STRING: {
1760 char *srchval, *val;
1762 verify(nvpair_value_string(pair, &srchval) == 0);
1763 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1767 * Search for the requested value. Special cases:
1769 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
1770 * "s0" or "s0/old". The "s0" part is hidden from the user,
1771 * but included in the string, so this matches around it.
1772 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1774 * Otherwise, all other searches are simple string compares.
1776 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
1777 ctd_check_path(val)) {
1778 uint64_t wholedisk = 0;
1780 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1783 int slen = strlen(srchval);
1784 int vlen = strlen(val);
1786 if (slen != vlen - 2)
1790 * make_leaf_vdev() should only set
1791 * wholedisk for ZPOOL_CONFIG_PATHs which
1792 * will include "/dev/dsk/", giving plenty of
1793 * room for the indices used next.
1798 * strings identical except trailing "s0"
1800 if (strcmp(&val[vlen - 2], "s0") == 0 &&
1801 strncmp(srchval, val, slen) == 0)
1805 * strings identical except trailing "s0/old"
1807 if (strcmp(&val[vlen - 6], "s0/old") == 0 &&
1808 strcmp(&srchval[slen - 4], "/old") == 0 &&
1809 strncmp(srchval, val, slen - 4) == 0)
1814 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
1815 char *type, *idx, *end, *p;
1816 uint64_t id, vdev_id;
1819 * Determine our vdev type, keeping in mind
1820 * that the srchval is composed of a type and
1821 * vdev id pair (i.e. mirror-4).
1823 if ((type = strdup(srchval)) == NULL)
1826 if ((p = strrchr(type, '-')) == NULL) {
1834 * If the types don't match then keep looking.
1836 if (strncmp(val, type, strlen(val)) != 0) {
1841 verify(strncmp(type, VDEV_TYPE_RAIDZ,
1842 strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1843 strncmp(type, VDEV_TYPE_MIRROR,
1844 strlen(VDEV_TYPE_MIRROR)) == 0);
1845 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
1849 vdev_id = strtoull(idx, &end, 10);
1856 * Now verify that we have the correct vdev id.
1865 if (strcmp(srchval, val) == 0)
1874 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1875 &child, &children) != 0)
1878 for (c = 0; c < children; c++) {
1879 if ((ret = vdev_to_nvlist_iter(child[c], search,
1880 avail_spare, l2cache, NULL)) != NULL) {
1882 * The 'is_log' value is only set for the toplevel
1883 * vdev, not the leaf vdevs. So we always lookup the
1884 * log device from the root of the vdev tree (where
1885 * 'log' is non-NULL).
1888 nvlist_lookup_uint64(child[c],
1889 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
1897 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1898 &child, &children) == 0) {
1899 for (c = 0; c < children; c++) {
1900 if ((ret = vdev_to_nvlist_iter(child[c], search,
1901 avail_spare, l2cache, NULL)) != NULL) {
1902 *avail_spare = B_TRUE;
1908 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1909 &child, &children) == 0) {
1910 for (c = 0; c < children; c++) {
1911 if ((ret = vdev_to_nvlist_iter(child[c], search,
1912 avail_spare, l2cache, NULL)) != NULL) {
1923 * Given a physical path (minus the "/devices" prefix), find the
1927 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
1928 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
1930 nvlist_t *search, *nvroot, *ret;
1932 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1933 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
1935 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1938 *avail_spare = B_FALSE;
1942 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1943 nvlist_free(search);
1949 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
1952 zpool_vdev_is_interior(const char *name)
1954 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1955 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
1961 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
1962 boolean_t *l2cache, boolean_t *log)
1964 char buf[MAXPATHLEN];
1966 nvlist_t *nvroot, *search, *ret;
1969 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1971 guid = strtoull(path, &end, 10);
1972 if (guid != 0 && *end == '\0') {
1973 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
1974 } else if (zpool_vdev_is_interior(path)) {
1975 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
1976 } else if (path[0] != '/') {
1977 (void) snprintf(buf, sizeof (buf), "%s%s", _PATH_DEV, path);
1978 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
1980 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
1983 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1986 *avail_spare = B_FALSE;
1990 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1991 nvlist_free(search);
1997 vdev_online(nvlist_t *nv)
2001 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2002 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2003 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2010 * Helper function for zpool_get_physpaths().
2013 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2014 size_t *bytes_written)
2016 size_t bytes_left, pos, rsz;
2020 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2022 return (EZFS_NODEVICE);
2024 pos = *bytes_written;
2025 bytes_left = physpath_size - pos;
2026 format = (pos == 0) ? "%s" : " %s";
2028 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2029 *bytes_written += rsz;
2031 if (rsz >= bytes_left) {
2032 /* if physpath was not copied properly, clear it */
2033 if (bytes_left != 0) {
2036 return (EZFS_NOSPC);
2042 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2043 size_t *rsz, boolean_t is_spare)
2048 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2049 return (EZFS_INVALCONFIG);
2051 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2053 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2054 * For a spare vdev, we only want to boot from the active
2059 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2062 return (EZFS_INVALCONFIG);
2065 if (vdev_online(nv)) {
2066 if ((ret = vdev_get_one_physpath(nv, physpath,
2067 phypath_size, rsz)) != 0)
2070 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2071 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2072 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2077 if (nvlist_lookup_nvlist_array(nv,
2078 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2079 return (EZFS_INVALCONFIG);
2081 for (i = 0; i < count; i++) {
2082 ret = vdev_get_physpaths(child[i], physpath,
2083 phypath_size, rsz, is_spare);
2084 if (ret == EZFS_NOSPC)
2089 return (EZFS_POOL_INVALARG);
2093 * Get phys_path for a root pool config.
2094 * Return 0 on success; non-zero on failure.
2097 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2100 nvlist_t *vdev_root;
2107 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2109 return (EZFS_INVALCONFIG);
2111 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2112 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2113 &child, &count) != 0)
2114 return (EZFS_INVALCONFIG);
2117 * root pool can not have EFI labeled disks and can only have
2118 * a single top-level vdev.
2120 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
2121 pool_uses_efi(vdev_root))
2122 return (EZFS_POOL_INVALARG);
2124 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2127 /* No online devices */
2129 return (EZFS_NODEVICE);
2135 * Get phys_path for a root pool
2136 * Return 0 on success; non-zero on failure.
2139 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2141 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2146 * If the device has being dynamically expanded then we need to relabel
2147 * the disk to use the new unallocated space.
2150 zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2153 char path[MAXPATHLEN];
2156 int (*_efi_use_whole_disk)(int);
2158 if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2159 "efi_use_whole_disk")) == NULL)
2162 (void) snprintf(path, sizeof (path), "%s/%s", RDISK_ROOT, name);
2164 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2165 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2166 "relabel '%s': unable to open device"), name);
2167 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2171 * It's possible that we might encounter an error if the device
2172 * does not have any unallocated space left. If so, we simply
2173 * ignore that error and continue on.
2175 error = _efi_use_whole_disk(fd);
2177 if (error && error != VT_ENOSPC) {
2178 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2179 "relabel '%s': unable to read disk capacity"), name);
2180 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2187 * Bring the specified vdev online. The 'flags' parameter is a set of the
2188 * ZFS_ONLINE_* flags.
2191 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2192 vdev_state_t *newstate)
2194 zfs_cmd_t zc = { 0 };
2197 boolean_t avail_spare, l2cache, islog;
2198 libzfs_handle_t *hdl = zhp->zpool_hdl;
2200 if (flags & ZFS_ONLINE_EXPAND) {
2201 (void) snprintf(msg, sizeof (msg),
2202 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2204 (void) snprintf(msg, sizeof (msg),
2205 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2208 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2209 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2211 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2213 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2216 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2218 if (flags & ZFS_ONLINE_EXPAND ||
2219 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2220 char *pathname = NULL;
2221 uint64_t wholedisk = 0;
2223 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2225 verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
2229 * XXX - L2ARC 1.0 devices can't support expansion.
2232 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2233 "cannot expand cache devices"));
2234 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2238 pathname += strlen(DISK_ROOT) + 1;
2239 (void) zpool_relabel_disk(hdl, pathname);
2243 zc.zc_cookie = VDEV_STATE_ONLINE;
2246 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2247 if (errno == EINVAL) {
2248 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2249 "from this pool into a new one. Use '%s' "
2250 "instead"), "zpool detach");
2251 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2253 return (zpool_standard_error(hdl, errno, msg));
2256 *newstate = zc.zc_cookie;
2261 * Take the specified vdev offline
2264 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2266 zfs_cmd_t zc = { 0 };
2269 boolean_t avail_spare, l2cache;
2270 libzfs_handle_t *hdl = zhp->zpool_hdl;
2272 (void) snprintf(msg, sizeof (msg),
2273 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2275 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2276 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2278 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2280 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2283 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2285 zc.zc_cookie = VDEV_STATE_OFFLINE;
2286 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2288 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2295 * There are no other replicas of this device.
2297 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2301 * The log device has unplayed logs
2303 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2306 return (zpool_standard_error(hdl, errno, msg));
2311 * Mark the given vdev faulted.
2314 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2316 zfs_cmd_t zc = { 0 };
2318 libzfs_handle_t *hdl = zhp->zpool_hdl;
2320 (void) snprintf(msg, sizeof (msg),
2321 dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2323 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2325 zc.zc_cookie = VDEV_STATE_FAULTED;
2328 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2335 * There are no other replicas of this device.
2337 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2340 return (zpool_standard_error(hdl, errno, msg));
2346 * Mark the given vdev degraded.
2349 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2351 zfs_cmd_t zc = { 0 };
2353 libzfs_handle_t *hdl = zhp->zpool_hdl;
2355 (void) snprintf(msg, sizeof (msg),
2356 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
2358 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2360 zc.zc_cookie = VDEV_STATE_DEGRADED;
2363 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2366 return (zpool_standard_error(hdl, errno, msg));
2370 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2374 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2380 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2382 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2385 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2386 children == 2 && child[which] == tgt)
2389 for (c = 0; c < children; c++)
2390 if (is_replacing_spare(child[c], tgt, which))
2398 * Attach new_disk (fully described by nvroot) to old_disk.
2399 * If 'replacing' is specified, the new disk will replace the old one.
2402 zpool_vdev_attach(zpool_handle_t *zhp,
2403 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2405 zfs_cmd_t zc = { 0 };
2409 boolean_t avail_spare, l2cache, islog;
2414 nvlist_t *config_root;
2415 libzfs_handle_t *hdl = zhp->zpool_hdl;
2416 boolean_t rootpool = zpool_is_bootable(zhp);
2419 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2420 "cannot replace %s with %s"), old_disk, new_disk);
2422 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2423 "cannot attach %s to %s"), new_disk, old_disk);
2426 * If this is a root pool, make sure that we're not attaching an
2427 * EFI labeled device.
2429 if (rootpool && pool_uses_efi(nvroot)) {
2430 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2431 "EFI labeled devices are not supported on root pools."));
2432 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2435 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2436 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2438 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2441 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2444 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2446 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2447 zc.zc_cookie = replacing;
2449 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2450 &child, &children) != 0 || children != 1) {
2451 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2452 "new device must be a single disk"));
2453 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2456 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2457 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2459 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2463 * If the target is a hot spare that has been swapped in, we can only
2464 * replace it with another hot spare.
2467 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2468 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2469 NULL) == NULL || !avail_spare) &&
2470 is_replacing_spare(config_root, tgt, 1)) {
2471 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2472 "can only be replaced by another hot spare"));
2474 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2479 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2482 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2484 zcmd_free_nvlists(&zc);
2489 * XXX need a better way to prevent user from
2490 * booting up a half-baked vdev.
2492 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2493 "sure to wait until resilver is done "
2494 "before rebooting.\n"));
2495 (void) fprintf(stderr, "\n");
2496 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "If "
2497 "you boot from pool '%s', you may need to update\n"
2498 "boot code on newly attached disk '%s'.\n\n"
2499 "Assuming you use GPT partitioning and 'da0' is "
2500 "your new boot disk\n"
2501 "you may use the following command:\n\n"
2502 "\tgpart bootcode -b /boot/pmbr -p "
2503 "/boot/gptzfsboot -i 1 da0\n\n"),
2504 zhp->zpool_name, new_disk);
2512 * Can't attach to or replace this type of vdev.
2515 uint64_t version = zpool_get_prop_int(zhp,
2516 ZPOOL_PROP_VERSION, NULL);
2519 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2520 "cannot replace a log with a spare"));
2521 else if (version >= SPA_VERSION_MULTI_REPLACE)
2522 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2523 "already in replacing/spare config; wait "
2524 "for completion or use 'zpool detach'"));
2526 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2527 "cannot replace a replacing device"));
2529 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2530 "can only attach to mirrors and top-level "
2533 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2538 * The new device must be a single disk.
2540 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2541 "new device must be a single disk"));
2542 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2546 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2548 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2553 * The new device is too small.
2555 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2556 "device is too small"));
2557 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2562 * The new device has a different alignment requirement.
2564 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2565 "devices have different sector alignment"));
2566 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2571 * The resulting top-level vdev spec won't fit in the label.
2573 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2577 (void) zpool_standard_error(hdl, errno, msg);
2584 * Detach the specified device.
2587 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2589 zfs_cmd_t zc = { 0 };
2592 boolean_t avail_spare, l2cache;
2593 libzfs_handle_t *hdl = zhp->zpool_hdl;
2595 (void) snprintf(msg, sizeof (msg),
2596 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2598 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2599 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2601 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2604 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2607 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2609 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2611 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2618 * Can't detach from this type of vdev.
2620 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2621 "applicable to mirror and replacing vdevs"));
2622 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2627 * There are no other replicas of this device.
2629 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2633 (void) zpool_standard_error(hdl, errno, msg);
2640 * Find a mirror vdev in the source nvlist.
2642 * The mchild array contains a list of disks in one of the top-level mirrors
2643 * of the source pool. The schild array contains a list of disks that the
2644 * user specified on the command line. We loop over the mchild array to
2645 * see if any entry in the schild array matches.
2647 * If a disk in the mchild array is found in the schild array, we return
2648 * the index of that entry. Otherwise we return -1.
2651 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2652 nvlist_t **schild, uint_t schildren)
2656 for (mc = 0; mc < mchildren; mc++) {
2658 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2659 mchild[mc], B_FALSE);
2661 for (sc = 0; sc < schildren; sc++) {
2662 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2663 schild[sc], B_FALSE);
2664 boolean_t result = (strcmp(mpath, spath) == 0);
2680 * Split a mirror pool. If newroot points to null, then a new nvlist
2681 * is generated and it is the responsibility of the caller to free it.
2684 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2685 nvlist_t *props, splitflags_t flags)
2687 zfs_cmd_t zc = { 0 };
2689 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2690 nvlist_t **varray = NULL, *zc_props = NULL;
2691 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2692 libzfs_handle_t *hdl = zhp->zpool_hdl;
2694 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2697 (void) snprintf(msg, sizeof (msg),
2698 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2700 if (!zpool_name_valid(hdl, B_FALSE, newname))
2701 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2703 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2704 (void) fprintf(stderr, gettext("Internal error: unable to "
2705 "retrieve pool configuration\n"));
2709 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2711 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2714 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2715 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2716 props, vers, flags, msg)) == NULL)
2720 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2722 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2723 "Source pool is missing vdev tree"));
2725 nvlist_free(zc_props);
2729 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2732 if (*newroot == NULL ||
2733 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2734 &newchild, &newchildren) != 0)
2737 for (c = 0; c < children; c++) {
2738 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2740 nvlist_t **mchild, *vdev;
2745 * Unlike cache & spares, slogs are stored in the
2746 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2748 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2750 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2752 if (is_log || is_hole) {
2754 * Create a hole vdev and put it in the config.
2756 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2758 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2759 VDEV_TYPE_HOLE) != 0)
2761 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2766 varray[vcount++] = vdev;
2770 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
2772 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2773 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2774 "Source pool must be composed only of mirrors\n"));
2775 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2779 verify(nvlist_lookup_nvlist_array(child[c],
2780 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2782 /* find or add an entry for this top-level vdev */
2783 if (newchildren > 0 &&
2784 (entry = find_vdev_entry(zhp, mchild, mchildren,
2785 newchild, newchildren)) >= 0) {
2786 /* We found a disk that the user specified. */
2787 vdev = mchild[entry];
2790 /* User didn't specify a disk for this vdev. */
2791 vdev = mchild[mchildren - 1];
2794 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
2798 /* did we find every disk the user specified? */
2799 if (found != newchildren) {
2800 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
2801 "include at most one disk from each mirror"));
2802 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2806 /* Prepare the nvlist for populating. */
2807 if (*newroot == NULL) {
2808 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
2811 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
2812 VDEV_TYPE_ROOT) != 0)
2815 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
2818 /* Add all the children we found */
2819 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
2820 lastlog == 0 ? vcount : lastlog) != 0)
2824 * If we're just doing a dry run, exit now with success.
2827 memory_err = B_FALSE;
2832 /* now build up the config list & call the ioctl */
2833 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
2836 if (nvlist_add_nvlist(newconfig,
2837 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
2838 nvlist_add_string(newconfig,
2839 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
2840 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
2844 * The new pool is automatically part of the namespace unless we
2845 * explicitly export it.
2848 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
2849 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2850 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
2851 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
2853 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
2856 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
2857 retval = zpool_standard_error(hdl, errno, msg);
2862 memory_err = B_FALSE;
2865 if (varray != NULL) {
2868 for (v = 0; v < vcount; v++)
2869 nvlist_free(varray[v]);
2872 zcmd_free_nvlists(&zc);
2874 nvlist_free(zc_props);
2876 nvlist_free(newconfig);
2878 nvlist_free(*newroot);
2886 return (no_memory(hdl));
2892 * Remove the given device. Currently, this is supported only for hot spares
2893 * and level 2 cache devices.
2896 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
2898 zfs_cmd_t zc = { 0 };
2901 boolean_t avail_spare, l2cache, islog;
2902 libzfs_handle_t *hdl = zhp->zpool_hdl;
2905 (void) snprintf(msg, sizeof (msg),
2906 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
2908 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2909 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2911 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2913 * XXX - this should just go away.
2915 if (!avail_spare && !l2cache && !islog) {
2916 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2917 "only inactive hot spares, cache, top-level, "
2918 "or log devices can be removed"));
2919 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2922 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
2923 if (islog && version < SPA_VERSION_HOLES) {
2924 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2925 "pool must be upgrade to support log removal"));
2926 return (zfs_error(hdl, EZFS_BADVERSION, msg));
2929 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2931 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
2934 return (zpool_standard_error(hdl, errno, msg));
2938 * Clear the errors for the pool, or the particular device if specified.
2941 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
2943 zfs_cmd_t zc = { 0 };
2946 zpool_rewind_policy_t policy;
2947 boolean_t avail_spare, l2cache;
2948 libzfs_handle_t *hdl = zhp->zpool_hdl;
2949 nvlist_t *nvi = NULL;
2953 (void) snprintf(msg, sizeof (msg),
2954 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2957 (void) snprintf(msg, sizeof (msg),
2958 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2961 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2963 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
2964 &l2cache, NULL)) == 0)
2965 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2968 * Don't allow error clearing for hot spares. Do allow
2969 * error clearing for l2cache devices.
2972 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2974 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
2978 zpool_get_rewind_policy(rewindnvl, &policy);
2979 zc.zc_cookie = policy.zrp_request;
2981 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
2984 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
2987 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
2989 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
2990 zcmd_free_nvlists(&zc);
2995 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
2996 errno != EPERM && errno != EACCES)) {
2997 if (policy.zrp_request &
2998 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2999 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3000 zpool_rewind_exclaim(hdl, zc.zc_name,
3001 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
3005 zcmd_free_nvlists(&zc);
3009 zcmd_free_nvlists(&zc);
3010 return (zpool_standard_error(hdl, errno, msg));
3014 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3017 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3019 zfs_cmd_t zc = { 0 };
3021 libzfs_handle_t *hdl = zhp->zpool_hdl;
3023 (void) snprintf(msg, sizeof (msg),
3024 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3027 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3029 zc.zc_cookie = ZPOOL_NO_REWIND;
3031 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3034 return (zpool_standard_error(hdl, errno, msg));
3038 * Change the GUID for a pool.
3041 zpool_reguid(zpool_handle_t *zhp)
3044 libzfs_handle_t *hdl = zhp->zpool_hdl;
3045 zfs_cmd_t zc = { 0 };
3047 (void) snprintf(msg, sizeof (msg),
3048 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3050 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3051 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3054 return (zpool_standard_error(hdl, errno, msg));
3061 zpool_reopen(zpool_handle_t *zhp)
3063 zfs_cmd_t zc = { 0 };
3065 libzfs_handle_t *hdl = zhp->zpool_hdl;
3067 (void) snprintf(msg, sizeof (msg),
3068 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
3071 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3072 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
3074 return (zpool_standard_error(hdl, errno, msg));
3078 * Convert from a devid string to a path.
3081 devid_to_path(char *devid_str)
3086 devid_nmlist_t *list = NULL;
3089 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3092 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3094 devid_str_free(minor);
3100 if ((path = strdup(list[0].devname)) == NULL)
3103 devid_free_nmlist(list);
3109 * Convert from a path to a devid string.
3112 path_to_devid(const char *path)
3118 if ((fd = open(path, O_RDONLY)) < 0)
3123 if (devid_get(fd, &devid) == 0) {
3124 if (devid_get_minor_name(fd, &minor) == 0)
3125 ret = devid_str_encode(devid, minor);
3127 devid_str_free(minor);
3136 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3137 * ignore any failure here, since a common case is for an unprivileged user to
3138 * type 'zpool status', and we'll display the correct information anyway.
3141 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3143 zfs_cmd_t zc = { 0 };
3145 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3146 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3147 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3150 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3154 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3155 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3156 * We also check if this is a whole disk, in which case we strip off the
3157 * trailing 's0' slice name.
3159 * This routine is also responsible for identifying when disks have been
3160 * reconfigured in a new location. The kernel will have opened the device by
3161 * devid, but the path will still refer to the old location. To catch this, we
3162 * first do a path -> devid translation (which is fast for the common case). If
3163 * the devid matches, we're done. If not, we do a reverse devid -> path
3164 * translation and issue the appropriate ioctl() to update the path of the vdev.
3165 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3169 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3180 have_stats = nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3181 (uint64_t **)&vs, &vsc) == 0;
3182 have_path = nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0;
3185 * If the device is not currently present, assume it will not
3186 * come back at the same device path. Display the device by GUID.
3188 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3189 have_path && have_stats && vs->vs_state <= VDEV_STATE_CANT_OPEN) {
3190 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3192 (void) snprintf(buf, sizeof (buf), "%llu",
3193 (u_longlong_t)value);
3195 } else if (have_path) {
3198 * If the device is dead (faulted, offline, etc) then don't
3199 * bother opening it. Otherwise we may be forcing the user to
3200 * open a misbehaving device, which can have undesirable
3203 if ((have_stats == 0 ||
3204 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3206 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3208 * Determine if the current path is correct.
3210 char *newdevid = path_to_devid(path);
3212 if (newdevid == NULL ||
3213 strcmp(devid, newdevid) != 0) {
3216 if ((newpath = devid_to_path(devid)) != NULL) {
3218 * Update the path appropriately.
3220 set_path(zhp, nv, newpath);
3221 if (nvlist_add_string(nv,
3222 ZPOOL_CONFIG_PATH, newpath) == 0)
3223 verify(nvlist_lookup_string(nv,
3231 devid_str_free(newdevid);
3235 if (strncmp(path, "/dev/dsk/", 9) == 0)
3238 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3239 &value) == 0 && value) {
3240 int pathlen = strlen(path);
3241 char *tmp = zfs_strdup(hdl, path);
3244 * If it starts with c#, and ends with "s0", chop
3245 * the "s0" off, or if it ends with "s0/old", remove
3246 * the "s0" from the middle.
3248 if (CTD_CHECK(tmp)) {
3249 if (strcmp(&tmp[pathlen - 2], "s0") == 0) {
3250 tmp[pathlen - 2] = '\0';
3251 } else if (pathlen > 6 &&
3252 strcmp(&tmp[pathlen - 6], "s0/old") == 0) {
3253 (void) strcpy(&tmp[pathlen - 6],
3260 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3261 path += sizeof(_PATH_DEV) - 1;
3264 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3267 * If it's a raidz device, we need to stick in the parity level.
3269 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3270 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3272 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3273 (u_longlong_t)value);
3278 * We identify each top-level vdev by using a <type-id>
3279 * naming convention.
3284 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3286 (void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3292 return (zfs_strdup(hdl, path));
3296 zbookmark_compare(const void *a, const void *b)
3298 return (memcmp(a, b, sizeof (zbookmark_t)));
3302 * Retrieve the persistent error log, uniquify the members, and return to the
3306 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3308 zfs_cmd_t zc = { 0 };
3310 zbookmark_t *zb = NULL;
3314 * Retrieve the raw error list from the kernel. If the number of errors
3315 * has increased, allocate more space and continue until we get the
3318 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3322 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3323 count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
3325 zc.zc_nvlist_dst_size = count;
3326 (void) strcpy(zc.zc_name, zhp->zpool_name);
3328 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3330 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3331 if (errno == ENOMEM) {
3332 count = zc.zc_nvlist_dst_size;
3333 if ((zc.zc_nvlist_dst = (uintptr_t)
3334 zfs_alloc(zhp->zpool_hdl, count *
3335 sizeof (zbookmark_t))) == (uintptr_t)NULL)
3346 * Sort the resulting bookmarks. This is a little confusing due to the
3347 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3348 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3349 * _not_ copied as part of the process. So we point the start of our
3350 * array appropriate and decrement the total number of elements.
3352 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
3353 zc.zc_nvlist_dst_size;
3354 count -= zc.zc_nvlist_dst_size;
3356 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
3358 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3361 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3363 for (i = 0; i < count; i++) {
3366 /* ignoring zb_blkid and zb_level for now */
3367 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3368 zb[i-1].zb_object == zb[i].zb_object)
3371 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3373 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3374 zb[i].zb_objset) != 0) {
3378 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3379 zb[i].zb_object) != 0) {
3383 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3390 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3394 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3395 return (no_memory(zhp->zpool_hdl));
3399 * Upgrade a ZFS pool to the latest on-disk version.
3402 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3404 zfs_cmd_t zc = { 0 };
3405 libzfs_handle_t *hdl = zhp->zpool_hdl;
3407 (void) strcpy(zc.zc_name, zhp->zpool_name);
3408 zc.zc_cookie = new_version;
3410 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3411 return (zpool_standard_error_fmt(hdl, errno,
3412 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3418 zpool_set_history_str(const char *subcommand, int argc, char **argv,
3423 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
3424 for (i = 1; i < argc; i++) {
3425 if (strlen(history_str) + 1 + strlen(argv[i]) >
3428 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
3429 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
3434 * Stage command history for logging.
3437 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
3439 if (history_str == NULL)
3442 if (strlen(history_str) > HIS_MAX_RECORD_LEN)
3445 if (hdl->libzfs_log_str != NULL)
3446 free(hdl->libzfs_log_str);
3448 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
3449 return (no_memory(hdl));
3455 * Perform ioctl to get some command history of a pool.
3457 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3458 * logical offset of the history buffer to start reading from.
3460 * Upon return, 'off' is the next logical offset to read from and
3461 * 'len' is the actual amount of bytes read into 'buf'.
3464 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3466 zfs_cmd_t zc = { 0 };
3467 libzfs_handle_t *hdl = zhp->zpool_hdl;
3469 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3471 zc.zc_history = (uint64_t)(uintptr_t)buf;
3472 zc.zc_history_len = *len;
3473 zc.zc_history_offset = *off;
3475 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3478 return (zfs_error_fmt(hdl, EZFS_PERM,
3479 dgettext(TEXT_DOMAIN,
3480 "cannot show history for pool '%s'"),
3483 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3484 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3485 "'%s'"), zhp->zpool_name));
3487 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3488 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3489 "'%s', pool must be upgraded"), zhp->zpool_name));
3491 return (zpool_standard_error_fmt(hdl, errno,
3492 dgettext(TEXT_DOMAIN,
3493 "cannot get history for '%s'"), zhp->zpool_name));
3497 *len = zc.zc_history_len;
3498 *off = zc.zc_history_offset;
3504 * Process the buffer of nvlists, unpacking and storing each nvlist record
3505 * into 'records'. 'leftover' is set to the number of bytes that weren't
3506 * processed as there wasn't a complete record.
3509 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3510 nvlist_t ***records, uint_t *numrecords)
3516 while (bytes_read > sizeof (reclen)) {
3518 /* get length of packed record (stored as little endian) */
3519 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3520 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3522 if (bytes_read < sizeof (reclen) + reclen)
3526 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3528 bytes_read -= sizeof (reclen) + reclen;
3529 buf += sizeof (reclen) + reclen;
3531 /* add record to nvlist array */
3533 if (ISP2(*numrecords + 1)) {
3534 *records = realloc(*records,
3535 *numrecords * 2 * sizeof (nvlist_t *));
3537 (*records)[*numrecords - 1] = nv;
3540 *leftover = bytes_read;
3544 #define HIS_BUF_LEN (128*1024)
3547 * Retrieve the command history of a pool.
3550 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3552 char buf[HIS_BUF_LEN];
3554 nvlist_t **records = NULL;
3555 uint_t numrecords = 0;
3559 uint64_t bytes_read = sizeof (buf);
3562 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3565 /* if nothing else was read in, we're at EOF, just return */
3569 if ((err = zpool_history_unpack(buf, bytes_read,
3570 &leftover, &records, &numrecords)) != 0)
3578 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3579 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3580 records, numrecords) == 0);
3582 for (i = 0; i < numrecords; i++)
3583 nvlist_free(records[i]);
3590 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3591 char *pathname, size_t len)
3593 zfs_cmd_t zc = { 0 };
3594 boolean_t mounted = B_FALSE;
3595 char *mntpnt = NULL;
3596 char dsname[MAXNAMELEN];
3599 /* special case for the MOS */
3600 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
3604 /* get the dataset's name */
3605 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3607 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3608 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3609 /* just write out a path of two object numbers */
3610 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3614 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3616 /* find out if the dataset is mounted */
3617 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3619 /* get the corrupted object's path */
3620 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3622 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3625 (void) snprintf(pathname, len, "%s%s", mntpnt,
3628 (void) snprintf(pathname, len, "%s:%s",
3629 dsname, zc.zc_value);
3632 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
3639 * Read the EFI label from the config, if a label does not exist then
3640 * pass back the error to the caller. If the caller has passed a non-NULL
3641 * diskaddr argument then we set it to the starting address of the EFI
3645 read_efi_label(nvlist_t *config, diskaddr_t *sb)
3649 char diskname[MAXPATHLEN];
3652 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3655 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
3656 strrchr(path, '/'));
3657 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
3658 struct dk_gpt *vtoc;
3660 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3662 *sb = vtoc->efi_parts[0].p_start;
3671 * determine where a partition starts on a disk in the current
3675 find_start_block(nvlist_t *config)
3679 diskaddr_t sb = MAXOFFSET_T;
3682 if (nvlist_lookup_nvlist_array(config,
3683 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3684 if (nvlist_lookup_uint64(config,
3685 ZPOOL_CONFIG_WHOLE_DISK,
3686 &wholedisk) != 0 || !wholedisk) {
3687 return (MAXOFFSET_T);
3689 if (read_efi_label(config, &sb) < 0)
3694 for (c = 0; c < children; c++) {
3695 sb = find_start_block(child[c]);
3696 if (sb != MAXOFFSET_T) {
3700 return (MAXOFFSET_T);
3705 * Label an individual disk. The name provided is the short name,
3706 * stripped of any leading /dev path.
3709 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name)
3712 char path[MAXPATHLEN];
3713 struct dk_gpt *vtoc;
3715 size_t resv = EFI_MIN_RESV_SIZE;
3716 uint64_t slice_size;
3717 diskaddr_t start_block;
3720 /* prepare an error message just in case */
3721 (void) snprintf(errbuf, sizeof (errbuf),
3722 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3727 if (zpool_is_bootable(zhp)) {
3728 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3729 "EFI labeled devices are not supported on root "
3731 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3734 verify(nvlist_lookup_nvlist(zhp->zpool_config,
3735 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3737 if (zhp->zpool_start_block == 0)
3738 start_block = find_start_block(nvroot);
3740 start_block = zhp->zpool_start_block;
3741 zhp->zpool_start_block = start_block;
3744 start_block = NEW_START_BLOCK;
3747 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
3750 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
3752 * This shouldn't happen. We've long since verified that this
3753 * is a valid device.
3756 dgettext(TEXT_DOMAIN, "unable to open device"));
3757 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
3760 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
3762 * The only way this can fail is if we run out of memory, or we
3763 * were unable to read the disk's capacity
3765 if (errno == ENOMEM)
3766 (void) no_memory(hdl);
3769 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3770 "unable to read disk capacity"), name);
3772 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
3775 slice_size = vtoc->efi_last_u_lba + 1;
3776 slice_size -= EFI_MIN_RESV_SIZE;
3777 if (start_block == MAXOFFSET_T)
3778 start_block = NEW_START_BLOCK;
3779 slice_size -= start_block;
3781 vtoc->efi_parts[0].p_start = start_block;
3782 vtoc->efi_parts[0].p_size = slice_size;
3785 * Why we use V_USR: V_BACKUP confuses users, and is considered
3786 * disposable by some EFI utilities (since EFI doesn't have a backup
3787 * slice). V_UNASSIGNED is supposed to be used only for zero size
3788 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
3789 * etc. were all pretty specific. V_USR is as close to reality as we
3790 * can get, in the absence of V_OTHER.
3792 vtoc->efi_parts[0].p_tag = V_USR;
3793 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
3795 vtoc->efi_parts[8].p_start = slice_size + start_block;
3796 vtoc->efi_parts[8].p_size = resv;
3797 vtoc->efi_parts[8].p_tag = V_RESERVED;
3799 if (efi_write(fd, vtoc) != 0) {
3801 * Some block drivers (like pcata) may not support EFI
3802 * GPT labels. Print out a helpful error message dir-
3803 * ecting the user to manually label the disk and give
3809 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3810 "try using fdisk(1M) and then provide a specific slice"));
3811 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3821 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
3827 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
3828 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
3829 strcmp(type, VDEV_TYPE_FILE) == 0 ||
3830 strcmp(type, VDEV_TYPE_LOG) == 0 ||
3831 strcmp(type, VDEV_TYPE_HOLE) == 0 ||
3832 strcmp(type, VDEV_TYPE_MISSING) == 0) {
3833 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3834 "vdev type '%s' is not supported"), type);
3835 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
3838 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
3839 &child, &children) == 0) {
3840 for (c = 0; c < children; c++) {
3841 if (!supported_dump_vdev_type(hdl, child[c], errbuf))
3849 * check if this zvol is allowable for use as a dump device; zero if
3850 * it is, > 0 if it isn't, < 0 if it isn't a zvol
3853 zvol_check_dump_config(char *arg)
3855 zpool_handle_t *zhp = NULL;
3856 nvlist_t *config, *nvroot;
3860 libzfs_handle_t *hdl;
3862 char poolname[ZPOOL_MAXNAMELEN];
3863 int pathlen = strlen(ZVOL_FULL_DEV_DIR);
3866 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
3870 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3871 "dump is not supported on device '%s'"), arg);
3873 if ((hdl = libzfs_init()) == NULL)
3875 libzfs_print_on_error(hdl, B_TRUE);
3877 volname = arg + pathlen;
3879 /* check the configuration of the pool */
3880 if ((p = strchr(volname, '/')) == NULL) {
3881 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3882 "malformed dataset name"));
3883 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3885 } else if (p - volname >= ZFS_MAXNAMELEN) {
3886 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3887 "dataset name is too long"));
3888 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
3891 (void) strncpy(poolname, volname, p - volname);
3892 poolname[p - volname] = '\0';
3895 if ((zhp = zpool_open(hdl, poolname)) == NULL) {
3896 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3897 "could not open pool '%s'"), poolname);
3898 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
3901 config = zpool_get_config(zhp, NULL);
3902 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3904 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3905 "could not obtain vdev configuration for '%s'"), poolname);
3906 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
3910 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3911 &top, &toplevels) == 0);
3912 if (toplevels != 1) {
3913 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3914 "'%s' has multiple top level vdevs"), poolname);
3915 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
3919 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {