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 https://opensource.org/licenses/CDDL-1.0.
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, 2024 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27 * Copyright (c) 2018 Datto Inc.
28 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
29 * Copyright (c) 2017, Intel Corporation.
30 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>
31 * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
32 * Copyright (c) 2021, 2023, Klara Inc.
44 #include <sys/efi_partition.h>
45 #include <sys/systeminfo.h>
46 #include <sys/zfs_ioctl.h>
47 #include <sys/zfs_sysfs.h>
48 #include <sys/vdev_disk.h>
49 #include <sys/types.h>
54 #include "zfs_namecheck.h"
56 #include "libzfs_impl.h"
57 #include "zfs_comutil.h"
58 #include "zfeature_common.h"
60 static boolean_t
zpool_vdev_is_interior(const char *name
);
62 typedef struct prop_flags
{
63 unsigned int create
:1; /* Validate property on creation */
64 unsigned int import
:1; /* Validate property on import */
65 unsigned int vdevprop
:1; /* Validate property as a VDEV property */
69 * ====================================================================
70 * zpool property functions
71 * ====================================================================
75 zpool_get_all_props(zpool_handle_t
*zhp
)
77 zfs_cmd_t zc
= {"\0"};
78 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
80 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
82 if (zhp
->zpool_n_propnames
> 0) {
83 nvlist_t
*innvl
= fnvlist_alloc();
84 fnvlist_add_string_array(innvl
, ZPOOL_GET_PROPS_NAMES
,
85 zhp
->zpool_propnames
, zhp
->zpool_n_propnames
);
86 zcmd_write_src_nvlist(hdl
, &zc
, innvl
);
89 zcmd_alloc_dst_nvlist(hdl
, &zc
, 0);
91 while (zfs_ioctl(hdl
, ZFS_IOC_POOL_GET_PROPS
, &zc
) != 0) {
93 zcmd_expand_dst_nvlist(hdl
, &zc
);
95 zcmd_free_nvlists(&zc
);
100 if (zcmd_read_dst_nvlist(hdl
, &zc
, &zhp
->zpool_props
) != 0) {
101 zcmd_free_nvlists(&zc
);
105 zcmd_free_nvlists(&zc
);
111 zpool_props_refresh(zpool_handle_t
*zhp
)
115 old_props
= zhp
->zpool_props
;
117 if (zpool_get_all_props(zhp
) != 0)
120 nvlist_free(old_props
);
125 zpool_get_prop_string(zpool_handle_t
*zhp
, zpool_prop_t prop
,
130 zprop_source_t source
;
132 nvl
= zhp
->zpool_props
;
133 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
134 source
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
135 value
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
137 source
= ZPROP_SRC_DEFAULT
;
138 if ((value
= zpool_prop_default_string(prop
)) == NULL
)
149 zpool_get_prop_int(zpool_handle_t
*zhp
, zpool_prop_t prop
, zprop_source_t
*src
)
153 zprop_source_t source
;
155 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
)) {
157 * zpool_get_all_props() has most likely failed because
158 * the pool is faulted, but if all we need is the top level
159 * vdev's guid then get it from the zhp config nvlist.
161 if ((prop
== ZPOOL_PROP_GUID
) &&
162 (nvlist_lookup_nvlist(zhp
->zpool_config
,
163 ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0) &&
164 (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
)
168 return (zpool_prop_default_numeric(prop
));
171 nvl
= zhp
->zpool_props
;
172 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
173 source
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
174 value
= fnvlist_lookup_uint64(nv
, ZPROP_VALUE
);
176 source
= ZPROP_SRC_DEFAULT
;
177 value
= zpool_prop_default_numeric(prop
);
187 * Map VDEV STATE to printed strings.
190 zpool_state_to_name(vdev_state_t state
, vdev_aux_t aux
)
193 case VDEV_STATE_CLOSED
:
194 case VDEV_STATE_OFFLINE
:
195 return (gettext("OFFLINE"));
196 case VDEV_STATE_REMOVED
:
197 return (gettext("REMOVED"));
198 case VDEV_STATE_CANT_OPEN
:
199 if (aux
== VDEV_AUX_CORRUPT_DATA
|| aux
== VDEV_AUX_BAD_LOG
)
200 return (gettext("FAULTED"));
201 else if (aux
== VDEV_AUX_SPLIT_POOL
)
202 return (gettext("SPLIT"));
204 return (gettext("UNAVAIL"));
205 case VDEV_STATE_FAULTED
:
206 return (gettext("FAULTED"));
207 case VDEV_STATE_DEGRADED
:
208 return (gettext("DEGRADED"));
209 case VDEV_STATE_HEALTHY
:
210 return (gettext("ONLINE"));
216 return (gettext("UNKNOWN"));
220 * Map POOL STATE to printed strings.
223 zpool_pool_state_to_name(pool_state_t state
)
228 case POOL_STATE_ACTIVE
:
229 return (gettext("ACTIVE"));
230 case POOL_STATE_EXPORTED
:
231 return (gettext("EXPORTED"));
232 case POOL_STATE_DESTROYED
:
233 return (gettext("DESTROYED"));
234 case POOL_STATE_SPARE
:
235 return (gettext("SPARE"));
236 case POOL_STATE_L2CACHE
:
237 return (gettext("L2CACHE"));
238 case POOL_STATE_UNINITIALIZED
:
239 return (gettext("UNINITIALIZED"));
240 case POOL_STATE_UNAVAIL
:
241 return (gettext("UNAVAIL"));
242 case POOL_STATE_POTENTIALLY_ACTIVE
:
243 return (gettext("POTENTIALLY_ACTIVE"));
246 return (gettext("UNKNOWN"));
250 * Given a pool handle, return the pool health string ("ONLINE", "DEGRADED",
254 zpool_get_state_str(zpool_handle_t
*zhp
)
256 zpool_errata_t errata
;
257 zpool_status_t status
;
260 status
= zpool_get_status(zhp
, NULL
, &errata
);
262 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
263 str
= gettext("FAULTED");
264 } else if (status
== ZPOOL_STATUS_IO_FAILURE_WAIT
||
265 status
== ZPOOL_STATUS_IO_FAILURE_CONTINUE
||
266 status
== ZPOOL_STATUS_IO_FAILURE_MMP
) {
267 str
= gettext("SUSPENDED");
269 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(
270 zpool_get_config(zhp
, NULL
), ZPOOL_CONFIG_VDEV_TREE
);
272 vdev_stat_t
*vs
= (vdev_stat_t
*)fnvlist_lookup_uint64_array(
273 nvroot
, ZPOOL_CONFIG_VDEV_STATS
, &vsc
);
274 str
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
280 * Get a zpool property value for 'prop' and return the value in
281 * a pre-allocated buffer.
284 zpool_get_prop(zpool_handle_t
*zhp
, zpool_prop_t prop
, char *buf
,
285 size_t len
, zprop_source_t
*srctype
, boolean_t literal
)
289 zprop_source_t src
= ZPROP_SRC_NONE
;
291 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
293 case ZPOOL_PROP_NAME
:
294 (void) strlcpy(buf
, zpool_get_name(zhp
), len
);
297 case ZPOOL_PROP_HEALTH
:
298 (void) strlcpy(buf
, zpool_get_state_str(zhp
), len
);
301 case ZPOOL_PROP_GUID
:
302 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
303 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
306 case ZPOOL_PROP_ALTROOT
:
307 case ZPOOL_PROP_CACHEFILE
:
308 case ZPOOL_PROP_COMMENT
:
309 case ZPOOL_PROP_COMPATIBILITY
:
310 if (zhp
->zpool_props
!= NULL
||
311 zpool_get_all_props(zhp
) == 0) {
313 zpool_get_prop_string(zhp
, prop
, &src
),
319 (void) strlcpy(buf
, "-", len
);
329 * ZPOOL_PROP_DEDUPCACHED can be fetched by name only using
330 * the ZPOOL_GET_PROPS_NAMES mechanism
332 if (prop
== ZPOOL_PROP_DEDUPCACHED
) {
333 zpool_add_propname(zhp
, ZPOOL_DEDUPCACHED_PROP_NAME
);
334 (void) zpool_get_all_props(zhp
);
337 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
) &&
338 prop
!= ZPOOL_PROP_NAME
)
341 switch (zpool_prop_get_type(prop
)) {
342 case PROP_TYPE_STRING
:
343 (void) strlcpy(buf
, zpool_get_prop_string(zhp
, prop
, &src
),
347 case PROP_TYPE_NUMBER
:
348 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
351 case ZPOOL_PROP_DEDUP_TABLE_QUOTA
:
353 * If dedup quota is 0, we translate this into 'none'
354 * (unless literal is set). And if it is UINT64_MAX
355 * we translate that as 'automatic' (limit to size of
356 * the dedicated dedup VDEV. Otherwise, fall throught
357 * into the regular number formating.
360 (void) strlcpy(buf
, literal
? "0" : "none",
363 } else if (intval
== UINT64_MAX
) {
364 (void) strlcpy(buf
, "auto", len
);
369 case ZPOOL_PROP_SIZE
:
370 case ZPOOL_PROP_ALLOCATED
:
371 case ZPOOL_PROP_FREE
:
372 case ZPOOL_PROP_FREEING
:
373 case ZPOOL_PROP_LEAKED
:
374 case ZPOOL_PROP_ASHIFT
:
375 case ZPOOL_PROP_MAXBLOCKSIZE
:
376 case ZPOOL_PROP_MAXDNODESIZE
:
377 case ZPOOL_PROP_BCLONESAVED
:
378 case ZPOOL_PROP_BCLONEUSED
:
379 case ZPOOL_PROP_DEDUP_TABLE_SIZE
:
380 case ZPOOL_PROP_DEDUPCACHED
:
382 (void) snprintf(buf
, len
, "%llu",
383 (u_longlong_t
)intval
);
385 (void) zfs_nicenum(intval
, buf
, len
);
388 case ZPOOL_PROP_EXPANDSZ
:
389 case ZPOOL_PROP_CHECKPOINT
:
391 (void) strlcpy(buf
, "-", len
);
392 } else if (literal
) {
393 (void) snprintf(buf
, len
, "%llu",
394 (u_longlong_t
)intval
);
396 (void) zfs_nicebytes(intval
, buf
, len
);
400 case ZPOOL_PROP_CAPACITY
:
402 (void) snprintf(buf
, len
, "%llu",
403 (u_longlong_t
)intval
);
405 (void) snprintf(buf
, len
, "%llu%%",
406 (u_longlong_t
)intval
);
410 case ZPOOL_PROP_FRAGMENTATION
:
411 if (intval
== UINT64_MAX
) {
412 (void) strlcpy(buf
, "-", len
);
413 } else if (literal
) {
414 (void) snprintf(buf
, len
, "%llu",
415 (u_longlong_t
)intval
);
417 (void) snprintf(buf
, len
, "%llu%%",
418 (u_longlong_t
)intval
);
422 case ZPOOL_PROP_BCLONERATIO
:
423 case ZPOOL_PROP_DEDUPRATIO
:
425 (void) snprintf(buf
, len
, "%llu.%02llu",
426 (u_longlong_t
)(intval
/ 100),
427 (u_longlong_t
)(intval
% 100));
429 (void) snprintf(buf
, len
, "%llu.%02llux",
430 (u_longlong_t
)(intval
/ 100),
431 (u_longlong_t
)(intval
% 100));
434 case ZPOOL_PROP_HEALTH
:
435 (void) strlcpy(buf
, zpool_get_state_str(zhp
), len
);
437 case ZPOOL_PROP_VERSION
:
438 if (intval
>= SPA_VERSION_FEATURES
) {
439 (void) snprintf(buf
, len
, "-");
444 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
448 case PROP_TYPE_INDEX
:
449 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
450 if (zpool_prop_index_to_string(prop
, intval
, &strval
)
453 (void) strlcpy(buf
, strval
, len
);
467 * Get a zpool property value for 'propname' and return the value in
468 * a pre-allocated buffer.
471 zpool_get_userprop(zpool_handle_t
*zhp
, const char *propname
, char *buf
,
472 size_t len
, zprop_source_t
*srctype
)
477 zprop_source_t source
= ZPROP_SRC_LOCAL
;
479 if (zhp
->zpool_props
== NULL
)
480 zpool_get_all_props(zhp
);
482 if (nvlist_lookup_nvlist(zhp
->zpool_props
, propname
, &nv
) == 0) {
483 if (nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &ival
) == 0)
485 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
487 source
= ZPROP_SRC_DEFAULT
;
494 (void) strlcpy(buf
, value
, len
);
500 * Check if the bootfs name has the same pool name as it is set to.
501 * Assuming bootfs is a valid dataset name.
504 bootfs_name_valid(const char *pool
, const char *bootfs
)
506 int len
= strlen(pool
);
507 if (bootfs
[0] == '\0')
510 if (!zfs_name_valid(bootfs
, ZFS_TYPE_FILESYSTEM
|ZFS_TYPE_SNAPSHOT
))
513 if (strncmp(pool
, bootfs
, len
) == 0 &&
514 (bootfs
[len
] == '/' || bootfs
[len
] == '\0'))
521 * Given an nvlist of zpool properties to be set, validate that they are
522 * correct, and parse any numeric properties (index, boolean, etc) if they are
523 * specified as strings.
526 zpool_valid_proplist(libzfs_handle_t
*hdl
, const char *poolname
,
527 nvlist_t
*props
, uint64_t version
, prop_flags_t flags
, char *errbuf
)
535 struct stat64 statbuf
;
537 char *parent
, *slash
;
540 if (nvlist_alloc(&retprops
, NV_UNIQUE_NAME
, 0) != 0) {
541 (void) no_memory(hdl
);
546 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
547 const char *propname
= nvpair_name(elem
);
549 if (flags
.vdevprop
&& zpool_prop_vdev(propname
)) {
550 vdev_prop_t vprop
= vdev_name_to_prop(propname
);
552 if (vdev_prop_readonly(vprop
)) {
553 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
554 "is readonly"), propname
);
555 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
560 if (zprop_parse_value(hdl
, elem
, vprop
, ZFS_TYPE_VDEV
,
561 retprops
, &strval
, &intval
, errbuf
) != 0)
565 } else if (flags
.vdevprop
&& vdev_prop_user(propname
)) {
566 if (nvlist_add_nvpair(retprops
, elem
) != 0) {
567 (void) no_memory(hdl
);
571 } else if (flags
.vdevprop
) {
572 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
573 "invalid property: '%s'"), propname
);
574 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
578 prop
= zpool_name_to_prop(propname
);
579 if (prop
== ZPOOL_PROP_INVAL
&& zpool_prop_feature(propname
)) {
581 char *fname
= strchr(propname
, '@') + 1;
583 err
= zfeature_lookup_name(fname
, NULL
);
585 ASSERT3U(err
, ==, ENOENT
);
586 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
587 "feature '%s' unsupported by kernel"),
589 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
593 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
594 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
595 "'%s' must be a string"), propname
);
596 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
600 (void) nvpair_value_string(elem
, &strval
);
601 if (strcmp(strval
, ZFS_FEATURE_ENABLED
) != 0 &&
602 strcmp(strval
, ZFS_FEATURE_DISABLED
) != 0) {
603 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
604 "property '%s' can only be set to "
605 "'enabled' or 'disabled'"), propname
);
606 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
611 strcmp(strval
, ZFS_FEATURE_DISABLED
) == 0) {
612 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
613 "property '%s' can only be set to "
614 "'disabled' at creation time"), propname
);
615 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
619 if (nvlist_add_uint64(retprops
, propname
, 0) != 0) {
620 (void) no_memory(hdl
);
624 } else if (prop
== ZPOOL_PROP_INVAL
&&
625 zfs_prop_user(propname
)) {
627 * This is a user property: make sure it's a
628 * string, and that it's less than ZAP_MAXNAMELEN.
630 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
631 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
632 "'%s' must be a string"), propname
);
633 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
637 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
638 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
639 "property name '%s' is too long"),
641 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
645 (void) nvpair_value_string(elem
, &strval
);
647 if (strlen(strval
) >= ZFS_MAXPROPLEN
) {
648 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
649 "property value '%s' is too long"),
651 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
655 if (nvlist_add_string(retprops
, propname
,
657 (void) no_memory(hdl
);
665 * Make sure this property is valid and applies to this type.
667 if (prop
== ZPOOL_PROP_INVAL
) {
668 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
669 "invalid property '%s'"), propname
);
670 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
674 if (zpool_prop_readonly(prop
)) {
675 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
676 "is readonly"), propname
);
677 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
681 if (!flags
.create
&& zpool_prop_setonce(prop
)) {
682 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
683 "property '%s' can only be set at "
684 "creation time"), propname
);
685 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
689 if (zprop_parse_value(hdl
, elem
, prop
, ZFS_TYPE_POOL
, retprops
,
690 &strval
, &intval
, errbuf
) != 0)
694 * Perform additional checking for specific properties.
697 case ZPOOL_PROP_VERSION
:
698 if (intval
< version
||
699 !SPA_VERSION_IS_SUPPORTED(intval
)) {
700 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
701 "property '%s' number %llu is invalid."),
702 propname
, (unsigned long long)intval
);
703 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
708 case ZPOOL_PROP_ASHIFT
:
710 (intval
< ASHIFT_MIN
|| intval
> ASHIFT_MAX
)) {
711 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
712 "property '%s' number %llu is invalid, "
713 "only values between %" PRId32
" and %"
714 PRId32
" are allowed."),
715 propname
, (unsigned long long)intval
,
716 ASHIFT_MIN
, ASHIFT_MAX
);
717 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
722 case ZPOOL_PROP_BOOTFS
:
723 if (flags
.create
|| flags
.import
) {
724 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
725 "property '%s' cannot be set at creation "
726 "or import time"), propname
);
727 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
731 if (version
< SPA_VERSION_BOOTFS
) {
732 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
733 "pool must be upgraded to support "
734 "'%s' property"), propname
);
735 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
740 * bootfs property value has to be a dataset name and
741 * the dataset has to be in the same pool as it sets to.
743 if (!bootfs_name_valid(poolname
, strval
)) {
744 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
745 "is an invalid name"), strval
);
746 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
750 if ((zhp
= zpool_open_canfail(hdl
, poolname
)) == NULL
) {
751 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
752 "could not open pool '%s'"), poolname
);
753 (void) zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
);
759 case ZPOOL_PROP_ALTROOT
:
760 if (!flags
.create
&& !flags
.import
) {
761 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
762 "property '%s' can only be set during pool "
763 "creation or import"), propname
);
764 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
768 if (strval
[0] != '/') {
769 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
770 "bad alternate root '%s'"), strval
);
771 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
776 case ZPOOL_PROP_CACHEFILE
:
777 if (strval
[0] == '\0')
780 if (strcmp(strval
, "none") == 0)
783 if (strval
[0] != '/') {
784 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
785 "property '%s' must be empty, an "
786 "absolute path, or 'none'"), propname
);
787 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
791 parent
= strdup(strval
);
792 if (parent
== NULL
) {
793 (void) zfs_error(hdl
, EZFS_NOMEM
, errbuf
);
796 slash
= strrchr(parent
, '/');
798 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
799 strcmp(slash
, "/..") == 0) {
800 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
801 "'%s' is not a valid file"), parent
);
802 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
809 if (parent
[0] != '\0' &&
810 (stat64(parent
, &statbuf
) != 0 ||
811 !S_ISDIR(statbuf
.st_mode
))) {
812 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
813 "'%s' is not a valid directory"),
815 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
823 case ZPOOL_PROP_COMPATIBILITY
:
824 switch (zpool_load_compat(strval
, NULL
, report
, 1024)) {
825 case ZPOOL_COMPATIBILITY_OK
:
826 case ZPOOL_COMPATIBILITY_WARNTOKEN
:
828 case ZPOOL_COMPATIBILITY_BADFILE
:
829 case ZPOOL_COMPATIBILITY_BADTOKEN
:
830 case ZPOOL_COMPATIBILITY_NOFILES
:
831 zfs_error_aux(hdl
, "%s", report
);
832 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
837 case ZPOOL_PROP_COMMENT
:
838 for (check
= strval
; *check
!= '\0'; check
++) {
839 if (!isprint(*check
)) {
841 dgettext(TEXT_DOMAIN
,
842 "comment may only have printable "
844 (void) zfs_error(hdl
, EZFS_BADPROP
,
849 if (strlen(strval
) > ZPROP_MAX_COMMENT
) {
850 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
851 "comment must not exceed %d characters"),
853 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
857 case ZPOOL_PROP_READONLY
:
859 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
860 "property '%s' can only be set at "
861 "import time"), propname
);
862 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
866 case ZPOOL_PROP_MULTIHOST
:
867 if (get_system_hostid() == 0) {
868 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
869 "requires a non-zero system hostid"));
870 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
874 case ZPOOL_PROP_DEDUPDITTO
:
875 printf("Note: property '%s' no longer has "
876 "any effect\n", propname
);
886 nvlist_free(retprops
);
891 * Set zpool property : propname=propval.
894 zpool_set_prop(zpool_handle_t
*zhp
, const char *propname
, const char *propval
)
896 zfs_cmd_t zc
= {"\0"};
898 char errbuf
[ERRBUFLEN
];
899 nvlist_t
*nvl
= NULL
;
902 prop_flags_t flags
= { 0 };
904 (void) snprintf(errbuf
, sizeof (errbuf
),
905 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
908 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
909 return (no_memory(zhp
->zpool_hdl
));
911 if (nvlist_add_string(nvl
, propname
, propval
) != 0) {
913 return (no_memory(zhp
->zpool_hdl
));
916 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
917 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
918 zhp
->zpool_name
, nvl
, version
, flags
, errbuf
)) == NULL
) {
927 * Execute the corresponding ioctl() to set this property.
929 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
931 zcmd_write_src_nvlist(zhp
->zpool_hdl
, &zc
, nvl
);
933 ret
= zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_SET_PROPS
, &zc
);
935 zcmd_free_nvlists(&zc
);
939 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
941 (void) zpool_props_refresh(zhp
);
947 zpool_expand_proplist(zpool_handle_t
*zhp
, zprop_list_t
**plp
,
948 zfs_type_t type
, boolean_t literal
)
950 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
952 char buf
[ZFS_MAXPROPLEN
];
953 nvlist_t
*features
= NULL
;
956 boolean_t firstexpand
= (NULL
== *plp
);
959 if (zprop_expand_list(hdl
, plp
, type
) != 0)
962 if (type
== ZFS_TYPE_VDEV
)
966 while (*last
!= NULL
)
967 last
= &(*last
)->pl_next
;
970 features
= zpool_get_features(zhp
);
972 if ((*plp
)->pl_all
&& firstexpand
) {
973 /* Handle userprops in the all properties case */
974 if (zhp
->zpool_props
== NULL
&& zpool_props_refresh(zhp
))
978 while ((nvp
= nvlist_next_nvpair(zhp
->zpool_props
, nvp
)) !=
980 const char *propname
= nvpair_name(nvp
);
982 if (!zfs_prop_user(propname
))
985 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
986 entry
->pl_prop
= ZPROP_USERPROP
;
987 entry
->pl_user_prop
= zfs_strdup(hdl
, propname
);
988 entry
->pl_width
= strlen(entry
->pl_user_prop
);
989 entry
->pl_all
= B_TRUE
;
992 last
= &entry
->pl_next
;
995 for (i
= 0; i
< SPA_FEATURES
; i
++) {
996 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
997 entry
->pl_prop
= ZPROP_USERPROP
;
998 entry
->pl_user_prop
= zfs_asprintf(hdl
, "feature@%s",
999 spa_feature_table
[i
].fi_uname
);
1000 entry
->pl_width
= strlen(entry
->pl_user_prop
);
1001 entry
->pl_all
= B_TRUE
;
1004 last
= &entry
->pl_next
;
1008 /* add any unsupported features */
1009 for (nvp
= nvlist_next_nvpair(features
, NULL
);
1010 nvp
!= NULL
; nvp
= nvlist_next_nvpair(features
, nvp
)) {
1014 if (zfeature_is_supported(nvpair_name(nvp
)))
1017 propname
= zfs_asprintf(hdl
, "unsupported@%s",
1021 * Before adding the property to the list make sure that no
1022 * other pool already added the same property.
1026 while (entry
!= NULL
) {
1027 if (entry
->pl_user_prop
!= NULL
&&
1028 strcmp(propname
, entry
->pl_user_prop
) == 0) {
1032 entry
= entry
->pl_next
;
1039 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
1040 entry
->pl_prop
= ZPROP_USERPROP
;
1041 entry
->pl_user_prop
= propname
;
1042 entry
->pl_width
= strlen(entry
->pl_user_prop
);
1043 entry
->pl_all
= B_TRUE
;
1046 last
= &entry
->pl_next
;
1049 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
1050 if (entry
->pl_fixed
&& !literal
)
1053 if (entry
->pl_prop
!= ZPROP_USERPROP
&&
1054 zpool_get_prop(zhp
, entry
->pl_prop
, buf
, sizeof (buf
),
1055 NULL
, literal
) == 0) {
1056 if (strlen(buf
) > entry
->pl_width
)
1057 entry
->pl_width
= strlen(buf
);
1058 } else if (entry
->pl_prop
== ZPROP_INVAL
&&
1059 zfs_prop_user(entry
->pl_user_prop
) &&
1060 zpool_get_userprop(zhp
, entry
->pl_user_prop
, buf
,
1061 sizeof (buf
), NULL
) == 0) {
1062 if (strlen(buf
) > entry
->pl_width
)
1063 entry
->pl_width
= strlen(buf
);
1071 vdev_expand_proplist(zpool_handle_t
*zhp
, const char *vdevname
,
1074 zprop_list_t
*entry
;
1075 char buf
[ZFS_MAXPROPLEN
];
1076 const char *strval
= NULL
;
1078 nvpair_t
*elem
= NULL
;
1079 nvlist_t
*vprops
= NULL
;
1080 nvlist_t
*propval
= NULL
;
1081 const char *propname
;
1083 zprop_list_t
**last
;
1085 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
1086 if (entry
->pl_fixed
)
1089 if (zpool_get_vdev_prop(zhp
, vdevname
, entry
->pl_prop
,
1090 entry
->pl_user_prop
, buf
, sizeof (buf
), NULL
,
1092 if (strlen(buf
) > entry
->pl_width
)
1093 entry
->pl_width
= strlen(buf
);
1095 if (entry
->pl_prop
== VDEV_PROP_NAME
&&
1096 strlen(vdevname
) > entry
->pl_width
)
1097 entry
->pl_width
= strlen(vdevname
);
1100 /* Handle the all properties case */
1102 if (*last
!= NULL
&& (*last
)->pl_all
== B_TRUE
) {
1103 while (*last
!= NULL
)
1104 last
= &(*last
)->pl_next
;
1106 err
= zpool_get_all_vdev_props(zhp
, vdevname
, &vprops
);
1110 while ((elem
= nvlist_next_nvpair(vprops
, elem
)) != NULL
) {
1111 propname
= nvpair_name(elem
);
1113 /* Skip properties that are not user defined */
1114 if ((prop
= vdev_name_to_prop(propname
)) !=
1118 if (nvpair_value_nvlist(elem
, &propval
) != 0)
1121 strval
= fnvlist_lookup_string(propval
, ZPROP_VALUE
);
1123 entry
= zfs_alloc(zhp
->zpool_hdl
,
1124 sizeof (zprop_list_t
));
1125 entry
->pl_prop
= prop
;
1126 entry
->pl_user_prop
= zfs_strdup(zhp
->zpool_hdl
,
1128 entry
->pl_width
= strlen(strval
);
1129 entry
->pl_all
= B_TRUE
;
1131 last
= &entry
->pl_next
;
1139 * Get the state for the given feature on the given ZFS pool.
1142 zpool_prop_get_feature(zpool_handle_t
*zhp
, const char *propname
, char *buf
,
1146 boolean_t found
= B_FALSE
;
1147 nvlist_t
*features
= zpool_get_features(zhp
);
1148 boolean_t supported
;
1149 const char *feature
= strchr(propname
, '@') + 1;
1151 supported
= zpool_prop_feature(propname
);
1152 ASSERT(supported
|| zpool_prop_unsupported(propname
));
1155 * Convert from feature name to feature guid. This conversion is
1156 * unnecessary for unsupported@... properties because they already
1163 ret
= zfeature_lookup_name(feature
, &fid
);
1165 (void) strlcpy(buf
, "-", len
);
1168 feature
= spa_feature_table
[fid
].fi_guid
;
1171 if (nvlist_lookup_uint64(features
, feature
, &refcount
) == 0)
1176 (void) strlcpy(buf
, ZFS_FEATURE_DISABLED
, len
);
1179 (void) strlcpy(buf
, ZFS_FEATURE_ENABLED
, len
);
1181 (void) strlcpy(buf
, ZFS_FEATURE_ACTIVE
, len
);
1185 if (refcount
== 0) {
1186 (void) strcpy(buf
, ZFS_UNSUPPORTED_INACTIVE
);
1188 (void) strcpy(buf
, ZFS_UNSUPPORTED_READONLY
);
1191 (void) strlcpy(buf
, "-", len
);
1200 * Validate the given pool name, optionally putting an extended error message in
1204 zpool_name_valid(libzfs_handle_t
*hdl
, boolean_t isopen
, const char *pool
)
1206 namecheck_err_t why
;
1210 ret
= pool_namecheck(pool
, &why
, &what
);
1213 * The rules for reserved pool names were extended at a later point.
1214 * But we need to support users with existing pools that may now be
1215 * invalid. So we only check for this expanded set of names during a
1216 * create (or import), and only in userland.
1218 if (ret
== 0 && !isopen
&&
1219 (strncmp(pool
, "mirror", 6) == 0 ||
1220 strncmp(pool
, "raidz", 5) == 0 ||
1221 strncmp(pool
, "draid", 5) == 0 ||
1222 strncmp(pool
, "spare", 5) == 0 ||
1223 strcmp(pool
, "log") == 0)) {
1226 dgettext(TEXT_DOMAIN
, "name is reserved"));
1234 case NAME_ERR_TOOLONG
:
1236 dgettext(TEXT_DOMAIN
, "name is too long"));
1239 case NAME_ERR_INVALCHAR
:
1241 dgettext(TEXT_DOMAIN
, "invalid character "
1242 "'%c' in pool name"), what
);
1245 case NAME_ERR_NOLETTER
:
1246 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1247 "name must begin with a letter"));
1250 case NAME_ERR_RESERVED
:
1251 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1252 "name is reserved"));
1255 case NAME_ERR_DISKLIKE
:
1256 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1257 "pool name is reserved"));
1260 case NAME_ERR_LEADING_SLASH
:
1261 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1262 "leading slash in name"));
1265 case NAME_ERR_EMPTY_COMPONENT
:
1266 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1267 "empty component in name"));
1270 case NAME_ERR_TRAILING_SLASH
:
1271 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1272 "trailing slash in name"));
1275 case NAME_ERR_MULTIPLE_DELIMITERS
:
1276 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1277 "multiple '@' and/or '#' delimiters in "
1281 case NAME_ERR_NO_AT
:
1282 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1283 "permission set is missing '@'"));
1287 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1288 "(%d) not defined"), why
);
1299 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1303 zpool_open_canfail(libzfs_handle_t
*hdl
, const char *pool
)
1305 zpool_handle_t
*zhp
;
1309 * Make sure the pool name is valid.
1311 if (!zpool_name_valid(hdl
, B_TRUE
, pool
)) {
1312 (void) zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1313 dgettext(TEXT_DOMAIN
, "cannot open '%s'"),
1318 zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
));
1320 zhp
->zpool_hdl
= hdl
;
1321 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1323 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1329 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "no such pool"));
1330 (void) zfs_error_fmt(hdl
, EZFS_NOENT
,
1331 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), pool
);
1340 * Like the above, but silent on error. Used when iterating over pools (because
1341 * the configuration cache may be out of date).
1344 zpool_open_silent(libzfs_handle_t
*hdl
, const char *pool
, zpool_handle_t
**ret
)
1346 zpool_handle_t
*zhp
;
1349 zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
));
1351 zhp
->zpool_hdl
= hdl
;
1352 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1354 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1370 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1374 zpool_open(libzfs_handle_t
*hdl
, const char *pool
)
1376 zpool_handle_t
*zhp
;
1378 if ((zhp
= zpool_open_canfail(hdl
, pool
)) == NULL
)
1381 if (zhp
->zpool_state
== POOL_STATE_UNAVAIL
) {
1382 (void) zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
1383 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), zhp
->zpool_name
);
1392 * Close the handle. Simply frees the memory associated with the handle.
1395 zpool_close(zpool_handle_t
*zhp
)
1397 nvlist_free(zhp
->zpool_config
);
1398 nvlist_free(zhp
->zpool_old_config
);
1399 nvlist_free(zhp
->zpool_props
);
1404 * Return the name of the pool.
1407 zpool_get_name(zpool_handle_t
*zhp
)
1409 return (zhp
->zpool_name
);
1414 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1417 zpool_get_state(zpool_handle_t
*zhp
)
1419 return (zhp
->zpool_state
);
1423 * Check if vdev list contains a special vdev
1426 zpool_has_special_vdev(nvlist_t
*nvroot
)
1431 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
, &child
,
1433 for (uint_t c
= 0; c
< children
; c
++) {
1436 if (nvlist_lookup_string(child
[c
],
1437 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
) == 0 &&
1438 strcmp(bias
, VDEV_ALLOC_BIAS_SPECIAL
) == 0) {
1447 * Check if vdev list contains a dRAID vdev
1450 zpool_has_draid_vdev(nvlist_t
*nvroot
)
1455 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
1456 &child
, &children
) == 0) {
1457 for (uint_t c
= 0; c
< children
; c
++) {
1460 if (nvlist_lookup_string(child
[c
],
1461 ZPOOL_CONFIG_TYPE
, &type
) == 0 &&
1462 strcmp(type
, VDEV_TYPE_DRAID
) == 0) {
1471 * Output a dRAID top-level vdev name in to the provided buffer.
1474 zpool_draid_name(char *name
, int len
, uint64_t data
, uint64_t parity
,
1475 uint64_t spares
, uint64_t children
)
1477 snprintf(name
, len
, "%s%llu:%llud:%lluc:%llus",
1478 VDEV_TYPE_DRAID
, (u_longlong_t
)parity
, (u_longlong_t
)data
,
1479 (u_longlong_t
)children
, (u_longlong_t
)spares
);
1485 * Return B_TRUE if the provided name is a dRAID spare name.
1488 zpool_is_draid_spare(const char *name
)
1490 uint64_t spare_id
, parity
, vdev_id
;
1492 if (sscanf(name
, VDEV_TYPE_DRAID
"%llu-%llu-%llu",
1493 (u_longlong_t
*)&parity
, (u_longlong_t
*)&vdev_id
,
1494 (u_longlong_t
*)&spare_id
) == 3) {
1502 * Create the named pool, using the provided vdev list. It is assumed
1503 * that the consumer has already validated the contents of the nvlist, so we
1504 * don't have to worry about error semantics.
1507 zpool_create(libzfs_handle_t
*hdl
, const char *pool
, nvlist_t
*nvroot
,
1508 nvlist_t
*props
, nvlist_t
*fsprops
)
1510 zfs_cmd_t zc
= {"\0"};
1511 nvlist_t
*zc_fsprops
= NULL
;
1512 nvlist_t
*zc_props
= NULL
;
1513 nvlist_t
*hidden_args
= NULL
;
1514 uint8_t *wkeydata
= NULL
;
1516 char errbuf
[ERRBUFLEN
];
1519 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1520 "cannot create '%s'"), pool
);
1522 if (!zpool_name_valid(hdl
, B_FALSE
, pool
))
1523 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
1525 zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
);
1528 prop_flags_t flags
= { .create
= B_TRUE
, .import
= B_FALSE
};
1530 if ((zc_props
= zpool_valid_proplist(hdl
, pool
, props
,
1531 SPA_VERSION_1
, flags
, errbuf
)) == NULL
) {
1538 const char *zonestr
;
1540 zoned
= ((nvlist_lookup_string(fsprops
,
1541 zfs_prop_to_name(ZFS_PROP_ZONED
), &zonestr
) == 0) &&
1542 strcmp(zonestr
, "on") == 0);
1544 if ((zc_fsprops
= zfs_valid_proplist(hdl
, ZFS_TYPE_FILESYSTEM
,
1545 fsprops
, zoned
, NULL
, NULL
, B_TRUE
, errbuf
)) == NULL
) {
1549 if (nvlist_exists(zc_fsprops
,
1550 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS
)) &&
1551 !zpool_has_special_vdev(nvroot
)) {
1552 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1553 "%s property requires a special vdev"),
1554 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS
));
1555 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1560 (nvlist_alloc(&zc_props
, NV_UNIQUE_NAME
, 0) != 0)) {
1563 if (zfs_crypto_create(hdl
, NULL
, zc_fsprops
, props
, B_TRUE
,
1564 &wkeydata
, &wkeylen
) != 0) {
1565 zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
);
1568 if (nvlist_add_nvlist(zc_props
,
1569 ZPOOL_ROOTFS_PROPS
, zc_fsprops
) != 0) {
1572 if (wkeydata
!= NULL
) {
1573 if (nvlist_alloc(&hidden_args
, NV_UNIQUE_NAME
, 0) != 0)
1576 if (nvlist_add_uint8_array(hidden_args
, "wkeydata",
1577 wkeydata
, wkeylen
) != 0)
1580 if (nvlist_add_nvlist(zc_props
, ZPOOL_HIDDEN_ARGS
,
1587 zcmd_write_src_nvlist(hdl
, &zc
, zc_props
);
1589 (void) strlcpy(zc
.zc_name
, pool
, sizeof (zc
.zc_name
));
1591 if ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_CREATE
, &zc
)) != 0) {
1593 zcmd_free_nvlists(&zc
);
1594 nvlist_free(zc_props
);
1595 nvlist_free(zc_fsprops
);
1596 nvlist_free(hidden_args
);
1597 if (wkeydata
!= NULL
)
1603 * This can happen if the user has specified the same
1604 * device multiple times. We can't reliably detect this
1605 * until we try to add it and see we already have a
1606 * label. This can also happen under if the device is
1607 * part of an active md or lvm device.
1609 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1610 "one or more vdevs refer to the same device, or "
1611 "one of\nthe devices is part of an active md or "
1613 return (zfs_error(hdl
, EZFS_BADDEV
, errbuf
));
1617 * This happens if the record size is smaller or larger
1618 * than the allowed size range, or not a power of 2.
1620 * NOTE: although zfs_valid_proplist is called earlier,
1621 * this case may have slipped through since the
1622 * pool does not exist yet and it is therefore
1623 * impossible to read properties e.g. max blocksize
1626 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1627 "record size invalid"));
1628 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1632 * This occurs when one of the devices is below
1633 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1634 * device was the problem device since there's no
1635 * reliable way to determine device size from userland.
1640 zfs_nicebytes(SPA_MINDEVSIZE
, buf
,
1643 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1644 "one or more devices is less than the "
1645 "minimum size (%s)"), buf
);
1647 return (zfs_error(hdl
, EZFS_BADDEV
, errbuf
));
1650 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1651 "one or more devices is out of space"));
1652 return (zfs_error(hdl
, EZFS_BADDEV
, errbuf
));
1655 if (zpool_has_draid_vdev(nvroot
) &&
1656 zfeature_lookup_name("draid", NULL
) != 0) {
1657 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1658 "dRAID vdevs are unsupported by the "
1660 return (zfs_error(hdl
, EZFS_BADDEV
, errbuf
));
1662 return (zpool_standard_error(hdl
, errno
,
1667 return (zpool_standard_error(hdl
, errno
, errbuf
));
1672 zcmd_free_nvlists(&zc
);
1673 nvlist_free(zc_props
);
1674 nvlist_free(zc_fsprops
);
1675 nvlist_free(hidden_args
);
1676 if (wkeydata
!= NULL
)
1682 * Destroy the given pool. It is up to the caller to ensure that there are no
1683 * datasets left in the pool.
1686 zpool_destroy(zpool_handle_t
*zhp
, const char *log_str
)
1688 zfs_cmd_t zc
= {"\0"};
1689 zfs_handle_t
*zfp
= NULL
;
1690 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1691 char errbuf
[ERRBUFLEN
];
1693 if (zhp
->zpool_state
== POOL_STATE_ACTIVE
&&
1694 (zfp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
1697 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1698 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1700 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_DESTROY
, &zc
) != 0) {
1701 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1702 "cannot destroy '%s'"), zhp
->zpool_name
);
1704 if (errno
== EROFS
) {
1705 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1706 "one or more devices is read only"));
1707 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
1709 (void) zpool_standard_error(hdl
, errno
, errbuf
);
1718 remove_mountpoint(zfp
);
1726 * Create a checkpoint in the given pool.
1729 zpool_checkpoint(zpool_handle_t
*zhp
)
1731 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1732 char errbuf
[ERRBUFLEN
];
1735 error
= lzc_pool_checkpoint(zhp
->zpool_name
);
1737 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1738 "cannot checkpoint '%s'"), zhp
->zpool_name
);
1739 (void) zpool_standard_error(hdl
, error
, errbuf
);
1747 * Discard the checkpoint from the given pool.
1750 zpool_discard_checkpoint(zpool_handle_t
*zhp
)
1752 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1753 char errbuf
[ERRBUFLEN
];
1756 error
= lzc_pool_checkpoint_discard(zhp
->zpool_name
);
1758 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1759 "cannot discard checkpoint in '%s'"), zhp
->zpool_name
);
1760 (void) zpool_standard_error(hdl
, error
, errbuf
);
1768 * Load data type for the given pool.
1771 zpool_prefetch(zpool_handle_t
*zhp
, zpool_prefetch_type_t type
)
1773 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1777 error
= lzc_pool_prefetch(zhp
->zpool_name
, type
);
1779 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1780 "cannot prefetch %s in '%s'"),
1781 type
== ZPOOL_PREFETCH_DDT
? "ddt" : "", zhp
->zpool_name
);
1782 (void) zpool_standard_error(hdl
, error
, msg
);
1790 * Add the given vdevs to the pool. The caller must have already performed the
1791 * necessary verification to ensure that the vdev specification is well-formed.
1794 zpool_add(zpool_handle_t
*zhp
, nvlist_t
*nvroot
, boolean_t check_ashift
)
1796 zfs_cmd_t zc
= {"\0"};
1798 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1799 char errbuf
[ERRBUFLEN
];
1800 nvlist_t
**spares
, **l2cache
;
1801 uint_t nspares
, nl2cache
;
1803 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1804 "cannot add to '%s'"), zhp
->zpool_name
);
1806 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1807 SPA_VERSION_SPARES
&&
1808 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1809 &spares
, &nspares
) == 0) {
1810 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1811 "upgraded to add hot spares"));
1812 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
1815 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1816 SPA_VERSION_L2CACHE
&&
1817 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1818 &l2cache
, &nl2cache
) == 0) {
1819 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1820 "upgraded to add cache devices"));
1821 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
1824 zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
);
1825 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1826 zc
.zc_flags
= check_ashift
;
1828 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_ADD
, &zc
) != 0) {
1832 * This can happen if the user has specified the same
1833 * device multiple times. We can't reliably detect this
1834 * until we try to add it and see we already have a
1837 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1838 "one or more vdevs refer to the same device"));
1839 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
1844 if (zpool_has_draid_vdev(nvroot
) &&
1845 zfeature_lookup_name("draid", NULL
) != 0) {
1846 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1847 "dRAID vdevs are unsupported by the "
1850 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1851 "invalid config; a pool with removing/"
1852 "removed vdevs does not support adding "
1853 "raidz or dRAID vdevs"));
1856 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
1861 * This occurs when one of the devices is below
1862 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1863 * device was the problem device since there's no
1864 * reliable way to determine device size from userland.
1869 zfs_nicebytes(SPA_MINDEVSIZE
, buf
,
1872 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1873 "device is less than the minimum "
1876 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
1880 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1881 "pool must be upgraded to add these vdevs"));
1882 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1886 (void) zpool_standard_error(hdl
, errno
, errbuf
);
1894 zcmd_free_nvlists(&zc
);
1900 * Exports the pool from the system. The caller must ensure that there are no
1901 * mounted datasets in the pool.
1904 zpool_export_common(zpool_handle_t
*zhp
, boolean_t force
, boolean_t hardforce
,
1905 const char *log_str
)
1907 zfs_cmd_t zc
= {"\0"};
1909 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1910 zc
.zc_cookie
= force
;
1911 zc
.zc_guid
= hardforce
;
1912 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1914 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_EXPORT
, &zc
) != 0) {
1917 zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
1918 "use '-f' to override the following errors:\n"
1919 "'%s' has an active shared spare which could be"
1920 " used by other pools once '%s' is exported."),
1921 zhp
->zpool_name
, zhp
->zpool_name
);
1922 return (zfs_error_fmt(zhp
->zpool_hdl
, EZFS_ACTIVE_SPARE
,
1923 dgettext(TEXT_DOMAIN
, "cannot export '%s'"),
1926 return (zpool_standard_error_fmt(zhp
->zpool_hdl
, errno
,
1927 dgettext(TEXT_DOMAIN
, "cannot export '%s'"),
1936 zpool_export(zpool_handle_t
*zhp
, boolean_t force
, const char *log_str
)
1938 return (zpool_export_common(zhp
, force
, B_FALSE
, log_str
));
1942 zpool_export_force(zpool_handle_t
*zhp
, const char *log_str
)
1944 return (zpool_export_common(zhp
, B_TRUE
, B_TRUE
, log_str
));
1948 zpool_rewind_exclaim(libzfs_handle_t
*hdl
, const char *name
, boolean_t dryrun
,
1951 nvlist_t
*nv
= NULL
;
1957 if (!hdl
->libzfs_printerr
|| config
== NULL
)
1960 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1961 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0) {
1965 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1967 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1969 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1970 ctime_r((time_t *)&rewindto
, timestr
) != NULL
) {
1973 (void) printf(dgettext(TEXT_DOMAIN
,
1974 "Would be able to return %s "
1975 "to its state as of %s.\n"),
1978 (void) printf(dgettext(TEXT_DOMAIN
,
1979 "Pool %s returned to its state as of %s.\n"),
1983 (void) printf(dgettext(TEXT_DOMAIN
,
1984 "%s approximately %lld "),
1985 dryrun
? "Would discard" : "Discarded",
1986 ((longlong_t
)loss
+ 30) / 60);
1987 (void) printf(dgettext(TEXT_DOMAIN
,
1988 "minutes of transactions.\n"));
1989 } else if (loss
> 0) {
1990 (void) printf(dgettext(TEXT_DOMAIN
,
1991 "%s approximately %lld "),
1992 dryrun
? "Would discard" : "Discarded",
1994 (void) printf(dgettext(TEXT_DOMAIN
,
1995 "seconds of transactions.\n"));
2001 zpool_explain_recover(libzfs_handle_t
*hdl
, const char *name
, int reason
,
2002 nvlist_t
*config
, char *buf
, size_t size
)
2004 nvlist_t
*nv
= NULL
;
2006 uint64_t edata
= UINT64_MAX
;
2009 char timestr
[128], temp
[1024];
2011 if (!hdl
->libzfs_printerr
)
2014 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
2015 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
2016 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0 ||
2017 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
2020 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
2021 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_DATA_ERRORS
,
2024 (void) snprintf(buf
, size
, dgettext(TEXT_DOMAIN
,
2025 "Recovery is possible, but will result in some data loss.\n"));
2027 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
2028 ctime_r((time_t *)&rewindto
, timestr
) != NULL
) {
2030 (void) snprintf(temp
, 1024, dgettext(TEXT_DOMAIN
,
2031 "\tReturning the pool to its state as of %s\n"
2032 "\tshould correct the problem. "), timestr
);
2033 (void) strlcat(buf
, temp
, size
);
2035 (void) strlcat(buf
, dgettext(TEXT_DOMAIN
,
2036 "\tReverting the pool to an earlier state "
2037 "should correct the problem.\n\t"), size
);
2041 (void) snprintf(temp
, 1024, dgettext(TEXT_DOMAIN
,
2042 "Approximately %lld minutes of data\n"
2043 "\tmust be discarded, irreversibly. "),
2044 ((longlong_t
)loss
+ 30) / 60);
2045 (void) strlcat(buf
, temp
, size
);
2046 } else if (loss
> 0) {
2047 (void) snprintf(temp
, 1024, dgettext(TEXT_DOMAIN
,
2048 "Approximately %lld seconds of data\n"
2049 "\tmust be discarded, irreversibly. "),
2051 (void) strlcat(buf
, temp
, size
);
2053 if (edata
!= 0 && edata
!= UINT64_MAX
) {
2055 (void) strlcat(buf
, dgettext(TEXT_DOMAIN
,
2056 "After rewind, at least\n"
2057 "\tone persistent user-data error will remain. "),
2060 (void) strlcat(buf
, dgettext(TEXT_DOMAIN
,
2061 "After rewind, several\n"
2062 "\tpersistent user-data errors will remain. "),
2066 (void) snprintf(temp
, 1024, dgettext(TEXT_DOMAIN
,
2067 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
2068 reason
>= 0 ? "clear" : "import", name
);
2069 (void) strlcat(buf
, temp
, size
);
2071 (void) strlcat(buf
, dgettext(TEXT_DOMAIN
,
2072 "A scrub of the pool\n"
2073 "\tis strongly recommended after recovery.\n"), size
);
2077 (void) strlcat(buf
, dgettext(TEXT_DOMAIN
,
2078 "Destroy and re-create the pool from\n\ta backup source.\n"), size
);
2082 * zpool_import() is a contracted interface. Should be kept the same
2085 * Applications should use zpool_import_props() to import a pool with
2086 * new properties value to be set.
2089 zpool_import(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
2092 nvlist_t
*props
= NULL
;
2095 if (altroot
!= NULL
) {
2096 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
2097 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
2098 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
2102 if (nvlist_add_string(props
,
2103 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), altroot
) != 0 ||
2104 nvlist_add_string(props
,
2105 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
), "none") != 0) {
2107 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
2108 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
2113 ret
= zpool_import_props(hdl
, config
, newname
, props
,
2120 print_vdev_tree(libzfs_handle_t
*hdl
, const char *name
, nvlist_t
*nv
,
2126 uint64_t is_log
= 0;
2128 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
,
2132 (void) printf("\t%*s%s%s\n", indent
, "", name
,
2133 is_log
? " [log]" : "");
2135 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2136 &child
, &children
) != 0)
2139 for (c
= 0; c
< children
; c
++) {
2140 vname
= zpool_vdev_name(hdl
, NULL
, child
[c
], VDEV_NAME_TYPE_ID
);
2141 print_vdev_tree(hdl
, vname
, child
[c
], indent
+ 2);
2147 zpool_collect_unsup_feat(nvlist_t
*config
, char *buf
, size_t size
)
2149 nvlist_t
*nvinfo
, *unsup_feat
;
2152 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2153 unsup_feat
= fnvlist_lookup_nvlist(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
);
2155 for (nvpair_t
*nvp
= nvlist_next_nvpair(unsup_feat
, NULL
);
2156 nvp
!= NULL
; nvp
= nvlist_next_nvpair(unsup_feat
, nvp
)) {
2157 const char *desc
= fnvpair_value_string(nvp
);
2158 if (strlen(desc
) > 0) {
2159 (void) snprintf(temp
, 512, "\t%s (%s)\n",
2160 nvpair_name(nvp
), desc
);
2161 (void) strlcat(buf
, temp
, size
);
2163 (void) snprintf(temp
, 512, "\t%s\n", nvpair_name(nvp
));
2164 (void) strlcat(buf
, temp
, size
);
2170 * Import the given pool using the known configuration and a list of
2171 * properties to be set. The configuration should have come from
2172 * zpool_find_import(). The 'newname' parameters control whether the pool
2173 * is imported with a different name.
2176 zpool_import_props(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
2177 nvlist_t
*props
, int flags
)
2179 zfs_cmd_t zc
= {"\0"};
2180 zpool_load_policy_t policy
;
2181 nvlist_t
*nv
= NULL
;
2182 nvlist_t
*nvinfo
= NULL
;
2183 nvlist_t
*missing
= NULL
;
2184 const char *thename
;
2185 const char *origname
;
2189 char errbuf
[ERRBUFLEN
];
2191 origname
= fnvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
);
2193 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2194 "cannot import pool '%s'"), origname
);
2196 if (newname
!= NULL
) {
2197 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
2198 return (zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
2199 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
2206 if (props
!= NULL
) {
2208 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
2210 version
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
2212 if ((props
= zpool_valid_proplist(hdl
, origname
,
2213 props
, version
, flags
, errbuf
)) == NULL
)
2215 zcmd_write_src_nvlist(hdl
, &zc
, props
);
2219 (void) strlcpy(zc
.zc_name
, thename
, sizeof (zc
.zc_name
));
2221 zc
.zc_guid
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
);
2223 zcmd_write_conf_nvlist(hdl
, &zc
, config
);
2224 zcmd_alloc_dst_nvlist(hdl
, &zc
, zc
.zc_nvlist_conf_size
* 2);
2226 zc
.zc_cookie
= flags
;
2227 while ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_IMPORT
, &zc
)) != 0 &&
2229 zcmd_expand_dst_nvlist(hdl
, &zc
);
2233 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nv
);
2235 zcmd_free_nvlists(&zc
);
2237 zpool_get_load_policy(config
, &policy
);
2244 * Dry-run failed, but we print out what success
2245 * looks like if we found a best txg
2247 if (policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) {
2248 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
2254 if (newname
== NULL
)
2255 (void) snprintf(desc
, sizeof (desc
),
2256 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
2259 (void) snprintf(desc
, sizeof (desc
),
2260 dgettext(TEXT_DOMAIN
, "cannot import '%s' as '%s'"),
2265 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
2266 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
2267 nvlist_exists(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
)) {
2268 (void) printf(dgettext(TEXT_DOMAIN
, "This "
2269 "pool uses the following feature(s) not "
2270 "supported by this system:\n"));
2271 memset(buf
, 0, 2048);
2272 zpool_collect_unsup_feat(nv
, buf
, 2048);
2273 (void) printf("%s", buf
);
2274 if (nvlist_exists(nvinfo
,
2275 ZPOOL_CONFIG_CAN_RDONLY
)) {
2276 (void) printf(dgettext(TEXT_DOMAIN
,
2277 "All unsupported features are only "
2278 "required for writing to the pool."
2279 "\nThe pool can be imported using "
2280 "'-o readonly=on'.\n"));
2284 * Unsupported version.
2286 (void) zfs_error(hdl
, EZFS_BADVERSION
, desc
);
2290 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
2291 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0) {
2292 const char *hostname
= "<unknown>";
2293 uint64_t hostid
= 0;
2294 mmp_state_t mmp_state
;
2296 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
2297 ZPOOL_CONFIG_MMP_STATE
);
2299 if (nvlist_exists(nvinfo
,
2300 ZPOOL_CONFIG_MMP_HOSTNAME
))
2301 hostname
= fnvlist_lookup_string(nvinfo
,
2302 ZPOOL_CONFIG_MMP_HOSTNAME
);
2304 if (nvlist_exists(nvinfo
,
2305 ZPOOL_CONFIG_MMP_HOSTID
))
2306 hostid
= fnvlist_lookup_uint64(nvinfo
,
2307 ZPOOL_CONFIG_MMP_HOSTID
);
2309 if (mmp_state
== MMP_STATE_ACTIVE
) {
2310 (void) snprintf(aux
, sizeof (aux
),
2311 dgettext(TEXT_DOMAIN
, "pool is imp"
2312 "orted on host '%s' (hostid=%lx).\n"
2313 "Export the pool on the other "
2314 "system, then run 'zpool import'."),
2315 hostname
, (unsigned long) hostid
);
2316 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
2317 (void) snprintf(aux
, sizeof (aux
),
2318 dgettext(TEXT_DOMAIN
, "pool has "
2319 "the multihost property on and "
2320 "the\nsystem's hostid is not set. "
2321 "Set a unique system hostid with "
2322 "the zgenhostid(8) command.\n"));
2325 (void) zfs_error_aux(hdl
, "%s", aux
);
2327 (void) zfs_error(hdl
, EZFS_ACTIVE_POOL
, desc
);
2331 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, desc
);
2335 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2336 "one or more devices is read only"));
2337 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
2341 if (nv
&& nvlist_lookup_nvlist(nv
,
2342 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
2343 nvlist_lookup_nvlist(nvinfo
,
2344 ZPOOL_CONFIG_MISSING_DEVICES
, &missing
) == 0) {
2345 (void) printf(dgettext(TEXT_DOMAIN
,
2346 "The devices below are missing or "
2347 "corrupted, use '-m' to import the pool "
2349 print_vdev_tree(hdl
, NULL
, missing
, 2);
2350 (void) printf("\n");
2352 (void) zpool_standard_error(hdl
, error
, desc
);
2356 (void) zpool_standard_error(hdl
, error
, desc
);
2360 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2361 "one or more devices are already in use\n"));
2362 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
2365 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2366 "new name of at least one dataset is longer than "
2367 "the maximum allowable length"));
2368 (void) zfs_error(hdl
, EZFS_NAMETOOLONG
, desc
);
2371 (void) zpool_standard_error(hdl
, error
, desc
);
2372 memset(buf
, 0, 2048);
2373 zpool_explain_recover(hdl
,
2374 newname
? origname
: thename
, -error
, nv
,
2376 (void) printf("\t%s", buf
);
2383 zpool_handle_t
*zhp
;
2386 * This should never fail, but play it safe anyway.
2388 if (zpool_open_silent(hdl
, thename
, &zhp
) != 0)
2390 else if (zhp
!= NULL
)
2392 if (policy
.zlp_rewind
&
2393 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
2394 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
2395 ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) != 0), nv
);
2404 * Translate vdev names to guids. If a vdev_path is determined to be
2405 * unsuitable then a vd_errlist is allocated and the vdev path and errno
2409 zpool_translate_vdev_guids(zpool_handle_t
*zhp
, nvlist_t
*vds
,
2410 nvlist_t
*vdev_guids
, nvlist_t
*guids_to_paths
, nvlist_t
**vd_errlist
)
2412 nvlist_t
*errlist
= NULL
;
2415 for (nvpair_t
*elem
= nvlist_next_nvpair(vds
, NULL
); elem
!= NULL
;
2416 elem
= nvlist_next_nvpair(vds
, elem
)) {
2417 boolean_t spare
, cache
;
2419 const char *vd_path
= nvpair_name(elem
);
2420 nvlist_t
*tgt
= zpool_find_vdev(zhp
, vd_path
, &spare
, &cache
,
2423 if ((tgt
== NULL
) || cache
|| spare
) {
2424 if (errlist
== NULL
) {
2425 errlist
= fnvlist_alloc();
2429 uint64_t err
= (tgt
== NULL
) ? EZFS_NODEVICE
:
2430 (spare
? EZFS_ISSPARE
: EZFS_ISL2CACHE
);
2431 fnvlist_add_int64(errlist
, vd_path
, err
);
2435 uint64_t guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
2436 fnvlist_add_uint64(vdev_guids
, vd_path
, guid
);
2438 char msg
[MAXNAMELEN
];
2439 (void) snprintf(msg
, sizeof (msg
), "%llu", (u_longlong_t
)guid
);
2440 fnvlist_add_string(guids_to_paths
, msg
, vd_path
);
2444 verify(errlist
!= NULL
);
2445 if (vd_errlist
!= NULL
)
2446 *vd_errlist
= errlist
;
2448 fnvlist_free(errlist
);
2455 xlate_init_err(int err
)
2459 return (EZFS_NODEVICE
);
2462 return (EZFS_BADDEV
);
2464 return (EZFS_INITIALIZING
);
2466 return (EZFS_NO_INITIALIZE
);
2472 * Begin, suspend, cancel, or uninit (clear) the initialization (initializing
2473 * of all free blocks) for the given vdevs in the given pool.
2476 zpool_initialize_impl(zpool_handle_t
*zhp
, pool_initialize_func_t cmd_type
,
2477 nvlist_t
*vds
, boolean_t wait
)
2481 nvlist_t
*vdev_guids
= fnvlist_alloc();
2482 nvlist_t
*guids_to_paths
= fnvlist_alloc();
2483 nvlist_t
*vd_errlist
= NULL
;
2487 err
= zpool_translate_vdev_guids(zhp
, vds
, vdev_guids
,
2488 guids_to_paths
, &vd_errlist
);
2491 verify(vd_errlist
!= NULL
);
2495 err
= lzc_initialize(zhp
->zpool_name
, cmd_type
,
2496 vdev_guids
, &errlist
);
2499 if (errlist
!= NULL
&& nvlist_lookup_nvlist(errlist
,
2500 ZPOOL_INITIALIZE_VDEVS
, &vd_errlist
) == 0) {
2504 if (err
== EINVAL
&& cmd_type
== POOL_INITIALIZE_UNINIT
) {
2505 zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
2506 "uninitialize is not supported by kernel"));
2509 (void) zpool_standard_error(zhp
->zpool_hdl
, err
,
2510 dgettext(TEXT_DOMAIN
, "operation failed"));
2515 for (elem
= nvlist_next_nvpair(vdev_guids
, NULL
); elem
!= NULL
;
2516 elem
= nvlist_next_nvpair(vdev_guids
, elem
)) {
2518 uint64_t guid
= fnvpair_value_uint64(elem
);
2520 err
= lzc_wait_tag(zhp
->zpool_name
,
2521 ZPOOL_WAIT_INITIALIZE
, guid
, NULL
);
2523 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
,
2524 err
, dgettext(TEXT_DOMAIN
, "error "
2525 "waiting for '%s' to initialize"),
2535 for (elem
= nvlist_next_nvpair(vd_errlist
, NULL
); elem
!= NULL
;
2536 elem
= nvlist_next_nvpair(vd_errlist
, elem
)) {
2537 int64_t vd_error
= xlate_init_err(fnvpair_value_int64(elem
));
2540 if (nvlist_lookup_string(guids_to_paths
, nvpair_name(elem
),
2542 path
= nvpair_name(elem
);
2544 (void) zfs_error_fmt(zhp
->zpool_hdl
, vd_error
,
2545 "cannot initialize '%s'", path
);
2549 fnvlist_free(vdev_guids
);
2550 fnvlist_free(guids_to_paths
);
2552 if (vd_errlist
!= NULL
)
2553 fnvlist_free(vd_errlist
);
2555 return (err
== 0 ? 0 : -1);
2559 zpool_initialize(zpool_handle_t
*zhp
, pool_initialize_func_t cmd_type
,
2562 return (zpool_initialize_impl(zhp
, cmd_type
, vds
, B_FALSE
));
2566 zpool_initialize_wait(zpool_handle_t
*zhp
, pool_initialize_func_t cmd_type
,
2569 return (zpool_initialize_impl(zhp
, cmd_type
, vds
, B_TRUE
));
2573 xlate_trim_err(int err
)
2577 return (EZFS_NODEVICE
);
2580 return (EZFS_BADDEV
);
2582 return (EZFS_TRIMMING
);
2584 return (EZFS_NO_TRIM
);
2586 return (EZFS_TRIM_NOTSUP
);
2592 zpool_trim_wait(zpool_handle_t
*zhp
, nvlist_t
*vdev_guids
)
2597 for (elem
= nvlist_next_nvpair(vdev_guids
, NULL
); elem
!= NULL
;
2598 elem
= nvlist_next_nvpair(vdev_guids
, elem
)) {
2600 uint64_t guid
= fnvpair_value_uint64(elem
);
2602 err
= lzc_wait_tag(zhp
->zpool_name
,
2603 ZPOOL_WAIT_TRIM
, guid
, NULL
);
2605 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
,
2606 err
, dgettext(TEXT_DOMAIN
, "error "
2607 "waiting to trim '%s'"), nvpair_name(elem
));
2616 * Check errlist and report any errors, omitting ones which should be
2617 * suppressed. Returns B_TRUE if any errors were reported.
2620 check_trim_errs(zpool_handle_t
*zhp
, trimflags_t
*trim_flags
,
2621 nvlist_t
*guids_to_paths
, nvlist_t
*vds
, nvlist_t
*errlist
)
2624 boolean_t reported_errs
= B_FALSE
;
2626 int num_suppressed_errs
= 0;
2628 for (elem
= nvlist_next_nvpair(vds
, NULL
);
2629 elem
!= NULL
; elem
= nvlist_next_nvpair(vds
, elem
)) {
2633 for (elem
= nvlist_next_nvpair(errlist
, NULL
);
2634 elem
!= NULL
; elem
= nvlist_next_nvpair(errlist
, elem
)) {
2635 int64_t vd_error
= xlate_trim_err(fnvpair_value_int64(elem
));
2639 * If only the pool was specified, and it was not a secure
2640 * trim then suppress warnings for individual vdevs which
2641 * do not support trimming.
2643 if (vd_error
== EZFS_TRIM_NOTSUP
&&
2644 trim_flags
->fullpool
&&
2645 !trim_flags
->secure
) {
2646 num_suppressed_errs
++;
2650 reported_errs
= B_TRUE
;
2651 if (nvlist_lookup_string(guids_to_paths
, nvpair_name(elem
),
2653 path
= nvpair_name(elem
);
2655 (void) zfs_error_fmt(zhp
->zpool_hdl
, vd_error
,
2656 "cannot trim '%s'", path
);
2659 if (num_suppressed_errs
== num_vds
) {
2660 (void) zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
2661 "no devices in pool support trim operations"));
2662 (void) (zfs_error(zhp
->zpool_hdl
, EZFS_TRIM_NOTSUP
,
2663 dgettext(TEXT_DOMAIN
, "cannot trim")));
2664 reported_errs
= B_TRUE
;
2667 return (reported_errs
);
2671 * Begin, suspend, or cancel the TRIM (discarding of all free blocks) for
2672 * the given vdevs in the given pool.
2675 zpool_trim(zpool_handle_t
*zhp
, pool_trim_func_t cmd_type
, nvlist_t
*vds
,
2676 trimflags_t
*trim_flags
)
2681 nvlist_t
*vdev_guids
= fnvlist_alloc();
2682 nvlist_t
*guids_to_paths
= fnvlist_alloc();
2683 nvlist_t
*errlist
= NULL
;
2685 err
= zpool_translate_vdev_guids(zhp
, vds
, vdev_guids
,
2686 guids_to_paths
, &errlist
);
2688 check_trim_errs(zhp
, trim_flags
, guids_to_paths
, vds
, errlist
);
2693 err
= lzc_trim(zhp
->zpool_name
, cmd_type
, trim_flags
->rate
,
2694 trim_flags
->secure
, vdev_guids
, &errlist
);
2696 nvlist_t
*vd_errlist
;
2697 if (errlist
!= NULL
&& nvlist_lookup_nvlist(errlist
,
2698 ZPOOL_TRIM_VDEVS
, &vd_errlist
) == 0) {
2699 if (check_trim_errs(zhp
, trim_flags
, guids_to_paths
,
2705 char errbuf
[ERRBUFLEN
];
2707 (void) snprintf(errbuf
, sizeof (errbuf
),
2708 dgettext(TEXT_DOMAIN
, "operation failed"));
2709 zpool_standard_error(zhp
->zpool_hdl
, err
, errbuf
);
2716 if (trim_flags
->wait
)
2717 retval
= zpool_trim_wait(zhp
, vdev_guids
);
2720 if (errlist
!= NULL
)
2721 fnvlist_free(errlist
);
2722 fnvlist_free(vdev_guids
);
2723 fnvlist_free(guids_to_paths
);
2731 zpool_scan(zpool_handle_t
*zhp
, pool_scan_func_t func
, pool_scrub_cmd_t cmd
)
2733 char errbuf
[ERRBUFLEN
];
2735 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2737 nvlist_t
*args
= fnvlist_alloc();
2738 fnvlist_add_uint64(args
, "scan_type", (uint64_t)func
);
2739 fnvlist_add_uint64(args
, "scan_command", (uint64_t)cmd
);
2741 err
= lzc_scrub(ZFS_IOC_POOL_SCRUB
, zhp
->zpool_name
, args
, NULL
);
2746 } else if (err
== ZFS_ERR_IOC_CMD_UNAVAIL
) {
2747 zfs_cmd_t zc
= {"\0"};
2748 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
,
2749 sizeof (zc
.zc_name
));
2750 zc
.zc_cookie
= func
;
2753 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_SCAN
, &zc
) == 0)
2758 * An ECANCELED on a scrub means one of the following:
2759 * 1. we resumed a paused scrub.
2760 * 2. we resumed a paused error scrub.
2761 * 3. Error scrub is not run because of no error log.
2763 if (err
== ECANCELED
&& (func
== POOL_SCAN_SCRUB
||
2764 func
== POOL_SCAN_ERRORSCRUB
) && cmd
== POOL_SCRUB_NORMAL
)
2767 * The following cases have been handled here:
2768 * 1. Paused a scrub/error scrub if there is none in progress.
2770 if (err
== ENOENT
&& func
!= POOL_SCAN_NONE
&& cmd
==
2775 ASSERT3U(func
, >=, POOL_SCAN_NONE
);
2776 ASSERT3U(func
, <, POOL_SCAN_FUNCS
);
2778 if (func
== POOL_SCAN_SCRUB
|| func
== POOL_SCAN_ERRORSCRUB
) {
2779 if (cmd
== POOL_SCRUB_PAUSE
) {
2780 (void) snprintf(errbuf
, sizeof (errbuf
),
2781 dgettext(TEXT_DOMAIN
, "cannot pause scrubbing %s"),
2784 assert(cmd
== POOL_SCRUB_NORMAL
);
2785 (void) snprintf(errbuf
, sizeof (errbuf
),
2786 dgettext(TEXT_DOMAIN
, "cannot scrub %s"),
2789 } else if (func
== POOL_SCAN_RESILVER
) {
2790 assert(cmd
== POOL_SCRUB_NORMAL
);
2791 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2792 "cannot restart resilver on %s"), zhp
->zpool_name
);
2793 } else if (func
== POOL_SCAN_NONE
) {
2794 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2795 "cannot cancel scrubbing %s"), zhp
->zpool_name
);
2797 assert(!"unexpected result");
2801 * With EBUSY, six cases are possible:
2803 * Current state Requested
2804 * 1. Normal Scrub Running Normal Scrub or Error Scrub
2805 * 2. Normal Scrub Paused Error Scrub
2806 * 3. Normal Scrub Paused Pause Normal Scrub
2807 * 4. Error Scrub Running Normal Scrub or Error Scrub
2808 * 5. Error Scrub Paused Pause Error Scrub
2809 * 6. Resilvering Anything else
2813 pool_scan_stat_t
*ps
= NULL
;
2816 nvroot
= fnvlist_lookup_nvlist(zhp
->zpool_config
,
2817 ZPOOL_CONFIG_VDEV_TREE
);
2818 (void) nvlist_lookup_uint64_array(nvroot
,
2819 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &psc
);
2820 if (ps
&& ps
->pss_func
== POOL_SCAN_SCRUB
&&
2821 ps
->pss_state
== DSS_SCANNING
) {
2822 if (ps
->pss_pass_scrub_pause
== 0) {
2823 /* handles case 1 */
2824 assert(cmd
== POOL_SCRUB_NORMAL
);
2825 return (zfs_error(hdl
, EZFS_SCRUBBING
,
2828 if (func
== POOL_SCAN_ERRORSCRUB
) {
2829 /* handles case 2 */
2830 ASSERT3U(cmd
, ==, POOL_SCRUB_NORMAL
);
2831 return (zfs_error(hdl
,
2832 EZFS_SCRUB_PAUSED_TO_CANCEL
,
2835 /* handles case 3 */
2836 ASSERT3U(func
, ==, POOL_SCAN_SCRUB
);
2837 ASSERT3U(cmd
, ==, POOL_SCRUB_PAUSE
);
2838 return (zfs_error(hdl
,
2839 EZFS_SCRUB_PAUSED
, errbuf
));
2843 ps
->pss_error_scrub_func
== POOL_SCAN_ERRORSCRUB
&&
2844 ps
->pss_error_scrub_state
== DSS_ERRORSCRUBBING
) {
2845 if (ps
->pss_pass_error_scrub_pause
== 0) {
2846 /* handles case 4 */
2847 ASSERT3U(cmd
, ==, POOL_SCRUB_NORMAL
);
2848 return (zfs_error(hdl
, EZFS_ERRORSCRUBBING
,
2851 /* handles case 5 */
2852 ASSERT3U(func
, ==, POOL_SCAN_ERRORSCRUB
);
2853 ASSERT3U(cmd
, ==, POOL_SCRUB_PAUSE
);
2854 return (zfs_error(hdl
, EZFS_ERRORSCRUB_PAUSED
,
2858 /* handles case 6 */
2859 return (zfs_error(hdl
, EZFS_RESILVERING
, errbuf
));
2861 } else if (err
== ENOENT
) {
2862 return (zfs_error(hdl
, EZFS_NO_SCRUB
, errbuf
));
2863 } else if (err
== ENOTSUP
&& func
== POOL_SCAN_RESILVER
) {
2864 return (zfs_error(hdl
, EZFS_NO_RESILVER_DEFER
, errbuf
));
2866 return (zpool_standard_error(hdl
, err
, errbuf
));
2871 * Find a vdev that matches the search criteria specified. We use the
2872 * the nvpair name to determine how we should look for the device.
2873 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2874 * spare; but FALSE if its an INUSE spare.
2876 * If 'return_parent' is set, then return the *parent* of the vdev you're
2877 * searching for rather than the vdev itself.
2880 vdev_to_nvlist_iter(nvlist_t
*nv
, nvlist_t
*search
, boolean_t
*avail_spare
,
2881 boolean_t
*l2cache
, boolean_t
*log
, boolean_t return_parent
)
2887 const char *srchkey
;
2888 nvpair_t
*pair
= nvlist_next_nvpair(search
, NULL
);
2889 const char *tmp
= NULL
;
2892 /* Nothing to look for */
2893 if (search
== NULL
|| pair
== NULL
)
2896 /* Obtain the key we will use to search */
2897 srchkey
= nvpair_name(pair
);
2899 nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &tmp
);
2900 if (strcmp(tmp
, "root") == 0)
2905 switch (nvpair_type(pair
)) {
2906 case DATA_TYPE_UINT64
:
2907 if (strcmp(srchkey
, ZPOOL_CONFIG_GUID
) == 0) {
2908 uint64_t srchval
= fnvpair_value_uint64(pair
);
2909 uint64_t theguid
= fnvlist_lookup_uint64(nv
,
2911 if (theguid
== srchval
)
2916 case DATA_TYPE_STRING
: {
2917 const char *srchval
, *val
;
2919 srchval
= fnvpair_value_string(pair
);
2920 if (nvlist_lookup_string(nv
, srchkey
, &val
) != 0)
2924 * Search for the requested value. Special cases:
2926 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2927 * "-part1", or "p1". The suffix is hidden from the user,
2928 * but included in the string, so this matches around it.
2929 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2930 * is used to check all possible expanded paths.
2931 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2933 * Otherwise, all other searches are simple string compares.
2935 if (strcmp(srchkey
, ZPOOL_CONFIG_PATH
) == 0) {
2936 uint64_t wholedisk
= 0;
2938 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
2940 if (zfs_strcmp_pathname(srchval
, val
, wholedisk
) == 0)
2943 } else if (strcmp(srchkey
, ZPOOL_CONFIG_TYPE
) == 0) {
2944 char *type
, *idx
, *end
, *p
;
2945 uint64_t id
, vdev_id
;
2948 * Determine our vdev type, keeping in mind
2949 * that the srchval is composed of a type and
2950 * vdev id pair (i.e. mirror-4).
2952 if ((type
= strdup(srchval
)) == NULL
)
2955 if ((p
= strrchr(type
, '-')) == NULL
) {
2963 * If the types don't match then keep looking.
2965 if (strncmp(val
, type
, strlen(val
)) != 0) {
2970 verify(zpool_vdev_is_interior(type
));
2972 id
= fnvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
);
2974 vdev_id
= strtoull(idx
, &end
, 10);
2977 * If we are looking for a raidz and a parity is
2978 * specified, make sure it matches.
2980 int rzlen
= strlen(VDEV_TYPE_RAIDZ
);
2981 assert(rzlen
== strlen(VDEV_TYPE_DRAID
));
2982 int typlen
= strlen(type
);
2983 if ((strncmp(type
, VDEV_TYPE_RAIDZ
, rzlen
) == 0 ||
2984 strncmp(type
, VDEV_TYPE_DRAID
, rzlen
) == 0) &&
2986 uint64_t vdev_parity
;
2987 int parity
= *(type
+ rzlen
) - '0';
2989 if (parity
<= 0 || parity
> 3 ||
2990 (typlen
- rzlen
) != 1) {
2992 * Nonsense parity specified, can
2998 vdev_parity
= fnvlist_lookup_uint64(nv
,
2999 ZPOOL_CONFIG_NPARITY
);
3000 if ((int)vdev_parity
!= parity
) {
3011 * Now verify that we have the correct vdev id.
3020 if (strcmp(srchval
, val
) == 0)
3029 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
3030 &child
, &children
) != 0)
3033 for (c
= 0; c
< children
; c
++) {
3034 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
3035 avail_spare
, l2cache
, NULL
, return_parent
)) != NULL
) {
3037 * The 'is_log' value is only set for the toplevel
3038 * vdev, not the leaf vdevs. So we always lookup the
3039 * log device from the root of the vdev tree (where
3040 * 'log' is non-NULL).
3043 nvlist_lookup_uint64(child
[c
],
3044 ZPOOL_CONFIG_IS_LOG
, &is_log
) == 0 &&
3048 return (ret
&& return_parent
&& !is_root
? nv
: ret
);
3052 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
3053 &child
, &children
) == 0) {
3054 for (c
= 0; c
< children
; c
++) {
3055 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
3056 avail_spare
, l2cache
, NULL
, return_parent
))
3058 *avail_spare
= B_TRUE
;
3059 return (ret
&& return_parent
&&
3060 !is_root
? nv
: ret
);
3065 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
3066 &child
, &children
) == 0) {
3067 for (c
= 0; c
< children
; c
++) {
3068 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
3069 avail_spare
, l2cache
, NULL
, return_parent
))
3072 return (ret
&& return_parent
&&
3073 !is_root
? nv
: ret
);
3082 * Given a physical path or guid, find the associated vdev.
3085 zpool_find_vdev_by_physpath(zpool_handle_t
*zhp
, const char *ppath
,
3086 boolean_t
*avail_spare
, boolean_t
*l2cache
, boolean_t
*log
)
3088 nvlist_t
*search
, *nvroot
, *ret
;
3092 search
= fnvlist_alloc();
3094 guid
= strtoull(ppath
, &end
, 0);
3095 if (guid
!= 0 && *end
== '\0') {
3096 fnvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
);
3098 fnvlist_add_string(search
, ZPOOL_CONFIG_PHYS_PATH
, ppath
);
3101 nvroot
= fnvlist_lookup_nvlist(zhp
->zpool_config
,
3102 ZPOOL_CONFIG_VDEV_TREE
);
3104 *avail_spare
= B_FALSE
;
3108 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
,
3110 fnvlist_free(search
);
3116 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
3119 zpool_vdev_is_interior(const char *name
)
3121 if (strncmp(name
, VDEV_TYPE_RAIDZ
, strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
3122 strncmp(name
, VDEV_TYPE_SPARE
, strlen(VDEV_TYPE_SPARE
)) == 0 ||
3124 VDEV_TYPE_REPLACING
, strlen(VDEV_TYPE_REPLACING
)) == 0 ||
3125 strncmp(name
, VDEV_TYPE_ROOT
, strlen(VDEV_TYPE_ROOT
)) == 0 ||
3126 strncmp(name
, VDEV_TYPE_MIRROR
, strlen(VDEV_TYPE_MIRROR
)) == 0)
3129 if (strncmp(name
, VDEV_TYPE_DRAID
, strlen(VDEV_TYPE_DRAID
)) == 0 &&
3130 !zpool_is_draid_spare(name
))
3137 * Lookup the nvlist for a given vdev or vdev's parent (depending on
3138 * if 'return_parent' is set).
3141 __zpool_find_vdev(zpool_handle_t
*zhp
, const char *path
, boolean_t
*avail_spare
,
3142 boolean_t
*l2cache
, boolean_t
*log
, boolean_t return_parent
)
3145 nvlist_t
*nvroot
, *search
, *ret
;
3147 boolean_t __avail_spare
, __l2cache
, __log
;
3149 search
= fnvlist_alloc();
3151 guid
= strtoull(path
, &end
, 0);
3152 if (guid
!= 0 && *end
== '\0') {
3153 fnvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
);
3154 } else if (zpool_vdev_is_interior(path
)) {
3155 fnvlist_add_string(search
, ZPOOL_CONFIG_TYPE
, path
);
3157 fnvlist_add_string(search
, ZPOOL_CONFIG_PATH
, path
);
3160 nvroot
= fnvlist_lookup_nvlist(zhp
->zpool_config
,
3161 ZPOOL_CONFIG_VDEV_TREE
);
3164 * User can pass NULL for avail_spare, l2cache, and log, but
3165 * we still need to provide variables to vdev_to_nvlist_iter(), so
3166 * just point them to junk variables here.
3169 avail_spare
= &__avail_spare
;
3171 l2cache
= &__l2cache
;
3175 *avail_spare
= B_FALSE
;
3179 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
,
3181 fnvlist_free(search
);
3187 zpool_find_vdev(zpool_handle_t
*zhp
, const char *path
, boolean_t
*avail_spare
,
3188 boolean_t
*l2cache
, boolean_t
*log
)
3190 return (__zpool_find_vdev(zhp
, path
, avail_spare
, l2cache
, log
,
3194 /* Given a vdev path, return its parent's nvlist */
3196 zpool_find_parent_vdev(zpool_handle_t
*zhp
, const char *path
,
3197 boolean_t
*avail_spare
, boolean_t
*l2cache
, boolean_t
*log
)
3199 return (__zpool_find_vdev(zhp
, path
, avail_spare
, l2cache
, log
,
3204 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
3206 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
3207 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
3211 zpool_vdev_path_to_guid_impl(zpool_handle_t
*zhp
, const char *path
,
3212 boolean_t
*is_spare
, boolean_t
*is_l2cache
, boolean_t
*is_log
)
3214 boolean_t spare
= B_FALSE
, l2cache
= B_FALSE
, log
= B_FALSE
;
3217 if ((tgt
= zpool_find_vdev(zhp
, path
, &spare
, &l2cache
,
3221 if (is_spare
!= NULL
)
3223 if (is_l2cache
!= NULL
)
3224 *is_l2cache
= l2cache
;
3228 return (fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
));
3231 /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
3233 zpool_vdev_path_to_guid(zpool_handle_t
*zhp
, const char *path
)
3235 return (zpool_vdev_path_to_guid_impl(zhp
, path
, NULL
, NULL
, NULL
));
3239 * Bring the specified vdev online. The 'flags' parameter is a set of the
3240 * ZFS_ONLINE_* flags.
3243 zpool_vdev_online(zpool_handle_t
*zhp
, const char *path
, int flags
,
3244 vdev_state_t
*newstate
)
3246 zfs_cmd_t zc
= {"\0"};
3247 char errbuf
[ERRBUFLEN
];
3249 boolean_t avail_spare
, l2cache
, islog
;
3250 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3252 if (flags
& ZFS_ONLINE_EXPAND
) {
3253 (void) snprintf(errbuf
, sizeof (errbuf
),
3254 dgettext(TEXT_DOMAIN
, "cannot expand %s"), path
);
3256 (void) snprintf(errbuf
, sizeof (errbuf
),
3257 dgettext(TEXT_DOMAIN
, "cannot online %s"), path
);
3260 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3261 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3263 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3265 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3267 if (!(flags
& ZFS_ONLINE_SPARE
) && avail_spare
)
3268 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
3271 const char *pathname
;
3272 if ((flags
& ZFS_ONLINE_EXPAND
||
3273 zpool_get_prop_int(zhp
, ZPOOL_PROP_AUTOEXPAND
, NULL
)) &&
3274 nvlist_lookup_string(tgt
, ZPOOL_CONFIG_PATH
, &pathname
) == 0) {
3275 uint64_t wholedisk
= 0;
3277 (void) nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_WHOLE_DISK
,
3281 * XXX - L2ARC 1.0 devices can't support expansion.
3284 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3285 "cannot expand cache devices"));
3286 return (zfs_error(hdl
, EZFS_VDEVNOTSUP
, errbuf
));
3290 const char *fullpath
= path
;
3291 char buf
[MAXPATHLEN
];
3294 if (path
[0] != '/') {
3295 error
= zfs_resolve_shortname(path
, buf
,
3298 return (zfs_error(hdl
, EZFS_NODEVICE
,
3304 error
= zpool_relabel_disk(hdl
, fullpath
, errbuf
);
3311 zc
.zc_cookie
= VDEV_STATE_ONLINE
;
3314 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) != 0) {
3315 if (errno
== EINVAL
) {
3316 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "was split "
3317 "from this pool into a new one. Use '%s' "
3318 "instead"), "zpool detach");
3319 return (zfs_error(hdl
, EZFS_POSTSPLIT_ONLINE
, errbuf
));
3321 return (zpool_standard_error(hdl
, errno
, errbuf
));
3324 *newstate
= zc
.zc_cookie
;
3329 * Take the specified vdev offline
3332 zpool_vdev_offline(zpool_handle_t
*zhp
, const char *path
, boolean_t istmp
)
3334 zfs_cmd_t zc
= {"\0"};
3335 char errbuf
[ERRBUFLEN
];
3337 boolean_t avail_spare
, l2cache
;
3338 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3340 (void) snprintf(errbuf
, sizeof (errbuf
),
3341 dgettext(TEXT_DOMAIN
, "cannot offline %s"), path
);
3343 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3344 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3346 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3348 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3351 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
3353 zc
.zc_cookie
= VDEV_STATE_OFFLINE
;
3354 zc
.zc_obj
= istmp
? ZFS_OFFLINE_TEMPORARY
: 0;
3356 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3363 * There are no other replicas of this device.
3365 return (zfs_error(hdl
, EZFS_NOREPLICAS
, errbuf
));
3369 * The log device has unplayed logs
3371 return (zfs_error(hdl
, EZFS_UNPLAYED_LOGS
, errbuf
));
3374 return (zpool_standard_error(hdl
, errno
, errbuf
));
3379 * Remove the specified vdev asynchronously from the configuration, so
3380 * that it may come ONLINE if reinserted. This is called from zed on
3381 * Udev remove event.
3382 * Note: We also have a similar function zpool_vdev_remove() that
3383 * removes the vdev from the pool.
3386 zpool_vdev_remove_wanted(zpool_handle_t
*zhp
, const char *path
)
3388 zfs_cmd_t zc
= {"\0"};
3389 char errbuf
[ERRBUFLEN
];
3391 boolean_t avail_spare
, l2cache
;
3392 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3394 (void) snprintf(errbuf
, sizeof (errbuf
),
3395 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3397 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3398 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3400 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3402 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3404 zc
.zc_cookie
= VDEV_STATE_REMOVED
;
3406 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3409 return (zpool_standard_error(hdl
, errno
, errbuf
));
3413 * Mark the given vdev faulted.
3416 zpool_vdev_fault(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
3418 zfs_cmd_t zc
= {"\0"};
3419 char errbuf
[ERRBUFLEN
];
3420 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3422 (void) snprintf(errbuf
, sizeof (errbuf
),
3423 dgettext(TEXT_DOMAIN
, "cannot fault %llu"), (u_longlong_t
)guid
);
3425 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3427 zc
.zc_cookie
= VDEV_STATE_FAULTED
;
3430 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3437 * There are no other replicas of this device.
3439 return (zfs_error(hdl
, EZFS_NOREPLICAS
, errbuf
));
3442 return (zpool_standard_error(hdl
, errno
, errbuf
));
3448 * Generic set vdev state function
3451 zpool_vdev_set_state(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
,
3454 zfs_cmd_t zc
= {"\0"};
3455 char errbuf
[ERRBUFLEN
];
3456 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3458 (void) snprintf(errbuf
, sizeof (errbuf
),
3459 dgettext(TEXT_DOMAIN
, "cannot set %s %llu"),
3460 zpool_state_to_name(state
, aux
), (u_longlong_t
)guid
);
3462 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3464 zc
.zc_cookie
= state
;
3467 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3470 return (zpool_standard_error(hdl
, errno
, errbuf
));
3474 * Mark the given vdev degraded.
3477 zpool_vdev_degrade(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
3479 return (zpool_vdev_set_state(zhp
, guid
, aux
, VDEV_STATE_DEGRADED
));
3483 * Mark the given vdev as in a removed state (as if the device does not exist).
3485 * This is different than zpool_vdev_remove() which does a removal of a device
3486 * from the pool (but the device does exist).
3489 zpool_vdev_set_removed_state(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
3491 return (zpool_vdev_set_state(zhp
, guid
, aux
, VDEV_STATE_REMOVED
));
3495 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
3499 is_replacing_spare(nvlist_t
*search
, nvlist_t
*tgt
, int which
)
3504 if (nvlist_lookup_nvlist_array(search
, ZPOOL_CONFIG_CHILDREN
, &child
,
3506 const char *type
= fnvlist_lookup_string(search
,
3508 if ((strcmp(type
, VDEV_TYPE_SPARE
) == 0 ||
3509 strcmp(type
, VDEV_TYPE_DRAID_SPARE
) == 0) &&
3510 children
== 2 && child
[which
] == tgt
)
3513 for (c
= 0; c
< children
; c
++)
3514 if (is_replacing_spare(child
[c
], tgt
, which
))
3522 * Attach new_disk (fully described by nvroot) to old_disk.
3523 * If 'replacing' is specified, the new disk will replace the old one.
3526 zpool_vdev_attach(zpool_handle_t
*zhp
, const char *old_disk
,
3527 const char *new_disk
, nvlist_t
*nvroot
, int replacing
, boolean_t rebuild
)
3529 zfs_cmd_t zc
= {"\0"};
3530 char errbuf
[ERRBUFLEN
];
3533 boolean_t avail_spare
, l2cache
, islog
;
3539 nvlist_t
*config_root
;
3540 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3543 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3544 "cannot replace %s with %s"), old_disk
, new_disk
);
3546 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3547 "cannot attach %s to %s"), new_disk
, old_disk
);
3549 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3550 if ((tgt
= zpool_find_vdev(zhp
, old_disk
, &avail_spare
, &l2cache
,
3552 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3555 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
3558 return (zfs_error(hdl
, EZFS_ISL2CACHE
, errbuf
));
3560 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3561 zc
.zc_cookie
= replacing
;
3562 zc
.zc_simple
= rebuild
;
3565 zfeature_lookup_guid("org.openzfs:device_rebuild", NULL
) != 0) {
3566 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3567 "the loaded zfs module doesn't support device rebuilds"));
3568 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, errbuf
));
3571 type
= fnvlist_lookup_string(tgt
, ZPOOL_CONFIG_TYPE
);
3572 if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0 &&
3573 zfeature_lookup_guid("org.openzfs:raidz_expansion", NULL
) != 0) {
3574 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3575 "the loaded zfs module doesn't support raidz expansion"));
3576 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, errbuf
));
3579 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
3580 &child
, &children
) != 0 || children
!= 1) {
3581 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3582 "new device must be a single disk"));
3583 return (zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
));
3586 config_root
= fnvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
3587 ZPOOL_CONFIG_VDEV_TREE
);
3589 if ((newname
= zpool_vdev_name(NULL
, NULL
, child
[0], 0)) == NULL
)
3593 * If the target is a hot spare that has been swapped in, we can only
3594 * replace it with another hot spare.
3597 nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_IS_SPARE
, &val
) == 0 &&
3598 (zpool_find_vdev(zhp
, newname
, &avail_spare
, &l2cache
,
3599 NULL
) == NULL
|| !avail_spare
) &&
3600 is_replacing_spare(config_root
, tgt
, 1)) {
3601 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3602 "can only be replaced by another hot spare"));
3604 return (zfs_error(hdl
, EZFS_BADTARGET
, errbuf
));
3609 zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
);
3611 ret
= zfs_ioctl(hdl
, ZFS_IOC_VDEV_ATTACH
, &zc
);
3613 zcmd_free_nvlists(&zc
);
3621 * Can't attach to or replace this type of vdev.
3624 uint64_t version
= zpool_get_prop_int(zhp
,
3625 ZPOOL_PROP_VERSION
, NULL
);
3628 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3629 "cannot replace a log with a spare"));
3630 } else if (rebuild
) {
3631 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3632 "only mirror and dRAID vdevs support "
3633 "sequential reconstruction"));
3634 } else if (zpool_is_draid_spare(new_disk
)) {
3635 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3636 "dRAID spares can only replace child "
3637 "devices in their parent's dRAID vdev"));
3638 } else if (version
>= SPA_VERSION_MULTI_REPLACE
) {
3639 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3640 "already in replacing/spare config; wait "
3641 "for completion or use 'zpool detach'"));
3643 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3644 "cannot replace a replacing device"));
3646 } else if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0) {
3647 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3648 "raidz_expansion feature must be enabled "
3649 "in order to attach a device to raidz"));
3651 char status
[64] = {0};
3652 zpool_prop_get_feature(zhp
,
3653 "feature@device_rebuild", status
, 63);
3655 strncmp(status
, ZFS_FEATURE_DISABLED
, 64) == 0) {
3656 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3657 "device_rebuild feature must be enabled "
3658 "in order to use sequential "
3661 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3662 "can only attach to mirrors and top-level "
3666 (void) zfs_error(hdl
, EZFS_BADTARGET
, errbuf
);
3671 * The new device must be a single disk.
3673 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3674 "new device must be a single disk"));
3675 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
);
3679 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "%s is busy"),
3681 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
3686 * The new device is too small.
3688 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3689 "device is too small"));
3690 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
3695 * The new device has a different optimal sector size.
3697 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3698 "new device has a different optimal sector size; use the "
3699 "option '-o ashift=N' to override the optimal size"));
3700 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
3705 * The resulting top-level vdev spec won't fit in the label.
3707 (void) zfs_error(hdl
, EZFS_DEVOVERFLOW
, errbuf
);
3712 * The existing raidz vdev has offline children
3714 if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0) {
3715 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3716 "raidz vdev has devices that are are offline or "
3718 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
3721 (void) zpool_standard_error(hdl
, errno
, errbuf
);
3727 * The boot reserved area is already being used (FreeBSD)
3729 if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0) {
3730 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3731 "the reserved boot area needed for the expansion "
3732 "is already being used by a boot loader"));
3733 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
3735 (void) zpool_standard_error(hdl
, errno
, errbuf
);
3739 case ZFS_ERR_ASHIFT_MISMATCH
:
3740 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3741 "The new device cannot have a higher alignment requirement "
3742 "than the top-level vdev."));
3743 (void) zfs_error(hdl
, EZFS_BADTARGET
, errbuf
);
3746 (void) zpool_standard_error(hdl
, errno
, errbuf
);
3753 * Detach the specified device.
3756 zpool_vdev_detach(zpool_handle_t
*zhp
, const char *path
)
3758 zfs_cmd_t zc
= {"\0"};
3759 char errbuf
[ERRBUFLEN
];
3761 boolean_t avail_spare
, l2cache
;
3762 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3764 (void) snprintf(errbuf
, sizeof (errbuf
),
3765 dgettext(TEXT_DOMAIN
, "cannot detach %s"), path
);
3767 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3768 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3770 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3773 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
3776 return (zfs_error(hdl
, EZFS_ISL2CACHE
, errbuf
));
3778 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3780 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_DETACH
, &zc
) == 0)
3787 * Can't detach from this type of vdev.
3789 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "only "
3790 "applicable to mirror and replacing vdevs"));
3791 (void) zfs_error(hdl
, EZFS_BADTARGET
, errbuf
);
3796 * There are no other replicas of this device.
3798 (void) zfs_error(hdl
, EZFS_NOREPLICAS
, errbuf
);
3802 (void) zpool_standard_error(hdl
, errno
, errbuf
);
3809 * Find a mirror vdev in the source nvlist.
3811 * The mchild array contains a list of disks in one of the top-level mirrors
3812 * of the source pool. The schild array contains a list of disks that the
3813 * user specified on the command line. We loop over the mchild array to
3814 * see if any entry in the schild array matches.
3816 * If a disk in the mchild array is found in the schild array, we return
3817 * the index of that entry. Otherwise we return -1.
3820 find_vdev_entry(zpool_handle_t
*zhp
, nvlist_t
**mchild
, uint_t mchildren
,
3821 nvlist_t
**schild
, uint_t schildren
)
3825 for (mc
= 0; mc
< mchildren
; mc
++) {
3827 char *mpath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
3830 for (sc
= 0; sc
< schildren
; sc
++) {
3831 char *spath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
3833 boolean_t result
= (strcmp(mpath
, spath
) == 0);
3849 * Split a mirror pool. If newroot points to null, then a new nvlist
3850 * is generated and it is the responsibility of the caller to free it.
3853 zpool_vdev_split(zpool_handle_t
*zhp
, char *newname
, nvlist_t
**newroot
,
3854 nvlist_t
*props
, splitflags_t flags
)
3856 zfs_cmd_t zc
= {"\0"};
3857 char errbuf
[ERRBUFLEN
];
3859 nvlist_t
*tree
, *config
, **child
, **newchild
, *newconfig
= NULL
;
3860 nvlist_t
**varray
= NULL
, *zc_props
= NULL
;
3861 uint_t c
, children
, newchildren
, lastlog
= 0, vcount
, found
= 0;
3862 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3863 uint64_t vers
, readonly
= B_FALSE
;
3864 boolean_t freelist
= B_FALSE
, memory_err
= B_TRUE
;
3867 (void) snprintf(errbuf
, sizeof (errbuf
),
3868 dgettext(TEXT_DOMAIN
, "Unable to split %s"), zhp
->zpool_name
);
3870 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
3871 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3873 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
3874 (void) fprintf(stderr
, gettext("Internal error: unable to "
3875 "retrieve pool configuration\n"));
3879 tree
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
3880 vers
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
3883 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
3884 if ((zc_props
= zpool_valid_proplist(hdl
, zhp
->zpool_name
,
3885 props
, vers
, flags
, errbuf
)) == NULL
)
3887 (void) nvlist_lookup_uint64(zc_props
,
3888 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
3890 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3891 "property %s can only be set at import time"),
3892 zpool_prop_to_name(ZPOOL_PROP_READONLY
));
3897 if (nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
3899 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3900 "Source pool is missing vdev tree"));
3901 nvlist_free(zc_props
);
3905 varray
= zfs_alloc(hdl
, children
* sizeof (nvlist_t
*));
3908 if (*newroot
== NULL
||
3909 nvlist_lookup_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
3910 &newchild
, &newchildren
) != 0)
3913 for (c
= 0; c
< children
; c
++) {
3914 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
3915 boolean_t is_special
= B_FALSE
, is_dedup
= B_FALSE
;
3917 nvlist_t
**mchild
, *vdev
;
3922 * Unlike cache & spares, slogs are stored in the
3923 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3925 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
3927 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
3929 if (is_log
|| is_hole
) {
3931 * Create a hole vdev and put it in the config.
3933 if (nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) != 0)
3935 if (nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
,
3936 VDEV_TYPE_HOLE
) != 0)
3938 if (nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_HOLE
,
3943 varray
[vcount
++] = vdev
;
3947 type
= fnvlist_lookup_string(child
[c
], ZPOOL_CONFIG_TYPE
);
3949 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0) {
3951 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
3954 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
3955 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3956 "Source pool must be composed only of mirrors\n"));
3957 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
);
3961 if (nvlist_lookup_string(child
[c
],
3962 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
) == 0) {
3963 if (strcmp(bias
, VDEV_ALLOC_BIAS_SPECIAL
) == 0)
3964 is_special
= B_TRUE
;
3965 else if (strcmp(bias
, VDEV_ALLOC_BIAS_DEDUP
) == 0)
3968 verify(nvlist_lookup_nvlist_array(child
[c
],
3969 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
3971 /* find or add an entry for this top-level vdev */
3972 if (newchildren
> 0 &&
3973 (entry
= find_vdev_entry(zhp
, mchild
, mchildren
,
3974 newchild
, newchildren
)) >= 0) {
3975 /* We found a disk that the user specified. */
3976 vdev
= mchild
[entry
];
3979 /* User didn't specify a disk for this vdev. */
3980 vdev
= mchild
[mchildren
- 1];
3983 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
3986 if (flags
.dryrun
!= 0) {
3987 if (is_dedup
== B_TRUE
) {
3988 if (nvlist_add_string(varray
[vcount
- 1],
3989 ZPOOL_CONFIG_ALLOCATION_BIAS
,
3990 VDEV_ALLOC_BIAS_DEDUP
) != 0)
3992 } else if (is_special
== B_TRUE
) {
3993 if (nvlist_add_string(varray
[vcount
- 1],
3994 ZPOOL_CONFIG_ALLOCATION_BIAS
,
3995 VDEV_ALLOC_BIAS_SPECIAL
) != 0)
4001 /* did we find every disk the user specified? */
4002 if (found
!= newchildren
) {
4003 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Device list must "
4004 "include at most one disk from each mirror"));
4005 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
);
4009 /* Prepare the nvlist for populating. */
4010 if (*newroot
== NULL
) {
4011 if (nvlist_alloc(newroot
, NV_UNIQUE_NAME
, 0) != 0)
4014 if (nvlist_add_string(*newroot
, ZPOOL_CONFIG_TYPE
,
4015 VDEV_TYPE_ROOT
) != 0)
4018 verify(nvlist_remove_all(*newroot
, ZPOOL_CONFIG_CHILDREN
) == 0);
4021 /* Add all the children we found */
4022 if (nvlist_add_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
4023 (const nvlist_t
**)varray
, lastlog
== 0 ? vcount
: lastlog
) != 0)
4027 * If we're just doing a dry run, exit now with success.
4030 memory_err
= B_FALSE
;
4035 /* now build up the config list & call the ioctl */
4036 if (nvlist_alloc(&newconfig
, NV_UNIQUE_NAME
, 0) != 0)
4039 if (nvlist_add_nvlist(newconfig
,
4040 ZPOOL_CONFIG_VDEV_TREE
, *newroot
) != 0 ||
4041 nvlist_add_string(newconfig
,
4042 ZPOOL_CONFIG_POOL_NAME
, newname
) != 0 ||
4043 nvlist_add_uint64(newconfig
, ZPOOL_CONFIG_VERSION
, vers
) != 0)
4047 * The new pool is automatically part of the namespace unless we
4048 * explicitly export it.
4051 zc
.zc_cookie
= ZPOOL_EXPORT_AFTER_SPLIT
;
4052 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4053 (void) strlcpy(zc
.zc_string
, newname
, sizeof (zc
.zc_string
));
4054 zcmd_write_conf_nvlist(hdl
, &zc
, newconfig
);
4055 if (zc_props
!= NULL
)
4056 zcmd_write_src_nvlist(hdl
, &zc
, zc_props
);
4058 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SPLIT
, &zc
) != 0) {
4059 retval
= zpool_standard_error(hdl
, errno
, errbuf
);
4064 memory_err
= B_FALSE
;
4067 if (varray
!= NULL
) {
4070 for (v
= 0; v
< vcount
; v
++)
4071 nvlist_free(varray
[v
]);
4074 zcmd_free_nvlists(&zc
);
4075 nvlist_free(zc_props
);
4076 nvlist_free(newconfig
);
4078 nvlist_free(*newroot
);
4086 return (no_memory(hdl
));
4092 * Remove the given device.
4095 zpool_vdev_remove(zpool_handle_t
*zhp
, const char *path
)
4097 zfs_cmd_t zc
= {"\0"};
4098 char errbuf
[ERRBUFLEN
];
4100 boolean_t avail_spare
, l2cache
, islog
;
4101 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4104 (void) snprintf(errbuf
, sizeof (errbuf
),
4105 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
4107 if (zpool_is_draid_spare(path
)) {
4108 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4109 "dRAID spares cannot be removed"));
4110 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
4113 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4114 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
4116 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
4118 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
4119 if (islog
&& version
< SPA_VERSION_HOLES
) {
4120 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4121 "pool must be upgraded to support log removal"));
4122 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
4125 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
4127 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
4133 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4134 "removal for this vdev is already in progress."));
4135 (void) zfs_error(hdl
, EZFS_BUSY
, errbuf
);
4139 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4140 "invalid config; all top-level vdevs must "
4141 "have the same sector size and not be raidz."));
4142 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
);
4147 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4148 "Mount encrypted datasets to replay logs."));
4150 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4151 "Pool busy; removal may already be in progress"));
4153 (void) zfs_error(hdl
, EZFS_BUSY
, errbuf
);
4158 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4159 "Mount encrypted datasets to replay logs."));
4160 (void) zfs_error(hdl
, EZFS_BUSY
, errbuf
);
4162 (void) zpool_standard_error(hdl
, errno
, errbuf
);
4167 (void) zpool_standard_error(hdl
, errno
, errbuf
);
4173 zpool_vdev_remove_cancel(zpool_handle_t
*zhp
)
4175 zfs_cmd_t zc
= {{0}};
4176 char errbuf
[ERRBUFLEN
];
4177 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4179 (void) snprintf(errbuf
, sizeof (errbuf
),
4180 dgettext(TEXT_DOMAIN
, "cannot cancel removal"));
4182 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4185 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
4188 return (zpool_standard_error(hdl
, errno
, errbuf
));
4192 zpool_vdev_indirect_size(zpool_handle_t
*zhp
, const char *path
,
4195 char errbuf
[ERRBUFLEN
];
4197 boolean_t avail_spare
, l2cache
, islog
;
4198 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4200 (void) snprintf(errbuf
, sizeof (errbuf
),
4201 dgettext(TEXT_DOMAIN
, "cannot determine indirect size of %s"),
4204 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
4206 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
4208 if (avail_spare
|| l2cache
|| islog
) {
4213 if (nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_INDIRECT_SIZE
, sizep
) != 0) {
4214 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4215 "indirect size not available"));
4216 return (zfs_error(hdl
, EINVAL
, errbuf
));
4222 * Clear the errors for the pool, or the particular device if specified.
4225 zpool_clear(zpool_handle_t
*zhp
, const char *path
, nvlist_t
*rewindnvl
)
4227 zfs_cmd_t zc
= {"\0"};
4228 char errbuf
[ERRBUFLEN
];
4230 zpool_load_policy_t policy
;
4231 boolean_t avail_spare
, l2cache
;
4232 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4233 nvlist_t
*nvi
= NULL
;
4237 (void) snprintf(errbuf
, sizeof (errbuf
),
4238 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
4241 (void) snprintf(errbuf
, sizeof (errbuf
),
4242 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
4245 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4247 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
,
4248 &l2cache
, NULL
)) == NULL
)
4249 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
4252 * Don't allow error clearing for hot spares. Do allow
4253 * error clearing for l2cache devices.
4256 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
4258 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
4261 zpool_get_load_policy(rewindnvl
, &policy
);
4262 zc
.zc_cookie
= policy
.zlp_rewind
;
4264 zcmd_alloc_dst_nvlist(hdl
, &zc
, zhp
->zpool_config_size
* 2);
4265 zcmd_write_src_nvlist(hdl
, &zc
, rewindnvl
);
4267 while ((error
= zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
)) != 0 &&
4269 zcmd_expand_dst_nvlist(hdl
, &zc
);
4271 if (!error
|| ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) &&
4272 errno
!= EPERM
&& errno
!= EACCES
)) {
4273 if (policy
.zlp_rewind
&
4274 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
4275 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nvi
);
4276 zpool_rewind_exclaim(hdl
, zc
.zc_name
,
4277 ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) != 0),
4281 zcmd_free_nvlists(&zc
);
4285 zcmd_free_nvlists(&zc
);
4286 return (zpool_standard_error(hdl
, errno
, errbuf
));
4290 * Similar to zpool_clear(), but takes a GUID (used by fmd).
4293 zpool_vdev_clear(zpool_handle_t
*zhp
, uint64_t guid
)
4295 zfs_cmd_t zc
= {"\0"};
4296 char errbuf
[ERRBUFLEN
];
4297 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4299 (void) snprintf(errbuf
, sizeof (errbuf
),
4300 dgettext(TEXT_DOMAIN
, "cannot clear errors for %llx"),
4301 (u_longlong_t
)guid
);
4303 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4305 zc
.zc_cookie
= ZPOOL_NO_REWIND
;
4307 if (zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
) == 0)
4310 return (zpool_standard_error(hdl
, errno
, errbuf
));
4314 * Change the GUID for a pool.
4316 * Similar to zpool_reguid(), but may take a GUID.
4318 * If the guid argument is NULL, then no GUID is passed in the nvlist to the
4322 zpool_set_guid(zpool_handle_t
*zhp
, const uint64_t *guid
)
4324 char errbuf
[ERRBUFLEN
];
4325 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4326 nvlist_t
*nvl
= NULL
;
4327 zfs_cmd_t zc
= {"\0"};
4331 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
4332 return (no_memory(hdl
));
4334 if (nvlist_add_uint64(nvl
, ZPOOL_REGUID_GUID
, *guid
) != 0) {
4336 return (no_memory(hdl
));
4339 zcmd_write_src_nvlist(hdl
, &zc
, nvl
);
4342 (void) snprintf(errbuf
, sizeof (errbuf
),
4343 dgettext(TEXT_DOMAIN
, "cannot reguid '%s'"), zhp
->zpool_name
);
4345 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4346 error
= zfs_ioctl(hdl
, ZFS_IOC_POOL_REGUID
, &zc
);
4348 return (zpool_standard_error(hdl
, errno
, errbuf
));
4351 zcmd_free_nvlists(&zc
);
4358 * Change the GUID for a pool.
4361 zpool_reguid(zpool_handle_t
*zhp
)
4363 return (zpool_set_guid(zhp
, NULL
));
4370 zpool_reopen_one(zpool_handle_t
*zhp
, void *data
)
4372 libzfs_handle_t
*hdl
= zpool_get_handle(zhp
);
4373 const char *pool_name
= zpool_get_name(zhp
);
4374 boolean_t
*scrub_restart
= data
;
4377 error
= lzc_reopen(pool_name
, *scrub_restart
);
4379 return (zpool_standard_error_fmt(hdl
, error
,
4380 dgettext(TEXT_DOMAIN
, "cannot reopen '%s'"), pool_name
));
4386 /* call into libzfs_core to execute the sync IOCTL per pool */
4388 zpool_sync_one(zpool_handle_t
*zhp
, void *data
)
4391 libzfs_handle_t
*hdl
= zpool_get_handle(zhp
);
4392 const char *pool_name
= zpool_get_name(zhp
);
4393 boolean_t
*force
= data
;
4394 nvlist_t
*innvl
= fnvlist_alloc();
4396 fnvlist_add_boolean_value(innvl
, "force", *force
);
4397 if ((ret
= lzc_sync(pool_name
, innvl
, NULL
)) != 0) {
4399 return (zpool_standard_error_fmt(hdl
, ret
,
4400 dgettext(TEXT_DOMAIN
, "sync '%s' failed"), pool_name
));
4407 #define PATH_BUF_LEN 64
4410 * Given a vdev, return the name to display in iostat. If the vdev has a path,
4411 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
4412 * We also check if this is a whole disk, in which case we strip off the
4413 * trailing 's0' slice name.
4415 * This routine is also responsible for identifying when disks have been
4416 * reconfigured in a new location. The kernel will have opened the device by
4417 * devid, but the path will still refer to the old location. To catch this, we
4418 * first do a path -> devid translation (which is fast for the common case). If
4419 * the devid matches, we're done. If not, we do a reverse devid -> path
4420 * translation and issue the appropriate ioctl() to update the path of the vdev.
4421 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
4425 zpool_vdev_name(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, nvlist_t
*nv
,
4428 const char *type
, *tpath
;
4431 char buf
[PATH_BUF_LEN
];
4432 char tmpbuf
[PATH_BUF_LEN
* 2];
4435 * vdev_name will be "root"/"root-0" for the root vdev, but it is the
4436 * zpool name that will be displayed to the user.
4438 type
= fnvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
);
4439 if (zhp
!= NULL
&& strcmp(type
, "root") == 0)
4440 return (zfs_strdup(hdl
, zpool_get_name(zhp
)));
4442 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_PATH"))
4443 name_flags
|= VDEV_NAME_PATH
;
4444 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_GUID"))
4445 name_flags
|= VDEV_NAME_GUID
;
4446 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_FOLLOW_LINKS"))
4447 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
4449 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
, &value
) == 0 ||
4450 name_flags
& VDEV_NAME_GUID
) {
4451 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
);
4452 (void) snprintf(buf
, sizeof (buf
), "%llu", (u_longlong_t
)value
);
4454 } else if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &tpath
) == 0) {
4457 if (name_flags
& VDEV_NAME_FOLLOW_LINKS
) {
4458 char *rp
= realpath(path
, NULL
);
4460 strlcpy(buf
, rp
, sizeof (buf
));
4467 * For a block device only use the name.
4469 if ((strcmp(type
, VDEV_TYPE_DISK
) == 0) &&
4470 !(name_flags
& VDEV_NAME_PATH
)) {
4471 path
= zfs_strip_path(path
);
4475 * Remove the partition from the path if this is a whole disk.
4477 if (strcmp(type
, VDEV_TYPE_DRAID_SPARE
) != 0 &&
4478 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
, &value
)
4479 == 0 && value
&& !(name_flags
& VDEV_NAME_PATH
)) {
4480 return (zfs_strip_partition(path
));
4486 * If it's a raidz device, we need to stick in the parity level.
4488 if (strcmp(path
, VDEV_TYPE_RAIDZ
) == 0) {
4489 value
= fnvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
);
4490 (void) snprintf(buf
, sizeof (buf
), "%s%llu", path
,
4491 (u_longlong_t
)value
);
4496 * If it's a dRAID device, we add parity, groups, and spares.
4498 if (strcmp(path
, VDEV_TYPE_DRAID
) == 0) {
4499 uint64_t ndata
, nparity
, nspares
;
4503 verify(nvlist_lookup_nvlist_array(nv
,
4504 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) == 0);
4505 nparity
= fnvlist_lookup_uint64(nv
,
4506 ZPOOL_CONFIG_NPARITY
);
4507 ndata
= fnvlist_lookup_uint64(nv
,
4508 ZPOOL_CONFIG_DRAID_NDATA
);
4509 nspares
= fnvlist_lookup_uint64(nv
,
4510 ZPOOL_CONFIG_DRAID_NSPARES
);
4512 path
= zpool_draid_name(buf
, sizeof (buf
), ndata
,
4513 nparity
, nspares
, children
);
4517 * We identify each top-level vdev by using a <type-id>
4518 * naming convention.
4520 if (name_flags
& VDEV_NAME_TYPE_ID
) {
4521 uint64_t id
= fnvlist_lookup_uint64(nv
,
4523 (void) snprintf(tmpbuf
, sizeof (tmpbuf
), "%s-%llu",
4524 path
, (u_longlong_t
)id
);
4529 return (zfs_strdup(hdl
, path
));
4533 zbookmark_mem_compare(const void *a
, const void *b
)
4535 return (memcmp(a
, b
, sizeof (zbookmark_phys_t
)));
4539 zpool_add_propname(zpool_handle_t
*zhp
, const char *propname
)
4541 assert(zhp
->zpool_n_propnames
< ZHP_MAX_PROPNAMES
);
4542 zhp
->zpool_propnames
[zhp
->zpool_n_propnames
] = propname
;
4543 zhp
->zpool_n_propnames
++;
4547 * Retrieve the persistent error log, uniquify the members, and return to the
4551 zpool_get_errlog(zpool_handle_t
*zhp
, nvlist_t
**nverrlistp
)
4553 zfs_cmd_t zc
= {"\0"};
4554 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4555 zbookmark_phys_t
*buf
;
4556 uint64_t buflen
= 10000; /* approx. 1MB of RAM */
4558 if (fnvlist_lookup_uint64(zhp
->zpool_config
,
4559 ZPOOL_CONFIG_ERRCOUNT
) == 0)
4563 * Retrieve the raw error list from the kernel. If it doesn't fit,
4564 * allocate a larger buffer and retry.
4566 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
4568 buf
= zfs_alloc(zhp
->zpool_hdl
,
4569 buflen
* sizeof (zbookmark_phys_t
));
4570 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4571 zc
.zc_nvlist_dst_size
= buflen
;
4572 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_ERROR_LOG
,
4575 if (errno
== ENOMEM
) {
4578 return (zpool_standard_error_fmt(hdl
, errno
,
4579 dgettext(TEXT_DOMAIN
, "errors: List of "
4580 "errors unavailable")));
4588 * Sort the resulting bookmarks. This is a little confusing due to the
4589 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
4590 * to first, and 'zc_nvlist_dst_size' indicates the number of bookmarks
4591 * _not_ copied as part of the process. So we point the start of our
4592 * array appropriate and decrement the total number of elements.
4594 zbookmark_phys_t
*zb
= buf
+ zc
.zc_nvlist_dst_size
;
4595 uint64_t zblen
= buflen
- zc
.zc_nvlist_dst_size
;
4597 qsort(zb
, zblen
, sizeof (zbookmark_phys_t
), zbookmark_mem_compare
);
4599 verify(nvlist_alloc(nverrlistp
, 0, KM_SLEEP
) == 0);
4602 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
4604 for (uint64_t i
= 0; i
< zblen
; i
++) {
4607 /* ignoring zb_blkid and zb_level for now */
4608 if (i
> 0 && zb
[i
-1].zb_objset
== zb
[i
].zb_objset
&&
4609 zb
[i
-1].zb_object
== zb
[i
].zb_object
)
4612 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) != 0)
4614 if (nvlist_add_uint64(nv
, ZPOOL_ERR_DATASET
,
4615 zb
[i
].zb_objset
) != 0) {
4619 if (nvlist_add_uint64(nv
, ZPOOL_ERR_OBJECT
,
4620 zb
[i
].zb_object
) != 0) {
4624 if (nvlist_add_nvlist(*nverrlistp
, "ejk", nv
) != 0) {
4636 return (no_memory(zhp
->zpool_hdl
));
4640 * Upgrade a ZFS pool to the latest on-disk version.
4643 zpool_upgrade(zpool_handle_t
*zhp
, uint64_t new_version
)
4645 zfs_cmd_t zc
= {"\0"};
4646 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4648 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
4649 zc
.zc_cookie
= new_version
;
4651 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_UPGRADE
, &zc
) != 0)
4652 return (zpool_standard_error_fmt(hdl
, errno
,
4653 dgettext(TEXT_DOMAIN
, "cannot upgrade '%s'"),
4659 zfs_save_arguments(int argc
, char **argv
, char *string
, int len
)
4663 (void) strlcpy(string
, zfs_basename(argv
[0]), len
);
4664 for (i
= 1; i
< argc
; i
++) {
4665 (void) strlcat(string
, " ", len
);
4666 (void) strlcat(string
, argv
[i
], len
);
4671 zpool_log_history(libzfs_handle_t
*hdl
, const char *message
)
4673 zfs_cmd_t zc
= {"\0"};
4676 args
= fnvlist_alloc();
4677 fnvlist_add_string(args
, "message", message
);
4678 zcmd_write_src_nvlist(hdl
, &zc
, args
);
4679 int err
= zfs_ioctl(hdl
, ZFS_IOC_LOG_HISTORY
, &zc
);
4681 zcmd_free_nvlists(&zc
);
4686 * Perform ioctl to get some command history of a pool.
4688 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
4689 * logical offset of the history buffer to start reading from.
4691 * Upon return, 'off' is the next logical offset to read from and
4692 * 'len' is the actual amount of bytes read into 'buf'.
4695 get_history(zpool_handle_t
*zhp
, char *buf
, uint64_t *off
, uint64_t *len
)
4697 zfs_cmd_t zc
= {"\0"};
4698 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4700 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4702 zc
.zc_history
= (uint64_t)(uintptr_t)buf
;
4703 zc
.zc_history_len
= *len
;
4704 zc
.zc_history_offset
= *off
;
4706 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_GET_HISTORY
, &zc
) != 0) {
4709 return (zfs_error_fmt(hdl
, EZFS_PERM
,
4710 dgettext(TEXT_DOMAIN
,
4711 "cannot show history for pool '%s'"),
4714 return (zfs_error_fmt(hdl
, EZFS_NOHISTORY
,
4715 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
4716 "'%s'"), zhp
->zpool_name
));
4718 return (zfs_error_fmt(hdl
, EZFS_BADVERSION
,
4719 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
4720 "'%s', pool must be upgraded"), zhp
->zpool_name
));
4722 return (zpool_standard_error_fmt(hdl
, errno
,
4723 dgettext(TEXT_DOMAIN
,
4724 "cannot get history for '%s'"), zhp
->zpool_name
));
4728 *len
= zc
.zc_history_len
;
4729 *off
= zc
.zc_history_offset
;
4735 * Retrieve the command history of a pool.
4738 zpool_get_history(zpool_handle_t
*zhp
, nvlist_t
**nvhisp
, uint64_t *off
,
4741 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4743 int buflen
= 128 * 1024;
4744 nvlist_t
**records
= NULL
;
4745 uint_t numrecords
= 0;
4747 uint64_t start
= *off
;
4749 buf
= zfs_alloc(hdl
, buflen
);
4751 /* process about 1MiB a time */
4752 while (*off
- start
< 1024 * 1024) {
4753 uint64_t bytes_read
= buflen
;
4756 if ((err
= get_history(zhp
, buf
, off
, &bytes_read
)) != 0)
4759 /* if nothing else was read in, we're at EOF, just return */
4765 if ((err
= zpool_history_unpack(buf
, bytes_read
,
4766 &leftover
, &records
, &numrecords
)) != 0) {
4767 zpool_standard_error_fmt(hdl
, err
,
4768 dgettext(TEXT_DOMAIN
,
4769 "cannot get history for '%s'"), zhp
->zpool_name
);
4773 if (leftover
== bytes_read
) {
4775 * no progress made, because buffer is not big enough
4776 * to hold this record; resize and retry.
4780 buf
= zfs_alloc(hdl
, buflen
);
4787 *nvhisp
= fnvlist_alloc();
4788 fnvlist_add_nvlist_array(*nvhisp
, ZPOOL_HIST_RECORD
,
4789 (const nvlist_t
**)records
, numrecords
);
4791 for (i
= 0; i
< numrecords
; i
++)
4792 nvlist_free(records
[i
]);
4799 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
4800 * If there is a new event available 'nvp' will contain a newly allocated
4801 * nvlist and 'dropped' will be set to the number of missed events since
4802 * the last call to this function. When 'nvp' is set to NULL it indicates
4803 * no new events are available. In either case the function returns 0 and
4804 * it is up to the caller to free 'nvp'. In the case of a fatal error the
4805 * function will return a non-zero value. When the function is called in
4806 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
4807 * it will not return until a new event is available.
4810 zpool_events_next(libzfs_handle_t
*hdl
, nvlist_t
**nvp
,
4811 int *dropped
, unsigned flags
, int zevent_fd
)
4813 zfs_cmd_t zc
= {"\0"};
4818 zc
.zc_cleanup_fd
= zevent_fd
;
4820 if (flags
& ZEVENT_NONBLOCK
)
4821 zc
.zc_guid
= ZEVENT_NONBLOCK
;
4823 zcmd_alloc_dst_nvlist(hdl
, &zc
, ZEVENT_SIZE
);
4826 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_NEXT
, &zc
) != 0) {
4829 error
= zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
4830 dgettext(TEXT_DOMAIN
, "zfs shutdown"));
4833 /* Blocking error case should not occur */
4834 if (!(flags
& ZEVENT_NONBLOCK
))
4835 error
= zpool_standard_error_fmt(hdl
, errno
,
4836 dgettext(TEXT_DOMAIN
, "cannot get event"));
4840 zcmd_expand_dst_nvlist(hdl
, &zc
);
4843 error
= zpool_standard_error_fmt(hdl
, errno
,
4844 dgettext(TEXT_DOMAIN
, "cannot get event"));
4849 error
= zcmd_read_dst_nvlist(hdl
, &zc
, nvp
);
4853 *dropped
= (int)zc
.zc_cookie
;
4855 zcmd_free_nvlists(&zc
);
4864 zpool_events_clear(libzfs_handle_t
*hdl
, int *count
)
4866 zfs_cmd_t zc
= {"\0"};
4868 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_CLEAR
, &zc
) != 0)
4869 return (zpool_standard_error(hdl
, errno
,
4870 dgettext(TEXT_DOMAIN
, "cannot clear events")));
4873 *count
= (int)zc
.zc_cookie
; /* # of events cleared */
4879 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4880 * the passed zevent_fd file handle. On success zero is returned,
4881 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4884 zpool_events_seek(libzfs_handle_t
*hdl
, uint64_t eid
, int zevent_fd
)
4886 zfs_cmd_t zc
= {"\0"};
4890 zc
.zc_cleanup_fd
= zevent_fd
;
4892 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_SEEK
, &zc
) != 0) {
4895 error
= zfs_error_fmt(hdl
, EZFS_NOENT
,
4896 dgettext(TEXT_DOMAIN
, "cannot get event"));
4900 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
4901 dgettext(TEXT_DOMAIN
, "cannot get event"));
4905 error
= zpool_standard_error_fmt(hdl
, errno
,
4906 dgettext(TEXT_DOMAIN
, "cannot get event"));
4915 zpool_obj_to_path_impl(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4916 char *pathname
, size_t len
, boolean_t always_unmounted
)
4918 zfs_cmd_t zc
= {"\0"};
4919 boolean_t mounted
= B_FALSE
;
4920 char *mntpnt
= NULL
;
4921 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
4924 /* special case for the MOS */
4925 (void) snprintf(pathname
, len
, "<metadata>:<0x%llx>",
4930 /* get the dataset's name */
4931 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4933 if (zfs_ioctl(zhp
->zpool_hdl
,
4934 ZFS_IOC_DSOBJ_TO_DSNAME
, &zc
) != 0) {
4935 /* just write out a path of two object numbers */
4936 (void) snprintf(pathname
, len
, "<0x%llx>:<0x%llx>",
4937 (longlong_t
)dsobj
, (longlong_t
)obj
);
4940 (void) strlcpy(dsname
, zc
.zc_value
, sizeof (dsname
));
4942 /* find out if the dataset is mounted */
4943 mounted
= !always_unmounted
&& is_mounted(zhp
->zpool_hdl
, dsname
,
4946 /* get the corrupted object's path */
4947 (void) strlcpy(zc
.zc_name
, dsname
, sizeof (zc
.zc_name
));
4949 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_OBJ_TO_PATH
,
4952 (void) snprintf(pathname
, len
, "%s%s", mntpnt
,
4955 (void) snprintf(pathname
, len
, "%s:%s",
4956 dsname
, zc
.zc_value
);
4959 (void) snprintf(pathname
, len
, "%s:<0x%llx>", dsname
,
4966 zpool_obj_to_path(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4967 char *pathname
, size_t len
)
4969 zpool_obj_to_path_impl(zhp
, dsobj
, obj
, pathname
, len
, B_FALSE
);
4973 zpool_obj_to_path_ds(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4974 char *pathname
, size_t len
)
4976 zpool_obj_to_path_impl(zhp
, dsobj
, obj
, pathname
, len
, B_TRUE
);
4979 * Wait while the specified activity is in progress in the pool.
4982 zpool_wait(zpool_handle_t
*zhp
, zpool_wait_activity_t activity
)
4986 int error
= zpool_wait_status(zhp
, activity
, &missing
, NULL
);
4989 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, ENOENT
,
4990 dgettext(TEXT_DOMAIN
, "error waiting in pool '%s'"),
4999 * Wait for the given activity and return the status of the wait (whether or not
5000 * any waiting was done) in the 'waited' parameter. Non-existent pools are
5001 * reported via the 'missing' parameter, rather than by printing an error
5002 * message. This is convenient when this function is called in a loop over a
5003 * long period of time (as it is, for example, by zpool's wait cmd). In that
5004 * scenario, a pool being exported or destroyed should be considered a normal
5005 * event, so we don't want to print an error when we find that the pool doesn't
5009 zpool_wait_status(zpool_handle_t
*zhp
, zpool_wait_activity_t activity
,
5010 boolean_t
*missing
, boolean_t
*waited
)
5012 int error
= lzc_wait(zhp
->zpool_name
, activity
, waited
);
5013 *missing
= (error
== ENOENT
);
5018 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
5019 dgettext(TEXT_DOMAIN
, "error waiting in pool '%s'"),
5027 zpool_set_bootenv(zpool_handle_t
*zhp
, const nvlist_t
*envmap
)
5029 int error
= lzc_set_bootenv(zhp
->zpool_name
, envmap
);
5031 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
5032 dgettext(TEXT_DOMAIN
,
5033 "error setting bootenv in pool '%s'"), zhp
->zpool_name
);
5040 zpool_get_bootenv(zpool_handle_t
*zhp
, nvlist_t
**nvlp
)
5046 error
= lzc_get_bootenv(zhp
->zpool_name
, &nvl
);
5048 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
5049 dgettext(TEXT_DOMAIN
,
5050 "error getting bootenv in pool '%s'"), zhp
->zpool_name
);
5059 * Attempt to read and parse feature file(s) (from "compatibility" property).
5060 * Files contain zpool feature names, comma or whitespace-separated.
5061 * Comments (# character to next newline) are discarded.
5064 * compatibility : string containing feature filenames
5065 * features : either NULL or pointer to array of boolean
5066 * report : either NULL or pointer to string buffer
5067 * rlen : length of "report" buffer
5069 * compatibility is NULL (unset), "", "off", "legacy", or list of
5070 * comma-separated filenames. filenames should either be absolute,
5072 * 1) ZPOOL_SYSCONF_COMPAT_D (eg: /etc/zfs/compatibility.d) or
5073 * 2) ZPOOL_DATA_COMPAT_D (eg: /usr/share/zfs/compatibility.d).
5074 * (Unset), "" or "off" => enable all features
5075 * "legacy" => disable all features
5077 * Any feature names read from files which match unames in spa_feature_table
5078 * will have the corresponding boolean set in the features array (if non-NULL).
5079 * If more than one feature set specified, only features present in *all* of
5082 * "report" if not NULL will be populated with a suitable status message.
5085 * ZPOOL_COMPATIBILITY_OK : files read and parsed ok
5086 * ZPOOL_COMPATIBILITY_BADFILE : file too big or not a text file
5087 * ZPOOL_COMPATIBILITY_BADTOKEN : SYSCONF file contains invalid feature name
5088 * ZPOOL_COMPATIBILITY_WARNTOKEN : DATA file contains invalid feature name
5089 * ZPOOL_COMPATIBILITY_NOFILES : no feature files found
5091 zpool_compat_status_t
5092 zpool_load_compat(const char *compat
, boolean_t
*features
, char *report
,
5095 int sdirfd
, ddirfd
, featfd
;
5099 char *file
, *line
, *word
;
5101 char l_compat
[ZFS_MAXPROPLEN
];
5103 boolean_t ret_nofiles
= B_TRUE
;
5104 boolean_t ret_badfile
= B_FALSE
;
5105 boolean_t ret_badtoken
= B_FALSE
;
5106 boolean_t ret_warntoken
= B_FALSE
;
5108 /* special cases (unset), "" and "off" => enable all features */
5109 if (compat
== NULL
|| compat
[0] == '\0' ||
5110 strcmp(compat
, ZPOOL_COMPAT_OFF
) == 0) {
5111 if (features
!= NULL
)
5112 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
5113 features
[i
] = B_TRUE
;
5115 strlcpy(report
, gettext("all features enabled"), rlen
);
5116 return (ZPOOL_COMPATIBILITY_OK
);
5119 /* Final special case "legacy" => disable all features */
5120 if (strcmp(compat
, ZPOOL_COMPAT_LEGACY
) == 0) {
5121 if (features
!= NULL
)
5122 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
5123 features
[i
] = B_FALSE
;
5125 strlcpy(report
, gettext("all features disabled"), rlen
);
5126 return (ZPOOL_COMPATIBILITY_OK
);
5130 * Start with all true; will be ANDed with results from each file
5132 if (features
!= NULL
)
5133 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
5134 features
[i
] = B_TRUE
;
5136 char err_badfile
[ZFS_MAXPROPLEN
] = "";
5137 char err_badtoken
[ZFS_MAXPROPLEN
] = "";
5140 * We ignore errors from the directory open()
5141 * as they're only needed if the filename is relative
5142 * which will be checked during the openat().
5145 /* O_PATH safer than O_RDONLY if system allows it */
5147 #define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_PATH)
5149 #define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_RDONLY)
5152 sdirfd
= open(ZPOOL_SYSCONF_COMPAT_D
, ZC_DIR_FLAGS
);
5153 ddirfd
= open(ZPOOL_DATA_COMPAT_D
, ZC_DIR_FLAGS
);
5155 (void) strlcpy(l_compat
, compat
, ZFS_MAXPROPLEN
);
5157 for (file
= strtok_r(l_compat
, ",", &ps
);
5159 file
= strtok_r(NULL
, ",", &ps
)) {
5161 boolean_t l_features
[SPA_FEATURES
];
5163 enum { Z_SYSCONF
, Z_DATA
} source
;
5165 /* try sysconfdir first, then datadir */
5167 if ((featfd
= openat(sdirfd
, file
, O_RDONLY
| O_CLOEXEC
)) < 0) {
5168 featfd
= openat(ddirfd
, file
, O_RDONLY
| O_CLOEXEC
);
5172 /* File readable and correct size? */
5174 fstat(featfd
, &fs
) < 0 ||
5176 fs
.st_size
> ZPOOL_COMPAT_MAXSIZE
) {
5177 (void) close(featfd
);
5178 strlcat(err_badfile
, file
, ZFS_MAXPROPLEN
);
5179 strlcat(err_badfile
, " ", ZFS_MAXPROPLEN
);
5180 ret_badfile
= B_TRUE
;
5184 /* Prefault the file if system allows */
5185 #if defined(MAP_POPULATE)
5186 #define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_POPULATE)
5187 #elif defined(MAP_PREFAULT_READ)
5188 #define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_PREFAULT_READ)
5190 #define ZC_MMAP_FLAGS (MAP_PRIVATE)
5193 /* private mmap() so we can strtok safely */
5194 fc
= (char *)mmap(NULL
, fs
.st_size
, PROT_READ
| PROT_WRITE
,
5195 ZC_MMAP_FLAGS
, featfd
, 0);
5196 (void) close(featfd
);
5198 /* map ok, and last character == newline? */
5199 if (fc
== MAP_FAILED
|| fc
[fs
.st_size
- 1] != '\n') {
5200 (void) munmap((void *) fc
, fs
.st_size
);
5201 strlcat(err_badfile
, file
, ZFS_MAXPROPLEN
);
5202 strlcat(err_badfile
, " ", ZFS_MAXPROPLEN
);
5203 ret_badfile
= B_TRUE
;
5207 ret_nofiles
= B_FALSE
;
5209 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
5210 l_features
[i
] = B_FALSE
;
5212 /* replace final newline with NULL to ensure string ends */
5213 fc
[fs
.st_size
- 1] = '\0';
5215 for (line
= strtok_r(fc
, "\n", &ls
);
5217 line
= strtok_r(NULL
, "\n", &ls
)) {
5218 /* discard comments */
5219 char *r
= strchr(line
, '#');
5223 for (word
= strtok_r(line
, ", \t", &ws
);
5225 word
= strtok_r(NULL
, ", \t", &ws
)) {
5226 /* Find matching feature name */
5228 for (f
= 0; f
< SPA_FEATURES
; f
++) {
5229 zfeature_info_t
*fi
=
5230 &spa_feature_table
[f
];
5231 if (strcmp(word
, fi
->fi_uname
) == 0) {
5232 l_features
[f
] = B_TRUE
;
5236 if (f
< SPA_FEATURES
)
5239 /* found an unrecognized word */
5240 /* lightly sanitize it */
5241 if (strlen(word
) > 32)
5243 for (char *c
= word
; *c
!= '\0'; c
++)
5247 strlcat(err_badtoken
, word
, ZFS_MAXPROPLEN
);
5248 strlcat(err_badtoken
, " ", ZFS_MAXPROPLEN
);
5249 if (source
== Z_SYSCONF
)
5250 ret_badtoken
= B_TRUE
;
5252 ret_warntoken
= B_TRUE
;
5255 (void) munmap((void *) fc
, fs
.st_size
);
5257 if (features
!= NULL
)
5258 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
5259 features
[i
] &= l_features
[i
];
5261 (void) close(sdirfd
);
5262 (void) close(ddirfd
);
5264 /* Return the most serious error */
5267 snprintf(report
, rlen
, gettext("could not read/"
5268 "parse feature file(s): %s"), err_badfile
);
5269 return (ZPOOL_COMPATIBILITY_BADFILE
);
5274 gettext("no valid compatibility files specified"),
5276 return (ZPOOL_COMPATIBILITY_NOFILES
);
5280 snprintf(report
, rlen
, gettext("invalid feature "
5281 "name(s) in local compatibility files: %s"),
5283 return (ZPOOL_COMPATIBILITY_BADTOKEN
);
5285 if (ret_warntoken
) {
5287 snprintf(report
, rlen
, gettext("unrecognized feature "
5288 "name(s) in distribution compatibility files: %s"),
5290 return (ZPOOL_COMPATIBILITY_WARNTOKEN
);
5293 strlcpy(report
, gettext("compatibility set ok"), rlen
);
5294 return (ZPOOL_COMPATIBILITY_OK
);
5298 zpool_vdev_guid(zpool_handle_t
*zhp
, const char *vdevname
, uint64_t *vdev_guid
)
5301 boolean_t avail_spare
, l2cache
;
5303 verify(zhp
!= NULL
);
5304 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
5305 char errbuf
[ERRBUFLEN
];
5306 (void) snprintf(errbuf
, sizeof (errbuf
),
5307 dgettext(TEXT_DOMAIN
, "pool is in an unavailable state"));
5308 return (zfs_error(zhp
->zpool_hdl
, EZFS_POOLUNAVAIL
, errbuf
));
5311 if ((tgt
= zpool_find_vdev(zhp
, vdevname
, &avail_spare
, &l2cache
,
5313 char errbuf
[ERRBUFLEN
];
5314 (void) snprintf(errbuf
, sizeof (errbuf
),
5315 dgettext(TEXT_DOMAIN
, "can not find %s in %s"),
5316 vdevname
, zhp
->zpool_name
);
5317 return (zfs_error(zhp
->zpool_hdl
, EZFS_NODEVICE
, errbuf
));
5320 *vdev_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
5325 * Get a vdev property value for 'prop' and return the value in
5326 * a pre-allocated buffer.
5329 zpool_get_vdev_prop_value(nvlist_t
*nvprop
, vdev_prop_t prop
, char *prop_name
,
5330 char *buf
, size_t len
, zprop_source_t
*srctype
, boolean_t literal
)
5335 zprop_source_t src
= ZPROP_SRC_NONE
;
5337 if (prop
== VDEV_PROP_USERPROP
) {
5338 /* user property, prop_name must contain the property name */
5339 assert(prop_name
!= NULL
);
5340 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
5341 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
5342 strval
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
5344 /* user prop not found */
5347 (void) strlcpy(buf
, strval
, len
);
5353 if (prop_name
== NULL
)
5354 prop_name
= (char *)vdev_prop_to_name(prop
);
5356 switch (vdev_prop_get_type(prop
)) {
5357 case PROP_TYPE_STRING
:
5358 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
5359 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
5360 strval
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
5362 src
= ZPROP_SRC_DEFAULT
;
5363 if ((strval
= vdev_prop_default_string(prop
)) == NULL
)
5366 (void) strlcpy(buf
, strval
, len
);
5369 case PROP_TYPE_NUMBER
:
5370 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
5371 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
5372 intval
= fnvlist_lookup_uint64(nv
, ZPROP_VALUE
);
5374 src
= ZPROP_SRC_DEFAULT
;
5375 intval
= vdev_prop_default_numeric(prop
);
5379 case VDEV_PROP_ASIZE
:
5380 case VDEV_PROP_PSIZE
:
5381 case VDEV_PROP_SIZE
:
5382 case VDEV_PROP_BOOTSIZE
:
5383 case VDEV_PROP_ALLOCATED
:
5384 case VDEV_PROP_FREE
:
5385 case VDEV_PROP_READ_ERRORS
:
5386 case VDEV_PROP_WRITE_ERRORS
:
5387 case VDEV_PROP_CHECKSUM_ERRORS
:
5388 case VDEV_PROP_INITIALIZE_ERRORS
:
5389 case VDEV_PROP_TRIM_ERRORS
:
5390 case VDEV_PROP_SLOW_IOS
:
5391 case VDEV_PROP_OPS_NULL
:
5392 case VDEV_PROP_OPS_READ
:
5393 case VDEV_PROP_OPS_WRITE
:
5394 case VDEV_PROP_OPS_FREE
:
5395 case VDEV_PROP_OPS_CLAIM
:
5396 case VDEV_PROP_OPS_TRIM
:
5397 case VDEV_PROP_BYTES_NULL
:
5398 case VDEV_PROP_BYTES_READ
:
5399 case VDEV_PROP_BYTES_WRITE
:
5400 case VDEV_PROP_BYTES_FREE
:
5401 case VDEV_PROP_BYTES_CLAIM
:
5402 case VDEV_PROP_BYTES_TRIM
:
5404 (void) snprintf(buf
, len
, "%llu",
5405 (u_longlong_t
)intval
);
5407 (void) zfs_nicenum(intval
, buf
, len
);
5410 case VDEV_PROP_EXPANDSZ
:
5412 (void) strlcpy(buf
, "-", len
);
5413 } else if (literal
) {
5414 (void) snprintf(buf
, len
, "%llu",
5415 (u_longlong_t
)intval
);
5417 (void) zfs_nicenum(intval
, buf
, len
);
5420 case VDEV_PROP_CAPACITY
:
5422 (void) snprintf(buf
, len
, "%llu",
5423 (u_longlong_t
)intval
);
5425 (void) snprintf(buf
, len
, "%llu%%",
5426 (u_longlong_t
)intval
);
5429 case VDEV_PROP_CHECKSUM_N
:
5430 case VDEV_PROP_CHECKSUM_T
:
5431 case VDEV_PROP_IO_N
:
5432 case VDEV_PROP_IO_T
:
5433 case VDEV_PROP_SLOW_IO_N
:
5434 case VDEV_PROP_SLOW_IO_T
:
5435 if (intval
== UINT64_MAX
) {
5436 (void) strlcpy(buf
, "-", len
);
5438 (void) snprintf(buf
, len
, "%llu",
5439 (u_longlong_t
)intval
);
5442 case VDEV_PROP_FRAGMENTATION
:
5443 if (intval
== UINT64_MAX
) {
5444 (void) strlcpy(buf
, "-", len
);
5446 (void) snprintf(buf
, len
, "%llu%%",
5447 (u_longlong_t
)intval
);
5450 case VDEV_PROP_STATE
:
5452 (void) snprintf(buf
, len
, "%llu",
5453 (u_longlong_t
)intval
);
5455 (void) strlcpy(buf
, zpool_state_to_name(intval
,
5456 VDEV_AUX_NONE
), len
);
5460 (void) snprintf(buf
, len
, "%llu",
5461 (u_longlong_t
)intval
);
5465 case PROP_TYPE_INDEX
:
5466 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
5467 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
5468 intval
= fnvlist_lookup_uint64(nv
, ZPROP_VALUE
);
5470 /* 'trim_support' only valid for leaf vdevs */
5471 if (prop
== VDEV_PROP_TRIM_SUPPORT
) {
5472 (void) strlcpy(buf
, "-", len
);
5475 src
= ZPROP_SRC_DEFAULT
;
5476 intval
= vdev_prop_default_numeric(prop
);
5477 /* Only use if provided by the RAIDZ VDEV above */
5478 if (prop
== VDEV_PROP_RAIDZ_EXPANDING
)
5481 if (vdev_prop_index_to_string(prop
, intval
,
5482 (const char **)&strval
) != 0)
5484 (void) strlcpy(buf
, strval
, len
);
5498 * Get a vdev property value for 'prop_name' and return the value in
5499 * a pre-allocated buffer.
5502 zpool_get_vdev_prop(zpool_handle_t
*zhp
, const char *vdevname
, vdev_prop_t prop
,
5503 char *prop_name
, char *buf
, size_t len
, zprop_source_t
*srctype
,
5506 nvlist_t
*reqnvl
, *reqprops
;
5507 nvlist_t
*retprops
= NULL
;
5508 uint64_t vdev_guid
= 0;
5511 if ((ret
= zpool_vdev_guid(zhp
, vdevname
, &vdev_guid
)) != 0)
5514 if (nvlist_alloc(&reqnvl
, NV_UNIQUE_NAME
, 0) != 0)
5515 return (no_memory(zhp
->zpool_hdl
));
5516 if (nvlist_alloc(&reqprops
, NV_UNIQUE_NAME
, 0) != 0)
5517 return (no_memory(zhp
->zpool_hdl
));
5519 fnvlist_add_uint64(reqnvl
, ZPOOL_VDEV_PROPS_GET_VDEV
, vdev_guid
);
5521 if (prop
!= VDEV_PROP_USERPROP
) {
5522 /* prop_name overrides prop value */
5523 if (prop_name
!= NULL
)
5524 prop
= vdev_name_to_prop(prop_name
);
5526 prop_name
= (char *)vdev_prop_to_name(prop
);
5527 assert(prop
< VDEV_NUM_PROPS
);
5530 assert(prop_name
!= NULL
);
5531 if (nvlist_add_uint64(reqprops
, prop_name
, prop
) != 0) {
5532 nvlist_free(reqnvl
);
5533 nvlist_free(reqprops
);
5534 return (no_memory(zhp
->zpool_hdl
));
5537 fnvlist_add_nvlist(reqnvl
, ZPOOL_VDEV_PROPS_GET_PROPS
, reqprops
);
5539 ret
= lzc_get_vdev_prop(zhp
->zpool_name
, reqnvl
, &retprops
);
5542 ret
= zpool_get_vdev_prop_value(retprops
, prop
, prop_name
, buf
,
5543 len
, srctype
, literal
);
5545 char errbuf
[ERRBUFLEN
];
5546 (void) snprintf(errbuf
, sizeof (errbuf
),
5547 dgettext(TEXT_DOMAIN
, "cannot get vdev property %s from"
5548 " %s in %s"), prop_name
, vdevname
, zhp
->zpool_name
);
5549 (void) zpool_standard_error(zhp
->zpool_hdl
, ret
, errbuf
);
5552 nvlist_free(reqnvl
);
5553 nvlist_free(reqprops
);
5554 nvlist_free(retprops
);
5560 * Get all vdev properties
5563 zpool_get_all_vdev_props(zpool_handle_t
*zhp
, const char *vdevname
,
5566 nvlist_t
*nvl
= NULL
;
5567 uint64_t vdev_guid
= 0;
5570 if ((ret
= zpool_vdev_guid(zhp
, vdevname
, &vdev_guid
)) != 0)
5573 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5574 return (no_memory(zhp
->zpool_hdl
));
5576 fnvlist_add_uint64(nvl
, ZPOOL_VDEV_PROPS_GET_VDEV
, vdev_guid
);
5578 ret
= lzc_get_vdev_prop(zhp
->zpool_name
, nvl
, outnvl
);
5583 char errbuf
[ERRBUFLEN
];
5584 (void) snprintf(errbuf
, sizeof (errbuf
),
5585 dgettext(TEXT_DOMAIN
, "cannot get vdev properties for"
5586 " %s in %s"), vdevname
, zhp
->zpool_name
);
5587 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
5597 zpool_set_vdev_prop(zpool_handle_t
*zhp
, const char *vdevname
,
5598 const char *propname
, const char *propval
)
5601 nvlist_t
*nvl
= NULL
;
5602 nvlist_t
*outnvl
= NULL
;
5604 nvlist_t
*realprops
;
5605 prop_flags_t flags
= { 0 };
5609 if ((ret
= zpool_vdev_guid(zhp
, vdevname
, &vdev_guid
)) != 0)
5612 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5613 return (no_memory(zhp
->zpool_hdl
));
5614 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
5615 return (no_memory(zhp
->zpool_hdl
));
5617 fnvlist_add_uint64(nvl
, ZPOOL_VDEV_PROPS_SET_VDEV
, vdev_guid
);
5619 if (nvlist_add_string(props
, propname
, propval
) != 0) {
5621 return (no_memory(zhp
->zpool_hdl
));
5624 char errbuf
[ERRBUFLEN
];
5625 (void) snprintf(errbuf
, sizeof (errbuf
),
5626 dgettext(TEXT_DOMAIN
, "cannot set property %s for %s on %s"),
5627 propname
, vdevname
, zhp
->zpool_name
);
5630 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
5631 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
5632 zhp
->zpool_name
, props
, version
, flags
, errbuf
)) == NULL
) {
5641 fnvlist_add_nvlist(nvl
, ZPOOL_VDEV_PROPS_SET_PROPS
, props
);
5643 ret
= lzc_set_vdev_prop(zhp
->zpool_name
, nvl
, &outnvl
);
5647 nvlist_free(outnvl
);
5650 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
5656 * Prune older entries from the DDT to reclaim space under the quota
5659 zpool_ddt_prune(zpool_handle_t
*zhp
, zpool_ddt_prune_unit_t unit
,
5662 int error
= lzc_ddt_prune(zhp
->zpool_name
, unit
, amount
);
5664 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
5665 char errbuf
[ERRBUFLEN
];
5667 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
5668 "cannot prune dedup table on '%s'"), zhp
->zpool_name
);
5670 if (error
== EALREADY
) {
5671 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5672 "a prune operation is already in progress"));
5673 (void) zfs_error(hdl
, EZFS_BUSY
, errbuf
);
5675 (void) zpool_standard_error(hdl
, errno
, errbuf
);