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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2017 by Delphix. All rights reserved.
25 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
26 * Copyright 2016 Nexenta Systems, Inc.
27 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
28 * Copyright (c) 2017 Datto Inc.
29 * Copyright (c) 2017, Intel Corporation.
32 #include <sys/types.h>
44 #include <sys/zfs_ioctl.h>
47 #include "zfs_namecheck.h"
49 #include "libzfs_impl.h"
50 #include "zfs_comutil.h"
51 #include "zfeature_common.h"
53 static int read_efi_label(nvlist_t *, diskaddr_t *, boolean_t *);
54 static boolean_t zpool_vdev_is_interior(const char *name);
56 #define BACKUP_SLICE "s2"
58 typedef struct prop_flags {
59 int create:1; /* Validate property on creation */
60 int import:1; /* Validate property on import */
64 * ====================================================================
65 * zpool property functions
66 * ====================================================================
70 zpool_get_all_props(zpool_handle_t *zhp)
73 libzfs_handle_t *hdl = zhp->zpool_hdl;
75 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
77 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
80 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
81 if (errno == ENOMEM) {
82 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
83 zcmd_free_nvlists(&zc);
87 zcmd_free_nvlists(&zc);
92 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
93 zcmd_free_nvlists(&zc);
97 zcmd_free_nvlists(&zc);
103 zpool_props_refresh(zpool_handle_t *zhp)
107 old_props = zhp->zpool_props;
109 if (zpool_get_all_props(zhp) != 0)
112 nvlist_free(old_props);
117 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
123 zprop_source_t source;
125 nvl = zhp->zpool_props;
126 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
127 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
129 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
131 source = ZPROP_SRC_DEFAULT;
132 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
143 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
147 zprop_source_t source;
149 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
151 * zpool_get_all_props() has most likely failed because
152 * the pool is faulted, but if all we need is the top level
153 * vdev's guid then get it from the zhp config nvlist.
155 if ((prop == ZPOOL_PROP_GUID) &&
156 (nvlist_lookup_nvlist(zhp->zpool_config,
157 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
158 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
162 return (zpool_prop_default_numeric(prop));
165 nvl = zhp->zpool_props;
166 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
167 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
169 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
171 source = ZPROP_SRC_DEFAULT;
172 value = zpool_prop_default_numeric(prop);
182 * Map VDEV STATE to printed strings.
185 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
188 case VDEV_STATE_CLOSED:
189 case VDEV_STATE_OFFLINE:
190 return (gettext("OFFLINE"));
191 case VDEV_STATE_REMOVED:
192 return (gettext("REMOVED"));
193 case VDEV_STATE_CANT_OPEN:
194 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
195 return (gettext("FAULTED"));
196 else if (aux == VDEV_AUX_SPLIT_POOL)
197 return (gettext("SPLIT"));
199 return (gettext("UNAVAIL"));
200 case VDEV_STATE_FAULTED:
201 return (gettext("FAULTED"));
202 case VDEV_STATE_DEGRADED:
203 return (gettext("DEGRADED"));
204 case VDEV_STATE_HEALTHY:
205 return (gettext("ONLINE"));
211 return (gettext("UNKNOWN"));
215 * Map POOL STATE to printed strings.
218 zpool_pool_state_to_name(pool_state_t state)
221 case POOL_STATE_ACTIVE:
222 return (gettext("ACTIVE"));
223 case POOL_STATE_EXPORTED:
224 return (gettext("EXPORTED"));
225 case POOL_STATE_DESTROYED:
226 return (gettext("DESTROYED"));
227 case POOL_STATE_SPARE:
228 return (gettext("SPARE"));
229 case POOL_STATE_L2CACHE:
230 return (gettext("L2CACHE"));
231 case POOL_STATE_UNINITIALIZED:
232 return (gettext("UNINITIALIZED"));
233 case POOL_STATE_UNAVAIL:
234 return (gettext("UNAVAIL"));
235 case POOL_STATE_POTENTIALLY_ACTIVE:
236 return (gettext("POTENTIALLY_ACTIVE"));
239 return (gettext("UNKNOWN"));
243 * Get a zpool property value for 'prop' and return the value in
244 * a pre-allocated buffer.
247 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
248 zprop_source_t *srctype, boolean_t literal)
252 zprop_source_t src = ZPROP_SRC_NONE;
257 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
259 case ZPOOL_PROP_NAME:
260 (void) strlcpy(buf, zpool_get_name(zhp), len);
263 case ZPOOL_PROP_HEALTH:
265 zpool_pool_state_to_name(POOL_STATE_UNAVAIL), len);
268 case ZPOOL_PROP_GUID:
269 intval = zpool_get_prop_int(zhp, prop, &src);
270 (void) snprintf(buf, len, "%llu", intval);
273 case ZPOOL_PROP_ALTROOT:
274 case ZPOOL_PROP_CACHEFILE:
275 case ZPOOL_PROP_COMMENT:
276 if (zhp->zpool_props != NULL ||
277 zpool_get_all_props(zhp) == 0) {
279 zpool_get_prop_string(zhp, prop, &src),
285 (void) strlcpy(buf, "-", len);
294 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
295 prop != ZPOOL_PROP_NAME)
298 switch (zpool_prop_get_type(prop)) {
299 case PROP_TYPE_STRING:
300 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
304 case PROP_TYPE_NUMBER:
305 intval = zpool_get_prop_int(zhp, prop, &src);
308 case ZPOOL_PROP_SIZE:
309 case ZPOOL_PROP_ALLOCATED:
310 case ZPOOL_PROP_FREE:
311 case ZPOOL_PROP_FREEING:
312 case ZPOOL_PROP_LEAKED:
314 (void) snprintf(buf, len, "%llu",
315 (u_longlong_t)intval);
317 (void) zfs_nicenum(intval, buf, len);
320 case ZPOOL_PROP_BOOTSIZE:
321 case ZPOOL_PROP_EXPANDSZ:
322 case ZPOOL_PROP_CHECKPOINT:
324 (void) strlcpy(buf, "-", len);
325 } else if (literal) {
326 (void) snprintf(buf, len, "%llu",
327 (u_longlong_t)intval);
329 (void) zfs_nicenum(intval, buf, len);
332 case ZPOOL_PROP_CAPACITY:
334 (void) snprintf(buf, len, "%llu",
335 (u_longlong_t)intval);
337 (void) snprintf(buf, len, "%llu%%",
338 (u_longlong_t)intval);
341 case ZPOOL_PROP_FRAGMENTATION:
342 if (intval == UINT64_MAX) {
343 (void) strlcpy(buf, "-", len);
345 (void) snprintf(buf, len, "%llu%%",
346 (u_longlong_t)intval);
349 case ZPOOL_PROP_DEDUPRATIO:
350 (void) snprintf(buf, len, "%llu.%02llux",
351 (u_longlong_t)(intval / 100),
352 (u_longlong_t)(intval % 100));
354 case ZPOOL_PROP_HEALTH:
355 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
356 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
357 verify(nvlist_lookup_uint64_array(nvroot,
358 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
361 (void) strlcpy(buf, zpool_state_to_name(intval,
364 case ZPOOL_PROP_VERSION:
365 if (intval >= SPA_VERSION_FEATURES) {
366 (void) snprintf(buf, len, "-");
371 (void) snprintf(buf, len, "%llu", intval);
375 case PROP_TYPE_INDEX:
376 intval = zpool_get_prop_int(zhp, prop, &src);
377 if (zpool_prop_index_to_string(prop, intval, &strval)
380 (void) strlcpy(buf, strval, len);
394 * Check if the bootfs name has the same pool name as it is set to.
395 * Assuming bootfs is a valid dataset name.
398 bootfs_name_valid(const char *pool, char *bootfs)
400 int len = strlen(pool);
402 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
405 if (strncmp(pool, bootfs, len) == 0 &&
406 (bootfs[len] == '/' || bootfs[len] == '\0'))
413 zpool_is_bootable(zpool_handle_t *zhp)
415 char bootfs[ZFS_MAX_DATASET_NAME_LEN];
417 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
418 sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-",
419 sizeof (bootfs)) != 0);
424 * Given an nvlist of zpool properties to be set, validate that they are
425 * correct, and parse any numeric properties (index, boolean, etc) if they are
426 * specified as strings.
429 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
430 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
438 struct stat64 statbuf;
441 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
442 (void) no_memory(hdl);
447 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
448 const char *propname = nvpair_name(elem);
450 prop = zpool_name_to_prop(propname);
451 if (prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname)) {
453 char *fname = strchr(propname, '@') + 1;
455 err = zfeature_lookup_name(fname, NULL);
457 ASSERT3U(err, ==, ENOENT);
458 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
459 "invalid feature '%s'"), fname);
460 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
464 if (nvpair_type(elem) != DATA_TYPE_STRING) {
465 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
466 "'%s' must be a string"), propname);
467 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
471 (void) nvpair_value_string(elem, &strval);
472 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) {
473 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
474 "property '%s' can only be set to "
475 "'enabled'"), propname);
476 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
480 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
481 (void) no_memory(hdl);
488 * Make sure this property is valid and applies to this type.
490 if (prop == ZPOOL_PROP_INVAL) {
491 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
492 "invalid property '%s'"), propname);
493 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
497 if (zpool_prop_readonly(prop)) {
498 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
499 "is readonly"), propname);
500 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
504 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
505 &strval, &intval, errbuf) != 0)
509 * Perform additional checking for specific properties.
512 case ZPOOL_PROP_VERSION:
513 if (intval < version ||
514 !SPA_VERSION_IS_SUPPORTED(intval)) {
515 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
516 "property '%s' number %d is invalid."),
518 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
523 case ZPOOL_PROP_BOOTSIZE:
525 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
526 "property '%s' can only be set during pool "
527 "creation"), propname);
528 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
533 case ZPOOL_PROP_BOOTFS:
534 if (flags.create || flags.import) {
535 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
536 "property '%s' cannot be set at creation "
537 "or import time"), propname);
538 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
542 if (version < SPA_VERSION_BOOTFS) {
543 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
544 "pool must be upgraded to support "
545 "'%s' property"), propname);
546 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
551 * bootfs property value has to be a dataset name and
552 * the dataset has to be in the same pool as it sets to.
554 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
556 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
557 "is an invalid name"), strval);
558 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
562 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
563 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
564 "could not open pool '%s'"), poolname);
565 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
571 case ZPOOL_PROP_ALTROOT:
572 if (!flags.create && !flags.import) {
573 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
574 "property '%s' can only be set during pool "
575 "creation or import"), propname);
576 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
580 if (strval[0] != '/') {
581 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
582 "bad alternate root '%s'"), strval);
583 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
588 case ZPOOL_PROP_CACHEFILE:
589 if (strval[0] == '\0')
592 if (strcmp(strval, "none") == 0)
595 if (strval[0] != '/') {
596 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
597 "property '%s' must be empty, an "
598 "absolute path, or 'none'"), propname);
599 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
603 slash = strrchr(strval, '/');
605 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
606 strcmp(slash, "/..") == 0) {
607 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
608 "'%s' is not a valid file"), strval);
609 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
615 if (strval[0] != '\0' &&
616 (stat64(strval, &statbuf) != 0 ||
617 !S_ISDIR(statbuf.st_mode))) {
618 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
619 "'%s' is not a valid directory"),
621 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
628 case ZPOOL_PROP_COMMENT:
629 for (check = strval; *check != '\0'; check++) {
630 if (!isprint(*check)) {
632 dgettext(TEXT_DOMAIN,
633 "comment may only have printable "
635 (void) zfs_error(hdl, EZFS_BADPROP,
640 if (strlen(strval) > ZPROP_MAX_COMMENT) {
641 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
642 "comment must not exceed %d characters"),
644 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
649 case ZPOOL_PROP_READONLY:
651 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
652 "property '%s' can only be set at "
653 "import time"), propname);
654 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
659 case ZPOOL_PROP_TNAME:
661 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
662 "property '%s' can only be set at "
663 "creation time"), propname);
664 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
669 case ZPOOL_PROP_MULTIHOST:
670 if (get_system_hostid() == 0) {
671 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
672 "requires a non-zero system hostid"));
673 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
679 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
680 "property '%s'(%d) not defined"), propname, prop);
687 nvlist_free(retprops);
692 * Set zpool property : propname=propval.
695 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
697 zfs_cmd_t zc = { 0 };
700 nvlist_t *nvl = NULL;
703 prop_flags_t flags = { 0 };
705 (void) snprintf(errbuf, sizeof (errbuf),
706 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
709 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
710 return (no_memory(zhp->zpool_hdl));
712 if (nvlist_add_string(nvl, propname, propval) != 0) {
714 return (no_memory(zhp->zpool_hdl));
717 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
718 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
719 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
728 * Execute the corresponding ioctl() to set this property.
730 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
732 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
737 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
739 zcmd_free_nvlists(&zc);
743 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
745 (void) zpool_props_refresh(zhp);
751 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
753 libzfs_handle_t *hdl = zhp->zpool_hdl;
755 char buf[ZFS_MAXPROPLEN];
756 nvlist_t *features = NULL;
758 boolean_t firstexpand = (NULL == *plp);
760 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
764 while (*last != NULL)
765 last = &(*last)->pl_next;
768 features = zpool_get_features(zhp);
770 if ((*plp)->pl_all && firstexpand) {
771 for (int i = 0; i < SPA_FEATURES; i++) {
772 zprop_list_t *entry = zfs_alloc(hdl,
773 sizeof (zprop_list_t));
774 entry->pl_prop = ZPROP_INVAL;
775 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
776 spa_feature_table[i].fi_uname);
777 entry->pl_width = strlen(entry->pl_user_prop);
778 entry->pl_all = B_TRUE;
781 last = &entry->pl_next;
785 /* add any unsupported features */
786 for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL);
787 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
792 if (zfeature_is_supported(nvpair_name(nvp)))
795 propname = zfs_asprintf(hdl, "unsupported@%s",
799 * Before adding the property to the list make sure that no
800 * other pool already added the same property.
804 while (entry != NULL) {
805 if (entry->pl_user_prop != NULL &&
806 strcmp(propname, entry->pl_user_prop) == 0) {
810 entry = entry->pl_next;
817 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
818 entry->pl_prop = ZPROP_INVAL;
819 entry->pl_user_prop = propname;
820 entry->pl_width = strlen(entry->pl_user_prop);
821 entry->pl_all = B_TRUE;
824 last = &entry->pl_next;
827 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
832 if (entry->pl_prop != ZPROP_INVAL &&
833 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
834 NULL, B_FALSE) == 0) {
835 if (strlen(buf) > entry->pl_width)
836 entry->pl_width = strlen(buf);
844 * Get the state for the given feature on the given ZFS pool.
847 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
851 boolean_t found = B_FALSE;
852 nvlist_t *features = zpool_get_features(zhp);
854 const char *feature = strchr(propname, '@') + 1;
856 supported = zpool_prop_feature(propname);
857 ASSERT(supported || zpool_prop_unsupported(propname));
860 * Convert from feature name to feature guid. This conversion is
861 * unecessary for unsupported@... properties because they already
868 ret = zfeature_lookup_name(feature, &fid);
870 (void) strlcpy(buf, "-", len);
873 feature = spa_feature_table[fid].fi_guid;
876 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
881 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
884 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
886 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
891 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
893 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
896 (void) strlcpy(buf, "-", len);
905 * Don't start the slice at the default block of 34; many storage
906 * devices will use a stripe width of 128k, so start there instead.
908 #define NEW_START_BLOCK 256
911 * Validate the given pool name, optionally putting an extended error message in
915 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
921 ret = pool_namecheck(pool, &why, &what);
924 * The rules for reserved pool names were extended at a later point.
925 * But we need to support users with existing pools that may now be
926 * invalid. So we only check for this expanded set of names during a
927 * create (or import), and only in userland.
929 if (ret == 0 && !isopen &&
930 (strncmp(pool, "mirror", 6) == 0 ||
931 strncmp(pool, "raidz", 5) == 0 ||
932 strncmp(pool, "spare", 5) == 0 ||
933 strcmp(pool, "log") == 0)) {
936 dgettext(TEXT_DOMAIN, "name is reserved"));
944 case NAME_ERR_TOOLONG:
946 dgettext(TEXT_DOMAIN, "name is too long"));
949 case NAME_ERR_INVALCHAR:
951 dgettext(TEXT_DOMAIN, "invalid character "
952 "'%c' in pool name"), what);
955 case NAME_ERR_NOLETTER:
956 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
957 "name must begin with a letter"));
960 case NAME_ERR_RESERVED:
961 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
962 "name is reserved"));
965 case NAME_ERR_DISKLIKE:
966 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
967 "pool name is reserved"));
970 case NAME_ERR_LEADING_SLASH:
971 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
972 "leading slash in name"));
975 case NAME_ERR_EMPTY_COMPONENT:
976 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
977 "empty component in name"));
980 case NAME_ERR_TRAILING_SLASH:
981 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
982 "trailing slash in name"));
985 case NAME_ERR_MULTIPLE_DELIMITERS:
986 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
987 "multiple '@' and/or '#' delimiters in "
992 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
993 "(%d) not defined"), why);
1004 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1008 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
1010 zpool_handle_t *zhp;
1014 * Make sure the pool name is valid.
1016 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
1017 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1018 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1023 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1026 zhp->zpool_hdl = hdl;
1027 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1029 if (zpool_refresh_stats(zhp, &missing) != 0) {
1035 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1036 (void) zfs_error_fmt(hdl, EZFS_NOENT,
1037 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1046 * Like the above, but silent on error. Used when iterating over pools (because
1047 * the configuration cache may be out of date).
1050 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1052 zpool_handle_t *zhp;
1055 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1058 zhp->zpool_hdl = hdl;
1059 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1061 if (zpool_refresh_stats(zhp, &missing) != 0) {
1077 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1081 zpool_open(libzfs_handle_t *hdl, const char *pool)
1083 zpool_handle_t *zhp;
1085 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1088 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1089 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1090 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1099 * Close the handle. Simply frees the memory associated with the handle.
1102 zpool_close(zpool_handle_t *zhp)
1104 nvlist_free(zhp->zpool_config);
1105 nvlist_free(zhp->zpool_old_config);
1106 nvlist_free(zhp->zpool_props);
1111 * Return the name of the pool.
1114 zpool_get_name(zpool_handle_t *zhp)
1116 return (zhp->zpool_name);
1121 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1124 zpool_get_state(zpool_handle_t *zhp)
1126 return (zhp->zpool_state);
1130 * Check if vdev list contains a special vdev
1133 zpool_has_special_vdev(nvlist_t *nvroot)
1138 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN, &child,
1140 for (uint_t c = 0; c < children; c++) {
1143 if (nvlist_lookup_string(child[c],
1144 ZPOOL_CONFIG_ALLOCATION_BIAS, &bias) == 0 &&
1145 strcmp(bias, VDEV_ALLOC_BIAS_SPECIAL) == 0) {
1154 * Create the named pool, using the provided vdev list. It is assumed
1155 * that the consumer has already validated the contents of the nvlist, so we
1156 * don't have to worry about error semantics.
1159 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1160 nvlist_t *props, nvlist_t *fsprops)
1162 zfs_cmd_t zc = { 0 };
1163 nvlist_t *zc_fsprops = NULL;
1164 nvlist_t *zc_props = NULL;
1168 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1169 "cannot create '%s'"), pool);
1171 if (!zpool_name_valid(hdl, B_FALSE, pool))
1172 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1174 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1178 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1180 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1181 SPA_VERSION_1, flags, msg)) == NULL) {
1190 zoned = ((nvlist_lookup_string(fsprops,
1191 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1192 strcmp(zonestr, "on") == 0);
1194 if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
1195 fsprops, zoned, NULL, NULL, msg)) == NULL) {
1199 if (nvlist_exists(zc_fsprops,
1200 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS)) &&
1201 !zpool_has_special_vdev(nvroot)) {
1202 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1203 "%s property requires a special vdev"),
1204 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS));
1205 (void) zfs_error(hdl, EZFS_BADPROP, msg);
1210 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1213 if (nvlist_add_nvlist(zc_props,
1214 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1219 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1222 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1224 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1226 zcmd_free_nvlists(&zc);
1227 nvlist_free(zc_props);
1228 nvlist_free(zc_fsprops);
1233 * This can happen if the user has specified the same
1234 * device multiple times. We can't reliably detect this
1235 * until we try to add it and see we already have a
1238 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1239 "one or more vdevs refer to the same device"));
1240 return (zfs_error(hdl, EZFS_BADDEV, msg));
1244 * This happens if the record size is smaller or larger
1245 * than the allowed size range, or not a power of 2.
1247 * NOTE: although zfs_valid_proplist is called earlier,
1248 * this case may have slipped through since the
1249 * pool does not exist yet and it is therefore
1250 * impossible to read properties e.g. max blocksize
1253 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1254 "record size invalid"));
1255 return (zfs_error(hdl, EZFS_BADPROP, msg));
1259 * This occurs when one of the devices is below
1260 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1261 * device was the problem device since there's no
1262 * reliable way to determine device size from userland.
1267 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1269 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1270 "one or more devices is less than the "
1271 "minimum size (%s)"), buf);
1273 return (zfs_error(hdl, EZFS_BADDEV, msg));
1276 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1277 "one or more devices is out of space"));
1278 return (zfs_error(hdl, EZFS_BADDEV, msg));
1281 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1282 "cache device must be a disk or disk slice"));
1283 return (zfs_error(hdl, EZFS_BADDEV, msg));
1286 return (zpool_standard_error(hdl, errno, msg));
1291 zcmd_free_nvlists(&zc);
1292 nvlist_free(zc_props);
1293 nvlist_free(zc_fsprops);
1298 * Destroy the given pool. It is up to the caller to ensure that there are no
1299 * datasets left in the pool.
1302 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1304 zfs_cmd_t zc = { 0 };
1305 zfs_handle_t *zfp = NULL;
1306 libzfs_handle_t *hdl = zhp->zpool_hdl;
1309 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1310 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1313 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1314 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1316 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1317 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1318 "cannot destroy '%s'"), zhp->zpool_name);
1320 if (errno == EROFS) {
1321 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1322 "one or more devices is read only"));
1323 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1325 (void) zpool_standard_error(hdl, errno, msg);
1334 remove_mountpoint(zfp);
1342 * Create a checkpoint in the given pool.
1345 zpool_checkpoint(zpool_handle_t *zhp)
1347 libzfs_handle_t *hdl = zhp->zpool_hdl;
1351 error = lzc_pool_checkpoint(zhp->zpool_name);
1353 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1354 "cannot checkpoint '%s'"), zhp->zpool_name);
1355 (void) zpool_standard_error(hdl, error, msg);
1363 * Discard the checkpoint from the given pool.
1366 zpool_discard_checkpoint(zpool_handle_t *zhp)
1368 libzfs_handle_t *hdl = zhp->zpool_hdl;
1372 error = lzc_pool_checkpoint_discard(zhp->zpool_name);
1374 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1375 "cannot discard checkpoint in '%s'"), zhp->zpool_name);
1376 (void) zpool_standard_error(hdl, error, msg);
1384 * Add the given vdevs to the pool. The caller must have already performed the
1385 * necessary verification to ensure that the vdev specification is well-formed.
1388 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1390 zfs_cmd_t zc = { 0 };
1392 libzfs_handle_t *hdl = zhp->zpool_hdl;
1394 nvlist_t **spares, **l2cache;
1395 uint_t nspares, nl2cache;
1397 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1398 "cannot add to '%s'"), zhp->zpool_name);
1400 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1401 SPA_VERSION_SPARES &&
1402 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1403 &spares, &nspares) == 0) {
1404 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1405 "upgraded to add hot spares"));
1406 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1409 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1410 SPA_VERSION_L2CACHE &&
1411 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1412 &l2cache, &nl2cache) == 0) {
1413 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1414 "upgraded to add cache devices"));
1415 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1418 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1420 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1422 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1426 * This can happen if the user has specified the same
1427 * device multiple times. We can't reliably detect this
1428 * until we try to add it and see we already have a
1431 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1432 "one or more vdevs refer to the same device"));
1433 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1437 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1438 "invalid config; a pool with removing/removed "
1439 "vdevs does not support adding raidz vdevs"));
1440 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1445 * This occurrs when one of the devices is below
1446 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1447 * device was the problem device since there's no
1448 * reliable way to determine device size from userland.
1453 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1455 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1456 "device is less than the minimum "
1459 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1463 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1464 "pool must be upgraded to add these vdevs"));
1465 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1469 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1470 "root pool can not have multiple vdevs"
1471 " or separate logs"));
1472 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1476 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1477 "cache device must be a disk or disk slice"));
1478 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1482 (void) zpool_standard_error(hdl, errno, msg);
1490 zcmd_free_nvlists(&zc);
1496 * Exports the pool from the system. The caller must ensure that there are no
1497 * mounted datasets in the pool.
1500 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1501 const char *log_str)
1503 zfs_cmd_t zc = { 0 };
1506 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1507 "cannot export '%s'"), zhp->zpool_name);
1509 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1510 zc.zc_cookie = force;
1511 zc.zc_guid = hardforce;
1512 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1514 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1517 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1518 "use '-f' to override the following errors:\n"
1519 "'%s' has an active shared spare which could be"
1520 " used by other pools once '%s' is exported."),
1521 zhp->zpool_name, zhp->zpool_name);
1522 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1525 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1534 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1536 return (zpool_export_common(zhp, force, B_FALSE, log_str));
1540 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1542 return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1546 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1549 nvlist_t *nv = NULL;
1555 if (!hdl->libzfs_printerr || config == NULL)
1558 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1559 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1563 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1565 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1567 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1568 strftime(timestr, 128, 0, &t) != 0) {
1570 (void) printf(dgettext(TEXT_DOMAIN,
1571 "Would be able to return %s "
1572 "to its state as of %s.\n"),
1575 (void) printf(dgettext(TEXT_DOMAIN,
1576 "Pool %s returned to its state as of %s.\n"),
1580 (void) printf(dgettext(TEXT_DOMAIN,
1581 "%s approximately %lld "),
1582 dryrun ? "Would discard" : "Discarded",
1584 (void) printf(dgettext(TEXT_DOMAIN,
1585 "minutes of transactions.\n"));
1586 } else if (loss > 0) {
1587 (void) printf(dgettext(TEXT_DOMAIN,
1588 "%s approximately %lld "),
1589 dryrun ? "Would discard" : "Discarded", loss);
1590 (void) printf(dgettext(TEXT_DOMAIN,
1591 "seconds of transactions.\n"));
1597 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1600 nvlist_t *nv = NULL;
1602 uint64_t edata = UINT64_MAX;
1607 if (!hdl->libzfs_printerr)
1611 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1613 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1615 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1616 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1617 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1618 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1621 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1622 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1625 (void) printf(dgettext(TEXT_DOMAIN,
1626 "Recovery is possible, but will result in some data loss.\n"));
1628 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1629 strftime(timestr, 128, 0, &t) != 0) {
1630 (void) printf(dgettext(TEXT_DOMAIN,
1631 "\tReturning the pool to its state as of %s\n"
1632 "\tshould correct the problem. "),
1635 (void) printf(dgettext(TEXT_DOMAIN,
1636 "\tReverting the pool to an earlier state "
1637 "should correct the problem.\n\t"));
1641 (void) printf(dgettext(TEXT_DOMAIN,
1642 "Approximately %lld minutes of data\n"
1643 "\tmust be discarded, irreversibly. "), (loss + 30) / 60);
1644 } else if (loss > 0) {
1645 (void) printf(dgettext(TEXT_DOMAIN,
1646 "Approximately %lld seconds of data\n"
1647 "\tmust be discarded, irreversibly. "), loss);
1649 if (edata != 0 && edata != UINT64_MAX) {
1651 (void) printf(dgettext(TEXT_DOMAIN,
1652 "After rewind, at least\n"
1653 "\tone persistent user-data error will remain. "));
1655 (void) printf(dgettext(TEXT_DOMAIN,
1656 "After rewind, several\n"
1657 "\tpersistent user-data errors will remain. "));
1660 (void) printf(dgettext(TEXT_DOMAIN,
1661 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1662 reason >= 0 ? "clear" : "import", name);
1664 (void) printf(dgettext(TEXT_DOMAIN,
1665 "A scrub of the pool\n"
1666 "\tis strongly recommended after recovery.\n"));
1670 (void) printf(dgettext(TEXT_DOMAIN,
1671 "Destroy and re-create the pool from\n\ta backup source.\n"));
1675 * zpool_import() is a contracted interface. Should be kept the same
1678 * Applications should use zpool_import_props() to import a pool with
1679 * new properties value to be set.
1682 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1685 nvlist_t *props = NULL;
1688 if (altroot != NULL) {
1689 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1690 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1691 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1695 if (nvlist_add_string(props,
1696 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1697 nvlist_add_string(props,
1698 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1700 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1701 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1706 ret = zpool_import_props(hdl, config, newname, props,
1713 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1719 uint64_t is_log = 0;
1721 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1725 (void) printf("\t%*s%s%s\n", indent, "", name,
1726 is_log ? " [log]" : "");
1728 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1729 &child, &children) != 0)
1732 for (c = 0; c < children; c++) {
1733 vname = zpool_vdev_name(hdl, NULL, child[c], VDEV_NAME_TYPE_ID);
1734 print_vdev_tree(hdl, vname, child[c], indent + 2);
1740 zpool_print_unsup_feat(nvlist_t *config)
1742 nvlist_t *nvinfo, *unsup_feat;
1744 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1746 verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1749 for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1750 nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1753 verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1754 verify(nvpair_value_string(nvp, &desc) == 0);
1756 if (strlen(desc) > 0)
1757 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1759 (void) printf("\t%s\n", nvpair_name(nvp));
1764 * Import the given pool using the known configuration and a list of
1765 * properties to be set. The configuration should have come from
1766 * zpool_find_import(). The 'newname' parameters control whether the pool
1767 * is imported with a different name.
1770 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1771 nvlist_t *props, int flags)
1773 zfs_cmd_t zc = { 0 };
1774 zpool_load_policy_t policy;
1775 nvlist_t *nv = NULL;
1776 nvlist_t *nvinfo = NULL;
1777 nvlist_t *missing = NULL;
1784 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1787 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1788 "cannot import pool '%s'"), origname);
1790 if (newname != NULL) {
1791 if (!zpool_name_valid(hdl, B_FALSE, newname))
1792 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1793 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1795 thename = (char *)newname;
1800 if (props != NULL) {
1802 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1804 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1807 if ((props = zpool_valid_proplist(hdl, origname,
1808 props, version, flags, errbuf)) == NULL)
1810 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1817 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1819 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1822 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1823 zcmd_free_nvlists(&zc);
1826 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1827 zcmd_free_nvlists(&zc);
1831 zc.zc_cookie = flags;
1832 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1834 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1835 zcmd_free_nvlists(&zc);
1842 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1844 zcmd_free_nvlists(&zc);
1846 zpool_get_load_policy(config, &policy);
1853 * Dry-run failed, but we print out what success
1854 * looks like if we found a best txg
1856 if (policy.zlp_rewind & ZPOOL_TRY_REWIND) {
1857 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1863 if (newname == NULL)
1864 (void) snprintf(desc, sizeof (desc),
1865 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1868 (void) snprintf(desc, sizeof (desc),
1869 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1874 if (nv != NULL && nvlist_lookup_nvlist(nv,
1875 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1876 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1877 (void) printf(dgettext(TEXT_DOMAIN, "This "
1878 "pool uses the following feature(s) not "
1879 "supported by this system:\n"));
1880 zpool_print_unsup_feat(nv);
1881 if (nvlist_exists(nvinfo,
1882 ZPOOL_CONFIG_CAN_RDONLY)) {
1883 (void) printf(dgettext(TEXT_DOMAIN,
1884 "All unsupported features are only "
1885 "required for writing to the pool."
1886 "\nThe pool can be imported using "
1887 "'-o readonly=on'.\n"));
1891 * Unsupported version.
1893 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1897 if (nv != NULL && nvlist_lookup_nvlist(nv,
1898 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0) {
1899 char *hostname = "<unknown>";
1900 uint64_t hostid = 0;
1901 mmp_state_t mmp_state;
1903 mmp_state = fnvlist_lookup_uint64(nvinfo,
1904 ZPOOL_CONFIG_MMP_STATE);
1906 if (nvlist_exists(nvinfo,
1907 ZPOOL_CONFIG_MMP_HOSTNAME))
1908 hostname = fnvlist_lookup_string(nvinfo,
1909 ZPOOL_CONFIG_MMP_HOSTNAME);
1911 if (nvlist_exists(nvinfo,
1912 ZPOOL_CONFIG_MMP_HOSTID))
1913 hostid = fnvlist_lookup_uint64(nvinfo,
1914 ZPOOL_CONFIG_MMP_HOSTID);
1916 if (mmp_state == MMP_STATE_ACTIVE) {
1917 (void) snprintf(aux, sizeof (aux),
1918 dgettext(TEXT_DOMAIN, "pool is imp"
1919 "orted on host '%s' (hostid=%lx).\n"
1920 "Export the pool on the other "
1921 "system, then run 'zpool import'."),
1922 hostname, (unsigned long) hostid);
1923 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
1924 (void) snprintf(aux, sizeof (aux),
1925 dgettext(TEXT_DOMAIN, "pool has "
1926 "the multihost property on and "
1927 "the\nsystem's hostid is not "
1931 (void) zfs_error_aux(hdl, aux);
1933 (void) zfs_error(hdl, EZFS_ACTIVE_POOL, desc);
1937 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1941 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1942 "one or more devices is read only"));
1943 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1947 if (nv && nvlist_lookup_nvlist(nv,
1948 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1949 nvlist_lookup_nvlist(nvinfo,
1950 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1951 (void) printf(dgettext(TEXT_DOMAIN,
1952 "The devices below are missing or "
1953 "corrupted, use '-m' to import the pool "
1955 print_vdev_tree(hdl, NULL, missing, 2);
1956 (void) printf("\n");
1958 (void) zpool_standard_error(hdl, error, desc);
1962 (void) zpool_standard_error(hdl, error, desc);
1965 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1966 "new name of at least one dataset is longer than "
1967 "the maximum allowable length"));
1968 (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
1971 (void) zpool_standard_error(hdl, error, desc);
1972 zpool_explain_recover(hdl,
1973 newname ? origname : thename, -error, nv);
1980 zpool_handle_t *zhp;
1983 * This should never fail, but play it safe anyway.
1985 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1987 else if (zhp != NULL)
1989 if (policy.zlp_rewind &
1990 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1991 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1992 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0), nv);
2005 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd)
2007 zfs_cmd_t zc = { 0 };
2010 libzfs_handle_t *hdl = zhp->zpool_hdl;
2012 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2013 zc.zc_cookie = func;
2016 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0)
2021 /* ECANCELED on a scrub means we resumed a paused scrub */
2022 if (err == ECANCELED && func == POOL_SCAN_SCRUB &&
2023 cmd == POOL_SCRUB_NORMAL)
2026 if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL)
2029 if (func == POOL_SCAN_SCRUB) {
2030 if (cmd == POOL_SCRUB_PAUSE) {
2031 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2032 "cannot pause scrubbing %s"), zc.zc_name);
2034 assert(cmd == POOL_SCRUB_NORMAL);
2035 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2036 "cannot scrub %s"), zc.zc_name);
2038 } else if (func == POOL_SCAN_NONE) {
2039 (void) snprintf(msg, sizeof (msg),
2040 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
2043 assert(!"unexpected result");
2048 pool_scan_stat_t *ps = NULL;
2051 verify(nvlist_lookup_nvlist(zhp->zpool_config,
2052 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2053 (void) nvlist_lookup_uint64_array(nvroot,
2054 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
2055 if (ps && ps->pss_func == POOL_SCAN_SCRUB) {
2056 if (cmd == POOL_SCRUB_PAUSE)
2057 return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg));
2059 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
2061 return (zfs_error(hdl, EZFS_RESILVERING, msg));
2063 } else if (err == ENOENT) {
2064 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
2066 return (zpool_standard_error(hdl, err, msg));
2071 xlate_init_err(int err)
2075 return (EZFS_NODEVICE);
2078 return (EZFS_BADDEV);
2080 return (EZFS_INITIALIZING);
2082 return (EZFS_NO_INITIALIZE);
2088 * Begin, suspend, or cancel the initialization (initializing of all free
2089 * blocks) for the given vdevs in the given pool.
2092 zpool_initialize(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2096 libzfs_handle_t *hdl = zhp->zpool_hdl;
2100 /* translate vdev names to guids */
2101 nvlist_t *vdev_guids = fnvlist_alloc();
2102 nvlist_t *guids_to_paths = fnvlist_alloc();
2103 boolean_t spare, cache;
2107 for (elem = nvlist_next_nvpair(vds, NULL); elem != NULL;
2108 elem = nvlist_next_nvpair(vds, elem)) {
2109 char *vd_path = nvpair_name(elem);
2110 tgt = zpool_find_vdev(zhp, vd_path, &spare, &cache, NULL);
2112 if ((tgt == NULL) || cache || spare) {
2113 (void) snprintf(msg, sizeof (msg),
2114 dgettext(TEXT_DOMAIN, "cannot initialize '%s'"),
2116 int err = (tgt == NULL) ? EZFS_NODEVICE :
2117 (spare ? EZFS_ISSPARE : EZFS_ISL2CACHE);
2118 fnvlist_free(vdev_guids);
2119 fnvlist_free(guids_to_paths);
2120 return (zfs_error(hdl, err, msg));
2123 uint64_t guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
2124 fnvlist_add_uint64(vdev_guids, vd_path, guid);
2126 (void) snprintf(msg, sizeof (msg), "%llu", guid);
2127 fnvlist_add_string(guids_to_paths, msg, vd_path);
2130 int err = lzc_initialize(zhp->zpool_name, cmd_type, vdev_guids,
2132 fnvlist_free(vdev_guids);
2135 fnvlist_free(guids_to_paths);
2139 nvlist_t *vd_errlist = NULL;
2140 if (errlist != NULL) {
2141 vd_errlist = fnvlist_lookup_nvlist(errlist,
2142 ZPOOL_INITIALIZE_VDEVS);
2145 (void) snprintf(msg, sizeof (msg),
2146 dgettext(TEXT_DOMAIN, "operation failed"));
2148 for (elem = nvlist_next_nvpair(vd_errlist, NULL); elem != NULL;
2149 elem = nvlist_next_nvpair(vd_errlist, elem)) {
2150 int64_t vd_error = xlate_init_err(fnvpair_value_int64(elem));
2151 char *path = fnvlist_lookup_string(guids_to_paths,
2153 (void) zfs_error_fmt(hdl, vd_error, "cannot initialize '%s'",
2157 fnvlist_free(guids_to_paths);
2158 if (vd_errlist != NULL)
2161 return (zpool_standard_error(hdl, err, msg));
2166 * This provides a very minimal check whether a given string is likely a
2167 * c#t#d# style string. Users of this are expected to do their own
2168 * verification of the s# part.
2170 #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1]))
2173 * More elaborate version for ones which may start with "/dev/dsk/"
2177 ctd_check_path(char *str)
2180 * If it starts with a slash, check the last component.
2182 if (str && str[0] == '/') {
2183 char *tmp = strrchr(str, '/');
2186 * If it ends in "/old", check the second-to-last
2187 * component of the string instead.
2189 if (tmp != str && strcmp(tmp, "/old") == 0) {
2190 for (tmp--; *tmp != '/'; tmp--)
2195 return (CTD_CHECK(str));
2200 * Find a vdev that matches the search criteria specified. We use the
2201 * the nvpair name to determine how we should look for the device.
2202 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2203 * spare; but FALSE if its an INUSE spare.
2206 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
2207 boolean_t *l2cache, boolean_t *log)
2214 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
2216 /* Nothing to look for */
2217 if (search == NULL || pair == NULL)
2220 /* Obtain the key we will use to search */
2221 srchkey = nvpair_name(pair);
2223 switch (nvpair_type(pair)) {
2224 case DATA_TYPE_UINT64:
2225 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
2226 uint64_t srchval, theguid;
2228 verify(nvpair_value_uint64(pair, &srchval) == 0);
2229 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2231 if (theguid == srchval)
2236 case DATA_TYPE_STRING: {
2237 char *srchval, *val;
2239 verify(nvpair_value_string(pair, &srchval) == 0);
2240 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2244 * Search for the requested value. Special cases:
2246 * - ZPOOL_CONFIG_PATH for whole disk entries. To support
2247 * UEFI boot, these end in "s0" or "s0/old" or "s1" or
2248 * "s1/old". The "s0" or "s1" part is hidden from the user,
2249 * but included in the string, so this matches around it.
2250 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2252 * Otherwise, all other searches are simple string compares.
2255 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
2256 ctd_check_path(val)) {
2257 uint64_t wholedisk = 0;
2259 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2262 int slen = strlen(srchval);
2263 int vlen = strlen(val);
2265 if (slen != vlen - 2)
2269 * make_leaf_vdev() should only set
2270 * wholedisk for ZPOOL_CONFIG_PATHs which
2271 * will include "/dev/dsk/", giving plenty of
2272 * room for the indices used next.
2277 * strings identical except trailing "s0"
2279 if ((strcmp(&val[vlen - 2], "s0") == 0 ||
2280 strcmp(&val[vlen - 2], "s1") == 0) &&
2281 strncmp(srchval, val, slen) == 0)
2285 * strings identical except trailing "s0/old"
2287 if ((strcmp(&val[vlen - 6], "s0/old") == 0 ||
2288 strcmp(&val[vlen - 6], "s1/old") == 0) &&
2289 strcmp(&srchval[slen - 4], "/old") == 0 &&
2290 strncmp(srchval, val, slen - 4) == 0)
2295 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2297 if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2299 char *type, *idx, *end, *p;
2300 uint64_t id, vdev_id;
2303 * Determine our vdev type, keeping in mind
2304 * that the srchval is composed of a type and
2305 * vdev id pair (i.e. mirror-4).
2307 if ((type = strdup(srchval)) == NULL)
2310 if ((p = strrchr(type, '-')) == NULL) {
2318 * If the types don't match then keep looking.
2320 if (strncmp(val, type, strlen(val)) != 0) {
2325 verify(zpool_vdev_is_interior(type));
2326 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2330 vdev_id = strtoull(idx, &end, 10);
2337 * Now verify that we have the correct vdev id.
2346 if (strcmp(srchval, val) == 0)
2355 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2356 &child, &children) != 0)
2359 for (c = 0; c < children; c++) {
2360 if ((ret = vdev_to_nvlist_iter(child[c], search,
2361 avail_spare, l2cache, NULL)) != NULL) {
2363 * The 'is_log' value is only set for the toplevel
2364 * vdev, not the leaf vdevs. So we always lookup the
2365 * log device from the root of the vdev tree (where
2366 * 'log' is non-NULL).
2369 nvlist_lookup_uint64(child[c],
2370 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2378 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2379 &child, &children) == 0) {
2380 for (c = 0; c < children; c++) {
2381 if ((ret = vdev_to_nvlist_iter(child[c], search,
2382 avail_spare, l2cache, NULL)) != NULL) {
2383 *avail_spare = B_TRUE;
2389 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2390 &child, &children) == 0) {
2391 for (c = 0; c < children; c++) {
2392 if ((ret = vdev_to_nvlist_iter(child[c], search,
2393 avail_spare, l2cache, NULL)) != NULL) {
2404 * Given a physical path (minus the "/devices" prefix), find the
2408 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2409 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2411 nvlist_t *search, *nvroot, *ret;
2413 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2414 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2416 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2419 *avail_spare = B_FALSE;
2423 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2424 nvlist_free(search);
2430 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2433 zpool_vdev_is_interior(const char *name)
2435 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2436 strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 ||
2438 VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 ||
2439 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2445 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2446 boolean_t *l2cache, boolean_t *log)
2448 char buf[MAXPATHLEN];
2450 nvlist_t *nvroot, *search, *ret;
2453 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2455 guid = strtoull(path, &end, 10);
2456 if (guid != 0 && *end == '\0') {
2457 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2458 } else if (zpool_vdev_is_interior(path)) {
2459 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2460 } else if (path[0] != '/') {
2461 (void) snprintf(buf, sizeof (buf), "%s%s", _PATH_DEV, path);
2462 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
2464 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2467 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2470 *avail_spare = B_FALSE;
2474 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2475 nvlist_free(search);
2481 vdev_is_online(nvlist_t *nv)
2485 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2486 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2487 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2494 * Helper function for zpool_get_physpaths().
2497 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2498 size_t *bytes_written)
2500 size_t bytes_left, pos, rsz;
2504 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2506 return (EZFS_NODEVICE);
2508 pos = *bytes_written;
2509 bytes_left = physpath_size - pos;
2510 format = (pos == 0) ? "%s" : " %s";
2512 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2513 *bytes_written += rsz;
2515 if (rsz >= bytes_left) {
2516 /* if physpath was not copied properly, clear it */
2517 if (bytes_left != 0) {
2520 return (EZFS_NOSPC);
2526 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2527 size_t *rsz, boolean_t is_spare)
2532 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2533 return (EZFS_INVALCONFIG);
2535 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2537 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2538 * For a spare vdev, we only want to boot from the active
2543 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2546 return (EZFS_INVALCONFIG);
2549 if (vdev_is_online(nv)) {
2550 if ((ret = vdev_get_one_physpath(nv, physpath,
2551 phypath_size, rsz)) != 0)
2554 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2555 strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
2556 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2557 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2562 if (nvlist_lookup_nvlist_array(nv,
2563 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2564 return (EZFS_INVALCONFIG);
2566 for (i = 0; i < count; i++) {
2567 ret = vdev_get_physpaths(child[i], physpath,
2568 phypath_size, rsz, is_spare);
2569 if (ret == EZFS_NOSPC)
2574 return (EZFS_POOL_INVALARG);
2578 * Get phys_path for a root pool config.
2579 * Return 0 on success; non-zero on failure.
2582 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2585 nvlist_t *vdev_root;
2592 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2594 return (EZFS_INVALCONFIG);
2596 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2597 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2598 &child, &count) != 0)
2599 return (EZFS_INVALCONFIG);
2602 * root pool can only have a single top-level vdev.
2604 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2605 return (EZFS_POOL_INVALARG);
2607 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2610 /* No online devices */
2612 return (EZFS_NODEVICE);
2618 * Get phys_path for a root pool
2619 * Return 0 on success; non-zero on failure.
2622 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2624 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2629 * If the device has being dynamically expanded then we need to relabel
2630 * the disk to use the new unallocated space.
2633 zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2636 char path[MAXPATHLEN];
2639 int (*_efi_use_whole_disk)(int);
2641 if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2642 "efi_use_whole_disk")) == NULL)
2645 (void) snprintf(path, sizeof (path), "%s/%s", ZFS_RDISK_ROOT, name);
2647 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2648 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2649 "relabel '%s': unable to open device"), name);
2650 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2654 * It's possible that we might encounter an error if the device
2655 * does not have any unallocated space left. If so, we simply
2656 * ignore that error and continue on.
2658 error = _efi_use_whole_disk(fd);
2660 if (error && error != VT_ENOSPC) {
2661 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2662 "relabel '%s': unable to read disk capacity"), name);
2663 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2665 #endif /* illumos */
2670 * Bring the specified vdev online. The 'flags' parameter is a set of the
2671 * ZFS_ONLINE_* flags.
2674 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2675 vdev_state_t *newstate)
2677 zfs_cmd_t zc = { 0 };
2681 boolean_t avail_spare, l2cache, islog;
2682 libzfs_handle_t *hdl = zhp->zpool_hdl;
2684 if (flags & ZFS_ONLINE_EXPAND) {
2685 (void) snprintf(msg, sizeof (msg),
2686 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2688 (void) snprintf(msg, sizeof (msg),
2689 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2692 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2693 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2695 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2697 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2700 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2702 if ((flags & ZFS_ONLINE_EXPAND ||
2703 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) &&
2704 nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) {
2705 uint64_t wholedisk = 0;
2707 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2711 * XXX - L2ARC 1.0 devices can't support expansion.
2714 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2715 "cannot expand cache devices"));
2716 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2720 pathname += strlen(ZFS_DISK_ROOT) + 1;
2721 (void) zpool_relabel_disk(hdl, pathname);
2725 zc.zc_cookie = VDEV_STATE_ONLINE;
2728 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2729 if (errno == EINVAL) {
2730 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2731 "from this pool into a new one. Use '%s' "
2732 "instead"), "zpool detach");
2733 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2735 return (zpool_standard_error(hdl, errno, msg));
2738 *newstate = zc.zc_cookie;
2743 * Take the specified vdev offline
2746 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2748 zfs_cmd_t zc = { 0 };
2751 boolean_t avail_spare, l2cache;
2752 libzfs_handle_t *hdl = zhp->zpool_hdl;
2754 (void) snprintf(msg, sizeof (msg),
2755 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2757 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2758 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2760 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2762 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2765 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2767 zc.zc_cookie = VDEV_STATE_OFFLINE;
2768 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2770 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2777 * There are no other replicas of this device.
2779 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2783 * The log device has unplayed logs
2785 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2788 return (zpool_standard_error(hdl, errno, msg));
2793 * Mark the given vdev faulted.
2796 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2798 zfs_cmd_t zc = { 0 };
2800 libzfs_handle_t *hdl = zhp->zpool_hdl;
2802 (void) snprintf(msg, sizeof (msg),
2803 dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2805 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2807 zc.zc_cookie = VDEV_STATE_FAULTED;
2810 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2817 * There are no other replicas of this device.
2819 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2822 return (zpool_standard_error(hdl, errno, msg));
2828 * Mark the given vdev degraded.
2831 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2833 zfs_cmd_t zc = { 0 };
2835 libzfs_handle_t *hdl = zhp->zpool_hdl;
2837 (void) snprintf(msg, sizeof (msg),
2838 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
2840 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2842 zc.zc_cookie = VDEV_STATE_DEGRADED;
2845 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2848 return (zpool_standard_error(hdl, errno, msg));
2852 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2856 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2862 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2864 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2867 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2868 children == 2 && child[which] == tgt)
2871 for (c = 0; c < children; c++)
2872 if (is_replacing_spare(child[c], tgt, which))
2880 * Attach new_disk (fully described by nvroot) to old_disk.
2881 * If 'replacing' is specified, the new disk will replace the old one.
2884 zpool_vdev_attach(zpool_handle_t *zhp,
2885 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2887 zfs_cmd_t zc = { 0 };
2891 boolean_t avail_spare, l2cache, islog;
2896 nvlist_t *config_root;
2897 libzfs_handle_t *hdl = zhp->zpool_hdl;
2898 boolean_t rootpool = zpool_is_bootable(zhp);
2901 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2902 "cannot replace %s with %s"), old_disk, new_disk);
2904 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2905 "cannot attach %s to %s"), new_disk, old_disk);
2907 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2908 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2910 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2913 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2916 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2918 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2919 zc.zc_cookie = replacing;
2921 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2922 &child, &children) != 0 || children != 1) {
2923 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2924 "new device must be a single disk"));
2925 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2928 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2929 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2931 if ((newname = zpool_vdev_name(NULL, NULL, child[0], 0)) == NULL)
2935 * If the target is a hot spare that has been swapped in, we can only
2936 * replace it with another hot spare.
2939 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2940 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2941 NULL) == NULL || !avail_spare) &&
2942 is_replacing_spare(config_root, tgt, 1)) {
2943 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2944 "can only be replaced by another hot spare"));
2946 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2951 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2954 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2956 zcmd_free_nvlists(&zc);
2961 * XXX need a better way to prevent user from
2962 * booting up a half-baked vdev.
2964 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2965 "sure to wait until resilver is done "
2966 "before rebooting.\n"));
2967 (void) fprintf(stderr, "\n");
2968 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "If "
2969 "you boot from pool '%s', you may need to update\n"
2970 "boot code on newly attached disk '%s'.\n\n"
2971 "Assuming you use GPT partitioning and 'da0' is "
2972 "your new boot disk\n"
2973 "you may use the following command:\n\n"
2974 "\tgpart bootcode -b /boot/pmbr -p "
2975 "/boot/gptzfsboot -i 1 da0\n\n"),
2976 zhp->zpool_name, new_disk);
2984 * Can't attach to or replace this type of vdev.
2987 uint64_t version = zpool_get_prop_int(zhp,
2988 ZPOOL_PROP_VERSION, NULL);
2991 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2992 "cannot replace a log with a spare"));
2993 else if (version >= SPA_VERSION_MULTI_REPLACE)
2994 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2995 "already in replacing/spare config; wait "
2996 "for completion or use 'zpool detach'"));
2998 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2999 "cannot replace a replacing device"));
3001 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3002 "can only attach to mirrors and top-level "
3005 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
3010 * The new device must be a single disk.
3012 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3013 "new device must be a single disk"));
3014 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3018 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy, "
3019 "or device removal is in progress"),
3021 (void) zfs_error(hdl, EZFS_BADDEV, msg);
3026 * The new device is too small.
3028 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3029 "device is too small"));
3030 (void) zfs_error(hdl, EZFS_BADDEV, msg);
3035 * The new device has a different alignment requirement.
3037 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3038 "devices have different sector alignment"));
3039 (void) zfs_error(hdl, EZFS_BADDEV, msg);
3044 * The resulting top-level vdev spec won't fit in the label.
3046 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
3050 (void) zpool_standard_error(hdl, errno, msg);
3057 * Detach the specified device.
3060 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
3062 zfs_cmd_t zc = { 0 };
3065 boolean_t avail_spare, l2cache;
3066 libzfs_handle_t *hdl = zhp->zpool_hdl;
3068 (void) snprintf(msg, sizeof (msg),
3069 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
3071 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3072 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3074 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3077 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3080 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
3082 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3084 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
3091 * Can't detach from this type of vdev.
3093 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
3094 "applicable to mirror and replacing vdevs"));
3095 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
3100 * There are no other replicas of this device.
3102 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
3106 (void) zpool_standard_error(hdl, errno, msg);
3113 * Find a mirror vdev in the source nvlist.
3115 * The mchild array contains a list of disks in one of the top-level mirrors
3116 * of the source pool. The schild array contains a list of disks that the
3117 * user specified on the command line. We loop over the mchild array to
3118 * see if any entry in the schild array matches.
3120 * If a disk in the mchild array is found in the schild array, we return
3121 * the index of that entry. Otherwise we return -1.
3124 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
3125 nvlist_t **schild, uint_t schildren)
3129 for (mc = 0; mc < mchildren; mc++) {
3131 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3134 for (sc = 0; sc < schildren; sc++) {
3135 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3137 boolean_t result = (strcmp(mpath, spath) == 0);
3153 * Split a mirror pool. If newroot points to null, then a new nvlist
3154 * is generated and it is the responsibility of the caller to free it.
3157 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
3158 nvlist_t *props, splitflags_t flags)
3160 zfs_cmd_t zc = { 0 };
3162 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
3163 nvlist_t **varray = NULL, *zc_props = NULL;
3164 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
3165 libzfs_handle_t *hdl = zhp->zpool_hdl;
3167 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
3170 (void) snprintf(msg, sizeof (msg),
3171 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
3173 if (!zpool_name_valid(hdl, B_FALSE, newname))
3174 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
3176 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
3177 (void) fprintf(stderr, gettext("Internal error: unable to "
3178 "retrieve pool configuration\n"));
3182 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
3184 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
3187 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
3188 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
3189 props, vers, flags, msg)) == NULL)
3193 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3195 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3196 "Source pool is missing vdev tree"));
3197 nvlist_free(zc_props);
3201 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
3204 if (*newroot == NULL ||
3205 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
3206 &newchild, &newchildren) != 0)
3209 for (c = 0; c < children; c++) {
3210 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
3212 nvlist_t **mchild, *vdev;
3217 * Unlike cache & spares, slogs are stored in the
3218 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3220 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3222 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
3224 if (is_log || is_hole) {
3226 * Create a hole vdev and put it in the config.
3228 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
3230 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
3231 VDEV_TYPE_HOLE) != 0)
3233 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
3238 varray[vcount++] = vdev;
3242 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
3244 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
3245 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3246 "Source pool must be composed only of mirrors\n"));
3247 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3251 verify(nvlist_lookup_nvlist_array(child[c],
3252 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3254 /* find or add an entry for this top-level vdev */
3255 if (newchildren > 0 &&
3256 (entry = find_vdev_entry(zhp, mchild, mchildren,
3257 newchild, newchildren)) >= 0) {
3258 /* We found a disk that the user specified. */
3259 vdev = mchild[entry];
3262 /* User didn't specify a disk for this vdev. */
3263 vdev = mchild[mchildren - 1];
3266 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3270 /* did we find every disk the user specified? */
3271 if (found != newchildren) {
3272 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3273 "include at most one disk from each mirror"));
3274 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3278 /* Prepare the nvlist for populating. */
3279 if (*newroot == NULL) {
3280 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3283 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3284 VDEV_TYPE_ROOT) != 0)
3287 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3290 /* Add all the children we found */
3291 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3292 lastlog == 0 ? vcount : lastlog) != 0)
3296 * If we're just doing a dry run, exit now with success.
3299 memory_err = B_FALSE;
3304 /* now build up the config list & call the ioctl */
3305 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3308 if (nvlist_add_nvlist(newconfig,
3309 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3310 nvlist_add_string(newconfig,
3311 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3312 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3316 * The new pool is automatically part of the namespace unless we
3317 * explicitly export it.
3320 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3321 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3322 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3323 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3325 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3328 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3329 retval = zpool_standard_error(hdl, errno, msg);
3334 memory_err = B_FALSE;
3337 if (varray != NULL) {
3340 for (v = 0; v < vcount; v++)
3341 nvlist_free(varray[v]);
3344 zcmd_free_nvlists(&zc);
3345 nvlist_free(zc_props);
3346 nvlist_free(newconfig);
3348 nvlist_free(*newroot);
3356 return (no_memory(hdl));
3362 * Remove the given device.
3365 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3367 zfs_cmd_t zc = { 0 };
3370 boolean_t avail_spare, l2cache, islog;
3371 libzfs_handle_t *hdl = zhp->zpool_hdl;
3374 (void) snprintf(msg, sizeof (msg),
3375 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3377 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3378 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3380 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3382 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3383 if (islog && version < SPA_VERSION_HOLES) {
3384 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3385 "pool must be upgraded to support log removal"));
3386 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3389 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3391 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3397 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3398 "invalid config; all top-level vdevs must "
3399 "have the same sector size and not be raidz."));
3400 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3404 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3405 "Pool busy; removal may already be in progress"));
3406 (void) zfs_error(hdl, EZFS_BUSY, msg);
3410 (void) zpool_standard_error(hdl, errno, msg);
3416 zpool_vdev_remove_cancel(zpool_handle_t *zhp)
3418 zfs_cmd_t zc = { 0 };
3420 libzfs_handle_t *hdl = zhp->zpool_hdl;
3422 (void) snprintf(msg, sizeof (msg),
3423 dgettext(TEXT_DOMAIN, "cannot cancel removal"));
3425 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3428 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3431 return (zpool_standard_error(hdl, errno, msg));
3435 zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path,
3440 boolean_t avail_spare, l2cache, islog;
3441 libzfs_handle_t *hdl = zhp->zpool_hdl;
3443 (void) snprintf(msg, sizeof (msg),
3444 dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"),
3447 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3449 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3451 if (avail_spare || l2cache || islog) {
3456 if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) {
3457 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3458 "indirect size not available"));
3459 return (zfs_error(hdl, EINVAL, msg));
3465 * Clear the errors for the pool, or the particular device if specified.
3468 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3470 zfs_cmd_t zc = { 0 };
3473 zpool_load_policy_t policy;
3474 boolean_t avail_spare, l2cache;
3475 libzfs_handle_t *hdl = zhp->zpool_hdl;
3476 nvlist_t *nvi = NULL;
3480 (void) snprintf(msg, sizeof (msg),
3481 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3484 (void) snprintf(msg, sizeof (msg),
3485 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3488 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3490 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3491 &l2cache, NULL)) == NULL)
3492 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3495 * Don't allow error clearing for hot spares. Do allow
3496 * error clearing for l2cache devices.
3499 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3501 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3505 zpool_get_load_policy(rewindnvl, &policy);
3506 zc.zc_cookie = policy.zlp_rewind;
3508 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3511 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3514 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3516 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3517 zcmd_free_nvlists(&zc);
3522 if (!error || ((policy.zlp_rewind & ZPOOL_TRY_REWIND) &&
3523 errno != EPERM && errno != EACCES)) {
3524 if (policy.zlp_rewind &
3525 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3526 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3527 zpool_rewind_exclaim(hdl, zc.zc_name,
3528 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0),
3532 zcmd_free_nvlists(&zc);
3536 zcmd_free_nvlists(&zc);
3537 return (zpool_standard_error(hdl, errno, msg));
3541 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3544 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3546 zfs_cmd_t zc = { 0 };
3548 libzfs_handle_t *hdl = zhp->zpool_hdl;
3550 (void) snprintf(msg, sizeof (msg),
3551 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3554 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3556 zc.zc_cookie = ZPOOL_NO_REWIND;
3558 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3561 return (zpool_standard_error(hdl, errno, msg));
3565 * Change the GUID for a pool.
3568 zpool_reguid(zpool_handle_t *zhp)
3571 libzfs_handle_t *hdl = zhp->zpool_hdl;
3572 zfs_cmd_t zc = { 0 };
3574 (void) snprintf(msg, sizeof (msg),
3575 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3577 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3578 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3581 return (zpool_standard_error(hdl, errno, msg));
3588 zpool_reopen(zpool_handle_t *zhp)
3590 zfs_cmd_t zc = { 0 };
3592 libzfs_handle_t *hdl = zhp->zpool_hdl;
3594 (void) snprintf(msg, sizeof (msg),
3595 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
3598 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3599 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
3601 return (zpool_standard_error(hdl, errno, msg));
3604 /* call into libzfs_core to execute the sync IOCTL per pool */
3606 zpool_sync_one(zpool_handle_t *zhp, void *data)
3609 libzfs_handle_t *hdl = zpool_get_handle(zhp);
3610 const char *pool_name = zpool_get_name(zhp);
3611 boolean_t *force = data;
3612 nvlist_t *innvl = fnvlist_alloc();
3614 fnvlist_add_boolean_value(innvl, "force", *force);
3615 if ((ret = lzc_sync(pool_name, innvl, NULL)) != 0) {
3617 return (zpool_standard_error_fmt(hdl, ret,
3618 dgettext(TEXT_DOMAIN, "sync '%s' failed"), pool_name));
3626 * Convert from a devid string to a path.
3629 devid_to_path(char *devid_str)
3634 devid_nmlist_t *list = NULL;
3637 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3640 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3642 devid_str_free(minor);
3649 * In a case the strdup() fails, we will just return NULL below.
3651 path = strdup(list[0].devname);
3653 devid_free_nmlist(list);
3659 * Convert from a path to a devid string.
3662 path_to_devid(const char *path)
3669 if ((fd = open(path, O_RDONLY)) < 0)
3674 if (devid_get(fd, &devid) == 0) {
3675 if (devid_get_minor_name(fd, &minor) == 0)
3676 ret = devid_str_encode(devid, minor);
3678 devid_str_free(minor);
3690 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3691 * ignore any failure here, since a common case is for an unprivileged user to
3692 * type 'zpool status', and we'll display the correct information anyway.
3695 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3697 zfs_cmd_t zc = { 0 };
3699 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3700 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3701 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3704 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3708 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3709 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3710 * We also check if this is a whole disk, in which case we strip off the
3711 * trailing 's0' slice name.
3713 * This routine is also responsible for identifying when disks have been
3714 * reconfigured in a new location. The kernel will have opened the device by
3715 * devid, but the path will still refer to the old location. To catch this, we
3716 * first do a path -> devid translation (which is fast for the common case). If
3717 * the devid matches, we're done. If not, we do a reverse devid -> path
3718 * translation and issue the appropriate ioctl() to update the path of the vdev.
3719 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3723 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3726 char *path, *devid, *env;
3734 env = getenv("ZPOOL_VDEV_NAME_PATH");
3735 if (env && (strtoul(env, NULL, 0) > 0 ||
3736 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3737 name_flags |= VDEV_NAME_PATH;
3739 env = getenv("ZPOOL_VDEV_NAME_GUID");
3740 if (env && (strtoul(env, NULL, 0) > 0 ||
3741 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3742 name_flags |= VDEV_NAME_GUID;
3744 env = getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
3745 if (env && (strtoul(env, NULL, 0) > 0 ||
3746 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3747 name_flags |= VDEV_NAME_FOLLOW_LINKS;
3749 have_stats = nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3750 (uint64_t **)&vs, &vsc) == 0;
3751 have_path = nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0;
3754 * If the device is not currently present, assume it will not
3755 * come back at the same device path. Display the device by GUID.
3757 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3758 (name_flags & VDEV_NAME_GUID) != 0 ||
3759 have_path && have_stats && vs->vs_state <= VDEV_STATE_CANT_OPEN) {
3760 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value);
3761 (void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)value);
3763 } else if (have_path) {
3766 * If the device is dead (faulted, offline, etc) then don't
3767 * bother opening it. Otherwise we may be forcing the user to
3768 * open a misbehaving device, which can have undesirable
3771 if ((have_stats == 0 ||
3772 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3774 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3776 * Determine if the current path is correct.
3778 char *newdevid = path_to_devid(path);
3780 if (newdevid == NULL ||
3781 strcmp(devid, newdevid) != 0) {
3784 if ((newpath = devid_to_path(devid)) != NULL) {
3786 * Update the path appropriately.
3788 set_path(zhp, nv, newpath);
3789 if (nvlist_add_string(nv,
3790 ZPOOL_CONFIG_PATH, newpath) == 0)
3791 verify(nvlist_lookup_string(nv,
3799 devid_str_free(newdevid);
3803 if (name_flags & VDEV_NAME_FOLLOW_LINKS) {
3804 char *rp = realpath(path, NULL);
3806 strlcpy(buf, rp, sizeof (buf));
3812 if (strncmp(path, ZFS_DISK_ROOTD, strlen(ZFS_DISK_ROOTD)) == 0)
3813 path += strlen(ZFS_DISK_ROOTD);
3816 * Remove the partition from the path it this is a whole disk.
3818 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, &value)
3819 == 0 && value && !(name_flags & VDEV_NAME_PATH)) {
3820 int pathlen = strlen(path);
3821 char *tmp = zfs_strdup(hdl, path);
3824 * If it starts with c#, and ends with "s0" or "s1",
3825 * chop the slice off, or if it ends with "s0/old" or
3826 * "s1/old", remove the slice from the middle.
3828 if (CTD_CHECK(tmp)) {
3829 if (strcmp(&tmp[pathlen - 2], "s0") == 0 ||
3830 strcmp(&tmp[pathlen - 2], "s1") == 0) {
3831 tmp[pathlen - 2] = '\0';
3832 } else if (pathlen > 6 &&
3833 (strcmp(&tmp[pathlen - 6], "s0/old") == 0 ||
3834 strcmp(&tmp[pathlen - 6], "s1/old") == 0)) {
3835 (void) strcpy(&tmp[pathlen - 6],
3841 #else /* !illumos */
3842 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3843 path += sizeof(_PATH_DEV) - 1;
3844 #endif /* illumos */
3846 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3849 * If it's a raidz device, we need to stick in the parity level.
3851 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3852 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3854 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3855 (u_longlong_t)value);
3860 * We identify each top-level vdev by using a <type-id>
3861 * naming convention.
3863 if (name_flags & VDEV_NAME_TYPE_ID) {
3866 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3868 (void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3874 return (zfs_strdup(hdl, path));
3878 zbookmark_mem_compare(const void *a, const void *b)
3880 return (memcmp(a, b, sizeof (zbookmark_phys_t)));
3884 * Retrieve the persistent error log, uniquify the members, and return to the
3888 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3890 zfs_cmd_t zc = { 0 };
3892 zbookmark_phys_t *zb = NULL;
3896 * Retrieve the raw error list from the kernel. If the number of errors
3897 * has increased, allocate more space and continue until we get the
3900 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3904 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3905 count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL)
3907 zc.zc_nvlist_dst_size = count;
3908 (void) strcpy(zc.zc_name, zhp->zpool_name);
3910 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3912 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3913 if (errno == ENOMEM) {
3916 count = zc.zc_nvlist_dst_size;
3917 dst = zfs_alloc(zhp->zpool_hdl, count *
3918 sizeof (zbookmark_phys_t));
3921 zc.zc_nvlist_dst = (uintptr_t)dst;
3931 * Sort the resulting bookmarks. This is a little confusing due to the
3932 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3933 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3934 * _not_ copied as part of the process. So we point the start of our
3935 * array appropriate and decrement the total number of elements.
3937 zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
3938 zc.zc_nvlist_dst_size;
3939 count -= zc.zc_nvlist_dst_size;
3941 qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
3943 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3946 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3948 for (i = 0; i < count; i++) {
3951 /* ignoring zb_blkid and zb_level for now */
3952 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3953 zb[i-1].zb_object == zb[i].zb_object)
3956 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3958 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3959 zb[i].zb_objset) != 0) {
3963 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3964 zb[i].zb_object) != 0) {
3968 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3975 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3979 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3980 return (no_memory(zhp->zpool_hdl));
3984 * Upgrade a ZFS pool to the latest on-disk version.
3987 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3989 zfs_cmd_t zc = { 0 };
3990 libzfs_handle_t *hdl = zhp->zpool_hdl;
3992 (void) strcpy(zc.zc_name, zhp->zpool_name);
3993 zc.zc_cookie = new_version;
3995 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3996 return (zpool_standard_error_fmt(hdl, errno,
3997 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
4003 zfs_save_arguments(int argc, char **argv, char *string, int len)
4005 (void) strlcpy(string, basename(argv[0]), len);
4006 for (int i = 1; i < argc; i++) {
4007 (void) strlcat(string, " ", len);
4008 (void) strlcat(string, argv[i], len);
4013 zpool_log_history(libzfs_handle_t *hdl, const char *message)
4015 zfs_cmd_t zc = { 0 };
4019 args = fnvlist_alloc();
4020 fnvlist_add_string(args, "message", message);
4021 err = zcmd_write_src_nvlist(hdl, &zc, args);
4023 err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
4025 zcmd_free_nvlists(&zc);
4030 * Perform ioctl to get some command history of a pool.
4032 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
4033 * logical offset of the history buffer to start reading from.
4035 * Upon return, 'off' is the next logical offset to read from and
4036 * 'len' is the actual amount of bytes read into 'buf'.
4039 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
4041 zfs_cmd_t zc = { 0 };
4042 libzfs_handle_t *hdl = zhp->zpool_hdl;
4044 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4046 zc.zc_history = (uint64_t)(uintptr_t)buf;
4047 zc.zc_history_len = *len;
4048 zc.zc_history_offset = *off;
4050 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
4053 return (zfs_error_fmt(hdl, EZFS_PERM,
4054 dgettext(TEXT_DOMAIN,
4055 "cannot show history for pool '%s'"),
4058 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
4059 dgettext(TEXT_DOMAIN, "cannot get history for pool "
4060 "'%s'"), zhp->zpool_name));
4062 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
4063 dgettext(TEXT_DOMAIN, "cannot get history for pool "
4064 "'%s', pool must be upgraded"), zhp->zpool_name));
4066 return (zpool_standard_error_fmt(hdl, errno,
4067 dgettext(TEXT_DOMAIN,
4068 "cannot get history for '%s'"), zhp->zpool_name));
4072 *len = zc.zc_history_len;
4073 *off = zc.zc_history_offset;
4079 * Process the buffer of nvlists, unpacking and storing each nvlist record
4080 * into 'records'. 'leftover' is set to the number of bytes that weren't
4081 * processed as there wasn't a complete record.
4084 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
4085 nvlist_t ***records, uint_t *numrecords)
4091 while (bytes_read > sizeof (reclen)) {
4093 /* get length of packed record (stored as little endian) */
4094 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
4095 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
4097 if (bytes_read < sizeof (reclen) + reclen)
4101 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
4103 bytes_read -= sizeof (reclen) + reclen;
4104 buf += sizeof (reclen) + reclen;
4106 /* add record to nvlist array */
4108 if (ISP2(*numrecords + 1)) {
4109 *records = realloc(*records,
4110 *numrecords * 2 * sizeof (nvlist_t *));
4112 (*records)[*numrecords - 1] = nv;
4115 *leftover = bytes_read;
4119 /* from spa_history.c: spa_history_create_obj() */
4120 #define HIS_BUF_LEN_DEF (128 << 10)
4121 #define HIS_BUF_LEN_MAX (1 << 30)
4124 * Retrieve the command history of a pool.
4127 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
4130 uint64_t buflen = HIS_BUF_LEN_DEF;
4132 nvlist_t **records = NULL;
4133 uint_t numrecords = 0;
4136 buf = malloc(buflen);
4140 uint64_t bytes_read = buflen;
4143 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
4146 /* if nothing else was read in, we're at EOF, just return */
4147 if (bytes_read == 0)
4150 if ((err = zpool_history_unpack(buf, bytes_read,
4151 &leftover, &records, &numrecords)) != 0)
4154 if (leftover == bytes_read) {
4156 * no progress made, because buffer is not big enough
4157 * to hold this record; resize and retry.
4162 if ((buflen >= HIS_BUF_LEN_MAX) ||
4163 ((buf = malloc(buflen)) == NULL)) {
4175 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
4176 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
4177 records, numrecords) == 0);
4179 for (i = 0; i < numrecords; i++)
4180 nvlist_free(records[i]);
4187 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4188 char *pathname, size_t len)
4190 zfs_cmd_t zc = { 0 };
4191 boolean_t mounted = B_FALSE;
4192 char *mntpnt = NULL;
4193 char dsname[ZFS_MAX_DATASET_NAME_LEN];
4196 /* special case for the MOS */
4197 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
4201 /* get the dataset's name */
4202 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4204 if (ioctl(zhp->zpool_hdl->libzfs_fd,
4205 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
4206 /* just write out a path of two object numbers */
4207 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
4211 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
4213 /* find out if the dataset is mounted */
4214 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
4216 /* get the corrupted object's path */
4217 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
4219 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
4222 (void) snprintf(pathname, len, "%s%s", mntpnt,
4225 (void) snprintf(pathname, len, "%s:%s",
4226 dsname, zc.zc_value);
4229 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
4236 * Read the EFI label from the config, if a label does not exist then
4237 * pass back the error to the caller. If the caller has passed a non-NULL
4238 * diskaddr argument then we set it to the starting address of the EFI
4239 * partition. If the caller has passed a non-NULL boolean argument, then
4240 * we set it to indicate if the disk does have efi system partition.
4243 read_efi_label(nvlist_t *config, diskaddr_t *sb, boolean_t *system)
4247 char diskname[MAXPATHLEN];
4248 boolean_t boot = B_FALSE;
4252 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
4255 (void) snprintf(diskname, sizeof (diskname), "%s%s", ZFS_RDISK_ROOT,
4256 strrchr(path, '/'));
4257 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
4258 struct dk_gpt *vtoc;
4260 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
4261 for (slice = 0; slice < vtoc->efi_nparts; slice++) {
4262 if (vtoc->efi_parts[slice].p_tag == V_SYSTEM)
4264 if (vtoc->efi_parts[slice].p_tag == V_USR)
4267 if (sb != NULL && vtoc->efi_parts[slice].p_tag == V_USR)
4268 *sb = vtoc->efi_parts[slice].p_start;
4279 * determine where a partition starts on a disk in the current
4283 find_start_block(nvlist_t *config)
4287 diskaddr_t sb = MAXOFFSET_T;
4290 if (nvlist_lookup_nvlist_array(config,
4291 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
4292 if (nvlist_lookup_uint64(config,
4293 ZPOOL_CONFIG_WHOLE_DISK,
4294 &wholedisk) != 0 || !wholedisk) {
4295 return (MAXOFFSET_T);
4297 if (read_efi_label(config, &sb, NULL) < 0)
4302 for (c = 0; c < children; c++) {
4303 sb = find_start_block(child[c]);
4304 if (sb != MAXOFFSET_T) {
4308 return (MAXOFFSET_T);
4310 #endif /* illumos */
4313 * Label an individual disk. The name provided is the short name,
4314 * stripped of any leading /dev path.
4317 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name,
4318 zpool_boot_label_t boot_type, uint64_t boot_size, int *slice)
4321 char path[MAXPATHLEN];
4322 struct dk_gpt *vtoc;
4324 size_t resv = EFI_MIN_RESV_SIZE;
4325 uint64_t slice_size;
4326 diskaddr_t start_block;
4329 /* prepare an error message just in case */
4330 (void) snprintf(errbuf, sizeof (errbuf),
4331 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
4336 verify(nvlist_lookup_nvlist(zhp->zpool_config,
4337 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4339 if (zhp->zpool_start_block == 0)
4340 start_block = find_start_block(nvroot);
4342 start_block = zhp->zpool_start_block;
4343 zhp->zpool_start_block = start_block;
4346 start_block = NEW_START_BLOCK;
4349 (void) snprintf(path, sizeof (path), "%s/%s%s", ZFS_RDISK_ROOT, name,
4352 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
4354 * This shouldn't happen. We've long since verified that this
4355 * is a valid device.
4358 dgettext(TEXT_DOMAIN, "unable to open device"));
4359 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
4362 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
4364 * The only way this can fail is if we run out of memory, or we
4365 * were unable to read the disk's capacity
4367 if (errno == ENOMEM)
4368 (void) no_memory(hdl);
4371 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4372 "unable to read disk capacity"), name);
4374 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
4378 * Why we use V_USR: V_BACKUP confuses users, and is considered
4379 * disposable by some EFI utilities (since EFI doesn't have a backup
4380 * slice). V_UNASSIGNED is supposed to be used only for zero size
4381 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4382 * etc. were all pretty specific. V_USR is as close to reality as we
4383 * can get, in the absence of V_OTHER.
4385 /* first fix the partition start block */
4386 if (start_block == MAXOFFSET_T)
4387 start_block = NEW_START_BLOCK;
4390 * EFI System partition is using slice 0.
4391 * ZFS is on slice 1 and slice 8 is reserved.
4392 * We assume the GPT partition table without system
4393 * partition has zfs p_start == NEW_START_BLOCK.
4394 * If start_block != NEW_START_BLOCK, it means we have
4395 * system partition. Correct solution would be to query/cache vtoc
4396 * from existing vdev member.
4398 if (boot_type == ZPOOL_CREATE_BOOT_LABEL) {
4399 if (boot_size % vtoc->efi_lbasize != 0) {
4400 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4401 "boot partition size must be a multiple of %d"),
4405 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4408 * System partition size checks.
4409 * Note the 1MB is quite arbitrary value, since we
4410 * are creating dedicated pool, it should be enough
4411 * to hold fat + efi bootloader. May need to be
4412 * adjusted if the bootloader size will grow.
4414 if (boot_size < 1024 * 1024) {
4416 zfs_nicenum(boot_size, buf, sizeof (buf));
4417 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4418 "Specified size %s for EFI System partition is too "
4419 "small, the minimum size is 1MB."), buf);
4422 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4424 /* 33MB is tested with mkfs -F pcfs */
4425 if (hdl->libzfs_printerr &&
4426 ((vtoc->efi_lbasize == 512 &&
4427 boot_size < 33 * 1024 * 1024) ||
4428 (vtoc->efi_lbasize == 4096 &&
4429 boot_size < 256 * 1024 * 1024))) {
4431 zfs_nicenum(boot_size, buf, sizeof (buf));
4432 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
4433 "Warning: EFI System partition size %s is "
4434 "not allowing to create FAT32 file\nsystem, which "
4435 "may result in unbootable system.\n"), buf);
4437 /* Adjust zfs partition start by size of system partition. */
4438 start_block += boot_size / vtoc->efi_lbasize;
4441 if (start_block == NEW_START_BLOCK) {
4443 * Use default layout.
4444 * ZFS is on slice 0 and slice 8 is reserved.
4446 slice_size = vtoc->efi_last_u_lba + 1;
4447 slice_size -= EFI_MIN_RESV_SIZE;
4448 slice_size -= start_block;
4452 vtoc->efi_parts[0].p_start = start_block;
4453 vtoc->efi_parts[0].p_size = slice_size;
4455 vtoc->efi_parts[0].p_tag = V_USR;
4456 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
4458 vtoc->efi_parts[8].p_start = slice_size + start_block;
4459 vtoc->efi_parts[8].p_size = resv;
4460 vtoc->efi_parts[8].p_tag = V_RESERVED;
4462 slice_size = start_block - NEW_START_BLOCK;
4463 vtoc->efi_parts[0].p_start = NEW_START_BLOCK;
4464 vtoc->efi_parts[0].p_size = slice_size;
4465 vtoc->efi_parts[0].p_tag = V_SYSTEM;
4466 (void) strcpy(vtoc->efi_parts[0].p_name, "loader");
4469 /* prepare slice 1 */
4470 slice_size = vtoc->efi_last_u_lba + 1 - slice_size;
4472 slice_size -= NEW_START_BLOCK;
4473 vtoc->efi_parts[1].p_start = start_block;
4474 vtoc->efi_parts[1].p_size = slice_size;
4475 vtoc->efi_parts[1].p_tag = V_USR;
4476 (void) strcpy(vtoc->efi_parts[1].p_name, "zfs");
4478 vtoc->efi_parts[8].p_start = slice_size + start_block;
4479 vtoc->efi_parts[8].p_size = resv;
4480 vtoc->efi_parts[8].p_tag = V_RESERVED;
4483 if (efi_write(fd, vtoc) != 0) {
4485 * Some block drivers (like pcata) may not support EFI
4486 * GPT labels. Print out a helpful error message dir-
4487 * ecting the user to manually label the disk and give
4493 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4494 "try using fdisk(1M) and then provide a specific slice"));
4495 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4500 #endif /* illumos */
4505 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
4511 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
4512 if (strcmp(type, VDEV_TYPE_FILE) == 0 ||
4513 strcmp(type, VDEV_TYPE_HOLE) == 0 ||
4514 strcmp(type, VDEV_TYPE_MISSING) == 0) {
4515 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4516 "vdev type '%s' is not supported"), type);
4517 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
4520 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
4521 &child, &children) == 0) {
4522 for (c = 0; c < children; c++) {
4523 if (!supported_dump_vdev_type(hdl, child[c], errbuf))
4531 * Check if this zvol is allowable for use as a dump device; zero if
4532 * it is, > 0 if it isn't, < 0 if it isn't a zvol.
4534 * Allowable storage configurations include mirrors, all raidz variants, and
4535 * pools with log, cache, and spare devices. Pools which are backed by files or
4536 * have missing/hole vdevs are not suitable.
4539 zvol_check_dump_config(char *arg)
4541 zpool_handle_t *zhp = NULL;
4542 nvlist_t *config, *nvroot;
4546 libzfs_handle_t *hdl;
4548 char poolname[ZFS_MAX_DATASET_NAME_LEN];
4549 int pathlen = strlen(ZVOL_FULL_DEV_DIR);
4552 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
4556 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4557 "dump is not supported on device '%s'"), arg);
4559 if ((hdl = libzfs_init()) == NULL)
4561 libzfs_print_on_error(hdl, B_TRUE);
4563 volname = arg + pathlen;
4565 /* check the configuration of the pool */
4566 if ((p = strchr(volname, '/')) == NULL) {
4567 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4568 "malformed dataset name"));
4569 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4571 } else if (p - volname >= ZFS_MAX_DATASET_NAME_LEN) {
4572 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4573 "dataset name is too long"));
4574 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
4577 (void) strncpy(poolname, volname, p - volname);
4578 poolname[p - volname] = '\0';
4581 if ((zhp = zpool_open(hdl, poolname)) == NULL) {
4582 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4583 "could not open pool '%s'"), poolname);
4584 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4587 config = zpool_get_config(zhp, NULL);
4588 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4590 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4591 "could not obtain vdev configuration for '%s'"), poolname);
4592 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4596 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4597 &top, &toplevels) == 0);
4599 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
4612 zpool_nextboot(libzfs_handle_t *hdl, uint64_t pool_guid, uint64_t dev_guid,
4613 const char *command)
4615 zfs_cmd_t zc = { 0 };
4621 args = fnvlist_alloc();
4622 fnvlist_add_uint64(args, ZPOOL_CONFIG_POOL_GUID, pool_guid);
4623 fnvlist_add_uint64(args, ZPOOL_CONFIG_GUID, dev_guid);
4624 fnvlist_add_string(args, "command", command);
4625 error = zcmd_write_src_nvlist(hdl, &zc, args);
4627 error = ioctl(hdl->libzfs_fd, ZFS_IOC_NEXTBOOT, &zc);
4628 zcmd_free_nvlists(&zc);