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.
39 #include <sys/efi_partition.h>
41 #include <sys/zfs_ioctl.h>
44 #include "zfs_namecheck.h"
46 #include "libzfs_impl.h"
47 #include "zfs_comutil.h"
49 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
51 typedef struct prop_flags {
52 int create:1; /* Validate property on creation */
53 int import:1; /* Validate property on import */
57 * ====================================================================
58 * zpool property functions
59 * ====================================================================
63 zpool_get_all_props(zpool_handle_t *zhp)
65 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
66 libzfs_handle_t *hdl = zhp->zpool_hdl;
68 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
70 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
73 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
74 if (errno == ENOMEM) {
75 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
76 zcmd_free_nvlists(&zc);
80 zcmd_free_nvlists(&zc);
85 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
86 zcmd_free_nvlists(&zc);
90 zcmd_free_nvlists(&zc);
96 zpool_props_refresh(zpool_handle_t *zhp)
100 old_props = zhp->zpool_props;
102 if (zpool_get_all_props(zhp) != 0)
105 nvlist_free(old_props);
110 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
116 zprop_source_t source;
118 nvl = zhp->zpool_props;
119 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
120 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
122 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
124 source = ZPROP_SRC_DEFAULT;
125 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
136 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
140 zprop_source_t source;
142 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
144 * zpool_get_all_props() has most likely failed because
145 * the pool is faulted, but if all we need is the top level
146 * vdev's guid then get it from the zhp config nvlist.
148 if ((prop == ZPOOL_PROP_GUID) &&
149 (nvlist_lookup_nvlist(zhp->zpool_config,
150 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
151 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
155 return (zpool_prop_default_numeric(prop));
158 nvl = zhp->zpool_props;
159 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
160 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
162 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
164 source = ZPROP_SRC_DEFAULT;
165 value = zpool_prop_default_numeric(prop);
175 * Map VDEV STATE to printed strings.
178 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
183 case VDEV_STATE_CLOSED:
184 case VDEV_STATE_OFFLINE:
185 return (gettext("OFFLINE"));
186 case VDEV_STATE_REMOVED:
187 return (gettext("REMOVED"));
188 case VDEV_STATE_CANT_OPEN:
189 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
190 return (gettext("FAULTED"));
191 else if (aux == VDEV_AUX_SPLIT_POOL)
192 return (gettext("SPLIT"));
194 return (gettext("UNAVAIL"));
195 case VDEV_STATE_FAULTED:
196 return (gettext("FAULTED"));
197 case VDEV_STATE_DEGRADED:
198 return (gettext("DEGRADED"));
199 case VDEV_STATE_HEALTHY:
200 return (gettext("ONLINE"));
203 return (gettext("UNKNOWN"));
207 * Get a zpool property value for 'prop' and return the value in
208 * a pre-allocated buffer.
211 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
212 zprop_source_t *srctype)
216 zprop_source_t src = ZPROP_SRC_NONE;
221 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
223 case ZPOOL_PROP_NAME:
224 (void) strlcpy(buf, zpool_get_name(zhp), len);
227 case ZPOOL_PROP_HEALTH:
228 (void) strlcpy(buf, "FAULTED", len);
231 case ZPOOL_PROP_GUID:
232 intval = zpool_get_prop_int(zhp, prop, &src);
233 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
236 case ZPOOL_PROP_ALTROOT:
237 case ZPOOL_PROP_CACHEFILE:
238 case ZPOOL_PROP_COMMENT:
239 if (zhp->zpool_props != NULL ||
240 zpool_get_all_props(zhp) == 0) {
242 zpool_get_prop_string(zhp, prop, &src),
250 (void) strlcpy(buf, "-", len);
259 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
260 prop != ZPOOL_PROP_NAME)
263 switch (zpool_prop_get_type(prop)) {
264 case PROP_TYPE_STRING:
265 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
269 case PROP_TYPE_NUMBER:
270 intval = zpool_get_prop_int(zhp, prop, &src);
273 case ZPOOL_PROP_SIZE:
274 case ZPOOL_PROP_ALLOCATED:
275 case ZPOOL_PROP_FREE:
276 case ZPOOL_PROP_EXPANDSZ:
277 case ZPOOL_PROP_ASHIFT:
278 (void) zfs_nicenum(intval, buf, len);
281 case ZPOOL_PROP_CAPACITY:
282 (void) snprintf(buf, len, "%llu%%",
283 (u_longlong_t)intval);
286 case ZPOOL_PROP_DEDUPRATIO:
287 (void) snprintf(buf, len, "%llu.%02llux",
288 (u_longlong_t)(intval / 100),
289 (u_longlong_t)(intval % 100));
292 case ZPOOL_PROP_HEALTH:
293 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
294 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
295 verify(nvlist_lookup_uint64_array(nvroot,
296 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
299 (void) strlcpy(buf, zpool_state_to_name(intval,
303 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
307 case PROP_TYPE_INDEX:
308 intval = zpool_get_prop_int(zhp, prop, &src);
309 if (zpool_prop_index_to_string(prop, intval, &strval)
312 (void) strlcpy(buf, strval, len);
326 * Check if the bootfs name has the same pool name as it is set to.
327 * Assuming bootfs is a valid dataset name.
330 bootfs_name_valid(const char *pool, char *bootfs)
332 int len = strlen(pool);
334 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
337 if (strncmp(pool, bootfs, len) == 0 &&
338 (bootfs[len] == '/' || bootfs[len] == '\0'))
345 * Inspect the configuration to determine if any of the devices contain
349 pool_uses_efi(nvlist_t *config)
354 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
355 &child, &children) != 0)
356 return (read_efi_label(config, NULL) >= 0);
358 for (c = 0; c < children; c++) {
359 if (pool_uses_efi(child[c]))
366 zpool_is_bootable(zpool_handle_t *zhp)
368 char bootfs[ZPOOL_MAXNAMELEN];
370 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
371 sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
372 sizeof (bootfs)) != 0);
377 * Given an nvlist of zpool properties to be set, validate that they are
378 * correct, and parse any numeric properties (index, boolean, etc) if they are
379 * specified as strings.
382 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
383 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
391 struct stat64 statbuf;
395 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
396 (void) no_memory(hdl);
401 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
402 const char *propname = nvpair_name(elem);
405 * Make sure this property is valid and applies to this type.
407 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
408 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
409 "invalid property '%s'"), propname);
410 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
414 if (zpool_prop_readonly(prop)) {
415 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
416 "is readonly"), propname);
417 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
421 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
422 &strval, &intval, errbuf) != 0)
426 * Perform additional checking for specific properties.
431 case ZPOOL_PROP_VERSION:
432 if (intval < version || intval > SPA_VERSION) {
433 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
434 "property '%s' number %d is invalid."),
436 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
441 case ZPOOL_PROP_ASHIFT:
443 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
444 "property '%s' can only be set at "
445 "creation time"), propname);
446 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
450 if (intval != 0 && (intval < 9 || intval > 13)) {
451 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
452 "property '%s' number %d is invalid."),
454 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
459 case ZPOOL_PROP_BOOTFS:
460 if (flags.create || flags.import) {
461 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
462 "property '%s' cannot be set at creation "
463 "or import time"), propname);
464 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
468 if (version < SPA_VERSION_BOOTFS) {
469 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
470 "pool must be upgraded to support "
471 "'%s' property"), propname);
472 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
477 * bootfs property value has to be a dataset name and
478 * the dataset has to be in the same pool as it sets to.
480 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
482 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
483 "is an invalid name"), strval);
484 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
488 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
489 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
490 "could not open pool '%s'"), poolname);
491 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
494 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
495 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
497 #if defined(__sun__) || defined(__sun)
499 * bootfs property cannot be set on a disk which has
502 if (pool_uses_efi(nvroot)) {
503 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
504 "property '%s' not supported on "
505 "EFI labeled devices"), propname);
506 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
514 case ZPOOL_PROP_ALTROOT:
515 if (!flags.create && !flags.import) {
516 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
517 "property '%s' can only be set during pool "
518 "creation or import"), propname);
519 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
523 if (strval[0] != '/') {
524 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
525 "bad alternate root '%s'"), strval);
526 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
531 case ZPOOL_PROP_CACHEFILE:
532 if (strval[0] == '\0')
535 if (strcmp(strval, "none") == 0)
538 if (strval[0] != '/') {
539 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
540 "property '%s' must be empty, an "
541 "absolute path, or 'none'"), propname);
542 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
546 slash = strrchr(strval, '/');
548 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
549 strcmp(slash, "/..") == 0) {
550 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
551 "'%s' is not a valid file"), strval);
552 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
558 if (strval[0] != '\0' &&
559 (stat64(strval, &statbuf) != 0 ||
560 !S_ISDIR(statbuf.st_mode))) {
561 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
562 "'%s' is not a valid directory"),
564 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
571 case ZPOOL_PROP_COMMENT:
572 for (check = strval; *check != '\0'; check++) {
573 if (!isprint(*check)) {
575 dgettext(TEXT_DOMAIN,
576 "comment may only have printable "
578 (void) zfs_error(hdl, EZFS_BADPROP,
583 if (strlen(strval) > ZPROP_MAX_COMMENT) {
584 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
585 "comment must not exceed %d characters"),
587 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
591 case ZPOOL_PROP_READONLY:
593 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
594 "property '%s' can only be set at "
595 "import time"), propname);
596 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
605 nvlist_free(retprops);
610 * Set zpool property : propname=propval.
613 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
615 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
618 nvlist_t *nvl = NULL;
621 prop_flags_t flags = { 0 };
623 (void) snprintf(errbuf, sizeof (errbuf),
624 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
627 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
628 return (no_memory(zhp->zpool_hdl));
630 if (nvlist_add_string(nvl, propname, propval) != 0) {
632 return (no_memory(zhp->zpool_hdl));
635 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
636 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
637 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
646 * Execute the corresponding ioctl() to set this property.
648 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
650 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
655 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
657 zcmd_free_nvlists(&zc);
661 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
663 (void) zpool_props_refresh(zhp);
669 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
671 libzfs_handle_t *hdl = zhp->zpool_hdl;
673 char buf[ZFS_MAXPROPLEN];
675 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
678 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
683 if (entry->pl_prop != ZPROP_INVAL &&
684 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
686 if (strlen(buf) > entry->pl_width)
687 entry->pl_width = strlen(buf);
696 * Don't start the slice at the default block of 34; many storage
697 * devices will use a stripe width of 128k, other vendors prefer a 1m
698 * alignment. It is best to play it safe and ensure a 1m alignment
699 * given 512B blocks. When the block size is larger by a power of 2
700 * we will still be 1m aligned. Some devices are sensitive to the
701 * partition ending alignment as well.
703 #define NEW_START_BLOCK 2048
704 #define PARTITION_END_ALIGNMENT 2048
707 * Validate the given pool name, optionally putting an extended error message in
711 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
717 ret = pool_namecheck(pool, &why, &what);
720 * The rules for reserved pool names were extended at a later point.
721 * But we need to support users with existing pools that may now be
722 * invalid. So we only check for this expanded set of names during a
723 * create (or import), and only in userland.
725 if (ret == 0 && !isopen &&
726 (strncmp(pool, "mirror", 6) == 0 ||
727 strncmp(pool, "raidz", 5) == 0 ||
728 strncmp(pool, "spare", 5) == 0 ||
729 strcmp(pool, "log") == 0)) {
732 dgettext(TEXT_DOMAIN, "name is reserved"));
740 case NAME_ERR_TOOLONG:
742 dgettext(TEXT_DOMAIN, "name is too long"));
745 case NAME_ERR_INVALCHAR:
747 dgettext(TEXT_DOMAIN, "invalid character "
748 "'%c' in pool name"), what);
751 case NAME_ERR_NOLETTER:
752 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
753 "name must begin with a letter"));
756 case NAME_ERR_RESERVED:
757 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
758 "name is reserved"));
761 case NAME_ERR_DISKLIKE:
762 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
763 "pool name is reserved"));
766 case NAME_ERR_LEADING_SLASH:
767 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
768 "leading slash in name"));
771 case NAME_ERR_EMPTY_COMPONENT:
772 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
773 "empty component in name"));
776 case NAME_ERR_TRAILING_SLASH:
777 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
778 "trailing slash in name"));
781 case NAME_ERR_MULTIPLE_AT:
782 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
783 "multiple '@' delimiters in name"));
786 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
787 "permission set is missing '@'"));
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", "\0", "\0", "\0", 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
999 * label. This can also happen under if the device is
1000 * part of an active md or lvm device.
1002 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1003 "one or more vdevs refer to the same device, or one of\n"
1004 "the devices is part of an active md or lvm device"));
1005 return (zfs_error(hdl, EZFS_BADDEV, msg));
1009 * This occurs when one of the devices is below
1010 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1011 * device was the problem device since there's no
1012 * reliable way to determine device size from userland.
1017 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1019 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1020 "one or more devices is less than the "
1021 "minimum size (%s)"), buf);
1023 return (zfs_error(hdl, EZFS_BADDEV, msg));
1026 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1027 "one or more devices is out of space"));
1028 return (zfs_error(hdl, EZFS_BADDEV, msg));
1031 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1032 "cache device must be a disk or disk slice"));
1033 return (zfs_error(hdl, EZFS_BADDEV, msg));
1036 return (zpool_standard_error(hdl, errno, msg));
1041 * If this is an alternate root pool, then we automatically set the
1042 * mountpoint of the root dataset to be '/'.
1044 if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
1048 verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
1049 verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1056 zcmd_free_nvlists(&zc);
1057 nvlist_free(zc_props);
1058 nvlist_free(zc_fsprops);
1063 * Destroy the given pool. It is up to the caller to ensure that there are no
1064 * datasets left in the pool.
1067 zpool_destroy(zpool_handle_t *zhp)
1069 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1070 zfs_handle_t *zfp = NULL;
1071 libzfs_handle_t *hdl = zhp->zpool_hdl;
1074 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1075 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1078 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1080 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1081 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1082 "cannot destroy '%s'"), zhp->zpool_name);
1084 if (errno == EROFS) {
1085 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1086 "one or more devices is read only"));
1087 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1089 (void) zpool_standard_error(hdl, errno, msg);
1098 remove_mountpoint(zfp);
1106 * Add the given vdevs to the pool. The caller must have already performed the
1107 * necessary verification to ensure that the vdev specification is well-formed.
1110 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1112 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1114 libzfs_handle_t *hdl = zhp->zpool_hdl;
1116 nvlist_t **spares, **l2cache;
1117 uint_t nspares, nl2cache;
1119 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1120 "cannot add to '%s'"), zhp->zpool_name);
1122 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1123 SPA_VERSION_SPARES &&
1124 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1125 &spares, &nspares) == 0) {
1126 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1127 "upgraded to add hot spares"));
1128 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1131 if (zpool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1132 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1135 for (s = 0; s < nspares; s++) {
1138 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1139 &path) == 0 && pool_uses_efi(spares[s])) {
1140 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1141 "device '%s' contains an EFI label and "
1142 "cannot be used on root pools."),
1143 zpool_vdev_name(hdl, NULL, spares[s],
1145 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1150 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1151 SPA_VERSION_L2CACHE &&
1152 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1153 &l2cache, &nl2cache) == 0) {
1154 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1155 "upgraded to add cache devices"));
1156 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1159 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1161 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1163 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1167 * This can happen if the user has specified the same
1168 * device multiple times. We can't reliably detect this
1169 * until we try to add it and see we already have a
1172 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1173 "one or more vdevs refer to the same device"));
1174 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1179 * This occurrs when one of the devices is below
1180 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1181 * device was the problem device since there's no
1182 * reliable way to determine device size from userland.
1187 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1189 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1190 "device is less than the minimum "
1193 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1197 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1198 "pool must be upgraded to add these vdevs"));
1199 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1203 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1204 "root pool can not have multiple vdevs"
1205 " or separate logs"));
1206 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1210 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1211 "cache device must be a disk or disk slice"));
1212 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1216 (void) zpool_standard_error(hdl, errno, msg);
1224 zcmd_free_nvlists(&zc);
1230 * Exports the pool from the system. The caller must ensure that there are no
1231 * mounted datasets in the pool.
1234 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1236 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1239 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1240 "cannot export '%s'"), zhp->zpool_name);
1242 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1243 zc.zc_cookie = force;
1244 zc.zc_guid = hardforce;
1246 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1249 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1250 "use '-f' to override the following errors:\n"
1251 "'%s' has an active shared spare which could be"
1252 " used by other pools once '%s' is exported."),
1253 zhp->zpool_name, zhp->zpool_name);
1254 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1257 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1266 zpool_export(zpool_handle_t *zhp, boolean_t force)
1268 return (zpool_export_common(zhp, force, B_FALSE));
1272 zpool_export_force(zpool_handle_t *zhp)
1274 return (zpool_export_common(zhp, B_TRUE, B_TRUE));
1278 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1281 nvlist_t *nv = NULL;
1287 if (!hdl->libzfs_printerr || config == NULL)
1290 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0)
1293 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1295 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1297 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1298 strftime(timestr, 128, "%c", &t) != 0) {
1300 (void) printf(dgettext(TEXT_DOMAIN,
1301 "Would be able to return %s "
1302 "to its state as of %s.\n"),
1305 (void) printf(dgettext(TEXT_DOMAIN,
1306 "Pool %s returned to its state as of %s.\n"),
1310 (void) printf(dgettext(TEXT_DOMAIN,
1311 "%s approximately %lld "),
1312 dryrun ? "Would discard" : "Discarded",
1313 ((longlong_t)loss + 30) / 60);
1314 (void) printf(dgettext(TEXT_DOMAIN,
1315 "minutes of transactions.\n"));
1316 } else if (loss > 0) {
1317 (void) printf(dgettext(TEXT_DOMAIN,
1318 "%s approximately %lld "),
1319 dryrun ? "Would discard" : "Discarded",
1321 (void) printf(dgettext(TEXT_DOMAIN,
1322 "seconds of transactions.\n"));
1328 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1331 nvlist_t *nv = NULL;
1333 uint64_t edata = UINT64_MAX;
1338 if (!hdl->libzfs_printerr)
1342 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1344 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1346 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1347 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1348 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1351 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1352 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1355 (void) printf(dgettext(TEXT_DOMAIN,
1356 "Recovery is possible, but will result in some data loss.\n"));
1358 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1359 strftime(timestr, 128, "%c", &t) != 0) {
1360 (void) printf(dgettext(TEXT_DOMAIN,
1361 "\tReturning the pool to its state as of %s\n"
1362 "\tshould correct the problem. "),
1365 (void) printf(dgettext(TEXT_DOMAIN,
1366 "\tReverting the pool to an earlier state "
1367 "should correct the problem.\n\t"));
1371 (void) printf(dgettext(TEXT_DOMAIN,
1372 "Approximately %lld minutes of data\n"
1373 "\tmust be discarded, irreversibly. "),
1374 ((longlong_t)loss + 30) / 60);
1375 } else if (loss > 0) {
1376 (void) printf(dgettext(TEXT_DOMAIN,
1377 "Approximately %lld seconds of data\n"
1378 "\tmust be discarded, irreversibly. "),
1381 if (edata != 0 && edata != UINT64_MAX) {
1383 (void) printf(dgettext(TEXT_DOMAIN,
1384 "After rewind, at least\n"
1385 "\tone persistent user-data error will remain. "));
1387 (void) printf(dgettext(TEXT_DOMAIN,
1388 "After rewind, several\n"
1389 "\tpersistent user-data errors will remain. "));
1392 (void) printf(dgettext(TEXT_DOMAIN,
1393 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1394 reason >= 0 ? "clear" : "import", name);
1396 (void) printf(dgettext(TEXT_DOMAIN,
1397 "A scrub of the pool\n"
1398 "\tis strongly recommended after recovery.\n"));
1402 (void) printf(dgettext(TEXT_DOMAIN,
1403 "Destroy and re-create the pool from\n\ta backup source.\n"));
1407 * zpool_import() is a contracted interface. Should be kept the same
1410 * Applications should use zpool_import_props() to import a pool with
1411 * new properties value to be set.
1414 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1417 nvlist_t *props = NULL;
1420 if (altroot != NULL) {
1421 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1422 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1423 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1427 if (nvlist_add_string(props,
1428 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1429 nvlist_add_string(props,
1430 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1432 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1433 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1438 ret = zpool_import_props(hdl, config, newname, props,
1446 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1452 uint64_t is_log = 0;
1454 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1458 (void) printf("\t%*s%s%s\n", indent, "", name,
1459 is_log ? " [log]" : "");
1461 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1462 &child, &children) != 0)
1465 for (c = 0; c < children; c++) {
1466 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
1467 print_vdev_tree(hdl, vname, child[c], indent + 2);
1473 * Import the given pool using the known configuration and a list of
1474 * properties to be set. The configuration should have come from
1475 * zpool_find_import(). The 'newname' parameters control whether the pool
1476 * is imported with a different name.
1479 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1480 nvlist_t *props, int flags)
1482 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1483 zpool_rewind_policy_t policy;
1484 nvlist_t *nv = NULL;
1485 nvlist_t *nvinfo = NULL;
1486 nvlist_t *missing = NULL;
1493 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1496 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1497 "cannot import pool '%s'"), origname);
1499 if (newname != NULL) {
1500 if (!zpool_name_valid(hdl, B_FALSE, newname))
1501 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1502 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1504 thename = (char *)newname;
1511 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1513 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1516 if ((props = zpool_valid_proplist(hdl, origname,
1517 props, version, flags, errbuf)) == NULL) {
1519 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1525 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1527 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1530 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1534 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1539 zc.zc_cookie = flags;
1540 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1542 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1543 zcmd_free_nvlists(&zc);
1550 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1551 zpool_get_rewind_policy(config, &policy);
1557 * Dry-run failed, but we print out what success
1558 * looks like if we found a best txg
1560 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1561 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1567 if (newname == NULL)
1568 (void) snprintf(desc, sizeof (desc),
1569 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1572 (void) snprintf(desc, sizeof (desc),
1573 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1579 * Unsupported version.
1581 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1585 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1589 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1590 "one or more devices is read only"));
1591 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1595 if (nv && nvlist_lookup_nvlist(nv,
1596 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1597 nvlist_lookup_nvlist(nvinfo,
1598 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1599 (void) printf(dgettext(TEXT_DOMAIN,
1600 "The devices below are missing, use "
1601 "'-m' to import the pool anyway:\n"));
1602 print_vdev_tree(hdl, NULL, missing, 2);
1603 (void) printf("\n");
1605 (void) zpool_standard_error(hdl, error, desc);
1609 (void) zpool_standard_error(hdl, error, desc);
1613 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1614 "one or more devices are already in use\n"));
1615 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1619 (void) zpool_standard_error(hdl, error, desc);
1620 zpool_explain_recover(hdl,
1621 newname ? origname : thename, -error, nv);
1628 zpool_handle_t *zhp;
1631 * This should never fail, but play it safe anyway.
1633 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1635 else if (zhp != NULL)
1637 if (policy.zrp_request &
1638 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1639 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1640 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1646 zcmd_free_nvlists(&zc);
1656 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1658 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1660 libzfs_handle_t *hdl = zhp->zpool_hdl;
1662 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1663 zc.zc_cookie = func;
1665 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
1666 (errno == ENOENT && func != POOL_SCAN_NONE))
1669 if (func == POOL_SCAN_SCRUB) {
1670 (void) snprintf(msg, sizeof (msg),
1671 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1672 } else if (func == POOL_SCAN_NONE) {
1673 (void) snprintf(msg, sizeof (msg),
1674 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1677 assert(!"unexpected result");
1680 if (errno == EBUSY) {
1682 pool_scan_stat_t *ps = NULL;
1685 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1686 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1687 (void) nvlist_lookup_uint64_array(nvroot,
1688 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1689 if (ps && ps->pss_func == POOL_SCAN_SCRUB)
1690 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1692 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1693 } else if (errno == ENOENT) {
1694 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1696 return (zpool_standard_error(hdl, errno, msg));
1701 * Find a vdev that matches the search criteria specified. We use the
1702 * the nvpair name to determine how we should look for the device.
1703 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1704 * spare; but FALSE if its an INUSE spare.
1707 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1708 boolean_t *l2cache, boolean_t *log)
1715 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1717 /* Nothing to look for */
1718 if (search == NULL || pair == NULL)
1721 /* Obtain the key we will use to search */
1722 srchkey = nvpair_name(pair);
1724 switch (nvpair_type(pair)) {
1725 case DATA_TYPE_UINT64:
1726 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1727 uint64_t srchval, theguid;
1729 verify(nvpair_value_uint64(pair, &srchval) == 0);
1730 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1732 if (theguid == srchval)
1737 case DATA_TYPE_STRING: {
1738 char *srchval, *val;
1740 verify(nvpair_value_string(pair, &srchval) == 0);
1741 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1745 * Search for the requested value. Special cases:
1747 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in with a
1748 * partition suffix "1", "-part1", or "p1". The suffix is hidden
1749 * from the user, but included in the string, so this matches around
1751 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1753 * Otherwise, all other searches are simple string compares.
1755 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) {
1756 uint64_t wholedisk = 0;
1758 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1761 char buf[MAXPATHLEN];
1763 zfs_append_partition(srchval, buf, sizeof (buf));
1764 if (strcmp(val, buf) == 0)
1769 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
1770 char *type, *idx, *end, *p;
1771 uint64_t id, vdev_id;
1774 * Determine our vdev type, keeping in mind
1775 * that the srchval is composed of a type and
1776 * vdev id pair (i.e. mirror-4).
1778 if ((type = strdup(srchval)) == NULL)
1781 if ((p = strrchr(type, '-')) == NULL) {
1789 * If the types don't match then keep looking.
1791 if (strncmp(val, type, strlen(val)) != 0) {
1796 verify(strncmp(type, VDEV_TYPE_RAIDZ,
1797 strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1798 strncmp(type, VDEV_TYPE_MIRROR,
1799 strlen(VDEV_TYPE_MIRROR)) == 0);
1800 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
1804 vdev_id = strtoull(idx, &end, 10);
1811 * Now verify that we have the correct vdev id.
1820 if (strcmp(srchval, val) == 0)
1829 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1830 &child, &children) != 0)
1833 for (c = 0; c < children; c++) {
1834 if ((ret = vdev_to_nvlist_iter(child[c], search,
1835 avail_spare, l2cache, NULL)) != NULL) {
1837 * The 'is_log' value is only set for the toplevel
1838 * vdev, not the leaf vdevs. So we always lookup the
1839 * log device from the root of the vdev tree (where
1840 * 'log' is non-NULL).
1843 nvlist_lookup_uint64(child[c],
1844 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
1852 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1853 &child, &children) == 0) {
1854 for (c = 0; c < children; c++) {
1855 if ((ret = vdev_to_nvlist_iter(child[c], search,
1856 avail_spare, l2cache, NULL)) != NULL) {
1857 *avail_spare = B_TRUE;
1863 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1864 &child, &children) == 0) {
1865 for (c = 0; c < children; c++) {
1866 if ((ret = vdev_to_nvlist_iter(child[c], search,
1867 avail_spare, l2cache, NULL)) != NULL) {
1878 * Given a physical path (minus the "/devices" prefix), find the
1882 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
1883 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
1885 nvlist_t *search, *nvroot, *ret;
1887 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1888 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
1890 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1893 *avail_spare = B_FALSE;
1897 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1898 nvlist_free(search);
1904 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
1907 zpool_vdev_is_interior(const char *name)
1909 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1910 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
1916 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
1917 boolean_t *l2cache, boolean_t *log)
1919 char buf[MAXPATHLEN];
1921 nvlist_t *nvroot, *search, *ret;
1924 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1926 guid = strtoull(path, &end, 10);
1927 if (guid != 0 && *end == '\0') {
1928 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
1929 } else if (zpool_vdev_is_interior(path)) {
1930 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
1931 } else if (path[0] != '/') {
1932 if (zfs_resolve_shortname(path, buf, sizeof (buf)) < 0) {
1933 nvlist_free(search);
1936 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
1938 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
1941 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1944 *avail_spare = B_FALSE;
1948 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1949 nvlist_free(search);
1955 vdev_online(nvlist_t *nv)
1959 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
1960 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
1961 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
1968 * Helper function for zpool_get_physpaths().
1971 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
1972 size_t *bytes_written)
1974 size_t bytes_left, pos, rsz;
1978 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
1980 return (EZFS_NODEVICE);
1982 pos = *bytes_written;
1983 bytes_left = physpath_size - pos;
1984 format = (pos == 0) ? "%s" : " %s";
1986 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
1987 *bytes_written += rsz;
1989 if (rsz >= bytes_left) {
1990 /* if physpath was not copied properly, clear it */
1991 if (bytes_left != 0) {
1994 return (EZFS_NOSPC);
2000 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2001 size_t *rsz, boolean_t is_spare)
2006 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2007 return (EZFS_INVALCONFIG);
2009 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2011 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2012 * For a spare vdev, we only want to boot from the active
2017 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2020 return (EZFS_INVALCONFIG);
2023 if (vdev_online(nv)) {
2024 if ((ret = vdev_get_one_physpath(nv, physpath,
2025 phypath_size, rsz)) != 0)
2028 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2029 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2030 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2035 if (nvlist_lookup_nvlist_array(nv,
2036 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2037 return (EZFS_INVALCONFIG);
2039 for (i = 0; i < count; i++) {
2040 ret = vdev_get_physpaths(child[i], physpath,
2041 phypath_size, rsz, is_spare);
2042 if (ret == EZFS_NOSPC)
2047 return (EZFS_POOL_INVALARG);
2051 * Get phys_path for a root pool config.
2052 * Return 0 on success; non-zero on failure.
2055 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2058 nvlist_t *vdev_root;
2065 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2067 return (EZFS_INVALCONFIG);
2069 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2070 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2071 &child, &count) != 0)
2072 return (EZFS_INVALCONFIG);
2075 * root pool can not have EFI labeled disks and can only have
2076 * a single top-level vdev.
2078 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
2079 pool_uses_efi(vdev_root))
2080 return (EZFS_POOL_INVALARG);
2082 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2085 /* No online devices */
2087 return (EZFS_NODEVICE);
2093 * Get phys_path for a root pool
2094 * Return 0 on success; non-zero on failure.
2097 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2099 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2104 * If the device has being dynamically expanded then we need to relabel
2105 * the disk to use the new unallocated space.
2108 zpool_relabel_disk(libzfs_handle_t *hdl, const char *path, const char *msg)
2112 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
2113 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2114 "relabel '%s': unable to open device: %d"), path, errno);
2115 return (zfs_error(hdl, EZFS_OPENFAILED, msg));
2119 * It's possible that we might encounter an error if the device
2120 * does not have any unallocated space left. If so, we simply
2121 * ignore that error and continue on.
2123 * Also, we don't call efi_rescan() - that would just return EBUSY.
2124 * The module will do it for us in vdev_disk_open().
2126 error = efi_use_whole_disk(fd);
2128 if (error && error != VT_ENOSPC) {
2129 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2130 "relabel '%s': unable to read disk capacity"), path);
2131 return (zfs_error(hdl, EZFS_NOCAP, msg));
2137 * Bring the specified vdev online. The 'flags' parameter is a set of the
2138 * ZFS_ONLINE_* flags.
2141 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2142 vdev_state_t *newstate)
2144 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2147 boolean_t avail_spare, l2cache, islog;
2148 libzfs_handle_t *hdl = zhp->zpool_hdl;
2151 if (flags & ZFS_ONLINE_EXPAND) {
2152 (void) snprintf(msg, sizeof (msg),
2153 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2155 (void) snprintf(msg, sizeof (msg),
2156 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2159 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2160 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2162 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2164 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2167 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2169 if (flags & ZFS_ONLINE_EXPAND ||
2170 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2171 uint64_t wholedisk = 0;
2173 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2177 * XXX - L2ARC 1.0 devices can't support expansion.
2180 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2181 "cannot expand cache devices"));
2182 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2186 const char *fullpath = path;
2187 char buf[MAXPATHLEN];
2189 if (path[0] != '/') {
2190 error = zfs_resolve_shortname(path, buf,
2193 return (zfs_error(hdl, EZFS_NODEVICE,
2199 error = zpool_relabel_disk(hdl, fullpath, msg);
2205 zc.zc_cookie = VDEV_STATE_ONLINE;
2208 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2209 if (errno == EINVAL) {
2210 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2211 "from this pool into a new one. Use '%s' "
2212 "instead"), "zpool detach");
2213 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2215 return (zpool_standard_error(hdl, errno, msg));
2218 *newstate = zc.zc_cookie;
2223 * Take the specified vdev offline
2226 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2228 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2231 boolean_t avail_spare, l2cache;
2232 libzfs_handle_t *hdl = zhp->zpool_hdl;
2234 (void) snprintf(msg, sizeof (msg),
2235 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2237 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2238 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2240 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2242 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2245 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2247 zc.zc_cookie = VDEV_STATE_OFFLINE;
2248 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2250 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2257 * There are no other replicas of this device.
2259 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2263 * The log device has unplayed logs
2265 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2268 return (zpool_standard_error(hdl, errno, msg));
2273 * Mark the given vdev faulted.
2276 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2278 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2280 libzfs_handle_t *hdl = zhp->zpool_hdl;
2282 (void) snprintf(msg, sizeof (msg),
2283 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
2285 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2287 zc.zc_cookie = VDEV_STATE_FAULTED;
2290 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2297 * There are no other replicas of this device.
2299 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2302 return (zpool_standard_error(hdl, errno, msg));
2308 * Mark the given vdev degraded.
2311 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2313 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2315 libzfs_handle_t *hdl = zhp->zpool_hdl;
2317 (void) snprintf(msg, sizeof (msg),
2318 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid);
2320 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2322 zc.zc_cookie = VDEV_STATE_DEGRADED;
2325 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2328 return (zpool_standard_error(hdl, errno, msg));
2332 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2336 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2342 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2344 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2347 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2348 children == 2 && child[which] == tgt)
2351 for (c = 0; c < children; c++)
2352 if (is_replacing_spare(child[c], tgt, which))
2360 * Attach new_disk (fully described by nvroot) to old_disk.
2361 * If 'replacing' is specified, the new disk will replace the old one.
2364 zpool_vdev_attach(zpool_handle_t *zhp,
2365 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2367 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2371 boolean_t avail_spare, l2cache, islog;
2376 nvlist_t *config_root;
2377 libzfs_handle_t *hdl = zhp->zpool_hdl;
2378 boolean_t rootpool = zpool_is_bootable(zhp);
2381 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2382 "cannot replace %s with %s"), old_disk, new_disk);
2384 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2385 "cannot attach %s to %s"), new_disk, old_disk);
2388 * If this is a root pool, make sure that we're not attaching an
2389 * EFI labeled device.
2391 if (rootpool && pool_uses_efi(nvroot)) {
2392 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2393 "EFI labeled devices are not supported on root pools."));
2394 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2397 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2398 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2400 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2403 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2406 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2408 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2409 zc.zc_cookie = replacing;
2411 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2412 &child, &children) != 0 || children != 1) {
2413 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2414 "new device must be a single disk"));
2415 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2418 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2419 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2421 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2425 * If the target is a hot spare that has been swapped in, we can only
2426 * replace it with another hot spare.
2429 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2430 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2431 NULL) == NULL || !avail_spare) &&
2432 is_replacing_spare(config_root, tgt, 1)) {
2433 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2434 "can only be replaced by another hot spare"));
2436 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2441 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2444 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2446 zcmd_free_nvlists(&zc);
2451 * XXX need a better way to prevent user from
2452 * booting up a half-baked vdev.
2454 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2455 "sure to wait until resilver is done "
2456 "before rebooting.\n"));
2464 * Can't attach to or replace this type of vdev.
2467 uint64_t version = zpool_get_prop_int(zhp,
2468 ZPOOL_PROP_VERSION, NULL);
2471 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2472 "cannot replace a log with a spare"));
2473 else if (version >= SPA_VERSION_MULTI_REPLACE)
2474 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2475 "already in replacing/spare config; wait "
2476 "for completion or use 'zpool detach'"));
2478 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2479 "cannot replace a replacing device"));
2481 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2482 "can only attach to mirrors and top-level "
2485 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2490 * The new device must be a single disk.
2492 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2493 "new device must be a single disk"));
2494 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2498 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2500 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2505 * The new device is too small.
2507 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2508 "device is too small"));
2509 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2514 * The new device has a different alignment requirement.
2516 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2517 "devices have different sector alignment"));
2518 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2523 * The resulting top-level vdev spec won't fit in the label.
2525 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2529 (void) zpool_standard_error(hdl, errno, msg);
2536 * Detach the specified device.
2539 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2541 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2544 boolean_t avail_spare, l2cache;
2545 libzfs_handle_t *hdl = zhp->zpool_hdl;
2547 (void) snprintf(msg, sizeof (msg),
2548 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2550 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2551 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2553 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2556 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2559 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2561 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2563 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2570 * Can't detach from this type of vdev.
2572 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2573 "applicable to mirror and replacing vdevs"));
2574 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2579 * There are no other replicas of this device.
2581 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2585 (void) zpool_standard_error(hdl, errno, msg);
2592 * Find a mirror vdev in the source nvlist.
2594 * The mchild array contains a list of disks in one of the top-level mirrors
2595 * of the source pool. The schild array contains a list of disks that the
2596 * user specified on the command line. We loop over the mchild array to
2597 * see if any entry in the schild array matches.
2599 * If a disk in the mchild array is found in the schild array, we return
2600 * the index of that entry. Otherwise we return -1.
2603 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2604 nvlist_t **schild, uint_t schildren)
2608 for (mc = 0; mc < mchildren; mc++) {
2610 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2611 mchild[mc], B_FALSE);
2613 for (sc = 0; sc < schildren; sc++) {
2614 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2615 schild[sc], B_FALSE);
2616 boolean_t result = (strcmp(mpath, spath) == 0);
2632 * Split a mirror pool. If newroot points to null, then a new nvlist
2633 * is generated and it is the responsibility of the caller to free it.
2636 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2637 nvlist_t *props, splitflags_t flags)
2639 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2641 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2642 nvlist_t **varray = NULL, *zc_props = NULL;
2643 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2644 libzfs_handle_t *hdl = zhp->zpool_hdl;
2646 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2649 (void) snprintf(msg, sizeof (msg),
2650 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2652 if (!zpool_name_valid(hdl, B_FALSE, newname))
2653 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2655 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2656 (void) fprintf(stderr, gettext("Internal error: unable to "
2657 "retrieve pool configuration\n"));
2661 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2663 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2666 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2667 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2668 props, vers, flags, msg)) == NULL)
2672 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2674 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2675 "Source pool is missing vdev tree"));
2677 nvlist_free(zc_props);
2681 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2684 if (*newroot == NULL ||
2685 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2686 &newchild, &newchildren) != 0)
2689 for (c = 0; c < children; c++) {
2690 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2692 nvlist_t **mchild, *vdev;
2697 * Unlike cache & spares, slogs are stored in the
2698 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2700 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2702 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2704 if (is_log || is_hole) {
2706 * Create a hole vdev and put it in the config.
2708 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2710 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2711 VDEV_TYPE_HOLE) != 0)
2713 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2718 varray[vcount++] = vdev;
2722 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
2724 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2725 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2726 "Source pool must be composed only of mirrors\n"));
2727 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2731 verify(nvlist_lookup_nvlist_array(child[c],
2732 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2734 /* find or add an entry for this top-level vdev */
2735 if (newchildren > 0 &&
2736 (entry = find_vdev_entry(zhp, mchild, mchildren,
2737 newchild, newchildren)) >= 0) {
2738 /* We found a disk that the user specified. */
2739 vdev = mchild[entry];
2742 /* User didn't specify a disk for this vdev. */
2743 vdev = mchild[mchildren - 1];
2746 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
2750 /* did we find every disk the user specified? */
2751 if (found != newchildren) {
2752 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
2753 "include at most one disk from each mirror"));
2754 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2758 /* Prepare the nvlist for populating. */
2759 if (*newroot == NULL) {
2760 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
2763 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
2764 VDEV_TYPE_ROOT) != 0)
2767 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
2770 /* Add all the children we found */
2771 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
2772 lastlog == 0 ? vcount : lastlog) != 0)
2776 * If we're just doing a dry run, exit now with success.
2779 memory_err = B_FALSE;
2784 /* now build up the config list & call the ioctl */
2785 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
2788 if (nvlist_add_nvlist(newconfig,
2789 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
2790 nvlist_add_string(newconfig,
2791 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
2792 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
2796 * The new pool is automatically part of the namespace unless we
2797 * explicitly export it.
2800 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
2801 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2802 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
2803 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
2805 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
2808 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
2809 retval = zpool_standard_error(hdl, errno, msg);
2814 memory_err = B_FALSE;
2817 if (varray != NULL) {
2820 for (v = 0; v < vcount; v++)
2821 nvlist_free(varray[v]);
2824 zcmd_free_nvlists(&zc);
2826 nvlist_free(zc_props);
2828 nvlist_free(newconfig);
2830 nvlist_free(*newroot);
2838 return (no_memory(hdl));
2844 * Remove the given device. Currently, this is supported only for hot spares
2845 * and level 2 cache devices.
2848 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
2850 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2853 boolean_t avail_spare, l2cache, islog;
2854 libzfs_handle_t *hdl = zhp->zpool_hdl;
2857 (void) snprintf(msg, sizeof (msg),
2858 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
2860 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2861 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2863 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2865 * XXX - this should just go away.
2867 if (!avail_spare && !l2cache && !islog) {
2868 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2869 "only inactive hot spares, cache, top-level, "
2870 "or log devices can be removed"));
2871 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2874 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
2875 if (islog && version < SPA_VERSION_HOLES) {
2876 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2877 "pool must be upgrade to support log removal"));
2878 return (zfs_error(hdl, EZFS_BADVERSION, msg));
2881 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2883 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
2886 return (zpool_standard_error(hdl, errno, msg));
2890 * Clear the errors for the pool, or the particular device if specified.
2893 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
2895 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2898 zpool_rewind_policy_t policy;
2899 boolean_t avail_spare, l2cache;
2900 libzfs_handle_t *hdl = zhp->zpool_hdl;
2901 nvlist_t *nvi = NULL;
2905 (void) snprintf(msg, sizeof (msg),
2906 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2909 (void) snprintf(msg, sizeof (msg),
2910 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2913 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2915 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
2916 &l2cache, NULL)) == 0)
2917 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2920 * Don't allow error clearing for hot spares. Do allow
2921 * error clearing for l2cache devices.
2924 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2926 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
2930 zpool_get_rewind_policy(rewindnvl, &policy);
2931 zc.zc_cookie = policy.zrp_request;
2933 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
2936 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
2939 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
2941 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
2942 zcmd_free_nvlists(&zc);
2947 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
2948 errno != EPERM && errno != EACCES)) {
2949 if (policy.zrp_request &
2950 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2951 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
2952 zpool_rewind_exclaim(hdl, zc.zc_name,
2953 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
2957 zcmd_free_nvlists(&zc);
2961 zcmd_free_nvlists(&zc);
2962 return (zpool_standard_error(hdl, errno, msg));
2966 * Similar to zpool_clear(), but takes a GUID (used by fmd).
2969 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
2971 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2973 libzfs_handle_t *hdl = zhp->zpool_hdl;
2975 (void) snprintf(msg, sizeof (msg),
2976 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
2977 (u_longlong_t)guid);
2979 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2981 zc.zc_cookie = ZPOOL_NO_REWIND;
2983 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
2986 return (zpool_standard_error(hdl, errno, msg));
2990 * Change the GUID for a pool.
2993 zpool_reguid(zpool_handle_t *zhp)
2996 libzfs_handle_t *hdl = zhp->zpool_hdl;
2997 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2999 (void) snprintf(msg, sizeof (msg),
3000 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3002 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3003 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3006 return (zpool_standard_error(hdl, errno, msg));
3013 zpool_reopen(zpool_handle_t *zhp)
3015 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3017 libzfs_handle_t *hdl = zhp->zpool_hdl;
3019 (void) snprintf(msg, sizeof (msg),
3020 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
3023 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3024 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
3026 return (zpool_standard_error(hdl, errno, msg));
3030 * Convert from a devid string to a path.
3033 devid_to_path(char *devid_str)
3038 devid_nmlist_t *list = NULL;
3041 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3044 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3046 devid_str_free(minor);
3052 if ((path = strdup(list[0].devname)) == NULL)
3055 devid_free_nmlist(list);
3061 * Convert from a path to a devid string.
3064 path_to_devid(const char *path)
3070 if ((fd = open(path, O_RDONLY)) < 0)
3075 if (devid_get(fd, &devid) == 0) {
3076 if (devid_get_minor_name(fd, &minor) == 0)
3077 ret = devid_str_encode(devid, minor);
3079 devid_str_free(minor);
3088 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3089 * ignore any failure here, since a common case is for an unprivileged user to
3090 * type 'zpool status', and we'll display the correct information anyway.
3093 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3095 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3097 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3098 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3099 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3102 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3106 * Remove partition suffix from a vdev path. Partition suffixes may take three
3107 * forms: "-partX", "pX", or "X", where X is a string of digits. The second
3108 * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
3109 * third case only occurs when preceded by a string matching the regular
3110 * expression "^[hs]d[a-z]+", i.e. a scsi or ide disk.
3113 strip_partition(libzfs_handle_t *hdl, char *path)
3115 char *tmp = zfs_strdup(hdl, path);
3116 char *part = NULL, *d = NULL;
3118 if ((part = strstr(tmp, "-part")) && part != tmp) {
3120 } else if ((part = strrchr(tmp, 'p')) &&
3121 part > tmp + 1 && isdigit(*(part-1))) {
3123 } else if ((tmp[0] == 'h' || tmp[0] == 's') && tmp[1] == 'd') {
3124 for (d = &tmp[2]; isalpha(*d); part = ++d);
3126 if (part && d && *d != '\0') {
3127 for (; isdigit(*d); d++);
3134 #define PATH_BUF_LEN 64
3137 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3138 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3139 * We also check if this is a whole disk, in which case we strip off the
3140 * trailing 's0' slice name.
3142 * This routine is also responsible for identifying when disks have been
3143 * reconfigured in a new location. The kernel will have opened the device by
3144 * devid, but the path will still refer to the old location. To catch this, we
3145 * first do a path -> devid translation (which is fast for the common case). If
3146 * the devid matches, we're done. If not, we do a reverse devid -> path
3147 * translation and issue the appropriate ioctl() to update the path of the vdev.
3148 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3152 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3155 char *path, *devid, *type;
3157 char buf[PATH_BUF_LEN];
3158 char tmpbuf[PATH_BUF_LEN];
3162 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
3164 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3166 (void) snprintf(buf, sizeof (buf), "%llu",
3167 (u_longlong_t)value);
3169 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3171 * If the device is dead (faulted, offline, etc) then don't
3172 * bother opening it. Otherwise we may be forcing the user to
3173 * open a misbehaving device, which can have undesirable
3176 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3177 (uint64_t **)&vs, &vsc) != 0 ||
3178 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3180 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3182 * Determine if the current path is correct.
3184 char *newdevid = path_to_devid(path);
3186 if (newdevid == NULL ||
3187 strcmp(devid, newdevid) != 0) {
3190 if ((newpath = devid_to_path(devid)) != NULL) {
3192 * Update the path appropriately.
3194 set_path(zhp, nv, newpath);
3195 if (nvlist_add_string(nv,
3196 ZPOOL_CONFIG_PATH, newpath) == 0)
3197 verify(nvlist_lookup_string(nv,
3205 devid_str_free(newdevid);
3209 * For a block device only use the name.
3211 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
3212 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
3213 path = strrchr(path, '/');
3218 * Remove the partition from the path it this is a whole disk.
3220 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3221 &value) == 0 && value) {
3222 return strip_partition(hdl, path);
3225 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3228 * If it's a raidz device, we need to stick in the parity level.
3230 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3232 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3234 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3235 (u_longlong_t)value);
3240 * We identify each top-level vdev by using a <type-id>
3241 * naming convention.
3246 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3248 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s-%llu",
3249 path, (u_longlong_t)id);
3254 return (zfs_strdup(hdl, path));
3258 zbookmark_compare(const void *a, const void *b)
3260 return (memcmp(a, b, sizeof (zbookmark_t)));
3264 * Retrieve the persistent error log, uniquify the members, and return to the
3268 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3270 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3272 zbookmark_t *zb = NULL;
3276 * Retrieve the raw error list from the kernel. If the number of errors
3277 * has increased, allocate more space and continue until we get the
3280 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3284 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3285 count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
3287 zc.zc_nvlist_dst_size = count;
3288 (void) strcpy(zc.zc_name, zhp->zpool_name);
3290 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3292 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3293 if (errno == ENOMEM) {
3294 count = zc.zc_nvlist_dst_size;
3295 if ((zc.zc_nvlist_dst = (uintptr_t)
3296 zfs_alloc(zhp->zpool_hdl, count *
3297 sizeof (zbookmark_t))) == (uintptr_t)NULL)
3308 * Sort the resulting bookmarks. This is a little confusing due to the
3309 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3310 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3311 * _not_ copied as part of the process. So we point the start of our
3312 * array appropriate and decrement the total number of elements.
3314 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
3315 zc.zc_nvlist_dst_size;
3316 count -= zc.zc_nvlist_dst_size;
3318 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
3320 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3323 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3325 for (i = 0; i < count; i++) {
3328 /* ignoring zb_blkid and zb_level for now */
3329 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3330 zb[i-1].zb_object == zb[i].zb_object)
3333 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3335 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3336 zb[i].zb_objset) != 0) {
3340 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3341 zb[i].zb_object) != 0) {
3345 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3352 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3356 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3357 return (no_memory(zhp->zpool_hdl));
3361 * Upgrade a ZFS pool to the latest on-disk version.
3364 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3366 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3367 libzfs_handle_t *hdl = zhp->zpool_hdl;
3369 (void) strcpy(zc.zc_name, zhp->zpool_name);
3370 zc.zc_cookie = new_version;
3372 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3373 return (zpool_standard_error_fmt(hdl, errno,
3374 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3380 zpool_set_history_str(const char *subcommand, int argc, char **argv,
3385 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
3386 for (i = 1; i < argc; i++) {
3387 if (strlen(history_str) + 1 + strlen(argv[i]) >
3390 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
3391 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
3396 * Stage command history for logging.
3399 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
3401 if (history_str == NULL)
3404 if (strlen(history_str) > HIS_MAX_RECORD_LEN)
3407 if (hdl->libzfs_log_str != NULL)
3408 free(hdl->libzfs_log_str);
3410 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
3411 return (no_memory(hdl));
3417 * Perform ioctl to get some command history of a pool.
3419 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3420 * logical offset of the history buffer to start reading from.
3422 * Upon return, 'off' is the next logical offset to read from and
3423 * 'len' is the actual amount of bytes read into 'buf'.
3426 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3428 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3429 libzfs_handle_t *hdl = zhp->zpool_hdl;
3431 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3433 zc.zc_history = (uint64_t)(uintptr_t)buf;
3434 zc.zc_history_len = *len;
3435 zc.zc_history_offset = *off;
3437 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3440 return (zfs_error_fmt(hdl, EZFS_PERM,
3441 dgettext(TEXT_DOMAIN,
3442 "cannot show history for pool '%s'"),
3445 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3446 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3447 "'%s'"), zhp->zpool_name));
3449 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3450 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3451 "'%s', pool must be upgraded"), zhp->zpool_name));
3453 return (zpool_standard_error_fmt(hdl, errno,
3454 dgettext(TEXT_DOMAIN,
3455 "cannot get history for '%s'"), zhp->zpool_name));
3459 *len = zc.zc_history_len;
3460 *off = zc.zc_history_offset;
3466 * Process the buffer of nvlists, unpacking and storing each nvlist record
3467 * into 'records'. 'leftover' is set to the number of bytes that weren't
3468 * processed as there wasn't a complete record.
3471 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3472 nvlist_t ***records, uint_t *numrecords)
3478 while (bytes_read > sizeof (reclen)) {
3480 /* get length of packed record (stored as little endian) */
3481 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3482 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3484 if (bytes_read < sizeof (reclen) + reclen)
3488 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3490 bytes_read -= sizeof (reclen) + reclen;
3491 buf += sizeof (reclen) + reclen;
3493 /* add record to nvlist array */
3495 if (ISP2(*numrecords + 1)) {
3496 *records = realloc(*records,
3497 *numrecords * 2 * sizeof (nvlist_t *));
3499 (*records)[*numrecords - 1] = nv;
3502 *leftover = bytes_read;
3506 #define HIS_BUF_LEN (128*1024)
3509 * Retrieve the command history of a pool.
3512 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3514 char buf[HIS_BUF_LEN];
3516 nvlist_t **records = NULL;
3517 uint_t numrecords = 0;
3521 uint64_t bytes_read = sizeof (buf);
3524 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3527 /* if nothing else was read in, we're at EOF, just return */
3531 if ((err = zpool_history_unpack(buf, bytes_read,
3532 &leftover, &records, &numrecords)) != 0)
3540 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3541 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3542 records, numrecords) == 0);
3544 for (i = 0; i < numrecords; i++)
3545 nvlist_free(records[i]);
3552 * Retrieve the next event. If there is a new event available 'nvp' will
3553 * contain a newly allocated nvlist and 'dropped' will be set to the number
3554 * of missed events since the last call to this function. When 'nvp' is
3555 * set to NULL it indicates no new events are available. In either case
3556 * the function returns 0 and it is up to the caller to free 'nvp'. In
3557 * the case of a fatal error the function will return a non-zero value.
3558 * When the function is called in blocking mode it will not return until
3559 * a new event is available.
3562 zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
3563 int *dropped, int block, int cleanup_fd)
3565 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3570 zc.zc_cleanup_fd = cleanup_fd;
3573 zc.zc_guid = ZEVENT_NONBLOCK;
3575 if (zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE) != 0)
3579 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
3582 error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
3583 dgettext(TEXT_DOMAIN, "zfs shutdown"));
3586 /* Blocking error case should not occur */
3588 error = zpool_standard_error_fmt(hdl, errno,
3589 dgettext(TEXT_DOMAIN, "cannot get event"));
3593 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3594 error = zfs_error_fmt(hdl, EZFS_NOMEM,
3595 dgettext(TEXT_DOMAIN, "cannot get event"));
3601 error = zpool_standard_error_fmt(hdl, errno,
3602 dgettext(TEXT_DOMAIN, "cannot get event"));
3607 error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
3611 *dropped = (int)zc.zc_cookie;
3613 zcmd_free_nvlists(&zc);
3622 zpool_events_clear(libzfs_handle_t *hdl, int *count)
3624 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3627 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3628 "cannot clear events"));
3630 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
3631 return (zpool_standard_error_fmt(hdl, errno, msg));
3634 *count = (int)zc.zc_cookie; /* # of events cleared */
3640 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3641 char *pathname, size_t len)
3643 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3644 boolean_t mounted = B_FALSE;
3645 char *mntpnt = NULL;
3646 char dsname[MAXNAMELEN];
3649 /* special case for the MOS */
3650 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", (longlong_t)obj);
3654 /* get the dataset's name */
3655 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3657 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3658 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3659 /* just write out a path of two object numbers */
3660 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3661 (longlong_t)dsobj, (longlong_t)obj);
3664 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3666 /* find out if the dataset is mounted */
3667 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3669 /* get the corrupted object's path */
3670 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3672 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3675 (void) snprintf(pathname, len, "%s%s", mntpnt,
3678 (void) snprintf(pathname, len, "%s:%s",
3679 dsname, zc.zc_value);
3682 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, (longlong_t)obj);
3688 * Read the EFI label from the config, if a label does not exist then
3689 * pass back the error to the caller. If the caller has passed a non-NULL
3690 * diskaddr argument then we set it to the starting address of the EFI
3694 read_efi_label(nvlist_t *config, diskaddr_t *sb)
3698 char diskname[MAXPATHLEN];
3701 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3704 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
3705 strrchr(path, '/'));
3706 if ((fd = open(diskname, O_RDWR|O_DIRECT)) >= 0) {
3707 struct dk_gpt *vtoc;
3709 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3711 *sb = vtoc->efi_parts[0].p_start;
3720 * determine where a partition starts on a disk in the current
3724 find_start_block(nvlist_t *config)
3728 diskaddr_t sb = MAXOFFSET_T;
3731 if (nvlist_lookup_nvlist_array(config,
3732 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3733 if (nvlist_lookup_uint64(config,
3734 ZPOOL_CONFIG_WHOLE_DISK,
3735 &wholedisk) != 0 || !wholedisk) {
3736 return (MAXOFFSET_T);
3738 if (read_efi_label(config, &sb) < 0)
3743 for (c = 0; c < children; c++) {
3744 sb = find_start_block(child[c]);
3745 if (sb != MAXOFFSET_T) {
3749 return (MAXOFFSET_T);
3753 zpool_label_disk_wait(char *path, int timeout)
3755 struct stat64 statbuf;
3759 * Wait timeout miliseconds for a newly created device to be available
3760 * from the given path. There is a small window when a /dev/ device
3761 * will exist and the udev link will not, so we must wait for the
3762 * symlink. Depending on the udev rules this may take a few seconds.
3764 for (i = 0; i < timeout; i++) {
3768 if ((stat64(path, &statbuf) == 0) && (errno == 0))
3776 zpool_label_disk_check(char *path)
3778 struct dk_gpt *vtoc;
3781 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0)
3784 if ((err = efi_alloc_and_read(fd, &vtoc)) != 0) {
3789 if (vtoc->efi_flags & EFI_GPT_PRIMARY_CORRUPT) {
3801 * Label an individual disk. The name provided is the short name,
3802 * stripped of any leading /dev path.
3805 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
3807 char path[MAXPATHLEN];
3808 struct dk_gpt *vtoc;
3810 size_t resv = EFI_MIN_RESV_SIZE;
3811 uint64_t slice_size;
3812 diskaddr_t start_block;
3815 /* prepare an error message just in case */
3816 (void) snprintf(errbuf, sizeof (errbuf),
3817 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3822 if (zpool_is_bootable(zhp)) {
3823 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3824 "EFI labeled devices are not supported on root "
3826 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3829 verify(nvlist_lookup_nvlist(zhp->zpool_config,
3830 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3832 if (zhp->zpool_start_block == 0)
3833 start_block = find_start_block(nvroot);
3835 start_block = zhp->zpool_start_block;
3836 zhp->zpool_start_block = start_block;
3839 start_block = NEW_START_BLOCK;
3842 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
3845 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
3847 * This shouldn't happen. We've long since verified that this
3848 * is a valid device.
3850 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
3851 "label '%s': unable to open device: %d"), path, errno);
3852 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
3855 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
3857 * The only way this can fail is if we run out of memory, or we
3858 * were unable to read the disk's capacity
3860 if (errno == ENOMEM)
3861 (void) no_memory(hdl);
3864 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
3865 "label '%s': unable to read disk capacity"), path);
3867 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
3870 slice_size = vtoc->efi_last_u_lba + 1;
3871 slice_size -= EFI_MIN_RESV_SIZE;
3872 if (start_block == MAXOFFSET_T)
3873 start_block = NEW_START_BLOCK;
3874 slice_size -= start_block;
3875 slice_size = P2ALIGN(slice_size, PARTITION_END_ALIGNMENT);
3877 vtoc->efi_parts[0].p_start = start_block;
3878 vtoc->efi_parts[0].p_size = slice_size;
3881 * Why we use V_USR: V_BACKUP confuses users, and is considered
3882 * disposable by some EFI utilities (since EFI doesn't have a backup
3883 * slice). V_UNASSIGNED is supposed to be used only for zero size
3884 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
3885 * etc. were all pretty specific. V_USR is as close to reality as we
3886 * can get, in the absence of V_OTHER.
3888 vtoc->efi_parts[0].p_tag = V_USR;
3889 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
3891 vtoc->efi_parts[8].p_start = slice_size + start_block;
3892 vtoc->efi_parts[8].p_size = resv;
3893 vtoc->efi_parts[8].p_tag = V_RESERVED;
3895 if ((rval = efi_write(fd, vtoc)) != 0 || (rval = efi_rescan(fd)) != 0) {
3897 * Some block drivers (like pcata) may not support EFI
3898 * GPT labels. Print out a helpful error message dir-
3899 * ecting the user to manually label the disk and give
3905 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "try using "
3906 "parted(8) and then provide a specific slice: %d"), rval);
3907 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3913 /* Wait for the first expected slice to appear. */
3914 (void) snprintf(path, sizeof (path), "%s/%s%s%s", DISK_ROOT, name,
3915 isdigit(name[strlen(name)-1]) ? "p" : "", FIRST_SLICE);
3916 rval = zpool_label_disk_wait(path, 3000);
3918 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "failed to "
3919 "detect device partitions on '%s': %d"), path, rval);
3920 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3923 /* We can't be to paranoid. Read the label back and verify it. */
3924 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
3925 rval = zpool_label_disk_check(path);
3927 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "freshly written "
3928 "EFI label on '%s' is damaged. Ensure\nthis device "
3929 "is not in in use, and is functioning properly: %d"),
3931 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));