4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2011 by Delphix. All rights reserved.
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 if (zhp->zpool_props != NULL ||
239 zpool_get_all_props(zhp) == 0) {
241 zpool_get_prop_string(zhp, prop, &src),
249 (void) strlcpy(buf, "-", len);
258 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
259 prop != ZPOOL_PROP_NAME)
262 switch (zpool_prop_get_type(prop)) {
263 case PROP_TYPE_STRING:
264 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
268 case PROP_TYPE_NUMBER:
269 intval = zpool_get_prop_int(zhp, prop, &src);
272 case ZPOOL_PROP_SIZE:
273 case ZPOOL_PROP_ALLOCATED:
274 case ZPOOL_PROP_FREE:
275 case ZPOOL_PROP_ASHIFT:
276 (void) zfs_nicenum(intval, buf, len);
279 case ZPOOL_PROP_CAPACITY:
280 (void) snprintf(buf, len, "%llu%%",
281 (u_longlong_t)intval);
284 case ZPOOL_PROP_DEDUPRATIO:
285 (void) snprintf(buf, len, "%llu.%02llux",
286 (u_longlong_t)(intval / 100),
287 (u_longlong_t)(intval % 100));
290 case ZPOOL_PROP_HEALTH:
291 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
292 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
293 verify(nvlist_lookup_uint64_array(nvroot,
294 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
297 (void) strlcpy(buf, zpool_state_to_name(intval,
301 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
305 case PROP_TYPE_INDEX:
306 intval = zpool_get_prop_int(zhp, prop, &src);
307 if (zpool_prop_index_to_string(prop, intval, &strval)
310 (void) strlcpy(buf, strval, len);
324 * Check if the bootfs name has the same pool name as it is set to.
325 * Assuming bootfs is a valid dataset name.
328 bootfs_name_valid(const char *pool, char *bootfs)
330 int len = strlen(pool);
332 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
335 if (strncmp(pool, bootfs, len) == 0 &&
336 (bootfs[len] == '/' || bootfs[len] == '\0'))
343 * Inspect the configuration to determine if any of the devices contain
347 pool_uses_efi(nvlist_t *config)
352 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
353 &child, &children) != 0)
354 return (read_efi_label(config, NULL) >= 0);
356 for (c = 0; c < children; c++) {
357 if (pool_uses_efi(child[c]))
364 pool_is_bootable(zpool_handle_t *zhp)
366 char bootfs[ZPOOL_MAXNAMELEN];
368 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
369 sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
370 sizeof (bootfs)) != 0);
375 * Given an nvlist of zpool properties to be set, validate that they are
376 * correct, and parse any numeric properties (index, boolean, etc) if they are
377 * specified as strings.
380 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
381 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
389 struct stat64 statbuf;
393 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
394 (void) no_memory(hdl);
399 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
400 const char *propname = nvpair_name(elem);
403 * Make sure this property is valid and applies to this type.
405 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
406 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
407 "invalid property '%s'"), propname);
408 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
412 if (zpool_prop_readonly(prop)) {
413 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
414 "is readonly"), propname);
415 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
419 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
420 &strval, &intval, errbuf) != 0)
424 * Perform additional checking for specific properties.
429 case ZPOOL_PROP_VERSION:
430 if (intval < version || intval > SPA_VERSION) {
431 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
432 "property '%s' number %d is invalid."),
434 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
439 case ZPOOL_PROP_ASHIFT:
441 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
442 "property '%s' can only be set at "
443 "creation time"), propname);
444 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
448 if (intval != 0 && (intval < 9 || intval > 13)) {
449 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
450 "property '%s' number %d is invalid."),
452 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
457 case ZPOOL_PROP_BOOTFS:
458 if (flags.create || flags.import) {
459 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
460 "property '%s' cannot be set at creation "
461 "or import time"), propname);
462 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
466 if (version < SPA_VERSION_BOOTFS) {
467 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
468 "pool must be upgraded to support "
469 "'%s' property"), propname);
470 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
475 * bootfs property value has to be a dataset name and
476 * the dataset has to be in the same pool as it sets to.
478 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
480 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
481 "is an invalid name"), strval);
482 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
486 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
488 "could not open pool '%s'"), poolname);
489 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
492 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
493 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
495 #if defined(__sun__) || defined(__sun)
497 * bootfs property cannot be set on a disk which has
500 if (pool_uses_efi(nvroot)) {
501 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
502 "property '%s' not supported on "
503 "EFI labeled devices"), propname);
504 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
512 case ZPOOL_PROP_ALTROOT:
513 if (!flags.create && !flags.import) {
514 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
515 "property '%s' can only be set during pool "
516 "creation or import"), propname);
517 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
521 if (strval[0] != '/') {
522 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
523 "bad alternate root '%s'"), strval);
524 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
529 case ZPOOL_PROP_CACHEFILE:
530 if (strval[0] == '\0')
533 if (strcmp(strval, "none") == 0)
536 if (strval[0] != '/') {
537 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
538 "property '%s' must be empty, an "
539 "absolute path, or 'none'"), propname);
540 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
544 slash = strrchr(strval, '/');
546 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
547 strcmp(slash, "/..") == 0) {
548 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
549 "'%s' is not a valid file"), strval);
550 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
556 if (strval[0] != '\0' &&
557 (stat64(strval, &statbuf) != 0 ||
558 !S_ISDIR(statbuf.st_mode))) {
559 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
560 "'%s' is not a valid directory"),
562 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
569 case ZPOOL_PROP_READONLY:
571 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
572 "property '%s' can only be set at "
573 "import time"), propname);
574 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
583 nvlist_free(retprops);
588 * Set zpool property : propname=propval.
591 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
593 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
596 nvlist_t *nvl = NULL;
599 prop_flags_t flags = { 0 };
601 (void) snprintf(errbuf, sizeof (errbuf),
602 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
605 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
606 return (no_memory(zhp->zpool_hdl));
608 if (nvlist_add_string(nvl, propname, propval) != 0) {
610 return (no_memory(zhp->zpool_hdl));
613 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
614 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
615 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
624 * Execute the corresponding ioctl() to set this property.
626 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
628 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
633 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
635 zcmd_free_nvlists(&zc);
639 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
641 (void) zpool_props_refresh(zhp);
647 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
649 libzfs_handle_t *hdl = zhp->zpool_hdl;
651 char buf[ZFS_MAXPROPLEN];
653 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
656 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
661 if (entry->pl_prop != ZPROP_INVAL &&
662 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
664 if (strlen(buf) > entry->pl_width)
665 entry->pl_width = strlen(buf);
674 * Don't start the slice at the default block of 34; many storage
675 * devices will use a stripe width of 128k, other vendors prefer a 1m
676 * alignment. It is best to play it safe and ensure a 1m alignment
677 * given 512B blocks. When the block size is larger by a power of 2
678 * we will still be 1m aligned. Some devices are sensitive to the
679 * partition ending alignment as well.
681 #define NEW_START_BLOCK 2048
682 #define PARTITION_END_ALIGNMENT 2048
685 * Validate the given pool name, optionally putting an extended error message in
689 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
695 ret = pool_namecheck(pool, &why, &what);
698 * The rules for reserved pool names were extended at a later point.
699 * But we need to support users with existing pools that may now be
700 * invalid. So we only check for this expanded set of names during a
701 * create (or import), and only in userland.
703 if (ret == 0 && !isopen &&
704 (strncmp(pool, "mirror", 6) == 0 ||
705 strncmp(pool, "raidz", 5) == 0 ||
706 strncmp(pool, "spare", 5) == 0 ||
707 strcmp(pool, "log") == 0)) {
710 dgettext(TEXT_DOMAIN, "name is reserved"));
718 case NAME_ERR_TOOLONG:
720 dgettext(TEXT_DOMAIN, "name is too long"));
723 case NAME_ERR_INVALCHAR:
725 dgettext(TEXT_DOMAIN, "invalid character "
726 "'%c' in pool name"), what);
729 case NAME_ERR_NOLETTER:
730 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
731 "name must begin with a letter"));
734 case NAME_ERR_RESERVED:
735 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
736 "name is reserved"));
739 case NAME_ERR_DISKLIKE:
740 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
741 "pool name is reserved"));
744 case NAME_ERR_LEADING_SLASH:
745 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
746 "leading slash in name"));
749 case NAME_ERR_EMPTY_COMPONENT:
750 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
751 "empty component in name"));
754 case NAME_ERR_TRAILING_SLASH:
755 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
756 "trailing slash in name"));
759 case NAME_ERR_MULTIPLE_AT:
760 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
761 "multiple '@' delimiters in name"));
764 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
765 "permission set is missing '@'"));
776 * Open a handle to the given pool, even if the pool is currently in the FAULTED
780 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
786 * Make sure the pool name is valid.
788 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
789 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
790 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
795 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
798 zhp->zpool_hdl = hdl;
799 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
801 if (zpool_refresh_stats(zhp, &missing) != 0) {
807 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
808 (void) zfs_error_fmt(hdl, EZFS_NOENT,
809 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
818 * Like the above, but silent on error. Used when iterating over pools (because
819 * the configuration cache may be out of date).
822 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
827 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
830 zhp->zpool_hdl = hdl;
831 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
833 if (zpool_refresh_stats(zhp, &missing) != 0) {
849 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
853 zpool_open(libzfs_handle_t *hdl, const char *pool)
857 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
860 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
861 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
862 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
871 * Close the handle. Simply frees the memory associated with the handle.
874 zpool_close(zpool_handle_t *zhp)
876 if (zhp->zpool_config)
877 nvlist_free(zhp->zpool_config);
878 if (zhp->zpool_old_config)
879 nvlist_free(zhp->zpool_old_config);
880 if (zhp->zpool_props)
881 nvlist_free(zhp->zpool_props);
886 * Return the name of the pool.
889 zpool_get_name(zpool_handle_t *zhp)
891 return (zhp->zpool_name);
896 * Return the state of the pool (ACTIVE or UNAVAILABLE)
899 zpool_get_state(zpool_handle_t *zhp)
901 return (zhp->zpool_state);
905 * Create the named pool, using the provided vdev list. It is assumed
906 * that the consumer has already validated the contents of the nvlist, so we
907 * don't have to worry about error semantics.
910 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
911 nvlist_t *props, nvlist_t *fsprops)
913 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
914 nvlist_t *zc_fsprops = NULL;
915 nvlist_t *zc_props = NULL;
920 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
921 "cannot create '%s'"), pool);
923 if (!zpool_name_valid(hdl, B_FALSE, pool))
924 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
926 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
930 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
932 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
933 SPA_VERSION_1, flags, msg)) == NULL) {
942 zoned = ((nvlist_lookup_string(fsprops,
943 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
944 strcmp(zonestr, "on") == 0);
946 if ((zc_fsprops = zfs_valid_proplist(hdl,
947 ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
951 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
954 if (nvlist_add_nvlist(zc_props,
955 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
960 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
963 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
965 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
967 zcmd_free_nvlists(&zc);
968 nvlist_free(zc_props);
969 nvlist_free(zc_fsprops);
974 * This can happen if the user has specified the same
975 * device multiple times. We can't reliably detect this
976 * until we try to add it and see we already have a
977 * label. This can also happen under if the device is
978 * part of an active md or lvm device.
980 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
981 "one or more vdevs refer to the same device, or one of\n"
982 "the devices is part of an active md or lvm device"));
983 return (zfs_error(hdl, EZFS_BADDEV, msg));
987 * This occurs when one of the devices is below
988 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
989 * device was the problem device since there's no
990 * reliable way to determine device size from userland.
995 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
997 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
998 "one or more devices is less than the "
999 "minimum size (%s)"), buf);
1001 return (zfs_error(hdl, EZFS_BADDEV, msg));
1004 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1005 "one or more devices is out of space"));
1006 return (zfs_error(hdl, EZFS_BADDEV, msg));
1009 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1010 "cache device must be a disk or disk slice"));
1011 return (zfs_error(hdl, EZFS_BADDEV, msg));
1014 return (zpool_standard_error(hdl, errno, msg));
1019 * If this is an alternate root pool, then we automatically set the
1020 * mountpoint of the root dataset to be '/'.
1022 if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
1026 verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
1027 verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
1034 zcmd_free_nvlists(&zc);
1035 nvlist_free(zc_props);
1036 nvlist_free(zc_fsprops);
1041 * Destroy the given pool. It is up to the caller to ensure that there are no
1042 * datasets left in the pool.
1045 zpool_destroy(zpool_handle_t *zhp)
1047 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1048 zfs_handle_t *zfp = NULL;
1049 libzfs_handle_t *hdl = zhp->zpool_hdl;
1052 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1053 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1056 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1058 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1059 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1060 "cannot destroy '%s'"), zhp->zpool_name);
1062 if (errno == EROFS) {
1063 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1064 "one or more devices is read only"));
1065 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1067 (void) zpool_standard_error(hdl, errno, msg);
1076 remove_mountpoint(zfp);
1084 * Add the given vdevs to the pool. The caller must have already performed the
1085 * necessary verification to ensure that the vdev specification is well-formed.
1088 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1090 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1092 libzfs_handle_t *hdl = zhp->zpool_hdl;
1094 nvlist_t **spares, **l2cache;
1095 uint_t nspares, nl2cache;
1097 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1098 "cannot add to '%s'"), zhp->zpool_name);
1100 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1101 SPA_VERSION_SPARES &&
1102 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1103 &spares, &nspares) == 0) {
1104 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1105 "upgraded to add hot spares"));
1106 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1109 if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1110 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1113 for (s = 0; s < nspares; s++) {
1116 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1117 &path) == 0 && pool_uses_efi(spares[s])) {
1118 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1119 "device '%s' contains an EFI label and "
1120 "cannot be used on root pools."),
1121 zpool_vdev_name(hdl, NULL, spares[s],
1123 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1128 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1129 SPA_VERSION_L2CACHE &&
1130 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1131 &l2cache, &nl2cache) == 0) {
1132 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1133 "upgraded to add cache devices"));
1134 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1137 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1139 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1141 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1145 * This can happen if the user has specified the same
1146 * device multiple times. We can't reliably detect this
1147 * until we try to add it and see we already have a
1150 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1151 "one or more vdevs refer to the same device"));
1152 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1157 * This occurrs when one of the devices is below
1158 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1159 * device was the problem device since there's no
1160 * reliable way to determine device size from userland.
1165 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1167 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1168 "device is less than the minimum "
1171 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1175 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1176 "pool must be upgraded to add these vdevs"));
1177 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1181 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1182 "root pool can not have multiple vdevs"
1183 " or separate logs"));
1184 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1188 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1189 "cache device must be a disk or disk slice"));
1190 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1194 (void) zpool_standard_error(hdl, errno, msg);
1202 zcmd_free_nvlists(&zc);
1208 * Exports the pool from the system. The caller must ensure that there are no
1209 * mounted datasets in the pool.
1212 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1214 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1217 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1218 "cannot export '%s'"), zhp->zpool_name);
1220 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1221 zc.zc_cookie = force;
1222 zc.zc_guid = hardforce;
1224 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1227 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1228 "use '-f' to override the following errors:\n"
1229 "'%s' has an active shared spare which could be"
1230 " used by other pools once '%s' is exported."),
1231 zhp->zpool_name, zhp->zpool_name);
1232 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1235 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1244 zpool_export(zpool_handle_t *zhp, boolean_t force)
1246 return (zpool_export_common(zhp, force, B_FALSE));
1250 zpool_export_force(zpool_handle_t *zhp)
1252 return (zpool_export_common(zhp, B_TRUE, B_TRUE));
1256 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1259 nvlist_t *nv = NULL;
1265 if (!hdl->libzfs_printerr || config == NULL)
1268 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0)
1271 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1273 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1275 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1276 strftime(timestr, 128, "%c", &t) != 0) {
1278 (void) printf(dgettext(TEXT_DOMAIN,
1279 "Would be able to return %s "
1280 "to its state as of %s.\n"),
1283 (void) printf(dgettext(TEXT_DOMAIN,
1284 "Pool %s returned to its state as of %s.\n"),
1288 (void) printf(dgettext(TEXT_DOMAIN,
1289 "%s approximately %lld "),
1290 dryrun ? "Would discard" : "Discarded",
1291 ((longlong_t)loss + 30) / 60);
1292 (void) printf(dgettext(TEXT_DOMAIN,
1293 "minutes of transactions.\n"));
1294 } else if (loss > 0) {
1295 (void) printf(dgettext(TEXT_DOMAIN,
1296 "%s approximately %lld "),
1297 dryrun ? "Would discard" : "Discarded",
1299 (void) printf(dgettext(TEXT_DOMAIN,
1300 "seconds of transactions.\n"));
1306 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1309 nvlist_t *nv = NULL;
1311 uint64_t edata = UINT64_MAX;
1316 if (!hdl->libzfs_printerr)
1320 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1322 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1324 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1325 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1326 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1329 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1330 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1333 (void) printf(dgettext(TEXT_DOMAIN,
1334 "Recovery is possible, but will result in some data loss.\n"));
1336 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1337 strftime(timestr, 128, "%c", &t) != 0) {
1338 (void) printf(dgettext(TEXT_DOMAIN,
1339 "\tReturning the pool to its state as of %s\n"
1340 "\tshould correct the problem. "),
1343 (void) printf(dgettext(TEXT_DOMAIN,
1344 "\tReverting the pool to an earlier state "
1345 "should correct the problem.\n\t"));
1349 (void) printf(dgettext(TEXT_DOMAIN,
1350 "Approximately %lld minutes of data\n"
1351 "\tmust be discarded, irreversibly. "),
1352 ((longlong_t)loss + 30) / 60);
1353 } else if (loss > 0) {
1354 (void) printf(dgettext(TEXT_DOMAIN,
1355 "Approximately %lld seconds of data\n"
1356 "\tmust be discarded, irreversibly. "),
1359 if (edata != 0 && edata != UINT64_MAX) {
1361 (void) printf(dgettext(TEXT_DOMAIN,
1362 "After rewind, at least\n"
1363 "\tone persistent user-data error will remain. "));
1365 (void) printf(dgettext(TEXT_DOMAIN,
1366 "After rewind, several\n"
1367 "\tpersistent user-data errors will remain. "));
1370 (void) printf(dgettext(TEXT_DOMAIN,
1371 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1372 reason >= 0 ? "clear" : "import", name);
1374 (void) printf(dgettext(TEXT_DOMAIN,
1375 "A scrub of the pool\n"
1376 "\tis strongly recommended after recovery.\n"));
1380 (void) printf(dgettext(TEXT_DOMAIN,
1381 "Destroy and re-create the pool from\n\ta backup source.\n"));
1385 * zpool_import() is a contracted interface. Should be kept the same
1388 * Applications should use zpool_import_props() to import a pool with
1389 * new properties value to be set.
1392 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1395 nvlist_t *props = NULL;
1398 if (altroot != NULL) {
1399 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1400 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1401 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1405 if (nvlist_add_string(props,
1406 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1407 nvlist_add_string(props,
1408 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1410 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1411 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1416 ret = zpool_import_props(hdl, config, newname, props,
1424 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1430 uint64_t is_log = 0;
1432 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1436 (void) printf("\t%*s%s%s\n", indent, "", name,
1437 is_log ? " [log]" : "");
1439 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1440 &child, &children) != 0)
1443 for (c = 0; c < children; c++) {
1444 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
1445 print_vdev_tree(hdl, vname, child[c], indent + 2);
1451 * Import the given pool using the known configuration and a list of
1452 * properties to be set. The configuration should have come from
1453 * zpool_find_import(). The 'newname' parameters control whether the pool
1454 * is imported with a different name.
1457 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1458 nvlist_t *props, int flags)
1460 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1461 zpool_rewind_policy_t policy;
1462 nvlist_t *nv = NULL;
1463 nvlist_t *nvinfo = NULL;
1464 nvlist_t *missing = NULL;
1471 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1474 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1475 "cannot import pool '%s'"), origname);
1477 if (newname != NULL) {
1478 if (!zpool_name_valid(hdl, B_FALSE, newname))
1479 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1480 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1482 thename = (char *)newname;
1489 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1491 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1494 if ((props = zpool_valid_proplist(hdl, origname,
1495 props, version, flags, errbuf)) == NULL) {
1497 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1503 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1505 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1508 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1512 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1517 zc.zc_cookie = flags;
1518 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1520 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1521 zcmd_free_nvlists(&zc);
1528 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1529 zpool_get_rewind_policy(config, &policy);
1535 * Dry-run failed, but we print out what success
1536 * looks like if we found a best txg
1538 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1539 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1545 if (newname == NULL)
1546 (void) snprintf(desc, sizeof (desc),
1547 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1550 (void) snprintf(desc, sizeof (desc),
1551 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1557 * Unsupported version.
1559 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1563 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1567 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1568 "one or more devices is read only"));
1569 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1573 if (nv && nvlist_lookup_nvlist(nv,
1574 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1575 nvlist_lookup_nvlist(nvinfo,
1576 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1577 (void) printf(dgettext(TEXT_DOMAIN,
1578 "The devices below are missing, use "
1579 "'-m' to import the pool anyway:\n"));
1580 print_vdev_tree(hdl, NULL, missing, 2);
1581 (void) printf("\n");
1583 (void) zpool_standard_error(hdl, error, desc);
1587 (void) zpool_standard_error(hdl, error, desc);
1591 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1592 "one or more devices are already in use\n"));
1593 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1597 (void) zpool_standard_error(hdl, error, desc);
1598 zpool_explain_recover(hdl,
1599 newname ? origname : thename, -error, nv);
1606 zpool_handle_t *zhp;
1609 * This should never fail, but play it safe anyway.
1611 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1613 else if (zhp != NULL)
1615 if (policy.zrp_request &
1616 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1617 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1618 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1624 zcmd_free_nvlists(&zc);
1634 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func)
1636 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
1638 libzfs_handle_t *hdl = zhp->zpool_hdl;
1640 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1641 zc.zc_cookie = func;
1643 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0 ||
1644 (errno == ENOENT && func != POOL_SCAN_NONE))
1647 if (func == POOL_SCAN_SCRUB) {
1648 (void) snprintf(msg, sizeof (msg),
1649 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1650 } else if (func == POOL_SCAN_NONE) {
1651 (void) snprintf(msg, sizeof (msg),
1652 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1655 assert(!"unexpected result");
1658 if (errno == EBUSY) {
1660 pool_scan_stat_t *ps = NULL;
1663 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1664 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1665 (void) nvlist_lookup_uint64_array(nvroot,
1666 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1667 if (ps && ps->pss_func == POOL_SCAN_SCRUB)
1668 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1670 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1671 } else if (errno == ENOENT) {
1672 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1674 return (zpool_standard_error(hdl, errno, msg));
1679 * Find a vdev that matches the search criteria specified. We use the
1680 * the nvpair name to determine how we should look for the device.
1681 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1682 * spare; but FALSE if its an INUSE spare.
1685 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
1686 boolean_t *l2cache, boolean_t *log)
1693 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
1695 /* Nothing to look for */
1696 if (search == NULL || pair == NULL)
1699 /* Obtain the key we will use to search */
1700 srchkey = nvpair_name(pair);
1702 switch (nvpair_type(pair)) {
1703 case DATA_TYPE_UINT64:
1704 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
1705 uint64_t srchval, theguid;
1707 verify(nvpair_value_uint64(pair, &srchval) == 0);
1708 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
1710 if (theguid == srchval)
1715 case DATA_TYPE_STRING: {
1716 char *srchval, *val;
1718 verify(nvpair_value_string(pair, &srchval) == 0);
1719 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
1723 * Search for the requested value. Special cases:
1725 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in with a
1726 * partition suffix "1", "-part1", or "p1". The suffix is hidden
1727 * from the user, but included in the string, so this matches around
1729 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1731 * Otherwise, all other searches are simple string compares.
1733 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) {
1734 uint64_t wholedisk = 0;
1736 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1739 char buf[MAXPATHLEN];
1741 zfs_append_partition(srchval, buf, sizeof (buf));
1742 if (strcmp(val, buf) == 0)
1747 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
1748 char *type, *idx, *end, *p;
1749 uint64_t id, vdev_id;
1752 * Determine our vdev type, keeping in mind
1753 * that the srchval is composed of a type and
1754 * vdev id pair (i.e. mirror-4).
1756 if ((type = strdup(srchval)) == NULL)
1759 if ((p = strrchr(type, '-')) == NULL) {
1767 * If the types don't match then keep looking.
1769 if (strncmp(val, type, strlen(val)) != 0) {
1774 verify(strncmp(type, VDEV_TYPE_RAIDZ,
1775 strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1776 strncmp(type, VDEV_TYPE_MIRROR,
1777 strlen(VDEV_TYPE_MIRROR)) == 0);
1778 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
1782 vdev_id = strtoull(idx, &end, 10);
1789 * Now verify that we have the correct vdev id.
1798 if (strcmp(srchval, val) == 0)
1807 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1808 &child, &children) != 0)
1811 for (c = 0; c < children; c++) {
1812 if ((ret = vdev_to_nvlist_iter(child[c], search,
1813 avail_spare, l2cache, NULL)) != NULL) {
1815 * The 'is_log' value is only set for the toplevel
1816 * vdev, not the leaf vdevs. So we always lookup the
1817 * log device from the root of the vdev tree (where
1818 * 'log' is non-NULL).
1821 nvlist_lookup_uint64(child[c],
1822 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
1830 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1831 &child, &children) == 0) {
1832 for (c = 0; c < children; c++) {
1833 if ((ret = vdev_to_nvlist_iter(child[c], search,
1834 avail_spare, l2cache, NULL)) != NULL) {
1835 *avail_spare = B_TRUE;
1841 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1842 &child, &children) == 0) {
1843 for (c = 0; c < children; c++) {
1844 if ((ret = vdev_to_nvlist_iter(child[c], search,
1845 avail_spare, l2cache, NULL)) != NULL) {
1856 * Given a physical path (minus the "/devices" prefix), find the
1860 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
1861 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
1863 nvlist_t *search, *nvroot, *ret;
1865 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1866 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
1868 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1871 *avail_spare = B_FALSE;
1875 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1876 nvlist_free(search);
1882 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
1885 zpool_vdev_is_interior(const char *name)
1887 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
1888 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
1894 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
1895 boolean_t *l2cache, boolean_t *log)
1897 char buf[MAXPATHLEN];
1899 nvlist_t *nvroot, *search, *ret;
1902 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
1904 guid = strtoull(path, &end, 10);
1905 if (guid != 0 && *end == '\0') {
1906 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
1907 } else if (zpool_vdev_is_interior(path)) {
1908 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
1909 } else if (path[0] != '/') {
1910 if (zfs_resolve_shortname(path, buf, sizeof (buf)) < 0) {
1911 nvlist_free(search);
1914 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
1916 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
1919 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1922 *avail_spare = B_FALSE;
1926 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
1927 nvlist_free(search);
1933 vdev_online(nvlist_t *nv)
1937 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
1938 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
1939 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
1946 * Helper function for zpool_get_physpaths().
1949 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
1950 size_t *bytes_written)
1952 size_t bytes_left, pos, rsz;
1956 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
1958 return (EZFS_NODEVICE);
1960 pos = *bytes_written;
1961 bytes_left = physpath_size - pos;
1962 format = (pos == 0) ? "%s" : " %s";
1964 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
1965 *bytes_written += rsz;
1967 if (rsz >= bytes_left) {
1968 /* if physpath was not copied properly, clear it */
1969 if (bytes_left != 0) {
1972 return (EZFS_NOSPC);
1978 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
1979 size_t *rsz, boolean_t is_spare)
1984 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
1985 return (EZFS_INVALCONFIG);
1987 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
1989 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
1990 * For a spare vdev, we only want to boot from the active
1995 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
1998 return (EZFS_INVALCONFIG);
2001 if (vdev_online(nv)) {
2002 if ((ret = vdev_get_one_physpath(nv, physpath,
2003 phypath_size, rsz)) != 0)
2006 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2007 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2008 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2013 if (nvlist_lookup_nvlist_array(nv,
2014 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2015 return (EZFS_INVALCONFIG);
2017 for (i = 0; i < count; i++) {
2018 ret = vdev_get_physpaths(child[i], physpath,
2019 phypath_size, rsz, is_spare);
2020 if (ret == EZFS_NOSPC)
2025 return (EZFS_POOL_INVALARG);
2029 * Get phys_path for a root pool config.
2030 * Return 0 on success; non-zero on failure.
2033 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2036 nvlist_t *vdev_root;
2043 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2045 return (EZFS_INVALCONFIG);
2047 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2048 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2049 &child, &count) != 0)
2050 return (EZFS_INVALCONFIG);
2053 * root pool can not have EFI labeled disks and can only have
2054 * a single top-level vdev.
2056 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1 ||
2057 pool_uses_efi(vdev_root))
2058 return (EZFS_POOL_INVALARG);
2060 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2063 /* No online devices */
2065 return (EZFS_NODEVICE);
2071 * Get phys_path for a root pool
2072 * Return 0 on success; non-zero on failure.
2075 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2077 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2082 * If the device has being dynamically expanded then we need to relabel
2083 * the disk to use the new unallocated space.
2086 zpool_relabel_disk(libzfs_handle_t *hdl, const char *path, const char *msg)
2090 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
2091 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2092 "relabel '%s': unable to open device: %d"), path, errno);
2093 return (zfs_error(hdl, EZFS_OPENFAILED, msg));
2097 * It's possible that we might encounter an error if the device
2098 * does not have any unallocated space left. If so, we simply
2099 * ignore that error and continue on.
2101 error = efi_use_whole_disk(fd);
2103 if (error && error != VT_ENOSPC) {
2104 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2105 "relabel '%s': unable to read disk capacity"), path);
2106 return (zfs_error(hdl, EZFS_NOCAP, msg));
2112 * Bring the specified vdev online. The 'flags' parameter is a set of the
2113 * ZFS_ONLINE_* flags.
2116 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2117 vdev_state_t *newstate)
2119 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2122 boolean_t avail_spare, l2cache, islog;
2123 libzfs_handle_t *hdl = zhp->zpool_hdl;
2126 if (flags & ZFS_ONLINE_EXPAND) {
2127 (void) snprintf(msg, sizeof (msg),
2128 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2130 (void) snprintf(msg, sizeof (msg),
2131 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2134 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2135 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2137 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2139 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2142 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2144 if (flags & ZFS_ONLINE_EXPAND ||
2145 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2146 char *pathname = NULL;
2147 uint64_t wholedisk = 0;
2149 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2151 verify(nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH,
2155 * XXX - L2ARC 1.0 devices can't support expansion.
2158 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2159 "cannot expand cache devices"));
2160 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2164 pathname += strlen(DISK_ROOT) + 1;
2165 error = zpool_relabel_disk(hdl, pathname, msg);
2171 zc.zc_cookie = VDEV_STATE_ONLINE;
2174 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2175 if (errno == EINVAL) {
2176 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2177 "from this pool into a new one. Use '%s' "
2178 "instead"), "zpool detach");
2179 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2181 return (zpool_standard_error(hdl, errno, msg));
2184 *newstate = zc.zc_cookie;
2189 * Take the specified vdev offline
2192 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2194 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2197 boolean_t avail_spare, l2cache;
2198 libzfs_handle_t *hdl = zhp->zpool_hdl;
2200 (void) snprintf(msg, sizeof (msg),
2201 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2203 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2204 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2206 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2208 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2211 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2213 zc.zc_cookie = VDEV_STATE_OFFLINE;
2214 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2216 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2223 * There are no other replicas of this device.
2225 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2229 * The log device has unplayed logs
2231 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2234 return (zpool_standard_error(hdl, errno, msg));
2239 * Mark the given vdev faulted.
2242 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2244 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2246 libzfs_handle_t *hdl = zhp->zpool_hdl;
2248 (void) snprintf(msg, sizeof (msg),
2249 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
2251 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2253 zc.zc_cookie = VDEV_STATE_FAULTED;
2256 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2263 * There are no other replicas of this device.
2265 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2268 return (zpool_standard_error(hdl, errno, msg));
2274 * Mark the given vdev degraded.
2277 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2279 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2281 libzfs_handle_t *hdl = zhp->zpool_hdl;
2283 (void) snprintf(msg, sizeof (msg),
2284 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid);
2286 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2288 zc.zc_cookie = VDEV_STATE_DEGRADED;
2291 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2294 return (zpool_standard_error(hdl, errno, msg));
2298 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2302 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2308 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2310 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2313 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2314 children == 2 && child[which] == tgt)
2317 for (c = 0; c < children; c++)
2318 if (is_replacing_spare(child[c], tgt, which))
2326 * Attach new_disk (fully described by nvroot) to old_disk.
2327 * If 'replacing' is specified, the new disk will replace the old one.
2330 zpool_vdev_attach(zpool_handle_t *zhp,
2331 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2333 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2337 boolean_t avail_spare, l2cache, islog;
2342 nvlist_t *config_root;
2343 libzfs_handle_t *hdl = zhp->zpool_hdl;
2344 boolean_t rootpool = pool_is_bootable(zhp);
2347 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2348 "cannot replace %s with %s"), old_disk, new_disk);
2350 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2351 "cannot attach %s to %s"), new_disk, old_disk);
2354 * If this is a root pool, make sure that we're not attaching an
2355 * EFI labeled device.
2357 if (rootpool && pool_uses_efi(nvroot)) {
2358 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2359 "EFI labeled devices are not supported on root pools."));
2360 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
2363 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2364 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2366 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2369 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2372 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2374 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2375 zc.zc_cookie = replacing;
2377 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2378 &child, &children) != 0 || children != 1) {
2379 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2380 "new device must be a single disk"));
2381 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2384 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2385 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2387 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2391 * If the target is a hot spare that has been swapped in, we can only
2392 * replace it with another hot spare.
2395 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2396 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2397 NULL) == NULL || !avail_spare) &&
2398 is_replacing_spare(config_root, tgt, 1)) {
2399 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2400 "can only be replaced by another hot spare"));
2402 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2407 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2410 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2412 zcmd_free_nvlists(&zc);
2417 * XXX need a better way to prevent user from
2418 * booting up a half-baked vdev.
2420 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2421 "sure to wait until resilver is done "
2422 "before rebooting.\n"));
2430 * Can't attach to or replace this type of vdev.
2433 uint64_t version = zpool_get_prop_int(zhp,
2434 ZPOOL_PROP_VERSION, NULL);
2437 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2438 "cannot replace a log with a spare"));
2439 else if (version >= SPA_VERSION_MULTI_REPLACE)
2440 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2441 "already in replacing/spare config; wait "
2442 "for completion or use 'zpool detach'"));
2444 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2445 "cannot replace a replacing device"));
2447 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2448 "can only attach to mirrors and top-level "
2451 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2456 * The new device must be a single disk.
2458 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2459 "new device must be a single disk"));
2460 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2464 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2466 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2471 * The new device is too small.
2473 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2474 "device is too small"));
2475 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2480 * The new device has a different alignment requirement.
2482 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2483 "devices have different sector alignment"));
2484 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2489 * The resulting top-level vdev spec won't fit in the label.
2491 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2495 (void) zpool_standard_error(hdl, errno, msg);
2502 * Detach the specified device.
2505 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2507 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2510 boolean_t avail_spare, l2cache;
2511 libzfs_handle_t *hdl = zhp->zpool_hdl;
2513 (void) snprintf(msg, sizeof (msg),
2514 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2516 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2517 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2519 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2522 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2525 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2527 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2529 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2536 * Can't detach from this type of vdev.
2538 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2539 "applicable to mirror and replacing vdevs"));
2540 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2545 * There are no other replicas of this device.
2547 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2551 (void) zpool_standard_error(hdl, errno, msg);
2558 * Find a mirror vdev in the source nvlist.
2560 * The mchild array contains a list of disks in one of the top-level mirrors
2561 * of the source pool. The schild array contains a list of disks that the
2562 * user specified on the command line. We loop over the mchild array to
2563 * see if any entry in the schild array matches.
2565 * If a disk in the mchild array is found in the schild array, we return
2566 * the index of that entry. Otherwise we return -1.
2569 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2570 nvlist_t **schild, uint_t schildren)
2574 for (mc = 0; mc < mchildren; mc++) {
2576 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2577 mchild[mc], B_FALSE);
2579 for (sc = 0; sc < schildren; sc++) {
2580 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2581 schild[sc], B_FALSE);
2582 boolean_t result = (strcmp(mpath, spath) == 0);
2598 * Split a mirror pool. If newroot points to null, then a new nvlist
2599 * is generated and it is the responsibility of the caller to free it.
2602 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2603 nvlist_t *props, splitflags_t flags)
2605 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2607 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2608 nvlist_t **varray = NULL, *zc_props = NULL;
2609 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2610 libzfs_handle_t *hdl = zhp->zpool_hdl;
2612 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2615 (void) snprintf(msg, sizeof (msg),
2616 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2618 if (!zpool_name_valid(hdl, B_FALSE, newname))
2619 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2621 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2622 (void) fprintf(stderr, gettext("Internal error: unable to "
2623 "retrieve pool configuration\n"));
2627 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
2629 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
2632 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
2633 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
2634 props, vers, flags, msg)) == NULL)
2638 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
2640 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2641 "Source pool is missing vdev tree"));
2643 nvlist_free(zc_props);
2647 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
2650 if (*newroot == NULL ||
2651 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
2652 &newchild, &newchildren) != 0)
2655 for (c = 0; c < children; c++) {
2656 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
2658 nvlist_t **mchild, *vdev;
2663 * Unlike cache & spares, slogs are stored in the
2664 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2666 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
2668 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
2670 if (is_log || is_hole) {
2672 * Create a hole vdev and put it in the config.
2674 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
2676 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
2677 VDEV_TYPE_HOLE) != 0)
2679 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
2684 varray[vcount++] = vdev;
2688 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
2690 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
2691 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2692 "Source pool must be composed only of mirrors\n"));
2693 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2697 verify(nvlist_lookup_nvlist_array(child[c],
2698 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
2700 /* find or add an entry for this top-level vdev */
2701 if (newchildren > 0 &&
2702 (entry = find_vdev_entry(zhp, mchild, mchildren,
2703 newchild, newchildren)) >= 0) {
2704 /* We found a disk that the user specified. */
2705 vdev = mchild[entry];
2708 /* User didn't specify a disk for this vdev. */
2709 vdev = mchild[mchildren - 1];
2712 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
2716 /* did we find every disk the user specified? */
2717 if (found != newchildren) {
2718 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
2719 "include at most one disk from each mirror"));
2720 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
2724 /* Prepare the nvlist for populating. */
2725 if (*newroot == NULL) {
2726 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
2729 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
2730 VDEV_TYPE_ROOT) != 0)
2733 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
2736 /* Add all the children we found */
2737 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
2738 lastlog == 0 ? vcount : lastlog) != 0)
2742 * If we're just doing a dry run, exit now with success.
2745 memory_err = B_FALSE;
2750 /* now build up the config list & call the ioctl */
2751 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
2754 if (nvlist_add_nvlist(newconfig,
2755 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
2756 nvlist_add_string(newconfig,
2757 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
2758 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
2762 * The new pool is automatically part of the namespace unless we
2763 * explicitly export it.
2766 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
2767 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2768 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
2769 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
2771 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
2774 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
2775 retval = zpool_standard_error(hdl, errno, msg);
2780 memory_err = B_FALSE;
2783 if (varray != NULL) {
2786 for (v = 0; v < vcount; v++)
2787 nvlist_free(varray[v]);
2790 zcmd_free_nvlists(&zc);
2792 nvlist_free(zc_props);
2794 nvlist_free(newconfig);
2796 nvlist_free(*newroot);
2804 return (no_memory(hdl));
2810 * Remove the given device. Currently, this is supported only for hot spares
2811 * and level 2 cache devices.
2814 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
2816 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2819 boolean_t avail_spare, l2cache, islog;
2820 libzfs_handle_t *hdl = zhp->zpool_hdl;
2823 (void) snprintf(msg, sizeof (msg),
2824 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
2826 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2827 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2829 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2831 * XXX - this should just go away.
2833 if (!avail_spare && !l2cache && !islog) {
2834 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2835 "only inactive hot spares, cache, top-level, "
2836 "or log devices can be removed"));
2837 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2840 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
2841 if (islog && version < SPA_VERSION_HOLES) {
2842 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2843 "pool must be upgrade to support log removal"));
2844 return (zfs_error(hdl, EZFS_BADVERSION, msg));
2847 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2849 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
2852 return (zpool_standard_error(hdl, errno, msg));
2856 * Clear the errors for the pool, or the particular device if specified.
2859 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
2861 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2864 zpool_rewind_policy_t policy;
2865 boolean_t avail_spare, l2cache;
2866 libzfs_handle_t *hdl = zhp->zpool_hdl;
2867 nvlist_t *nvi = NULL;
2871 (void) snprintf(msg, sizeof (msg),
2872 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2875 (void) snprintf(msg, sizeof (msg),
2876 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2879 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2881 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
2882 &l2cache, NULL)) == 0)
2883 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2886 * Don't allow error clearing for hot spares. Do allow
2887 * error clearing for l2cache devices.
2890 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2892 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
2896 zpool_get_rewind_policy(rewindnvl, &policy);
2897 zc.zc_cookie = policy.zrp_request;
2899 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
2902 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
2905 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
2907 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
2908 zcmd_free_nvlists(&zc);
2913 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
2914 errno != EPERM && errno != EACCES)) {
2915 if (policy.zrp_request &
2916 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
2917 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
2918 zpool_rewind_exclaim(hdl, zc.zc_name,
2919 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
2923 zcmd_free_nvlists(&zc);
2927 zcmd_free_nvlists(&zc);
2928 return (zpool_standard_error(hdl, errno, msg));
2932 * Similar to zpool_clear(), but takes a GUID (used by fmd).
2935 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
2937 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2939 libzfs_handle_t *hdl = zhp->zpool_hdl;
2941 (void) snprintf(msg, sizeof (msg),
2942 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
2943 (u_longlong_t)guid);
2945 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2947 zc.zc_cookie = ZPOOL_NO_REWIND;
2949 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
2952 return (zpool_standard_error(hdl, errno, msg));
2956 * Change the GUID for a pool.
2959 zpool_reguid(zpool_handle_t *zhp)
2962 libzfs_handle_t *hdl = zhp->zpool_hdl;
2963 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
2965 (void) snprintf(msg, sizeof (msg),
2966 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
2968 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2969 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
2972 return (zpool_standard_error(hdl, errno, msg));
2976 * Convert from a devid string to a path.
2979 devid_to_path(char *devid_str)
2984 devid_nmlist_t *list = NULL;
2987 if (devid_str_decode(devid_str, &devid, &minor) != 0)
2990 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
2992 devid_str_free(minor);
2998 if ((path = strdup(list[0].devname)) == NULL)
3001 devid_free_nmlist(list);
3007 * Convert from a path to a devid string.
3010 path_to_devid(const char *path)
3016 if ((fd = open(path, O_RDONLY)) < 0)
3021 if (devid_get(fd, &devid) == 0) {
3022 if (devid_get_minor_name(fd, &minor) == 0)
3023 ret = devid_str_encode(devid, minor);
3025 devid_str_free(minor);
3034 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3035 * ignore any failure here, since a common case is for an unprivileged user to
3036 * type 'zpool status', and we'll display the correct information anyway.
3039 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3041 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3043 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3044 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3045 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3048 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3052 * Remove partition suffix from a vdev path. Partition suffixes may take three
3053 * forms: "-partX", "pX", or "X", where X is a string of digits. The second
3054 * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
3055 * third case only occurs when preceded by a string matching the regular
3056 * expression "^[hs]d[a-z]+", i.e. a scsi or ide disk.
3059 strip_partition(libzfs_handle_t *hdl, char *path)
3061 char *tmp = zfs_strdup(hdl, path);
3062 char *part = NULL, *d = NULL;
3064 if ((part = strstr(tmp, "-part")) && part != tmp) {
3066 } else if ((part = strrchr(tmp, 'p')) &&
3067 part > tmp + 1 && isdigit(*(part-1))) {
3069 } else if ((tmp[0] == 'h' || tmp[0] == 's') && tmp[1] == 'd') {
3070 for (d = &tmp[2]; isalpha(*d); part = ++d);
3072 if (part && d && *d != '\0') {
3073 for (; isdigit(*d); d++);
3080 #define PATH_BUF_LEN 64
3083 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3084 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3085 * We also check if this is a whole disk, in which case we strip off the
3086 * trailing 's0' slice name.
3088 * This routine is also responsible for identifying when disks have been
3089 * reconfigured in a new location. The kernel will have opened the device by
3090 * devid, but the path will still refer to the old location. To catch this, we
3091 * first do a path -> devid translation (which is fast for the common case). If
3092 * the devid matches, we're done. If not, we do a reverse devid -> path
3093 * translation and issue the appropriate ioctl() to update the path of the vdev.
3094 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3098 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3101 char *path, *devid, *type;
3103 char buf[PATH_BUF_LEN];
3107 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
3109 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3111 (void) snprintf(buf, sizeof (buf), "%llu",
3112 (u_longlong_t)value);
3114 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3116 * If the device is dead (faulted, offline, etc) then don't
3117 * bother opening it. Otherwise we may be forcing the user to
3118 * open a misbehaving device, which can have undesirable
3121 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3122 (uint64_t **)&vs, &vsc) != 0 ||
3123 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3125 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3127 * Determine if the current path is correct.
3129 char *newdevid = path_to_devid(path);
3131 if (newdevid == NULL ||
3132 strcmp(devid, newdevid) != 0) {
3135 if ((newpath = devid_to_path(devid)) != NULL) {
3137 * Update the path appropriately.
3139 set_path(zhp, nv, newpath);
3140 if (nvlist_add_string(nv,
3141 ZPOOL_CONFIG_PATH, newpath) == 0)
3142 verify(nvlist_lookup_string(nv,
3150 devid_str_free(newdevid);
3154 * For a block device only use the name.
3156 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
3157 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
3158 path = strrchr(path, '/');
3163 * Remove the partition from the path it this is a whole disk.
3165 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3166 &value) == 0 && value) {
3167 return strip_partition(hdl, path);
3170 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3173 * If it's a raidz device, we need to stick in the parity level.
3175 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3176 char tmpbuf[PATH_BUF_LEN];
3178 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3180 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s%llu", path,
3181 (u_longlong_t)value);
3186 * We identify each top-level vdev by using a <type-id>
3187 * naming convention.
3192 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3194 (void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3200 return (zfs_strdup(hdl, path));
3204 zbookmark_compare(const void *a, const void *b)
3206 return (memcmp(a, b, sizeof (zbookmark_t)));
3210 * Retrieve the persistent error log, uniquify the members, and return to the
3214 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3216 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3218 zbookmark_t *zb = NULL;
3222 * Retrieve the raw error list from the kernel. If the number of errors
3223 * has increased, allocate more space and continue until we get the
3226 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3230 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3231 count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
3233 zc.zc_nvlist_dst_size = count;
3234 (void) strcpy(zc.zc_name, zhp->zpool_name);
3236 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3238 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3239 if (errno == ENOMEM) {
3240 count = zc.zc_nvlist_dst_size;
3241 if ((zc.zc_nvlist_dst = (uintptr_t)
3242 zfs_alloc(zhp->zpool_hdl, count *
3243 sizeof (zbookmark_t))) == (uintptr_t)NULL)
3254 * Sort the resulting bookmarks. This is a little confusing due to the
3255 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3256 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3257 * _not_ copied as part of the process. So we point the start of our
3258 * array appropriate and decrement the total number of elements.
3260 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
3261 zc.zc_nvlist_dst_size;
3262 count -= zc.zc_nvlist_dst_size;
3264 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
3266 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3269 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3271 for (i = 0; i < count; i++) {
3274 /* ignoring zb_blkid and zb_level for now */
3275 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3276 zb[i-1].zb_object == zb[i].zb_object)
3279 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3281 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3282 zb[i].zb_objset) != 0) {
3286 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3287 zb[i].zb_object) != 0) {
3291 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3298 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3302 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3303 return (no_memory(zhp->zpool_hdl));
3307 * Upgrade a ZFS pool to the latest on-disk version.
3310 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3312 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3313 libzfs_handle_t *hdl = zhp->zpool_hdl;
3315 (void) strcpy(zc.zc_name, zhp->zpool_name);
3316 zc.zc_cookie = new_version;
3318 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3319 return (zpool_standard_error_fmt(hdl, errno,
3320 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3326 zpool_set_history_str(const char *subcommand, int argc, char **argv,
3331 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
3332 for (i = 1; i < argc; i++) {
3333 if (strlen(history_str) + 1 + strlen(argv[i]) >
3336 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
3337 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
3342 * Stage command history for logging.
3345 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
3347 if (history_str == NULL)
3350 if (strlen(history_str) > HIS_MAX_RECORD_LEN)
3353 if (hdl->libzfs_log_str != NULL)
3354 free(hdl->libzfs_log_str);
3356 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
3357 return (no_memory(hdl));
3363 * Perform ioctl to get some command history of a pool.
3365 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3366 * logical offset of the history buffer to start reading from.
3368 * Upon return, 'off' is the next logical offset to read from and
3369 * 'len' is the actual amount of bytes read into 'buf'.
3372 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3374 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3375 libzfs_handle_t *hdl = zhp->zpool_hdl;
3377 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3379 zc.zc_history = (uint64_t)(uintptr_t)buf;
3380 zc.zc_history_len = *len;
3381 zc.zc_history_offset = *off;
3383 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3386 return (zfs_error_fmt(hdl, EZFS_PERM,
3387 dgettext(TEXT_DOMAIN,
3388 "cannot show history for pool '%s'"),
3391 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3392 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3393 "'%s'"), zhp->zpool_name));
3395 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3396 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3397 "'%s', pool must be upgraded"), zhp->zpool_name));
3399 return (zpool_standard_error_fmt(hdl, errno,
3400 dgettext(TEXT_DOMAIN,
3401 "cannot get history for '%s'"), zhp->zpool_name));
3405 *len = zc.zc_history_len;
3406 *off = zc.zc_history_offset;
3412 * Process the buffer of nvlists, unpacking and storing each nvlist record
3413 * into 'records'. 'leftover' is set to the number of bytes that weren't
3414 * processed as there wasn't a complete record.
3417 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3418 nvlist_t ***records, uint_t *numrecords)
3424 while (bytes_read > sizeof (reclen)) {
3426 /* get length of packed record (stored as little endian) */
3427 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3428 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3430 if (bytes_read < sizeof (reclen) + reclen)
3434 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3436 bytes_read -= sizeof (reclen) + reclen;
3437 buf += sizeof (reclen) + reclen;
3439 /* add record to nvlist array */
3441 if (ISP2(*numrecords + 1)) {
3442 *records = realloc(*records,
3443 *numrecords * 2 * sizeof (nvlist_t *));
3445 (*records)[*numrecords - 1] = nv;
3448 *leftover = bytes_read;
3452 #define HIS_BUF_LEN (128*1024)
3455 * Retrieve the command history of a pool.
3458 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3460 char buf[HIS_BUF_LEN];
3462 nvlist_t **records = NULL;
3463 uint_t numrecords = 0;
3467 uint64_t bytes_read = sizeof (buf);
3470 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3473 /* if nothing else was read in, we're at EOF, just return */
3477 if ((err = zpool_history_unpack(buf, bytes_read,
3478 &leftover, &records, &numrecords)) != 0)
3486 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3487 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3488 records, numrecords) == 0);
3490 for (i = 0; i < numrecords; i++)
3491 nvlist_free(records[i]);
3498 * Retrieve the next event. If there is a new event available 'nvp' will
3499 * contain a newly allocated nvlist and 'dropped' will be set to the number
3500 * of missed events since the last call to this function. When 'nvp' is
3501 * set to NULL it indicates no new events are available. In either case
3502 * the function returns 0 and it is up to the caller to free 'nvp'. In
3503 * the case of a fatal error the function will return a non-zero value.
3504 * When the function is called in blocking mode it will not return until
3505 * a new event is available.
3508 zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
3509 int *dropped, int block, int cleanup_fd)
3511 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3516 zc.zc_cleanup_fd = cleanup_fd;
3519 zc.zc_guid = ZEVENT_NONBLOCK;
3521 if (zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE) != 0)
3525 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
3528 error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
3529 dgettext(TEXT_DOMAIN, "zfs shutdown"));
3532 /* Blocking error case should not occur */
3534 error = zpool_standard_error_fmt(hdl, errno,
3535 dgettext(TEXT_DOMAIN, "cannot get event"));
3539 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3540 error = zfs_error_fmt(hdl, EZFS_NOMEM,
3541 dgettext(TEXT_DOMAIN, "cannot get event"));
3547 error = zpool_standard_error_fmt(hdl, errno,
3548 dgettext(TEXT_DOMAIN, "cannot get event"));
3553 error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
3557 *dropped = (int)zc.zc_cookie;
3559 zcmd_free_nvlists(&zc);
3568 zpool_events_clear(libzfs_handle_t *hdl, int *count)
3570 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3573 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
3574 "cannot clear events"));
3576 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
3577 return (zpool_standard_error_fmt(hdl, errno, msg));
3580 *count = (int)zc.zc_cookie; /* # of events cleared */
3586 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3587 char *pathname, size_t len)
3589 zfs_cmd_t zc = { "\0", "\0", "\0", "\0", 0 };
3590 boolean_t mounted = B_FALSE;
3591 char *mntpnt = NULL;
3592 char dsname[MAXNAMELEN];
3595 /* special case for the MOS */
3596 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", (longlong_t)obj);
3600 /* get the dataset's name */
3601 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3603 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3604 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3605 /* just write out a path of two object numbers */
3606 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3607 (longlong_t)dsobj, (longlong_t)obj);
3610 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3612 /* find out if the dataset is mounted */
3613 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3615 /* get the corrupted object's path */
3616 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3618 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
3621 (void) snprintf(pathname, len, "%s%s", mntpnt,
3624 (void) snprintf(pathname, len, "%s:%s",
3625 dsname, zc.zc_value);
3628 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, (longlong_t)obj);
3634 * Read the EFI label from the config, if a label does not exist then
3635 * pass back the error to the caller. If the caller has passed a non-NULL
3636 * diskaddr argument then we set it to the starting address of the EFI
3640 read_efi_label(nvlist_t *config, diskaddr_t *sb)
3644 char diskname[MAXPATHLEN];
3647 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
3650 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
3651 strrchr(path, '/'));
3652 if ((fd = open(diskname, O_RDWR|O_DIRECT)) >= 0) {
3653 struct dk_gpt *vtoc;
3655 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
3657 *sb = vtoc->efi_parts[0].p_start;
3666 * determine where a partition starts on a disk in the current
3670 find_start_block(nvlist_t *config)
3674 diskaddr_t sb = MAXOFFSET_T;
3677 if (nvlist_lookup_nvlist_array(config,
3678 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
3679 if (nvlist_lookup_uint64(config,
3680 ZPOOL_CONFIG_WHOLE_DISK,
3681 &wholedisk) != 0 || !wholedisk) {
3682 return (MAXOFFSET_T);
3684 if (read_efi_label(config, &sb) < 0)
3689 for (c = 0; c < children; c++) {
3690 sb = find_start_block(child[c]);
3691 if (sb != MAXOFFSET_T) {
3695 return (MAXOFFSET_T);
3699 zpool_label_disk_wait(char *path, int timeout)
3701 struct stat64 statbuf;
3705 * Wait timeout miliseconds for a newly created device to be available
3706 * from the given path. There is a small window when a /dev/ device
3707 * will exist and the udev link will not, so we must wait for the
3708 * symlink. Depending on the udev rules this may take a few seconds.
3710 for (i = 0; i < timeout; i++) {
3714 if ((stat64(path, &statbuf) == 0) && (errno == 0))
3722 zpool_label_disk_check(char *path)
3724 struct dk_gpt *vtoc;
3727 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0)
3730 if ((err = efi_alloc_and_read(fd, &vtoc)) != 0) {
3735 if (vtoc->efi_flags & EFI_GPT_PRIMARY_CORRUPT) {
3747 * Label an individual disk. The name provided is the short name,
3748 * stripped of any leading /dev path.
3751 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
3753 char path[MAXPATHLEN];
3754 struct dk_gpt *vtoc;
3756 size_t resv = EFI_MIN_RESV_SIZE;
3757 uint64_t slice_size;
3758 diskaddr_t start_block;
3761 /* prepare an error message just in case */
3762 (void) snprintf(errbuf, sizeof (errbuf),
3763 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
3768 if (pool_is_bootable(zhp)) {
3769 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3770 "EFI labeled devices are not supported on root "
3772 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
3775 verify(nvlist_lookup_nvlist(zhp->zpool_config,
3776 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
3778 if (zhp->zpool_start_block == 0)
3779 start_block = find_start_block(nvroot);
3781 start_block = zhp->zpool_start_block;
3782 zhp->zpool_start_block = start_block;
3785 start_block = NEW_START_BLOCK;
3788 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
3791 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
3793 * This shouldn't happen. We've long since verified that this
3794 * is a valid device.
3796 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
3797 "label '%s': unable to open device: %d"), path, errno);
3798 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
3801 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
3803 * The only way this can fail is if we run out of memory, or we
3804 * were unable to read the disk's capacity
3806 if (errno == ENOMEM)
3807 (void) no_memory(hdl);
3810 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
3811 "label '%s': unable to read disk capacity"), path);
3813 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
3816 slice_size = vtoc->efi_last_u_lba + 1;
3817 slice_size -= EFI_MIN_RESV_SIZE;
3818 if (start_block == MAXOFFSET_T)
3819 start_block = NEW_START_BLOCK;
3820 slice_size -= start_block;
3821 slice_size = P2ALIGN(slice_size, PARTITION_END_ALIGNMENT);
3823 vtoc->efi_parts[0].p_start = start_block;
3824 vtoc->efi_parts[0].p_size = slice_size;
3827 * Why we use V_USR: V_BACKUP confuses users, and is considered
3828 * disposable by some EFI utilities (since EFI doesn't have a backup
3829 * slice). V_UNASSIGNED is supposed to be used only for zero size
3830 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
3831 * etc. were all pretty specific. V_USR is as close to reality as we
3832 * can get, in the absence of V_OTHER.
3834 vtoc->efi_parts[0].p_tag = V_USR;
3835 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
3837 vtoc->efi_parts[8].p_start = slice_size + start_block;
3838 vtoc->efi_parts[8].p_size = resv;
3839 vtoc->efi_parts[8].p_tag = V_RESERVED;
3841 if ((rval = efi_write(fd, vtoc)) != 0) {
3843 * Some block drivers (like pcata) may not support EFI
3844 * GPT labels. Print out a helpful error message dir-
3845 * ecting the user to manually label the disk and give
3851 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "try using "
3852 "parted(8) and then provide a specific slice: %d"), rval);
3853 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3859 /* Wait for the first expected slice to appear. */
3860 (void) snprintf(path, sizeof (path), "%s/%s%s%s", DISK_ROOT, name,
3861 isdigit(name[strlen(name)-1]) ? "p" : "", FIRST_SLICE);
3862 rval = zpool_label_disk_wait(path, 3000);
3864 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "failed to "
3865 "detect device partitions on '%s': %d"), path, rval);
3866 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3869 /* We can't be to paranoid. Read the label back and verify it. */
3870 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
3871 rval = zpool_label_disk_check(path);
3873 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "freshly written "
3874 "EFI label on '%s' is damaged. Ensure\nthis device "
3875 "is not in in use, and is functioning properly: %d"),
3877 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));