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, 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_MMP
) {
259 str
= gettext("SUSPENDED");
261 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(
262 zpool_get_config(zhp
, NULL
), ZPOOL_CONFIG_VDEV_TREE
);
264 vdev_stat_t
*vs
= (vdev_stat_t
*)fnvlist_lookup_uint64_array(
265 nvroot
, ZPOOL_CONFIG_VDEV_STATS
, &vsc
);
266 str
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
272 * Get a zpool property value for 'prop' and return the value in
273 * a pre-allocated buffer.
276 zpool_get_prop(zpool_handle_t
*zhp
, zpool_prop_t prop
, char *buf
,
277 size_t len
, zprop_source_t
*srctype
, boolean_t literal
)
281 zprop_source_t src
= ZPROP_SRC_NONE
;
283 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
285 case ZPOOL_PROP_NAME
:
286 (void) strlcpy(buf
, zpool_get_name(zhp
), len
);
289 case ZPOOL_PROP_HEALTH
:
290 (void) strlcpy(buf
, zpool_get_state_str(zhp
), len
);
293 case ZPOOL_PROP_GUID
:
294 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
295 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
298 case ZPOOL_PROP_ALTROOT
:
299 case ZPOOL_PROP_CACHEFILE
:
300 case ZPOOL_PROP_COMMENT
:
301 case ZPOOL_PROP_COMPATIBILITY
:
302 if (zhp
->zpool_props
!= NULL
||
303 zpool_get_all_props(zhp
) == 0) {
305 zpool_get_prop_string(zhp
, prop
, &src
),
311 (void) strlcpy(buf
, "-", len
);
320 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
) &&
321 prop
!= ZPOOL_PROP_NAME
)
324 switch (zpool_prop_get_type(prop
)) {
325 case PROP_TYPE_STRING
:
326 (void) strlcpy(buf
, zpool_get_prop_string(zhp
, prop
, &src
),
330 case PROP_TYPE_NUMBER
:
331 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
334 case ZPOOL_PROP_SIZE
:
335 case ZPOOL_PROP_ALLOCATED
:
336 case ZPOOL_PROP_FREE
:
337 case ZPOOL_PROP_FREEING
:
338 case ZPOOL_PROP_LEAKED
:
339 case ZPOOL_PROP_ASHIFT
:
340 case ZPOOL_PROP_MAXBLOCKSIZE
:
341 case ZPOOL_PROP_MAXDNODESIZE
:
342 case ZPOOL_PROP_BCLONESAVED
:
343 case ZPOOL_PROP_BCLONEUSED
:
345 (void) snprintf(buf
, len
, "%llu",
346 (u_longlong_t
)intval
);
348 (void) zfs_nicenum(intval
, buf
, len
);
351 case ZPOOL_PROP_EXPANDSZ
:
352 case ZPOOL_PROP_CHECKPOINT
:
354 (void) strlcpy(buf
, "-", len
);
355 } else if (literal
) {
356 (void) snprintf(buf
, len
, "%llu",
357 (u_longlong_t
)intval
);
359 (void) zfs_nicebytes(intval
, buf
, len
);
363 case ZPOOL_PROP_CAPACITY
:
365 (void) snprintf(buf
, len
, "%llu",
366 (u_longlong_t
)intval
);
368 (void) snprintf(buf
, len
, "%llu%%",
369 (u_longlong_t
)intval
);
373 case ZPOOL_PROP_FRAGMENTATION
:
374 if (intval
== UINT64_MAX
) {
375 (void) strlcpy(buf
, "-", len
);
376 } else if (literal
) {
377 (void) snprintf(buf
, len
, "%llu",
378 (u_longlong_t
)intval
);
380 (void) snprintf(buf
, len
, "%llu%%",
381 (u_longlong_t
)intval
);
385 case ZPOOL_PROP_BCLONERATIO
:
386 case ZPOOL_PROP_DEDUPRATIO
:
388 (void) snprintf(buf
, len
, "%llu.%02llu",
389 (u_longlong_t
)(intval
/ 100),
390 (u_longlong_t
)(intval
% 100));
392 (void) snprintf(buf
, len
, "%llu.%02llux",
393 (u_longlong_t
)(intval
/ 100),
394 (u_longlong_t
)(intval
% 100));
397 case ZPOOL_PROP_HEALTH
:
398 (void) strlcpy(buf
, zpool_get_state_str(zhp
), len
);
400 case ZPOOL_PROP_VERSION
:
401 if (intval
>= SPA_VERSION_FEATURES
) {
402 (void) snprintf(buf
, len
, "-");
407 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
411 case PROP_TYPE_INDEX
:
412 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
413 if (zpool_prop_index_to_string(prop
, intval
, &strval
)
416 (void) strlcpy(buf
, strval
, len
);
430 * Get a zpool property value for 'propname' and return the value in
431 * a pre-allocated buffer.
434 zpool_get_userprop(zpool_handle_t
*zhp
, const char *propname
, char *buf
,
435 size_t len
, zprop_source_t
*srctype
)
440 zprop_source_t source
= ZPROP_SRC_LOCAL
;
442 nvl
= zhp
->zpool_props
;
443 if (nvlist_lookup_nvlist(nvl
, propname
, &nv
) == 0) {
444 if (nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &ival
) == 0)
446 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
448 source
= ZPROP_SRC_DEFAULT
;
455 (void) strlcpy(buf
, value
, len
);
461 * Check if the bootfs name has the same pool name as it is set to.
462 * Assuming bootfs is a valid dataset name.
465 bootfs_name_valid(const char *pool
, const char *bootfs
)
467 int len
= strlen(pool
);
468 if (bootfs
[0] == '\0')
471 if (!zfs_name_valid(bootfs
, ZFS_TYPE_FILESYSTEM
|ZFS_TYPE_SNAPSHOT
))
474 if (strncmp(pool
, bootfs
, len
) == 0 &&
475 (bootfs
[len
] == '/' || bootfs
[len
] == '\0'))
482 * Given an nvlist of zpool properties to be set, validate that they are
483 * correct, and parse any numeric properties (index, boolean, etc) if they are
484 * specified as strings.
487 zpool_valid_proplist(libzfs_handle_t
*hdl
, const char *poolname
,
488 nvlist_t
*props
, uint64_t version
, prop_flags_t flags
, char *errbuf
)
495 const char *slash
, *check
;
496 struct stat64 statbuf
;
500 if (nvlist_alloc(&retprops
, NV_UNIQUE_NAME
, 0) != 0) {
501 (void) no_memory(hdl
);
506 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
507 const char *propname
= nvpair_name(elem
);
509 if (flags
.vdevprop
&& zpool_prop_vdev(propname
)) {
510 vdev_prop_t vprop
= vdev_name_to_prop(propname
);
512 if (vdev_prop_readonly(vprop
)) {
513 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
514 "is readonly"), propname
);
515 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
520 if (zprop_parse_value(hdl
, elem
, vprop
, ZFS_TYPE_VDEV
,
521 retprops
, &strval
, &intval
, errbuf
) != 0)
525 } else if (flags
.vdevprop
&& vdev_prop_user(propname
)) {
526 if (nvlist_add_nvpair(retprops
, elem
) != 0) {
527 (void) no_memory(hdl
);
531 } else if (flags
.vdevprop
) {
532 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
533 "invalid property: '%s'"), propname
);
534 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
538 prop
= zpool_name_to_prop(propname
);
539 if (prop
== ZPOOL_PROP_INVAL
&& zpool_prop_feature(propname
)) {
541 char *fname
= strchr(propname
, '@') + 1;
543 err
= zfeature_lookup_name(fname
, NULL
);
545 ASSERT3U(err
, ==, ENOENT
);
546 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
547 "feature '%s' unsupported by kernel"),
549 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
553 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
554 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
555 "'%s' must be a string"), propname
);
556 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
560 (void) nvpair_value_string(elem
, &strval
);
561 if (strcmp(strval
, ZFS_FEATURE_ENABLED
) != 0 &&
562 strcmp(strval
, ZFS_FEATURE_DISABLED
) != 0) {
563 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
564 "property '%s' can only be set to "
565 "'enabled' or 'disabled'"), propname
);
566 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
571 strcmp(strval
, ZFS_FEATURE_DISABLED
) == 0) {
572 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
573 "property '%s' can only be set to "
574 "'disabled' at creation time"), propname
);
575 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
579 if (nvlist_add_uint64(retprops
, propname
, 0) != 0) {
580 (void) no_memory(hdl
);
584 } else if (prop
== ZPOOL_PROP_INVAL
&&
585 zfs_prop_user(propname
)) {
587 * This is a user property: make sure it's a
588 * string, and that it's less than ZAP_MAXNAMELEN.
590 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
591 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
592 "'%s' must be a string"), propname
);
593 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
597 if (strlen(nvpair_name(elem
)) >= ZAP_MAXNAMELEN
) {
598 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
599 "property name '%s' is too long"),
601 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
605 (void) nvpair_value_string(elem
, &strval
);
607 if (strlen(strval
) >= ZFS_MAXPROPLEN
) {
608 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
609 "property value '%s' is too long"),
611 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
615 if (nvlist_add_string(retprops
, propname
,
617 (void) no_memory(hdl
);
625 * Make sure this property is valid and applies to this type.
627 if (prop
== ZPOOL_PROP_INVAL
) {
628 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
629 "invalid property '%s'"), propname
);
630 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
634 if (zpool_prop_readonly(prop
)) {
635 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
636 "is readonly"), propname
);
637 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
641 if (!flags
.create
&& zpool_prop_setonce(prop
)) {
642 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
643 "property '%s' can only be set at "
644 "creation time"), propname
);
645 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
649 if (zprop_parse_value(hdl
, elem
, prop
, ZFS_TYPE_POOL
, retprops
,
650 &strval
, &intval
, errbuf
) != 0)
654 * Perform additional checking for specific properties.
657 case ZPOOL_PROP_VERSION
:
658 if (intval
< version
||
659 !SPA_VERSION_IS_SUPPORTED(intval
)) {
660 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
661 "property '%s' number %llu is invalid."),
662 propname
, (unsigned long long)intval
);
663 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
668 case ZPOOL_PROP_ASHIFT
:
670 (intval
< ASHIFT_MIN
|| intval
> ASHIFT_MAX
)) {
671 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
672 "property '%s' number %llu is invalid, "
673 "only values between %" PRId32
" and %"
674 PRId32
" are allowed."),
675 propname
, (unsigned long long)intval
,
676 ASHIFT_MIN
, ASHIFT_MAX
);
677 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
682 case ZPOOL_PROP_BOOTFS
:
683 if (flags
.create
|| flags
.import
) {
684 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
685 "property '%s' cannot be set at creation "
686 "or import time"), propname
);
687 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
691 if (version
< SPA_VERSION_BOOTFS
) {
692 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
693 "pool must be upgraded to support "
694 "'%s' property"), propname
);
695 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
700 * bootfs property value has to be a dataset name and
701 * the dataset has to be in the same pool as it sets to.
703 if (!bootfs_name_valid(poolname
, strval
)) {
704 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
705 "is an invalid name"), strval
);
706 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
710 if ((zhp
= zpool_open_canfail(hdl
, poolname
)) == NULL
) {
711 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
712 "could not open pool '%s'"), poolname
);
713 (void) zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
);
719 case ZPOOL_PROP_ALTROOT
:
720 if (!flags
.create
&& !flags
.import
) {
721 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
722 "property '%s' can only be set during pool "
723 "creation or import"), propname
);
724 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
728 if (strval
[0] != '/') {
729 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
730 "bad alternate root '%s'"), strval
);
731 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
736 case ZPOOL_PROP_CACHEFILE
:
737 if (strval
[0] == '\0')
740 if (strcmp(strval
, "none") == 0)
743 if (strval
[0] != '/') {
744 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
745 "property '%s' must be empty, an "
746 "absolute path, or 'none'"), propname
);
747 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
751 slash
= strrchr(strval
, '/');
753 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
754 strcmp(slash
, "/..") == 0) {
755 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
756 "'%s' is not a valid file"), strval
);
757 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
761 *(char *)slash
= '\0';
763 if (strval
[0] != '\0' &&
764 (stat64(strval
, &statbuf
) != 0 ||
765 !S_ISDIR(statbuf
.st_mode
))) {
766 *(char *)slash
= '/';
767 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
768 "'%s' is not a valid directory"),
770 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
774 *(char *)slash
= '/';
777 case ZPOOL_PROP_COMPATIBILITY
:
778 switch (zpool_load_compat(strval
, NULL
, report
, 1024)) {
779 case ZPOOL_COMPATIBILITY_OK
:
780 case ZPOOL_COMPATIBILITY_WARNTOKEN
:
782 case ZPOOL_COMPATIBILITY_BADFILE
:
783 case ZPOOL_COMPATIBILITY_BADTOKEN
:
784 case ZPOOL_COMPATIBILITY_NOFILES
:
785 zfs_error_aux(hdl
, "%s", report
);
786 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
791 case ZPOOL_PROP_COMMENT
:
792 for (check
= strval
; *check
!= '\0'; check
++) {
793 if (!isprint(*check
)) {
795 dgettext(TEXT_DOMAIN
,
796 "comment may only have printable "
798 (void) zfs_error(hdl
, EZFS_BADPROP
,
803 if (strlen(strval
) > ZPROP_MAX_COMMENT
) {
804 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
805 "comment must not exceed %d characters"),
807 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
811 case ZPOOL_PROP_READONLY
:
813 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
814 "property '%s' can only be set at "
815 "import time"), propname
);
816 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
820 case ZPOOL_PROP_MULTIHOST
:
821 if (get_system_hostid() == 0) {
822 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
823 "requires a non-zero system hostid"));
824 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
828 case ZPOOL_PROP_DEDUPDITTO
:
829 printf("Note: property '%s' no longer has "
830 "any effect\n", propname
);
840 nvlist_free(retprops
);
845 * Set zpool property : propname=propval.
848 zpool_set_prop(zpool_handle_t
*zhp
, const char *propname
, const char *propval
)
850 zfs_cmd_t zc
= {"\0"};
852 char errbuf
[ERRBUFLEN
];
853 nvlist_t
*nvl
= NULL
;
856 prop_flags_t flags
= { 0 };
858 (void) snprintf(errbuf
, sizeof (errbuf
),
859 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
862 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
863 return (no_memory(zhp
->zpool_hdl
));
865 if (nvlist_add_string(nvl
, propname
, propval
) != 0) {
867 return (no_memory(zhp
->zpool_hdl
));
870 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
871 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
872 zhp
->zpool_name
, nvl
, version
, flags
, errbuf
)) == NULL
) {
881 * Execute the corresponding ioctl() to set this property.
883 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
885 zcmd_write_src_nvlist(zhp
->zpool_hdl
, &zc
, nvl
);
887 ret
= zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_SET_PROPS
, &zc
);
889 zcmd_free_nvlists(&zc
);
893 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
895 (void) zpool_props_refresh(zhp
);
901 zpool_expand_proplist(zpool_handle_t
*zhp
, zprop_list_t
**plp
,
902 zfs_type_t type
, boolean_t literal
)
904 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
906 char buf
[ZFS_MAXPROPLEN
];
907 nvlist_t
*features
= NULL
;
910 boolean_t firstexpand
= (NULL
== *plp
);
913 if (zprop_expand_list(hdl
, plp
, type
) != 0)
916 if (type
== ZFS_TYPE_VDEV
)
920 while (*last
!= NULL
)
921 last
= &(*last
)->pl_next
;
924 features
= zpool_get_features(zhp
);
926 if ((*plp
)->pl_all
&& firstexpand
) {
927 /* Handle userprops in the all properties case */
928 if (zhp
->zpool_props
== NULL
&& zpool_props_refresh(zhp
))
932 while ((nvp
= nvlist_next_nvpair(zhp
->zpool_props
, nvp
)) !=
934 const char *propname
= nvpair_name(nvp
);
936 if (!zfs_prop_user(propname
))
939 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
940 entry
->pl_prop
= ZPROP_USERPROP
;
941 entry
->pl_user_prop
= zfs_strdup(hdl
, propname
);
942 entry
->pl_width
= strlen(entry
->pl_user_prop
);
943 entry
->pl_all
= B_TRUE
;
946 last
= &entry
->pl_next
;
949 for (i
= 0; i
< SPA_FEATURES
; i
++) {
950 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
951 entry
->pl_prop
= ZPROP_USERPROP
;
952 entry
->pl_user_prop
= zfs_asprintf(hdl
, "feature@%s",
953 spa_feature_table
[i
].fi_uname
);
954 entry
->pl_width
= strlen(entry
->pl_user_prop
);
955 entry
->pl_all
= B_TRUE
;
958 last
= &entry
->pl_next
;
962 /* add any unsupported features */
963 for (nvp
= nvlist_next_nvpair(features
, NULL
);
964 nvp
!= NULL
; nvp
= nvlist_next_nvpair(features
, nvp
)) {
968 if (zfeature_is_supported(nvpair_name(nvp
)))
971 propname
= zfs_asprintf(hdl
, "unsupported@%s",
975 * Before adding the property to the list make sure that no
976 * other pool already added the same property.
980 while (entry
!= NULL
) {
981 if (entry
->pl_user_prop
!= NULL
&&
982 strcmp(propname
, entry
->pl_user_prop
) == 0) {
986 entry
= entry
->pl_next
;
993 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
994 entry
->pl_prop
= ZPROP_USERPROP
;
995 entry
->pl_user_prop
= propname
;
996 entry
->pl_width
= strlen(entry
->pl_user_prop
);
997 entry
->pl_all
= B_TRUE
;
1000 last
= &entry
->pl_next
;
1003 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
1004 if (entry
->pl_fixed
&& !literal
)
1007 if (entry
->pl_prop
!= ZPROP_USERPROP
&&
1008 zpool_get_prop(zhp
, entry
->pl_prop
, buf
, sizeof (buf
),
1009 NULL
, literal
) == 0) {
1010 if (strlen(buf
) > entry
->pl_width
)
1011 entry
->pl_width
= strlen(buf
);
1012 } else if (entry
->pl_prop
== ZPROP_INVAL
&&
1013 zfs_prop_user(entry
->pl_user_prop
) &&
1014 zpool_get_userprop(zhp
, entry
->pl_user_prop
, buf
,
1015 sizeof (buf
), NULL
) == 0) {
1016 if (strlen(buf
) > entry
->pl_width
)
1017 entry
->pl_width
= strlen(buf
);
1025 vdev_expand_proplist(zpool_handle_t
*zhp
, const char *vdevname
,
1028 zprop_list_t
*entry
;
1029 char buf
[ZFS_MAXPROPLEN
];
1030 const char *strval
= NULL
;
1032 nvpair_t
*elem
= NULL
;
1033 nvlist_t
*vprops
= NULL
;
1034 nvlist_t
*propval
= NULL
;
1035 const char *propname
;
1037 zprop_list_t
**last
;
1039 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
1040 if (entry
->pl_fixed
)
1043 if (zpool_get_vdev_prop(zhp
, vdevname
, entry
->pl_prop
,
1044 entry
->pl_user_prop
, buf
, sizeof (buf
), NULL
,
1046 if (strlen(buf
) > entry
->pl_width
)
1047 entry
->pl_width
= strlen(buf
);
1049 if (entry
->pl_prop
== VDEV_PROP_NAME
&&
1050 strlen(vdevname
) > entry
->pl_width
)
1051 entry
->pl_width
= strlen(vdevname
);
1054 /* Handle the all properties case */
1056 if (*last
!= NULL
&& (*last
)->pl_all
== B_TRUE
) {
1057 while (*last
!= NULL
)
1058 last
= &(*last
)->pl_next
;
1060 err
= zpool_get_all_vdev_props(zhp
, vdevname
, &vprops
);
1064 while ((elem
= nvlist_next_nvpair(vprops
, elem
)) != NULL
) {
1065 propname
= nvpair_name(elem
);
1067 /* Skip properties that are not user defined */
1068 if ((prop
= vdev_name_to_prop(propname
)) !=
1072 if (nvpair_value_nvlist(elem
, &propval
) != 0)
1075 strval
= fnvlist_lookup_string(propval
, ZPROP_VALUE
);
1077 entry
= zfs_alloc(zhp
->zpool_hdl
,
1078 sizeof (zprop_list_t
));
1079 entry
->pl_prop
= prop
;
1080 entry
->pl_user_prop
= zfs_strdup(zhp
->zpool_hdl
,
1082 entry
->pl_width
= strlen(strval
);
1083 entry
->pl_all
= B_TRUE
;
1085 last
= &entry
->pl_next
;
1093 * Get the state for the given feature on the given ZFS pool.
1096 zpool_prop_get_feature(zpool_handle_t
*zhp
, const char *propname
, char *buf
,
1100 boolean_t found
= B_FALSE
;
1101 nvlist_t
*features
= zpool_get_features(zhp
);
1102 boolean_t supported
;
1103 const char *feature
= strchr(propname
, '@') + 1;
1105 supported
= zpool_prop_feature(propname
);
1106 ASSERT(supported
|| zpool_prop_unsupported(propname
));
1109 * Convert from feature name to feature guid. This conversion is
1110 * unnecessary for unsupported@... properties because they already
1117 ret
= zfeature_lookup_name(feature
, &fid
);
1119 (void) strlcpy(buf
, "-", len
);
1122 feature
= spa_feature_table
[fid
].fi_guid
;
1125 if (nvlist_lookup_uint64(features
, feature
, &refcount
) == 0)
1130 (void) strlcpy(buf
, ZFS_FEATURE_DISABLED
, len
);
1133 (void) strlcpy(buf
, ZFS_FEATURE_ENABLED
, len
);
1135 (void) strlcpy(buf
, ZFS_FEATURE_ACTIVE
, len
);
1139 if (refcount
== 0) {
1140 (void) strcpy(buf
, ZFS_UNSUPPORTED_INACTIVE
);
1142 (void) strcpy(buf
, ZFS_UNSUPPORTED_READONLY
);
1145 (void) strlcpy(buf
, "-", len
);
1154 * Validate the given pool name, optionally putting an extended error message in
1158 zpool_name_valid(libzfs_handle_t
*hdl
, boolean_t isopen
, const char *pool
)
1160 namecheck_err_t why
;
1164 ret
= pool_namecheck(pool
, &why
, &what
);
1167 * The rules for reserved pool names were extended at a later point.
1168 * But we need to support users with existing pools that may now be
1169 * invalid. So we only check for this expanded set of names during a
1170 * create (or import), and only in userland.
1172 if (ret
== 0 && !isopen
&&
1173 (strncmp(pool
, "mirror", 6) == 0 ||
1174 strncmp(pool
, "raidz", 5) == 0 ||
1175 strncmp(pool
, "draid", 5) == 0 ||
1176 strncmp(pool
, "spare", 5) == 0 ||
1177 strcmp(pool
, "log") == 0)) {
1180 dgettext(TEXT_DOMAIN
, "name is reserved"));
1188 case NAME_ERR_TOOLONG
:
1190 dgettext(TEXT_DOMAIN
, "name is too long"));
1193 case NAME_ERR_INVALCHAR
:
1195 dgettext(TEXT_DOMAIN
, "invalid character "
1196 "'%c' in pool name"), what
);
1199 case NAME_ERR_NOLETTER
:
1200 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1201 "name must begin with a letter"));
1204 case NAME_ERR_RESERVED
:
1205 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1206 "name is reserved"));
1209 case NAME_ERR_DISKLIKE
:
1210 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1211 "pool name is reserved"));
1214 case NAME_ERR_LEADING_SLASH
:
1215 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1216 "leading slash in name"));
1219 case NAME_ERR_EMPTY_COMPONENT
:
1220 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1221 "empty component in name"));
1224 case NAME_ERR_TRAILING_SLASH
:
1225 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1226 "trailing slash in name"));
1229 case NAME_ERR_MULTIPLE_DELIMITERS
:
1230 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1231 "multiple '@' and/or '#' delimiters in "
1235 case NAME_ERR_NO_AT
:
1236 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1237 "permission set is missing '@'"));
1241 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1242 "(%d) not defined"), why
);
1253 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1257 zpool_open_canfail(libzfs_handle_t
*hdl
, const char *pool
)
1259 zpool_handle_t
*zhp
;
1263 * Make sure the pool name is valid.
1265 if (!zpool_name_valid(hdl
, B_TRUE
, pool
)) {
1266 (void) zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1267 dgettext(TEXT_DOMAIN
, "cannot open '%s'"),
1272 zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
));
1274 zhp
->zpool_hdl
= hdl
;
1275 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1277 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1283 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "no such pool"));
1284 (void) zfs_error_fmt(hdl
, EZFS_NOENT
,
1285 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), pool
);
1294 * Like the above, but silent on error. Used when iterating over pools (because
1295 * the configuration cache may be out of date).
1298 zpool_open_silent(libzfs_handle_t
*hdl
, const char *pool
, zpool_handle_t
**ret
)
1300 zpool_handle_t
*zhp
;
1303 zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
));
1305 zhp
->zpool_hdl
= hdl
;
1306 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1308 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1324 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1328 zpool_open(libzfs_handle_t
*hdl
, const char *pool
)
1330 zpool_handle_t
*zhp
;
1332 if ((zhp
= zpool_open_canfail(hdl
, pool
)) == NULL
)
1335 if (zhp
->zpool_state
== POOL_STATE_UNAVAIL
) {
1336 (void) zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
1337 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), zhp
->zpool_name
);
1346 * Close the handle. Simply frees the memory associated with the handle.
1349 zpool_close(zpool_handle_t
*zhp
)
1351 nvlist_free(zhp
->zpool_config
);
1352 nvlist_free(zhp
->zpool_old_config
);
1353 nvlist_free(zhp
->zpool_props
);
1358 * Return the name of the pool.
1361 zpool_get_name(zpool_handle_t
*zhp
)
1363 return (zhp
->zpool_name
);
1368 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1371 zpool_get_state(zpool_handle_t
*zhp
)
1373 return (zhp
->zpool_state
);
1377 * Check if vdev list contains a special vdev
1380 zpool_has_special_vdev(nvlist_t
*nvroot
)
1385 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
, &child
,
1387 for (uint_t c
= 0; c
< children
; c
++) {
1390 if (nvlist_lookup_string(child
[c
],
1391 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
) == 0 &&
1392 strcmp(bias
, VDEV_ALLOC_BIAS_SPECIAL
) == 0) {
1401 * Check if vdev list contains a dRAID vdev
1404 zpool_has_draid_vdev(nvlist_t
*nvroot
)
1409 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
1410 &child
, &children
) == 0) {
1411 for (uint_t c
= 0; c
< children
; c
++) {
1414 if (nvlist_lookup_string(child
[c
],
1415 ZPOOL_CONFIG_TYPE
, &type
) == 0 &&
1416 strcmp(type
, VDEV_TYPE_DRAID
) == 0) {
1425 * Output a dRAID top-level vdev name in to the provided buffer.
1428 zpool_draid_name(char *name
, int len
, uint64_t data
, uint64_t parity
,
1429 uint64_t spares
, uint64_t children
)
1431 snprintf(name
, len
, "%s%llu:%llud:%lluc:%llus",
1432 VDEV_TYPE_DRAID
, (u_longlong_t
)parity
, (u_longlong_t
)data
,
1433 (u_longlong_t
)children
, (u_longlong_t
)spares
);
1439 * Return B_TRUE if the provided name is a dRAID spare name.
1442 zpool_is_draid_spare(const char *name
)
1444 uint64_t spare_id
, parity
, vdev_id
;
1446 if (sscanf(name
, VDEV_TYPE_DRAID
"%llu-%llu-%llu",
1447 (u_longlong_t
*)&parity
, (u_longlong_t
*)&vdev_id
,
1448 (u_longlong_t
*)&spare_id
) == 3) {
1456 * Create the named pool, using the provided vdev list. It is assumed
1457 * that the consumer has already validated the contents of the nvlist, so we
1458 * don't have to worry about error semantics.
1461 zpool_create(libzfs_handle_t
*hdl
, const char *pool
, nvlist_t
*nvroot
,
1462 nvlist_t
*props
, nvlist_t
*fsprops
)
1464 zfs_cmd_t zc
= {"\0"};
1465 nvlist_t
*zc_fsprops
= NULL
;
1466 nvlist_t
*zc_props
= NULL
;
1467 nvlist_t
*hidden_args
= NULL
;
1468 uint8_t *wkeydata
= NULL
;
1470 char errbuf
[ERRBUFLEN
];
1473 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1474 "cannot create '%s'"), pool
);
1476 if (!zpool_name_valid(hdl
, B_FALSE
, pool
))
1477 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
1479 zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
);
1482 prop_flags_t flags
= { .create
= B_TRUE
, .import
= B_FALSE
};
1484 if ((zc_props
= zpool_valid_proplist(hdl
, pool
, props
,
1485 SPA_VERSION_1
, flags
, errbuf
)) == NULL
) {
1492 const char *zonestr
;
1494 zoned
= ((nvlist_lookup_string(fsprops
,
1495 zfs_prop_to_name(ZFS_PROP_ZONED
), &zonestr
) == 0) &&
1496 strcmp(zonestr
, "on") == 0);
1498 if ((zc_fsprops
= zfs_valid_proplist(hdl
, ZFS_TYPE_FILESYSTEM
,
1499 fsprops
, zoned
, NULL
, NULL
, B_TRUE
, errbuf
)) == NULL
) {
1503 if (nvlist_exists(zc_fsprops
,
1504 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS
)) &&
1505 !zpool_has_special_vdev(nvroot
)) {
1506 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1507 "%s property requires a special vdev"),
1508 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS
));
1509 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1514 (nvlist_alloc(&zc_props
, NV_UNIQUE_NAME
, 0) != 0)) {
1517 if (zfs_crypto_create(hdl
, NULL
, zc_fsprops
, props
, B_TRUE
,
1518 &wkeydata
, &wkeylen
) != 0) {
1519 zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
);
1522 if (nvlist_add_nvlist(zc_props
,
1523 ZPOOL_ROOTFS_PROPS
, zc_fsprops
) != 0) {
1526 if (wkeydata
!= NULL
) {
1527 if (nvlist_alloc(&hidden_args
, NV_UNIQUE_NAME
, 0) != 0)
1530 if (nvlist_add_uint8_array(hidden_args
, "wkeydata",
1531 wkeydata
, wkeylen
) != 0)
1534 if (nvlist_add_nvlist(zc_props
, ZPOOL_HIDDEN_ARGS
,
1541 zcmd_write_src_nvlist(hdl
, &zc
, zc_props
);
1543 (void) strlcpy(zc
.zc_name
, pool
, sizeof (zc
.zc_name
));
1545 if ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_CREATE
, &zc
)) != 0) {
1547 zcmd_free_nvlists(&zc
);
1548 nvlist_free(zc_props
);
1549 nvlist_free(zc_fsprops
);
1550 nvlist_free(hidden_args
);
1551 if (wkeydata
!= NULL
)
1557 * This can happen if the user has specified the same
1558 * device multiple times. We can't reliably detect this
1559 * until we try to add it and see we already have a
1560 * label. This can also happen under if the device is
1561 * part of an active md or lvm device.
1563 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1564 "one or more vdevs refer to the same device, or "
1565 "one of\nthe devices is part of an active md or "
1567 return (zfs_error(hdl
, EZFS_BADDEV
, errbuf
));
1571 * This happens if the record size is smaller or larger
1572 * than the allowed size range, or not a power of 2.
1574 * NOTE: although zfs_valid_proplist is called earlier,
1575 * this case may have slipped through since the
1576 * pool does not exist yet and it is therefore
1577 * impossible to read properties e.g. max blocksize
1580 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1581 "record size invalid"));
1582 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1586 * This occurs when one of the devices is below
1587 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1588 * device was the problem device since there's no
1589 * reliable way to determine device size from userland.
1594 zfs_nicebytes(SPA_MINDEVSIZE
, buf
,
1597 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1598 "one or more devices is less than the "
1599 "minimum size (%s)"), buf
);
1601 return (zfs_error(hdl
, EZFS_BADDEV
, errbuf
));
1604 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1605 "one or more devices is out of space"));
1606 return (zfs_error(hdl
, EZFS_BADDEV
, errbuf
));
1609 if (zpool_has_draid_vdev(nvroot
) &&
1610 zfeature_lookup_name("draid", NULL
) != 0) {
1611 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1612 "dRAID vdevs are unsupported by the "
1614 return (zfs_error(hdl
, EZFS_BADDEV
, errbuf
));
1616 return (zpool_standard_error(hdl
, errno
,
1621 return (zpool_standard_error(hdl
, errno
, errbuf
));
1626 zcmd_free_nvlists(&zc
);
1627 nvlist_free(zc_props
);
1628 nvlist_free(zc_fsprops
);
1629 nvlist_free(hidden_args
);
1630 if (wkeydata
!= NULL
)
1636 * Destroy the given pool. It is up to the caller to ensure that there are no
1637 * datasets left in the pool.
1640 zpool_destroy(zpool_handle_t
*zhp
, const char *log_str
)
1642 zfs_cmd_t zc
= {"\0"};
1643 zfs_handle_t
*zfp
= NULL
;
1644 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1645 char errbuf
[ERRBUFLEN
];
1647 if (zhp
->zpool_state
== POOL_STATE_ACTIVE
&&
1648 (zfp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
1651 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1652 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1654 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_DESTROY
, &zc
) != 0) {
1655 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1656 "cannot destroy '%s'"), zhp
->zpool_name
);
1658 if (errno
== EROFS
) {
1659 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1660 "one or more devices is read only"));
1661 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
1663 (void) zpool_standard_error(hdl
, errno
, errbuf
);
1672 remove_mountpoint(zfp
);
1680 * Create a checkpoint in the given pool.
1683 zpool_checkpoint(zpool_handle_t
*zhp
)
1685 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1686 char errbuf
[ERRBUFLEN
];
1689 error
= lzc_pool_checkpoint(zhp
->zpool_name
);
1691 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1692 "cannot checkpoint '%s'"), zhp
->zpool_name
);
1693 (void) zpool_standard_error(hdl
, error
, errbuf
);
1701 * Discard the checkpoint from the given pool.
1704 zpool_discard_checkpoint(zpool_handle_t
*zhp
)
1706 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1707 char errbuf
[ERRBUFLEN
];
1710 error
= lzc_pool_checkpoint_discard(zhp
->zpool_name
);
1712 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1713 "cannot discard checkpoint in '%s'"), zhp
->zpool_name
);
1714 (void) zpool_standard_error(hdl
, error
, errbuf
);
1722 * Add the given vdevs to the pool. The caller must have already performed the
1723 * necessary verification to ensure that the vdev specification is well-formed.
1726 zpool_add(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
1728 zfs_cmd_t zc
= {"\0"};
1730 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1731 char errbuf
[ERRBUFLEN
];
1732 nvlist_t
**spares
, **l2cache
;
1733 uint_t nspares
, nl2cache
;
1735 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1736 "cannot add to '%s'"), zhp
->zpool_name
);
1738 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1739 SPA_VERSION_SPARES
&&
1740 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1741 &spares
, &nspares
) == 0) {
1742 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1743 "upgraded to add hot spares"));
1744 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
1747 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1748 SPA_VERSION_L2CACHE
&&
1749 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1750 &l2cache
, &nl2cache
) == 0) {
1751 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1752 "upgraded to add cache devices"));
1753 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
1756 zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
);
1757 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1759 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_ADD
, &zc
) != 0) {
1763 * This can happen if the user has specified the same
1764 * device multiple times. We can't reliably detect this
1765 * until we try to add it and see we already have a
1768 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1769 "one or more vdevs refer to the same device"));
1770 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
1775 if (zpool_has_draid_vdev(nvroot
) &&
1776 zfeature_lookup_name("draid", NULL
) != 0) {
1777 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1778 "dRAID vdevs are unsupported by the "
1781 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1782 "invalid config; a pool with removing/"
1783 "removed vdevs does not support adding "
1784 "raidz or dRAID vdevs"));
1787 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
1792 * This occurs when one of the devices is below
1793 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1794 * device was the problem device since there's no
1795 * reliable way to determine device size from userland.
1800 zfs_nicebytes(SPA_MINDEVSIZE
, buf
,
1803 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1804 "device is less than the minimum "
1807 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
1811 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1812 "pool must be upgraded to add these vdevs"));
1813 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1817 (void) zpool_standard_error(hdl
, errno
, errbuf
);
1825 zcmd_free_nvlists(&zc
);
1831 * Exports the pool from the system. The caller must ensure that there are no
1832 * mounted datasets in the pool.
1835 zpool_export_common(zpool_handle_t
*zhp
, boolean_t force
, boolean_t hardforce
,
1836 const char *log_str
)
1838 zfs_cmd_t zc
= {"\0"};
1840 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1841 zc
.zc_cookie
= force
;
1842 zc
.zc_guid
= hardforce
;
1843 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1845 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_EXPORT
, &zc
) != 0) {
1848 zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
1849 "use '-f' to override the following errors:\n"
1850 "'%s' has an active shared spare which could be"
1851 " used by other pools once '%s' is exported."),
1852 zhp
->zpool_name
, zhp
->zpool_name
);
1853 return (zfs_error_fmt(zhp
->zpool_hdl
, EZFS_ACTIVE_SPARE
,
1854 dgettext(TEXT_DOMAIN
, "cannot export '%s'"),
1857 return (zpool_standard_error_fmt(zhp
->zpool_hdl
, errno
,
1858 dgettext(TEXT_DOMAIN
, "cannot export '%s'"),
1867 zpool_export(zpool_handle_t
*zhp
, boolean_t force
, const char *log_str
)
1869 return (zpool_export_common(zhp
, force
, B_FALSE
, log_str
));
1873 zpool_export_force(zpool_handle_t
*zhp
, const char *log_str
)
1875 return (zpool_export_common(zhp
, B_TRUE
, B_TRUE
, log_str
));
1879 zpool_rewind_exclaim(libzfs_handle_t
*hdl
, const char *name
, boolean_t dryrun
,
1882 nvlist_t
*nv
= NULL
;
1888 if (!hdl
->libzfs_printerr
|| config
== NULL
)
1891 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1892 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0) {
1896 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1898 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1900 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1901 strftime(timestr
, 128, "%c", &t
) != 0) {
1903 (void) printf(dgettext(TEXT_DOMAIN
,
1904 "Would be able to return %s "
1905 "to its state as of %s.\n"),
1908 (void) printf(dgettext(TEXT_DOMAIN
,
1909 "Pool %s returned to its state as of %s.\n"),
1913 (void) printf(dgettext(TEXT_DOMAIN
,
1914 "%s approximately %lld "),
1915 dryrun
? "Would discard" : "Discarded",
1916 ((longlong_t
)loss
+ 30) / 60);
1917 (void) printf(dgettext(TEXT_DOMAIN
,
1918 "minutes of transactions.\n"));
1919 } else if (loss
> 0) {
1920 (void) printf(dgettext(TEXT_DOMAIN
,
1921 "%s approximately %lld "),
1922 dryrun
? "Would discard" : "Discarded",
1924 (void) printf(dgettext(TEXT_DOMAIN
,
1925 "seconds of transactions.\n"));
1931 zpool_explain_recover(libzfs_handle_t
*hdl
, const char *name
, int reason
,
1934 nvlist_t
*nv
= NULL
;
1936 uint64_t edata
= UINT64_MAX
;
1941 if (!hdl
->libzfs_printerr
)
1945 (void) printf(dgettext(TEXT_DOMAIN
, "action: "));
1947 (void) printf(dgettext(TEXT_DOMAIN
, "\t"));
1949 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1950 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1951 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0 ||
1952 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1955 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1956 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_DATA_ERRORS
,
1959 (void) printf(dgettext(TEXT_DOMAIN
,
1960 "Recovery is possible, but will result in some data loss.\n"));
1962 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1963 strftime(timestr
, 128, "%c", &t
) != 0) {
1964 (void) printf(dgettext(TEXT_DOMAIN
,
1965 "\tReturning the pool to its state as of %s\n"
1966 "\tshould correct the problem. "),
1969 (void) printf(dgettext(TEXT_DOMAIN
,
1970 "\tReverting the pool to an earlier state "
1971 "should correct the problem.\n\t"));
1975 (void) printf(dgettext(TEXT_DOMAIN
,
1976 "Approximately %lld minutes of data\n"
1977 "\tmust be discarded, irreversibly. "),
1978 ((longlong_t
)loss
+ 30) / 60);
1979 } else if (loss
> 0) {
1980 (void) printf(dgettext(TEXT_DOMAIN
,
1981 "Approximately %lld seconds of data\n"
1982 "\tmust be discarded, irreversibly. "),
1985 if (edata
!= 0 && edata
!= UINT64_MAX
) {
1987 (void) printf(dgettext(TEXT_DOMAIN
,
1988 "After rewind, at least\n"
1989 "\tone persistent user-data error will remain. "));
1991 (void) printf(dgettext(TEXT_DOMAIN
,
1992 "After rewind, several\n"
1993 "\tpersistent user-data errors will remain. "));
1996 (void) printf(dgettext(TEXT_DOMAIN
,
1997 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1998 reason
>= 0 ? "clear" : "import", name
);
2000 (void) printf(dgettext(TEXT_DOMAIN
,
2001 "A scrub of the pool\n"
2002 "\tis strongly recommended after recovery.\n"));
2006 (void) printf(dgettext(TEXT_DOMAIN
,
2007 "Destroy and re-create the pool from\n\ta backup source.\n"));
2011 * zpool_import() is a contracted interface. Should be kept the same
2014 * Applications should use zpool_import_props() to import a pool with
2015 * new properties value to be set.
2018 zpool_import(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
2021 nvlist_t
*props
= NULL
;
2024 if (altroot
!= NULL
) {
2025 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
2026 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
2027 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
2031 if (nvlist_add_string(props
,
2032 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), altroot
) != 0 ||
2033 nvlist_add_string(props
,
2034 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
), "none") != 0) {
2036 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
2037 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
2042 ret
= zpool_import_props(hdl
, config
, newname
, props
,
2049 print_vdev_tree(libzfs_handle_t
*hdl
, const char *name
, nvlist_t
*nv
,
2055 uint64_t is_log
= 0;
2057 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
,
2061 (void) printf("\t%*s%s%s\n", indent
, "", name
,
2062 is_log
? " [log]" : "");
2064 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2065 &child
, &children
) != 0)
2068 for (c
= 0; c
< children
; c
++) {
2069 vname
= zpool_vdev_name(hdl
, NULL
, child
[c
], VDEV_NAME_TYPE_ID
);
2070 print_vdev_tree(hdl
, vname
, child
[c
], indent
+ 2);
2076 zpool_print_unsup_feat(nvlist_t
*config
)
2078 nvlist_t
*nvinfo
, *unsup_feat
;
2080 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
2081 unsup_feat
= fnvlist_lookup_nvlist(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
);
2083 for (nvpair_t
*nvp
= nvlist_next_nvpair(unsup_feat
, NULL
);
2084 nvp
!= NULL
; nvp
= nvlist_next_nvpair(unsup_feat
, nvp
)) {
2085 const char *desc
= fnvpair_value_string(nvp
);
2086 if (strlen(desc
) > 0)
2087 (void) printf("\t%s (%s)\n", nvpair_name(nvp
), desc
);
2089 (void) printf("\t%s\n", nvpair_name(nvp
));
2094 * Import the given pool using the known configuration and a list of
2095 * properties to be set. The configuration should have come from
2096 * zpool_find_import(). The 'newname' parameters control whether the pool
2097 * is imported with a different name.
2100 zpool_import_props(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
2101 nvlist_t
*props
, int flags
)
2103 zfs_cmd_t zc
= {"\0"};
2104 zpool_load_policy_t policy
;
2105 nvlist_t
*nv
= NULL
;
2106 nvlist_t
*nvinfo
= NULL
;
2107 nvlist_t
*missing
= NULL
;
2108 const char *thename
;
2109 const char *origname
;
2112 char errbuf
[ERRBUFLEN
];
2114 origname
= fnvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
);
2116 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2117 "cannot import pool '%s'"), origname
);
2119 if (newname
!= NULL
) {
2120 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
2121 return (zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
2122 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
2129 if (props
!= NULL
) {
2131 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
2133 version
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
2135 if ((props
= zpool_valid_proplist(hdl
, origname
,
2136 props
, version
, flags
, errbuf
)) == NULL
)
2138 zcmd_write_src_nvlist(hdl
, &zc
, props
);
2142 (void) strlcpy(zc
.zc_name
, thename
, sizeof (zc
.zc_name
));
2144 zc
.zc_guid
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
);
2146 zcmd_write_conf_nvlist(hdl
, &zc
, config
);
2147 zcmd_alloc_dst_nvlist(hdl
, &zc
, zc
.zc_nvlist_conf_size
* 2);
2149 zc
.zc_cookie
= flags
;
2150 while ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_IMPORT
, &zc
)) != 0 &&
2152 zcmd_expand_dst_nvlist(hdl
, &zc
);
2156 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nv
);
2158 zcmd_free_nvlists(&zc
);
2160 zpool_get_load_policy(config
, &policy
);
2167 * Dry-run failed, but we print out what success
2168 * looks like if we found a best txg
2170 if (policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) {
2171 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
2177 if (newname
== NULL
)
2178 (void) snprintf(desc
, sizeof (desc
),
2179 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
2182 (void) snprintf(desc
, sizeof (desc
),
2183 dgettext(TEXT_DOMAIN
, "cannot import '%s' as '%s'"),
2188 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
2189 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
2190 nvlist_exists(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
)) {
2191 (void) printf(dgettext(TEXT_DOMAIN
, "This "
2192 "pool uses the following feature(s) not "
2193 "supported by this system:\n"));
2194 zpool_print_unsup_feat(nv
);
2195 if (nvlist_exists(nvinfo
,
2196 ZPOOL_CONFIG_CAN_RDONLY
)) {
2197 (void) printf(dgettext(TEXT_DOMAIN
,
2198 "All unsupported features are only "
2199 "required for writing to the pool."
2200 "\nThe pool can be imported using "
2201 "'-o readonly=on'.\n"));
2205 * Unsupported version.
2207 (void) zfs_error(hdl
, EZFS_BADVERSION
, desc
);
2211 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
2212 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0) {
2213 const char *hostname
= "<unknown>";
2214 uint64_t hostid
= 0;
2215 mmp_state_t mmp_state
;
2217 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
2218 ZPOOL_CONFIG_MMP_STATE
);
2220 if (nvlist_exists(nvinfo
,
2221 ZPOOL_CONFIG_MMP_HOSTNAME
))
2222 hostname
= fnvlist_lookup_string(nvinfo
,
2223 ZPOOL_CONFIG_MMP_HOSTNAME
);
2225 if (nvlist_exists(nvinfo
,
2226 ZPOOL_CONFIG_MMP_HOSTID
))
2227 hostid
= fnvlist_lookup_uint64(nvinfo
,
2228 ZPOOL_CONFIG_MMP_HOSTID
);
2230 if (mmp_state
== MMP_STATE_ACTIVE
) {
2231 (void) snprintf(aux
, sizeof (aux
),
2232 dgettext(TEXT_DOMAIN
, "pool is imp"
2233 "orted on host '%s' (hostid=%lx).\n"
2234 "Export the pool on the other "
2235 "system, then run 'zpool import'."),
2236 hostname
, (unsigned long) hostid
);
2237 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
2238 (void) snprintf(aux
, sizeof (aux
),
2239 dgettext(TEXT_DOMAIN
, "pool has "
2240 "the multihost property on and "
2241 "the\nsystem's hostid is not set. "
2242 "Set a unique system hostid with "
2243 "the zgenhostid(8) command.\n"));
2246 (void) zfs_error_aux(hdl
, "%s", aux
);
2248 (void) zfs_error(hdl
, EZFS_ACTIVE_POOL
, desc
);
2252 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, desc
);
2256 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2257 "one or more devices is read only"));
2258 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
2262 if (nv
&& nvlist_lookup_nvlist(nv
,
2263 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
2264 nvlist_lookup_nvlist(nvinfo
,
2265 ZPOOL_CONFIG_MISSING_DEVICES
, &missing
) == 0) {
2266 (void) printf(dgettext(TEXT_DOMAIN
,
2267 "The devices below are missing or "
2268 "corrupted, use '-m' to import the pool "
2270 print_vdev_tree(hdl
, NULL
, missing
, 2);
2271 (void) printf("\n");
2273 (void) zpool_standard_error(hdl
, error
, desc
);
2277 (void) zpool_standard_error(hdl
, error
, desc
);
2281 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2282 "one or more devices are already in use\n"));
2283 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
2286 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2287 "new name of at least one dataset is longer than "
2288 "the maximum allowable length"));
2289 (void) zfs_error(hdl
, EZFS_NAMETOOLONG
, desc
);
2292 (void) zpool_standard_error(hdl
, error
, desc
);
2293 zpool_explain_recover(hdl
,
2294 newname
? origname
: thename
, -error
, nv
);
2301 zpool_handle_t
*zhp
;
2304 * This should never fail, but play it safe anyway.
2306 if (zpool_open_silent(hdl
, thename
, &zhp
) != 0)
2308 else if (zhp
!= NULL
)
2310 if (policy
.zlp_rewind
&
2311 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
2312 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
2313 ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) != 0), nv
);
2322 * Translate vdev names to guids. If a vdev_path is determined to be
2323 * unsuitable then a vd_errlist is allocated and the vdev path and errno
2327 zpool_translate_vdev_guids(zpool_handle_t
*zhp
, nvlist_t
*vds
,
2328 nvlist_t
*vdev_guids
, nvlist_t
*guids_to_paths
, nvlist_t
**vd_errlist
)
2330 nvlist_t
*errlist
= NULL
;
2333 for (nvpair_t
*elem
= nvlist_next_nvpair(vds
, NULL
); elem
!= NULL
;
2334 elem
= nvlist_next_nvpair(vds
, elem
)) {
2335 boolean_t spare
, cache
;
2337 const char *vd_path
= nvpair_name(elem
);
2338 nvlist_t
*tgt
= zpool_find_vdev(zhp
, vd_path
, &spare
, &cache
,
2341 if ((tgt
== NULL
) || cache
|| spare
) {
2342 if (errlist
== NULL
) {
2343 errlist
= fnvlist_alloc();
2347 uint64_t err
= (tgt
== NULL
) ? EZFS_NODEVICE
:
2348 (spare
? EZFS_ISSPARE
: EZFS_ISL2CACHE
);
2349 fnvlist_add_int64(errlist
, vd_path
, err
);
2353 uint64_t guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
2354 fnvlist_add_uint64(vdev_guids
, vd_path
, guid
);
2356 char msg
[MAXNAMELEN
];
2357 (void) snprintf(msg
, sizeof (msg
), "%llu", (u_longlong_t
)guid
);
2358 fnvlist_add_string(guids_to_paths
, msg
, vd_path
);
2362 verify(errlist
!= NULL
);
2363 if (vd_errlist
!= NULL
)
2364 *vd_errlist
= errlist
;
2366 fnvlist_free(errlist
);
2373 xlate_init_err(int err
)
2377 return (EZFS_NODEVICE
);
2380 return (EZFS_BADDEV
);
2382 return (EZFS_INITIALIZING
);
2384 return (EZFS_NO_INITIALIZE
);
2390 * Begin, suspend, or cancel the initialization (initializing of all free
2391 * blocks) for the given vdevs in the given pool.
2394 zpool_initialize_impl(zpool_handle_t
*zhp
, pool_initialize_func_t cmd_type
,
2395 nvlist_t
*vds
, boolean_t wait
)
2399 nvlist_t
*vdev_guids
= fnvlist_alloc();
2400 nvlist_t
*guids_to_paths
= fnvlist_alloc();
2401 nvlist_t
*vd_errlist
= NULL
;
2405 err
= zpool_translate_vdev_guids(zhp
, vds
, vdev_guids
,
2406 guids_to_paths
, &vd_errlist
);
2409 verify(vd_errlist
!= NULL
);
2413 err
= lzc_initialize(zhp
->zpool_name
, cmd_type
,
2414 vdev_guids
, &errlist
);
2417 if (errlist
!= NULL
) {
2418 vd_errlist
= fnvlist_lookup_nvlist(errlist
,
2419 ZPOOL_INITIALIZE_VDEVS
);
2422 (void) zpool_standard_error(zhp
->zpool_hdl
, err
,
2423 dgettext(TEXT_DOMAIN
, "operation failed"));
2428 for (elem
= nvlist_next_nvpair(vdev_guids
, NULL
); elem
!= NULL
;
2429 elem
= nvlist_next_nvpair(vdev_guids
, elem
)) {
2431 uint64_t guid
= fnvpair_value_uint64(elem
);
2433 err
= lzc_wait_tag(zhp
->zpool_name
,
2434 ZPOOL_WAIT_INITIALIZE
, guid
, NULL
);
2436 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
,
2437 err
, dgettext(TEXT_DOMAIN
, "error "
2438 "waiting for '%s' to initialize"),
2448 for (elem
= nvlist_next_nvpair(vd_errlist
, NULL
); elem
!= NULL
;
2449 elem
= nvlist_next_nvpair(vd_errlist
, elem
)) {
2450 int64_t vd_error
= xlate_init_err(fnvpair_value_int64(elem
));
2453 if (nvlist_lookup_string(guids_to_paths
, nvpair_name(elem
),
2455 path
= nvpair_name(elem
);
2457 (void) zfs_error_fmt(zhp
->zpool_hdl
, vd_error
,
2458 "cannot initialize '%s'", path
);
2462 fnvlist_free(vdev_guids
);
2463 fnvlist_free(guids_to_paths
);
2465 if (vd_errlist
!= NULL
)
2466 fnvlist_free(vd_errlist
);
2468 return (err
== 0 ? 0 : -1);
2472 zpool_initialize(zpool_handle_t
*zhp
, pool_initialize_func_t cmd_type
,
2475 return (zpool_initialize_impl(zhp
, cmd_type
, vds
, B_FALSE
));
2479 zpool_initialize_wait(zpool_handle_t
*zhp
, pool_initialize_func_t cmd_type
,
2482 return (zpool_initialize_impl(zhp
, cmd_type
, vds
, B_TRUE
));
2486 xlate_trim_err(int err
)
2490 return (EZFS_NODEVICE
);
2493 return (EZFS_BADDEV
);
2495 return (EZFS_TRIMMING
);
2497 return (EZFS_NO_TRIM
);
2499 return (EZFS_TRIM_NOTSUP
);
2505 zpool_trim_wait(zpool_handle_t
*zhp
, nvlist_t
*vdev_guids
)
2510 for (elem
= nvlist_next_nvpair(vdev_guids
, NULL
); elem
!= NULL
;
2511 elem
= nvlist_next_nvpair(vdev_guids
, elem
)) {
2513 uint64_t guid
= fnvpair_value_uint64(elem
);
2515 err
= lzc_wait_tag(zhp
->zpool_name
,
2516 ZPOOL_WAIT_TRIM
, guid
, NULL
);
2518 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
,
2519 err
, dgettext(TEXT_DOMAIN
, "error "
2520 "waiting to trim '%s'"), nvpair_name(elem
));
2529 * Check errlist and report any errors, omitting ones which should be
2530 * suppressed. Returns B_TRUE if any errors were reported.
2533 check_trim_errs(zpool_handle_t
*zhp
, trimflags_t
*trim_flags
,
2534 nvlist_t
*guids_to_paths
, nvlist_t
*vds
, nvlist_t
*errlist
)
2537 boolean_t reported_errs
= B_FALSE
;
2539 int num_suppressed_errs
= 0;
2541 for (elem
= nvlist_next_nvpair(vds
, NULL
);
2542 elem
!= NULL
; elem
= nvlist_next_nvpair(vds
, elem
)) {
2546 for (elem
= nvlist_next_nvpair(errlist
, NULL
);
2547 elem
!= NULL
; elem
= nvlist_next_nvpair(errlist
, elem
)) {
2548 int64_t vd_error
= xlate_trim_err(fnvpair_value_int64(elem
));
2552 * If only the pool was specified, and it was not a secure
2553 * trim then suppress warnings for individual vdevs which
2554 * do not support trimming.
2556 if (vd_error
== EZFS_TRIM_NOTSUP
&&
2557 trim_flags
->fullpool
&&
2558 !trim_flags
->secure
) {
2559 num_suppressed_errs
++;
2563 reported_errs
= B_TRUE
;
2564 if (nvlist_lookup_string(guids_to_paths
, nvpair_name(elem
),
2566 path
= nvpair_name(elem
);
2568 (void) zfs_error_fmt(zhp
->zpool_hdl
, vd_error
,
2569 "cannot trim '%s'", path
);
2572 if (num_suppressed_errs
== num_vds
) {
2573 (void) zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
2574 "no devices in pool support trim operations"));
2575 (void) (zfs_error(zhp
->zpool_hdl
, EZFS_TRIM_NOTSUP
,
2576 dgettext(TEXT_DOMAIN
, "cannot trim")));
2577 reported_errs
= B_TRUE
;
2580 return (reported_errs
);
2584 * Begin, suspend, or cancel the TRIM (discarding of all free blocks) for
2585 * the given vdevs in the given pool.
2588 zpool_trim(zpool_handle_t
*zhp
, pool_trim_func_t cmd_type
, nvlist_t
*vds
,
2589 trimflags_t
*trim_flags
)
2594 nvlist_t
*vdev_guids
= fnvlist_alloc();
2595 nvlist_t
*guids_to_paths
= fnvlist_alloc();
2596 nvlist_t
*errlist
= NULL
;
2598 err
= zpool_translate_vdev_guids(zhp
, vds
, vdev_guids
,
2599 guids_to_paths
, &errlist
);
2601 check_trim_errs(zhp
, trim_flags
, guids_to_paths
, vds
, errlist
);
2606 err
= lzc_trim(zhp
->zpool_name
, cmd_type
, trim_flags
->rate
,
2607 trim_flags
->secure
, vdev_guids
, &errlist
);
2609 nvlist_t
*vd_errlist
;
2610 if (errlist
!= NULL
&& nvlist_lookup_nvlist(errlist
,
2611 ZPOOL_TRIM_VDEVS
, &vd_errlist
) == 0) {
2612 if (check_trim_errs(zhp
, trim_flags
, guids_to_paths
,
2618 char errbuf
[ERRBUFLEN
];
2620 (void) snprintf(errbuf
, sizeof (errbuf
),
2621 dgettext(TEXT_DOMAIN
, "operation failed"));
2622 zpool_standard_error(zhp
->zpool_hdl
, err
, errbuf
);
2629 if (trim_flags
->wait
)
2630 retval
= zpool_trim_wait(zhp
, vdev_guids
);
2633 if (errlist
!= NULL
)
2634 fnvlist_free(errlist
);
2635 fnvlist_free(vdev_guids
);
2636 fnvlist_free(guids_to_paths
);
2644 zpool_scan(zpool_handle_t
*zhp
, pool_scan_func_t func
, pool_scrub_cmd_t cmd
)
2646 zfs_cmd_t zc
= {"\0"};
2647 char errbuf
[ERRBUFLEN
];
2649 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2651 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2652 zc
.zc_cookie
= func
;
2655 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_SCAN
, &zc
) == 0)
2660 /* ECANCELED on a scrub means we resumed a paused scrub */
2661 if (err
== ECANCELED
&& func
== POOL_SCAN_SCRUB
&&
2662 cmd
== POOL_SCRUB_NORMAL
)
2665 if (err
== ENOENT
&& func
!= POOL_SCAN_NONE
&& cmd
== POOL_SCRUB_NORMAL
)
2668 if (func
== POOL_SCAN_SCRUB
) {
2669 if (cmd
== POOL_SCRUB_PAUSE
) {
2670 (void) snprintf(errbuf
, sizeof (errbuf
),
2671 dgettext(TEXT_DOMAIN
, "cannot pause scrubbing %s"),
2674 assert(cmd
== POOL_SCRUB_NORMAL
);
2675 (void) snprintf(errbuf
, sizeof (errbuf
),
2676 dgettext(TEXT_DOMAIN
, "cannot scrub %s"),
2679 } else if (func
== POOL_SCAN_RESILVER
) {
2680 assert(cmd
== POOL_SCRUB_NORMAL
);
2681 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2682 "cannot restart resilver on %s"), zc
.zc_name
);
2683 } else if (func
== POOL_SCAN_NONE
) {
2684 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2685 "cannot cancel scrubbing %s"), zc
.zc_name
);
2687 assert(!"unexpected result");
2692 pool_scan_stat_t
*ps
= NULL
;
2695 nvroot
= fnvlist_lookup_nvlist(zhp
->zpool_config
,
2696 ZPOOL_CONFIG_VDEV_TREE
);
2697 (void) nvlist_lookup_uint64_array(nvroot
,
2698 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &psc
);
2699 if (ps
&& ps
->pss_func
== POOL_SCAN_SCRUB
&&
2700 ps
->pss_state
== DSS_SCANNING
) {
2701 if (cmd
== POOL_SCRUB_PAUSE
)
2702 return (zfs_error(hdl
, EZFS_SCRUB_PAUSED
,
2705 return (zfs_error(hdl
, EZFS_SCRUBBING
, errbuf
));
2707 return (zfs_error(hdl
, EZFS_RESILVERING
, errbuf
));
2709 } else if (err
== ENOENT
) {
2710 return (zfs_error(hdl
, EZFS_NO_SCRUB
, errbuf
));
2711 } else if (err
== ENOTSUP
&& func
== POOL_SCAN_RESILVER
) {
2712 return (zfs_error(hdl
, EZFS_NO_RESILVER_DEFER
, errbuf
));
2714 return (zpool_standard_error(hdl
, err
, errbuf
));
2719 * Find a vdev that matches the search criteria specified. We use the
2720 * the nvpair name to determine how we should look for the device.
2721 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2722 * spare; but FALSE if its an INUSE spare.
2725 vdev_to_nvlist_iter(nvlist_t
*nv
, nvlist_t
*search
, boolean_t
*avail_spare
,
2726 boolean_t
*l2cache
, boolean_t
*log
)
2732 const char *srchkey
;
2733 nvpair_t
*pair
= nvlist_next_nvpair(search
, NULL
);
2735 /* Nothing to look for */
2736 if (search
== NULL
|| pair
== NULL
)
2739 /* Obtain the key we will use to search */
2740 srchkey
= nvpair_name(pair
);
2742 switch (nvpair_type(pair
)) {
2743 case DATA_TYPE_UINT64
:
2744 if (strcmp(srchkey
, ZPOOL_CONFIG_GUID
) == 0) {
2745 uint64_t srchval
= fnvpair_value_uint64(pair
);
2746 uint64_t theguid
= fnvlist_lookup_uint64(nv
,
2748 if (theguid
== srchval
)
2753 case DATA_TYPE_STRING
: {
2754 const char *srchval
, *val
;
2756 srchval
= fnvpair_value_string(pair
);
2757 if (nvlist_lookup_string(nv
, srchkey
, &val
) != 0)
2761 * Search for the requested value. Special cases:
2763 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2764 * "-part1", or "p1". The suffix is hidden from the user,
2765 * but included in the string, so this matches around it.
2766 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2767 * is used to check all possible expanded paths.
2768 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2770 * Otherwise, all other searches are simple string compares.
2772 if (strcmp(srchkey
, ZPOOL_CONFIG_PATH
) == 0) {
2773 uint64_t wholedisk
= 0;
2775 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
2777 if (zfs_strcmp_pathname(srchval
, val
, wholedisk
) == 0)
2780 } else if (strcmp(srchkey
, ZPOOL_CONFIG_TYPE
) == 0) {
2781 char *type
, *idx
, *end
, *p
;
2782 uint64_t id
, vdev_id
;
2785 * Determine our vdev type, keeping in mind
2786 * that the srchval is composed of a type and
2787 * vdev id pair (i.e. mirror-4).
2789 if ((type
= strdup(srchval
)) == NULL
)
2792 if ((p
= strrchr(type
, '-')) == NULL
) {
2800 * If the types don't match then keep looking.
2802 if (strncmp(val
, type
, strlen(val
)) != 0) {
2807 verify(zpool_vdev_is_interior(type
));
2809 id
= fnvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
);
2811 vdev_id
= strtoull(idx
, &end
, 10);
2814 * If we are looking for a raidz and a parity is
2815 * specified, make sure it matches.
2817 int rzlen
= strlen(VDEV_TYPE_RAIDZ
);
2818 assert(rzlen
== strlen(VDEV_TYPE_DRAID
));
2819 int typlen
= strlen(type
);
2820 if ((strncmp(type
, VDEV_TYPE_RAIDZ
, rzlen
) == 0 ||
2821 strncmp(type
, VDEV_TYPE_DRAID
, rzlen
) == 0) &&
2823 uint64_t vdev_parity
;
2824 int parity
= *(type
+ rzlen
) - '0';
2826 if (parity
<= 0 || parity
> 3 ||
2827 (typlen
- rzlen
) != 1) {
2829 * Nonsense parity specified, can
2835 vdev_parity
= fnvlist_lookup_uint64(nv
,
2836 ZPOOL_CONFIG_NPARITY
);
2837 if ((int)vdev_parity
!= parity
) {
2848 * Now verify that we have the correct vdev id.
2857 if (strcmp(srchval
, val
) == 0)
2866 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2867 &child
, &children
) != 0)
2870 for (c
= 0; c
< children
; c
++) {
2871 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2872 avail_spare
, l2cache
, NULL
)) != NULL
) {
2874 * The 'is_log' value is only set for the toplevel
2875 * vdev, not the leaf vdevs. So we always lookup the
2876 * log device from the root of the vdev tree (where
2877 * 'log' is non-NULL).
2880 nvlist_lookup_uint64(child
[c
],
2881 ZPOOL_CONFIG_IS_LOG
, &is_log
) == 0 &&
2889 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2890 &child
, &children
) == 0) {
2891 for (c
= 0; c
< children
; c
++) {
2892 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2893 avail_spare
, l2cache
, NULL
)) != NULL
) {
2894 *avail_spare
= B_TRUE
;
2900 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2901 &child
, &children
) == 0) {
2902 for (c
= 0; c
< children
; c
++) {
2903 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2904 avail_spare
, l2cache
, NULL
)) != NULL
) {
2915 * Given a physical path or guid, find the associated vdev.
2918 zpool_find_vdev_by_physpath(zpool_handle_t
*zhp
, const char *ppath
,
2919 boolean_t
*avail_spare
, boolean_t
*l2cache
, boolean_t
*log
)
2921 nvlist_t
*search
, *nvroot
, *ret
;
2925 search
= fnvlist_alloc();
2927 guid
= strtoull(ppath
, &end
, 0);
2928 if (guid
!= 0 && *end
== '\0') {
2929 fnvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
);
2931 fnvlist_add_string(search
, ZPOOL_CONFIG_PHYS_PATH
, ppath
);
2934 nvroot
= fnvlist_lookup_nvlist(zhp
->zpool_config
,
2935 ZPOOL_CONFIG_VDEV_TREE
);
2937 *avail_spare
= B_FALSE
;
2941 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2942 fnvlist_free(search
);
2948 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2951 zpool_vdev_is_interior(const char *name
)
2953 if (strncmp(name
, VDEV_TYPE_RAIDZ
, strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2954 strncmp(name
, VDEV_TYPE_SPARE
, strlen(VDEV_TYPE_SPARE
)) == 0 ||
2956 VDEV_TYPE_REPLACING
, strlen(VDEV_TYPE_REPLACING
)) == 0 ||
2957 strncmp(name
, VDEV_TYPE_ROOT
, strlen(VDEV_TYPE_ROOT
)) == 0 ||
2958 strncmp(name
, VDEV_TYPE_MIRROR
, strlen(VDEV_TYPE_MIRROR
)) == 0)
2961 if (strncmp(name
, VDEV_TYPE_DRAID
, strlen(VDEV_TYPE_DRAID
)) == 0 &&
2962 !zpool_is_draid_spare(name
))
2969 zpool_find_vdev(zpool_handle_t
*zhp
, const char *path
, boolean_t
*avail_spare
,
2970 boolean_t
*l2cache
, boolean_t
*log
)
2973 nvlist_t
*nvroot
, *search
, *ret
;
2976 search
= fnvlist_alloc();
2978 guid
= strtoull(path
, &end
, 0);
2979 if (guid
!= 0 && *end
== '\0') {
2980 fnvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
);
2981 } else if (zpool_vdev_is_interior(path
)) {
2982 fnvlist_add_string(search
, ZPOOL_CONFIG_TYPE
, path
);
2984 fnvlist_add_string(search
, ZPOOL_CONFIG_PATH
, path
);
2987 nvroot
= fnvlist_lookup_nvlist(zhp
->zpool_config
,
2988 ZPOOL_CONFIG_VDEV_TREE
);
2990 *avail_spare
= B_FALSE
;
2994 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2995 fnvlist_free(search
);
3001 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
3003 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
3004 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
3008 zpool_vdev_path_to_guid_impl(zpool_handle_t
*zhp
, const char *path
,
3009 boolean_t
*is_spare
, boolean_t
*is_l2cache
, boolean_t
*is_log
)
3011 boolean_t spare
= B_FALSE
, l2cache
= B_FALSE
, log
= B_FALSE
;
3014 if ((tgt
= zpool_find_vdev(zhp
, path
, &spare
, &l2cache
,
3018 if (is_spare
!= NULL
)
3020 if (is_l2cache
!= NULL
)
3021 *is_l2cache
= l2cache
;
3025 return (fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
));
3028 /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
3030 zpool_vdev_path_to_guid(zpool_handle_t
*zhp
, const char *path
)
3032 return (zpool_vdev_path_to_guid_impl(zhp
, path
, NULL
, NULL
, NULL
));
3036 * Bring the specified vdev online. The 'flags' parameter is a set of the
3037 * ZFS_ONLINE_* flags.
3040 zpool_vdev_online(zpool_handle_t
*zhp
, const char *path
, int flags
,
3041 vdev_state_t
*newstate
)
3043 zfs_cmd_t zc
= {"\0"};
3044 char errbuf
[ERRBUFLEN
];
3046 boolean_t avail_spare
, l2cache
, islog
;
3047 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3049 if (flags
& ZFS_ONLINE_EXPAND
) {
3050 (void) snprintf(errbuf
, sizeof (errbuf
),
3051 dgettext(TEXT_DOMAIN
, "cannot expand %s"), path
);
3053 (void) snprintf(errbuf
, sizeof (errbuf
),
3054 dgettext(TEXT_DOMAIN
, "cannot online %s"), path
);
3057 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3058 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3060 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3062 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3064 if (!(flags
& ZFS_ONLINE_SPARE
) && avail_spare
)
3065 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
3068 const char *pathname
;
3069 if ((flags
& ZFS_ONLINE_EXPAND
||
3070 zpool_get_prop_int(zhp
, ZPOOL_PROP_AUTOEXPAND
, NULL
)) &&
3071 nvlist_lookup_string(tgt
, ZPOOL_CONFIG_PATH
, &pathname
) == 0) {
3072 uint64_t wholedisk
= 0;
3074 (void) nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_WHOLE_DISK
,
3078 * XXX - L2ARC 1.0 devices can't support expansion.
3081 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3082 "cannot expand cache devices"));
3083 return (zfs_error(hdl
, EZFS_VDEVNOTSUP
, errbuf
));
3087 const char *fullpath
= path
;
3088 char buf
[MAXPATHLEN
];
3091 if (path
[0] != '/') {
3092 error
= zfs_resolve_shortname(path
, buf
,
3095 return (zfs_error(hdl
, EZFS_NODEVICE
,
3101 error
= zpool_relabel_disk(hdl
, fullpath
, errbuf
);
3108 zc
.zc_cookie
= VDEV_STATE_ONLINE
;
3111 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) != 0) {
3112 if (errno
== EINVAL
) {
3113 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "was split "
3114 "from this pool into a new one. Use '%s' "
3115 "instead"), "zpool detach");
3116 return (zfs_error(hdl
, EZFS_POSTSPLIT_ONLINE
, errbuf
));
3118 return (zpool_standard_error(hdl
, errno
, errbuf
));
3121 *newstate
= zc
.zc_cookie
;
3126 * Take the specified vdev offline
3129 zpool_vdev_offline(zpool_handle_t
*zhp
, const char *path
, boolean_t istmp
)
3131 zfs_cmd_t zc
= {"\0"};
3132 char errbuf
[ERRBUFLEN
];
3134 boolean_t avail_spare
, l2cache
;
3135 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3137 (void) snprintf(errbuf
, sizeof (errbuf
),
3138 dgettext(TEXT_DOMAIN
, "cannot offline %s"), path
);
3140 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3141 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3143 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3145 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3148 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
3150 zc
.zc_cookie
= VDEV_STATE_OFFLINE
;
3151 zc
.zc_obj
= istmp
? ZFS_OFFLINE_TEMPORARY
: 0;
3153 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3160 * There are no other replicas of this device.
3162 return (zfs_error(hdl
, EZFS_NOREPLICAS
, errbuf
));
3166 * The log device has unplayed logs
3168 return (zfs_error(hdl
, EZFS_UNPLAYED_LOGS
, errbuf
));
3171 return (zpool_standard_error(hdl
, errno
, errbuf
));
3176 * Remove the specified vdev asynchronously from the configuration, so
3177 * that it may come ONLINE if reinserted. This is called from zed on
3178 * Udev remove event.
3179 * Note: We also have a similar function zpool_vdev_remove() that
3180 * removes the vdev from the pool.
3183 zpool_vdev_remove_wanted(zpool_handle_t
*zhp
, const char *path
)
3185 zfs_cmd_t zc
= {"\0"};
3186 char errbuf
[ERRBUFLEN
];
3188 boolean_t avail_spare
, l2cache
;
3189 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3191 (void) snprintf(errbuf
, sizeof (errbuf
),
3192 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3194 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3195 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3197 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3199 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3201 zc
.zc_cookie
= VDEV_STATE_REMOVED
;
3203 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3206 return (zpool_standard_error(hdl
, errno
, errbuf
));
3210 * Mark the given vdev faulted.
3213 zpool_vdev_fault(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
3215 zfs_cmd_t zc
= {"\0"};
3216 char errbuf
[ERRBUFLEN
];
3217 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3219 (void) snprintf(errbuf
, sizeof (errbuf
),
3220 dgettext(TEXT_DOMAIN
, "cannot fault %llu"), (u_longlong_t
)guid
);
3222 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3224 zc
.zc_cookie
= VDEV_STATE_FAULTED
;
3227 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3234 * There are no other replicas of this device.
3236 return (zfs_error(hdl
, EZFS_NOREPLICAS
, errbuf
));
3239 return (zpool_standard_error(hdl
, errno
, errbuf
));
3245 * Mark the given vdev degraded.
3248 zpool_vdev_degrade(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
3250 zfs_cmd_t zc
= {"\0"};
3251 char errbuf
[ERRBUFLEN
];
3252 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3254 (void) snprintf(errbuf
, sizeof (errbuf
),
3255 dgettext(TEXT_DOMAIN
, "cannot degrade %llu"), (u_longlong_t
)guid
);
3257 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3259 zc
.zc_cookie
= VDEV_STATE_DEGRADED
;
3262 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3265 return (zpool_standard_error(hdl
, errno
, errbuf
));
3269 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
3273 is_replacing_spare(nvlist_t
*search
, nvlist_t
*tgt
, int which
)
3278 if (nvlist_lookup_nvlist_array(search
, ZPOOL_CONFIG_CHILDREN
, &child
,
3280 const char *type
= fnvlist_lookup_string(search
,
3282 if ((strcmp(type
, VDEV_TYPE_SPARE
) == 0 ||
3283 strcmp(type
, VDEV_TYPE_DRAID_SPARE
) == 0) &&
3284 children
== 2 && child
[which
] == tgt
)
3287 for (c
= 0; c
< children
; c
++)
3288 if (is_replacing_spare(child
[c
], tgt
, which
))
3296 * Attach new_disk (fully described by nvroot) to old_disk.
3297 * If 'replacing' is specified, the new disk will replace the old one.
3300 zpool_vdev_attach(zpool_handle_t
*zhp
, const char *old_disk
,
3301 const char *new_disk
, nvlist_t
*nvroot
, int replacing
, boolean_t rebuild
)
3303 zfs_cmd_t zc
= {"\0"};
3304 char errbuf
[ERRBUFLEN
];
3307 boolean_t avail_spare
, l2cache
, islog
;
3312 nvlist_t
*config_root
;
3313 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3316 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3317 "cannot replace %s with %s"), old_disk
, new_disk
);
3319 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3320 "cannot attach %s to %s"), new_disk
, old_disk
);
3322 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3323 if ((tgt
= zpool_find_vdev(zhp
, old_disk
, &avail_spare
, &l2cache
,
3325 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3328 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
3331 return (zfs_error(hdl
, EZFS_ISL2CACHE
, errbuf
));
3333 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3334 zc
.zc_cookie
= replacing
;
3335 zc
.zc_simple
= rebuild
;
3338 zfeature_lookup_guid("org.openzfs:device_rebuild", NULL
) != 0) {
3339 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3340 "the loaded zfs module doesn't support device rebuilds"));
3341 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, errbuf
));
3344 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
3345 &child
, &children
) != 0 || children
!= 1) {
3346 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3347 "new device must be a single disk"));
3348 return (zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
));
3351 config_root
= fnvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
3352 ZPOOL_CONFIG_VDEV_TREE
);
3354 if ((newname
= zpool_vdev_name(NULL
, NULL
, child
[0], 0)) == NULL
)
3358 * If the target is a hot spare that has been swapped in, we can only
3359 * replace it with another hot spare.
3362 nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_IS_SPARE
, &val
) == 0 &&
3363 (zpool_find_vdev(zhp
, newname
, &avail_spare
, &l2cache
,
3364 NULL
) == NULL
|| !avail_spare
) &&
3365 is_replacing_spare(config_root
, tgt
, 1)) {
3366 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3367 "can only be replaced by another hot spare"));
3369 return (zfs_error(hdl
, EZFS_BADTARGET
, errbuf
));
3374 zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
);
3376 ret
= zfs_ioctl(hdl
, ZFS_IOC_VDEV_ATTACH
, &zc
);
3378 zcmd_free_nvlists(&zc
);
3386 * Can't attach to or replace this type of vdev.
3389 uint64_t version
= zpool_get_prop_int(zhp
,
3390 ZPOOL_PROP_VERSION
, NULL
);
3393 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3394 "cannot replace a log with a spare"));
3395 } else if (rebuild
) {
3396 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3397 "only mirror and dRAID vdevs support "
3398 "sequential reconstruction"));
3399 } else if (zpool_is_draid_spare(new_disk
)) {
3400 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3401 "dRAID spares can only replace child "
3402 "devices in their parent's dRAID vdev"));
3403 } else if (version
>= SPA_VERSION_MULTI_REPLACE
) {
3404 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3405 "already in replacing/spare config; wait "
3406 "for completion or use 'zpool detach'"));
3408 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3409 "cannot replace a replacing device"));
3412 char status
[64] = {0};
3413 zpool_prop_get_feature(zhp
,
3414 "feature@device_rebuild", status
, 63);
3416 strncmp(status
, ZFS_FEATURE_DISABLED
, 64) == 0) {
3417 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3418 "device_rebuild feature must be enabled "
3419 "in order to use sequential "
3422 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3423 "can only attach to mirrors and top-level "
3427 (void) zfs_error(hdl
, EZFS_BADTARGET
, errbuf
);
3432 * The new device must be a single disk.
3434 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3435 "new device must be a single disk"));
3436 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
);
3440 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "%s is busy, "
3441 "or device removal is in progress"),
3443 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
3448 * The new device is too small.
3450 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3451 "device is too small"));
3452 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
3457 * The new device has a different optimal sector size.
3459 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3460 "new device has a different optimal sector size; use the "
3461 "option '-o ashift=N' to override the optimal size"));
3462 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
3467 * The resulting top-level vdev spec won't fit in the label.
3469 (void) zfs_error(hdl
, EZFS_DEVOVERFLOW
, errbuf
);
3473 (void) zpool_standard_error(hdl
, errno
, errbuf
);
3480 * Detach the specified device.
3483 zpool_vdev_detach(zpool_handle_t
*zhp
, const char *path
)
3485 zfs_cmd_t zc
= {"\0"};
3486 char errbuf
[ERRBUFLEN
];
3488 boolean_t avail_spare
, l2cache
;
3489 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3491 (void) snprintf(errbuf
, sizeof (errbuf
),
3492 dgettext(TEXT_DOMAIN
, "cannot detach %s"), path
);
3494 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3495 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3497 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3500 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
3503 return (zfs_error(hdl
, EZFS_ISL2CACHE
, errbuf
));
3505 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3507 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_DETACH
, &zc
) == 0)
3514 * Can't detach from this type of vdev.
3516 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "only "
3517 "applicable to mirror and replacing vdevs"));
3518 (void) zfs_error(hdl
, EZFS_BADTARGET
, errbuf
);
3523 * There are no other replicas of this device.
3525 (void) zfs_error(hdl
, EZFS_NOREPLICAS
, errbuf
);
3529 (void) zpool_standard_error(hdl
, errno
, errbuf
);
3536 * Find a mirror vdev in the source nvlist.
3538 * The mchild array contains a list of disks in one of the top-level mirrors
3539 * of the source pool. The schild array contains a list of disks that the
3540 * user specified on the command line. We loop over the mchild array to
3541 * see if any entry in the schild array matches.
3543 * If a disk in the mchild array is found in the schild array, we return
3544 * the index of that entry. Otherwise we return -1.
3547 find_vdev_entry(zpool_handle_t
*zhp
, nvlist_t
**mchild
, uint_t mchildren
,
3548 nvlist_t
**schild
, uint_t schildren
)
3552 for (mc
= 0; mc
< mchildren
; mc
++) {
3554 char *mpath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
3557 for (sc
= 0; sc
< schildren
; sc
++) {
3558 char *spath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
3560 boolean_t result
= (strcmp(mpath
, spath
) == 0);
3576 * Split a mirror pool. If newroot points to null, then a new nvlist
3577 * is generated and it is the responsibility of the caller to free it.
3580 zpool_vdev_split(zpool_handle_t
*zhp
, char *newname
, nvlist_t
**newroot
,
3581 nvlist_t
*props
, splitflags_t flags
)
3583 zfs_cmd_t zc
= {"\0"};
3584 char errbuf
[ERRBUFLEN
];
3586 nvlist_t
*tree
, *config
, **child
, **newchild
, *newconfig
= NULL
;
3587 nvlist_t
**varray
= NULL
, *zc_props
= NULL
;
3588 uint_t c
, children
, newchildren
, lastlog
= 0, vcount
, found
= 0;
3589 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3590 uint64_t vers
, readonly
= B_FALSE
;
3591 boolean_t freelist
= B_FALSE
, memory_err
= B_TRUE
;
3594 (void) snprintf(errbuf
, sizeof (errbuf
),
3595 dgettext(TEXT_DOMAIN
, "Unable to split %s"), zhp
->zpool_name
);
3597 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
3598 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3600 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
3601 (void) fprintf(stderr
, gettext("Internal error: unable to "
3602 "retrieve pool configuration\n"));
3606 tree
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
3607 vers
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
3610 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
3611 if ((zc_props
= zpool_valid_proplist(hdl
, zhp
->zpool_name
,
3612 props
, vers
, flags
, errbuf
)) == NULL
)
3614 (void) nvlist_lookup_uint64(zc_props
,
3615 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
3617 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3618 "property %s can only be set at import time"),
3619 zpool_prop_to_name(ZPOOL_PROP_READONLY
));
3624 if (nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
3626 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3627 "Source pool is missing vdev tree"));
3628 nvlist_free(zc_props
);
3632 varray
= zfs_alloc(hdl
, children
* sizeof (nvlist_t
*));
3635 if (*newroot
== NULL
||
3636 nvlist_lookup_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
3637 &newchild
, &newchildren
) != 0)
3640 for (c
= 0; c
< children
; c
++) {
3641 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
3642 boolean_t is_special
= B_FALSE
, is_dedup
= B_FALSE
;
3644 nvlist_t
**mchild
, *vdev
;
3649 * Unlike cache & spares, slogs are stored in the
3650 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3652 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
3654 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
3656 if (is_log
|| is_hole
) {
3658 * Create a hole vdev and put it in the config.
3660 if (nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) != 0)
3662 if (nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
,
3663 VDEV_TYPE_HOLE
) != 0)
3665 if (nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_HOLE
,
3670 varray
[vcount
++] = vdev
;
3674 type
= fnvlist_lookup_string(child
[c
], ZPOOL_CONFIG_TYPE
);
3676 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0) {
3678 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
3681 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
3682 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3683 "Source pool must be composed only of mirrors\n"));
3684 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
);
3688 if (nvlist_lookup_string(child
[c
],
3689 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
) == 0) {
3690 if (strcmp(bias
, VDEV_ALLOC_BIAS_SPECIAL
) == 0)
3691 is_special
= B_TRUE
;
3692 else if (strcmp(bias
, VDEV_ALLOC_BIAS_DEDUP
) == 0)
3695 verify(nvlist_lookup_nvlist_array(child
[c
],
3696 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
3698 /* find or add an entry for this top-level vdev */
3699 if (newchildren
> 0 &&
3700 (entry
= find_vdev_entry(zhp
, mchild
, mchildren
,
3701 newchild
, newchildren
)) >= 0) {
3702 /* We found a disk that the user specified. */
3703 vdev
= mchild
[entry
];
3706 /* User didn't specify a disk for this vdev. */
3707 vdev
= mchild
[mchildren
- 1];
3710 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
3713 if (flags
.dryrun
!= 0) {
3714 if (is_dedup
== B_TRUE
) {
3715 if (nvlist_add_string(varray
[vcount
- 1],
3716 ZPOOL_CONFIG_ALLOCATION_BIAS
,
3717 VDEV_ALLOC_BIAS_DEDUP
) != 0)
3719 } else if (is_special
== B_TRUE
) {
3720 if (nvlist_add_string(varray
[vcount
- 1],
3721 ZPOOL_CONFIG_ALLOCATION_BIAS
,
3722 VDEV_ALLOC_BIAS_SPECIAL
) != 0)
3728 /* did we find every disk the user specified? */
3729 if (found
!= newchildren
) {
3730 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Device list must "
3731 "include at most one disk from each mirror"));
3732 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
);
3736 /* Prepare the nvlist for populating. */
3737 if (*newroot
== NULL
) {
3738 if (nvlist_alloc(newroot
, NV_UNIQUE_NAME
, 0) != 0)
3741 if (nvlist_add_string(*newroot
, ZPOOL_CONFIG_TYPE
,
3742 VDEV_TYPE_ROOT
) != 0)
3745 verify(nvlist_remove_all(*newroot
, ZPOOL_CONFIG_CHILDREN
) == 0);
3748 /* Add all the children we found */
3749 if (nvlist_add_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
3750 (const nvlist_t
**)varray
, lastlog
== 0 ? vcount
: lastlog
) != 0)
3754 * If we're just doing a dry run, exit now with success.
3757 memory_err
= B_FALSE
;
3762 /* now build up the config list & call the ioctl */
3763 if (nvlist_alloc(&newconfig
, NV_UNIQUE_NAME
, 0) != 0)
3766 if (nvlist_add_nvlist(newconfig
,
3767 ZPOOL_CONFIG_VDEV_TREE
, *newroot
) != 0 ||
3768 nvlist_add_string(newconfig
,
3769 ZPOOL_CONFIG_POOL_NAME
, newname
) != 0 ||
3770 nvlist_add_uint64(newconfig
, ZPOOL_CONFIG_VERSION
, vers
) != 0)
3774 * The new pool is automatically part of the namespace unless we
3775 * explicitly export it.
3778 zc
.zc_cookie
= ZPOOL_EXPORT_AFTER_SPLIT
;
3779 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3780 (void) strlcpy(zc
.zc_string
, newname
, sizeof (zc
.zc_string
));
3781 zcmd_write_conf_nvlist(hdl
, &zc
, newconfig
);
3782 if (zc_props
!= NULL
)
3783 zcmd_write_src_nvlist(hdl
, &zc
, zc_props
);
3785 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SPLIT
, &zc
) != 0) {
3786 retval
= zpool_standard_error(hdl
, errno
, errbuf
);
3791 memory_err
= B_FALSE
;
3794 if (varray
!= NULL
) {
3797 for (v
= 0; v
< vcount
; v
++)
3798 nvlist_free(varray
[v
]);
3801 zcmd_free_nvlists(&zc
);
3802 nvlist_free(zc_props
);
3803 nvlist_free(newconfig
);
3805 nvlist_free(*newroot
);
3813 return (no_memory(hdl
));
3819 * Remove the given device.
3822 zpool_vdev_remove(zpool_handle_t
*zhp
, const char *path
)
3824 zfs_cmd_t zc
= {"\0"};
3825 char errbuf
[ERRBUFLEN
];
3827 boolean_t avail_spare
, l2cache
, islog
;
3828 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3831 (void) snprintf(errbuf
, sizeof (errbuf
),
3832 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3834 if (zpool_is_draid_spare(path
)) {
3835 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3836 "dRAID spares cannot be removed"));
3837 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3840 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3841 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3843 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3845 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
3846 if (islog
&& version
< SPA_VERSION_HOLES
) {
3847 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3848 "pool must be upgraded to support log removal"));
3849 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3852 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3854 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3860 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3861 "invalid config; all top-level vdevs must "
3862 "have the same sector size and not be raidz."));
3863 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
);
3868 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3869 "Mount encrypted datasets to replay logs."));
3871 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3872 "Pool busy; removal may already be in progress"));
3874 (void) zfs_error(hdl
, EZFS_BUSY
, errbuf
);
3879 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3880 "Mount encrypted datasets to replay logs."));
3881 (void) zfs_error(hdl
, EZFS_BUSY
, errbuf
);
3883 (void) zpool_standard_error(hdl
, errno
, errbuf
);
3888 (void) zpool_standard_error(hdl
, errno
, errbuf
);
3894 zpool_vdev_remove_cancel(zpool_handle_t
*zhp
)
3896 zfs_cmd_t zc
= {{0}};
3897 char errbuf
[ERRBUFLEN
];
3898 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3900 (void) snprintf(errbuf
, sizeof (errbuf
),
3901 dgettext(TEXT_DOMAIN
, "cannot cancel removal"));
3903 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3906 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3909 return (zpool_standard_error(hdl
, errno
, errbuf
));
3913 zpool_vdev_indirect_size(zpool_handle_t
*zhp
, const char *path
,
3916 char errbuf
[ERRBUFLEN
];
3918 boolean_t avail_spare
, l2cache
, islog
;
3919 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3921 (void) snprintf(errbuf
, sizeof (errbuf
),
3922 dgettext(TEXT_DOMAIN
, "cannot determine indirect size of %s"),
3925 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3927 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3929 if (avail_spare
|| l2cache
|| islog
) {
3934 if (nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_INDIRECT_SIZE
, sizep
) != 0) {
3935 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3936 "indirect size not available"));
3937 return (zfs_error(hdl
, EINVAL
, errbuf
));
3943 * Clear the errors for the pool, or the particular device if specified.
3946 zpool_clear(zpool_handle_t
*zhp
, const char *path
, nvlist_t
*rewindnvl
)
3948 zfs_cmd_t zc
= {"\0"};
3949 char errbuf
[ERRBUFLEN
];
3951 zpool_load_policy_t policy
;
3952 boolean_t avail_spare
, l2cache
;
3953 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3954 nvlist_t
*nvi
= NULL
;
3958 (void) snprintf(errbuf
, sizeof (errbuf
),
3959 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3962 (void) snprintf(errbuf
, sizeof (errbuf
),
3963 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3966 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3968 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
,
3969 &l2cache
, NULL
)) == NULL
)
3970 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3973 * Don't allow error clearing for hot spares. Do allow
3974 * error clearing for l2cache devices.
3977 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
3979 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3982 zpool_get_load_policy(rewindnvl
, &policy
);
3983 zc
.zc_cookie
= policy
.zlp_rewind
;
3985 zcmd_alloc_dst_nvlist(hdl
, &zc
, zhp
->zpool_config_size
* 2);
3986 zcmd_write_src_nvlist(hdl
, &zc
, rewindnvl
);
3988 while ((error
= zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
)) != 0 &&
3990 zcmd_expand_dst_nvlist(hdl
, &zc
);
3992 if (!error
|| ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) &&
3993 errno
!= EPERM
&& errno
!= EACCES
)) {
3994 if (policy
.zlp_rewind
&
3995 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
3996 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nvi
);
3997 zpool_rewind_exclaim(hdl
, zc
.zc_name
,
3998 ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) != 0),
4002 zcmd_free_nvlists(&zc
);
4006 zcmd_free_nvlists(&zc
);
4007 return (zpool_standard_error(hdl
, errno
, errbuf
));
4011 * Similar to zpool_clear(), but takes a GUID (used by fmd).
4014 zpool_vdev_clear(zpool_handle_t
*zhp
, uint64_t guid
)
4016 zfs_cmd_t zc
= {"\0"};
4017 char errbuf
[ERRBUFLEN
];
4018 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4020 (void) snprintf(errbuf
, sizeof (errbuf
),
4021 dgettext(TEXT_DOMAIN
, "cannot clear errors for %llx"),
4022 (u_longlong_t
)guid
);
4024 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4026 zc
.zc_cookie
= ZPOOL_NO_REWIND
;
4028 if (zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
) == 0)
4031 return (zpool_standard_error(hdl
, errno
, errbuf
));
4035 * Change the GUID for a pool.
4038 zpool_reguid(zpool_handle_t
*zhp
)
4040 char errbuf
[ERRBUFLEN
];
4041 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4042 zfs_cmd_t zc
= {"\0"};
4044 (void) snprintf(errbuf
, sizeof (errbuf
),
4045 dgettext(TEXT_DOMAIN
, "cannot reguid '%s'"), zhp
->zpool_name
);
4047 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4048 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REGUID
, &zc
) == 0)
4051 return (zpool_standard_error(hdl
, errno
, errbuf
));
4058 zpool_reopen_one(zpool_handle_t
*zhp
, void *data
)
4060 libzfs_handle_t
*hdl
= zpool_get_handle(zhp
);
4061 const char *pool_name
= zpool_get_name(zhp
);
4062 boolean_t
*scrub_restart
= data
;
4065 error
= lzc_reopen(pool_name
, *scrub_restart
);
4067 return (zpool_standard_error_fmt(hdl
, error
,
4068 dgettext(TEXT_DOMAIN
, "cannot reopen '%s'"), pool_name
));
4074 /* call into libzfs_core to execute the sync IOCTL per pool */
4076 zpool_sync_one(zpool_handle_t
*zhp
, void *data
)
4079 libzfs_handle_t
*hdl
= zpool_get_handle(zhp
);
4080 const char *pool_name
= zpool_get_name(zhp
);
4081 boolean_t
*force
= data
;
4082 nvlist_t
*innvl
= fnvlist_alloc();
4084 fnvlist_add_boolean_value(innvl
, "force", *force
);
4085 if ((ret
= lzc_sync(pool_name
, innvl
, NULL
)) != 0) {
4087 return (zpool_standard_error_fmt(hdl
, ret
,
4088 dgettext(TEXT_DOMAIN
, "sync '%s' failed"), pool_name
));
4095 #define PATH_BUF_LEN 64
4098 * Given a vdev, return the name to display in iostat. If the vdev has a path,
4099 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
4100 * We also check if this is a whole disk, in which case we strip off the
4101 * trailing 's0' slice name.
4103 * This routine is also responsible for identifying when disks have been
4104 * reconfigured in a new location. The kernel will have opened the device by
4105 * devid, but the path will still refer to the old location. To catch this, we
4106 * first do a path -> devid translation (which is fast for the common case). If
4107 * the devid matches, we're done. If not, we do a reverse devid -> path
4108 * translation and issue the appropriate ioctl() to update the path of the vdev.
4109 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
4113 zpool_vdev_name(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, nvlist_t
*nv
,
4116 const char *type
, *tpath
;
4119 char buf
[PATH_BUF_LEN
];
4120 char tmpbuf
[PATH_BUF_LEN
* 2];
4123 * vdev_name will be "root"/"root-0" for the root vdev, but it is the
4124 * zpool name that will be displayed to the user.
4126 type
= fnvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
);
4127 if (zhp
!= NULL
&& strcmp(type
, "root") == 0)
4128 return (zfs_strdup(hdl
, zpool_get_name(zhp
)));
4130 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_PATH"))
4131 name_flags
|= VDEV_NAME_PATH
;
4132 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_GUID"))
4133 name_flags
|= VDEV_NAME_GUID
;
4134 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_FOLLOW_LINKS"))
4135 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
4137 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
, &value
) == 0 ||
4138 name_flags
& VDEV_NAME_GUID
) {
4139 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
);
4140 (void) snprintf(buf
, sizeof (buf
), "%llu", (u_longlong_t
)value
);
4142 } else if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &tpath
) == 0) {
4145 if (name_flags
& VDEV_NAME_FOLLOW_LINKS
) {
4146 char *rp
= realpath(path
, NULL
);
4148 strlcpy(buf
, rp
, sizeof (buf
));
4155 * For a block device only use the name.
4157 if ((strcmp(type
, VDEV_TYPE_DISK
) == 0) &&
4158 !(name_flags
& VDEV_NAME_PATH
)) {
4159 path
= zfs_strip_path(path
);
4163 * Remove the partition from the path if this is a whole disk.
4165 if (strcmp(type
, VDEV_TYPE_DRAID_SPARE
) != 0 &&
4166 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
, &value
)
4167 == 0 && value
&& !(name_flags
& VDEV_NAME_PATH
)) {
4168 return (zfs_strip_partition(path
));
4174 * If it's a raidz device, we need to stick in the parity level.
4176 if (strcmp(path
, VDEV_TYPE_RAIDZ
) == 0) {
4177 value
= fnvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
);
4178 (void) snprintf(buf
, sizeof (buf
), "%s%llu", path
,
4179 (u_longlong_t
)value
);
4184 * If it's a dRAID device, we add parity, groups, and spares.
4186 if (strcmp(path
, VDEV_TYPE_DRAID
) == 0) {
4187 uint64_t ndata
, nparity
, nspares
;
4191 verify(nvlist_lookup_nvlist_array(nv
,
4192 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) == 0);
4193 nparity
= fnvlist_lookup_uint64(nv
,
4194 ZPOOL_CONFIG_NPARITY
);
4195 ndata
= fnvlist_lookup_uint64(nv
,
4196 ZPOOL_CONFIG_DRAID_NDATA
);
4197 nspares
= fnvlist_lookup_uint64(nv
,
4198 ZPOOL_CONFIG_DRAID_NSPARES
);
4200 path
= zpool_draid_name(buf
, sizeof (buf
), ndata
,
4201 nparity
, nspares
, children
);
4205 * We identify each top-level vdev by using a <type-id>
4206 * naming convention.
4208 if (name_flags
& VDEV_NAME_TYPE_ID
) {
4209 uint64_t id
= fnvlist_lookup_uint64(nv
,
4211 (void) snprintf(tmpbuf
, sizeof (tmpbuf
), "%s-%llu",
4212 path
, (u_longlong_t
)id
);
4217 return (zfs_strdup(hdl
, path
));
4221 zbookmark_mem_compare(const void *a
, const void *b
)
4223 return (memcmp(a
, b
, sizeof (zbookmark_phys_t
)));
4227 * Retrieve the persistent error log, uniquify the members, and return to the
4231 zpool_get_errlog(zpool_handle_t
*zhp
, nvlist_t
**nverrlistp
)
4233 zfs_cmd_t zc
= {"\0"};
4234 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4235 zbookmark_phys_t
*buf
;
4236 uint64_t buflen
= 10000; /* approx. 1MB of RAM */
4238 if (fnvlist_lookup_uint64(zhp
->zpool_config
,
4239 ZPOOL_CONFIG_ERRCOUNT
) == 0)
4243 * Retrieve the raw error list from the kernel. If it doesn't fit,
4244 * allocate a larger buffer and retry.
4246 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
4248 buf
= zfs_alloc(zhp
->zpool_hdl
,
4249 buflen
* sizeof (zbookmark_phys_t
));
4250 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4251 zc
.zc_nvlist_dst_size
= buflen
;
4252 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_ERROR_LOG
,
4255 if (errno
== ENOMEM
) {
4258 return (zpool_standard_error_fmt(hdl
, errno
,
4259 dgettext(TEXT_DOMAIN
, "errors: List of "
4260 "errors unavailable")));
4268 * Sort the resulting bookmarks. This is a little confusing due to the
4269 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
4270 * to first, and 'zc_nvlist_dst_size' indicates the number of bookmarks
4271 * _not_ copied as part of the process. So we point the start of our
4272 * array appropriate and decrement the total number of elements.
4274 zbookmark_phys_t
*zb
= buf
+ zc
.zc_nvlist_dst_size
;
4275 uint64_t zblen
= buflen
- zc
.zc_nvlist_dst_size
;
4277 qsort(zb
, zblen
, sizeof (zbookmark_phys_t
), zbookmark_mem_compare
);
4279 verify(nvlist_alloc(nverrlistp
, 0, KM_SLEEP
) == 0);
4282 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
4284 for (uint64_t i
= 0; i
< zblen
; i
++) {
4287 /* ignoring zb_blkid and zb_level for now */
4288 if (i
> 0 && zb
[i
-1].zb_objset
== zb
[i
].zb_objset
&&
4289 zb
[i
-1].zb_object
== zb
[i
].zb_object
)
4292 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) != 0)
4294 if (nvlist_add_uint64(nv
, ZPOOL_ERR_DATASET
,
4295 zb
[i
].zb_objset
) != 0) {
4299 if (nvlist_add_uint64(nv
, ZPOOL_ERR_OBJECT
,
4300 zb
[i
].zb_object
) != 0) {
4304 if (nvlist_add_nvlist(*nverrlistp
, "ejk", nv
) != 0) {
4316 return (no_memory(zhp
->zpool_hdl
));
4320 * Upgrade a ZFS pool to the latest on-disk version.
4323 zpool_upgrade(zpool_handle_t
*zhp
, uint64_t new_version
)
4325 zfs_cmd_t zc
= {"\0"};
4326 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4328 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
4329 zc
.zc_cookie
= new_version
;
4331 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_UPGRADE
, &zc
) != 0)
4332 return (zpool_standard_error_fmt(hdl
, errno
,
4333 dgettext(TEXT_DOMAIN
, "cannot upgrade '%s'"),
4339 zfs_save_arguments(int argc
, char **argv
, char *string
, int len
)
4343 (void) strlcpy(string
, zfs_basename(argv
[0]), len
);
4344 for (i
= 1; i
< argc
; i
++) {
4345 (void) strlcat(string
, " ", len
);
4346 (void) strlcat(string
, argv
[i
], len
);
4351 zpool_log_history(libzfs_handle_t
*hdl
, const char *message
)
4353 zfs_cmd_t zc
= {"\0"};
4356 args
= fnvlist_alloc();
4357 fnvlist_add_string(args
, "message", message
);
4358 zcmd_write_src_nvlist(hdl
, &zc
, args
);
4359 int err
= zfs_ioctl(hdl
, ZFS_IOC_LOG_HISTORY
, &zc
);
4361 zcmd_free_nvlists(&zc
);
4366 * Perform ioctl to get some command history of a pool.
4368 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
4369 * logical offset of the history buffer to start reading from.
4371 * Upon return, 'off' is the next logical offset to read from and
4372 * 'len' is the actual amount of bytes read into 'buf'.
4375 get_history(zpool_handle_t
*zhp
, char *buf
, uint64_t *off
, uint64_t *len
)
4377 zfs_cmd_t zc
= {"\0"};
4378 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4380 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4382 zc
.zc_history
= (uint64_t)(uintptr_t)buf
;
4383 zc
.zc_history_len
= *len
;
4384 zc
.zc_history_offset
= *off
;
4386 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_GET_HISTORY
, &zc
) != 0) {
4389 return (zfs_error_fmt(hdl
, EZFS_PERM
,
4390 dgettext(TEXT_DOMAIN
,
4391 "cannot show history for pool '%s'"),
4394 return (zfs_error_fmt(hdl
, EZFS_NOHISTORY
,
4395 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
4396 "'%s'"), zhp
->zpool_name
));
4398 return (zfs_error_fmt(hdl
, EZFS_BADVERSION
,
4399 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
4400 "'%s', pool must be upgraded"), zhp
->zpool_name
));
4402 return (zpool_standard_error_fmt(hdl
, errno
,
4403 dgettext(TEXT_DOMAIN
,
4404 "cannot get history for '%s'"), zhp
->zpool_name
));
4408 *len
= zc
.zc_history_len
;
4409 *off
= zc
.zc_history_offset
;
4415 * Retrieve the command history of a pool.
4418 zpool_get_history(zpool_handle_t
*zhp
, nvlist_t
**nvhisp
, uint64_t *off
,
4421 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4423 int buflen
= 128 * 1024;
4424 nvlist_t
**records
= NULL
;
4425 uint_t numrecords
= 0;
4427 uint64_t start
= *off
;
4429 buf
= zfs_alloc(hdl
, buflen
);
4431 /* process about 1MiB a time */
4432 while (*off
- start
< 1024 * 1024) {
4433 uint64_t bytes_read
= buflen
;
4436 if ((err
= get_history(zhp
, buf
, off
, &bytes_read
)) != 0)
4439 /* if nothing else was read in, we're at EOF, just return */
4445 if ((err
= zpool_history_unpack(buf
, bytes_read
,
4446 &leftover
, &records
, &numrecords
)) != 0) {
4447 zpool_standard_error_fmt(hdl
, err
,
4448 dgettext(TEXT_DOMAIN
,
4449 "cannot get history for '%s'"), zhp
->zpool_name
);
4453 if (leftover
== bytes_read
) {
4455 * no progress made, because buffer is not big enough
4456 * to hold this record; resize and retry.
4460 buf
= zfs_alloc(hdl
, buflen
);
4467 *nvhisp
= fnvlist_alloc();
4468 fnvlist_add_nvlist_array(*nvhisp
, ZPOOL_HIST_RECORD
,
4469 (const nvlist_t
**)records
, numrecords
);
4471 for (i
= 0; i
< numrecords
; i
++)
4472 nvlist_free(records
[i
]);
4479 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
4480 * If there is a new event available 'nvp' will contain a newly allocated
4481 * nvlist and 'dropped' will be set to the number of missed events since
4482 * the last call to this function. When 'nvp' is set to NULL it indicates
4483 * no new events are available. In either case the function returns 0 and
4484 * it is up to the caller to free 'nvp'. In the case of a fatal error the
4485 * function will return a non-zero value. When the function is called in
4486 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
4487 * it will not return until a new event is available.
4490 zpool_events_next(libzfs_handle_t
*hdl
, nvlist_t
**nvp
,
4491 int *dropped
, unsigned flags
, int zevent_fd
)
4493 zfs_cmd_t zc
= {"\0"};
4498 zc
.zc_cleanup_fd
= zevent_fd
;
4500 if (flags
& ZEVENT_NONBLOCK
)
4501 zc
.zc_guid
= ZEVENT_NONBLOCK
;
4503 zcmd_alloc_dst_nvlist(hdl
, &zc
, ZEVENT_SIZE
);
4506 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_NEXT
, &zc
) != 0) {
4509 error
= zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
4510 dgettext(TEXT_DOMAIN
, "zfs shutdown"));
4513 /* Blocking error case should not occur */
4514 if (!(flags
& ZEVENT_NONBLOCK
))
4515 error
= zpool_standard_error_fmt(hdl
, errno
,
4516 dgettext(TEXT_DOMAIN
, "cannot get event"));
4520 zcmd_expand_dst_nvlist(hdl
, &zc
);
4523 error
= zpool_standard_error_fmt(hdl
, errno
,
4524 dgettext(TEXT_DOMAIN
, "cannot get event"));
4529 error
= zcmd_read_dst_nvlist(hdl
, &zc
, nvp
);
4533 *dropped
= (int)zc
.zc_cookie
;
4535 zcmd_free_nvlists(&zc
);
4544 zpool_events_clear(libzfs_handle_t
*hdl
, int *count
)
4546 zfs_cmd_t zc
= {"\0"};
4548 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_CLEAR
, &zc
) != 0)
4549 return (zpool_standard_error(hdl
, errno
,
4550 dgettext(TEXT_DOMAIN
, "cannot clear events")));
4553 *count
= (int)zc
.zc_cookie
; /* # of events cleared */
4559 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4560 * the passed zevent_fd file handle. On success zero is returned,
4561 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4564 zpool_events_seek(libzfs_handle_t
*hdl
, uint64_t eid
, int zevent_fd
)
4566 zfs_cmd_t zc
= {"\0"};
4570 zc
.zc_cleanup_fd
= zevent_fd
;
4572 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_SEEK
, &zc
) != 0) {
4575 error
= zfs_error_fmt(hdl
, EZFS_NOENT
,
4576 dgettext(TEXT_DOMAIN
, "cannot get event"));
4580 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
4581 dgettext(TEXT_DOMAIN
, "cannot get event"));
4585 error
= zpool_standard_error_fmt(hdl
, errno
,
4586 dgettext(TEXT_DOMAIN
, "cannot get event"));
4595 zpool_obj_to_path_impl(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4596 char *pathname
, size_t len
, boolean_t always_unmounted
)
4598 zfs_cmd_t zc
= {"\0"};
4599 boolean_t mounted
= B_FALSE
;
4600 char *mntpnt
= NULL
;
4601 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
4604 /* special case for the MOS */
4605 (void) snprintf(pathname
, len
, "<metadata>:<0x%llx>",
4610 /* get the dataset's name */
4611 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4613 if (zfs_ioctl(zhp
->zpool_hdl
,
4614 ZFS_IOC_DSOBJ_TO_DSNAME
, &zc
) != 0) {
4615 /* just write out a path of two object numbers */
4616 (void) snprintf(pathname
, len
, "<0x%llx>:<0x%llx>",
4617 (longlong_t
)dsobj
, (longlong_t
)obj
);
4620 (void) strlcpy(dsname
, zc
.zc_value
, sizeof (dsname
));
4622 /* find out if the dataset is mounted */
4623 mounted
= !always_unmounted
&& is_mounted(zhp
->zpool_hdl
, dsname
,
4626 /* get the corrupted object's path */
4627 (void) strlcpy(zc
.zc_name
, dsname
, sizeof (zc
.zc_name
));
4629 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_OBJ_TO_PATH
,
4632 (void) snprintf(pathname
, len
, "%s%s", mntpnt
,
4635 (void) snprintf(pathname
, len
, "%s:%s",
4636 dsname
, zc
.zc_value
);
4639 (void) snprintf(pathname
, len
, "%s:<0x%llx>", dsname
,
4646 zpool_obj_to_path(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4647 char *pathname
, size_t len
)
4649 zpool_obj_to_path_impl(zhp
, dsobj
, obj
, pathname
, len
, B_FALSE
);
4653 zpool_obj_to_path_ds(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4654 char *pathname
, size_t len
)
4656 zpool_obj_to_path_impl(zhp
, dsobj
, obj
, pathname
, len
, B_TRUE
);
4659 * Wait while the specified activity is in progress in the pool.
4662 zpool_wait(zpool_handle_t
*zhp
, zpool_wait_activity_t activity
)
4666 int error
= zpool_wait_status(zhp
, activity
, &missing
, NULL
);
4669 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, ENOENT
,
4670 dgettext(TEXT_DOMAIN
, "error waiting in pool '%s'"),
4679 * Wait for the given activity and return the status of the wait (whether or not
4680 * any waiting was done) in the 'waited' parameter. Non-existent pools are
4681 * reported via the 'missing' parameter, rather than by printing an error
4682 * message. This is convenient when this function is called in a loop over a
4683 * long period of time (as it is, for example, by zpool's wait cmd). In that
4684 * scenario, a pool being exported or destroyed should be considered a normal
4685 * event, so we don't want to print an error when we find that the pool doesn't
4689 zpool_wait_status(zpool_handle_t
*zhp
, zpool_wait_activity_t activity
,
4690 boolean_t
*missing
, boolean_t
*waited
)
4692 int error
= lzc_wait(zhp
->zpool_name
, activity
, waited
);
4693 *missing
= (error
== ENOENT
);
4698 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
4699 dgettext(TEXT_DOMAIN
, "error waiting in pool '%s'"),
4707 zpool_set_bootenv(zpool_handle_t
*zhp
, const nvlist_t
*envmap
)
4709 int error
= lzc_set_bootenv(zhp
->zpool_name
, envmap
);
4711 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
4712 dgettext(TEXT_DOMAIN
,
4713 "error setting bootenv in pool '%s'"), zhp
->zpool_name
);
4720 zpool_get_bootenv(zpool_handle_t
*zhp
, nvlist_t
**nvlp
)
4726 error
= lzc_get_bootenv(zhp
->zpool_name
, &nvl
);
4728 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
4729 dgettext(TEXT_DOMAIN
,
4730 "error getting bootenv in pool '%s'"), zhp
->zpool_name
);
4739 * Attempt to read and parse feature file(s) (from "compatibility" property).
4740 * Files contain zpool feature names, comma or whitespace-separated.
4741 * Comments (# character to next newline) are discarded.
4744 * compatibility : string containing feature filenames
4745 * features : either NULL or pointer to array of boolean
4746 * report : either NULL or pointer to string buffer
4747 * rlen : length of "report" buffer
4749 * compatibility is NULL (unset), "", "off", "legacy", or list of
4750 * comma-separated filenames. filenames should either be absolute,
4752 * 1) ZPOOL_SYSCONF_COMPAT_D (eg: /etc/zfs/compatibility.d) or
4753 * 2) ZPOOL_DATA_COMPAT_D (eg: /usr/share/zfs/compatibility.d).
4754 * (Unset), "" or "off" => enable all features
4755 * "legacy" => disable all features
4757 * Any feature names read from files which match unames in spa_feature_table
4758 * will have the corresponding boolean set in the features array (if non-NULL).
4759 * If more than one feature set specified, only features present in *all* of
4762 * "report" if not NULL will be populated with a suitable status message.
4765 * ZPOOL_COMPATIBILITY_OK : files read and parsed ok
4766 * ZPOOL_COMPATIBILITY_BADFILE : file too big or not a text file
4767 * ZPOOL_COMPATIBILITY_BADTOKEN : SYSCONF file contains invalid feature name
4768 * ZPOOL_COMPATIBILITY_WARNTOKEN : DATA file contains invalid feature name
4769 * ZPOOL_COMPATIBILITY_NOFILES : no feature files found
4771 zpool_compat_status_t
4772 zpool_load_compat(const char *compat
, boolean_t
*features
, char *report
,
4775 int sdirfd
, ddirfd
, featfd
;
4779 char *file
, *line
, *word
;
4781 char l_compat
[ZFS_MAXPROPLEN
];
4783 boolean_t ret_nofiles
= B_TRUE
;
4784 boolean_t ret_badfile
= B_FALSE
;
4785 boolean_t ret_badtoken
= B_FALSE
;
4786 boolean_t ret_warntoken
= B_FALSE
;
4788 /* special cases (unset), "" and "off" => enable all features */
4789 if (compat
== NULL
|| compat
[0] == '\0' ||
4790 strcmp(compat
, ZPOOL_COMPAT_OFF
) == 0) {
4791 if (features
!= NULL
)
4792 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4793 features
[i
] = B_TRUE
;
4795 strlcpy(report
, gettext("all features enabled"), rlen
);
4796 return (ZPOOL_COMPATIBILITY_OK
);
4799 /* Final special case "legacy" => disable all features */
4800 if (strcmp(compat
, ZPOOL_COMPAT_LEGACY
) == 0) {
4801 if (features
!= NULL
)
4802 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4803 features
[i
] = B_FALSE
;
4805 strlcpy(report
, gettext("all features disabled"), rlen
);
4806 return (ZPOOL_COMPATIBILITY_OK
);
4810 * Start with all true; will be ANDed with results from each file
4812 if (features
!= NULL
)
4813 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4814 features
[i
] = B_TRUE
;
4816 char err_badfile
[ZFS_MAXPROPLEN
] = "";
4817 char err_badtoken
[ZFS_MAXPROPLEN
] = "";
4820 * We ignore errors from the directory open()
4821 * as they're only needed if the filename is relative
4822 * which will be checked during the openat().
4825 /* O_PATH safer than O_RDONLY if system allows it */
4827 #define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_PATH)
4829 #define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_RDONLY)
4832 sdirfd
= open(ZPOOL_SYSCONF_COMPAT_D
, ZC_DIR_FLAGS
);
4833 ddirfd
= open(ZPOOL_DATA_COMPAT_D
, ZC_DIR_FLAGS
);
4835 (void) strlcpy(l_compat
, compat
, ZFS_MAXPROPLEN
);
4837 for (file
= strtok_r(l_compat
, ",", &ps
);
4839 file
= strtok_r(NULL
, ",", &ps
)) {
4841 boolean_t l_features
[SPA_FEATURES
];
4843 enum { Z_SYSCONF
, Z_DATA
} source
;
4845 /* try sysconfdir first, then datadir */
4847 if ((featfd
= openat(sdirfd
, file
, O_RDONLY
| O_CLOEXEC
)) < 0) {
4848 featfd
= openat(ddirfd
, file
, O_RDONLY
| O_CLOEXEC
);
4852 /* File readable and correct size? */
4854 fstat(featfd
, &fs
) < 0 ||
4856 fs
.st_size
> ZPOOL_COMPAT_MAXSIZE
) {
4857 (void) close(featfd
);
4858 strlcat(err_badfile
, file
, ZFS_MAXPROPLEN
);
4859 strlcat(err_badfile
, " ", ZFS_MAXPROPLEN
);
4860 ret_badfile
= B_TRUE
;
4864 /* Prefault the file if system allows */
4865 #if defined(MAP_POPULATE)
4866 #define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_POPULATE)
4867 #elif defined(MAP_PREFAULT_READ)
4868 #define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_PREFAULT_READ)
4870 #define ZC_MMAP_FLAGS (MAP_PRIVATE)
4873 /* private mmap() so we can strtok safely */
4874 fc
= (char *)mmap(NULL
, fs
.st_size
, PROT_READ
| PROT_WRITE
,
4875 ZC_MMAP_FLAGS
, featfd
, 0);
4876 (void) close(featfd
);
4878 /* map ok, and last character == newline? */
4879 if (fc
== MAP_FAILED
|| fc
[fs
.st_size
- 1] != '\n') {
4880 (void) munmap((void *) fc
, fs
.st_size
);
4881 strlcat(err_badfile
, file
, ZFS_MAXPROPLEN
);
4882 strlcat(err_badfile
, " ", ZFS_MAXPROPLEN
);
4883 ret_badfile
= B_TRUE
;
4887 ret_nofiles
= B_FALSE
;
4889 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4890 l_features
[i
] = B_FALSE
;
4892 /* replace final newline with NULL to ensure string ends */
4893 fc
[fs
.st_size
- 1] = '\0';
4895 for (line
= strtok_r(fc
, "\n", &ls
);
4897 line
= strtok_r(NULL
, "\n", &ls
)) {
4898 /* discard comments */
4899 char *r
= strchr(line
, '#');
4903 for (word
= strtok_r(line
, ", \t", &ws
);
4905 word
= strtok_r(NULL
, ", \t", &ws
)) {
4906 /* Find matching feature name */
4908 for (f
= 0; f
< SPA_FEATURES
; f
++) {
4909 zfeature_info_t
*fi
=
4910 &spa_feature_table
[f
];
4911 if (strcmp(word
, fi
->fi_uname
) == 0) {
4912 l_features
[f
] = B_TRUE
;
4916 if (f
< SPA_FEATURES
)
4919 /* found an unrecognized word */
4920 /* lightly sanitize it */
4921 if (strlen(word
) > 32)
4923 for (char *c
= word
; *c
!= '\0'; c
++)
4927 strlcat(err_badtoken
, word
, ZFS_MAXPROPLEN
);
4928 strlcat(err_badtoken
, " ", ZFS_MAXPROPLEN
);
4929 if (source
== Z_SYSCONF
)
4930 ret_badtoken
= B_TRUE
;
4932 ret_warntoken
= B_TRUE
;
4935 (void) munmap((void *) fc
, fs
.st_size
);
4937 if (features
!= NULL
)
4938 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4939 features
[i
] &= l_features
[i
];
4941 (void) close(sdirfd
);
4942 (void) close(ddirfd
);
4944 /* Return the most serious error */
4947 snprintf(report
, rlen
, gettext("could not read/"
4948 "parse feature file(s): %s"), err_badfile
);
4949 return (ZPOOL_COMPATIBILITY_BADFILE
);
4954 gettext("no valid compatibility files specified"),
4956 return (ZPOOL_COMPATIBILITY_NOFILES
);
4960 snprintf(report
, rlen
, gettext("invalid feature "
4961 "name(s) in local compatibility files: %s"),
4963 return (ZPOOL_COMPATIBILITY_BADTOKEN
);
4965 if (ret_warntoken
) {
4967 snprintf(report
, rlen
, gettext("unrecognized feature "
4968 "name(s) in distribution compatibility files: %s"),
4970 return (ZPOOL_COMPATIBILITY_WARNTOKEN
);
4973 strlcpy(report
, gettext("compatibility set ok"), rlen
);
4974 return (ZPOOL_COMPATIBILITY_OK
);
4978 zpool_vdev_guid(zpool_handle_t
*zhp
, const char *vdevname
, uint64_t *vdev_guid
)
4981 boolean_t avail_spare
, l2cache
;
4983 verify(zhp
!= NULL
);
4984 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
4985 char errbuf
[ERRBUFLEN
];
4986 (void) snprintf(errbuf
, sizeof (errbuf
),
4987 dgettext(TEXT_DOMAIN
, "pool is in an unavailable state"));
4988 return (zfs_error(zhp
->zpool_hdl
, EZFS_POOLUNAVAIL
, errbuf
));
4991 if ((tgt
= zpool_find_vdev(zhp
, vdevname
, &avail_spare
, &l2cache
,
4993 char errbuf
[ERRBUFLEN
];
4994 (void) snprintf(errbuf
, sizeof (errbuf
),
4995 dgettext(TEXT_DOMAIN
, "can not find %s in %s"),
4996 vdevname
, zhp
->zpool_name
);
4997 return (zfs_error(zhp
->zpool_hdl
, EZFS_NODEVICE
, errbuf
));
5000 *vdev_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
5005 * Get a vdev property value for 'prop' and return the value in
5006 * a pre-allocated buffer.
5009 zpool_get_vdev_prop_value(nvlist_t
*nvprop
, vdev_prop_t prop
, char *prop_name
,
5010 char *buf
, size_t len
, zprop_source_t
*srctype
, boolean_t literal
)
5015 zprop_source_t src
= ZPROP_SRC_NONE
;
5017 if (prop
== VDEV_PROP_USERPROP
) {
5018 /* user property, prop_name must contain the property name */
5019 assert(prop_name
!= NULL
);
5020 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
5021 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
5022 strval
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
5024 /* user prop not found */
5027 (void) strlcpy(buf
, strval
, len
);
5033 if (prop_name
== NULL
)
5034 prop_name
= (char *)vdev_prop_to_name(prop
);
5036 switch (vdev_prop_get_type(prop
)) {
5037 case PROP_TYPE_STRING
:
5038 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
5039 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
5040 strval
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
5042 src
= ZPROP_SRC_DEFAULT
;
5043 if ((strval
= vdev_prop_default_string(prop
)) == NULL
)
5046 (void) strlcpy(buf
, strval
, len
);
5049 case PROP_TYPE_NUMBER
:
5050 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
5051 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
5052 intval
= fnvlist_lookup_uint64(nv
, ZPROP_VALUE
);
5054 src
= ZPROP_SRC_DEFAULT
;
5055 intval
= vdev_prop_default_numeric(prop
);
5059 case VDEV_PROP_ASIZE
:
5060 case VDEV_PROP_PSIZE
:
5061 case VDEV_PROP_SIZE
:
5062 case VDEV_PROP_BOOTSIZE
:
5063 case VDEV_PROP_ALLOCATED
:
5064 case VDEV_PROP_FREE
:
5065 case VDEV_PROP_READ_ERRORS
:
5066 case VDEV_PROP_WRITE_ERRORS
:
5067 case VDEV_PROP_CHECKSUM_ERRORS
:
5068 case VDEV_PROP_INITIALIZE_ERRORS
:
5069 case VDEV_PROP_OPS_NULL
:
5070 case VDEV_PROP_OPS_READ
:
5071 case VDEV_PROP_OPS_WRITE
:
5072 case VDEV_PROP_OPS_FREE
:
5073 case VDEV_PROP_OPS_CLAIM
:
5074 case VDEV_PROP_OPS_TRIM
:
5075 case VDEV_PROP_BYTES_NULL
:
5076 case VDEV_PROP_BYTES_READ
:
5077 case VDEV_PROP_BYTES_WRITE
:
5078 case VDEV_PROP_BYTES_FREE
:
5079 case VDEV_PROP_BYTES_CLAIM
:
5080 case VDEV_PROP_BYTES_TRIM
:
5082 (void) snprintf(buf
, len
, "%llu",
5083 (u_longlong_t
)intval
);
5085 (void) zfs_nicenum(intval
, buf
, len
);
5088 case VDEV_PROP_EXPANDSZ
:
5090 (void) strlcpy(buf
, "-", len
);
5091 } else if (literal
) {
5092 (void) snprintf(buf
, len
, "%llu",
5093 (u_longlong_t
)intval
);
5095 (void) zfs_nicenum(intval
, buf
, len
);
5098 case VDEV_PROP_CAPACITY
:
5100 (void) snprintf(buf
, len
, "%llu",
5101 (u_longlong_t
)intval
);
5103 (void) snprintf(buf
, len
, "%llu%%",
5104 (u_longlong_t
)intval
);
5107 case VDEV_PROP_CHECKSUM_N
:
5108 case VDEV_PROP_CHECKSUM_T
:
5109 case VDEV_PROP_IO_N
:
5110 case VDEV_PROP_IO_T
:
5111 if (intval
== UINT64_MAX
) {
5112 (void) strlcpy(buf
, "-", len
);
5114 (void) snprintf(buf
, len
, "%llu",
5115 (u_longlong_t
)intval
);
5118 case VDEV_PROP_FRAGMENTATION
:
5119 if (intval
== UINT64_MAX
) {
5120 (void) strlcpy(buf
, "-", len
);
5122 (void) snprintf(buf
, len
, "%llu%%",
5123 (u_longlong_t
)intval
);
5126 case VDEV_PROP_STATE
:
5128 (void) snprintf(buf
, len
, "%llu",
5129 (u_longlong_t
)intval
);
5131 (void) strlcpy(buf
, zpool_state_to_name(intval
,
5132 VDEV_AUX_NONE
), len
);
5136 (void) snprintf(buf
, len
, "%llu",
5137 (u_longlong_t
)intval
);
5141 case PROP_TYPE_INDEX
:
5142 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
5143 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
5144 intval
= fnvlist_lookup_uint64(nv
, ZPROP_VALUE
);
5146 src
= ZPROP_SRC_DEFAULT
;
5147 intval
= vdev_prop_default_numeric(prop
);
5149 if (vdev_prop_index_to_string(prop
, intval
,
5150 (const char **)&strval
) != 0)
5152 (void) strlcpy(buf
, strval
, len
);
5166 * Get a vdev property value for 'prop_name' and return the value in
5167 * a pre-allocated buffer.
5170 zpool_get_vdev_prop(zpool_handle_t
*zhp
, const char *vdevname
, vdev_prop_t prop
,
5171 char *prop_name
, char *buf
, size_t len
, zprop_source_t
*srctype
,
5174 nvlist_t
*reqnvl
, *reqprops
;
5175 nvlist_t
*retprops
= NULL
;
5176 uint64_t vdev_guid
= 0;
5179 if ((ret
= zpool_vdev_guid(zhp
, vdevname
, &vdev_guid
)) != 0)
5182 if (nvlist_alloc(&reqnvl
, NV_UNIQUE_NAME
, 0) != 0)
5183 return (no_memory(zhp
->zpool_hdl
));
5184 if (nvlist_alloc(&reqprops
, NV_UNIQUE_NAME
, 0) != 0)
5185 return (no_memory(zhp
->zpool_hdl
));
5187 fnvlist_add_uint64(reqnvl
, ZPOOL_VDEV_PROPS_GET_VDEV
, vdev_guid
);
5189 if (prop
!= VDEV_PROP_USERPROP
) {
5190 /* prop_name overrides prop value */
5191 if (prop_name
!= NULL
)
5192 prop
= vdev_name_to_prop(prop_name
);
5194 prop_name
= (char *)vdev_prop_to_name(prop
);
5195 assert(prop
< VDEV_NUM_PROPS
);
5198 assert(prop_name
!= NULL
);
5199 if (nvlist_add_uint64(reqprops
, prop_name
, prop
) != 0) {
5200 nvlist_free(reqnvl
);
5201 nvlist_free(reqprops
);
5202 return (no_memory(zhp
->zpool_hdl
));
5205 fnvlist_add_nvlist(reqnvl
, ZPOOL_VDEV_PROPS_GET_PROPS
, reqprops
);
5207 ret
= lzc_get_vdev_prop(zhp
->zpool_name
, reqnvl
, &retprops
);
5210 ret
= zpool_get_vdev_prop_value(retprops
, prop
, prop_name
, buf
,
5211 len
, srctype
, literal
);
5213 char errbuf
[ERRBUFLEN
];
5214 (void) snprintf(errbuf
, sizeof (errbuf
),
5215 dgettext(TEXT_DOMAIN
, "cannot get vdev property %s from"
5216 " %s in %s"), prop_name
, vdevname
, zhp
->zpool_name
);
5217 (void) zpool_standard_error(zhp
->zpool_hdl
, ret
, errbuf
);
5220 nvlist_free(reqnvl
);
5221 nvlist_free(reqprops
);
5222 nvlist_free(retprops
);
5228 * Get all vdev properties
5231 zpool_get_all_vdev_props(zpool_handle_t
*zhp
, const char *vdevname
,
5234 nvlist_t
*nvl
= NULL
;
5235 uint64_t vdev_guid
= 0;
5238 if ((ret
= zpool_vdev_guid(zhp
, vdevname
, &vdev_guid
)) != 0)
5241 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5242 return (no_memory(zhp
->zpool_hdl
));
5244 fnvlist_add_uint64(nvl
, ZPOOL_VDEV_PROPS_GET_VDEV
, vdev_guid
);
5246 ret
= lzc_get_vdev_prop(zhp
->zpool_name
, nvl
, outnvl
);
5251 char errbuf
[ERRBUFLEN
];
5252 (void) snprintf(errbuf
, sizeof (errbuf
),
5253 dgettext(TEXT_DOMAIN
, "cannot get vdev properties for"
5254 " %s in %s"), vdevname
, zhp
->zpool_name
);
5255 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
5265 zpool_set_vdev_prop(zpool_handle_t
*zhp
, const char *vdevname
,
5266 const char *propname
, const char *propval
)
5269 nvlist_t
*nvl
= NULL
;
5270 nvlist_t
*outnvl
= NULL
;
5272 nvlist_t
*realprops
;
5273 prop_flags_t flags
= { 0 };
5277 if ((ret
= zpool_vdev_guid(zhp
, vdevname
, &vdev_guid
)) != 0)
5280 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5281 return (no_memory(zhp
->zpool_hdl
));
5282 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
5283 return (no_memory(zhp
->zpool_hdl
));
5285 fnvlist_add_uint64(nvl
, ZPOOL_VDEV_PROPS_SET_VDEV
, vdev_guid
);
5287 if (nvlist_add_string(props
, propname
, propval
) != 0) {
5289 return (no_memory(zhp
->zpool_hdl
));
5292 char errbuf
[ERRBUFLEN
];
5293 (void) snprintf(errbuf
, sizeof (errbuf
),
5294 dgettext(TEXT_DOMAIN
, "cannot set property %s for %s on %s"),
5295 propname
, vdevname
, zhp
->zpool_name
);
5298 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
5299 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
5300 zhp
->zpool_name
, props
, version
, flags
, errbuf
)) == NULL
) {
5309 fnvlist_add_nvlist(nvl
, ZPOOL_VDEV_PROPS_SET_PROPS
, props
);
5311 ret
= lzc_set_vdev_prop(zhp
->zpool_name
, nvl
, &outnvl
);
5315 nvlist_free(outnvl
);
5318 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);