4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27 * Copyright (c) 2017 Datto Inc.
42 #include <sys/efi_partition.h>
43 #include <sys/systeminfo.h>
45 #include <sys/zfs_ioctl.h>
48 #include "zfs_namecheck.h"
50 #include "libzfs_impl.h"
51 #include "zfs_comutil.h"
52 #include "zfeature_common.h"
54 static int read_efi_label(nvlist_t *config, diskaddr_t *sb);
56 typedef struct prop_flags {
57 int create:1; /* Validate property on creation */
58 int import:1; /* Validate property on import */
62 * ====================================================================
63 * zpool property functions
64 * ====================================================================
68 zpool_get_all_props(zpool_handle_t *zhp)
70 zfs_cmd_t zc = {"\0"};
71 libzfs_handle_t *hdl = zhp->zpool_hdl;
73 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
75 if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0)
78 while (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_PROPS, &zc) != 0) {
79 if (errno == ENOMEM) {
80 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
81 zcmd_free_nvlists(&zc);
85 zcmd_free_nvlists(&zc);
90 if (zcmd_read_dst_nvlist(hdl, &zc, &zhp->zpool_props) != 0) {
91 zcmd_free_nvlists(&zc);
95 zcmd_free_nvlists(&zc);
101 zpool_props_refresh(zpool_handle_t *zhp)
105 old_props = zhp->zpool_props;
107 if (zpool_get_all_props(zhp) != 0)
110 nvlist_free(old_props);
115 zpool_get_prop_string(zpool_handle_t *zhp, zpool_prop_t prop,
121 zprop_source_t source;
123 nvl = zhp->zpool_props;
124 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
125 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &ival) == 0);
127 verify(nvlist_lookup_string(nv, ZPROP_VALUE, &value) == 0);
129 source = ZPROP_SRC_DEFAULT;
130 if ((value = (char *)zpool_prop_default_string(prop)) == NULL)
141 zpool_get_prop_int(zpool_handle_t *zhp, zpool_prop_t prop, zprop_source_t *src)
145 zprop_source_t source;
147 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp)) {
149 * zpool_get_all_props() has most likely failed because
150 * the pool is faulted, but if all we need is the top level
151 * vdev's guid then get it from the zhp config nvlist.
153 if ((prop == ZPOOL_PROP_GUID) &&
154 (nvlist_lookup_nvlist(zhp->zpool_config,
155 ZPOOL_CONFIG_VDEV_TREE, &nv) == 0) &&
156 (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value)
160 return (zpool_prop_default_numeric(prop));
163 nvl = zhp->zpool_props;
164 if (nvlist_lookup_nvlist(nvl, zpool_prop_to_name(prop), &nv) == 0) {
165 verify(nvlist_lookup_uint64(nv, ZPROP_SOURCE, &value) == 0);
167 verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0);
169 source = ZPROP_SRC_DEFAULT;
170 value = zpool_prop_default_numeric(prop);
180 * Map VDEV STATE to printed strings.
183 zpool_state_to_name(vdev_state_t state, vdev_aux_t aux)
186 case VDEV_STATE_CLOSED:
187 case VDEV_STATE_OFFLINE:
188 return (gettext("OFFLINE"));
189 case VDEV_STATE_REMOVED:
190 return (gettext("REMOVED"));
191 case VDEV_STATE_CANT_OPEN:
192 if (aux == VDEV_AUX_CORRUPT_DATA || aux == VDEV_AUX_BAD_LOG)
193 return (gettext("FAULTED"));
194 else if (aux == VDEV_AUX_SPLIT_POOL)
195 return (gettext("SPLIT"));
197 return (gettext("UNAVAIL"));
198 case VDEV_STATE_FAULTED:
199 return (gettext("FAULTED"));
200 case VDEV_STATE_DEGRADED:
201 return (gettext("DEGRADED"));
202 case VDEV_STATE_HEALTHY:
203 return (gettext("ONLINE"));
209 return (gettext("UNKNOWN"));
213 * Map POOL STATE to printed strings.
216 zpool_pool_state_to_name(pool_state_t state)
221 case POOL_STATE_ACTIVE:
222 return (gettext("ACTIVE"));
223 case POOL_STATE_EXPORTED:
224 return (gettext("EXPORTED"));
225 case POOL_STATE_DESTROYED:
226 return (gettext("DESTROYED"));
227 case POOL_STATE_SPARE:
228 return (gettext("SPARE"));
229 case POOL_STATE_L2CACHE:
230 return (gettext("L2CACHE"));
231 case POOL_STATE_UNINITIALIZED:
232 return (gettext("UNINITIALIZED"));
233 case POOL_STATE_UNAVAIL:
234 return (gettext("UNAVAIL"));
235 case POOL_STATE_POTENTIALLY_ACTIVE:
236 return (gettext("POTENTIALLY_ACTIVE"));
239 return (gettext("UNKNOWN"));
243 * Get a zpool property value for 'prop' and return the value in
244 * a pre-allocated buffer.
247 zpool_get_prop(zpool_handle_t *zhp, zpool_prop_t prop, char *buf,
248 size_t len, zprop_source_t *srctype, boolean_t literal)
252 zprop_source_t src = ZPROP_SRC_NONE;
257 if (zpool_get_state(zhp) == POOL_STATE_UNAVAIL) {
259 case ZPOOL_PROP_NAME:
260 (void) strlcpy(buf, zpool_get_name(zhp), len);
263 case ZPOOL_PROP_HEALTH:
264 (void) strlcpy(buf, "FAULTED", len);
267 case ZPOOL_PROP_GUID:
268 intval = zpool_get_prop_int(zhp, prop, &src);
269 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
272 case ZPOOL_PROP_ALTROOT:
273 case ZPOOL_PROP_CACHEFILE:
274 case ZPOOL_PROP_COMMENT:
275 if (zhp->zpool_props != NULL ||
276 zpool_get_all_props(zhp) == 0) {
278 zpool_get_prop_string(zhp, prop, &src),
284 (void) strlcpy(buf, "-", len);
293 if (zhp->zpool_props == NULL && zpool_get_all_props(zhp) &&
294 prop != ZPOOL_PROP_NAME)
297 switch (zpool_prop_get_type(prop)) {
298 case PROP_TYPE_STRING:
299 (void) strlcpy(buf, zpool_get_prop_string(zhp, prop, &src),
303 case PROP_TYPE_NUMBER:
304 intval = zpool_get_prop_int(zhp, prop, &src);
307 case ZPOOL_PROP_SIZE:
308 case ZPOOL_PROP_ALLOCATED:
309 case ZPOOL_PROP_FREE:
310 case ZPOOL_PROP_FREEING:
311 case ZPOOL_PROP_LEAKED:
312 case ZPOOL_PROP_ASHIFT:
314 (void) snprintf(buf, len, "%llu",
315 (u_longlong_t)intval);
317 (void) zfs_nicenum(intval, buf, len);
320 case ZPOOL_PROP_EXPANDSZ:
322 (void) strlcpy(buf, "-", len);
323 } else if (literal) {
324 (void) snprintf(buf, len, "%llu",
325 (u_longlong_t)intval);
327 (void) zfs_nicebytes(intval, buf, len);
331 case ZPOOL_PROP_CAPACITY:
333 (void) snprintf(buf, len, "%llu",
334 (u_longlong_t)intval);
336 (void) snprintf(buf, len, "%llu%%",
337 (u_longlong_t)intval);
341 case ZPOOL_PROP_FRAGMENTATION:
342 if (intval == UINT64_MAX) {
343 (void) strlcpy(buf, "-", len);
344 } else if (literal) {
345 (void) snprintf(buf, len, "%llu",
346 (u_longlong_t)intval);
348 (void) snprintf(buf, len, "%llu%%",
349 (u_longlong_t)intval);
353 case ZPOOL_PROP_DEDUPRATIO:
355 (void) snprintf(buf, len, "%llu.%02llu",
356 (u_longlong_t)(intval / 100),
357 (u_longlong_t)(intval % 100));
359 (void) snprintf(buf, len, "%llu.%02llux",
360 (u_longlong_t)(intval / 100),
361 (u_longlong_t)(intval % 100));
364 case ZPOOL_PROP_HEALTH:
365 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
366 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
367 verify(nvlist_lookup_uint64_array(nvroot,
368 ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &vsc)
371 (void) strlcpy(buf, zpool_state_to_name(intval,
374 case ZPOOL_PROP_VERSION:
375 if (intval >= SPA_VERSION_FEATURES) {
376 (void) snprintf(buf, len, "-");
381 (void) snprintf(buf, len, "%llu", (u_longlong_t)intval);
385 case PROP_TYPE_INDEX:
386 intval = zpool_get_prop_int(zhp, prop, &src);
387 if (zpool_prop_index_to_string(prop, intval, &strval)
390 (void) strlcpy(buf, strval, len);
404 * Check if the bootfs name has the same pool name as it is set to.
405 * Assuming bootfs is a valid dataset name.
408 bootfs_name_valid(const char *pool, char *bootfs)
410 int len = strlen(pool);
412 if (!zfs_name_valid(bootfs, ZFS_TYPE_FILESYSTEM|ZFS_TYPE_SNAPSHOT))
415 if (strncmp(pool, bootfs, len) == 0 &&
416 (bootfs[len] == '/' || bootfs[len] == '\0'))
423 zpool_is_bootable(zpool_handle_t *zhp)
425 char bootfs[ZFS_MAX_DATASET_NAME_LEN];
427 return (zpool_get_prop(zhp, ZPOOL_PROP_BOOTFS, bootfs,
428 sizeof (bootfs), NULL, B_FALSE) == 0 && strncmp(bootfs, "-",
429 sizeof (bootfs)) != 0);
434 * Given an nvlist of zpool properties to be set, validate that they are
435 * correct, and parse any numeric properties (index, boolean, etc) if they are
436 * specified as strings.
439 zpool_valid_proplist(libzfs_handle_t *hdl, const char *poolname,
440 nvlist_t *props, uint64_t version, prop_flags_t flags, char *errbuf)
448 struct stat64 statbuf;
451 if (nvlist_alloc(&retprops, NV_UNIQUE_NAME, 0) != 0) {
452 (void) no_memory(hdl);
457 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
458 const char *propname = nvpair_name(elem);
460 prop = zpool_name_to_prop(propname);
461 if (prop == ZPROP_INVAL && zpool_prop_feature(propname)) {
463 char *fname = strchr(propname, '@') + 1;
465 err = zfeature_lookup_name(fname, NULL);
467 ASSERT3U(err, ==, ENOENT);
468 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
469 "invalid feature '%s'"), fname);
470 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
474 if (nvpair_type(elem) != DATA_TYPE_STRING) {
475 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
476 "'%s' must be a string"), propname);
477 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
481 (void) nvpair_value_string(elem, &strval);
482 if (strcmp(strval, ZFS_FEATURE_ENABLED) != 0 &&
483 strcmp(strval, ZFS_FEATURE_DISABLED) != 0) {
484 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
485 "property '%s' can only be set to "
486 "'enabled' or 'disabled'"), propname);
487 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
491 if (nvlist_add_uint64(retprops, propname, 0) != 0) {
492 (void) no_memory(hdl);
499 * Make sure this property is valid and applies to this type.
501 if (prop == ZPROP_INVAL) {
502 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
503 "invalid property '%s'"), propname);
504 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
508 if (zpool_prop_readonly(prop)) {
509 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
510 "is readonly"), propname);
511 (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf);
515 if (zprop_parse_value(hdl, elem, prop, ZFS_TYPE_POOL, retprops,
516 &strval, &intval, errbuf) != 0)
520 * Perform additional checking for specific properties.
523 case ZPOOL_PROP_VERSION:
524 if (intval < version ||
525 !SPA_VERSION_IS_SUPPORTED(intval)) {
526 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
527 "property '%s' number %d is invalid."),
529 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
534 case ZPOOL_PROP_ASHIFT:
536 (intval < ASHIFT_MIN || intval > ASHIFT_MAX)) {
537 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
538 "invalid '%s=%d' property: only values "
539 "between %" PRId32 " and %" PRId32 " "
541 propname, intval, ASHIFT_MIN, ASHIFT_MAX);
542 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
547 case ZPOOL_PROP_BOOTFS:
548 if (flags.create || flags.import) {
549 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
550 "property '%s' cannot be set at creation "
551 "or import time"), propname);
552 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
556 if (version < SPA_VERSION_BOOTFS) {
557 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
558 "pool must be upgraded to support "
559 "'%s' property"), propname);
560 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
565 * bootfs property value has to be a dataset name and
566 * the dataset has to be in the same pool as it sets to.
568 if (strval[0] != '\0' && !bootfs_name_valid(poolname,
570 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "'%s' "
571 "is an invalid name"), strval);
572 (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
576 if ((zhp = zpool_open_canfail(hdl, poolname)) == NULL) {
577 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
578 "could not open pool '%s'"), poolname);
579 (void) zfs_error(hdl, EZFS_OPENFAILED, errbuf);
585 case ZPOOL_PROP_ALTROOT:
586 if (!flags.create && !flags.import) {
587 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
588 "property '%s' can only be set during pool "
589 "creation or import"), propname);
590 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
594 if (strval[0] != '/') {
595 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
596 "bad alternate root '%s'"), strval);
597 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
602 case ZPOOL_PROP_CACHEFILE:
603 if (strval[0] == '\0')
606 if (strcmp(strval, "none") == 0)
609 if (strval[0] != '/') {
610 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
611 "property '%s' must be empty, an "
612 "absolute path, or 'none'"), propname);
613 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
617 slash = strrchr(strval, '/');
619 if (slash[1] == '\0' || strcmp(slash, "/.") == 0 ||
620 strcmp(slash, "/..") == 0) {
621 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
622 "'%s' is not a valid file"), strval);
623 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
629 if (strval[0] != '\0' &&
630 (stat64(strval, &statbuf) != 0 ||
631 !S_ISDIR(statbuf.st_mode))) {
632 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
633 "'%s' is not a valid directory"),
635 (void) zfs_error(hdl, EZFS_BADPATH, errbuf);
642 case ZPOOL_PROP_COMMENT:
643 for (check = strval; *check != '\0'; check++) {
644 if (!isprint(*check)) {
646 dgettext(TEXT_DOMAIN,
647 "comment may only have printable "
649 (void) zfs_error(hdl, EZFS_BADPROP,
654 if (strlen(strval) > ZPROP_MAX_COMMENT) {
655 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
656 "comment must not exceed %d characters"),
658 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
662 case ZPOOL_PROP_READONLY:
664 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
665 "property '%s' can only be set at "
666 "import time"), propname);
667 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
671 case ZPOOL_PROP_TNAME:
673 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
674 "property '%s' can only be set at "
675 "creation time"), propname);
676 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
680 case ZPOOL_PROP_MULTIHOST:
681 if (get_system_hostid() == 0) {
682 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
683 "requires a non-zero system hostid"));
684 (void) zfs_error(hdl, EZFS_BADPROP, errbuf);
689 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
690 "property '%s'(%d) not defined"), propname, prop);
697 nvlist_free(retprops);
702 * Set zpool property : propname=propval.
705 zpool_set_prop(zpool_handle_t *zhp, const char *propname, const char *propval)
707 zfs_cmd_t zc = {"\0"};
710 nvlist_t *nvl = NULL;
713 prop_flags_t flags = { 0 };
715 (void) snprintf(errbuf, sizeof (errbuf),
716 dgettext(TEXT_DOMAIN, "cannot set property for '%s'"),
719 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0)
720 return (no_memory(zhp->zpool_hdl));
722 if (nvlist_add_string(nvl, propname, propval) != 0) {
724 return (no_memory(zhp->zpool_hdl));
727 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
728 if ((realprops = zpool_valid_proplist(zhp->zpool_hdl,
729 zhp->zpool_name, nvl, version, flags, errbuf)) == NULL) {
738 * Execute the corresponding ioctl() to set this property.
740 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
742 if (zcmd_write_src_nvlist(zhp->zpool_hdl, &zc, nvl) != 0) {
747 ret = zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_SET_PROPS, &zc);
749 zcmd_free_nvlists(&zc);
753 (void) zpool_standard_error(zhp->zpool_hdl, errno, errbuf);
755 (void) zpool_props_refresh(zhp);
761 zpool_expand_proplist(zpool_handle_t *zhp, zprop_list_t **plp)
763 libzfs_handle_t *hdl = zhp->zpool_hdl;
765 char buf[ZFS_MAXPROPLEN];
766 nvlist_t *features = NULL;
769 boolean_t firstexpand = (NULL == *plp);
772 if (zprop_expand_list(hdl, plp, ZFS_TYPE_POOL) != 0)
776 while (*last != NULL)
777 last = &(*last)->pl_next;
780 features = zpool_get_features(zhp);
782 if ((*plp)->pl_all && firstexpand) {
783 for (i = 0; i < SPA_FEATURES; i++) {
784 zprop_list_t *entry = zfs_alloc(hdl,
785 sizeof (zprop_list_t));
786 entry->pl_prop = ZPROP_INVAL;
787 entry->pl_user_prop = zfs_asprintf(hdl, "feature@%s",
788 spa_feature_table[i].fi_uname);
789 entry->pl_width = strlen(entry->pl_user_prop);
790 entry->pl_all = B_TRUE;
793 last = &entry->pl_next;
797 /* add any unsupported features */
798 for (nvp = nvlist_next_nvpair(features, NULL);
799 nvp != NULL; nvp = nvlist_next_nvpair(features, nvp)) {
804 if (zfeature_is_supported(nvpair_name(nvp)))
807 propname = zfs_asprintf(hdl, "unsupported@%s",
811 * Before adding the property to the list make sure that no
812 * other pool already added the same property.
816 while (entry != NULL) {
817 if (entry->pl_user_prop != NULL &&
818 strcmp(propname, entry->pl_user_prop) == 0) {
822 entry = entry->pl_next;
829 entry = zfs_alloc(hdl, sizeof (zprop_list_t));
830 entry->pl_prop = ZPROP_INVAL;
831 entry->pl_user_prop = propname;
832 entry->pl_width = strlen(entry->pl_user_prop);
833 entry->pl_all = B_TRUE;
836 last = &entry->pl_next;
839 for (entry = *plp; entry != NULL; entry = entry->pl_next) {
844 if (entry->pl_prop != ZPROP_INVAL &&
845 zpool_get_prop(zhp, entry->pl_prop, buf, sizeof (buf),
846 NULL, B_FALSE) == 0) {
847 if (strlen(buf) > entry->pl_width)
848 entry->pl_width = strlen(buf);
856 * Get the state for the given feature on the given ZFS pool.
859 zpool_prop_get_feature(zpool_handle_t *zhp, const char *propname, char *buf,
863 boolean_t found = B_FALSE;
864 nvlist_t *features = zpool_get_features(zhp);
866 const char *feature = strchr(propname, '@') + 1;
868 supported = zpool_prop_feature(propname);
869 ASSERT(supported || zpool_prop_unsupported(propname));
872 * Convert from feature name to feature guid. This conversion is
873 * unnecessary for unsupported@... properties because they already
880 ret = zfeature_lookup_name(feature, &fid);
882 (void) strlcpy(buf, "-", len);
885 feature = spa_feature_table[fid].fi_guid;
888 if (nvlist_lookup_uint64(features, feature, &refcount) == 0)
893 (void) strlcpy(buf, ZFS_FEATURE_DISABLED, len);
896 (void) strlcpy(buf, ZFS_FEATURE_ENABLED, len);
898 (void) strlcpy(buf, ZFS_FEATURE_ACTIVE, len);
903 (void) strcpy(buf, ZFS_UNSUPPORTED_INACTIVE);
905 (void) strcpy(buf, ZFS_UNSUPPORTED_READONLY);
908 (void) strlcpy(buf, "-", len);
917 * Don't start the slice at the default block of 34; many storage
918 * devices will use a stripe width of 128k, other vendors prefer a 1m
919 * alignment. It is best to play it safe and ensure a 1m alignment
920 * given 512B blocks. When the block size is larger by a power of 2
921 * we will still be 1m aligned. Some devices are sensitive to the
922 * partition ending alignment as well.
924 #define NEW_START_BLOCK 2048
925 #define PARTITION_END_ALIGNMENT 2048
928 * Validate the given pool name, optionally putting an extended error message in
932 zpool_name_valid(libzfs_handle_t *hdl, boolean_t isopen, const char *pool)
938 ret = pool_namecheck(pool, &why, &what);
941 * The rules for reserved pool names were extended at a later point.
942 * But we need to support users with existing pools that may now be
943 * invalid. So we only check for this expanded set of names during a
944 * create (or import), and only in userland.
946 if (ret == 0 && !isopen &&
947 (strncmp(pool, "mirror", 6) == 0 ||
948 strncmp(pool, "raidz", 5) == 0 ||
949 strncmp(pool, "spare", 5) == 0 ||
950 strcmp(pool, "log") == 0)) {
953 dgettext(TEXT_DOMAIN, "name is reserved"));
961 case NAME_ERR_TOOLONG:
963 dgettext(TEXT_DOMAIN, "name is too long"));
966 case NAME_ERR_INVALCHAR:
968 dgettext(TEXT_DOMAIN, "invalid character "
969 "'%c' in pool name"), what);
972 case NAME_ERR_NOLETTER:
973 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
974 "name must begin with a letter"));
977 case NAME_ERR_RESERVED:
978 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
979 "name is reserved"));
982 case NAME_ERR_DISKLIKE:
983 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
984 "pool name is reserved"));
987 case NAME_ERR_LEADING_SLASH:
988 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
989 "leading slash in name"));
992 case NAME_ERR_EMPTY_COMPONENT:
993 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
994 "empty component in name"));
997 case NAME_ERR_TRAILING_SLASH:
998 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
999 "trailing slash in name"));
1002 case NAME_ERR_MULTIPLE_DELIMITERS:
1003 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1004 "multiple '@' and/or '#' delimiters in "
1008 case NAME_ERR_NO_AT:
1009 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1010 "permission set is missing '@'"));
1014 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1015 "(%d) not defined"), why);
1026 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1030 zpool_open_canfail(libzfs_handle_t *hdl, const char *pool)
1032 zpool_handle_t *zhp;
1036 * Make sure the pool name is valid.
1038 if (!zpool_name_valid(hdl, B_TRUE, pool)) {
1039 (void) zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1040 dgettext(TEXT_DOMAIN, "cannot open '%s'"),
1045 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1048 zhp->zpool_hdl = hdl;
1049 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1051 if (zpool_refresh_stats(zhp, &missing) != 0) {
1057 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "no such pool"));
1058 (void) zfs_error_fmt(hdl, EZFS_NOENT,
1059 dgettext(TEXT_DOMAIN, "cannot open '%s'"), pool);
1068 * Like the above, but silent on error. Used when iterating over pools (because
1069 * the configuration cache may be out of date).
1072 zpool_open_silent(libzfs_handle_t *hdl, const char *pool, zpool_handle_t **ret)
1074 zpool_handle_t *zhp;
1077 if ((zhp = zfs_alloc(hdl, sizeof (zpool_handle_t))) == NULL)
1080 zhp->zpool_hdl = hdl;
1081 (void) strlcpy(zhp->zpool_name, pool, sizeof (zhp->zpool_name));
1083 if (zpool_refresh_stats(zhp, &missing) != 0) {
1099 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1103 zpool_open(libzfs_handle_t *hdl, const char *pool)
1105 zpool_handle_t *zhp;
1107 if ((zhp = zpool_open_canfail(hdl, pool)) == NULL)
1110 if (zhp->zpool_state == POOL_STATE_UNAVAIL) {
1111 (void) zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
1112 dgettext(TEXT_DOMAIN, "cannot open '%s'"), zhp->zpool_name);
1121 * Close the handle. Simply frees the memory associated with the handle.
1124 zpool_close(zpool_handle_t *zhp)
1126 nvlist_free(zhp->zpool_config);
1127 nvlist_free(zhp->zpool_old_config);
1128 nvlist_free(zhp->zpool_props);
1133 * Return the name of the pool.
1136 zpool_get_name(zpool_handle_t *zhp)
1138 return (zhp->zpool_name);
1143 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1146 zpool_get_state(zpool_handle_t *zhp)
1148 return (zhp->zpool_state);
1152 * Create the named pool, using the provided vdev list. It is assumed
1153 * that the consumer has already validated the contents of the nvlist, so we
1154 * don't have to worry about error semantics.
1157 zpool_create(libzfs_handle_t *hdl, const char *pool, nvlist_t *nvroot,
1158 nvlist_t *props, nvlist_t *fsprops)
1160 zfs_cmd_t zc = {"\0"};
1161 nvlist_t *zc_fsprops = NULL;
1162 nvlist_t *zc_props = NULL;
1166 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1167 "cannot create '%s'"), pool);
1169 if (!zpool_name_valid(hdl, B_FALSE, pool))
1170 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
1172 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1176 prop_flags_t flags = { .create = B_TRUE, .import = B_FALSE };
1178 if ((zc_props = zpool_valid_proplist(hdl, pool, props,
1179 SPA_VERSION_1, flags, msg)) == NULL) {
1188 zoned = ((nvlist_lookup_string(fsprops,
1189 zfs_prop_to_name(ZFS_PROP_ZONED), &zonestr) == 0) &&
1190 strcmp(zonestr, "on") == 0);
1192 if ((zc_fsprops = zfs_valid_proplist(hdl, ZFS_TYPE_FILESYSTEM,
1193 fsprops, zoned, NULL, NULL, msg)) == NULL) {
1197 (nvlist_alloc(&zc_props, NV_UNIQUE_NAME, 0) != 0)) {
1200 if (nvlist_add_nvlist(zc_props,
1201 ZPOOL_ROOTFS_PROPS, zc_fsprops) != 0) {
1206 if (zc_props && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
1209 (void) strlcpy(zc.zc_name, pool, sizeof (zc.zc_name));
1211 if ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_CREATE, &zc)) != 0) {
1213 zcmd_free_nvlists(&zc);
1214 nvlist_free(zc_props);
1215 nvlist_free(zc_fsprops);
1220 * This can happen if the user has specified the same
1221 * device multiple times. We can't reliably detect this
1222 * until we try to add it and see we already have a
1223 * label. This can also happen under if the device is
1224 * part of an active md or lvm device.
1226 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1227 "one or more vdevs refer to the same device, or "
1228 "one of\nthe devices is part of an active md or "
1230 return (zfs_error(hdl, EZFS_BADDEV, msg));
1234 * This happens if the record size is smaller or larger
1235 * than the allowed size range, or not a power of 2.
1237 * NOTE: although zfs_valid_proplist is called earlier,
1238 * this case may have slipped through since the
1239 * pool does not exist yet and it is therefore
1240 * impossible to read properties e.g. max blocksize
1243 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1244 "record size invalid"));
1245 return (zfs_error(hdl, EZFS_BADPROP, msg));
1249 * This occurs when one of the devices is below
1250 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1251 * device was the problem device since there's no
1252 * reliable way to determine device size from userland.
1257 zfs_nicebytes(SPA_MINDEVSIZE, buf,
1260 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1261 "one or more devices is less than the "
1262 "minimum size (%s)"), buf);
1264 return (zfs_error(hdl, EZFS_BADDEV, msg));
1267 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1268 "one or more devices is out of space"));
1269 return (zfs_error(hdl, EZFS_BADDEV, msg));
1272 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1273 "cache device must be a disk or disk slice"));
1274 return (zfs_error(hdl, EZFS_BADDEV, msg));
1277 return (zpool_standard_error(hdl, errno, msg));
1282 zcmd_free_nvlists(&zc);
1283 nvlist_free(zc_props);
1284 nvlist_free(zc_fsprops);
1289 * Destroy the given pool. It is up to the caller to ensure that there are no
1290 * datasets left in the pool.
1293 zpool_destroy(zpool_handle_t *zhp, const char *log_str)
1295 zfs_cmd_t zc = {"\0"};
1296 zfs_handle_t *zfp = NULL;
1297 libzfs_handle_t *hdl = zhp->zpool_hdl;
1300 if (zhp->zpool_state == POOL_STATE_ACTIVE &&
1301 (zfp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_FILESYSTEM)) == NULL)
1304 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1305 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1307 if (zfs_ioctl(hdl, ZFS_IOC_POOL_DESTROY, &zc) != 0) {
1308 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1309 "cannot destroy '%s'"), zhp->zpool_name);
1311 if (errno == EROFS) {
1312 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1313 "one or more devices is read only"));
1314 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1316 (void) zpool_standard_error(hdl, errno, msg);
1325 remove_mountpoint(zfp);
1333 * Add the given vdevs to the pool. The caller must have already performed the
1334 * necessary verification to ensure that the vdev specification is well-formed.
1337 zpool_add(zpool_handle_t *zhp, nvlist_t *nvroot)
1339 zfs_cmd_t zc = {"\0"};
1341 libzfs_handle_t *hdl = zhp->zpool_hdl;
1343 nvlist_t **spares, **l2cache;
1344 uint_t nspares, nl2cache;
1346 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1347 "cannot add to '%s'"), zhp->zpool_name);
1349 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1350 SPA_VERSION_SPARES &&
1351 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_SPARES,
1352 &spares, &nspares) == 0) {
1353 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1354 "upgraded to add hot spares"));
1355 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1358 if (zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL) <
1359 SPA_VERSION_L2CACHE &&
1360 nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_L2CACHE,
1361 &l2cache, &nl2cache) == 0) {
1362 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "pool must be "
1363 "upgraded to add cache devices"));
1364 return (zfs_error(hdl, EZFS_BADVERSION, msg));
1367 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
1369 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1371 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_ADD, &zc) != 0) {
1375 * This can happen if the user has specified the same
1376 * device multiple times. We can't reliably detect this
1377 * until we try to add it and see we already have a
1380 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1381 "one or more vdevs refer to the same device"));
1382 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1387 * This occurrs when one of the devices is below
1388 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1389 * device was the problem device since there's no
1390 * reliable way to determine device size from userland.
1395 zfs_nicebytes(SPA_MINDEVSIZE, buf,
1398 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1399 "device is less than the minimum "
1402 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1406 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1407 "pool must be upgraded to add these vdevs"));
1408 (void) zfs_error(hdl, EZFS_BADVERSION, msg);
1412 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1413 "cache device must be a disk or disk slice"));
1414 (void) zfs_error(hdl, EZFS_BADDEV, msg);
1418 (void) zpool_standard_error(hdl, errno, msg);
1426 zcmd_free_nvlists(&zc);
1432 * Exports the pool from the system. The caller must ensure that there are no
1433 * mounted datasets in the pool.
1436 zpool_export_common(zpool_handle_t *zhp, boolean_t force, boolean_t hardforce,
1437 const char *log_str)
1439 zfs_cmd_t zc = {"\0"};
1442 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1443 "cannot export '%s'"), zhp->zpool_name);
1445 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1446 zc.zc_cookie = force;
1447 zc.zc_guid = hardforce;
1448 zc.zc_history = (uint64_t)(uintptr_t)log_str;
1450 if (zfs_ioctl(zhp->zpool_hdl, ZFS_IOC_POOL_EXPORT, &zc) != 0) {
1453 zfs_error_aux(zhp->zpool_hdl, dgettext(TEXT_DOMAIN,
1454 "use '-f' to override the following errors:\n"
1455 "'%s' has an active shared spare which could be"
1456 " used by other pools once '%s' is exported."),
1457 zhp->zpool_name, zhp->zpool_name);
1458 return (zfs_error(zhp->zpool_hdl, EZFS_ACTIVE_SPARE,
1461 return (zpool_standard_error_fmt(zhp->zpool_hdl, errno,
1470 zpool_export(zpool_handle_t *zhp, boolean_t force, const char *log_str)
1472 return (zpool_export_common(zhp, force, B_FALSE, log_str));
1476 zpool_export_force(zpool_handle_t *zhp, const char *log_str)
1478 return (zpool_export_common(zhp, B_TRUE, B_TRUE, log_str));
1482 zpool_rewind_exclaim(libzfs_handle_t *hdl, const char *name, boolean_t dryrun,
1485 nvlist_t *nv = NULL;
1491 if (!hdl->libzfs_printerr || config == NULL)
1494 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1495 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0) {
1499 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1501 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1503 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1504 strftime(timestr, 128, "%c", &t) != 0) {
1506 (void) printf(dgettext(TEXT_DOMAIN,
1507 "Would be able to return %s "
1508 "to its state as of %s.\n"),
1511 (void) printf(dgettext(TEXT_DOMAIN,
1512 "Pool %s returned to its state as of %s.\n"),
1516 (void) printf(dgettext(TEXT_DOMAIN,
1517 "%s approximately %lld "),
1518 dryrun ? "Would discard" : "Discarded",
1519 ((longlong_t)loss + 30) / 60);
1520 (void) printf(dgettext(TEXT_DOMAIN,
1521 "minutes of transactions.\n"));
1522 } else if (loss > 0) {
1523 (void) printf(dgettext(TEXT_DOMAIN,
1524 "%s approximately %lld "),
1525 dryrun ? "Would discard" : "Discarded",
1527 (void) printf(dgettext(TEXT_DOMAIN,
1528 "seconds of transactions.\n"));
1534 zpool_explain_recover(libzfs_handle_t *hdl, const char *name, int reason,
1537 nvlist_t *nv = NULL;
1539 uint64_t edata = UINT64_MAX;
1544 if (!hdl->libzfs_printerr)
1548 (void) printf(dgettext(TEXT_DOMAIN, "action: "));
1550 (void) printf(dgettext(TEXT_DOMAIN, "\t"));
1552 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1553 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nv) != 0 ||
1554 nvlist_lookup_nvlist(nv, ZPOOL_CONFIG_REWIND_INFO, &nv) != 0 ||
1555 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_TIME, &rewindto) != 0)
1558 (void) nvlist_lookup_int64(nv, ZPOOL_CONFIG_REWIND_TIME, &loss);
1559 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_LOAD_DATA_ERRORS,
1562 (void) printf(dgettext(TEXT_DOMAIN,
1563 "Recovery is possible, but will result in some data loss.\n"));
1565 if (localtime_r((time_t *)&rewindto, &t) != NULL &&
1566 strftime(timestr, 128, "%c", &t) != 0) {
1567 (void) printf(dgettext(TEXT_DOMAIN,
1568 "\tReturning the pool to its state as of %s\n"
1569 "\tshould correct the problem. "),
1572 (void) printf(dgettext(TEXT_DOMAIN,
1573 "\tReverting the pool to an earlier state "
1574 "should correct the problem.\n\t"));
1578 (void) printf(dgettext(TEXT_DOMAIN,
1579 "Approximately %lld minutes of data\n"
1580 "\tmust be discarded, irreversibly. "),
1581 ((longlong_t)loss + 30) / 60);
1582 } else if (loss > 0) {
1583 (void) printf(dgettext(TEXT_DOMAIN,
1584 "Approximately %lld seconds of data\n"
1585 "\tmust be discarded, irreversibly. "),
1588 if (edata != 0 && edata != UINT64_MAX) {
1590 (void) printf(dgettext(TEXT_DOMAIN,
1591 "After rewind, at least\n"
1592 "\tone persistent user-data error will remain. "));
1594 (void) printf(dgettext(TEXT_DOMAIN,
1595 "After rewind, several\n"
1596 "\tpersistent user-data errors will remain. "));
1599 (void) printf(dgettext(TEXT_DOMAIN,
1600 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1601 reason >= 0 ? "clear" : "import", name);
1603 (void) printf(dgettext(TEXT_DOMAIN,
1604 "A scrub of the pool\n"
1605 "\tis strongly recommended after recovery.\n"));
1609 (void) printf(dgettext(TEXT_DOMAIN,
1610 "Destroy and re-create the pool from\n\ta backup source.\n"));
1614 * zpool_import() is a contracted interface. Should be kept the same
1617 * Applications should use zpool_import_props() to import a pool with
1618 * new properties value to be set.
1621 zpool_import(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1624 nvlist_t *props = NULL;
1627 if (altroot != NULL) {
1628 if (nvlist_alloc(&props, NV_UNIQUE_NAME, 0) != 0) {
1629 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1630 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1634 if (nvlist_add_string(props,
1635 zpool_prop_to_name(ZPOOL_PROP_ALTROOT), altroot) != 0 ||
1636 nvlist_add_string(props,
1637 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE), "none") != 0) {
1639 return (zfs_error_fmt(hdl, EZFS_NOMEM,
1640 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1645 ret = zpool_import_props(hdl, config, newname, props,
1652 print_vdev_tree(libzfs_handle_t *hdl, const char *name, nvlist_t *nv,
1658 uint64_t is_log = 0;
1660 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG,
1664 (void) printf("\t%*s%s%s\n", indent, "", name,
1665 is_log ? " [log]" : "");
1667 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
1668 &child, &children) != 0)
1671 for (c = 0; c < children; c++) {
1672 vname = zpool_vdev_name(hdl, NULL, child[c], VDEV_NAME_TYPE_ID);
1673 print_vdev_tree(hdl, vname, child[c], indent + 2);
1679 zpool_print_unsup_feat(nvlist_t *config)
1681 nvlist_t *nvinfo, *unsup_feat;
1684 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_LOAD_INFO, &nvinfo) ==
1686 verify(nvlist_lookup_nvlist(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT,
1689 for (nvp = nvlist_next_nvpair(unsup_feat, NULL); nvp != NULL;
1690 nvp = nvlist_next_nvpair(unsup_feat, nvp)) {
1693 verify(nvpair_type(nvp) == DATA_TYPE_STRING);
1694 verify(nvpair_value_string(nvp, &desc) == 0);
1696 if (strlen(desc) > 0)
1697 (void) printf("\t%s (%s)\n", nvpair_name(nvp), desc);
1699 (void) printf("\t%s\n", nvpair_name(nvp));
1704 * Import the given pool using the known configuration and a list of
1705 * properties to be set. The configuration should have come from
1706 * zpool_find_import(). The 'newname' parameters control whether the pool
1707 * is imported with a different name.
1710 zpool_import_props(libzfs_handle_t *hdl, nvlist_t *config, const char *newname,
1711 nvlist_t *props, int flags)
1713 zfs_cmd_t zc = {"\0"};
1714 zpool_rewind_policy_t policy;
1715 nvlist_t *nv = NULL;
1716 nvlist_t *nvinfo = NULL;
1717 nvlist_t *missing = NULL;
1724 verify(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1727 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1728 "cannot import pool '%s'"), origname);
1730 if (newname != NULL) {
1731 if (!zpool_name_valid(hdl, B_FALSE, newname))
1732 return (zfs_error_fmt(hdl, EZFS_INVALIDNAME,
1733 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1735 thename = (char *)newname;
1740 if (props != NULL) {
1742 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
1744 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION,
1747 if ((props = zpool_valid_proplist(hdl, origname,
1748 props, version, flags, errbuf)) == NULL)
1750 if (zcmd_write_src_nvlist(hdl, &zc, props) != 0) {
1757 (void) strlcpy(zc.zc_name, thename, sizeof (zc.zc_name));
1759 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID,
1762 if (zcmd_write_conf_nvlist(hdl, &zc, config) != 0) {
1763 zcmd_free_nvlists(&zc);
1766 if (zcmd_alloc_dst_nvlist(hdl, &zc, zc.zc_nvlist_conf_size * 2) != 0) {
1767 zcmd_free_nvlists(&zc);
1771 zc.zc_cookie = flags;
1772 while ((ret = zfs_ioctl(hdl, ZFS_IOC_POOL_IMPORT, &zc)) != 0 &&
1774 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
1775 zcmd_free_nvlists(&zc);
1782 (void) zcmd_read_dst_nvlist(hdl, &zc, &nv);
1784 zcmd_free_nvlists(&zc);
1786 zpool_get_rewind_policy(config, &policy);
1793 * Dry-run failed, but we print out what success
1794 * looks like if we found a best txg
1796 if (policy.zrp_request & ZPOOL_TRY_REWIND) {
1797 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1803 if (newname == NULL)
1804 (void) snprintf(desc, sizeof (desc),
1805 dgettext(TEXT_DOMAIN, "cannot import '%s'"),
1808 (void) snprintf(desc, sizeof (desc),
1809 dgettext(TEXT_DOMAIN, "cannot import '%s' as '%s'"),
1814 if (nv != NULL && nvlist_lookup_nvlist(nv,
1815 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1816 nvlist_exists(nvinfo, ZPOOL_CONFIG_UNSUP_FEAT)) {
1817 (void) printf(dgettext(TEXT_DOMAIN, "This "
1818 "pool uses the following feature(s) not "
1819 "supported by this system:\n"));
1820 zpool_print_unsup_feat(nv);
1821 if (nvlist_exists(nvinfo,
1822 ZPOOL_CONFIG_CAN_RDONLY)) {
1823 (void) printf(dgettext(TEXT_DOMAIN,
1824 "All unsupported features are only "
1825 "required for writing to the pool."
1826 "\nThe pool can be imported using "
1827 "'-o readonly=on'.\n"));
1831 * Unsupported version.
1833 (void) zfs_error(hdl, EZFS_BADVERSION, desc);
1837 if (nv != NULL && nvlist_lookup_nvlist(nv,
1838 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0) {
1839 char *hostname = "<unknown>";
1840 uint64_t hostid = 0;
1841 mmp_state_t mmp_state;
1843 mmp_state = fnvlist_lookup_uint64(nvinfo,
1844 ZPOOL_CONFIG_MMP_STATE);
1846 if (nvlist_exists(nvinfo,
1847 ZPOOL_CONFIG_MMP_HOSTNAME))
1848 hostname = fnvlist_lookup_string(nvinfo,
1849 ZPOOL_CONFIG_MMP_HOSTNAME);
1851 if (nvlist_exists(nvinfo,
1852 ZPOOL_CONFIG_MMP_HOSTID))
1853 hostid = fnvlist_lookup_uint64(nvinfo,
1854 ZPOOL_CONFIG_MMP_HOSTID);
1856 if (mmp_state == MMP_STATE_ACTIVE) {
1857 (void) snprintf(aux, sizeof (aux),
1858 dgettext(TEXT_DOMAIN, "pool is imp"
1859 "orted on host '%s' (hostid=%lx).\n"
1860 "Export the pool on the other "
1861 "system, then run 'zpool import'."),
1862 hostname, (unsigned long) hostid);
1863 } else if (mmp_state == MMP_STATE_NO_HOSTID) {
1864 (void) snprintf(aux, sizeof (aux),
1865 dgettext(TEXT_DOMAIN, "pool has "
1866 "the multihost property on and "
1867 "the\nsystem's hostid is not set. "
1868 "Set a unique system hostid with "
1869 "the zgenhostid(8) command.\n"));
1872 (void) zfs_error_aux(hdl, aux);
1874 (void) zfs_error(hdl, EZFS_ACTIVE_POOL, desc);
1878 (void) zfs_error(hdl, EZFS_INVALCONFIG, desc);
1882 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1883 "one or more devices is read only"));
1884 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1888 if (nv && nvlist_lookup_nvlist(nv,
1889 ZPOOL_CONFIG_LOAD_INFO, &nvinfo) == 0 &&
1890 nvlist_lookup_nvlist(nvinfo,
1891 ZPOOL_CONFIG_MISSING_DEVICES, &missing) == 0) {
1892 (void) printf(dgettext(TEXT_DOMAIN,
1893 "The devices below are missing, use "
1894 "'-m' to import the pool anyway:\n"));
1895 print_vdev_tree(hdl, NULL, missing, 2);
1896 (void) printf("\n");
1898 (void) zpool_standard_error(hdl, error, desc);
1902 (void) zpool_standard_error(hdl, error, desc);
1906 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1907 "one or more devices are already in use\n"));
1908 (void) zfs_error(hdl, EZFS_BADDEV, desc);
1911 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1912 "new name of at least one dataset is longer than "
1913 "the maximum allowable length"));
1914 (void) zfs_error(hdl, EZFS_NAMETOOLONG, desc);
1917 (void) zpool_standard_error(hdl, error, desc);
1918 zpool_explain_recover(hdl,
1919 newname ? origname : thename, -error, nv);
1926 zpool_handle_t *zhp;
1929 * This should never fail, but play it safe anyway.
1931 if (zpool_open_silent(hdl, thename, &zhp) != 0)
1933 else if (zhp != NULL)
1935 if (policy.zrp_request &
1936 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
1937 zpool_rewind_exclaim(hdl, newname ? origname : thename,
1938 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0), nv);
1951 zpool_scan(zpool_handle_t *zhp, pool_scan_func_t func, pool_scrub_cmd_t cmd)
1953 zfs_cmd_t zc = {"\0"};
1956 libzfs_handle_t *hdl = zhp->zpool_hdl;
1958 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
1959 zc.zc_cookie = func;
1962 if (zfs_ioctl(hdl, ZFS_IOC_POOL_SCAN, &zc) == 0)
1967 /* ECANCELED on a scrub means we resumed a paused scrub */
1968 if (err == ECANCELED && func == POOL_SCAN_SCRUB &&
1969 cmd == POOL_SCRUB_NORMAL)
1972 if (err == ENOENT && func != POOL_SCAN_NONE && cmd == POOL_SCRUB_NORMAL)
1975 if (func == POOL_SCAN_SCRUB) {
1976 if (cmd == POOL_SCRUB_PAUSE) {
1977 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1978 "cannot pause scrubbing %s"), zc.zc_name);
1980 assert(cmd == POOL_SCRUB_NORMAL);
1981 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
1982 "cannot scrub %s"), zc.zc_name);
1984 } else if (func == POOL_SCAN_NONE) {
1985 (void) snprintf(msg, sizeof (msg),
1986 dgettext(TEXT_DOMAIN, "cannot cancel scrubbing %s"),
1989 assert(!"unexpected result");
1994 pool_scan_stat_t *ps = NULL;
1997 verify(nvlist_lookup_nvlist(zhp->zpool_config,
1998 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
1999 (void) nvlist_lookup_uint64_array(nvroot,
2000 ZPOOL_CONFIG_SCAN_STATS, (uint64_t **)&ps, &psc);
2001 if (ps && ps->pss_func == POOL_SCAN_SCRUB) {
2002 if (cmd == POOL_SCRUB_PAUSE)
2003 return (zfs_error(hdl, EZFS_SCRUB_PAUSED, msg));
2005 return (zfs_error(hdl, EZFS_SCRUBBING, msg));
2007 return (zfs_error(hdl, EZFS_RESILVERING, msg));
2009 } else if (err == ENOENT) {
2010 return (zfs_error(hdl, EZFS_NO_SCRUB, msg));
2012 return (zpool_standard_error(hdl, err, msg));
2017 * Find a vdev that matches the search criteria specified. We use the
2018 * the nvpair name to determine how we should look for the device.
2019 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2020 * spare; but FALSE if its an INUSE spare.
2023 vdev_to_nvlist_iter(nvlist_t *nv, nvlist_t *search, boolean_t *avail_spare,
2024 boolean_t *l2cache, boolean_t *log)
2031 nvpair_t *pair = nvlist_next_nvpair(search, NULL);
2033 /* Nothing to look for */
2034 if (search == NULL || pair == NULL)
2037 /* Obtain the key we will use to search */
2038 srchkey = nvpair_name(pair);
2040 switch (nvpair_type(pair)) {
2041 case DATA_TYPE_UINT64:
2042 if (strcmp(srchkey, ZPOOL_CONFIG_GUID) == 0) {
2043 uint64_t srchval, theguid;
2045 verify(nvpair_value_uint64(pair, &srchval) == 0);
2046 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
2048 if (theguid == srchval)
2053 case DATA_TYPE_STRING: {
2054 char *srchval, *val;
2056 verify(nvpair_value_string(pair, &srchval) == 0);
2057 if (nvlist_lookup_string(nv, srchkey, &val) != 0)
2061 * Search for the requested value. Special cases:
2063 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2064 * "-part1", or "p1". The suffix is hidden from the user,
2065 * but included in the string, so this matches around it.
2066 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2067 * is used to check all possible expanded paths.
2068 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2070 * Otherwise, all other searches are simple string compares.
2072 if (strcmp(srchkey, ZPOOL_CONFIG_PATH) == 0) {
2073 uint64_t wholedisk = 0;
2075 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK,
2077 if (zfs_strcmp_pathname(srchval, val, wholedisk) == 0)
2080 } else if (strcmp(srchkey, ZPOOL_CONFIG_TYPE) == 0 && val) {
2081 char *type, *idx, *end, *p;
2082 uint64_t id, vdev_id;
2085 * Determine our vdev type, keeping in mind
2086 * that the srchval is composed of a type and
2087 * vdev id pair (i.e. mirror-4).
2089 if ((type = strdup(srchval)) == NULL)
2092 if ((p = strrchr(type, '-')) == NULL) {
2100 * If the types don't match then keep looking.
2102 if (strncmp(val, type, strlen(val)) != 0) {
2107 verify(strncmp(type, VDEV_TYPE_RAIDZ,
2108 strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2109 strncmp(type, VDEV_TYPE_MIRROR,
2110 strlen(VDEV_TYPE_MIRROR)) == 0);
2111 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
2115 vdev_id = strtoull(idx, &end, 10);
2122 * Now verify that we have the correct vdev id.
2131 if (strcmp(srchval, val) == 0)
2140 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
2141 &child, &children) != 0)
2144 for (c = 0; c < children; c++) {
2145 if ((ret = vdev_to_nvlist_iter(child[c], search,
2146 avail_spare, l2cache, NULL)) != NULL) {
2148 * The 'is_log' value is only set for the toplevel
2149 * vdev, not the leaf vdevs. So we always lookup the
2150 * log device from the root of the vdev tree (where
2151 * 'log' is non-NULL).
2154 nvlist_lookup_uint64(child[c],
2155 ZPOOL_CONFIG_IS_LOG, &is_log) == 0 &&
2163 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_SPARES,
2164 &child, &children) == 0) {
2165 for (c = 0; c < children; c++) {
2166 if ((ret = vdev_to_nvlist_iter(child[c], search,
2167 avail_spare, l2cache, NULL)) != NULL) {
2168 *avail_spare = B_TRUE;
2174 if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_L2CACHE,
2175 &child, &children) == 0) {
2176 for (c = 0; c < children; c++) {
2177 if ((ret = vdev_to_nvlist_iter(child[c], search,
2178 avail_spare, l2cache, NULL)) != NULL) {
2189 * Given a physical path (minus the "/devices" prefix), find the
2193 zpool_find_vdev_by_physpath(zpool_handle_t *zhp, const char *ppath,
2194 boolean_t *avail_spare, boolean_t *l2cache, boolean_t *log)
2196 nvlist_t *search, *nvroot, *ret;
2198 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2199 verify(nvlist_add_string(search, ZPOOL_CONFIG_PHYS_PATH, ppath) == 0);
2201 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2204 *avail_spare = B_FALSE;
2208 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2209 nvlist_free(search);
2215 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2218 zpool_vdev_is_interior(const char *name)
2220 if (strncmp(name, VDEV_TYPE_RAIDZ, strlen(VDEV_TYPE_RAIDZ)) == 0 ||
2221 strncmp(name, VDEV_TYPE_MIRROR, strlen(VDEV_TYPE_MIRROR)) == 0)
2227 zpool_find_vdev(zpool_handle_t *zhp, const char *path, boolean_t *avail_spare,
2228 boolean_t *l2cache, boolean_t *log)
2231 nvlist_t *nvroot, *search, *ret;
2234 verify(nvlist_alloc(&search, NV_UNIQUE_NAME, KM_SLEEP) == 0);
2236 guid = strtoull(path, &end, 0);
2237 if (guid != 0 && *end == '\0') {
2238 verify(nvlist_add_uint64(search, ZPOOL_CONFIG_GUID, guid) == 0);
2239 } else if (zpool_vdev_is_interior(path)) {
2240 verify(nvlist_add_string(search, ZPOOL_CONFIG_TYPE, path) == 0);
2242 verify(nvlist_add_string(search, ZPOOL_CONFIG_PATH, path) == 0);
2245 verify(nvlist_lookup_nvlist(zhp->zpool_config, ZPOOL_CONFIG_VDEV_TREE,
2248 *avail_spare = B_FALSE;
2252 ret = vdev_to_nvlist_iter(nvroot, search, avail_spare, l2cache, log);
2253 nvlist_free(search);
2259 vdev_is_online(nvlist_t *nv)
2263 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_OFFLINE, &ival) == 0 ||
2264 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_FAULTED, &ival) == 0 ||
2265 nvlist_lookup_uint64(nv, ZPOOL_CONFIG_REMOVED, &ival) == 0)
2272 * Helper function for zpool_get_physpaths().
2275 vdev_get_one_physpath(nvlist_t *config, char *physpath, size_t physpath_size,
2276 size_t *bytes_written)
2278 size_t bytes_left, pos, rsz;
2282 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PHYS_PATH,
2284 return (EZFS_NODEVICE);
2286 pos = *bytes_written;
2287 bytes_left = physpath_size - pos;
2288 format = (pos == 0) ? "%s" : " %s";
2290 rsz = snprintf(physpath + pos, bytes_left, format, tmppath);
2291 *bytes_written += rsz;
2293 if (rsz >= bytes_left) {
2294 /* if physpath was not copied properly, clear it */
2295 if (bytes_left != 0) {
2298 return (EZFS_NOSPC);
2304 vdev_get_physpaths(nvlist_t *nv, char *physpath, size_t phypath_size,
2305 size_t *rsz, boolean_t is_spare)
2310 if (nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) != 0)
2311 return (EZFS_INVALCONFIG);
2313 if (strcmp(type, VDEV_TYPE_DISK) == 0) {
2315 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2316 * For a spare vdev, we only want to boot from the active
2321 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_SPARE,
2324 return (EZFS_INVALCONFIG);
2327 if (vdev_is_online(nv)) {
2328 if ((ret = vdev_get_one_physpath(nv, physpath,
2329 phypath_size, rsz)) != 0)
2332 } else if (strcmp(type, VDEV_TYPE_MIRROR) == 0 ||
2333 strcmp(type, VDEV_TYPE_RAIDZ) == 0 ||
2334 strcmp(type, VDEV_TYPE_REPLACING) == 0 ||
2335 (is_spare = (strcmp(type, VDEV_TYPE_SPARE) == 0))) {
2340 if (nvlist_lookup_nvlist_array(nv,
2341 ZPOOL_CONFIG_CHILDREN, &child, &count) != 0)
2342 return (EZFS_INVALCONFIG);
2344 for (i = 0; i < count; i++) {
2345 ret = vdev_get_physpaths(child[i], physpath,
2346 phypath_size, rsz, is_spare);
2347 if (ret == EZFS_NOSPC)
2352 return (EZFS_POOL_INVALARG);
2356 * Get phys_path for a root pool config.
2357 * Return 0 on success; non-zero on failure.
2360 zpool_get_config_physpath(nvlist_t *config, char *physpath, size_t phypath_size)
2363 nvlist_t *vdev_root;
2370 if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
2372 return (EZFS_INVALCONFIG);
2374 if (nvlist_lookup_string(vdev_root, ZPOOL_CONFIG_TYPE, &type) != 0 ||
2375 nvlist_lookup_nvlist_array(vdev_root, ZPOOL_CONFIG_CHILDREN,
2376 &child, &count) != 0)
2377 return (EZFS_INVALCONFIG);
2380 * root pool can only have a single top-level vdev.
2382 if (strcmp(type, VDEV_TYPE_ROOT) != 0 || count != 1)
2383 return (EZFS_POOL_INVALARG);
2385 (void) vdev_get_physpaths(child[0], physpath, phypath_size, &rsz,
2388 /* No online devices */
2390 return (EZFS_NODEVICE);
2396 * Get phys_path for a root pool
2397 * Return 0 on success; non-zero on failure.
2400 zpool_get_physpath(zpool_handle_t *zhp, char *physpath, size_t phypath_size)
2402 return (zpool_get_config_physpath(zhp->zpool_config, physpath,
2407 * If the device has being dynamically expanded then we need to relabel
2408 * the disk to use the new unallocated space.
2411 zpool_relabel_disk(libzfs_handle_t *hdl, const char *path, const char *msg)
2415 if ((fd = open(path, O_RDWR|O_DIRECT)) < 0) {
2416 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2417 "relabel '%s': unable to open device: %d"), path, errno);
2418 return (zfs_error(hdl, EZFS_OPENFAILED, msg));
2422 * It's possible that we might encounter an error if the device
2423 * does not have any unallocated space left. If so, we simply
2424 * ignore that error and continue on.
2426 * Also, we don't call efi_rescan() - that would just return EBUSY.
2427 * The module will do it for us in vdev_disk_open().
2429 error = efi_use_whole_disk(fd);
2431 /* Flush the buffers to disk and invalidate the page cache. */
2433 (void) ioctl(fd, BLKFLSBUF);
2436 if (error && error != VT_ENOSPC) {
2437 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
2438 "relabel '%s': unable to read disk capacity"), path);
2439 return (zfs_error(hdl, EZFS_NOCAP, msg));
2446 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
2448 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
2449 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
2453 zpool_vdev_path_to_guid_impl(zpool_handle_t *zhp, const char *path,
2454 boolean_t *is_spare, boolean_t *is_l2cache, boolean_t *is_log)
2457 boolean_t spare = B_FALSE, l2cache = B_FALSE, log = B_FALSE;
2460 if ((tgt = zpool_find_vdev(zhp, path, &spare, &l2cache,
2464 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &guid) == 0);
2465 if (is_spare != NULL)
2467 if (is_l2cache != NULL)
2468 *is_l2cache = l2cache;
2475 /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
2477 zpool_vdev_path_to_guid(zpool_handle_t *zhp, const char *path)
2479 return (zpool_vdev_path_to_guid_impl(zhp, path, NULL, NULL, NULL));
2483 * Bring the specified vdev online. The 'flags' parameter is a set of the
2484 * ZFS_ONLINE_* flags.
2487 zpool_vdev_online(zpool_handle_t *zhp, const char *path, int flags,
2488 vdev_state_t *newstate)
2490 zfs_cmd_t zc = {"\0"};
2493 boolean_t avail_spare, l2cache, islog;
2494 libzfs_handle_t *hdl = zhp->zpool_hdl;
2497 if (flags & ZFS_ONLINE_EXPAND) {
2498 (void) snprintf(msg, sizeof (msg),
2499 dgettext(TEXT_DOMAIN, "cannot expand %s"), path);
2501 (void) snprintf(msg, sizeof (msg),
2502 dgettext(TEXT_DOMAIN, "cannot online %s"), path);
2505 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2506 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2508 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2510 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2513 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2515 if (flags & ZFS_ONLINE_EXPAND ||
2516 zpool_get_prop_int(zhp, ZPOOL_PROP_AUTOEXPAND, NULL)) {
2517 uint64_t wholedisk = 0;
2519 (void) nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_WHOLE_DISK,
2523 * XXX - L2ARC 1.0 devices can't support expansion.
2526 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2527 "cannot expand cache devices"));
2528 return (zfs_error(hdl, EZFS_VDEVNOTSUP, msg));
2532 const char *fullpath = path;
2533 char buf[MAXPATHLEN];
2535 if (path[0] != '/') {
2536 error = zfs_resolve_shortname(path, buf,
2539 return (zfs_error(hdl, EZFS_NODEVICE,
2545 error = zpool_relabel_disk(hdl, fullpath, msg);
2551 zc.zc_cookie = VDEV_STATE_ONLINE;
2554 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) != 0) {
2555 if (errno == EINVAL) {
2556 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "was split "
2557 "from this pool into a new one. Use '%s' "
2558 "instead"), "zpool detach");
2559 return (zfs_error(hdl, EZFS_POSTSPLIT_ONLINE, msg));
2561 return (zpool_standard_error(hdl, errno, msg));
2564 *newstate = zc.zc_cookie;
2569 * Take the specified vdev offline
2572 zpool_vdev_offline(zpool_handle_t *zhp, const char *path, boolean_t istmp)
2574 zfs_cmd_t zc = {"\0"};
2577 boolean_t avail_spare, l2cache;
2578 libzfs_handle_t *hdl = zhp->zpool_hdl;
2580 (void) snprintf(msg, sizeof (msg),
2581 dgettext(TEXT_DOMAIN, "cannot offline %s"), path);
2583 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2584 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2586 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2588 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2591 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2593 zc.zc_cookie = VDEV_STATE_OFFLINE;
2594 zc.zc_obj = istmp ? ZFS_OFFLINE_TEMPORARY : 0;
2596 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2603 * There are no other replicas of this device.
2605 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2609 * The log device has unplayed logs
2611 return (zfs_error(hdl, EZFS_UNPLAYED_LOGS, msg));
2614 return (zpool_standard_error(hdl, errno, msg));
2619 * Mark the given vdev faulted.
2622 zpool_vdev_fault(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2624 zfs_cmd_t zc = {"\0"};
2626 libzfs_handle_t *hdl = zhp->zpool_hdl;
2628 (void) snprintf(msg, sizeof (msg),
2629 dgettext(TEXT_DOMAIN, "cannot fault %llu"), (u_longlong_t)guid);
2631 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2633 zc.zc_cookie = VDEV_STATE_FAULTED;
2636 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2643 * There are no other replicas of this device.
2645 return (zfs_error(hdl, EZFS_NOREPLICAS, msg));
2648 return (zpool_standard_error(hdl, errno, msg));
2654 * Mark the given vdev degraded.
2657 zpool_vdev_degrade(zpool_handle_t *zhp, uint64_t guid, vdev_aux_t aux)
2659 zfs_cmd_t zc = {"\0"};
2661 libzfs_handle_t *hdl = zhp->zpool_hdl;
2663 (void) snprintf(msg, sizeof (msg),
2664 dgettext(TEXT_DOMAIN, "cannot degrade %llu"), (u_longlong_t)guid);
2666 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2668 zc.zc_cookie = VDEV_STATE_DEGRADED;
2671 if (ioctl(hdl->libzfs_fd, ZFS_IOC_VDEV_SET_STATE, &zc) == 0)
2674 return (zpool_standard_error(hdl, errno, msg));
2678 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2682 is_replacing_spare(nvlist_t *search, nvlist_t *tgt, int which)
2688 if (nvlist_lookup_nvlist_array(search, ZPOOL_CONFIG_CHILDREN, &child,
2690 verify(nvlist_lookup_string(search, ZPOOL_CONFIG_TYPE,
2693 if (strcmp(type, VDEV_TYPE_SPARE) == 0 &&
2694 children == 2 && child[which] == tgt)
2697 for (c = 0; c < children; c++)
2698 if (is_replacing_spare(child[c], tgt, which))
2706 * Attach new_disk (fully described by nvroot) to old_disk.
2707 * If 'replacing' is specified, the new disk will replace the old one.
2710 zpool_vdev_attach(zpool_handle_t *zhp,
2711 const char *old_disk, const char *new_disk, nvlist_t *nvroot, int replacing)
2713 zfs_cmd_t zc = {"\0"};
2717 boolean_t avail_spare, l2cache, islog;
2722 nvlist_t *config_root;
2723 libzfs_handle_t *hdl = zhp->zpool_hdl;
2724 boolean_t rootpool = zpool_is_bootable(zhp);
2727 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2728 "cannot replace %s with %s"), old_disk, new_disk);
2730 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
2731 "cannot attach %s to %s"), new_disk, old_disk);
2733 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2734 if ((tgt = zpool_find_vdev(zhp, old_disk, &avail_spare, &l2cache,
2736 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2739 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2742 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2744 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2745 zc.zc_cookie = replacing;
2747 if (nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
2748 &child, &children) != 0 || children != 1) {
2749 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2750 "new device must be a single disk"));
2751 return (zfs_error(hdl, EZFS_INVALCONFIG, msg));
2754 verify(nvlist_lookup_nvlist(zpool_get_config(zhp, NULL),
2755 ZPOOL_CONFIG_VDEV_TREE, &config_root) == 0);
2757 if ((newname = zpool_vdev_name(NULL, NULL, child[0], 0)) == NULL)
2761 * If the target is a hot spare that has been swapped in, we can only
2762 * replace it with another hot spare.
2765 nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_IS_SPARE, &val) == 0 &&
2766 (zpool_find_vdev(zhp, newname, &avail_spare, &l2cache,
2767 NULL) == NULL || !avail_spare) &&
2768 is_replacing_spare(config_root, tgt, 1)) {
2769 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2770 "can only be replaced by another hot spare"));
2772 return (zfs_error(hdl, EZFS_BADTARGET, msg));
2777 if (zcmd_write_conf_nvlist(hdl, &zc, nvroot) != 0)
2780 ret = zfs_ioctl(hdl, ZFS_IOC_VDEV_ATTACH, &zc);
2782 zcmd_free_nvlists(&zc);
2787 * XXX need a better way to prevent user from
2788 * booting up a half-baked vdev.
2790 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Make "
2791 "sure to wait until resilver is done "
2792 "before rebooting.\n"));
2800 * Can't attach to or replace this type of vdev.
2803 uint64_t version = zpool_get_prop_int(zhp,
2804 ZPOOL_PROP_VERSION, NULL);
2807 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2808 "cannot replace a log with a spare"));
2809 else if (version >= SPA_VERSION_MULTI_REPLACE)
2810 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2811 "already in replacing/spare config; wait "
2812 "for completion or use 'zpool detach'"));
2814 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2815 "cannot replace a replacing device"));
2817 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2818 "can only attach to mirrors and top-level "
2821 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2826 * The new device must be a single disk.
2828 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2829 "new device must be a single disk"));
2830 (void) zfs_error(hdl, EZFS_INVALCONFIG, msg);
2834 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "%s is busy"),
2836 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2841 * The new device is too small.
2843 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2844 "device is too small"));
2845 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2850 * The new device has a different optimal sector size.
2852 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2853 "new device has a different optimal sector size; use the "
2854 "option '-o ashift=N' to override the optimal size"));
2855 (void) zfs_error(hdl, EZFS_BADDEV, msg);
2860 * The resulting top-level vdev spec won't fit in the label.
2862 (void) zfs_error(hdl, EZFS_DEVOVERFLOW, msg);
2866 (void) zpool_standard_error(hdl, errno, msg);
2873 * Detach the specified device.
2876 zpool_vdev_detach(zpool_handle_t *zhp, const char *path)
2878 zfs_cmd_t zc = {"\0"};
2881 boolean_t avail_spare, l2cache;
2882 libzfs_handle_t *hdl = zhp->zpool_hdl;
2884 (void) snprintf(msg, sizeof (msg),
2885 dgettext(TEXT_DOMAIN, "cannot detach %s"), path);
2887 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
2888 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
2890 return (zfs_error(hdl, EZFS_NODEVICE, msg));
2893 return (zfs_error(hdl, EZFS_ISSPARE, msg));
2896 return (zfs_error(hdl, EZFS_ISL2CACHE, msg));
2898 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
2900 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_DETACH, &zc) == 0)
2907 * Can't detach from this type of vdev.
2909 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only "
2910 "applicable to mirror and replacing vdevs"));
2911 (void) zfs_error(hdl, EZFS_BADTARGET, msg);
2916 * There are no other replicas of this device.
2918 (void) zfs_error(hdl, EZFS_NOREPLICAS, msg);
2922 (void) zpool_standard_error(hdl, errno, msg);
2929 * Find a mirror vdev in the source nvlist.
2931 * The mchild array contains a list of disks in one of the top-level mirrors
2932 * of the source pool. The schild array contains a list of disks that the
2933 * user specified on the command line. We loop over the mchild array to
2934 * see if any entry in the schild array matches.
2936 * If a disk in the mchild array is found in the schild array, we return
2937 * the index of that entry. Otherwise we return -1.
2940 find_vdev_entry(zpool_handle_t *zhp, nvlist_t **mchild, uint_t mchildren,
2941 nvlist_t **schild, uint_t schildren)
2945 for (mc = 0; mc < mchildren; mc++) {
2947 char *mpath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2950 for (sc = 0; sc < schildren; sc++) {
2951 char *spath = zpool_vdev_name(zhp->zpool_hdl, zhp,
2953 boolean_t result = (strcmp(mpath, spath) == 0);
2969 * Split a mirror pool. If newroot points to null, then a new nvlist
2970 * is generated and it is the responsibility of the caller to free it.
2973 zpool_vdev_split(zpool_handle_t *zhp, char *newname, nvlist_t **newroot,
2974 nvlist_t *props, splitflags_t flags)
2976 zfs_cmd_t zc = {"\0"};
2978 nvlist_t *tree, *config, **child, **newchild, *newconfig = NULL;
2979 nvlist_t **varray = NULL, *zc_props = NULL;
2980 uint_t c, children, newchildren, lastlog = 0, vcount, found = 0;
2981 libzfs_handle_t *hdl = zhp->zpool_hdl;
2983 boolean_t freelist = B_FALSE, memory_err = B_TRUE;
2986 (void) snprintf(msg, sizeof (msg),
2987 dgettext(TEXT_DOMAIN, "Unable to split %s"), zhp->zpool_name);
2989 if (!zpool_name_valid(hdl, B_FALSE, newname))
2990 return (zfs_error(hdl, EZFS_INVALIDNAME, msg));
2992 if ((config = zpool_get_config(zhp, NULL)) == NULL) {
2993 (void) fprintf(stderr, gettext("Internal error: unable to "
2994 "retrieve pool configuration\n"));
2998 verify(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &tree)
3000 verify(nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &vers) == 0);
3003 prop_flags_t flags = { .create = B_FALSE, .import = B_TRUE };
3004 if ((zc_props = zpool_valid_proplist(hdl, zhp->zpool_name,
3005 props, vers, flags, msg)) == NULL)
3009 if (nvlist_lookup_nvlist_array(tree, ZPOOL_CONFIG_CHILDREN, &child,
3011 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3012 "Source pool is missing vdev tree"));
3013 nvlist_free(zc_props);
3017 varray = zfs_alloc(hdl, children * sizeof (nvlist_t *));
3020 if (*newroot == NULL ||
3021 nvlist_lookup_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN,
3022 &newchild, &newchildren) != 0)
3025 for (c = 0; c < children; c++) {
3026 uint64_t is_log = B_FALSE, is_hole = B_FALSE;
3028 nvlist_t **mchild, *vdev;
3033 * Unlike cache & spares, slogs are stored in the
3034 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3036 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_LOG,
3038 (void) nvlist_lookup_uint64(child[c], ZPOOL_CONFIG_IS_HOLE,
3040 if (is_log || is_hole) {
3042 * Create a hole vdev and put it in the config.
3044 if (nvlist_alloc(&vdev, NV_UNIQUE_NAME, 0) != 0)
3046 if (nvlist_add_string(vdev, ZPOOL_CONFIG_TYPE,
3047 VDEV_TYPE_HOLE) != 0)
3049 if (nvlist_add_uint64(vdev, ZPOOL_CONFIG_IS_HOLE,
3054 varray[vcount++] = vdev;
3058 verify(nvlist_lookup_string(child[c], ZPOOL_CONFIG_TYPE, &type)
3060 if (strcmp(type, VDEV_TYPE_MIRROR) != 0) {
3061 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3062 "Source pool must be composed only of mirrors\n"));
3063 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3067 verify(nvlist_lookup_nvlist_array(child[c],
3068 ZPOOL_CONFIG_CHILDREN, &mchild, &mchildren) == 0);
3070 /* find or add an entry for this top-level vdev */
3071 if (newchildren > 0 &&
3072 (entry = find_vdev_entry(zhp, mchild, mchildren,
3073 newchild, newchildren)) >= 0) {
3074 /* We found a disk that the user specified. */
3075 vdev = mchild[entry];
3078 /* User didn't specify a disk for this vdev. */
3079 vdev = mchild[mchildren - 1];
3082 if (nvlist_dup(vdev, &varray[vcount++], 0) != 0)
3086 /* did we find every disk the user specified? */
3087 if (found != newchildren) {
3088 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "Device list must "
3089 "include at most one disk from each mirror"));
3090 retval = zfs_error(hdl, EZFS_INVALCONFIG, msg);
3094 /* Prepare the nvlist for populating. */
3095 if (*newroot == NULL) {
3096 if (nvlist_alloc(newroot, NV_UNIQUE_NAME, 0) != 0)
3099 if (nvlist_add_string(*newroot, ZPOOL_CONFIG_TYPE,
3100 VDEV_TYPE_ROOT) != 0)
3103 verify(nvlist_remove_all(*newroot, ZPOOL_CONFIG_CHILDREN) == 0);
3106 /* Add all the children we found */
3107 if (nvlist_add_nvlist_array(*newroot, ZPOOL_CONFIG_CHILDREN, varray,
3108 lastlog == 0 ? vcount : lastlog) != 0)
3112 * If we're just doing a dry run, exit now with success.
3115 memory_err = B_FALSE;
3120 /* now build up the config list & call the ioctl */
3121 if (nvlist_alloc(&newconfig, NV_UNIQUE_NAME, 0) != 0)
3124 if (nvlist_add_nvlist(newconfig,
3125 ZPOOL_CONFIG_VDEV_TREE, *newroot) != 0 ||
3126 nvlist_add_string(newconfig,
3127 ZPOOL_CONFIG_POOL_NAME, newname) != 0 ||
3128 nvlist_add_uint64(newconfig, ZPOOL_CONFIG_VERSION, vers) != 0)
3132 * The new pool is automatically part of the namespace unless we
3133 * explicitly export it.
3136 zc.zc_cookie = ZPOOL_EXPORT_AFTER_SPLIT;
3137 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3138 (void) strlcpy(zc.zc_string, newname, sizeof (zc.zc_string));
3139 if (zcmd_write_conf_nvlist(hdl, &zc, newconfig) != 0)
3141 if (zc_props != NULL && zcmd_write_src_nvlist(hdl, &zc, zc_props) != 0)
3144 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_SPLIT, &zc) != 0) {
3145 retval = zpool_standard_error(hdl, errno, msg);
3150 memory_err = B_FALSE;
3153 if (varray != NULL) {
3156 for (v = 0; v < vcount; v++)
3157 nvlist_free(varray[v]);
3160 zcmd_free_nvlists(&zc);
3161 nvlist_free(zc_props);
3162 nvlist_free(newconfig);
3164 nvlist_free(*newroot);
3172 return (no_memory(hdl));
3178 * Remove the given device. Currently, this is supported only for hot spares,
3179 * cache, and log devices.
3182 zpool_vdev_remove(zpool_handle_t *zhp, const char *path)
3184 zfs_cmd_t zc = {"\0"};
3187 boolean_t avail_spare, l2cache, islog;
3188 libzfs_handle_t *hdl = zhp->zpool_hdl;
3191 (void) snprintf(msg, sizeof (msg),
3192 dgettext(TEXT_DOMAIN, "cannot remove %s"), path);
3194 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3195 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare, &l2cache,
3197 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3199 * XXX - this should just go away.
3201 if (!avail_spare && !l2cache && !islog) {
3202 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3203 "only inactive hot spares, cache, "
3204 "or log devices can be removed"));
3205 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3208 version = zpool_get_prop_int(zhp, ZPOOL_PROP_VERSION, NULL);
3209 if (islog && version < SPA_VERSION_HOLES) {
3210 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3211 "pool must be upgrade to support log removal"));
3212 return (zfs_error(hdl, EZFS_BADVERSION, msg));
3215 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID, &zc.zc_guid) == 0);
3217 if (zfs_ioctl(hdl, ZFS_IOC_VDEV_REMOVE, &zc) == 0)
3220 return (zpool_standard_error(hdl, errno, msg));
3224 * Clear the errors for the pool, or the particular device if specified.
3227 zpool_clear(zpool_handle_t *zhp, const char *path, nvlist_t *rewindnvl)
3229 zfs_cmd_t zc = {"\0"};
3232 zpool_rewind_policy_t policy;
3233 boolean_t avail_spare, l2cache;
3234 libzfs_handle_t *hdl = zhp->zpool_hdl;
3235 nvlist_t *nvi = NULL;
3239 (void) snprintf(msg, sizeof (msg),
3240 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3243 (void) snprintf(msg, sizeof (msg),
3244 dgettext(TEXT_DOMAIN, "cannot clear errors for %s"),
3247 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3249 if ((tgt = zpool_find_vdev(zhp, path, &avail_spare,
3250 &l2cache, NULL)) == 0)
3251 return (zfs_error(hdl, EZFS_NODEVICE, msg));
3254 * Don't allow error clearing for hot spares. Do allow
3255 * error clearing for l2cache devices.
3258 return (zfs_error(hdl, EZFS_ISSPARE, msg));
3260 verify(nvlist_lookup_uint64(tgt, ZPOOL_CONFIG_GUID,
3264 zpool_get_rewind_policy(rewindnvl, &policy);
3265 zc.zc_cookie = policy.zrp_request;
3267 if (zcmd_alloc_dst_nvlist(hdl, &zc, zhp->zpool_config_size * 2) != 0)
3270 if (zcmd_write_src_nvlist(hdl, &zc, rewindnvl) != 0)
3273 while ((error = zfs_ioctl(hdl, ZFS_IOC_CLEAR, &zc)) != 0 &&
3275 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
3276 zcmd_free_nvlists(&zc);
3281 if (!error || ((policy.zrp_request & ZPOOL_TRY_REWIND) &&
3282 errno != EPERM && errno != EACCES)) {
3283 if (policy.zrp_request &
3284 (ZPOOL_DO_REWIND | ZPOOL_TRY_REWIND)) {
3285 (void) zcmd_read_dst_nvlist(hdl, &zc, &nvi);
3286 zpool_rewind_exclaim(hdl, zc.zc_name,
3287 ((policy.zrp_request & ZPOOL_TRY_REWIND) != 0),
3291 zcmd_free_nvlists(&zc);
3295 zcmd_free_nvlists(&zc);
3296 return (zpool_standard_error(hdl, errno, msg));
3300 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3303 zpool_vdev_clear(zpool_handle_t *zhp, uint64_t guid)
3305 zfs_cmd_t zc = {"\0"};
3307 libzfs_handle_t *hdl = zhp->zpool_hdl;
3309 (void) snprintf(msg, sizeof (msg),
3310 dgettext(TEXT_DOMAIN, "cannot clear errors for %llx"),
3311 (u_longlong_t)guid);
3313 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3315 zc.zc_cookie = ZPOOL_NO_REWIND;
3317 if (ioctl(hdl->libzfs_fd, ZFS_IOC_CLEAR, &zc) == 0)
3320 return (zpool_standard_error(hdl, errno, msg));
3324 * Change the GUID for a pool.
3327 zpool_reguid(zpool_handle_t *zhp)
3330 libzfs_handle_t *hdl = zhp->zpool_hdl;
3331 zfs_cmd_t zc = {"\0"};
3333 (void) snprintf(msg, sizeof (msg),
3334 dgettext(TEXT_DOMAIN, "cannot reguid '%s'"), zhp->zpool_name);
3336 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3337 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REGUID, &zc) == 0)
3340 return (zpool_standard_error(hdl, errno, msg));
3347 zpool_reopen(zpool_handle_t *zhp)
3349 zfs_cmd_t zc = {"\0"};
3351 libzfs_handle_t *hdl = zhp->zpool_hdl;
3353 (void) snprintf(msg, sizeof (msg),
3354 dgettext(TEXT_DOMAIN, "cannot reopen '%s'"),
3357 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3358 if (zfs_ioctl(hdl, ZFS_IOC_POOL_REOPEN, &zc) == 0)
3360 return (zpool_standard_error(hdl, errno, msg));
3363 /* call into libzfs_core to execute the sync IOCTL per pool */
3365 zpool_sync_one(zpool_handle_t *zhp, void *data)
3368 libzfs_handle_t *hdl = zpool_get_handle(zhp);
3369 const char *pool_name = zpool_get_name(zhp);
3370 boolean_t *force = data;
3371 nvlist_t *innvl = fnvlist_alloc();
3373 fnvlist_add_boolean_value(innvl, "force", *force);
3374 if ((ret = lzc_sync(pool_name, innvl, NULL)) != 0) {
3376 return (zpool_standard_error_fmt(hdl, ret,
3377 dgettext(TEXT_DOMAIN, "sync '%s' failed"), pool_name));
3384 #if defined(__sun__) || defined(__sun)
3386 * Convert from a devid string to a path.
3389 devid_to_path(char *devid_str)
3394 devid_nmlist_t *list = NULL;
3397 if (devid_str_decode(devid_str, &devid, &minor) != 0)
3400 ret = devid_deviceid_to_nmlist("/dev", devid, minor, &list);
3402 devid_str_free(minor);
3409 * In a case the strdup() fails, we will just return NULL below.
3411 path = strdup(list[0].devname);
3413 devid_free_nmlist(list);
3419 * Convert from a path to a devid string.
3422 path_to_devid(const char *path)
3428 if ((fd = open(path, O_RDONLY)) < 0)
3433 if (devid_get(fd, &devid) == 0) {
3434 if (devid_get_minor_name(fd, &minor) == 0)
3435 ret = devid_str_encode(devid, minor);
3437 devid_str_free(minor);
3446 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3447 * ignore any failure here, since a common case is for an unprivileged user to
3448 * type 'zpool status', and we'll display the correct information anyway.
3451 set_path(zpool_handle_t *zhp, nvlist_t *nv, const char *path)
3453 zfs_cmd_t zc = {"\0"};
3455 (void) strncpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3456 (void) strncpy(zc.zc_value, path, sizeof (zc.zc_value));
3457 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID,
3460 (void) ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_VDEV_SETPATH, &zc);
3465 * Remove partition suffix from a vdev path. Partition suffixes may take three
3466 * forms: "-partX", "pX", or "X", where X is a string of digits. The second
3467 * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
3468 * third case only occurs when preceded by a string matching the regular
3469 * expression "^([hsv]|xv)d[a-z]+", i.e. a scsi, ide, virtio or xen disk.
3471 * caller must free the returned string
3474 zfs_strip_partition(char *path)
3476 char *tmp = strdup(path);
3477 char *part = NULL, *d = NULL;
3481 if ((part = strstr(tmp, "-part")) && part != tmp) {
3483 } else if ((part = strrchr(tmp, 'p')) &&
3484 part > tmp + 1 && isdigit(*(part-1))) {
3486 } else if ((tmp[0] == 'h' || tmp[0] == 's' || tmp[0] == 'v') &&
3488 for (d = &tmp[2]; isalpha(*d); part = ++d) { }
3489 } else if (strncmp("xvd", tmp, 3) == 0) {
3490 for (d = &tmp[3]; isalpha(*d); part = ++d) { }
3492 if (part && d && *d != '\0') {
3493 for (; isdigit(*d); d++) { }
3502 * Same as zfs_strip_partition, but allows "/dev/" to be in the pathname
3507 * Returned string must be freed.
3510 zfs_strip_partition_path(char *path)
3512 char *newpath = strdup(path);
3519 /* Point to "sda1" part of "/dev/sda1" */
3520 sd_offset = strrchr(newpath, '/') + 1;
3522 /* Get our new name "sda" */
3523 new_sd = zfs_strip_partition(sd_offset);
3529 /* Paste the "sda" where "sda1" was */
3530 strlcpy(sd_offset, new_sd, strlen(sd_offset) + 1);
3532 /* Free temporary "sda" */
3538 #define PATH_BUF_LEN 64
3541 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3542 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3543 * We also check if this is a whole disk, in which case we strip off the
3544 * trailing 's0' slice name.
3546 * This routine is also responsible for identifying when disks have been
3547 * reconfigured in a new location. The kernel will have opened the device by
3548 * devid, but the path will still refer to the old location. To catch this, we
3549 * first do a path -> devid translation (which is fast for the common case). If
3550 * the devid matches, we're done. If not, we do a reverse devid -> path
3551 * translation and issue the appropriate ioctl() to update the path of the vdev.
3552 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3556 zpool_vdev_name(libzfs_handle_t *hdl, zpool_handle_t *zhp, nvlist_t *nv,
3559 char *path, *type, *env;
3561 char buf[PATH_BUF_LEN];
3562 char tmpbuf[PATH_BUF_LEN];
3564 env = getenv("ZPOOL_VDEV_NAME_PATH");
3565 if (env && (strtoul(env, NULL, 0) > 0 ||
3566 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3567 name_flags |= VDEV_NAME_PATH;
3569 env = getenv("ZPOOL_VDEV_NAME_GUID");
3570 if (env && (strtoul(env, NULL, 0) > 0 ||
3571 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3572 name_flags |= VDEV_NAME_GUID;
3574 env = getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
3575 if (env && (strtoul(env, NULL, 0) > 0 ||
3576 !strncasecmp(env, "YES", 3) || !strncasecmp(env, "ON", 2)))
3577 name_flags |= VDEV_NAME_FOLLOW_LINKS;
3579 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NOT_PRESENT, &value) == 0 ||
3580 name_flags & VDEV_NAME_GUID) {
3581 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_GUID, &value);
3582 (void) snprintf(buf, sizeof (buf), "%llu", (u_longlong_t)value);
3584 } else if (nvlist_lookup_string(nv, ZPOOL_CONFIG_PATH, &path) == 0) {
3585 #if defined(__sun__) || defined(__sun)
3587 * Live VDEV path updates to a kernel VDEV during a
3588 * zpool_vdev_name lookup are not supported on Linux.
3595 * If the device is dead (faulted, offline, etc) then don't
3596 * bother opening it. Otherwise we may be forcing the user to
3597 * open a misbehaving device, which can have undesirable
3600 if ((nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
3601 (uint64_t **)&vs, &vsc) != 0 ||
3602 vs->vs_state >= VDEV_STATE_DEGRADED) &&
3604 nvlist_lookup_string(nv, ZPOOL_CONFIG_DEVID, &devid) == 0) {
3606 * Determine if the current path is correct.
3608 char *newdevid = path_to_devid(path);
3610 if (newdevid == NULL ||
3611 strcmp(devid, newdevid) != 0) {
3614 if ((newpath = devid_to_path(devid)) != NULL) {
3616 * Update the path appropriately.
3618 set_path(zhp, nv, newpath);
3619 if (nvlist_add_string(nv,
3620 ZPOOL_CONFIG_PATH, newpath) == 0)
3621 verify(nvlist_lookup_string(nv,
3629 devid_str_free(newdevid);
3633 if (name_flags & VDEV_NAME_FOLLOW_LINKS) {
3634 char *rp = realpath(path, NULL);
3636 strlcpy(buf, rp, sizeof (buf));
3643 * For a block device only use the name.
3645 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &type) == 0);
3646 if ((strcmp(type, VDEV_TYPE_DISK) == 0) &&
3647 !(name_flags & VDEV_NAME_PATH)) {
3648 path = strrchr(path, '/');
3653 * Remove the partition from the path it this is a whole disk.
3655 if (nvlist_lookup_uint64(nv, ZPOOL_CONFIG_WHOLE_DISK, &value)
3656 == 0 && value && !(name_flags & VDEV_NAME_PATH)) {
3657 return (zfs_strip_partition(path));
3660 verify(nvlist_lookup_string(nv, ZPOOL_CONFIG_TYPE, &path) == 0);
3663 * If it's a raidz device, we need to stick in the parity level.
3665 if (strcmp(path, VDEV_TYPE_RAIDZ) == 0) {
3666 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_NPARITY,
3668 (void) snprintf(buf, sizeof (buf), "%s%llu", path,
3669 (u_longlong_t)value);
3674 * We identify each top-level vdev by using a <type-id>
3675 * naming convention.
3677 if (name_flags & VDEV_NAME_TYPE_ID) {
3679 verify(nvlist_lookup_uint64(nv, ZPOOL_CONFIG_ID,
3681 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s-%llu",
3682 path, (u_longlong_t)id);
3687 return (zfs_strdup(hdl, path));
3691 zbookmark_mem_compare(const void *a, const void *b)
3693 return (memcmp(a, b, sizeof (zbookmark_phys_t)));
3697 * Retrieve the persistent error log, uniquify the members, and return to the
3701 zpool_get_errlog(zpool_handle_t *zhp, nvlist_t **nverrlistp)
3703 zfs_cmd_t zc = {"\0"};
3704 libzfs_handle_t *hdl = zhp->zpool_hdl;
3706 zbookmark_phys_t *zb = NULL;
3710 * Retrieve the raw error list from the kernel. If the number of errors
3711 * has increased, allocate more space and continue until we get the
3714 verify(nvlist_lookup_uint64(zhp->zpool_config, ZPOOL_CONFIG_ERRCOUNT,
3718 zc.zc_nvlist_dst = (uintptr_t)zfs_alloc(zhp->zpool_hdl,
3719 count * sizeof (zbookmark_phys_t));
3720 zc.zc_nvlist_dst_size = count;
3721 (void) strcpy(zc.zc_name, zhp->zpool_name);
3723 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_ERROR_LOG,
3725 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3726 if (errno == ENOMEM) {
3729 count = zc.zc_nvlist_dst_size;
3730 dst = zfs_alloc(zhp->zpool_hdl, count *
3731 sizeof (zbookmark_phys_t));
3732 zc.zc_nvlist_dst = (uintptr_t)dst;
3734 return (zpool_standard_error_fmt(hdl, errno,
3735 dgettext(TEXT_DOMAIN, "errors: List of "
3736 "errors unavailable")));
3744 * Sort the resulting bookmarks. This is a little confusing due to the
3745 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3746 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3747 * _not_ copied as part of the process. So we point the start of our
3748 * array appropriate and decrement the total number of elements.
3750 zb = ((zbookmark_phys_t *)(uintptr_t)zc.zc_nvlist_dst) +
3751 zc.zc_nvlist_dst_size;
3752 count -= zc.zc_nvlist_dst_size;
3754 qsort(zb, count, sizeof (zbookmark_phys_t), zbookmark_mem_compare);
3756 verify(nvlist_alloc(nverrlistp, 0, KM_SLEEP) == 0);
3759 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3761 for (i = 0; i < count; i++) {
3764 /* ignoring zb_blkid and zb_level for now */
3765 if (i > 0 && zb[i-1].zb_objset == zb[i].zb_objset &&
3766 zb[i-1].zb_object == zb[i].zb_object)
3769 if (nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) != 0)
3771 if (nvlist_add_uint64(nv, ZPOOL_ERR_DATASET,
3772 zb[i].zb_objset) != 0) {
3776 if (nvlist_add_uint64(nv, ZPOOL_ERR_OBJECT,
3777 zb[i].zb_object) != 0) {
3781 if (nvlist_add_nvlist(*nverrlistp, "ejk", nv) != 0) {
3788 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3792 free((void *)(uintptr_t)zc.zc_nvlist_dst);
3793 return (no_memory(zhp->zpool_hdl));
3797 * Upgrade a ZFS pool to the latest on-disk version.
3800 zpool_upgrade(zpool_handle_t *zhp, uint64_t new_version)
3802 zfs_cmd_t zc = {"\0"};
3803 libzfs_handle_t *hdl = zhp->zpool_hdl;
3805 (void) strcpy(zc.zc_name, zhp->zpool_name);
3806 zc.zc_cookie = new_version;
3808 if (zfs_ioctl(hdl, ZFS_IOC_POOL_UPGRADE, &zc) != 0)
3809 return (zpool_standard_error_fmt(hdl, errno,
3810 dgettext(TEXT_DOMAIN, "cannot upgrade '%s'"),
3816 zfs_save_arguments(int argc, char **argv, char *string, int len)
3820 (void) strlcpy(string, basename(argv[0]), len);
3821 for (i = 1; i < argc; i++) {
3822 (void) strlcat(string, " ", len);
3823 (void) strlcat(string, argv[i], len);
3828 zpool_log_history(libzfs_handle_t *hdl, const char *message)
3830 zfs_cmd_t zc = {"\0"};
3834 args = fnvlist_alloc();
3835 fnvlist_add_string(args, "message", message);
3836 err = zcmd_write_src_nvlist(hdl, &zc, args);
3838 err = ioctl(hdl->libzfs_fd, ZFS_IOC_LOG_HISTORY, &zc);
3840 zcmd_free_nvlists(&zc);
3845 * Perform ioctl to get some command history of a pool.
3847 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3848 * logical offset of the history buffer to start reading from.
3850 * Upon return, 'off' is the next logical offset to read from and
3851 * 'len' is the actual amount of bytes read into 'buf'.
3854 get_history(zpool_handle_t *zhp, char *buf, uint64_t *off, uint64_t *len)
3856 zfs_cmd_t zc = {"\0"};
3857 libzfs_handle_t *hdl = zhp->zpool_hdl;
3859 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
3861 zc.zc_history = (uint64_t)(uintptr_t)buf;
3862 zc.zc_history_len = *len;
3863 zc.zc_history_offset = *off;
3865 if (ioctl(hdl->libzfs_fd, ZFS_IOC_POOL_GET_HISTORY, &zc) != 0) {
3868 return (zfs_error_fmt(hdl, EZFS_PERM,
3869 dgettext(TEXT_DOMAIN,
3870 "cannot show history for pool '%s'"),
3873 return (zfs_error_fmt(hdl, EZFS_NOHISTORY,
3874 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3875 "'%s'"), zhp->zpool_name));
3877 return (zfs_error_fmt(hdl, EZFS_BADVERSION,
3878 dgettext(TEXT_DOMAIN, "cannot get history for pool "
3879 "'%s', pool must be upgraded"), zhp->zpool_name));
3881 return (zpool_standard_error_fmt(hdl, errno,
3882 dgettext(TEXT_DOMAIN,
3883 "cannot get history for '%s'"), zhp->zpool_name));
3887 *len = zc.zc_history_len;
3888 *off = zc.zc_history_offset;
3894 * Process the buffer of nvlists, unpacking and storing each nvlist record
3895 * into 'records'. 'leftover' is set to the number of bytes that weren't
3896 * processed as there wasn't a complete record.
3899 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
3900 nvlist_t ***records, uint_t *numrecords)
3907 while (bytes_read > sizeof (reclen)) {
3909 /* get length of packed record (stored as little endian) */
3910 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
3911 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
3913 if (bytes_read < sizeof (reclen) + reclen)
3917 if (nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0) != 0)
3919 bytes_read -= sizeof (reclen) + reclen;
3920 buf += sizeof (reclen) + reclen;
3922 /* add record to nvlist array */
3924 if (ISP2(*numrecords + 1)) {
3925 tmp = realloc(*records,
3926 *numrecords * 2 * sizeof (nvlist_t *));
3934 (*records)[*numrecords - 1] = nv;
3937 *leftover = bytes_read;
3942 * Retrieve the command history of a pool.
3945 zpool_get_history(zpool_handle_t *zhp, nvlist_t **nvhisp)
3948 int buflen = 128 * 1024;
3950 nvlist_t **records = NULL;
3951 uint_t numrecords = 0;
3954 buf = malloc(buflen);
3958 uint64_t bytes_read = buflen;
3961 if ((err = get_history(zhp, buf, &off, &bytes_read)) != 0)
3964 /* if nothing else was read in, we're at EOF, just return */
3968 if ((err = zpool_history_unpack(buf, bytes_read,
3969 &leftover, &records, &numrecords)) != 0)
3972 if (leftover == bytes_read) {
3974 * no progress made, because buffer is not big enough
3975 * to hold this record; resize and retry.
3979 buf = malloc(buflen);
3990 verify(nvlist_alloc(nvhisp, NV_UNIQUE_NAME, 0) == 0);
3991 verify(nvlist_add_nvlist_array(*nvhisp, ZPOOL_HIST_RECORD,
3992 records, numrecords) == 0);
3994 for (i = 0; i < numrecords; i++)
3995 nvlist_free(records[i]);
4002 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
4003 * If there is a new event available 'nvp' will contain a newly allocated
4004 * nvlist and 'dropped' will be set to the number of missed events since
4005 * the last call to this function. When 'nvp' is set to NULL it indicates
4006 * no new events are available. In either case the function returns 0 and
4007 * it is up to the caller to free 'nvp'. In the case of a fatal error the
4008 * function will return a non-zero value. When the function is called in
4009 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
4010 * it will not return until a new event is available.
4013 zpool_events_next(libzfs_handle_t *hdl, nvlist_t **nvp,
4014 int *dropped, unsigned flags, int zevent_fd)
4016 zfs_cmd_t zc = {"\0"};
4021 zc.zc_cleanup_fd = zevent_fd;
4023 if (flags & ZEVENT_NONBLOCK)
4024 zc.zc_guid = ZEVENT_NONBLOCK;
4026 if (zcmd_alloc_dst_nvlist(hdl, &zc, ZEVENT_SIZE) != 0)
4030 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_NEXT, &zc) != 0) {
4033 error = zfs_error_fmt(hdl, EZFS_POOLUNAVAIL,
4034 dgettext(TEXT_DOMAIN, "zfs shutdown"));
4037 /* Blocking error case should not occur */
4038 if (!(flags & ZEVENT_NONBLOCK))
4039 error = zpool_standard_error_fmt(hdl, errno,
4040 dgettext(TEXT_DOMAIN, "cannot get event"));
4044 if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) {
4045 error = zfs_error_fmt(hdl, EZFS_NOMEM,
4046 dgettext(TEXT_DOMAIN, "cannot get event"));
4052 error = zpool_standard_error_fmt(hdl, errno,
4053 dgettext(TEXT_DOMAIN, "cannot get event"));
4058 error = zcmd_read_dst_nvlist(hdl, &zc, nvp);
4062 *dropped = (int)zc.zc_cookie;
4064 zcmd_free_nvlists(&zc);
4073 zpool_events_clear(libzfs_handle_t *hdl, int *count)
4075 zfs_cmd_t zc = {"\0"};
4078 (void) snprintf(msg, sizeof (msg), dgettext(TEXT_DOMAIN,
4079 "cannot clear events"));
4081 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_CLEAR, &zc) != 0)
4082 return (zpool_standard_error_fmt(hdl, errno, msg));
4085 *count = (int)zc.zc_cookie; /* # of events cleared */
4091 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4092 * the passed zevent_fd file handle. On success zero is returned,
4093 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4096 zpool_events_seek(libzfs_handle_t *hdl, uint64_t eid, int zevent_fd)
4098 zfs_cmd_t zc = {"\0"};
4102 zc.zc_cleanup_fd = zevent_fd;
4104 if (zfs_ioctl(hdl, ZFS_IOC_EVENTS_SEEK, &zc) != 0) {
4107 error = zfs_error_fmt(hdl, EZFS_NOENT,
4108 dgettext(TEXT_DOMAIN, "cannot get event"));
4112 error = zfs_error_fmt(hdl, EZFS_NOMEM,
4113 dgettext(TEXT_DOMAIN, "cannot get event"));
4117 error = zpool_standard_error_fmt(hdl, errno,
4118 dgettext(TEXT_DOMAIN, "cannot get event"));
4127 zpool_obj_to_path(zpool_handle_t *zhp, uint64_t dsobj, uint64_t obj,
4128 char *pathname, size_t len)
4130 zfs_cmd_t zc = {"\0"};
4131 boolean_t mounted = B_FALSE;
4132 char *mntpnt = NULL;
4133 char dsname[ZFS_MAX_DATASET_NAME_LEN];
4136 /* special case for the MOS */
4137 (void) snprintf(pathname, len, "<metadata>:<0x%llx>",
4142 /* get the dataset's name */
4143 (void) strlcpy(zc.zc_name, zhp->zpool_name, sizeof (zc.zc_name));
4145 if (ioctl(zhp->zpool_hdl->libzfs_fd,
4146 ZFS_IOC_DSOBJ_TO_DSNAME, &zc) != 0) {
4147 /* just write out a path of two object numbers */
4148 (void) snprintf(pathname, len, "<0x%llx>:<0x%llx>",
4149 (longlong_t)dsobj, (longlong_t)obj);
4152 (void) strlcpy(dsname, zc.zc_value, sizeof (dsname));
4154 /* find out if the dataset is mounted */
4155 mounted = is_mounted(zhp->zpool_hdl, dsname, &mntpnt);
4157 /* get the corrupted object's path */
4158 (void) strlcpy(zc.zc_name, dsname, sizeof (zc.zc_name));
4160 if (ioctl(zhp->zpool_hdl->libzfs_fd, ZFS_IOC_OBJ_TO_PATH,
4163 (void) snprintf(pathname, len, "%s%s", mntpnt,
4166 (void) snprintf(pathname, len, "%s:%s",
4167 dsname, zc.zc_value);
4170 (void) snprintf(pathname, len, "%s:<0x%llx>", dsname,
4177 * Read the EFI label from the config, if a label does not exist then
4178 * pass back the error to the caller. If the caller has passed a non-NULL
4179 * diskaddr argument then we set it to the starting address of the EFI
4183 read_efi_label(nvlist_t *config, diskaddr_t *sb)
4187 char diskname[MAXPATHLEN];
4190 if (nvlist_lookup_string(config, ZPOOL_CONFIG_PATH, &path) != 0)
4193 (void) snprintf(diskname, sizeof (diskname), "%s%s", DISK_ROOT,
4194 strrchr(path, '/'));
4195 if ((fd = open(diskname, O_RDONLY|O_DIRECT)) >= 0) {
4196 struct dk_gpt *vtoc;
4198 if ((err = efi_alloc_and_read(fd, &vtoc)) >= 0) {
4200 *sb = vtoc->efi_parts[0].p_start;
4209 * determine where a partition starts on a disk in the current
4213 find_start_block(nvlist_t *config)
4217 diskaddr_t sb = MAXOFFSET_T;
4220 if (nvlist_lookup_nvlist_array(config,
4221 ZPOOL_CONFIG_CHILDREN, &child, &children) != 0) {
4222 if (nvlist_lookup_uint64(config,
4223 ZPOOL_CONFIG_WHOLE_DISK,
4224 &wholedisk) != 0 || !wholedisk) {
4225 return (MAXOFFSET_T);
4227 if (read_efi_label(config, &sb) < 0)
4232 for (c = 0; c < children; c++) {
4233 sb = find_start_block(child[c]);
4234 if (sb != MAXOFFSET_T) {
4238 return (MAXOFFSET_T);
4242 zpool_label_disk_check(char *path)
4244 struct dk_gpt *vtoc;
4247 if ((fd = open(path, O_RDONLY|O_DIRECT)) < 0)
4250 if ((err = efi_alloc_and_read(fd, &vtoc)) != 0) {
4255 if (vtoc->efi_flags & EFI_GPT_PRIMARY_CORRUPT) {
4267 * Generate a unique partition name for the ZFS member. Partitions must
4268 * have unique names to ensure udev will be able to create symlinks under
4269 * /dev/disk/by-partlabel/ for all pool members. The partition names are
4270 * of the form <pool>-<unique-id>.
4273 zpool_label_name(char *label_name, int label_size)
4278 fd = open("/dev/urandom", O_RDONLY);
4280 if (read(fd, &id, sizeof (id)) != sizeof (id))
4287 id = (((uint64_t)rand()) << 32) | (uint64_t)rand();
4289 snprintf(label_name, label_size, "zfs-%016llx", (u_longlong_t)id);
4293 * Label an individual disk. The name provided is the short name,
4294 * stripped of any leading /dev path.
4297 zpool_label_disk(libzfs_handle_t *hdl, zpool_handle_t *zhp, char *name)
4299 char path[MAXPATHLEN];
4300 struct dk_gpt *vtoc;
4302 size_t resv = EFI_MIN_RESV_SIZE;
4303 uint64_t slice_size;
4304 diskaddr_t start_block;
4307 /* prepare an error message just in case */
4308 (void) snprintf(errbuf, sizeof (errbuf),
4309 dgettext(TEXT_DOMAIN, "cannot label '%s'"), name);
4314 verify(nvlist_lookup_nvlist(zhp->zpool_config,
4315 ZPOOL_CONFIG_VDEV_TREE, &nvroot) == 0);
4317 if (zhp->zpool_start_block == 0)
4318 start_block = find_start_block(nvroot);
4320 start_block = zhp->zpool_start_block;
4321 zhp->zpool_start_block = start_block;
4324 start_block = NEW_START_BLOCK;
4327 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
4329 if ((fd = open(path, O_RDWR|O_DIRECT|O_EXCL)) < 0) {
4331 * This shouldn't happen. We've long since verified that this
4332 * is a valid device.
4334 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
4335 "label '%s': unable to open device: %d"), path, errno);
4336 return (zfs_error(hdl, EZFS_OPENFAILED, errbuf));
4339 if (efi_alloc_and_init(fd, EFI_NUMPAR, &vtoc) != 0) {
4341 * The only way this can fail is if we run out of memory, or we
4342 * were unable to read the disk's capacity
4344 if (errno == ENOMEM)
4345 (void) no_memory(hdl);
4348 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "cannot "
4349 "label '%s': unable to read disk capacity"), path);
4351 return (zfs_error(hdl, EZFS_NOCAP, errbuf));
4354 slice_size = vtoc->efi_last_u_lba + 1;
4355 slice_size -= EFI_MIN_RESV_SIZE;
4356 if (start_block == MAXOFFSET_T)
4357 start_block = NEW_START_BLOCK;
4358 slice_size -= start_block;
4359 slice_size = P2ALIGN(slice_size, PARTITION_END_ALIGNMENT);
4361 vtoc->efi_parts[0].p_start = start_block;
4362 vtoc->efi_parts[0].p_size = slice_size;
4365 * Why we use V_USR: V_BACKUP confuses users, and is considered
4366 * disposable by some EFI utilities (since EFI doesn't have a backup
4367 * slice). V_UNASSIGNED is supposed to be used only for zero size
4368 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4369 * etc. were all pretty specific. V_USR is as close to reality as we
4370 * can get, in the absence of V_OTHER.
4372 vtoc->efi_parts[0].p_tag = V_USR;
4373 zpool_label_name(vtoc->efi_parts[0].p_name, EFI_PART_NAME_LEN);
4375 vtoc->efi_parts[8].p_start = slice_size + start_block;
4376 vtoc->efi_parts[8].p_size = resv;
4377 vtoc->efi_parts[8].p_tag = V_RESERVED;
4379 rval = efi_write(fd, vtoc);
4381 /* Flush the buffers to disk and invalidate the page cache. */
4383 (void) ioctl(fd, BLKFLSBUF);
4386 rval = efi_rescan(fd);
4389 * Some block drivers (like pcata) may not support EFI GPT labels.
4390 * Print out a helpful error message directing the user to manually
4391 * label the disk and give a specific slice.
4397 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "try using "
4398 "parted(8) and then provide a specific slice: %d"), rval);
4399 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4405 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
4406 (void) zfs_append_partition(path, MAXPATHLEN);
4408 /* Wait to udev to signal use the device has settled. */
4409 rval = zpool_label_disk_wait(path, DISK_LABEL_WAIT);
4411 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "failed to "
4412 "detect device partitions on '%s': %d"), path, rval);
4413 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4416 /* We can't be to paranoid. Read the label back and verify it. */
4417 (void) snprintf(path, sizeof (path), "%s/%s", DISK_ROOT, name);
4418 rval = zpool_label_disk_check(path);
4420 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "freshly written "
4421 "EFI label on '%s' is damaged. Ensure\nthis device "
4422 "is not in in use, and is functioning properly: %d"),
4424 return (zfs_error(hdl, EZFS_LABELFAILED, errbuf));
4431 * Allocate and return the underlying device name for a device mapper device.
4432 * If a device mapper device maps to multiple devices, return the first device.
4434 * For example, dm_name = "/dev/dm-0" could return "/dev/sda". Symlinks to a
4435 * DM device (like /dev/disk/by-vdev/A0) are also allowed.
4437 * Returns device name, or NULL on error or no match. If dm_name is not a DM
4438 * device then return NULL.
4440 * NOTE: The returned name string must be *freed*.
4443 dm_get_underlying_path(char *dm_name)
4453 if (dm_name == NULL)
4456 /* dm name may be a symlink (like /dev/disk/by-vdev/A0) */
4457 realp = realpath(dm_name, NULL);
4462 * If they preface 'dev' with a path (like "/dev") then strip it off.
4463 * We just want the 'dm-N' part.
4465 tmp = strrchr(realp, '/');
4467 dev_str = tmp + 1; /* +1 since we want the chr after '/' */
4471 size = asprintf(&tmp, "/sys/block/%s/slaves/", dev_str);
4472 if (size == -1 || !tmp)
4479 /* Return first sd* entry in /sys/block/dm-N/slaves/ */
4480 while ((ep = readdir(dp))) {
4481 if (ep->d_type != DT_DIR) { /* skip "." and ".." dirs */
4482 size = asprintf(&path, "/dev/%s", ep->d_name);
4496 * Return 1 if device is a device mapper or multipath device.
4500 zfs_dev_is_dm(char *dev_name)
4504 tmp = dm_get_underlying_path(dev_name);
4513 * By "whole disk" we mean an entire physical disk (something we can
4514 * label, toggle the write cache on, etc.) as opposed to the full
4515 * capacity of a pseudo-device such as lofi or did. We act as if we
4516 * are labeling the disk, which should be a pretty good test of whether
4517 * it's a viable device or not. Returns B_TRUE if it is and B_FALSE if
4521 zfs_dev_is_whole_disk(char *dev_name)
4523 struct dk_gpt *label;
4526 if ((fd = open(dev_name, O_RDONLY | O_DIRECT)) < 0)
4529 if (efi_alloc_and_init(fd, EFI_NUMPAR, &label) != 0) {
4541 * Lookup the underlying device for a device name
4543 * Often you'll have a symlink to a device, a partition device,
4544 * or a multipath device, and want to look up the underlying device.
4545 * This function returns the underlying device name. If the device
4546 * name is already the underlying device, then just return the same
4547 * name. If the device is a DM device with multiple underlying devices
4548 * then return the first one.
4552 * 1. /dev/disk/by-id/ata-QEMU_HARDDISK_QM00001 -> ../../sda
4553 * dev_name: /dev/disk/by-id/ata-QEMU_HARDDISK_QM00001
4556 * 2. /dev/mapper/mpatha (made up of /dev/sda and /dev/sdb)
4557 * dev_name: /dev/mapper/mpatha
4558 * returns: /dev/sda (first device)
4560 * 3. /dev/sda (already the underlying device)
4561 * dev_name: /dev/sda
4564 * 4. /dev/dm-3 (mapped to /dev/sda)
4565 * dev_name: /dev/dm-3
4568 * 5. /dev/disk/by-id/scsi-0QEMU_drive-scsi0-0-0-0-part9 -> ../../sdb9
4569 * dev_name: /dev/disk/by-id/scsi-0QEMU_drive-scsi0-0-0-0-part9
4572 * 6. /dev/disk/by-uuid/5df030cf-3cd9-46e4-8e99-3ccb462a4e9a -> ../dev/sda2
4573 * dev_name: /dev/disk/by-uuid/5df030cf-3cd9-46e4-8e99-3ccb462a4e9a
4576 * Returns underlying device name, or NULL on error or no match.
4578 * NOTE: The returned name string must be *freed*.
4581 zfs_get_underlying_path(char *dev_name)
4586 if (dev_name == NULL)
4589 tmp = dm_get_underlying_path(dev_name);
4591 /* dev_name not a DM device, so just un-symlinkize it */
4593 tmp = realpath(dev_name, NULL);
4596 name = zfs_strip_partition_path(tmp);
4604 * Given a dev name like "sda", return the full enclosure sysfs path to
4605 * the disk. You can also pass in the name with "/dev" prepended
4606 * to it (like /dev/sda).
4608 * For example, disk "sda" in enclosure slot 1:
4610 * returns: "/sys/class/enclosure/1:0:3:0/Slot 1"
4612 * 'dev' must be a non-devicemapper device.
4614 * Returned string must be freed.
4617 zfs_get_enclosure_sysfs_path(char *dev_name)
4621 char buf[MAXPATHLEN];
4629 if (dev_name == NULL)
4632 /* If they preface 'dev' with a path (like "/dev") then strip it off */
4633 tmp1 = strrchr(dev_name, '/');
4635 dev_name = tmp1 + 1; /* +1 since we want the chr after '/' */
4637 tmpsize = asprintf(&tmp1, "/sys/block/%s/device", dev_name);
4638 if (tmpsize == -1 || tmp1 == NULL) {
4645 tmp1 = NULL; /* To make free() at the end a NOP */
4650 * Look though all sysfs entries in /sys/block/<dev>/device for
4651 * the enclosure symlink.
4653 while ((ep = readdir(dp))) {
4654 /* Ignore everything that's not our enclosure_device link */
4655 if (strstr(ep->d_name, "enclosure_device") == NULL)
4658 if (asprintf(&tmp2, "%s/%s", tmp1, ep->d_name) == -1 ||
4662 size = readlink(tmp2, buf, sizeof (buf));
4664 /* Did readlink fail or crop the link name? */
4665 if (size == -1 || size >= sizeof (buf)) {
4667 tmp2 = NULL; /* To make free() at the end a NOP */
4672 * We got a valid link. readlink() doesn't terminate strings
4673 * so we have to do it.
4678 * Our link will look like:
4680 * "../../../../port-11:1:2/..STUFF../enclosure/1:0:3:0/SLOT 1"
4682 * We want to grab the "enclosure/1:0:3:0/SLOT 1" part
4684 tmp3 = strstr(buf, "enclosure");
4688 if (asprintf(&path, "/sys/class/%s", tmp3) == -1) {
4689 /* If asprintf() fails, 'path' is undefined */