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>
46 #include "zfs_namecheck.h"
48 #include "libzfs_impl.h"
50 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
52 #if defined(__i386) || defined(__amd64)
53 #define BOOTCMD "installgrub(1M)"
55 #define BOOTCMD "installboot(1M)"
59 * ====================================================================
60 * zpool property functions
61 * ====================================================================
65 zpool_get_all_props(zpool_handle_t *zhp)
68 libzfs_handle_t *hdl = zhp->zpool_hdl;
70 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
72 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
75 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
76 if (errno == ENOMEM) {
77 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
78 zcmd_free_nvlists(&zc);
82 zcmd_free_nvlists(&zc);
87 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
88 zcmd_free_nvlists(&zc);
92 zcmd_free_nvlists(&zc);
98 zpool_props_refresh(zpool_handle_t *zhp)
102 old_props = zhp->zpool_props;
104 if (zpool_get_all_props(zhp) != 0)
107 nvlist_free(old_props);
112 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
118 zprop_source_t source;
120 nvl = zhp->zpool_props;
121 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
122 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
124 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
126 source = ZPROP_SRC_DEFAULT;
127 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
138 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
142 zprop_source_t source;
144 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
146 * zpool_get_all_props() has most likely failed because
147 * the pool is faulted, but if all we need is the top level
148 * vdev's guid then get it from the zhp config nvlist.
150 if ((prop == ZPOOL_PROP_GUID) &&
151 (nvlist_lookup_nvlist(zhp->zpool_config,
152 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
153 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
157 return (zpool_prop_default_numeric(prop));
160 nvl = zhp->zpool_props;
161 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
162 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
164 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
166 source = ZPROP_SRC_DEFAULT;
167 value = zpool_prop_default_numeric(prop);
177 * Map VDEV STATE to printed strings.
180 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
183 case VDEV_STATE_CLOSED:
184 case VDEV_STATE_OFFLINE:
185 return (gettext("OFFLINE"));
186 case VDEV_STATE_REMOVED:
187 return (gettext("REMOVED"));
188 case VDEV_STATE_CANT_OPEN:
189 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
190 return (gettext("FAULTED"));
192 return (gettext("UNAVAIL"));
193 case VDEV_STATE_FAULTED:
194 return (gettext("FAULTED"));
195 case VDEV_STATE_DEGRADED:
196 return (gettext("DEGRADED"));
197 case VDEV_STATE_HEALTHY:
198 return (gettext("ONLINE"));
201 return (gettext("UNKNOWN"));
205 * Get a zpool property value for 'prop' and return the value in
206 * a pre-allocated buffer.
209 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
210 zprop_source_t *srctype)
214 zprop_source_t src = ZPROP_SRC_NONE;
219 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
221 case ZPOOL_PROP_NAME:
222 (void) strlcpy(buf, zpool_get_name(zhp), len);
225 case ZPOOL_PROP_HEALTH:
226 (void) strlcpy(buf, "FAULTED", len);
229 case ZPOOL_PROP_GUID:
230 intval = zpool_get_prop_int(zhp, prop, &src);
231 (void) snprintf(buf, len, "%llu", intval);
234 case ZPOOL_PROP_ALTROOT:
235 case ZPOOL_PROP_CACHEFILE:
236 if (zhp->zpool_props != NULL ||
237 zpool_get_all_props(zhp) == 0) {
239 zpool_get_prop_string(zhp, prop, &src),
247 (void) strlcpy(buf, "-", len);
256 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
257 prop != ZPOOL_PROP_NAME)
260 switch (zpool_prop_get_type(prop)) {
261 case PROP_TYPE_STRING:
262 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
266 case PROP_TYPE_NUMBER:
267 intval = zpool_get_prop_int(zhp, prop, &src);
270 case ZPOOL_PROP_SIZE:
271 case ZPOOL_PROP_USED:
272 case ZPOOL_PROP_AVAILABLE:
273 (void) zfs_nicenum(intval, buf, len);
276 case ZPOOL_PROP_CAPACITY:
277 (void) snprintf(buf, len, "%llu%%",
278 (u_longlong_t)intval);
281 case ZPOOL_PROP_HEALTH:
282 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
283 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
284 verify(nvlist_lookup_uint64_array(nvroot,
285 ZPOOL_CONFIG_STATS, (uint64_t **)&vs, &vsc) == 0);
287 (void) strlcpy(buf, zpool_state_to_name(intval,
291 (void) snprintf(buf, len, "%llu", intval);
295 case PROP_TYPE_INDEX:
296 intval = zpool_get_prop_int(zhp, prop, &src);
297 if (zpool_prop_index_to_string(prop, intval, &strval)
300 (void) strlcpy(buf, strval, len);
314 pool_is_bootable(zpool_handle_t *zhp)
316 char bootfs[ZPOOL_MAXNAMELEN];
318 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
319 sizeof (bootfs), NULL) == 0 && strncmp(bootfs, "-",
320 sizeof (bootfs)) != 0);
325 * Check if the bootfs name has the same pool name as it is set to.
326 * Assuming bootfs is a valid dataset name.
329 bootfs_name_valid(const char *pool, char *bootfs)
331 int len = strlen(pool);
333 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
336 if (strncmp(pool, bootfs, len) == 0 &&
337 (bootfs[len] == '/' || bootfs[len] == '\0'))
344 * Inspect the configuration to determine if any of the devices contain
348 pool_uses_efi(nvlist_t *config)
354 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
355 &child, &children) != 0)
356 return (read_efi_label(config, NULL) >= 0);
358 for (c = 0; c < children; c++) {
359 if (pool_uses_efi(child[c]))
367 * Given an nvlist of zpool properties to be set, validate that they are
368 * correct, and parse any numeric properties (index, boolean, etc) if they are
369 * specified as strings.
372 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
373 nvlist_t *props, uint64_t version, boolean_t create_or_import, char *errbuf)
381 struct stat64 statbuf;
385 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
386 (void) no_memory(hdl);
391 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
392 const char *propname = nvpair_name(elem);
395 * Make sure this property is valid and applies to this type.
397 if ((prop = zpool_name_to_prop(propname)) == ZPROP_INVAL) {
398 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
399 "invalid property '%s'"), propname);
400 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
404 if (zpool_prop_readonly(prop)) {
405 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
406 "is readonly"), propname);
407 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
411 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
412 &strval, &intval, errbuf) != 0)
416 * Perform additional checking for specific properties.
419 case ZPOOL_PROP_VERSION:
420 if (intval < version || intval > SPA_VERSION) {
421 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
422 "property '%s' number %d is invalid."),
424 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
429 case ZPOOL_PROP_BOOTFS:
430 if (create_or_import) {
431 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
432 "property '%s' cannot be set at creation "
433 "or import time"), propname);
434 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
438 if (version < SPA_VERSION_BOOTFS) {
439 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
440 "pool must be upgraded to support "
441 "'%s' property"), propname);
442 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
447 * bootfs property value has to be a dataset name and
448 * the dataset has to be in the same pool as it sets to.
450 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
452 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
453 "is an invalid name"), strval);
454 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
458 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
459 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
460 "could not open pool '%s'"), poolname);
461 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
464 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
465 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
469 * bootfs property cannot be set on a disk which has
472 if (pool_uses_efi(nvroot)) {
473 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
474 "property '%s' not supported on "
475 "EFI labeled devices"), propname);
476 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf);
484 case ZPOOL_PROP_ALTROOT:
485 if (!create_or_import) {
486 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
487 "property '%s' can only be set during pool "
488 "creation or import"), propname);
489 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
493 if (strval[0] != '/') {
494 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
495 "bad alternate root '%s'"), strval);
496 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
501 case ZPOOL_PROP_CACHEFILE:
502 if (strval[0] == '\0')
505 if (strcmp(strval, "none") == 0)
508 if (strval[0] != '/') {
509 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
510 "property '%s' must be empty, an "
511 "absolute path, or 'none'"), propname);
512 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
516 slash = strrchr(strval, '/');
518 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
519 strcmp(slash, "/..") == 0) {
520 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
521 "'%s' is not a valid file"), strval);
522 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
528 if (strval[0] != '\0' &&
529 (stat64(strval, &statbuf) != 0 ||
530 !S_ISDIR(statbuf.st_mode))) {
531 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
532 "'%s' is not a valid directory"),
534 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
545 nvlist_free(retprops);
550 * Set zpool property : propname=propval.
553 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
555 zfs_cmd_t zc = { 0 };
558 nvlist_t *nvl = NULL;
562 (void) snprintf(errbuf, sizeof (errbuf),
563 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
566 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
567 return (no_memory(zhp->zpool_hdl));
569 if (nvlist_add_string(nvl, propname, propval) != 0) {
571 return (no_memory(zhp->zpool_hdl));
574 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
575 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
576 zhp->zpool_name, nvl, version, B_FALSE, errbuf)) == NULL) {
585 * Execute the corresponding ioctl() to set this property.
587 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
589 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
594 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
596 zcmd_free_nvlists(&zc);
600 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
602 (void) zpool_props_refresh(zhp);
608 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
610 libzfs_handle_t *hdl = zhp->zpool_hdl;
612 char buf[ZFS_MAXPROPLEN];
614 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
617 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
622 if (entry->pl_prop != ZPROP_INVAL &&
623 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
625 if (strlen(buf) > entry->pl_width)
626 entry->pl_width = strlen(buf);
635 * Validate the given pool name, optionally putting an extended error message in
639 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
645 ret = pool_namecheck(pool, &why, &what);
648 * The rules for reserved pool names were extended at a later point.
649 * But we need to support users with existing pools that may now be
650 * invalid. So we only check for this expanded set of names during a
651 * create (or import), and only in userland.
653 if (ret == 0 && !isopen &&
654 (strncmp(pool, "mirror", 6) == 0 ||
655 strncmp(pool, "raidz", 5) == 0 ||
656 strncmp(pool, "spare", 5) == 0 ||
657 strcmp(pool, "log") == 0)) {
660 dgettext(TEXT_DOMAIN, "name is reserved"));
668 case NAME_ERR_TOOLONG:
670 dgettext(TEXT_DOMAIN, "name is too long"));
673 case NAME_ERR_INVALCHAR:
675 dgettext(TEXT_DOMAIN, "invalid character "
676 "'%c' in pool name"), what);
679 case NAME_ERR_NOLETTER:
680 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
681 "name must begin with a letter"));
684 case NAME_ERR_RESERVED:
685 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
686 "name is reserved"));
689 case NAME_ERR_DISKLIKE:
690 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
691 "pool name is reserved"));
694 case NAME_ERR_LEADING_SLASH:
695 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
696 "leading slash in name"));
699 case NAME_ERR_EMPTY_COMPONENT:
700 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
701 "empty component in name"));
704 case NAME_ERR_TRAILING_SLASH:
705 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
706 "trailing slash in name"));
709 case NAME_ERR_MULTIPLE_AT:
710 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
711 "multiple '@' delimiters in name"));
723 * Open a handle to the given pool, even if the pool is currently in the FAULTED
727 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
733 * Make sure the pool name is valid.
735 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
736 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
737 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
742 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
745 zhp->zpool_hdl = hdl;
746 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
748 if (zpool_refresh_stats(zhp, &missing) != 0) {
754 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
755 (void) zfs_error_fmt(hdl, EZFS_NOENT,
756 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
765 * Like the above, but silent on error. Used when iterating over pools (because
766 * the configuration cache may be out of date).
769 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
774 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
777 zhp->zpool_hdl = hdl;
778 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
780 if (zpool_refresh_stats(zhp, &missing) != 0) {
796 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
800 zpool_open(libzfs_handle_t *hdl, const char *pool)
804 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
807 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
808 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
809 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
818 * Close the handle. Simply frees the memory associated with the handle.
821 zpool_close(zpool_handle_t *zhp)
823 if (zhp->zpool_config)
824 nvlist_free(zhp->zpool_config);
825 if (zhp->zpool_old_config)
826 nvlist_free(zhp->zpool_old_config);
827 if (zhp->zpool_props)
828 nvlist_free(zhp->zpool_props);
833 * Return the name of the pool.
836 zpool_get_name(zpool_handle_t *zhp)
838 return (zhp->zpool_name);
843 * Return the state of the pool (ACTIVE or UNAVAILABLE)
846 zpool_get_state(zpool_handle_t *zhp)
848 return (zhp->zpool_state);
852 * Create the named pool, using the provided vdev list. It is assumed
853 * that the consumer has already validated the contents of the nvlist, so we
854 * don't have to worry about error semantics.
857 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
858 nvlist_t *props, nvlist_t *fsprops)
860 zfs_cmd_t zc = { 0 };
861 nvlist_t *zc_fsprops = NULL;
862 nvlist_t *zc_props = NULL;
867 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
868 "cannot create '%s'"), pool);
870 if (!zpool_name_valid(hdl, B_FALSE, pool))
871 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
873 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
877 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
878 SPA_VERSION_1, B_TRUE, msg)) == NULL) {
887 zoned = ((nvlist_lookup_string(fsprops,
888 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
889 strcmp(zonestr, "on") == 0);
891 if ((zc_fsprops = zfs_valid_proplist(hdl,
892 ZFS_TYPE_FILESYSTEM, fsprops, zoned, NULL, msg)) == NULL) {
896 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
899 if (nvlist_add_nvlist(zc_props,
900 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
905 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
908 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
910 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
912 zcmd_free_nvlists(&zc);
913 nvlist_free(zc_props);
914 nvlist_free(zc_fsprops);
919 * This can happen if the user has specified the same
920 * device multiple times. We can't reliably detect this
921 * until we try to add it and see we already have a
924 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
925 "one or more vdevs refer to the same device"));
926 return (zfs_error(hdl, EZFS_BADDEV, msg));
930 * This occurs when one of the devices is below
931 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
932 * device was the problem device since there's no
933 * reliable way to determine device size from userland.
938 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
940 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
941 "one or more devices is less than the "
942 "minimum size (%s)"), buf);
944 return (zfs_error(hdl, EZFS_BADDEV, msg));
947 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
948 "one or more devices is out of space"));
949 return (zfs_error(hdl, EZFS_BADDEV, msg));
952 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
953 "cache device must be a disk or disk slice"));
954 return (zfs_error(hdl, EZFS_BADDEV, msg));
957 return (zpool_standard_error(hdl, errno, msg));
962 * If this is an alternate root pool, then we automatically set the
963 * mountpoint of the root dataset to be '/'.
965 if (nvlist_lookup_string(props, zpool_prop_to_name(ZPOOL_PROP_ALTROOT),
969 verify((zhp = zfs_open(hdl, pool, ZFS_TYPE_DATASET)) != NULL);
970 verify(zfs_prop_set(zhp, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT),
977 zcmd_free_nvlists(&zc);
978 nvlist_free(zc_props);
979 nvlist_free(zc_fsprops);
984 * Destroy the given pool. It is up to the caller to ensure that there are no
985 * datasets left in the pool.
988 zpool_destroy(zpool_handle_t *zhp)
990 zfs_cmd_t zc = { 0 };
991 zfs_handle_t *zfp = NULL;
992 libzfs_handle_t *hdl = zhp->zpool_hdl;
995 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
996 (zfp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
997 ZFS_TYPE_FILESYSTEM)) == NULL)
1000 if (zpool_remove_zvol_links(zhp) != 0)
1003 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1005 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1006 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1007 "cannot destroy '%s'"), zhp->zpool_name);
1009 if (errno == EROFS) {
1010 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1011 "one or more devices is read only"));
1012 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1014 (void) zpool_standard_error(hdl, errno, msg);
1023 remove_mountpoint(zfp);
1031 * Add the given vdevs to the pool. The caller must have already performed the
1032 * necessary verification to ensure that the vdev specification is well-formed.
1035 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1037 zfs_cmd_t zc = { 0 };
1039 libzfs_handle_t *hdl = zhp->zpool_hdl;
1041 nvlist_t **spares, **l2cache;
1042 uint_t nspares, nl2cache;
1044 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1045 "cannot add to '%s'"), zhp->zpool_name);
1047 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1048 SPA_VERSION_SPARES &&
1049 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1050 &spares, &nspares) == 0) {
1051 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1052 "upgraded to add hot spares"));
1053 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1056 if (pool_is_bootable(zhp) && nvlist_lookup_nvlist_array(nvroot,
1057 ZPOOL_CONFIG_SPARES, &spares, &nspares) == 0) {
1060 for (s = 0; s < nspares; s++) {
1063 if (nvlist_lookup_string(spares[s], ZPOOL_CONFIG_PATH,
1064 &path) == 0 && pool_uses_efi(spares[s])) {
1065 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1066 "device '%s' contains an EFI label and "
1067 "cannot be used on root pools."),
1068 zpool_vdev_name(hdl, NULL, spares[s]));
1069 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1074 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1075 SPA_VERSION_L2CACHE &&
1076 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1077 &l2cache, &nl2cache) == 0) {
1078 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1079 "upgraded to add cache devices"));
1080 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1083 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1085 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1087 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1091 * This can happen if the user has specified the same
1092 * device multiple times. We can't reliably detect this
1093 * until we try to add it and see we already have a
1096 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1097 "one or more vdevs refer to the same device"));
1098 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1103 * This occurrs when one of the devices is below
1104 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1105 * device was the problem device since there's no
1106 * reliable way to determine device size from userland.
1111 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1113 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1114 "device is less than the minimum "
1117 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1121 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1122 "pool must be upgraded to add these vdevs"));
1123 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1127 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1128 "root pool can not have multiple vdevs"
1129 " or separate logs"));
1130 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1134 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1135 "cache device must be a disk or disk slice"));
1136 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1140 (void) zpool_standard_error(hdl, errno, msg);
1148 zcmd_free_nvlists(&zc);
1154 * Exports the pool from the system. The caller must ensure that there are no
1155 * mounted datasets in the pool.
1158 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce)
1160 zfs_cmd_t zc = { 0 };
1163 if (zpool_remove_zvol_links(zhp) != 0)
1166 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1167 "cannot export '%s'"), zhp->zpool_name);
1169 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1170 zc.zc_cookie = force;
1171 zc.zc_guid = hardforce;
1173 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1176 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1177 "use '-f' to override the following errors:\n"
1178 "'%s' has an active shared spare which could be"
1179 " used by other pools once '%s' is exported."),
1180 zhp->zpool_name, zhp->zpool_name);
1181 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1184 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1193 zpool_export(zpool_handle_t *zhp, boolean_t force)
1195 return (zpool_export_common(zhp, force, B_FALSE));
1199 zpool_export_force(zpool_handle_t *zhp)
1201 return (zpool_export_common(zhp, B_TRUE, B_TRUE));
1205 * zpool_import() is a contracted interface. Should be kept the same
1208 * Applications should use zpool_import_props() to import a pool with
1209 * new properties value to be set.
1212 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1215 nvlist_t *props = NULL;
1218 if (altroot != NULL) {
1219 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1220 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1221 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1225 if (nvlist_add_string(props,
1226 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1227 nvlist_add_string(props,
1228 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1230 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1231 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1236 ret = zpool_import_props(hdl, config, newname, props, B_FALSE);
1243 * Import the given pool using the known configuration and a list of
1244 * properties to be set. The configuration should have come from
1245 * zpool_find_import(). The 'newname' parameters control whether the pool
1246 * is imported with a different name.
1249 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1250 nvlist_t *props, boolean_t importfaulted)
1252 zfs_cmd_t zc = { 0 };
1258 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1261 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1262 "cannot import pool '%s'"), origname);
1264 if (newname != NULL) {
1265 if (!zpool_name_valid(hdl, B_FALSE, newname))
1266 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1267 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1269 thename = (char *)newname;
1277 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1280 if ((props = zpool_valid_proplist(hdl, origname,
1281 props, version, B_TRUE, errbuf)) == NULL) {
1283 } else if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1289 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1291 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1294 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1299 zc.zc_cookie = (uint64_t)importfaulted;
1301 if (zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc) != 0) {
1303 if (newname == NULL)
1304 (void) snprintf(desc, sizeof (desc),
1305 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1308 (void) snprintf(desc, sizeof (desc),
1309 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1315 * Unsupported version.
1317 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1321 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1325 (void) zpool_standard_error(hdl, errno, desc);
1330 zpool_handle_t *zhp;
1333 * This should never fail, but play it safe anyway.
1335 if (zpool_open_silent(hdl, thename, &zhp) != 0) {
1337 } else if (zhp != NULL) {
1338 ret = zpool_create_zvol_links(zhp);
1344 zcmd_free_nvlists(&zc);
1354 zpool_scrub(zpool_handle_t *zhp, pool_scrub_type_t type)
1356 zfs_cmd_t zc = { 0 };
1358 libzfs_handle_t *hdl = zhp->zpool_hdl;
1360 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1361 zc.zc_cookie = type;
1363 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SCRUB, &zc) == 0)
1366 (void) snprintf(msg, sizeof (msg),
1367 dgettext(TEXT_DOMAIN, "cannot scrub %s"), zc.zc_name);
1370 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1372 return (zpool_standard_error(hdl, errno, msg));
1376 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1377 * spare; but FALSE if its an INUSE spare.
1380 vdev_to_nvlist_iter(nvlist_t *nv, const char *search, uint64_t guid,
1381 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
1385 uint64_t theguid, present;
1387 uint64_t wholedisk = 0;
1391 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &theguid) == 0);
1393 if (search == NULL &&
1394 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &present) == 0) {
1396 * If the device has never been present since import, the only
1397 * reliable way to match the vdev is by GUID.
1399 if (theguid == guid)
1401 } else if (search != NULL &&
1402 nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
1403 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
1407 * For whole disks, the internal path has 's0', but the
1408 * path passed in by the user doesn't.
1410 if (strlen(search) == strlen(path) - 2 &&
1411 strncmp(search, path, strlen(search)) == 0)
1413 } else if (strcmp(search, path) == 0) {
1418 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1419 &child, &children) != 0)
1422 for (c = 0; c < children; c++) {
1423 if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1424 avail_spare, l2cache, NULL)) != NULL) {
1426 * The 'is_log' value is only set for the toplevel
1427 * vdev, not the leaf vdevs. So we always lookup the
1428 * log device from the root of the vdev tree (where
1429 * 'log' is non-NULL).
1432 nvlist_lookup_uint64(child[c],
1433 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
1441 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
1442 &child, &children) == 0) {
1443 for (c = 0; c < children; c++) {
1444 if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1445 avail_spare, l2cache, NULL)) != NULL) {
1446 *avail_spare = B_TRUE;
1452 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
1453 &child, &children) == 0) {
1454 for (c = 0; c < children; c++) {
1455 if ((ret = vdev_to_nvlist_iter(child[c], search, guid,
1456 avail_spare, l2cache, NULL)) != NULL) {
1467 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
1468 boolean_t *l2cache, boolean_t *log)
1470 char buf[MAXPATHLEN];
1476 guid = strtoull(path, &end, 10);
1477 if (guid != 0 && *end == '\0') {
1479 } else if (path[0] != '/') {
1480 (void) snprintf(buf, sizeof (buf), "%s%s", _PATH_DEV, path);
1486 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1489 *avail_spare = B_FALSE;
1493 return (vdev_to_nvlist_iter(nvroot, search, guid, avail_spare,
1498 vdev_online(nvlist_t *nv)
1502 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
1503 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
1504 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
1511 * Get phys_path for a root pool
1512 * Return 0 on success; non-zeron on failure.
1515 zpool_get_physpath(zpool_handle_t *zhp, char *physpath)
1517 nvlist_t *vdev_root;
1523 * Make sure this is a root pool, as phys_path doesn't mean
1524 * anything to a non-root pool.
1526 if (!pool_is_bootable(zhp))
1529 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1530 ZPOOL_CONFIG_VDEV_TREE, &vdev_root) == 0);
1532 if (nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
1533 &child, &count) != 0)
1536 for (i = 0; i < count; i++) {
1543 if (nvlist_lookup_string(child[i], ZPOOL_CONFIG_TYPE, &type)
1547 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
1548 if (!vdev_online(child[i]))
1550 verify(nvlist_lookup_string(child[i],
1551 ZPOOL_CONFIG_PHYS_PATH, &tmppath) == 0);
1552 (void) strncpy(physpath, tmppath, strlen(tmppath));
1553 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0) {
1554 if (nvlist_lookup_nvlist_array(child[i],
1555 ZPOOL_CONFIG_CHILDREN, &child2, &count2) != 0)
1558 for (j = 0; j < count2; j++) {
1559 if (!vdev_online(child2[j]))
1561 if (nvlist_lookup_string(child2[j],
1562 ZPOOL_CONFIG_PHYS_PATH, &tmppath) != 0)
1565 if ((strlen(physpath) + strlen(tmppath)) >
1569 if (strlen(physpath) == 0) {
1570 (void) strncpy(physpath, tmppath,
1573 (void) strcat(physpath, " ");
1574 (void) strcat(physpath, tmppath);
1586 * Returns TRUE if the given guid corresponds to the given type.
1587 * This is used to check for hot spares (INUSE or not), and level 2 cache
1591 is_guid_type(zpool_handle_t *zhp, uint64_t guid, const char *type)
1593 uint64_t target_guid;
1599 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
1601 if (nvlist_lookup_nvlist_array(nvroot, type, &list, &count) == 0) {
1602 for (i = 0; i < count; i++) {
1603 verify(nvlist_lookup_uint64(list[i], ZPOOL_CONFIG_GUID,
1604 &target_guid) == 0);
1605 if (guid == target_guid)
1614 * Bring the specified vdev online. The 'flags' parameter is a set of the
1615 * ZFS_ONLINE_* flags.
1618 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
1619 vdev_state_t *newstate)
1621 zfs_cmd_t zc = { 0 };
1624 boolean_t avail_spare, l2cache;
1625 libzfs_handle_t *hdl = zhp->zpool_hdl;
1627 (void) snprintf(msg, sizeof (msg),
1628 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
1630 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1631 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1633 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1635 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1638 is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
1639 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1641 zc.zc_cookie = VDEV_STATE_ONLINE;
1644 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0)
1645 return (zpool_standard_error(hdl, errno, msg));
1647 *newstate = zc.zc_cookie;
1652 * Take the specified vdev offline
1655 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
1657 zfs_cmd_t zc = { 0 };
1660 boolean_t avail_spare, l2cache;
1661 libzfs_handle_t *hdl = zhp->zpool_hdl;
1663 (void) snprintf(msg, sizeof (msg),
1664 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
1666 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1667 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1669 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1671 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1674 is_guid_type(zhp, zc.zc_guid, ZPOOL_CONFIG_SPARES) == B_TRUE)
1675 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1677 zc.zc_cookie = VDEV_STATE_OFFLINE;
1678 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
1680 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1687 * There are no other replicas of this device.
1689 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
1692 return (zpool_standard_error(hdl, errno, msg));
1697 * Mark the given vdev faulted.
1700 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid)
1702 zfs_cmd_t zc = { 0 };
1704 libzfs_handle_t *hdl = zhp->zpool_hdl;
1706 (void) snprintf(msg, sizeof (msg),
1707 dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
1709 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1711 zc.zc_cookie = VDEV_STATE_FAULTED;
1713 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1720 * There are no other replicas of this device.
1722 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
1725 return (zpool_standard_error(hdl, errno, msg));
1731 * Mark the given vdev degraded.
1734 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid)
1736 zfs_cmd_t zc = { 0 };
1738 libzfs_handle_t *hdl = zhp->zpool_hdl;
1740 (void) snprintf(msg, sizeof (msg),
1741 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
1743 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1745 zc.zc_cookie = VDEV_STATE_DEGRADED;
1747 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
1750 return (zpool_standard_error(hdl, errno, msg));
1754 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
1758 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
1764 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
1766 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
1769 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
1770 children == 2 && child[which] == tgt)
1773 for (c = 0; c < children; c++)
1774 if (is_replacing_spare(child[c], tgt, which))
1782 * Attach new_disk (fully described by nvroot) to old_disk.
1783 * If 'replacing' is specified, the new disk will replace the old one.
1786 zpool_vdev_attach(zpool_handle_t *zhp,
1787 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
1789 zfs_cmd_t zc = { 0 };
1793 boolean_t avail_spare, l2cache, islog;
1795 char *path, *newname;
1798 nvlist_t *config_root;
1799 libzfs_handle_t *hdl = zhp->zpool_hdl;
1800 boolean_t rootpool = pool_is_bootable(zhp);
1803 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1804 "cannot replace %s with %s"), old_disk, new_disk);
1806 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1807 "cannot attach %s to %s"), new_disk, old_disk);
1810 * If this is a root pool, make sure that we're not attaching an
1811 * EFI labeled device.
1813 if (rootpool && pool_uses_efi(nvroot)) {
1814 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1815 "EFI labeled devices are not supported on root pools."));
1816 return (zfs_error(hdl, EZFS_POOL_NOTSUP, msg));
1819 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1820 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
1822 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1825 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1828 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
1830 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1831 zc.zc_cookie = replacing;
1833 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
1834 &child, &children) != 0 || children != 1) {
1835 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1836 "new device must be a single disk"));
1837 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
1840 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
1841 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
1843 if ((newname = zpool_vdev_name(NULL, NULL, child[0])) == NULL)
1847 * If the target is a hot spare that has been swapped in, we can only
1848 * replace it with another hot spare.
1851 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
1852 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
1853 NULL) == NULL || !avail_spare) &&
1854 is_replacing_spare(config_root, tgt, 1)) {
1855 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1856 "can only be replaced by another hot spare"));
1858 return (zfs_error(hdl, EZFS_BADTARGET, msg));
1862 * If we are attempting to replace a spare, it canot be applied to an
1863 * already spared device.
1866 nvlist_lookup_string(child[0], ZPOOL_CONFIG_PATH, &path) == 0 &&
1867 zpool_find_vdev(zhp, newname, &avail_spare,
1868 &l2cache, NULL) != NULL && avail_spare &&
1869 is_replacing_spare(config_root, tgt, 0)) {
1870 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1871 "device has already been replaced with a spare"));
1873 return (zfs_error(hdl, EZFS_BADTARGET, msg));
1878 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1881 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_VDEV_ATTACH, &zc);
1883 zcmd_free_nvlists(&zc);
1888 * XXX - This should be removed once we can
1889 * automatically install the bootblocks on the
1890 * newly attached disk.
1892 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Please "
1893 "be sure to invoke %s to make '%s' bootable.\n"),
1902 * Can't attach to or replace this type of vdev.
1906 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1907 "cannot replace a log with a spare"));
1909 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1910 "cannot replace a replacing device"));
1912 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1913 "can only attach to mirrors and top-level "
1916 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
1921 * The new device must be a single disk.
1923 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1924 "new device must be a single disk"));
1925 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
1929 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
1931 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1936 * The new device is too small.
1938 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1939 "device is too small"));
1940 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1945 * The new device has a different alignment requirement.
1947 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1948 "devices have different sector alignment"));
1949 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1954 * The resulting top-level vdev spec won't fit in the label.
1956 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
1960 (void) zpool_standard_error(hdl, errno, msg);
1967 * Detach the specified device.
1970 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
1972 zfs_cmd_t zc = { 0 };
1975 boolean_t avail_spare, l2cache;
1976 libzfs_handle_t *hdl = zhp->zpool_hdl;
1978 (void) snprintf(msg, sizeof (msg),
1979 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
1981 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1982 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
1984 return (zfs_error(hdl, EZFS_NODEVICE, msg));
1987 return (zfs_error(hdl, EZFS_ISSPARE, msg));
1990 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
1992 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
1994 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2001 * Can't detach from this type of vdev.
2003 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2004 "applicable to mirror and replacing vdevs"));
2005 (void) zfs_error(zhp->zpool_hdl, EZFS_BADTARGET, msg);
2010 * There are no other replicas of this device.
2012 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2016 (void) zpool_standard_error(hdl, errno, msg);
2023 * Remove the given device. Currently, this is supported only for hot spares
2024 * and level 2 cache devices.
2027 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
2029 zfs_cmd_t zc = { 0 };
2032 boolean_t avail_spare, l2cache;
2033 libzfs_handle_t *hdl = zhp->zpool_hdl;
2035 (void) snprintf(msg, sizeof (msg),
2036 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
2038 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2039 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2041 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2043 if (!avail_spare && !l2cache) {
2044 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2045 "only inactive hot spares or cache devices "
2047 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2050 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2052 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
2055 return (zpool_standard_error(hdl, errno, msg));
2059 * Clear the errors for the pool, or the particular device if specified.
2062 zpool_clear(zpool_handle_t *zhp, const char *path)
2064 zfs_cmd_t zc = { 0 };
2067 boolean_t avail_spare, l2cache;
2068 libzfs_handle_t *hdl = zhp->zpool_hdl;
2071 (void) snprintf(msg, sizeof (msg),
2072 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2075 (void) snprintf(msg, sizeof (msg),
2076 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
2079 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2081 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
2082 &l2cache, NULL)) == 0)
2083 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2086 * Don't allow error clearing for hot spares. Do allow
2087 * error clearing for l2cache devices.
2090 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2092 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
2096 if (zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc) == 0)
2099 return (zpool_standard_error(hdl, errno, msg));
2103 * Similar to zpool_clear(), but takes a GUID (used by fmd).
2106 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
2108 zfs_cmd_t zc = { 0 };
2110 libzfs_handle_t *hdl = zhp->zpool_hdl;
2112 (void) snprintf(msg, sizeof (msg),
2113 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
2116 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2119 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
2122 return (zpool_standard_error(hdl, errno, msg));
2126 * Iterate over all zvols in a given pool by walking the /dev/zvol/dsk/<pool>
2130 zpool_iter_zvol(zpool_handle_t *zhp, int (*cb)(const char *, void *),
2133 libzfs_handle_t *hdl = zhp->zpool_hdl;
2134 char (*paths)[MAXPATHLEN];
2135 char path[MAXPATHLEN];
2137 int curr, fd, base, ret = 0;
2142 if ((base = open(ZVOL_FULL_DEV_DIR, O_RDONLY)) < 0)
2143 return (errno == ENOENT ? 0 : -1);
2145 snprintf(path, sizeof(path), "%s/%s", ZVOL_FULL_DEV_DIR,
2147 if (stat(path, &st) != 0) {
2150 return (err == ENOENT ? 0 : -1);
2154 * Oddly this wasn't a directory -- ignore that failure since we
2155 * know there are no links lower in the (non-existant) hierarchy.
2157 if (!S_ISDIR(st.st_mode)) {
2162 if ((paths = zfs_alloc(hdl, size * sizeof (paths[0]))) == NULL) {
2167 (void) strlcpy(paths[0], zhp->zpool_name, sizeof (paths[0]));
2171 snprintf(path, sizeof(path), "%s/%s", ZVOL_FULL_DEV_DIR,
2173 if (lstat(path, &st) != 0)
2176 if (S_ISDIR(st.st_mode)) {
2177 if ((dirp = opendir(path)) == NULL) {
2181 while ((dp = readdir(dirp)) != NULL) {
2182 if (dp->d_name[0] == '.')
2185 if (curr + 1 == size) {
2186 paths = zfs_realloc(hdl, paths,
2187 size * sizeof (paths[0]),
2188 size * 2 * sizeof (paths[0]));
2189 if (paths == NULL) {
2190 (void) closedir(dirp);
2197 (void) strlcpy(paths[curr + 1], paths[curr],
2198 sizeof (paths[curr + 1]));
2199 (void) strlcat(paths[curr], "/",
2200 sizeof (paths[curr]));
2201 (void) strlcat(paths[curr], dp->d_name,
2202 sizeof (paths[curr]));
2206 (void) closedir(dirp);
2209 if ((ret = cb(paths[curr], data)) != 0)
2227 typedef struct zvol_cb {
2228 zpool_handle_t *zcb_pool;
2229 boolean_t zcb_create;
2234 do_zvol_create(zfs_handle_t *zhp, void *data)
2238 if (ZFS_IS_VOLUME(zhp)) {
2239 (void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name);
2240 ret = zfs_iter_snapshots(zhp, do_zvol_create, NULL);
2244 ret = zfs_iter_filesystems(zhp, do_zvol_create, NULL);
2252 * Iterate over all zvols in the pool and make any necessary minor nodes.
2255 zpool_create_zvol_links(zpool_handle_t *zhp)
2261 * If the pool is unavailable, just return success.
2263 if ((zfp = make_dataset_handle(zhp->zpool_hdl,
2264 zhp->zpool_name)) == NULL)
2267 ret = zfs_iter_filesystems(zfp, do_zvol_create, NULL);
2274 do_zvol_remove(const char *dataset, void *data)
2276 zpool_handle_t *zhp = data;
2278 return (zvol_remove_link(zhp->zpool_hdl, dataset));
2282 * Iterate over all zvols in the pool and remove any minor nodes. We iterate
2283 * by examining the /dev links so that a corrupted pool doesn't impede this
2287 zpool_remove_zvol_links(zpool_handle_t *zhp)
2289 return (zpool_iter_zvol(zhp, do_zvol_remove, zhp));
2293 * Convert from a devid string to a path.
2296 devid_to_path(char *devid_str)
2301 devid_nmlist_t *list = NULL;
2304 if (devid_str_decode(devid_str, &devid, &minor) != 0)
2307 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
2309 devid_str_free(minor);
2315 if ((path = strdup(list[0].devname)) == NULL)
2318 devid_free_nmlist(list);
2324 * Convert from a path to a devid string.
2327 path_to_devid(const char *path)
2333 if ((fd = open(path, O_RDONLY)) < 0)
2338 if (devid_get(fd, &devid) == 0) {
2339 if (devid_get_minor_name(fd, &minor) == 0)
2340 ret = devid_str_encode(devid, minor);
2342 devid_str_free(minor);
2351 * Issue the necessary ioctl() to update the stored path value for the vdev. We
2352 * ignore any failure here, since a common case is for an unprivileged user to
2353 * type 'zpool status', and we'll display the correct information anyway.
2356 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
2358 zfs_cmd_t zc = { 0 };
2360 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2361 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
2362 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2365 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
2369 * Given a vdev, return the name to display in iostat. If the vdev has a path,
2370 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
2371 * We also check if this is a whole disk, in which case we strip off the
2372 * trailing 's0' slice name.
2374 * This routine is also responsible for identifying when disks have been
2375 * reconfigured in a new location. The kernel will have opened the device by
2376 * devid, but the path will still refer to the old location. To catch this, we
2377 * first do a path -> devid translation (which is fast for the common case). If
2378 * the devid matches, we're done. If not, we do a reverse devid -> path
2379 * translation and issue the appropriate ioctl() to update the path of the vdev.
2380 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
2384 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv)
2392 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT,
2394 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2396 (void) snprintf(buf, sizeof (buf), "%llu",
2397 (u_longlong_t)value);
2399 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
2402 * If the device is dead (faulted, offline, etc) then don't
2403 * bother opening it. Otherwise we may be forcing the user to
2404 * open a misbehaving device, which can have undesirable
2407 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
2408 (uint64_t **)&vs, &vsc) != 0 ||
2409 vs->vs_state >= VDEV_STATE_DEGRADED) &&
2411 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
2413 * Determine if the current path is correct.
2415 char *newdevid = path_to_devid(path);
2417 if (newdevid == NULL ||
2418 strcmp(devid, newdevid) != 0) {
2421 if ((newpath = devid_to_path(devid)) != NULL) {
2423 * Update the path appropriately.
2425 set_path(zhp, nv, newpath);
2426 if (nvlist_add_string(nv,
2427 ZPOOL_CONFIG_PATH, newpath) == 0)
2428 verify(nvlist_lookup_string(nv,
2436 devid_str_free(newdevid);
2439 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
2440 path += sizeof(_PATH_DEV) - 1;
2442 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2443 &value) == 0 && value) {
2444 char *tmp = zfs_strdup(hdl, path);
2447 tmp[strlen(path) - 2] = '\0';
2451 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
2454 * If it's a raidz device, we need to stick in the parity level.
2456 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
2457 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
2459 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
2460 (u_longlong_t)value);
2465 return (zfs_strdup(hdl, path));
2469 zbookmark_compare(const void *a, const void *b)
2471 return (memcmp(a, b, sizeof (zbookmark_t)));
2475 * Retrieve the persistent error log, uniquify the members, and return to the
2479 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
2481 zfs_cmd_t zc = { 0 };
2483 zbookmark_t *zb = NULL;
2487 * Retrieve the raw error list from the kernel. If the number of errors
2488 * has increased, allocate more space and continue until we get the
2491 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
2495 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
2496 count * sizeof (zbookmark_t))) == (uintptr_t)NULL)
2498 zc.zc_nvlist_dst_size = count;
2499 (void) strcpy(zc.zc_name, zhp->zpool_name);
2501 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
2503 free((void *)(uintptr_t)zc.zc_nvlist_dst);
2504 if (errno == ENOMEM) {
2505 count = zc.zc_nvlist_dst_size;
2506 if ((zc.zc_nvlist_dst = (uintptr_t)
2507 zfs_alloc(zhp->zpool_hdl, count *
2508 sizeof (zbookmark_t))) == (uintptr_t)NULL)
2519 * Sort the resulting bookmarks. This is a little confusing due to the
2520 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
2521 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
2522 * _not_ copied as part of the process. So we point the start of our
2523 * array appropriate and decrement the total number of elements.
2525 zb = ((zbookmark_t *)(uintptr_t)zc.zc_nvlist_dst) +
2526 zc.zc_nvlist_dst_size;
2527 count -= zc.zc_nvlist_dst_size;
2529 qsort(zb, count, sizeof (zbookmark_t), zbookmark_compare);
2531 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
2534 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
2536 for (i = 0; i < count; i++) {
2539 /* ignoring zb_blkid and zb_level for now */
2540 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
2541 zb[i-1].zb_object == zb[i].zb_object)
2544 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
2546 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
2547 zb[i].zb_objset) != 0) {
2551 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
2552 zb[i].zb_object) != 0) {
2556 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
2563 free((void *)(uintptr_t)zc.zc_nvlist_dst);
2567 free((void *)(uintptr_t)zc.zc_nvlist_dst);
2568 return (no_memory(zhp->zpool_hdl));
2572 * Upgrade a ZFS pool to the latest on-disk version.
2575 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
2577 zfs_cmd_t zc = { 0 };
2578 libzfs_handle_t *hdl = zhp->zpool_hdl;
2580 (void) strcpy(zc.zc_name, zhp->zpool_name);
2581 zc.zc_cookie = new_version;
2583 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
2584 return (zpool_standard_error_fmt(hdl, errno,
2585 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
2591 zpool_set_history_str(const char *subcommand, int argc, char **argv,
2596 (void) strlcpy(history_str, subcommand, HIS_MAX_RECORD_LEN);
2597 for (i = 1; i < argc; i++) {
2598 if (strlen(history_str) + 1 + strlen(argv[i]) >
2601 (void) strlcat(history_str, " ", HIS_MAX_RECORD_LEN);
2602 (void) strlcat(history_str, argv[i], HIS_MAX_RECORD_LEN);
2607 * Stage command history for logging.
2610 zpool_stage_history(libzfs_handle_t *hdl, const char *history_str)
2612 if (history_str == NULL)
2615 if (strlen(history_str) > HIS_MAX_RECORD_LEN)
2618 if (hdl->libzfs_log_str != NULL)
2619 free(hdl->libzfs_log_str);
2621 if ((hdl->libzfs_log_str = strdup(history_str)) == NULL)
2622 return (no_memory(hdl));
2628 * Perform ioctl to get some command history of a pool.
2630 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
2631 * logical offset of the history buffer to start reading from.
2633 * Upon return, 'off' is the next logical offset to read from and
2634 * 'len' is the actual amount of bytes read into 'buf'.
2637 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
2639 zfs_cmd_t zc = { 0 };
2640 libzfs_handle_t *hdl = zhp->zpool_hdl;
2642 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2644 zc.zc_history = (uint64_t)(uintptr_t)buf;
2645 zc.zc_history_len = *len;
2646 zc.zc_history_offset = *off;
2648 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
2651 return (zfs_error_fmt(hdl, EZFS_PERM,
2652 dgettext(TEXT_DOMAIN,
2653 "cannot show history for pool '%s'"),
2656 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
2657 dgettext(TEXT_DOMAIN, "cannot get history for pool "
2658 "'%s'"), zhp->zpool_name));
2660 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
2661 dgettext(TEXT_DOMAIN, "cannot get history for pool "
2662 "'%s', pool must be upgraded"), zhp->zpool_name));
2664 return (zpool_standard_error_fmt(hdl, errno,
2665 dgettext(TEXT_DOMAIN,
2666 "cannot get history for '%s'"), zhp->zpool_name));
2670 *len = zc.zc_history_len;
2671 *off = zc.zc_history_offset;
2677 * Process the buffer of nvlists, unpacking and storing each nvlist record
2678 * into 'records'. 'leftover' is set to the number of bytes that weren't
2679 * processed as there wasn't a complete record.
2682 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
2683 nvlist_t ***records, uint_t *numrecords)
2689 while (bytes_read > sizeof (reclen)) {
2691 /* get length of packed record (stored as little endian) */
2692 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
2693 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
2695 if (bytes_read < sizeof (reclen) + reclen)
2699 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
2701 bytes_read -= sizeof (reclen) + reclen;
2702 buf += sizeof (reclen) + reclen;
2704 /* add record to nvlist array */
2706 if (ISP2(*numrecords + 1)) {
2707 *records = realloc(*records,
2708 *numrecords * 2 * sizeof (nvlist_t *));
2710 (*records)[*numrecords - 1] = nv;
2713 *leftover = bytes_read;
2717 #define HIS_BUF_LEN (128*1024)
2720 * Retrieve the command history of a pool.
2723 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
2725 char buf[HIS_BUF_LEN];
2727 nvlist_t **records = NULL;
2728 uint_t numrecords = 0;
2732 uint64_t bytes_read = sizeof (buf);
2735 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
2738 /* if nothing else was read in, we're at EOF, just return */
2742 if ((err = zpool_history_unpack(buf, bytes_read,
2743 &leftover, &records, &numrecords)) != 0)
2751 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
2752 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
2753 records, numrecords) == 0);
2755 for (i = 0; i < numrecords; i++)
2756 nvlist_free(records[i]);
2763 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
2764 char *pathname, size_t len)
2766 zfs_cmd_t zc = { 0 };
2767 boolean_t mounted = B_FALSE;
2768 char *mntpnt = NULL;
2769 char dsname[MAXNAMELEN];
2772 /* special case for the MOS */
2773 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
2777 /* get the dataset's name */
2778 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2780 if (ioctl(zhp->zpool_hdl->libzfs_fd,
2781 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
2782 /* just write out a path of two object numbers */
2783 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
2787 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
2789 /* find out if the dataset is mounted */
2790 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
2792 /* get the corrupted object's path */
2793 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
2795 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
2798 (void) snprintf(pathname, len, "%s%s", mntpnt,
2801 (void) snprintf(pathname, len, "%s:%s",
2802 dsname, zc.zc_value);
2805 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
2810 #define RDISK_ROOT "/dev/rdsk"
2811 #define BACKUP_SLICE "s2"
2813 * Don't start the slice at the default block of 34; many storage
2814 * devices will use a stripe width of 128k, so start there instead.
2816 #define NEW_START_BLOCK 256
2820 * Read the EFI label from the config, if a label does not exist then
2821 * pass back the error to the caller. If the caller has passed a non-NULL
2822 * diskaddr argument then we set it to the starting address of the EFI
2826 read_efi_label(nvlist_t *config, diskaddr_t *sb)
2830 char diskname[MAXPATHLEN];
2833 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
2836 (void) snprintf(diskname, sizeof (diskname), "%s%s", RDISK_ROOT,
2837 strrchr(path, '/'));
2838 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
2839 struct dk_gpt *vtoc;
2841 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
2843 *sb = vtoc->efi_parts[0].p_start;
2852 * determine where a partition starts on a disk in the current
2856 find_start_block(nvlist_t *config)
2860 diskaddr_t sb = MAXOFFSET_T;
2863 if (nvlist_lookup_nvlist_array(config,
2864 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
2865 if (nvlist_lookup_uint64(config,
2866 ZPOOL_CONFIG_WHOLE_DISK,
2867 &wholedisk) != 0 || !wholedisk) {
2868 return (MAXOFFSET_T);
2870 if (read_efi_label(config, &sb) < 0)
2875 for (c = 0; c < children; c++) {
2876 sb = find_start_block(child[c]);
2877 if (sb != MAXOFFSET_T) {
2881 return (MAXOFFSET_T);
2886 * Label an individual disk. The name provided is the short name,
2887 * stripped of any leading /dev path.
2890 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
2893 char path[MAXPATHLEN];
2894 struct dk_gpt *vtoc;
2896 size_t resv = EFI_MIN_RESV_SIZE;
2897 uint64_t slice_size;
2898 diskaddr_t start_block;
2901 /* prepare an error message just in case */
2902 (void) snprintf(errbuf, sizeof (errbuf),
2903 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
2908 if (pool_is_bootable(zhp)) {
2909 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2910 "EFI labeled devices are not supported on root "
2912 return (zfs_error(hdl, EZFS_POOL_NOTSUP, errbuf));
2915 verify(nvlist_lookup_nvlist(zhp->zpool_config,
2916 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2918 if (zhp->zpool_start_block == 0)
2919 start_block = find_start_block(nvroot);
2921 start_block = zhp->zpool_start_block;
2922 zhp->zpool_start_block = start_block;
2925 start_block = NEW_START_BLOCK;
2928 (void) snprintf(path, sizeof (path), "%s/%s%s", RDISK_ROOT, name,
2931 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2933 * This shouldn't happen. We've long since verified that this
2934 * is a valid device.
2937 dgettext(TEXT_DOMAIN, "unable to open device"));
2938 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2941 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
2943 * The only way this can fail is if we run out of memory, or we
2944 * were unable to read the disk's capacity
2946 if (errno == ENOMEM)
2947 (void) no_memory(hdl);
2950 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2951 "unable to read disk capacity"), name);
2953 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2956 slice_size = vtoc->efi_last_u_lba + 1;
2957 slice_size -= EFI_MIN_RESV_SIZE;
2958 if (start_block == MAXOFFSET_T)
2959 start_block = NEW_START_BLOCK;
2960 slice_size -= start_block;
2962 vtoc->efi_parts[0].p_start = start_block;
2963 vtoc->efi_parts[0].p_size = slice_size;
2966 * Why we use V_USR: V_BACKUP confuses users, and is considered
2967 * disposable by some EFI utilities (since EFI doesn't have a backup
2968 * slice). V_UNASSIGNED is supposed to be used only for zero size
2969 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
2970 * etc. were all pretty specific. V_USR is as close to reality as we
2971 * can get, in the absence of V_OTHER.
2973 vtoc->efi_parts[0].p_tag = V_USR;
2974 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
2976 vtoc->efi_parts[8].p_start = slice_size + start_block;
2977 vtoc->efi_parts[8].p_size = resv;
2978 vtoc->efi_parts[8].p_tag = V_RESERVED;
2980 if (efi_write(fd, vtoc) != 0) {
2982 * Some block drivers (like pcata) may not support EFI
2983 * GPT labels. Print out a helpful error message dir-
2984 * ecting the user to manually label the disk and give
2990 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2991 "try using fdisk(1M) and then provide a specific slice"));
2992 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
3002 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
3008 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
3009 if (strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
3010 strcmp(type, VDEV_TYPE_FILE) == 0 ||
3011 strcmp(type, VDEV_TYPE_LOG) == 0 ||
3012 strcmp(type, VDEV_TYPE_MISSING) == 0) {
3013 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3014 "vdev type '%s' is not supported"), type);
3015 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
3018 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
3019 &child, &children) == 0) {
3020 for (c = 0; c < children; c++) {
3021 if (!supported_dump_vdev_type(hdl, child[c], errbuf))
3029 * check if this zvol is allowable for use as a dump device; zero if
3030 * it is, > 0 if it isn't, < 0 if it isn't a zvol
3033 zvol_check_dump_config(char *arg)
3035 zpool_handle_t *zhp = NULL;
3036 nvlist_t *config, *nvroot;
3040 libzfs_handle_t *hdl;
3042 char poolname[ZPOOL_MAXNAMELEN];
3043 int pathlen = strlen(ZVOL_FULL_DEV_DIR);
3046 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
3050 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3051 "dump is not supported on device '%s'"), arg);
3053 if ((hdl = libzfs_init()) == NULL)
3055 libzfs_print_on_error(hdl, B_TRUE);
3057 volname = arg + pathlen;
3059 /* check the configuration of the pool */
3060 if ((p = strchr(volname, '/')) == NULL) {
3061 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3062 "malformed dataset name"));
3063 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3065 } else if (p - volname >= ZFS_MAXNAMELEN) {
3066 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3067 "dataset name is too long"));
3068 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
3071 (void) strncpy(poolname, volname, p - volname);
3072 poolname[p - volname] = '\0';
3075 if ((zhp = zpool_open(hdl, poolname)) == NULL) {
3076 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3077 "could not open pool '%s'"), poolname);
3078 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
3081 config = zpool_get_config(zhp, NULL);
3082 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
3084 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3085 "could not obtain vdev configuration for '%s'"), poolname);
3086 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
3090 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
3091 &top, &toplevels) == 0);
3092 if (toplevels != 1) {
3093 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3094 "'%s' has multiple top level vdevs"), poolname);
3095 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, errbuf);
3099 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {