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, 2020 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 zcmd_alloc_dst_nvlist(hdl
, &zc
, 0);
84 while (zfs_ioctl(hdl
, ZFS_IOC_POOL_GET_PROPS
, &zc
) != 0) {
86 zcmd_expand_dst_nvlist(hdl
, &zc
);
88 zcmd_free_nvlists(&zc
);
93 if (zcmd_read_dst_nvlist(hdl
, &zc
, &zhp
->zpool_props
) != 0) {
94 zcmd_free_nvlists(&zc
);
98 zcmd_free_nvlists(&zc
);
104 zpool_props_refresh(zpool_handle_t
*zhp
)
108 old_props
= zhp
->zpool_props
;
110 if (zpool_get_all_props(zhp
) != 0)
113 nvlist_free(old_props
);
118 zpool_get_prop_string(zpool_handle_t
*zhp
, zpool_prop_t prop
,
123 zprop_source_t source
;
125 nvl
= zhp
->zpool_props
;
126 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
127 source
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
128 value
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
130 source
= ZPROP_SRC_DEFAULT
;
131 if ((value
= zpool_prop_default_string(prop
)) == NULL
)
142 zpool_get_prop_int(zpool_handle_t
*zhp
, zpool_prop_t prop
, zprop_source_t
*src
)
146 zprop_source_t source
;
148 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
)) {
150 * zpool_get_all_props() has most likely failed because
151 * the pool is faulted, but if all we need is the top level
152 * vdev's guid then get it from the zhp config nvlist.
154 if ((prop
== ZPOOL_PROP_GUID
) &&
155 (nvlist_lookup_nvlist(zhp
->zpool_config
,
156 ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0) &&
157 (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
)
161 return (zpool_prop_default_numeric(prop
));
164 nvl
= zhp
->zpool_props
;
165 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
166 source
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
167 value
= fnvlist_lookup_uint64(nv
, ZPROP_VALUE
);
169 source
= ZPROP_SRC_DEFAULT
;
170 value
= zpool_prop_default_numeric(prop
);
180 * Map VDEV STATE to printed strings.
183 zpool_state_to_name(vdev_state_t state
, vdev_aux_t aux
)
186 case VDEV_STATE_CLOSED
:
187 case VDEV_STATE_OFFLINE
:
188 return (gettext("OFFLINE"));
189 case VDEV_STATE_REMOVED
:
190 return (gettext("REMOVED"));
191 case VDEV_STATE_CANT_OPEN
:
192 if (aux
== VDEV_AUX_CORRUPT_DATA
|| aux
== VDEV_AUX_BAD_LOG
)
193 return (gettext("FAULTED"));
194 else if (aux
== VDEV_AUX_SPLIT_POOL
)
195 return (gettext("SPLIT"));
197 return (gettext("UNAVAIL"));
198 case VDEV_STATE_FAULTED
:
199 return (gettext("FAULTED"));
200 case VDEV_STATE_DEGRADED
:
201 return (gettext("DEGRADED"));
202 case VDEV_STATE_HEALTHY
:
203 return (gettext("ONLINE"));
209 return (gettext("UNKNOWN"));
213 * Map POOL STATE to printed strings.
216 zpool_pool_state_to_name(pool_state_t state
)
221 case POOL_STATE_ACTIVE
:
222 return (gettext("ACTIVE"));
223 case POOL_STATE_EXPORTED
:
224 return (gettext("EXPORTED"));
225 case POOL_STATE_DESTROYED
:
226 return (gettext("DESTROYED"));
227 case POOL_STATE_SPARE
:
228 return (gettext("SPARE"));
229 case POOL_STATE_L2CACHE
:
230 return (gettext("L2CACHE"));
231 case POOL_STATE_UNINITIALIZED
:
232 return (gettext("UNINITIALIZED"));
233 case POOL_STATE_UNAVAIL
:
234 return (gettext("UNAVAIL"));
235 case POOL_STATE_POTENTIALLY_ACTIVE
:
236 return (gettext("POTENTIALLY_ACTIVE"));
239 return (gettext("UNKNOWN"));
243 * Given a pool handle, return the pool health string ("ONLINE", "DEGRADED",
247 zpool_get_state_str(zpool_handle_t
*zhp
)
249 zpool_errata_t errata
;
250 zpool_status_t status
;
253 status
= zpool_get_status(zhp
, NULL
, &errata
);
255 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
256 str
= gettext("FAULTED");
257 } else if (status
== ZPOOL_STATUS_IO_FAILURE_WAIT
||
258 status
== ZPOOL_STATUS_IO_FAILURE_CONTINUE
||
259 status
== ZPOOL_STATUS_IO_FAILURE_MMP
) {
260 str
= gettext("SUSPENDED");
262 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(
263 zpool_get_config(zhp
, NULL
), ZPOOL_CONFIG_VDEV_TREE
);
265 vdev_stat_t
*vs
= (vdev_stat_t
*)fnvlist_lookup_uint64_array(
266 nvroot
, ZPOOL_CONFIG_VDEV_STATS
, &vsc
);
267 str
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
273 * Get a zpool property value for 'prop' and return the value in
274 * a pre-allocated buffer.
277 zpool_get_prop(zpool_handle_t
*zhp
, zpool_prop_t prop
, char *buf
,
278 size_t len
, zprop_source_t
*srctype
, boolean_t literal
)
282 zprop_source_t src
= ZPROP_SRC_NONE
;
284 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
286 case ZPOOL_PROP_NAME
:
287 (void) strlcpy(buf
, zpool_get_name(zhp
), len
);
290 case ZPOOL_PROP_HEALTH
:
291 (void) strlcpy(buf
, zpool_get_state_str(zhp
), len
);
294 case ZPOOL_PROP_GUID
:
295 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
296 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
299 case ZPOOL_PROP_ALTROOT
:
300 case ZPOOL_PROP_CACHEFILE
:
301 case ZPOOL_PROP_COMMENT
:
302 case ZPOOL_PROP_COMPATIBILITY
:
303 if (zhp
->zpool_props
!= NULL
||
304 zpool_get_all_props(zhp
) == 0) {
306 zpool_get_prop_string(zhp
, prop
, &src
),
312 (void) strlcpy(buf
, "-", len
);
321 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
) &&
322 prop
!= ZPOOL_PROP_NAME
)
325 switch (zpool_prop_get_type(prop
)) {
326 case PROP_TYPE_STRING
:
327 (void) strlcpy(buf
, zpool_get_prop_string(zhp
, prop
, &src
),
331 case PROP_TYPE_NUMBER
:
332 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
335 case ZPOOL_PROP_SIZE
:
336 case ZPOOL_PROP_ALLOCATED
:
337 case ZPOOL_PROP_FREE
:
338 case ZPOOL_PROP_FREEING
:
339 case ZPOOL_PROP_LEAKED
:
340 case ZPOOL_PROP_ASHIFT
:
341 case ZPOOL_PROP_MAXBLOCKSIZE
:
342 case ZPOOL_PROP_MAXDNODESIZE
:
343 case ZPOOL_PROP_BCLONESAVED
:
344 case ZPOOL_PROP_BCLONEUSED
:
346 (void) snprintf(buf
, len
, "%llu",
347 (u_longlong_t
)intval
);
349 (void) zfs_nicenum(intval
, buf
, len
);
352 case ZPOOL_PROP_EXPANDSZ
:
353 case ZPOOL_PROP_CHECKPOINT
:
355 (void) strlcpy(buf
, "-", len
);
356 } else if (literal
) {
357 (void) snprintf(buf
, len
, "%llu",
358 (u_longlong_t
)intval
);
360 (void) zfs_nicebytes(intval
, buf
, len
);
364 case ZPOOL_PROP_CAPACITY
:
366 (void) snprintf(buf
, len
, "%llu",
367 (u_longlong_t
)intval
);
369 (void) snprintf(buf
, len
, "%llu%%",
370 (u_longlong_t
)intval
);
374 case ZPOOL_PROP_FRAGMENTATION
:
375 if (intval
== UINT64_MAX
) {
376 (void) strlcpy(buf
, "-", len
);
377 } else if (literal
) {
378 (void) snprintf(buf
, len
, "%llu",
379 (u_longlong_t
)intval
);
381 (void) snprintf(buf
, len
, "%llu%%",
382 (u_longlong_t
)intval
);
386 case ZPOOL_PROP_BCLONERATIO
:
387 case ZPOOL_PROP_DEDUPRATIO
:
389 (void) snprintf(buf
, len
, "%llu.%02llu",
390 (u_longlong_t
)(intval
/ 100),
391 (u_longlong_t
)(intval
% 100));
393 (void) snprintf(buf
, len
, "%llu.%02llux",
394 (u_longlong_t
)(intval
/ 100),
395 (u_longlong_t
)(intval
% 100));
398 case ZPOOL_PROP_HEALTH
:
399 (void) strlcpy(buf
, zpool_get_state_str(zhp
), len
);
401 case ZPOOL_PROP_VERSION
:
402 if (intval
>= SPA_VERSION_FEATURES
) {
403 (void) snprintf(buf
, len
, "-");
408 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
412 case PROP_TYPE_INDEX
:
413 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
414 if (zpool_prop_index_to_string(prop
, intval
, &strval
)
417 (void) strlcpy(buf
, strval
, len
);
431 * Get a zpool property value for 'propname' and return the value in
432 * a pre-allocated buffer.
435 zpool_get_userprop(zpool_handle_t
*zhp
, const char *propname
, char *buf
,
436 size_t len
, zprop_source_t
*srctype
)
441 zprop_source_t source
= ZPROP_SRC_LOCAL
;
443 nvl
= zhp
->zpool_props
;
444 if (nvlist_lookup_nvlist(nvl
, propname
, &nv
) == 0) {
445 if (nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &ival
) == 0)
447 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
449 source
= ZPROP_SRC_DEFAULT
;
456 (void) strlcpy(buf
, value
, len
);
462 * Check if the bootfs name has the same pool name as it is set to.
463 * Assuming bootfs is a valid dataset name.
466 bootfs_name_valid(const char *pool
, const char *bootfs
)
468 int len
= strlen(pool
);
469 if (bootfs
[0] == '\0')
472 if (!zfs_name_valid(bootfs
, ZFS_TYPE_FILESYSTEM
|ZFS_TYPE_SNAPSHOT
))
475 if (strncmp(pool
, bootfs
, len
) == 0 &&
476 (bootfs
[len
] == '/' || bootfs
[len
] == '\0'))
483 * Given an nvlist of zpool properties to be set, validate that they are
484 * correct, and parse any numeric properties (index, boolean, etc) if they are
485 * specified as strings.
488 zpool_valid_proplist(libzfs_handle_t
*hdl
, const char *poolname
,
489 nvlist_t
*props
, uint64_t version
, prop_flags_t flags
, char *errbuf
)
496 const char *slash
, *check
;
497 struct stat64 statbuf
;
501 if (nvlist_alloc(&retprops
, NV_UNIQUE_NAME
, 0) != 0) {
502 (void) no_memory(hdl
);
507 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
508 const char *propname
= nvpair_name(elem
);
510 if (flags
.vdevprop
&& zpool_prop_vdev(propname
)) {
511 vdev_prop_t vprop
= vdev_name_to_prop(propname
);
513 if (vdev_prop_readonly(vprop
)) {
514 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
515 "is readonly"), propname
);
516 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
521 if (zprop_parse_value(hdl
, elem
, vprop
, ZFS_TYPE_VDEV
,
522 retprops
, &strval
, &intval
, errbuf
) != 0)
526 } else if (flags
.vdevprop
&& vdev_prop_user(propname
)) {
527 if (nvlist_add_nvpair(retprops
, elem
) != 0) {
528 (void) no_memory(hdl
);
532 } else if (flags
.vdevprop
) {
533 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
534 "invalid property: '%s'"), propname
);
535 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
539 prop
= zpool_name_to_prop(propname
);
540 if (prop
== ZPOOL_PROP_INVAL
&& zpool_prop_feature(propname
)) {
542 char *fname
= strchr(propname
, '@') + 1;
544 err
= zfeature_lookup_name(fname
, NULL
);
546 ASSERT3U(err
, ==, ENOENT
);
547 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
548 "feature '%s' unsupported by kernel"),
550 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
554 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
555 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
556 "'%s' must be a string"), propname
);
557 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
561 (void) nvpair_value_string(elem
, &strval
);
562 if (strcmp(strval
, ZFS_FEATURE_ENABLED
) != 0 &&
563 strcmp(strval
, ZFS_FEATURE_DISABLED
) != 0) {
564 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
565 "property '%s' can only be set to "
566 "'enabled' or 'disabled'"), propname
);
567 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
572 strcmp(strval
, ZFS_FEATURE_DISABLED
) == 0) {
573 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
574 "property '%s' can only be set to "
575 "'disabled' at creation time"), propname
);
576 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
580 if (nvlist_add_uint64(retprops
, propname
, 0) != 0) {
581 (void) no_memory(hdl
);
585 } else if (prop
== ZPOOL_PROP_INVAL
&&
586 zfs_prop_user(propname
)) {
588 * This is a user property: make sure it's a
589 * string, and that it's less than ZAP_MAXNAMELEN.
591 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
592 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
593 "'%s' must be a string"), propname
);
594 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
598 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
599 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
600 "property name '%s' is too long"),
602 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
606 (void) nvpair_value_string(elem
, &strval
);
608 if (strlen(strval
) >= ZFS_MAXPROPLEN
) {
609 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
610 "property value '%s' is too long"),
612 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
616 if (nvlist_add_string(retprops
, propname
,
618 (void) no_memory(hdl
);
626 * Make sure this property is valid and applies to this type.
628 if (prop
== ZPOOL_PROP_INVAL
) {
629 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
630 "invalid property '%s'"), propname
);
631 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
635 if (zpool_prop_readonly(prop
)) {
636 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
637 "is readonly"), propname
);
638 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
642 if (!flags
.create
&& zpool_prop_setonce(prop
)) {
643 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
644 "property '%s' can only be set at "
645 "creation time"), propname
);
646 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
650 if (zprop_parse_value(hdl
, elem
, prop
, ZFS_TYPE_POOL
, retprops
,
651 &strval
, &intval
, errbuf
) != 0)
655 * Perform additional checking for specific properties.
658 case ZPOOL_PROP_VERSION
:
659 if (intval
< version
||
660 !SPA_VERSION_IS_SUPPORTED(intval
)) {
661 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
662 "property '%s' number %llu is invalid."),
663 propname
, (unsigned long long)intval
);
664 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
669 case ZPOOL_PROP_ASHIFT
:
671 (intval
< ASHIFT_MIN
|| intval
> ASHIFT_MAX
)) {
672 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
673 "property '%s' number %llu is invalid, "
674 "only values between %" PRId32
" and %"
675 PRId32
" are allowed."),
676 propname
, (unsigned long long)intval
,
677 ASHIFT_MIN
, ASHIFT_MAX
);
678 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
683 case ZPOOL_PROP_BOOTFS
:
684 if (flags
.create
|| flags
.import
) {
685 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
686 "property '%s' cannot be set at creation "
687 "or import time"), propname
);
688 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
692 if (version
< SPA_VERSION_BOOTFS
) {
693 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
694 "pool must be upgraded to support "
695 "'%s' property"), propname
);
696 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
701 * bootfs property value has to be a dataset name and
702 * the dataset has to be in the same pool as it sets to.
704 if (!bootfs_name_valid(poolname
, strval
)) {
705 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
706 "is an invalid name"), strval
);
707 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
711 if ((zhp
= zpool_open_canfail(hdl
, poolname
)) == NULL
) {
712 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
713 "could not open pool '%s'"), poolname
);
714 (void) zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
);
720 case ZPOOL_PROP_ALTROOT
:
721 if (!flags
.create
&& !flags
.import
) {
722 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
723 "property '%s' can only be set during pool "
724 "creation or import"), propname
);
725 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
729 if (strval
[0] != '/') {
730 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
731 "bad alternate root '%s'"), strval
);
732 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
737 case ZPOOL_PROP_CACHEFILE
:
738 if (strval
[0] == '\0')
741 if (strcmp(strval
, "none") == 0)
744 if (strval
[0] != '/') {
745 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
746 "property '%s' must be empty, an "
747 "absolute path, or 'none'"), propname
);
748 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
752 slash
= strrchr(strval
, '/');
754 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
755 strcmp(slash
, "/..") == 0) {
756 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
757 "'%s' is not a valid file"), strval
);
758 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
762 *(char *)slash
= '\0';
764 if (strval
[0] != '\0' &&
765 (stat64(strval
, &statbuf
) != 0 ||
766 !S_ISDIR(statbuf
.st_mode
))) {
767 *(char *)slash
= '/';
768 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
769 "'%s' is not a valid directory"),
771 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
775 *(char *)slash
= '/';
778 case ZPOOL_PROP_COMPATIBILITY
:
779 switch (zpool_load_compat(strval
, NULL
, report
, 1024)) {
780 case ZPOOL_COMPATIBILITY_OK
:
781 case ZPOOL_COMPATIBILITY_WARNTOKEN
:
783 case ZPOOL_COMPATIBILITY_BADFILE
:
784 case ZPOOL_COMPATIBILITY_BADTOKEN
:
785 case ZPOOL_COMPATIBILITY_NOFILES
:
786 zfs_error_aux(hdl
, "%s", report
);
787 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
792 case ZPOOL_PROP_COMMENT
:
793 for (check
= strval
; *check
!= '\0'; check
++) {
794 if (!isprint(*check
)) {
796 dgettext(TEXT_DOMAIN
,
797 "comment may only have printable "
799 (void) zfs_error(hdl
, EZFS_BADPROP
,
804 if (strlen(strval
) > ZPROP_MAX_COMMENT
) {
805 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
806 "comment must not exceed %d characters"),
808 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
812 case ZPOOL_PROP_READONLY
:
814 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
815 "property '%s' can only be set at "
816 "import time"), propname
);
817 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
821 case ZPOOL_PROP_MULTIHOST
:
822 if (get_system_hostid() == 0) {
823 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
824 "requires a non-zero system hostid"));
825 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
829 case ZPOOL_PROP_DEDUPDITTO
:
830 printf("Note: property '%s' no longer has "
831 "any effect\n", propname
);
841 nvlist_free(retprops
);
846 * Set zpool property : propname=propval.
849 zpool_set_prop(zpool_handle_t
*zhp
, const char *propname
, const char *propval
)
851 zfs_cmd_t zc
= {"\0"};
853 char errbuf
[ERRBUFLEN
];
854 nvlist_t
*nvl
= NULL
;
857 prop_flags_t flags
= { 0 };
859 (void) snprintf(errbuf
, sizeof (errbuf
),
860 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
863 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
864 return (no_memory(zhp
->zpool_hdl
));
866 if (nvlist_add_string(nvl
, propname
, propval
) != 0) {
868 return (no_memory(zhp
->zpool_hdl
));
871 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
872 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
873 zhp
->zpool_name
, nvl
, version
, flags
, errbuf
)) == NULL
) {
882 * Execute the corresponding ioctl() to set this property.
884 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
886 zcmd_write_src_nvlist(zhp
->zpool_hdl
, &zc
, nvl
);
888 ret
= zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_SET_PROPS
, &zc
);
890 zcmd_free_nvlists(&zc
);
894 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
896 (void) zpool_props_refresh(zhp
);
902 zpool_expand_proplist(zpool_handle_t
*zhp
, zprop_list_t
**plp
,
903 zfs_type_t type
, boolean_t literal
)
905 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
907 char buf
[ZFS_MAXPROPLEN
];
908 nvlist_t
*features
= NULL
;
911 boolean_t firstexpand
= (NULL
== *plp
);
914 if (zprop_expand_list(hdl
, plp
, type
) != 0)
917 if (type
== ZFS_TYPE_VDEV
)
921 while (*last
!= NULL
)
922 last
= &(*last
)->pl_next
;
925 features
= zpool_get_features(zhp
);
927 if ((*plp
)->pl_all
&& firstexpand
) {
928 /* Handle userprops in the all properties case */
929 if (zhp
->zpool_props
== NULL
&& zpool_props_refresh(zhp
))
933 while ((nvp
= nvlist_next_nvpair(zhp
->zpool_props
, nvp
)) !=
935 const char *propname
= nvpair_name(nvp
);
937 if (!zfs_prop_user(propname
))
940 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
941 entry
->pl_prop
= ZPROP_USERPROP
;
942 entry
->pl_user_prop
= zfs_strdup(hdl
, propname
);
943 entry
->pl_width
= strlen(entry
->pl_user_prop
);
944 entry
->pl_all
= B_TRUE
;
947 last
= &entry
->pl_next
;
950 for (i
= 0; i
< SPA_FEATURES
; i
++) {
951 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
952 entry
->pl_prop
= ZPROP_USERPROP
;
953 entry
->pl_user_prop
= zfs_asprintf(hdl
, "feature@%s",
954 spa_feature_table
[i
].fi_uname
);
955 entry
->pl_width
= strlen(entry
->pl_user_prop
);
956 entry
->pl_all
= B_TRUE
;
959 last
= &entry
->pl_next
;
963 /* add any unsupported features */
964 for (nvp
= nvlist_next_nvpair(features
, NULL
);
965 nvp
!= NULL
; nvp
= nvlist_next_nvpair(features
, nvp
)) {
969 if (zfeature_is_supported(nvpair_name(nvp
)))
972 propname
= zfs_asprintf(hdl
, "unsupported@%s",
976 * Before adding the property to the list make sure that no
977 * other pool already added the same property.
981 while (entry
!= NULL
) {
982 if (entry
->pl_user_prop
!= NULL
&&
983 strcmp(propname
, entry
->pl_user_prop
) == 0) {
987 entry
= entry
->pl_next
;
994 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
995 entry
->pl_prop
= ZPROP_USERPROP
;
996 entry
->pl_user_prop
= propname
;
997 entry
->pl_width
= strlen(entry
->pl_user_prop
);
998 entry
->pl_all
= B_TRUE
;
1001 last
= &entry
->pl_next
;
1004 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
1005 if (entry
->pl_fixed
&& !literal
)
1008 if (entry
->pl_prop
!= ZPROP_USERPROP
&&
1009 zpool_get_prop(zhp
, entry
->pl_prop
, buf
, sizeof (buf
),
1010 NULL
, literal
) == 0) {
1011 if (strlen(buf
) > entry
->pl_width
)
1012 entry
->pl_width
= strlen(buf
);
1013 } else if (entry
->pl_prop
== ZPROP_INVAL
&&
1014 zfs_prop_user(entry
->pl_user_prop
) &&
1015 zpool_get_userprop(zhp
, entry
->pl_user_prop
, buf
,
1016 sizeof (buf
), NULL
) == 0) {
1017 if (strlen(buf
) > entry
->pl_width
)
1018 entry
->pl_width
= strlen(buf
);
1026 vdev_expand_proplist(zpool_handle_t
*zhp
, const char *vdevname
,
1029 zprop_list_t
*entry
;
1030 char buf
[ZFS_MAXPROPLEN
];
1031 const char *strval
= NULL
;
1033 nvpair_t
*elem
= NULL
;
1034 nvlist_t
*vprops
= NULL
;
1035 nvlist_t
*propval
= NULL
;
1036 const char *propname
;
1038 zprop_list_t
**last
;
1040 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
1041 if (entry
->pl_fixed
)
1044 if (zpool_get_vdev_prop(zhp
, vdevname
, entry
->pl_prop
,
1045 entry
->pl_user_prop
, buf
, sizeof (buf
), NULL
,
1047 if (strlen(buf
) > entry
->pl_width
)
1048 entry
->pl_width
= strlen(buf
);
1050 if (entry
->pl_prop
== VDEV_PROP_NAME
&&
1051 strlen(vdevname
) > entry
->pl_width
)
1052 entry
->pl_width
= strlen(vdevname
);
1055 /* Handle the all properties case */
1057 if (*last
!= NULL
&& (*last
)->pl_all
== B_TRUE
) {
1058 while (*last
!= NULL
)
1059 last
= &(*last
)->pl_next
;
1061 err
= zpool_get_all_vdev_props(zhp
, vdevname
, &vprops
);
1065 while ((elem
= nvlist_next_nvpair(vprops
, elem
)) != NULL
) {
1066 propname
= nvpair_name(elem
);
1068 /* Skip properties that are not user defined */
1069 if ((prop
= vdev_name_to_prop(propname
)) !=
1073 if (nvpair_value_nvlist(elem
, &propval
) != 0)
1076 strval
= fnvlist_lookup_string(propval
, ZPROP_VALUE
);
1078 entry
= zfs_alloc(zhp
->zpool_hdl
,
1079 sizeof (zprop_list_t
));
1080 entry
->pl_prop
= prop
;
1081 entry
->pl_user_prop
= zfs_strdup(zhp
->zpool_hdl
,
1083 entry
->pl_width
= strlen(strval
);
1084 entry
->pl_all
= B_TRUE
;
1086 last
= &entry
->pl_next
;
1094 * Get the state for the given feature on the given ZFS pool.
1097 zpool_prop_get_feature(zpool_handle_t
*zhp
, const char *propname
, char *buf
,
1101 boolean_t found
= B_FALSE
;
1102 nvlist_t
*features
= zpool_get_features(zhp
);
1103 boolean_t supported
;
1104 const char *feature
= strchr(propname
, '@') + 1;
1106 supported
= zpool_prop_feature(propname
);
1107 ASSERT(supported
|| zpool_prop_unsupported(propname
));
1110 * Convert from feature name to feature guid. This conversion is
1111 * unnecessary for unsupported@... properties because they already
1118 ret
= zfeature_lookup_name(feature
, &fid
);
1120 (void) strlcpy(buf
, "-", len
);
1123 feature
= spa_feature_table
[fid
].fi_guid
;
1126 if (nvlist_lookup_uint64(features
, feature
, &refcount
) == 0)
1131 (void) strlcpy(buf
, ZFS_FEATURE_DISABLED
, len
);
1134 (void) strlcpy(buf
, ZFS_FEATURE_ENABLED
, len
);
1136 (void) strlcpy(buf
, ZFS_FEATURE_ACTIVE
, len
);
1140 if (refcount
== 0) {
1141 (void) strcpy(buf
, ZFS_UNSUPPORTED_INACTIVE
);
1143 (void) strcpy(buf
, ZFS_UNSUPPORTED_READONLY
);
1146 (void) strlcpy(buf
, "-", len
);
1155 * Validate the given pool name, optionally putting an extended error message in
1159 zpool_name_valid(libzfs_handle_t
*hdl
, boolean_t isopen
, const char *pool
)
1161 namecheck_err_t why
;
1165 ret
= pool_namecheck(pool
, &why
, &what
);
1168 * The rules for reserved pool names were extended at a later point.
1169 * But we need to support users with existing pools that may now be
1170 * invalid. So we only check for this expanded set of names during a
1171 * create (or import), and only in userland.
1173 if (ret
== 0 && !isopen
&&
1174 (strncmp(pool
, "mirror", 6) == 0 ||
1175 strncmp(pool
, "raidz", 5) == 0 ||
1176 strncmp(pool
, "draid", 5) == 0 ||
1177 strncmp(pool
, "spare", 5) == 0 ||
1178 strcmp(pool
, "log") == 0)) {
1181 dgettext(TEXT_DOMAIN
, "name is reserved"));
1189 case NAME_ERR_TOOLONG
:
1191 dgettext(TEXT_DOMAIN
, "name is too long"));
1194 case NAME_ERR_INVALCHAR
:
1196 dgettext(TEXT_DOMAIN
, "invalid character "
1197 "'%c' in pool name"), what
);
1200 case NAME_ERR_NOLETTER
:
1201 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1202 "name must begin with a letter"));
1205 case NAME_ERR_RESERVED
:
1206 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1207 "name is reserved"));
1210 case NAME_ERR_DISKLIKE
:
1211 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1212 "pool name is reserved"));
1215 case NAME_ERR_LEADING_SLASH
:
1216 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1217 "leading slash in name"));
1220 case NAME_ERR_EMPTY_COMPONENT
:
1221 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1222 "empty component in name"));
1225 case NAME_ERR_TRAILING_SLASH
:
1226 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1227 "trailing slash in name"));
1230 case NAME_ERR_MULTIPLE_DELIMITERS
:
1231 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1232 "multiple '@' and/or '#' delimiters in "
1236 case NAME_ERR_NO_AT
:
1237 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1238 "permission set is missing '@'"));
1242 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1243 "(%d) not defined"), why
);
1254 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1258 zpool_open_canfail(libzfs_handle_t
*hdl
, const char *pool
)
1260 zpool_handle_t
*zhp
;
1264 * Make sure the pool name is valid.
1266 if (!zpool_name_valid(hdl
, B_TRUE
, pool
)) {
1267 (void) zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1268 dgettext(TEXT_DOMAIN
, "cannot open '%s'"),
1273 zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
));
1275 zhp
->zpool_hdl
= hdl
;
1276 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1278 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1284 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "no such pool"));
1285 (void) zfs_error_fmt(hdl
, EZFS_NOENT
,
1286 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), pool
);
1295 * Like the above, but silent on error. Used when iterating over pools (because
1296 * the configuration cache may be out of date).
1299 zpool_open_silent(libzfs_handle_t
*hdl
, const char *pool
, zpool_handle_t
**ret
)
1301 zpool_handle_t
*zhp
;
1304 zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
));
1306 zhp
->zpool_hdl
= hdl
;
1307 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1309 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1325 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1329 zpool_open(libzfs_handle_t
*hdl
, const char *pool
)
1331 zpool_handle_t
*zhp
;
1333 if ((zhp
= zpool_open_canfail(hdl
, pool
)) == NULL
)
1336 if (zhp
->zpool_state
== POOL_STATE_UNAVAIL
) {
1337 (void) zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
1338 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), zhp
->zpool_name
);
1347 * Close the handle. Simply frees the memory associated with the handle.
1350 zpool_close(zpool_handle_t
*zhp
)
1352 nvlist_free(zhp
->zpool_config
);
1353 nvlist_free(zhp
->zpool_old_config
);
1354 nvlist_free(zhp
->zpool_props
);
1359 * Return the name of the pool.
1362 zpool_get_name(zpool_handle_t
*zhp
)
1364 return (zhp
->zpool_name
);
1369 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1372 zpool_get_state(zpool_handle_t
*zhp
)
1374 return (zhp
->zpool_state
);
1378 * Check if vdev list contains a special vdev
1381 zpool_has_special_vdev(nvlist_t
*nvroot
)
1386 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
, &child
,
1388 for (uint_t c
= 0; c
< children
; c
++) {
1391 if (nvlist_lookup_string(child
[c
],
1392 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
) == 0 &&
1393 strcmp(bias
, VDEV_ALLOC_BIAS_SPECIAL
) == 0) {
1402 * Check if vdev list contains a dRAID vdev
1405 zpool_has_draid_vdev(nvlist_t
*nvroot
)
1410 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
1411 &child
, &children
) == 0) {
1412 for (uint_t c
= 0; c
< children
; c
++) {
1415 if (nvlist_lookup_string(child
[c
],
1416 ZPOOL_CONFIG_TYPE
, &type
) == 0 &&
1417 strcmp(type
, VDEV_TYPE_DRAID
) == 0) {
1426 * Output a dRAID top-level vdev name in to the provided buffer.
1429 zpool_draid_name(char *name
, int len
, uint64_t data
, uint64_t parity
,
1430 uint64_t spares
, uint64_t children
)
1432 snprintf(name
, len
, "%s%llu:%llud:%lluc:%llus",
1433 VDEV_TYPE_DRAID
, (u_longlong_t
)parity
, (u_longlong_t
)data
,
1434 (u_longlong_t
)children
, (u_longlong_t
)spares
);
1440 * Return B_TRUE if the provided name is a dRAID spare name.
1443 zpool_is_draid_spare(const char *name
)
1445 uint64_t spare_id
, parity
, vdev_id
;
1447 if (sscanf(name
, VDEV_TYPE_DRAID
"%llu-%llu-%llu",
1448 (u_longlong_t
*)&parity
, (u_longlong_t
*)&vdev_id
,
1449 (u_longlong_t
*)&spare_id
) == 3) {
1457 * Create the named pool, using the provided vdev list. It is assumed
1458 * that the consumer has already validated the contents of the nvlist, so we
1459 * don't have to worry about error semantics.
1462 zpool_create(libzfs_handle_t
*hdl
, const char *pool
, nvlist_t
*nvroot
,
1463 nvlist_t
*props
, nvlist_t
*fsprops
)
1465 zfs_cmd_t zc
= {"\0"};
1466 nvlist_t
*zc_fsprops
= NULL
;
1467 nvlist_t
*zc_props
= NULL
;
1468 nvlist_t
*hidden_args
= NULL
;
1469 uint8_t *wkeydata
= NULL
;
1471 char errbuf
[ERRBUFLEN
];
1474 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1475 "cannot create '%s'"), pool
);
1477 if (!zpool_name_valid(hdl
, B_FALSE
, pool
))
1478 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
1480 zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
);
1483 prop_flags_t flags
= { .create
= B_TRUE
, .import
= B_FALSE
};
1485 if ((zc_props
= zpool_valid_proplist(hdl
, pool
, props
,
1486 SPA_VERSION_1
, flags
, errbuf
)) == NULL
) {
1493 const char *zonestr
;
1495 zoned
= ((nvlist_lookup_string(fsprops
,
1496 zfs_prop_to_name(ZFS_PROP_ZONED
), &zonestr
) == 0) &&
1497 strcmp(zonestr
, "on") == 0);
1499 if ((zc_fsprops
= zfs_valid_proplist(hdl
, ZFS_TYPE_FILESYSTEM
,
1500 fsprops
, zoned
, NULL
, NULL
, B_TRUE
, errbuf
)) == NULL
) {
1504 if (nvlist_exists(zc_fsprops
,
1505 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS
)) &&
1506 !zpool_has_special_vdev(nvroot
)) {
1507 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1508 "%s property requires a special vdev"),
1509 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS
));
1510 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1515 (nvlist_alloc(&zc_props
, NV_UNIQUE_NAME
, 0) != 0)) {
1518 if (zfs_crypto_create(hdl
, NULL
, zc_fsprops
, props
, B_TRUE
,
1519 &wkeydata
, &wkeylen
) != 0) {
1520 zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
);
1523 if (nvlist_add_nvlist(zc_props
,
1524 ZPOOL_ROOTFS_PROPS
, zc_fsprops
) != 0) {
1527 if (wkeydata
!= NULL
) {
1528 if (nvlist_alloc(&hidden_args
, NV_UNIQUE_NAME
, 0) != 0)
1531 if (nvlist_add_uint8_array(hidden_args
, "wkeydata",
1532 wkeydata
, wkeylen
) != 0)
1535 if (nvlist_add_nvlist(zc_props
, ZPOOL_HIDDEN_ARGS
,
1542 zcmd_write_src_nvlist(hdl
, &zc
, zc_props
);
1544 (void) strlcpy(zc
.zc_name
, pool
, sizeof (zc
.zc_name
));
1546 if ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_CREATE
, &zc
)) != 0) {
1548 zcmd_free_nvlists(&zc
);
1549 nvlist_free(zc_props
);
1550 nvlist_free(zc_fsprops
);
1551 nvlist_free(hidden_args
);
1552 if (wkeydata
!= NULL
)
1558 * This can happen if the user has specified the same
1559 * device multiple times. We can't reliably detect this
1560 * until we try to add it and see we already have a
1561 * label. This can also happen under if the device is
1562 * part of an active md or lvm device.
1564 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1565 "one or more vdevs refer to the same device, or "
1566 "one of\nthe devices is part of an active md or "
1568 return (zfs_error(hdl
, EZFS_BADDEV
, errbuf
));
1572 * This happens if the record size is smaller or larger
1573 * than the allowed size range, or not a power of 2.
1575 * NOTE: although zfs_valid_proplist is called earlier,
1576 * this case may have slipped through since the
1577 * pool does not exist yet and it is therefore
1578 * impossible to read properties e.g. max blocksize
1581 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1582 "record size invalid"));
1583 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1587 * This occurs when one of the devices is below
1588 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1589 * device was the problem device since there's no
1590 * reliable way to determine device size from userland.
1595 zfs_nicebytes(SPA_MINDEVSIZE
, buf
,
1598 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1599 "one or more devices is less than the "
1600 "minimum size (%s)"), buf
);
1602 return (zfs_error(hdl
, EZFS_BADDEV
, errbuf
));
1605 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1606 "one or more devices is out of space"));
1607 return (zfs_error(hdl
, EZFS_BADDEV
, errbuf
));
1610 if (zpool_has_draid_vdev(nvroot
) &&
1611 zfeature_lookup_name("draid", NULL
) != 0) {
1612 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1613 "dRAID vdevs are unsupported by the "
1615 return (zfs_error(hdl
, EZFS_BADDEV
, errbuf
));
1617 return (zpool_standard_error(hdl
, errno
,
1622 return (zpool_standard_error(hdl
, errno
, errbuf
));
1627 zcmd_free_nvlists(&zc
);
1628 nvlist_free(zc_props
);
1629 nvlist_free(zc_fsprops
);
1630 nvlist_free(hidden_args
);
1631 if (wkeydata
!= NULL
)
1637 * Destroy the given pool. It is up to the caller to ensure that there are no
1638 * datasets left in the pool.
1641 zpool_destroy(zpool_handle_t
*zhp
, const char *log_str
)
1643 zfs_cmd_t zc
= {"\0"};
1644 zfs_handle_t
*zfp
= NULL
;
1645 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1646 char errbuf
[ERRBUFLEN
];
1648 if (zhp
->zpool_state
== POOL_STATE_ACTIVE
&&
1649 (zfp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
1652 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1653 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1655 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_DESTROY
, &zc
) != 0) {
1656 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1657 "cannot destroy '%s'"), zhp
->zpool_name
);
1659 if (errno
== EROFS
) {
1660 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1661 "one or more devices is read only"));
1662 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
1664 (void) zpool_standard_error(hdl
, errno
, errbuf
);
1673 remove_mountpoint(zfp
);
1681 * Create a checkpoint in the given pool.
1684 zpool_checkpoint(zpool_handle_t
*zhp
)
1686 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1687 char errbuf
[ERRBUFLEN
];
1690 error
= lzc_pool_checkpoint(zhp
->zpool_name
);
1692 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1693 "cannot checkpoint '%s'"), zhp
->zpool_name
);
1694 (void) zpool_standard_error(hdl
, error
, errbuf
);
1702 * Discard the checkpoint from the given pool.
1705 zpool_discard_checkpoint(zpool_handle_t
*zhp
)
1707 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1708 char errbuf
[ERRBUFLEN
];
1711 error
= lzc_pool_checkpoint_discard(zhp
->zpool_name
);
1713 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1714 "cannot discard checkpoint in '%s'"), zhp
->zpool_name
);
1715 (void) zpool_standard_error(hdl
, error
, errbuf
);
1723 * Add the given vdevs to the pool. The caller must have already performed the
1724 * necessary verification to ensure that the vdev specification is well-formed.
1727 zpool_add(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
1729 zfs_cmd_t zc
= {"\0"};
1731 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1732 char errbuf
[ERRBUFLEN
];
1733 nvlist_t
**spares
, **l2cache
;
1734 uint_t nspares
, nl2cache
;
1736 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1737 "cannot add to '%s'"), zhp
->zpool_name
);
1739 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1740 SPA_VERSION_SPARES
&&
1741 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1742 &spares
, &nspares
) == 0) {
1743 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1744 "upgraded to add hot spares"));
1745 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
1748 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1749 SPA_VERSION_L2CACHE
&&
1750 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1751 &l2cache
, &nl2cache
) == 0) {
1752 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1753 "upgraded to add cache devices"));
1754 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
1757 zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
);
1758 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1760 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_ADD
, &zc
) != 0) {
1764 * This can happen if the user has specified the same
1765 * device multiple times. We can't reliably detect this
1766 * until we try to add it and see we already have a
1769 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1770 "one or more vdevs refer to the same device"));
1771 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
1776 if (zpool_has_draid_vdev(nvroot
) &&
1777 zfeature_lookup_name("draid", NULL
) != 0) {
1778 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1779 "dRAID vdevs are unsupported by the "
1782 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1783 "invalid config; a pool with removing/"
1784 "removed vdevs does not support adding "
1785 "raidz or dRAID vdevs"));
1788 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
1793 * This occurs when one of the devices is below
1794 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1795 * device was the problem device since there's no
1796 * reliable way to determine device size from userland.
1801 zfs_nicebytes(SPA_MINDEVSIZE
, buf
,
1804 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1805 "device is less than the minimum "
1808 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
1812 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1813 "pool must be upgraded to add these vdevs"));
1814 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1818 (void) zpool_standard_error(hdl
, errno
, errbuf
);
1826 zcmd_free_nvlists(&zc
);
1832 * Exports the pool from the system. The caller must ensure that there are no
1833 * mounted datasets in the pool.
1836 zpool_export_common(zpool_handle_t
*zhp
, boolean_t force
, boolean_t hardforce
,
1837 const char *log_str
)
1839 zfs_cmd_t zc
= {"\0"};
1841 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1842 zc
.zc_cookie
= force
;
1843 zc
.zc_guid
= hardforce
;
1844 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1846 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_EXPORT
, &zc
) != 0) {
1849 zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
1850 "use '-f' to override the following errors:\n"
1851 "'%s' has an active shared spare which could be"
1852 " used by other pools once '%s' is exported."),
1853 zhp
->zpool_name
, zhp
->zpool_name
);
1854 return (zfs_error_fmt(zhp
->zpool_hdl
, EZFS_ACTIVE_SPARE
,
1855 dgettext(TEXT_DOMAIN
, "cannot export '%s'"),
1858 return (zpool_standard_error_fmt(zhp
->zpool_hdl
, errno
,
1859 dgettext(TEXT_DOMAIN
, "cannot export '%s'"),
1868 zpool_export(zpool_handle_t
*zhp
, boolean_t force
, const char *log_str
)
1870 return (zpool_export_common(zhp
, force
, B_FALSE
, log_str
));
1874 zpool_export_force(zpool_handle_t
*zhp
, const char *log_str
)
1876 return (zpool_export_common(zhp
, B_TRUE
, B_TRUE
, log_str
));
1880 zpool_rewind_exclaim(libzfs_handle_t
*hdl
, const char *name
, boolean_t dryrun
,
1883 nvlist_t
*nv
= NULL
;
1889 if (!hdl
->libzfs_printerr
|| config
== NULL
)
1892 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1893 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0) {
1897 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1899 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1901 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1902 strftime(timestr
, 128, "%c", &t
) != 0) {
1904 (void) printf(dgettext(TEXT_DOMAIN
,
1905 "Would be able to return %s "
1906 "to its state as of %s.\n"),
1909 (void) printf(dgettext(TEXT_DOMAIN
,
1910 "Pool %s returned to its state as of %s.\n"),
1914 (void) printf(dgettext(TEXT_DOMAIN
,
1915 "%s approximately %lld "),
1916 dryrun
? "Would discard" : "Discarded",
1917 ((longlong_t
)loss
+ 30) / 60);
1918 (void) printf(dgettext(TEXT_DOMAIN
,
1919 "minutes of transactions.\n"));
1920 } else if (loss
> 0) {
1921 (void) printf(dgettext(TEXT_DOMAIN
,
1922 "%s approximately %lld "),
1923 dryrun
? "Would discard" : "Discarded",
1925 (void) printf(dgettext(TEXT_DOMAIN
,
1926 "seconds of transactions.\n"));
1932 zpool_explain_recover(libzfs_handle_t
*hdl
, const char *name
, int reason
,
1935 nvlist_t
*nv
= NULL
;
1937 uint64_t edata
= UINT64_MAX
;
1942 if (!hdl
->libzfs_printerr
)
1946 (void) printf(dgettext(TEXT_DOMAIN
, "action: "));
1948 (void) printf(dgettext(TEXT_DOMAIN
, "\t"));
1950 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1951 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1952 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0 ||
1953 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1956 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1957 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_DATA_ERRORS
,
1960 (void) printf(dgettext(TEXT_DOMAIN
,
1961 "Recovery is possible, but will result in some data loss.\n"));
1963 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1964 strftime(timestr
, 128, "%c", &t
) != 0) {
1965 (void) printf(dgettext(TEXT_DOMAIN
,
1966 "\tReturning the pool to its state as of %s\n"
1967 "\tshould correct the problem. "),
1970 (void) printf(dgettext(TEXT_DOMAIN
,
1971 "\tReverting the pool to an earlier state "
1972 "should correct the problem.\n\t"));
1976 (void) printf(dgettext(TEXT_DOMAIN
,
1977 "Approximately %lld minutes of data\n"
1978 "\tmust be discarded, irreversibly. "),
1979 ((longlong_t
)loss
+ 30) / 60);
1980 } else if (loss
> 0) {
1981 (void) printf(dgettext(TEXT_DOMAIN
,
1982 "Approximately %lld seconds of data\n"
1983 "\tmust be discarded, irreversibly. "),
1986 if (edata
!= 0 && edata
!= UINT64_MAX
) {
1988 (void) printf(dgettext(TEXT_DOMAIN
,
1989 "After rewind, at least\n"
1990 "\tone persistent user-data error will remain. "));
1992 (void) printf(dgettext(TEXT_DOMAIN
,
1993 "After rewind, several\n"
1994 "\tpersistent user-data errors will remain. "));
1997 (void) printf(dgettext(TEXT_DOMAIN
,
1998 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1999 reason
>= 0 ? "clear" : "import", name
);
2001 (void) printf(dgettext(TEXT_DOMAIN
,
2002 "A scrub of the pool\n"
2003 "\tis strongly recommended after recovery.\n"));
2007 (void) printf(dgettext(TEXT_DOMAIN
,
2008 "Destroy and re-create the pool from\n\ta backup source.\n"));
2012 * zpool_import() is a contracted interface. Should be kept the same
2015 * Applications should use zpool_import_props() to import a pool with
2016 * new properties value to be set.
2019 zpool_import(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
2022 nvlist_t
*props
= NULL
;
2025 if (altroot
!= NULL
) {
2026 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
2027 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
2028 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
2032 if (nvlist_add_string(props
,
2033 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), altroot
) != 0 ||
2034 nvlist_add_string(props
,
2035 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
), "none") != 0) {
2037 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
2038 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
2043 ret
= zpool_import_props(hdl
, config
, newname
, props
,
2050 print_vdev_tree(libzfs_handle_t
*hdl
, const char *name
, nvlist_t
*nv
,
2056 uint64_t is_log
= 0;
2058 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
,
2062 (void) printf("\t%*s%s%s\n", indent
, "", name
,
2063 is_log
? " [log]" : "");
2065 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2066 &child
, &children
) != 0)
2069 for (c
= 0; c
< children
; c
++) {
2070 vname
= zpool_vdev_name(hdl
, NULL
, child
[c
], VDEV_NAME_TYPE_ID
);
2071 print_vdev_tree(hdl
, vname
, child
[c
], indent
+ 2);
2077 zpool_print_unsup_feat(nvlist_t
*config
)
2079 nvlist_t
*nvinfo
, *unsup_feat
;
2081 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2082 unsup_feat
= fnvlist_lookup_nvlist(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
);
2084 for (nvpair_t
*nvp
= nvlist_next_nvpair(unsup_feat
, NULL
);
2085 nvp
!= NULL
; nvp
= nvlist_next_nvpair(unsup_feat
, nvp
)) {
2086 const char *desc
= fnvpair_value_string(nvp
);
2087 if (strlen(desc
) > 0)
2088 (void) printf("\t%s (%s)\n", nvpair_name(nvp
), desc
);
2090 (void) printf("\t%s\n", nvpair_name(nvp
));
2095 * Import the given pool using the known configuration and a list of
2096 * properties to be set. The configuration should have come from
2097 * zpool_find_import(). The 'newname' parameters control whether the pool
2098 * is imported with a different name.
2101 zpool_import_props(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
2102 nvlist_t
*props
, int flags
)
2104 zfs_cmd_t zc
= {"\0"};
2105 zpool_load_policy_t policy
;
2106 nvlist_t
*nv
= NULL
;
2107 nvlist_t
*nvinfo
= NULL
;
2108 nvlist_t
*missing
= NULL
;
2109 const char *thename
;
2110 const char *origname
;
2113 char errbuf
[ERRBUFLEN
];
2115 origname
= fnvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
);
2117 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2118 "cannot import pool '%s'"), origname
);
2120 if (newname
!= NULL
) {
2121 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
2122 return (zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
2123 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
2130 if (props
!= NULL
) {
2132 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
2134 version
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
2136 if ((props
= zpool_valid_proplist(hdl
, origname
,
2137 props
, version
, flags
, errbuf
)) == NULL
)
2139 zcmd_write_src_nvlist(hdl
, &zc
, props
);
2143 (void) strlcpy(zc
.zc_name
, thename
, sizeof (zc
.zc_name
));
2145 zc
.zc_guid
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
);
2147 zcmd_write_conf_nvlist(hdl
, &zc
, config
);
2148 zcmd_alloc_dst_nvlist(hdl
, &zc
, zc
.zc_nvlist_conf_size
* 2);
2150 zc
.zc_cookie
= flags
;
2151 while ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_IMPORT
, &zc
)) != 0 &&
2153 zcmd_expand_dst_nvlist(hdl
, &zc
);
2157 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nv
);
2159 zcmd_free_nvlists(&zc
);
2161 zpool_get_load_policy(config
, &policy
);
2168 * Dry-run failed, but we print out what success
2169 * looks like if we found a best txg
2171 if (policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) {
2172 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
2178 if (newname
== NULL
)
2179 (void) snprintf(desc
, sizeof (desc
),
2180 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
2183 (void) snprintf(desc
, sizeof (desc
),
2184 dgettext(TEXT_DOMAIN
, "cannot import '%s' as '%s'"),
2189 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
2190 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
2191 nvlist_exists(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
)) {
2192 (void) printf(dgettext(TEXT_DOMAIN
, "This "
2193 "pool uses the following feature(s) not "
2194 "supported by this system:\n"));
2195 zpool_print_unsup_feat(nv
);
2196 if (nvlist_exists(nvinfo
,
2197 ZPOOL_CONFIG_CAN_RDONLY
)) {
2198 (void) printf(dgettext(TEXT_DOMAIN
,
2199 "All unsupported features are only "
2200 "required for writing to the pool."
2201 "\nThe pool can be imported using "
2202 "'-o readonly=on'.\n"));
2206 * Unsupported version.
2208 (void) zfs_error(hdl
, EZFS_BADVERSION
, desc
);
2212 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
2213 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0) {
2214 const char *hostname
= "<unknown>";
2215 uint64_t hostid
= 0;
2216 mmp_state_t mmp_state
;
2218 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
2219 ZPOOL_CONFIG_MMP_STATE
);
2221 if (nvlist_exists(nvinfo
,
2222 ZPOOL_CONFIG_MMP_HOSTNAME
))
2223 hostname
= fnvlist_lookup_string(nvinfo
,
2224 ZPOOL_CONFIG_MMP_HOSTNAME
);
2226 if (nvlist_exists(nvinfo
,
2227 ZPOOL_CONFIG_MMP_HOSTID
))
2228 hostid
= fnvlist_lookup_uint64(nvinfo
,
2229 ZPOOL_CONFIG_MMP_HOSTID
);
2231 if (mmp_state
== MMP_STATE_ACTIVE
) {
2232 (void) snprintf(aux
, sizeof (aux
),
2233 dgettext(TEXT_DOMAIN
, "pool is imp"
2234 "orted on host '%s' (hostid=%lx).\n"
2235 "Export the pool on the other "
2236 "system, then run 'zpool import'."),
2237 hostname
, (unsigned long) hostid
);
2238 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
2239 (void) snprintf(aux
, sizeof (aux
),
2240 dgettext(TEXT_DOMAIN
, "pool has "
2241 "the multihost property on and "
2242 "the\nsystem's hostid is not set. "
2243 "Set a unique system hostid with "
2244 "the zgenhostid(8) command.\n"));
2247 (void) zfs_error_aux(hdl
, "%s", aux
);
2249 (void) zfs_error(hdl
, EZFS_ACTIVE_POOL
, desc
);
2253 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, desc
);
2257 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2258 "one or more devices is read only"));
2259 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
2263 if (nv
&& nvlist_lookup_nvlist(nv
,
2264 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
2265 nvlist_lookup_nvlist(nvinfo
,
2266 ZPOOL_CONFIG_MISSING_DEVICES
, &missing
) == 0) {
2267 (void) printf(dgettext(TEXT_DOMAIN
,
2268 "The devices below are missing or "
2269 "corrupted, use '-m' to import the pool "
2271 print_vdev_tree(hdl
, NULL
, missing
, 2);
2272 (void) printf("\n");
2274 (void) zpool_standard_error(hdl
, error
, desc
);
2278 (void) zpool_standard_error(hdl
, error
, desc
);
2282 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2283 "one or more devices are already in use\n"));
2284 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
2287 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2288 "new name of at least one dataset is longer than "
2289 "the maximum allowable length"));
2290 (void) zfs_error(hdl
, EZFS_NAMETOOLONG
, desc
);
2293 (void) zpool_standard_error(hdl
, error
, desc
);
2294 zpool_explain_recover(hdl
,
2295 newname
? origname
: thename
, -error
, nv
);
2302 zpool_handle_t
*zhp
;
2305 * This should never fail, but play it safe anyway.
2307 if (zpool_open_silent(hdl
, thename
, &zhp
) != 0)
2309 else if (zhp
!= NULL
)
2311 if (policy
.zlp_rewind
&
2312 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
2313 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
2314 ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) != 0), nv
);
2323 * Translate vdev names to guids. If a vdev_path is determined to be
2324 * unsuitable then a vd_errlist is allocated and the vdev path and errno
2328 zpool_translate_vdev_guids(zpool_handle_t
*zhp
, nvlist_t
*vds
,
2329 nvlist_t
*vdev_guids
, nvlist_t
*guids_to_paths
, nvlist_t
**vd_errlist
)
2331 nvlist_t
*errlist
= NULL
;
2334 for (nvpair_t
*elem
= nvlist_next_nvpair(vds
, NULL
); elem
!= NULL
;
2335 elem
= nvlist_next_nvpair(vds
, elem
)) {
2336 boolean_t spare
, cache
;
2338 const char *vd_path
= nvpair_name(elem
);
2339 nvlist_t
*tgt
= zpool_find_vdev(zhp
, vd_path
, &spare
, &cache
,
2342 if ((tgt
== NULL
) || cache
|| spare
) {
2343 if (errlist
== NULL
) {
2344 errlist
= fnvlist_alloc();
2348 uint64_t err
= (tgt
== NULL
) ? EZFS_NODEVICE
:
2349 (spare
? EZFS_ISSPARE
: EZFS_ISL2CACHE
);
2350 fnvlist_add_int64(errlist
, vd_path
, err
);
2354 uint64_t guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
2355 fnvlist_add_uint64(vdev_guids
, vd_path
, guid
);
2357 char msg
[MAXNAMELEN
];
2358 (void) snprintf(msg
, sizeof (msg
), "%llu", (u_longlong_t
)guid
);
2359 fnvlist_add_string(guids_to_paths
, msg
, vd_path
);
2363 verify(errlist
!= NULL
);
2364 if (vd_errlist
!= NULL
)
2365 *vd_errlist
= errlist
;
2367 fnvlist_free(errlist
);
2374 xlate_init_err(int err
)
2378 return (EZFS_NODEVICE
);
2381 return (EZFS_BADDEV
);
2383 return (EZFS_INITIALIZING
);
2385 return (EZFS_NO_INITIALIZE
);
2391 * Begin, suspend, cancel, or uninit (clear) the initialization (initializing
2392 * of all free blocks) for the given vdevs in the given pool.
2395 zpool_initialize_impl(zpool_handle_t
*zhp
, pool_initialize_func_t cmd_type
,
2396 nvlist_t
*vds
, boolean_t wait
)
2400 nvlist_t
*vdev_guids
= fnvlist_alloc();
2401 nvlist_t
*guids_to_paths
= fnvlist_alloc();
2402 nvlist_t
*vd_errlist
= NULL
;
2406 err
= zpool_translate_vdev_guids(zhp
, vds
, vdev_guids
,
2407 guids_to_paths
, &vd_errlist
);
2410 verify(vd_errlist
!= NULL
);
2414 err
= lzc_initialize(zhp
->zpool_name
, cmd_type
,
2415 vdev_guids
, &errlist
);
2418 if (errlist
!= NULL
&& nvlist_lookup_nvlist(errlist
,
2419 ZPOOL_INITIALIZE_VDEVS
, &vd_errlist
) == 0) {
2423 if (err
== EINVAL
&& cmd_type
== POOL_INITIALIZE_UNINIT
) {
2424 zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
2425 "uninitialize is not supported by kernel"));
2428 (void) zpool_standard_error(zhp
->zpool_hdl
, err
,
2429 dgettext(TEXT_DOMAIN
, "operation failed"));
2434 for (elem
= nvlist_next_nvpair(vdev_guids
, NULL
); elem
!= NULL
;
2435 elem
= nvlist_next_nvpair(vdev_guids
, elem
)) {
2437 uint64_t guid
= fnvpair_value_uint64(elem
);
2439 err
= lzc_wait_tag(zhp
->zpool_name
,
2440 ZPOOL_WAIT_INITIALIZE
, guid
, NULL
);
2442 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
,
2443 err
, dgettext(TEXT_DOMAIN
, "error "
2444 "waiting for '%s' to initialize"),
2454 for (elem
= nvlist_next_nvpair(vd_errlist
, NULL
); elem
!= NULL
;
2455 elem
= nvlist_next_nvpair(vd_errlist
, elem
)) {
2456 int64_t vd_error
= xlate_init_err(fnvpair_value_int64(elem
));
2459 if (nvlist_lookup_string(guids_to_paths
, nvpair_name(elem
),
2461 path
= nvpair_name(elem
);
2463 (void) zfs_error_fmt(zhp
->zpool_hdl
, vd_error
,
2464 "cannot initialize '%s'", path
);
2468 fnvlist_free(vdev_guids
);
2469 fnvlist_free(guids_to_paths
);
2471 if (vd_errlist
!= NULL
)
2472 fnvlist_free(vd_errlist
);
2474 return (err
== 0 ? 0 : -1);
2478 zpool_initialize(zpool_handle_t
*zhp
, pool_initialize_func_t cmd_type
,
2481 return (zpool_initialize_impl(zhp
, cmd_type
, vds
, B_FALSE
));
2485 zpool_initialize_wait(zpool_handle_t
*zhp
, pool_initialize_func_t cmd_type
,
2488 return (zpool_initialize_impl(zhp
, cmd_type
, vds
, B_TRUE
));
2492 xlate_trim_err(int err
)
2496 return (EZFS_NODEVICE
);
2499 return (EZFS_BADDEV
);
2501 return (EZFS_TRIMMING
);
2503 return (EZFS_NO_TRIM
);
2505 return (EZFS_TRIM_NOTSUP
);
2511 zpool_trim_wait(zpool_handle_t
*zhp
, nvlist_t
*vdev_guids
)
2516 for (elem
= nvlist_next_nvpair(vdev_guids
, NULL
); elem
!= NULL
;
2517 elem
= nvlist_next_nvpair(vdev_guids
, elem
)) {
2519 uint64_t guid
= fnvpair_value_uint64(elem
);
2521 err
= lzc_wait_tag(zhp
->zpool_name
,
2522 ZPOOL_WAIT_TRIM
, guid
, NULL
);
2524 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
,
2525 err
, dgettext(TEXT_DOMAIN
, "error "
2526 "waiting to trim '%s'"), nvpair_name(elem
));
2535 * Check errlist and report any errors, omitting ones which should be
2536 * suppressed. Returns B_TRUE if any errors were reported.
2539 check_trim_errs(zpool_handle_t
*zhp
, trimflags_t
*trim_flags
,
2540 nvlist_t
*guids_to_paths
, nvlist_t
*vds
, nvlist_t
*errlist
)
2543 boolean_t reported_errs
= B_FALSE
;
2545 int num_suppressed_errs
= 0;
2547 for (elem
= nvlist_next_nvpair(vds
, NULL
);
2548 elem
!= NULL
; elem
= nvlist_next_nvpair(vds
, elem
)) {
2552 for (elem
= nvlist_next_nvpair(errlist
, NULL
);
2553 elem
!= NULL
; elem
= nvlist_next_nvpair(errlist
, elem
)) {
2554 int64_t vd_error
= xlate_trim_err(fnvpair_value_int64(elem
));
2558 * If only the pool was specified, and it was not a secure
2559 * trim then suppress warnings for individual vdevs which
2560 * do not support trimming.
2562 if (vd_error
== EZFS_TRIM_NOTSUP
&&
2563 trim_flags
->fullpool
&&
2564 !trim_flags
->secure
) {
2565 num_suppressed_errs
++;
2569 reported_errs
= B_TRUE
;
2570 if (nvlist_lookup_string(guids_to_paths
, nvpair_name(elem
),
2572 path
= nvpair_name(elem
);
2574 (void) zfs_error_fmt(zhp
->zpool_hdl
, vd_error
,
2575 "cannot trim '%s'", path
);
2578 if (num_suppressed_errs
== num_vds
) {
2579 (void) zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
2580 "no devices in pool support trim operations"));
2581 (void) (zfs_error(zhp
->zpool_hdl
, EZFS_TRIM_NOTSUP
,
2582 dgettext(TEXT_DOMAIN
, "cannot trim")));
2583 reported_errs
= B_TRUE
;
2586 return (reported_errs
);
2590 * Begin, suspend, or cancel the TRIM (discarding of all free blocks) for
2591 * the given vdevs in the given pool.
2594 zpool_trim(zpool_handle_t
*zhp
, pool_trim_func_t cmd_type
, nvlist_t
*vds
,
2595 trimflags_t
*trim_flags
)
2600 nvlist_t
*vdev_guids
= fnvlist_alloc();
2601 nvlist_t
*guids_to_paths
= fnvlist_alloc();
2602 nvlist_t
*errlist
= NULL
;
2604 err
= zpool_translate_vdev_guids(zhp
, vds
, vdev_guids
,
2605 guids_to_paths
, &errlist
);
2607 check_trim_errs(zhp
, trim_flags
, guids_to_paths
, vds
, errlist
);
2612 err
= lzc_trim(zhp
->zpool_name
, cmd_type
, trim_flags
->rate
,
2613 trim_flags
->secure
, vdev_guids
, &errlist
);
2615 nvlist_t
*vd_errlist
;
2616 if (errlist
!= NULL
&& nvlist_lookup_nvlist(errlist
,
2617 ZPOOL_TRIM_VDEVS
, &vd_errlist
) == 0) {
2618 if (check_trim_errs(zhp
, trim_flags
, guids_to_paths
,
2624 char errbuf
[ERRBUFLEN
];
2626 (void) snprintf(errbuf
, sizeof (errbuf
),
2627 dgettext(TEXT_DOMAIN
, "operation failed"));
2628 zpool_standard_error(zhp
->zpool_hdl
, err
, errbuf
);
2635 if (trim_flags
->wait
)
2636 retval
= zpool_trim_wait(zhp
, vdev_guids
);
2639 if (errlist
!= NULL
)
2640 fnvlist_free(errlist
);
2641 fnvlist_free(vdev_guids
);
2642 fnvlist_free(guids_to_paths
);
2650 zpool_scan(zpool_handle_t
*zhp
, pool_scan_func_t func
, pool_scrub_cmd_t cmd
)
2652 char errbuf
[ERRBUFLEN
];
2654 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2656 nvlist_t
*args
= fnvlist_alloc();
2657 fnvlist_add_uint64(args
, "scan_type", (uint64_t)func
);
2658 fnvlist_add_uint64(args
, "scan_command", (uint64_t)cmd
);
2660 err
= lzc_scrub(ZFS_IOC_POOL_SCRUB
, zhp
->zpool_name
, args
, NULL
);
2665 } else if (err
== ZFS_ERR_IOC_CMD_UNAVAIL
) {
2666 zfs_cmd_t zc
= {"\0"};
2667 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
,
2668 sizeof (zc
.zc_name
));
2669 zc
.zc_cookie
= func
;
2672 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_SCAN
, &zc
) == 0)
2677 * An ECANCELED on a scrub means one of the following:
2678 * 1. we resumed a paused scrub.
2679 * 2. we resumed a paused error scrub.
2680 * 3. Error scrub is not run because of no error log.
2682 if (err
== ECANCELED
&& (func
== POOL_SCAN_SCRUB
||
2683 func
== POOL_SCAN_ERRORSCRUB
) && cmd
== POOL_SCRUB_NORMAL
)
2686 * The following cases have been handled here:
2687 * 1. Paused a scrub/error scrub if there is none in progress.
2689 if (err
== ENOENT
&& func
!= POOL_SCAN_NONE
&& cmd
==
2694 ASSERT3U(func
, >=, POOL_SCAN_NONE
);
2695 ASSERT3U(func
, <, POOL_SCAN_FUNCS
);
2697 if (func
== POOL_SCAN_SCRUB
|| func
== POOL_SCAN_ERRORSCRUB
) {
2698 if (cmd
== POOL_SCRUB_PAUSE
) {
2699 (void) snprintf(errbuf
, sizeof (errbuf
),
2700 dgettext(TEXT_DOMAIN
, "cannot pause scrubbing %s"),
2703 assert(cmd
== POOL_SCRUB_NORMAL
);
2704 (void) snprintf(errbuf
, sizeof (errbuf
),
2705 dgettext(TEXT_DOMAIN
, "cannot scrub %s"),
2708 } else if (func
== POOL_SCAN_RESILVER
) {
2709 assert(cmd
== POOL_SCRUB_NORMAL
);
2710 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2711 "cannot restart resilver on %s"), zhp
->zpool_name
);
2712 } else if (func
== POOL_SCAN_NONE
) {
2713 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2714 "cannot cancel scrubbing %s"), zhp
->zpool_name
);
2716 assert(!"unexpected result");
2720 * With EBUSY, five cases are possible:
2722 * Current state Requested
2723 * 1. Normal Scrub Running Normal Scrub or Error Scrub
2724 * 2. Normal Scrub Paused Error Scrub
2725 * 3. Normal Scrub Paused Pause Normal Scrub
2726 * 4. Error Scrub Running Normal Scrub or Error Scrub
2727 * 5. Error Scrub Paused Pause Error Scrub
2728 * 6. Resilvering Anything else
2732 pool_scan_stat_t
*ps
= NULL
;
2735 nvroot
= fnvlist_lookup_nvlist(zhp
->zpool_config
,
2736 ZPOOL_CONFIG_VDEV_TREE
);
2737 (void) nvlist_lookup_uint64_array(nvroot
,
2738 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &psc
);
2739 if (ps
&& ps
->pss_func
== POOL_SCAN_SCRUB
&&
2740 ps
->pss_state
== DSS_SCANNING
) {
2741 if (ps
->pss_pass_scrub_pause
== 0) {
2742 /* handles case 1 */
2743 assert(cmd
== POOL_SCRUB_NORMAL
);
2744 return (zfs_error(hdl
, EZFS_SCRUBBING
,
2747 if (func
== POOL_SCAN_ERRORSCRUB
) {
2748 /* handles case 2 */
2749 ASSERT3U(cmd
, ==, POOL_SCRUB_NORMAL
);
2750 return (zfs_error(hdl
,
2751 EZFS_SCRUB_PAUSED_TO_CANCEL
,
2754 /* handles case 3 */
2755 ASSERT3U(func
, ==, POOL_SCAN_SCRUB
);
2756 ASSERT3U(cmd
, ==, POOL_SCRUB_PAUSE
);
2757 return (zfs_error(hdl
,
2758 EZFS_SCRUB_PAUSED
, errbuf
));
2762 ps
->pss_error_scrub_func
== POOL_SCAN_ERRORSCRUB
&&
2763 ps
->pss_error_scrub_state
== DSS_ERRORSCRUBBING
) {
2764 if (ps
->pss_pass_error_scrub_pause
== 0) {
2765 /* handles case 4 */
2766 ASSERT3U(cmd
, ==, POOL_SCRUB_NORMAL
);
2767 return (zfs_error(hdl
, EZFS_ERRORSCRUBBING
,
2770 /* handles case 5 */
2771 ASSERT3U(func
, ==, POOL_SCAN_ERRORSCRUB
);
2772 ASSERT3U(cmd
, ==, POOL_SCRUB_PAUSE
);
2773 return (zfs_error(hdl
, EZFS_ERRORSCRUB_PAUSED
,
2777 /* handles case 6 */
2778 return (zfs_error(hdl
, EZFS_RESILVERING
, errbuf
));
2780 } else if (err
== ENOENT
) {
2781 return (zfs_error(hdl
, EZFS_NO_SCRUB
, errbuf
));
2782 } else if (err
== ENOTSUP
&& func
== POOL_SCAN_RESILVER
) {
2783 return (zfs_error(hdl
, EZFS_NO_RESILVER_DEFER
, errbuf
));
2785 return (zpool_standard_error(hdl
, err
, errbuf
));
2790 * Find a vdev that matches the search criteria specified. We use the
2791 * the nvpair name to determine how we should look for the device.
2792 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2793 * spare; but FALSE if its an INUSE spare.
2796 vdev_to_nvlist_iter(nvlist_t
*nv
, nvlist_t
*search
, boolean_t
*avail_spare
,
2797 boolean_t
*l2cache
, boolean_t
*log
)
2803 const char *srchkey
;
2804 nvpair_t
*pair
= nvlist_next_nvpair(search
, NULL
);
2806 /* Nothing to look for */
2807 if (search
== NULL
|| pair
== NULL
)
2810 /* Obtain the key we will use to search */
2811 srchkey
= nvpair_name(pair
);
2813 switch (nvpair_type(pair
)) {
2814 case DATA_TYPE_UINT64
:
2815 if (strcmp(srchkey
, ZPOOL_CONFIG_GUID
) == 0) {
2816 uint64_t srchval
= fnvpair_value_uint64(pair
);
2817 uint64_t theguid
= fnvlist_lookup_uint64(nv
,
2819 if (theguid
== srchval
)
2824 case DATA_TYPE_STRING
: {
2825 const char *srchval
, *val
;
2827 srchval
= fnvpair_value_string(pair
);
2828 if (nvlist_lookup_string(nv
, srchkey
, &val
) != 0)
2832 * Search for the requested value. Special cases:
2834 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2835 * "-part1", or "p1". The suffix is hidden from the user,
2836 * but included in the string, so this matches around it.
2837 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2838 * is used to check all possible expanded paths.
2839 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2841 * Otherwise, all other searches are simple string compares.
2843 if (strcmp(srchkey
, ZPOOL_CONFIG_PATH
) == 0) {
2844 uint64_t wholedisk
= 0;
2846 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
2848 if (zfs_strcmp_pathname(srchval
, val
, wholedisk
) == 0)
2851 } else if (strcmp(srchkey
, ZPOOL_CONFIG_TYPE
) == 0) {
2852 char *type
, *idx
, *end
, *p
;
2853 uint64_t id
, vdev_id
;
2856 * Determine our vdev type, keeping in mind
2857 * that the srchval is composed of a type and
2858 * vdev id pair (i.e. mirror-4).
2860 if ((type
= strdup(srchval
)) == NULL
)
2863 if ((p
= strrchr(type
, '-')) == NULL
) {
2871 * If the types don't match then keep looking.
2873 if (strncmp(val
, type
, strlen(val
)) != 0) {
2878 verify(zpool_vdev_is_interior(type
));
2880 id
= fnvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
);
2882 vdev_id
= strtoull(idx
, &end
, 10);
2885 * If we are looking for a raidz and a parity is
2886 * specified, make sure it matches.
2888 int rzlen
= strlen(VDEV_TYPE_RAIDZ
);
2889 assert(rzlen
== strlen(VDEV_TYPE_DRAID
));
2890 int typlen
= strlen(type
);
2891 if ((strncmp(type
, VDEV_TYPE_RAIDZ
, rzlen
) == 0 ||
2892 strncmp(type
, VDEV_TYPE_DRAID
, rzlen
) == 0) &&
2894 uint64_t vdev_parity
;
2895 int parity
= *(type
+ rzlen
) - '0';
2897 if (parity
<= 0 || parity
> 3 ||
2898 (typlen
- rzlen
) != 1) {
2900 * Nonsense parity specified, can
2906 vdev_parity
= fnvlist_lookup_uint64(nv
,
2907 ZPOOL_CONFIG_NPARITY
);
2908 if ((int)vdev_parity
!= parity
) {
2919 * Now verify that we have the correct vdev id.
2928 if (strcmp(srchval
, val
) == 0)
2937 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2938 &child
, &children
) != 0)
2941 for (c
= 0; c
< children
; c
++) {
2942 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2943 avail_spare
, l2cache
, NULL
)) != NULL
) {
2945 * The 'is_log' value is only set for the toplevel
2946 * vdev, not the leaf vdevs. So we always lookup the
2947 * log device from the root of the vdev tree (where
2948 * 'log' is non-NULL).
2951 nvlist_lookup_uint64(child
[c
],
2952 ZPOOL_CONFIG_IS_LOG
, &is_log
) == 0 &&
2960 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2961 &child
, &children
) == 0) {
2962 for (c
= 0; c
< children
; c
++) {
2963 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2964 avail_spare
, l2cache
, NULL
)) != NULL
) {
2965 *avail_spare
= B_TRUE
;
2971 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2972 &child
, &children
) == 0) {
2973 for (c
= 0; c
< children
; c
++) {
2974 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2975 avail_spare
, l2cache
, NULL
)) != NULL
) {
2986 * Given a physical path or guid, find the associated vdev.
2989 zpool_find_vdev_by_physpath(zpool_handle_t
*zhp
, const char *ppath
,
2990 boolean_t
*avail_spare
, boolean_t
*l2cache
, boolean_t
*log
)
2992 nvlist_t
*search
, *nvroot
, *ret
;
2996 search
= fnvlist_alloc();
2998 guid
= strtoull(ppath
, &end
, 0);
2999 if (guid
!= 0 && *end
== '\0') {
3000 fnvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
);
3002 fnvlist_add_string(search
, ZPOOL_CONFIG_PHYS_PATH
, ppath
);
3005 nvroot
= fnvlist_lookup_nvlist(zhp
->zpool_config
,
3006 ZPOOL_CONFIG_VDEV_TREE
);
3008 *avail_spare
= B_FALSE
;
3012 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
3013 fnvlist_free(search
);
3019 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
3022 zpool_vdev_is_interior(const char *name
)
3024 if (strncmp(name
, VDEV_TYPE_RAIDZ
, strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
3025 strncmp(name
, VDEV_TYPE_SPARE
, strlen(VDEV_TYPE_SPARE
)) == 0 ||
3027 VDEV_TYPE_REPLACING
, strlen(VDEV_TYPE_REPLACING
)) == 0 ||
3028 strncmp(name
, VDEV_TYPE_ROOT
, strlen(VDEV_TYPE_ROOT
)) == 0 ||
3029 strncmp(name
, VDEV_TYPE_MIRROR
, strlen(VDEV_TYPE_MIRROR
)) == 0)
3032 if (strncmp(name
, VDEV_TYPE_DRAID
, strlen(VDEV_TYPE_DRAID
)) == 0 &&
3033 !zpool_is_draid_spare(name
))
3040 zpool_find_vdev(zpool_handle_t
*zhp
, const char *path
, boolean_t
*avail_spare
,
3041 boolean_t
*l2cache
, boolean_t
*log
)
3044 nvlist_t
*nvroot
, *search
, *ret
;
3047 search
= fnvlist_alloc();
3049 guid
= strtoull(path
, &end
, 0);
3050 if (guid
!= 0 && *end
== '\0') {
3051 fnvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
);
3052 } else if (zpool_vdev_is_interior(path
)) {
3053 fnvlist_add_string(search
, ZPOOL_CONFIG_TYPE
, path
);
3055 fnvlist_add_string(search
, ZPOOL_CONFIG_PATH
, path
);
3058 nvroot
= fnvlist_lookup_nvlist(zhp
->zpool_config
,
3059 ZPOOL_CONFIG_VDEV_TREE
);
3061 *avail_spare
= B_FALSE
;
3065 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
3066 fnvlist_free(search
);
3072 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
3074 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
3075 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
3079 zpool_vdev_path_to_guid_impl(zpool_handle_t
*zhp
, const char *path
,
3080 boolean_t
*is_spare
, boolean_t
*is_l2cache
, boolean_t
*is_log
)
3082 boolean_t spare
= B_FALSE
, l2cache
= B_FALSE
, log
= B_FALSE
;
3085 if ((tgt
= zpool_find_vdev(zhp
, path
, &spare
, &l2cache
,
3089 if (is_spare
!= NULL
)
3091 if (is_l2cache
!= NULL
)
3092 *is_l2cache
= l2cache
;
3096 return (fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
));
3099 /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
3101 zpool_vdev_path_to_guid(zpool_handle_t
*zhp
, const char *path
)
3103 return (zpool_vdev_path_to_guid_impl(zhp
, path
, NULL
, NULL
, NULL
));
3107 * Bring the specified vdev online. The 'flags' parameter is a set of the
3108 * ZFS_ONLINE_* flags.
3111 zpool_vdev_online(zpool_handle_t
*zhp
, const char *path
, int flags
,
3112 vdev_state_t
*newstate
)
3114 zfs_cmd_t zc
= {"\0"};
3115 char errbuf
[ERRBUFLEN
];
3117 boolean_t avail_spare
, l2cache
, islog
;
3118 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3120 if (flags
& ZFS_ONLINE_EXPAND
) {
3121 (void) snprintf(errbuf
, sizeof (errbuf
),
3122 dgettext(TEXT_DOMAIN
, "cannot expand %s"), path
);
3124 (void) snprintf(errbuf
, sizeof (errbuf
),
3125 dgettext(TEXT_DOMAIN
, "cannot online %s"), path
);
3128 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3129 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3131 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3133 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3135 if (!(flags
& ZFS_ONLINE_SPARE
) && avail_spare
)
3136 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
3139 const char *pathname
;
3140 if ((flags
& ZFS_ONLINE_EXPAND
||
3141 zpool_get_prop_int(zhp
, ZPOOL_PROP_AUTOEXPAND
, NULL
)) &&
3142 nvlist_lookup_string(tgt
, ZPOOL_CONFIG_PATH
, &pathname
) == 0) {
3143 uint64_t wholedisk
= 0;
3145 (void) nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_WHOLE_DISK
,
3149 * XXX - L2ARC 1.0 devices can't support expansion.
3152 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3153 "cannot expand cache devices"));
3154 return (zfs_error(hdl
, EZFS_VDEVNOTSUP
, errbuf
));
3158 const char *fullpath
= path
;
3159 char buf
[MAXPATHLEN
];
3162 if (path
[0] != '/') {
3163 error
= zfs_resolve_shortname(path
, buf
,
3166 return (zfs_error(hdl
, EZFS_NODEVICE
,
3172 error
= zpool_relabel_disk(hdl
, fullpath
, errbuf
);
3179 zc
.zc_cookie
= VDEV_STATE_ONLINE
;
3182 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) != 0) {
3183 if (errno
== EINVAL
) {
3184 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "was split "
3185 "from this pool into a new one. Use '%s' "
3186 "instead"), "zpool detach");
3187 return (zfs_error(hdl
, EZFS_POSTSPLIT_ONLINE
, errbuf
));
3189 return (zpool_standard_error(hdl
, errno
, errbuf
));
3192 *newstate
= zc
.zc_cookie
;
3197 * Take the specified vdev offline
3200 zpool_vdev_offline(zpool_handle_t
*zhp
, const char *path
, boolean_t istmp
)
3202 zfs_cmd_t zc
= {"\0"};
3203 char errbuf
[ERRBUFLEN
];
3205 boolean_t avail_spare
, l2cache
;
3206 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3208 (void) snprintf(errbuf
, sizeof (errbuf
),
3209 dgettext(TEXT_DOMAIN
, "cannot offline %s"), path
);
3211 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3212 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3214 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3216 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3219 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
3221 zc
.zc_cookie
= VDEV_STATE_OFFLINE
;
3222 zc
.zc_obj
= istmp
? ZFS_OFFLINE_TEMPORARY
: 0;
3224 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3231 * There are no other replicas of this device.
3233 return (zfs_error(hdl
, EZFS_NOREPLICAS
, errbuf
));
3237 * The log device has unplayed logs
3239 return (zfs_error(hdl
, EZFS_UNPLAYED_LOGS
, errbuf
));
3242 return (zpool_standard_error(hdl
, errno
, errbuf
));
3247 * Remove the specified vdev asynchronously from the configuration, so
3248 * that it may come ONLINE if reinserted. This is called from zed on
3249 * Udev remove event.
3250 * Note: We also have a similar function zpool_vdev_remove() that
3251 * removes the vdev from the pool.
3254 zpool_vdev_remove_wanted(zpool_handle_t
*zhp
, const char *path
)
3256 zfs_cmd_t zc
= {"\0"};
3257 char errbuf
[ERRBUFLEN
];
3259 boolean_t avail_spare
, l2cache
;
3260 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3262 (void) snprintf(errbuf
, sizeof (errbuf
),
3263 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3265 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3266 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3268 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3270 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3272 zc
.zc_cookie
= VDEV_STATE_REMOVED
;
3274 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3277 return (zpool_standard_error(hdl
, errno
, errbuf
));
3281 * Mark the given vdev faulted.
3284 zpool_vdev_fault(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
3286 zfs_cmd_t zc
= {"\0"};
3287 char errbuf
[ERRBUFLEN
];
3288 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3290 (void) snprintf(errbuf
, sizeof (errbuf
),
3291 dgettext(TEXT_DOMAIN
, "cannot fault %llu"), (u_longlong_t
)guid
);
3293 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3295 zc
.zc_cookie
= VDEV_STATE_FAULTED
;
3298 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3305 * There are no other replicas of this device.
3307 return (zfs_error(hdl
, EZFS_NOREPLICAS
, errbuf
));
3310 return (zpool_standard_error(hdl
, errno
, errbuf
));
3316 * Mark the given vdev degraded.
3319 zpool_vdev_degrade(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
3321 zfs_cmd_t zc
= {"\0"};
3322 char errbuf
[ERRBUFLEN
];
3323 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3325 (void) snprintf(errbuf
, sizeof (errbuf
),
3326 dgettext(TEXT_DOMAIN
, "cannot degrade %llu"), (u_longlong_t
)guid
);
3328 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3330 zc
.zc_cookie
= VDEV_STATE_DEGRADED
;
3333 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3336 return (zpool_standard_error(hdl
, errno
, errbuf
));
3340 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
3344 is_replacing_spare(nvlist_t
*search
, nvlist_t
*tgt
, int which
)
3349 if (nvlist_lookup_nvlist_array(search
, ZPOOL_CONFIG_CHILDREN
, &child
,
3351 const char *type
= fnvlist_lookup_string(search
,
3353 if ((strcmp(type
, VDEV_TYPE_SPARE
) == 0 ||
3354 strcmp(type
, VDEV_TYPE_DRAID_SPARE
) == 0) &&
3355 children
== 2 && child
[which
] == tgt
)
3358 for (c
= 0; c
< children
; c
++)
3359 if (is_replacing_spare(child
[c
], tgt
, which
))
3367 * Attach new_disk (fully described by nvroot) to old_disk.
3368 * If 'replacing' is specified, the new disk will replace the old one.
3371 zpool_vdev_attach(zpool_handle_t
*zhp
, const char *old_disk
,
3372 const char *new_disk
, nvlist_t
*nvroot
, int replacing
, boolean_t rebuild
)
3374 zfs_cmd_t zc
= {"\0"};
3375 char errbuf
[ERRBUFLEN
];
3378 boolean_t avail_spare
, l2cache
, islog
;
3384 nvlist_t
*config_root
;
3385 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3388 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3389 "cannot replace %s with %s"), old_disk
, new_disk
);
3391 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3392 "cannot attach %s to %s"), new_disk
, old_disk
);
3394 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3395 if ((tgt
= zpool_find_vdev(zhp
, old_disk
, &avail_spare
, &l2cache
,
3397 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3400 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
3403 return (zfs_error(hdl
, EZFS_ISL2CACHE
, errbuf
));
3405 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3406 zc
.zc_cookie
= replacing
;
3407 zc
.zc_simple
= rebuild
;
3410 zfeature_lookup_guid("org.openzfs:device_rebuild", NULL
) != 0) {
3411 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3412 "the loaded zfs module doesn't support device rebuilds"));
3413 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, errbuf
));
3416 type
= fnvlist_lookup_string(tgt
, ZPOOL_CONFIG_TYPE
);
3417 if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0 &&
3418 zfeature_lookup_guid("org.openzfs:raidz_expansion", NULL
) != 0) {
3419 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3420 "the loaded zfs module doesn't support raidz expansion"));
3421 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, errbuf
));
3424 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
3425 &child
, &children
) != 0 || children
!= 1) {
3426 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3427 "new device must be a single disk"));
3428 return (zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
));
3431 config_root
= fnvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
3432 ZPOOL_CONFIG_VDEV_TREE
);
3434 if ((newname
= zpool_vdev_name(NULL
, NULL
, child
[0], 0)) == NULL
)
3438 * If the target is a hot spare that has been swapped in, we can only
3439 * replace it with another hot spare.
3442 nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_IS_SPARE
, &val
) == 0 &&
3443 (zpool_find_vdev(zhp
, newname
, &avail_spare
, &l2cache
,
3444 NULL
) == NULL
|| !avail_spare
) &&
3445 is_replacing_spare(config_root
, tgt
, 1)) {
3446 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3447 "can only be replaced by another hot spare"));
3449 return (zfs_error(hdl
, EZFS_BADTARGET
, errbuf
));
3454 zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
);
3456 ret
= zfs_ioctl(hdl
, ZFS_IOC_VDEV_ATTACH
, &zc
);
3458 zcmd_free_nvlists(&zc
);
3466 * Can't attach to or replace this type of vdev.
3469 uint64_t version
= zpool_get_prop_int(zhp
,
3470 ZPOOL_PROP_VERSION
, NULL
);
3473 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3474 "cannot replace a log with a spare"));
3475 } else if (rebuild
) {
3476 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3477 "only mirror and dRAID vdevs support "
3478 "sequential reconstruction"));
3479 } else if (zpool_is_draid_spare(new_disk
)) {
3480 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3481 "dRAID spares can only replace child "
3482 "devices in their parent's dRAID vdev"));
3483 } else if (version
>= SPA_VERSION_MULTI_REPLACE
) {
3484 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3485 "already in replacing/spare config; wait "
3486 "for completion or use 'zpool detach'"));
3488 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3489 "cannot replace a replacing device"));
3491 } else if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0) {
3492 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3493 "raidz_expansion feature must be enabled "
3494 "in order to attach a device to raidz"));
3496 char status
[64] = {0};
3497 zpool_prop_get_feature(zhp
,
3498 "feature@device_rebuild", status
, 63);
3500 strncmp(status
, ZFS_FEATURE_DISABLED
, 64) == 0) {
3501 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3502 "device_rebuild feature must be enabled "
3503 "in order to use sequential "
3506 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3507 "can only attach to mirrors and top-level "
3511 (void) zfs_error(hdl
, EZFS_BADTARGET
, errbuf
);
3516 * The new device must be a single disk.
3518 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3519 "new device must be a single disk"));
3520 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
);
3524 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "%s is busy"),
3526 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
3531 * The new device is too small.
3533 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3534 "device is too small"));
3535 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
3540 * The new device has a different optimal sector size.
3542 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3543 "new device has a different optimal sector size; use the "
3544 "option '-o ashift=N' to override the optimal size"));
3545 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
3550 * The resulting top-level vdev spec won't fit in the label.
3552 (void) zfs_error(hdl
, EZFS_DEVOVERFLOW
, errbuf
);
3557 * The existing raidz vdev has offline children
3559 if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0) {
3560 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3561 "raidz vdev has devices that are are offline or "
3563 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
3566 (void) zpool_standard_error(hdl
, errno
, errbuf
);
3572 * The boot reserved area is already being used (FreeBSD)
3574 if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0) {
3575 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3576 "the reserved boot area needed for the expansion "
3577 "is already being used by a boot loader"));
3578 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
3580 (void) zpool_standard_error(hdl
, errno
, errbuf
);
3584 (void) zpool_standard_error(hdl
, errno
, errbuf
);
3591 * Detach the specified device.
3594 zpool_vdev_detach(zpool_handle_t
*zhp
, const char *path
)
3596 zfs_cmd_t zc
= {"\0"};
3597 char errbuf
[ERRBUFLEN
];
3599 boolean_t avail_spare
, l2cache
;
3600 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3602 (void) snprintf(errbuf
, sizeof (errbuf
),
3603 dgettext(TEXT_DOMAIN
, "cannot detach %s"), path
);
3605 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3606 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3608 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3611 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
3614 return (zfs_error(hdl
, EZFS_ISL2CACHE
, errbuf
));
3616 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3618 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_DETACH
, &zc
) == 0)
3625 * Can't detach from this type of vdev.
3627 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "only "
3628 "applicable to mirror and replacing vdevs"));
3629 (void) zfs_error(hdl
, EZFS_BADTARGET
, errbuf
);
3634 * There are no other replicas of this device.
3636 (void) zfs_error(hdl
, EZFS_NOREPLICAS
, errbuf
);
3640 (void) zpool_standard_error(hdl
, errno
, errbuf
);
3647 * Find a mirror vdev in the source nvlist.
3649 * The mchild array contains a list of disks in one of the top-level mirrors
3650 * of the source pool. The schild array contains a list of disks that the
3651 * user specified on the command line. We loop over the mchild array to
3652 * see if any entry in the schild array matches.
3654 * If a disk in the mchild array is found in the schild array, we return
3655 * the index of that entry. Otherwise we return -1.
3658 find_vdev_entry(zpool_handle_t
*zhp
, nvlist_t
**mchild
, uint_t mchildren
,
3659 nvlist_t
**schild
, uint_t schildren
)
3663 for (mc
= 0; mc
< mchildren
; mc
++) {
3665 char *mpath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
3668 for (sc
= 0; sc
< schildren
; sc
++) {
3669 char *spath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
3671 boolean_t result
= (strcmp(mpath
, spath
) == 0);
3687 * Split a mirror pool. If newroot points to null, then a new nvlist
3688 * is generated and it is the responsibility of the caller to free it.
3691 zpool_vdev_split(zpool_handle_t
*zhp
, char *newname
, nvlist_t
**newroot
,
3692 nvlist_t
*props
, splitflags_t flags
)
3694 zfs_cmd_t zc
= {"\0"};
3695 char errbuf
[ERRBUFLEN
];
3697 nvlist_t
*tree
, *config
, **child
, **newchild
, *newconfig
= NULL
;
3698 nvlist_t
**varray
= NULL
, *zc_props
= NULL
;
3699 uint_t c
, children
, newchildren
, lastlog
= 0, vcount
, found
= 0;
3700 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3701 uint64_t vers
, readonly
= B_FALSE
;
3702 boolean_t freelist
= B_FALSE
, memory_err
= B_TRUE
;
3705 (void) snprintf(errbuf
, sizeof (errbuf
),
3706 dgettext(TEXT_DOMAIN
, "Unable to split %s"), zhp
->zpool_name
);
3708 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
3709 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3711 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
3712 (void) fprintf(stderr
, gettext("Internal error: unable to "
3713 "retrieve pool configuration\n"));
3717 tree
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
3718 vers
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
3721 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
3722 if ((zc_props
= zpool_valid_proplist(hdl
, zhp
->zpool_name
,
3723 props
, vers
, flags
, errbuf
)) == NULL
)
3725 (void) nvlist_lookup_uint64(zc_props
,
3726 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
3728 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3729 "property %s can only be set at import time"),
3730 zpool_prop_to_name(ZPOOL_PROP_READONLY
));
3735 if (nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
3737 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3738 "Source pool is missing vdev tree"));
3739 nvlist_free(zc_props
);
3743 varray
= zfs_alloc(hdl
, children
* sizeof (nvlist_t
*));
3746 if (*newroot
== NULL
||
3747 nvlist_lookup_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
3748 &newchild
, &newchildren
) != 0)
3751 for (c
= 0; c
< children
; c
++) {
3752 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
3753 boolean_t is_special
= B_FALSE
, is_dedup
= B_FALSE
;
3755 nvlist_t
**mchild
, *vdev
;
3760 * Unlike cache & spares, slogs are stored in the
3761 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3763 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
3765 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
3767 if (is_log
|| is_hole
) {
3769 * Create a hole vdev and put it in the config.
3771 if (nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) != 0)
3773 if (nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
,
3774 VDEV_TYPE_HOLE
) != 0)
3776 if (nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_HOLE
,
3781 varray
[vcount
++] = vdev
;
3785 type
= fnvlist_lookup_string(child
[c
], ZPOOL_CONFIG_TYPE
);
3787 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0) {
3789 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
3792 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
3793 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3794 "Source pool must be composed only of mirrors\n"));
3795 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
);
3799 if (nvlist_lookup_string(child
[c
],
3800 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
) == 0) {
3801 if (strcmp(bias
, VDEV_ALLOC_BIAS_SPECIAL
) == 0)
3802 is_special
= B_TRUE
;
3803 else if (strcmp(bias
, VDEV_ALLOC_BIAS_DEDUP
) == 0)
3806 verify(nvlist_lookup_nvlist_array(child
[c
],
3807 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
3809 /* find or add an entry for this top-level vdev */
3810 if (newchildren
> 0 &&
3811 (entry
= find_vdev_entry(zhp
, mchild
, mchildren
,
3812 newchild
, newchildren
)) >= 0) {
3813 /* We found a disk that the user specified. */
3814 vdev
= mchild
[entry
];
3817 /* User didn't specify a disk for this vdev. */
3818 vdev
= mchild
[mchildren
- 1];
3821 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
3824 if (flags
.dryrun
!= 0) {
3825 if (is_dedup
== B_TRUE
) {
3826 if (nvlist_add_string(varray
[vcount
- 1],
3827 ZPOOL_CONFIG_ALLOCATION_BIAS
,
3828 VDEV_ALLOC_BIAS_DEDUP
) != 0)
3830 } else if (is_special
== B_TRUE
) {
3831 if (nvlist_add_string(varray
[vcount
- 1],
3832 ZPOOL_CONFIG_ALLOCATION_BIAS
,
3833 VDEV_ALLOC_BIAS_SPECIAL
) != 0)
3839 /* did we find every disk the user specified? */
3840 if (found
!= newchildren
) {
3841 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Device list must "
3842 "include at most one disk from each mirror"));
3843 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
);
3847 /* Prepare the nvlist for populating. */
3848 if (*newroot
== NULL
) {
3849 if (nvlist_alloc(newroot
, NV_UNIQUE_NAME
, 0) != 0)
3852 if (nvlist_add_string(*newroot
, ZPOOL_CONFIG_TYPE
,
3853 VDEV_TYPE_ROOT
) != 0)
3856 verify(nvlist_remove_all(*newroot
, ZPOOL_CONFIG_CHILDREN
) == 0);
3859 /* Add all the children we found */
3860 if (nvlist_add_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
3861 (const nvlist_t
**)varray
, lastlog
== 0 ? vcount
: lastlog
) != 0)
3865 * If we're just doing a dry run, exit now with success.
3868 memory_err
= B_FALSE
;
3873 /* now build up the config list & call the ioctl */
3874 if (nvlist_alloc(&newconfig
, NV_UNIQUE_NAME
, 0) != 0)
3877 if (nvlist_add_nvlist(newconfig
,
3878 ZPOOL_CONFIG_VDEV_TREE
, *newroot
) != 0 ||
3879 nvlist_add_string(newconfig
,
3880 ZPOOL_CONFIG_POOL_NAME
, newname
) != 0 ||
3881 nvlist_add_uint64(newconfig
, ZPOOL_CONFIG_VERSION
, vers
) != 0)
3885 * The new pool is automatically part of the namespace unless we
3886 * explicitly export it.
3889 zc
.zc_cookie
= ZPOOL_EXPORT_AFTER_SPLIT
;
3890 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3891 (void) strlcpy(zc
.zc_string
, newname
, sizeof (zc
.zc_string
));
3892 zcmd_write_conf_nvlist(hdl
, &zc
, newconfig
);
3893 if (zc_props
!= NULL
)
3894 zcmd_write_src_nvlist(hdl
, &zc
, zc_props
);
3896 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SPLIT
, &zc
) != 0) {
3897 retval
= zpool_standard_error(hdl
, errno
, errbuf
);
3902 memory_err
= B_FALSE
;
3905 if (varray
!= NULL
) {
3908 for (v
= 0; v
< vcount
; v
++)
3909 nvlist_free(varray
[v
]);
3912 zcmd_free_nvlists(&zc
);
3913 nvlist_free(zc_props
);
3914 nvlist_free(newconfig
);
3916 nvlist_free(*newroot
);
3924 return (no_memory(hdl
));
3930 * Remove the given device.
3933 zpool_vdev_remove(zpool_handle_t
*zhp
, const char *path
)
3935 zfs_cmd_t zc
= {"\0"};
3936 char errbuf
[ERRBUFLEN
];
3938 boolean_t avail_spare
, l2cache
, islog
;
3939 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3942 (void) snprintf(errbuf
, sizeof (errbuf
),
3943 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3945 if (zpool_is_draid_spare(path
)) {
3946 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3947 "dRAID spares cannot be removed"));
3948 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3951 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3952 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3954 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3956 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
3957 if (islog
&& version
< SPA_VERSION_HOLES
) {
3958 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3959 "pool must be upgraded to support log removal"));
3960 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3963 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3965 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3971 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3972 "removal for this vdev is already in progress."));
3973 (void) zfs_error(hdl
, EZFS_BUSY
, errbuf
);
3977 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3978 "invalid config; all top-level vdevs must "
3979 "have the same sector size and not be raidz."));
3980 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
);
3985 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3986 "Mount encrypted datasets to replay logs."));
3988 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3989 "Pool busy; removal may already be in progress"));
3991 (void) zfs_error(hdl
, EZFS_BUSY
, errbuf
);
3996 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3997 "Mount encrypted datasets to replay logs."));
3998 (void) zfs_error(hdl
, EZFS_BUSY
, errbuf
);
4000 (void) zpool_standard_error(hdl
, errno
, errbuf
);
4005 (void) zpool_standard_error(hdl
, errno
, errbuf
);
4011 zpool_vdev_remove_cancel(zpool_handle_t
*zhp
)
4013 zfs_cmd_t zc
= {{0}};
4014 char errbuf
[ERRBUFLEN
];
4015 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4017 (void) snprintf(errbuf
, sizeof (errbuf
),
4018 dgettext(TEXT_DOMAIN
, "cannot cancel removal"));
4020 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4023 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
4026 return (zpool_standard_error(hdl
, errno
, errbuf
));
4030 zpool_vdev_indirect_size(zpool_handle_t
*zhp
, const char *path
,
4033 char errbuf
[ERRBUFLEN
];
4035 boolean_t avail_spare
, l2cache
, islog
;
4036 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4038 (void) snprintf(errbuf
, sizeof (errbuf
),
4039 dgettext(TEXT_DOMAIN
, "cannot determine indirect size of %s"),
4042 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
4044 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
4046 if (avail_spare
|| l2cache
|| islog
) {
4051 if (nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_INDIRECT_SIZE
, sizep
) != 0) {
4052 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4053 "indirect size not available"));
4054 return (zfs_error(hdl
, EINVAL
, errbuf
));
4060 * Clear the errors for the pool, or the particular device if specified.
4063 zpool_clear(zpool_handle_t
*zhp
, const char *path
, nvlist_t
*rewindnvl
)
4065 zfs_cmd_t zc
= {"\0"};
4066 char errbuf
[ERRBUFLEN
];
4068 zpool_load_policy_t policy
;
4069 boolean_t avail_spare
, l2cache
;
4070 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4071 nvlist_t
*nvi
= NULL
;
4075 (void) snprintf(errbuf
, sizeof (errbuf
),
4076 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
4079 (void) snprintf(errbuf
, sizeof (errbuf
),
4080 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
4083 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4085 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
,
4086 &l2cache
, NULL
)) == NULL
)
4087 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
4090 * Don't allow error clearing for hot spares. Do allow
4091 * error clearing for l2cache devices.
4094 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
4096 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
4099 zpool_get_load_policy(rewindnvl
, &policy
);
4100 zc
.zc_cookie
= policy
.zlp_rewind
;
4102 zcmd_alloc_dst_nvlist(hdl
, &zc
, zhp
->zpool_config_size
* 2);
4103 zcmd_write_src_nvlist(hdl
, &zc
, rewindnvl
);
4105 while ((error
= zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
)) != 0 &&
4107 zcmd_expand_dst_nvlist(hdl
, &zc
);
4109 if (!error
|| ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) &&
4110 errno
!= EPERM
&& errno
!= EACCES
)) {
4111 if (policy
.zlp_rewind
&
4112 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
4113 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nvi
);
4114 zpool_rewind_exclaim(hdl
, zc
.zc_name
,
4115 ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) != 0),
4119 zcmd_free_nvlists(&zc
);
4123 zcmd_free_nvlists(&zc
);
4124 return (zpool_standard_error(hdl
, errno
, errbuf
));
4128 * Similar to zpool_clear(), but takes a GUID (used by fmd).
4131 zpool_vdev_clear(zpool_handle_t
*zhp
, uint64_t guid
)
4133 zfs_cmd_t zc
= {"\0"};
4134 char errbuf
[ERRBUFLEN
];
4135 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4137 (void) snprintf(errbuf
, sizeof (errbuf
),
4138 dgettext(TEXT_DOMAIN
, "cannot clear errors for %llx"),
4139 (u_longlong_t
)guid
);
4141 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4143 zc
.zc_cookie
= ZPOOL_NO_REWIND
;
4145 if (zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
) == 0)
4148 return (zpool_standard_error(hdl
, errno
, errbuf
));
4152 * Change the GUID for a pool.
4155 zpool_reguid(zpool_handle_t
*zhp
)
4157 char errbuf
[ERRBUFLEN
];
4158 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4159 zfs_cmd_t zc
= {"\0"};
4161 (void) snprintf(errbuf
, sizeof (errbuf
),
4162 dgettext(TEXT_DOMAIN
, "cannot reguid '%s'"), zhp
->zpool_name
);
4164 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4165 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REGUID
, &zc
) == 0)
4168 return (zpool_standard_error(hdl
, errno
, errbuf
));
4175 zpool_reopen_one(zpool_handle_t
*zhp
, void *data
)
4177 libzfs_handle_t
*hdl
= zpool_get_handle(zhp
);
4178 const char *pool_name
= zpool_get_name(zhp
);
4179 boolean_t
*scrub_restart
= data
;
4182 error
= lzc_reopen(pool_name
, *scrub_restart
);
4184 return (zpool_standard_error_fmt(hdl
, error
,
4185 dgettext(TEXT_DOMAIN
, "cannot reopen '%s'"), pool_name
));
4191 /* call into libzfs_core to execute the sync IOCTL per pool */
4193 zpool_sync_one(zpool_handle_t
*zhp
, void *data
)
4196 libzfs_handle_t
*hdl
= zpool_get_handle(zhp
);
4197 const char *pool_name
= zpool_get_name(zhp
);
4198 boolean_t
*force
= data
;
4199 nvlist_t
*innvl
= fnvlist_alloc();
4201 fnvlist_add_boolean_value(innvl
, "force", *force
);
4202 if ((ret
= lzc_sync(pool_name
, innvl
, NULL
)) != 0) {
4204 return (zpool_standard_error_fmt(hdl
, ret
,
4205 dgettext(TEXT_DOMAIN
, "sync '%s' failed"), pool_name
));
4212 #define PATH_BUF_LEN 64
4215 * Given a vdev, return the name to display in iostat. If the vdev has a path,
4216 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
4217 * We also check if this is a whole disk, in which case we strip off the
4218 * trailing 's0' slice name.
4220 * This routine is also responsible for identifying when disks have been
4221 * reconfigured in a new location. The kernel will have opened the device by
4222 * devid, but the path will still refer to the old location. To catch this, we
4223 * first do a path -> devid translation (which is fast for the common case). If
4224 * the devid matches, we're done. If not, we do a reverse devid -> path
4225 * translation and issue the appropriate ioctl() to update the path of the vdev.
4226 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
4230 zpool_vdev_name(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, nvlist_t
*nv
,
4233 const char *type
, *tpath
;
4236 char buf
[PATH_BUF_LEN
];
4237 char tmpbuf
[PATH_BUF_LEN
* 2];
4240 * vdev_name will be "root"/"root-0" for the root vdev, but it is the
4241 * zpool name that will be displayed to the user.
4243 type
= fnvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
);
4244 if (zhp
!= NULL
&& strcmp(type
, "root") == 0)
4245 return (zfs_strdup(hdl
, zpool_get_name(zhp
)));
4247 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_PATH"))
4248 name_flags
|= VDEV_NAME_PATH
;
4249 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_GUID"))
4250 name_flags
|= VDEV_NAME_GUID
;
4251 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_FOLLOW_LINKS"))
4252 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
4254 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
, &value
) == 0 ||
4255 name_flags
& VDEV_NAME_GUID
) {
4256 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
);
4257 (void) snprintf(buf
, sizeof (buf
), "%llu", (u_longlong_t
)value
);
4259 } else if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &tpath
) == 0) {
4262 if (name_flags
& VDEV_NAME_FOLLOW_LINKS
) {
4263 char *rp
= realpath(path
, NULL
);
4265 strlcpy(buf
, rp
, sizeof (buf
));
4272 * For a block device only use the name.
4274 if ((strcmp(type
, VDEV_TYPE_DISK
) == 0) &&
4275 !(name_flags
& VDEV_NAME_PATH
)) {
4276 path
= zfs_strip_path(path
);
4280 * Remove the partition from the path if this is a whole disk.
4282 if (strcmp(type
, VDEV_TYPE_DRAID_SPARE
) != 0 &&
4283 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
, &value
)
4284 == 0 && value
&& !(name_flags
& VDEV_NAME_PATH
)) {
4285 return (zfs_strip_partition(path
));
4291 * If it's a raidz device, we need to stick in the parity level.
4293 if (strcmp(path
, VDEV_TYPE_RAIDZ
) == 0) {
4294 value
= fnvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
);
4295 (void) snprintf(buf
, sizeof (buf
), "%s%llu", path
,
4296 (u_longlong_t
)value
);
4301 * If it's a dRAID device, we add parity, groups, and spares.
4303 if (strcmp(path
, VDEV_TYPE_DRAID
) == 0) {
4304 uint64_t ndata
, nparity
, nspares
;
4308 verify(nvlist_lookup_nvlist_array(nv
,
4309 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) == 0);
4310 nparity
= fnvlist_lookup_uint64(nv
,
4311 ZPOOL_CONFIG_NPARITY
);
4312 ndata
= fnvlist_lookup_uint64(nv
,
4313 ZPOOL_CONFIG_DRAID_NDATA
);
4314 nspares
= fnvlist_lookup_uint64(nv
,
4315 ZPOOL_CONFIG_DRAID_NSPARES
);
4317 path
= zpool_draid_name(buf
, sizeof (buf
), ndata
,
4318 nparity
, nspares
, children
);
4322 * We identify each top-level vdev by using a <type-id>
4323 * naming convention.
4325 if (name_flags
& VDEV_NAME_TYPE_ID
) {
4326 uint64_t id
= fnvlist_lookup_uint64(nv
,
4328 (void) snprintf(tmpbuf
, sizeof (tmpbuf
), "%s-%llu",
4329 path
, (u_longlong_t
)id
);
4334 return (zfs_strdup(hdl
, path
));
4338 zbookmark_mem_compare(const void *a
, const void *b
)
4340 return (memcmp(a
, b
, sizeof (zbookmark_phys_t
)));
4344 * Retrieve the persistent error log, uniquify the members, and return to the
4348 zpool_get_errlog(zpool_handle_t
*zhp
, nvlist_t
**nverrlistp
)
4350 zfs_cmd_t zc
= {"\0"};
4351 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4352 zbookmark_phys_t
*buf
;
4353 uint64_t buflen
= 10000; /* approx. 1MB of RAM */
4355 if (fnvlist_lookup_uint64(zhp
->zpool_config
,
4356 ZPOOL_CONFIG_ERRCOUNT
) == 0)
4360 * Retrieve the raw error list from the kernel. If it doesn't fit,
4361 * allocate a larger buffer and retry.
4363 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
4365 buf
= zfs_alloc(zhp
->zpool_hdl
,
4366 buflen
* sizeof (zbookmark_phys_t
));
4367 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4368 zc
.zc_nvlist_dst_size
= buflen
;
4369 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_ERROR_LOG
,
4372 if (errno
== ENOMEM
) {
4375 return (zpool_standard_error_fmt(hdl
, errno
,
4376 dgettext(TEXT_DOMAIN
, "errors: List of "
4377 "errors unavailable")));
4385 * Sort the resulting bookmarks. This is a little confusing due to the
4386 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
4387 * to first, and 'zc_nvlist_dst_size' indicates the number of bookmarks
4388 * _not_ copied as part of the process. So we point the start of our
4389 * array appropriate and decrement the total number of elements.
4391 zbookmark_phys_t
*zb
= buf
+ zc
.zc_nvlist_dst_size
;
4392 uint64_t zblen
= buflen
- zc
.zc_nvlist_dst_size
;
4394 qsort(zb
, zblen
, sizeof (zbookmark_phys_t
), zbookmark_mem_compare
);
4396 verify(nvlist_alloc(nverrlistp
, 0, KM_SLEEP
) == 0);
4399 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
4401 for (uint64_t i
= 0; i
< zblen
; i
++) {
4404 /* ignoring zb_blkid and zb_level for now */
4405 if (i
> 0 && zb
[i
-1].zb_objset
== zb
[i
].zb_objset
&&
4406 zb
[i
-1].zb_object
== zb
[i
].zb_object
)
4409 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) != 0)
4411 if (nvlist_add_uint64(nv
, ZPOOL_ERR_DATASET
,
4412 zb
[i
].zb_objset
) != 0) {
4416 if (nvlist_add_uint64(nv
, ZPOOL_ERR_OBJECT
,
4417 zb
[i
].zb_object
) != 0) {
4421 if (nvlist_add_nvlist(*nverrlistp
, "ejk", nv
) != 0) {
4433 return (no_memory(zhp
->zpool_hdl
));
4437 * Upgrade a ZFS pool to the latest on-disk version.
4440 zpool_upgrade(zpool_handle_t
*zhp
, uint64_t new_version
)
4442 zfs_cmd_t zc
= {"\0"};
4443 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4445 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
4446 zc
.zc_cookie
= new_version
;
4448 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_UPGRADE
, &zc
) != 0)
4449 return (zpool_standard_error_fmt(hdl
, errno
,
4450 dgettext(TEXT_DOMAIN
, "cannot upgrade '%s'"),
4456 zfs_save_arguments(int argc
, char **argv
, char *string
, int len
)
4460 (void) strlcpy(string
, zfs_basename(argv
[0]), len
);
4461 for (i
= 1; i
< argc
; i
++) {
4462 (void) strlcat(string
, " ", len
);
4463 (void) strlcat(string
, argv
[i
], len
);
4468 zpool_log_history(libzfs_handle_t
*hdl
, const char *message
)
4470 zfs_cmd_t zc
= {"\0"};
4473 args
= fnvlist_alloc();
4474 fnvlist_add_string(args
, "message", message
);
4475 zcmd_write_src_nvlist(hdl
, &zc
, args
);
4476 int err
= zfs_ioctl(hdl
, ZFS_IOC_LOG_HISTORY
, &zc
);
4478 zcmd_free_nvlists(&zc
);
4483 * Perform ioctl to get some command history of a pool.
4485 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
4486 * logical offset of the history buffer to start reading from.
4488 * Upon return, 'off' is the next logical offset to read from and
4489 * 'len' is the actual amount of bytes read into 'buf'.
4492 get_history(zpool_handle_t
*zhp
, char *buf
, uint64_t *off
, uint64_t *len
)
4494 zfs_cmd_t zc
= {"\0"};
4495 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4497 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4499 zc
.zc_history
= (uint64_t)(uintptr_t)buf
;
4500 zc
.zc_history_len
= *len
;
4501 zc
.zc_history_offset
= *off
;
4503 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_GET_HISTORY
, &zc
) != 0) {
4506 return (zfs_error_fmt(hdl
, EZFS_PERM
,
4507 dgettext(TEXT_DOMAIN
,
4508 "cannot show history for pool '%s'"),
4511 return (zfs_error_fmt(hdl
, EZFS_NOHISTORY
,
4512 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
4513 "'%s'"), zhp
->zpool_name
));
4515 return (zfs_error_fmt(hdl
, EZFS_BADVERSION
,
4516 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
4517 "'%s', pool must be upgraded"), zhp
->zpool_name
));
4519 return (zpool_standard_error_fmt(hdl
, errno
,
4520 dgettext(TEXT_DOMAIN
,
4521 "cannot get history for '%s'"), zhp
->zpool_name
));
4525 *len
= zc
.zc_history_len
;
4526 *off
= zc
.zc_history_offset
;
4532 * Retrieve the command history of a pool.
4535 zpool_get_history(zpool_handle_t
*zhp
, nvlist_t
**nvhisp
, uint64_t *off
,
4538 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4540 int buflen
= 128 * 1024;
4541 nvlist_t
**records
= NULL
;
4542 uint_t numrecords
= 0;
4544 uint64_t start
= *off
;
4546 buf
= zfs_alloc(hdl
, buflen
);
4548 /* process about 1MiB a time */
4549 while (*off
- start
< 1024 * 1024) {
4550 uint64_t bytes_read
= buflen
;
4553 if ((err
= get_history(zhp
, buf
, off
, &bytes_read
)) != 0)
4556 /* if nothing else was read in, we're at EOF, just return */
4562 if ((err
= zpool_history_unpack(buf
, bytes_read
,
4563 &leftover
, &records
, &numrecords
)) != 0) {
4564 zpool_standard_error_fmt(hdl
, err
,
4565 dgettext(TEXT_DOMAIN
,
4566 "cannot get history for '%s'"), zhp
->zpool_name
);
4570 if (leftover
== bytes_read
) {
4572 * no progress made, because buffer is not big enough
4573 * to hold this record; resize and retry.
4577 buf
= zfs_alloc(hdl
, buflen
);
4584 *nvhisp
= fnvlist_alloc();
4585 fnvlist_add_nvlist_array(*nvhisp
, ZPOOL_HIST_RECORD
,
4586 (const nvlist_t
**)records
, numrecords
);
4588 for (i
= 0; i
< numrecords
; i
++)
4589 nvlist_free(records
[i
]);
4596 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
4597 * If there is a new event available 'nvp' will contain a newly allocated
4598 * nvlist and 'dropped' will be set to the number of missed events since
4599 * the last call to this function. When 'nvp' is set to NULL it indicates
4600 * no new events are available. In either case the function returns 0 and
4601 * it is up to the caller to free 'nvp'. In the case of a fatal error the
4602 * function will return a non-zero value. When the function is called in
4603 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
4604 * it will not return until a new event is available.
4607 zpool_events_next(libzfs_handle_t
*hdl
, nvlist_t
**nvp
,
4608 int *dropped
, unsigned flags
, int zevent_fd
)
4610 zfs_cmd_t zc
= {"\0"};
4615 zc
.zc_cleanup_fd
= zevent_fd
;
4617 if (flags
& ZEVENT_NONBLOCK
)
4618 zc
.zc_guid
= ZEVENT_NONBLOCK
;
4620 zcmd_alloc_dst_nvlist(hdl
, &zc
, ZEVENT_SIZE
);
4623 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_NEXT
, &zc
) != 0) {
4626 error
= zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
4627 dgettext(TEXT_DOMAIN
, "zfs shutdown"));
4630 /* Blocking error case should not occur */
4631 if (!(flags
& ZEVENT_NONBLOCK
))
4632 error
= zpool_standard_error_fmt(hdl
, errno
,
4633 dgettext(TEXT_DOMAIN
, "cannot get event"));
4637 zcmd_expand_dst_nvlist(hdl
, &zc
);
4640 error
= zpool_standard_error_fmt(hdl
, errno
,
4641 dgettext(TEXT_DOMAIN
, "cannot get event"));
4646 error
= zcmd_read_dst_nvlist(hdl
, &zc
, nvp
);
4650 *dropped
= (int)zc
.zc_cookie
;
4652 zcmd_free_nvlists(&zc
);
4661 zpool_events_clear(libzfs_handle_t
*hdl
, int *count
)
4663 zfs_cmd_t zc
= {"\0"};
4665 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_CLEAR
, &zc
) != 0)
4666 return (zpool_standard_error(hdl
, errno
,
4667 dgettext(TEXT_DOMAIN
, "cannot clear events")));
4670 *count
= (int)zc
.zc_cookie
; /* # of events cleared */
4676 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4677 * the passed zevent_fd file handle. On success zero is returned,
4678 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4681 zpool_events_seek(libzfs_handle_t
*hdl
, uint64_t eid
, int zevent_fd
)
4683 zfs_cmd_t zc
= {"\0"};
4687 zc
.zc_cleanup_fd
= zevent_fd
;
4689 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_SEEK
, &zc
) != 0) {
4692 error
= zfs_error_fmt(hdl
, EZFS_NOENT
,
4693 dgettext(TEXT_DOMAIN
, "cannot get event"));
4697 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
4698 dgettext(TEXT_DOMAIN
, "cannot get event"));
4702 error
= zpool_standard_error_fmt(hdl
, errno
,
4703 dgettext(TEXT_DOMAIN
, "cannot get event"));
4712 zpool_obj_to_path_impl(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4713 char *pathname
, size_t len
, boolean_t always_unmounted
)
4715 zfs_cmd_t zc
= {"\0"};
4716 boolean_t mounted
= B_FALSE
;
4717 char *mntpnt
= NULL
;
4718 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
4721 /* special case for the MOS */
4722 (void) snprintf(pathname
, len
, "<metadata>:<0x%llx>",
4727 /* get the dataset's name */
4728 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4730 if (zfs_ioctl(zhp
->zpool_hdl
,
4731 ZFS_IOC_DSOBJ_TO_DSNAME
, &zc
) != 0) {
4732 /* just write out a path of two object numbers */
4733 (void) snprintf(pathname
, len
, "<0x%llx>:<0x%llx>",
4734 (longlong_t
)dsobj
, (longlong_t
)obj
);
4737 (void) strlcpy(dsname
, zc
.zc_value
, sizeof (dsname
));
4739 /* find out if the dataset is mounted */
4740 mounted
= !always_unmounted
&& is_mounted(zhp
->zpool_hdl
, dsname
,
4743 /* get the corrupted object's path */
4744 (void) strlcpy(zc
.zc_name
, dsname
, sizeof (zc
.zc_name
));
4746 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_OBJ_TO_PATH
,
4749 (void) snprintf(pathname
, len
, "%s%s", mntpnt
,
4752 (void) snprintf(pathname
, len
, "%s:%s",
4753 dsname
, zc
.zc_value
);
4756 (void) snprintf(pathname
, len
, "%s:<0x%llx>", dsname
,
4763 zpool_obj_to_path(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4764 char *pathname
, size_t len
)
4766 zpool_obj_to_path_impl(zhp
, dsobj
, obj
, pathname
, len
, B_FALSE
);
4770 zpool_obj_to_path_ds(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4771 char *pathname
, size_t len
)
4773 zpool_obj_to_path_impl(zhp
, dsobj
, obj
, pathname
, len
, B_TRUE
);
4776 * Wait while the specified activity is in progress in the pool.
4779 zpool_wait(zpool_handle_t
*zhp
, zpool_wait_activity_t activity
)
4783 int error
= zpool_wait_status(zhp
, activity
, &missing
, NULL
);
4786 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, ENOENT
,
4787 dgettext(TEXT_DOMAIN
, "error waiting in pool '%s'"),
4796 * Wait for the given activity and return the status of the wait (whether or not
4797 * any waiting was done) in the 'waited' parameter. Non-existent pools are
4798 * reported via the 'missing' parameter, rather than by printing an error
4799 * message. This is convenient when this function is called in a loop over a
4800 * long period of time (as it is, for example, by zpool's wait cmd). In that
4801 * scenario, a pool being exported or destroyed should be considered a normal
4802 * event, so we don't want to print an error when we find that the pool doesn't
4806 zpool_wait_status(zpool_handle_t
*zhp
, zpool_wait_activity_t activity
,
4807 boolean_t
*missing
, boolean_t
*waited
)
4809 int error
= lzc_wait(zhp
->zpool_name
, activity
, waited
);
4810 *missing
= (error
== ENOENT
);
4815 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
4816 dgettext(TEXT_DOMAIN
, "error waiting in pool '%s'"),
4824 zpool_set_bootenv(zpool_handle_t
*zhp
, const nvlist_t
*envmap
)
4826 int error
= lzc_set_bootenv(zhp
->zpool_name
, envmap
);
4828 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
4829 dgettext(TEXT_DOMAIN
,
4830 "error setting bootenv in pool '%s'"), zhp
->zpool_name
);
4837 zpool_get_bootenv(zpool_handle_t
*zhp
, nvlist_t
**nvlp
)
4843 error
= lzc_get_bootenv(zhp
->zpool_name
, &nvl
);
4845 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
4846 dgettext(TEXT_DOMAIN
,
4847 "error getting bootenv in pool '%s'"), zhp
->zpool_name
);
4856 * Attempt to read and parse feature file(s) (from "compatibility" property).
4857 * Files contain zpool feature names, comma or whitespace-separated.
4858 * Comments (# character to next newline) are discarded.
4861 * compatibility : string containing feature filenames
4862 * features : either NULL or pointer to array of boolean
4863 * report : either NULL or pointer to string buffer
4864 * rlen : length of "report" buffer
4866 * compatibility is NULL (unset), "", "off", "legacy", or list of
4867 * comma-separated filenames. filenames should either be absolute,
4869 * 1) ZPOOL_SYSCONF_COMPAT_D (eg: /etc/zfs/compatibility.d) or
4870 * 2) ZPOOL_DATA_COMPAT_D (eg: /usr/share/zfs/compatibility.d).
4871 * (Unset), "" or "off" => enable all features
4872 * "legacy" => disable all features
4874 * Any feature names read from files which match unames in spa_feature_table
4875 * will have the corresponding boolean set in the features array (if non-NULL).
4876 * If more than one feature set specified, only features present in *all* of
4879 * "report" if not NULL will be populated with a suitable status message.
4882 * ZPOOL_COMPATIBILITY_OK : files read and parsed ok
4883 * ZPOOL_COMPATIBILITY_BADFILE : file too big or not a text file
4884 * ZPOOL_COMPATIBILITY_BADTOKEN : SYSCONF file contains invalid feature name
4885 * ZPOOL_COMPATIBILITY_WARNTOKEN : DATA file contains invalid feature name
4886 * ZPOOL_COMPATIBILITY_NOFILES : no feature files found
4888 zpool_compat_status_t
4889 zpool_load_compat(const char *compat
, boolean_t
*features
, char *report
,
4892 int sdirfd
, ddirfd
, featfd
;
4896 char *file
, *line
, *word
;
4898 char l_compat
[ZFS_MAXPROPLEN
];
4900 boolean_t ret_nofiles
= B_TRUE
;
4901 boolean_t ret_badfile
= B_FALSE
;
4902 boolean_t ret_badtoken
= B_FALSE
;
4903 boolean_t ret_warntoken
= B_FALSE
;
4905 /* special cases (unset), "" and "off" => enable all features */
4906 if (compat
== NULL
|| compat
[0] == '\0' ||
4907 strcmp(compat
, ZPOOL_COMPAT_OFF
) == 0) {
4908 if (features
!= NULL
)
4909 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4910 features
[i
] = B_TRUE
;
4912 strlcpy(report
, gettext("all features enabled"), rlen
);
4913 return (ZPOOL_COMPATIBILITY_OK
);
4916 /* Final special case "legacy" => disable all features */
4917 if (strcmp(compat
, ZPOOL_COMPAT_LEGACY
) == 0) {
4918 if (features
!= NULL
)
4919 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4920 features
[i
] = B_FALSE
;
4922 strlcpy(report
, gettext("all features disabled"), rlen
);
4923 return (ZPOOL_COMPATIBILITY_OK
);
4927 * Start with all true; will be ANDed with results from each file
4929 if (features
!= NULL
)
4930 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4931 features
[i
] = B_TRUE
;
4933 char err_badfile
[ZFS_MAXPROPLEN
] = "";
4934 char err_badtoken
[ZFS_MAXPROPLEN
] = "";
4937 * We ignore errors from the directory open()
4938 * as they're only needed if the filename is relative
4939 * which will be checked during the openat().
4942 /* O_PATH safer than O_RDONLY if system allows it */
4944 #define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_PATH)
4946 #define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_RDONLY)
4949 sdirfd
= open(ZPOOL_SYSCONF_COMPAT_D
, ZC_DIR_FLAGS
);
4950 ddirfd
= open(ZPOOL_DATA_COMPAT_D
, ZC_DIR_FLAGS
);
4952 (void) strlcpy(l_compat
, compat
, ZFS_MAXPROPLEN
);
4954 for (file
= strtok_r(l_compat
, ",", &ps
);
4956 file
= strtok_r(NULL
, ",", &ps
)) {
4958 boolean_t l_features
[SPA_FEATURES
];
4960 enum { Z_SYSCONF
, Z_DATA
} source
;
4962 /* try sysconfdir first, then datadir */
4964 if ((featfd
= openat(sdirfd
, file
, O_RDONLY
| O_CLOEXEC
)) < 0) {
4965 featfd
= openat(ddirfd
, file
, O_RDONLY
| O_CLOEXEC
);
4969 /* File readable and correct size? */
4971 fstat(featfd
, &fs
) < 0 ||
4973 fs
.st_size
> ZPOOL_COMPAT_MAXSIZE
) {
4974 (void) close(featfd
);
4975 strlcat(err_badfile
, file
, ZFS_MAXPROPLEN
);
4976 strlcat(err_badfile
, " ", ZFS_MAXPROPLEN
);
4977 ret_badfile
= B_TRUE
;
4981 /* Prefault the file if system allows */
4982 #if defined(MAP_POPULATE)
4983 #define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_POPULATE)
4984 #elif defined(MAP_PREFAULT_READ)
4985 #define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_PREFAULT_READ)
4987 #define ZC_MMAP_FLAGS (MAP_PRIVATE)
4990 /* private mmap() so we can strtok safely */
4991 fc
= (char *)mmap(NULL
, fs
.st_size
, PROT_READ
| PROT_WRITE
,
4992 ZC_MMAP_FLAGS
, featfd
, 0);
4993 (void) close(featfd
);
4995 /* map ok, and last character == newline? */
4996 if (fc
== MAP_FAILED
|| fc
[fs
.st_size
- 1] != '\n') {
4997 (void) munmap((void *) fc
, fs
.st_size
);
4998 strlcat(err_badfile
, file
, ZFS_MAXPROPLEN
);
4999 strlcat(err_badfile
, " ", ZFS_MAXPROPLEN
);
5000 ret_badfile
= B_TRUE
;
5004 ret_nofiles
= B_FALSE
;
5006 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
5007 l_features
[i
] = B_FALSE
;
5009 /* replace final newline with NULL to ensure string ends */
5010 fc
[fs
.st_size
- 1] = '\0';
5012 for (line
= strtok_r(fc
, "\n", &ls
);
5014 line
= strtok_r(NULL
, "\n", &ls
)) {
5015 /* discard comments */
5016 char *r
= strchr(line
, '#');
5020 for (word
= strtok_r(line
, ", \t", &ws
);
5022 word
= strtok_r(NULL
, ", \t", &ws
)) {
5023 /* Find matching feature name */
5025 for (f
= 0; f
< SPA_FEATURES
; f
++) {
5026 zfeature_info_t
*fi
=
5027 &spa_feature_table
[f
];
5028 if (strcmp(word
, fi
->fi_uname
) == 0) {
5029 l_features
[f
] = B_TRUE
;
5033 if (f
< SPA_FEATURES
)
5036 /* found an unrecognized word */
5037 /* lightly sanitize it */
5038 if (strlen(word
) > 32)
5040 for (char *c
= word
; *c
!= '\0'; c
++)
5044 strlcat(err_badtoken
, word
, ZFS_MAXPROPLEN
);
5045 strlcat(err_badtoken
, " ", ZFS_MAXPROPLEN
);
5046 if (source
== Z_SYSCONF
)
5047 ret_badtoken
= B_TRUE
;
5049 ret_warntoken
= B_TRUE
;
5052 (void) munmap((void *) fc
, fs
.st_size
);
5054 if (features
!= NULL
)
5055 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
5056 features
[i
] &= l_features
[i
];
5058 (void) close(sdirfd
);
5059 (void) close(ddirfd
);
5061 /* Return the most serious error */
5064 snprintf(report
, rlen
, gettext("could not read/"
5065 "parse feature file(s): %s"), err_badfile
);
5066 return (ZPOOL_COMPATIBILITY_BADFILE
);
5071 gettext("no valid compatibility files specified"),
5073 return (ZPOOL_COMPATIBILITY_NOFILES
);
5077 snprintf(report
, rlen
, gettext("invalid feature "
5078 "name(s) in local compatibility files: %s"),
5080 return (ZPOOL_COMPATIBILITY_BADTOKEN
);
5082 if (ret_warntoken
) {
5084 snprintf(report
, rlen
, gettext("unrecognized feature "
5085 "name(s) in distribution compatibility files: %s"),
5087 return (ZPOOL_COMPATIBILITY_WARNTOKEN
);
5090 strlcpy(report
, gettext("compatibility set ok"), rlen
);
5091 return (ZPOOL_COMPATIBILITY_OK
);
5095 zpool_vdev_guid(zpool_handle_t
*zhp
, const char *vdevname
, uint64_t *vdev_guid
)
5098 boolean_t avail_spare
, l2cache
;
5100 verify(zhp
!= NULL
);
5101 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
5102 char errbuf
[ERRBUFLEN
];
5103 (void) snprintf(errbuf
, sizeof (errbuf
),
5104 dgettext(TEXT_DOMAIN
, "pool is in an unavailable state"));
5105 return (zfs_error(zhp
->zpool_hdl
, EZFS_POOLUNAVAIL
, errbuf
));
5108 if ((tgt
= zpool_find_vdev(zhp
, vdevname
, &avail_spare
, &l2cache
,
5110 char errbuf
[ERRBUFLEN
];
5111 (void) snprintf(errbuf
, sizeof (errbuf
),
5112 dgettext(TEXT_DOMAIN
, "can not find %s in %s"),
5113 vdevname
, zhp
->zpool_name
);
5114 return (zfs_error(zhp
->zpool_hdl
, EZFS_NODEVICE
, errbuf
));
5117 *vdev_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
5122 * Get a vdev property value for 'prop' and return the value in
5123 * a pre-allocated buffer.
5126 zpool_get_vdev_prop_value(nvlist_t
*nvprop
, vdev_prop_t prop
, char *prop_name
,
5127 char *buf
, size_t len
, zprop_source_t
*srctype
, boolean_t literal
)
5132 zprop_source_t src
= ZPROP_SRC_NONE
;
5134 if (prop
== VDEV_PROP_USERPROP
) {
5135 /* user property, prop_name must contain the property name */
5136 assert(prop_name
!= NULL
);
5137 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
5138 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
5139 strval
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
5141 /* user prop not found */
5144 (void) strlcpy(buf
, strval
, len
);
5150 if (prop_name
== NULL
)
5151 prop_name
= (char *)vdev_prop_to_name(prop
);
5153 switch (vdev_prop_get_type(prop
)) {
5154 case PROP_TYPE_STRING
:
5155 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
5156 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
5157 strval
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
5159 src
= ZPROP_SRC_DEFAULT
;
5160 if ((strval
= vdev_prop_default_string(prop
)) == NULL
)
5163 (void) strlcpy(buf
, strval
, len
);
5166 case PROP_TYPE_NUMBER
:
5167 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
5168 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
5169 intval
= fnvlist_lookup_uint64(nv
, ZPROP_VALUE
);
5171 src
= ZPROP_SRC_DEFAULT
;
5172 intval
= vdev_prop_default_numeric(prop
);
5176 case VDEV_PROP_ASIZE
:
5177 case VDEV_PROP_PSIZE
:
5178 case VDEV_PROP_SIZE
:
5179 case VDEV_PROP_BOOTSIZE
:
5180 case VDEV_PROP_ALLOCATED
:
5181 case VDEV_PROP_FREE
:
5182 case VDEV_PROP_READ_ERRORS
:
5183 case VDEV_PROP_WRITE_ERRORS
:
5184 case VDEV_PROP_CHECKSUM_ERRORS
:
5185 case VDEV_PROP_INITIALIZE_ERRORS
:
5186 case VDEV_PROP_OPS_NULL
:
5187 case VDEV_PROP_OPS_READ
:
5188 case VDEV_PROP_OPS_WRITE
:
5189 case VDEV_PROP_OPS_FREE
:
5190 case VDEV_PROP_OPS_CLAIM
:
5191 case VDEV_PROP_OPS_TRIM
:
5192 case VDEV_PROP_BYTES_NULL
:
5193 case VDEV_PROP_BYTES_READ
:
5194 case VDEV_PROP_BYTES_WRITE
:
5195 case VDEV_PROP_BYTES_FREE
:
5196 case VDEV_PROP_BYTES_CLAIM
:
5197 case VDEV_PROP_BYTES_TRIM
:
5199 (void) snprintf(buf
, len
, "%llu",
5200 (u_longlong_t
)intval
);
5202 (void) zfs_nicenum(intval
, buf
, len
);
5205 case VDEV_PROP_EXPANDSZ
:
5207 (void) strlcpy(buf
, "-", len
);
5208 } else if (literal
) {
5209 (void) snprintf(buf
, len
, "%llu",
5210 (u_longlong_t
)intval
);
5212 (void) zfs_nicenum(intval
, buf
, len
);
5215 case VDEV_PROP_CAPACITY
:
5217 (void) snprintf(buf
, len
, "%llu",
5218 (u_longlong_t
)intval
);
5220 (void) snprintf(buf
, len
, "%llu%%",
5221 (u_longlong_t
)intval
);
5224 case VDEV_PROP_CHECKSUM_N
:
5225 case VDEV_PROP_CHECKSUM_T
:
5226 case VDEV_PROP_IO_N
:
5227 case VDEV_PROP_IO_T
:
5228 if (intval
== UINT64_MAX
) {
5229 (void) strlcpy(buf
, "-", len
);
5231 (void) snprintf(buf
, len
, "%llu",
5232 (u_longlong_t
)intval
);
5235 case VDEV_PROP_FRAGMENTATION
:
5236 if (intval
== UINT64_MAX
) {
5237 (void) strlcpy(buf
, "-", len
);
5239 (void) snprintf(buf
, len
, "%llu%%",
5240 (u_longlong_t
)intval
);
5243 case VDEV_PROP_STATE
:
5245 (void) snprintf(buf
, len
, "%llu",
5246 (u_longlong_t
)intval
);
5248 (void) strlcpy(buf
, zpool_state_to_name(intval
,
5249 VDEV_AUX_NONE
), len
);
5253 (void) snprintf(buf
, len
, "%llu",
5254 (u_longlong_t
)intval
);
5258 case PROP_TYPE_INDEX
:
5259 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
5260 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
5261 intval
= fnvlist_lookup_uint64(nv
, ZPROP_VALUE
);
5263 src
= ZPROP_SRC_DEFAULT
;
5264 intval
= vdev_prop_default_numeric(prop
);
5265 /* Only use if provided by the RAIDZ VDEV above */
5266 if (prop
== VDEV_PROP_RAIDZ_EXPANDING
)
5269 if (vdev_prop_index_to_string(prop
, intval
,
5270 (const char **)&strval
) != 0)
5272 (void) strlcpy(buf
, strval
, len
);
5286 * Get a vdev property value for 'prop_name' and return the value in
5287 * a pre-allocated buffer.
5290 zpool_get_vdev_prop(zpool_handle_t
*zhp
, const char *vdevname
, vdev_prop_t prop
,
5291 char *prop_name
, char *buf
, size_t len
, zprop_source_t
*srctype
,
5294 nvlist_t
*reqnvl
, *reqprops
;
5295 nvlist_t
*retprops
= NULL
;
5296 uint64_t vdev_guid
= 0;
5299 if ((ret
= zpool_vdev_guid(zhp
, vdevname
, &vdev_guid
)) != 0)
5302 if (nvlist_alloc(&reqnvl
, NV_UNIQUE_NAME
, 0) != 0)
5303 return (no_memory(zhp
->zpool_hdl
));
5304 if (nvlist_alloc(&reqprops
, NV_UNIQUE_NAME
, 0) != 0)
5305 return (no_memory(zhp
->zpool_hdl
));
5307 fnvlist_add_uint64(reqnvl
, ZPOOL_VDEV_PROPS_GET_VDEV
, vdev_guid
);
5309 if (prop
!= VDEV_PROP_USERPROP
) {
5310 /* prop_name overrides prop value */
5311 if (prop_name
!= NULL
)
5312 prop
= vdev_name_to_prop(prop_name
);
5314 prop_name
= (char *)vdev_prop_to_name(prop
);
5315 assert(prop
< VDEV_NUM_PROPS
);
5318 assert(prop_name
!= NULL
);
5319 if (nvlist_add_uint64(reqprops
, prop_name
, prop
) != 0) {
5320 nvlist_free(reqnvl
);
5321 nvlist_free(reqprops
);
5322 return (no_memory(zhp
->zpool_hdl
));
5325 fnvlist_add_nvlist(reqnvl
, ZPOOL_VDEV_PROPS_GET_PROPS
, reqprops
);
5327 ret
= lzc_get_vdev_prop(zhp
->zpool_name
, reqnvl
, &retprops
);
5330 ret
= zpool_get_vdev_prop_value(retprops
, prop
, prop_name
, buf
,
5331 len
, srctype
, literal
);
5333 char errbuf
[ERRBUFLEN
];
5334 (void) snprintf(errbuf
, sizeof (errbuf
),
5335 dgettext(TEXT_DOMAIN
, "cannot get vdev property %s from"
5336 " %s in %s"), prop_name
, vdevname
, zhp
->zpool_name
);
5337 (void) zpool_standard_error(zhp
->zpool_hdl
, ret
, errbuf
);
5340 nvlist_free(reqnvl
);
5341 nvlist_free(reqprops
);
5342 nvlist_free(retprops
);
5348 * Get all vdev properties
5351 zpool_get_all_vdev_props(zpool_handle_t
*zhp
, const char *vdevname
,
5354 nvlist_t
*nvl
= NULL
;
5355 uint64_t vdev_guid
= 0;
5358 if ((ret
= zpool_vdev_guid(zhp
, vdevname
, &vdev_guid
)) != 0)
5361 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5362 return (no_memory(zhp
->zpool_hdl
));
5364 fnvlist_add_uint64(nvl
, ZPOOL_VDEV_PROPS_GET_VDEV
, vdev_guid
);
5366 ret
= lzc_get_vdev_prop(zhp
->zpool_name
, nvl
, outnvl
);
5371 char errbuf
[ERRBUFLEN
];
5372 (void) snprintf(errbuf
, sizeof (errbuf
),
5373 dgettext(TEXT_DOMAIN
, "cannot get vdev properties for"
5374 " %s in %s"), vdevname
, zhp
->zpool_name
);
5375 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
5385 zpool_set_vdev_prop(zpool_handle_t
*zhp
, const char *vdevname
,
5386 const char *propname
, const char *propval
)
5389 nvlist_t
*nvl
= NULL
;
5390 nvlist_t
*outnvl
= NULL
;
5392 nvlist_t
*realprops
;
5393 prop_flags_t flags
= { 0 };
5397 if ((ret
= zpool_vdev_guid(zhp
, vdevname
, &vdev_guid
)) != 0)
5400 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5401 return (no_memory(zhp
->zpool_hdl
));
5402 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
5403 return (no_memory(zhp
->zpool_hdl
));
5405 fnvlist_add_uint64(nvl
, ZPOOL_VDEV_PROPS_SET_VDEV
, vdev_guid
);
5407 if (nvlist_add_string(props
, propname
, propval
) != 0) {
5409 return (no_memory(zhp
->zpool_hdl
));
5412 char errbuf
[ERRBUFLEN
];
5413 (void) snprintf(errbuf
, sizeof (errbuf
),
5414 dgettext(TEXT_DOMAIN
, "cannot set property %s for %s on %s"),
5415 propname
, vdevname
, zhp
->zpool_name
);
5418 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
5419 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
5420 zhp
->zpool_name
, props
, version
, flags
, errbuf
)) == NULL
) {
5429 fnvlist_add_nvlist(nvl
, ZPOOL_VDEV_PROPS_SET_PROPS
, props
);
5431 ret
= lzc_set_vdev_prop(zhp
->zpool_name
, nvl
, &outnvl
);
5435 nvlist_free(outnvl
);
5438 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);