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));
1986 * This provides a very minimal check whether a given string is likely a
1987 * c#t#d# style string. Users of this are expected to do their own
1988 * verification of the s# part.
1990 #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1]))
1993 * More elaborate version for ones which may start with "/dev/dsk/"
1997 ctd_check_path(char *str)
2000 * If it starts with a slash, check the last component.
2002 if (str && str[0] == '/') {
2003 char *tmp = strrchr(str, '/');
2006 * If it ends in "/old", check the second-to-last
2007 * component of the string instead.
2009 if (tmp != str && strcmp(tmp, "/old") == 0) {
2010 for (tmp--; *tmp != '/'; tmp--)
2015 return (CTD_CHECK(str));
2020 * Find a vdev that matches the search criteria specified. We use the
2021 * the nvpair name to determine how we should look for the device.
2022 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2023 * spare; but FALSE if its an INUSE spare.
2026 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
2027 boolean_t *l2cache, boolean_t *log)
2034 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
2036 /* Nothing to look for */
2037 if (search == NULL || pair == NULL)
2040 /* Obtain the key we will use to search */
2041 srchkey = nvpair_name(pair);
2043 switch (nvpair_type(pair)) {
2044 case DATA_TYPE_UINT64:
2045 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
2046 uint64_t srchval, theguid;
2048 verify(nvpair_value_uint64(pair, &srchval) == 0);
2049 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2051 if (theguid == srchval)
2056 case DATA_TYPE_STRING: {
2057 char *srchval, *val;
2059 verify(nvpair_value_string(pair, &srchval) == 0);
2060 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2064 * Search for the requested value. Special cases:
2066 * - ZPOOL_CONFIG_PATH for whole disk entries. To support
2067 * UEFI boot, these end in "s0" or "s0/old" or "s1" or
2068 * "s1/old". The "s0" or "s1" part is hidden from the user,
2069 * but included in the string, so this matches around it.
2070 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2072 * Otherwise, all other searches are simple string compares.
2075 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0 &&
2076 ctd_check_path(val)) {
2077 uint64_t wholedisk = 0;
2079 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2082 int slen = strlen(srchval);
2083 int vlen = strlen(val);
2085 if (slen != vlen - 2)
2089 * make_leaf_vdev() should only set
2090 * wholedisk for ZPOOL_CONFIG_PATHs which
2091 * will include "/dev/dsk/", giving plenty of
2092 * room for the indices used next.
2097 * strings identical except trailing "s0"
2099 if ((strcmp(&val[vlen - 2], "s0") == 0 ||
2100 strcmp(&val[vlen - 2], "s1") == 0) &&
2101 strncmp(srchval, val, slen) == 0)
2105 * strings identical except trailing "s0/old"
2107 if ((strcmp(&val[vlen - 6], "s0/old") == 0 ||
2108 strcmp(&val[vlen - 6], "s1/old") == 0) &&
2109 strcmp(&srchval[slen - 4], "/old") == 0 &&
2110 strncmp(srchval, val, slen - 4) == 0)
2115 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2117 if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2119 char *type, *idx, *end, *p;
2120 uint64_t id, vdev_id;
2123 * Determine our vdev type, keeping in mind
2124 * that the srchval is composed of a type and
2125 * vdev id pair (i.e. mirror-4).
2127 if ((type = strdup(srchval)) == NULL)
2130 if ((p = strrchr(type, '-')) == NULL) {
2138 * If the types don't match then keep looking.
2140 if (strncmp(val, type, strlen(val)) != 0) {
2145 verify(zpool_vdev_is_interior(type));
2146 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2150 vdev_id = strtoull(idx, &end, 10);
2157 * Now verify that we have the correct vdev id.
2166 if (strcmp(srchval, val) == 0)
2175 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2176 &child, &children) != 0)
2179 for (c = 0; c < children; c++) {
2180 if ((ret = vdev_to_nvlist_iter(child[c], search,
2181 avail_spare, l2cache, NULL)) != NULL) {
2183 * The 'is_log' value is only set for the toplevel
2184 * vdev, not the leaf vdevs. So we always lookup the
2185 * log device from the root of the vdev tree (where
2186 * 'log' is non-NULL).
2189 nvlist_lookup_uint64(child[c],
2190 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2198 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2199 &child, &children) == 0) {
2200 for (c = 0; c < children; c++) {
2201 if ((ret = vdev_to_nvlist_iter(child[c], search,
2202 avail_spare, l2cache, NULL)) != NULL) {
2203 *avail_spare = B_TRUE;
2209 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2210 &child, &children) == 0) {
2211 for (c = 0; c < children; c++) {
2212 if ((ret = vdev_to_nvlist_iter(child[c], search,
2213 avail_spare, l2cache, NULL)) != NULL) {
2224 * Given a physical path (minus the "/devices" prefix), find the
2228 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2229 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2231 nvlist_t *search, *nvroot, *ret;
2233 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2234 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2236 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2239 *avail_spare = B_FALSE;
2243 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2244 nvlist_free(search);
2250 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2253 zpool_vdev_is_interior(const char *name)
2255 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2256 strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 ||
2258 VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 ||
2259 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2265 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2266 boolean_t *l2cache, boolean_t *log)
2268 char buf[MAXPATHLEN];
2270 nvlist_t *nvroot, *search, *ret;
2273 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2275 guid = strtoull(path, &end, 10);
2276 if (guid != 0 && *end == '\0') {
2277 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2278 } else if (zpool_vdev_is_interior(path)) {
2279 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2280 } else if (path[0] != '/') {
2281 (void) snprintf(buf, sizeof (buf), "%s%s", _PATH_DEV, path);
2282 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, buf) == 0);
2284 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2287 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2290 *avail_spare = B_FALSE;
2294 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2295 nvlist_free(search);
2301 vdev_online(nvlist_t *nv)
2305 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2306 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2307 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2314 * Helper function for zpool_get_physpaths().
2317 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2318 size_t *bytes_written)
2320 size_t bytes_left, pos, rsz;
2324 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2326 return (EZFS_NODEVICE);
2328 pos = *bytes_written;
2329 bytes_left = physpath_size - pos;
2330 format = (pos == 0) ? "%s" : " %s";
2332 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2333 *bytes_written += rsz;
2335 if (rsz >= bytes_left) {
2336 /* if physpath was not copied properly, clear it */
2337 if (bytes_left != 0) {
2340 return (EZFS_NOSPC);
2346 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2347 size_t *rsz, boolean_t is_spare)
2352 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2353 return (EZFS_INVALCONFIG);
2355 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2357 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2358 * For a spare vdev, we only want to boot from the active
2363 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2366 return (EZFS_INVALCONFIG);
2369 if (vdev_online(nv)) {
2370 if ((ret = vdev_get_one_physpath(nv, physpath,
2371 phypath_size, rsz)) != 0)
2374 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2375 strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
2376 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2377 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2382 if (nvlist_lookup_nvlist_array(nv,
2383 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2384 return (EZFS_INVALCONFIG);
2386 for (i = 0; i < count; i++) {
2387 ret = vdev_get_physpaths(child[i], physpath,
2388 phypath_size, rsz, is_spare);
2389 if (ret == EZFS_NOSPC)
2394 return (EZFS_POOL_INVALARG);
2398 * Get phys_path for a root pool config.
2399 * Return 0 on success; non-zero on failure.
2402 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2405 nvlist_t *vdev_root;
2412 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2414 return (EZFS_INVALCONFIG);
2416 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2417 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2418 &child, &count) != 0)
2419 return (EZFS_INVALCONFIG);
2422 * root pool can only have a single top-level vdev.
2424 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2425 return (EZFS_POOL_INVALARG);
2427 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2430 /* No online devices */
2432 return (EZFS_NODEVICE);
2438 * Get phys_path for a root pool
2439 * Return 0 on success; non-zero on failure.
2442 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2444 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2449 * If the device has being dynamically expanded then we need to relabel
2450 * the disk to use the new unallocated space.
2453 zpool_relabel_disk(libzfs_handle_t *hdl, const char *name)
2456 char path[MAXPATHLEN];
2459 int (*_efi_use_whole_disk)(int);
2461 if ((_efi_use_whole_disk = (int (*)(int))dlsym(RTLD_DEFAULT,
2462 "efi_use_whole_disk")) == NULL)
2465 (void) snprintf(path, sizeof (path), "%s/%s", ZFS_RDISK_ROOT, name);
2467 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
2468 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2469 "relabel '%s': unable to open device"), name);
2470 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
2474 * It's possible that we might encounter an error if the device
2475 * does not have any unallocated space left. If so, we simply
2476 * ignore that error and continue on.
2478 error = _efi_use_whole_disk(fd);
2480 if (error && error != VT_ENOSPC) {
2481 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2482 "relabel '%s': unable to read disk capacity"), name);
2483 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
2485 #endif /* illumos */
2490 * Bring the specified vdev online. The 'flags' parameter is a set of the
2491 * ZFS_ONLINE_* flags.
2494 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2495 vdev_state_t *newstate)
2497 zfs_cmd_t zc = { 0 };
2501 boolean_t avail_spare, l2cache, islog;
2502 libzfs_handle_t *hdl = zhp->zpool_hdl;
2504 if (flags & ZFS_ONLINE_EXPAND) {
2505 (void) snprintf(msg, sizeof (msg),
2506 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2508 (void) snprintf(msg, sizeof (msg),
2509 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2512 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2513 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2515 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2517 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2520 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2522 if ((flags & ZFS_ONLINE_EXPAND ||
2523 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) &&
2524 nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) {
2525 uint64_t wholedisk = 0;
2527 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2531 * XXX - L2ARC 1.0 devices can't support expansion.
2534 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2535 "cannot expand cache devices"));
2536 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2540 pathname += strlen(ZFS_DISK_ROOT) + 1;
2541 (void) zpool_relabel_disk(hdl, pathname);
2545 zc.zc_cookie = VDEV_STATE_ONLINE;
2548 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2549 if (errno == EINVAL) {
2550 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2551 "from this pool into a new one. Use '%s' "
2552 "instead"), "zpool detach");
2553 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2555 return (zpool_standard_error(hdl, errno, msg));
2558 *newstate = zc.zc_cookie;
2563 * Take the specified vdev offline
2566 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2568 zfs_cmd_t zc = { 0 };
2571 boolean_t avail_spare, l2cache;
2572 libzfs_handle_t *hdl = zhp->zpool_hdl;
2574 (void) snprintf(msg, sizeof (msg),
2575 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2577 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2578 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2580 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2582 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2585 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2587 zc.zc_cookie = VDEV_STATE_OFFLINE;
2588 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2590 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2597 * There are no other replicas of this device.
2599 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2603 * The log device has unplayed logs
2605 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2608 return (zpool_standard_error(hdl, errno, msg));
2613 * Mark the given vdev faulted.
2616 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2618 zfs_cmd_t zc = { 0 };
2620 libzfs_handle_t *hdl = zhp->zpool_hdl;
2622 (void) snprintf(msg, sizeof (msg),
2623 dgettext(TEXT_DOMAIN, "cannot fault %llu"), guid);
2625 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2627 zc.zc_cookie = VDEV_STATE_FAULTED;
2630 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2637 * There are no other replicas of this device.
2639 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2642 return (zpool_standard_error(hdl, errno, msg));
2648 * Mark the given vdev degraded.
2651 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2653 zfs_cmd_t zc = { 0 };
2655 libzfs_handle_t *hdl = zhp->zpool_hdl;
2657 (void) snprintf(msg, sizeof (msg),
2658 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), guid);
2660 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2662 zc.zc_cookie = VDEV_STATE_DEGRADED;
2665 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2668 return (zpool_standard_error(hdl, errno, msg));
2672 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2676 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2682 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2684 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2687 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2688 children == 2 && child[which] == tgt)
2691 for (c = 0; c < children; c++)
2692 if (is_replacing_spare(child[c], tgt, which))
2700 * Attach new_disk (fully described by nvroot) to old_disk.
2701 * If 'replacing' is specified, the new disk will replace the old one.
2704 zpool_vdev_attach(zpool_handle_t *zhp,
2705 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2707 zfs_cmd_t zc = { 0 };
2711 boolean_t avail_spare, l2cache, islog;
2716 nvlist_t *config_root;
2717 libzfs_handle_t *hdl = zhp->zpool_hdl;
2718 boolean_t rootpool = zpool_is_bootable(zhp);
2721 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2722 "cannot replace %s with %s"), old_disk, new_disk);
2724 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2725 "cannot attach %s to %s"), new_disk, old_disk);
2727 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2728 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2730 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2733 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2736 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2738 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2739 zc.zc_cookie = replacing;
2741 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2742 &child, &children) != 0 || children != 1) {
2743 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2744 "new device must be a single disk"));
2745 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2748 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2749 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2751 if ((newname = zpool_vdev_name(NULL, NULL, child[0], B_FALSE)) == NULL)
2755 * If the target is a hot spare that has been swapped in, we can only
2756 * replace it with another hot spare.
2759 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2760 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2761 NULL) == NULL || !avail_spare) &&
2762 is_replacing_spare(config_root, tgt, 1)) {
2763 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2764 "can only be replaced by another hot spare"));
2766 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2771 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2774 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2776 zcmd_free_nvlists(&zc);
2781 * XXX need a better way to prevent user from
2782 * booting up a half-baked vdev.
2784 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2785 "sure to wait until resilver is done "
2786 "before rebooting.\n"));
2787 (void) fprintf(stderr, "\n");
2788 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "If "
2789 "you boot from pool '%s', you may need to update\n"
2790 "boot code on newly attached disk '%s'.\n\n"
2791 "Assuming you use GPT partitioning and 'da0' is "
2792 "your new boot disk\n"
2793 "you may use the following command:\n\n"
2794 "\tgpart bootcode -b /boot/pmbr -p "
2795 "/boot/gptzfsboot -i 1 da0\n\n"),
2796 zhp->zpool_name, new_disk);
2804 * Can't attach to or replace this type of vdev.
2807 uint64_t version = zpool_get_prop_int(zhp,
2808 ZPOOL_PROP_VERSION, NULL);
2811 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2812 "cannot replace a log with a spare"));
2813 else if (version >= SPA_VERSION_MULTI_REPLACE)
2814 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2815 "already in replacing/spare config; wait "
2816 "for completion or use 'zpool detach'"));
2818 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2819 "cannot replace a replacing device"));
2821 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2822 "can only attach to mirrors and top-level "
2825 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2830 * The new device must be a single disk.
2832 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2833 "new device must be a single disk"));
2834 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2838 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy, "
2839 "or pool has removing/removed vdevs"),
2841 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2846 * The new device is too small.
2848 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2849 "device is too small"));
2850 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2855 * The new device has a different alignment requirement.
2857 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2858 "devices have different sector alignment"));
2859 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2864 * The resulting top-level vdev spec won't fit in the label.
2866 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2870 (void) zpool_standard_error(hdl, errno, msg);
2877 * Detach the specified device.
2880 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2882 zfs_cmd_t zc = { 0 };
2885 boolean_t avail_spare, l2cache;
2886 libzfs_handle_t *hdl = zhp->zpool_hdl;
2888 (void) snprintf(msg, sizeof (msg),
2889 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2891 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2892 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2894 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2897 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2900 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2902 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2904 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2911 * Can't detach from this type of vdev.
2913 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2914 "applicable to mirror and replacing vdevs"));
2915 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2920 * There are no other replicas of this device.
2922 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2926 (void) zpool_standard_error(hdl, errno, msg);
2933 * Find a mirror vdev in the source nvlist.
2935 * The mchild array contains a list of disks in one of the top-level mirrors
2936 * of the source pool. The schild array contains a list of disks that the
2937 * user specified on the command line. We loop over the mchild array to
2938 * see if any entry in the schild array matches.
2940 * If a disk in the mchild array is found in the schild array, we return
2941 * the index of that entry. Otherwise we return -1.
2944 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2945 nvlist_t **schild, uint_t schildren)
2949 for (mc = 0; mc < mchildren; mc++) {
2951 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2952 mchild[mc], B_FALSE);
2954 for (sc = 0; sc < schildren; sc++) {
2955 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2956 schild[sc], B_FALSE);
2957 boolean_t result = (strcmp(mpath, spath) == 0);
2973 * Split a mirror pool. If newroot points to null, then a new nvlist
2974 * is generated and it is the responsibility of the caller to free it.
2977 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2978 nvlist_t *props, splitflags_t flags)
2980 zfs_cmd_t zc = { 0 };
2982 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2983 nvlist_t **varray = NULL, *zc_props = NULL;
2984 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2985 libzfs_handle_t *hdl = zhp->zpool_hdl;
2987 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2990 (void) snprintf(msg, sizeof (msg),
2991 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2993 if (!zpool_name_valid(hdl, B_FALSE, newname))
2994 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2996 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2997 (void) fprintf(stderr, gettext("Internal error: unable to "
2998 "retrieve pool configuration\n"));
3002 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
3004 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
3007 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
3008 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
3009 props, vers, flags, msg)) == NULL)
3013 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3015 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3016 "Source pool is missing vdev tree"));
3017 nvlist_free(zc_props);
3021 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
3024 if (*newroot == NULL ||
3025 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
3026 &newchild, &newchildren) != 0)
3029 for (c = 0; c < children; c++) {
3030 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
3032 nvlist_t **mchild, *vdev;
3037 * Unlike cache & spares, slogs are stored in the
3038 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3040 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3042 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
3044 if (is_log || is_hole) {
3046 * Create a hole vdev and put it in the config.
3048 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
3050 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
3051 VDEV_TYPE_HOLE) != 0)
3053 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
3058 varray[vcount++] = vdev;
3062 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
3064 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
3065 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3066 "Source pool must be composed only of mirrors\n"));
3067 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3071 verify(nvlist_lookup_nvlist_array(child[c],
3072 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3074 /* find or add an entry for this top-level vdev */
3075 if (newchildren > 0 &&
3076 (entry = find_vdev_entry(zhp, mchild, mchildren,
3077 newchild, newchildren)) >= 0) {
3078 /* We found a disk that the user specified. */
3079 vdev = mchild[entry];
3082 /* User didn't specify a disk for this vdev. */
3083 vdev = mchild[mchildren - 1];
3086 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3090 /* did we find every disk the user specified? */
3091 if (found != newchildren) {
3092 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3093 "include at most one disk from each mirror"));
3094 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3098 /* Prepare the nvlist for populating. */
3099 if (*newroot == NULL) {
3100 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3103 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3104 VDEV_TYPE_ROOT) != 0)
3107 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3110 /* Add all the children we found */
3111 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3112 lastlog == 0 ? vcount : lastlog) != 0)
3116 * If we're just doing a dry run, exit now with success.
3119 memory_err = B_FALSE;
3124 /* now build up the config list & call the ioctl */
3125 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3128 if (nvlist_add_nvlist(newconfig,
3129 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3130 nvlist_add_string(newconfig,
3131 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3132 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3136 * The new pool is automatically part of the namespace unless we
3137 * explicitly export it.
3140 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3141 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3142 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3143 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3145 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3148 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3149 retval = zpool_standard_error(hdl, errno, msg);
3154 memory_err = B_FALSE;
3157 if (varray != NULL) {
3160 for (v = 0; v < vcount; v++)
3161 nvlist_free(varray[v]);
3164 zcmd_free_nvlists(&zc);
3165 nvlist_free(zc_props);
3166 nvlist_free(newconfig);
3168 nvlist_free(*newroot);
3176 return (no_memory(hdl));
3182 * Remove the given device.
3185 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3187 zfs_cmd_t zc = { 0 };
3190 boolean_t avail_spare, l2cache, islog;
3191 libzfs_handle_t *hdl = zhp->zpool_hdl;
3194 (void) snprintf(msg, sizeof (msg),
3195 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3197 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3198 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3200 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3202 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3203 if (islog && version < SPA_VERSION_HOLES) {
3204 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3205 "pool must be upgraded to support log removal"));
3206 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3209 if (!islog && !avail_spare && !l2cache && zpool_is_bootable(zhp)) {
3210 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3211 "root pool can not have removed devices, "
3212 "because GRUB does not understand them"));
3213 return (zfs_error(hdl, EINVAL, msg));
3216 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3218 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3224 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3225 "invalid config; all top-level vdevs must "
3226 "have the same sector size and not be raidz."));
3227 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3231 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3232 "Pool busy; removal may already be in progress"));
3233 (void) zfs_error(hdl, EZFS_BUSY, msg);
3237 (void) zpool_standard_error(hdl, errno, msg);
3243 zpool_vdev_remove_cancel(zpool_handle_t *zhp)
3245 zfs_cmd_t zc = { 0 };
3247 libzfs_handle_t *hdl = zhp->zpool_hdl;
3249 (void) snprintf(msg, sizeof (msg),
3250 dgettext(TEXT_DOMAIN, "cannot cancel removal"));
3252 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3255 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3258 return (zpool_standard_error(hdl, errno, msg));
3262 zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path,
3267 boolean_t avail_spare, l2cache, islog;
3268 libzfs_handle_t *hdl = zhp->zpool_hdl;
3270 (void) snprintf(msg, sizeof (msg),
3271 dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"),
3274 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3276 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3278 if (avail_spare || l2cache || islog) {
3283 if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) {
3284 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3285 "indirect size not available"));
3286 return (zfs_error(hdl, EINVAL, msg));
3292 * Clear the errors for the pool, or the particular device if specified.
3295 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3297 zfs_cmd_t zc = { 0 };
3300 zpool_load_policy_t policy;
3301 boolean_t avail_spare, l2cache;
3302 libzfs_handle_t *hdl = zhp->zpool_hdl;
3303 nvlist_t *nvi = NULL;
3307 (void) snprintf(msg, sizeof (msg),
3308 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3311 (void) snprintf(msg, sizeof (msg),
3312 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3315 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3317 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3318 &l2cache, NULL)) == NULL)
3319 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3322 * Don't allow error clearing for hot spares. Do allow
3323 * error clearing for l2cache devices.
3326 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3328 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3332 zpool_get_load_policy(rewindnvl, &policy);
3333 zc.zc_cookie = policy.zlp_rewind;
3335 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3338 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3341 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3343 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3344 zcmd_free_nvlists(&zc);
3349 if (!error || ((policy.zlp_rewind & ZPOOL_TRY_REWIND) &&
3350 errno != EPERM && errno != EACCES)) {
3351 if (policy.zlp_rewind &
3352 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3353 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3354 zpool_rewind_exclaim(hdl, zc.zc_name,
3355 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0),
3359 zcmd_free_nvlists(&zc);
3363 zcmd_free_nvlists(&zc);
3364 return (zpool_standard_error(hdl, errno, msg));
3368 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3371 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3373 zfs_cmd_t zc = { 0 };
3375 libzfs_handle_t *hdl = zhp->zpool_hdl;
3377 (void) snprintf(msg, sizeof (msg),
3378 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3381 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3383 zc.zc_cookie = ZPOOL_NO_REWIND;
3385 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3388 return (zpool_standard_error(hdl, errno, msg));
3392 * Change the GUID for a pool.
3395 zpool_reguid(zpool_handle_t *zhp)
3398 libzfs_handle_t *hdl = zhp->zpool_hdl;
3399 zfs_cmd_t zc = { 0 };
3401 (void) snprintf(msg, sizeof (msg),
3402 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3404 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3405 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3408 return (zpool_standard_error(hdl, errno, msg));
3415 zpool_reopen(zpool_handle_t *zhp)
3417 zfs_cmd_t zc = { 0 };
3419 libzfs_handle_t *hdl = zhp->zpool_hdl;
3421 (void) snprintf(msg, sizeof (msg),
3422 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
3425 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3426 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
3428 return (zpool_standard_error(hdl, errno, msg));
3432 * Convert from a devid string to a path.
3435 devid_to_path(char *devid_str)
3440 devid_nmlist_t *list = NULL;
3443 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3446 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3448 devid_str_free(minor);
3455 * In a case the strdup() fails, we will just return NULL below.
3457 path = strdup(list[0].devname);
3459 devid_free_nmlist(list);
3465 * Convert from a path to a devid string.
3468 path_to_devid(const char *path)
3475 if ((fd = open(path, O_RDONLY)) < 0)
3480 if (devid_get(fd, &devid) == 0) {
3481 if (devid_get_minor_name(fd, &minor) == 0)
3482 ret = devid_str_encode(devid, minor);
3484 devid_str_free(minor);
3496 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3497 * ignore any failure here, since a common case is for an unprivileged user to
3498 * type 'zpool status', and we'll display the correct information anyway.
3501 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3503 zfs_cmd_t zc = { 0 };
3505 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3506 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3507 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3510 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3514 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3515 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3516 * We also check if this is a whole disk, in which case we strip off the
3517 * trailing 's0' slice name.
3519 * This routine is also responsible for identifying when disks have been
3520 * reconfigured in a new location. The kernel will have opened the device by
3521 * devid, but the path will still refer to the old location. To catch this, we
3522 * first do a path -> devid translation (which is fast for the common case). If
3523 * the devid matches, we're done. If not, we do a reverse devid -> path
3524 * translation and issue the appropriate ioctl() to update the path of the vdev.
3525 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3529 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3540 have_stats = nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3541 (uint64_t **)&vs, &vsc) == 0;
3542 have_path = nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0;
3545 * If the device is not currently present, assume it will not
3546 * come back at the same device path. Display the device by GUID.
3548 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3549 have_path && have_stats && vs->vs_state <= VDEV_STATE_CANT_OPEN) {
3550 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3552 (void) snprintf(buf, sizeof (buf), "%llu",
3553 (u_longlong_t)value);
3555 } else if (have_path) {
3558 * If the device is dead (faulted, offline, etc) then don't
3559 * bother opening it. Otherwise we may be forcing the user to
3560 * open a misbehaving device, which can have undesirable
3563 if ((have_stats == 0 ||
3564 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3566 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3568 * Determine if the current path is correct.
3570 char *newdevid = path_to_devid(path);
3572 if (newdevid == NULL ||
3573 strcmp(devid, newdevid) != 0) {
3576 if ((newpath = devid_to_path(devid)) != NULL) {
3578 * Update the path appropriately.
3580 set_path(zhp, nv, newpath);
3581 if (nvlist_add_string(nv,
3582 ZPOOL_CONFIG_PATH, newpath) == 0)
3583 verify(nvlist_lookup_string(nv,
3591 devid_str_free(newdevid);
3595 if (strncmp(path, ZFS_DISK_ROOTD, strlen(ZFS_DISK_ROOTD)) == 0)
3596 path += strlen(ZFS_DISK_ROOTD);
3598 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
3599 &value) == 0 && value) {
3600 int pathlen = strlen(path);
3601 char *tmp = zfs_strdup(hdl, path);
3604 * If it starts with c#, and ends with "s0" or "s1",
3605 * chop the slice off, or if it ends with "s0/old" or
3606 * "s1/old", remove the slice from the middle.
3608 if (CTD_CHECK(tmp)) {
3609 if (strcmp(&tmp[pathlen - 2], "s0") == 0 ||
3610 strcmp(&tmp[pathlen - 2], "s1") == 0) {
3611 tmp[pathlen - 2] = '\0';
3612 } else if (pathlen > 6 &&
3613 (strcmp(&tmp[pathlen - 6], "s0/old") == 0 ||
3614 strcmp(&tmp[pathlen - 6], "s1/old") == 0)) {
3615 (void) strcpy(&tmp[pathlen - 6],
3621 #else /* !illumos */
3622 if (strncmp(path, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0)
3623 path += sizeof(_PATH_DEV) - 1;
3624 #endif /* illumos */
3626 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3629 * If it's a raidz device, we need to stick in the parity level.
3631 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3632 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3634 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3635 (u_longlong_t)value);
3640 * We identify each top-level vdev by using a <type-id>
3641 * naming convention.
3646 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3648 (void) snprintf(buf, sizeof (buf), "%s-%llu", path,
3654 return (zfs_strdup(hdl, path));
3658 zbookmark_mem_compare(const void *a, const void *b)
3660 return (memcmp(a, b, sizeof (zbookmark_phys_t)));
3664 * Retrieve the persistent error log, uniquify the members, and return to the
3668 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3670 zfs_cmd_t zc = { 0 };
3672 zbookmark_phys_t *zb = NULL;
3676 * Retrieve the raw error list from the kernel. If the number of errors
3677 * has increased, allocate more space and continue until we get the
3680 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3684 if ((zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3685 count * sizeof (zbookmark_phys_t))) == (uintptr_t)NULL)
3687 zc.zc_nvlist_dst_size = count;
3688 (void) strcpy(zc.zc_name, zhp->zpool_name);
3690 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3692 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3693 if (errno == ENOMEM) {
3696 count = zc.zc_nvlist_dst_size;
3697 dst = zfs_alloc(zhp->zpool_hdl, count *
3698 sizeof (zbookmark_phys_t));
3701 zc.zc_nvlist_dst = (uintptr_t)dst;
3711 * Sort the resulting bookmarks. This is a little confusing due to the
3712 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3713 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3714 * _not_ copied as part of the process. So we point the start of our
3715 * array appropriate and decrement the total number of elements.
3717 zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
3718 zc.zc_nvlist_dst_size;
3719 count -= zc.zc_nvlist_dst_size;
3721 qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
3723 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3726 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3728 for (i = 0; i < count; i++) {
3731 /* ignoring zb_blkid and zb_level for now */
3732 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3733 zb[i-1].zb_object == zb[i].zb_object)
3736 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3738 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3739 zb[i].zb_objset) != 0) {
3743 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3744 zb[i].zb_object) != 0) {
3748 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3755 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3759 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3760 return (no_memory(zhp->zpool_hdl));
3764 * Upgrade a ZFS pool to the latest on-disk version.
3767 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3769 zfs_cmd_t zc = { 0 };
3770 libzfs_handle_t *hdl = zhp->zpool_hdl;
3772 (void) strcpy(zc.zc_name, zhp->zpool_name);
3773 zc.zc_cookie = new_version;
3775 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3776 return (zpool_standard_error_fmt(hdl, errno,
3777 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3783 zfs_save_arguments(int argc, char **argv, char *string, int len)
3785 (void) strlcpy(string, basename(argv[0]), len);
3786 for (int i = 1; i < argc; i++) {
3787 (void) strlcat(string, " ", len);
3788 (void) strlcat(string, argv[i], len);
3793 zpool_log_history(libzfs_handle_t *hdl, const char *message)
3795 zfs_cmd_t zc = { 0 };
3799 args = fnvlist_alloc();
3800 fnvlist_add_string(args, "message", message);
3801 err = zcmd_write_src_nvlist(hdl, &zc, args);
3803 err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
3805 zcmd_free_nvlists(&zc);
3810 * Perform ioctl to get some command history of a pool.
3812 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3813 * logical offset of the history buffer to start reading from.
3815 * Upon return, 'off' is the next logical offset to read from and
3816 * 'len' is the actual amount of bytes read into 'buf'.
3819 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3821 zfs_cmd_t zc = { 0 };
3822 libzfs_handle_t *hdl = zhp->zpool_hdl;
3824 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3826 zc.zc_history = (uint64_t)(uintptr_t)buf;
3827 zc.zc_history_len = *len;
3828 zc.zc_history_offset = *off;
3830 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3833 return (zfs_error_fmt(hdl, EZFS_PERM,
3834 dgettext(TEXT_DOMAIN,
3835 "cannot show history for pool '%s'"),
3838 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3839 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3840 "'%s'"), zhp->zpool_name));
3842 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3843 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3844 "'%s', pool must be upgraded"), zhp->zpool_name));
3846 return (zpool_standard_error_fmt(hdl, errno,
3847 dgettext(TEXT_DOMAIN,
3848 "cannot get history for '%s'"), zhp->zpool_name));
3852 *len = zc.zc_history_len;
3853 *off = zc.zc_history_offset;
3859 * Process the buffer of nvlists, unpacking and storing each nvlist record
3860 * into 'records'. 'leftover' is set to the number of bytes that weren't
3861 * processed as there wasn't a complete record.
3864 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3865 nvlist_t ***records, uint_t *numrecords)
3871 while (bytes_read > sizeof (reclen)) {
3873 /* get length of packed record (stored as little endian) */
3874 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3875 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3877 if (bytes_read < sizeof (reclen) + reclen)
3881 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3883 bytes_read -= sizeof (reclen) + reclen;
3884 buf += sizeof (reclen) + reclen;
3886 /* add record to nvlist array */
3888 if (ISP2(*numrecords + 1)) {
3889 *records = realloc(*records,
3890 *numrecords * 2 * sizeof (nvlist_t *));
3892 (*records)[*numrecords - 1] = nv;
3895 *leftover = bytes_read;
3899 /* from spa_history.c: spa_history_create_obj() */
3900 #define HIS_BUF_LEN_DEF (128 << 10)
3901 #define HIS_BUF_LEN_MAX (1 << 30)
3904 * Retrieve the command history of a pool.
3907 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3910 uint64_t buflen = HIS_BUF_LEN_DEF;
3912 nvlist_t **records = NULL;
3913 uint_t numrecords = 0;
3916 buf = malloc(buflen);
3920 uint64_t bytes_read = buflen;
3923 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3926 /* if nothing else was read in, we're at EOF, just return */
3927 if (bytes_read == 0)
3930 if ((err = zpool_history_unpack(buf, bytes_read,
3931 &leftover, &records, &numrecords)) != 0)
3934 if (leftover == bytes_read) {
3936 * no progress made, because buffer is not big enough
3937 * to hold this record; resize and retry.
3942 if ((buflen >= HIS_BUF_LEN_MAX) ||
3943 ((buf = malloc(buflen)) == NULL)) {
3955 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3956 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3957 records, numrecords) == 0);
3959 for (i = 0; i < numrecords; i++)
3960 nvlist_free(records[i]);
3967 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
3968 char *pathname, size_t len)
3970 zfs_cmd_t zc = { 0 };
3971 boolean_t mounted = B_FALSE;
3972 char *mntpnt = NULL;
3973 char dsname[ZFS_MAX_DATASET_NAME_LEN];
3976 /* special case for the MOS */
3977 (void) snprintf(pathname, len, "<metadata>:<0x%llx>", obj);
3981 /* get the dataset's name */
3982 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3984 if (ioctl(zhp->zpool_hdl->libzfs_fd,
3985 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
3986 /* just write out a path of two object numbers */
3987 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
3991 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
3993 /* find out if the dataset is mounted */
3994 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
3996 /* get the corrupted object's path */
3997 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
3999 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
4002 (void) snprintf(pathname, len, "%s%s", mntpnt,
4005 (void) snprintf(pathname, len, "%s:%s",
4006 dsname, zc.zc_value);
4009 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname, obj);
4016 * Read the EFI label from the config, if a label does not exist then
4017 * pass back the error to the caller. If the caller has passed a non-NULL
4018 * diskaddr argument then we set it to the starting address of the EFI
4019 * partition. If the caller has passed a non-NULL boolean argument, then
4020 * we set it to indicate if the disk does have efi system partition.
4023 read_efi_label(nvlist_t *config, diskaddr_t *sb, boolean_t *system)
4027 char diskname[MAXPATHLEN];
4028 boolean_t boot = B_FALSE;
4032 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
4035 (void) snprintf(diskname, sizeof (diskname), "%s%s", ZFS_RDISK_ROOT,
4036 strrchr(path, '/'));
4037 if ((fd = open(diskname, O_RDONLY|O_NDELAY)) >= 0) {
4038 struct dk_gpt *vtoc;
4040 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
4041 for (slice = 0; slice < vtoc->efi_nparts; slice++) {
4042 if (vtoc->efi_parts[slice].p_tag == V_SYSTEM)
4044 if (vtoc->efi_parts[slice].p_tag == V_USR)
4047 if (sb != NULL && vtoc->efi_parts[slice].p_tag == V_USR)
4048 *sb = vtoc->efi_parts[slice].p_start;
4059 * determine where a partition starts on a disk in the current
4063 find_start_block(nvlist_t *config)
4067 diskaddr_t sb = MAXOFFSET_T;
4070 if (nvlist_lookup_nvlist_array(config,
4071 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
4072 if (nvlist_lookup_uint64(config,
4073 ZPOOL_CONFIG_WHOLE_DISK,
4074 &wholedisk) != 0 || !wholedisk) {
4075 return (MAXOFFSET_T);
4077 if (read_efi_label(config, &sb, NULL) < 0)
4082 for (c = 0; c < children; c++) {
4083 sb = find_start_block(child[c]);
4084 if (sb != MAXOFFSET_T) {
4088 return (MAXOFFSET_T);
4090 #endif /* illumos */
4093 * Label an individual disk. The name provided is the short name,
4094 * stripped of any leading /dev path.
4097 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, const char *name,
4098 zpool_boot_label_t boot_type, uint64_t boot_size, int *slice)
4101 char path[MAXPATHLEN];
4102 struct dk_gpt *vtoc;
4104 size_t resv = EFI_MIN_RESV_SIZE;
4105 uint64_t slice_size;
4106 diskaddr_t start_block;
4109 /* prepare an error message just in case */
4110 (void) snprintf(errbuf, sizeof (errbuf),
4111 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
4116 verify(nvlist_lookup_nvlist(zhp->zpool_config,
4117 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4119 if (zhp->zpool_start_block == 0)
4120 start_block = find_start_block(nvroot);
4122 start_block = zhp->zpool_start_block;
4123 zhp->zpool_start_block = start_block;
4126 start_block = NEW_START_BLOCK;
4129 (void) snprintf(path, sizeof (path), "%s/%s%s", ZFS_RDISK_ROOT, name,
4132 if ((fd = open(path, O_RDWR | O_NDELAY)) < 0) {
4134 * This shouldn't happen. We've long since verified that this
4135 * is a valid device.
4138 dgettext(TEXT_DOMAIN, "unable to open device"));
4139 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
4142 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
4144 * The only way this can fail is if we run out of memory, or we
4145 * were unable to read the disk's capacity
4147 if (errno == ENOMEM)
4148 (void) no_memory(hdl);
4151 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4152 "unable to read disk capacity"), name);
4154 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
4158 * Why we use V_USR: V_BACKUP confuses users, and is considered
4159 * disposable by some EFI utilities (since EFI doesn't have a backup
4160 * slice). V_UNASSIGNED is supposed to be used only for zero size
4161 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4162 * etc. were all pretty specific. V_USR is as close to reality as we
4163 * can get, in the absence of V_OTHER.
4165 /* first fix the partition start block */
4166 if (start_block == MAXOFFSET_T)
4167 start_block = NEW_START_BLOCK;
4170 * EFI System partition is using slice 0.
4171 * ZFS is on slice 1 and slice 8 is reserved.
4172 * We assume the GPT partition table without system
4173 * partition has zfs p_start == NEW_START_BLOCK.
4174 * If start_block != NEW_START_BLOCK, it means we have
4175 * system partition. Correct solution would be to query/cache vtoc
4176 * from existing vdev member.
4178 if (boot_type == ZPOOL_CREATE_BOOT_LABEL) {
4179 if (boot_size % vtoc->efi_lbasize != 0) {
4180 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4181 "boot partition size must be a multiple of %d"),
4185 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4188 * System partition size checks.
4189 * Note the 1MB is quite arbitrary value, since we
4190 * are creating dedicated pool, it should be enough
4191 * to hold fat + efi bootloader. May need to be
4192 * adjusted if the bootloader size will grow.
4194 if (boot_size < 1024 * 1024) {
4196 zfs_nicenum(boot_size, buf, sizeof (buf));
4197 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4198 "Specified size %s for EFI System partition is too "
4199 "small, the minimum size is 1MB."), buf);
4202 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4204 /* 33MB is tested with mkfs -F pcfs */
4205 if (hdl->libzfs_printerr &&
4206 ((vtoc->efi_lbasize == 512 &&
4207 boot_size < 33 * 1024 * 1024) ||
4208 (vtoc->efi_lbasize == 4096 &&
4209 boot_size < 256 * 1024 * 1024))) {
4211 zfs_nicenum(boot_size, buf, sizeof (buf));
4212 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
4213 "Warning: EFI System partition size %s is "
4214 "not allowing to create FAT32 file\nsystem, which "
4215 "may result in unbootable system.\n"), buf);
4217 /* Adjust zfs partition start by size of system partition. */
4218 start_block += boot_size / vtoc->efi_lbasize;
4221 if (start_block == NEW_START_BLOCK) {
4223 * Use default layout.
4224 * ZFS is on slice 0 and slice 8 is reserved.
4226 slice_size = vtoc->efi_last_u_lba + 1;
4227 slice_size -= EFI_MIN_RESV_SIZE;
4228 slice_size -= start_block;
4232 vtoc->efi_parts[0].p_start = start_block;
4233 vtoc->efi_parts[0].p_size = slice_size;
4235 vtoc->efi_parts[0].p_tag = V_USR;
4236 (void) strcpy(vtoc->efi_parts[0].p_name, "zfs");
4238 vtoc->efi_parts[8].p_start = slice_size + start_block;
4239 vtoc->efi_parts[8].p_size = resv;
4240 vtoc->efi_parts[8].p_tag = V_RESERVED;
4242 slice_size = start_block - NEW_START_BLOCK;
4243 vtoc->efi_parts[0].p_start = NEW_START_BLOCK;
4244 vtoc->efi_parts[0].p_size = slice_size;
4245 vtoc->efi_parts[0].p_tag = V_SYSTEM;
4246 (void) strcpy(vtoc->efi_parts[0].p_name, "loader");
4249 /* prepare slice 1 */
4250 slice_size = vtoc->efi_last_u_lba + 1 - slice_size;
4252 slice_size -= NEW_START_BLOCK;
4253 vtoc->efi_parts[1].p_start = start_block;
4254 vtoc->efi_parts[1].p_size = slice_size;
4255 vtoc->efi_parts[1].p_tag = V_USR;
4256 (void) strcpy(vtoc->efi_parts[1].p_name, "zfs");
4258 vtoc->efi_parts[8].p_start = slice_size + start_block;
4259 vtoc->efi_parts[8].p_size = resv;
4260 vtoc->efi_parts[8].p_tag = V_RESERVED;
4263 if (efi_write(fd, vtoc) != 0) {
4265 * Some block drivers (like pcata) may not support EFI
4266 * GPT labels. Print out a helpful error message dir-
4267 * ecting the user to manually label the disk and give
4273 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4274 "try using fdisk(1M) and then provide a specific slice"));
4275 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4280 #endif /* illumos */
4285 supported_dump_vdev_type(libzfs_handle_t *hdl, nvlist_t *config, char *errbuf)
4291 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_TYPE, &type) == 0);
4292 if (strcmp(type, VDEV_TYPE_FILE) == 0 ||
4293 strcmp(type, VDEV_TYPE_HOLE) == 0 ||
4294 strcmp(type, VDEV_TYPE_MISSING) == 0) {
4295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4296 "vdev type '%s' is not supported"), type);
4297 (void) zfs_error(hdl, EZFS_VDEVNOTSUP, errbuf);
4300 if (nvlist_lookup_nvlist_array(config, ZPOOL_CONFIG_CHILDREN,
4301 &child, &children) == 0) {
4302 for (c = 0; c < children; c++) {
4303 if (!supported_dump_vdev_type(hdl, child[c], errbuf))
4311 * Check if this zvol is allowable for use as a dump device; zero if
4312 * it is, > 0 if it isn't, < 0 if it isn't a zvol.
4314 * Allowable storage configurations include mirrors, all raidz variants, and
4315 * pools with log, cache, and spare devices. Pools which are backed by files or
4316 * have missing/hole vdevs are not suitable.
4319 zvol_check_dump_config(char *arg)
4321 zpool_handle_t *zhp = NULL;
4322 nvlist_t *config, *nvroot;
4326 libzfs_handle_t *hdl;
4328 char poolname[ZFS_MAX_DATASET_NAME_LEN];
4329 int pathlen = strlen(ZVOL_FULL_DEV_DIR);
4332 if (strncmp(arg, ZVOL_FULL_DEV_DIR, pathlen)) {
4336 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4337 "dump is not supported on device '%s'"), arg);
4339 if ((hdl = libzfs_init()) == NULL)
4341 libzfs_print_on_error(hdl, B_TRUE);
4343 volname = arg + pathlen;
4345 /* check the configuration of the pool */
4346 if ((p = strchr(volname, '/')) == NULL) {
4347 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4348 "malformed dataset name"));
4349 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4351 } else if (p - volname >= ZFS_MAX_DATASET_NAME_LEN) {
4352 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4353 "dataset name is too long"));
4354 (void) zfs_error(hdl, EZFS_NAMETOOLONG, errbuf);
4357 (void) strncpy(poolname, volname, p - volname);
4358 poolname[p - volname] = '\0';
4361 if ((zhp = zpool_open(hdl, poolname)) == NULL) {
4362 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4363 "could not open pool '%s'"), poolname);
4364 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
4367 config = zpool_get_config(zhp, NULL);
4368 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
4370 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4371 "could not obtain vdev configuration for '%s'"), poolname);
4372 (void) zfs_error(hdl, EZFS_INVALCONFIG, errbuf);
4376 verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
4377 &top, &toplevels) == 0);
4379 if (!supported_dump_vdev_type(hdl, top[0], errbuf)) {
4392 zpool_nextboot(libzfs_handle_t *hdl, uint64_t pool_guid, uint64_t dev_guid,
4393 const char *command)
4395 zfs_cmd_t zc = { 0 };
4401 args = fnvlist_alloc();
4402 fnvlist_add_uint64(args, ZPOOL_CONFIG_POOL_GUID, pool_guid);
4403 fnvlist_add_uint64(args, ZPOOL_CONFIG_GUID, dev_guid);
4404 fnvlist_add_string(args, "command", command);
4405 error = zcmd_write_src_nvlist(hdl, &zc, args);
4407 error = ioctl(hdl->libzfs_fd, ZFS_IOC_NEXTBOOT, &zc);
4408 zcmd_free_nvlists(&zc);