4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <sys/types.h>
41 #include <sys/zfs_ioctl.h>
46 #include "zfs_namecheck.h"
48 #include "libzfs_impl.h"
50 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
53 * ====================================================================
54 * zpool property functions
55 * ====================================================================
59 zpool_get_all_props(zpool_handle_t *zhp)
62 libzfs_handle_t *hdl = zhp->zpool_hdl;
64 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
66 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
69 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
70 if (errno == ENOMEM) {
71 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
72 zcmd_free_nvlists(&zc);
76 zcmd_free_nvlists(&zc);
81 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
82 zcmd_free_nvlists(&zc);
86 zcmd_free_nvlists(&zc);
92 zpool_props_refresh(zpool_handle_t *zhp)
96 old_props = zhp->zpool_props;
98 if (zpool_get_all_props(zhp) != 0)
101 nvlist_free(old_props);
106 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
112 zprop_source_t source;
114 nvl = zhp->zpool_props;
115 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
116 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
118 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
120 source = ZPROP_SRC_DEFAULT;
121 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
132 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
136 zprop_source_t source;
138 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
140 * zpool_get_all_props() has most likely failed because
141 * the pool is faulted, but if all we need is the top level
142 * vdev's guid then get it from the zhp config nvlist.
144 if ((prop == ZPOOL_PROP_GUID) &&
145 (nvlist_lookup_nvlist(zhp->zpool_config,
146 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
147 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
151 return (zpool_prop_default_numeric(prop));
154 nvl = zhp->zpool_props;
155 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
156 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
158 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
160 source = ZPROP_SRC_DEFAULT;
161 value = zpool_prop_default_numeric(prop);
171 * Map VDEV STATE to printed strings.
174 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
177 case VDEV_STATE_CLOSED:
178 case VDEV_STATE_OFFLINE:
179 return (gettext("OFFLINE"));
180 case VDEV_STATE_REMOVED:
181 return (gettext("REMOVED"));
182 case VDEV_STATE_CANT_OPEN:
183 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
184 return (gettext("FAULTED"));
186 return (gettext("UNAVAIL"));
187 case VDEV_STATE_FAULTED:
188 return (gettext("FAULTED"));
189 case VDEV_STATE_DEGRADED:
190 return (gettext("DEGRADED"));
191 case VDEV_STATE_HEALTHY:
192 return (gettext("ONLINE"));
195 return (gettext("UNKNOWN"));
199 * Get a zpool property value for 'prop' and return the value in
200 * a pre-allocated buffer.
203 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
204 zprop_source_t *srctype)
208 zprop_source_t src = ZPROP_SRC_NONE;
213 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
214 if (prop == ZPOOL_PROP_NAME)
215 (void) strlcpy(buf, zpool_get_name(zhp), len);
216 else if (prop == ZPOOL_PROP_HEALTH)
217 (void) strlcpy(buf, "FAULTED", len);
219 (void) strlcpy(buf, "-", len);
223 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
224 prop != ZPOOL_PROP_NAME)
227 switch (zpool_prop_get_type(prop)) {
228 case PROP_TYPE_STRING:
229 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
233 case PROP_TYPE_NUMBER:
234 intval = zpool_get_prop_int(zhp, prop, &src);
237 case ZPOOL_PROP_SIZE:
238 case ZPOOL_PROP_USED:
239 case ZPOOL_PROP_AVAILABLE:
240 (void) zfs_nicenum(intval, buf, len);
243 case ZPOOL_PROP_CAPACITY:
244 (void) snprintf(buf, len, "%llu%%",
245 (u_longlong_t)intval);
248 case ZPOOL_PROP_HEALTH:
249 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
250 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
251 verify(nvlist_lookup_uint64_array(nvroot,
252 ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
254 (void) strlcpy(buf, zpool_state_to_name(intval,
258 (void) snprintf(buf, len, "%llu", intval);
262 case PROP_TYPE_INDEX:
263 intval = zpool_get_prop_int(zhp, prop, &src);
264 if (zpool_prop_index_to_string(prop, intval, &strval)
267 (void) strlcpy(buf, strval, len);
281 * Check if the bootfs name has the same pool name as it is set to.
282 * Assuming bootfs is a valid dataset name.
285 bootfs_name_valid(const char *pool, char *bootfs)
287 int len = strlen(pool);
289 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
292 if (strncmp(pool, bootfs, len) == 0 &&
293 (bootfs[len] == '/' || bootfs[len] == '\0'))
301 * Inspect the configuration to determine if any of the devices contain
305 pool_uses_efi(nvlist_t *config)
310 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
311 &child, &children) != 0)
312 return (read_efi_label(config, NULL) >= 0);
314 for (c = 0; c < children; c++) {
315 if (pool_uses_efi(child[c]))
323 * Given an nvlist of zpool properties to be set, validate that they are
324 * correct, and parse any numeric properties (index, boolean, etc) if they are
325 * specified as strings.
328 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
329 nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf)
337 struct stat64 statbuf;
341 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
342 (void) no_memory(hdl);
347 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
348 const char *propname = nvpair_name(elem);
351 * Make sure this property is valid and applies to this type.
353 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
354 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
355 "invalid property '%s'"), propname);
356 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
360 if (zpool_prop_readonly(prop)) {
361 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
362 "is readonly"), propname);
363 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
367 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
368 &strval, &intval, errbuf) != 0)
372 * Perform additional checking for specific properties.
375 case ZPOOL_PROP_VERSION:
376 if (intval < version || intval > SPA_VERSION) {
377 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
378 "property '%s' number %d is invalid."),
380 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
385 case ZPOOL_PROP_BOOTFS:
386 if (create_or_import) {
387 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
388 "property '%s' cannot be set at creation "
389 "or import time"), propname);
390 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
394 if (version < SPA_VERSION_BOOTFS) {
395 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
396 "pool must be upgraded to support "
397 "'%s' property"), propname);
398 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
403 * bootfs property value has to be a dataset name and
404 * the dataset has to be in the same pool as it sets to.
406 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
408 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
409 "is an invalid name"), strval);
410 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
414 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
415 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
416 "could not open pool '%s'"), poolname);
417 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
420 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
421 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
425 * bootfs property cannot be set on a disk which has
428 if (pool_uses_efi(nvroot)) {
429 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
430 "property '%s' not supported on "
431 "EFI labeled devices"), propname);
432 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
440 case ZPOOL_PROP_ALTROOT:
441 if (!create_or_import) {
442 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
443 "property '%s' can only be set during pool "
444 "creation or import"), propname);
445 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
449 if (strval[0] != '/') {
450 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
451 "bad alternate root '%s'"), strval);
452 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
457 case ZPOOL_PROP_CACHEFILE:
458 if (strval[0] == '\0')
461 if (strcmp(strval, "none") == 0)
464 if (strval[0] != '/') {
465 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
466 "property '%s' must be empty, an "
467 "absolute path, or 'none'"), propname);
468 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
472 slash = strrchr(strval, '/');
474 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
475 strcmp(slash, "/..") == 0) {
476 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
477 "'%s' is not a valid file"), strval);
478 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
484 if (strval[0] != '\0' &&
485 (stat64(strval, &statbuf) != 0 ||
486 !S_ISDIR(statbuf.st_mode))) {
487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
488 "'%s' is not a valid directory"),
490 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
501 nvlist_free(retprops);
506 * Set zpool property : propname=propval.
509 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
511 zfs_cmd_t zc = { 0 };
514 nvlist_t *nvl = NULL;
518 (void) snprintf(errbuf, sizeof (errbuf),
519 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
522 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp))
523 return (zfs_error(zhp->zpool_hdl, EZFS_POOLPROPS, errbuf));
525 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
526 return (no_memory(zhp->zpool_hdl));
528 if (nvlist_add_string(nvl, propname, propval) != 0) {
530 return (no_memory(zhp->zpool_hdl));
533 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
534 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
535 zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) {
544 * Execute the corresponding ioctl() to set this property.
546 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
548 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
553 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
555 zcmd_free_nvlists(&zc);
559 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
561 (void) zpool_props_refresh(zhp);
567 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
569 libzfs_handle_t *hdl = zhp->zpool_hdl;
571 char buf[ZFS_MAXPROPLEN];
573 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
576 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
581 if (entry->pl_prop != ZPROP_INVAL &&
582 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
584 if (strlen(buf) > entry->pl_width)
585 entry->pl_width = strlen(buf);
594 * Validate the given pool name, optionally putting an extended error message in
598 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
604 ret = pool_namecheck(pool, &why, &what);
607 * The rules for reserved pool names were extended at a later point.
608 * But we need to support users with existing pools that may now be
609 * invalid. So we only check for this expanded set of names during a
610 * create (or import), and only in userland.
612 if (ret == 0 && !isopen &&
613 (strncmp(pool, "mirror", 6) == 0 ||
614 strncmp(pool, "raidz", 5) == 0 ||
615 strncmp(pool, "spare", 5) == 0 ||
616 strcmp(pool, "log") == 0)) {
619 dgettext(TEXT_DOMAIN, "name is reserved"));
627 case NAME_ERR_TOOLONG:
629 dgettext(TEXT_DOMAIN, "name is too long"));
632 case NAME_ERR_INVALCHAR:
634 dgettext(TEXT_DOMAIN, "invalid character "
635 "'%c' in pool name"), what);
638 case NAME_ERR_NOLETTER:
639 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
640 "name must begin with a letter"));
643 case NAME_ERR_RESERVED:
644 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
645 "name is reserved"));
648 case NAME_ERR_DISKLIKE:
649 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
650 "pool name is reserved"));
653 case NAME_ERR_LEADING_SLASH:
654 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
655 "leading slash in name"));
658 case NAME_ERR_EMPTY_COMPONENT:
659 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
660 "empty component in name"));
663 case NAME_ERR_TRAILING_SLASH:
664 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
665 "trailing slash in name"));
668 case NAME_ERR_MULTIPLE_AT:
669 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
670 "multiple '@' delimiters in name"));
682 * Open a handle to the given pool, even if the pool is currently in the FAULTED
686 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
692 * Make sure the pool name is valid.
694 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
695 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
696 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
701 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
704 zhp->zpool_hdl = hdl;
705 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
707 if (zpool_refresh_stats(zhp, &missing) != 0) {
713 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
714 (void) zfs_error_fmt(hdl, EZFS_NOENT,
715 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
724 * Like the above, but silent on error. Used when iterating over pools (because
725 * the configuration cache may be out of date).
728 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
733 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
736 zhp->zpool_hdl = hdl;
737 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
739 if (zpool_refresh_stats(zhp, &missing) != 0) {
755 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
759 zpool_open(libzfs_handle_t *hdl, const char *pool)
763 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
766 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
767 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
768 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
777 * Close the handle. Simply frees the memory associated with the handle.
780 zpool_close(zpool_handle_t *zhp)
782 if (zhp->zpool_config)
783 nvlist_free(zhp->zpool_config);
784 if (zhp->zpool_old_config)
785 nvlist_free(zhp->zpool_old_config);
786 if (zhp->zpool_props)
787 nvlist_free(zhp->zpool_props);
792 * Return the name of the pool.
795 zpool_get_name(zpool_handle_t *zhp)
797 return (zhp->zpool_name);
802 * Return the state of the pool (ACTIVE or UNAVAILABLE)
805 zpool_get_state(zpool_handle_t *zhp)
807 return (zhp->zpool_state);
811 * Create the named pool, using the provided vdev list. It is assumed
812 * that the consumer has already validated the contents of the nvlist, so we
813 * don't have to worry about error semantics.
816 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
817 nvlist_t *props, nvlist_t *fsprops)
819 zfs_cmd_t zc = { 0 };
820 nvlist_t *zc_fsprops = NULL;
821 nvlist_t *zc_props = NULL;
826 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
827 "cannot create '%s'"), pool);
829 if (!zpool_name_valid(hdl, B_FALSE, pool))
830 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
832 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
836 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
837 SPA_VERSION_1, B_TRUE, msg)) == NULL) {
846 zoned = ((nvlist_lookup_string(fsprops,
847 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
848 strcmp(zonestr, "on") == 0);
850 if ((zc_fsprops = zfs_valid_proplist(hdl,
851 ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
855 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
858 if (nvlist_add_nvlist(zc_props,
859 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
864 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
867 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
869 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
871 zcmd_free_nvlists(&zc);
872 nvlist_free(zc_props);
873 nvlist_free(zc_fsprops);
878 * This can happen if the user has specified the same
879 * device multiple times. We can't reliably detect this
880 * until we try to add it and see we already have a
883 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
884 "one or more vdevs refer to the same device"));
885 return (zfs_error(hdl, EZFS_BADDEV, msg));
889 * This occurs when one of the devices is below
890 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
891 * device was the problem device since there's no
892 * reliable way to determine device size from userland.
897 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
899 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
900 "one or more devices is less than the "
901 "minimum size (%s)"), buf);
903 return (zfs_error(hdl, EZFS_BADDEV, msg));
906 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
907 "one or more devices is out of space"));
908 return (zfs_error(hdl, EZFS_BADDEV, msg));
911 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
912 "cache device must be a disk or disk slice"));
913 return (zfs_error(hdl, EZFS_BADDEV, msg));
916 return (zpool_standard_error(hdl, errno, msg));
921 * If this is an alternate root pool, then we automatically set the
922 * mountpoint of the root dataset to be '/'.
924 if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
928 verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
929 verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
936 zcmd_free_nvlists(&zc);
937 nvlist_free(zc_props);
938 nvlist_free(zc_fsprops);
943 * Destroy the given pool. It is up to the caller to ensure that there are no
944 * datasets left in the pool.
947 zpool_destroy(zpool_handle_t *zhp)
949 zfs_cmd_t zc = { 0 };
950 zfs_handle_t *zfp = NULL;
951 libzfs_handle_t *hdl = zhp->zpool_hdl;
954 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
955 (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
956 ZFS_TYPE_FILESYSTEM)) == NULL)
959 if (zpool_remove_zvol_links(zhp) != 0)
962 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
964 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
965 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
966 "cannot destroy '%s'"), zhp->zpool_name);
968 if (errno == EROFS) {
969 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
970 "one or more devices is read only"));
971 (void) zfs_error(hdl, EZFS_BADDEV, msg);
973 (void) zpool_standard_error(hdl, errno, msg);
982 remove_mountpoint(zfp);
990 * Add the given vdevs to the pool. The caller must have already performed the
991 * necessary verification to ensure that the vdev specification is well-formed.
994 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
996 zfs_cmd_t zc = { 0 };
998 libzfs_handle_t *hdl = zhp->zpool_hdl;
1000 nvlist_t **spares, **l2cache;
1001 uint_t nspares, nl2cache;
1003 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1004 "cannot add to '%s'"), zhp->zpool_name);
1006 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1007 SPA_VERSION_SPARES &&
1008 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1009 &spares, &nspares) == 0) {
1010 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1011 "upgraded to add hot spares"));
1012 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1015 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1016 SPA_VERSION_L2CACHE &&
1017 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1018 &l2cache, &nl2cache) == 0) {
1019 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1020 "upgraded to add cache devices"));
1021 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1024 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1026 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1028 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1032 * This can happen if the user has specified the same
1033 * device multiple times. We can't reliably detect this
1034 * until we try to add it and see we already have a
1037 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1038 "one or more vdevs refer to the same device"));
1039 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1044 * This occurrs when one of the devices is below
1045 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1046 * device was the problem device since there's no
1047 * reliable way to determine device size from userland.
1052 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1054 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1055 "device is less than the minimum "
1058 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1062 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1063 "pool must be upgraded to add these vdevs"));
1064 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1068 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1069 "root pool can not have multiple vdevs"
1070 " or separate logs"));
1071 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1075 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1076 "cache device must be a disk or disk slice"));
1077 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1081 (void) zpool_standard_error(hdl, errno, msg);
1089 zcmd_free_nvlists(&zc);
1095 * Exports the pool from the system. The caller must ensure that there are no
1096 * mounted datasets in the pool.
1099 zpool_export(zpool_handle_t *zhp, boolean_t force)
1101 zfs_cmd_t zc = { 0 };
1104 if (zpool_remove_zvol_links(zhp) != 0)
1107 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1108 "cannot export '%s'"), zhp->zpool_name);
1110 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1111 zc.zc_cookie = force;
1113 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1116 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1117 "use '-f' to override the following errors:\n"
1118 "'%s' has an active shared spare which could be"
1119 " used by other pools once '%s' is exported."),
1120 zhp->zpool_name, zhp->zpool_name);
1121 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1124 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1133 * zpool_import() is a contracted interface. Should be kept the same
1136 * Applications should use zpool_import_props() to import a pool with
1137 * new properties value to be set.
1140 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1143 nvlist_t *props = NULL;
1146 if (altroot != NULL) {
1147 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1148 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1149 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1153 if (nvlist_add_string(props,
1154 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0) {
1156 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1157 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1162 ret = zpool_import_props(hdl, config, newname, props, B_FALSE);
1169 * Import the given pool using the known configuration and a list of
1170 * properties to be set. The configuration should have come from
1171 * zpool_find_import(). The 'newname' parameters control whether the pool
1172 * is imported with a different name.
1175 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1176 nvlist_t *props, boolean_t importfaulted)
1178 zfs_cmd_t zc = { 0 };
1184 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1187 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1188 "cannot import pool '%s'"), origname);
1190 if (newname != NULL) {
1191 if (!zpool_name_valid(hdl, B_FALSE, newname))
1192 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1193 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1195 thename = (char *)newname;
1203 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1206 if ((props = zpool_valid_proplist(hdl, origname,
1207 props, version, B_TRUE, errbuf)) == NULL) {
1209 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1215 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1217 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1220 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1225 zc.zc_cookie = (uint64_t)importfaulted;
1227 if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) {
1229 if (newname == NULL)
1230 (void) snprintf(desc, sizeof (desc),
1231 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1234 (void) snprintf(desc, sizeof (desc),
1235 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1241 * Unsupported version.
1243 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1247 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1251 (void) zpool_standard_error(hdl, errno, desc);
1256 zpool_handle_t *zhp;
1259 * This should never fail, but play it safe anyway.
1261 if (zpool_open_silent(hdl, thename, &zhp) != 0) {
1263 } else if (zhp != NULL) {
1264 ret = zpool_create_zvol_links(zhp);
1270 zcmd_free_nvlists(&zc);
1280 zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type)
1282 zfs_cmd_t zc = { 0 };
1284 libzfs_handle_t *hdl = zhp->zpool_hdl;
1286 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1287 zc.zc_cookie = type;
1289 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0)
1292 (void) snprintf(msg, sizeof (msg),
1293 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1296 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1298 return (zpool_standard_error(hdl, errno, msg));
1302 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1303 * spare; but FALSE if its an INUSE spare.
1306 vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid,
1307 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
1311 uint64_t theguid, present;
1313 uint64_t wholedisk = 0;
1317 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0);
1319 if (search == NULL &&
1320 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) {
1322 * If the device has never been present since import, the only
1323 * reliable way to match the vdev is by GUID.
1325 if (theguid == guid)
1327 } else if (search != NULL &&
1328 nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1329 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1333 * For whole disks, the internal path has 's0', but the
1334 * path passed in by the user doesn't.
1336 if (strlen(search) == strlen(path) - 2 &&
1337 strncmp(search, path, strlen(search)) == 0)
1339 } else if (strcmp(search, path) == 0) {
1344 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1345 &child, &children) != 0)
1348 for (c = 0; c < children; c++) {
1349 if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1350 avail_spare, l2cache, NULL)) != NULL) {
1352 * The 'is_log' value is only set for the toplevel
1353 * vdev, not the leaf vdevs. So we always lookup the
1354 * log device from the root of the vdev tree (where
1355 * 'log' is non-NULL).
1358 nvlist_lookup_uint64(child[c],
1359 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
1367 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1368 &child, &children) == 0) {
1369 for (c = 0; c < children; c++) {
1370 if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1371 avail_spare, l2cache, NULL)) != NULL) {
1372 *avail_spare = B_TRUE;
1378 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1379 &child, &children) == 0) {
1380 for (c = 0; c < children; c++) {
1381 if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1382 avail_spare, l2cache, NULL)) != NULL) {
1393 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
1394 boolean_t *l2cache, boolean_t *log)
1396 char buf[MAXPATHLEN];
1402 guid = strtoull(path, &end, 10);
1403 if (guid != 0 && *end == '\0') {
1405 } else if (path[0] != '/') {
1406 (void) snprintf(buf, sizeof (buf), "%s%s", _PATH_DEV, path);
1412 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1415 *avail_spare = B_FALSE;
1419 return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare,
1424 vdev_online(nvlist_t *nv)
1428 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
1429 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
1430 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
1437 * Get phys_path for a root pool
1438 * Return 0 on success; non-zeron on failure.
1441 zpool_get_physpath(zpool_handle_t *zhp, char *physpath)
1443 char bootfs[ZPOOL_MAXNAMELEN];
1444 nvlist_t *vdev_root;
1450 * Make sure this is a root pool, as phys_path doesn't mean
1451 * anything to a non-root pool.
1453 if (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
1454 sizeof (bootfs), NULL) != 0)
1457 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1458 ZPOOL_CONFIG_VDEV_TREE, &vdev_root) == 0);
1460 if (nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
1461 &child, &count) != 0)
1464 for (i = 0; i < count; i++) {
1471 if (nvlist_lookup_string(child[i], ZPOOL_CONFIG_TYPE, &type)
1475 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
1476 if (!vdev_online(child[i]))
1478 verify(nvlist_lookup_string(child[i],
1479 ZPOOL_CONFIG_PHYS_PATH, &tmppath) == 0);
1480 (void) strncpy(physpath, tmppath, strlen(tmppath));
1481 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0) {
1482 if (nvlist_lookup_nvlist_array(child[i],
1483 ZPOOL_CONFIG_CHILDREN, &child2, &count2) != 0)
1486 for (j = 0; j < count2; j++) {
1487 if (!vdev_online(child2[j]))
1489 if (nvlist_lookup_string(child2[j],
1490 ZPOOL_CONFIG_PHYS_PATH, &tmppath) != 0)
1493 if ((strlen(physpath) + strlen(tmppath)) >
1497 if (strlen(physpath) == 0) {
1498 (void) strncpy(physpath, tmppath,
1501 (void) strcat(physpath, " ");
1502 (void) strcat(physpath, tmppath);
1514 * Returns TRUE if the given guid corresponds to the given type.
1515 * This is used to check for hot spares (INUSE or not), and level 2 cache
1519 is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type)
1521 uint64_t target_guid;
1527 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1529 if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) {
1530 for (i = 0; i < count; i++) {
1531 verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID,
1532 &target_guid) == 0);
1533 if (guid == target_guid)
1542 * Bring the specified vdev online. The 'flags' parameter is a set of the
1543 * ZFS_ONLINE_* flags.
1546 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
1547 vdev_state_t *newstate)
1549 zfs_cmd_t zc = { 0 };
1552 boolean_t avail_spare, l2cache;
1553 libzfs_handle_t *hdl = zhp->zpool_hdl;
1555 (void) snprintf(msg, sizeof (msg),
1556 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
1558 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1559 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1561 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1563 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1566 is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
1567 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1569 zc.zc_cookie = VDEV_STATE_ONLINE;
1572 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0)
1573 return (zpool_standard_error(hdl, errno, msg));
1575 *newstate = zc.zc_cookie;
1580 * Take the specified vdev offline
1583 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
1585 zfs_cmd_t zc = { 0 };
1588 boolean_t avail_spare, l2cache;
1589 libzfs_handle_t *hdl = zhp->zpool_hdl;
1591 (void) snprintf(msg, sizeof (msg),
1592 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
1594 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1595 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1597 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1599 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1602 is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
1603 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1605 zc.zc_cookie = VDEV_STATE_OFFLINE;
1606 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
1608 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1615 * There are no other replicas of this device.
1617 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
1620 return (zpool_standard_error(hdl, errno, msg));
1625 * Mark the given vdev faulted.
1628 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid)
1630 zfs_cmd_t zc = { 0 };
1632 libzfs_handle_t *hdl = zhp->zpool_hdl;
1634 (void) snprintf(msg, sizeof (msg),
1635 dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
1637 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1639 zc.zc_cookie = VDEV_STATE_FAULTED;
1641 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1648 * There are no other replicas of this device.
1650 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
1653 return (zpool_standard_error(hdl, errno, msg));
1659 * Mark the given vdev degraded.
1662 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid)
1664 zfs_cmd_t zc = { 0 };
1666 libzfs_handle_t *hdl = zhp->zpool_hdl;
1668 (void) snprintf(msg, sizeof (msg),
1669 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
1671 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1673 zc.zc_cookie = VDEV_STATE_DEGRADED;
1675 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1678 return (zpool_standard_error(hdl, errno, msg));
1682 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
1686 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
1692 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
1694 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
1697 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
1698 children == 2 && child[which] == tgt)
1701 for (c = 0; c < children; c++)
1702 if (is_replacing_spare(child[c], tgt, which))
1710 * Attach new_disk (fully described by nvroot) to old_disk.
1711 * If 'replacing' is specified, the new disk will replace the old one.
1714 zpool_vdev_attach(zpool_handle_t *zhp,
1715 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
1717 zfs_cmd_t zc = { 0 };
1721 boolean_t avail_spare, l2cache, islog;
1723 char *path, *newname;
1726 nvlist_t *config_root;
1727 libzfs_handle_t *hdl = zhp->zpool_hdl;
1730 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1731 "cannot replace %s with %s"), old_disk, new_disk);
1733 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1734 "cannot attach %s to %s"), new_disk, old_disk);
1736 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1737 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
1739 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1742 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1745 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
1747 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1748 zc.zc_cookie = replacing;
1750 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1751 &child, &children) != 0 || children != 1) {
1752 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1753 "new device must be a single disk"));
1754 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
1757 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
1758 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
1760 if ((newname = zpool_vdev_name(NULL, NULL, child[0])) == NULL)
1764 * If the target is a hot spare that has been swapped in, we can only
1765 * replace it with another hot spare.
1768 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
1769 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
1770 NULL) == NULL || !avail_spare) &&
1771 is_replacing_spare(config_root, tgt, 1)) {
1772 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1773 "can only be replaced by another hot spare"));
1775 return (zfs_error(hdl, EZFS_BADTARGET, msg));
1779 * If we are attempting to replace a spare, it canot be applied to an
1780 * already spared device.
1783 nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
1784 zpool_find_vdev(zhp, newname, &avail_spare,
1785 &l2cache, NULL) != NULL && avail_spare &&
1786 is_replacing_spare(config_root, tgt, 0)) {
1787 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1788 "device has already been replaced with a spare"));
1790 return (zfs_error(hdl, EZFS_BADTARGET, msg));
1795 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1798 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc);
1800 zcmd_free_nvlists(&zc);
1808 * Can't attach to or replace this type of vdev.
1812 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1813 "cannot replace a log with a spare"));
1815 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1816 "cannot replace a replacing device"));
1818 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1819 "can only attach to mirrors and top-level "
1822 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
1827 * The new device must be a single disk.
1829 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1830 "new device must be a single disk"));
1831 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
1835 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
1837 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1842 * The new device is too small.
1844 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1845 "device is too small"));
1846 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1851 * The new device has a different alignment requirement.
1853 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1854 "devices have different sector alignment"));
1855 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1860 * The resulting top-level vdev spec won't fit in the label.
1862 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
1866 (void) zpool_standard_error(hdl, errno, msg);
1873 * Detach the specified device.
1876 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
1878 zfs_cmd_t zc = { 0 };
1881 boolean_t avail_spare, l2cache;
1882 libzfs_handle_t *hdl = zhp->zpool_hdl;
1884 (void) snprintf(msg, sizeof (msg),
1885 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
1887 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1888 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1890 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1893 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1896 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
1898 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1900 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
1907 * Can't detach from this type of vdev.
1909 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
1910 "applicable to mirror and replacing vdevs"));
1911 (void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg);
1916 * There are no other replicas of this device.
1918 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
1922 (void) zpool_standard_error(hdl, errno, msg);
1929 * Remove the given device. Currently, this is supported only for hot spares
1930 * and level 2 cache devices.
1933 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
1935 zfs_cmd_t zc = { 0 };
1938 boolean_t avail_spare, l2cache;
1939 libzfs_handle_t *hdl = zhp->zpool_hdl;
1941 (void) snprintf(msg, sizeof (msg),
1942 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
1944 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1945 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1947 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1949 if (!avail_spare && !l2cache) {
1950 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1951 "only inactive hot spares or cache devices "
1953 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1956 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1958 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
1961 return (zpool_standard_error(hdl, errno, msg));
1965 * Clear the errors for the pool, or the particular device if specified.
1968 zpool_clear(zpool_handle_t *zhp, const char *path)
1970 zfs_cmd_t zc = { 0 };
1973 boolean_t avail_spare, l2cache;
1974 libzfs_handle_t *hdl = zhp->zpool_hdl;
1977 (void) snprintf(msg, sizeof (msg),
1978 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
1981 (void) snprintf(msg, sizeof (msg),
1982 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
1985 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1987 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
1988 &l2cache, NULL)) == 0)
1989 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1992 * Don't allow error clearing for hot spares. Do allow
1993 * error clearing for l2cache devices.
1996 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1998 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
2002 if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
2005 return (zpool_standard_error(hdl, errno, msg));
2009 * Similar to zpool_clear(), but takes a GUID (used by fmd).
2012 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
2014 zfs_cmd_t zc = { 0 };
2016 libzfs_handle_t *hdl = zhp->zpool_hdl;
2018 (void) snprintf(msg, sizeof (msg),
2019 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
2022 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2025 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
2028 return (zpool_standard_error(hdl, errno, msg));
2032 * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool>
2036 zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *),
2039 libzfs_handle_t *hdl = zhp->zpool_hdl;
2040 char (*paths)[MAXPATHLEN];
2041 char path[MAXPATHLEN];
2043 int curr, fd, base, ret = 0;
2048 if ((base = open(ZVOL_FULL_DEV_DIR, O_RDONLY)) < 0)
2049 return (errno == ENOENT ? 0 : -1);
2051 snprintf(path, sizeof(path), "%s/%s", ZVOL_FULL_DEV_DIR,
2053 if (stat(path, &st) != 0) {
2056 return (err == ENOENT ? 0 : -1);
2060 * Oddly this wasn't a directory -- ignore that failure since we
2061 * know there are no links lower in the (non-existant) hierarchy.
2063 if (!S_ISDIR(st.st_mode)) {
2068 if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) {
2073 (void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0]));
2077 snprintf(path, sizeof(path), "%s/%s", ZVOL_FULL_DEV_DIR,
2079 if (lstat(path, &st) != 0)
2082 if (S_ISDIR(st.st_mode)) {
2083 if ((dirp = opendir(path)) == NULL) {
2087 while ((dp = readdir(dirp)) != NULL) {
2088 if (dp->d_name[0] == '.')
2091 if (curr + 1 == size) {
2092 paths = zfs_realloc(hdl, paths,
2093 size * sizeof (paths[0]),
2094 size * 2 * sizeof (paths[0]));
2095 if (paths == NULL) {
2096 (void) closedir(dirp);
2103 (void) strlcpy(paths[curr + 1], paths[curr],
2104 sizeof (paths[curr + 1]));
2105 (void) strlcat(paths[curr], "/",
2106 sizeof (paths[curr]));
2107 (void) strlcat(paths[curr], dp->d_name,
2108 sizeof (paths[curr]));
2112 (void) closedir(dirp);
2115 if ((ret = cb(paths[curr], data)) != 0)
2133 typedef struct zvol_cb {
2134 zpool_handle_t *zcb_pool;
2135 boolean_t zcb_create;
2140 do_zvol_create(zfs_handle_t *zhp, void *data)
2144 if (ZFS_IS_VOLUME(zhp)) {
2145 (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
2146 ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL);
2150 ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL);
2158 * Iterate over all zvols in the pool and make any necessary minor nodes.
2161 zpool_create_zvol_links(zpool_handle_t *zhp)
2167 * If the pool is unavailable, just return success.
2169 if ((zfp = make_dataset_handle(zhp->zpool_hdl,
2170 zhp->zpool_name)) == NULL)
2173 ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL);
2180 do_zvol_remove(const char *dataset, void *data)
2182 zpool_handle_t *zhp = data;
2184 return (zvol_remove_link(zhp->zpool_hdl, dataset));
2188 * Iterate over all zvols in the pool and remove any minor nodes. We iterate
2189 * by examining the /dev links so that a corrupted pool doesn't impede this
2193 zpool_remove_zvol_links(zpool_handle_t *zhp)
2195 return (zpool_iter_zvol(zhp, do_zvol_remove, zhp));
2199 * Convert from a devid string to a path.
2202 devid_to_path(char *devid_str)
2207 devid_nmlist_t *list = NULL;
2210 if (devid_str_decode(devid_str, &devid, &minor) != 0)
2213 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
2215 devid_str_free(minor);
2221 if ((path = strdup(list[0].devname)) == NULL)
2224 devid_free_nmlist(list);
2230 * Convert from a path to a devid string.
2233 path_to_devid(const char *path)
2239 if ((fd = open(path, O_RDONLY)) < 0)
2244 if (devid_get(fd, &devid) == 0) {
2245 if (devid_get_minor_name(fd, &minor) == 0)
2246 ret = devid_str_encode(devid, minor);
2248 devid_str_free(minor);
2257 * Issue the necessary ioctl() to update the stored path value for the vdev. We
2258 * ignore any failure here, since a common case is for an unprivileged user to
2259 * type 'zpool status', and we'll display the correct information anyway.
2262 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
2264 zfs_cmd_t zc = { 0 };
2266 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2267 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
2268 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2271 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
2275 * Given a vdev, return the name to display in iostat. If the vdev has a path,
2276 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
2277 * We also check if this is a whole disk, in which case we strip off the
2278 * trailing 's0' slice name.
2280 * This routine is also responsible for identifying when disks have been
2281 * reconfigured in a new location. The kernel will have opened the device by
2282 * devid, but the path will still refer to the old location. To catch this, we
2283 * first do a path -> devid translation (which is fast for the common case). If
2284 * the devid matches, we're done. If not, we do a reverse devid -> path
2285 * translation and issue the appropriate ioctl() to update the path of the vdev.
2286 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
2290 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv)
2298 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
2300 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2302 (void) snprintf(buf, sizeof (buf), "%llu",
2303 (u_longlong_t)value);
2305 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
2308 * If the device is dead (faulted, offline, etc) then don't
2309 * bother opening it. Otherwise we may be forcing the user to
2310 * open a misbehaving device, which can have undesirable
2313 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
2314 (uint64_t **)&vs, &vsc) != 0 ||
2315 vs->vs_state >= VDEV_STATE_DEGRADED) &&
2317 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
2319 * Determine if the current path is correct.
2321 char *newdevid = path_to_devid(path);
2323 if (newdevid == NULL ||
2324 strcmp(devid, newdevid) != 0) {
2327 if ((newpath = devid_to_path(devid)) != NULL) {
2329 * Update the path appropriately.
2331 set_path(zhp, nv, newpath);
2332 if (nvlist_add_string(nv,
2333 ZPOOL_CONFIG_PATH, newpath) == 0)
2334 verify(nvlist_lookup_string(nv,
2342 devid_str_free(newdevid);
2345 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
2346 path += sizeof(_PATH_DEV) - 1;
2348 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2349 &value) == 0 && value) {
2350 char *tmp = zfs_strdup(hdl, path);
2353 tmp[strlen(path) - 2] = '\0';
2357 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
2360 * If it's a raidz device, we need to stick in the parity level.
2362 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
2363 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
2365 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
2366 (u_longlong_t)value);
2371 return (zfs_strdup(hdl, path));
2375 zbookmark_compare(const void *a, const void *b)
2377 return (memcmp(a, b, sizeof (zbookmark_t)));
2381 * Retrieve the persistent error log, uniquify the members, and return to the
2385 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
2387 zfs_cmd_t zc = { 0 };
2389 zbookmark_t *zb = NULL;
2393 * Retrieve the raw error list from the kernel. If the number of errors
2394 * has increased, allocate more space and continue until we get the
2397 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
2401 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
2402 count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
2404 zc.zc_nvlist_dst_size = count;
2405 (void) strcpy(zc.zc_name, zhp->zpool_name);
2407 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
2409 free((void *)(uintptr_t)zc.zc_nvlist_dst);
2410 if (errno == ENOMEM) {
2411 count = zc.zc_nvlist_dst_size;
2412 if ((zc.zc_nvlist_dst = (uintptr_t)
2413 zfs_alloc(zhp->zpool_hdl, count *
2414 sizeof (zbookmark_t))) == (uintptr_t)NULL)
2425 * Sort the resulting bookmarks. This is a little confusing due to the
2426 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
2427 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
2428 * _not_ copied as part of the process. So we point the start of our
2429 * array appropriate and decrement the total number of elements.
2431 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
2432 zc.zc_nvlist_dst_size;
2433 count -= zc.zc_nvlist_dst_size;
2435 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
2437 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
2440 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
2442 for (i = 0; i < count; i++) {
2445 /* ignoring zb_blkid and zb_level for now */
2446 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
2447 zb[i-1].zb_object == zb[i].zb_object)
2450 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
2452 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
2453 zb[i].zb_objset) != 0) {
2457 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
2458 zb[i].zb_object) != 0) {
2462 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
2469 free((void *)(uintptr_t)zc.zc_nvlist_dst);
2473 free((void *)(uintptr_t)zc.zc_nvlist_dst);
2474 return (no_memory(zhp->zpool_hdl));
2478 * Upgrade a ZFS pool to the latest on-disk version.
2481 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
2483 zfs_cmd_t zc = { 0 };
2484 libzfs_handle_t *hdl = zhp->zpool_hdl;
2486 (void) strcpy(zc.zc_name, zhp->zpool_name);
2487 zc.zc_cookie = new_version;
2489 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
2490 return (zpool_standard_error_fmt(hdl, errno,
2491 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
2497 zpool_set_history_str(const char *subcommand, int argc, char **argv,
2502 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
2503 for (i = 1; i < argc; i++) {
2504 if (strlen(history_str) + 1 + strlen(argv[i]) >
2507 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
2508 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
2513 * Stage command history for logging.
2516 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
2518 if (history_str == NULL)
2521 if (strlen(history_str) > HIS_MAX_RECORD_LEN)
2524 if (hdl->libzfs_log_str != NULL)
2525 free(hdl->libzfs_log_str);
2527 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
2528 return (no_memory(hdl));
2534 * Perform ioctl to get some command history of a pool.
2536 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
2537 * logical offset of the history buffer to start reading from.
2539 * Upon return, 'off' is the next logical offset to read from and
2540 * 'len' is the actual amount of bytes read into 'buf'.
2543 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
2545 zfs_cmd_t zc = { 0 };
2546 libzfs_handle_t *hdl = zhp->zpool_hdl;
2548 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2550 zc.zc_history = (uint64_t)(uintptr_t)buf;
2551 zc.zc_history_len = *len;
2552 zc.zc_history_offset = *off;
2554 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
2557 return (zfs_error_fmt(hdl, EZFS_PERM,
2558 dgettext(TEXT_DOMAIN,
2559 "cannot show history for pool '%s'"),
2562 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
2563 dgettext(TEXT_DOMAIN, "cannot get history for pool "
2564 "'%s'"), zhp->zpool_name));
2566 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
2567 dgettext(TEXT_DOMAIN, "cannot get history for pool "
2568 "'%s', pool must be upgraded"), zhp->zpool_name));
2570 return (zpool_standard_error_fmt(hdl, errno,
2571 dgettext(TEXT_DOMAIN,
2572 "cannot get history for '%s'"), zhp->zpool_name));
2576 *len = zc.zc_history_len;
2577 *off = zc.zc_history_offset;
2583 * Process the buffer of nvlists, unpacking and storing each nvlist record
2584 * into 'records'. 'leftover' is set to the number of bytes that weren't
2585 * processed as there wasn't a complete record.
2588 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
2589 nvlist_t ***records, uint_t *numrecords)
2595 while (bytes_read > sizeof (reclen)) {
2597 /* get length of packed record (stored as little endian) */
2598 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
2599 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
2601 if (bytes_read < sizeof (reclen) + reclen)
2605 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
2607 bytes_read -= sizeof (reclen) + reclen;
2608 buf += sizeof (reclen) + reclen;
2610 /* add record to nvlist array */
2612 if (ISP2(*numrecords + 1)) {
2613 *records = realloc(*records,
2614 *numrecords * 2 * sizeof (nvlist_t *));
2616 (*records)[*numrecords - 1] = nv;
2619 *leftover = bytes_read;
2623 #define HIS_BUF_LEN (128*1024)
2626 * Retrieve the command history of a pool.
2629 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
2631 char buf[HIS_BUF_LEN];
2633 nvlist_t **records = NULL;
2634 uint_t numrecords = 0;
2638 uint64_t bytes_read = sizeof (buf);
2641 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
2644 /* if nothing else was read in, we're at EOF, just return */
2648 if ((err = zpool_history_unpack(buf, bytes_read,
2649 &leftover, &records, &numrecords)) != 0)
2657 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
2658 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
2659 records, numrecords) == 0);
2661 for (i = 0; i < numrecords; i++)
2662 nvlist_free(records[i]);
2669 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
2670 char *pathname, size_t len)
2672 zfs_cmd_t zc = { 0 };
2673 boolean_t mounted = B_FALSE;
2674 char *mntpnt = NULL;
2675 char dsname[MAXNAMELEN];
2678 /* special case for the MOS */
2679 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
2683 /* get the dataset's name */
2684 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2686 if (ioctl(zhp->zpool_hdl->libzfs_fd,
2687 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
2688 /* just write out a path of two object numbers */
2689 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
2693 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
2695 /* find out if the dataset is mounted */
2696 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
2698 /* get the corrupted object's path */
2699 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
2701 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
2704 (void) snprintf(pathname, len, "%s%s", mntpnt,
2707 (void) snprintf(pathname, len, "%s:%s",
2708 dsname, zc.zc_value);
2711 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
2716 #define RDISK_ROOT "/dev/rdsk"
2717 #define BACKUP_SLICE "s2"
2719 * Don't start the slice at the default block of 34; many storage
2720 * devices will use a stripe width of 128k, so start there instead.
2722 #define NEW_START_BLOCK 256
2726 * Read the EFI label from the config, if a label does not exist then
2727 * pass back the error to the caller. If the caller has passed a non-NULL
2728 * diskaddr argument then we set it to the starting address of the EFI
2732 read_efi_label(nvlist_t *config, diskaddr_t *sb)
2736 char diskname[MAXPATHLEN];
2739 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
2742 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
2743 strrchr(path, '/'));
2744 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
2745 struct dk_gpt *vtoc;
2747 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
2749 *sb = vtoc->efi_parts[0].p_start;
2758 * determine where a partition starts on a disk in the current
2762 find_start_block(nvlist_t *config)
2766 diskaddr_t sb = MAXOFFSET_T;
2769 if (nvlist_lookup_nvlist_array(config,
2770 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
2771 if (nvlist_lookup_uint64(config,
2772 ZPOOL_CONFIG_WHOLE_DISK,
2773 &wholedisk) != 0 || !wholedisk) {
2774 return (MAXOFFSET_T);
2776 if (read_efi_label(config, &sb) < 0)
2781 for (c = 0; c < children; c++) {
2782 sb = find_start_block(child[c]);
2783 if (sb != MAXOFFSET_T) {
2787 return (MAXOFFSET_T);
2792 * Label an individual disk. The name provided is the short name,
2793 * stripped of any leading /dev path.
2796 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
2799 char path[MAXPATHLEN];
2800 struct dk_gpt *vtoc;
2802 size_t resv = EFI_MIN_RESV_SIZE;
2803 uint64_t slice_size;
2804 diskaddr_t start_block;
2807 /* prepare an error message just in case */
2808 (void) snprintf(errbuf, sizeof (errbuf),
2809 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
2814 verify(nvlist_lookup_nvlist(zhp->zpool_config,
2815 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2817 if (zhp->zpool_start_block == 0)
2818 start_block = find_start_block(nvroot);
2820 start_block = zhp->zpool_start_block;
2821 zhp->zpool_start_block = start_block;
2824 start_block = NEW_START_BLOCK;
2827 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
2830 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2832 * This shouldn't happen. We've long since verified that this
2833 * is a valid device.
2836 dgettext(TEXT_DOMAIN, "unable to open device"));
2837 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2840 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
2842 * The only way this can fail is if we run out of memory, or we
2843 * were unable to read the disk's capacity
2845 if (errno == ENOMEM)
2846 (void) no_memory(hdl);
2849 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2850 "unable to read disk capacity"), name);
2852 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2855 slice_size = vtoc->efi_last_u_lba + 1;
2856 slice_size -= EFI_MIN_RESV_SIZE;
2857 if (start_block == MAXOFFSET_T)
2858 start_block = NEW_START_BLOCK;
2859 slice_size -= start_block;
2861 vtoc->efi_parts[0].p_start = start_block;
2862 vtoc->efi_parts[0].p_size = slice_size;
2865 * Why we use V_USR: V_BACKUP confuses users, and is considered
2866 * disposable by some EFI utilities (since EFI doesn't have a backup
2867 * slice). V_UNASSIGNED is supposed to be used only for zero size
2868 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
2869 * etc. were all pretty specific. V_USR is as close to reality as we
2870 * can get, in the absence of V_OTHER.
2872 vtoc->efi_parts[0].p_tag = V_USR;
2873 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
2875 vtoc->efi_parts[8].p_start = slice_size + start_block;
2876 vtoc->efi_parts[8].p_size = resv;
2877 vtoc->efi_parts[8].p_tag = V_RESERVED;
2879 if (efi_write(fd, vtoc) != 0) {
2881 * Some block drivers (like pcata) may not support EFI
2882 * GPT labels. Print out a helpful error message dir-
2883 * ecting the user to manually label the disk and give
2889 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2890 "try using fdisk(1M) and then provide a specific slice"));
2891 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
2901 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
2907 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
2908 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
2909 strcmp(type, VDEV_TYPE_FILE) == 0 ||
2910 strcmp(type, VDEV_TYPE_LOG) == 0 ||
2911 strcmp(type, VDEV_TYPE_MISSING) == 0) {
2912 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2913 "vdev type '%s' is not supported"), type);
2914 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
2917 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
2918 &child, &children) == 0) {
2919 for (c = 0; c < children; c++) {
2920 if (!supported_dump_vdev_type(hdl, child[c], errbuf))
2928 * check if this zvol is allowable for use as a dump device; zero if
2929 * it is, > 0 if it isn't, < 0 if it isn't a zvol
2932 zvol_check_dump_config(char *arg)
2934 zpool_handle_t *zhp = NULL;
2935 nvlist_t *config, *nvroot;
2939 libzfs_handle_t *hdl;
2941 char poolname[ZPOOL_MAXNAMELEN];
2942 int pathlen = strlen(ZVOL_FULL_DEV_DIR);
2945 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
2949 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2950 "dump is not supported on device '%s'"), arg);
2952 if ((hdl = libzfs_init()) == NULL)
2954 libzfs_print_on_error(hdl, B_TRUE);
2956 volname = arg + pathlen;
2958 /* check the configuration of the pool */
2959 if ((p = strchr(volname, '/')) == NULL) {
2960 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2961 "malformed dataset name"));
2962 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
2964 } else if (p - volname >= ZFS_MAXNAMELEN) {
2965 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2966 "dataset name is too long"));
2967 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
2970 (void) strncpy(poolname, volname, p - volname);
2971 poolname[p - volname] = '\0';
2974 if ((zhp = zpool_open(hdl, poolname)) == NULL) {
2975 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2976 "could not open pool '%s'"), poolname);
2977 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
2980 config = zpool_get_config(zhp, NULL);
2981 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2983 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2984 "could not obtain vdev configuration for '%s'"), poolname);
2985 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
2989 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2990 &top, &toplevels) == 0);
2991 if (toplevels != 1) {
2992 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2993 "'%s' has multiple top level vdevs"), poolname);
2994 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
2998 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {