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.
31 #include <sys/types.h>
43 #include <sys/zfs_ioctl.h>
46 #include "zfs_namecheck.h"
48 #include "libzfs_impl.h"
49 #include "zfs_comutil.h"
50 #include "zfeature_common.h"
52 static int read_efi_label(nvlist_t *, diskaddr_t *, boolean_t *);
53 static boolean_t zpool_vdev_is_interior(const char *name);
55 #define BACKUP_SLICE "s2"
57 typedef struct prop_flags {
58 int create:1; /* Validate property on creation */
59 int import:1; /* Validate property on import */
63 * ====================================================================
64 * zpool property functions
65 * ====================================================================
69 zpool_get_all_props(zpool_handle_t *zhp)
72 libzfs_handle_t *hdl = zhp->zpool_hdl;
74 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
76 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
79 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
80 if (errno == ENOMEM) {
81 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
82 zcmd_free_nvlists(&zc);
86 zcmd_free_nvlists(&zc);
91 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
92 zcmd_free_nvlists(&zc);
96 zcmd_free_nvlists(&zc);
102 zpool_props_refresh(zpool_handle_t *zhp)
106 old_props = zhp->zpool_props;
108 if (zpool_get_all_props(zhp) != 0)
111 nvlist_free(old_props);
116 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
122 zprop_source_t source;
124 nvl = zhp->zpool_props;
125 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
126 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
128 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
130 source = ZPROP_SRC_DEFAULT;
131 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
142 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
146 zprop_source_t source;
148 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
150 * zpool_get_all_props() has most likely failed because
151 * the pool is faulted, but if all we need is the top level
152 * vdev's guid then get it from the zhp config nvlist.
154 if ((prop == ZPOOL_PROP_GUID) &&
155 (nvlist_lookup_nvlist(zhp->zpool_config,
156 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
157 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
161 return (zpool_prop_default_numeric(prop));
164 nvl = zhp->zpool_props;
165 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
166 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
168 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
170 source = ZPROP_SRC_DEFAULT;
171 value = zpool_prop_default_numeric(prop);
181 * Map VDEV STATE to printed strings.
184 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
187 case VDEV_STATE_CLOSED:
188 case VDEV_STATE_OFFLINE:
189 return (gettext("OFFLINE"));
190 case VDEV_STATE_REMOVED:
191 return (gettext("REMOVED"));
192 case VDEV_STATE_CANT_OPEN:
193 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
194 return (gettext("FAULTED"));
195 else if (aux == VDEV_AUX_SPLIT_POOL)
196 return (gettext("SPLIT"));
198 return (gettext("UNAVAIL"));
199 case VDEV_STATE_FAULTED:
200 return (gettext("FAULTED"));
201 case VDEV_STATE_DEGRADED:
202 return (gettext("DEGRADED"));
203 case VDEV_STATE_HEALTHY:
204 return (gettext("ONLINE"));
210 return (gettext("UNKNOWN"));
214 * Map POOL STATE to printed strings.
217 zpool_pool_state_to_name(pool_state_t state)
220 case POOL_STATE_ACTIVE:
221 return (gettext("ACTIVE"));
222 case POOL_STATE_EXPORTED:
223 return (gettext("EXPORTED"));
224 case POOL_STATE_DESTROYED:
225 return (gettext("DESTROYED"));
226 case POOL_STATE_SPARE:
227 return (gettext("SPARE"));
228 case POOL_STATE_L2CACHE:
229 return (gettext("L2CACHE"));
230 case POOL_STATE_UNINITIALIZED:
231 return (gettext("UNINITIALIZED"));
232 case POOL_STATE_UNAVAIL:
233 return (gettext("UNAVAIL"));
234 case POOL_STATE_POTENTIALLY_ACTIVE:
235 return (gettext("POTENTIALLY_ACTIVE"));
238 return (gettext("UNKNOWN"));
242 * Get a zpool property value for 'prop' and return the value in
243 * a pre-allocated buffer.
246 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf, size_t len,
247 zprop_source_t *srctype, boolean_t literal)
251 zprop_source_t src = ZPROP_SRC_NONE;
256 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
258 case ZPOOL_PROP_NAME:
259 (void) strlcpy(buf, zpool_get_name(zhp), len);
262 case ZPOOL_PROP_HEALTH:
264 zpool_pool_state_to_name(POOL_STATE_UNAVAIL), len);
267 case ZPOOL_PROP_GUID:
268 intval = zpool_get_prop_int(zhp, prop, &src);
269 (void) snprintf(buf, len, "%llu", intval);
272 case ZPOOL_PROP_ALTROOT:
273 case ZPOOL_PROP_CACHEFILE:
274 case ZPOOL_PROP_COMMENT:
275 if (zhp->zpool_props != NULL ||
276 zpool_get_all_props(zhp) == 0) {
278 zpool_get_prop_string(zhp, prop, &src),
284 (void) strlcpy(buf, "-", len);
293 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
294 prop != ZPOOL_PROP_NAME)
297 switch (zpool_prop_get_type(prop)) {
298 case PROP_TYPE_STRING:
299 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
303 case PROP_TYPE_NUMBER:
304 intval = zpool_get_prop_int(zhp, prop, &src);
307 case ZPOOL_PROP_SIZE:
308 case ZPOOL_PROP_ALLOCATED:
309 case ZPOOL_PROP_FREE:
310 case ZPOOL_PROP_FREEING:
311 case ZPOOL_PROP_LEAKED:
313 (void) snprintf(buf, len, "%llu",
314 (u_longlong_t)intval);
316 (void) zfs_nicenum(intval, buf, len);
319 case ZPOOL_PROP_BOOTSIZE:
320 case ZPOOL_PROP_EXPANDSZ:
321 case ZPOOL_PROP_CHECKPOINT:
323 (void) strlcpy(buf, "-", len);
324 } else if (literal) {
325 (void) snprintf(buf, len, "%llu",
326 (u_longlong_t)intval);
328 (void) zfs_nicenum(intval, buf, len);
331 case ZPOOL_PROP_CAPACITY:
333 (void) snprintf(buf, len, "%llu",
334 (u_longlong_t)intval);
336 (void) snprintf(buf, len, "%llu%%",
337 (u_longlong_t)intval);
340 case ZPOOL_PROP_FRAGMENTATION:
341 if (intval == UINT64_MAX) {
342 (void) strlcpy(buf, "-", len);
344 (void) snprintf(buf, len, "%llu%%",
345 (u_longlong_t)intval);
348 case ZPOOL_PROP_DEDUPRATIO:
349 (void) snprintf(buf, len, "%llu.%02llux",
350 (u_longlong_t)(intval / 100),
351 (u_longlong_t)(intval % 100));
353 case ZPOOL_PROP_HEALTH:
354 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
355 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
356 verify(nvlist_lookup_uint64_array(nvroot,
357 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
360 (void) strlcpy(buf, zpool_state_to_name(intval,
363 case ZPOOL_PROP_VERSION:
364 if (intval >= SPA_VERSION_FEATURES) {
365 (void) snprintf(buf, len, "-");
370 (void) snprintf(buf, len, "%llu", intval);
374 case PROP_TYPE_INDEX:
375 intval = zpool_get_prop_int(zhp, prop, &src);
376 if (zpool_prop_index_to_string(prop, intval, &strval)
379 (void) strlcpy(buf, strval, len);
393 * Check if the bootfs name has the same pool name as it is set to.
394 * Assuming bootfs is a valid dataset name.
397 bootfs_name_valid(const char *pool, char *bootfs)
399 int len = strlen(pool);
401 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
404 if (strncmp(pool, bootfs, len) == 0 &&
405 (bootfs[len] == '/' || bootfs[len] == '\0'))
412 zpool_is_bootable(zpool_handle_t *zhp)
414 char bootfs[ZFS_MAX_DATASET_NAME_LEN];
416 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
417 sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-",
418 sizeof (bootfs)) != 0);
423 * Given an nvlist of zpool properties to be set, validate that they are
424 * correct, and parse any numeric properties (index, boolean, etc) if they are
425 * specified as strings.
428 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
429 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
437 struct stat64 statbuf;
440 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
441 (void) no_memory(hdl);
446 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
447 const char *propname = nvpair_name(elem);
449 prop = zpool_name_to_prop(propname);
450 if (prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname)) {
452 char *fname = strchr(propname, '@') + 1;
454 err = zfeature_lookup_name(fname, NULL);
456 ASSERT3U(err, ==, ENOENT);
457 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
458 "invalid feature '%s'"), fname);
459 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
463 if (nvpair_type(elem) != DATA_TYPE_STRING) {
464 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
465 "'%s' must be a string"), propname);
466 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
470 (void) nvpair_value_string(elem, &strval);
471 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0) {
472 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
473 "property '%s' can only be set to "
474 "'enabled'"), propname);
475 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
479 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
480 (void) no_memory(hdl);
487 * Make sure this property is valid and applies to this type.
489 if (prop == ZPOOL_PROP_INVAL) {
490 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
491 "invalid property '%s'"), propname);
492 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
496 if (zpool_prop_readonly(prop)) {
497 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
498 "is readonly"), propname);
499 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
503 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
504 &strval, &intval, errbuf) != 0)
508 * Perform additional checking for specific properties.
511 case ZPOOL_PROP_VERSION:
512 if (intval < version ||
513 !SPA_VERSION_IS_SUPPORTED(intval)) {
514 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
515 "property '%s' number %d is invalid."),
517 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
522 case ZPOOL_PROP_BOOTSIZE:
524 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
525 "property '%s' can only be set during pool "
526 "creation"), propname);
527 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
532 case ZPOOL_PROP_BOOTFS:
533 if (flags.create || flags.import) {
534 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
535 "property '%s' cannot be set at creation "
536 "or import time"), propname);
537 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
541 if (version < SPA_VERSION_BOOTFS) {
542 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
543 "pool must be upgraded to support "
544 "'%s' property"), propname);
545 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
550 * bootfs property value has to be a dataset name and
551 * the dataset has to be in the same pool as it sets to.
553 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
555 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
556 "is an invalid name"), strval);
557 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
561 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
562 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
563 "could not open pool '%s'"), poolname);
564 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
570 case ZPOOL_PROP_ALTROOT:
571 if (!flags.create && !flags.import) {
572 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
573 "property '%s' can only be set during pool "
574 "creation or import"), propname);
575 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
579 if (strval[0] != '/') {
580 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
581 "bad alternate root '%s'"), strval);
582 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
587 case ZPOOL_PROP_CACHEFILE:
588 if (strval[0] == '\0')
591 if (strcmp(strval, "none") == 0)
594 if (strval[0] != '/') {
595 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
596 "property '%s' must be empty, an "
597 "absolute path, or 'none'"), propname);
598 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
602 slash = strrchr(strval, '/');
604 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
605 strcmp(slash, "/..") == 0) {
606 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
607 "'%s' is not a valid file"), strval);
608 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
614 if (strval[0] != '\0' &&
615 (stat64(strval, &statbuf) != 0 ||
616 !S_ISDIR(statbuf.st_mode))) {
617 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
618 "'%s' is not a valid directory"),
620 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
627 case ZPOOL_PROP_COMMENT:
628 for (check = strval; *check != '\0'; check++) {
629 if (!isprint(*check)) {
631 dgettext(TEXT_DOMAIN,
632 "comment may only have printable "
634 (void) zfs_error(hdl, EZFS_BADPROP,
639 if (strlen(strval) > ZPROP_MAX_COMMENT) {
640 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
641 "comment must not exceed %d characters"),
643 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
648 case ZPOOL_PROP_READONLY:
650 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
651 "property '%s' can only be set at "
652 "import time"), propname);
653 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
658 case ZPOOL_PROP_TNAME:
660 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
661 "property '%s' can only be set at "
662 "creation time"), propname);
663 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
669 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
670 "property '%s'(%d) not defined"), propname, prop);
677 nvlist_free(retprops);
682 * Set zpool property : propname=propval.
685 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
687 zfs_cmd_t zc = { 0 };
690 nvlist_t *nvl = NULL;
693 prop_flags_t flags = { 0 };
695 (void) snprintf(errbuf, sizeof (errbuf),
696 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
699 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
700 return (no_memory(zhp->zpool_hdl));
702 if (nvlist_add_string(nvl, propname, propval) != 0) {
704 return (no_memory(zhp->zpool_hdl));
707 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
708 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
709 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
718 * Execute the corresponding ioctl() to set this property.
720 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
722 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
727 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
729 zcmd_free_nvlists(&zc);
733 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
735 (void) zpool_props_refresh(zhp);
741 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
743 libzfs_handle_t *hdl = zhp->zpool_hdl;
745 char buf[ZFS_MAXPROPLEN];
746 nvlist_t *features = NULL;
748 boolean_t firstexpand = (NULL == *plp);
750 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
754 while (*last != NULL)
755 last = &(*last)->pl_next;
758 features = zpool_get_features(zhp);
760 if ((*plp)->pl_all && firstexpand) {
761 for (int i = 0; i < SPA_FEATURES; i++) {
762 zprop_list_t *entry = zfs_alloc(hdl,
763 sizeof (zprop_list_t));
764 entry->pl_prop = ZPROP_INVAL;
765 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
766 spa_feature_table[i].fi_uname);
767 entry->pl_width = strlen(entry->pl_user_prop);
768 entry->pl_all = B_TRUE;
771 last = &entry->pl_next;
775 /* add any unsupported features */
776 for (nvpair_t *nvp = nvlist_next_nvpair(features, NULL);
777 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
782 if (zfeature_is_supported(nvpair_name(nvp)))
785 propname = zfs_asprintf(hdl, "unsupported@%s",
789 * Before adding the property to the list make sure that no
790 * other pool already added the same property.
794 while (entry != NULL) {
795 if (entry->pl_user_prop != NULL &&
796 strcmp(propname, entry->pl_user_prop) == 0) {
800 entry = entry->pl_next;
807 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
808 entry->pl_prop = ZPROP_INVAL;
809 entry->pl_user_prop = propname;
810 entry->pl_width = strlen(entry->pl_user_prop);
811 entry->pl_all = B_TRUE;
814 last = &entry->pl_next;
817 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
822 if (entry->pl_prop != ZPROP_INVAL &&
823 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
824 NULL, B_FALSE) == 0) {
825 if (strlen(buf) > entry->pl_width)
826 entry->pl_width = strlen(buf);
834 * Get the state for the given feature on the given ZFS pool.
837 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
841 boolean_t found = B_FALSE;
842 nvlist_t *features = zpool_get_features(zhp);
844 const char *feature = strchr(propname, '@') + 1;
846 supported = zpool_prop_feature(propname);
847 ASSERT(supported || zpool_prop_unsupported(propname));
850 * Convert from feature name to feature guid. This conversion is
851 * unecessary for unsupported@... properties because they already
858 ret = zfeature_lookup_name(feature, &fid);
860 (void) strlcpy(buf, "-", len);
863 feature = spa_feature_table[fid].fi_guid;
866 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
871 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
874 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
876 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
881 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
883 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
886 (void) strlcpy(buf, "-", len);
895 * Don't start the slice at the default block of 34; many storage
896 * devices will use a stripe width of 128k, so start there instead.
898 #define NEW_START_BLOCK 256
901 * Validate the given pool name, optionally putting an extended error message in
905 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
911 ret = pool_namecheck(pool, &why, &what);
914 * The rules for reserved pool names were extended at a later point.
915 * But we need to support users with existing pools that may now be
916 * invalid. So we only check for this expanded set of names during a
917 * create (or import), and only in userland.
919 if (ret == 0 && !isopen &&
920 (strncmp(pool, "mirror", 6) == 0 ||
921 strncmp(pool, "raidz", 5) == 0 ||
922 strncmp(pool, "spare", 5) == 0 ||
923 strcmp(pool, "log") == 0)) {
926 dgettext(TEXT_DOMAIN, "name is reserved"));
934 case NAME_ERR_TOOLONG:
936 dgettext(TEXT_DOMAIN, "name is too long"));
939 case NAME_ERR_INVALCHAR:
941 dgettext(TEXT_DOMAIN, "invalid character "
942 "'%c' in pool name"), what);
945 case NAME_ERR_NOLETTER:
946 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
947 "name must begin with a letter"));
950 case NAME_ERR_RESERVED:
951 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
952 "name is reserved"));
955 case NAME_ERR_DISKLIKE:
956 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
957 "pool name is reserved"));
960 case NAME_ERR_LEADING_SLASH:
961 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
962 "leading slash in name"));
965 case NAME_ERR_EMPTY_COMPONENT:
966 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
967 "empty component in name"));
970 case NAME_ERR_TRAILING_SLASH:
971 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
972 "trailing slash in name"));
975 case NAME_ERR_MULTIPLE_DELIMITERS:
976 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
977 "multiple '@' and/or '#' delimiters in "
982 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
983 "(%d) not defined"), why);
994 * Open a handle to the given pool, even if the pool is currently in the FAULTED
998 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
1000 zpool_handle_t *zhp;
1004 * Make sure the pool name is valid.
1006 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
1007 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1008 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1013 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1016 zhp->zpool_hdl = hdl;
1017 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1019 if (zpool_refresh_stats(zhp, &missing) != 0) {
1025 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1026 (void) zfs_error_fmt(hdl, EZFS_NOENT,
1027 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1036 * Like the above, but silent on error. Used when iterating over pools (because
1037 * the configuration cache may be out of date).
1040 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1042 zpool_handle_t *zhp;
1045 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1048 zhp->zpool_hdl = hdl;
1049 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1051 if (zpool_refresh_stats(zhp, &missing) != 0) {
1067 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1071 zpool_open(libzfs_handle_t *hdl, const char *pool)
1073 zpool_handle_t *zhp;
1075 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1078 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1079 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1080 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1089 * Close the handle. Simply frees the memory associated with the handle.
1092 zpool_close(zpool_handle_t *zhp)
1094 nvlist_free(zhp->zpool_config);
1095 nvlist_free(zhp->zpool_old_config);
1096 nvlist_free(zhp->zpool_props);
1101 * Return the name of the pool.
1104 zpool_get_name(zpool_handle_t *zhp)
1106 return (zhp->zpool_name);
1111 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1114 zpool_get_state(zpool_handle_t *zhp)
1116 return (zhp->zpool_state);
1120 * Create the named pool, using the provided vdev list. It is assumed
1121 * that the consumer has already validated the contents of the nvlist, so we
1122 * don't have to worry about error semantics.
1125 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1126 nvlist_t *props, nvlist_t *fsprops)
1128 zfs_cmd_t zc = { 0 };
1129 nvlist_t *zc_fsprops = NULL;
1130 nvlist_t *zc_props = NULL;
1134 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1135 "cannot create '%s'"), pool);
1137 if (!zpool_name_valid(hdl, B_FALSE, pool))
1138 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1140 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1144 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1146 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1147 SPA_VERSION_1, flags, msg)) == NULL) {
1156 zoned = ((nvlist_lookup_string(fsprops,
1157 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1158 strcmp(zonestr, "on") == 0);
1160 if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
1161 fsprops, zoned, NULL, NULL, msg)) == NULL) {
1165 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1168 if (nvlist_add_nvlist(zc_props,
1169 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1174 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1177 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1179 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1181 zcmd_free_nvlists(&zc);
1182 nvlist_free(zc_props);
1183 nvlist_free(zc_fsprops);
1188 * This can happen if the user has specified the same
1189 * device multiple times. We can't reliably detect this
1190 * until we try to add it and see we already have a
1193 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1194 "one or more vdevs refer to the same device"));
1195 return (zfs_error(hdl, EZFS_BADDEV, msg));
1199 * This happens if the record size is smaller or larger
1200 * than the allowed size range, or not a power of 2.
1202 * NOTE: although zfs_valid_proplist is called earlier,
1203 * this case may have slipped through since the
1204 * pool does not exist yet and it is therefore
1205 * impossible to read properties e.g. max blocksize
1208 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1209 "record size invalid"));
1210 return (zfs_error(hdl, EZFS_BADPROP, msg));
1214 * This occurs when one of the devices is below
1215 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1216 * device was the problem device since there's no
1217 * reliable way to determine device size from userland.
1222 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1224 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1225 "one or more devices is less than the "
1226 "minimum size (%s)"), buf);
1228 return (zfs_error(hdl, EZFS_BADDEV, msg));
1231 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1232 "one or more devices is out of space"));
1233 return (zfs_error(hdl, EZFS_BADDEV, msg));
1236 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1237 "cache device must be a disk or disk slice"));
1238 return (zfs_error(hdl, EZFS_BADDEV, msg));
1241 return (zpool_standard_error(hdl, errno, msg));
1246 zcmd_free_nvlists(&zc);
1247 nvlist_free(zc_props);
1248 nvlist_free(zc_fsprops);
1253 * Destroy the given pool. It is up to the caller to ensure that there are no
1254 * datasets left in the pool.
1257 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1259 zfs_cmd_t zc = { 0 };
1260 zfs_handle_t *zfp = NULL;
1261 libzfs_handle_t *hdl = zhp->zpool_hdl;
1264 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1265 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1268 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1269 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1271 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1272 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1273 "cannot destroy '%s'"), zhp->zpool_name);
1275 if (errno == EROFS) {
1276 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1277 "one or more devices is read only"));
1278 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1280 (void) zpool_standard_error(hdl, errno, msg);
1289 remove_mountpoint(zfp);
1297 * Create a checkpoint in the given pool.
1300 zpool_checkpoint(zpool_handle_t *zhp)
1302 libzfs_handle_t *hdl = zhp->zpool_hdl;
1306 error = lzc_pool_checkpoint(zhp->zpool_name);
1308 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1309 "cannot checkpoint '%s'"), zhp->zpool_name);
1310 (void) zpool_standard_error(hdl, error, msg);
1318 * Discard the checkpoint from the given pool.
1321 zpool_discard_checkpoint(zpool_handle_t *zhp)
1323 libzfs_handle_t *hdl = zhp->zpool_hdl;
1327 error = lzc_pool_checkpoint_discard(zhp->zpool_name);
1329 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1330 "cannot discard checkpoint in '%s'"), zhp->zpool_name);
1331 (void) zpool_standard_error(hdl, error, msg);
1339 * Add the given vdevs to the pool. The caller must have already performed the
1340 * necessary verification to ensure that the vdev specification is well-formed.
1343 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1345 zfs_cmd_t zc = { 0 };
1347 libzfs_handle_t *hdl = zhp->zpool_hdl;
1349 nvlist_t **spares, **l2cache;
1350 uint_t nspares, nl2cache;
1352 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1353 "cannot add to '%s'"), zhp->zpool_name);
1355 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1356 SPA_VERSION_SPARES &&
1357 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1358 &spares, &nspares) == 0) {
1359 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1360 "upgraded to add hot spares"));
1361 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1364 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1365 SPA_VERSION_L2CACHE &&
1366 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1367 &l2cache, &nl2cache) == 0) {
1368 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1369 "upgraded to add cache devices"));
1370 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1373 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1375 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1377 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1381 * This can happen if the user has specified the same
1382 * device multiple times. We can't reliably detect this
1383 * until we try to add it and see we already have a
1386 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1387 "one or more vdevs refer to the same device"));
1388 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1392 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1393 "invalid config; a pool with removing/removed "
1394 "vdevs does not support adding raidz vdevs"));
1395 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1400 * This occurrs when one of the devices is below
1401 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1402 * device was the problem device since there's no
1403 * reliable way to determine device size from userland.
1408 zfs_nicenum(SPA_MINDEVSIZE, buf, sizeof (buf));
1410 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1411 "device is less than the minimum "
1414 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1418 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1419 "pool must be upgraded to add these vdevs"));
1420 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1424 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1425 "root pool can not have multiple vdevs"
1426 " or separate logs"));
1427 (void) zfs_error(hdl, EZFS_POOL_NOTSUP, msg);
1431 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1432 "cache device must be a disk or disk slice"));
1433 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1437 (void) zpool_standard_error(hdl, errno, msg);
1445 zcmd_free_nvlists(&zc);
1451 * Exports the pool from the system. The caller must ensure that there are no
1452 * mounted datasets in the pool.
1455 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1456 const char *log_str)
1458 zfs_cmd_t zc = { 0 };
1461 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1462 "cannot export '%s'"), zhp->zpool_name);
1464 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1465 zc.zc_cookie = force;
1466 zc.zc_guid = hardforce;
1467 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1469 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1472 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1473 "use '-f' to override the following errors:\n"
1474 "'%s' has an active shared spare which could be"
1475 " used by other pools once '%s' is exported."),
1476 zhp->zpool_name, zhp->zpool_name);
1477 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1480 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1489 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1491 return (zpool_export_common(zhp, force, B_FALSE, log_str));
1495 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1497 return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1501 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1504 nvlist_t *nv = NULL;
1510 if (!hdl->libzfs_printerr || config == NULL)
1513 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1514 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1518 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1520 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1522 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1523 strftime(timestr, 128, 0, &t) != 0) {
1525 (void) printf(dgettext(TEXT_DOMAIN,
1526 "Would be able to return %s "
1527 "to its state as of %s.\n"),
1530 (void) printf(dgettext(TEXT_DOMAIN,
1531 "Pool %s returned to its state as of %s.\n"),
1535 (void) printf(dgettext(TEXT_DOMAIN,
1536 "%s approximately %lld "),
1537 dryrun ? "Would discard" : "Discarded",
1539 (void) printf(dgettext(TEXT_DOMAIN,
1540 "minutes of transactions.\n"));
1541 } else if (loss > 0) {
1542 (void) printf(dgettext(TEXT_DOMAIN,
1543 "%s approximately %lld "),
1544 dryrun ? "Would discard" : "Discarded", loss);
1545 (void) printf(dgettext(TEXT_DOMAIN,
1546 "seconds of transactions.\n"));
1552 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1555 nvlist_t *nv = NULL;
1557 uint64_t edata = UINT64_MAX;
1562 if (!hdl->libzfs_printerr)
1566 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1568 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1570 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1571 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1572 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1573 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1576 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1577 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1580 (void) printf(dgettext(TEXT_DOMAIN,
1581 "Recovery is possible, but will result in some data loss.\n"));
1583 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1584 strftime(timestr, 128, 0, &t) != 0) {
1585 (void) printf(dgettext(TEXT_DOMAIN,
1586 "\tReturning the pool to its state as of %s\n"
1587 "\tshould correct the problem. "),
1590 (void) printf(dgettext(TEXT_DOMAIN,
1591 "\tReverting the pool to an earlier state "
1592 "should correct the problem.\n\t"));
1596 (void) printf(dgettext(TEXT_DOMAIN,
1597 "Approximately %lld minutes of data\n"
1598 "\tmust be discarded, irreversibly. "), (loss + 30) / 60);
1599 } else if (loss > 0) {
1600 (void) printf(dgettext(TEXT_DOMAIN,
1601 "Approximately %lld seconds of data\n"
1602 "\tmust be discarded, irreversibly. "), loss);
1604 if (edata != 0 && edata != UINT64_MAX) {
1606 (void) printf(dgettext(TEXT_DOMAIN,
1607 "After rewind, at least\n"
1608 "\tone persistent user-data error will remain. "));
1610 (void) printf(dgettext(TEXT_DOMAIN,
1611 "After rewind, several\n"
1612 "\tpersistent user-data errors will remain. "));
1615 (void) printf(dgettext(TEXT_DOMAIN,
1616 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1617 reason >= 0 ? "clear" : "import", name);
1619 (void) printf(dgettext(TEXT_DOMAIN,
1620 "A scrub of the pool\n"
1621 "\tis strongly recommended after recovery.\n"));
1625 (void) printf(dgettext(TEXT_DOMAIN,
1626 "Destroy and re-create the pool from\n\ta backup source.\n"));
1630 * zpool_import() is a contracted interface. Should be kept the same
1633 * Applications should use zpool_import_props() to import a pool with
1634 * new properties value to be set.
1637 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1640 nvlist_t *props = NULL;
1643 if (altroot != NULL) {
1644 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1645 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1646 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1650 if (nvlist_add_string(props,
1651 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1652 nvlist_add_string(props,
1653 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1655 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1656 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1661 ret = zpool_import_props(hdl, config, newname, props,
1668 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1674 uint64_t is_log = 0;
1676 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1680 (void) printf("\t%*s%s%s\n", indent, "", name,
1681 is_log ? " [log]" : "");
1683 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1684 &child, &children) != 0)
1687 for (c = 0; c < children; c++) {
1688 vname = zpool_vdev_name(hdl, NULL, child[c], B_TRUE);
1689 print_vdev_tree(hdl, vname, child[c], indent + 2);
1695 zpool_print_unsup_feat(nvlist_t *config)
1697 nvlist_t *nvinfo, *unsup_feat;
1699 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1701 verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1704 for (nvpair_t *nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1705 nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1708 verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1709 verify(nvpair_value_string(nvp, &desc) == 0);
1711 if (strlen(desc) > 0)
1712 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1714 (void) printf("\t%s\n", nvpair_name(nvp));
1719 * Import the given pool using the known configuration and a list of
1720 * properties to be set. The configuration should have come from
1721 * zpool_find_import(). The 'newname' parameters control whether the pool
1722 * is imported with a different name.
1725 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1726 nvlist_t *props, int flags)
1728 zfs_cmd_t zc = { 0 };
1729 zpool_load_policy_t policy;
1730 nvlist_t *nv = NULL;
1731 nvlist_t *nvinfo = NULL;
1732 nvlist_t *missing = NULL;
1739 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1742 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1743 "cannot import pool '%s'"), origname);
1745 if (newname != NULL) {
1746 if (!zpool_name_valid(hdl, B_FALSE, newname))
1747 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1748 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1750 thename = (char *)newname;
1755 if (props != NULL) {
1757 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1759 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1762 if ((props = zpool_valid_proplist(hdl, origname,
1763 props, version, flags, errbuf)) == NULL)
1765 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1772 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1774 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1777 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1778 zcmd_free_nvlists(&zc);
1781 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1782 zcmd_free_nvlists(&zc);
1786 zc.zc_cookie = flags;
1787 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1789 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1790 zcmd_free_nvlists(&zc);
1797 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1799 zcmd_free_nvlists(&zc);
1801 zpool_get_load_policy(config, &policy);
1807 * Dry-run failed, but we print out what success
1808 * looks like if we found a best txg
1810 if (policy.zlp_rewind & ZPOOL_TRY_REWIND) {
1811 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1817 if (newname == NULL)
1818 (void) snprintf(desc, sizeof (desc),
1819 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1822 (void) snprintf(desc, sizeof (desc),
1823 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1828 if (nv != NULL && nvlist_lookup_nvlist(nv,
1829 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1830 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1831 (void) printf(dgettext(TEXT_DOMAIN, "This "
1832 "pool uses the following feature(s) not "
1833 "supported by this system:\n"));
1834 zpool_print_unsup_feat(nv);
1835 if (nvlist_exists(nvinfo,
1836 ZPOOL_CONFIG_CAN_RDONLY)) {
1837 (void) printf(dgettext(TEXT_DOMAIN,
1838 "All unsupported features are only "
1839 "required for writing to the pool."
1840 "\nThe pool can be imported using "
1841 "'-o readonly=on'.\n"));
1845 * Unsupported version.
1847 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1851 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1855 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1856 "one or more devices is read only"));
1857 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1861 if (nv && nvlist_lookup_nvlist(nv,
1862 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1863 nvlist_lookup_nvlist(nvinfo,
1864 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1865 (void) printf(dgettext(TEXT_DOMAIN,
1866 "The devices below are missing or "
1867 "corrupted, use '-m' to import the pool "
1869 print_vdev_tree(hdl, NULL, missing, 2);
1870 (void) printf("\n");
1872 (void) zpool_standard_error(hdl, error, desc);
1876 (void) zpool_standard_error(hdl, error, desc);
1879 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1880 "new name of at least one dataset is longer than "
1881 "the maximum allowable length"));
1882 (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
1885 (void) zpool_standard_error(hdl, error, desc);
1886 zpool_explain_recover(hdl,
1887 newname ? origname : thename, -error, nv);
1894 zpool_handle_t *zhp;
1897 * This should never fail, but play it safe anyway.
1899 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1901 else if (zhp != NULL)
1903 if (policy.zlp_rewind &
1904 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1905 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1906 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0), nv);
1919 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd)
1921 zfs_cmd_t zc = { 0 };
1924 libzfs_handle_t *hdl = zhp->zpool_hdl;
1926 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1927 zc.zc_cookie = func;
1930 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0)
1935 /* ECANCELED on a scrub means we resumed a paused scrub */
1936 if (err == ECANCELED && func == POOL_SCAN_SCRUB &&
1937 cmd == POOL_SCRUB_NORMAL)
1940 if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL)
1943 if (func == POOL_SCAN_SCRUB) {
1944 if (cmd == POOL_SCRUB_PAUSE) {
1945 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1946 "cannot pause scrubbing %s"), zc.zc_name);
1948 assert(cmd == POOL_SCRUB_NORMAL);
1949 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1950 "cannot scrub %s"), zc.zc_name);
1952 } else if (func == POOL_SCAN_NONE) {
1953 (void) snprintf(msg, sizeof (msg),
1954 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1957 assert(!"unexpected result");
1962 pool_scan_stat_t *ps = NULL;
1965 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1966 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1967 (void) nvlist_lookup_uint64_array(nvroot,
1968 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
1969 if (ps && ps->pss_func == POOL_SCAN_SCRUB) {
1970 if (cmd == POOL_SCRUB_PAUSE)
1971 return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg));
1973 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
1975 return (zfs_error(hdl, EZFS_RESILVERING, msg));
1977 } else if (err == ENOENT) {
1978 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
1980 return (zpool_standard_error(hdl, err, msg));
1985 xlate_init_err(int err)
1989 return (EZFS_NODEVICE);
1992 return (EZFS_BADDEV);
1994 return (EZFS_INITIALIZING);
1996 return (EZFS_NO_INITIALIZE);
2002 * Begin, suspend, or cancel the initialization (initializing of all free
2003 * blocks) for the given vdevs in the given pool.
2006 zpool_initialize(zpool_handle_t *zhp, pool_initialize_func_t cmd_type,
2010 libzfs_handle_t *hdl = zhp->zpool_hdl;
2014 /* translate vdev names to guids */
2015 nvlist_t *vdev_guids = fnvlist_alloc();
2016 nvlist_t *guids_to_paths = fnvlist_alloc();
2017 boolean_t spare, cache;
2021 for (elem = nvlist_next_nvpair(vds, NULL); elem != NULL;
2022 elem = nvlist_next_nvpair(vds, elem)) {
2023 char *vd_path = nvpair_name(elem);
2024 tgt = zpool_find_vdev(zhp, vd_path, &spare, &cache, NULL);
2026 if ((tgt == NULL) || cache || spare) {
2027 (void) snprintf(msg, sizeof (msg),
2028 dgettext(TEXT_DOMAIN, "cannot initialize '%s'"),
2030 int err = (tgt == NULL) ? EZFS_NODEVICE :
2031 (spare ? EZFS_ISSPARE : EZFS_ISL2CACHE);
2032 fnvlist_free(vdev_guids);
2033 fnvlist_free(guids_to_paths);
2034 return (zfs_error(hdl, err, msg));
2037 uint64_t guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
2038 fnvlist_add_uint64(vdev_guids, vd_path, guid);
2040 (void) snprintf(msg, sizeof (msg), "%llu", guid);
2041 fnvlist_add_string(guids_to_paths, msg, vd_path);
2044 int err = lzc_initialize(zhp->zpool_name, cmd_type, vdev_guids,
2046 fnvlist_free(vdev_guids);
2049 fnvlist_free(guids_to_paths);
2053 nvlist_t *vd_errlist = NULL;
2054 if (errlist != NULL) {
2055 vd_errlist = fnvlist_lookup_nvlist(errlist,
2056 ZPOOL_INITIALIZE_VDEVS);
2059 (void) snprintf(msg, sizeof (msg),
2060 dgettext(TEXT_DOMAIN, "operation failed"));
2062 for (elem = nvlist_next_nvpair(vd_errlist, NULL); elem != NULL;
2063 elem = nvlist_next_nvpair(vd_errlist, elem)) {
2064 int64_t vd_error = xlate_init_err(fnvpair_value_int64(elem));
2065 char *path = fnvlist_lookup_string(guids_to_paths,
2067 (void) zfs_error_fmt(hdl, vd_error, "cannot initialize '%s'",
2071 fnvlist_free(guids_to_paths);
2072 if (vd_errlist != NULL)
2075 return (zpool_standard_error(hdl, err, msg));
2080 * This provides a very minimal check whether a given string is likely a
2081 * c#t#d# style string. Users of this are expected to do their own
2082 * verification of the s# part.
2084 #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1]))
2087 * More elaborate version for ones which may start with "/dev/dsk/"
2091 ctd_check_path(char *str)
2094 * If it starts with a slash, check the last component.
2096 if (str && str[0] == '/') {
2097 char *tmp = strrchr(str, '/');
2100 * If it ends in "/old", check the second-to-last
2101 * component of the string instead.
2103 if (tmp != str && strcmp(tmp, "/old") == 0) {
2104 for (tmp--; *tmp != '/'; tmp--)
2109 return (CTD_CHECK(str));
2114 * Find a vdev that matches the search criteria specified. We use the
2115 * the nvpair name to determine how we should look for the device.
2116 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2117 * spare; but FALSE if its an INUSE spare.
2120 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
2121 boolean_t *l2cache, boolean_t *log)
2128 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
2130 /* Nothing to look for */
2131 if (search == NULL || pair == NULL)
2134 /* Obtain the key we will use to search */
2135 srchkey = nvpair_name(pair);
2137 switch (nvpair_type(pair)) {
2138 case DATA_TYPE_UINT64:
2139 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
2140 uint64_t srchval, theguid;
2142 verify(nvpair_value_uint64(pair, &srchval) == 0);
2143 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2145 if (theguid == srchval)
2150 case DATA_TYPE_STRING: {
2151 char *srchval, *val;
2153 verify(nvpair_value_string(pair, &srchval) == 0);
2154 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2158 * Search for the requested value. Special cases:
2160 * - ZPOOL_CONFIG_PATH for whole disk entries. To support
2161 * UEFI boot, these end in "s0" or "s0/old" or "s1" or
2162 * "s1/old". The "s0" or "s1" part is hidden from the user,
2163 * but included in the string, so this matches around it.
2164 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2166 * Otherwise, all other searches are simple string compares.
2169 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
2170 ctd_check_path(val)) {
2171 uint64_t wholedisk = 0;
2173 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2176 int slen = strlen(srchval);
2177 int vlen = strlen(val);
2179 if (slen != vlen - 2)
2183 * make_leaf_vdev() should only set
2184 * wholedisk for ZPOOL_CONFIG_PATHs which
2185 * will include "/dev/dsk/", giving plenty of
2186 * room for the indices used next.
2191 * strings identical except trailing "s0"
2193 if ((strcmp(&val[vlen - 2], "s0") == 0 ||
2194 strcmp(&val[vlen - 2], "s1") == 0) &&
2195 strncmp(srchval, val, slen) == 0)
2199 * strings identical except trailing "s0/old"
2201 if ((strcmp(&val[vlen - 6], "s0/old") == 0 ||
2202 strcmp(&val[vlen - 6], "s1/old") == 0) &&
2203 strcmp(&srchval[slen - 4], "/old") == 0 &&
2204 strncmp(srchval, val, slen - 4) == 0)
2209 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2211 if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2213 char *type, *idx, *end, *p;
2214 uint64_t id, vdev_id;
2217 * Determine our vdev type, keeping in mind
2218 * that the srchval is composed of a type and
2219 * vdev id pair (i.e. mirror-4).
2221 if ((type = strdup(srchval)) == NULL)
2224 if ((p = strrchr(type, '-')) == NULL) {
2232 * If the types don't match then keep looking.
2234 if (strncmp(val, type, strlen(val)) != 0) {
2239 verify(zpool_vdev_is_interior(type));
2240 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2244 vdev_id = strtoull(idx, &end, 10);
2251 * Now verify that we have the correct vdev id.
2260 if (strcmp(srchval, val) == 0)
2269 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2270 &child, &children) != 0)
2273 for (c = 0; c < children; c++) {
2274 if ((ret = vdev_to_nvlist_iter(child[c], search,
2275 avail_spare, l2cache, NULL)) != NULL) {
2277 * The 'is_log' value is only set for the toplevel
2278 * vdev, not the leaf vdevs. So we always lookup the
2279 * log device from the root of the vdev tree (where
2280 * 'log' is non-NULL).
2283 nvlist_lookup_uint64(child[c],
2284 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2292 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2293 &child, &children) == 0) {
2294 for (c = 0; c < children; c++) {
2295 if ((ret = vdev_to_nvlist_iter(child[c], search,
2296 avail_spare, l2cache, NULL)) != NULL) {
2297 *avail_spare = B_TRUE;
2303 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2304 &child, &children) == 0) {
2305 for (c = 0; c < children; c++) {
2306 if ((ret = vdev_to_nvlist_iter(child[c], search,
2307 avail_spare, l2cache, NULL)) != NULL) {
2318 * Given a physical path (minus the "/devices" prefix), find the
2322 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2323 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2325 nvlist_t *search, *nvroot, *ret;
2327 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2328 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2330 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2333 *avail_spare = B_FALSE;
2337 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2338 nvlist_free(search);
2344 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2347 zpool_vdev_is_interior(const char *name)
2349 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2350 strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 ||
2352 VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 ||
2353 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2359 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2360 boolean_t *l2cache, boolean_t *log)
2362 char buf[MAXPATHLEN];
2364 nvlist_t *nvroot, *search, *ret;
2367 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2369 guid = strtoull(path, &end, 10);
2370 if (guid != 0 && *end == '\0') {
2371 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2372 } else if (zpool_vdev_is_interior(path)) {
2373 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2374 } else if (path[0] != '/') {
2375 (void) snprintf(buf, sizeof (buf), "%s%s", _PATH_DEV, path);
2376 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
2378 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2381 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2384 *avail_spare = B_FALSE;
2388 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2389 nvlist_free(search);
2395 vdev_online(nvlist_t *nv)
2399 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2400 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2401 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2408 * Helper function for zpool_get_physpaths().
2411 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2412 size_t *bytes_written)
2414 size_t bytes_left, pos, rsz;
2418 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2420 return (EZFS_NODEVICE);
2422 pos = *bytes_written;
2423 bytes_left = physpath_size - pos;
2424 format = (pos == 0) ? "%s" : " %s";
2426 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2427 *bytes_written += rsz;
2429 if (rsz >= bytes_left) {
2430 /* if physpath was not copied properly, clear it */
2431 if (bytes_left != 0) {
2434 return (EZFS_NOSPC);
2440 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2441 size_t *rsz, boolean_t is_spare)
2446 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2447 return (EZFS_INVALCONFIG);
2449 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2451 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2452 * For a spare vdev, we only want to boot from the active
2457 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2460 return (EZFS_INVALCONFIG);
2463 if (vdev_online(nv)) {
2464 if ((ret = vdev_get_one_physpath(nv, physpath,
2465 phypath_size, rsz)) != 0)
2468 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2469 strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
2470 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2471 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2476 if (nvlist_lookup_nvlist_array(nv,
2477 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2478 return (EZFS_INVALCONFIG);
2480 for (i = 0; i < count; i++) {
2481 ret = vdev_get_physpaths(child[i], physpath,
2482 phypath_size, rsz, is_spare);
2483 if (ret == EZFS_NOSPC)
2488 return (EZFS_POOL_INVALARG);
2492 * Get phys_path for a root pool config.
2493 * Return 0 on success; non-zero on failure.
2496 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2499 nvlist_t *vdev_root;
2506 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2508 return (EZFS_INVALCONFIG);
2510 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2511 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2512 &child, &count) != 0)
2513 return (EZFS_INVALCONFIG);
2516 * root pool can only have a single top-level vdev.
2518 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2519 return (EZFS_POOL_INVALARG);
2521 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2524 /* No online devices */
2526 return (EZFS_NODEVICE);
2532 * Get phys_path for a root pool
2533 * Return 0 on success; non-zero on failure.
2536 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2538 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2543 * If the device has being dynamically expanded then we need to relabel
2544 * the disk to use the new unallocated space.
2547 zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2550 char path[MAXPATHLEN];
2553 int (*_efi_use_whole_disk)(int);
2555 if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2556 "efi_use_whole_disk")) == NULL)
2559 (void) snprintf(path, sizeof (path), "%s/%s", ZFS_RDISK_ROOT, name);
2561 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2562 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2563 "relabel '%s': unable to open device"), name);
2564 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2568 * It's possible that we might encounter an error if the device
2569 * does not have any unallocated space left. If so, we simply
2570 * ignore that error and continue on.
2572 error = _efi_use_whole_disk(fd);
2574 if (error && error != VT_ENOSPC) {
2575 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2576 "relabel '%s': unable to read disk capacity"), name);
2577 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2579 #endif /* illumos */
2584 * Bring the specified vdev online. The 'flags' parameter is a set of the
2585 * ZFS_ONLINE_* flags.
2588 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2589 vdev_state_t *newstate)
2591 zfs_cmd_t zc = { 0 };
2595 boolean_t avail_spare, l2cache, islog;
2596 libzfs_handle_t *hdl = zhp->zpool_hdl;
2598 if (flags & ZFS_ONLINE_EXPAND) {
2599 (void) snprintf(msg, sizeof (msg),
2600 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2602 (void) snprintf(msg, sizeof (msg),
2603 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2606 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2607 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2609 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2611 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2614 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2616 if ((flags & ZFS_ONLINE_EXPAND ||
2617 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) &&
2618 nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) {
2619 uint64_t wholedisk = 0;
2621 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2625 * XXX - L2ARC 1.0 devices can't support expansion.
2628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2629 "cannot expand cache devices"));
2630 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2634 pathname += strlen(ZFS_DISK_ROOT) + 1;
2635 (void) zpool_relabel_disk(hdl, pathname);
2639 zc.zc_cookie = VDEV_STATE_ONLINE;
2642 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2643 if (errno == EINVAL) {
2644 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2645 "from this pool into a new one. Use '%s' "
2646 "instead"), "zpool detach");
2647 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2649 return (zpool_standard_error(hdl, errno, msg));
2652 *newstate = zc.zc_cookie;
2657 * Take the specified vdev offline
2660 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2662 zfs_cmd_t zc = { 0 };
2665 boolean_t avail_spare, l2cache;
2666 libzfs_handle_t *hdl = zhp->zpool_hdl;
2668 (void) snprintf(msg, sizeof (msg),
2669 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2671 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2672 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2674 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2676 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2679 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2681 zc.zc_cookie = VDEV_STATE_OFFLINE;
2682 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2684 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2691 * There are no other replicas of this device.
2693 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2697 * The log device has unplayed logs
2699 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2702 return (zpool_standard_error(hdl, errno, msg));
2707 * Mark the given vdev faulted.
2710 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2712 zfs_cmd_t zc = { 0 };
2714 libzfs_handle_t *hdl = zhp->zpool_hdl;
2716 (void) snprintf(msg, sizeof (msg),
2717 dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2719 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2721 zc.zc_cookie = VDEV_STATE_FAULTED;
2724 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2731 * There are no other replicas of this device.
2733 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2736 return (zpool_standard_error(hdl, errno, msg));
2742 * Mark the given vdev degraded.
2745 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2747 zfs_cmd_t zc = { 0 };
2749 libzfs_handle_t *hdl = zhp->zpool_hdl;
2751 (void) snprintf(msg, sizeof (msg),
2752 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
2754 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2756 zc.zc_cookie = VDEV_STATE_DEGRADED;
2759 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2762 return (zpool_standard_error(hdl, errno, msg));
2766 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2770 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2776 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2778 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2781 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2782 children == 2 && child[which] == tgt)
2785 for (c = 0; c < children; c++)
2786 if (is_replacing_spare(child[c], tgt, which))
2794 * Attach new_disk (fully described by nvroot) to old_disk.
2795 * If 'replacing' is specified, the new disk will replace the old one.
2798 zpool_vdev_attach(zpool_handle_t *zhp,
2799 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2801 zfs_cmd_t zc = { 0 };
2805 boolean_t avail_spare, l2cache, islog;
2810 nvlist_t *config_root;
2811 libzfs_handle_t *hdl = zhp->zpool_hdl;
2812 boolean_t rootpool = zpool_is_bootable(zhp);
2815 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2816 "cannot replace %s with %s"), old_disk, new_disk);
2818 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2819 "cannot attach %s to %s"), new_disk, old_disk);
2821 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2822 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2824 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2827 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2830 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2832 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2833 zc.zc_cookie = replacing;
2835 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2836 &child, &children) != 0 || children != 1) {
2837 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2838 "new device must be a single disk"));
2839 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2842 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2843 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2845 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2849 * If the target is a hot spare that has been swapped in, we can only
2850 * replace it with another hot spare.
2853 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2854 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2855 NULL) == NULL || !avail_spare) &&
2856 is_replacing_spare(config_root, tgt, 1)) {
2857 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2858 "can only be replaced by another hot spare"));
2860 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2865 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2868 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2870 zcmd_free_nvlists(&zc);
2875 * XXX need a better way to prevent user from
2876 * booting up a half-baked vdev.
2878 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2879 "sure to wait until resilver is done "
2880 "before rebooting.\n"));
2881 (void) fprintf(stderr, "\n");
2882 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "If "
2883 "you boot from pool '%s', you may need to update\n"
2884 "boot code on newly attached disk '%s'.\n\n"
2885 "Assuming you use GPT partitioning and 'da0' is "
2886 "your new boot disk\n"
2887 "you may use the following command:\n\n"
2888 "\tgpart bootcode -b /boot/pmbr -p "
2889 "/boot/gptzfsboot -i 1 da0\n\n"),
2890 zhp->zpool_name, new_disk);
2898 * Can't attach to or replace this type of vdev.
2901 uint64_t version = zpool_get_prop_int(zhp,
2902 ZPOOL_PROP_VERSION, NULL);
2905 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2906 "cannot replace a log with a spare"));
2907 else if (version >= SPA_VERSION_MULTI_REPLACE)
2908 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2909 "already in replacing/spare config; wait "
2910 "for completion or use 'zpool detach'"));
2912 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2913 "cannot replace a replacing device"));
2915 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2916 "can only attach to mirrors and top-level "
2919 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2924 * The new device must be a single disk.
2926 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2927 "new device must be a single disk"));
2928 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2932 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy, "
2933 "or device removal is in progress"),
2935 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2940 * The new device is too small.
2942 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2943 "device is too small"));
2944 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2949 * The new device has a different alignment requirement.
2951 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2952 "devices have different sector alignment"));
2953 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2958 * The resulting top-level vdev spec won't fit in the label.
2960 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2964 (void) zpool_standard_error(hdl, errno, msg);
2971 * Detach the specified device.
2974 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2976 zfs_cmd_t zc = { 0 };
2979 boolean_t avail_spare, l2cache;
2980 libzfs_handle_t *hdl = zhp->zpool_hdl;
2982 (void) snprintf(msg, sizeof (msg),
2983 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2985 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2986 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2988 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2991 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2994 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2996 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2998 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
3005 * Can't detach from this type of vdev.
3007 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
3008 "applicable to mirror and replacing vdevs"));
3009 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
3014 * There are no other replicas of this device.
3016 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
3020 (void) zpool_standard_error(hdl, errno, msg);
3027 * Find a mirror vdev in the source nvlist.
3029 * The mchild array contains a list of disks in one of the top-level mirrors
3030 * of the source pool. The schild array contains a list of disks that the
3031 * user specified on the command line. We loop over the mchild array to
3032 * see if any entry in the schild array matches.
3034 * If a disk in the mchild array is found in the schild array, we return
3035 * the index of that entry. Otherwise we return -1.
3038 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
3039 nvlist_t **schild, uint_t schildren)
3043 for (mc = 0; mc < mchildren; mc++) {
3045 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3046 mchild[mc], B_FALSE);
3048 for (sc = 0; sc < schildren; sc++) {
3049 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
3050 schild[sc], B_FALSE);
3051 boolean_t result = (strcmp(mpath, spath) == 0);
3067 * Split a mirror pool. If newroot points to null, then a new nvlist
3068 * is generated and it is the responsibility of the caller to free it.
3071 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
3072 nvlist_t *props, splitflags_t flags)
3074 zfs_cmd_t zc = { 0 };
3076 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
3077 nvlist_t **varray = NULL, *zc_props = NULL;
3078 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
3079 libzfs_handle_t *hdl = zhp->zpool_hdl;
3081 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
3084 (void) snprintf(msg, sizeof (msg),
3085 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
3087 if (!zpool_name_valid(hdl, B_FALSE, newname))
3088 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
3090 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
3091 (void) fprintf(stderr, gettext("Internal error: unable to "
3092 "retrieve pool configuration\n"));
3096 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
3098 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
3101 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
3102 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
3103 props, vers, flags, msg)) == NULL)
3107 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3109 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3110 "Source pool is missing vdev tree"));
3111 nvlist_free(zc_props);
3115 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
3118 if (*newroot == NULL ||
3119 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
3120 &newchild, &newchildren) != 0)
3123 for (c = 0; c < children; c++) {
3124 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
3126 nvlist_t **mchild, *vdev;
3131 * Unlike cache & spares, slogs are stored in the
3132 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3134 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3136 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
3138 if (is_log || is_hole) {
3140 * Create a hole vdev and put it in the config.
3142 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
3144 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
3145 VDEV_TYPE_HOLE) != 0)
3147 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
3152 varray[vcount++] = vdev;
3156 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
3158 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
3159 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3160 "Source pool must be composed only of mirrors\n"));
3161 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3165 verify(nvlist_lookup_nvlist_array(child[c],
3166 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3168 /* find or add an entry for this top-level vdev */
3169 if (newchildren > 0 &&
3170 (entry = find_vdev_entry(zhp, mchild, mchildren,
3171 newchild, newchildren)) >= 0) {
3172 /* We found a disk that the user specified. */
3173 vdev = mchild[entry];
3176 /* User didn't specify a disk for this vdev. */
3177 vdev = mchild[mchildren - 1];
3180 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3184 /* did we find every disk the user specified? */
3185 if (found != newchildren) {
3186 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3187 "include at most one disk from each mirror"));
3188 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3192 /* Prepare the nvlist for populating. */
3193 if (*newroot == NULL) {
3194 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3197 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3198 VDEV_TYPE_ROOT) != 0)
3201 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3204 /* Add all the children we found */
3205 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3206 lastlog == 0 ? vcount : lastlog) != 0)
3210 * If we're just doing a dry run, exit now with success.
3213 memory_err = B_FALSE;
3218 /* now build up the config list & call the ioctl */
3219 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3222 if (nvlist_add_nvlist(newconfig,
3223 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3224 nvlist_add_string(newconfig,
3225 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3226 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3230 * The new pool is automatically part of the namespace unless we
3231 * explicitly export it.
3234 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3235 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3236 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3237 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3239 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3242 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3243 retval = zpool_standard_error(hdl, errno, msg);
3248 memory_err = B_FALSE;
3251 if (varray != NULL) {
3254 for (v = 0; v < vcount; v++)
3255 nvlist_free(varray[v]);
3258 zcmd_free_nvlists(&zc);
3259 nvlist_free(zc_props);
3260 nvlist_free(newconfig);
3262 nvlist_free(*newroot);
3270 return (no_memory(hdl));
3276 * Remove the given device.
3279 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3281 zfs_cmd_t zc = { 0 };
3284 boolean_t avail_spare, l2cache, islog;
3285 libzfs_handle_t *hdl = zhp->zpool_hdl;
3288 (void) snprintf(msg, sizeof (msg),
3289 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3291 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3292 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3294 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3296 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3297 if (islog && version < SPA_VERSION_HOLES) {
3298 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3299 "pool must be upgraded to support log removal"));
3300 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3303 if (!islog && !avail_spare && !l2cache && zpool_is_bootable(zhp)) {
3304 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3305 "root pool can not have removed devices, "
3306 "because GRUB does not understand them"));
3307 return (zfs_error(hdl, EINVAL, msg));
3310 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3312 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3318 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3319 "invalid config; all top-level vdevs must "
3320 "have the same sector size and not be raidz."));
3321 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3325 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3326 "Pool busy; removal may already be in progress"));
3327 (void) zfs_error(hdl, EZFS_BUSY, msg);
3331 (void) zpool_standard_error(hdl, errno, msg);
3337 zpool_vdev_remove_cancel(zpool_handle_t *zhp)
3339 zfs_cmd_t zc = { 0 };
3341 libzfs_handle_t *hdl = zhp->zpool_hdl;
3343 (void) snprintf(msg, sizeof (msg),
3344 dgettext(TEXT_DOMAIN, "cannot cancel removal"));
3346 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3349 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3352 return (zpool_standard_error(hdl, errno, msg));
3356 zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path,
3361 boolean_t avail_spare, l2cache, islog;
3362 libzfs_handle_t *hdl = zhp->zpool_hdl;
3364 (void) snprintf(msg, sizeof (msg),
3365 dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"),
3368 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3370 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3372 if (avail_spare || l2cache || islog) {
3377 if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) {
3378 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3379 "indirect size not available"));
3380 return (zfs_error(hdl, EINVAL, msg));
3386 * Clear the errors for the pool, or the particular device if specified.
3389 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3391 zfs_cmd_t zc = { 0 };
3394 zpool_load_policy_t policy;
3395 boolean_t avail_spare, l2cache;
3396 libzfs_handle_t *hdl = zhp->zpool_hdl;
3397 nvlist_t *nvi = NULL;
3401 (void) snprintf(msg, sizeof (msg),
3402 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3405 (void) snprintf(msg, sizeof (msg),
3406 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3409 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3411 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3412 &l2cache, NULL)) == NULL)
3413 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3416 * Don't allow error clearing for hot spares. Do allow
3417 * error clearing for l2cache devices.
3420 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3422 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3426 zpool_get_load_policy(rewindnvl, &policy);
3427 zc.zc_cookie = policy.zlp_rewind;
3429 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3432 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3435 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3437 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3438 zcmd_free_nvlists(&zc);
3443 if (!error || ((policy.zlp_rewind & ZPOOL_TRY_REWIND) &&
3444 errno != EPERM && errno != EACCES)) {
3445 if (policy.zlp_rewind &
3446 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3447 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3448 zpool_rewind_exclaim(hdl, zc.zc_name,
3449 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0),
3453 zcmd_free_nvlists(&zc);
3457 zcmd_free_nvlists(&zc);
3458 return (zpool_standard_error(hdl, errno, msg));
3462 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3465 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3467 zfs_cmd_t zc = { 0 };
3469 libzfs_handle_t *hdl = zhp->zpool_hdl;
3471 (void) snprintf(msg, sizeof (msg),
3472 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3475 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3477 zc.zc_cookie = ZPOOL_NO_REWIND;
3479 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3482 return (zpool_standard_error(hdl, errno, msg));
3486 * Change the GUID for a pool.
3489 zpool_reguid(zpool_handle_t *zhp)
3492 libzfs_handle_t *hdl = zhp->zpool_hdl;
3493 zfs_cmd_t zc = { 0 };
3495 (void) snprintf(msg, sizeof (msg),
3496 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3498 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3499 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3502 return (zpool_standard_error(hdl, errno, msg));
3509 zpool_reopen(zpool_handle_t *zhp)
3511 zfs_cmd_t zc = { 0 };
3513 libzfs_handle_t *hdl = zhp->zpool_hdl;
3515 (void) snprintf(msg, sizeof (msg),
3516 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
3519 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3520 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
3522 return (zpool_standard_error(hdl, errno, msg));
3526 * Convert from a devid string to a path.
3529 devid_to_path(char *devid_str)
3534 devid_nmlist_t *list = NULL;
3537 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3540 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3542 devid_str_free(minor);
3549 * In a case the strdup() fails, we will just return NULL below.
3551 path = strdup(list[0].devname);
3553 devid_free_nmlist(list);
3559 * Convert from a path to a devid string.
3562 path_to_devid(const char *path)
3569 if ((fd = open(path, O_RDONLY)) < 0)
3574 if (devid_get(fd, &devid) == 0) {
3575 if (devid_get_minor_name(fd, &minor) == 0)
3576 ret = devid_str_encode(devid, minor);
3578 devid_str_free(minor);
3590 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3591 * ignore any failure here, since a common case is for an unprivileged user to
3592 * type 'zpool status', and we'll display the correct information anyway.
3595 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3597 zfs_cmd_t zc = { 0 };
3599 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3600 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3601 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3604 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3608 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3609 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3610 * We also check if this is a whole disk, in which case we strip off the
3611 * trailing 's0' slice name.
3613 * This routine is also responsible for identifying when disks have been
3614 * reconfigured in a new location. The kernel will have opened the device by
3615 * devid, but the path will still refer to the old location. To catch this, we
3616 * first do a path -> devid translation (which is fast for the common case). If
3617 * the devid matches, we're done. If not, we do a reverse devid -> path
3618 * translation and issue the appropriate ioctl() to update the path of the vdev.
3619 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3623 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3634 have_stats = nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3635 (uint64_t **)&vs, &vsc) == 0;
3636 have_path = nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0;
3639 * If the device is not currently present, assume it will not
3640 * come back at the same device path. Display the device by GUID.
3642 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3643 have_path && have_stats && vs->vs_state <= VDEV_STATE_CANT_OPEN) {
3644 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3646 (void) snprintf(buf, sizeof (buf), "%llu",
3647 (u_longlong_t)value);
3649 } else if (have_path) {
3652 * If the device is dead (faulted, offline, etc) then don't
3653 * bother opening it. Otherwise we may be forcing the user to
3654 * open a misbehaving device, which can have undesirable
3657 if ((have_stats == 0 ||
3658 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3660 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3662 * Determine if the current path is correct.
3664 char *newdevid = path_to_devid(path);
3666 if (newdevid == NULL ||
3667 strcmp(devid, newdevid) != 0) {
3670 if ((newpath = devid_to_path(devid)) != NULL) {
3672 * Update the path appropriately.
3674 set_path(zhp, nv, newpath);
3675 if (nvlist_add_string(nv,
3676 ZPOOL_CONFIG_PATH, newpath) == 0)
3677 verify(nvlist_lookup_string(nv,
3685 devid_str_free(newdevid);
3689 if (strncmp(path, ZFS_DISK_ROOTD, strlen(ZFS_DISK_ROOTD)) == 0)
3690 path += strlen(ZFS_DISK_ROOTD);
3692 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3693 &value) == 0 && value) {
3694 int pathlen = strlen(path);
3695 char *tmp = zfs_strdup(hdl, path);
3698 * If it starts with c#, and ends with "s0" or "s1",
3699 * chop the slice off, or if it ends with "s0/old" or
3700 * "s1/old", remove the slice from the middle.
3702 if (CTD_CHECK(tmp)) {
3703 if (strcmp(&tmp[pathlen - 2], "s0") == 0 ||
3704 strcmp(&tmp[pathlen - 2], "s1") == 0) {
3705 tmp[pathlen - 2] = '\0';
3706 } else if (pathlen > 6 &&
3707 (strcmp(&tmp[pathlen - 6], "s0/old") == 0 ||
3708 strcmp(&tmp[pathlen - 6], "s1/old") == 0)) {
3709 (void) strcpy(&tmp[pathlen - 6],
3715 #else /* !illumos */
3716 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3717 path += sizeof(_PATH_DEV) - 1;
3718 #endif /* illumos */
3720 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3723 * If it's a raidz device, we need to stick in the parity level.
3725 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3726 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3728 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3729 (u_longlong_t)value);
3734 * We identify each top-level vdev by using a <type-id>
3735 * naming convention.
3740 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3742 (void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3748 return (zfs_strdup(hdl, path));
3752 zbookmark_mem_compare(const void *a, const void *b)
3754 return (memcmp(a, b, sizeof (zbookmark_phys_t)));
3758 * Retrieve the persistent error log, uniquify the members, and return to the
3762 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3764 zfs_cmd_t zc = { 0 };
3766 zbookmark_phys_t *zb = NULL;
3770 * Retrieve the raw error list from the kernel. If the number of errors
3771 * has increased, allocate more space and continue until we get the
3774 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3778 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3779 count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL)
3781 zc.zc_nvlist_dst_size = count;
3782 (void) strcpy(zc.zc_name, zhp->zpool_name);
3784 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3786 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3787 if (errno == ENOMEM) {
3790 count = zc.zc_nvlist_dst_size;
3791 dst = zfs_alloc(zhp->zpool_hdl, count *
3792 sizeof (zbookmark_phys_t));
3795 zc.zc_nvlist_dst = (uintptr_t)dst;
3805 * Sort the resulting bookmarks. This is a little confusing due to the
3806 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3807 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3808 * _not_ copied as part of the process. So we point the start of our
3809 * array appropriate and decrement the total number of elements.
3811 zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
3812 zc.zc_nvlist_dst_size;
3813 count -= zc.zc_nvlist_dst_size;
3815 qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
3817 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3820 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3822 for (i = 0; i < count; i++) {
3825 /* ignoring zb_blkid and zb_level for now */
3826 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3827 zb[i-1].zb_object == zb[i].zb_object)
3830 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3832 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3833 zb[i].zb_objset) != 0) {
3837 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3838 zb[i].zb_object) != 0) {
3842 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3849 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3853 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3854 return (no_memory(zhp->zpool_hdl));
3858 * Upgrade a ZFS pool to the latest on-disk version.
3861 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3863 zfs_cmd_t zc = { 0 };
3864 libzfs_handle_t *hdl = zhp->zpool_hdl;
3866 (void) strcpy(zc.zc_name, zhp->zpool_name);
3867 zc.zc_cookie = new_version;
3869 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3870 return (zpool_standard_error_fmt(hdl, errno,
3871 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3877 zfs_save_arguments(int argc, char **argv, char *string, int len)
3879 (void) strlcpy(string, basename(argv[0]), len);
3880 for (int i = 1; i < argc; i++) {
3881 (void) strlcat(string, " ", len);
3882 (void) strlcat(string, argv[i], len);
3887 zpool_log_history(libzfs_handle_t *hdl, const char *message)
3889 zfs_cmd_t zc = { 0 };
3893 args = fnvlist_alloc();
3894 fnvlist_add_string(args, "message", message);
3895 err = zcmd_write_src_nvlist(hdl, &zc, args);
3897 err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
3899 zcmd_free_nvlists(&zc);
3904 * Perform ioctl to get some command history of a pool.
3906 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3907 * logical offset of the history buffer to start reading from.
3909 * Upon return, 'off' is the next logical offset to read from and
3910 * 'len' is the actual amount of bytes read into 'buf'.
3913 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3915 zfs_cmd_t zc = { 0 };
3916 libzfs_handle_t *hdl = zhp->zpool_hdl;
3918 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3920 zc.zc_history = (uint64_t)(uintptr_t)buf;
3921 zc.zc_history_len = *len;
3922 zc.zc_history_offset = *off;
3924 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3927 return (zfs_error_fmt(hdl, EZFS_PERM,
3928 dgettext(TEXT_DOMAIN,
3929 "cannot show history for pool '%s'"),
3932 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3933 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3934 "'%s'"), zhp->zpool_name));
3936 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3937 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3938 "'%s', pool must be upgraded"), zhp->zpool_name));
3940 return (zpool_standard_error_fmt(hdl, errno,
3941 dgettext(TEXT_DOMAIN,
3942 "cannot get history for '%s'"), zhp->zpool_name));
3946 *len = zc.zc_history_len;
3947 *off = zc.zc_history_offset;
3953 * Process the buffer of nvlists, unpacking and storing each nvlist record
3954 * into 'records'. 'leftover' is set to the number of bytes that weren't
3955 * processed as there wasn't a complete record.
3958 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3959 nvlist_t ***records, uint_t *numrecords)
3965 while (bytes_read > sizeof (reclen)) {
3967 /* get length of packed record (stored as little endian) */
3968 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3969 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3971 if (bytes_read < sizeof (reclen) + reclen)
3975 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3977 bytes_read -= sizeof (reclen) + reclen;
3978 buf += sizeof (reclen) + reclen;
3980 /* add record to nvlist array */
3982 if (ISP2(*numrecords + 1)) {
3983 *records = realloc(*records,
3984 *numrecords * 2 * sizeof (nvlist_t *));
3986 (*records)[*numrecords - 1] = nv;
3989 *leftover = bytes_read;
3993 /* from spa_history.c: spa_history_create_obj() */
3994 #define HIS_BUF_LEN_DEF (128 << 10)
3995 #define HIS_BUF_LEN_MAX (1 << 30)
3998 * Retrieve the command history of a pool.
4001 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
4004 uint64_t buflen = HIS_BUF_LEN_DEF;
4006 nvlist_t **records = NULL;
4007 uint_t numrecords = 0;
4010 buf = malloc(buflen);
4014 uint64_t bytes_read = buflen;
4017 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
4020 /* if nothing else was read in, we're at EOF, just return */
4021 if (bytes_read == 0)
4024 if ((err = zpool_history_unpack(buf, bytes_read,
4025 &leftover, &records, &numrecords)) != 0)
4028 if (leftover == bytes_read) {
4030 * no progress made, because buffer is not big enough
4031 * to hold this record; resize and retry.
4036 if ((buflen >= HIS_BUF_LEN_MAX) ||
4037 ((buf = malloc(buflen)) == NULL)) {
4049 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
4050 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
4051 records, numrecords) == 0);
4053 for (i = 0; i < numrecords; i++)
4054 nvlist_free(records[i]);
4061 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4062 char *pathname, size_t len)
4064 zfs_cmd_t zc = { 0 };
4065 boolean_t mounted = B_FALSE;
4066 char *mntpnt = NULL;
4067 char dsname[ZFS_MAX_DATASET_NAME_LEN];
4070 /* special case for the MOS */
4071 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
4075 /* get the dataset's name */
4076 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4078 if (ioctl(zhp->zpool_hdl->libzfs_fd,
4079 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
4080 /* just write out a path of two object numbers */
4081 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
4085 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
4087 /* find out if the dataset is mounted */
4088 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
4090 /* get the corrupted object's path */
4091 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
4093 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
4096 (void) snprintf(pathname, len, "%s%s", mntpnt,
4099 (void) snprintf(pathname, len, "%s:%s",
4100 dsname, zc.zc_value);
4103 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
4110 * Read the EFI label from the config, if a label does not exist then
4111 * pass back the error to the caller. If the caller has passed a non-NULL
4112 * diskaddr argument then we set it to the starting address of the EFI
4113 * partition. If the caller has passed a non-NULL boolean argument, then
4114 * we set it to indicate if the disk does have efi system partition.
4117 read_efi_label(nvlist_t *config, diskaddr_t *sb, boolean_t *system)
4121 char diskname[MAXPATHLEN];
4122 boolean_t boot = B_FALSE;
4126 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
4129 (void) snprintf(diskname, sizeof (diskname), "%s%s", ZFS_RDISK_ROOT,
4130 strrchr(path, '/'));
4131 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
4132 struct dk_gpt *vtoc;
4134 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
4135 for (slice = 0; slice < vtoc->efi_nparts; slice++) {
4136 if (vtoc->efi_parts[slice].p_tag == V_SYSTEM)
4138 if (vtoc->efi_parts[slice].p_tag == V_USR)
4141 if (sb != NULL && vtoc->efi_parts[slice].p_tag == V_USR)
4142 *sb = vtoc->efi_parts[slice].p_start;
4153 * determine where a partition starts on a disk in the current
4157 find_start_block(nvlist_t *config)
4161 diskaddr_t sb = MAXOFFSET_T;
4164 if (nvlist_lookup_nvlist_array(config,
4165 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
4166 if (nvlist_lookup_uint64(config,
4167 ZPOOL_CONFIG_WHOLE_DISK,
4168 &wholedisk) != 0 || !wholedisk) {
4169 return (MAXOFFSET_T);
4171 if (read_efi_label(config, &sb, NULL) < 0)
4176 for (c = 0; c < children; c++) {
4177 sb = find_start_block(child[c]);
4178 if (sb != MAXOFFSET_T) {
4182 return (MAXOFFSET_T);
4184 #endif /* illumos */
4187 * Label an individual disk. The name provided is the short name,
4188 * stripped of any leading /dev path.
4191 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name,
4192 zpool_boot_label_t boot_type, uint64_t boot_size, int *slice)
4195 char path[MAXPATHLEN];
4196 struct dk_gpt *vtoc;
4198 size_t resv = EFI_MIN_RESV_SIZE;
4199 uint64_t slice_size;
4200 diskaddr_t start_block;
4203 /* prepare an error message just in case */
4204 (void) snprintf(errbuf, sizeof (errbuf),
4205 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
4210 verify(nvlist_lookup_nvlist(zhp->zpool_config,
4211 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4213 if (zhp->zpool_start_block == 0)
4214 start_block = find_start_block(nvroot);
4216 start_block = zhp->zpool_start_block;
4217 zhp->zpool_start_block = start_block;
4220 start_block = NEW_START_BLOCK;
4223 (void) snprintf(path, sizeof (path), "%s/%s%s", ZFS_RDISK_ROOT, name,
4226 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
4228 * This shouldn't happen. We've long since verified that this
4229 * is a valid device.
4232 dgettext(TEXT_DOMAIN, "unable to open device"));
4233 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
4236 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
4238 * The only way this can fail is if we run out of memory, or we
4239 * were unable to read the disk's capacity
4241 if (errno == ENOMEM)
4242 (void) no_memory(hdl);
4245 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4246 "unable to read disk capacity"), name);
4248 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
4252 * Why we use V_USR: V_BACKUP confuses users, and is considered
4253 * disposable by some EFI utilities (since EFI doesn't have a backup
4254 * slice). V_UNASSIGNED is supposed to be used only for zero size
4255 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4256 * etc. were all pretty specific. V_USR is as close to reality as we
4257 * can get, in the absence of V_OTHER.
4259 /* first fix the partition start block */
4260 if (start_block == MAXOFFSET_T)
4261 start_block = NEW_START_BLOCK;
4264 * EFI System partition is using slice 0.
4265 * ZFS is on slice 1 and slice 8 is reserved.
4266 * We assume the GPT partition table without system
4267 * partition has zfs p_start == NEW_START_BLOCK.
4268 * If start_block != NEW_START_BLOCK, it means we have
4269 * system partition. Correct solution would be to query/cache vtoc
4270 * from existing vdev member.
4272 if (boot_type == ZPOOL_CREATE_BOOT_LABEL) {
4273 if (boot_size % vtoc->efi_lbasize != 0) {
4274 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4275 "boot partition size must be a multiple of %d"),
4279 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4282 * System partition size checks.
4283 * Note the 1MB is quite arbitrary value, since we
4284 * are creating dedicated pool, it should be enough
4285 * to hold fat + efi bootloader. May need to be
4286 * adjusted if the bootloader size will grow.
4288 if (boot_size < 1024 * 1024) {
4290 zfs_nicenum(boot_size, buf, sizeof (buf));
4291 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4292 "Specified size %s for EFI System partition is too "
4293 "small, the minimum size is 1MB."), buf);
4296 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4298 /* 33MB is tested with mkfs -F pcfs */
4299 if (hdl->libzfs_printerr &&
4300 ((vtoc->efi_lbasize == 512 &&
4301 boot_size < 33 * 1024 * 1024) ||
4302 (vtoc->efi_lbasize == 4096 &&
4303 boot_size < 256 * 1024 * 1024))) {
4305 zfs_nicenum(boot_size, buf, sizeof (buf));
4306 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
4307 "Warning: EFI System partition size %s is "
4308 "not allowing to create FAT32 file\nsystem, which "
4309 "may result in unbootable system.\n"), buf);
4311 /* Adjust zfs partition start by size of system partition. */
4312 start_block += boot_size / vtoc->efi_lbasize;
4315 if (start_block == NEW_START_BLOCK) {
4317 * Use default layout.
4318 * ZFS is on slice 0 and slice 8 is reserved.
4320 slice_size = vtoc->efi_last_u_lba + 1;
4321 slice_size -= EFI_MIN_RESV_SIZE;
4322 slice_size -= start_block;
4326 vtoc->efi_parts[0].p_start = start_block;
4327 vtoc->efi_parts[0].p_size = slice_size;
4329 vtoc->efi_parts[0].p_tag = V_USR;
4330 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
4332 vtoc->efi_parts[8].p_start = slice_size + start_block;
4333 vtoc->efi_parts[8].p_size = resv;
4334 vtoc->efi_parts[8].p_tag = V_RESERVED;
4336 slice_size = start_block - NEW_START_BLOCK;
4337 vtoc->efi_parts[0].p_start = NEW_START_BLOCK;
4338 vtoc->efi_parts[0].p_size = slice_size;
4339 vtoc->efi_parts[0].p_tag = V_SYSTEM;
4340 (void) strcpy(vtoc->efi_parts[0].p_name, "loader");
4343 /* prepare slice 1 */
4344 slice_size = vtoc->efi_last_u_lba + 1 - slice_size;
4346 slice_size -= NEW_START_BLOCK;
4347 vtoc->efi_parts[1].p_start = start_block;
4348 vtoc->efi_parts[1].p_size = slice_size;
4349 vtoc->efi_parts[1].p_tag = V_USR;
4350 (void) strcpy(vtoc->efi_parts[1].p_name, "zfs");
4352 vtoc->efi_parts[8].p_start = slice_size + start_block;
4353 vtoc->efi_parts[8].p_size = resv;
4354 vtoc->efi_parts[8].p_tag = V_RESERVED;
4357 if (efi_write(fd, vtoc) != 0) {
4359 * Some block drivers (like pcata) may not support EFI
4360 * GPT labels. Print out a helpful error message dir-
4361 * ecting the user to manually label the disk and give
4367 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4368 "try using fdisk(1M) and then provide a specific slice"));
4369 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4374 #endif /* illumos */
4379 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
4385 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
4386 if (strcmp(type, VDEV_TYPE_FILE) == 0 ||
4387 strcmp(type, VDEV_TYPE_HOLE) == 0 ||
4388 strcmp(type, VDEV_TYPE_MISSING) == 0) {
4389 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4390 "vdev type '%s' is not supported"), type);
4391 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
4394 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
4395 &child, &children) == 0) {
4396 for (c = 0; c < children; c++) {
4397 if (!supported_dump_vdev_type(hdl, child[c], errbuf))
4405 * Check if this zvol is allowable for use as a dump device; zero if
4406 * it is, > 0 if it isn't, < 0 if it isn't a zvol.
4408 * Allowable storage configurations include mirrors, all raidz variants, and
4409 * pools with log, cache, and spare devices. Pools which are backed by files or
4410 * have missing/hole vdevs are not suitable.
4413 zvol_check_dump_config(char *arg)
4415 zpool_handle_t *zhp = NULL;
4416 nvlist_t *config, *nvroot;
4420 libzfs_handle_t *hdl;
4422 char poolname[ZFS_MAX_DATASET_NAME_LEN];
4423 int pathlen = strlen(ZVOL_FULL_DEV_DIR);
4426 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
4430 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4431 "dump is not supported on device '%s'"), arg);
4433 if ((hdl = libzfs_init()) == NULL)
4435 libzfs_print_on_error(hdl, B_TRUE);
4437 volname = arg + pathlen;
4439 /* check the configuration of the pool */
4440 if ((p = strchr(volname, '/')) == NULL) {
4441 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4442 "malformed dataset name"));
4443 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4445 } else if (p - volname >= ZFS_MAX_DATASET_NAME_LEN) {
4446 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4447 "dataset name is too long"));
4448 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
4451 (void) strncpy(poolname, volname, p - volname);
4452 poolname[p - volname] = '\0';
4455 if ((zhp = zpool_open(hdl, poolname)) == NULL) {
4456 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4457 "could not open pool '%s'"), poolname);
4458 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4461 config = zpool_get_config(zhp, NULL);
4462 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4464 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4465 "could not obtain vdev configuration for '%s'"), poolname);
4466 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4470 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4471 &top, &toplevels) == 0);
4473 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
4486 zpool_nextboot(libzfs_handle_t *hdl, uint64_t pool_guid, uint64_t dev_guid,
4487 const char *command)
4489 zfs_cmd_t zc = { 0 };
4495 args = fnvlist_alloc();
4496 fnvlist_add_uint64(args, ZPOOL_CONFIG_POOL_GUID, pool_guid);
4497 fnvlist_add_uint64(args, ZPOOL_CONFIG_GUID, dev_guid);
4498 fnvlist_add_string(args, "command", command);
4499 error = zcmd_write_src_nvlist(hdl, &zc, args);
4501 error = ioctl(hdl->libzfs_fd, ZFS_IOC_NEXTBOOT, &zc);
4502 zcmd_free_nvlists(&zc);