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 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <sys/types.h>
41 #include <sys/zfs_ioctl.h>
45 #include "zfs_namecheck.h"
47 #include "libzfs_impl.h"
49 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
51 #if defined(__i386) || defined(__amd64)
52 #define BOOTCMD "installgrub(1M)"
54 #define BOOTCMD "installboot(1M)"
58 * ====================================================================
59 * zpool property functions
60 * ====================================================================
64 zpool_get_all_props(zpool_handle_t *zhp)
67 libzfs_handle_t *hdl = zhp->zpool_hdl;
69 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
71 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
74 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
75 if (errno == ENOMEM) {
76 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
77 zcmd_free_nvlists(&zc);
81 zcmd_free_nvlists(&zc);
86 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
87 zcmd_free_nvlists(&zc);
91 zcmd_free_nvlists(&zc);
97 zpool_props_refresh(zpool_handle_t *zhp)
101 old_props = zhp->zpool_props;
103 if (zpool_get_all_props(zhp) != 0)
106 nvlist_free(old_props);
111 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
117 zprop_source_t source;
119 nvl = zhp->zpool_props;
120 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
121 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
123 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
125 source = ZPROP_SRC_DEFAULT;
126 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
137 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
141 zprop_source_t source;
143 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
145 * zpool_get_all_props() has most likely failed because
146 * the pool is faulted, but if all we need is the top level
147 * vdev's guid then get it from the zhp config nvlist.
149 if ((prop == ZPOOL_PROP_GUID) &&
150 (nvlist_lookup_nvlist(zhp->zpool_config,
151 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
152 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
156 return (zpool_prop_default_numeric(prop));
159 nvl = zhp->zpool_props;
160 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
161 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
163 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
165 source = ZPROP_SRC_DEFAULT;
166 value = zpool_prop_default_numeric(prop);
176 * Map VDEV STATE to printed strings.
179 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
182 case VDEV_STATE_CLOSED:
183 case VDEV_STATE_OFFLINE:
184 return (gettext("OFFLINE"));
185 case VDEV_STATE_REMOVED:
186 return (gettext("REMOVED"));
187 case VDEV_STATE_CANT_OPEN:
188 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
189 return (gettext("FAULTED"));
191 return (gettext("UNAVAIL"));
192 case VDEV_STATE_FAULTED:
193 return (gettext("FAULTED"));
194 case VDEV_STATE_DEGRADED:
195 return (gettext("DEGRADED"));
196 case VDEV_STATE_HEALTHY:
197 return (gettext("ONLINE"));
200 return (gettext("UNKNOWN"));
204 * Get a zpool property value for 'prop' and return the value in
205 * a pre-allocated buffer.
208 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
209 zprop_source_t *srctype)
213 zprop_source_t src = ZPROP_SRC_NONE;
218 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
220 case ZPOOL_PROP_NAME:
221 (void) strlcpy(buf, zpool_get_name(zhp), len);
224 case ZPOOL_PROP_HEALTH:
225 (void) strlcpy(buf, "FAULTED", len);
228 case ZPOOL_PROP_GUID:
229 intval = zpool_get_prop_int(zhp, prop, &src);
230 (void) snprintf(buf, len, "%llu", intval);
233 case ZPOOL_PROP_ALTROOT:
234 case ZPOOL_PROP_CACHEFILE:
235 if (zhp->zpool_props != NULL ||
236 zpool_get_all_props(zhp) == 0) {
238 zpool_get_prop_string(zhp, prop, &src),
246 (void) strlcpy(buf, "-", len);
255 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
256 prop != ZPOOL_PROP_NAME)
259 switch (zpool_prop_get_type(prop)) {
260 case PROP_TYPE_STRING:
261 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
265 case PROP_TYPE_NUMBER:
266 intval = zpool_get_prop_int(zhp, prop, &src);
269 case ZPOOL_PROP_SIZE:
270 case ZPOOL_PROP_USED:
271 case ZPOOL_PROP_AVAILABLE:
272 (void) zfs_nicenum(intval, buf, len);
275 case ZPOOL_PROP_CAPACITY:
276 (void) snprintf(buf, len, "%llu%%",
277 (u_longlong_t)intval);
280 case ZPOOL_PROP_HEALTH:
281 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
282 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
283 verify(nvlist_lookup_uint64_array(nvroot,
284 ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
286 (void) strlcpy(buf, zpool_state_to_name(intval,
290 (void) snprintf(buf, len, "%llu", intval);
294 case PROP_TYPE_INDEX:
295 intval = zpool_get_prop_int(zhp, prop, &src);
296 if (zpool_prop_index_to_string(prop, intval, &strval)
299 (void) strlcpy(buf, strval, len);
313 pool_is_bootable(zpool_handle_t *zhp)
315 char bootfs[ZPOOL_MAXNAMELEN];
317 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
318 sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
319 sizeof (bootfs)) != 0);
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)
353 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
354 &child, &children) != 0)
355 return (read_efi_label(config, NULL) >= 0);
357 for (c = 0; c < children; c++) {
358 if (pool_uses_efi(child[c]))
366 * Given an nvlist of zpool properties to be set, validate that they are
367 * correct, and parse any numeric properties (index, boolean, etc) if they are
368 * specified as strings.
371 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
372 nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf)
380 struct stat64 statbuf;
384 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
385 (void) no_memory(hdl);
390 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
391 const char *propname = nvpair_name(elem);
394 * Make sure this property is valid and applies to this type.
396 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
397 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
398 "invalid property '%s'"), propname);
399 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
403 if (zpool_prop_readonly(prop)) {
404 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
405 "is readonly"), propname);
406 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
410 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
411 &strval, &intval, errbuf) != 0)
415 * Perform additional checking for specific properties.
418 case ZPOOL_PROP_VERSION:
419 if (intval < version || intval > SPA_VERSION) {
420 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
421 "property '%s' number %d is invalid."),
423 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
428 case ZPOOL_PROP_BOOTFS:
429 if (create_or_import) {
430 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
431 "property '%s' cannot be set at creation "
432 "or import time"), propname);
433 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
437 if (version < SPA_VERSION_BOOTFS) {
438 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
439 "pool must be upgraded to support "
440 "'%s' property"), propname);
441 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
446 * bootfs property value has to be a dataset name and
447 * the dataset has to be in the same pool as it sets to.
449 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
451 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
452 "is an invalid name"), strval);
453 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
457 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
458 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
459 "could not open pool '%s'"), poolname);
460 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
463 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
464 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
468 * bootfs property cannot be set on a disk which has
471 if (pool_uses_efi(nvroot)) {
472 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
473 "property '%s' not supported on "
474 "EFI labeled devices"), propname);
475 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
483 case ZPOOL_PROP_ALTROOT:
484 if (!create_or_import) {
485 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
486 "property '%s' can only be set during pool "
487 "creation or import"), propname);
488 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
492 if (strval[0] != '/') {
493 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
494 "bad alternate root '%s'"), strval);
495 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
500 case ZPOOL_PROP_CACHEFILE:
501 if (strval[0] == '\0')
504 if (strcmp(strval, "none") == 0)
507 if (strval[0] != '/') {
508 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
509 "property '%s' must be empty, an "
510 "absolute path, or 'none'"), propname);
511 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
515 slash = strrchr(strval, '/');
517 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
518 strcmp(slash, "/..") == 0) {
519 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
520 "'%s' is not a valid file"), strval);
521 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
527 if (strval[0] != '\0' &&
528 (stat64(strval, &statbuf) != 0 ||
529 !S_ISDIR(statbuf.st_mode))) {
530 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
531 "'%s' is not a valid directory"),
533 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
544 nvlist_free(retprops);
549 * Set zpool property : propname=propval.
552 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
554 zfs_cmd_t zc = { 0 };
557 nvlist_t *nvl = NULL;
561 (void) snprintf(errbuf, sizeof (errbuf),
562 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
565 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
566 return (no_memory(zhp->zpool_hdl));
568 if (nvlist_add_string(nvl, propname, propval) != 0) {
570 return (no_memory(zhp->zpool_hdl));
573 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
574 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
575 zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) {
584 * Execute the corresponding ioctl() to set this property.
586 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
588 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
593 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
595 zcmd_free_nvlists(&zc);
599 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
601 (void) zpool_props_refresh(zhp);
607 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
609 libzfs_handle_t *hdl = zhp->zpool_hdl;
611 char buf[ZFS_MAXPROPLEN];
613 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
616 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
621 if (entry->pl_prop != ZPROP_INVAL &&
622 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
624 if (strlen(buf) > entry->pl_width)
625 entry->pl_width = strlen(buf);
634 * Validate the given pool name, optionally putting an extended error message in
638 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
644 ret = pool_namecheck(pool, &why, &what);
647 * The rules for reserved pool names were extended at a later point.
648 * But we need to support users with existing pools that may now be
649 * invalid. So we only check for this expanded set of names during a
650 * create (or import), and only in userland.
652 if (ret == 0 && !isopen &&
653 (strncmp(pool, "mirror", 6) == 0 ||
654 strncmp(pool, "raidz", 5) == 0 ||
655 strncmp(pool, "spare", 5) == 0 ||
656 strcmp(pool, "log") == 0)) {
659 dgettext(TEXT_DOMAIN, "name is reserved"));
667 case NAME_ERR_TOOLONG:
669 dgettext(TEXT_DOMAIN, "name is too long"));
672 case NAME_ERR_INVALCHAR:
674 dgettext(TEXT_DOMAIN, "invalid character "
675 "'%c' in pool name"), what);
678 case NAME_ERR_NOLETTER:
679 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
680 "name must begin with a letter"));
683 case NAME_ERR_RESERVED:
684 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
685 "name is reserved"));
688 case NAME_ERR_DISKLIKE:
689 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
690 "pool name is reserved"));
693 case NAME_ERR_LEADING_SLASH:
694 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
695 "leading slash in name"));
698 case NAME_ERR_EMPTY_COMPONENT:
699 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
700 "empty component in name"));
703 case NAME_ERR_TRAILING_SLASH:
704 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
705 "trailing slash in name"));
708 case NAME_ERR_MULTIPLE_AT:
709 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
710 "multiple '@' delimiters in name"));
722 * Open a handle to the given pool, even if the pool is currently in the FAULTED
726 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
732 * Make sure the pool name is valid.
734 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
735 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
736 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
741 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
744 zhp->zpool_hdl = hdl;
745 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
747 if (zpool_refresh_stats(zhp, &missing) != 0) {
753 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
754 (void) zfs_error_fmt(hdl, EZFS_NOENT,
755 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
764 * Like the above, but silent on error. Used when iterating over pools (because
765 * the configuration cache may be out of date).
768 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
773 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
776 zhp->zpool_hdl = hdl;
777 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
779 if (zpool_refresh_stats(zhp, &missing) != 0) {
795 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
799 zpool_open(libzfs_handle_t *hdl, const char *pool)
803 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
806 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
807 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
808 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
817 * Close the handle. Simply frees the memory associated with the handle.
820 zpool_close(zpool_handle_t *zhp)
822 if (zhp->zpool_config)
823 nvlist_free(zhp->zpool_config);
824 if (zhp->zpool_old_config)
825 nvlist_free(zhp->zpool_old_config);
826 if (zhp->zpool_props)
827 nvlist_free(zhp->zpool_props);
832 * Return the name of the pool.
835 zpool_get_name(zpool_handle_t *zhp)
837 return (zhp->zpool_name);
842 * Return the state of the pool (ACTIVE or UNAVAILABLE)
845 zpool_get_state(zpool_handle_t *zhp)
847 return (zhp->zpool_state);
851 * Create the named pool, using the provided vdev list. It is assumed
852 * that the consumer has already validated the contents of the nvlist, so we
853 * don't have to worry about error semantics.
856 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
857 nvlist_t *props, nvlist_t *fsprops)
859 zfs_cmd_t zc = { 0 };
860 nvlist_t *zc_fsprops = NULL;
861 nvlist_t *zc_props = NULL;
866 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
867 "cannot create '%s'"), pool);
869 if (!zpool_name_valid(hdl, B_FALSE, pool))
870 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
872 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
876 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
877 SPA_VERSION_1, B_TRUE, msg)) == NULL) {
886 zoned = ((nvlist_lookup_string(fsprops,
887 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
888 strcmp(zonestr, "on") == 0);
890 if ((zc_fsprops = zfs_valid_proplist(hdl,
891 ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
895 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
898 if (nvlist_add_nvlist(zc_props,
899 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
904 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
907 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
909 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
911 zcmd_free_nvlists(&zc);
912 nvlist_free(zc_props);
913 nvlist_free(zc_fsprops);
918 * This can happen if the user has specified the same
919 * device multiple times. We can't reliably detect this
920 * until we try to add it and see we already have a
923 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
924 "one or more vdevs refer to the same device"));
925 return (zfs_error(hdl, EZFS_BADDEV, msg));
929 * This occurs when one of the devices is below
930 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
931 * device was the problem device since there's no
932 * reliable way to determine device size from userland.
937 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
939 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
940 "one or more devices is less than the "
941 "minimum size (%s)"), buf);
943 return (zfs_error(hdl, EZFS_BADDEV, msg));
946 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
947 "one or more devices is out of space"));
948 return (zfs_error(hdl, EZFS_BADDEV, msg));
951 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
952 "cache device must be a disk or disk slice"));
953 return (zfs_error(hdl, EZFS_BADDEV, msg));
956 return (zpool_standard_error(hdl, errno, msg));
961 * If this is an alternate root pool, then we automatically set the
962 * mountpoint of the root dataset to be '/'.
964 if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
968 verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
969 verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
976 zcmd_free_nvlists(&zc);
977 nvlist_free(zc_props);
978 nvlist_free(zc_fsprops);
983 * Destroy the given pool. It is up to the caller to ensure that there are no
984 * datasets left in the pool.
987 zpool_destroy(zpool_handle_t *zhp)
989 zfs_cmd_t zc = { 0 };
990 zfs_handle_t *zfp = NULL;
991 libzfs_handle_t *hdl = zhp->zpool_hdl;
994 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
995 (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
996 ZFS_TYPE_FILESYSTEM)) == NULL)
999 if (zpool_remove_zvol_links(zhp) != 0)
1002 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1004 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1005 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1006 "cannot destroy '%s'"), zhp->zpool_name);
1008 if (errno == EROFS) {
1009 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1010 "one or more devices is read only"));
1011 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1013 (void) zpool_standard_error(hdl, errno, msg);
1022 remove_mountpoint(zfp);
1030 * Add the given vdevs to the pool. The caller must have already performed the
1031 * necessary verification to ensure that the vdev specification is well-formed.
1034 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1036 zfs_cmd_t zc = { 0 };
1038 libzfs_handle_t *hdl = zhp->zpool_hdl;
1040 nvlist_t **spares, **l2cache;
1041 uint_t nspares, nl2cache;
1043 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1044 "cannot add to '%s'"), zhp->zpool_name);
1046 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1047 SPA_VERSION_SPARES &&
1048 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1049 &spares, &nspares) == 0) {
1050 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1051 "upgraded to add hot spares"));
1052 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1055 if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1056 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1059 for (s = 0; s < nspares; s++) {
1062 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1063 &path) == 0 && pool_uses_efi(spares[s])) {
1064 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1065 "device '%s' contains an EFI label and "
1066 "cannot be used on root pools."),
1067 zpool_vdev_name(hdl, NULL, spares[s]));
1068 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1073 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1074 SPA_VERSION_L2CACHE &&
1075 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1076 &l2cache, &nl2cache) == 0) {
1077 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1078 "upgraded to add cache devices"));
1079 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1082 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1084 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1086 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1090 * This can happen if the user has specified the same
1091 * device multiple times. We can't reliably detect this
1092 * until we try to add it and see we already have a
1095 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1096 "one or more vdevs refer to the same device"));
1097 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1102 * This occurrs when one of the devices is below
1103 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1104 * device was the problem device since there's no
1105 * reliable way to determine device size from userland.
1110 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1112 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1113 "device is less than the minimum "
1116 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1120 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1121 "pool must be upgraded to add these vdevs"));
1122 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1126 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1127 "root pool can not have multiple vdevs"
1128 " or separate logs"));
1129 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1133 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1134 "cache device must be a disk or disk slice"));
1135 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1139 (void) zpool_standard_error(hdl, errno, msg);
1147 zcmd_free_nvlists(&zc);
1153 * Exports the pool from the system. The caller must ensure that there are no
1154 * mounted datasets in the pool.
1157 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1159 zfs_cmd_t zc = { 0 };
1162 if (zpool_remove_zvol_links(zhp) != 0)
1165 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1166 "cannot export '%s'"), zhp->zpool_name);
1168 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1169 zc.zc_cookie = force;
1170 zc.zc_guid = hardforce;
1172 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1175 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1176 "use '-f' to override the following errors:\n"
1177 "'%s' has an active shared spare which could be"
1178 " used by other pools once '%s' is exported."),
1179 zhp->zpool_name, zhp->zpool_name);
1180 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1183 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1192 zpool_export(zpool_handle_t *zhp, boolean_t force)
1194 return (zpool_export_common(zhp, force, B_FALSE));
1198 zpool_export_force(zpool_handle_t *zhp)
1200 return (zpool_export_common(zhp, B_TRUE, B_TRUE));
1204 * zpool_import() is a contracted interface. Should be kept the same
1207 * Applications should use zpool_import_props() to import a pool with
1208 * new properties value to be set.
1211 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1214 nvlist_t *props = NULL;
1217 if (altroot != NULL) {
1218 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1219 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1220 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1224 if (nvlist_add_string(props,
1225 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1226 nvlist_add_string(props,
1227 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1229 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1230 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1235 ret = zpool_import_props(hdl, config, newname, props, B_FALSE);
1242 * Import the given pool using the known configuration and a list of
1243 * properties to be set. The configuration should have come from
1244 * zpool_find_import(). The 'newname' parameters control whether the pool
1245 * is imported with a different name.
1248 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1249 nvlist_t *props, boolean_t importfaulted)
1251 zfs_cmd_t zc = { 0 };
1257 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1260 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1261 "cannot import pool '%s'"), origname);
1263 if (newname != NULL) {
1264 if (!zpool_name_valid(hdl, B_FALSE, newname))
1265 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1266 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1268 thename = (char *)newname;
1276 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1279 if ((props = zpool_valid_proplist(hdl, origname,
1280 props, version, B_TRUE, errbuf)) == NULL) {
1282 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1288 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1290 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1293 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1298 zc.zc_cookie = (uint64_t)importfaulted;
1300 if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) {
1302 if (newname == NULL)
1303 (void) snprintf(desc, sizeof (desc),
1304 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1307 (void) snprintf(desc, sizeof (desc),
1308 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1314 * Unsupported version.
1316 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1320 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1324 (void) zpool_standard_error(hdl, errno, desc);
1329 zpool_handle_t *zhp;
1332 * This should never fail, but play it safe anyway.
1334 if (zpool_open_silent(hdl, thename, &zhp) != 0) {
1336 } else if (zhp != NULL) {
1337 ret = zpool_create_zvol_links(zhp);
1343 zcmd_free_nvlists(&zc);
1353 zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type)
1355 zfs_cmd_t zc = { 0 };
1357 libzfs_handle_t *hdl = zhp->zpool_hdl;
1359 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1360 zc.zc_cookie = type;
1362 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0)
1365 (void) snprintf(msg, sizeof (msg),
1366 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1369 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1371 return (zpool_standard_error(hdl, errno, msg));
1375 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1376 * spare; but FALSE if its an INUSE spare.
1379 vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid,
1380 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
1384 uint64_t theguid, present;
1386 uint64_t wholedisk = 0;
1390 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0);
1392 if (search == NULL &&
1393 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) {
1395 * If the device has never been present since import, the only
1396 * reliable way to match the vdev is by GUID.
1398 if (theguid == guid)
1400 } else if (search != NULL &&
1401 nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1402 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1406 * For whole disks, the internal path has 's0', but the
1407 * path passed in by the user doesn't.
1409 if (strlen(search) == strlen(path) - 2 &&
1410 strncmp(search, path, strlen(search)) == 0)
1412 } else if (strcmp(search, path) == 0) {
1417 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1418 &child, &children) != 0)
1421 for (c = 0; c < children; c++) {
1422 if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1423 avail_spare, l2cache, NULL)) != NULL) {
1425 * The 'is_log' value is only set for the toplevel
1426 * vdev, not the leaf vdevs. So we always lookup the
1427 * log device from the root of the vdev tree (where
1428 * 'log' is non-NULL).
1431 nvlist_lookup_uint64(child[c],
1432 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
1440 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1441 &child, &children) == 0) {
1442 for (c = 0; c < children; c++) {
1443 if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1444 avail_spare, l2cache, NULL)) != NULL) {
1445 *avail_spare = B_TRUE;
1451 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1452 &child, &children) == 0) {
1453 for (c = 0; c < children; c++) {
1454 if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1455 avail_spare, l2cache, NULL)) != NULL) {
1466 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
1467 boolean_t *l2cache, boolean_t *log)
1469 char buf[MAXPATHLEN];
1475 guid = strtoull(path, &end, 10);
1476 if (guid != 0 && *end == '\0') {
1478 } else if (path[0] != '/') {
1479 (void) snprintf(buf, sizeof (buf), "%s%s", _PATH_DEV, path);
1485 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1488 *avail_spare = B_FALSE;
1492 return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare,
1497 vdev_online(nvlist_t *nv)
1501 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
1502 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
1503 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
1510 * Get phys_path for a root pool
1511 * Return 0 on success; non-zeron on failure.
1514 zpool_get_physpath(zpool_handle_t *zhp, char *physpath)
1516 nvlist_t *vdev_root;
1522 * Make sure this is a root pool, as phys_path doesn't mean
1523 * anything to a non-root pool.
1525 if (!pool_is_bootable(zhp))
1528 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1529 ZPOOL_CONFIG_VDEV_TREE, &vdev_root) == 0);
1531 if (nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
1532 &child, &count) != 0)
1535 for (i = 0; i < count; i++) {
1542 if (nvlist_lookup_string(child[i], ZPOOL_CONFIG_TYPE, &type)
1546 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
1547 if (!vdev_online(child[i]))
1549 verify(nvlist_lookup_string(child[i],
1550 ZPOOL_CONFIG_PHYS_PATH, &tmppath) == 0);
1551 (void) strncpy(physpath, tmppath, strlen(tmppath));
1552 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0) {
1553 if (nvlist_lookup_nvlist_array(child[i],
1554 ZPOOL_CONFIG_CHILDREN, &child2, &count2) != 0)
1557 for (j = 0; j < count2; j++) {
1558 if (!vdev_online(child2[j]))
1560 if (nvlist_lookup_string(child2[j],
1561 ZPOOL_CONFIG_PHYS_PATH, &tmppath) != 0)
1564 if ((strlen(physpath) + strlen(tmppath)) >
1568 if (strlen(physpath) == 0) {
1569 (void) strncpy(physpath, tmppath,
1572 (void) strcat(physpath, " ");
1573 (void) strcat(physpath, tmppath);
1585 * Returns TRUE if the given guid corresponds to the given type.
1586 * This is used to check for hot spares (INUSE or not), and level 2 cache
1590 is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type)
1592 uint64_t target_guid;
1598 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1600 if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) {
1601 for (i = 0; i < count; i++) {
1602 verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID,
1603 &target_guid) == 0);
1604 if (guid == target_guid)
1613 * Bring the specified vdev online. The 'flags' parameter is a set of the
1614 * ZFS_ONLINE_* flags.
1617 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
1618 vdev_state_t *newstate)
1620 zfs_cmd_t zc = { 0 };
1623 boolean_t avail_spare, l2cache;
1624 libzfs_handle_t *hdl = zhp->zpool_hdl;
1626 (void) snprintf(msg, sizeof (msg),
1627 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
1629 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1630 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1632 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1634 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1637 is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
1638 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1640 zc.zc_cookie = VDEV_STATE_ONLINE;
1643 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0)
1644 return (zpool_standard_error(hdl, errno, msg));
1646 *newstate = zc.zc_cookie;
1651 * Take the specified vdev offline
1654 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
1656 zfs_cmd_t zc = { 0 };
1659 boolean_t avail_spare, l2cache;
1660 libzfs_handle_t *hdl = zhp->zpool_hdl;
1662 (void) snprintf(msg, sizeof (msg),
1663 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
1665 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1666 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1668 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1670 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1673 is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
1674 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1676 zc.zc_cookie = VDEV_STATE_OFFLINE;
1677 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
1679 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1686 * There are no other replicas of this device.
1688 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
1691 return (zpool_standard_error(hdl, errno, msg));
1696 * Mark the given vdev faulted.
1699 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid)
1701 zfs_cmd_t zc = { 0 };
1703 libzfs_handle_t *hdl = zhp->zpool_hdl;
1705 (void) snprintf(msg, sizeof (msg),
1706 dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
1708 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1710 zc.zc_cookie = VDEV_STATE_FAULTED;
1712 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1719 * There are no other replicas of this device.
1721 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
1725 * The log device has unplayed logs
1727 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
1730 return (zpool_standard_error(hdl, errno, msg));
1736 * Mark the given vdev degraded.
1739 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid)
1741 zfs_cmd_t zc = { 0 };
1743 libzfs_handle_t *hdl = zhp->zpool_hdl;
1745 (void) snprintf(msg, sizeof (msg),
1746 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
1748 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1750 zc.zc_cookie = VDEV_STATE_DEGRADED;
1752 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1755 return (zpool_standard_error(hdl, errno, msg));
1759 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
1763 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
1769 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
1771 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
1774 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
1775 children == 2 && child[which] == tgt)
1778 for (c = 0; c < children; c++)
1779 if (is_replacing_spare(child[c], tgt, which))
1787 * Attach new_disk (fully described by nvroot) to old_disk.
1788 * If 'replacing' is specified, the new disk will replace the old one.
1791 zpool_vdev_attach(zpool_handle_t *zhp,
1792 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
1794 zfs_cmd_t zc = { 0 };
1798 boolean_t avail_spare, l2cache, islog;
1800 char *path, *newname;
1803 nvlist_t *config_root;
1804 libzfs_handle_t *hdl = zhp->zpool_hdl;
1805 boolean_t rootpool = pool_is_bootable(zhp);
1808 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1809 "cannot replace %s with %s"), old_disk, new_disk);
1811 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1812 "cannot attach %s to %s"), new_disk, old_disk);
1815 * If this is a root pool, make sure that we're not attaching an
1816 * EFI labeled device.
1818 if (rootpool && pool_uses_efi(nvroot)) {
1819 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1820 "EFI labeled devices are not supported on root pools."));
1821 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1824 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1825 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
1827 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1830 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1833 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
1835 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1836 zc.zc_cookie = replacing;
1838 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1839 &child, &children) != 0 || children != 1) {
1840 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1841 "new device must be a single disk"));
1842 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
1845 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
1846 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
1848 if ((newname = zpool_vdev_name(NULL, NULL, child[0])) == NULL)
1852 * If the target is a hot spare that has been swapped in, we can only
1853 * replace it with another hot spare.
1856 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
1857 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
1858 NULL) == NULL || !avail_spare) &&
1859 is_replacing_spare(config_root, tgt, 1)) {
1860 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1861 "can only be replaced by another hot spare"));
1863 return (zfs_error(hdl, EZFS_BADTARGET, msg));
1867 * If we are attempting to replace a spare, it canot be applied to an
1868 * already spared device.
1871 nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
1872 zpool_find_vdev(zhp, newname, &avail_spare,
1873 &l2cache, NULL) != NULL && avail_spare &&
1874 is_replacing_spare(config_root, tgt, 0)) {
1875 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1876 "device has already been replaced with a spare"));
1878 return (zfs_error(hdl, EZFS_BADTARGET, msg));
1883 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1886 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc);
1888 zcmd_free_nvlists(&zc);
1893 * XXX - This should be removed once we can
1894 * automatically install the bootblocks on the
1895 * newly attached disk.
1897 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Please "
1898 "be sure to invoke %s to make '%s' bootable.\n"),
1907 * Can't attach to or replace this type of vdev.
1911 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1912 "cannot replace a log with a spare"));
1914 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1915 "cannot replace a replacing device"));
1917 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1918 "can only attach to mirrors and top-level "
1921 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
1926 * The new device must be a single disk.
1928 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1929 "new device must be a single disk"));
1930 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
1934 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
1936 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1941 * The new device is too small.
1943 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1944 "device is too small"));
1945 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1950 * The new device has a different alignment requirement.
1952 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1953 "devices have different sector alignment"));
1954 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1959 * The resulting top-level vdev spec won't fit in the label.
1961 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
1965 (void) zpool_standard_error(hdl, errno, msg);
1972 * Detach the specified device.
1975 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
1977 zfs_cmd_t zc = { 0 };
1980 boolean_t avail_spare, l2cache;
1981 libzfs_handle_t *hdl = zhp->zpool_hdl;
1983 (void) snprintf(msg, sizeof (msg),
1984 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
1986 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1987 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1989 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1992 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1995 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
1997 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1999 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2006 * Can't detach from this type of vdev.
2008 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2009 "applicable to mirror and replacing vdevs"));
2010 (void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg);
2015 * There are no other replicas of this device.
2017 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2021 (void) zpool_standard_error(hdl, errno, msg);
2028 * Remove the given device. Currently, this is supported only for hot spares
2029 * and level 2 cache devices.
2032 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
2034 zfs_cmd_t zc = { 0 };
2037 boolean_t avail_spare, l2cache;
2038 libzfs_handle_t *hdl = zhp->zpool_hdl;
2040 (void) snprintf(msg, sizeof (msg),
2041 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
2043 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2044 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2046 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2048 if (!avail_spare && !l2cache) {
2049 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2050 "only inactive hot spares or cache devices "
2052 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2055 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2057 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
2060 return (zpool_standard_error(hdl, errno, msg));
2064 * Clear the errors for the pool, or the particular device if specified.
2067 zpool_clear(zpool_handle_t *zhp, const char *path)
2069 zfs_cmd_t zc = { 0 };
2072 boolean_t avail_spare, l2cache;
2073 libzfs_handle_t *hdl = zhp->zpool_hdl;
2076 (void) snprintf(msg, sizeof (msg),
2077 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2080 (void) snprintf(msg, sizeof (msg),
2081 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2084 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2086 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
2087 &l2cache, NULL)) == 0)
2088 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2091 * Don't allow error clearing for hot spares. Do allow
2092 * error clearing for l2cache devices.
2095 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2097 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
2101 if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
2104 return (zpool_standard_error(hdl, errno, msg));
2108 * Similar to zpool_clear(), but takes a GUID (used by fmd).
2111 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
2113 zfs_cmd_t zc = { 0 };
2115 libzfs_handle_t *hdl = zhp->zpool_hdl;
2117 (void) snprintf(msg, sizeof (msg),
2118 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
2121 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2124 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
2127 return (zpool_standard_error(hdl, errno, msg));
2131 * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool>
2135 zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *),
2138 libzfs_handle_t *hdl = zhp->zpool_hdl;
2139 char (*paths)[MAXPATHLEN];
2140 char path[MAXPATHLEN];
2142 int curr, fd, base, ret = 0;
2147 if ((base = open(ZVOL_FULL_DEV_DIR, O_RDONLY)) < 0)
2148 return (errno == ENOENT ? 0 : -1);
2150 snprintf(path, sizeof(path), "%s/%s", ZVOL_FULL_DEV_DIR,
2152 if (stat(path, &st) != 0) {
2155 return (err == ENOENT ? 0 : -1);
2159 * Oddly this wasn't a directory -- ignore that failure since we
2160 * know there are no links lower in the (non-existant) hierarchy.
2162 if (!S_ISDIR(st.st_mode)) {
2167 if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) {
2172 (void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0]));
2176 snprintf(path, sizeof(path), "%s/%s", ZVOL_FULL_DEV_DIR,
2178 if (lstat(path, &st) != 0)
2181 if (S_ISDIR(st.st_mode)) {
2182 if ((dirp = opendir(path)) == NULL) {
2186 while ((dp = readdir(dirp)) != NULL) {
2187 if (dp->d_name[0] == '.')
2190 if (curr + 1 == size) {
2191 paths = zfs_realloc(hdl, paths,
2192 size * sizeof (paths[0]),
2193 size * 2 * sizeof (paths[0]));
2194 if (paths == NULL) {
2195 (void) closedir(dirp);
2202 (void) strlcpy(paths[curr + 1], paths[curr],
2203 sizeof (paths[curr + 1]));
2204 (void) strlcat(paths[curr], "/",
2205 sizeof (paths[curr]));
2206 (void) strlcat(paths[curr], dp->d_name,
2207 sizeof (paths[curr]));
2211 (void) closedir(dirp);
2214 if ((ret = cb(paths[curr], data)) != 0)
2232 typedef struct zvol_cb {
2233 zpool_handle_t *zcb_pool;
2234 boolean_t zcb_create;
2239 do_zvol_create(zfs_handle_t *zhp, void *data)
2243 if (ZFS_IS_VOLUME(zhp)) {
2244 (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
2245 ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL);
2249 ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL);
2257 * Iterate over all zvols in the pool and make any necessary minor nodes.
2260 zpool_create_zvol_links(zpool_handle_t *zhp)
2266 * If the pool is unavailable, just return success.
2268 if ((zfp = make_dataset_handle(zhp->zpool_hdl,
2269 zhp->zpool_name)) == NULL)
2272 ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL);
2279 do_zvol_remove(const char *dataset, void *data)
2281 zpool_handle_t *zhp = data;
2283 return (zvol_remove_link(zhp->zpool_hdl, dataset));
2287 * Iterate over all zvols in the pool and remove any minor nodes. We iterate
2288 * by examining the /dev links so that a corrupted pool doesn't impede this
2292 zpool_remove_zvol_links(zpool_handle_t *zhp)
2294 return (zpool_iter_zvol(zhp, do_zvol_remove, zhp));
2298 * Convert from a devid string to a path.
2301 devid_to_path(char *devid_str)
2306 devid_nmlist_t *list = NULL;
2309 if (devid_str_decode(devid_str, &devid, &minor) != 0)
2312 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
2314 devid_str_free(minor);
2320 if ((path = strdup(list[0].devname)) == NULL)
2323 devid_free_nmlist(list);
2329 * Convert from a path to a devid string.
2332 path_to_devid(const char *path)
2338 if ((fd = open(path, O_RDONLY)) < 0)
2343 if (devid_get(fd, &devid) == 0) {
2344 if (devid_get_minor_name(fd, &minor) == 0)
2345 ret = devid_str_encode(devid, minor);
2347 devid_str_free(minor);
2356 * Issue the necessary ioctl() to update the stored path value for the vdev. We
2357 * ignore any failure here, since a common case is for an unprivileged user to
2358 * type 'zpool status', and we'll display the correct information anyway.
2361 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
2363 zfs_cmd_t zc = { 0 };
2365 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2366 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
2367 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2370 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
2374 * Given a vdev, return the name to display in iostat. If the vdev has a path,
2375 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
2376 * We also check if this is a whole disk, in which case we strip off the
2377 * trailing 's0' slice name.
2379 * This routine is also responsible for identifying when disks have been
2380 * reconfigured in a new location. The kernel will have opened the device by
2381 * devid, but the path will still refer to the old location. To catch this, we
2382 * first do a path -> devid translation (which is fast for the common case). If
2383 * the devid matches, we're done. If not, we do a reverse devid -> path
2384 * translation and issue the appropriate ioctl() to update the path of the vdev.
2385 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
2389 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv)
2397 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
2399 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2401 (void) snprintf(buf, sizeof (buf), "%llu",
2402 (u_longlong_t)value);
2404 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
2407 * If the device is dead (faulted, offline, etc) then don't
2408 * bother opening it. Otherwise we may be forcing the user to
2409 * open a misbehaving device, which can have undesirable
2412 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
2413 (uint64_t **)&vs, &vsc) != 0 ||
2414 vs->vs_state >= VDEV_STATE_DEGRADED) &&
2416 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
2418 * Determine if the current path is correct.
2420 char *newdevid = path_to_devid(path);
2422 if (newdevid == NULL ||
2423 strcmp(devid, newdevid) != 0) {
2426 if ((newpath = devid_to_path(devid)) != NULL) {
2428 * Update the path appropriately.
2430 set_path(zhp, nv, newpath);
2431 if (nvlist_add_string(nv,
2432 ZPOOL_CONFIG_PATH, newpath) == 0)
2433 verify(nvlist_lookup_string(nv,
2441 devid_str_free(newdevid);
2444 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
2445 path += sizeof(_PATH_DEV) - 1;
2447 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2448 &value) == 0 && value) {
2449 char *tmp = zfs_strdup(hdl, path);
2452 tmp[strlen(path) - 2] = '\0';
2456 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
2459 * If it's a raidz device, we need to stick in the parity level.
2461 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
2462 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
2464 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
2465 (u_longlong_t)value);
2470 return (zfs_strdup(hdl, path));
2474 zbookmark_compare(const void *a, const void *b)
2476 return (memcmp(a, b, sizeof (zbookmark_t)));
2480 * Retrieve the persistent error log, uniquify the members, and return to the
2484 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
2486 zfs_cmd_t zc = { 0 };
2488 zbookmark_t *zb = NULL;
2492 * Retrieve the raw error list from the kernel. If the number of errors
2493 * has increased, allocate more space and continue until we get the
2496 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
2500 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
2501 count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
2503 zc.zc_nvlist_dst_size = count;
2504 (void) strcpy(zc.zc_name, zhp->zpool_name);
2506 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
2508 free((void *)(uintptr_t)zc.zc_nvlist_dst);
2509 if (errno == ENOMEM) {
2510 count = zc.zc_nvlist_dst_size;
2511 if ((zc.zc_nvlist_dst = (uintptr_t)
2512 zfs_alloc(zhp->zpool_hdl, count *
2513 sizeof (zbookmark_t))) == (uintptr_t)NULL)
2524 * Sort the resulting bookmarks. This is a little confusing due to the
2525 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
2526 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
2527 * _not_ copied as part of the process. So we point the start of our
2528 * array appropriate and decrement the total number of elements.
2530 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
2531 zc.zc_nvlist_dst_size;
2532 count -= zc.zc_nvlist_dst_size;
2534 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
2536 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
2539 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
2541 for (i = 0; i < count; i++) {
2544 /* ignoring zb_blkid and zb_level for now */
2545 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
2546 zb[i-1].zb_object == zb[i].zb_object)
2549 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
2551 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
2552 zb[i].zb_objset) != 0) {
2556 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
2557 zb[i].zb_object) != 0) {
2561 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
2568 free((void *)(uintptr_t)zc.zc_nvlist_dst);
2572 free((void *)(uintptr_t)zc.zc_nvlist_dst);
2573 return (no_memory(zhp->zpool_hdl));
2577 * Upgrade a ZFS pool to the latest on-disk version.
2580 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
2582 zfs_cmd_t zc = { 0 };
2583 libzfs_handle_t *hdl = zhp->zpool_hdl;
2585 (void) strcpy(zc.zc_name, zhp->zpool_name);
2586 zc.zc_cookie = new_version;
2588 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
2589 return (zpool_standard_error_fmt(hdl, errno,
2590 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
2596 zpool_set_history_str(const char *subcommand, int argc, char **argv,
2601 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
2602 for (i = 1; i < argc; i++) {
2603 if (strlen(history_str) + 1 + strlen(argv[i]) >
2606 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
2607 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
2612 * Stage command history for logging.
2615 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
2617 if (history_str == NULL)
2620 if (strlen(history_str) > HIS_MAX_RECORD_LEN)
2623 if (hdl->libzfs_log_str != NULL)
2624 free(hdl->libzfs_log_str);
2626 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
2627 return (no_memory(hdl));
2633 * Perform ioctl to get some command history of a pool.
2635 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
2636 * logical offset of the history buffer to start reading from.
2638 * Upon return, 'off' is the next logical offset to read from and
2639 * 'len' is the actual amount of bytes read into 'buf'.
2642 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
2644 zfs_cmd_t zc = { 0 };
2645 libzfs_handle_t *hdl = zhp->zpool_hdl;
2647 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2649 zc.zc_history = (uint64_t)(uintptr_t)buf;
2650 zc.zc_history_len = *len;
2651 zc.zc_history_offset = *off;
2653 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
2656 return (zfs_error_fmt(hdl, EZFS_PERM,
2657 dgettext(TEXT_DOMAIN,
2658 "cannot show history for pool '%s'"),
2661 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
2662 dgettext(TEXT_DOMAIN, "cannot get history for pool "
2663 "'%s'"), zhp->zpool_name));
2665 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
2666 dgettext(TEXT_DOMAIN, "cannot get history for pool "
2667 "'%s', pool must be upgraded"), zhp->zpool_name));
2669 return (zpool_standard_error_fmt(hdl, errno,
2670 dgettext(TEXT_DOMAIN,
2671 "cannot get history for '%s'"), zhp->zpool_name));
2675 *len = zc.zc_history_len;
2676 *off = zc.zc_history_offset;
2682 * Process the buffer of nvlists, unpacking and storing each nvlist record
2683 * into 'records'. 'leftover' is set to the number of bytes that weren't
2684 * processed as there wasn't a complete record.
2687 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
2688 nvlist_t ***records, uint_t *numrecords)
2694 while (bytes_read > sizeof (reclen)) {
2696 /* get length of packed record (stored as little endian) */
2697 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
2698 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
2700 if (bytes_read < sizeof (reclen) + reclen)
2704 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
2706 bytes_read -= sizeof (reclen) + reclen;
2707 buf += sizeof (reclen) + reclen;
2709 /* add record to nvlist array */
2711 if (ISP2(*numrecords + 1)) {
2712 *records = realloc(*records,
2713 *numrecords * 2 * sizeof (nvlist_t *));
2715 (*records)[*numrecords - 1] = nv;
2718 *leftover = bytes_read;
2722 #define HIS_BUF_LEN (128*1024)
2725 * Retrieve the command history of a pool.
2728 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
2730 char buf[HIS_BUF_LEN];
2732 nvlist_t **records = NULL;
2733 uint_t numrecords = 0;
2737 uint64_t bytes_read = sizeof (buf);
2740 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
2743 /* if nothing else was read in, we're at EOF, just return */
2747 if ((err = zpool_history_unpack(buf, bytes_read,
2748 &leftover, &records, &numrecords)) != 0)
2756 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
2757 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
2758 records, numrecords) == 0);
2760 for (i = 0; i < numrecords; i++)
2761 nvlist_free(records[i]);
2768 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
2769 char *pathname, size_t len)
2771 zfs_cmd_t zc = { 0 };
2772 boolean_t mounted = B_FALSE;
2773 char *mntpnt = NULL;
2774 char dsname[MAXNAMELEN];
2777 /* special case for the MOS */
2778 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
2782 /* get the dataset's name */
2783 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2785 if (ioctl(zhp->zpool_hdl->libzfs_fd,
2786 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
2787 /* just write out a path of two object numbers */
2788 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
2792 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
2794 /* find out if the dataset is mounted */
2795 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
2797 /* get the corrupted object's path */
2798 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
2800 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
2803 (void) snprintf(pathname, len, "%s%s", mntpnt,
2806 (void) snprintf(pathname, len, "%s:%s",
2807 dsname, zc.zc_value);
2810 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
2815 #define RDISK_ROOT "/dev/rdsk"
2816 #define BACKUP_SLICE "s2"
2818 * Don't start the slice at the default block of 34; many storage
2819 * devices will use a stripe width of 128k, so start there instead.
2821 #define NEW_START_BLOCK 256
2825 * Read the EFI label from the config, if a label does not exist then
2826 * pass back the error to the caller. If the caller has passed a non-NULL
2827 * diskaddr argument then we set it to the starting address of the EFI
2831 read_efi_label(nvlist_t *config, diskaddr_t *sb)
2835 char diskname[MAXPATHLEN];
2838 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
2841 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
2842 strrchr(path, '/'));
2843 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
2844 struct dk_gpt *vtoc;
2846 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
2848 *sb = vtoc->efi_parts[0].p_start;
2857 * determine where a partition starts on a disk in the current
2861 find_start_block(nvlist_t *config)
2865 diskaddr_t sb = MAXOFFSET_T;
2868 if (nvlist_lookup_nvlist_array(config,
2869 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
2870 if (nvlist_lookup_uint64(config,
2871 ZPOOL_CONFIG_WHOLE_DISK,
2872 &wholedisk) != 0 || !wholedisk) {
2873 return (MAXOFFSET_T);
2875 if (read_efi_label(config, &sb) < 0)
2880 for (c = 0; c < children; c++) {
2881 sb = find_start_block(child[c]);
2882 if (sb != MAXOFFSET_T) {
2886 return (MAXOFFSET_T);
2891 * Label an individual disk. The name provided is the short name,
2892 * stripped of any leading /dev path.
2895 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
2898 char path[MAXPATHLEN];
2899 struct dk_gpt *vtoc;
2901 size_t resv = EFI_MIN_RESV_SIZE;
2902 uint64_t slice_size;
2903 diskaddr_t start_block;
2906 /* prepare an error message just in case */
2907 (void) snprintf(errbuf, sizeof (errbuf),
2908 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
2913 if (pool_is_bootable(zhp)) {
2914 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2915 "EFI labeled devices are not supported on root "
2917 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
2920 verify(nvlist_lookup_nvlist(zhp->zpool_config,
2921 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2923 if (zhp->zpool_start_block == 0)
2924 start_block = find_start_block(nvroot);
2926 start_block = zhp->zpool_start_block;
2927 zhp->zpool_start_block = start_block;
2930 start_block = NEW_START_BLOCK;
2933 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
2936 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2938 * This shouldn't happen. We've long since verified that this
2939 * is a valid device.
2942 dgettext(TEXT_DOMAIN, "unable to open device"));
2943 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2946 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
2948 * The only way this can fail is if we run out of memory, or we
2949 * were unable to read the disk's capacity
2951 if (errno == ENOMEM)
2952 (void) no_memory(hdl);
2955 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2956 "unable to read disk capacity"), name);
2958 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2961 slice_size = vtoc->efi_last_u_lba + 1;
2962 slice_size -= EFI_MIN_RESV_SIZE;
2963 if (start_block == MAXOFFSET_T)
2964 start_block = NEW_START_BLOCK;
2965 slice_size -= start_block;
2967 vtoc->efi_parts[0].p_start = start_block;
2968 vtoc->efi_parts[0].p_size = slice_size;
2971 * Why we use V_USR: V_BACKUP confuses users, and is considered
2972 * disposable by some EFI utilities (since EFI doesn't have a backup
2973 * slice). V_UNASSIGNED is supposed to be used only for zero size
2974 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
2975 * etc. were all pretty specific. V_USR is as close to reality as we
2976 * can get, in the absence of V_OTHER.
2978 vtoc->efi_parts[0].p_tag = V_USR;
2979 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
2981 vtoc->efi_parts[8].p_start = slice_size + start_block;
2982 vtoc->efi_parts[8].p_size = resv;
2983 vtoc->efi_parts[8].p_tag = V_RESERVED;
2985 if (efi_write(fd, vtoc) != 0) {
2987 * Some block drivers (like pcata) may not support EFI
2988 * GPT labels. Print out a helpful error message dir-
2989 * ecting the user to manually label the disk and give
2995 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2996 "try using fdisk(1M) and then provide a specific slice"));
2997 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3007 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
3013 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
3014 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
3015 strcmp(type, VDEV_TYPE_FILE) == 0 ||
3016 strcmp(type, VDEV_TYPE_LOG) == 0 ||
3017 strcmp(type, VDEV_TYPE_MISSING) == 0) {
3018 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3019 "vdev type '%s' is not supported"), type);
3020 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
3023 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
3024 &child, &children) == 0) {
3025 for (c = 0; c < children; c++) {
3026 if (!supported_dump_vdev_type(hdl, child[c], errbuf))
3034 * check if this zvol is allowable for use as a dump device; zero if
3035 * it is, > 0 if it isn't, < 0 if it isn't a zvol
3038 zvol_check_dump_config(char *arg)
3040 zpool_handle_t *zhp = NULL;
3041 nvlist_t *config, *nvroot;
3045 libzfs_handle_t *hdl;
3047 char poolname[ZPOOL_MAXNAMELEN];
3048 int pathlen = strlen(ZVOL_FULL_DEV_DIR);
3051 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
3055 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3056 "dump is not supported on device '%s'"), arg);
3058 if ((hdl = libzfs_init()) == NULL)
3060 libzfs_print_on_error(hdl, B_TRUE);
3062 volname = arg + pathlen;
3064 /* check the configuration of the pool */
3065 if ((p = strchr(volname, '/')) == NULL) {
3066 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3067 "malformed dataset name"));
3068 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3070 } else if (p - volname >= ZFS_MAXNAMELEN) {
3071 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3072 "dataset name is too long"));
3073 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
3076 (void) strncpy(poolname, volname, p - volname);
3077 poolname[p - volname] = '\0';
3080 if ((zhp = zpool_open(hdl, poolname)) == NULL) {
3081 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3082 "could not open pool '%s'"), poolname);
3083 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
3086 config = zpool_get_config(zhp, NULL);
3087 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3089 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3090 "could not obtain vdev configuration for '%s'"), poolname);
3091 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
3095 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3096 &top, &toplevels) == 0);
3097 if (toplevels != 1) {
3098 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3099 "'%s' has multiple top level vdevs"), poolname);
3100 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
3104 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {