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
1748 * "-part1", or "p1". The suffix is hidden from the user,
1749 * but included in the string, so this matches around it.
1750 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
1751 * is used to check all possible expanded paths.
1752 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1754 * Otherwise, all other searches are simple string compares.
1756 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) {
1757 uint64_t wholedisk = 0;
1759 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1761 if (zfs_strcmp_pathname(srchval, val, wholedisk) == 0)
1764 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
1765 char *type, *idx, *end, *p;
1766 uint64_t id, vdev_id;
1769 * Determine our vdev type, keeping in mind
1770 * that the srchval is composed of a type and
1771 * vdev id pair (i.e. mirror-4).
1773 if ((type = strdup(srchval)) == NULL)
1776 if ((p = strrchr(type, '-')) == NULL) {
1784 * If the types don't match then keep looking.
1786 if (strncmp(val, type, strlen(val)) != 0) {
1791 verify(strncmp(type, VDEV_TYPE_RAIDZ,
1792 strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1793 strncmp(type, VDEV_TYPE_MIRROR,
1794 strlen(VDEV_TYPE_MIRROR)) == 0);
1795 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
1799 vdev_id = strtoull(idx, &end, 10);
1806 * Now verify that we have the correct vdev id.
1815 if (strcmp(srchval, val) == 0)
1824 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1825 &child, &children) != 0)
1828 for (c = 0; c < children; c++) {
1829 if ((ret = vdev_to_nvlist_iter(child[c], search,
1830 avail_spare, l2cache, NULL)) != NULL) {
1832 * The 'is_log' value is only set for the toplevel
1833 * vdev, not the leaf vdevs. So we always lookup the
1834 * log device from the root of the vdev tree (where
1835 * 'log' is non-NULL).
1838 nvlist_lookup_uint64(child[c],
1839 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
1847 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1848 &child, &children) == 0) {
1849 for (c = 0; c < children; c++) {
1850 if ((ret = vdev_to_nvlist_iter(child[c], search,
1851 avail_spare, l2cache, NULL)) != NULL) {
1852 *avail_spare = B_TRUE;
1858 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1859 &child, &children) == 0) {
1860 for (c = 0; c < children; c++) {
1861 if ((ret = vdev_to_nvlist_iter(child[c], search,
1862 avail_spare, l2cache, NULL)) != NULL) {
1873 * Given a physical path (minus the "/devices" prefix), find the
1877 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
1878 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
1880 nvlist_t *search, *nvroot, *ret;
1882 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1883 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
1885 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1888 *avail_spare = B_FALSE;
1892 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1893 nvlist_free(search);
1899 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
1902 zpool_vdev_is_interior(const char *name)
1904 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1905 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
1911 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
1912 boolean_t *l2cache, boolean_t *log)
1915 nvlist_t *nvroot, *search, *ret;
1918 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1920 guid = strtoull(path, &end, 10);
1921 if (guid != 0 && *end == '\0') {
1922 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
1923 } else if (zpool_vdev_is_interior(path)) {
1924 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
1926 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
1929 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1932 *avail_spare = B_FALSE;
1936 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1937 nvlist_free(search);
1943 vdev_online(nvlist_t *nv)
1947 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
1948 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
1949 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
1956 * Helper function for zpool_get_physpaths().
1959 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
1960 size_t *bytes_written)
1962 size_t bytes_left, pos, rsz;
1966 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
1968 return (EZFS_NODEVICE);
1970 pos = *bytes_written;
1971 bytes_left = physpath_size - pos;
1972 format = (pos == 0) ? "%s" : " %s";
1974 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
1975 *bytes_written += rsz;
1977 if (rsz >= bytes_left) {
1978 /* if physpath was not copied properly, clear it */
1979 if (bytes_left != 0) {
1982 return (EZFS_NOSPC);
1988 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
1989 size_t *rsz, boolean_t is_spare)
1994 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
1995 return (EZFS_INVALCONFIG);
1997 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
1999 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2000 * For a spare vdev, we only want to boot from the active
2005 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2008 return (EZFS_INVALCONFIG);
2011 if (vdev_online(nv)) {
2012 if ((ret = vdev_get_one_physpath(nv, physpath,
2013 phypath_size, rsz)) != 0)
2016 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2017 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2018 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2023 if (nvlist_lookup_nvlist_array(nv,
2024 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2025 return (EZFS_INVALCONFIG);
2027 for (i = 0; i < count; i++) {
2028 ret = vdev_get_physpaths(child[i], physpath,
2029 phypath_size, rsz, is_spare);
2030 if (ret == EZFS_NOSPC)
2035 return (EZFS_POOL_INVALARG);
2039 * Get phys_path for a root pool config.
2040 * Return 0 on success; non-zero on failure.
2043 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2046 nvlist_t *vdev_root;
2053 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2055 return (EZFS_INVALCONFIG);
2057 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2058 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2059 &child, &count) != 0)
2060 return (EZFS_INVALCONFIG);
2063 * root pool can not have EFI labeled disks and can only have
2064 * a single top-level vdev.
2066 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
2067 pool_uses_efi(vdev_root))
2068 return (EZFS_POOL_INVALARG);
2070 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2073 /* No online devices */
2075 return (EZFS_NODEVICE);
2081 * Get phys_path for a root pool
2082 * Return 0 on success; non-zero on failure.
2085 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2087 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2092 * If the device has being dynamically expanded then we need to relabel
2093 * the disk to use the new unallocated space.
2096 zpool_relabel_disk(libzfs_handle_t *hdl, const char *path, const char *msg)
2100 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
2101 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2102 "relabel '%s': unable to open device: %d"), path, errno);
2103 return (zfs_error(hdl, EZFS_OPENFAILED, msg));
2107 * It's possible that we might encounter an error if the device
2108 * does not have any unallocated space left. If so, we simply
2109 * ignore that error and continue on.
2111 * Also, we don't call efi_rescan() - that would just return EBUSY.
2112 * The module will do it for us in vdev_disk_open().
2114 error = efi_use_whole_disk(fd);
2116 if (error && error != VT_ENOSPC) {
2117 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2118 "relabel '%s': unable to read disk capacity"), path);
2119 return (zfs_error(hdl, EZFS_NOCAP, msg));
2125 * Bring the specified vdev online. The 'flags' parameter is a set of the
2126 * ZFS_ONLINE_* flags.
2129 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2130 vdev_state_t *newstate)
2132 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2135 boolean_t avail_spare, l2cache, islog;
2136 libzfs_handle_t *hdl = zhp->zpool_hdl;
2139 if (flags & ZFS_ONLINE_EXPAND) {
2140 (void) snprintf(msg, sizeof (msg),
2141 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2143 (void) snprintf(msg, sizeof (msg),
2144 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2147 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2148 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2150 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2152 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2155 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2157 if (flags & ZFS_ONLINE_EXPAND ||
2158 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2159 uint64_t wholedisk = 0;
2161 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2165 * XXX - L2ARC 1.0 devices can't support expansion.
2168 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2169 "cannot expand cache devices"));
2170 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2174 const char *fullpath = path;
2175 char buf[MAXPATHLEN];
2177 if (path[0] != '/') {
2178 error = zfs_resolve_shortname(path, buf,
2181 return (zfs_error(hdl, EZFS_NODEVICE,
2187 error = zpool_relabel_disk(hdl, fullpath, msg);
2193 zc.zc_cookie = VDEV_STATE_ONLINE;
2196 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2197 if (errno == EINVAL) {
2198 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2199 "from this pool into a new one. Use '%s' "
2200 "instead"), "zpool detach");
2201 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2203 return (zpool_standard_error(hdl, errno, msg));
2206 *newstate = zc.zc_cookie;
2211 * Take the specified vdev offline
2214 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2216 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2219 boolean_t avail_spare, l2cache;
2220 libzfs_handle_t *hdl = zhp->zpool_hdl;
2222 (void) snprintf(msg, sizeof (msg),
2223 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2225 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2226 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2228 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2230 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2233 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2235 zc.zc_cookie = VDEV_STATE_OFFLINE;
2236 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2238 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2245 * There are no other replicas of this device.
2247 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2251 * The log device has unplayed logs
2253 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2256 return (zpool_standard_error(hdl, errno, msg));
2261 * Mark the given vdev faulted.
2264 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2266 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2268 libzfs_handle_t *hdl = zhp->zpool_hdl;
2270 (void) snprintf(msg, sizeof (msg),
2271 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
2273 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2275 zc.zc_cookie = VDEV_STATE_FAULTED;
2278 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2285 * There are no other replicas of this device.
2287 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2290 return (zpool_standard_error(hdl, errno, msg));
2296 * Mark the given vdev degraded.
2299 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2301 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2303 libzfs_handle_t *hdl = zhp->zpool_hdl;
2305 (void) snprintf(msg, sizeof (msg),
2306 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid);
2308 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2310 zc.zc_cookie = VDEV_STATE_DEGRADED;
2313 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2316 return (zpool_standard_error(hdl, errno, msg));
2320 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2324 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2330 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2332 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2335 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2336 children == 2 && child[which] == tgt)
2339 for (c = 0; c < children; c++)
2340 if (is_replacing_spare(child[c], tgt, which))
2348 * Attach new_disk (fully described by nvroot) to old_disk.
2349 * If 'replacing' is specified, the new disk will replace the old one.
2352 zpool_vdev_attach(zpool_handle_t *zhp,
2353 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2355 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2359 boolean_t avail_spare, l2cache, islog;
2364 nvlist_t *config_root;
2365 libzfs_handle_t *hdl = zhp->zpool_hdl;
2366 boolean_t rootpool = zpool_is_bootable(zhp);
2369 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2370 "cannot replace %s with %s"), old_disk, new_disk);
2372 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2373 "cannot attach %s to %s"), new_disk, old_disk);
2376 * If this is a root pool, make sure that we're not attaching an
2377 * EFI labeled device.
2379 if (rootpool && pool_uses_efi(nvroot)) {
2380 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2381 "EFI labeled devices are not supported on root pools."));
2382 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2385 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2386 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2388 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2391 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2394 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2396 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2397 zc.zc_cookie = replacing;
2399 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2400 &child, &children) != 0 || children != 1) {
2401 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2402 "new device must be a single disk"));
2403 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2406 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2407 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2409 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2413 * If the target is a hot spare that has been swapped in, we can only
2414 * replace it with another hot spare.
2417 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2418 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2419 NULL) == NULL || !avail_spare) &&
2420 is_replacing_spare(config_root, tgt, 1)) {
2421 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2422 "can only be replaced by another hot spare"));
2424 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2429 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2432 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2434 zcmd_free_nvlists(&zc);
2439 * XXX need a better way to prevent user from
2440 * booting up a half-baked vdev.
2442 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2443 "sure to wait until resilver is done "
2444 "before rebooting.\n"));
2452 * Can't attach to or replace this type of vdev.
2455 uint64_t version = zpool_get_prop_int(zhp,
2456 ZPOOL_PROP_VERSION, NULL);
2459 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2460 "cannot replace a log with a spare"));
2461 else if (version >= SPA_VERSION_MULTI_REPLACE)
2462 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2463 "already in replacing/spare config; wait "
2464 "for completion or use 'zpool detach'"));
2466 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2467 "cannot replace a replacing device"));
2469 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2470 "can only attach to mirrors and top-level "
2473 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2478 * The new device must be a single disk.
2480 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2481 "new device must be a single disk"));
2482 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2486 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2488 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2493 * The new device is too small.
2495 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2496 "device is too small"));
2497 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2502 * The new device has a different alignment requirement.
2504 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2505 "devices have different sector alignment"));
2506 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2511 * The resulting top-level vdev spec won't fit in the label.
2513 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2517 (void) zpool_standard_error(hdl, errno, msg);
2524 * Detach the specified device.
2527 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2529 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2532 boolean_t avail_spare, l2cache;
2533 libzfs_handle_t *hdl = zhp->zpool_hdl;
2535 (void) snprintf(msg, sizeof (msg),
2536 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2538 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2539 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2541 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2544 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2547 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2549 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2551 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2558 * Can't detach from this type of vdev.
2560 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2561 "applicable to mirror and replacing vdevs"));
2562 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2567 * There are no other replicas of this device.
2569 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2573 (void) zpool_standard_error(hdl, errno, msg);
2580 * Find a mirror vdev in the source nvlist.
2582 * The mchild array contains a list of disks in one of the top-level mirrors
2583 * of the source pool. The schild array contains a list of disks that the
2584 * user specified on the command line. We loop over the mchild array to
2585 * see if any entry in the schild array matches.
2587 * If a disk in the mchild array is found in the schild array, we return
2588 * the index of that entry. Otherwise we return -1.
2591 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2592 nvlist_t **schild, uint_t schildren)
2596 for (mc = 0; mc < mchildren; mc++) {
2598 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2599 mchild[mc], B_FALSE);
2601 for (sc = 0; sc < schildren; sc++) {
2602 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2603 schild[sc], B_FALSE);
2604 boolean_t result = (strcmp(mpath, spath) == 0);
2620 * Split a mirror pool. If newroot points to null, then a new nvlist
2621 * is generated and it is the responsibility of the caller to free it.
2624 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2625 nvlist_t *props, splitflags_t flags)
2627 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2629 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2630 nvlist_t **varray = NULL, *zc_props = NULL;
2631 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2632 libzfs_handle_t *hdl = zhp->zpool_hdl;
2634 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2637 (void) snprintf(msg, sizeof (msg),
2638 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2640 if (!zpool_name_valid(hdl, B_FALSE, newname))
2641 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2643 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2644 (void) fprintf(stderr, gettext("Internal error: unable to "
2645 "retrieve pool configuration\n"));
2649 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2651 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2654 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2655 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2656 props, vers, flags, msg)) == NULL)
2660 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2662 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2663 "Source pool is missing vdev tree"));
2665 nvlist_free(zc_props);
2669 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2672 if (*newroot == NULL ||
2673 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2674 &newchild, &newchildren) != 0)
2677 for (c = 0; c < children; c++) {
2678 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2680 nvlist_t **mchild, *vdev;
2685 * Unlike cache & spares, slogs are stored in the
2686 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2688 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2690 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2692 if (is_log || is_hole) {
2694 * Create a hole vdev and put it in the config.
2696 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2698 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2699 VDEV_TYPE_HOLE) != 0)
2701 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2706 varray[vcount++] = vdev;
2710 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
2712 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2713 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2714 "Source pool must be composed only of mirrors\n"));
2715 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2719 verify(nvlist_lookup_nvlist_array(child[c],
2720 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2722 /* find or add an entry for this top-level vdev */
2723 if (newchildren > 0 &&
2724 (entry = find_vdev_entry(zhp, mchild, mchildren,
2725 newchild, newchildren)) >= 0) {
2726 /* We found a disk that the user specified. */
2727 vdev = mchild[entry];
2730 /* User didn't specify a disk for this vdev. */
2731 vdev = mchild[mchildren - 1];
2734 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
2738 /* did we find every disk the user specified? */
2739 if (found != newchildren) {
2740 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
2741 "include at most one disk from each mirror"));
2742 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2746 /* Prepare the nvlist for populating. */
2747 if (*newroot == NULL) {
2748 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
2751 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
2752 VDEV_TYPE_ROOT) != 0)
2755 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
2758 /* Add all the children we found */
2759 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
2760 lastlog == 0 ? vcount : lastlog) != 0)
2764 * If we're just doing a dry run, exit now with success.
2767 memory_err = B_FALSE;
2772 /* now build up the config list & call the ioctl */
2773 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
2776 if (nvlist_add_nvlist(newconfig,
2777 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
2778 nvlist_add_string(newconfig,
2779 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
2780 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
2784 * The new pool is automatically part of the namespace unless we
2785 * explicitly export it.
2788 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
2789 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2790 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
2791 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
2793 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
2796 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
2797 retval = zpool_standard_error(hdl, errno, msg);
2802 memory_err = B_FALSE;
2805 if (varray != NULL) {
2808 for (v = 0; v < vcount; v++)
2809 nvlist_free(varray[v]);
2812 zcmd_free_nvlists(&zc);
2814 nvlist_free(zc_props);
2816 nvlist_free(newconfig);
2818 nvlist_free(*newroot);
2826 return (no_memory(hdl));
2832 * Remove the given device. Currently, this is supported only for hot spares
2833 * and level 2 cache devices.
2836 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
2838 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2841 boolean_t avail_spare, l2cache, islog;
2842 libzfs_handle_t *hdl = zhp->zpool_hdl;
2845 (void) snprintf(msg, sizeof (msg),
2846 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
2848 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2849 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2851 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2853 * XXX - this should just go away.
2855 if (!avail_spare && !l2cache && !islog) {
2856 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2857 "only inactive hot spares, cache, top-level, "
2858 "or log devices can be removed"));
2859 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2862 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
2863 if (islog && version < SPA_VERSION_HOLES) {
2864 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2865 "pool must be upgrade to support log removal"));
2866 return (zfs_error(hdl, EZFS_BADVERSION, msg));
2869 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2871 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
2874 return (zpool_standard_error(hdl, errno, msg));
2878 * Clear the errors for the pool, or the particular device if specified.
2881 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
2883 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2886 zpool_rewind_policy_t policy;
2887 boolean_t avail_spare, l2cache;
2888 libzfs_handle_t *hdl = zhp->zpool_hdl;
2889 nvlist_t *nvi = NULL;
2893 (void) snprintf(msg, sizeof (msg),
2894 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2897 (void) snprintf(msg, sizeof (msg),
2898 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2901 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2903 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
2904 &l2cache, NULL)) == 0)
2905 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2908 * Don't allow error clearing for hot spares. Do allow
2909 * error clearing for l2cache devices.
2912 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2914 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
2918 zpool_get_rewind_policy(rewindnvl, &policy);
2919 zc.zc_cookie = policy.zrp_request;
2921 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
2924 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
2927 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
2929 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
2930 zcmd_free_nvlists(&zc);
2935 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
2936 errno != EPERM && errno != EACCES)) {
2937 if (policy.zrp_request &
2938 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2939 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
2940 zpool_rewind_exclaim(hdl, zc.zc_name,
2941 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
2945 zcmd_free_nvlists(&zc);
2949 zcmd_free_nvlists(&zc);
2950 return (zpool_standard_error(hdl, errno, msg));
2954 * Similar to zpool_clear(), but takes a GUID (used by fmd).
2957 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
2959 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2961 libzfs_handle_t *hdl = zhp->zpool_hdl;
2963 (void) snprintf(msg, sizeof (msg),
2964 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
2965 (u_longlong_t)guid);
2967 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2969 zc.zc_cookie = ZPOOL_NO_REWIND;
2971 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
2974 return (zpool_standard_error(hdl, errno, msg));
2978 * Change the GUID for a pool.
2981 zpool_reguid(zpool_handle_t *zhp)
2984 libzfs_handle_t *hdl = zhp->zpool_hdl;
2985 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2987 (void) snprintf(msg, sizeof (msg),
2988 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
2990 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2991 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
2994 return (zpool_standard_error(hdl, errno, msg));
3001 zpool_reopen(zpool_handle_t *zhp)
3003 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3005 libzfs_handle_t *hdl = zhp->zpool_hdl;
3007 (void) snprintf(msg, sizeof (msg),
3008 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
3011 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3012 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
3014 return (zpool_standard_error(hdl, errno, msg));
3018 * Convert from a devid string to a path.
3021 devid_to_path(char *devid_str)
3026 devid_nmlist_t *list = NULL;
3029 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3032 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3034 devid_str_free(minor);
3040 if ((path = strdup(list[0].devname)) == NULL)
3043 devid_free_nmlist(list);
3049 * Convert from a path to a devid string.
3052 path_to_devid(const char *path)
3058 if ((fd = open(path, O_RDONLY)) < 0)
3063 if (devid_get(fd, &devid) == 0) {
3064 if (devid_get_minor_name(fd, &minor) == 0)
3065 ret = devid_str_encode(devid, minor);
3067 devid_str_free(minor);
3076 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3077 * ignore any failure here, since a common case is for an unprivileged user to
3078 * type 'zpool status', and we'll display the correct information anyway.
3081 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3083 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3085 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3086 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3087 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3090 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3094 * Remove partition suffix from a vdev path. Partition suffixes may take three
3095 * forms: "-partX", "pX", or "X", where X is a string of digits. The second
3096 * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
3097 * third case only occurs when preceded by a string matching the regular
3098 * expression "^[hs]d[a-z]+", i.e. a scsi or ide disk.
3101 strip_partition(libzfs_handle_t *hdl, char *path)
3103 char *tmp = zfs_strdup(hdl, path);
3104 char *part = NULL, *d = NULL;
3106 if ((part = strstr(tmp, "-part")) && part != tmp) {
3108 } else if ((part = strrchr(tmp, 'p')) &&
3109 part > tmp + 1 && isdigit(*(part-1))) {
3111 } else if ((tmp[0] == 'h' || tmp[0] == 's') && tmp[1] == 'd') {
3112 for (d = &tmp[2]; isalpha(*d); part = ++d);
3114 if (part && d && *d != '\0') {
3115 for (; isdigit(*d); d++);
3122 #define PATH_BUF_LEN 64
3125 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3126 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3127 * We also check if this is a whole disk, in which case we strip off the
3128 * trailing 's0' slice name.
3130 * This routine is also responsible for identifying when disks have been
3131 * reconfigured in a new location. The kernel will have opened the device by
3132 * devid, but the path will still refer to the old location. To catch this, we
3133 * first do a path -> devid translation (which is fast for the common case). If
3134 * the devid matches, we're done. If not, we do a reverse devid -> path
3135 * translation and issue the appropriate ioctl() to update the path of the vdev.
3136 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3140 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3143 char *path, *devid, *type;
3145 char buf[PATH_BUF_LEN];
3146 char tmpbuf[PATH_BUF_LEN];
3150 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
3152 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3154 (void) snprintf(buf, sizeof (buf), "%llu",
3155 (u_longlong_t)value);
3157 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3159 * If the device is dead (faulted, offline, etc) then don't
3160 * bother opening it. Otherwise we may be forcing the user to
3161 * open a misbehaving device, which can have undesirable
3164 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3165 (uint64_t **)&vs, &vsc) != 0 ||
3166 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3168 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3170 * Determine if the current path is correct.
3172 char *newdevid = path_to_devid(path);
3174 if (newdevid == NULL ||
3175 strcmp(devid, newdevid) != 0) {
3178 if ((newpath = devid_to_path(devid)) != NULL) {
3180 * Update the path appropriately.
3182 set_path(zhp, nv, newpath);
3183 if (nvlist_add_string(nv,
3184 ZPOOL_CONFIG_PATH, newpath) == 0)
3185 verify(nvlist_lookup_string(nv,
3193 devid_str_free(newdevid);
3197 * For a block device only use the name.
3199 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
3200 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
3201 path = strrchr(path, '/');
3206 * Remove the partition from the path it this is a whole disk.
3208 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3209 &value) == 0 && value) {
3210 return strip_partition(hdl, path);
3213 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3216 * If it's a raidz device, we need to stick in the parity level.
3218 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3220 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3222 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3223 (u_longlong_t)value);
3228 * We identify each top-level vdev by using a <type-id>
3229 * naming convention.
3234 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3236 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s-%llu",
3237 path, (u_longlong_t)id);
3242 return (zfs_strdup(hdl, path));
3246 zbookmark_compare(const void *a, const void *b)
3248 return (memcmp(a, b, sizeof (zbookmark_t)));
3252 * Retrieve the persistent error log, uniquify the members, and return to the
3256 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3258 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3260 zbookmark_t *zb = NULL;
3264 * Retrieve the raw error list from the kernel. If the number of errors
3265 * has increased, allocate more space and continue until we get the
3268 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3272 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3273 count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
3275 zc.zc_nvlist_dst_size = count;
3276 (void) strcpy(zc.zc_name, zhp->zpool_name);
3278 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3280 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3281 if (errno == ENOMEM) {
3282 count = zc.zc_nvlist_dst_size;
3283 if ((zc.zc_nvlist_dst = (uintptr_t)
3284 zfs_alloc(zhp->zpool_hdl, count *
3285 sizeof (zbookmark_t))) == (uintptr_t)NULL)
3296 * Sort the resulting bookmarks. This is a little confusing due to the
3297 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3298 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3299 * _not_ copied as part of the process. So we point the start of our
3300 * array appropriate and decrement the total number of elements.
3302 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
3303 zc.zc_nvlist_dst_size;
3304 count -= zc.zc_nvlist_dst_size;
3306 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
3308 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3311 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3313 for (i = 0; i < count; i++) {
3316 /* ignoring zb_blkid and zb_level for now */
3317 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3318 zb[i-1].zb_object == zb[i].zb_object)
3321 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3323 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3324 zb[i].zb_objset) != 0) {
3328 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3329 zb[i].zb_object) != 0) {
3333 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3340 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3344 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3345 return (no_memory(zhp->zpool_hdl));
3349 * Upgrade a ZFS pool to the latest on-disk version.
3352 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3354 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3355 libzfs_handle_t *hdl = zhp->zpool_hdl;
3357 (void) strcpy(zc.zc_name, zhp->zpool_name);
3358 zc.zc_cookie = new_version;
3360 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3361 return (zpool_standard_error_fmt(hdl, errno,
3362 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3368 zpool_set_history_str(const char *subcommand, int argc, char **argv,
3373 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
3374 for (i = 1; i < argc; i++) {
3375 if (strlen(history_str) + 1 + strlen(argv[i]) >
3378 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
3379 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
3384 * Stage command history for logging.
3387 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
3389 if (history_str == NULL)
3392 if (strlen(history_str) > HIS_MAX_RECORD_LEN)
3395 if (hdl->libzfs_log_str != NULL)
3396 free(hdl->libzfs_log_str);
3398 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
3399 return (no_memory(hdl));
3405 * Perform ioctl to get some command history of a pool.
3407 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3408 * logical offset of the history buffer to start reading from.
3410 * Upon return, 'off' is the next logical offset to read from and
3411 * 'len' is the actual amount of bytes read into 'buf'.
3414 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3416 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3417 libzfs_handle_t *hdl = zhp->zpool_hdl;
3419 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3421 zc.zc_history = (uint64_t)(uintptr_t)buf;
3422 zc.zc_history_len = *len;
3423 zc.zc_history_offset = *off;
3425 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3428 return (zfs_error_fmt(hdl, EZFS_PERM,
3429 dgettext(TEXT_DOMAIN,
3430 "cannot show history for pool '%s'"),
3433 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3434 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3435 "'%s'"), zhp->zpool_name));
3437 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3438 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3439 "'%s', pool must be upgraded"), zhp->zpool_name));
3441 return (zpool_standard_error_fmt(hdl, errno,
3442 dgettext(TEXT_DOMAIN,
3443 "cannot get history for '%s'"), zhp->zpool_name));
3447 *len = zc.zc_history_len;
3448 *off = zc.zc_history_offset;
3454 * Process the buffer of nvlists, unpacking and storing each nvlist record
3455 * into 'records'. 'leftover' is set to the number of bytes that weren't
3456 * processed as there wasn't a complete record.
3459 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3460 nvlist_t ***records, uint_t *numrecords)
3466 while (bytes_read > sizeof (reclen)) {
3468 /* get length of packed record (stored as little endian) */
3469 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3470 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3472 if (bytes_read < sizeof (reclen) + reclen)
3476 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3478 bytes_read -= sizeof (reclen) + reclen;
3479 buf += sizeof (reclen) + reclen;
3481 /* add record to nvlist array */
3483 if (ISP2(*numrecords + 1)) {
3484 *records = realloc(*records,
3485 *numrecords * 2 * sizeof (nvlist_t *));
3487 (*records)[*numrecords - 1] = nv;
3490 *leftover = bytes_read;
3494 #define HIS_BUF_LEN (128*1024)
3497 * Retrieve the command history of a pool.
3500 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3502 char buf[HIS_BUF_LEN];
3504 nvlist_t **records = NULL;
3505 uint_t numrecords = 0;
3509 uint64_t bytes_read = sizeof (buf);
3512 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3515 /* if nothing else was read in, we're at EOF, just return */
3519 if ((err = zpool_history_unpack(buf, bytes_read,
3520 &leftover, &records, &numrecords)) != 0)
3528 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3529 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3530 records, numrecords) == 0);
3532 for (i = 0; i < numrecords; i++)
3533 nvlist_free(records[i]);
3540 * Retrieve the next event. If there is a new event available 'nvp' will
3541 * contain a newly allocated nvlist and 'dropped' will be set to the number
3542 * of missed events since the last call to this function. When 'nvp' is
3543 * set to NULL it indicates no new events are available. In either case
3544 * the function returns 0 and it is up to the caller to free 'nvp'. In
3545 * the case of a fatal error the function will return a non-zero value.
3546 * When the function is called in blocking mode it will not return until
3547 * a new event is available.
3550 zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
3551 int *dropped, int block, int cleanup_fd)
3553 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3558 zc.zc_cleanup_fd = cleanup_fd;
3561 zc.zc_guid = ZEVENT_NONBLOCK;
3563 if (zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE) != 0)
3567 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
3570 error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
3571 dgettext(TEXT_DOMAIN, "zfs shutdown"));
3574 /* Blocking error case should not occur */
3576 error = zpool_standard_error_fmt(hdl, errno,
3577 dgettext(TEXT_DOMAIN, "cannot get event"));
3581 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3582 error = zfs_error_fmt(hdl, EZFS_NOMEM,
3583 dgettext(TEXT_DOMAIN, "cannot get event"));
3589 error = zpool_standard_error_fmt(hdl, errno,
3590 dgettext(TEXT_DOMAIN, "cannot get event"));
3595 error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
3599 *dropped = (int)zc.zc_cookie;
3601 zcmd_free_nvlists(&zc);
3610 zpool_events_clear(libzfs_handle_t *hdl, int *count)
3612 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3615 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3616 "cannot clear events"));
3618 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
3619 return (zpool_standard_error_fmt(hdl, errno, msg));
3622 *count = (int)zc.zc_cookie; /* # of events cleared */
3628 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3629 char *pathname, size_t len)
3631 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3632 boolean_t mounted = B_FALSE;
3633 char *mntpnt = NULL;
3634 char dsname[MAXNAMELEN];
3637 /* special case for the MOS */
3638 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", (longlong_t)obj);
3642 /* get the dataset's name */
3643 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3645 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3646 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3647 /* just write out a path of two object numbers */
3648 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3649 (longlong_t)dsobj, (longlong_t)obj);
3652 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3654 /* find out if the dataset is mounted */
3655 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3657 /* get the corrupted object's path */
3658 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3660 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3663 (void) snprintf(pathname, len, "%s%s", mntpnt,
3666 (void) snprintf(pathname, len, "%s:%s",
3667 dsname, zc.zc_value);
3670 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, (longlong_t)obj);
3676 * Read the EFI label from the config, if a label does not exist then
3677 * pass back the error to the caller. If the caller has passed a non-NULL
3678 * diskaddr argument then we set it to the starting address of the EFI
3682 read_efi_label(nvlist_t *config, diskaddr_t *sb)
3686 char diskname[MAXPATHLEN];
3689 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3692 (void) snprintf(diskname, sizeof (diskname), "%s%s", DISK_ROOT,
3693 strrchr(path, '/'));
3694 if ((fd = open(diskname, O_RDWR|O_DIRECT)) >= 0) {
3695 struct dk_gpt *vtoc;
3697 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3699 *sb = vtoc->efi_parts[0].p_start;
3708 * determine where a partition starts on a disk in the current
3712 find_start_block(nvlist_t *config)
3716 diskaddr_t sb = MAXOFFSET_T;
3719 if (nvlist_lookup_nvlist_array(config,
3720 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3721 if (nvlist_lookup_uint64(config,
3722 ZPOOL_CONFIG_WHOLE_DISK,
3723 &wholedisk) != 0 || !wholedisk) {
3724 return (MAXOFFSET_T);
3726 if (read_efi_label(config, &sb) < 0)
3731 for (c = 0; c < children; c++) {
3732 sb = find_start_block(child[c]);
3733 if (sb != MAXOFFSET_T) {
3737 return (MAXOFFSET_T);
3741 zpool_label_disk_wait(char *path, int timeout)
3743 struct stat64 statbuf;
3747 * Wait timeout miliseconds for a newly created device to be available
3748 * from the given path. There is a small window when a /dev/ device
3749 * will exist and the udev link will not, so we must wait for the
3750 * symlink. Depending on the udev rules this may take a few seconds.
3752 for (i = 0; i < timeout; i++) {
3756 if ((stat64(path, &statbuf) == 0) && (errno == 0))
3764 zpool_label_disk_check(char *path)
3766 struct dk_gpt *vtoc;
3769 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0)
3772 if ((err = efi_alloc_and_read(fd, &vtoc)) != 0) {
3777 if (vtoc->efi_flags & EFI_GPT_PRIMARY_CORRUPT) {
3789 * Label an individual disk. The name provided is the short name,
3790 * stripped of any leading /dev path.
3793 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
3795 char path[MAXPATHLEN];
3796 struct dk_gpt *vtoc;
3798 size_t resv = EFI_MIN_RESV_SIZE;
3799 uint64_t slice_size;
3800 diskaddr_t start_block;
3803 /* prepare an error message just in case */
3804 (void) snprintf(errbuf, sizeof (errbuf),
3805 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3810 if (zpool_is_bootable(zhp)) {
3811 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3812 "EFI labeled devices are not supported on root "
3814 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3817 verify(nvlist_lookup_nvlist(zhp->zpool_config,
3818 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3820 if (zhp->zpool_start_block == 0)
3821 start_block = find_start_block(nvroot);
3823 start_block = zhp->zpool_start_block;
3824 zhp->zpool_start_block = start_block;
3827 start_block = NEW_START_BLOCK;
3830 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
3832 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
3834 * This shouldn't happen. We've long since verified that this
3835 * is a valid device.
3837 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
3838 "label '%s': unable to open device: %d"), path, errno);
3839 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
3842 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
3844 * The only way this can fail is if we run out of memory, or we
3845 * were unable to read the disk's capacity
3847 if (errno == ENOMEM)
3848 (void) no_memory(hdl);
3851 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
3852 "label '%s': unable to read disk capacity"), path);
3854 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
3857 slice_size = vtoc->efi_last_u_lba + 1;
3858 slice_size -= EFI_MIN_RESV_SIZE;
3859 if (start_block == MAXOFFSET_T)
3860 start_block = NEW_START_BLOCK;
3861 slice_size -= start_block;
3862 slice_size = P2ALIGN(slice_size, PARTITION_END_ALIGNMENT);
3864 vtoc->efi_parts[0].p_start = start_block;
3865 vtoc->efi_parts[0].p_size = slice_size;
3868 * Why we use V_USR: V_BACKUP confuses users, and is considered
3869 * disposable by some EFI utilities (since EFI doesn't have a backup
3870 * slice). V_UNASSIGNED is supposed to be used only for zero size
3871 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
3872 * etc. were all pretty specific. V_USR is as close to reality as we
3873 * can get, in the absence of V_OTHER.
3875 vtoc->efi_parts[0].p_tag = V_USR;
3876 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
3878 vtoc->efi_parts[8].p_start = slice_size + start_block;
3879 vtoc->efi_parts[8].p_size = resv;
3880 vtoc->efi_parts[8].p_tag = V_RESERVED;
3882 if ((rval = efi_write(fd, vtoc)) != 0 || (rval = efi_rescan(fd)) != 0) {
3884 * Some block drivers (like pcata) may not support EFI
3885 * GPT labels. Print out a helpful error message dir-
3886 * ecting the user to manually label the disk and give
3892 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "try using "
3893 "parted(8) and then provide a specific slice: %d"), rval);
3894 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3900 /* Wait for the first expected partition to appear. */
3902 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
3903 (void) zfs_append_partition(path, MAXPATHLEN);
3905 rval = zpool_label_disk_wait(path, 3000);
3907 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "failed to "
3908 "detect device partitions on '%s': %d"), path, rval);
3909 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3912 /* We can't be to paranoid. Read the label back and verify it. */
3913 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
3914 rval = zpool_label_disk_check(path);
3916 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "freshly written "
3917 "EFI label on '%s' is damaged. Ensure\nthis device "
3918 "is not in in use, and is functioning properly: %d"),
3920 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));