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, 2018 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>
47 #include <sys/vdev_disk.h>
50 #include "zfs_namecheck.h"
52 #include "libzfs_impl.h"
53 #include "zfs_comutil.h"
54 #include "zfeature_common.h"
56 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
57 static boolean_t zpool_vdev_is_interior(const char *name);
59 typedef struct prop_flags {
60 int create:1; /* Validate property on creation */
61 int import:1; /* Validate property on import */
65 * ====================================================================
66 * zpool property functions
67 * ====================================================================
71 zpool_get_all_props(zpool_handle_t *zhp)
73 zfs_cmd_t zc = {"\0"};
74 libzfs_handle_t *hdl = zhp->zpool_hdl;
76 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
78 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
81 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
82 if (errno == ENOMEM) {
83 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
84 zcmd_free_nvlists(&zc);
88 zcmd_free_nvlists(&zc);
93 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
94 zcmd_free_nvlists(&zc);
98 zcmd_free_nvlists(&zc);
104 zpool_props_refresh(zpool_handle_t *zhp)
108 old_props = zhp->zpool_props;
110 if (zpool_get_all_props(zhp) != 0)
113 nvlist_free(old_props);
118 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
124 zprop_source_t source;
126 nvl = zhp->zpool_props;
127 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
128 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
130 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
132 source = ZPROP_SRC_DEFAULT;
133 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
144 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
148 zprop_source_t source;
150 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
152 * zpool_get_all_props() has most likely failed because
153 * the pool is faulted, but if all we need is the top level
154 * vdev's guid then get it from the zhp config nvlist.
156 if ((prop == ZPOOL_PROP_GUID) &&
157 (nvlist_lookup_nvlist(zhp->zpool_config,
158 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
159 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
163 return (zpool_prop_default_numeric(prop));
166 nvl = zhp->zpool_props;
167 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
168 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
170 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
172 source = ZPROP_SRC_DEFAULT;
173 value = zpool_prop_default_numeric(prop);
183 * Map VDEV STATE to printed strings.
186 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
189 case VDEV_STATE_CLOSED:
190 case VDEV_STATE_OFFLINE:
191 return (gettext("OFFLINE"));
192 case VDEV_STATE_REMOVED:
193 return (gettext("REMOVED"));
194 case VDEV_STATE_CANT_OPEN:
195 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
196 return (gettext("FAULTED"));
197 else if (aux == VDEV_AUX_SPLIT_POOL)
198 return (gettext("SPLIT"));
200 return (gettext("UNAVAIL"));
201 case VDEV_STATE_FAULTED:
202 return (gettext("FAULTED"));
203 case VDEV_STATE_DEGRADED:
204 return (gettext("DEGRADED"));
205 case VDEV_STATE_HEALTHY:
206 return (gettext("ONLINE"));
212 return (gettext("UNKNOWN"));
216 * Map POOL STATE to printed strings.
219 zpool_pool_state_to_name(pool_state_t state)
224 case POOL_STATE_ACTIVE:
225 return (gettext("ACTIVE"));
226 case POOL_STATE_EXPORTED:
227 return (gettext("EXPORTED"));
228 case POOL_STATE_DESTROYED:
229 return (gettext("DESTROYED"));
230 case POOL_STATE_SPARE:
231 return (gettext("SPARE"));
232 case POOL_STATE_L2CACHE:
233 return (gettext("L2CACHE"));
234 case POOL_STATE_UNINITIALIZED:
235 return (gettext("UNINITIALIZED"));
236 case POOL_STATE_UNAVAIL:
237 return (gettext("UNAVAIL"));
238 case POOL_STATE_POTENTIALLY_ACTIVE:
239 return (gettext("POTENTIALLY_ACTIVE"));
242 return (gettext("UNKNOWN"));
246 * Get a zpool property value for 'prop' and return the value in
247 * a pre-allocated buffer.
250 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf,
251 size_t len, zprop_source_t *srctype, boolean_t literal)
255 zprop_source_t src = ZPROP_SRC_NONE;
260 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
262 case ZPOOL_PROP_NAME:
263 (void) strlcpy(buf, zpool_get_name(zhp), len);
266 case ZPOOL_PROP_HEALTH:
267 (void) strlcpy(buf, "FAULTED", len);
270 case ZPOOL_PROP_GUID:
271 intval = zpool_get_prop_int(zhp, prop, &src);
272 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
275 case ZPOOL_PROP_ALTROOT:
276 case ZPOOL_PROP_CACHEFILE:
277 case ZPOOL_PROP_COMMENT:
278 if (zhp->zpool_props != NULL ||
279 zpool_get_all_props(zhp) == 0) {
281 zpool_get_prop_string(zhp, prop, &src),
287 (void) strlcpy(buf, "-", len);
296 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
297 prop != ZPOOL_PROP_NAME)
300 switch (zpool_prop_get_type(prop)) {
301 case PROP_TYPE_STRING:
302 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
306 case PROP_TYPE_NUMBER:
307 intval = zpool_get_prop_int(zhp, prop, &src);
310 case ZPOOL_PROP_SIZE:
311 case ZPOOL_PROP_ALLOCATED:
312 case ZPOOL_PROP_FREE:
313 case ZPOOL_PROP_FREEING:
314 case ZPOOL_PROP_LEAKED:
315 case ZPOOL_PROP_ASHIFT:
317 (void) snprintf(buf, len, "%llu",
318 (u_longlong_t)intval);
320 (void) zfs_nicenum(intval, buf, len);
323 case ZPOOL_PROP_EXPANDSZ:
325 (void) strlcpy(buf, "-", len);
326 } else if (literal) {
327 (void) snprintf(buf, len, "%llu",
328 (u_longlong_t)intval);
330 (void) zfs_nicebytes(intval, buf, len);
334 case ZPOOL_PROP_CAPACITY:
336 (void) snprintf(buf, len, "%llu",
337 (u_longlong_t)intval);
339 (void) snprintf(buf, len, "%llu%%",
340 (u_longlong_t)intval);
344 case ZPOOL_PROP_FRAGMENTATION:
345 if (intval == UINT64_MAX) {
346 (void) strlcpy(buf, "-", len);
347 } else if (literal) {
348 (void) snprintf(buf, len, "%llu",
349 (u_longlong_t)intval);
351 (void) snprintf(buf, len, "%llu%%",
352 (u_longlong_t)intval);
356 case ZPOOL_PROP_DEDUPRATIO:
358 (void) snprintf(buf, len, "%llu.%02llu",
359 (u_longlong_t)(intval / 100),
360 (u_longlong_t)(intval % 100));
362 (void) snprintf(buf, len, "%llu.%02llux",
363 (u_longlong_t)(intval / 100),
364 (u_longlong_t)(intval % 100));
367 case ZPOOL_PROP_HEALTH:
368 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
369 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
370 verify(nvlist_lookup_uint64_array(nvroot,
371 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
374 (void) strlcpy(buf, zpool_state_to_name(intval,
377 case ZPOOL_PROP_VERSION:
378 if (intval >= SPA_VERSION_FEATURES) {
379 (void) snprintf(buf, len, "-");
384 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
388 case PROP_TYPE_INDEX:
389 intval = zpool_get_prop_int(zhp, prop, &src);
390 if (zpool_prop_index_to_string(prop, intval, &strval)
393 (void) strlcpy(buf, strval, len);
407 * Check if the bootfs name has the same pool name as it is set to.
408 * Assuming bootfs is a valid dataset name.
411 bootfs_name_valid(const char *pool, char *bootfs)
413 int len = strlen(pool);
414 if (bootfs[0] == '\0')
417 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
420 if (strncmp(pool, bootfs, len) == 0 &&
421 (bootfs[len] == '/' || bootfs[len] == '\0'))
428 zpool_is_bootable(zpool_handle_t *zhp)
430 char bootfs[ZFS_MAX_DATASET_NAME_LEN];
432 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
433 sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-",
434 sizeof (bootfs)) != 0);
439 * Given an nvlist of zpool properties to be set, validate that they are
440 * correct, and parse any numeric properties (index, boolean, etc) if they are
441 * specified as strings.
444 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
445 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
453 struct stat64 statbuf;
456 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
457 (void) no_memory(hdl);
462 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
463 const char *propname = nvpair_name(elem);
465 prop = zpool_name_to_prop(propname);
466 if (prop == ZPOOL_PROP_INVAL && zpool_prop_feature(propname)) {
468 char *fname = strchr(propname, '@') + 1;
470 err = zfeature_lookup_name(fname, NULL);
472 ASSERT3U(err, ==, ENOENT);
473 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
474 "invalid feature '%s'"), fname);
475 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
479 if (nvpair_type(elem) != DATA_TYPE_STRING) {
480 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
481 "'%s' must be a string"), propname);
482 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
486 (void) nvpair_value_string(elem, &strval);
487 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0 &&
488 strcmp(strval, ZFS_FEATURE_DISABLED) != 0) {
489 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
490 "property '%s' can only be set to "
491 "'enabled' or 'disabled'"), propname);
492 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
497 strcmp(strval, ZFS_FEATURE_DISABLED) == 0) {
498 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
499 "property '%s' can only be set to "
500 "'disabled' at creation time"), propname);
501 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
505 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
506 (void) no_memory(hdl);
513 * Make sure this property is valid and applies to this type.
515 if (prop == ZPOOL_PROP_INVAL) {
516 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
517 "invalid property '%s'"), propname);
518 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
522 if (zpool_prop_readonly(prop)) {
523 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
524 "is readonly"), propname);
525 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
529 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
530 &strval, &intval, errbuf) != 0)
534 * Perform additional checking for specific properties.
537 case ZPOOL_PROP_VERSION:
538 if (intval < version ||
539 !SPA_VERSION_IS_SUPPORTED(intval)) {
540 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
541 "property '%s' number %d is invalid."),
543 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
548 case ZPOOL_PROP_ASHIFT:
550 (intval < ASHIFT_MIN || intval > ASHIFT_MAX)) {
551 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
552 "invalid '%s=%d' property: only values "
553 "between %" PRId32 " and %" PRId32 " "
555 propname, intval, ASHIFT_MIN, ASHIFT_MAX);
556 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
561 case ZPOOL_PROP_BOOTFS:
562 if (flags.create || flags.import) {
563 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
564 "property '%s' cannot be set at creation "
565 "or import time"), propname);
566 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
570 if (version < SPA_VERSION_BOOTFS) {
571 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
572 "pool must be upgraded to support "
573 "'%s' property"), propname);
574 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
579 * bootfs property value has to be a dataset name and
580 * the dataset has to be in the same pool as it sets to.
582 if (!bootfs_name_valid(poolname, strval)) {
583 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
584 "is an invalid name"), strval);
585 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
589 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
590 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
591 "could not open pool '%s'"), poolname);
592 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
598 case ZPOOL_PROP_ALTROOT:
599 if (!flags.create && !flags.import) {
600 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
601 "property '%s' can only be set during pool "
602 "creation or import"), propname);
603 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
607 if (strval[0] != '/') {
608 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
609 "bad alternate root '%s'"), strval);
610 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
615 case ZPOOL_PROP_CACHEFILE:
616 if (strval[0] == '\0')
619 if (strcmp(strval, "none") == 0)
622 if (strval[0] != '/') {
623 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
624 "property '%s' must be empty, an "
625 "absolute path, or 'none'"), propname);
626 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
630 slash = strrchr(strval, '/');
632 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
633 strcmp(slash, "/..") == 0) {
634 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
635 "'%s' is not a valid file"), strval);
636 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
642 if (strval[0] != '\0' &&
643 (stat64(strval, &statbuf) != 0 ||
644 !S_ISDIR(statbuf.st_mode))) {
645 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
646 "'%s' is not a valid directory"),
648 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
655 case ZPOOL_PROP_COMMENT:
656 for (check = strval; *check != '\0'; check++) {
657 if (!isprint(*check)) {
659 dgettext(TEXT_DOMAIN,
660 "comment may only have printable "
662 (void) zfs_error(hdl, EZFS_BADPROP,
667 if (strlen(strval) > ZPROP_MAX_COMMENT) {
668 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
669 "comment must not exceed %d characters"),
671 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
675 case ZPOOL_PROP_READONLY:
677 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
678 "property '%s' can only be set at "
679 "import time"), propname);
680 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
684 case ZPOOL_PROP_TNAME:
686 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
687 "property '%s' can only be set at "
688 "creation time"), propname);
689 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
693 case ZPOOL_PROP_MULTIHOST:
694 if (get_system_hostid() == 0) {
695 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
696 "requires a non-zero system hostid"));
697 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
702 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
703 "property '%s'(%d) not defined"), propname, prop);
710 nvlist_free(retprops);
715 * Set zpool property : propname=propval.
718 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
720 zfs_cmd_t zc = {"\0"};
723 nvlist_t *nvl = NULL;
726 prop_flags_t flags = { 0 };
728 (void) snprintf(errbuf, sizeof (errbuf),
729 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
732 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
733 return (no_memory(zhp->zpool_hdl));
735 if (nvlist_add_string(nvl, propname, propval) != 0) {
737 return (no_memory(zhp->zpool_hdl));
740 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
741 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
742 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
751 * Execute the corresponding ioctl() to set this property.
753 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
755 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
760 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
762 zcmd_free_nvlists(&zc);
766 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
768 (void) zpool_props_refresh(zhp);
774 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
776 libzfs_handle_t *hdl = zhp->zpool_hdl;
778 char buf[ZFS_MAXPROPLEN];
779 nvlist_t *features = NULL;
782 boolean_t firstexpand = (NULL == *plp);
785 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
789 while (*last != NULL)
790 last = &(*last)->pl_next;
793 features = zpool_get_features(zhp);
795 if ((*plp)->pl_all && firstexpand) {
796 for (i = 0; i < SPA_FEATURES; i++) {
797 zprop_list_t *entry = zfs_alloc(hdl,
798 sizeof (zprop_list_t));
799 entry->pl_prop = ZPROP_INVAL;
800 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
801 spa_feature_table[i].fi_uname);
802 entry->pl_width = strlen(entry->pl_user_prop);
803 entry->pl_all = B_TRUE;
806 last = &entry->pl_next;
810 /* add any unsupported features */
811 for (nvp = nvlist_next_nvpair(features, NULL);
812 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
817 if (zfeature_is_supported(nvpair_name(nvp)))
820 propname = zfs_asprintf(hdl, "unsupported@%s",
824 * Before adding the property to the list make sure that no
825 * other pool already added the same property.
829 while (entry != NULL) {
830 if (entry->pl_user_prop != NULL &&
831 strcmp(propname, entry->pl_user_prop) == 0) {
835 entry = entry->pl_next;
842 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
843 entry->pl_prop = ZPROP_INVAL;
844 entry->pl_user_prop = propname;
845 entry->pl_width = strlen(entry->pl_user_prop);
846 entry->pl_all = B_TRUE;
849 last = &entry->pl_next;
852 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
857 if (entry->pl_prop != ZPROP_INVAL &&
858 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
859 NULL, B_FALSE) == 0) {
860 if (strlen(buf) > entry->pl_width)
861 entry->pl_width = strlen(buf);
869 * Get the state for the given feature on the given ZFS pool.
872 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
876 boolean_t found = B_FALSE;
877 nvlist_t *features = zpool_get_features(zhp);
879 const char *feature = strchr(propname, '@') + 1;
881 supported = zpool_prop_feature(propname);
882 ASSERT(supported || zpool_prop_unsupported(propname));
885 * Convert from feature name to feature guid. This conversion is
886 * unnecessary for unsupported@... properties because they already
893 ret = zfeature_lookup_name(feature, &fid);
895 (void) strlcpy(buf, "-", len);
898 feature = spa_feature_table[fid].fi_guid;
901 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
906 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
909 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
911 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
916 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
918 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
921 (void) strlcpy(buf, "-", len);
930 * Validate the given pool name, optionally putting an extended error message in
934 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
940 ret = pool_namecheck(pool, &why, &what);
943 * The rules for reserved pool names were extended at a later point.
944 * But we need to support users with existing pools that may now be
945 * invalid. So we only check for this expanded set of names during a
946 * create (or import), and only in userland.
948 if (ret == 0 && !isopen &&
949 (strncmp(pool, "mirror", 6) == 0 ||
950 strncmp(pool, "raidz", 5) == 0 ||
951 strncmp(pool, "spare", 5) == 0 ||
952 strcmp(pool, "log") == 0)) {
955 dgettext(TEXT_DOMAIN, "name is reserved"));
963 case NAME_ERR_TOOLONG:
965 dgettext(TEXT_DOMAIN, "name is too long"));
968 case NAME_ERR_INVALCHAR:
970 dgettext(TEXT_DOMAIN, "invalid character "
971 "'%c' in pool name"), what);
974 case NAME_ERR_NOLETTER:
975 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
976 "name must begin with a letter"));
979 case NAME_ERR_RESERVED:
980 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
981 "name is reserved"));
984 case NAME_ERR_DISKLIKE:
985 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
986 "pool name is reserved"));
989 case NAME_ERR_LEADING_SLASH:
990 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
991 "leading slash in name"));
994 case NAME_ERR_EMPTY_COMPONENT:
995 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
996 "empty component in name"));
999 case NAME_ERR_TRAILING_SLASH:
1000 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1001 "trailing slash in name"));
1004 case NAME_ERR_MULTIPLE_DELIMITERS:
1005 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1006 "multiple '@' and/or '#' delimiters in "
1010 case NAME_ERR_NO_AT:
1011 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1012 "permission set is missing '@'"));
1016 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1017 "(%d) not defined"), why);
1028 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1032 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
1034 zpool_handle_t *zhp;
1038 * Make sure the pool name is valid.
1040 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
1041 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1042 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1047 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1050 zhp->zpool_hdl = hdl;
1051 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1053 if (zpool_refresh_stats(zhp, &missing) != 0) {
1059 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1060 (void) zfs_error_fmt(hdl, EZFS_NOENT,
1061 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1070 * Like the above, but silent on error. Used when iterating over pools (because
1071 * the configuration cache may be out of date).
1074 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1076 zpool_handle_t *zhp;
1079 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1082 zhp->zpool_hdl = hdl;
1083 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1085 if (zpool_refresh_stats(zhp, &missing) != 0) {
1101 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1105 zpool_open(libzfs_handle_t *hdl, const char *pool)
1107 zpool_handle_t *zhp;
1109 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1112 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1113 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1114 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1123 * Close the handle. Simply frees the memory associated with the handle.
1126 zpool_close(zpool_handle_t *zhp)
1128 nvlist_free(zhp->zpool_config);
1129 nvlist_free(zhp->zpool_old_config);
1130 nvlist_free(zhp->zpool_props);
1135 * Return the name of the pool.
1138 zpool_get_name(zpool_handle_t *zhp)
1140 return (zhp->zpool_name);
1145 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1148 zpool_get_state(zpool_handle_t *zhp)
1150 return (zhp->zpool_state);
1154 * Create the named pool, using the provided vdev list. It is assumed
1155 * that the consumer has already validated the contents of the nvlist, so we
1156 * don't have to worry about error semantics.
1159 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1160 nvlist_t *props, nvlist_t *fsprops)
1162 zfs_cmd_t zc = {"\0"};
1163 nvlist_t *zc_fsprops = NULL;
1164 nvlist_t *zc_props = NULL;
1165 nvlist_t *hidden_args = NULL;
1166 uint8_t *wkeydata = NULL;
1171 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1172 "cannot create '%s'"), pool);
1174 if (!zpool_name_valid(hdl, B_FALSE, pool))
1175 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1177 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1181 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1183 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1184 SPA_VERSION_1, flags, msg)) == NULL) {
1193 zoned = ((nvlist_lookup_string(fsprops,
1194 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1195 strcmp(zonestr, "on") == 0);
1197 if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
1198 fsprops, zoned, NULL, NULL, B_TRUE, msg)) == NULL) {
1202 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1205 if (zfs_crypto_create(hdl, NULL, zc_fsprops, props,
1206 &wkeydata, &wkeylen) != 0) {
1207 zfs_error(hdl, EZFS_CRYPTOFAILED, msg);
1210 if (nvlist_add_nvlist(zc_props,
1211 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1214 if (wkeydata != NULL) {
1215 if (nvlist_alloc(&hidden_args, NV_UNIQUE_NAME, 0) != 0)
1218 if (nvlist_add_uint8_array(hidden_args, "wkeydata",
1219 wkeydata, wkeylen) != 0)
1222 if (nvlist_add_nvlist(zc_props, ZPOOL_HIDDEN_ARGS,
1228 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1231 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1233 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1235 zcmd_free_nvlists(&zc);
1236 nvlist_free(zc_props);
1237 nvlist_free(zc_fsprops);
1238 nvlist_free(hidden_args);
1239 if (wkeydata != NULL)
1245 * This can happen if the user has specified the same
1246 * device multiple times. We can't reliably detect this
1247 * until we try to add it and see we already have a
1248 * label. This can also happen under if the device is
1249 * part of an active md or lvm device.
1251 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1252 "one or more vdevs refer to the same device, or "
1253 "one of\nthe devices is part of an active md or "
1255 return (zfs_error(hdl, EZFS_BADDEV, msg));
1259 * This happens if the record size is smaller or larger
1260 * than the allowed size range, or not a power of 2.
1262 * NOTE: although zfs_valid_proplist is called earlier,
1263 * this case may have slipped through since the
1264 * pool does not exist yet and it is therefore
1265 * impossible to read properties e.g. max blocksize
1268 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1269 "record size invalid"));
1270 return (zfs_error(hdl, EZFS_BADPROP, msg));
1274 * This occurs when one of the devices is below
1275 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1276 * device was the problem device since there's no
1277 * reliable way to determine device size from userland.
1282 zfs_nicebytes(SPA_MINDEVSIZE, buf,
1285 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1286 "one or more devices is less than the "
1287 "minimum size (%s)"), buf);
1289 return (zfs_error(hdl, EZFS_BADDEV, msg));
1292 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1293 "one or more devices is out of space"));
1294 return (zfs_error(hdl, EZFS_BADDEV, msg));
1297 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1298 "cache device must be a disk or disk slice"));
1299 return (zfs_error(hdl, EZFS_BADDEV, msg));
1302 return (zpool_standard_error(hdl, errno, msg));
1307 zcmd_free_nvlists(&zc);
1308 nvlist_free(zc_props);
1309 nvlist_free(zc_fsprops);
1310 nvlist_free(hidden_args);
1311 if (wkeydata != NULL)
1317 * Destroy the given pool. It is up to the caller to ensure that there are no
1318 * datasets left in the pool.
1321 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1323 zfs_cmd_t zc = {"\0"};
1324 zfs_handle_t *zfp = NULL;
1325 libzfs_handle_t *hdl = zhp->zpool_hdl;
1328 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1329 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1332 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1333 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1335 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1336 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1337 "cannot destroy '%s'"), zhp->zpool_name);
1339 if (errno == EROFS) {
1340 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1341 "one or more devices is read only"));
1342 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1344 (void) zpool_standard_error(hdl, errno, msg);
1353 remove_mountpoint(zfp);
1361 * Add the given vdevs to the pool. The caller must have already performed the
1362 * necessary verification to ensure that the vdev specification is well-formed.
1365 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1367 zfs_cmd_t zc = {"\0"};
1369 libzfs_handle_t *hdl = zhp->zpool_hdl;
1371 nvlist_t **spares, **l2cache;
1372 uint_t nspares, nl2cache;
1374 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1375 "cannot add to '%s'"), zhp->zpool_name);
1377 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1378 SPA_VERSION_SPARES &&
1379 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1380 &spares, &nspares) == 0) {
1381 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1382 "upgraded to add hot spares"));
1383 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1386 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1387 SPA_VERSION_L2CACHE &&
1388 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1389 &l2cache, &nl2cache) == 0) {
1390 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1391 "upgraded to add cache devices"));
1392 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1395 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1397 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1399 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1403 * This can happen if the user has specified the same
1404 * device multiple times. We can't reliably detect this
1405 * until we try to add it and see we already have a
1408 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1409 "one or more vdevs refer to the same device"));
1410 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1414 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1415 "invalid config; a pool with removing/removed "
1416 "vdevs does not support adding raidz vdevs"));
1417 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1422 * This occurrs when one of the devices is below
1423 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1424 * device was the problem device since there's no
1425 * reliable way to determine device size from userland.
1430 zfs_nicebytes(SPA_MINDEVSIZE, buf,
1433 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1434 "device is less than the minimum "
1437 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1441 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1442 "pool must be upgraded to add these vdevs"));
1443 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1447 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1448 "cache device must be a disk or disk slice"));
1449 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1453 (void) zpool_standard_error(hdl, errno, msg);
1461 zcmd_free_nvlists(&zc);
1467 * Exports the pool from the system. The caller must ensure that there are no
1468 * mounted datasets in the pool.
1471 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1472 const char *log_str)
1474 zfs_cmd_t zc = {"\0"};
1477 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1478 "cannot export '%s'"), zhp->zpool_name);
1480 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1481 zc.zc_cookie = force;
1482 zc.zc_guid = hardforce;
1483 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1485 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1488 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1489 "use '-f' to override the following errors:\n"
1490 "'%s' has an active shared spare which could be"
1491 " used by other pools once '%s' is exported."),
1492 zhp->zpool_name, zhp->zpool_name);
1493 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1496 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1505 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1507 return (zpool_export_common(zhp, force, B_FALSE, log_str));
1511 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1513 return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1517 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1520 nvlist_t *nv = NULL;
1526 if (!hdl->libzfs_printerr || config == NULL)
1529 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1530 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1534 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1536 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1538 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1539 strftime(timestr, 128, "%c", &t) != 0) {
1541 (void) printf(dgettext(TEXT_DOMAIN,
1542 "Would be able to return %s "
1543 "to its state as of %s.\n"),
1546 (void) printf(dgettext(TEXT_DOMAIN,
1547 "Pool %s returned to its state as of %s.\n"),
1551 (void) printf(dgettext(TEXT_DOMAIN,
1552 "%s approximately %lld "),
1553 dryrun ? "Would discard" : "Discarded",
1554 ((longlong_t)loss + 30) / 60);
1555 (void) printf(dgettext(TEXT_DOMAIN,
1556 "minutes of transactions.\n"));
1557 } else if (loss > 0) {
1558 (void) printf(dgettext(TEXT_DOMAIN,
1559 "%s approximately %lld "),
1560 dryrun ? "Would discard" : "Discarded",
1562 (void) printf(dgettext(TEXT_DOMAIN,
1563 "seconds of transactions.\n"));
1569 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1572 nvlist_t *nv = NULL;
1574 uint64_t edata = UINT64_MAX;
1579 if (!hdl->libzfs_printerr)
1583 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1585 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1587 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1588 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1589 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1590 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1593 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1594 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1597 (void) printf(dgettext(TEXT_DOMAIN,
1598 "Recovery is possible, but will result in some data loss.\n"));
1600 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1601 strftime(timestr, 128, "%c", &t) != 0) {
1602 (void) printf(dgettext(TEXT_DOMAIN,
1603 "\tReturning the pool to its state as of %s\n"
1604 "\tshould correct the problem. "),
1607 (void) printf(dgettext(TEXT_DOMAIN,
1608 "\tReverting the pool to an earlier state "
1609 "should correct the problem.\n\t"));
1613 (void) printf(dgettext(TEXT_DOMAIN,
1614 "Approximately %lld minutes of data\n"
1615 "\tmust be discarded, irreversibly. "),
1616 ((longlong_t)loss + 30) / 60);
1617 } else if (loss > 0) {
1618 (void) printf(dgettext(TEXT_DOMAIN,
1619 "Approximately %lld seconds of data\n"
1620 "\tmust be discarded, irreversibly. "),
1623 if (edata != 0 && edata != UINT64_MAX) {
1625 (void) printf(dgettext(TEXT_DOMAIN,
1626 "After rewind, at least\n"
1627 "\tone persistent user-data error will remain. "));
1629 (void) printf(dgettext(TEXT_DOMAIN,
1630 "After rewind, several\n"
1631 "\tpersistent user-data errors will remain. "));
1634 (void) printf(dgettext(TEXT_DOMAIN,
1635 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1636 reason >= 0 ? "clear" : "import", name);
1638 (void) printf(dgettext(TEXT_DOMAIN,
1639 "A scrub of the pool\n"
1640 "\tis strongly recommended after recovery.\n"));
1644 (void) printf(dgettext(TEXT_DOMAIN,
1645 "Destroy and re-create the pool from\n\ta backup source.\n"));
1649 * zpool_import() is a contracted interface. Should be kept the same
1652 * Applications should use zpool_import_props() to import a pool with
1653 * new properties value to be set.
1656 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1659 nvlist_t *props = NULL;
1662 if (altroot != NULL) {
1663 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1664 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1665 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1669 if (nvlist_add_string(props,
1670 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1671 nvlist_add_string(props,
1672 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1674 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1675 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1680 ret = zpool_import_props(hdl, config, newname, props,
1687 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1693 uint64_t is_log = 0;
1695 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1699 (void) printf("\t%*s%s%s\n", indent, "", name,
1700 is_log ? " [log]" : "");
1702 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1703 &child, &children) != 0)
1706 for (c = 0; c < children; c++) {
1707 vname = zpool_vdev_name(hdl, NULL, child[c], VDEV_NAME_TYPE_ID);
1708 print_vdev_tree(hdl, vname, child[c], indent + 2);
1714 zpool_print_unsup_feat(nvlist_t *config)
1716 nvlist_t *nvinfo, *unsup_feat;
1719 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1721 verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1724 for (nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1725 nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1728 verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1729 verify(nvpair_value_string(nvp, &desc) == 0);
1731 if (strlen(desc) > 0)
1732 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1734 (void) printf("\t%s\n", nvpair_name(nvp));
1739 * Import the given pool using the known configuration and a list of
1740 * properties to be set. The configuration should have come from
1741 * zpool_find_import(). The 'newname' parameters control whether the pool
1742 * is imported with a different name.
1745 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1746 nvlist_t *props, int flags)
1748 zfs_cmd_t zc = {"\0"};
1749 zpool_load_policy_t policy;
1750 nvlist_t *nv = NULL;
1751 nvlist_t *nvinfo = NULL;
1752 nvlist_t *missing = NULL;
1759 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1762 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1763 "cannot import pool '%s'"), origname);
1765 if (newname != NULL) {
1766 if (!zpool_name_valid(hdl, B_FALSE, newname))
1767 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1768 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1770 thename = (char *)newname;
1775 if (props != NULL) {
1777 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1779 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1782 if ((props = zpool_valid_proplist(hdl, origname,
1783 props, version, flags, errbuf)) == NULL)
1785 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1792 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1794 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1797 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1798 zcmd_free_nvlists(&zc);
1801 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1802 zcmd_free_nvlists(&zc);
1806 zc.zc_cookie = flags;
1807 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1809 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1810 zcmd_free_nvlists(&zc);
1817 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1819 zcmd_free_nvlists(&zc);
1821 zpool_get_load_policy(config, &policy);
1828 * Dry-run failed, but we print out what success
1829 * looks like if we found a best txg
1831 if (policy.zlp_rewind & ZPOOL_TRY_REWIND) {
1832 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1838 if (newname == NULL)
1839 (void) snprintf(desc, sizeof (desc),
1840 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1843 (void) snprintf(desc, sizeof (desc),
1844 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1849 if (nv != NULL && nvlist_lookup_nvlist(nv,
1850 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1851 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1852 (void) printf(dgettext(TEXT_DOMAIN, "This "
1853 "pool uses the following feature(s) not "
1854 "supported by this system:\n"));
1855 zpool_print_unsup_feat(nv);
1856 if (nvlist_exists(nvinfo,
1857 ZPOOL_CONFIG_CAN_RDONLY)) {
1858 (void) printf(dgettext(TEXT_DOMAIN,
1859 "All unsupported features are only "
1860 "required for writing to the pool."
1861 "\nThe pool can be imported using "
1862 "'-o readonly=on'.\n"));
1866 * Unsupported version.
1868 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1872 if (nv != NULL && nvlist_lookup_nvlist(nv,
1873 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0) {
1874 char *hostname = "<unknown>";
1875 uint64_t hostid = 0;
1876 mmp_state_t mmp_state;
1878 mmp_state = fnvlist_lookup_uint64(nvinfo,
1879 ZPOOL_CONFIG_MMP_STATE);
1881 if (nvlist_exists(nvinfo,
1882 ZPOOL_CONFIG_MMP_HOSTNAME))
1883 hostname = fnvlist_lookup_string(nvinfo,
1884 ZPOOL_CONFIG_MMP_HOSTNAME);
1886 if (nvlist_exists(nvinfo,
1887 ZPOOL_CONFIG_MMP_HOSTID))
1888 hostid = fnvlist_lookup_uint64(nvinfo,
1889 ZPOOL_CONFIG_MMP_HOSTID);
1891 if (mmp_state == MMP_STATE_ACTIVE) {
1892 (void) snprintf(aux, sizeof (aux),
1893 dgettext(TEXT_DOMAIN, "pool is imp"
1894 "orted on host '%s' (hostid=%lx).\n"
1895 "Export the pool on the other "
1896 "system, then run 'zpool import'."),
1897 hostname, (unsigned long) hostid);
1898 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
1899 (void) snprintf(aux, sizeof (aux),
1900 dgettext(TEXT_DOMAIN, "pool has "
1901 "the multihost property on and "
1902 "the\nsystem's hostid is not set. "
1903 "Set a unique system hostid with "
1904 "the zgenhostid(8) command.\n"));
1907 (void) zfs_error_aux(hdl, aux);
1909 (void) zfs_error(hdl, EZFS_ACTIVE_POOL, desc);
1913 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1917 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1918 "one or more devices is read only"));
1919 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1923 if (nv && nvlist_lookup_nvlist(nv,
1924 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1925 nvlist_lookup_nvlist(nvinfo,
1926 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1927 (void) printf(dgettext(TEXT_DOMAIN,
1928 "The devices below are missing or "
1929 "corrupted, use '-m' to import the pool "
1931 print_vdev_tree(hdl, NULL, missing, 2);
1932 (void) printf("\n");
1934 (void) zpool_standard_error(hdl, error, desc);
1938 (void) zpool_standard_error(hdl, error, desc);
1942 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1943 "one or more devices are already in use\n"));
1944 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1947 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1948 "new name of at least one dataset is longer than "
1949 "the maximum allowable length"));
1950 (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
1953 (void) zpool_standard_error(hdl, error, desc);
1954 zpool_explain_recover(hdl,
1955 newname ? origname : thename, -error, nv);
1962 zpool_handle_t *zhp;
1965 * This should never fail, but play it safe anyway.
1967 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1969 else if (zhp != NULL)
1971 if (policy.zlp_rewind &
1972 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1973 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1974 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0), nv);
1987 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd)
1989 zfs_cmd_t zc = {"\0"};
1992 libzfs_handle_t *hdl = zhp->zpool_hdl;
1994 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1995 zc.zc_cookie = func;
1998 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0)
2003 /* ECANCELED on a scrub means we resumed a paused scrub */
2004 if (err == ECANCELED && func == POOL_SCAN_SCRUB &&
2005 cmd == POOL_SCRUB_NORMAL)
2008 if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL)
2011 if (func == POOL_SCAN_SCRUB) {
2012 if (cmd == POOL_SCRUB_PAUSE) {
2013 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2014 "cannot pause scrubbing %s"), zc.zc_name);
2016 assert(cmd == POOL_SCRUB_NORMAL);
2017 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2018 "cannot scrub %s"), zc.zc_name);
2020 } else if (func == POOL_SCAN_NONE) {
2021 (void) snprintf(msg, sizeof (msg),
2022 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
2025 assert(!"unexpected result");
2030 pool_scan_stat_t *ps = NULL;
2033 verify(nvlist_lookup_nvlist(zhp->zpool_config,
2034 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
2035 (void) nvlist_lookup_uint64_array(nvroot,
2036 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
2037 if (ps && ps->pss_func == POOL_SCAN_SCRUB) {
2038 if (cmd == POOL_SCRUB_PAUSE)
2039 return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg));
2041 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
2043 return (zfs_error(hdl, EZFS_RESILVERING, msg));
2045 } else if (err == ENOENT) {
2046 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
2048 return (zpool_standard_error(hdl, err, msg));
2053 * Find a vdev that matches the search criteria specified. We use the
2054 * the nvpair name to determine how we should look for the device.
2055 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2056 * spare; but FALSE if its an INUSE spare.
2059 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
2060 boolean_t *l2cache, boolean_t *log)
2067 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
2069 /* Nothing to look for */
2070 if (search == NULL || pair == NULL)
2073 /* Obtain the key we will use to search */
2074 srchkey = nvpair_name(pair);
2076 switch (nvpair_type(pair)) {
2077 case DATA_TYPE_UINT64:
2078 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
2079 uint64_t srchval, theguid;
2081 verify(nvpair_value_uint64(pair, &srchval) == 0);
2082 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2084 if (theguid == srchval)
2089 case DATA_TYPE_STRING: {
2090 char *srchval, *val;
2092 verify(nvpair_value_string(pair, &srchval) == 0);
2093 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2097 * Search for the requested value. Special cases:
2099 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2100 * "-part1", or "p1". The suffix is hidden from the user,
2101 * but included in the string, so this matches around it.
2102 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2103 * is used to check all possible expanded paths.
2104 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2106 * Otherwise, all other searches are simple string compares.
2108 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) {
2109 uint64_t wholedisk = 0;
2111 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2113 if (zfs_strcmp_pathname(srchval, val, wholedisk) == 0)
2116 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2117 char *type, *idx, *end, *p;
2118 uint64_t id, vdev_id;
2121 * Determine our vdev type, keeping in mind
2122 * that the srchval is composed of a type and
2123 * vdev id pair (i.e. mirror-4).
2125 if ((type = strdup(srchval)) == NULL)
2128 if ((p = strrchr(type, '-')) == NULL) {
2136 * If the types don't match then keep looking.
2138 if (strncmp(val, type, strlen(val)) != 0) {
2143 verify(zpool_vdev_is_interior(type));
2144 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2148 vdev_id = strtoull(idx, &end, 10);
2155 * Now verify that we have the correct vdev id.
2164 if (strcmp(srchval, val) == 0)
2173 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2174 &child, &children) != 0)
2177 for (c = 0; c < children; c++) {
2178 if ((ret = vdev_to_nvlist_iter(child[c], search,
2179 avail_spare, l2cache, NULL)) != NULL) {
2181 * The 'is_log' value is only set for the toplevel
2182 * vdev, not the leaf vdevs. So we always lookup the
2183 * log device from the root of the vdev tree (where
2184 * 'log' is non-NULL).
2187 nvlist_lookup_uint64(child[c],
2188 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2196 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2197 &child, &children) == 0) {
2198 for (c = 0; c < children; c++) {
2199 if ((ret = vdev_to_nvlist_iter(child[c], search,
2200 avail_spare, l2cache, NULL)) != NULL) {
2201 *avail_spare = B_TRUE;
2207 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2208 &child, &children) == 0) {
2209 for (c = 0; c < children; c++) {
2210 if ((ret = vdev_to_nvlist_iter(child[c], search,
2211 avail_spare, l2cache, NULL)) != NULL) {
2222 * Given a physical path (minus the "/devices" prefix), find the
2226 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2227 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2229 nvlist_t *search, *nvroot, *ret;
2231 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2232 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2234 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2237 *avail_spare = B_FALSE;
2241 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2242 nvlist_free(search);
2248 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2251 zpool_vdev_is_interior(const char *name)
2253 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2254 strncmp(name, VDEV_TYPE_SPARE, strlen(VDEV_TYPE_SPARE)) == 0 ||
2256 VDEV_TYPE_REPLACING, strlen(VDEV_TYPE_REPLACING)) == 0 ||
2257 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2263 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2264 boolean_t *l2cache, boolean_t *log)
2267 nvlist_t *nvroot, *search, *ret;
2270 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2272 guid = strtoull(path, &end, 0);
2273 if (guid != 0 && *end == '\0') {
2274 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2275 } else if (zpool_vdev_is_interior(path)) {
2276 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2278 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2281 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2284 *avail_spare = B_FALSE;
2288 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2289 nvlist_free(search);
2295 vdev_is_online(nvlist_t *nv)
2299 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2300 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2301 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2308 * Helper function for zpool_get_physpaths().
2311 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2312 size_t *bytes_written)
2314 size_t bytes_left, pos, rsz;
2318 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2320 return (EZFS_NODEVICE);
2322 pos = *bytes_written;
2323 bytes_left = physpath_size - pos;
2324 format = (pos == 0) ? "%s" : " %s";
2326 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2327 *bytes_written += rsz;
2329 if (rsz >= bytes_left) {
2330 /* if physpath was not copied properly, clear it */
2331 if (bytes_left != 0) {
2334 return (EZFS_NOSPC);
2340 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2341 size_t *rsz, boolean_t is_spare)
2346 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2347 return (EZFS_INVALCONFIG);
2349 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2351 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2352 * For a spare vdev, we only want to boot from the active
2357 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2360 return (EZFS_INVALCONFIG);
2363 if (vdev_is_online(nv)) {
2364 if ((ret = vdev_get_one_physpath(nv, physpath,
2365 phypath_size, rsz)) != 0)
2368 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2369 strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
2370 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2371 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2376 if (nvlist_lookup_nvlist_array(nv,
2377 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2378 return (EZFS_INVALCONFIG);
2380 for (i = 0; i < count; i++) {
2381 ret = vdev_get_physpaths(child[i], physpath,
2382 phypath_size, rsz, is_spare);
2383 if (ret == EZFS_NOSPC)
2388 return (EZFS_POOL_INVALARG);
2392 * Get phys_path for a root pool config.
2393 * Return 0 on success; non-zero on failure.
2396 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2399 nvlist_t *vdev_root;
2406 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2408 return (EZFS_INVALCONFIG);
2410 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2411 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2412 &child, &count) != 0)
2413 return (EZFS_INVALCONFIG);
2416 * root pool can only have a single top-level vdev.
2418 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2419 return (EZFS_POOL_INVALARG);
2421 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2424 /* No online devices */
2426 return (EZFS_NODEVICE);
2432 * Get phys_path for a root pool
2433 * Return 0 on success; non-zero on failure.
2436 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2438 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2443 * If the device has being dynamically expanded then we need to relabel
2444 * the disk to use the new unallocated space.
2447 zpool_relabel_disk(libzfs_handle_t *hdl, const char *path, const char *msg)
2451 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
2452 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2453 "relabel '%s': unable to open device: %d"), path, errno);
2454 return (zfs_error(hdl, EZFS_OPENFAILED, msg));
2458 * It's possible that we might encounter an error if the device
2459 * does not have any unallocated space left. If so, we simply
2460 * ignore that error and continue on.
2462 * Also, we don't call efi_rescan() - that would just return EBUSY.
2463 * The module will do it for us in vdev_disk_open().
2465 error = efi_use_whole_disk(fd);
2467 /* Flush the buffers to disk and invalidate the page cache. */
2469 (void) ioctl(fd, BLKFLSBUF);
2472 if (error && error != VT_ENOSPC) {
2473 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2474 "relabel '%s': unable to read disk capacity"), path);
2475 return (zfs_error(hdl, EZFS_NOCAP, msg));
2482 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
2484 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
2485 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
2489 zpool_vdev_path_to_guid_impl(zpool_handle_t *zhp, const char *path,
2490 boolean_t *is_spare, boolean_t *is_l2cache, boolean_t *is_log)
2493 boolean_t spare = B_FALSE, l2cache = B_FALSE, log = B_FALSE;
2496 if ((tgt = zpool_find_vdev(zhp, path, &spare, &l2cache,
2500 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &guid) == 0);
2501 if (is_spare != NULL)
2503 if (is_l2cache != NULL)
2504 *is_l2cache = l2cache;
2511 /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
2513 zpool_vdev_path_to_guid(zpool_handle_t *zhp, const char *path)
2515 return (zpool_vdev_path_to_guid_impl(zhp, path, NULL, NULL, NULL));
2519 * Bring the specified vdev online. The 'flags' parameter is a set of the
2520 * ZFS_ONLINE_* flags.
2523 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2524 vdev_state_t *newstate)
2526 zfs_cmd_t zc = {"\0"};
2530 boolean_t avail_spare, l2cache, islog;
2531 libzfs_handle_t *hdl = zhp->zpool_hdl;
2534 if (flags & ZFS_ONLINE_EXPAND) {
2535 (void) snprintf(msg, sizeof (msg),
2536 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2538 (void) snprintf(msg, sizeof (msg),
2539 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2542 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2543 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2545 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2547 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2550 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2552 if ((flags & ZFS_ONLINE_EXPAND ||
2553 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) &&
2554 nvlist_lookup_string(tgt, ZPOOL_CONFIG_PATH, &pathname) == 0) {
2555 uint64_t wholedisk = 0;
2557 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2561 * XXX - L2ARC 1.0 devices can't support expansion.
2564 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2565 "cannot expand cache devices"));
2566 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2570 const char *fullpath = path;
2571 char buf[MAXPATHLEN];
2573 if (path[0] != '/') {
2574 error = zfs_resolve_shortname(path, buf,
2577 return (zfs_error(hdl, EZFS_NODEVICE,
2583 error = zpool_relabel_disk(hdl, fullpath, msg);
2589 zc.zc_cookie = VDEV_STATE_ONLINE;
2592 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2593 if (errno == EINVAL) {
2594 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2595 "from this pool into a new one. Use '%s' "
2596 "instead"), "zpool detach");
2597 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2599 return (zpool_standard_error(hdl, errno, msg));
2602 *newstate = zc.zc_cookie;
2607 * Take the specified vdev offline
2610 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2612 zfs_cmd_t zc = {"\0"};
2615 boolean_t avail_spare, l2cache;
2616 libzfs_handle_t *hdl = zhp->zpool_hdl;
2618 (void) snprintf(msg, sizeof (msg),
2619 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2621 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2622 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2624 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2626 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2629 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2631 zc.zc_cookie = VDEV_STATE_OFFLINE;
2632 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2634 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2641 * There are no other replicas of this device.
2643 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2647 * The log device has unplayed logs
2649 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2652 return (zpool_standard_error(hdl, errno, msg));
2657 * Mark the given vdev faulted.
2660 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2662 zfs_cmd_t zc = {"\0"};
2664 libzfs_handle_t *hdl = zhp->zpool_hdl;
2666 (void) snprintf(msg, sizeof (msg),
2667 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
2669 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2671 zc.zc_cookie = VDEV_STATE_FAULTED;
2674 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2681 * There are no other replicas of this device.
2683 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2686 return (zpool_standard_error(hdl, errno, msg));
2692 * Mark the given vdev degraded.
2695 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2697 zfs_cmd_t zc = {"\0"};
2699 libzfs_handle_t *hdl = zhp->zpool_hdl;
2701 (void) snprintf(msg, sizeof (msg),
2702 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid);
2704 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2706 zc.zc_cookie = VDEV_STATE_DEGRADED;
2709 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2712 return (zpool_standard_error(hdl, errno, msg));
2716 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2720 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2726 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2728 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2731 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2732 children == 2 && child[which] == tgt)
2735 for (c = 0; c < children; c++)
2736 if (is_replacing_spare(child[c], tgt, which))
2744 * Attach new_disk (fully described by nvroot) to old_disk.
2745 * If 'replacing' is specified, the new disk will replace the old one.
2748 zpool_vdev_attach(zpool_handle_t *zhp,
2749 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2751 zfs_cmd_t zc = {"\0"};
2755 boolean_t avail_spare, l2cache, islog;
2760 nvlist_t *config_root;
2761 libzfs_handle_t *hdl = zhp->zpool_hdl;
2762 boolean_t rootpool = zpool_is_bootable(zhp);
2765 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2766 "cannot replace %s with %s"), old_disk, new_disk);
2768 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2769 "cannot attach %s to %s"), new_disk, old_disk);
2771 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2772 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2774 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2777 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2780 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2782 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2783 zc.zc_cookie = replacing;
2785 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2786 &child, &children) != 0 || children != 1) {
2787 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2788 "new device must be a single disk"));
2789 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2792 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2793 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2795 if ((newname = zpool_vdev_name(NULL, NULL, child[0], 0)) == NULL)
2799 * If the target is a hot spare that has been swapped in, we can only
2800 * replace it with another hot spare.
2803 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2804 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2805 NULL) == NULL || !avail_spare) &&
2806 is_replacing_spare(config_root, tgt, 1)) {
2807 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2808 "can only be replaced by another hot spare"));
2810 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2815 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2818 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2820 zcmd_free_nvlists(&zc);
2825 * XXX need a better way to prevent user from
2826 * booting up a half-baked vdev.
2828 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2829 "sure to wait until resilver is done "
2830 "before rebooting.\n"));
2838 * Can't attach to or replace this type of vdev.
2841 uint64_t version = zpool_get_prop_int(zhp,
2842 ZPOOL_PROP_VERSION, NULL);
2845 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2846 "cannot replace a log with a spare"));
2847 else if (version >= SPA_VERSION_MULTI_REPLACE)
2848 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2849 "already in replacing/spare config; wait "
2850 "for completion or use 'zpool detach'"));
2852 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2853 "cannot replace a replacing device"));
2855 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2856 "can only attach to mirrors and top-level "
2859 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2864 * The new device must be a single disk.
2866 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2867 "new device must be a single disk"));
2868 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2872 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy, "
2873 "or device removal is in progress"),
2875 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2880 * The new device is too small.
2882 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2883 "device is too small"));
2884 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2889 * The new device has a different optimal sector size.
2891 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2892 "new device has a different optimal sector size; use the "
2893 "option '-o ashift=N' to override the optimal size"));
2894 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2899 * The resulting top-level vdev spec won't fit in the label.
2901 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2905 (void) zpool_standard_error(hdl, errno, msg);
2912 * Detach the specified device.
2915 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2917 zfs_cmd_t zc = {"\0"};
2920 boolean_t avail_spare, l2cache;
2921 libzfs_handle_t *hdl = zhp->zpool_hdl;
2923 (void) snprintf(msg, sizeof (msg),
2924 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2926 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2927 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2929 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2932 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2935 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2937 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2939 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2946 * Can't detach from this type of vdev.
2948 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2949 "applicable to mirror and replacing vdevs"));
2950 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2955 * There are no other replicas of this device.
2957 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2961 (void) zpool_standard_error(hdl, errno, msg);
2968 * Find a mirror vdev in the source nvlist.
2970 * The mchild array contains a list of disks in one of the top-level mirrors
2971 * of the source pool. The schild array contains a list of disks that the
2972 * user specified on the command line. We loop over the mchild array to
2973 * see if any entry in the schild array matches.
2975 * If a disk in the mchild array is found in the schild array, we return
2976 * the index of that entry. Otherwise we return -1.
2979 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2980 nvlist_t **schild, uint_t schildren)
2984 for (mc = 0; mc < mchildren; mc++) {
2986 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2989 for (sc = 0; sc < schildren; sc++) {
2990 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2992 boolean_t result = (strcmp(mpath, spath) == 0);
3008 * Split a mirror pool. If newroot points to null, then a new nvlist
3009 * is generated and it is the responsibility of the caller to free it.
3012 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
3013 nvlist_t *props, splitflags_t flags)
3015 zfs_cmd_t zc = {"\0"};
3017 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
3018 nvlist_t **varray = NULL, *zc_props = NULL;
3019 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
3020 libzfs_handle_t *hdl = zhp->zpool_hdl;
3021 uint64_t vers, readonly = B_FALSE;
3022 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
3025 (void) snprintf(msg, sizeof (msg),
3026 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
3028 if (!zpool_name_valid(hdl, B_FALSE, newname))
3029 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
3031 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
3032 (void) fprintf(stderr, gettext("Internal error: unable to "
3033 "retrieve pool configuration\n"));
3037 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
3039 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
3042 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
3043 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
3044 props, vers, flags, msg)) == NULL)
3046 (void) nvlist_lookup_uint64(zc_props,
3047 zpool_prop_to_name(ZPOOL_PROP_READONLY), &readonly);
3049 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3050 "property %s can only be set at import time"),
3051 zpool_prop_to_name(ZPOOL_PROP_READONLY));
3056 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3058 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3059 "Source pool is missing vdev tree"));
3060 nvlist_free(zc_props);
3064 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
3067 if (*newroot == NULL ||
3068 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
3069 &newchild, &newchildren) != 0)
3072 for (c = 0; c < children; c++) {
3073 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
3075 nvlist_t **mchild, *vdev;
3080 * Unlike cache & spares, slogs are stored in the
3081 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3083 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3085 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
3087 if (is_log || is_hole) {
3089 * Create a hole vdev and put it in the config.
3091 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
3093 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
3094 VDEV_TYPE_HOLE) != 0)
3096 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
3101 varray[vcount++] = vdev;
3105 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
3107 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
3108 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3109 "Source pool must be composed only of mirrors\n"));
3110 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3114 verify(nvlist_lookup_nvlist_array(child[c],
3115 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3117 /* find or add an entry for this top-level vdev */
3118 if (newchildren > 0 &&
3119 (entry = find_vdev_entry(zhp, mchild, mchildren,
3120 newchild, newchildren)) >= 0) {
3121 /* We found a disk that the user specified. */
3122 vdev = mchild[entry];
3125 /* User didn't specify a disk for this vdev. */
3126 vdev = mchild[mchildren - 1];
3129 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3133 /* did we find every disk the user specified? */
3134 if (found != newchildren) {
3135 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3136 "include at most one disk from each mirror"));
3137 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3141 /* Prepare the nvlist for populating. */
3142 if (*newroot == NULL) {
3143 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3146 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3147 VDEV_TYPE_ROOT) != 0)
3150 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3153 /* Add all the children we found */
3154 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3155 lastlog == 0 ? vcount : lastlog) != 0)
3159 * If we're just doing a dry run, exit now with success.
3162 memory_err = B_FALSE;
3167 /* now build up the config list & call the ioctl */
3168 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3171 if (nvlist_add_nvlist(newconfig,
3172 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3173 nvlist_add_string(newconfig,
3174 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3175 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3179 * The new pool is automatically part of the namespace unless we
3180 * explicitly export it.
3183 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3184 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3185 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3186 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3188 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3191 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3192 retval = zpool_standard_error(hdl, errno, msg);
3197 memory_err = B_FALSE;
3200 if (varray != NULL) {
3203 for (v = 0; v < vcount; v++)
3204 nvlist_free(varray[v]);
3207 zcmd_free_nvlists(&zc);
3208 nvlist_free(zc_props);
3209 nvlist_free(newconfig);
3211 nvlist_free(*newroot);
3219 return (no_memory(hdl));
3225 * Remove the given device.
3228 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3230 zfs_cmd_t zc = {"\0"};
3233 boolean_t avail_spare, l2cache, islog;
3234 libzfs_handle_t *hdl = zhp->zpool_hdl;
3237 (void) snprintf(msg, sizeof (msg),
3238 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3240 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3241 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3243 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3245 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3246 if (islog && version < SPA_VERSION_HOLES) {
3247 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3248 "pool must be upgraded to support log removal"));
3249 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3252 if (!islog && !avail_spare && !l2cache && zpool_is_bootable(zhp)) {
3253 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3254 "root pool can not have removed devices, "
3255 "because GRUB does not understand them"));
3256 return (zfs_error(hdl, EINVAL, msg));
3259 zc.zc_guid = fnvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID);
3261 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3267 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3268 "invalid config; all top-level vdevs must "
3269 "have the same sector size and not be raidz."));
3270 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
3274 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3275 "Pool busy; removal may already be in progress"));
3276 (void) zfs_error(hdl, EZFS_BUSY, msg);
3280 (void) zpool_standard_error(hdl, errno, msg);
3286 zpool_vdev_remove_cancel(zpool_handle_t *zhp)
3290 libzfs_handle_t *hdl = zhp->zpool_hdl;
3292 (void) snprintf(msg, sizeof (msg),
3293 dgettext(TEXT_DOMAIN, "cannot cancel removal"));
3295 bzero(&zc, sizeof (zc));
3296 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3299 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3302 return (zpool_standard_error(hdl, errno, msg));
3306 zpool_vdev_indirect_size(zpool_handle_t *zhp, const char *path,
3311 boolean_t avail_spare, l2cache, islog;
3312 libzfs_handle_t *hdl = zhp->zpool_hdl;
3314 (void) snprintf(msg, sizeof (msg),
3315 dgettext(TEXT_DOMAIN, "cannot determine indirect size of %s"),
3318 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3320 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3322 if (avail_spare || l2cache || islog) {
3327 if (nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_INDIRECT_SIZE, sizep) != 0) {
3328 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3329 "indirect size not available"));
3330 return (zfs_error(hdl, EINVAL, msg));
3336 * Clear the errors for the pool, or the particular device if specified.
3339 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3341 zfs_cmd_t zc = {"\0"};
3344 zpool_load_policy_t policy;
3345 boolean_t avail_spare, l2cache;
3346 libzfs_handle_t *hdl = zhp->zpool_hdl;
3347 nvlist_t *nvi = NULL;
3351 (void) snprintf(msg, sizeof (msg),
3352 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3355 (void) snprintf(msg, sizeof (msg),
3356 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3359 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3361 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3362 &l2cache, NULL)) == NULL)
3363 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3366 * Don't allow error clearing for hot spares. Do allow
3367 * error clearing for l2cache devices.
3370 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3372 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3376 zpool_get_load_policy(rewindnvl, &policy);
3377 zc.zc_cookie = policy.zlp_rewind;
3379 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3382 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3385 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3387 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3388 zcmd_free_nvlists(&zc);
3393 if (!error || ((policy.zlp_rewind & ZPOOL_TRY_REWIND) &&
3394 errno != EPERM && errno != EACCES)) {
3395 if (policy.zlp_rewind &
3396 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3397 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3398 zpool_rewind_exclaim(hdl, zc.zc_name,
3399 ((policy.zlp_rewind & ZPOOL_TRY_REWIND) != 0),
3403 zcmd_free_nvlists(&zc);
3407 zcmd_free_nvlists(&zc);
3408 return (zpool_standard_error(hdl, errno, msg));
3412 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3415 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3417 zfs_cmd_t zc = {"\0"};
3419 libzfs_handle_t *hdl = zhp->zpool_hdl;
3421 (void) snprintf(msg, sizeof (msg),
3422 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3423 (u_longlong_t)guid);
3425 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3427 zc.zc_cookie = ZPOOL_NO_REWIND;
3429 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3432 return (zpool_standard_error(hdl, errno, msg));
3436 * Change the GUID for a pool.
3439 zpool_reguid(zpool_handle_t *zhp)
3442 libzfs_handle_t *hdl = zhp->zpool_hdl;
3443 zfs_cmd_t zc = {"\0"};
3445 (void) snprintf(msg, sizeof (msg),
3446 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3448 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3449 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3452 return (zpool_standard_error(hdl, errno, msg));
3459 zpool_reopen_one(zpool_handle_t *zhp, void *data)
3461 libzfs_handle_t *hdl = zpool_get_handle(zhp);
3462 const char *pool_name = zpool_get_name(zhp);
3463 boolean_t *scrub_restart = data;
3466 error = lzc_reopen(pool_name, *scrub_restart);
3468 return (zpool_standard_error_fmt(hdl, error,
3469 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"), pool_name));
3475 /* call into libzfs_core to execute the sync IOCTL per pool */
3477 zpool_sync_one(zpool_handle_t *zhp, void *data)
3480 libzfs_handle_t *hdl = zpool_get_handle(zhp);
3481 const char *pool_name = zpool_get_name(zhp);
3482 boolean_t *force = data;
3483 nvlist_t *innvl = fnvlist_alloc();
3485 fnvlist_add_boolean_value(innvl, "force", *force);
3486 if ((ret = lzc_sync(pool_name, innvl, NULL)) != 0) {
3488 return (zpool_standard_error_fmt(hdl, ret,
3489 dgettext(TEXT_DOMAIN, "sync '%s' failed"), pool_name));
3496 #if defined(__sun__) || defined(__sun)
3498 * Convert from a devid string to a path.
3501 devid_to_path(char *devid_str)
3506 devid_nmlist_t *list = NULL;
3509 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3512 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3514 devid_str_free(minor);
3521 * In a case the strdup() fails, we will just return NULL below.
3523 path = strdup(list[0].devname);
3525 devid_free_nmlist(list);
3531 * Convert from a path to a devid string.
3534 path_to_devid(const char *path)
3540 if ((fd = open(path, O_RDONLY)) < 0)
3545 if (devid_get(fd, &devid) == 0) {
3546 if (devid_get_minor_name(fd, &minor) == 0)
3547 ret = devid_str_encode(devid, minor);
3549 devid_str_free(minor);
3558 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3559 * ignore any failure here, since a common case is for an unprivileged user to
3560 * type 'zpool status', and we'll display the correct information anyway.
3563 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3565 zfs_cmd_t zc = {"\0"};
3567 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3568 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3569 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3572 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3577 * Remove partition suffix from a vdev path. Partition suffixes may take three
3578 * forms: "-partX", "pX", or "X", where X is a string of digits. The second
3579 * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
3580 * third case only occurs when preceded by a string matching the regular
3581 * expression "^([hsv]|xv)d[a-z]+", i.e. a scsi, ide, virtio or xen disk.
3583 * caller must free the returned string
3586 zfs_strip_partition(char *path)
3588 char *tmp = strdup(path);
3589 char *part = NULL, *d = NULL;
3593 if ((part = strstr(tmp, "-part")) && part != tmp) {
3595 } else if ((part = strrchr(tmp, 'p')) &&
3596 part > tmp + 1 && isdigit(*(part-1))) {
3598 } else if ((tmp[0] == 'h' || tmp[0] == 's' || tmp[0] == 'v') &&
3600 for (d = &tmp[2]; isalpha(*d); part = ++d) { }
3601 } else if (strncmp("xvd", tmp, 3) == 0) {
3602 for (d = &tmp[3]; isalpha(*d); part = ++d) { }
3604 if (part && d && *d != '\0') {
3605 for (; isdigit(*d); d++) { }
3614 * Same as zfs_strip_partition, but allows "/dev/" to be in the pathname
3619 * Returned string must be freed.
3622 zfs_strip_partition_path(char *path)
3624 char *newpath = strdup(path);
3631 /* Point to "sda1" part of "/dev/sda1" */
3632 sd_offset = strrchr(newpath, '/') + 1;
3634 /* Get our new name "sda" */
3635 new_sd = zfs_strip_partition(sd_offset);
3641 /* Paste the "sda" where "sda1" was */
3642 strlcpy(sd_offset, new_sd, strlen(sd_offset) + 1);
3644 /* Free temporary "sda" */
3650 #define PATH_BUF_LEN 64
3653 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3654 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3655 * We also check if this is a whole disk, in which case we strip off the
3656 * trailing 's0' slice name.
3658 * This routine is also responsible for identifying when disks have been
3659 * reconfigured in a new location. The kernel will have opened the device by
3660 * devid, but the path will still refer to the old location. To catch this, we
3661 * first do a path -> devid translation (which is fast for the common case). If
3662 * the devid matches, we're done. If not, we do a reverse devid -> path
3663 * translation and issue the appropriate ioctl() to update the path of the vdev.
3664 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3668 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3671 char *path, *type, *env;
3673 char buf[PATH_BUF_LEN];
3674 char tmpbuf[PATH_BUF_LEN];
3677 * vdev_name will be "root"/"root-0" for the root vdev, but it is the
3678 * zpool name that will be displayed to the user.
3680 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
3681 if (zhp != NULL && strcmp(type, "root") == 0)
3682 return (zfs_strdup(hdl, zpool_get_name(zhp)));
3684 env = getenv("ZPOOL_VDEV_NAME_PATH");
3685 if (env && (strtoul(env, NULL, 0) > 0 ||
3686 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3687 name_flags |= VDEV_NAME_PATH;
3689 env = getenv("ZPOOL_VDEV_NAME_GUID");
3690 if (env && (strtoul(env, NULL, 0) > 0 ||
3691 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3692 name_flags |= VDEV_NAME_GUID;
3694 env = getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
3695 if (env && (strtoul(env, NULL, 0) > 0 ||
3696 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3697 name_flags |= VDEV_NAME_FOLLOW_LINKS;
3699 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3700 name_flags & VDEV_NAME_GUID) {
3701 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value);
3702 (void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)value);
3704 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3705 #if defined(__sun__) || defined(__sun)
3707 * Live VDEV path updates to a kernel VDEV during a
3708 * zpool_vdev_name lookup are not supported on Linux.
3715 * If the device is dead (faulted, offline, etc) then don't
3716 * bother opening it. Otherwise we may be forcing the user to
3717 * open a misbehaving device, which can have undesirable
3720 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3721 (uint64_t **)&vs, &vsc) != 0 ||
3722 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3724 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3726 * Determine if the current path is correct.
3728 char *newdevid = path_to_devid(path);
3730 if (newdevid == NULL ||
3731 strcmp(devid, newdevid) != 0) {
3734 if ((newpath = devid_to_path(devid)) != NULL) {
3736 * Update the path appropriately.
3738 set_path(zhp, nv, newpath);
3739 if (nvlist_add_string(nv,
3740 ZPOOL_CONFIG_PATH, newpath) == 0)
3741 verify(nvlist_lookup_string(nv,
3749 devid_str_free(newdevid);
3753 if (name_flags & VDEV_NAME_FOLLOW_LINKS) {
3754 char *rp = realpath(path, NULL);
3756 strlcpy(buf, rp, sizeof (buf));
3763 * For a block device only use the name.
3765 if ((strcmp(type, VDEV_TYPE_DISK) == 0) &&
3766 !(name_flags & VDEV_NAME_PATH)) {
3767 path = strrchr(path, '/');
3772 * Remove the partition from the path it this is a whole disk.
3774 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, &value)
3775 == 0 && value && !(name_flags & VDEV_NAME_PATH)) {
3776 return (zfs_strip_partition(path));
3782 * If it's a raidz device, we need to stick in the parity level.
3784 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3785 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3787 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3788 (u_longlong_t)value);
3793 * We identify each top-level vdev by using a <type-id>
3794 * naming convention.
3796 if (name_flags & VDEV_NAME_TYPE_ID) {
3798 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3800 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s-%llu",
3801 path, (u_longlong_t)id);
3806 return (zfs_strdup(hdl, path));
3810 zbookmark_mem_compare(const void *a, const void *b)
3812 return (memcmp(a, b, sizeof (zbookmark_phys_t)));
3816 * Retrieve the persistent error log, uniquify the members, and return to the
3820 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3822 zfs_cmd_t zc = {"\0"};
3823 libzfs_handle_t *hdl = zhp->zpool_hdl;
3825 zbookmark_phys_t *zb = NULL;
3829 * Retrieve the raw error list from the kernel. If the number of errors
3830 * has increased, allocate more space and continue until we get the
3833 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3837 zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3838 count * sizeof (zbookmark_phys_t));
3839 zc.zc_nvlist_dst_size = count;
3840 (void) strcpy(zc.zc_name, zhp->zpool_name);
3842 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3844 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3845 if (errno == ENOMEM) {
3848 count = zc.zc_nvlist_dst_size;
3849 dst = zfs_alloc(zhp->zpool_hdl, count *
3850 sizeof (zbookmark_phys_t));
3851 zc.zc_nvlist_dst = (uintptr_t)dst;
3853 return (zpool_standard_error_fmt(hdl, errno,
3854 dgettext(TEXT_DOMAIN, "errors: List of "
3855 "errors unavailable")));
3863 * Sort the resulting bookmarks. This is a little confusing due to the
3864 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3865 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3866 * _not_ copied as part of the process. So we point the start of our
3867 * array appropriate and decrement the total number of elements.
3869 zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
3870 zc.zc_nvlist_dst_size;
3871 count -= zc.zc_nvlist_dst_size;
3873 qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
3875 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3878 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3880 for (i = 0; i < count; i++) {
3883 /* ignoring zb_blkid and zb_level for now */
3884 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3885 zb[i-1].zb_object == zb[i].zb_object)
3888 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3890 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3891 zb[i].zb_objset) != 0) {
3895 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3896 zb[i].zb_object) != 0) {
3900 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3907 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3911 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3912 return (no_memory(zhp->zpool_hdl));
3916 * Upgrade a ZFS pool to the latest on-disk version.
3919 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3921 zfs_cmd_t zc = {"\0"};
3922 libzfs_handle_t *hdl = zhp->zpool_hdl;
3924 (void) strcpy(zc.zc_name, zhp->zpool_name);
3925 zc.zc_cookie = new_version;
3927 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3928 return (zpool_standard_error_fmt(hdl, errno,
3929 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3935 zfs_save_arguments(int argc, char **argv, char *string, int len)
3939 (void) strlcpy(string, basename(argv[0]), len);
3940 for (i = 1; i < argc; i++) {
3941 (void) strlcat(string, " ", len);
3942 (void) strlcat(string, argv[i], len);
3947 zpool_log_history(libzfs_handle_t *hdl, const char *message)
3949 zfs_cmd_t zc = {"\0"};
3953 args = fnvlist_alloc();
3954 fnvlist_add_string(args, "message", message);
3955 err = zcmd_write_src_nvlist(hdl, &zc, args);
3957 err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
3959 zcmd_free_nvlists(&zc);
3964 * Perform ioctl to get some command history of a pool.
3966 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3967 * logical offset of the history buffer to start reading from.
3969 * Upon return, 'off' is the next logical offset to read from and
3970 * 'len' is the actual amount of bytes read into 'buf'.
3973 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3975 zfs_cmd_t zc = {"\0"};
3976 libzfs_handle_t *hdl = zhp->zpool_hdl;
3978 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3980 zc.zc_history = (uint64_t)(uintptr_t)buf;
3981 zc.zc_history_len = *len;
3982 zc.zc_history_offset = *off;
3984 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3987 return (zfs_error_fmt(hdl, EZFS_PERM,
3988 dgettext(TEXT_DOMAIN,
3989 "cannot show history for pool '%s'"),
3992 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3993 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3994 "'%s'"), zhp->zpool_name));
3996 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3997 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3998 "'%s', pool must be upgraded"), zhp->zpool_name));
4000 return (zpool_standard_error_fmt(hdl, errno,
4001 dgettext(TEXT_DOMAIN,
4002 "cannot get history for '%s'"), zhp->zpool_name));
4006 *len = zc.zc_history_len;
4007 *off = zc.zc_history_offset;
4013 * Process the buffer of nvlists, unpacking and storing each nvlist record
4014 * into 'records'. 'leftover' is set to the number of bytes that weren't
4015 * processed as there wasn't a complete record.
4018 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
4019 nvlist_t ***records, uint_t *numrecords)
4026 while (bytes_read > sizeof (reclen)) {
4028 /* get length of packed record (stored as little endian) */
4029 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
4030 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
4032 if (bytes_read < sizeof (reclen) + reclen)
4036 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
4038 bytes_read -= sizeof (reclen) + reclen;
4039 buf += sizeof (reclen) + reclen;
4041 /* add record to nvlist array */
4043 if (ISP2(*numrecords + 1)) {
4044 tmp = realloc(*records,
4045 *numrecords * 2 * sizeof (nvlist_t *));
4053 (*records)[*numrecords - 1] = nv;
4056 *leftover = bytes_read;
4061 * Retrieve the command history of a pool.
4064 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
4067 int buflen = 128 * 1024;
4069 nvlist_t **records = NULL;
4070 uint_t numrecords = 0;
4073 buf = malloc(buflen);
4077 uint64_t bytes_read = buflen;
4080 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
4083 /* if nothing else was read in, we're at EOF, just return */
4087 if ((err = zpool_history_unpack(buf, bytes_read,
4088 &leftover, &records, &numrecords)) != 0)
4091 if (leftover == bytes_read) {
4093 * no progress made, because buffer is not big enough
4094 * to hold this record; resize and retry.
4098 buf = malloc(buflen);
4109 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
4110 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
4111 records, numrecords) == 0);
4113 for (i = 0; i < numrecords; i++)
4114 nvlist_free(records[i]);
4121 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
4122 * If there is a new event available 'nvp' will contain a newly allocated
4123 * nvlist and 'dropped' will be set to the number of missed events since
4124 * the last call to this function. When 'nvp' is set to NULL it indicates
4125 * no new events are available. In either case the function returns 0 and
4126 * it is up to the caller to free 'nvp'. In the case of a fatal error the
4127 * function will return a non-zero value. When the function is called in
4128 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
4129 * it will not return until a new event is available.
4132 zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
4133 int *dropped, unsigned flags, int zevent_fd)
4135 zfs_cmd_t zc = {"\0"};
4140 zc.zc_cleanup_fd = zevent_fd;
4142 if (flags & ZEVENT_NONBLOCK)
4143 zc.zc_guid = ZEVENT_NONBLOCK;
4145 if (zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE) != 0)
4149 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
4152 error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
4153 dgettext(TEXT_DOMAIN, "zfs shutdown"));
4156 /* Blocking error case should not occur */
4157 if (!(flags & ZEVENT_NONBLOCK))
4158 error = zpool_standard_error_fmt(hdl, errno,
4159 dgettext(TEXT_DOMAIN, "cannot get event"));
4163 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
4164 error = zfs_error_fmt(hdl, EZFS_NOMEM,
4165 dgettext(TEXT_DOMAIN, "cannot get event"));
4171 error = zpool_standard_error_fmt(hdl, errno,
4172 dgettext(TEXT_DOMAIN, "cannot get event"));
4177 error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
4181 *dropped = (int)zc.zc_cookie;
4183 zcmd_free_nvlists(&zc);
4192 zpool_events_clear(libzfs_handle_t *hdl, int *count)
4194 zfs_cmd_t zc = {"\0"};
4197 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
4198 "cannot clear events"));
4200 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
4201 return (zpool_standard_error_fmt(hdl, errno, msg));
4204 *count = (int)zc.zc_cookie; /* # of events cleared */
4210 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4211 * the passed zevent_fd file handle. On success zero is returned,
4212 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4215 zpool_events_seek(libzfs_handle_t *hdl, uint64_t eid, int zevent_fd)
4217 zfs_cmd_t zc = {"\0"};
4221 zc.zc_cleanup_fd = zevent_fd;
4223 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_SEEK, &zc) != 0) {
4226 error = zfs_error_fmt(hdl, EZFS_NOENT,
4227 dgettext(TEXT_DOMAIN, "cannot get event"));
4231 error = zfs_error_fmt(hdl, EZFS_NOMEM,
4232 dgettext(TEXT_DOMAIN, "cannot get event"));
4236 error = zpool_standard_error_fmt(hdl, errno,
4237 dgettext(TEXT_DOMAIN, "cannot get event"));
4246 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4247 char *pathname, size_t len)
4249 zfs_cmd_t zc = {"\0"};
4250 boolean_t mounted = B_FALSE;
4251 char *mntpnt = NULL;
4252 char dsname[ZFS_MAX_DATASET_NAME_LEN];
4255 /* special case for the MOS */
4256 (void) snprintf(pathname, len, "<metadata>:<0x%llx>",
4261 /* get the dataset's name */
4262 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4264 if (ioctl(zhp->zpool_hdl->libzfs_fd,
4265 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
4266 /* just write out a path of two object numbers */
4267 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
4268 (longlong_t)dsobj, (longlong_t)obj);
4271 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
4273 /* find out if the dataset is mounted */
4274 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
4276 /* get the corrupted object's path */
4277 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
4279 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
4282 (void) snprintf(pathname, len, "%s%s", mntpnt,
4285 (void) snprintf(pathname, len, "%s:%s",
4286 dsname, zc.zc_value);
4289 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname,
4296 * Read the EFI label from the config, if a label does not exist then
4297 * pass back the error to the caller. If the caller has passed a non-NULL
4298 * diskaddr argument then we set it to the starting address of the EFI
4302 read_efi_label(nvlist_t *config, diskaddr_t *sb)
4306 char diskname[MAXPATHLEN];
4309 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
4312 (void) snprintf(diskname, sizeof (diskname), "%s%s", DISK_ROOT,
4313 strrchr(path, '/'));
4314 if ((fd = open(diskname, O_RDONLY|O_DIRECT)) >= 0) {
4315 struct dk_gpt *vtoc;
4317 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
4319 *sb = vtoc->efi_parts[0].p_start;
4328 * determine where a partition starts on a disk in the current
4332 find_start_block(nvlist_t *config)
4336 diskaddr_t sb = MAXOFFSET_T;
4339 if (nvlist_lookup_nvlist_array(config,
4340 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
4341 if (nvlist_lookup_uint64(config,
4342 ZPOOL_CONFIG_WHOLE_DISK,
4343 &wholedisk) != 0 || !wholedisk) {
4344 return (MAXOFFSET_T);
4346 if (read_efi_label(config, &sb) < 0)
4351 for (c = 0; c < children; c++) {
4352 sb = find_start_block(child[c]);
4353 if (sb != MAXOFFSET_T) {
4357 return (MAXOFFSET_T);
4361 zpool_label_disk_check(char *path)
4363 struct dk_gpt *vtoc;
4366 if ((fd = open(path, O_RDONLY|O_DIRECT)) < 0)
4369 if ((err = efi_alloc_and_read(fd, &vtoc)) != 0) {
4374 if (vtoc->efi_flags & EFI_GPT_PRIMARY_CORRUPT) {
4386 * Generate a unique partition name for the ZFS member. Partitions must
4387 * have unique names to ensure udev will be able to create symlinks under
4388 * /dev/disk/by-partlabel/ for all pool members. The partition names are
4389 * of the form <pool>-<unique-id>.
4392 zpool_label_name(char *label_name, int label_size)
4397 fd = open("/dev/urandom", O_RDONLY);
4399 if (read(fd, &id, sizeof (id)) != sizeof (id))
4406 id = (((uint64_t)rand()) << 32) | (uint64_t)rand();
4408 snprintf(label_name, label_size, "zfs-%016llx", (u_longlong_t)id);
4412 * Label an individual disk. The name provided is the short name,
4413 * stripped of any leading /dev path.
4416 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
4418 char path[MAXPATHLEN];
4419 struct dk_gpt *vtoc;
4421 size_t resv = EFI_MIN_RESV_SIZE;
4422 uint64_t slice_size;
4423 diskaddr_t start_block;
4426 /* prepare an error message just in case */
4427 (void) snprintf(errbuf, sizeof (errbuf),
4428 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
4433 verify(nvlist_lookup_nvlist(zhp->zpool_config,
4434 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4436 if (zhp->zpool_start_block == 0)
4437 start_block = find_start_block(nvroot);
4439 start_block = zhp->zpool_start_block;
4440 zhp->zpool_start_block = start_block;
4443 start_block = NEW_START_BLOCK;
4446 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
4448 if ((fd = open(path, O_RDWR|O_DIRECT|O_EXCL)) < 0) {
4450 * This shouldn't happen. We've long since verified that this
4451 * is a valid device.
4453 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
4454 "label '%s': unable to open device: %d"), path, errno);
4455 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
4458 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
4460 * The only way this can fail is if we run out of memory, or we
4461 * were unable to read the disk's capacity
4463 if (errno == ENOMEM)
4464 (void) no_memory(hdl);
4467 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
4468 "label '%s': unable to read disk capacity"), path);
4470 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
4473 slice_size = vtoc->efi_last_u_lba + 1;
4474 slice_size -= EFI_MIN_RESV_SIZE;
4475 if (start_block == MAXOFFSET_T)
4476 start_block = NEW_START_BLOCK;
4477 slice_size -= start_block;
4478 slice_size = P2ALIGN(slice_size, PARTITION_END_ALIGNMENT);
4480 vtoc->efi_parts[0].p_start = start_block;
4481 vtoc->efi_parts[0].p_size = slice_size;
4484 * Why we use V_USR: V_BACKUP confuses users, and is considered
4485 * disposable by some EFI utilities (since EFI doesn't have a backup
4486 * slice). V_UNASSIGNED is supposed to be used only for zero size
4487 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4488 * etc. were all pretty specific. V_USR is as close to reality as we
4489 * can get, in the absence of V_OTHER.
4491 vtoc->efi_parts[0].p_tag = V_USR;
4492 zpool_label_name(vtoc->efi_parts[0].p_name, EFI_PART_NAME_LEN);
4494 vtoc->efi_parts[8].p_start = slice_size + start_block;
4495 vtoc->efi_parts[8].p_size = resv;
4496 vtoc->efi_parts[8].p_tag = V_RESERVED;
4498 rval = efi_write(fd, vtoc);
4500 /* Flush the buffers to disk and invalidate the page cache. */
4502 (void) ioctl(fd, BLKFLSBUF);
4505 rval = efi_rescan(fd);
4508 * Some block drivers (like pcata) may not support EFI GPT labels.
4509 * Print out a helpful error message directing the user to manually
4510 * label the disk and give a specific slice.
4516 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "try using "
4517 "parted(8) and then provide a specific slice: %d"), rval);
4518 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4524 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
4525 (void) zfs_append_partition(path, MAXPATHLEN);
4527 /* Wait to udev to signal use the device has settled. */
4528 rval = zpool_label_disk_wait(path, DISK_LABEL_WAIT);
4530 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "failed to "
4531 "detect device partitions on '%s': %d"), path, rval);
4532 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4535 /* We can't be to paranoid. Read the label back and verify it. */
4536 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
4537 rval = zpool_label_disk_check(path);
4539 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "freshly written "
4540 "EFI label on '%s' is damaged. Ensure\nthis device "
4541 "is not in in use, and is functioning properly: %d"),
4543 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4550 * Allocate and return the underlying device name for a device mapper device.
4551 * If a device mapper device maps to multiple devices, return the first device.
4553 * For example, dm_name = "/dev/dm-0" could return "/dev/sda". Symlinks to a
4554 * DM device (like /dev/disk/by-vdev/A0) are also allowed.
4556 * Returns device name, or NULL on error or no match. If dm_name is not a DM
4557 * device then return NULL.
4559 * NOTE: The returned name string must be *freed*.
4562 dm_get_underlying_path(char *dm_name)
4572 if (dm_name == NULL)
4575 /* dm name may be a symlink (like /dev/disk/by-vdev/A0) */
4576 realp = realpath(dm_name, NULL);
4581 * If they preface 'dev' with a path (like "/dev") then strip it off.
4582 * We just want the 'dm-N' part.
4584 tmp = strrchr(realp, '/');
4586 dev_str = tmp + 1; /* +1 since we want the chr after '/' */
4590 size = asprintf(&tmp, "/sys/block/%s/slaves/", dev_str);
4591 if (size == -1 || !tmp)
4598 /* Return first sd* entry in /sys/block/dm-N/slaves/ */
4599 while ((ep = readdir(dp))) {
4600 if (ep->d_type != DT_DIR) { /* skip "." and ".." dirs */
4601 size = asprintf(&path, "/dev/%s", ep->d_name);
4615 * Return 1 if device is a device mapper or multipath device.
4619 zfs_dev_is_dm(char *dev_name)
4623 tmp = dm_get_underlying_path(dev_name);
4632 * By "whole disk" we mean an entire physical disk (something we can
4633 * label, toggle the write cache on, etc.) as opposed to the full
4634 * capacity of a pseudo-device such as lofi or did. We act as if we
4635 * are labeling the disk, which should be a pretty good test of whether
4636 * it's a viable device or not. Returns B_TRUE if it is and B_FALSE if
4640 zfs_dev_is_whole_disk(char *dev_name)
4642 struct dk_gpt *label;
4645 if ((fd = open(dev_name, O_RDONLY | O_DIRECT)) < 0)
4648 if (efi_alloc_and_init(fd, EFI_NUMPAR, &label) != 0) {
4660 * Lookup the underlying device for a device name
4662 * Often you'll have a symlink to a device, a partition device,
4663 * or a multipath device, and want to look up the underlying device.
4664 * This function returns the underlying device name. If the device
4665 * name is already the underlying device, then just return the same
4666 * name. If the device is a DM device with multiple underlying devices
4667 * then return the first one.
4671 * 1. /dev/disk/by-id/ata-QEMU_HARDDISK_QM00001 -> ../../sda
4672 * dev_name: /dev/disk/by-id/ata-QEMU_HARDDISK_QM00001
4675 * 2. /dev/mapper/mpatha (made up of /dev/sda and /dev/sdb)
4676 * dev_name: /dev/mapper/mpatha
4677 * returns: /dev/sda (first device)
4679 * 3. /dev/sda (already the underlying device)
4680 * dev_name: /dev/sda
4683 * 4. /dev/dm-3 (mapped to /dev/sda)
4684 * dev_name: /dev/dm-3
4687 * 5. /dev/disk/by-id/scsi-0QEMU_drive-scsi0-0-0-0-part9 -> ../../sdb9
4688 * dev_name: /dev/disk/by-id/scsi-0QEMU_drive-scsi0-0-0-0-part9
4691 * 6. /dev/disk/by-uuid/5df030cf-3cd9-46e4-8e99-3ccb462a4e9a -> ../dev/sda2
4692 * dev_name: /dev/disk/by-uuid/5df030cf-3cd9-46e4-8e99-3ccb462a4e9a
4695 * Returns underlying device name, or NULL on error or no match.
4697 * NOTE: The returned name string must be *freed*.
4700 zfs_get_underlying_path(char *dev_name)
4705 if (dev_name == NULL)
4708 tmp = dm_get_underlying_path(dev_name);
4710 /* dev_name not a DM device, so just un-symlinkize it */
4712 tmp = realpath(dev_name, NULL);
4715 name = zfs_strip_partition_path(tmp);
4723 * Given a dev name like "sda", return the full enclosure sysfs path to
4724 * the disk. You can also pass in the name with "/dev" prepended
4725 * to it (like /dev/sda).
4727 * For example, disk "sda" in enclosure slot 1:
4729 * returns: "/sys/class/enclosure/1:0:3:0/Slot 1"
4731 * 'dev' must be a non-devicemapper device.
4733 * Returned string must be freed.
4736 zfs_get_enclosure_sysfs_path(char *dev_name)
4740 char buf[MAXPATHLEN];
4748 if (dev_name == NULL)
4751 /* If they preface 'dev' with a path (like "/dev") then strip it off */
4752 tmp1 = strrchr(dev_name, '/');
4754 dev_name = tmp1 + 1; /* +1 since we want the chr after '/' */
4756 tmpsize = asprintf(&tmp1, "/sys/block/%s/device", dev_name);
4757 if (tmpsize == -1 || tmp1 == NULL) {
4764 tmp1 = NULL; /* To make free() at the end a NOP */
4769 * Look though all sysfs entries in /sys/block/<dev>/device for
4770 * the enclosure symlink.
4772 while ((ep = readdir(dp))) {
4773 /* Ignore everything that's not our enclosure_device link */
4774 if (strstr(ep->d_name, "enclosure_device") == NULL)
4777 if (asprintf(&tmp2, "%s/%s", tmp1, ep->d_name) == -1 ||
4781 size = readlink(tmp2, buf, sizeof (buf));
4783 /* Did readlink fail or crop the link name? */
4784 if (size == -1 || size >= sizeof (buf)) {
4786 tmp2 = NULL; /* To make free() at the end a NOP */
4791 * We got a valid link. readlink() doesn't terminate strings
4792 * so we have to do it.
4797 * Our link will look like:
4799 * "../../../../port-11:1:2/..STUFF../enclosure/1:0:3:0/SLOT 1"
4801 * We want to grab the "enclosure/1:0:3:0/SLOT 1" part
4803 tmp3 = strstr(buf, "enclosure");
4807 if (asprintf(&path, "/sys/class/%s", tmp3) == -1) {
4808 /* If asprintf() fails, 'path' is undefined */