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);
52 #if defined(__i386) || defined(__amd64)
53 #define BOOTCMD "installgrub(1M)"
55 #define BOOTCMD "installboot(1M)"
60 * ====================================================================
61 * zpool property functions
62 * ====================================================================
66 zpool_get_all_props(zpool_handle_t *zhp)
69 libzfs_handle_t *hdl = zhp->zpool_hdl;
71 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
73 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
76 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
77 if (errno == ENOMEM) {
78 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
79 zcmd_free_nvlists(&zc);
83 zcmd_free_nvlists(&zc);
88 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
89 zcmd_free_nvlists(&zc);
93 zcmd_free_nvlists(&zc);
99 zpool_props_refresh(zpool_handle_t *zhp)
103 old_props = zhp->zpool_props;
105 if (zpool_get_all_props(zhp) != 0)
108 nvlist_free(old_props);
113 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
119 zprop_source_t source;
121 nvl = zhp->zpool_props;
122 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
123 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
125 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
127 source = ZPROP_SRC_DEFAULT;
128 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
139 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
143 zprop_source_t source;
145 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
147 * zpool_get_all_props() has most likely failed because
148 * the pool is faulted, but if all we need is the top level
149 * vdev's guid then get it from the zhp config nvlist.
151 if ((prop == ZPOOL_PROP_GUID) &&
152 (nvlist_lookup_nvlist(zhp->zpool_config,
153 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
154 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
158 return (zpool_prop_default_numeric(prop));
161 nvl = zhp->zpool_props;
162 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
163 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
165 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
167 source = ZPROP_SRC_DEFAULT;
168 value = zpool_prop_default_numeric(prop);
178 * Map VDEV STATE to printed strings.
181 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
184 case VDEV_STATE_CLOSED:
185 case VDEV_STATE_OFFLINE:
186 return (gettext("OFFLINE"));
187 case VDEV_STATE_REMOVED:
188 return (gettext("REMOVED"));
189 case VDEV_STATE_CANT_OPEN:
190 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
191 return (gettext("FAULTED"));
193 return (gettext("UNAVAIL"));
194 case VDEV_STATE_FAULTED:
195 return (gettext("FAULTED"));
196 case VDEV_STATE_DEGRADED:
197 return (gettext("DEGRADED"));
198 case VDEV_STATE_HEALTHY:
199 return (gettext("ONLINE"));
202 return (gettext("UNKNOWN"));
206 * Get a zpool property value for 'prop' and return the value in
207 * a pre-allocated buffer.
210 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
211 zprop_source_t *srctype)
215 zprop_source_t src = ZPROP_SRC_NONE;
220 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
222 case ZPOOL_PROP_NAME:
223 (void) strlcpy(buf, zpool_get_name(zhp), len);
226 case ZPOOL_PROP_HEALTH:
227 (void) strlcpy(buf, "FAULTED", len);
230 case ZPOOL_PROP_GUID:
231 intval = zpool_get_prop_int(zhp, prop, &src);
232 (void) snprintf(buf, len, "%llu", intval);
235 case ZPOOL_PROP_ALTROOT:
236 case ZPOOL_PROP_CACHEFILE:
237 if (zhp->zpool_props != NULL ||
238 zpool_get_all_props(zhp) == 0) {
240 zpool_get_prop_string(zhp, prop, &src),
248 (void) strlcpy(buf, "-", len);
257 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
258 prop != ZPOOL_PROP_NAME)
261 switch (zpool_prop_get_type(prop)) {
262 case PROP_TYPE_STRING:
263 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
267 case PROP_TYPE_NUMBER:
268 intval = zpool_get_prop_int(zhp, prop, &src);
271 case ZPOOL_PROP_SIZE:
272 case ZPOOL_PROP_USED:
273 case ZPOOL_PROP_AVAILABLE:
274 (void) zfs_nicenum(intval, buf, len);
277 case ZPOOL_PROP_CAPACITY:
278 (void) snprintf(buf, len, "%llu%%",
279 (u_longlong_t)intval);
282 case ZPOOL_PROP_HEALTH:
283 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
284 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
285 verify(nvlist_lookup_uint64_array(nvroot,
286 ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
288 (void) strlcpy(buf, zpool_state_to_name(intval,
292 (void) snprintf(buf, len, "%llu", intval);
296 case PROP_TYPE_INDEX:
297 intval = zpool_get_prop_int(zhp, prop, &src);
298 if (zpool_prop_index_to_string(prop, intval, &strval)
301 (void) strlcpy(buf, strval, len);
315 pool_is_bootable(zpool_handle_t *zhp)
317 char bootfs[ZPOOL_MAXNAMELEN];
319 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
320 sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
321 sizeof (bootfs)) != 0);
326 * Check if the bootfs name has the same pool name as it is set to.
327 * Assuming bootfs is a valid dataset name.
330 bootfs_name_valid(const char *pool, char *bootfs)
332 int len = strlen(pool);
334 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
337 if (strncmp(pool, bootfs, len) == 0 &&
338 (bootfs[len] == '/' || bootfs[len] == '\0'))
345 * Inspect the configuration to determine if any of the devices contain
349 pool_uses_efi(nvlist_t *config)
355 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
356 &child, &children) != 0)
357 return (read_efi_label(config, NULL) >= 0);
359 for (c = 0; c < children; c++) {
360 if (pool_uses_efi(child[c]))
368 * Given an nvlist of zpool properties to be set, validate that they are
369 * correct, and parse any numeric properties (index, boolean, etc) if they are
370 * specified as strings.
373 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
374 nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf)
382 struct stat64 statbuf;
386 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
387 (void) no_memory(hdl);
392 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
393 const char *propname = nvpair_name(elem);
396 * Make sure this property is valid and applies to this type.
398 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
399 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
400 "invalid property '%s'"), propname);
401 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
405 if (zpool_prop_readonly(prop)) {
406 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
407 "is readonly"), propname);
408 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
412 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
413 &strval, &intval, errbuf) != 0)
417 * Perform additional checking for specific properties.
420 case ZPOOL_PROP_VERSION:
421 if (intval < version || intval > SPA_VERSION) {
422 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
423 "property '%s' number %d is invalid."),
425 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
430 case ZPOOL_PROP_BOOTFS:
431 if (create_or_import) {
432 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
433 "property '%s' cannot be set at creation "
434 "or import time"), propname);
435 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
439 if (version < SPA_VERSION_BOOTFS) {
440 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
441 "pool must be upgraded to support "
442 "'%s' property"), propname);
443 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
448 * bootfs property value has to be a dataset name and
449 * the dataset has to be in the same pool as it sets to.
451 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
453 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
454 "is an invalid name"), strval);
455 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
459 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
460 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
461 "could not open pool '%s'"), poolname);
462 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
465 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
466 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
470 * bootfs property cannot be set on a disk which has
473 if (pool_uses_efi(nvroot)) {
474 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
475 "property '%s' not supported on "
476 "EFI labeled devices"), propname);
477 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
485 case ZPOOL_PROP_ALTROOT:
486 if (!create_or_import) {
487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
488 "property '%s' can only be set during pool "
489 "creation or import"), propname);
490 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
494 if (strval[0] != '/') {
495 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
496 "bad alternate root '%s'"), strval);
497 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
502 case ZPOOL_PROP_CACHEFILE:
503 if (strval[0] == '\0')
506 if (strcmp(strval, "none") == 0)
509 if (strval[0] != '/') {
510 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
511 "property '%s' must be empty, an "
512 "absolute path, or 'none'"), propname);
513 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
517 slash = strrchr(strval, '/');
519 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
520 strcmp(slash, "/..") == 0) {
521 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
522 "'%s' is not a valid file"), strval);
523 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
529 if (strval[0] != '\0' &&
530 (stat64(strval, &statbuf) != 0 ||
531 !S_ISDIR(statbuf.st_mode))) {
532 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
533 "'%s' is not a valid directory"),
535 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
546 nvlist_free(retprops);
551 * Set zpool property : propname=propval.
554 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
556 zfs_cmd_t zc = { 0 };
559 nvlist_t *nvl = NULL;
563 (void) snprintf(errbuf, sizeof (errbuf),
564 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
567 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
568 return (no_memory(zhp->zpool_hdl));
570 if (nvlist_add_string(nvl, propname, propval) != 0) {
572 return (no_memory(zhp->zpool_hdl));
575 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
576 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
577 zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) {
586 * Execute the corresponding ioctl() to set this property.
588 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
590 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
595 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
597 zcmd_free_nvlists(&zc);
601 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
603 (void) zpool_props_refresh(zhp);
609 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
611 libzfs_handle_t *hdl = zhp->zpool_hdl;
613 char buf[ZFS_MAXPROPLEN];
615 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
618 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
623 if (entry->pl_prop != ZPROP_INVAL &&
624 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
626 if (strlen(buf) > entry->pl_width)
627 entry->pl_width = strlen(buf);
636 * Validate the given pool name, optionally putting an extended error message in
640 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
646 ret = pool_namecheck(pool, &why, &what);
649 * The rules for reserved pool names were extended at a later point.
650 * But we need to support users with existing pools that may now be
651 * invalid. So we only check for this expanded set of names during a
652 * create (or import), and only in userland.
654 if (ret == 0 && !isopen &&
655 (strncmp(pool, "mirror", 6) == 0 ||
656 strncmp(pool, "raidz", 5) == 0 ||
657 strncmp(pool, "spare", 5) == 0 ||
658 strcmp(pool, "log") == 0)) {
661 dgettext(TEXT_DOMAIN, "name is reserved"));
669 case NAME_ERR_TOOLONG:
671 dgettext(TEXT_DOMAIN, "name is too long"));
674 case NAME_ERR_INVALCHAR:
676 dgettext(TEXT_DOMAIN, "invalid character "
677 "'%c' in pool name"), what);
680 case NAME_ERR_NOLETTER:
681 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
682 "name must begin with a letter"));
685 case NAME_ERR_RESERVED:
686 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
687 "name is reserved"));
690 case NAME_ERR_DISKLIKE:
691 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
692 "pool name is reserved"));
695 case NAME_ERR_LEADING_SLASH:
696 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
697 "leading slash in name"));
700 case NAME_ERR_EMPTY_COMPONENT:
701 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
702 "empty component in name"));
705 case NAME_ERR_TRAILING_SLASH:
706 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
707 "trailing slash in name"));
710 case NAME_ERR_MULTIPLE_AT:
711 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
712 "multiple '@' delimiters in name"));
724 * Open a handle to the given pool, even if the pool is currently in the FAULTED
728 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
734 * Make sure the pool name is valid.
736 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
737 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
738 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
743 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
746 zhp->zpool_hdl = hdl;
747 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
749 if (zpool_refresh_stats(zhp, &missing) != 0) {
755 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
756 (void) zfs_error_fmt(hdl, EZFS_NOENT,
757 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
766 * Like the above, but silent on error. Used when iterating over pools (because
767 * the configuration cache may be out of date).
770 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
775 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
778 zhp->zpool_hdl = hdl;
779 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
781 if (zpool_refresh_stats(zhp, &missing) != 0) {
797 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
801 zpool_open(libzfs_handle_t *hdl, const char *pool)
805 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
808 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
809 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
810 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
819 * Close the handle. Simply frees the memory associated with the handle.
822 zpool_close(zpool_handle_t *zhp)
824 if (zhp->zpool_config)
825 nvlist_free(zhp->zpool_config);
826 if (zhp->zpool_old_config)
827 nvlist_free(zhp->zpool_old_config);
828 if (zhp->zpool_props)
829 nvlist_free(zhp->zpool_props);
834 * Return the name of the pool.
837 zpool_get_name(zpool_handle_t *zhp)
839 return (zhp->zpool_name);
844 * Return the state of the pool (ACTIVE or UNAVAILABLE)
847 zpool_get_state(zpool_handle_t *zhp)
849 return (zhp->zpool_state);
853 * Create the named pool, using the provided vdev list. It is assumed
854 * that the consumer has already validated the contents of the nvlist, so we
855 * don't have to worry about error semantics.
858 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
859 nvlist_t *props, nvlist_t *fsprops)
861 zfs_cmd_t zc = { 0 };
862 nvlist_t *zc_fsprops = NULL;
863 nvlist_t *zc_props = NULL;
868 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
869 "cannot create '%s'"), pool);
871 if (!zpool_name_valid(hdl, B_FALSE, pool))
872 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
874 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
878 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
879 SPA_VERSION_1, B_TRUE, msg)) == NULL) {
888 zoned = ((nvlist_lookup_string(fsprops,
889 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
890 strcmp(zonestr, "on") == 0);
892 if ((zc_fsprops = zfs_valid_proplist(hdl,
893 ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
897 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
900 if (nvlist_add_nvlist(zc_props,
901 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
906 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
909 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
911 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
913 zcmd_free_nvlists(&zc);
914 nvlist_free(zc_props);
915 nvlist_free(zc_fsprops);
920 * This can happen if the user has specified the same
921 * device multiple times. We can't reliably detect this
922 * until we try to add it and see we already have a
925 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
926 "one or more vdevs refer to the same device"));
927 return (zfs_error(hdl, EZFS_BADDEV, msg));
931 * This occurs when one of the devices is below
932 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
933 * device was the problem device since there's no
934 * reliable way to determine device size from userland.
939 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
941 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
942 "one or more devices is less than the "
943 "minimum size (%s)"), buf);
945 return (zfs_error(hdl, EZFS_BADDEV, msg));
948 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
949 "one or more devices is out of space"));
950 return (zfs_error(hdl, EZFS_BADDEV, msg));
953 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
954 "cache device must be a disk or disk slice"));
955 return (zfs_error(hdl, EZFS_BADDEV, msg));
958 return (zpool_standard_error(hdl, errno, msg));
963 * If this is an alternate root pool, then we automatically set the
964 * mountpoint of the root dataset to be '/'.
966 if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
970 verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
971 verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
978 zcmd_free_nvlists(&zc);
979 nvlist_free(zc_props);
980 nvlist_free(zc_fsprops);
985 * Destroy the given pool. It is up to the caller to ensure that there are no
986 * datasets left in the pool.
989 zpool_destroy(zpool_handle_t *zhp)
991 zfs_cmd_t zc = { 0 };
992 zfs_handle_t *zfp = NULL;
993 libzfs_handle_t *hdl = zhp->zpool_hdl;
996 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
997 (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
998 ZFS_TYPE_FILESYSTEM)) == NULL)
1001 if (zpool_remove_zvol_links(zhp) != 0)
1004 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1006 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1007 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1008 "cannot destroy '%s'"), zhp->zpool_name);
1010 if (errno == EROFS) {
1011 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1012 "one or more devices is read only"));
1013 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1015 (void) zpool_standard_error(hdl, errno, msg);
1024 remove_mountpoint(zfp);
1032 * Add the given vdevs to the pool. The caller must have already performed the
1033 * necessary verification to ensure that the vdev specification is well-formed.
1036 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1038 zfs_cmd_t zc = { 0 };
1040 libzfs_handle_t *hdl = zhp->zpool_hdl;
1042 nvlist_t **spares, **l2cache;
1043 uint_t nspares, nl2cache;
1045 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1046 "cannot add to '%s'"), zhp->zpool_name);
1048 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1049 SPA_VERSION_SPARES &&
1050 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1051 &spares, &nspares) == 0) {
1052 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1053 "upgraded to add hot spares"));
1054 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1057 if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1058 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1061 for (s = 0; s < nspares; s++) {
1064 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1065 &path) == 0 && pool_uses_efi(spares[s])) {
1066 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1067 "device '%s' contains an EFI label and "
1068 "cannot be used on root pools."),
1069 zpool_vdev_name(hdl, NULL, spares[s]));
1070 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1075 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1076 SPA_VERSION_L2CACHE &&
1077 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1078 &l2cache, &nl2cache) == 0) {
1079 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1080 "upgraded to add cache devices"));
1081 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1084 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1086 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1088 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1092 * This can happen if the user has specified the same
1093 * device multiple times. We can't reliably detect this
1094 * until we try to add it and see we already have a
1097 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1098 "one or more vdevs refer to the same device"));
1099 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1104 * This occurrs when one of the devices is below
1105 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1106 * device was the problem device since there's no
1107 * reliable way to determine device size from userland.
1112 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1114 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1115 "device is less than the minimum "
1118 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1122 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1123 "pool must be upgraded to add these vdevs"));
1124 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1128 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1129 "root pool can not have multiple vdevs"
1130 " or separate logs"));
1131 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1135 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1136 "cache device must be a disk or disk slice"));
1137 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1141 (void) zpool_standard_error(hdl, errno, msg);
1149 zcmd_free_nvlists(&zc);
1155 * Exports the pool from the system. The caller must ensure that there are no
1156 * mounted datasets in the pool.
1159 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1161 zfs_cmd_t zc = { 0 };
1164 if (zpool_remove_zvol_links(zhp) != 0)
1167 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1168 "cannot export '%s'"), zhp->zpool_name);
1170 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1171 zc.zc_cookie = force;
1172 zc.zc_guid = hardforce;
1174 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1177 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1178 "use '-f' to override the following errors:\n"
1179 "'%s' has an active shared spare which could be"
1180 " used by other pools once '%s' is exported."),
1181 zhp->zpool_name, zhp->zpool_name);
1182 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1185 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1194 zpool_export(zpool_handle_t *zhp, boolean_t force)
1196 return (zpool_export_common(zhp, force, B_FALSE));
1200 zpool_export_force(zpool_handle_t *zhp)
1202 return (zpool_export_common(zhp, B_TRUE, B_TRUE));
1206 * zpool_import() is a contracted interface. Should be kept the same
1209 * Applications should use zpool_import_props() to import a pool with
1210 * new properties value to be set.
1213 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1216 nvlist_t *props = NULL;
1219 if (altroot != NULL) {
1220 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1221 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1222 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1226 if (nvlist_add_string(props,
1227 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1228 nvlist_add_string(props,
1229 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1231 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1232 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1237 ret = zpool_import_props(hdl, config, newname, props, B_FALSE);
1244 * Import the given pool using the known configuration and a list of
1245 * properties to be set. The configuration should have come from
1246 * zpool_find_import(). The 'newname' parameters control whether the pool
1247 * is imported with a different name.
1250 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1251 nvlist_t *props, boolean_t importfaulted)
1253 zfs_cmd_t zc = { 0 };
1259 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1262 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1263 "cannot import pool '%s'"), origname);
1265 if (newname != NULL) {
1266 if (!zpool_name_valid(hdl, B_FALSE, newname))
1267 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1268 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1270 thename = (char *)newname;
1278 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1281 if ((props = zpool_valid_proplist(hdl, origname,
1282 props, version, B_TRUE, errbuf)) == NULL) {
1284 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1290 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1292 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1295 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1300 zc.zc_cookie = (uint64_t)importfaulted;
1302 if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) {
1304 if (newname == NULL)
1305 (void) snprintf(desc, sizeof (desc),
1306 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1309 (void) snprintf(desc, sizeof (desc),
1310 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1316 * Unsupported version.
1318 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1322 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1326 (void) zpool_standard_error(hdl, errno, desc);
1331 zpool_handle_t *zhp;
1334 * This should never fail, but play it safe anyway.
1336 if (zpool_open_silent(hdl, thename, &zhp) != 0) {
1338 } else if (zhp != NULL) {
1339 ret = zpool_create_zvol_links(zhp);
1345 zcmd_free_nvlists(&zc);
1355 zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type)
1357 zfs_cmd_t zc = { 0 };
1359 libzfs_handle_t *hdl = zhp->zpool_hdl;
1361 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1362 zc.zc_cookie = type;
1364 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0)
1367 (void) snprintf(msg, sizeof (msg),
1368 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1371 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1373 return (zpool_standard_error(hdl, errno, msg));
1377 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1378 * spare; but FALSE if its an INUSE spare.
1381 vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid,
1382 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
1386 uint64_t theguid, present;
1388 uint64_t wholedisk = 0;
1392 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0);
1394 if (search == NULL &&
1395 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) {
1397 * If the device has never been present since import, the only
1398 * reliable way to match the vdev is by GUID.
1400 if (theguid == guid)
1402 } else if (search != NULL &&
1403 nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1404 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1408 * For whole disks, the internal path has 's0', but the
1409 * path passed in by the user doesn't.
1411 if (strlen(search) == strlen(path) - 2 &&
1412 strncmp(search, path, strlen(search)) == 0)
1414 } else if (strcmp(search, path) == 0) {
1419 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1420 &child, &children) != 0)
1423 for (c = 0; c < children; c++) {
1424 if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1425 avail_spare, l2cache, NULL)) != NULL) {
1427 * The 'is_log' value is only set for the toplevel
1428 * vdev, not the leaf vdevs. So we always lookup the
1429 * log device from the root of the vdev tree (where
1430 * 'log' is non-NULL).
1433 nvlist_lookup_uint64(child[c],
1434 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
1442 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1443 &child, &children) == 0) {
1444 for (c = 0; c < children; c++) {
1445 if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1446 avail_spare, l2cache, NULL)) != NULL) {
1447 *avail_spare = B_TRUE;
1453 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1454 &child, &children) == 0) {
1455 for (c = 0; c < children; c++) {
1456 if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1457 avail_spare, l2cache, NULL)) != NULL) {
1468 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
1469 boolean_t *l2cache, boolean_t *log)
1471 char buf[MAXPATHLEN];
1477 guid = strtoull(path, &end, 10);
1478 if (guid != 0 && *end == '\0') {
1480 } else if (path[0] != '/') {
1481 (void) snprintf(buf, sizeof (buf), "%s%s", _PATH_DEV, path);
1487 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1490 *avail_spare = B_FALSE;
1494 return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare,
1499 vdev_online(nvlist_t *nv)
1503 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
1504 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
1505 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
1512 * Get phys_path for a root pool
1513 * Return 0 on success; non-zeron on failure.
1516 zpool_get_physpath(zpool_handle_t *zhp, char *physpath)
1518 nvlist_t *vdev_root;
1524 * Make sure this is a root pool, as phys_path doesn't mean
1525 * anything to a non-root pool.
1527 if (!pool_is_bootable(zhp))
1530 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1531 ZPOOL_CONFIG_VDEV_TREE, &vdev_root) == 0);
1533 if (nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
1534 &child, &count) != 0)
1537 for (i = 0; i < count; i++) {
1544 if (nvlist_lookup_string(child[i], ZPOOL_CONFIG_TYPE, &type)
1548 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
1549 if (!vdev_online(child[i]))
1551 verify(nvlist_lookup_string(child[i],
1552 ZPOOL_CONFIG_PHYS_PATH, &tmppath) == 0);
1553 (void) strncpy(physpath, tmppath, strlen(tmppath));
1554 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0) {
1555 if (nvlist_lookup_nvlist_array(child[i],
1556 ZPOOL_CONFIG_CHILDREN, &child2, &count2) != 0)
1559 for (j = 0; j < count2; j++) {
1560 if (!vdev_online(child2[j]))
1562 if (nvlist_lookup_string(child2[j],
1563 ZPOOL_CONFIG_PHYS_PATH, &tmppath) != 0)
1566 if ((strlen(physpath) + strlen(tmppath)) >
1570 if (strlen(physpath) == 0) {
1571 (void) strncpy(physpath, tmppath,
1574 (void) strcat(physpath, " ");
1575 (void) strcat(physpath, tmppath);
1587 * Returns TRUE if the given guid corresponds to the given type.
1588 * This is used to check for hot spares (INUSE or not), and level 2 cache
1592 is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type)
1594 uint64_t target_guid;
1600 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1602 if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) {
1603 for (i = 0; i < count; i++) {
1604 verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID,
1605 &target_guid) == 0);
1606 if (guid == target_guid)
1615 * Bring the specified vdev online. The 'flags' parameter is a set of the
1616 * ZFS_ONLINE_* flags.
1619 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
1620 vdev_state_t *newstate)
1622 zfs_cmd_t zc = { 0 };
1625 boolean_t avail_spare, l2cache;
1626 libzfs_handle_t *hdl = zhp->zpool_hdl;
1628 (void) snprintf(msg, sizeof (msg),
1629 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
1631 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1632 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1634 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1636 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1639 is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
1640 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1642 zc.zc_cookie = VDEV_STATE_ONLINE;
1645 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0)
1646 return (zpool_standard_error(hdl, errno, msg));
1648 *newstate = zc.zc_cookie;
1653 * Take the specified vdev offline
1656 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
1658 zfs_cmd_t zc = { 0 };
1661 boolean_t avail_spare, l2cache;
1662 libzfs_handle_t *hdl = zhp->zpool_hdl;
1664 (void) snprintf(msg, sizeof (msg),
1665 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
1667 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1668 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1670 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1672 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1675 is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
1676 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1678 zc.zc_cookie = VDEV_STATE_OFFLINE;
1679 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
1681 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1688 * There are no other replicas of this device.
1690 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
1693 return (zpool_standard_error(hdl, errno, msg));
1698 * Mark the given vdev faulted.
1701 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid)
1703 zfs_cmd_t zc = { 0 };
1705 libzfs_handle_t *hdl = zhp->zpool_hdl;
1707 (void) snprintf(msg, sizeof (msg),
1708 dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
1710 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1712 zc.zc_cookie = VDEV_STATE_FAULTED;
1714 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1721 * There are no other replicas of this device.
1723 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
1727 * The log device has unplayed logs
1729 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
1732 return (zpool_standard_error(hdl, errno, msg));
1738 * Mark the given vdev degraded.
1741 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid)
1743 zfs_cmd_t zc = { 0 };
1745 libzfs_handle_t *hdl = zhp->zpool_hdl;
1747 (void) snprintf(msg, sizeof (msg),
1748 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
1750 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1752 zc.zc_cookie = VDEV_STATE_DEGRADED;
1754 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1757 return (zpool_standard_error(hdl, errno, msg));
1761 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
1765 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
1771 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
1773 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
1776 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
1777 children == 2 && child[which] == tgt)
1780 for (c = 0; c < children; c++)
1781 if (is_replacing_spare(child[c], tgt, which))
1789 * Attach new_disk (fully described by nvroot) to old_disk.
1790 * If 'replacing' is specified, the new disk will replace the old one.
1793 zpool_vdev_attach(zpool_handle_t *zhp,
1794 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
1796 zfs_cmd_t zc = { 0 };
1800 boolean_t avail_spare, l2cache, islog;
1802 char *path, *newname;
1805 nvlist_t *config_root;
1806 libzfs_handle_t *hdl = zhp->zpool_hdl;
1807 boolean_t rootpool = pool_is_bootable(zhp);
1810 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1811 "cannot replace %s with %s"), old_disk, new_disk);
1813 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1814 "cannot attach %s to %s"), new_disk, old_disk);
1817 * If this is a root pool, make sure that we're not attaching an
1818 * EFI labeled device.
1820 if (rootpool && pool_uses_efi(nvroot)) {
1821 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1822 "EFI labeled devices are not supported on root pools."));
1823 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1826 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1827 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
1829 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1832 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1835 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
1837 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1838 zc.zc_cookie = replacing;
1840 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1841 &child, &children) != 0 || children != 1) {
1842 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1843 "new device must be a single disk"));
1844 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
1847 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
1848 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
1850 if ((newname = zpool_vdev_name(NULL, NULL, child[0])) == NULL)
1854 * If the target is a hot spare that has been swapped in, we can only
1855 * replace it with another hot spare.
1858 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
1859 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
1860 NULL) == NULL || !avail_spare) &&
1861 is_replacing_spare(config_root, tgt, 1)) {
1862 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1863 "can only be replaced by another hot spare"));
1865 return (zfs_error(hdl, EZFS_BADTARGET, msg));
1869 * If we are attempting to replace a spare, it canot be applied to an
1870 * already spared device.
1873 nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
1874 zpool_find_vdev(zhp, newname, &avail_spare,
1875 &l2cache, NULL) != NULL && avail_spare &&
1876 is_replacing_spare(config_root, tgt, 0)) {
1877 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1878 "device has already been replaced with a spare"));
1880 return (zfs_error(hdl, EZFS_BADTARGET, msg));
1885 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1888 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc);
1890 zcmd_free_nvlists(&zc);
1894 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "If "
1895 "you boot from pool '%s', you may need to update\n"
1896 "boot code on newly attached disk '%s'.\n\n"
1897 "Assuming you use GPT partitioning and 'da0' is "
1898 "your new boot disk\n"
1899 "you may use the following command:\n\n"
1900 "\tgpart bootcode -b /boot/pmbr -p "
1901 "/boot/gptzfsboot -i 1 da0\n\n"),
1902 zhp->zpool_name, new_disk);
1910 * Can't attach to or replace this type of vdev.
1914 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1915 "cannot replace a log with a spare"));
1917 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1918 "cannot replace a replacing device"));
1920 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1921 "can only attach to mirrors and top-level "
1924 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
1929 * The new device must be a single disk.
1931 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1932 "new device must be a single disk"));
1933 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
1937 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
1939 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1944 * The new device is too small.
1946 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1947 "device is too small"));
1948 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1953 * The new device has a different alignment requirement.
1955 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1956 "devices have different sector alignment"));
1957 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1962 * The resulting top-level vdev spec won't fit in the label.
1964 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
1968 (void) zpool_standard_error(hdl, errno, msg);
1975 * Detach the specified device.
1978 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
1980 zfs_cmd_t zc = { 0 };
1983 boolean_t avail_spare, l2cache;
1984 libzfs_handle_t *hdl = zhp->zpool_hdl;
1986 (void) snprintf(msg, sizeof (msg),
1987 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
1989 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1990 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1992 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1995 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1998 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2000 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2002 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2009 * Can't detach from this type of vdev.
2011 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2012 "applicable to mirror and replacing vdevs"));
2013 (void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg);
2018 * There are no other replicas of this device.
2020 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2024 (void) zpool_standard_error(hdl, errno, msg);
2031 * Remove the given device. Currently, this is supported only for hot spares
2032 * and level 2 cache devices.
2035 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
2037 zfs_cmd_t zc = { 0 };
2040 boolean_t avail_spare, l2cache;
2041 libzfs_handle_t *hdl = zhp->zpool_hdl;
2043 (void) snprintf(msg, sizeof (msg),
2044 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
2046 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2047 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2049 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2051 if (!avail_spare && !l2cache) {
2052 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2053 "only inactive hot spares or cache devices "
2055 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2058 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2060 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
2063 return (zpool_standard_error(hdl, errno, msg));
2067 * Clear the errors for the pool, or the particular device if specified.
2070 zpool_clear(zpool_handle_t *zhp, const char *path)
2072 zfs_cmd_t zc = { 0 };
2075 boolean_t avail_spare, l2cache;
2076 libzfs_handle_t *hdl = zhp->zpool_hdl;
2079 (void) snprintf(msg, sizeof (msg),
2080 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2083 (void) snprintf(msg, sizeof (msg),
2084 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2087 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2089 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
2090 &l2cache, NULL)) == 0)
2091 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2094 * Don't allow error clearing for hot spares. Do allow
2095 * error clearing for l2cache devices.
2098 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2100 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
2104 if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
2107 return (zpool_standard_error(hdl, errno, msg));
2111 * Similar to zpool_clear(), but takes a GUID (used by fmd).
2114 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
2116 zfs_cmd_t zc = { 0 };
2118 libzfs_handle_t *hdl = zhp->zpool_hdl;
2120 (void) snprintf(msg, sizeof (msg),
2121 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
2124 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2127 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
2130 return (zpool_standard_error(hdl, errno, msg));
2134 * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool>
2138 zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *),
2141 libzfs_handle_t *hdl = zhp->zpool_hdl;
2142 char (*paths)[MAXPATHLEN];
2143 char path[MAXPATHLEN];
2145 int curr, fd, base, ret = 0;
2150 if ((base = open(ZVOL_FULL_DEV_DIR, O_RDONLY)) < 0)
2151 return (errno == ENOENT ? 0 : -1);
2153 snprintf(path, sizeof(path), "%s/%s", ZVOL_FULL_DEV_DIR,
2155 if (stat(path, &st) != 0) {
2158 return (err == ENOENT ? 0 : -1);
2162 * Oddly this wasn't a directory -- ignore that failure since we
2163 * know there are no links lower in the (non-existant) hierarchy.
2165 if (!S_ISDIR(st.st_mode)) {
2170 if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) {
2175 (void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0]));
2179 snprintf(path, sizeof(path), "%s/%s", ZVOL_FULL_DEV_DIR,
2181 if (lstat(path, &st) != 0)
2184 if (S_ISDIR(st.st_mode)) {
2185 if ((dirp = opendir(path)) == NULL) {
2189 while ((dp = readdir(dirp)) != NULL) {
2190 if (dp->d_name[0] == '.')
2193 if (curr + 1 == size) {
2194 paths = zfs_realloc(hdl, paths,
2195 size * sizeof (paths[0]),
2196 size * 2 * sizeof (paths[0]));
2197 if (paths == NULL) {
2198 (void) closedir(dirp);
2205 (void) strlcpy(paths[curr + 1], paths[curr],
2206 sizeof (paths[curr + 1]));
2207 (void) strlcat(paths[curr], "/",
2208 sizeof (paths[curr]));
2209 (void) strlcat(paths[curr], dp->d_name,
2210 sizeof (paths[curr]));
2214 (void) closedir(dirp);
2217 if ((ret = cb(paths[curr], data)) != 0)
2235 typedef struct zvol_cb {
2236 zpool_handle_t *zcb_pool;
2237 boolean_t zcb_create;
2242 do_zvol_create(zfs_handle_t *zhp, void *data)
2246 if (ZFS_IS_VOLUME(zhp)) {
2247 (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
2248 ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL);
2252 ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL);
2260 * Iterate over all zvols in the pool and make any necessary minor nodes.
2263 zpool_create_zvol_links(zpool_handle_t *zhp)
2269 * If the pool is unavailable, just return success.
2271 if ((zfp = make_dataset_handle(zhp->zpool_hdl,
2272 zhp->zpool_name)) == NULL)
2275 ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL);
2282 do_zvol_remove(const char *dataset, void *data)
2284 zpool_handle_t *zhp = data;
2286 return (zvol_remove_link(zhp->zpool_hdl, dataset));
2290 * Iterate over all zvols in the pool and remove any minor nodes. We iterate
2291 * by examining the /dev links so that a corrupted pool doesn't impede this
2295 zpool_remove_zvol_links(zpool_handle_t *zhp)
2297 return (zpool_iter_zvol(zhp, do_zvol_remove, zhp));
2301 * Convert from a devid string to a path.
2304 devid_to_path(char *devid_str)
2309 devid_nmlist_t *list = NULL;
2312 if (devid_str_decode(devid_str, &devid, &minor) != 0)
2315 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
2317 devid_str_free(minor);
2323 if ((path = strdup(list[0].devname)) == NULL)
2326 devid_free_nmlist(list);
2332 * Convert from a path to a devid string.
2335 path_to_devid(const char *path)
2341 if ((fd = open(path, O_RDONLY)) < 0)
2346 if (devid_get(fd, &devid) == 0) {
2347 if (devid_get_minor_name(fd, &minor) == 0)
2348 ret = devid_str_encode(devid, minor);
2350 devid_str_free(minor);
2359 * Issue the necessary ioctl() to update the stored path value for the vdev. We
2360 * ignore any failure here, since a common case is for an unprivileged user to
2361 * type 'zpool status', and we'll display the correct information anyway.
2364 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
2366 zfs_cmd_t zc = { 0 };
2368 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2369 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
2370 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2373 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
2377 * Given a vdev, return the name to display in iostat. If the vdev has a path,
2378 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
2379 * We also check if this is a whole disk, in which case we strip off the
2380 * trailing 's0' slice name.
2382 * This routine is also responsible for identifying when disks have been
2383 * reconfigured in a new location. The kernel will have opened the device by
2384 * devid, but the path will still refer to the old location. To catch this, we
2385 * first do a path -> devid translation (which is fast for the common case). If
2386 * the devid matches, we're done. If not, we do a reverse devid -> path
2387 * translation and issue the appropriate ioctl() to update the path of the vdev.
2388 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
2392 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv)
2400 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
2402 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2404 (void) snprintf(buf, sizeof (buf), "%llu",
2405 (u_longlong_t)value);
2407 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
2410 * If the device is dead (faulted, offline, etc) then don't
2411 * bother opening it. Otherwise we may be forcing the user to
2412 * open a misbehaving device, which can have undesirable
2415 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
2416 (uint64_t **)&vs, &vsc) != 0 ||
2417 vs->vs_state >= VDEV_STATE_DEGRADED) &&
2419 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
2421 * Determine if the current path is correct.
2423 char *newdevid = path_to_devid(path);
2425 if (newdevid == NULL ||
2426 strcmp(devid, newdevid) != 0) {
2429 if ((newpath = devid_to_path(devid)) != NULL) {
2431 * Update the path appropriately.
2433 set_path(zhp, nv, newpath);
2434 if (nvlist_add_string(nv,
2435 ZPOOL_CONFIG_PATH, newpath) == 0)
2436 verify(nvlist_lookup_string(nv,
2444 devid_str_free(newdevid);
2447 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
2448 path += sizeof(_PATH_DEV) - 1;
2450 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2451 &value) == 0 && value) {
2452 char *tmp = zfs_strdup(hdl, path);
2455 tmp[strlen(path) - 2] = '\0';
2459 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
2462 * If it's a raidz device, we need to stick in the parity level.
2464 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
2465 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
2467 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
2468 (u_longlong_t)value);
2473 return (zfs_strdup(hdl, path));
2477 zbookmark_compare(const void *a, const void *b)
2479 return (memcmp(a, b, sizeof (zbookmark_t)));
2483 * Retrieve the persistent error log, uniquify the members, and return to the
2487 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
2489 zfs_cmd_t zc = { 0 };
2491 zbookmark_t *zb = NULL;
2495 * Retrieve the raw error list from the kernel. If the number of errors
2496 * has increased, allocate more space and continue until we get the
2499 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
2503 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
2504 count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
2506 zc.zc_nvlist_dst_size = count;
2507 (void) strcpy(zc.zc_name, zhp->zpool_name);
2509 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
2511 free((void *)(uintptr_t)zc.zc_nvlist_dst);
2512 if (errno == ENOMEM) {
2513 count = zc.zc_nvlist_dst_size;
2514 if ((zc.zc_nvlist_dst = (uintptr_t)
2515 zfs_alloc(zhp->zpool_hdl, count *
2516 sizeof (zbookmark_t))) == (uintptr_t)NULL)
2527 * Sort the resulting bookmarks. This is a little confusing due to the
2528 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
2529 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
2530 * _not_ copied as part of the process. So we point the start of our
2531 * array appropriate and decrement the total number of elements.
2533 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
2534 zc.zc_nvlist_dst_size;
2535 count -= zc.zc_nvlist_dst_size;
2537 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
2539 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
2542 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
2544 for (i = 0; i < count; i++) {
2547 /* ignoring zb_blkid and zb_level for now */
2548 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
2549 zb[i-1].zb_object == zb[i].zb_object)
2552 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
2554 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
2555 zb[i].zb_objset) != 0) {
2559 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
2560 zb[i].zb_object) != 0) {
2564 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
2571 free((void *)(uintptr_t)zc.zc_nvlist_dst);
2575 free((void *)(uintptr_t)zc.zc_nvlist_dst);
2576 return (no_memory(zhp->zpool_hdl));
2580 * Upgrade a ZFS pool to the latest on-disk version.
2583 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
2585 zfs_cmd_t zc = { 0 };
2586 libzfs_handle_t *hdl = zhp->zpool_hdl;
2588 (void) strcpy(zc.zc_name, zhp->zpool_name);
2589 zc.zc_cookie = new_version;
2591 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
2592 return (zpool_standard_error_fmt(hdl, errno,
2593 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
2599 zpool_set_history_str(const char *subcommand, int argc, char **argv,
2604 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
2605 for (i = 1; i < argc; i++) {
2606 if (strlen(history_str) + 1 + strlen(argv[i]) >
2609 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
2610 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
2615 * Stage command history for logging.
2618 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
2620 if (history_str == NULL)
2623 if (strlen(history_str) > HIS_MAX_RECORD_LEN)
2626 if (hdl->libzfs_log_str != NULL)
2627 free(hdl->libzfs_log_str);
2629 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
2630 return (no_memory(hdl));
2636 * Perform ioctl to get some command history of a pool.
2638 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
2639 * logical offset of the history buffer to start reading from.
2641 * Upon return, 'off' is the next logical offset to read from and
2642 * 'len' is the actual amount of bytes read into 'buf'.
2645 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
2647 zfs_cmd_t zc = { 0 };
2648 libzfs_handle_t *hdl = zhp->zpool_hdl;
2650 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2652 zc.zc_history = (uint64_t)(uintptr_t)buf;
2653 zc.zc_history_len = *len;
2654 zc.zc_history_offset = *off;
2656 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
2659 return (zfs_error_fmt(hdl, EZFS_PERM,
2660 dgettext(TEXT_DOMAIN,
2661 "cannot show history for pool '%s'"),
2664 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
2665 dgettext(TEXT_DOMAIN, "cannot get history for pool "
2666 "'%s'"), zhp->zpool_name));
2668 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
2669 dgettext(TEXT_DOMAIN, "cannot get history for pool "
2670 "'%s', pool must be upgraded"), zhp->zpool_name));
2672 return (zpool_standard_error_fmt(hdl, errno,
2673 dgettext(TEXT_DOMAIN,
2674 "cannot get history for '%s'"), zhp->zpool_name));
2678 *len = zc.zc_history_len;
2679 *off = zc.zc_history_offset;
2685 * Process the buffer of nvlists, unpacking and storing each nvlist record
2686 * into 'records'. 'leftover' is set to the number of bytes that weren't
2687 * processed as there wasn't a complete record.
2690 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
2691 nvlist_t ***records, uint_t *numrecords)
2697 while (bytes_read > sizeof (reclen)) {
2699 /* get length of packed record (stored as little endian) */
2700 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
2701 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
2703 if (bytes_read < sizeof (reclen) + reclen)
2707 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
2709 bytes_read -= sizeof (reclen) + reclen;
2710 buf += sizeof (reclen) + reclen;
2712 /* add record to nvlist array */
2714 if (ISP2(*numrecords + 1)) {
2715 *records = realloc(*records,
2716 *numrecords * 2 * sizeof (nvlist_t *));
2718 (*records)[*numrecords - 1] = nv;
2721 *leftover = bytes_read;
2725 #define HIS_BUF_LEN (128*1024)
2728 * Retrieve the command history of a pool.
2731 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
2733 char buf[HIS_BUF_LEN];
2735 nvlist_t **records = NULL;
2736 uint_t numrecords = 0;
2740 uint64_t bytes_read = sizeof (buf);
2743 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
2746 /* if nothing else was read in, we're at EOF, just return */
2750 if ((err = zpool_history_unpack(buf, bytes_read,
2751 &leftover, &records, &numrecords)) != 0)
2759 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
2760 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
2761 records, numrecords) == 0);
2763 for (i = 0; i < numrecords; i++)
2764 nvlist_free(records[i]);
2771 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
2772 char *pathname, size_t len)
2774 zfs_cmd_t zc = { 0 };
2775 boolean_t mounted = B_FALSE;
2776 char *mntpnt = NULL;
2777 char dsname[MAXNAMELEN];
2780 /* special case for the MOS */
2781 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
2785 /* get the dataset's name */
2786 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2788 if (ioctl(zhp->zpool_hdl->libzfs_fd,
2789 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
2790 /* just write out a path of two object numbers */
2791 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
2795 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
2797 /* find out if the dataset is mounted */
2798 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
2800 /* get the corrupted object's path */
2801 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
2803 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
2806 (void) snprintf(pathname, len, "%s%s", mntpnt,
2809 (void) snprintf(pathname, len, "%s:%s",
2810 dsname, zc.zc_value);
2813 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
2818 #define RDISK_ROOT "/dev/rdsk"
2819 #define BACKUP_SLICE "s2"
2821 * Don't start the slice at the default block of 34; many storage
2822 * devices will use a stripe width of 128k, so start there instead.
2824 #define NEW_START_BLOCK 256
2828 * Read the EFI label from the config, if a label does not exist then
2829 * pass back the error to the caller. If the caller has passed a non-NULL
2830 * diskaddr argument then we set it to the starting address of the EFI
2834 read_efi_label(nvlist_t *config, diskaddr_t *sb)
2838 char diskname[MAXPATHLEN];
2841 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
2844 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
2845 strrchr(path, '/'));
2846 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
2847 struct dk_gpt *vtoc;
2849 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
2851 *sb = vtoc->efi_parts[0].p_start;
2860 * determine where a partition starts on a disk in the current
2864 find_start_block(nvlist_t *config)
2868 diskaddr_t sb = MAXOFFSET_T;
2871 if (nvlist_lookup_nvlist_array(config,
2872 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
2873 if (nvlist_lookup_uint64(config,
2874 ZPOOL_CONFIG_WHOLE_DISK,
2875 &wholedisk) != 0 || !wholedisk) {
2876 return (MAXOFFSET_T);
2878 if (read_efi_label(config, &sb) < 0)
2883 for (c = 0; c < children; c++) {
2884 sb = find_start_block(child[c]);
2885 if (sb != MAXOFFSET_T) {
2889 return (MAXOFFSET_T);
2894 * Label an individual disk. The name provided is the short name,
2895 * stripped of any leading /dev path.
2898 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
2901 char path[MAXPATHLEN];
2902 struct dk_gpt *vtoc;
2904 size_t resv = EFI_MIN_RESV_SIZE;
2905 uint64_t slice_size;
2906 diskaddr_t start_block;
2909 /* prepare an error message just in case */
2910 (void) snprintf(errbuf, sizeof (errbuf),
2911 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
2916 if (pool_is_bootable(zhp)) {
2917 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2918 "EFI labeled devices are not supported on root "
2920 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
2923 verify(nvlist_lookup_nvlist(zhp->zpool_config,
2924 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2926 if (zhp->zpool_start_block == 0)
2927 start_block = find_start_block(nvroot);
2929 start_block = zhp->zpool_start_block;
2930 zhp->zpool_start_block = start_block;
2933 start_block = NEW_START_BLOCK;
2936 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
2939 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2941 * This shouldn't happen. We've long since verified that this
2942 * is a valid device.
2945 dgettext(TEXT_DOMAIN, "unable to open device"));
2946 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2949 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
2951 * The only way this can fail is if we run out of memory, or we
2952 * were unable to read the disk's capacity
2954 if (errno == ENOMEM)
2955 (void) no_memory(hdl);
2958 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2959 "unable to read disk capacity"), name);
2961 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2964 slice_size = vtoc->efi_last_u_lba + 1;
2965 slice_size -= EFI_MIN_RESV_SIZE;
2966 if (start_block == MAXOFFSET_T)
2967 start_block = NEW_START_BLOCK;
2968 slice_size -= start_block;
2970 vtoc->efi_parts[0].p_start = start_block;
2971 vtoc->efi_parts[0].p_size = slice_size;
2974 * Why we use V_USR: V_BACKUP confuses users, and is considered
2975 * disposable by some EFI utilities (since EFI doesn't have a backup
2976 * slice). V_UNASSIGNED is supposed to be used only for zero size
2977 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
2978 * etc. were all pretty specific. V_USR is as close to reality as we
2979 * can get, in the absence of V_OTHER.
2981 vtoc->efi_parts[0].p_tag = V_USR;
2982 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
2984 vtoc->efi_parts[8].p_start = slice_size + start_block;
2985 vtoc->efi_parts[8].p_size = resv;
2986 vtoc->efi_parts[8].p_tag = V_RESERVED;
2988 if (efi_write(fd, vtoc) != 0) {
2990 * Some block drivers (like pcata) may not support EFI
2991 * GPT labels. Print out a helpful error message dir-
2992 * ecting the user to manually label the disk and give
2998 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2999 "try using fdisk(1M) and then provide a specific slice"));
3000 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3010 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
3016 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
3017 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
3018 strcmp(type, VDEV_TYPE_FILE) == 0 ||
3019 strcmp(type, VDEV_TYPE_LOG) == 0 ||
3020 strcmp(type, VDEV_TYPE_MISSING) == 0) {
3021 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3022 "vdev type '%s' is not supported"), type);
3023 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
3026 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
3027 &child, &children) == 0) {
3028 for (c = 0; c < children; c++) {
3029 if (!supported_dump_vdev_type(hdl, child[c], errbuf))
3037 * check if this zvol is allowable for use as a dump device; zero if
3038 * it is, > 0 if it isn't, < 0 if it isn't a zvol
3041 zvol_check_dump_config(char *arg)
3043 zpool_handle_t *zhp = NULL;
3044 nvlist_t *config, *nvroot;
3048 libzfs_handle_t *hdl;
3050 char poolname[ZPOOL_MAXNAMELEN];
3051 int pathlen = strlen(ZVOL_FULL_DEV_DIR);
3054 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
3058 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3059 "dump is not supported on device '%s'"), arg);
3061 if ((hdl = libzfs_init()) == NULL)
3063 libzfs_print_on_error(hdl, B_TRUE);
3065 volname = arg + pathlen;
3067 /* check the configuration of the pool */
3068 if ((p = strchr(volname, '/')) == NULL) {
3069 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3070 "malformed dataset name"));
3071 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3073 } else if (p - volname >= ZFS_MAXNAMELEN) {
3074 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3075 "dataset name is too long"));
3076 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
3079 (void) strncpy(poolname, volname, p - volname);
3080 poolname[p - volname] = '\0';
3083 if ((zhp = zpool_open(hdl, poolname)) == NULL) {
3084 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3085 "could not open pool '%s'"), poolname);
3086 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
3089 config = zpool_get_config(zhp, NULL);
3090 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3092 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3093 "could not obtain vdev configuration for '%s'"), poolname);
3094 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
3098 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3099 &top, &toplevels) == 0);
3100 if (toplevels != 1) {
3101 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3102 "'%s' has multiple top level vdevs"), poolname);
3103 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
3107 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {