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 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
40 #include <sys/efi_partition.h>
42 #include <sys/zfs_ioctl.h>
46 #include "zfs_namecheck.h"
48 #include "libzfs_impl.h"
50 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
52 #if defined(__i386) || defined(__amd64)
53 #define BOOTCMD "installgrub(1M)"
55 #define BOOTCMD "installboot(1M)"
59 * ====================================================================
60 * zpool property functions
61 * ====================================================================
65 zpool_get_all_props(zpool_handle_t *zhp)
68 libzfs_handle_t *hdl = zhp->zpool_hdl;
70 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
72 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
75 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
76 if (errno == ENOMEM) {
77 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
78 zcmd_free_nvlists(&zc);
82 zcmd_free_nvlists(&zc);
87 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
88 zcmd_free_nvlists(&zc);
92 zcmd_free_nvlists(&zc);
98 zpool_props_refresh(zpool_handle_t *zhp)
102 old_props = zhp->zpool_props;
104 if (zpool_get_all_props(zhp) != 0)
107 nvlist_free(old_props);
112 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
118 zprop_source_t source;
120 nvl = zhp->zpool_props;
121 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
122 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
124 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
126 source = ZPROP_SRC_DEFAULT;
127 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
138 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
142 zprop_source_t source;
144 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
146 * zpool_get_all_props() has most likely failed because
147 * the pool is faulted, but if all we need is the top level
148 * vdev's guid then get it from the zhp config nvlist.
150 if ((prop == ZPOOL_PROP_GUID) &&
151 (nvlist_lookup_nvlist(zhp->zpool_config,
152 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
153 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
157 return (zpool_prop_default_numeric(prop));
160 nvl = zhp->zpool_props;
161 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
162 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
164 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
166 source = ZPROP_SRC_DEFAULT;
167 value = zpool_prop_default_numeric(prop);
177 * Map VDEV STATE to printed strings.
180 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
183 case VDEV_STATE_CLOSED:
184 case VDEV_STATE_OFFLINE:
185 return (gettext("OFFLINE"));
186 case VDEV_STATE_REMOVED:
187 return (gettext("REMOVED"));
188 case VDEV_STATE_CANT_OPEN:
189 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
190 return (gettext("FAULTED"));
192 return (gettext("UNAVAIL"));
193 case VDEV_STATE_FAULTED:
194 return (gettext("FAULTED"));
195 case VDEV_STATE_DEGRADED:
196 return (gettext("DEGRADED"));
197 case VDEV_STATE_HEALTHY:
198 return (gettext("ONLINE"));
201 return (gettext("UNKNOWN"));
205 * Get a zpool property value for 'prop' and return the value in
206 * a pre-allocated buffer.
209 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
210 zprop_source_t *srctype)
214 zprop_source_t src = ZPROP_SRC_NONE;
219 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
220 if (prop == ZPOOL_PROP_NAME)
221 (void) strlcpy(buf, zpool_get_name(zhp), len);
222 else if (prop == ZPOOL_PROP_HEALTH)
223 (void) strlcpy(buf, "FAULTED", len);
225 (void) strlcpy(buf, "-", len);
229 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
230 prop != ZPOOL_PROP_NAME)
233 switch (zpool_prop_get_type(prop)) {
234 case PROP_TYPE_STRING:
235 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
239 case PROP_TYPE_NUMBER:
240 intval = zpool_get_prop_int(zhp, prop, &src);
243 case ZPOOL_PROP_SIZE:
244 case ZPOOL_PROP_USED:
245 case ZPOOL_PROP_AVAILABLE:
246 (void) zfs_nicenum(intval, buf, len);
249 case ZPOOL_PROP_CAPACITY:
250 (void) snprintf(buf, len, "%llu%%",
251 (u_longlong_t)intval);
254 case ZPOOL_PROP_HEALTH:
255 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
256 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
257 verify(nvlist_lookup_uint64_array(nvroot,
258 ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
260 (void) strlcpy(buf, zpool_state_to_name(intval,
264 (void) snprintf(buf, len, "%llu", intval);
268 case PROP_TYPE_INDEX:
269 intval = zpool_get_prop_int(zhp, prop, &src);
270 if (zpool_prop_index_to_string(prop, intval, &strval)
273 (void) strlcpy(buf, strval, len);
287 * Check if the bootfs name has the same pool name as it is set to.
288 * Assuming bootfs is a valid dataset name.
291 bootfs_name_valid(const char *pool, char *bootfs)
293 int len = strlen(pool);
295 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
298 if (strncmp(pool, bootfs, len) == 0 &&
299 (bootfs[len] == '/' || bootfs[len] == '\0'))
306 * Inspect the configuration to determine if any of the devices contain
310 pool_uses_efi(nvlist_t *config)
315 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
316 &child, &children) != 0)
317 return (read_efi_label(config, NULL) >= 0);
319 for (c = 0; c < children; c++) {
320 if (pool_uses_efi(child[c]))
327 pool_is_bootable(zpool_handle_t *zhp)
329 char bootfs[ZPOOL_MAXNAMELEN];
331 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
332 sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
333 sizeof (bootfs)) != 0);
338 * Given an nvlist of zpool properties to be set, validate that they are
339 * correct, and parse any numeric properties (index, boolean, etc) if they are
340 * specified as strings.
343 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
344 nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf)
352 struct stat64 statbuf;
356 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
357 (void) no_memory(hdl);
362 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
363 const char *propname = nvpair_name(elem);
366 * Make sure this property is valid and applies to this type.
368 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
369 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
370 "invalid property '%s'"), propname);
371 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
375 if (zpool_prop_readonly(prop)) {
376 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
377 "is readonly"), propname);
378 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
382 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
383 &strval, &intval, errbuf) != 0)
387 * Perform additional checking for specific properties.
390 case ZPOOL_PROP_VERSION:
391 if (intval < version || intval > SPA_VERSION) {
392 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
393 "property '%s' number %d is invalid."),
395 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
400 case ZPOOL_PROP_BOOTFS:
401 if (create_or_import) {
402 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
403 "property '%s' cannot be set at creation "
404 "or import time"), propname);
405 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
409 if (version < SPA_VERSION_BOOTFS) {
410 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
411 "pool must be upgraded to support "
412 "'%s' property"), propname);
413 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
418 * bootfs property value has to be a dataset name and
419 * the dataset has to be in the same pool as it sets to.
421 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
423 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
424 "is an invalid name"), strval);
425 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
429 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
430 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
431 "could not open pool '%s'"), poolname);
432 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
435 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
436 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
439 * bootfs property cannot be set on a disk which has
442 if (pool_uses_efi(nvroot)) {
443 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
444 "property '%s' not supported on "
445 "EFI labeled devices"), propname);
446 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
453 case ZPOOL_PROP_ALTROOT:
454 if (!create_or_import) {
455 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
456 "property '%s' can only be set during pool "
457 "creation or import"), propname);
458 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
462 if (strval[0] != '/') {
463 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
464 "bad alternate root '%s'"), strval);
465 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
470 case ZPOOL_PROP_CACHEFILE:
471 if (strval[0] == '\0')
474 if (strcmp(strval, "none") == 0)
477 if (strval[0] != '/') {
478 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
479 "property '%s' must be empty, an "
480 "absolute path, or 'none'"), propname);
481 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
485 slash = strrchr(strval, '/');
487 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
488 strcmp(slash, "/..") == 0) {
489 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
490 "'%s' is not a valid file"), strval);
491 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
497 if (strval[0] != '\0' &&
498 (stat64(strval, &statbuf) != 0 ||
499 !S_ISDIR(statbuf.st_mode))) {
500 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
501 "'%s' is not a valid directory"),
503 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
514 nvlist_free(retprops);
519 * Set zpool property : propname=propval.
522 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
524 zfs_cmd_t zc = { 0 };
527 nvlist_t *nvl = NULL;
531 (void) snprintf(errbuf, sizeof (errbuf),
532 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
535 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp))
536 return (zfs_error(zhp->zpool_hdl, EZFS_POOLPROPS, errbuf));
538 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
539 return (no_memory(zhp->zpool_hdl));
541 if (nvlist_add_string(nvl, propname, propval) != 0) {
543 return (no_memory(zhp->zpool_hdl));
546 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
547 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
548 zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) {
557 * Execute the corresponding ioctl() to set this property.
559 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
561 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
566 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
568 zcmd_free_nvlists(&zc);
572 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
574 (void) zpool_props_refresh(zhp);
580 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
582 libzfs_handle_t *hdl = zhp->zpool_hdl;
584 char buf[ZFS_MAXPROPLEN];
586 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
589 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
594 if (entry->pl_prop != ZPROP_INVAL &&
595 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
597 if (strlen(buf) > entry->pl_width)
598 entry->pl_width = strlen(buf);
607 * Validate the given pool name, optionally putting an extended error message in
611 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
617 ret = pool_namecheck(pool, &why, &what);
620 * The rules for reserved pool names were extended at a later point.
621 * But we need to support users with existing pools that may now be
622 * invalid. So we only check for this expanded set of names during a
623 * create (or import), and only in userland.
625 if (ret == 0 && !isopen &&
626 (strncmp(pool, "mirror", 6) == 0 ||
627 strncmp(pool, "raidz", 5) == 0 ||
628 strncmp(pool, "spare", 5) == 0 ||
629 strcmp(pool, "log") == 0)) {
632 dgettext(TEXT_DOMAIN, "name is reserved"));
640 case NAME_ERR_TOOLONG:
642 dgettext(TEXT_DOMAIN, "name is too long"));
645 case NAME_ERR_INVALCHAR:
647 dgettext(TEXT_DOMAIN, "invalid character "
648 "'%c' in pool name"), what);
651 case NAME_ERR_NOLETTER:
652 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
653 "name must begin with a letter"));
656 case NAME_ERR_RESERVED:
657 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
658 "name is reserved"));
661 case NAME_ERR_DISKLIKE:
662 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
663 "pool name is reserved"));
666 case NAME_ERR_LEADING_SLASH:
667 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
668 "leading slash in name"));
671 case NAME_ERR_EMPTY_COMPONENT:
672 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
673 "empty component in name"));
676 case NAME_ERR_TRAILING_SLASH:
677 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
678 "trailing slash in name"));
681 case NAME_ERR_MULTIPLE_AT:
682 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
683 "multiple '@' delimiters in name"));
695 * Open a handle to the given pool, even if the pool is currently in the FAULTED
699 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
705 * Make sure the pool name is valid.
707 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
708 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
709 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
714 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
717 zhp->zpool_hdl = hdl;
718 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
720 if (zpool_refresh_stats(zhp, &missing) != 0) {
726 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
727 (void) zfs_error_fmt(hdl, EZFS_NOENT,
728 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
737 * Like the above, but silent on error. Used when iterating over pools (because
738 * the configuration cache may be out of date).
741 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
746 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
749 zhp->zpool_hdl = hdl;
750 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
752 if (zpool_refresh_stats(zhp, &missing) != 0) {
768 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
772 zpool_open(libzfs_handle_t *hdl, const char *pool)
776 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
779 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
780 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
781 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
790 * Close the handle. Simply frees the memory associated with the handle.
793 zpool_close(zpool_handle_t *zhp)
795 if (zhp->zpool_config)
796 nvlist_free(zhp->zpool_config);
797 if (zhp->zpool_old_config)
798 nvlist_free(zhp->zpool_old_config);
799 if (zhp->zpool_props)
800 nvlist_free(zhp->zpool_props);
805 * Return the name of the pool.
808 zpool_get_name(zpool_handle_t *zhp)
810 return (zhp->zpool_name);
815 * Return the state of the pool (ACTIVE or UNAVAILABLE)
818 zpool_get_state(zpool_handle_t *zhp)
820 return (zhp->zpool_state);
824 * Create the named pool, using the provided vdev list. It is assumed
825 * that the consumer has already validated the contents of the nvlist, so we
826 * don't have to worry about error semantics.
829 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
830 nvlist_t *props, nvlist_t *fsprops)
832 zfs_cmd_t zc = { 0 };
833 nvlist_t *zc_fsprops = NULL;
834 nvlist_t *zc_props = NULL;
839 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
840 "cannot create '%s'"), pool);
842 if (!zpool_name_valid(hdl, B_FALSE, pool))
843 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
845 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
849 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
850 SPA_VERSION_1, B_TRUE, msg)) == NULL) {
859 zoned = ((nvlist_lookup_string(fsprops,
860 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
861 strcmp(zonestr, "on") == 0);
863 if ((zc_fsprops = zfs_valid_proplist(hdl,
864 ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
868 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
871 if (nvlist_add_nvlist(zc_props,
872 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
877 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
880 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
882 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
884 zcmd_free_nvlists(&zc);
885 nvlist_free(zc_props);
886 nvlist_free(zc_fsprops);
891 * This can happen if the user has specified the same
892 * device multiple times. We can't reliably detect this
893 * until we try to add it and see we already have a
896 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
897 "one or more vdevs refer to the same device"));
898 return (zfs_error(hdl, EZFS_BADDEV, msg));
902 * This occurs when one of the devices is below
903 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
904 * device was the problem device since there's no
905 * reliable way to determine device size from userland.
910 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
912 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
913 "one or more devices is less than the "
914 "minimum size (%s)"), buf);
916 return (zfs_error(hdl, EZFS_BADDEV, msg));
919 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
920 "one or more devices is out of space"));
921 return (zfs_error(hdl, EZFS_BADDEV, msg));
924 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
925 "cache device must be a disk or disk slice"));
926 return (zfs_error(hdl, EZFS_BADDEV, msg));
929 return (zpool_standard_error(hdl, errno, msg));
934 * If this is an alternate root pool, then we automatically set the
935 * mountpoint of the root dataset to be '/'.
937 if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
941 verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
942 verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
949 zcmd_free_nvlists(&zc);
950 nvlist_free(zc_props);
951 nvlist_free(zc_fsprops);
956 * Destroy the given pool. It is up to the caller to ensure that there are no
957 * datasets left in the pool.
960 zpool_destroy(zpool_handle_t *zhp)
962 zfs_cmd_t zc = { 0 };
963 zfs_handle_t *zfp = NULL;
964 libzfs_handle_t *hdl = zhp->zpool_hdl;
967 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
968 (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
969 ZFS_TYPE_FILESYSTEM)) == NULL)
972 if (zpool_remove_zvol_links(zhp) != 0)
975 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
977 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
978 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
979 "cannot destroy '%s'"), zhp->zpool_name);
981 if (errno == EROFS) {
982 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
983 "one or more devices is read only"));
984 (void) zfs_error(hdl, EZFS_BADDEV, msg);
986 (void) zpool_standard_error(hdl, errno, msg);
995 remove_mountpoint(zfp);
1003 * Add the given vdevs to the pool. The caller must have already performed the
1004 * necessary verification to ensure that the vdev specification is well-formed.
1007 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1009 zfs_cmd_t zc = { 0 };
1011 libzfs_handle_t *hdl = zhp->zpool_hdl;
1013 nvlist_t **spares, **l2cache;
1014 uint_t nspares, nl2cache;
1016 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1017 "cannot add to '%s'"), zhp->zpool_name);
1019 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1020 SPA_VERSION_SPARES &&
1021 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1022 &spares, &nspares) == 0) {
1023 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1024 "upgraded to add hot spares"));
1025 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1028 if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1029 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1032 for (s = 0; s < nspares; s++) {
1035 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1036 &path) == 0 && pool_uses_efi(spares[s])) {
1037 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1038 "device '%s' contains an EFI label and "
1039 "cannot be used on root pools."),
1040 zpool_vdev_name(hdl, NULL, spares[s]));
1041 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1046 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1047 SPA_VERSION_L2CACHE &&
1048 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1049 &l2cache, &nl2cache) == 0) {
1050 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1051 "upgraded to add cache devices"));
1052 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1055 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1057 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1059 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1063 * This can happen if the user has specified the same
1064 * device multiple times. We can't reliably detect this
1065 * until we try to add it and see we already have a
1068 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1069 "one or more vdevs refer to the same device"));
1070 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1075 * This occurrs when one of the devices is below
1076 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1077 * device was the problem device since there's no
1078 * reliable way to determine device size from userland.
1083 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1085 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1086 "device is less than the minimum "
1089 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1093 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1094 "pool must be upgraded to add these vdevs"));
1095 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1099 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1100 "root pool can not have multiple vdevs"
1101 " or separate logs"));
1102 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1106 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1107 "cache device must be a disk or disk slice"));
1108 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1112 (void) zpool_standard_error(hdl, errno, msg);
1120 zcmd_free_nvlists(&zc);
1126 * Exports the pool from the system. The caller must ensure that there are no
1127 * mounted datasets in the pool.
1130 zpool_export(zpool_handle_t *zhp, boolean_t force)
1132 zfs_cmd_t zc = { 0 };
1135 if (zpool_remove_zvol_links(zhp) != 0)
1138 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1139 "cannot export '%s'"), zhp->zpool_name);
1141 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1142 zc.zc_cookie = force;
1144 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1147 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1148 "use '-f' to override the following errors:\n"
1149 "'%s' has an active shared spare which could be"
1150 " used by other pools once '%s' is exported."),
1151 zhp->zpool_name, zhp->zpool_name);
1152 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1155 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1164 * zpool_import() is a contracted interface. Should be kept the same
1167 * Applications should use zpool_import_props() to import a pool with
1168 * new properties value to be set.
1171 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1174 nvlist_t *props = NULL;
1177 if (altroot != NULL) {
1178 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1179 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1180 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1184 if (nvlist_add_string(props,
1185 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0) {
1187 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1188 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1193 ret = zpool_import_props(hdl, config, newname, props, B_FALSE);
1200 * Import the given pool using the known configuration and a list of
1201 * properties to be set. The configuration should have come from
1202 * zpool_find_import(). The 'newname' parameters control whether the pool
1203 * is imported with a different name.
1206 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1207 nvlist_t *props, boolean_t importfaulted)
1209 zfs_cmd_t zc = { 0 };
1215 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1218 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1219 "cannot import pool '%s'"), origname);
1221 if (newname != NULL) {
1222 if (!zpool_name_valid(hdl, B_FALSE, newname))
1223 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1224 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1226 thename = (char *)newname;
1234 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1237 if ((props = zpool_valid_proplist(hdl, origname,
1238 props, version, B_TRUE, errbuf)) == NULL) {
1240 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1246 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1248 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1251 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1256 zc.zc_cookie = (uint64_t)importfaulted;
1258 if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) {
1260 if (newname == NULL)
1261 (void) snprintf(desc, sizeof (desc),
1262 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1265 (void) snprintf(desc, sizeof (desc),
1266 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1272 * Unsupported version.
1274 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1278 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1282 (void) zpool_standard_error(hdl, errno, desc);
1287 zpool_handle_t *zhp;
1290 * This should never fail, but play it safe anyway.
1292 if (zpool_open_silent(hdl, thename, &zhp) != 0) {
1294 } else if (zhp != NULL) {
1295 ret = zpool_create_zvol_links(zhp);
1301 zcmd_free_nvlists(&zc);
1311 zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type)
1313 zfs_cmd_t zc = { 0 };
1315 libzfs_handle_t *hdl = zhp->zpool_hdl;
1317 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1318 zc.zc_cookie = type;
1320 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0)
1323 (void) snprintf(msg, sizeof (msg),
1324 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1327 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1329 return (zpool_standard_error(hdl, errno, msg));
1333 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1334 * spare; but FALSE if its an INUSE spare.
1337 vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid,
1338 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
1342 uint64_t theguid, present;
1344 uint64_t wholedisk = 0;
1348 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0);
1350 if (search == NULL &&
1351 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) {
1353 * If the device has never been present since import, the only
1354 * reliable way to match the vdev is by GUID.
1356 if (theguid == guid)
1358 } else if (search != NULL &&
1359 nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1360 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1364 * For whole disks, the internal path has 's0', but the
1365 * path passed in by the user doesn't.
1367 if (strlen(search) == strlen(path) - 2 &&
1368 strncmp(search, path, strlen(search)) == 0)
1370 } else if (strcmp(search, path) == 0) {
1375 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1376 &child, &children) != 0)
1379 for (c = 0; c < children; c++) {
1380 if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1381 avail_spare, l2cache, NULL)) != NULL) {
1383 * The 'is_log' value is only set for the toplevel
1384 * vdev, not the leaf vdevs. So we always lookup the
1385 * log device from the root of the vdev tree (where
1386 * 'log' is non-NULL).
1389 nvlist_lookup_uint64(child[c],
1390 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
1398 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1399 &child, &children) == 0) {
1400 for (c = 0; c < children; c++) {
1401 if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1402 avail_spare, l2cache, NULL)) != NULL) {
1403 *avail_spare = B_TRUE;
1409 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1410 &child, &children) == 0) {
1411 for (c = 0; c < children; c++) {
1412 if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1413 avail_spare, l2cache, NULL)) != NULL) {
1424 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
1425 boolean_t *l2cache, boolean_t *log)
1427 char buf[MAXPATHLEN];
1433 guid = strtoull(path, &end, 10);
1434 if (guid != 0 && *end == '\0') {
1436 } else if (path[0] != '/') {
1437 (void) snprintf(buf, sizeof (buf), "%s%s", "/dev/dsk/", path);
1443 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1446 *avail_spare = B_FALSE;
1450 return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare,
1455 vdev_online(nvlist_t *nv)
1459 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
1460 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
1461 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
1468 * Get phys_path for a root pool
1469 * Return 0 on success; non-zeron on failure.
1472 zpool_get_physpath(zpool_handle_t *zhp, char *physpath)
1474 nvlist_t *vdev_root;
1480 * Make sure this is a root pool, as phys_path doesn't mean
1481 * anything to a non-root pool.
1483 if (!pool_is_bootable(zhp))
1486 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1487 ZPOOL_CONFIG_VDEV_TREE, &vdev_root) == 0);
1489 if (nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
1490 &child, &count) != 0)
1493 for (i = 0; i < count; i++) {
1500 if (nvlist_lookup_string(child[i], ZPOOL_CONFIG_TYPE, &type)
1504 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
1505 if (!vdev_online(child[i]))
1507 verify(nvlist_lookup_string(child[i],
1508 ZPOOL_CONFIG_PHYS_PATH, &tmppath) == 0);
1509 (void) strncpy(physpath, tmppath, strlen(tmppath));
1510 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0) {
1511 if (nvlist_lookup_nvlist_array(child[i],
1512 ZPOOL_CONFIG_CHILDREN, &child2, &count2) != 0)
1515 for (j = 0; j < count2; j++) {
1516 if (!vdev_online(child2[j]))
1518 if (nvlist_lookup_string(child2[j],
1519 ZPOOL_CONFIG_PHYS_PATH, &tmppath) != 0)
1522 if ((strlen(physpath) + strlen(tmppath)) >
1526 if (strlen(physpath) == 0) {
1527 (void) strncpy(physpath, tmppath,
1530 (void) strcat(physpath, " ");
1531 (void) strcat(physpath, tmppath);
1543 * Returns TRUE if the given guid corresponds to the given type.
1544 * This is used to check for hot spares (INUSE or not), and level 2 cache
1548 is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type)
1550 uint64_t target_guid;
1556 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1558 if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) {
1559 for (i = 0; i < count; i++) {
1560 verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID,
1561 &target_guid) == 0);
1562 if (guid == target_guid)
1571 * Bring the specified vdev online. The 'flags' parameter is a set of the
1572 * ZFS_ONLINE_* flags.
1575 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
1576 vdev_state_t *newstate)
1578 zfs_cmd_t zc = { 0 };
1581 boolean_t avail_spare, l2cache;
1582 libzfs_handle_t *hdl = zhp->zpool_hdl;
1584 (void) snprintf(msg, sizeof (msg),
1585 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
1587 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1588 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1590 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1592 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1595 is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
1596 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1598 zc.zc_cookie = VDEV_STATE_ONLINE;
1601 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0)
1602 return (zpool_standard_error(hdl, errno, msg));
1604 *newstate = zc.zc_cookie;
1609 * Take the specified vdev offline
1612 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
1614 zfs_cmd_t zc = { 0 };
1617 boolean_t avail_spare, l2cache;
1618 libzfs_handle_t *hdl = zhp->zpool_hdl;
1620 (void) snprintf(msg, sizeof (msg),
1621 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
1623 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1624 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1626 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1628 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1631 is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
1632 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1634 zc.zc_cookie = VDEV_STATE_OFFLINE;
1635 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
1637 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1644 * There are no other replicas of this device.
1646 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
1649 return (zpool_standard_error(hdl, errno, msg));
1654 * Mark the given vdev faulted.
1657 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid)
1659 zfs_cmd_t zc = { 0 };
1661 libzfs_handle_t *hdl = zhp->zpool_hdl;
1663 (void) snprintf(msg, sizeof (msg),
1664 dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
1666 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1668 zc.zc_cookie = VDEV_STATE_FAULTED;
1670 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1677 * There are no other replicas of this device.
1679 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
1682 return (zpool_standard_error(hdl, errno, msg));
1688 * Mark the given vdev degraded.
1691 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid)
1693 zfs_cmd_t zc = { 0 };
1695 libzfs_handle_t *hdl = zhp->zpool_hdl;
1697 (void) snprintf(msg, sizeof (msg),
1698 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
1700 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1702 zc.zc_cookie = VDEV_STATE_DEGRADED;
1704 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1707 return (zpool_standard_error(hdl, errno, msg));
1711 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
1715 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
1721 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
1723 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
1726 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
1727 children == 2 && child[which] == tgt)
1730 for (c = 0; c < children; c++)
1731 if (is_replacing_spare(child[c], tgt, which))
1739 * Attach new_disk (fully described by nvroot) to old_disk.
1740 * If 'replacing' is specified, the new disk will replace the old one.
1743 zpool_vdev_attach(zpool_handle_t *zhp,
1744 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
1746 zfs_cmd_t zc = { 0 };
1750 boolean_t avail_spare, l2cache, islog;
1752 char *path, *newname;
1755 nvlist_t *config_root;
1756 libzfs_handle_t *hdl = zhp->zpool_hdl;
1757 boolean_t rootpool = pool_is_bootable(zhp);
1760 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1761 "cannot replace %s with %s"), old_disk, new_disk);
1763 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1764 "cannot attach %s to %s"), new_disk, old_disk);
1767 * If this is a root pool, make sure that we're not attaching an
1768 * EFI labeled device.
1770 if (rootpool && pool_uses_efi(nvroot)) {
1771 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1772 "EFI labeled devices are not supported on root pools."));
1773 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1776 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1777 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
1779 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1782 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1785 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
1787 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1788 zc.zc_cookie = replacing;
1790 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1791 &child, &children) != 0 || children != 1) {
1792 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1793 "new device must be a single disk"));
1794 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
1797 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
1798 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
1800 if ((newname = zpool_vdev_name(NULL, NULL, child[0])) == NULL)
1804 * If the target is a hot spare that has been swapped in, we can only
1805 * replace it with another hot spare.
1808 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
1809 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
1810 NULL) == NULL || !avail_spare) &&
1811 is_replacing_spare(config_root, tgt, 1)) {
1812 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1813 "can only be replaced by another hot spare"));
1815 return (zfs_error(hdl, EZFS_BADTARGET, msg));
1819 * If we are attempting to replace a spare, it canot be applied to an
1820 * already spared device.
1823 nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
1824 zpool_find_vdev(zhp, newname, &avail_spare,
1825 &l2cache, NULL) != NULL && avail_spare &&
1826 is_replacing_spare(config_root, tgt, 0)) {
1827 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1828 "device has already been replaced with a spare"));
1830 return (zfs_error(hdl, EZFS_BADTARGET, msg));
1835 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1838 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc);
1840 zcmd_free_nvlists(&zc);
1845 * XXX - This should be removed once we can
1846 * automatically install the bootblocks on the
1847 * newly attached disk.
1849 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Please "
1850 "be sure to invoke %s to make '%s' bootable.\n"),
1859 * Can't attach to or replace this type of vdev.
1863 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1864 "cannot replace a log with a spare"));
1866 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1867 "cannot replace a replacing device"));
1869 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1870 "can only attach to mirrors and top-level "
1873 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
1878 * The new device must be a single disk.
1880 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1881 "new device must be a single disk"));
1882 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
1886 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
1888 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1893 * The new device is too small.
1895 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1896 "device is too small"));
1897 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1902 * The new device has a different alignment requirement.
1904 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1905 "devices have different sector alignment"));
1906 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1911 * The resulting top-level vdev spec won't fit in the label.
1913 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
1917 (void) zpool_standard_error(hdl, errno, msg);
1924 * Detach the specified device.
1927 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
1929 zfs_cmd_t zc = { 0 };
1932 boolean_t avail_spare, l2cache;
1933 libzfs_handle_t *hdl = zhp->zpool_hdl;
1935 (void) snprintf(msg, sizeof (msg),
1936 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
1938 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1939 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1941 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1944 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1947 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
1949 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1951 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
1958 * Can't detach from this type of vdev.
1960 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
1961 "applicable to mirror and replacing vdevs"));
1962 (void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg);
1967 * There are no other replicas of this device.
1969 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
1973 (void) zpool_standard_error(hdl, errno, msg);
1980 * Remove the given device. Currently, this is supported only for hot spares
1981 * and level 2 cache devices.
1984 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
1986 zfs_cmd_t zc = { 0 };
1989 boolean_t avail_spare, l2cache;
1990 libzfs_handle_t *hdl = zhp->zpool_hdl;
1992 (void) snprintf(msg, sizeof (msg),
1993 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
1995 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1996 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1998 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2000 if (!avail_spare && !l2cache) {
2001 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2002 "only inactive hot spares or cache devices "
2004 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2007 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2009 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
2012 return (zpool_standard_error(hdl, errno, msg));
2016 * Clear the errors for the pool, or the particular device if specified.
2019 zpool_clear(zpool_handle_t *zhp, const char *path)
2021 zfs_cmd_t zc = { 0 };
2024 boolean_t avail_spare, l2cache;
2025 libzfs_handle_t *hdl = zhp->zpool_hdl;
2028 (void) snprintf(msg, sizeof (msg),
2029 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2032 (void) snprintf(msg, sizeof (msg),
2033 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2036 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2038 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
2039 &l2cache, NULL)) == 0)
2040 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2043 * Don't allow error clearing for hot spares. Do allow
2044 * error clearing for l2cache devices.
2047 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2049 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
2053 if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
2056 return (zpool_standard_error(hdl, errno, msg));
2060 * Similar to zpool_clear(), but takes a GUID (used by fmd).
2063 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
2065 zfs_cmd_t zc = { 0 };
2067 libzfs_handle_t *hdl = zhp->zpool_hdl;
2069 (void) snprintf(msg, sizeof (msg),
2070 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
2073 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2076 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
2079 return (zpool_standard_error(hdl, errno, msg));
2083 * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool>
2087 zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *),
2090 libzfs_handle_t *hdl = zhp->zpool_hdl;
2091 char (*paths)[MAXPATHLEN];
2093 int curr, fd, base, ret = 0;
2098 if ((base = open("/dev/zvol/dsk", O_RDONLY)) < 0)
2099 return (errno == ENOENT ? 0 : -1);
2101 if (fstatat(base, zhp->zpool_name, &st, 0) != 0) {
2104 return (err == ENOENT ? 0 : -1);
2108 * Oddly this wasn't a directory -- ignore that failure since we
2109 * know there are no links lower in the (non-existant) hierarchy.
2111 if (!S_ISDIR(st.st_mode)) {
2116 if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) {
2121 (void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0]));
2125 if (fstatat(base, paths[curr], &st, AT_SYMLINK_NOFOLLOW) != 0)
2128 if (S_ISDIR(st.st_mode)) {
2129 if ((fd = openat(base, paths[curr], O_RDONLY)) < 0)
2132 if ((dirp = fdopendir(fd)) == NULL) {
2137 while ((dp = readdir(dirp)) != NULL) {
2138 if (dp->d_name[0] == '.')
2141 if (curr + 1 == size) {
2142 paths = zfs_realloc(hdl, paths,
2143 size * sizeof (paths[0]),
2144 size * 2 * sizeof (paths[0]));
2145 if (paths == NULL) {
2146 (void) closedir(dirp);
2154 (void) strlcpy(paths[curr + 1], paths[curr],
2155 sizeof (paths[curr + 1]));
2156 (void) strlcat(paths[curr], "/",
2157 sizeof (paths[curr]));
2158 (void) strlcat(paths[curr], dp->d_name,
2159 sizeof (paths[curr]));
2163 (void) closedir(dirp);
2166 if ((ret = cb(paths[curr], data)) != 0)
2184 typedef struct zvol_cb {
2185 zpool_handle_t *zcb_pool;
2186 boolean_t zcb_create;
2191 do_zvol_create(zfs_handle_t *zhp, void *data)
2195 if (ZFS_IS_VOLUME(zhp)) {
2196 (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
2197 ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL);
2201 ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL);
2209 * Iterate over all zvols in the pool and make any necessary minor nodes.
2212 zpool_create_zvol_links(zpool_handle_t *zhp)
2218 * If the pool is unavailable, just return success.
2220 if ((zfp = make_dataset_handle(zhp->zpool_hdl,
2221 zhp->zpool_name)) == NULL)
2224 ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL);
2231 do_zvol_remove(const char *dataset, void *data)
2233 zpool_handle_t *zhp = data;
2235 return (zvol_remove_link(zhp->zpool_hdl, dataset));
2239 * Iterate over all zvols in the pool and remove any minor nodes. We iterate
2240 * by examining the /dev links so that a corrupted pool doesn't impede this
2244 zpool_remove_zvol_links(zpool_handle_t *zhp)
2246 return (zpool_iter_zvol(zhp, do_zvol_remove, zhp));
2250 * Convert from a devid string to a path.
2253 devid_to_path(char *devid_str)
2258 devid_nmlist_t *list = NULL;
2261 if (devid_str_decode(devid_str, &devid, &minor) != 0)
2264 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
2266 devid_str_free(minor);
2272 if ((path = strdup(list[0].devname)) == NULL)
2275 devid_free_nmlist(list);
2281 * Convert from a path to a devid string.
2284 path_to_devid(const char *path)
2290 if ((fd = open(path, O_RDONLY)) < 0)
2295 if (devid_get(fd, &devid) == 0) {
2296 if (devid_get_minor_name(fd, &minor) == 0)
2297 ret = devid_str_encode(devid, minor);
2299 devid_str_free(minor);
2308 * Issue the necessary ioctl() to update the stored path value for the vdev. We
2309 * ignore any failure here, since a common case is for an unprivileged user to
2310 * type 'zpool status', and we'll display the correct information anyway.
2313 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
2315 zfs_cmd_t zc = { 0 };
2317 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2318 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
2319 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2322 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
2326 * Given a vdev, return the name to display in iostat. If the vdev has a path,
2327 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
2328 * We also check if this is a whole disk, in which case we strip off the
2329 * trailing 's0' slice name.
2331 * This routine is also responsible for identifying when disks have been
2332 * reconfigured in a new location. The kernel will have opened the device by
2333 * devid, but the path will still refer to the old location. To catch this, we
2334 * first do a path -> devid translation (which is fast for the common case). If
2335 * the devid matches, we're done. If not, we do a reverse devid -> path
2336 * translation and issue the appropriate ioctl() to update the path of the vdev.
2337 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
2341 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv)
2349 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
2351 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2353 (void) snprintf(buf, sizeof (buf), "%llu",
2354 (u_longlong_t)value);
2356 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
2359 * If the device is dead (faulted, offline, etc) then don't
2360 * bother opening it. Otherwise we may be forcing the user to
2361 * open a misbehaving device, which can have undesirable
2364 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
2365 (uint64_t **)&vs, &vsc) != 0 ||
2366 vs->vs_state >= VDEV_STATE_DEGRADED) &&
2368 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
2370 * Determine if the current path is correct.
2372 char *newdevid = path_to_devid(path);
2374 if (newdevid == NULL ||
2375 strcmp(devid, newdevid) != 0) {
2378 if ((newpath = devid_to_path(devid)) != NULL) {
2380 * Update the path appropriately.
2382 set_path(zhp, nv, newpath);
2383 if (nvlist_add_string(nv,
2384 ZPOOL_CONFIG_PATH, newpath) == 0)
2385 verify(nvlist_lookup_string(nv,
2393 devid_str_free(newdevid);
2396 if (strncmp(path, "/dev/dsk/", 9) == 0)
2399 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2400 &value) == 0 && value) {
2401 char *tmp = zfs_strdup(hdl, path);
2404 tmp[strlen(path) - 2] = '\0';
2408 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
2411 * If it's a raidz device, we need to stick in the parity level.
2413 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
2414 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
2416 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
2417 (u_longlong_t)value);
2422 return (zfs_strdup(hdl, path));
2426 zbookmark_compare(const void *a, const void *b)
2428 return (memcmp(a, b, sizeof (zbookmark_t)));
2432 * Retrieve the persistent error log, uniquify the members, and return to the
2436 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
2438 zfs_cmd_t zc = { 0 };
2440 zbookmark_t *zb = NULL;
2444 * Retrieve the raw error list from the kernel. If the number of errors
2445 * has increased, allocate more space and continue until we get the
2448 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
2452 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
2453 count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
2455 zc.zc_nvlist_dst_size = count;
2456 (void) strcpy(zc.zc_name, zhp->zpool_name);
2458 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
2460 free((void *)(uintptr_t)zc.zc_nvlist_dst);
2461 if (errno == ENOMEM) {
2462 count = zc.zc_nvlist_dst_size;
2463 if ((zc.zc_nvlist_dst = (uintptr_t)
2464 zfs_alloc(zhp->zpool_hdl, count *
2465 sizeof (zbookmark_t))) == (uintptr_t)NULL)
2476 * Sort the resulting bookmarks. This is a little confusing due to the
2477 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
2478 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
2479 * _not_ copied as part of the process. So we point the start of our
2480 * array appropriate and decrement the total number of elements.
2482 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
2483 zc.zc_nvlist_dst_size;
2484 count -= zc.zc_nvlist_dst_size;
2486 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
2488 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
2491 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
2493 for (i = 0; i < count; i++) {
2496 /* ignoring zb_blkid and zb_level for now */
2497 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
2498 zb[i-1].zb_object == zb[i].zb_object)
2501 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
2503 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
2504 zb[i].zb_objset) != 0) {
2508 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
2509 zb[i].zb_object) != 0) {
2513 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
2520 free((void *)(uintptr_t)zc.zc_nvlist_dst);
2524 free((void *)(uintptr_t)zc.zc_nvlist_dst);
2525 return (no_memory(zhp->zpool_hdl));
2529 * Upgrade a ZFS pool to the latest on-disk version.
2532 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
2534 zfs_cmd_t zc = { 0 };
2535 libzfs_handle_t *hdl = zhp->zpool_hdl;
2537 (void) strcpy(zc.zc_name, zhp->zpool_name);
2538 zc.zc_cookie = new_version;
2540 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
2541 return (zpool_standard_error_fmt(hdl, errno,
2542 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
2548 zpool_set_history_str(const char *subcommand, int argc, char **argv,
2553 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
2554 for (i = 1; i < argc; i++) {
2555 if (strlen(history_str) + 1 + strlen(argv[i]) >
2558 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
2559 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
2564 * Stage command history for logging.
2567 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
2569 if (history_str == NULL)
2572 if (strlen(history_str) > HIS_MAX_RECORD_LEN)
2575 if (hdl->libzfs_log_str != NULL)
2576 free(hdl->libzfs_log_str);
2578 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
2579 return (no_memory(hdl));
2585 * Perform ioctl to get some command history of a pool.
2587 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
2588 * logical offset of the history buffer to start reading from.
2590 * Upon return, 'off' is the next logical offset to read from and
2591 * 'len' is the actual amount of bytes read into 'buf'.
2594 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
2596 zfs_cmd_t zc = { 0 };
2597 libzfs_handle_t *hdl = zhp->zpool_hdl;
2599 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2601 zc.zc_history = (uint64_t)(uintptr_t)buf;
2602 zc.zc_history_len = *len;
2603 zc.zc_history_offset = *off;
2605 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
2608 return (zfs_error_fmt(hdl, EZFS_PERM,
2609 dgettext(TEXT_DOMAIN,
2610 "cannot show history for pool '%s'"),
2613 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
2614 dgettext(TEXT_DOMAIN, "cannot get history for pool "
2615 "'%s'"), zhp->zpool_name));
2617 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
2618 dgettext(TEXT_DOMAIN, "cannot get history for pool "
2619 "'%s', pool must be upgraded"), zhp->zpool_name));
2621 return (zpool_standard_error_fmt(hdl, errno,
2622 dgettext(TEXT_DOMAIN,
2623 "cannot get history for '%s'"), zhp->zpool_name));
2627 *len = zc.zc_history_len;
2628 *off = zc.zc_history_offset;
2634 * Process the buffer of nvlists, unpacking and storing each nvlist record
2635 * into 'records'. 'leftover' is set to the number of bytes that weren't
2636 * processed as there wasn't a complete record.
2639 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
2640 nvlist_t ***records, uint_t *numrecords)
2646 while (bytes_read > sizeof (reclen)) {
2648 /* get length of packed record (stored as little endian) */
2649 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
2650 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
2652 if (bytes_read < sizeof (reclen) + reclen)
2656 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
2658 bytes_read -= sizeof (reclen) + reclen;
2659 buf += sizeof (reclen) + reclen;
2661 /* add record to nvlist array */
2663 if (ISP2(*numrecords + 1)) {
2664 *records = realloc(*records,
2665 *numrecords * 2 * sizeof (nvlist_t *));
2667 (*records)[*numrecords - 1] = nv;
2670 *leftover = bytes_read;
2674 #define HIS_BUF_LEN (128*1024)
2677 * Retrieve the command history of a pool.
2680 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
2682 char buf[HIS_BUF_LEN];
2684 nvlist_t **records = NULL;
2685 uint_t numrecords = 0;
2689 uint64_t bytes_read = sizeof (buf);
2692 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
2695 /* if nothing else was read in, we're at EOF, just return */
2699 if ((err = zpool_history_unpack(buf, bytes_read,
2700 &leftover, &records, &numrecords)) != 0)
2708 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
2709 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
2710 records, numrecords) == 0);
2712 for (i = 0; i < numrecords; i++)
2713 nvlist_free(records[i]);
2720 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
2721 char *pathname, size_t len)
2723 zfs_cmd_t zc = { 0 };
2724 boolean_t mounted = B_FALSE;
2725 char *mntpnt = NULL;
2726 char dsname[MAXNAMELEN];
2729 /* special case for the MOS */
2730 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
2734 /* get the dataset's name */
2735 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2737 if (ioctl(zhp->zpool_hdl->libzfs_fd,
2738 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
2739 /* just write out a path of two object numbers */
2740 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
2744 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
2746 /* find out if the dataset is mounted */
2747 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
2749 /* get the corrupted object's path */
2750 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
2752 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
2755 (void) snprintf(pathname, len, "%s%s", mntpnt,
2758 (void) snprintf(pathname, len, "%s:%s",
2759 dsname, zc.zc_value);
2762 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
2767 #define RDISK_ROOT "/dev/rdsk"
2768 #define BACKUP_SLICE "s2"
2770 * Don't start the slice at the default block of 34; many storage
2771 * devices will use a stripe width of 128k, so start there instead.
2773 #define NEW_START_BLOCK 256
2776 * Read the EFI label from the config, if a label does not exist then
2777 * pass back the error to the caller. If the caller has passed a non-NULL
2778 * diskaddr argument then we set it to the starting address of the EFI
2782 read_efi_label(nvlist_t *config, diskaddr_t *sb)
2786 char diskname[MAXPATHLEN];
2789 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
2792 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
2793 strrchr(path, '/'));
2794 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
2795 struct dk_gpt *vtoc;
2797 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
2799 *sb = vtoc->efi_parts[0].p_start;
2808 * determine where a partition starts on a disk in the current
2812 find_start_block(nvlist_t *config)
2816 diskaddr_t sb = MAXOFFSET_T;
2819 if (nvlist_lookup_nvlist_array(config,
2820 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
2821 if (nvlist_lookup_uint64(config,
2822 ZPOOL_CONFIG_WHOLE_DISK,
2823 &wholedisk) != 0 || !wholedisk) {
2824 return (MAXOFFSET_T);
2826 if (read_efi_label(config, &sb) < 0)
2831 for (c = 0; c < children; c++) {
2832 sb = find_start_block(child[c]);
2833 if (sb != MAXOFFSET_T) {
2837 return (MAXOFFSET_T);
2841 * Label an individual disk. The name provided is the short name,
2842 * stripped of any leading /dev path.
2845 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
2847 char path[MAXPATHLEN];
2848 struct dk_gpt *vtoc;
2850 size_t resv = EFI_MIN_RESV_SIZE;
2851 uint64_t slice_size;
2852 diskaddr_t start_block;
2855 /* prepare an error message just in case */
2856 (void) snprintf(errbuf, sizeof (errbuf),
2857 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
2862 if (pool_is_bootable(zhp)) {
2863 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2864 "EFI labeled devices are not supported on root "
2866 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
2869 verify(nvlist_lookup_nvlist(zhp->zpool_config,
2870 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2872 if (zhp->zpool_start_block == 0)
2873 start_block = find_start_block(nvroot);
2875 start_block = zhp->zpool_start_block;
2876 zhp->zpool_start_block = start_block;
2879 start_block = NEW_START_BLOCK;
2882 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
2885 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2887 * This shouldn't happen. We've long since verified that this
2888 * is a valid device.
2891 dgettext(TEXT_DOMAIN, "unable to open device"));
2892 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2895 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
2897 * The only way this can fail is if we run out of memory, or we
2898 * were unable to read the disk's capacity
2900 if (errno == ENOMEM)
2901 (void) no_memory(hdl);
2904 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2905 "unable to read disk capacity"), name);
2907 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2910 slice_size = vtoc->efi_last_u_lba + 1;
2911 slice_size -= EFI_MIN_RESV_SIZE;
2912 if (start_block == MAXOFFSET_T)
2913 start_block = NEW_START_BLOCK;
2914 slice_size -= start_block;
2916 vtoc->efi_parts[0].p_start = start_block;
2917 vtoc->efi_parts[0].p_size = slice_size;
2920 * Why we use V_USR: V_BACKUP confuses users, and is considered
2921 * disposable by some EFI utilities (since EFI doesn't have a backup
2922 * slice). V_UNASSIGNED is supposed to be used only for zero size
2923 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
2924 * etc. were all pretty specific. V_USR is as close to reality as we
2925 * can get, in the absence of V_OTHER.
2927 vtoc->efi_parts[0].p_tag = V_USR;
2928 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
2930 vtoc->efi_parts[8].p_start = slice_size + start_block;
2931 vtoc->efi_parts[8].p_size = resv;
2932 vtoc->efi_parts[8].p_tag = V_RESERVED;
2934 if (efi_write(fd, vtoc) != 0) {
2936 * Some block drivers (like pcata) may not support EFI
2937 * GPT labels. Print out a helpful error message dir-
2938 * ecting the user to manually label the disk and give
2944 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2945 "try using fdisk(1M) and then provide a specific slice"));
2946 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
2955 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
2961 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
2962 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
2963 strcmp(type, VDEV_TYPE_FILE) == 0 ||
2964 strcmp(type, VDEV_TYPE_LOG) == 0 ||
2965 strcmp(type, VDEV_TYPE_MISSING) == 0) {
2966 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2967 "vdev type '%s' is not supported"), type);
2968 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
2971 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
2972 &child, &children) == 0) {
2973 for (c = 0; c < children; c++) {
2974 if (!supported_dump_vdev_type(hdl, child[c], errbuf))
2982 * check if this zvol is allowable for use as a dump device; zero if
2983 * it is, > 0 if it isn't, < 0 if it isn't a zvol
2986 zvol_check_dump_config(char *arg)
2988 zpool_handle_t *zhp = NULL;
2989 nvlist_t *config, *nvroot;
2993 libzfs_handle_t *hdl;
2995 char poolname[ZPOOL_MAXNAMELEN];
2996 int pathlen = strlen(ZVOL_FULL_DEV_DIR);
2999 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
3003 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3004 "dump is not supported on device '%s'"), arg);
3006 if ((hdl = libzfs_init()) == NULL)
3008 libzfs_print_on_error(hdl, B_TRUE);
3010 volname = arg + pathlen;
3012 /* check the configuration of the pool */
3013 if ((p = strchr(volname, '/')) == NULL) {
3014 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3015 "malformed dataset name"));
3016 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3018 } else if (p - volname >= ZFS_MAXNAMELEN) {
3019 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3020 "dataset name is too long"));
3021 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
3024 (void) strncpy(poolname, volname, p - volname);
3025 poolname[p - volname] = '\0';
3028 if ((zhp = zpool_open(hdl, poolname)) == NULL) {
3029 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3030 "could not open pool '%s'"), poolname);
3031 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
3034 config = zpool_get_config(zhp, NULL);
3035 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3037 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3038 "could not obtain vdev configuration for '%s'"), poolname);
3039 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
3043 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3044 &top, &toplevels) == 0);
3045 if (toplevels != 1) {
3046 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3047 "'%s' has multiple top level vdevs"), poolname);
3048 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
3052 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {