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 2015 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27 * Copyright (c) 2017 Datto Inc.
28 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
43 #include <sys/efi_partition.h>
44 #include <sys/systeminfo.h>
46 #include <sys/zfs_ioctl.h>
49 #include "zfs_namecheck.h"
51 #include "libzfs_impl.h"
52 #include "zfs_comutil.h"
53 #include "zfeature_common.h"
55 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
56 static boolean_t zpool_vdev_is_interior(const char *name);
58 typedef struct prop_flags {
59 int create:1; /* Validate property on creation */
60 int import:1; /* Validate property on import */
64 * ====================================================================
65 * zpool property functions
66 * ====================================================================
70 zpool_get_all_props(zpool_handle_t *zhp)
72 zfs_cmd_t zc = {"\0"};
73 libzfs_handle_t *hdl = zhp->zpool_hdl;
75 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
77 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
80 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
81 if (errno == ENOMEM) {
82 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
83 zcmd_free_nvlists(&zc);
87 zcmd_free_nvlists(&zc);
92 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
93 zcmd_free_nvlists(&zc);
97 zcmd_free_nvlists(&zc);
103 zpool_props_refresh(zpool_handle_t *zhp)
107 old_props = zhp->zpool_props;
109 if (zpool_get_all_props(zhp) != 0)
112 nvlist_free(old_props);
117 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
123 zprop_source_t source;
125 nvl = zhp->zpool_props;
126 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
127 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
129 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
131 source = ZPROP_SRC_DEFAULT;
132 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
143 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
147 zprop_source_t source;
149 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
151 * zpool_get_all_props() has most likely failed because
152 * the pool is faulted, but if all we need is the top level
153 * vdev's guid then get it from the zhp config nvlist.
155 if ((prop == ZPOOL_PROP_GUID) &&
156 (nvlist_lookup_nvlist(zhp->zpool_config,
157 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
158 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
162 return (zpool_prop_default_numeric(prop));
165 nvl = zhp->zpool_props;
166 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
167 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
169 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
171 source = ZPROP_SRC_DEFAULT;
172 value = zpool_prop_default_numeric(prop);
182 * Map VDEV STATE to printed strings.
185 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
188 case VDEV_STATE_CLOSED:
189 case VDEV_STATE_OFFLINE:
190 return (gettext("OFFLINE"));
191 case VDEV_STATE_REMOVED:
192 return (gettext("REMOVED"));
193 case VDEV_STATE_CANT_OPEN:
194 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
195 return (gettext("FAULTED"));
196 else if (aux == VDEV_AUX_SPLIT_POOL)
197 return (gettext("SPLIT"));
199 return (gettext("UNAVAIL"));
200 case VDEV_STATE_FAULTED:
201 return (gettext("FAULTED"));
202 case VDEV_STATE_DEGRADED:
203 return (gettext("DEGRADED"));
204 case VDEV_STATE_HEALTHY:
205 return (gettext("ONLINE"));
211 return (gettext("UNKNOWN"));
215 * Map POOL STATE to printed strings.
218 zpool_pool_state_to_name(pool_state_t state)
223 case POOL_STATE_ACTIVE:
224 return (gettext("ACTIVE"));
225 case POOL_STATE_EXPORTED:
226 return (gettext("EXPORTED"));
227 case POOL_STATE_DESTROYED:
228 return (gettext("DESTROYED"));
229 case POOL_STATE_SPARE:
230 return (gettext("SPARE"));
231 case POOL_STATE_L2CACHE:
232 return (gettext("L2CACHE"));
233 case POOL_STATE_UNINITIALIZED:
234 return (gettext("UNINITIALIZED"));
235 case POOL_STATE_UNAVAIL:
236 return (gettext("UNAVAIL"));
237 case POOL_STATE_POTENTIALLY_ACTIVE:
238 return (gettext("POTENTIALLY_ACTIVE"));
241 return (gettext("UNKNOWN"));
245 * Get a zpool property value for 'prop' and return the value in
246 * a pre-allocated buffer.
249 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf,
250 size_t len, zprop_source_t *srctype, boolean_t literal)
254 zprop_source_t src = ZPROP_SRC_NONE;
259 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
261 case ZPOOL_PROP_NAME:
262 (void) strlcpy(buf, zpool_get_name(zhp), len);
265 case ZPOOL_PROP_HEALTH:
266 (void) strlcpy(buf, "FAULTED", len);
269 case ZPOOL_PROP_GUID:
270 intval = zpool_get_prop_int(zhp, prop, &src);
271 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
274 case ZPOOL_PROP_ALTROOT:
275 case ZPOOL_PROP_CACHEFILE:
276 case ZPOOL_PROP_COMMENT:
277 if (zhp->zpool_props != NULL ||
278 zpool_get_all_props(zhp) == 0) {
280 zpool_get_prop_string(zhp, prop, &src),
286 (void) strlcpy(buf, "-", len);
295 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
296 prop != ZPOOL_PROP_NAME)
299 switch (zpool_prop_get_type(prop)) {
300 case PROP_TYPE_STRING:
301 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
305 case PROP_TYPE_NUMBER:
306 intval = zpool_get_prop_int(zhp, prop, &src);
309 case ZPOOL_PROP_SIZE:
310 case ZPOOL_PROP_ALLOCATED:
311 case ZPOOL_PROP_FREE:
312 case ZPOOL_PROP_FREEING:
313 case ZPOOL_PROP_LEAKED:
314 case ZPOOL_PROP_ASHIFT:
316 (void) snprintf(buf, len, "%llu",
317 (u_longlong_t)intval);
319 (void) zfs_nicenum(intval, buf, len);
322 case ZPOOL_PROP_EXPANDSZ:
324 (void) strlcpy(buf, "-", len);
325 } else if (literal) {
326 (void) snprintf(buf, len, "%llu",
327 (u_longlong_t)intval);
329 (void) zfs_nicebytes(intval, buf, len);
333 case ZPOOL_PROP_CAPACITY:
335 (void) snprintf(buf, len, "%llu",
336 (u_longlong_t)intval);
338 (void) snprintf(buf, len, "%llu%%",
339 (u_longlong_t)intval);
343 case ZPOOL_PROP_FRAGMENTATION:
344 if (intval == UINT64_MAX) {
345 (void) strlcpy(buf, "-", len);
346 } else if (literal) {
347 (void) snprintf(buf, len, "%llu",
348 (u_longlong_t)intval);
350 (void) snprintf(buf, len, "%llu%%",
351 (u_longlong_t)intval);
355 case ZPOOL_PROP_DEDUPRATIO:
357 (void) snprintf(buf, len, "%llu.%02llu",
358 (u_longlong_t)(intval / 100),
359 (u_longlong_t)(intval % 100));
361 (void) snprintf(buf, len, "%llu.%02llux",
362 (u_longlong_t)(intval / 100),
363 (u_longlong_t)(intval % 100));
366 case ZPOOL_PROP_HEALTH:
367 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
368 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
369 verify(nvlist_lookup_uint64_array(nvroot,
370 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
373 (void) strlcpy(buf, zpool_state_to_name(intval,
376 case ZPOOL_PROP_VERSION:
377 if (intval >= SPA_VERSION_FEATURES) {
378 (void) snprintf(buf, len, "-");
383 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
387 case PROP_TYPE_INDEX:
388 intval = zpool_get_prop_int(zhp, prop, &src);
389 if (zpool_prop_index_to_string(prop, intval, &strval)
392 (void) strlcpy(buf, strval, len);
406 * Check if the bootfs name has the same pool name as it is set to.
407 * Assuming bootfs is a valid dataset name.
410 bootfs_name_valid(const char *pool, char *bootfs)
412 int len = strlen(pool);
413 if (bootfs[0] == '\0')
416 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
419 if (strncmp(pool, bootfs, len) == 0 &&
420 (bootfs[len] == '/' || bootfs[len] == '\0'))
427 zpool_is_bootable(zpool_handle_t *zhp)
429 char bootfs[ZFS_MAX_DATASET_NAME_LEN];
431 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
432 sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-",
433 sizeof (bootfs)) != 0);
438 * Given an nvlist of zpool properties to be set, validate that they are
439 * correct, and parse any numeric properties (index, boolean, etc) if they are
440 * specified as strings.
443 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
444 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
452 struct stat64 statbuf;
455 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
456 (void) no_memory(hdl);
461 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
462 const char *propname = nvpair_name(elem);
464 prop = zpool_name_to_prop(propname);
465 if (prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname)) {
467 char *fname = strchr(propname, '@') + 1;
469 err = zfeature_lookup_name(fname, NULL);
471 ASSERT3U(err, ==, ENOENT);
472 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
473 "invalid feature '%s'"), fname);
474 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
478 if (nvpair_type(elem) != DATA_TYPE_STRING) {
479 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
480 "'%s' must be a string"), propname);
481 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
485 (void) nvpair_value_string(elem, &strval);
486 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0 &&
487 strcmp(strval, ZFS_FEATURE_DISABLED) != 0) {
488 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
489 "property '%s' can only be set to "
490 "'enabled' or 'disabled'"), propname);
491 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
495 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
496 (void) no_memory(hdl);
503 * Make sure this property is valid and applies to this type.
505 if (prop == ZPOOL_PROP_INVAL) {
506 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
507 "invalid property '%s'"), propname);
508 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
512 if (zpool_prop_readonly(prop)) {
513 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
514 "is readonly"), propname);
515 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
519 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
520 &strval, &intval, errbuf) != 0)
524 * Perform additional checking for specific properties.
527 case ZPOOL_PROP_VERSION:
528 if (intval < version ||
529 !SPA_VERSION_IS_SUPPORTED(intval)) {
530 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
531 "property '%s' number %d is invalid."),
533 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
538 case ZPOOL_PROP_ASHIFT:
540 (intval < ASHIFT_MIN || intval > ASHIFT_MAX)) {
541 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
542 "invalid '%s=%d' property: only values "
543 "between %" PRId32 " and %" PRId32 " "
545 propname, intval, ASHIFT_MIN, ASHIFT_MAX);
546 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
551 case ZPOOL_PROP_BOOTFS:
552 if (flags.create || flags.import) {
553 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
554 "property '%s' cannot be set at creation "
555 "or import time"), propname);
556 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
560 if (version < SPA_VERSION_BOOTFS) {
561 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
562 "pool must be upgraded to support "
563 "'%s' property"), propname);
564 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
569 * bootfs property value has to be a dataset name and
570 * the dataset has to be in the same pool as it sets to.
572 if (!bootfs_name_valid(poolname, strval)) {
573 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
574 "is an invalid name"), strval);
575 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
579 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
580 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
581 "could not open pool '%s'"), poolname);
582 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
588 case ZPOOL_PROP_ALTROOT:
589 if (!flags.create && !flags.import) {
590 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
591 "property '%s' can only be set during pool "
592 "creation or import"), propname);
593 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
597 if (strval[0] != '/') {
598 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
599 "bad alternate root '%s'"), strval);
600 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
605 case ZPOOL_PROP_CACHEFILE:
606 if (strval[0] == '\0')
609 if (strcmp(strval, "none") == 0)
612 if (strval[0] != '/') {
613 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
614 "property '%s' must be empty, an "
615 "absolute path, or 'none'"), propname);
616 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
620 slash = strrchr(strval, '/');
622 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
623 strcmp(slash, "/..") == 0) {
624 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
625 "'%s' is not a valid file"), strval);
626 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
632 if (strval[0] != '\0' &&
633 (stat64(strval, &statbuf) != 0 ||
634 !S_ISDIR(statbuf.st_mode))) {
635 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
636 "'%s' is not a valid directory"),
638 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
645 case ZPOOL_PROP_COMMENT:
646 for (check = strval; *check != '\0'; check++) {
647 if (!isprint(*check)) {
649 dgettext(TEXT_DOMAIN,
650 "comment may only have printable "
652 (void) zfs_error(hdl, EZFS_BADPROP,
657 if (strlen(strval) > ZPROP_MAX_COMMENT) {
658 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
659 "comment must not exceed %d characters"),
661 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
665 case ZPOOL_PROP_READONLY:
667 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
668 "property '%s' can only be set at "
669 "import time"), propname);
670 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
674 case ZPOOL_PROP_TNAME:
676 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
677 "property '%s' can only be set at "
678 "creation time"), propname);
679 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
683 case ZPOOL_PROP_MULTIHOST:
684 if (get_system_hostid() == 0) {
685 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
686 "requires a non-zero system hostid"));
687 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
692 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
693 "property '%s'(%d) not defined"), propname, prop);
700 nvlist_free(retprops);
705 * Set zpool property : propname=propval.
708 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
710 zfs_cmd_t zc = {"\0"};
713 nvlist_t *nvl = NULL;
716 prop_flags_t flags = { 0 };
718 (void) snprintf(errbuf, sizeof (errbuf),
719 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
722 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
723 return (no_memory(zhp->zpool_hdl));
725 if (nvlist_add_string(nvl, propname, propval) != 0) {
727 return (no_memory(zhp->zpool_hdl));
730 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
731 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
732 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
741 * Execute the corresponding ioctl() to set this property.
743 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
745 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
750 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
752 zcmd_free_nvlists(&zc);
756 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
758 (void) zpool_props_refresh(zhp);
764 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
766 libzfs_handle_t *hdl = zhp->zpool_hdl;
768 char buf[ZFS_MAXPROPLEN];
769 nvlist_t *features = NULL;
772 boolean_t firstexpand = (NULL == *plp);
775 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
779 while (*last != NULL)
780 last = &(*last)->pl_next;
783 features = zpool_get_features(zhp);
785 if ((*plp)->pl_all && firstexpand) {
786 for (i = 0; i < SPA_FEATURES; i++) {
787 zprop_list_t *entry = zfs_alloc(hdl,
788 sizeof (zprop_list_t));
789 entry->pl_prop = ZPROP_INVAL;
790 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
791 spa_feature_table[i].fi_uname);
792 entry->pl_width = strlen(entry->pl_user_prop);
793 entry->pl_all = B_TRUE;
796 last = &entry->pl_next;
800 /* add any unsupported features */
801 for (nvp = nvlist_next_nvpair(features, NULL);
802 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
807 if (zfeature_is_supported(nvpair_name(nvp)))
810 propname = zfs_asprintf(hdl, "unsupported@%s",
814 * Before adding the property to the list make sure that no
815 * other pool already added the same property.
819 while (entry != NULL) {
820 if (entry->pl_user_prop != NULL &&
821 strcmp(propname, entry->pl_user_prop) == 0) {
825 entry = entry->pl_next;
832 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
833 entry->pl_prop = ZPROP_INVAL;
834 entry->pl_user_prop = propname;
835 entry->pl_width = strlen(entry->pl_user_prop);
836 entry->pl_all = B_TRUE;
839 last = &entry->pl_next;
842 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
847 if (entry->pl_prop != ZPROP_INVAL &&
848 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
849 NULL, B_FALSE) == 0) {
850 if (strlen(buf) > entry->pl_width)
851 entry->pl_width = strlen(buf);
859 * Get the state for the given feature on the given ZFS pool.
862 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
866 boolean_t found = B_FALSE;
867 nvlist_t *features = zpool_get_features(zhp);
869 const char *feature = strchr(propname, '@') + 1;
871 supported = zpool_prop_feature(propname);
872 ASSERT(supported || zpool_prop_unsupported(propname));
875 * Convert from feature name to feature guid. This conversion is
876 * unnecessary for unsupported@... properties because they already
883 ret = zfeature_lookup_name(feature, &fid);
885 (void) strlcpy(buf, "-", len);
888 feature = spa_feature_table[fid].fi_guid;
891 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
896 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
899 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
901 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
906 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
908 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
911 (void) strlcpy(buf, "-", len);
920 * Don't start the slice at the default block of 34; many storage
921 * devices will use a stripe width of 128k, other vendors prefer a 1m
922 * alignment. It is best to play it safe and ensure a 1m alignment
923 * given 512B blocks. When the block size is larger by a power of 2
924 * we will still be 1m aligned. Some devices are sensitive to the
925 * partition ending alignment as well.
927 #define NEW_START_BLOCK 2048
928 #define PARTITION_END_ALIGNMENT 2048
931 * Validate the given pool name, optionally putting an extended error message in
935 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
941 ret = pool_namecheck(pool, &why, &what);
944 * The rules for reserved pool names were extended at a later point.
945 * But we need to support users with existing pools that may now be
946 * invalid. So we only check for this expanded set of names during a
947 * create (or import), and only in userland.
949 if (ret == 0 && !isopen &&
950 (strncmp(pool, "mirror", 6) == 0 ||
951 strncmp(pool, "raidz", 5) == 0 ||
952 strncmp(pool, "spare", 5) == 0 ||
953 strcmp(pool, "log") == 0)) {
956 dgettext(TEXT_DOMAIN, "name is reserved"));
964 case NAME_ERR_TOOLONG:
966 dgettext(TEXT_DOMAIN, "name is too long"));
969 case NAME_ERR_INVALCHAR:
971 dgettext(TEXT_DOMAIN, "invalid character "
972 "'%c' in pool name"), what);
975 case NAME_ERR_NOLETTER:
976 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
977 "name must begin with a letter"));
980 case NAME_ERR_RESERVED:
981 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
982 "name is reserved"));
985 case NAME_ERR_DISKLIKE:
986 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
987 "pool name is reserved"));
990 case NAME_ERR_LEADING_SLASH:
991 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
992 "leading slash in name"));
995 case NAME_ERR_EMPTY_COMPONENT:
996 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
997 "empty component in name"));
1000 case NAME_ERR_TRAILING_SLASH:
1001 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1002 "trailing slash in name"));
1005 case NAME_ERR_MULTIPLE_DELIMITERS:
1006 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1007 "multiple '@' and/or '#' delimiters in "
1011 case NAME_ERR_NO_AT:
1012 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1013 "permission set is missing '@'"));
1017 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1018 "(%d) not defined"), why);
1029 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1033 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
1035 zpool_handle_t *zhp;
1039 * Make sure the pool name is valid.
1041 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
1042 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1043 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1048 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1051 zhp->zpool_hdl = hdl;
1052 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1054 if (zpool_refresh_stats(zhp, &missing) != 0) {
1060 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1061 (void) zfs_error_fmt(hdl, EZFS_NOENT,
1062 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1071 * Like the above, but silent on error. Used when iterating over pools (because
1072 * the configuration cache may be out of date).
1075 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1077 zpool_handle_t *zhp;
1080 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1083 zhp->zpool_hdl = hdl;
1084 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1086 if (zpool_refresh_stats(zhp, &missing) != 0) {
1102 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1106 zpool_open(libzfs_handle_t *hdl, const char *pool)
1108 zpool_handle_t *zhp;
1110 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1113 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1114 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1115 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1124 * Close the handle. Simply frees the memory associated with the handle.
1127 zpool_close(zpool_handle_t *zhp)
1129 nvlist_free(zhp->zpool_config);
1130 nvlist_free(zhp->zpool_old_config);
1131 nvlist_free(zhp->zpool_props);
1136 * Return the name of the pool.
1139 zpool_get_name(zpool_handle_t *zhp)
1141 return (zhp->zpool_name);
1146 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1149 zpool_get_state(zpool_handle_t *zhp)
1151 return (zhp->zpool_state);
1155 * Create the named pool, using the provided vdev list. It is assumed
1156 * that the consumer has already validated the contents of the nvlist, so we
1157 * don't have to worry about error semantics.
1160 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1161 nvlist_t *props, nvlist_t *fsprops)
1163 zfs_cmd_t zc = {"\0"};
1164 nvlist_t *zc_fsprops = NULL;
1165 nvlist_t *zc_props = NULL;
1166 nvlist_t *hidden_args = NULL;
1167 uint8_t *wkeydata = NULL;
1172 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1173 "cannot create '%s'"), pool);
1175 if (!zpool_name_valid(hdl, B_FALSE, pool))
1176 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1178 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1182 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1184 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1185 SPA_VERSION_1, flags, msg)) == NULL) {
1194 zoned = ((nvlist_lookup_string(fsprops,
1195 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1196 strcmp(zonestr, "on") == 0);
1198 if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
1199 fsprops, zoned, NULL, NULL, B_TRUE, msg)) == NULL) {
1203 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1206 if (zfs_crypto_create(hdl, NULL, zc_fsprops, props,
1207 &wkeydata, &wkeylen) != 0) {
1208 zfs_error(hdl, EZFS_CRYPTOFAILED, msg);
1211 if (nvlist_add_nvlist(zc_props,
1212 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1215 if (wkeydata != NULL) {
1216 if (nvlist_alloc(&hidden_args, NV_UNIQUE_NAME, 0) != 0)
1219 if (nvlist_add_uint8_array(hidden_args, "wkeydata",
1220 wkeydata, wkeylen) != 0)
1223 if (nvlist_add_nvlist(zc_props, ZPOOL_HIDDEN_ARGS,
1229 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1232 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1234 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1236 zcmd_free_nvlists(&zc);
1237 nvlist_free(zc_props);
1238 nvlist_free(zc_fsprops);
1239 nvlist_free(hidden_args);
1240 if (wkeydata != NULL)
1246 * This can happen if the user has specified the same
1247 * device multiple times. We can't reliably detect this
1248 * until we try to add it and see we already have a
1249 * label. This can also happen under if the device is
1250 * part of an active md or lvm device.
1252 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1253 "one or more vdevs refer to the same device, or "
1254 "one of\nthe devices is part of an active md or "
1256 return (zfs_error(hdl, EZFS_BADDEV, msg));
1260 * This happens if the record size is smaller or larger
1261 * than the allowed size range, or not a power of 2.
1263 * NOTE: although zfs_valid_proplist is called earlier,
1264 * this case may have slipped through since the
1265 * pool does not exist yet and it is therefore
1266 * impossible to read properties e.g. max blocksize
1269 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1270 "record size invalid"));
1271 return (zfs_error(hdl, EZFS_BADPROP, msg));
1275 * This occurs when one of the devices is below
1276 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1277 * device was the problem device since there's no
1278 * reliable way to determine device size from userland.
1283 zfs_nicebytes(SPA_MINDEVSIZE, buf,
1286 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1287 "one or more devices is less than the "
1288 "minimum size (%s)"), buf);
1290 return (zfs_error(hdl, EZFS_BADDEV, msg));
1293 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1294 "one or more devices is out of space"));
1295 return (zfs_error(hdl, EZFS_BADDEV, msg));
1298 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1299 "cache device must be a disk or disk slice"));
1300 return (zfs_error(hdl, EZFS_BADDEV, msg));
1303 return (zpool_standard_error(hdl, errno, msg));
1308 zcmd_free_nvlists(&zc);
1309 nvlist_free(zc_props);
1310 nvlist_free(zc_fsprops);
1311 nvlist_free(hidden_args);
1312 if (wkeydata != NULL)
1318 * Destroy the given pool. It is up to the caller to ensure that there are no
1319 * datasets left in the pool.
1322 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1324 zfs_cmd_t zc = {"\0"};
1325 zfs_handle_t *zfp = NULL;
1326 libzfs_handle_t *hdl = zhp->zpool_hdl;
1329 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1330 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1333 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1334 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1336 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1337 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1338 "cannot destroy '%s'"), zhp->zpool_name);
1340 if (errno == EROFS) {
1341 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1342 "one or more devices is read only"));
1343 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1345 (void) zpool_standard_error(hdl, errno, msg);
1354 remove_mountpoint(zfp);
1362 * Add the given vdevs to the pool. The caller must have already performed the
1363 * necessary verification to ensure that the vdev specification is well-formed.
1366 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1368 zfs_cmd_t zc = {"\0"};
1370 libzfs_handle_t *hdl = zhp->zpool_hdl;
1372 nvlist_t **spares, **l2cache;
1373 uint_t nspares, nl2cache;
1375 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1376 "cannot add to '%s'"), zhp->zpool_name);
1378 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1379 SPA_VERSION_SPARES &&
1380 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1381 &spares, &nspares) == 0) {
1382 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1383 "upgraded to add hot spares"));
1384 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1387 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1388 SPA_VERSION_L2CACHE &&
1389 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1390 &l2cache, &nl2cache) == 0) {
1391 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1392 "upgraded to add cache devices"));
1393 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1396 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1398 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1400 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1404 * This can happen if the user has specified the same
1405 * device multiple times. We can't reliably detect this
1406 * until we try to add it and see we already have a
1409 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1410 "one or more vdevs refer to the same device"));
1411 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1416 * This occurrs when one of the devices is below
1417 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1418 * device was the problem device since there's no
1419 * reliable way to determine device size from userland.
1424 zfs_nicebytes(SPA_MINDEVSIZE, buf,
1427 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1428 "device is less than the minimum "
1431 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1435 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1436 "pool must be upgraded to add these vdevs"));
1437 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1441 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1442 "cache device must be a disk or disk slice"));
1443 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1447 (void) zpool_standard_error(hdl, errno, msg);
1455 zcmd_free_nvlists(&zc);
1461 * Exports the pool from the system. The caller must ensure that there are no
1462 * mounted datasets in the pool.
1465 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1466 const char *log_str)
1468 zfs_cmd_t zc = {"\0"};
1471 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1472 "cannot export '%s'"), zhp->zpool_name);
1474 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1475 zc.zc_cookie = force;
1476 zc.zc_guid = hardforce;
1477 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1479 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1482 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1483 "use '-f' to override the following errors:\n"
1484 "'%s' has an active shared spare which could be"
1485 " used by other pools once '%s' is exported."),
1486 zhp->zpool_name, zhp->zpool_name);
1487 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1490 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1499 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1501 return (zpool_export_common(zhp, force, B_FALSE, log_str));
1505 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1507 return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1511 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1514 nvlist_t *nv = NULL;
1520 if (!hdl->libzfs_printerr || config == NULL)
1523 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1524 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1528 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1530 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1532 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1533 strftime(timestr, 128, "%c", &t) != 0) {
1535 (void) printf(dgettext(TEXT_DOMAIN,
1536 "Would be able to return %s "
1537 "to its state as of %s.\n"),
1540 (void) printf(dgettext(TEXT_DOMAIN,
1541 "Pool %s returned to its state as of %s.\n"),
1545 (void) printf(dgettext(TEXT_DOMAIN,
1546 "%s approximately %lld "),
1547 dryrun ? "Would discard" : "Discarded",
1548 ((longlong_t)loss + 30) / 60);
1549 (void) printf(dgettext(TEXT_DOMAIN,
1550 "minutes of transactions.\n"));
1551 } else if (loss > 0) {
1552 (void) printf(dgettext(TEXT_DOMAIN,
1553 "%s approximately %lld "),
1554 dryrun ? "Would discard" : "Discarded",
1556 (void) printf(dgettext(TEXT_DOMAIN,
1557 "seconds of transactions.\n"));
1563 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1566 nvlist_t *nv = NULL;
1568 uint64_t edata = UINT64_MAX;
1573 if (!hdl->libzfs_printerr)
1577 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1579 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1581 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1582 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1583 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1584 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1587 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1588 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1591 (void) printf(dgettext(TEXT_DOMAIN,
1592 "Recovery is possible, but will result in some data loss.\n"));
1594 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1595 strftime(timestr, 128, "%c", &t) != 0) {
1596 (void) printf(dgettext(TEXT_DOMAIN,
1597 "\tReturning the pool to its state as of %s\n"
1598 "\tshould correct the problem. "),
1601 (void) printf(dgettext(TEXT_DOMAIN,
1602 "\tReverting the pool to an earlier state "
1603 "should correct the problem.\n\t"));
1607 (void) printf(dgettext(TEXT_DOMAIN,
1608 "Approximately %lld minutes of data\n"
1609 "\tmust be discarded, irreversibly. "),
1610 ((longlong_t)loss + 30) / 60);
1611 } else if (loss > 0) {
1612 (void) printf(dgettext(TEXT_DOMAIN,
1613 "Approximately %lld seconds of data\n"
1614 "\tmust be discarded, irreversibly. "),
1617 if (edata != 0 && edata != UINT64_MAX) {
1619 (void) printf(dgettext(TEXT_DOMAIN,
1620 "After rewind, at least\n"
1621 "\tone persistent user-data error will remain. "));
1623 (void) printf(dgettext(TEXT_DOMAIN,
1624 "After rewind, several\n"
1625 "\tpersistent user-data errors will remain. "));
1628 (void) printf(dgettext(TEXT_DOMAIN,
1629 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1630 reason >= 0 ? "clear" : "import", name);
1632 (void) printf(dgettext(TEXT_DOMAIN,
1633 "A scrub of the pool\n"
1634 "\tis strongly recommended after recovery.\n"));
1638 (void) printf(dgettext(TEXT_DOMAIN,
1639 "Destroy and re-create the pool from\n\ta backup source.\n"));
1643 * zpool_import() is a contracted interface. Should be kept the same
1646 * Applications should use zpool_import_props() to import a pool with
1647 * new properties value to be set.
1650 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1653 nvlist_t *props = NULL;
1656 if (altroot != NULL) {
1657 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1658 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1659 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1663 if (nvlist_add_string(props,
1664 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1665 nvlist_add_string(props,
1666 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1668 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1669 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1674 ret = zpool_import_props(hdl, config, newname, props,
1681 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1687 uint64_t is_log = 0;
1689 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1693 (void) printf("\t%*s%s%s\n", indent, "", name,
1694 is_log ? " [log]" : "");
1696 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1697 &child, &children) != 0)
1700 for (c = 0; c < children; c++) {
1701 vname = zpool_vdev_name(hdl, NULL, child[c], VDEV_NAME_TYPE_ID);
1702 print_vdev_tree(hdl, vname, child[c], indent + 2);
1708 zpool_print_unsup_feat(nvlist_t *config)
1710 nvlist_t *nvinfo, *unsup_feat;
1713 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1715 verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1718 for (nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1719 nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1722 verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1723 verify(nvpair_value_string(nvp, &desc) == 0);
1725 if (strlen(desc) > 0)
1726 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1728 (void) printf("\t%s\n", nvpair_name(nvp));
1733 * Import the given pool using the known configuration and a list of
1734 * properties to be set. The configuration should have come from
1735 * zpool_find_import(). The 'newname' parameters control whether the pool
1736 * is imported with a different name.
1739 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1740 nvlist_t *props, int flags)
1742 zfs_cmd_t zc = {"\0"};
1743 zpool_rewind_policy_t policy;
1744 nvlist_t *nv = NULL;
1745 nvlist_t *nvinfo = NULL;
1746 nvlist_t *missing = NULL;
1753 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1756 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1757 "cannot import pool '%s'"), origname);
1759 if (newname != NULL) {
1760 if (!zpool_name_valid(hdl, B_FALSE, newname))
1761 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1762 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1764 thename = (char *)newname;
1769 if (props != NULL) {
1771 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1773 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1776 if ((props = zpool_valid_proplist(hdl, origname,
1777 props, version, flags, errbuf)) == NULL)
1779 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1786 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1788 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1791 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1792 zcmd_free_nvlists(&zc);
1795 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1796 zcmd_free_nvlists(&zc);
1800 zc.zc_cookie = flags;
1801 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1803 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1804 zcmd_free_nvlists(&zc);
1811 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1813 zcmd_free_nvlists(&zc);
1815 zpool_get_rewind_policy(config, &policy);
1822 * Dry-run failed, but we print out what success
1823 * looks like if we found a best txg
1825 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1826 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1832 if (newname == NULL)
1833 (void) snprintf(desc, sizeof (desc),
1834 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1837 (void) snprintf(desc, sizeof (desc),
1838 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1843 if (nv != NULL && nvlist_lookup_nvlist(nv,
1844 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1845 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1846 (void) printf(dgettext(TEXT_DOMAIN, "This "
1847 "pool uses the following feature(s) not "
1848 "supported by this system:\n"));
1849 zpool_print_unsup_feat(nv);
1850 if (nvlist_exists(nvinfo,
1851 ZPOOL_CONFIG_CAN_RDONLY)) {
1852 (void) printf(dgettext(TEXT_DOMAIN,
1853 "All unsupported features are only "
1854 "required for writing to the pool."
1855 "\nThe pool can be imported using "
1856 "'-o readonly=on'.\n"));
1860 * Unsupported version.
1862 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1866 if (nv != NULL && nvlist_lookup_nvlist(nv,
1867 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0) {
1868 char *hostname = "<unknown>";
1869 uint64_t hostid = 0;
1870 mmp_state_t mmp_state;
1872 mmp_state = fnvlist_lookup_uint64(nvinfo,
1873 ZPOOL_CONFIG_MMP_STATE);
1875 if (nvlist_exists(nvinfo,
1876 ZPOOL_CONFIG_MMP_HOSTNAME))
1877 hostname = fnvlist_lookup_string(nvinfo,
1878 ZPOOL_CONFIG_MMP_HOSTNAME);
1880 if (nvlist_exists(nvinfo,
1881 ZPOOL_CONFIG_MMP_HOSTID))
1882 hostid = fnvlist_lookup_uint64(nvinfo,
1883 ZPOOL_CONFIG_MMP_HOSTID);
1885 if (mmp_state == MMP_STATE_ACTIVE) {
1886 (void) snprintf(aux, sizeof (aux),
1887 dgettext(TEXT_DOMAIN, "pool is imp"
1888 "orted on host '%s' (hostid=%lx).\n"
1889 "Export the pool on the other "
1890 "system, then run 'zpool import'."),
1891 hostname, (unsigned long) hostid);
1892 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
1893 (void) snprintf(aux, sizeof (aux),
1894 dgettext(TEXT_DOMAIN, "pool has "
1895 "the multihost property on and "
1896 "the\nsystem's hostid is not set. "
1897 "Set a unique system hostid with "
1898 "the zgenhostid(8) command.\n"));
1901 (void) zfs_error_aux(hdl, aux);
1903 (void) zfs_error(hdl, EZFS_ACTIVE_POOL, desc);
1907 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1911 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1912 "one or more devices is read only"));
1913 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1917 if (nv && nvlist_lookup_nvlist(nv,
1918 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1919 nvlist_lookup_nvlist(nvinfo,
1920 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1921 (void) printf(dgettext(TEXT_DOMAIN,
1922 "The devices below are missing, use "
1923 "'-m' to import the pool anyway:\n"));
1924 print_vdev_tree(hdl, NULL, missing, 2);
1925 (void) printf("\n");
1927 (void) zpool_standard_error(hdl, error, desc);
1931 (void) zpool_standard_error(hdl, error, desc);
1935 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1936 "one or more devices are already in use\n"));
1937 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1940 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1941 "new name of at least one dataset is longer than "
1942 "the maximum allowable length"));
1943 (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
1946 (void) zpool_standard_error(hdl, error, desc);
1947 zpool_explain_recover(hdl,
1948 newname ? origname : thename, -error, nv);
1955 zpool_handle_t *zhp;
1958 * This should never fail, but play it safe anyway.
1960 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1962 else if (zhp != NULL)
1964 if (policy.zrp_request &
1965 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1966 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1967 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1980 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd)
1982 zfs_cmd_t zc = {"\0"};
1985 libzfs_handle_t *hdl = zhp->zpool_hdl;
1987 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1988 zc.zc_cookie = func;
1991 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0)
1996 /* ECANCELED on a scrub means we resumed a paused scrub */
1997 if (err == ECANCELED && func == POOL_SCAN_SCRUB &&
1998 cmd == POOL_SCRUB_NORMAL)
2001 if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL)
2004 if (func == POOL_SCAN_SCRUB) {
2005 if (cmd == POOL_SCRUB_PAUSE) {
2006 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2007 "cannot pause scrubbing %s"), zc.zc_name);
2009 assert(cmd == POOL_SCRUB_NORMAL);
2010 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2011 "cannot scrub %s"), zc.zc_name);
2013 } else if (func == POOL_SCAN_NONE) {
2014 (void) snprintf(msg, sizeof (msg),
2015 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
2018 assert(!"unexpected result");
2023 pool_scan_stat_t *ps = NULL;
2026 verify(nvlist_lookup_nvlist(zhp->zpool_config,
2027 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2028 (void) nvlist_lookup_uint64_array(nvroot,
2029 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
2030 if (ps && ps->pss_func == POOL_SCAN_SCRUB) {
2031 if (cmd == POOL_SCRUB_PAUSE)
2032 return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg));
2034 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
2036 return (zfs_error(hdl, EZFS_RESILVERING, msg));
2038 } else if (err == ENOENT) {
2039 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
2041 return (zpool_standard_error(hdl, err, msg));
2046 * Find a vdev that matches the search criteria specified. We use the
2047 * the nvpair name to determine how we should look for the device.
2048 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2049 * spare; but FALSE if its an INUSE spare.
2052 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
2053 boolean_t *l2cache, boolean_t *log)
2060 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
2062 /* Nothing to look for */
2063 if (search == NULL || pair == NULL)
2066 /* Obtain the key we will use to search */
2067 srchkey = nvpair_name(pair);
2069 switch (nvpair_type(pair)) {
2070 case DATA_TYPE_UINT64:
2071 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
2072 uint64_t srchval, theguid;
2074 verify(nvpair_value_uint64(pair, &srchval) == 0);
2075 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2077 if (theguid == srchval)
2082 case DATA_TYPE_STRING: {
2083 char *srchval, *val;
2085 verify(nvpair_value_string(pair, &srchval) == 0);
2086 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2090 * Search for the requested value. Special cases:
2092 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2093 * "-part1", or "p1". The suffix is hidden from the user,
2094 * but included in the string, so this matches around it.
2095 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2096 * is used to check all possible expanded paths.
2097 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2099 * Otherwise, all other searches are simple string compares.
2101 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) {
2102 uint64_t wholedisk = 0;
2104 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2106 if (zfs_strcmp_pathname(srchval, val, wholedisk) == 0)
2109 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2110 char *type, *idx, *end, *p;
2111 uint64_t id, vdev_id;
2114 * Determine our vdev type, keeping in mind
2115 * that the srchval is composed of a type and
2116 * vdev id pair (i.e. mirror-4).
2118 if ((type = strdup(srchval)) == NULL)
2121 if ((p = strrchr(type, '-')) == NULL) {
2129 * If the types don't match then keep looking.
2131 if (strncmp(val, type, strlen(val)) != 0) {
2136 verify(zpool_vdev_is_interior(type));
2137 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2141 vdev_id = strtoull(idx, &end, 10);
2148 * Now verify that we have the correct vdev id.
2157 if (strcmp(srchval, val) == 0)
2166 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2167 &child, &children) != 0)
2170 for (c = 0; c < children; c++) {
2171 if ((ret = vdev_to_nvlist_iter(child[c], search,
2172 avail_spare, l2cache, NULL)) != NULL) {
2174 * The 'is_log' value is only set for the toplevel
2175 * vdev, not the leaf vdevs. So we always lookup the
2176 * log device from the root of the vdev tree (where
2177 * 'log' is non-NULL).
2180 nvlist_lookup_uint64(child[c],
2181 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2189 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2190 &child, &children) == 0) {
2191 for (c = 0; c < children; c++) {
2192 if ((ret = vdev_to_nvlist_iter(child[c], search,
2193 avail_spare, l2cache, NULL)) != NULL) {
2194 *avail_spare = B_TRUE;
2200 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2201 &child, &children) == 0) {
2202 for (c = 0; c < children; c++) {
2203 if ((ret = vdev_to_nvlist_iter(child[c], search,
2204 avail_spare, l2cache, NULL)) != NULL) {
2215 * Given a physical path (minus the "/devices" prefix), find the
2219 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2220 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2222 nvlist_t *search, *nvroot, *ret;
2224 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2225 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2227 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2230 *avail_spare = B_FALSE;
2234 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2235 nvlist_free(search);
2241 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2244 zpool_vdev_is_interior(const char *name)
2246 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2247 strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 ||
2249 VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 ||
2250 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2256 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2257 boolean_t *l2cache, boolean_t *log)
2260 nvlist_t *nvroot, *search, *ret;
2263 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2265 guid = strtoull(path, &end, 0);
2266 if (guid != 0 && *end == '\0') {
2267 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2268 } else if (zpool_vdev_is_interior(path)) {
2269 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2271 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2274 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2277 *avail_spare = B_FALSE;
2281 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2282 nvlist_free(search);
2288 vdev_is_online(nvlist_t *nv)
2292 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2293 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2294 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2301 * Helper function for zpool_get_physpaths().
2304 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2305 size_t *bytes_written)
2307 size_t bytes_left, pos, rsz;
2311 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2313 return (EZFS_NODEVICE);
2315 pos = *bytes_written;
2316 bytes_left = physpath_size - pos;
2317 format = (pos == 0) ? "%s" : " %s";
2319 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2320 *bytes_written += rsz;
2322 if (rsz >= bytes_left) {
2323 /* if physpath was not copied properly, clear it */
2324 if (bytes_left != 0) {
2327 return (EZFS_NOSPC);
2333 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2334 size_t *rsz, boolean_t is_spare)
2339 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2340 return (EZFS_INVALCONFIG);
2342 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2344 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2345 * For a spare vdev, we only want to boot from the active
2350 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2353 return (EZFS_INVALCONFIG);
2356 if (vdev_is_online(nv)) {
2357 if ((ret = vdev_get_one_physpath(nv, physpath,
2358 phypath_size, rsz)) != 0)
2361 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2362 strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
2363 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2364 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2369 if (nvlist_lookup_nvlist_array(nv,
2370 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2371 return (EZFS_INVALCONFIG);
2373 for (i = 0; i < count; i++) {
2374 ret = vdev_get_physpaths(child[i], physpath,
2375 phypath_size, rsz, is_spare);
2376 if (ret == EZFS_NOSPC)
2381 return (EZFS_POOL_INVALARG);
2385 * Get phys_path for a root pool config.
2386 * Return 0 on success; non-zero on failure.
2389 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2392 nvlist_t *vdev_root;
2399 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2401 return (EZFS_INVALCONFIG);
2403 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2404 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2405 &child, &count) != 0)
2406 return (EZFS_INVALCONFIG);
2409 * root pool can only have a single top-level vdev.
2411 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2412 return (EZFS_POOL_INVALARG);
2414 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2417 /* No online devices */
2419 return (EZFS_NODEVICE);
2425 * Get phys_path for a root pool
2426 * Return 0 on success; non-zero on failure.
2429 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2431 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2436 * If the device has being dynamically expanded then we need to relabel
2437 * the disk to use the new unallocated space.
2440 zpool_relabel_disk(libzfs_handle_t *hdl, const char *path, const char *msg)
2444 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
2445 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2446 "relabel '%s': unable to open device: %d"), path, errno);
2447 return (zfs_error(hdl, EZFS_OPENFAILED, msg));
2451 * It's possible that we might encounter an error if the device
2452 * does not have any unallocated space left. If so, we simply
2453 * ignore that error and continue on.
2455 * Also, we don't call efi_rescan() - that would just return EBUSY.
2456 * The module will do it for us in vdev_disk_open().
2458 error = efi_use_whole_disk(fd);
2460 /* Flush the buffers to disk and invalidate the page cache. */
2462 (void) ioctl(fd, BLKFLSBUF);
2465 if (error && error != VT_ENOSPC) {
2466 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2467 "relabel '%s': unable to read disk capacity"), path);
2468 return (zfs_error(hdl, EZFS_NOCAP, msg));
2475 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
2477 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
2478 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
2482 zpool_vdev_path_to_guid_impl(zpool_handle_t *zhp, const char *path,
2483 boolean_t *is_spare, boolean_t *is_l2cache, boolean_t *is_log)
2486 boolean_t spare = B_FALSE, l2cache = B_FALSE, log = B_FALSE;
2489 if ((tgt = zpool_find_vdev(zhp, path, &spare, &l2cache,
2493 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &guid) == 0);
2494 if (is_spare != NULL)
2496 if (is_l2cache != NULL)
2497 *is_l2cache = l2cache;
2504 /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
2506 zpool_vdev_path_to_guid(zpool_handle_t *zhp, const char *path)
2508 return (zpool_vdev_path_to_guid_impl(zhp, path, NULL, NULL, NULL));
2512 * Bring the specified vdev online. The 'flags' parameter is a set of the
2513 * ZFS_ONLINE_* flags.
2516 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2517 vdev_state_t *newstate)
2519 zfs_cmd_t zc = {"\0"};
2523 boolean_t avail_spare, l2cache, islog;
2524 libzfs_handle_t *hdl = zhp->zpool_hdl;
2527 if (flags & ZFS_ONLINE_EXPAND) {
2528 (void) snprintf(msg, sizeof (msg),
2529 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2531 (void) snprintf(msg, sizeof (msg),
2532 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2535 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2536 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2538 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2540 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2543 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2545 if ((flags & ZFS_ONLINE_EXPAND ||
2546 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) &&
2547 nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) {
2548 uint64_t wholedisk = 0;
2550 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2554 * XXX - L2ARC 1.0 devices can't support expansion.
2557 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2558 "cannot expand cache devices"));
2559 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2563 const char *fullpath = path;
2564 char buf[MAXPATHLEN];
2566 if (path[0] != '/') {
2567 error = zfs_resolve_shortname(path, buf,
2570 return (zfs_error(hdl, EZFS_NODEVICE,
2576 error = zpool_relabel_disk(hdl, fullpath, msg);
2582 zc.zc_cookie = VDEV_STATE_ONLINE;
2585 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2586 if (errno == EINVAL) {
2587 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2588 "from this pool into a new one. Use '%s' "
2589 "instead"), "zpool detach");
2590 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2592 return (zpool_standard_error(hdl, errno, msg));
2595 *newstate = zc.zc_cookie;
2600 * Take the specified vdev offline
2603 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2605 zfs_cmd_t zc = {"\0"};
2608 boolean_t avail_spare, l2cache;
2609 libzfs_handle_t *hdl = zhp->zpool_hdl;
2611 (void) snprintf(msg, sizeof (msg),
2612 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2614 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2615 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2617 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2619 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2622 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2624 zc.zc_cookie = VDEV_STATE_OFFLINE;
2625 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2627 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2634 * There are no other replicas of this device.
2636 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2640 * The log device has unplayed logs
2642 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2645 return (zpool_standard_error(hdl, errno, msg));
2650 * Mark the given vdev faulted.
2653 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2655 zfs_cmd_t zc = {"\0"};
2657 libzfs_handle_t *hdl = zhp->zpool_hdl;
2659 (void) snprintf(msg, sizeof (msg),
2660 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
2662 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2664 zc.zc_cookie = VDEV_STATE_FAULTED;
2667 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2674 * There are no other replicas of this device.
2676 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2679 return (zpool_standard_error(hdl, errno, msg));
2685 * Mark the given vdev degraded.
2688 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2690 zfs_cmd_t zc = {"\0"};
2692 libzfs_handle_t *hdl = zhp->zpool_hdl;
2694 (void) snprintf(msg, sizeof (msg),
2695 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid);
2697 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2699 zc.zc_cookie = VDEV_STATE_DEGRADED;
2702 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2705 return (zpool_standard_error(hdl, errno, msg));
2709 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2713 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2719 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2721 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2724 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2725 children == 2 && child[which] == tgt)
2728 for (c = 0; c < children; c++)
2729 if (is_replacing_spare(child[c], tgt, which))
2737 * Attach new_disk (fully described by nvroot) to old_disk.
2738 * If 'replacing' is specified, the new disk will replace the old one.
2741 zpool_vdev_attach(zpool_handle_t *zhp,
2742 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2744 zfs_cmd_t zc = {"\0"};
2748 boolean_t avail_spare, l2cache, islog;
2753 nvlist_t *config_root;
2754 libzfs_handle_t *hdl = zhp->zpool_hdl;
2755 boolean_t rootpool = zpool_is_bootable(zhp);
2758 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2759 "cannot replace %s with %s"), old_disk, new_disk);
2761 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2762 "cannot attach %s to %s"), new_disk, old_disk);
2764 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2765 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2767 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2770 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2773 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2775 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2776 zc.zc_cookie = replacing;
2778 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2779 &child, &children) != 0 || children != 1) {
2780 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2781 "new device must be a single disk"));
2782 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2785 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2786 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2788 if ((newname = zpool_vdev_name(NULL, NULL, child[0], 0)) == NULL)
2792 * If the target is a hot spare that has been swapped in, we can only
2793 * replace it with another hot spare.
2796 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2797 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2798 NULL) == NULL || !avail_spare) &&
2799 is_replacing_spare(config_root, tgt, 1)) {
2800 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2801 "can only be replaced by another hot spare"));
2803 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2808 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2811 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2813 zcmd_free_nvlists(&zc);
2818 * XXX need a better way to prevent user from
2819 * booting up a half-baked vdev.
2821 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2822 "sure to wait until resilver is done "
2823 "before rebooting.\n"));
2831 * Can't attach to or replace this type of vdev.
2834 uint64_t version = zpool_get_prop_int(zhp,
2835 ZPOOL_PROP_VERSION, NULL);
2838 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2839 "cannot replace a log with a spare"));
2840 else if (version >= SPA_VERSION_MULTI_REPLACE)
2841 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2842 "already in replacing/spare config; wait "
2843 "for completion or use 'zpool detach'"));
2845 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2846 "cannot replace a replacing device"));
2848 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2849 "can only attach to mirrors and top-level "
2852 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2857 * The new device must be a single disk.
2859 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2860 "new device must be a single disk"));
2861 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2865 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2867 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2872 * The new device is too small.
2874 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2875 "device is too small"));
2876 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2881 * The new device has a different optimal sector size.
2883 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2884 "new device has a different optimal sector size; use the "
2885 "option '-o ashift=N' to override the optimal size"));
2886 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2891 * The resulting top-level vdev spec won't fit in the label.
2893 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2897 (void) zpool_standard_error(hdl, errno, msg);
2904 * Detach the specified device.
2907 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2909 zfs_cmd_t zc = {"\0"};
2912 boolean_t avail_spare, l2cache;
2913 libzfs_handle_t *hdl = zhp->zpool_hdl;
2915 (void) snprintf(msg, sizeof (msg),
2916 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2918 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2919 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2921 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2924 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2927 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2929 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2931 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2938 * Can't detach from this type of vdev.
2940 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2941 "applicable to mirror and replacing vdevs"));
2942 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2947 * There are no other replicas of this device.
2949 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2953 (void) zpool_standard_error(hdl, errno, msg);
2960 * Find a mirror vdev in the source nvlist.
2962 * The mchild array contains a list of disks in one of the top-level mirrors
2963 * of the source pool. The schild array contains a list of disks that the
2964 * user specified on the command line. We loop over the mchild array to
2965 * see if any entry in the schild array matches.
2967 * If a disk in the mchild array is found in the schild array, we return
2968 * the index of that entry. Otherwise we return -1.
2971 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2972 nvlist_t **schild, uint_t schildren)
2976 for (mc = 0; mc < mchildren; mc++) {
2978 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2981 for (sc = 0; sc < schildren; sc++) {
2982 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2984 boolean_t result = (strcmp(mpath, spath) == 0);
3000 * Split a mirror pool. If newroot points to null, then a new nvlist
3001 * is generated and it is the responsibility of the caller to free it.
3004 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
3005 nvlist_t *props, splitflags_t flags)
3007 zfs_cmd_t zc = {"\0"};
3009 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
3010 nvlist_t **varray = NULL, *zc_props = NULL;
3011 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
3012 libzfs_handle_t *hdl = zhp->zpool_hdl;
3014 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
3017 (void) snprintf(msg, sizeof (msg),
3018 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
3020 if (!zpool_name_valid(hdl, B_FALSE, newname))
3021 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
3023 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
3024 (void) fprintf(stderr, gettext("Internal error: unable to "
3025 "retrieve pool configuration\n"));
3029 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
3031 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
3034 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
3035 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
3036 props, vers, flags, msg)) == NULL)
3040 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3042 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3043 "Source pool is missing vdev tree"));
3044 nvlist_free(zc_props);
3048 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
3051 if (*newroot == NULL ||
3052 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
3053 &newchild, &newchildren) != 0)
3056 for (c = 0; c < children; c++) {
3057 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
3059 nvlist_t **mchild, *vdev;
3064 * Unlike cache & spares, slogs are stored in the
3065 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3067 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3069 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
3071 if (is_log || is_hole) {
3073 * Create a hole vdev and put it in the config.
3075 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
3077 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
3078 VDEV_TYPE_HOLE) != 0)
3080 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
3085 varray[vcount++] = vdev;
3089 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
3091 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
3092 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3093 "Source pool must be composed only of mirrors\n"));
3094 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3098 verify(nvlist_lookup_nvlist_array(child[c],
3099 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3101 /* find or add an entry for this top-level vdev */
3102 if (newchildren > 0 &&
3103 (entry = find_vdev_entry(zhp, mchild, mchildren,
3104 newchild, newchildren)) >= 0) {
3105 /* We found a disk that the user specified. */
3106 vdev = mchild[entry];
3109 /* User didn't specify a disk for this vdev. */
3110 vdev = mchild[mchildren - 1];
3113 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3117 /* did we find every disk the user specified? */
3118 if (found != newchildren) {
3119 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3120 "include at most one disk from each mirror"));
3121 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3125 /* Prepare the nvlist for populating. */
3126 if (*newroot == NULL) {
3127 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3130 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3131 VDEV_TYPE_ROOT) != 0)
3134 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3137 /* Add all the children we found */
3138 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3139 lastlog == 0 ? vcount : lastlog) != 0)
3143 * If we're just doing a dry run, exit now with success.
3146 memory_err = B_FALSE;
3151 /* now build up the config list & call the ioctl */
3152 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3155 if (nvlist_add_nvlist(newconfig,
3156 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3157 nvlist_add_string(newconfig,
3158 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3159 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3163 * The new pool is automatically part of the namespace unless we
3164 * explicitly export it.
3167 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3168 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3169 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3170 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3172 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3175 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3176 retval = zpool_standard_error(hdl, errno, msg);
3181 memory_err = B_FALSE;
3184 if (varray != NULL) {
3187 for (v = 0; v < vcount; v++)
3188 nvlist_free(varray[v]);
3191 zcmd_free_nvlists(&zc);
3192 nvlist_free(zc_props);
3193 nvlist_free(newconfig);
3195 nvlist_free(*newroot);
3203 return (no_memory(hdl));
3209 * Remove the given device. Currently, this is supported only for hot spares,
3210 * cache, and log devices.
3213 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3215 zfs_cmd_t zc = {"\0"};
3218 boolean_t avail_spare, l2cache, islog;
3219 libzfs_handle_t *hdl = zhp->zpool_hdl;
3222 (void) snprintf(msg, sizeof (msg),
3223 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3225 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3226 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3228 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3230 * XXX - this should just go away.
3232 if (!avail_spare && !l2cache && !islog) {
3233 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3234 "only inactive hot spares, cache, "
3235 "or log devices can be removed"));
3236 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3239 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3240 if (islog && version < SPA_VERSION_HOLES) {
3241 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3242 "pool must be upgrade to support log removal"));
3243 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3246 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3248 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3251 return (zpool_standard_error(hdl, errno, msg));
3255 * Clear the errors for the pool, or the particular device if specified.
3258 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3260 zfs_cmd_t zc = {"\0"};
3263 zpool_rewind_policy_t policy;
3264 boolean_t avail_spare, l2cache;
3265 libzfs_handle_t *hdl = zhp->zpool_hdl;
3266 nvlist_t *nvi = NULL;
3270 (void) snprintf(msg, sizeof (msg),
3271 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3274 (void) snprintf(msg, sizeof (msg),
3275 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3278 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3280 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3281 &l2cache, NULL)) == 0)
3282 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3285 * Don't allow error clearing for hot spares. Do allow
3286 * error clearing for l2cache devices.
3289 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3291 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3295 zpool_get_rewind_policy(rewindnvl, &policy);
3296 zc.zc_cookie = policy.zrp_request;
3298 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3301 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3304 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3306 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3307 zcmd_free_nvlists(&zc);
3312 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
3313 errno != EPERM && errno != EACCES)) {
3314 if (policy.zrp_request &
3315 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3316 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3317 zpool_rewind_exclaim(hdl, zc.zc_name,
3318 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
3322 zcmd_free_nvlists(&zc);
3326 zcmd_free_nvlists(&zc);
3327 return (zpool_standard_error(hdl, errno, msg));
3331 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3334 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3336 zfs_cmd_t zc = {"\0"};
3338 libzfs_handle_t *hdl = zhp->zpool_hdl;
3340 (void) snprintf(msg, sizeof (msg),
3341 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3342 (u_longlong_t)guid);
3344 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3346 zc.zc_cookie = ZPOOL_NO_REWIND;
3348 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3351 return (zpool_standard_error(hdl, errno, msg));
3355 * Change the GUID for a pool.
3358 zpool_reguid(zpool_handle_t *zhp)
3361 libzfs_handle_t *hdl = zhp->zpool_hdl;
3362 zfs_cmd_t zc = {"\0"};
3364 (void) snprintf(msg, sizeof (msg),
3365 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3367 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3368 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3371 return (zpool_standard_error(hdl, errno, msg));
3378 zpool_reopen_one(zpool_handle_t *zhp, void *data)
3380 libzfs_handle_t *hdl = zpool_get_handle(zhp);
3381 const char *pool_name = zpool_get_name(zhp);
3382 boolean_t *scrub_restart = data;
3385 error = lzc_reopen(pool_name, *scrub_restart);
3387 return (zpool_standard_error_fmt(hdl, error,
3388 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"), pool_name));
3394 /* call into libzfs_core to execute the sync IOCTL per pool */
3396 zpool_sync_one(zpool_handle_t *zhp, void *data)
3399 libzfs_handle_t *hdl = zpool_get_handle(zhp);
3400 const char *pool_name = zpool_get_name(zhp);
3401 boolean_t *force = data;
3402 nvlist_t *innvl = fnvlist_alloc();
3404 fnvlist_add_boolean_value(innvl, "force", *force);
3405 if ((ret = lzc_sync(pool_name, innvl, NULL)) != 0) {
3407 return (zpool_standard_error_fmt(hdl, ret,
3408 dgettext(TEXT_DOMAIN, "sync '%s' failed"), pool_name));
3415 #if defined(__sun__) || defined(__sun)
3417 * Convert from a devid string to a path.
3420 devid_to_path(char *devid_str)
3425 devid_nmlist_t *list = NULL;
3428 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3431 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3433 devid_str_free(minor);
3440 * In a case the strdup() fails, we will just return NULL below.
3442 path = strdup(list[0].devname);
3444 devid_free_nmlist(list);
3450 * Convert from a path to a devid string.
3453 path_to_devid(const char *path)
3459 if ((fd = open(path, O_RDONLY)) < 0)
3464 if (devid_get(fd, &devid) == 0) {
3465 if (devid_get_minor_name(fd, &minor) == 0)
3466 ret = devid_str_encode(devid, minor);
3468 devid_str_free(minor);
3477 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3478 * ignore any failure here, since a common case is for an unprivileged user to
3479 * type 'zpool status', and we'll display the correct information anyway.
3482 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3484 zfs_cmd_t zc = {"\0"};
3486 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3487 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3488 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3491 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3496 * Remove partition suffix from a vdev path. Partition suffixes may take three
3497 * forms: "-partX", "pX", or "X", where X is a string of digits. The second
3498 * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
3499 * third case only occurs when preceded by a string matching the regular
3500 * expression "^([hsv]|xv)d[a-z]+", i.e. a scsi, ide, virtio or xen disk.
3502 * caller must free the returned string
3505 zfs_strip_partition(char *path)
3507 char *tmp = strdup(path);
3508 char *part = NULL, *d = NULL;
3512 if ((part = strstr(tmp, "-part")) && part != tmp) {
3514 } else if ((part = strrchr(tmp, 'p')) &&
3515 part > tmp + 1 && isdigit(*(part-1))) {
3517 } else if ((tmp[0] == 'h' || tmp[0] == 's' || tmp[0] == 'v') &&
3519 for (d = &tmp[2]; isalpha(*d); part = ++d) { }
3520 } else if (strncmp("xvd", tmp, 3) == 0) {
3521 for (d = &tmp[3]; isalpha(*d); part = ++d) { }
3523 if (part && d && *d != '\0') {
3524 for (; isdigit(*d); d++) { }
3533 * Same as zfs_strip_partition, but allows "/dev/" to be in the pathname
3538 * Returned string must be freed.
3541 zfs_strip_partition_path(char *path)
3543 char *newpath = strdup(path);
3550 /* Point to "sda1" part of "/dev/sda1" */
3551 sd_offset = strrchr(newpath, '/') + 1;
3553 /* Get our new name "sda" */
3554 new_sd = zfs_strip_partition(sd_offset);
3560 /* Paste the "sda" where "sda1" was */
3561 strlcpy(sd_offset, new_sd, strlen(sd_offset) + 1);
3563 /* Free temporary "sda" */
3569 #define PATH_BUF_LEN 64
3572 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3573 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3574 * We also check if this is a whole disk, in which case we strip off the
3575 * trailing 's0' slice name.
3577 * This routine is also responsible for identifying when disks have been
3578 * reconfigured in a new location. The kernel will have opened the device by
3579 * devid, but the path will still refer to the old location. To catch this, we
3580 * first do a path -> devid translation (which is fast for the common case). If
3581 * the devid matches, we're done. If not, we do a reverse devid -> path
3582 * translation and issue the appropriate ioctl() to update the path of the vdev.
3583 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3587 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3590 char *path, *type, *env;
3592 char buf[PATH_BUF_LEN];
3593 char tmpbuf[PATH_BUF_LEN];
3596 * vdev_name will be "root"/"root-0" for the root vdev, but it is the
3597 * zpool name that will be displayed to the user.
3599 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
3600 if (zhp != NULL && strcmp(type, "root") == 0)
3601 return (zfs_strdup(hdl, zpool_get_name(zhp)));
3603 env = getenv("ZPOOL_VDEV_NAME_PATH");
3604 if (env && (strtoul(env, NULL, 0) > 0 ||
3605 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3606 name_flags |= VDEV_NAME_PATH;
3608 env = getenv("ZPOOL_VDEV_NAME_GUID");
3609 if (env && (strtoul(env, NULL, 0) > 0 ||
3610 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3611 name_flags |= VDEV_NAME_GUID;
3613 env = getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
3614 if (env && (strtoul(env, NULL, 0) > 0 ||
3615 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3616 name_flags |= VDEV_NAME_FOLLOW_LINKS;
3618 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3619 name_flags & VDEV_NAME_GUID) {
3620 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value);
3621 (void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)value);
3623 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3624 #if defined(__sun__) || defined(__sun)
3626 * Live VDEV path updates to a kernel VDEV during a
3627 * zpool_vdev_name lookup are not supported on Linux.
3634 * If the device is dead (faulted, offline, etc) then don't
3635 * bother opening it. Otherwise we may be forcing the user to
3636 * open a misbehaving device, which can have undesirable
3639 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3640 (uint64_t **)&vs, &vsc) != 0 ||
3641 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3643 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3645 * Determine if the current path is correct.
3647 char *newdevid = path_to_devid(path);
3649 if (newdevid == NULL ||
3650 strcmp(devid, newdevid) != 0) {
3653 if ((newpath = devid_to_path(devid)) != NULL) {
3655 * Update the path appropriately.
3657 set_path(zhp, nv, newpath);
3658 if (nvlist_add_string(nv,
3659 ZPOOL_CONFIG_PATH, newpath) == 0)
3660 verify(nvlist_lookup_string(nv,
3668 devid_str_free(newdevid);
3672 if (name_flags & VDEV_NAME_FOLLOW_LINKS) {
3673 char *rp = realpath(path, NULL);
3675 strlcpy(buf, rp, sizeof (buf));
3682 * For a block device only use the name.
3684 if ((strcmp(type, VDEV_TYPE_DISK) == 0) &&
3685 !(name_flags & VDEV_NAME_PATH)) {
3686 path = strrchr(path, '/');
3691 * Remove the partition from the path it this is a whole disk.
3693 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, &value)
3694 == 0 && value && !(name_flags & VDEV_NAME_PATH)) {
3695 return (zfs_strip_partition(path));
3701 * If it's a raidz device, we need to stick in the parity level.
3703 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3704 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3706 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3707 (u_longlong_t)value);
3712 * We identify each top-level vdev by using a <type-id>
3713 * naming convention.
3715 if (name_flags & VDEV_NAME_TYPE_ID) {
3717 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3719 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s-%llu",
3720 path, (u_longlong_t)id);
3725 return (zfs_strdup(hdl, path));
3729 zbookmark_mem_compare(const void *a, const void *b)
3731 return (memcmp(a, b, sizeof (zbookmark_phys_t)));
3735 * Retrieve the persistent error log, uniquify the members, and return to the
3739 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3741 zfs_cmd_t zc = {"\0"};
3742 libzfs_handle_t *hdl = zhp->zpool_hdl;
3744 zbookmark_phys_t *zb = NULL;
3748 * Retrieve the raw error list from the kernel. If the number of errors
3749 * has increased, allocate more space and continue until we get the
3752 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3756 zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3757 count * sizeof (zbookmark_phys_t));
3758 zc.zc_nvlist_dst_size = count;
3759 (void) strcpy(zc.zc_name, zhp->zpool_name);
3761 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3763 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3764 if (errno == ENOMEM) {
3767 count = zc.zc_nvlist_dst_size;
3768 dst = zfs_alloc(zhp->zpool_hdl, count *
3769 sizeof (zbookmark_phys_t));
3770 zc.zc_nvlist_dst = (uintptr_t)dst;
3772 return (zpool_standard_error_fmt(hdl, errno,
3773 dgettext(TEXT_DOMAIN, "errors: List of "
3774 "errors unavailable")));
3782 * Sort the resulting bookmarks. This is a little confusing due to the
3783 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3784 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3785 * _not_ copied as part of the process. So we point the start of our
3786 * array appropriate and decrement the total number of elements.
3788 zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
3789 zc.zc_nvlist_dst_size;
3790 count -= zc.zc_nvlist_dst_size;
3792 qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
3794 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3797 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3799 for (i = 0; i < count; i++) {
3802 /* ignoring zb_blkid and zb_level for now */
3803 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3804 zb[i-1].zb_object == zb[i].zb_object)
3807 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3809 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3810 zb[i].zb_objset) != 0) {
3814 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3815 zb[i].zb_object) != 0) {
3819 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3826 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3830 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3831 return (no_memory(zhp->zpool_hdl));
3835 * Upgrade a ZFS pool to the latest on-disk version.
3838 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3840 zfs_cmd_t zc = {"\0"};
3841 libzfs_handle_t *hdl = zhp->zpool_hdl;
3843 (void) strcpy(zc.zc_name, zhp->zpool_name);
3844 zc.zc_cookie = new_version;
3846 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3847 return (zpool_standard_error_fmt(hdl, errno,
3848 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3854 zfs_save_arguments(int argc, char **argv, char *string, int len)
3858 (void) strlcpy(string, basename(argv[0]), len);
3859 for (i = 1; i < argc; i++) {
3860 (void) strlcat(string, " ", len);
3861 (void) strlcat(string, argv[i], len);
3866 zpool_log_history(libzfs_handle_t *hdl, const char *message)
3868 zfs_cmd_t zc = {"\0"};
3872 args = fnvlist_alloc();
3873 fnvlist_add_string(args, "message", message);
3874 err = zcmd_write_src_nvlist(hdl, &zc, args);
3876 err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
3878 zcmd_free_nvlists(&zc);
3883 * Perform ioctl to get some command history of a pool.
3885 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3886 * logical offset of the history buffer to start reading from.
3888 * Upon return, 'off' is the next logical offset to read from and
3889 * 'len' is the actual amount of bytes read into 'buf'.
3892 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3894 zfs_cmd_t zc = {"\0"};
3895 libzfs_handle_t *hdl = zhp->zpool_hdl;
3897 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3899 zc.zc_history = (uint64_t)(uintptr_t)buf;
3900 zc.zc_history_len = *len;
3901 zc.zc_history_offset = *off;
3903 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3906 return (zfs_error_fmt(hdl, EZFS_PERM,
3907 dgettext(TEXT_DOMAIN,
3908 "cannot show history for pool '%s'"),
3911 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3912 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3913 "'%s'"), zhp->zpool_name));
3915 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3916 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3917 "'%s', pool must be upgraded"), zhp->zpool_name));
3919 return (zpool_standard_error_fmt(hdl, errno,
3920 dgettext(TEXT_DOMAIN,
3921 "cannot get history for '%s'"), zhp->zpool_name));
3925 *len = zc.zc_history_len;
3926 *off = zc.zc_history_offset;
3932 * Process the buffer of nvlists, unpacking and storing each nvlist record
3933 * into 'records'. 'leftover' is set to the number of bytes that weren't
3934 * processed as there wasn't a complete record.
3937 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3938 nvlist_t ***records, uint_t *numrecords)
3945 while (bytes_read > sizeof (reclen)) {
3947 /* get length of packed record (stored as little endian) */
3948 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3949 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3951 if (bytes_read < sizeof (reclen) + reclen)
3955 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3957 bytes_read -= sizeof (reclen) + reclen;
3958 buf += sizeof (reclen) + reclen;
3960 /* add record to nvlist array */
3962 if (ISP2(*numrecords + 1)) {
3963 tmp = realloc(*records,
3964 *numrecords * 2 * sizeof (nvlist_t *));
3972 (*records)[*numrecords - 1] = nv;
3975 *leftover = bytes_read;
3980 * Retrieve the command history of a pool.
3983 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3986 int buflen = 128 * 1024;
3988 nvlist_t **records = NULL;
3989 uint_t numrecords = 0;
3992 buf = malloc(buflen);
3996 uint64_t bytes_read = buflen;
3999 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
4002 /* if nothing else was read in, we're at EOF, just return */
4006 if ((err = zpool_history_unpack(buf, bytes_read,
4007 &leftover, &records, &numrecords)) != 0)
4010 if (leftover == bytes_read) {
4012 * no progress made, because buffer is not big enough
4013 * to hold this record; resize and retry.
4017 buf = malloc(buflen);
4028 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
4029 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
4030 records, numrecords) == 0);
4032 for (i = 0; i < numrecords; i++)
4033 nvlist_free(records[i]);
4040 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
4041 * If there is a new event available 'nvp' will contain a newly allocated
4042 * nvlist and 'dropped' will be set to the number of missed events since
4043 * the last call to this function. When 'nvp' is set to NULL it indicates
4044 * no new events are available. In either case the function returns 0 and
4045 * it is up to the caller to free 'nvp'. In the case of a fatal error the
4046 * function will return a non-zero value. When the function is called in
4047 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
4048 * it will not return until a new event is available.
4051 zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
4052 int *dropped, unsigned flags, int zevent_fd)
4054 zfs_cmd_t zc = {"\0"};
4059 zc.zc_cleanup_fd = zevent_fd;
4061 if (flags & ZEVENT_NONBLOCK)
4062 zc.zc_guid = ZEVENT_NONBLOCK;
4064 if (zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE) != 0)
4068 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
4071 error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
4072 dgettext(TEXT_DOMAIN, "zfs shutdown"));
4075 /* Blocking error case should not occur */
4076 if (!(flags & ZEVENT_NONBLOCK))
4077 error = zpool_standard_error_fmt(hdl, errno,
4078 dgettext(TEXT_DOMAIN, "cannot get event"));
4082 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
4083 error = zfs_error_fmt(hdl, EZFS_NOMEM,
4084 dgettext(TEXT_DOMAIN, "cannot get event"));
4090 error = zpool_standard_error_fmt(hdl, errno,
4091 dgettext(TEXT_DOMAIN, "cannot get event"));
4096 error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
4100 *dropped = (int)zc.zc_cookie;
4102 zcmd_free_nvlists(&zc);
4111 zpool_events_clear(libzfs_handle_t *hdl, int *count)
4113 zfs_cmd_t zc = {"\0"};
4116 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
4117 "cannot clear events"));
4119 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
4120 return (zpool_standard_error_fmt(hdl, errno, msg));
4123 *count = (int)zc.zc_cookie; /* # of events cleared */
4129 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4130 * the passed zevent_fd file handle. On success zero is returned,
4131 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4134 zpool_events_seek(libzfs_handle_t *hdl, uint64_t eid, int zevent_fd)
4136 zfs_cmd_t zc = {"\0"};
4140 zc.zc_cleanup_fd = zevent_fd;
4142 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_SEEK, &zc) != 0) {
4145 error = zfs_error_fmt(hdl, EZFS_NOENT,
4146 dgettext(TEXT_DOMAIN, "cannot get event"));
4150 error = zfs_error_fmt(hdl, EZFS_NOMEM,
4151 dgettext(TEXT_DOMAIN, "cannot get event"));
4155 error = zpool_standard_error_fmt(hdl, errno,
4156 dgettext(TEXT_DOMAIN, "cannot get event"));
4165 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4166 char *pathname, size_t len)
4168 zfs_cmd_t zc = {"\0"};
4169 boolean_t mounted = B_FALSE;
4170 char *mntpnt = NULL;
4171 char dsname[ZFS_MAX_DATASET_NAME_LEN];
4174 /* special case for the MOS */
4175 (void) snprintf(pathname, len, "<metadata>:<0x%llx>",
4180 /* get the dataset's name */
4181 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4183 if (ioctl(zhp->zpool_hdl->libzfs_fd,
4184 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
4185 /* just write out a path of two object numbers */
4186 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
4187 (longlong_t)dsobj, (longlong_t)obj);
4190 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
4192 /* find out if the dataset is mounted */
4193 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
4195 /* get the corrupted object's path */
4196 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
4198 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
4201 (void) snprintf(pathname, len, "%s%s", mntpnt,
4204 (void) snprintf(pathname, len, "%s:%s",
4205 dsname, zc.zc_value);
4208 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname,
4215 * Read the EFI label from the config, if a label does not exist then
4216 * pass back the error to the caller. If the caller has passed a non-NULL
4217 * diskaddr argument then we set it to the starting address of the EFI
4221 read_efi_label(nvlist_t *config, diskaddr_t *sb)
4225 char diskname[MAXPATHLEN];
4228 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
4231 (void) snprintf(diskname, sizeof (diskname), "%s%s", DISK_ROOT,
4232 strrchr(path, '/'));
4233 if ((fd = open(diskname, O_RDONLY|O_DIRECT)) >= 0) {
4234 struct dk_gpt *vtoc;
4236 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
4238 *sb = vtoc->efi_parts[0].p_start;
4247 * determine where a partition starts on a disk in the current
4251 find_start_block(nvlist_t *config)
4255 diskaddr_t sb = MAXOFFSET_T;
4258 if (nvlist_lookup_nvlist_array(config,
4259 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
4260 if (nvlist_lookup_uint64(config,
4261 ZPOOL_CONFIG_WHOLE_DISK,
4262 &wholedisk) != 0 || !wholedisk) {
4263 return (MAXOFFSET_T);
4265 if (read_efi_label(config, &sb) < 0)
4270 for (c = 0; c < children; c++) {
4271 sb = find_start_block(child[c]);
4272 if (sb != MAXOFFSET_T) {
4276 return (MAXOFFSET_T);
4280 zpool_label_disk_check(char *path)
4282 struct dk_gpt *vtoc;
4285 if ((fd = open(path, O_RDONLY|O_DIRECT)) < 0)
4288 if ((err = efi_alloc_and_read(fd, &vtoc)) != 0) {
4293 if (vtoc->efi_flags & EFI_GPT_PRIMARY_CORRUPT) {
4305 * Generate a unique partition name for the ZFS member. Partitions must
4306 * have unique names to ensure udev will be able to create symlinks under
4307 * /dev/disk/by-partlabel/ for all pool members. The partition names are
4308 * of the form <pool>-<unique-id>.
4311 zpool_label_name(char *label_name, int label_size)
4316 fd = open("/dev/urandom", O_RDONLY);
4318 if (read(fd, &id, sizeof (id)) != sizeof (id))
4325 id = (((uint64_t)rand()) << 32) | (uint64_t)rand();
4327 snprintf(label_name, label_size, "zfs-%016llx", (u_longlong_t)id);
4331 * Label an individual disk. The name provided is the short name,
4332 * stripped of any leading /dev path.
4335 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
4337 char path[MAXPATHLEN];
4338 struct dk_gpt *vtoc;
4340 size_t resv = EFI_MIN_RESV_SIZE;
4341 uint64_t slice_size;
4342 diskaddr_t start_block;
4345 /* prepare an error message just in case */
4346 (void) snprintf(errbuf, sizeof (errbuf),
4347 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
4352 verify(nvlist_lookup_nvlist(zhp->zpool_config,
4353 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4355 if (zhp->zpool_start_block == 0)
4356 start_block = find_start_block(nvroot);
4358 start_block = zhp->zpool_start_block;
4359 zhp->zpool_start_block = start_block;
4362 start_block = NEW_START_BLOCK;
4365 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
4367 if ((fd = open(path, O_RDWR|O_DIRECT|O_EXCL)) < 0) {
4369 * This shouldn't happen. We've long since verified that this
4370 * is a valid device.
4372 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
4373 "label '%s': unable to open device: %d"), path, errno);
4374 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
4377 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
4379 * The only way this can fail is if we run out of memory, or we
4380 * were unable to read the disk's capacity
4382 if (errno == ENOMEM)
4383 (void) no_memory(hdl);
4386 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
4387 "label '%s': unable to read disk capacity"), path);
4389 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
4392 slice_size = vtoc->efi_last_u_lba + 1;
4393 slice_size -= EFI_MIN_RESV_SIZE;
4394 if (start_block == MAXOFFSET_T)
4395 start_block = NEW_START_BLOCK;
4396 slice_size -= start_block;
4397 slice_size = P2ALIGN(slice_size, PARTITION_END_ALIGNMENT);
4399 vtoc->efi_parts[0].p_start = start_block;
4400 vtoc->efi_parts[0].p_size = slice_size;
4403 * Why we use V_USR: V_BACKUP confuses users, and is considered
4404 * disposable by some EFI utilities (since EFI doesn't have a backup
4405 * slice). V_UNASSIGNED is supposed to be used only for zero size
4406 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4407 * etc. were all pretty specific. V_USR is as close to reality as we
4408 * can get, in the absence of V_OTHER.
4410 vtoc->efi_parts[0].p_tag = V_USR;
4411 zpool_label_name(vtoc->efi_parts[0].p_name, EFI_PART_NAME_LEN);
4413 vtoc->efi_parts[8].p_start = slice_size + start_block;
4414 vtoc->efi_parts[8].p_size = resv;
4415 vtoc->efi_parts[8].p_tag = V_RESERVED;
4417 rval = efi_write(fd, vtoc);
4419 /* Flush the buffers to disk and invalidate the page cache. */
4421 (void) ioctl(fd, BLKFLSBUF);
4424 rval = efi_rescan(fd);
4427 * Some block drivers (like pcata) may not support EFI GPT labels.
4428 * Print out a helpful error message directing the user to manually
4429 * label the disk and give a specific slice.
4435 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "try using "
4436 "parted(8) and then provide a specific slice: %d"), rval);
4437 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4443 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
4444 (void) zfs_append_partition(path, MAXPATHLEN);
4446 /* Wait to udev to signal use the device has settled. */
4447 rval = zpool_label_disk_wait(path, DISK_LABEL_WAIT);
4449 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "failed to "
4450 "detect device partitions on '%s': %d"), path, rval);
4451 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4454 /* We can't be to paranoid. Read the label back and verify it. */
4455 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
4456 rval = zpool_label_disk_check(path);
4458 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "freshly written "
4459 "EFI label on '%s' is damaged. Ensure\nthis device "
4460 "is not in in use, and is functioning properly: %d"),
4462 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4469 * Allocate and return the underlying device name for a device mapper device.
4470 * If a device mapper device maps to multiple devices, return the first device.
4472 * For example, dm_name = "/dev/dm-0" could return "/dev/sda". Symlinks to a
4473 * DM device (like /dev/disk/by-vdev/A0) are also allowed.
4475 * Returns device name, or NULL on error or no match. If dm_name is not a DM
4476 * device then return NULL.
4478 * NOTE: The returned name string must be *freed*.
4481 dm_get_underlying_path(char *dm_name)
4491 if (dm_name == NULL)
4494 /* dm name may be a symlink (like /dev/disk/by-vdev/A0) */
4495 realp = realpath(dm_name, NULL);
4500 * If they preface 'dev' with a path (like "/dev") then strip it off.
4501 * We just want the 'dm-N' part.
4503 tmp = strrchr(realp, '/');
4505 dev_str = tmp + 1; /* +1 since we want the chr after '/' */
4509 size = asprintf(&tmp, "/sys/block/%s/slaves/", dev_str);
4510 if (size == -1 || !tmp)
4517 /* Return first sd* entry in /sys/block/dm-N/slaves/ */
4518 while ((ep = readdir(dp))) {
4519 if (ep->d_type != DT_DIR) { /* skip "." and ".." dirs */
4520 size = asprintf(&path, "/dev/%s", ep->d_name);
4534 * Return 1 if device is a device mapper or multipath device.
4538 zfs_dev_is_dm(char *dev_name)
4542 tmp = dm_get_underlying_path(dev_name);
4551 * By "whole disk" we mean an entire physical disk (something we can
4552 * label, toggle the write cache on, etc.) as opposed to the full
4553 * capacity of a pseudo-device such as lofi or did. We act as if we
4554 * are labeling the disk, which should be a pretty good test of whether
4555 * it's a viable device or not. Returns B_TRUE if it is and B_FALSE if
4559 zfs_dev_is_whole_disk(char *dev_name)
4561 struct dk_gpt *label;
4564 if ((fd = open(dev_name, O_RDONLY | O_DIRECT)) < 0)
4567 if (efi_alloc_and_init(fd, EFI_NUMPAR, &label) != 0) {
4579 * Lookup the underlying device for a device name
4581 * Often you'll have a symlink to a device, a partition device,
4582 * or a multipath device, and want to look up the underlying device.
4583 * This function returns the underlying device name. If the device
4584 * name is already the underlying device, then just return the same
4585 * name. If the device is a DM device with multiple underlying devices
4586 * then return the first one.
4590 * 1. /dev/disk/by-id/ata-QEMU_HARDDISK_QM00001 -> ../../sda
4591 * dev_name: /dev/disk/by-id/ata-QEMU_HARDDISK_QM00001
4594 * 2. /dev/mapper/mpatha (made up of /dev/sda and /dev/sdb)
4595 * dev_name: /dev/mapper/mpatha
4596 * returns: /dev/sda (first device)
4598 * 3. /dev/sda (already the underlying device)
4599 * dev_name: /dev/sda
4602 * 4. /dev/dm-3 (mapped to /dev/sda)
4603 * dev_name: /dev/dm-3
4606 * 5. /dev/disk/by-id/scsi-0QEMU_drive-scsi0-0-0-0-part9 -> ../../sdb9
4607 * dev_name: /dev/disk/by-id/scsi-0QEMU_drive-scsi0-0-0-0-part9
4610 * 6. /dev/disk/by-uuid/5df030cf-3cd9-46e4-8e99-3ccb462a4e9a -> ../dev/sda2
4611 * dev_name: /dev/disk/by-uuid/5df030cf-3cd9-46e4-8e99-3ccb462a4e9a
4614 * Returns underlying device name, or NULL on error or no match.
4616 * NOTE: The returned name string must be *freed*.
4619 zfs_get_underlying_path(char *dev_name)
4624 if (dev_name == NULL)
4627 tmp = dm_get_underlying_path(dev_name);
4629 /* dev_name not a DM device, so just un-symlinkize it */
4631 tmp = realpath(dev_name, NULL);
4634 name = zfs_strip_partition_path(tmp);
4642 * Given a dev name like "sda", return the full enclosure sysfs path to
4643 * the disk. You can also pass in the name with "/dev" prepended
4644 * to it (like /dev/sda).
4646 * For example, disk "sda" in enclosure slot 1:
4648 * returns: "/sys/class/enclosure/1:0:3:0/Slot 1"
4650 * 'dev' must be a non-devicemapper device.
4652 * Returned string must be freed.
4655 zfs_get_enclosure_sysfs_path(char *dev_name)
4659 char buf[MAXPATHLEN];
4667 if (dev_name == NULL)
4670 /* If they preface 'dev' with a path (like "/dev") then strip it off */
4671 tmp1 = strrchr(dev_name, '/');
4673 dev_name = tmp1 + 1; /* +1 since we want the chr after '/' */
4675 tmpsize = asprintf(&tmp1, "/sys/block/%s/device", dev_name);
4676 if (tmpsize == -1 || tmp1 == NULL) {
4683 tmp1 = NULL; /* To make free() at the end a NOP */
4688 * Look though all sysfs entries in /sys/block/<dev>/device for
4689 * the enclosure symlink.
4691 while ((ep = readdir(dp))) {
4692 /* Ignore everything that's not our enclosure_device link */
4693 if (strstr(ep->d_name, "enclosure_device") == NULL)
4696 if (asprintf(&tmp2, "%s/%s", tmp1, ep->d_name) == -1 ||
4700 size = readlink(tmp2, buf, sizeof (buf));
4702 /* Did readlink fail or crop the link name? */
4703 if (size == -1 || size >= sizeof (buf)) {
4705 tmp2 = NULL; /* To make free() at the end a NOP */
4710 * We got a valid link. readlink() doesn't terminate strings
4711 * so we have to do it.
4716 * Our link will look like:
4718 * "../../../../port-11:1:2/..STUFF../enclosure/1:0:3:0/SLOT 1"
4720 * We want to grab the "enclosure/1:0:3:0/SLOT 1" part
4722 tmp3 = strstr(buf, "enclosure");
4726 if (asprintf(&path, "/sys/class/%s", tmp3) == -1) {
4727 /* If asprintf() fails, 'path' is undefined */