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
:
343 (void) snprintf(buf
, len
, "%llu",
344 (u_longlong_t
)intval
);
346 (void) zfs_nicenum(intval
, buf
, len
);
349 case ZPOOL_PROP_EXPANDSZ
:
350 case ZPOOL_PROP_CHECKPOINT
:
352 (void) strlcpy(buf
, "-", len
);
353 } else if (literal
) {
354 (void) snprintf(buf
, len
, "%llu",
355 (u_longlong_t
)intval
);
357 (void) zfs_nicebytes(intval
, buf
, len
);
361 case ZPOOL_PROP_CAPACITY
:
363 (void) snprintf(buf
, len
, "%llu",
364 (u_longlong_t
)intval
);
366 (void) snprintf(buf
, len
, "%llu%%",
367 (u_longlong_t
)intval
);
371 case ZPOOL_PROP_FRAGMENTATION
:
372 if (intval
== UINT64_MAX
) {
373 (void) strlcpy(buf
, "-", len
);
374 } else if (literal
) {
375 (void) snprintf(buf
, len
, "%llu",
376 (u_longlong_t
)intval
);
378 (void) snprintf(buf
, len
, "%llu%%",
379 (u_longlong_t
)intval
);
383 case ZPOOL_PROP_DEDUPRATIO
:
385 (void) snprintf(buf
, len
, "%llu.%02llu",
386 (u_longlong_t
)(intval
/ 100),
387 (u_longlong_t
)(intval
% 100));
389 (void) snprintf(buf
, len
, "%llu.%02llux",
390 (u_longlong_t
)(intval
/ 100),
391 (u_longlong_t
)(intval
% 100));
394 case ZPOOL_PROP_HEALTH
:
395 (void) strlcpy(buf
, zpool_get_state_str(zhp
), len
);
397 case ZPOOL_PROP_VERSION
:
398 if (intval
>= SPA_VERSION_FEATURES
) {
399 (void) snprintf(buf
, len
, "-");
404 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
408 case PROP_TYPE_INDEX
:
409 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
410 if (zpool_prop_index_to_string(prop
, intval
, &strval
)
413 (void) strlcpy(buf
, strval
, len
);
427 * Check if the bootfs name has the same pool name as it is set to.
428 * Assuming bootfs is a valid dataset name.
431 bootfs_name_valid(const char *pool
, const char *bootfs
)
433 int len
= strlen(pool
);
434 if (bootfs
[0] == '\0')
437 if (!zfs_name_valid(bootfs
, ZFS_TYPE_FILESYSTEM
|ZFS_TYPE_SNAPSHOT
))
440 if (strncmp(pool
, bootfs
, len
) == 0 &&
441 (bootfs
[len
] == '/' || bootfs
[len
] == '\0'))
448 * Given an nvlist of zpool properties to be set, validate that they are
449 * correct, and parse any numeric properties (index, boolean, etc) if they are
450 * specified as strings.
453 zpool_valid_proplist(libzfs_handle_t
*hdl
, const char *poolname
,
454 nvlist_t
*props
, uint64_t version
, prop_flags_t flags
, char *errbuf
)
462 struct stat64 statbuf
;
466 if (nvlist_alloc(&retprops
, NV_UNIQUE_NAME
, 0) != 0) {
467 (void) no_memory(hdl
);
472 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
473 const char *propname
= nvpair_name(elem
);
475 if (flags
.vdevprop
&& zpool_prop_vdev(propname
)) {
476 vdev_prop_t vprop
= vdev_name_to_prop(propname
);
478 if (vdev_prop_readonly(vprop
)) {
479 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
480 "is readonly"), propname
);
481 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
486 if (zprop_parse_value(hdl
, elem
, vprop
, ZFS_TYPE_VDEV
,
487 retprops
, &strval
, &intval
, errbuf
) != 0)
491 } else if (flags
.vdevprop
&& vdev_prop_user(propname
)) {
492 if (nvlist_add_nvpair(retprops
, elem
) != 0) {
493 (void) no_memory(hdl
);
497 } else if (flags
.vdevprop
) {
498 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
499 "invalid property: '%s'"), propname
);
500 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
504 prop
= zpool_name_to_prop(propname
);
505 if (prop
== ZPOOL_PROP_INVAL
&& zpool_prop_feature(propname
)) {
507 char *fname
= strchr(propname
, '@') + 1;
509 err
= zfeature_lookup_name(fname
, NULL
);
511 ASSERT3U(err
, ==, ENOENT
);
512 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
513 "feature '%s' unsupported by kernel"),
515 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
519 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
520 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
521 "'%s' must be a string"), propname
);
522 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
526 (void) nvpair_value_string(elem
, &strval
);
527 if (strcmp(strval
, ZFS_FEATURE_ENABLED
) != 0 &&
528 strcmp(strval
, ZFS_FEATURE_DISABLED
) != 0) {
529 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
530 "property '%s' can only be set to "
531 "'enabled' or 'disabled'"), propname
);
532 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
537 strcmp(strval
, ZFS_FEATURE_DISABLED
) == 0) {
538 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
539 "property '%s' can only be set to "
540 "'disabled' at creation time"), propname
);
541 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
545 if (nvlist_add_uint64(retprops
, propname
, 0) != 0) {
546 (void) no_memory(hdl
);
553 * Make sure this property is valid and applies to this type.
555 if (prop
== ZPOOL_PROP_INVAL
) {
556 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
557 "invalid property '%s'"), propname
);
558 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
562 if (zpool_prop_readonly(prop
)) {
563 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
564 "is readonly"), propname
);
565 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
569 if (!flags
.create
&& zpool_prop_setonce(prop
)) {
570 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
571 "property '%s' can only be set at "
572 "creation time"), propname
);
573 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
577 if (zprop_parse_value(hdl
, elem
, prop
, ZFS_TYPE_POOL
, retprops
,
578 &strval
, &intval
, errbuf
) != 0)
582 * Perform additional checking for specific properties.
585 case ZPOOL_PROP_VERSION
:
586 if (intval
< version
||
587 !SPA_VERSION_IS_SUPPORTED(intval
)) {
588 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
589 "property '%s' number %llu is invalid."),
590 propname
, (unsigned long long)intval
);
591 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
596 case ZPOOL_PROP_ASHIFT
:
598 (intval
< ASHIFT_MIN
|| intval
> ASHIFT_MAX
)) {
599 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
600 "property '%s' number %llu is invalid, "
601 "only values between %" PRId32
" and %"
602 PRId32
" are allowed."),
603 propname
, (unsigned long long)intval
,
604 ASHIFT_MIN
, ASHIFT_MAX
);
605 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
610 case ZPOOL_PROP_BOOTFS
:
611 if (flags
.create
|| flags
.import
) {
612 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
613 "property '%s' cannot be set at creation "
614 "or import time"), propname
);
615 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
619 if (version
< SPA_VERSION_BOOTFS
) {
620 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
621 "pool must be upgraded to support "
622 "'%s' property"), propname
);
623 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
628 * bootfs property value has to be a dataset name and
629 * the dataset has to be in the same pool as it sets to.
631 if (!bootfs_name_valid(poolname
, strval
)) {
632 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
633 "is an invalid name"), strval
);
634 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
638 if ((zhp
= zpool_open_canfail(hdl
, poolname
)) == NULL
) {
639 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
640 "could not open pool '%s'"), poolname
);
641 (void) zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
);
647 case ZPOOL_PROP_ALTROOT
:
648 if (!flags
.create
&& !flags
.import
) {
649 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
650 "property '%s' can only be set during pool "
651 "creation or import"), propname
);
652 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
656 if (strval
[0] != '/') {
657 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
658 "bad alternate root '%s'"), strval
);
659 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
664 case ZPOOL_PROP_CACHEFILE
:
665 if (strval
[0] == '\0')
668 if (strcmp(strval
, "none") == 0)
671 if (strval
[0] != '/') {
672 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
673 "property '%s' must be empty, an "
674 "absolute path, or 'none'"), propname
);
675 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
679 slash
= strrchr(strval
, '/');
681 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
682 strcmp(slash
, "/..") == 0) {
683 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
684 "'%s' is not a valid file"), strval
);
685 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
691 if (strval
[0] != '\0' &&
692 (stat64(strval
, &statbuf
) != 0 ||
693 !S_ISDIR(statbuf
.st_mode
))) {
694 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
695 "'%s' is not a valid directory"),
697 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
704 case ZPOOL_PROP_COMPATIBILITY
:
705 switch (zpool_load_compat(strval
, NULL
, report
, 1024)) {
706 case ZPOOL_COMPATIBILITY_OK
:
707 case ZPOOL_COMPATIBILITY_WARNTOKEN
:
709 case ZPOOL_COMPATIBILITY_BADFILE
:
710 case ZPOOL_COMPATIBILITY_BADTOKEN
:
711 case ZPOOL_COMPATIBILITY_NOFILES
:
712 zfs_error_aux(hdl
, "%s", report
);
713 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
718 case ZPOOL_PROP_COMMENT
:
719 for (check
= strval
; *check
!= '\0'; check
++) {
720 if (!isprint(*check
)) {
722 dgettext(TEXT_DOMAIN
,
723 "comment may only have printable "
725 (void) zfs_error(hdl
, EZFS_BADPROP
,
730 if (strlen(strval
) > ZPROP_MAX_COMMENT
) {
731 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
732 "comment must not exceed %d characters"),
734 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
738 case ZPOOL_PROP_READONLY
:
740 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
741 "property '%s' can only be set at "
742 "import time"), propname
);
743 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
747 case ZPOOL_PROP_MULTIHOST
:
748 if (get_system_hostid() == 0) {
749 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
750 "requires a non-zero system hostid"));
751 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
755 case ZPOOL_PROP_DEDUPDITTO
:
756 printf("Note: property '%s' no longer has "
757 "any effect\n", propname
);
767 nvlist_free(retprops
);
772 * Set zpool property : propname=propval.
775 zpool_set_prop(zpool_handle_t
*zhp
, const char *propname
, const char *propval
)
777 zfs_cmd_t zc
= {"\0"};
779 char errbuf
[ERRBUFLEN
];
780 nvlist_t
*nvl
= NULL
;
783 prop_flags_t flags
= { 0 };
785 (void) snprintf(errbuf
, sizeof (errbuf
),
786 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
789 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
790 return (no_memory(zhp
->zpool_hdl
));
792 if (nvlist_add_string(nvl
, propname
, propval
) != 0) {
794 return (no_memory(zhp
->zpool_hdl
));
797 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
798 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
799 zhp
->zpool_name
, nvl
, version
, flags
, errbuf
)) == NULL
) {
808 * Execute the corresponding ioctl() to set this property.
810 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
812 zcmd_write_src_nvlist(zhp
->zpool_hdl
, &zc
, nvl
);
814 ret
= zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_SET_PROPS
, &zc
);
816 zcmd_free_nvlists(&zc
);
820 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
822 (void) zpool_props_refresh(zhp
);
828 zpool_expand_proplist(zpool_handle_t
*zhp
, zprop_list_t
**plp
,
829 zfs_type_t type
, boolean_t literal
)
831 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
833 char buf
[ZFS_MAXPROPLEN
];
834 nvlist_t
*features
= NULL
;
837 boolean_t firstexpand
= (NULL
== *plp
);
840 if (zprop_expand_list(hdl
, plp
, type
) != 0)
843 if (type
== ZFS_TYPE_VDEV
)
847 while (*last
!= NULL
)
848 last
= &(*last
)->pl_next
;
851 features
= zpool_get_features(zhp
);
853 if ((*plp
)->pl_all
&& firstexpand
) {
854 for (i
= 0; i
< SPA_FEATURES
; i
++) {
855 zprop_list_t
*entry
= zfs_alloc(hdl
,
856 sizeof (zprop_list_t
));
857 entry
->pl_prop
= ZPROP_USERPROP
;
858 entry
->pl_user_prop
= zfs_asprintf(hdl
, "feature@%s",
859 spa_feature_table
[i
].fi_uname
);
860 entry
->pl_width
= strlen(entry
->pl_user_prop
);
861 entry
->pl_all
= B_TRUE
;
864 last
= &entry
->pl_next
;
868 /* add any unsupported features */
869 for (nvp
= nvlist_next_nvpair(features
, NULL
);
870 nvp
!= NULL
; nvp
= nvlist_next_nvpair(features
, nvp
)) {
875 if (zfeature_is_supported(nvpair_name(nvp
)))
878 propname
= zfs_asprintf(hdl
, "unsupported@%s",
882 * Before adding the property to the list make sure that no
883 * other pool already added the same property.
887 while (entry
!= NULL
) {
888 if (entry
->pl_user_prop
!= NULL
&&
889 strcmp(propname
, entry
->pl_user_prop
) == 0) {
893 entry
= entry
->pl_next
;
900 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
901 entry
->pl_prop
= ZPROP_USERPROP
;
902 entry
->pl_user_prop
= propname
;
903 entry
->pl_width
= strlen(entry
->pl_user_prop
);
904 entry
->pl_all
= B_TRUE
;
907 last
= &entry
->pl_next
;
910 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
911 if (entry
->pl_fixed
&& !literal
)
914 if (entry
->pl_prop
!= ZPROP_USERPROP
&&
915 zpool_get_prop(zhp
, entry
->pl_prop
, buf
, sizeof (buf
),
916 NULL
, literal
) == 0) {
917 if (strlen(buf
) > entry
->pl_width
)
918 entry
->pl_width
= strlen(buf
);
926 vdev_expand_proplist(zpool_handle_t
*zhp
, const char *vdevname
,
930 char buf
[ZFS_MAXPROPLEN
];
933 nvpair_t
*elem
= NULL
;
934 nvlist_t
*vprops
= NULL
;
935 nvlist_t
*propval
= NULL
;
936 const char *propname
;
940 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
944 if (zpool_get_vdev_prop(zhp
, vdevname
, entry
->pl_prop
,
945 entry
->pl_user_prop
, buf
, sizeof (buf
), NULL
,
947 if (strlen(buf
) > entry
->pl_width
)
948 entry
->pl_width
= strlen(buf
);
950 if (entry
->pl_prop
== VDEV_PROP_NAME
&&
951 strlen(vdevname
) > entry
->pl_width
)
952 entry
->pl_width
= strlen(vdevname
);
955 /* Handle the all properties case */
957 if (*last
!= NULL
&& (*last
)->pl_all
== B_TRUE
) {
958 while (*last
!= NULL
)
959 last
= &(*last
)->pl_next
;
961 err
= zpool_get_all_vdev_props(zhp
, vdevname
, &vprops
);
965 while ((elem
= nvlist_next_nvpair(vprops
, elem
)) != NULL
) {
966 propname
= nvpair_name(elem
);
968 /* Skip properties that are not user defined */
969 if ((prop
= vdev_name_to_prop(propname
)) !=
973 if (nvpair_value_nvlist(elem
, &propval
) != 0)
976 strval
= fnvlist_lookup_string(propval
, ZPROP_VALUE
);
978 entry
= zfs_alloc(zhp
->zpool_hdl
,
979 sizeof (zprop_list_t
));
980 entry
->pl_prop
= prop
;
981 entry
->pl_user_prop
= zfs_strdup(zhp
->zpool_hdl
,
983 entry
->pl_width
= strlen(strval
);
984 entry
->pl_all
= B_TRUE
;
986 last
= &entry
->pl_next
;
994 * Get the state for the given feature on the given ZFS pool.
997 zpool_prop_get_feature(zpool_handle_t
*zhp
, const char *propname
, char *buf
,
1001 boolean_t found
= B_FALSE
;
1002 nvlist_t
*features
= zpool_get_features(zhp
);
1003 boolean_t supported
;
1004 const char *feature
= strchr(propname
, '@') + 1;
1006 supported
= zpool_prop_feature(propname
);
1007 ASSERT(supported
|| zpool_prop_unsupported(propname
));
1010 * Convert from feature name to feature guid. This conversion is
1011 * unnecessary for unsupported@... properties because they already
1018 ret
= zfeature_lookup_name(feature
, &fid
);
1020 (void) strlcpy(buf
, "-", len
);
1023 feature
= spa_feature_table
[fid
].fi_guid
;
1026 if (nvlist_lookup_uint64(features
, feature
, &refcount
) == 0)
1031 (void) strlcpy(buf
, ZFS_FEATURE_DISABLED
, len
);
1034 (void) strlcpy(buf
, ZFS_FEATURE_ENABLED
, len
);
1036 (void) strlcpy(buf
, ZFS_FEATURE_ACTIVE
, len
);
1040 if (refcount
== 0) {
1041 (void) strcpy(buf
, ZFS_UNSUPPORTED_INACTIVE
);
1043 (void) strcpy(buf
, ZFS_UNSUPPORTED_READONLY
);
1046 (void) strlcpy(buf
, "-", len
);
1055 * Validate the given pool name, optionally putting an extended error message in
1059 zpool_name_valid(libzfs_handle_t
*hdl
, boolean_t isopen
, const char *pool
)
1061 namecheck_err_t why
;
1065 ret
= pool_namecheck(pool
, &why
, &what
);
1068 * The rules for reserved pool names were extended at a later point.
1069 * But we need to support users with existing pools that may now be
1070 * invalid. So we only check for this expanded set of names during a
1071 * create (or import), and only in userland.
1073 if (ret
== 0 && !isopen
&&
1074 (strncmp(pool
, "mirror", 6) == 0 ||
1075 strncmp(pool
, "raidz", 5) == 0 ||
1076 strncmp(pool
, "draid", 5) == 0 ||
1077 strncmp(pool
, "spare", 5) == 0 ||
1078 strcmp(pool
, "log") == 0)) {
1081 dgettext(TEXT_DOMAIN
, "name is reserved"));
1089 case NAME_ERR_TOOLONG
:
1091 dgettext(TEXT_DOMAIN
, "name is too long"));
1094 case NAME_ERR_INVALCHAR
:
1096 dgettext(TEXT_DOMAIN
, "invalid character "
1097 "'%c' in pool name"), what
);
1100 case NAME_ERR_NOLETTER
:
1101 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1102 "name must begin with a letter"));
1105 case NAME_ERR_RESERVED
:
1106 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1107 "name is reserved"));
1110 case NAME_ERR_DISKLIKE
:
1111 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1112 "pool name is reserved"));
1115 case NAME_ERR_LEADING_SLASH
:
1116 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1117 "leading slash in name"));
1120 case NAME_ERR_EMPTY_COMPONENT
:
1121 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1122 "empty component in name"));
1125 case NAME_ERR_TRAILING_SLASH
:
1126 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1127 "trailing slash in name"));
1130 case NAME_ERR_MULTIPLE_DELIMITERS
:
1131 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1132 "multiple '@' and/or '#' delimiters in "
1136 case NAME_ERR_NO_AT
:
1137 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1138 "permission set is missing '@'"));
1142 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1143 "(%d) not defined"), why
);
1154 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1158 zpool_open_canfail(libzfs_handle_t
*hdl
, const char *pool
)
1160 zpool_handle_t
*zhp
;
1164 * Make sure the pool name is valid.
1166 if (!zpool_name_valid(hdl
, B_TRUE
, pool
)) {
1167 (void) zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1168 dgettext(TEXT_DOMAIN
, "cannot open '%s'"),
1173 zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
));
1175 zhp
->zpool_hdl
= hdl
;
1176 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1178 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1184 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "no such pool"));
1185 (void) zfs_error_fmt(hdl
, EZFS_NOENT
,
1186 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), pool
);
1195 * Like the above, but silent on error. Used when iterating over pools (because
1196 * the configuration cache may be out of date).
1199 zpool_open_silent(libzfs_handle_t
*hdl
, const char *pool
, zpool_handle_t
**ret
)
1201 zpool_handle_t
*zhp
;
1204 zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
));
1206 zhp
->zpool_hdl
= hdl
;
1207 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1209 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1225 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1229 zpool_open(libzfs_handle_t
*hdl
, const char *pool
)
1231 zpool_handle_t
*zhp
;
1233 if ((zhp
= zpool_open_canfail(hdl
, pool
)) == NULL
)
1236 if (zhp
->zpool_state
== POOL_STATE_UNAVAIL
) {
1237 (void) zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
1238 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), zhp
->zpool_name
);
1247 * Close the handle. Simply frees the memory associated with the handle.
1250 zpool_close(zpool_handle_t
*zhp
)
1252 nvlist_free(zhp
->zpool_config
);
1253 nvlist_free(zhp
->zpool_old_config
);
1254 nvlist_free(zhp
->zpool_props
);
1259 * Return the name of the pool.
1262 zpool_get_name(zpool_handle_t
*zhp
)
1264 return (zhp
->zpool_name
);
1269 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1272 zpool_get_state(zpool_handle_t
*zhp
)
1274 return (zhp
->zpool_state
);
1278 * Check if vdev list contains a special vdev
1281 zpool_has_special_vdev(nvlist_t
*nvroot
)
1286 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
, &child
,
1288 for (uint_t c
= 0; c
< children
; c
++) {
1291 if (nvlist_lookup_string(child
[c
],
1292 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
) == 0 &&
1293 strcmp(bias
, VDEV_ALLOC_BIAS_SPECIAL
) == 0) {
1302 * Check if vdev list contains a dRAID vdev
1305 zpool_has_draid_vdev(nvlist_t
*nvroot
)
1310 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
1311 &child
, &children
) == 0) {
1312 for (uint_t c
= 0; c
< children
; c
++) {
1315 if (nvlist_lookup_string(child
[c
],
1316 ZPOOL_CONFIG_TYPE
, &type
) == 0 &&
1317 strcmp(type
, VDEV_TYPE_DRAID
) == 0) {
1326 * Output a dRAID top-level vdev name in to the provided buffer.
1329 zpool_draid_name(char *name
, int len
, uint64_t data
, uint64_t parity
,
1330 uint64_t spares
, uint64_t children
)
1332 snprintf(name
, len
, "%s%llu:%llud:%lluc:%llus",
1333 VDEV_TYPE_DRAID
, (u_longlong_t
)parity
, (u_longlong_t
)data
,
1334 (u_longlong_t
)children
, (u_longlong_t
)spares
);
1340 * Return B_TRUE if the provided name is a dRAID spare name.
1343 zpool_is_draid_spare(const char *name
)
1345 uint64_t spare_id
, parity
, vdev_id
;
1347 if (sscanf(name
, VDEV_TYPE_DRAID
"%llu-%llu-%llu",
1348 (u_longlong_t
*)&parity
, (u_longlong_t
*)&vdev_id
,
1349 (u_longlong_t
*)&spare_id
) == 3) {
1357 * Create the named pool, using the provided vdev list. It is assumed
1358 * that the consumer has already validated the contents of the nvlist, so we
1359 * don't have to worry about error semantics.
1362 zpool_create(libzfs_handle_t
*hdl
, const char *pool
, nvlist_t
*nvroot
,
1363 nvlist_t
*props
, nvlist_t
*fsprops
)
1365 zfs_cmd_t zc
= {"\0"};
1366 nvlist_t
*zc_fsprops
= NULL
;
1367 nvlist_t
*zc_props
= NULL
;
1368 nvlist_t
*hidden_args
= NULL
;
1369 uint8_t *wkeydata
= NULL
;
1371 char errbuf
[ERRBUFLEN
];
1374 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1375 "cannot create '%s'"), pool
);
1377 if (!zpool_name_valid(hdl
, B_FALSE
, pool
))
1378 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
1380 zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
);
1383 prop_flags_t flags
= { .create
= B_TRUE
, .import
= B_FALSE
};
1385 if ((zc_props
= zpool_valid_proplist(hdl
, pool
, props
,
1386 SPA_VERSION_1
, flags
, errbuf
)) == NULL
) {
1395 zoned
= ((nvlist_lookup_string(fsprops
,
1396 zfs_prop_to_name(ZFS_PROP_ZONED
), &zonestr
) == 0) &&
1397 strcmp(zonestr
, "on") == 0);
1399 if ((zc_fsprops
= zfs_valid_proplist(hdl
, ZFS_TYPE_FILESYSTEM
,
1400 fsprops
, zoned
, NULL
, NULL
, B_TRUE
, errbuf
)) == NULL
) {
1404 if (nvlist_exists(zc_fsprops
,
1405 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS
)) &&
1406 !zpool_has_special_vdev(nvroot
)) {
1407 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1408 "%s property requires a special vdev"),
1409 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS
));
1410 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1415 (nvlist_alloc(&zc_props
, NV_UNIQUE_NAME
, 0) != 0)) {
1418 if (zfs_crypto_create(hdl
, NULL
, zc_fsprops
, props
, B_TRUE
,
1419 &wkeydata
, &wkeylen
) != 0) {
1420 zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
);
1423 if (nvlist_add_nvlist(zc_props
,
1424 ZPOOL_ROOTFS_PROPS
, zc_fsprops
) != 0) {
1427 if (wkeydata
!= NULL
) {
1428 if (nvlist_alloc(&hidden_args
, NV_UNIQUE_NAME
, 0) != 0)
1431 if (nvlist_add_uint8_array(hidden_args
, "wkeydata",
1432 wkeydata
, wkeylen
) != 0)
1435 if (nvlist_add_nvlist(zc_props
, ZPOOL_HIDDEN_ARGS
,
1442 zcmd_write_src_nvlist(hdl
, &zc
, zc_props
);
1444 (void) strlcpy(zc
.zc_name
, pool
, sizeof (zc
.zc_name
));
1446 if ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_CREATE
, &zc
)) != 0) {
1448 zcmd_free_nvlists(&zc
);
1449 nvlist_free(zc_props
);
1450 nvlist_free(zc_fsprops
);
1451 nvlist_free(hidden_args
);
1452 if (wkeydata
!= NULL
)
1458 * This can happen if the user has specified the same
1459 * device multiple times. We can't reliably detect this
1460 * until we try to add it and see we already have a
1461 * label. This can also happen under if the device is
1462 * part of an active md or lvm device.
1464 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1465 "one or more vdevs refer to the same device, or "
1466 "one of\nthe devices is part of an active md or "
1468 return (zfs_error(hdl
, EZFS_BADDEV
, errbuf
));
1472 * This happens if the record size is smaller or larger
1473 * than the allowed size range, or not a power of 2.
1475 * NOTE: although zfs_valid_proplist is called earlier,
1476 * this case may have slipped through since the
1477 * pool does not exist yet and it is therefore
1478 * impossible to read properties e.g. max blocksize
1481 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1482 "record size invalid"));
1483 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1487 * This occurs when one of the devices is below
1488 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1489 * device was the problem device since there's no
1490 * reliable way to determine device size from userland.
1495 zfs_nicebytes(SPA_MINDEVSIZE
, buf
,
1498 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1499 "one or more devices is less than the "
1500 "minimum size (%s)"), buf
);
1502 return (zfs_error(hdl
, EZFS_BADDEV
, errbuf
));
1505 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1506 "one or more devices is out of space"));
1507 return (zfs_error(hdl
, EZFS_BADDEV
, errbuf
));
1510 if (zpool_has_draid_vdev(nvroot
) &&
1511 zfeature_lookup_name("draid", NULL
) != 0) {
1512 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1513 "dRAID vdevs are unsupported by the "
1515 return (zfs_error(hdl
, EZFS_BADDEV
, errbuf
));
1517 return (zpool_standard_error(hdl
, errno
,
1522 return (zpool_standard_error(hdl
, errno
, errbuf
));
1527 zcmd_free_nvlists(&zc
);
1528 nvlist_free(zc_props
);
1529 nvlist_free(zc_fsprops
);
1530 nvlist_free(hidden_args
);
1531 if (wkeydata
!= NULL
)
1537 * Destroy the given pool. It is up to the caller to ensure that there are no
1538 * datasets left in the pool.
1541 zpool_destroy(zpool_handle_t
*zhp
, const char *log_str
)
1543 zfs_cmd_t zc
= {"\0"};
1544 zfs_handle_t
*zfp
= NULL
;
1545 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1546 char errbuf
[ERRBUFLEN
];
1548 if (zhp
->zpool_state
== POOL_STATE_ACTIVE
&&
1549 (zfp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
1552 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1553 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1555 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_DESTROY
, &zc
) != 0) {
1556 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1557 "cannot destroy '%s'"), zhp
->zpool_name
);
1559 if (errno
== EROFS
) {
1560 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1561 "one or more devices is read only"));
1562 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
1564 (void) zpool_standard_error(hdl
, errno
, errbuf
);
1573 remove_mountpoint(zfp
);
1581 * Create a checkpoint in the given pool.
1584 zpool_checkpoint(zpool_handle_t
*zhp
)
1586 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1587 char errbuf
[ERRBUFLEN
];
1590 error
= lzc_pool_checkpoint(zhp
->zpool_name
);
1592 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1593 "cannot checkpoint '%s'"), zhp
->zpool_name
);
1594 (void) zpool_standard_error(hdl
, error
, errbuf
);
1602 * Discard the checkpoint from the given pool.
1605 zpool_discard_checkpoint(zpool_handle_t
*zhp
)
1607 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1608 char errbuf
[ERRBUFLEN
];
1611 error
= lzc_pool_checkpoint_discard(zhp
->zpool_name
);
1613 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1614 "cannot discard checkpoint in '%s'"), zhp
->zpool_name
);
1615 (void) zpool_standard_error(hdl
, error
, errbuf
);
1623 * Add the given vdevs to the pool. The caller must have already performed the
1624 * necessary verification to ensure that the vdev specification is well-formed.
1627 zpool_add(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
1629 zfs_cmd_t zc
= {"\0"};
1631 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1632 char errbuf
[ERRBUFLEN
];
1633 nvlist_t
**spares
, **l2cache
;
1634 uint_t nspares
, nl2cache
;
1636 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1637 "cannot add to '%s'"), zhp
->zpool_name
);
1639 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1640 SPA_VERSION_SPARES
&&
1641 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1642 &spares
, &nspares
) == 0) {
1643 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1644 "upgraded to add hot spares"));
1645 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
1648 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1649 SPA_VERSION_L2CACHE
&&
1650 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1651 &l2cache
, &nl2cache
) == 0) {
1652 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1653 "upgraded to add cache devices"));
1654 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
1657 zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
);
1658 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1660 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_ADD
, &zc
) != 0) {
1664 * This can happen if the user has specified the same
1665 * device multiple times. We can't reliably detect this
1666 * until we try to add it and see we already have a
1669 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1670 "one or more vdevs refer to the same device"));
1671 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
1676 if (zpool_has_draid_vdev(nvroot
) &&
1677 zfeature_lookup_name("draid", NULL
) != 0) {
1678 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1679 "dRAID vdevs are unsupported by the "
1682 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1683 "invalid config; a pool with removing/"
1684 "removed vdevs does not support adding "
1685 "raidz or dRAID vdevs"));
1688 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
1693 * This occurs when one of the devices is below
1694 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1695 * device was the problem device since there's no
1696 * reliable way to determine device size from userland.
1701 zfs_nicebytes(SPA_MINDEVSIZE
, buf
,
1704 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1705 "device is less than the minimum "
1708 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
1712 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1713 "pool must be upgraded to add these vdevs"));
1714 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1718 (void) zpool_standard_error(hdl
, errno
, errbuf
);
1726 zcmd_free_nvlists(&zc
);
1732 * Exports the pool from the system. The caller must ensure that there are no
1733 * mounted datasets in the pool.
1736 zpool_export_common(zpool_handle_t
*zhp
, boolean_t force
, boolean_t hardforce
,
1737 const char *log_str
)
1739 zfs_cmd_t zc
= {"\0"};
1741 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1742 zc
.zc_cookie
= force
;
1743 zc
.zc_guid
= hardforce
;
1744 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1746 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_EXPORT
, &zc
) != 0) {
1749 zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
1750 "use '-f' to override the following errors:\n"
1751 "'%s' has an active shared spare which could be"
1752 " used by other pools once '%s' is exported."),
1753 zhp
->zpool_name
, zhp
->zpool_name
);
1754 return (zfs_error_fmt(zhp
->zpool_hdl
, EZFS_ACTIVE_SPARE
,
1755 dgettext(TEXT_DOMAIN
, "cannot export '%s'"),
1758 return (zpool_standard_error_fmt(zhp
->zpool_hdl
, errno
,
1759 dgettext(TEXT_DOMAIN
, "cannot export '%s'"),
1768 zpool_export(zpool_handle_t
*zhp
, boolean_t force
, const char *log_str
)
1770 return (zpool_export_common(zhp
, force
, B_FALSE
, log_str
));
1774 zpool_export_force(zpool_handle_t
*zhp
, const char *log_str
)
1776 return (zpool_export_common(zhp
, B_TRUE
, B_TRUE
, log_str
));
1780 zpool_rewind_exclaim(libzfs_handle_t
*hdl
, const char *name
, boolean_t dryrun
,
1783 nvlist_t
*nv
= NULL
;
1789 if (!hdl
->libzfs_printerr
|| config
== NULL
)
1792 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1793 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0) {
1797 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1799 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1801 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1802 strftime(timestr
, 128, "%c", &t
) != 0) {
1804 (void) printf(dgettext(TEXT_DOMAIN
,
1805 "Would be able to return %s "
1806 "to its state as of %s.\n"),
1809 (void) printf(dgettext(TEXT_DOMAIN
,
1810 "Pool %s returned to its state as of %s.\n"),
1814 (void) printf(dgettext(TEXT_DOMAIN
,
1815 "%s approximately %lld "),
1816 dryrun
? "Would discard" : "Discarded",
1817 ((longlong_t
)loss
+ 30) / 60);
1818 (void) printf(dgettext(TEXT_DOMAIN
,
1819 "minutes of transactions.\n"));
1820 } else if (loss
> 0) {
1821 (void) printf(dgettext(TEXT_DOMAIN
,
1822 "%s approximately %lld "),
1823 dryrun
? "Would discard" : "Discarded",
1825 (void) printf(dgettext(TEXT_DOMAIN
,
1826 "seconds of transactions.\n"));
1832 zpool_explain_recover(libzfs_handle_t
*hdl
, const char *name
, int reason
,
1835 nvlist_t
*nv
= NULL
;
1837 uint64_t edata
= UINT64_MAX
;
1842 if (!hdl
->libzfs_printerr
)
1846 (void) printf(dgettext(TEXT_DOMAIN
, "action: "));
1848 (void) printf(dgettext(TEXT_DOMAIN
, "\t"));
1850 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1851 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1852 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0 ||
1853 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1856 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1857 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_DATA_ERRORS
,
1860 (void) printf(dgettext(TEXT_DOMAIN
,
1861 "Recovery is possible, but will result in some data loss.\n"));
1863 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1864 strftime(timestr
, 128, "%c", &t
) != 0) {
1865 (void) printf(dgettext(TEXT_DOMAIN
,
1866 "\tReturning the pool to its state as of %s\n"
1867 "\tshould correct the problem. "),
1870 (void) printf(dgettext(TEXT_DOMAIN
,
1871 "\tReverting the pool to an earlier state "
1872 "should correct the problem.\n\t"));
1876 (void) printf(dgettext(TEXT_DOMAIN
,
1877 "Approximately %lld minutes of data\n"
1878 "\tmust be discarded, irreversibly. "),
1879 ((longlong_t
)loss
+ 30) / 60);
1880 } else if (loss
> 0) {
1881 (void) printf(dgettext(TEXT_DOMAIN
,
1882 "Approximately %lld seconds of data\n"
1883 "\tmust be discarded, irreversibly. "),
1886 if (edata
!= 0 && edata
!= UINT64_MAX
) {
1888 (void) printf(dgettext(TEXT_DOMAIN
,
1889 "After rewind, at least\n"
1890 "\tone persistent user-data error will remain. "));
1892 (void) printf(dgettext(TEXT_DOMAIN
,
1893 "After rewind, several\n"
1894 "\tpersistent user-data errors will remain. "));
1897 (void) printf(dgettext(TEXT_DOMAIN
,
1898 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1899 reason
>= 0 ? "clear" : "import", name
);
1901 (void) printf(dgettext(TEXT_DOMAIN
,
1902 "A scrub of the pool\n"
1903 "\tis strongly recommended after recovery.\n"));
1907 (void) printf(dgettext(TEXT_DOMAIN
,
1908 "Destroy and re-create the pool from\n\ta backup source.\n"));
1912 * zpool_import() is a contracted interface. Should be kept the same
1915 * Applications should use zpool_import_props() to import a pool with
1916 * new properties value to be set.
1919 zpool_import(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1922 nvlist_t
*props
= NULL
;
1925 if (altroot
!= NULL
) {
1926 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
1927 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1928 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1932 if (nvlist_add_string(props
,
1933 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), altroot
) != 0 ||
1934 nvlist_add_string(props
,
1935 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
), "none") != 0) {
1937 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1938 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1943 ret
= zpool_import_props(hdl
, config
, newname
, props
,
1950 print_vdev_tree(libzfs_handle_t
*hdl
, const char *name
, nvlist_t
*nv
,
1956 uint64_t is_log
= 0;
1958 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
,
1962 (void) printf("\t%*s%s%s\n", indent
, "", name
,
1963 is_log
? " [log]" : "");
1965 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1966 &child
, &children
) != 0)
1969 for (c
= 0; c
< children
; c
++) {
1970 vname
= zpool_vdev_name(hdl
, NULL
, child
[c
], VDEV_NAME_TYPE_ID
);
1971 print_vdev_tree(hdl
, vname
, child
[c
], indent
+ 2);
1977 zpool_print_unsup_feat(nvlist_t
*config
)
1979 nvlist_t
*nvinfo
, *unsup_feat
;
1981 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
1982 unsup_feat
= fnvlist_lookup_nvlist(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
);
1984 for (nvpair_t
*nvp
= nvlist_next_nvpair(unsup_feat
, NULL
);
1985 nvp
!= NULL
; nvp
= nvlist_next_nvpair(unsup_feat
, nvp
)) {
1986 char *desc
= fnvpair_value_string(nvp
);
1987 if (strlen(desc
) > 0)
1988 (void) printf("\t%s (%s)\n", nvpair_name(nvp
), desc
);
1990 (void) printf("\t%s\n", nvpair_name(nvp
));
1995 * Import the given pool using the known configuration and a list of
1996 * properties to be set. The configuration should have come from
1997 * zpool_find_import(). The 'newname' parameters control whether the pool
1998 * is imported with a different name.
2001 zpool_import_props(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
2002 nvlist_t
*props
, int flags
)
2004 zfs_cmd_t zc
= {"\0"};
2005 zpool_load_policy_t policy
;
2006 nvlist_t
*nv
= NULL
;
2007 nvlist_t
*nvinfo
= NULL
;
2008 nvlist_t
*missing
= NULL
;
2009 const char *thename
;
2013 char errbuf
[ERRBUFLEN
];
2015 origname
= fnvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
);
2017 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2018 "cannot import pool '%s'"), origname
);
2020 if (newname
!= NULL
) {
2021 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
2022 return (zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
2023 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
2030 if (props
!= NULL
) {
2032 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
2034 version
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
2036 if ((props
= zpool_valid_proplist(hdl
, origname
,
2037 props
, version
, flags
, errbuf
)) == NULL
)
2039 zcmd_write_src_nvlist(hdl
, &zc
, props
);
2043 (void) strlcpy(zc
.zc_name
, thename
, sizeof (zc
.zc_name
));
2045 zc
.zc_guid
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
);
2047 zcmd_write_conf_nvlist(hdl
, &zc
, config
);
2048 zcmd_alloc_dst_nvlist(hdl
, &zc
, zc
.zc_nvlist_conf_size
* 2);
2050 zc
.zc_cookie
= flags
;
2051 while ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_IMPORT
, &zc
)) != 0 &&
2053 zcmd_expand_dst_nvlist(hdl
, &zc
);
2057 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nv
);
2059 zcmd_free_nvlists(&zc
);
2061 zpool_get_load_policy(config
, &policy
);
2068 * Dry-run failed, but we print out what success
2069 * looks like if we found a best txg
2071 if (policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) {
2072 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
2078 if (newname
== NULL
)
2079 (void) snprintf(desc
, sizeof (desc
),
2080 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
2083 (void) snprintf(desc
, sizeof (desc
),
2084 dgettext(TEXT_DOMAIN
, "cannot import '%s' as '%s'"),
2089 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
2090 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
2091 nvlist_exists(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
)) {
2092 (void) printf(dgettext(TEXT_DOMAIN
, "This "
2093 "pool uses the following feature(s) not "
2094 "supported by this system:\n"));
2095 zpool_print_unsup_feat(nv
);
2096 if (nvlist_exists(nvinfo
,
2097 ZPOOL_CONFIG_CAN_RDONLY
)) {
2098 (void) printf(dgettext(TEXT_DOMAIN
,
2099 "All unsupported features are only "
2100 "required for writing to the pool."
2101 "\nThe pool can be imported using "
2102 "'-o readonly=on'.\n"));
2106 * Unsupported version.
2108 (void) zfs_error(hdl
, EZFS_BADVERSION
, desc
);
2112 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
2113 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0) {
2114 const char *hostname
= "<unknown>";
2115 uint64_t hostid
= 0;
2116 mmp_state_t mmp_state
;
2118 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
2119 ZPOOL_CONFIG_MMP_STATE
);
2121 if (nvlist_exists(nvinfo
,
2122 ZPOOL_CONFIG_MMP_HOSTNAME
))
2123 hostname
= fnvlist_lookup_string(nvinfo
,
2124 ZPOOL_CONFIG_MMP_HOSTNAME
);
2126 if (nvlist_exists(nvinfo
,
2127 ZPOOL_CONFIG_MMP_HOSTID
))
2128 hostid
= fnvlist_lookup_uint64(nvinfo
,
2129 ZPOOL_CONFIG_MMP_HOSTID
);
2131 if (mmp_state
== MMP_STATE_ACTIVE
) {
2132 (void) snprintf(aux
, sizeof (aux
),
2133 dgettext(TEXT_DOMAIN
, "pool is imp"
2134 "orted on host '%s' (hostid=%lx).\n"
2135 "Export the pool on the other "
2136 "system, then run 'zpool import'."),
2137 hostname
, (unsigned long) hostid
);
2138 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
2139 (void) snprintf(aux
, sizeof (aux
),
2140 dgettext(TEXT_DOMAIN
, "pool has "
2141 "the multihost property on and "
2142 "the\nsystem's hostid is not set. "
2143 "Set a unique system hostid with "
2144 "the zgenhostid(8) command.\n"));
2147 (void) zfs_error_aux(hdl
, "%s", aux
);
2149 (void) zfs_error(hdl
, EZFS_ACTIVE_POOL
, desc
);
2153 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, desc
);
2157 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2158 "one or more devices is read only"));
2159 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
2163 if (nv
&& nvlist_lookup_nvlist(nv
,
2164 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
2165 nvlist_lookup_nvlist(nvinfo
,
2166 ZPOOL_CONFIG_MISSING_DEVICES
, &missing
) == 0) {
2167 (void) printf(dgettext(TEXT_DOMAIN
,
2168 "The devices below are missing or "
2169 "corrupted, use '-m' to import the pool "
2171 print_vdev_tree(hdl
, NULL
, missing
, 2);
2172 (void) printf("\n");
2174 (void) zpool_standard_error(hdl
, error
, desc
);
2178 (void) zpool_standard_error(hdl
, error
, desc
);
2182 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2183 "one or more devices are already in use\n"));
2184 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
2187 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2188 "new name of at least one dataset is longer than "
2189 "the maximum allowable length"));
2190 (void) zfs_error(hdl
, EZFS_NAMETOOLONG
, desc
);
2193 (void) zpool_standard_error(hdl
, error
, desc
);
2194 zpool_explain_recover(hdl
,
2195 newname
? origname
: thename
, -error
, nv
);
2202 zpool_handle_t
*zhp
;
2205 * This should never fail, but play it safe anyway.
2207 if (zpool_open_silent(hdl
, thename
, &zhp
) != 0)
2209 else if (zhp
!= NULL
)
2211 if (policy
.zlp_rewind
&
2212 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
2213 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
2214 ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) != 0), nv
);
2223 * Translate vdev names to guids. If a vdev_path is determined to be
2224 * unsuitable then a vd_errlist is allocated and the vdev path and errno
2228 zpool_translate_vdev_guids(zpool_handle_t
*zhp
, nvlist_t
*vds
,
2229 nvlist_t
*vdev_guids
, nvlist_t
*guids_to_paths
, nvlist_t
**vd_errlist
)
2231 nvlist_t
*errlist
= NULL
;
2234 for (nvpair_t
*elem
= nvlist_next_nvpair(vds
, NULL
); elem
!= NULL
;
2235 elem
= nvlist_next_nvpair(vds
, elem
)) {
2236 boolean_t spare
, cache
;
2238 char *vd_path
= nvpair_name(elem
);
2239 nvlist_t
*tgt
= zpool_find_vdev(zhp
, vd_path
, &spare
, &cache
,
2242 if ((tgt
== NULL
) || cache
|| spare
) {
2243 if (errlist
== NULL
) {
2244 errlist
= fnvlist_alloc();
2248 uint64_t err
= (tgt
== NULL
) ? EZFS_NODEVICE
:
2249 (spare
? EZFS_ISSPARE
: EZFS_ISL2CACHE
);
2250 fnvlist_add_int64(errlist
, vd_path
, err
);
2254 uint64_t guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
2255 fnvlist_add_uint64(vdev_guids
, vd_path
, guid
);
2257 char msg
[MAXNAMELEN
];
2258 (void) snprintf(msg
, sizeof (msg
), "%llu", (u_longlong_t
)guid
);
2259 fnvlist_add_string(guids_to_paths
, msg
, vd_path
);
2263 verify(errlist
!= NULL
);
2264 if (vd_errlist
!= NULL
)
2265 *vd_errlist
= errlist
;
2267 fnvlist_free(errlist
);
2274 xlate_init_err(int err
)
2278 return (EZFS_NODEVICE
);
2281 return (EZFS_BADDEV
);
2283 return (EZFS_INITIALIZING
);
2285 return (EZFS_NO_INITIALIZE
);
2291 * Begin, suspend, or cancel the initialization (initializing of all free
2292 * blocks) for the given vdevs in the given pool.
2295 zpool_initialize_impl(zpool_handle_t
*zhp
, pool_initialize_func_t cmd_type
,
2296 nvlist_t
*vds
, boolean_t wait
)
2300 nvlist_t
*vdev_guids
= fnvlist_alloc();
2301 nvlist_t
*guids_to_paths
= fnvlist_alloc();
2302 nvlist_t
*vd_errlist
= NULL
;
2306 err
= zpool_translate_vdev_guids(zhp
, vds
, vdev_guids
,
2307 guids_to_paths
, &vd_errlist
);
2310 verify(vd_errlist
!= NULL
);
2314 err
= lzc_initialize(zhp
->zpool_name
, cmd_type
,
2315 vdev_guids
, &errlist
);
2318 if (errlist
!= NULL
) {
2319 vd_errlist
= fnvlist_lookup_nvlist(errlist
,
2320 ZPOOL_INITIALIZE_VDEVS
);
2323 (void) zpool_standard_error(zhp
->zpool_hdl
, err
,
2324 dgettext(TEXT_DOMAIN
, "operation failed"));
2329 for (elem
= nvlist_next_nvpair(vdev_guids
, NULL
); elem
!= NULL
;
2330 elem
= nvlist_next_nvpair(vdev_guids
, elem
)) {
2332 uint64_t guid
= fnvpair_value_uint64(elem
);
2334 err
= lzc_wait_tag(zhp
->zpool_name
,
2335 ZPOOL_WAIT_INITIALIZE
, guid
, NULL
);
2337 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
,
2338 err
, dgettext(TEXT_DOMAIN
, "error "
2339 "waiting for '%s' to initialize"),
2349 for (elem
= nvlist_next_nvpair(vd_errlist
, NULL
); elem
!= NULL
;
2350 elem
= nvlist_next_nvpair(vd_errlist
, elem
)) {
2351 int64_t vd_error
= xlate_init_err(fnvpair_value_int64(elem
));
2354 if (nvlist_lookup_string(guids_to_paths
, nvpair_name(elem
),
2356 path
= nvpair_name(elem
);
2358 (void) zfs_error_fmt(zhp
->zpool_hdl
, vd_error
,
2359 "cannot initialize '%s'", path
);
2363 fnvlist_free(vdev_guids
);
2364 fnvlist_free(guids_to_paths
);
2366 if (vd_errlist
!= NULL
)
2367 fnvlist_free(vd_errlist
);
2369 return (err
== 0 ? 0 : -1);
2373 zpool_initialize(zpool_handle_t
*zhp
, pool_initialize_func_t cmd_type
,
2376 return (zpool_initialize_impl(zhp
, cmd_type
, vds
, B_FALSE
));
2380 zpool_initialize_wait(zpool_handle_t
*zhp
, pool_initialize_func_t cmd_type
,
2383 return (zpool_initialize_impl(zhp
, cmd_type
, vds
, B_TRUE
));
2387 xlate_trim_err(int err
)
2391 return (EZFS_NODEVICE
);
2394 return (EZFS_BADDEV
);
2396 return (EZFS_TRIMMING
);
2398 return (EZFS_NO_TRIM
);
2400 return (EZFS_TRIM_NOTSUP
);
2406 zpool_trim_wait(zpool_handle_t
*zhp
, nvlist_t
*vdev_guids
)
2411 for (elem
= nvlist_next_nvpair(vdev_guids
, NULL
); elem
!= NULL
;
2412 elem
= nvlist_next_nvpair(vdev_guids
, elem
)) {
2414 uint64_t guid
= fnvpair_value_uint64(elem
);
2416 err
= lzc_wait_tag(zhp
->zpool_name
,
2417 ZPOOL_WAIT_TRIM
, guid
, NULL
);
2419 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
,
2420 err
, dgettext(TEXT_DOMAIN
, "error "
2421 "waiting to trim '%s'"), nvpair_name(elem
));
2430 * Check errlist and report any errors, omitting ones which should be
2431 * suppressed. Returns B_TRUE if any errors were reported.
2434 check_trim_errs(zpool_handle_t
*zhp
, trimflags_t
*trim_flags
,
2435 nvlist_t
*guids_to_paths
, nvlist_t
*vds
, nvlist_t
*errlist
)
2438 boolean_t reported_errs
= B_FALSE
;
2440 int num_suppressed_errs
= 0;
2442 for (elem
= nvlist_next_nvpair(vds
, NULL
);
2443 elem
!= NULL
; elem
= nvlist_next_nvpair(vds
, elem
)) {
2447 for (elem
= nvlist_next_nvpair(errlist
, NULL
);
2448 elem
!= NULL
; elem
= nvlist_next_nvpair(errlist
, elem
)) {
2449 int64_t vd_error
= xlate_trim_err(fnvpair_value_int64(elem
));
2453 * If only the pool was specified, and it was not a secure
2454 * trim then suppress warnings for individual vdevs which
2455 * do not support trimming.
2457 if (vd_error
== EZFS_TRIM_NOTSUP
&&
2458 trim_flags
->fullpool
&&
2459 !trim_flags
->secure
) {
2460 num_suppressed_errs
++;
2464 reported_errs
= B_TRUE
;
2465 if (nvlist_lookup_string(guids_to_paths
, nvpair_name(elem
),
2467 path
= nvpair_name(elem
);
2469 (void) zfs_error_fmt(zhp
->zpool_hdl
, vd_error
,
2470 "cannot trim '%s'", path
);
2473 if (num_suppressed_errs
== num_vds
) {
2474 (void) zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
2475 "no devices in pool support trim operations"));
2476 (void) (zfs_error(zhp
->zpool_hdl
, EZFS_TRIM_NOTSUP
,
2477 dgettext(TEXT_DOMAIN
, "cannot trim")));
2478 reported_errs
= B_TRUE
;
2481 return (reported_errs
);
2485 * Begin, suspend, or cancel the TRIM (discarding of all free blocks) for
2486 * the given vdevs in the given pool.
2489 zpool_trim(zpool_handle_t
*zhp
, pool_trim_func_t cmd_type
, nvlist_t
*vds
,
2490 trimflags_t
*trim_flags
)
2495 nvlist_t
*vdev_guids
= fnvlist_alloc();
2496 nvlist_t
*guids_to_paths
= fnvlist_alloc();
2497 nvlist_t
*errlist
= NULL
;
2499 err
= zpool_translate_vdev_guids(zhp
, vds
, vdev_guids
,
2500 guids_to_paths
, &errlist
);
2502 check_trim_errs(zhp
, trim_flags
, guids_to_paths
, vds
, errlist
);
2507 err
= lzc_trim(zhp
->zpool_name
, cmd_type
, trim_flags
->rate
,
2508 trim_flags
->secure
, vdev_guids
, &errlist
);
2510 nvlist_t
*vd_errlist
;
2511 if (errlist
!= NULL
&& nvlist_lookup_nvlist(errlist
,
2512 ZPOOL_TRIM_VDEVS
, &vd_errlist
) == 0) {
2513 if (check_trim_errs(zhp
, trim_flags
, guids_to_paths
,
2519 char errbuf
[ERRBUFLEN
];
2521 (void) snprintf(errbuf
, sizeof (errbuf
),
2522 dgettext(TEXT_DOMAIN
, "operation failed"));
2523 zpool_standard_error(zhp
->zpool_hdl
, err
, errbuf
);
2530 if (trim_flags
->wait
)
2531 retval
= zpool_trim_wait(zhp
, vdev_guids
);
2534 if (errlist
!= NULL
)
2535 fnvlist_free(errlist
);
2536 fnvlist_free(vdev_guids
);
2537 fnvlist_free(guids_to_paths
);
2545 zpool_scan(zpool_handle_t
*zhp
, pool_scan_func_t func
, pool_scrub_cmd_t cmd
)
2547 zfs_cmd_t zc
= {"\0"};
2548 char errbuf
[ERRBUFLEN
];
2550 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2552 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2553 zc
.zc_cookie
= func
;
2556 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_SCAN
, &zc
) == 0)
2561 /* ECANCELED on a scrub means we resumed a paused scrub */
2562 if (err
== ECANCELED
&& func
== POOL_SCAN_SCRUB
&&
2563 cmd
== POOL_SCRUB_NORMAL
)
2566 if (err
== ENOENT
&& func
!= POOL_SCAN_NONE
&& cmd
== POOL_SCRUB_NORMAL
)
2569 if (func
== POOL_SCAN_SCRUB
) {
2570 if (cmd
== POOL_SCRUB_PAUSE
) {
2571 (void) snprintf(errbuf
, sizeof (errbuf
),
2572 dgettext(TEXT_DOMAIN
, "cannot pause scrubbing %s"),
2575 assert(cmd
== POOL_SCRUB_NORMAL
);
2576 (void) snprintf(errbuf
, sizeof (errbuf
),
2577 dgettext(TEXT_DOMAIN
, "cannot scrub %s"),
2580 } else if (func
== POOL_SCAN_RESILVER
) {
2581 assert(cmd
== POOL_SCRUB_NORMAL
);
2582 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2583 "cannot restart resilver on %s"), zc
.zc_name
);
2584 } else if (func
== POOL_SCAN_NONE
) {
2585 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2586 "cannot cancel scrubbing %s"), zc
.zc_name
);
2588 assert(!"unexpected result");
2593 pool_scan_stat_t
*ps
= NULL
;
2596 nvroot
= fnvlist_lookup_nvlist(zhp
->zpool_config
,
2597 ZPOOL_CONFIG_VDEV_TREE
);
2598 (void) nvlist_lookup_uint64_array(nvroot
,
2599 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &psc
);
2600 if (ps
&& ps
->pss_func
== POOL_SCAN_SCRUB
&&
2601 ps
->pss_state
== DSS_SCANNING
) {
2602 if (cmd
== POOL_SCRUB_PAUSE
)
2603 return (zfs_error(hdl
, EZFS_SCRUB_PAUSED
,
2606 return (zfs_error(hdl
, EZFS_SCRUBBING
, errbuf
));
2608 return (zfs_error(hdl
, EZFS_RESILVERING
, errbuf
));
2610 } else if (err
== ENOENT
) {
2611 return (zfs_error(hdl
, EZFS_NO_SCRUB
, errbuf
));
2612 } else if (err
== ENOTSUP
&& func
== POOL_SCAN_RESILVER
) {
2613 return (zfs_error(hdl
, EZFS_NO_RESILVER_DEFER
, errbuf
));
2615 return (zpool_standard_error(hdl
, err
, errbuf
));
2620 * Find a vdev that matches the search criteria specified. We use the
2621 * the nvpair name to determine how we should look for the device.
2622 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2623 * spare; but FALSE if its an INUSE spare.
2626 vdev_to_nvlist_iter(nvlist_t
*nv
, nvlist_t
*search
, boolean_t
*avail_spare
,
2627 boolean_t
*l2cache
, boolean_t
*log
)
2634 nvpair_t
*pair
= nvlist_next_nvpair(search
, NULL
);
2636 /* Nothing to look for */
2637 if (search
== NULL
|| pair
== NULL
)
2640 /* Obtain the key we will use to search */
2641 srchkey
= nvpair_name(pair
);
2643 switch (nvpair_type(pair
)) {
2644 case DATA_TYPE_UINT64
:
2645 if (strcmp(srchkey
, ZPOOL_CONFIG_GUID
) == 0) {
2646 uint64_t srchval
= fnvpair_value_uint64(pair
);
2647 uint64_t theguid
= fnvlist_lookup_uint64(nv
,
2649 if (theguid
== srchval
)
2654 case DATA_TYPE_STRING
: {
2655 char *srchval
, *val
;
2657 srchval
= fnvpair_value_string(pair
);
2658 if (nvlist_lookup_string(nv
, srchkey
, &val
) != 0)
2662 * Search for the requested value. Special cases:
2664 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2665 * "-part1", or "p1". The suffix is hidden from the user,
2666 * but included in the string, so this matches around it.
2667 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2668 * is used to check all possible expanded paths.
2669 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2671 * Otherwise, all other searches are simple string compares.
2673 if (strcmp(srchkey
, ZPOOL_CONFIG_PATH
) == 0) {
2674 uint64_t wholedisk
= 0;
2676 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
2678 if (zfs_strcmp_pathname(srchval
, val
, wholedisk
) == 0)
2681 } else if (strcmp(srchkey
, ZPOOL_CONFIG_TYPE
) == 0) {
2682 char *type
, *idx
, *end
, *p
;
2683 uint64_t id
, vdev_id
;
2686 * Determine our vdev type, keeping in mind
2687 * that the srchval is composed of a type and
2688 * vdev id pair (i.e. mirror-4).
2690 if ((type
= strdup(srchval
)) == NULL
)
2693 if ((p
= strrchr(type
, '-')) == NULL
) {
2701 * If the types don't match then keep looking.
2703 if (strncmp(val
, type
, strlen(val
)) != 0) {
2708 verify(zpool_vdev_is_interior(type
));
2710 id
= fnvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
);
2712 vdev_id
= strtoull(idx
, &end
, 10);
2715 * If we are looking for a raidz and a parity is
2716 * specified, make sure it matches.
2718 int rzlen
= strlen(VDEV_TYPE_RAIDZ
);
2719 assert(rzlen
== strlen(VDEV_TYPE_DRAID
));
2720 int typlen
= strlen(type
);
2721 if ((strncmp(type
, VDEV_TYPE_RAIDZ
, rzlen
) == 0 ||
2722 strncmp(type
, VDEV_TYPE_DRAID
, rzlen
) == 0) &&
2724 uint64_t vdev_parity
;
2725 int parity
= *(type
+ rzlen
) - '0';
2727 if (parity
<= 0 || parity
> 3 ||
2728 (typlen
- rzlen
) != 1) {
2730 * Nonsense parity specified, can
2736 vdev_parity
= fnvlist_lookup_uint64(nv
,
2737 ZPOOL_CONFIG_NPARITY
);
2738 if ((int)vdev_parity
!= parity
) {
2749 * Now verify that we have the correct vdev id.
2758 if (strcmp(srchval
, val
) == 0)
2767 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2768 &child
, &children
) != 0)
2771 for (c
= 0; c
< children
; c
++) {
2772 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2773 avail_spare
, l2cache
, NULL
)) != NULL
) {
2775 * The 'is_log' value is only set for the toplevel
2776 * vdev, not the leaf vdevs. So we always lookup the
2777 * log device from the root of the vdev tree (where
2778 * 'log' is non-NULL).
2781 nvlist_lookup_uint64(child
[c
],
2782 ZPOOL_CONFIG_IS_LOG
, &is_log
) == 0 &&
2790 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2791 &child
, &children
) == 0) {
2792 for (c
= 0; c
< children
; c
++) {
2793 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2794 avail_spare
, l2cache
, NULL
)) != NULL
) {
2795 *avail_spare
= B_TRUE
;
2801 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2802 &child
, &children
) == 0) {
2803 for (c
= 0; c
< children
; c
++) {
2804 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2805 avail_spare
, l2cache
, NULL
)) != NULL
) {
2816 * Given a physical path or guid, find the associated vdev.
2819 zpool_find_vdev_by_physpath(zpool_handle_t
*zhp
, const char *ppath
,
2820 boolean_t
*avail_spare
, boolean_t
*l2cache
, boolean_t
*log
)
2822 nvlist_t
*search
, *nvroot
, *ret
;
2826 search
= fnvlist_alloc();
2828 guid
= strtoull(ppath
, &end
, 0);
2829 if (guid
!= 0 && *end
== '\0') {
2830 fnvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
);
2832 fnvlist_add_string(search
, ZPOOL_CONFIG_PHYS_PATH
, ppath
);
2835 nvroot
= fnvlist_lookup_nvlist(zhp
->zpool_config
,
2836 ZPOOL_CONFIG_VDEV_TREE
);
2838 *avail_spare
= B_FALSE
;
2842 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2843 fnvlist_free(search
);
2849 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2852 zpool_vdev_is_interior(const char *name
)
2854 if (strncmp(name
, VDEV_TYPE_RAIDZ
, strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2855 strncmp(name
, VDEV_TYPE_SPARE
, strlen(VDEV_TYPE_SPARE
)) == 0 ||
2857 VDEV_TYPE_REPLACING
, strlen(VDEV_TYPE_REPLACING
)) == 0 ||
2858 strncmp(name
, VDEV_TYPE_MIRROR
, strlen(VDEV_TYPE_MIRROR
)) == 0)
2861 if (strncmp(name
, VDEV_TYPE_DRAID
, strlen(VDEV_TYPE_DRAID
)) == 0 &&
2862 !zpool_is_draid_spare(name
))
2869 zpool_find_vdev(zpool_handle_t
*zhp
, const char *path
, boolean_t
*avail_spare
,
2870 boolean_t
*l2cache
, boolean_t
*log
)
2873 nvlist_t
*nvroot
, *search
, *ret
;
2876 search
= fnvlist_alloc();
2878 guid
= strtoull(path
, &end
, 0);
2879 if (guid
!= 0 && *end
== '\0') {
2880 fnvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
);
2881 } else if (zpool_vdev_is_interior(path
)) {
2882 fnvlist_add_string(search
, ZPOOL_CONFIG_TYPE
, path
);
2884 fnvlist_add_string(search
, ZPOOL_CONFIG_PATH
, path
);
2887 nvroot
= fnvlist_lookup_nvlist(zhp
->zpool_config
,
2888 ZPOOL_CONFIG_VDEV_TREE
);
2890 *avail_spare
= B_FALSE
;
2894 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2895 fnvlist_free(search
);
2901 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
2903 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
2904 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
2908 zpool_vdev_path_to_guid_impl(zpool_handle_t
*zhp
, const char *path
,
2909 boolean_t
*is_spare
, boolean_t
*is_l2cache
, boolean_t
*is_log
)
2911 boolean_t spare
= B_FALSE
, l2cache
= B_FALSE
, log
= B_FALSE
;
2914 if ((tgt
= zpool_find_vdev(zhp
, path
, &spare
, &l2cache
,
2918 if (is_spare
!= NULL
)
2920 if (is_l2cache
!= NULL
)
2921 *is_l2cache
= l2cache
;
2925 return (fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
));
2928 /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
2930 zpool_vdev_path_to_guid(zpool_handle_t
*zhp
, const char *path
)
2932 return (zpool_vdev_path_to_guid_impl(zhp
, path
, NULL
, NULL
, NULL
));
2936 * Bring the specified vdev online. The 'flags' parameter is a set of the
2937 * ZFS_ONLINE_* flags.
2940 zpool_vdev_online(zpool_handle_t
*zhp
, const char *path
, int flags
,
2941 vdev_state_t
*newstate
)
2943 zfs_cmd_t zc
= {"\0"};
2944 char errbuf
[ERRBUFLEN
];
2946 boolean_t avail_spare
, l2cache
, islog
;
2947 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2949 if (flags
& ZFS_ONLINE_EXPAND
) {
2950 (void) snprintf(errbuf
, sizeof (errbuf
),
2951 dgettext(TEXT_DOMAIN
, "cannot expand %s"), path
);
2953 (void) snprintf(errbuf
, sizeof (errbuf
),
2954 dgettext(TEXT_DOMAIN
, "cannot online %s"), path
);
2957 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2958 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2960 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
2962 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
2964 if (!(flags
& ZFS_ONLINE_SPARE
) && avail_spare
)
2965 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
2969 if ((flags
& ZFS_ONLINE_EXPAND
||
2970 zpool_get_prop_int(zhp
, ZPOOL_PROP_AUTOEXPAND
, NULL
)) &&
2971 nvlist_lookup_string(tgt
, ZPOOL_CONFIG_PATH
, &pathname
) == 0) {
2972 uint64_t wholedisk
= 0;
2974 (void) nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_WHOLE_DISK
,
2978 * XXX - L2ARC 1.0 devices can't support expansion.
2981 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2982 "cannot expand cache devices"));
2983 return (zfs_error(hdl
, EZFS_VDEVNOTSUP
, errbuf
));
2987 const char *fullpath
= path
;
2988 char buf
[MAXPATHLEN
];
2991 if (path
[0] != '/') {
2992 error
= zfs_resolve_shortname(path
, buf
,
2995 return (zfs_error(hdl
, EZFS_NODEVICE
,
3001 error
= zpool_relabel_disk(hdl
, fullpath
, errbuf
);
3008 zc
.zc_cookie
= VDEV_STATE_ONLINE
;
3011 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) != 0) {
3012 if (errno
== EINVAL
) {
3013 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "was split "
3014 "from this pool into a new one. Use '%s' "
3015 "instead"), "zpool detach");
3016 return (zfs_error(hdl
, EZFS_POSTSPLIT_ONLINE
, errbuf
));
3018 return (zpool_standard_error(hdl
, errno
, errbuf
));
3021 *newstate
= zc
.zc_cookie
;
3026 * Take the specified vdev offline
3029 zpool_vdev_offline(zpool_handle_t
*zhp
, const char *path
, boolean_t istmp
)
3031 zfs_cmd_t zc
= {"\0"};
3032 char errbuf
[ERRBUFLEN
];
3034 boolean_t avail_spare
, l2cache
;
3035 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3037 (void) snprintf(errbuf
, sizeof (errbuf
),
3038 dgettext(TEXT_DOMAIN
, "cannot offline %s"), path
);
3040 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3041 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3043 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3045 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3048 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
3050 zc
.zc_cookie
= VDEV_STATE_OFFLINE
;
3051 zc
.zc_obj
= istmp
? ZFS_OFFLINE_TEMPORARY
: 0;
3053 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3060 * There are no other replicas of this device.
3062 return (zfs_error(hdl
, EZFS_NOREPLICAS
, errbuf
));
3066 * The log device has unplayed logs
3068 return (zfs_error(hdl
, EZFS_UNPLAYED_LOGS
, errbuf
));
3071 return (zpool_standard_error(hdl
, errno
, errbuf
));
3076 * Remove the specified vdev asynchronously from the configuration, so
3077 * that it may come ONLINE if reinserted. This is called from zed on
3078 * Udev remove event.
3079 * Note: We also have a similar function zpool_vdev_remove() that
3080 * removes the vdev from the pool.
3083 zpool_vdev_remove_wanted(zpool_handle_t
*zhp
, const char *path
)
3085 zfs_cmd_t zc
= {"\0"};
3086 char errbuf
[ERRBUFLEN
];
3088 boolean_t avail_spare
, l2cache
;
3089 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3091 (void) snprintf(errbuf
, sizeof (errbuf
),
3092 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3094 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3095 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3097 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3099 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3101 zc
.zc_cookie
= VDEV_STATE_REMOVED
;
3103 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3106 return (zpool_standard_error(hdl
, errno
, errbuf
));
3110 * Mark the given vdev faulted.
3113 zpool_vdev_fault(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
3115 zfs_cmd_t zc
= {"\0"};
3116 char errbuf
[ERRBUFLEN
];
3117 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3119 (void) snprintf(errbuf
, sizeof (errbuf
),
3120 dgettext(TEXT_DOMAIN
, "cannot fault %llu"), (u_longlong_t
)guid
);
3122 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3124 zc
.zc_cookie
= VDEV_STATE_FAULTED
;
3127 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3134 * There are no other replicas of this device.
3136 return (zfs_error(hdl
, EZFS_NOREPLICAS
, errbuf
));
3139 return (zpool_standard_error(hdl
, errno
, errbuf
));
3145 * Mark the given vdev degraded.
3148 zpool_vdev_degrade(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
3150 zfs_cmd_t zc
= {"\0"};
3151 char errbuf
[ERRBUFLEN
];
3152 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3154 (void) snprintf(errbuf
, sizeof (errbuf
),
3155 dgettext(TEXT_DOMAIN
, "cannot degrade %llu"), (u_longlong_t
)guid
);
3157 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3159 zc
.zc_cookie
= VDEV_STATE_DEGRADED
;
3162 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3165 return (zpool_standard_error(hdl
, errno
, errbuf
));
3169 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
3173 is_replacing_spare(nvlist_t
*search
, nvlist_t
*tgt
, int which
)
3178 if (nvlist_lookup_nvlist_array(search
, ZPOOL_CONFIG_CHILDREN
, &child
,
3180 char *type
= fnvlist_lookup_string(search
, ZPOOL_CONFIG_TYPE
);
3181 if ((strcmp(type
, VDEV_TYPE_SPARE
) == 0 ||
3182 strcmp(type
, VDEV_TYPE_DRAID_SPARE
) == 0) &&
3183 children
== 2 && child
[which
] == tgt
)
3186 for (c
= 0; c
< children
; c
++)
3187 if (is_replacing_spare(child
[c
], tgt
, which
))
3195 * Attach new_disk (fully described by nvroot) to old_disk.
3196 * If 'replacing' is specified, the new disk will replace the old one.
3199 zpool_vdev_attach(zpool_handle_t
*zhp
, const char *old_disk
,
3200 const char *new_disk
, nvlist_t
*nvroot
, int replacing
, boolean_t rebuild
)
3202 zfs_cmd_t zc
= {"\0"};
3203 char errbuf
[ERRBUFLEN
];
3206 boolean_t avail_spare
, l2cache
, islog
;
3211 nvlist_t
*config_root
;
3212 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3215 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3216 "cannot replace %s with %s"), old_disk
, new_disk
);
3218 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3219 "cannot attach %s to %s"), new_disk
, old_disk
);
3221 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3222 if ((tgt
= zpool_find_vdev(zhp
, old_disk
, &avail_spare
, &l2cache
,
3224 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3227 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
3230 return (zfs_error(hdl
, EZFS_ISL2CACHE
, errbuf
));
3232 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3233 zc
.zc_cookie
= replacing
;
3234 zc
.zc_simple
= rebuild
;
3237 zfeature_lookup_guid("org.openzfs:device_rebuild", NULL
) != 0) {
3238 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3239 "the loaded zfs module doesn't support device rebuilds"));
3240 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, errbuf
));
3243 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
3244 &child
, &children
) != 0 || children
!= 1) {
3245 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3246 "new device must be a single disk"));
3247 return (zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
));
3250 config_root
= fnvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
3251 ZPOOL_CONFIG_VDEV_TREE
);
3253 if ((newname
= zpool_vdev_name(NULL
, NULL
, child
[0], 0)) == NULL
)
3257 * If the target is a hot spare that has been swapped in, we can only
3258 * replace it with another hot spare.
3261 nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_IS_SPARE
, &val
) == 0 &&
3262 (zpool_find_vdev(zhp
, newname
, &avail_spare
, &l2cache
,
3263 NULL
) == NULL
|| !avail_spare
) &&
3264 is_replacing_spare(config_root
, tgt
, 1)) {
3265 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3266 "can only be replaced by another hot spare"));
3268 return (zfs_error(hdl
, EZFS_BADTARGET
, errbuf
));
3273 zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
);
3275 ret
= zfs_ioctl(hdl
, ZFS_IOC_VDEV_ATTACH
, &zc
);
3277 zcmd_free_nvlists(&zc
);
3285 * Can't attach to or replace this type of vdev.
3288 uint64_t version
= zpool_get_prop_int(zhp
,
3289 ZPOOL_PROP_VERSION
, NULL
);
3292 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3293 "cannot replace a log with a spare"));
3294 } else if (rebuild
) {
3295 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3296 "only mirror and dRAID vdevs support "
3297 "sequential reconstruction"));
3298 } else if (zpool_is_draid_spare(new_disk
)) {
3299 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3300 "dRAID spares can only replace child "
3301 "devices in their parent's dRAID vdev"));
3302 } else if (version
>= SPA_VERSION_MULTI_REPLACE
) {
3303 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3304 "already in replacing/spare config; wait "
3305 "for completion or use 'zpool detach'"));
3307 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3308 "cannot replace a replacing device"));
3311 char status
[64] = {0};
3312 zpool_prop_get_feature(zhp
,
3313 "feature@device_rebuild", status
, 63);
3315 strncmp(status
, ZFS_FEATURE_DISABLED
, 64) == 0) {
3316 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3317 "device_rebuild feature must be enabled "
3318 "in order to use sequential "
3321 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3322 "can only attach to mirrors and top-level "
3326 (void) zfs_error(hdl
, EZFS_BADTARGET
, errbuf
);
3331 * The new device must be a single disk.
3333 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3334 "new device must be a single disk"));
3335 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
);
3339 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "%s is busy, "
3340 "or device removal is in progress"),
3342 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
3347 * The new device is too small.
3349 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3350 "device is too small"));
3351 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
3356 * The new device has a different optimal sector size.
3358 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3359 "new device has a different optimal sector size; use the "
3360 "option '-o ashift=N' to override the optimal size"));
3361 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
3366 * The resulting top-level vdev spec won't fit in the label.
3368 (void) zfs_error(hdl
, EZFS_DEVOVERFLOW
, errbuf
);
3372 (void) zpool_standard_error(hdl
, errno
, errbuf
);
3379 * Detach the specified device.
3382 zpool_vdev_detach(zpool_handle_t
*zhp
, const char *path
)
3384 zfs_cmd_t zc
= {"\0"};
3385 char errbuf
[ERRBUFLEN
];
3387 boolean_t avail_spare
, l2cache
;
3388 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3390 (void) snprintf(errbuf
, sizeof (errbuf
),
3391 dgettext(TEXT_DOMAIN
, "cannot detach %s"), path
);
3393 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3394 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3396 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3399 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
3402 return (zfs_error(hdl
, EZFS_ISL2CACHE
, errbuf
));
3404 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3406 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_DETACH
, &zc
) == 0)
3413 * Can't detach from this type of vdev.
3415 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "only "
3416 "applicable to mirror and replacing vdevs"));
3417 (void) zfs_error(hdl
, EZFS_BADTARGET
, errbuf
);
3422 * There are no other replicas of this device.
3424 (void) zfs_error(hdl
, EZFS_NOREPLICAS
, errbuf
);
3428 (void) zpool_standard_error(hdl
, errno
, errbuf
);
3435 * Find a mirror vdev in the source nvlist.
3437 * The mchild array contains a list of disks in one of the top-level mirrors
3438 * of the source pool. The schild array contains a list of disks that the
3439 * user specified on the command line. We loop over the mchild array to
3440 * see if any entry in the schild array matches.
3442 * If a disk in the mchild array is found in the schild array, we return
3443 * the index of that entry. Otherwise we return -1.
3446 find_vdev_entry(zpool_handle_t
*zhp
, nvlist_t
**mchild
, uint_t mchildren
,
3447 nvlist_t
**schild
, uint_t schildren
)
3451 for (mc
= 0; mc
< mchildren
; mc
++) {
3453 char *mpath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
3456 for (sc
= 0; sc
< schildren
; sc
++) {
3457 char *spath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
3459 boolean_t result
= (strcmp(mpath
, spath
) == 0);
3475 * Split a mirror pool. If newroot points to null, then a new nvlist
3476 * is generated and it is the responsibility of the caller to free it.
3479 zpool_vdev_split(zpool_handle_t
*zhp
, char *newname
, nvlist_t
**newroot
,
3480 nvlist_t
*props
, splitflags_t flags
)
3482 zfs_cmd_t zc
= {"\0"};
3483 char errbuf
[ERRBUFLEN
], *bias
;
3484 nvlist_t
*tree
, *config
, **child
, **newchild
, *newconfig
= NULL
;
3485 nvlist_t
**varray
= NULL
, *zc_props
= NULL
;
3486 uint_t c
, children
, newchildren
, lastlog
= 0, vcount
, found
= 0;
3487 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3488 uint64_t vers
, readonly
= B_FALSE
;
3489 boolean_t freelist
= B_FALSE
, memory_err
= B_TRUE
;
3492 (void) snprintf(errbuf
, sizeof (errbuf
),
3493 dgettext(TEXT_DOMAIN
, "Unable to split %s"), zhp
->zpool_name
);
3495 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
3496 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3498 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
3499 (void) fprintf(stderr
, gettext("Internal error: unable to "
3500 "retrieve pool configuration\n"));
3504 tree
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
3505 vers
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
3508 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
3509 if ((zc_props
= zpool_valid_proplist(hdl
, zhp
->zpool_name
,
3510 props
, vers
, flags
, errbuf
)) == NULL
)
3512 (void) nvlist_lookup_uint64(zc_props
,
3513 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
3515 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3516 "property %s can only be set at import time"),
3517 zpool_prop_to_name(ZPOOL_PROP_READONLY
));
3522 if (nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
3524 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3525 "Source pool is missing vdev tree"));
3526 nvlist_free(zc_props
);
3530 varray
= zfs_alloc(hdl
, children
* sizeof (nvlist_t
*));
3533 if (*newroot
== NULL
||
3534 nvlist_lookup_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
3535 &newchild
, &newchildren
) != 0)
3538 for (c
= 0; c
< children
; c
++) {
3539 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
3540 boolean_t is_special
= B_FALSE
, is_dedup
= B_FALSE
;
3542 nvlist_t
**mchild
, *vdev
;
3547 * Unlike cache & spares, slogs are stored in the
3548 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3550 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
3552 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
3554 if (is_log
|| is_hole
) {
3556 * Create a hole vdev and put it in the config.
3558 if (nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) != 0)
3560 if (nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
,
3561 VDEV_TYPE_HOLE
) != 0)
3563 if (nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_HOLE
,
3568 varray
[vcount
++] = vdev
;
3572 type
= fnvlist_lookup_string(child
[c
], ZPOOL_CONFIG_TYPE
);
3574 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0) {
3576 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
3579 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
3580 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3581 "Source pool must be composed only of mirrors\n"));
3582 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
);
3586 if (nvlist_lookup_string(child
[c
],
3587 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
) == 0) {
3588 if (strcmp(bias
, VDEV_ALLOC_BIAS_SPECIAL
) == 0)
3589 is_special
= B_TRUE
;
3590 else if (strcmp(bias
, VDEV_ALLOC_BIAS_DEDUP
) == 0)
3593 verify(nvlist_lookup_nvlist_array(child
[c
],
3594 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
3596 /* find or add an entry for this top-level vdev */
3597 if (newchildren
> 0 &&
3598 (entry
= find_vdev_entry(zhp
, mchild
, mchildren
,
3599 newchild
, newchildren
)) >= 0) {
3600 /* We found a disk that the user specified. */
3601 vdev
= mchild
[entry
];
3604 /* User didn't specify a disk for this vdev. */
3605 vdev
= mchild
[mchildren
- 1];
3608 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
3611 if (flags
.dryrun
!= 0) {
3612 if (is_dedup
== B_TRUE
) {
3613 if (nvlist_add_string(varray
[vcount
- 1],
3614 ZPOOL_CONFIG_ALLOCATION_BIAS
,
3615 VDEV_ALLOC_BIAS_DEDUP
) != 0)
3617 } else if (is_special
== B_TRUE
) {
3618 if (nvlist_add_string(varray
[vcount
- 1],
3619 ZPOOL_CONFIG_ALLOCATION_BIAS
,
3620 VDEV_ALLOC_BIAS_SPECIAL
) != 0)
3626 /* did we find every disk the user specified? */
3627 if (found
!= newchildren
) {
3628 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Device list must "
3629 "include at most one disk from each mirror"));
3630 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
);
3634 /* Prepare the nvlist for populating. */
3635 if (*newroot
== NULL
) {
3636 if (nvlist_alloc(newroot
, NV_UNIQUE_NAME
, 0) != 0)
3639 if (nvlist_add_string(*newroot
, ZPOOL_CONFIG_TYPE
,
3640 VDEV_TYPE_ROOT
) != 0)
3643 verify(nvlist_remove_all(*newroot
, ZPOOL_CONFIG_CHILDREN
) == 0);
3646 /* Add all the children we found */
3647 if (nvlist_add_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
3648 (const nvlist_t
**)varray
, lastlog
== 0 ? vcount
: lastlog
) != 0)
3652 * If we're just doing a dry run, exit now with success.
3655 memory_err
= B_FALSE
;
3660 /* now build up the config list & call the ioctl */
3661 if (nvlist_alloc(&newconfig
, NV_UNIQUE_NAME
, 0) != 0)
3664 if (nvlist_add_nvlist(newconfig
,
3665 ZPOOL_CONFIG_VDEV_TREE
, *newroot
) != 0 ||
3666 nvlist_add_string(newconfig
,
3667 ZPOOL_CONFIG_POOL_NAME
, newname
) != 0 ||
3668 nvlist_add_uint64(newconfig
, ZPOOL_CONFIG_VERSION
, vers
) != 0)
3672 * The new pool is automatically part of the namespace unless we
3673 * explicitly export it.
3676 zc
.zc_cookie
= ZPOOL_EXPORT_AFTER_SPLIT
;
3677 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3678 (void) strlcpy(zc
.zc_string
, newname
, sizeof (zc
.zc_string
));
3679 zcmd_write_conf_nvlist(hdl
, &zc
, newconfig
);
3680 if (zc_props
!= NULL
)
3681 zcmd_write_src_nvlist(hdl
, &zc
, zc_props
);
3683 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SPLIT
, &zc
) != 0) {
3684 retval
= zpool_standard_error(hdl
, errno
, errbuf
);
3689 memory_err
= B_FALSE
;
3692 if (varray
!= NULL
) {
3695 for (v
= 0; v
< vcount
; v
++)
3696 nvlist_free(varray
[v
]);
3699 zcmd_free_nvlists(&zc
);
3700 nvlist_free(zc_props
);
3701 nvlist_free(newconfig
);
3703 nvlist_free(*newroot
);
3711 return (no_memory(hdl
));
3717 * Remove the given device.
3720 zpool_vdev_remove(zpool_handle_t
*zhp
, const char *path
)
3722 zfs_cmd_t zc
= {"\0"};
3723 char errbuf
[ERRBUFLEN
];
3725 boolean_t avail_spare
, l2cache
, islog
;
3726 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3729 (void) snprintf(errbuf
, sizeof (errbuf
),
3730 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3732 if (zpool_is_draid_spare(path
)) {
3733 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3734 "dRAID spares cannot be removed"));
3735 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3738 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3739 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3741 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3743 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
3744 if (islog
&& version
< SPA_VERSION_HOLES
) {
3745 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3746 "pool must be upgraded to support log removal"));
3747 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3750 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3752 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3758 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3759 "invalid config; all top-level vdevs must "
3760 "have the same sector size and not be raidz."));
3761 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
);
3766 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3767 "Mount encrypted datasets to replay logs."));
3769 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3770 "Pool busy; removal may already be in progress"));
3772 (void) zfs_error(hdl
, EZFS_BUSY
, errbuf
);
3777 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3778 "Mount encrypted datasets to replay logs."));
3779 (void) zfs_error(hdl
, EZFS_BUSY
, errbuf
);
3781 (void) zpool_standard_error(hdl
, errno
, errbuf
);
3786 (void) zpool_standard_error(hdl
, errno
, errbuf
);
3792 zpool_vdev_remove_cancel(zpool_handle_t
*zhp
)
3794 zfs_cmd_t zc
= {{0}};
3795 char errbuf
[ERRBUFLEN
];
3796 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3798 (void) snprintf(errbuf
, sizeof (errbuf
),
3799 dgettext(TEXT_DOMAIN
, "cannot cancel removal"));
3801 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3804 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3807 return (zpool_standard_error(hdl
, errno
, errbuf
));
3811 zpool_vdev_indirect_size(zpool_handle_t
*zhp
, const char *path
,
3814 char errbuf
[ERRBUFLEN
];
3816 boolean_t avail_spare
, l2cache
, islog
;
3817 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3819 (void) snprintf(errbuf
, sizeof (errbuf
),
3820 dgettext(TEXT_DOMAIN
, "cannot determine indirect size of %s"),
3823 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3825 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3827 if (avail_spare
|| l2cache
|| islog
) {
3832 if (nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_INDIRECT_SIZE
, sizep
) != 0) {
3833 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3834 "indirect size not available"));
3835 return (zfs_error(hdl
, EINVAL
, errbuf
));
3841 * Clear the errors for the pool, or the particular device if specified.
3844 zpool_clear(zpool_handle_t
*zhp
, const char *path
, nvlist_t
*rewindnvl
)
3846 zfs_cmd_t zc
= {"\0"};
3847 char errbuf
[ERRBUFLEN
];
3849 zpool_load_policy_t policy
;
3850 boolean_t avail_spare
, l2cache
;
3851 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3852 nvlist_t
*nvi
= NULL
;
3856 (void) snprintf(errbuf
, sizeof (errbuf
),
3857 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3860 (void) snprintf(errbuf
, sizeof (errbuf
),
3861 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3864 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3866 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
,
3867 &l2cache
, NULL
)) == NULL
)
3868 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3871 * Don't allow error clearing for hot spares. Do allow
3872 * error clearing for l2cache devices.
3875 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
3877 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3880 zpool_get_load_policy(rewindnvl
, &policy
);
3881 zc
.zc_cookie
= policy
.zlp_rewind
;
3883 zcmd_alloc_dst_nvlist(hdl
, &zc
, zhp
->zpool_config_size
* 2);
3884 zcmd_write_src_nvlist(hdl
, &zc
, rewindnvl
);
3886 while ((error
= zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
)) != 0 &&
3888 zcmd_expand_dst_nvlist(hdl
, &zc
);
3890 if (!error
|| ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) &&
3891 errno
!= EPERM
&& errno
!= EACCES
)) {
3892 if (policy
.zlp_rewind
&
3893 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
3894 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nvi
);
3895 zpool_rewind_exclaim(hdl
, zc
.zc_name
,
3896 ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) != 0),
3900 zcmd_free_nvlists(&zc
);
3904 zcmd_free_nvlists(&zc
);
3905 return (zpool_standard_error(hdl
, errno
, errbuf
));
3909 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3912 zpool_vdev_clear(zpool_handle_t
*zhp
, uint64_t guid
)
3914 zfs_cmd_t zc
= {"\0"};
3915 char errbuf
[ERRBUFLEN
];
3916 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3918 (void) snprintf(errbuf
, sizeof (errbuf
),
3919 dgettext(TEXT_DOMAIN
, "cannot clear errors for %llx"),
3920 (u_longlong_t
)guid
);
3922 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3924 zc
.zc_cookie
= ZPOOL_NO_REWIND
;
3926 if (zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
) == 0)
3929 return (zpool_standard_error(hdl
, errno
, errbuf
));
3933 * Change the GUID for a pool.
3936 zpool_reguid(zpool_handle_t
*zhp
)
3938 char errbuf
[ERRBUFLEN
];
3939 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3940 zfs_cmd_t zc
= {"\0"};
3942 (void) snprintf(errbuf
, sizeof (errbuf
),
3943 dgettext(TEXT_DOMAIN
, "cannot reguid '%s'"), zhp
->zpool_name
);
3945 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3946 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REGUID
, &zc
) == 0)
3949 return (zpool_standard_error(hdl
, errno
, errbuf
));
3956 zpool_reopen_one(zpool_handle_t
*zhp
, void *data
)
3958 libzfs_handle_t
*hdl
= zpool_get_handle(zhp
);
3959 const char *pool_name
= zpool_get_name(zhp
);
3960 boolean_t
*scrub_restart
= data
;
3963 error
= lzc_reopen(pool_name
, *scrub_restart
);
3965 return (zpool_standard_error_fmt(hdl
, error
,
3966 dgettext(TEXT_DOMAIN
, "cannot reopen '%s'"), pool_name
));
3972 /* call into libzfs_core to execute the sync IOCTL per pool */
3974 zpool_sync_one(zpool_handle_t
*zhp
, void *data
)
3977 libzfs_handle_t
*hdl
= zpool_get_handle(zhp
);
3978 const char *pool_name
= zpool_get_name(zhp
);
3979 boolean_t
*force
= data
;
3980 nvlist_t
*innvl
= fnvlist_alloc();
3982 fnvlist_add_boolean_value(innvl
, "force", *force
);
3983 if ((ret
= lzc_sync(pool_name
, innvl
, NULL
)) != 0) {
3985 return (zpool_standard_error_fmt(hdl
, ret
,
3986 dgettext(TEXT_DOMAIN
, "sync '%s' failed"), pool_name
));
3993 #define PATH_BUF_LEN 64
3996 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3997 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3998 * We also check if this is a whole disk, in which case we strip off the
3999 * trailing 's0' slice name.
4001 * This routine is also responsible for identifying when disks have been
4002 * reconfigured in a new location. The kernel will have opened the device by
4003 * devid, but the path will still refer to the old location. To catch this, we
4004 * first do a path -> devid translation (which is fast for the common case). If
4005 * the devid matches, we're done. If not, we do a reverse devid -> path
4006 * translation and issue the appropriate ioctl() to update the path of the vdev.
4007 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
4011 zpool_vdev_name(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, nvlist_t
*nv
,
4017 char buf
[PATH_BUF_LEN
];
4018 char tmpbuf
[PATH_BUF_LEN
* 2];
4021 * vdev_name will be "root"/"root-0" for the root vdev, but it is the
4022 * zpool name that will be displayed to the user.
4024 type
= fnvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
);
4025 if (zhp
!= NULL
&& strcmp(type
, "root") == 0)
4026 return (zfs_strdup(hdl
, zpool_get_name(zhp
)));
4028 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_PATH"))
4029 name_flags
|= VDEV_NAME_PATH
;
4030 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_GUID"))
4031 name_flags
|= VDEV_NAME_GUID
;
4032 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_FOLLOW_LINKS"))
4033 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
4035 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
, &value
) == 0 ||
4036 name_flags
& VDEV_NAME_GUID
) {
4037 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
);
4038 (void) snprintf(buf
, sizeof (buf
), "%llu", (u_longlong_t
)value
);
4040 } else if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &tpath
) == 0) {
4043 if (name_flags
& VDEV_NAME_FOLLOW_LINKS
) {
4044 char *rp
= realpath(path
, NULL
);
4046 strlcpy(buf
, rp
, sizeof (buf
));
4053 * For a block device only use the name.
4055 if ((strcmp(type
, VDEV_TYPE_DISK
) == 0) &&
4056 !(name_flags
& VDEV_NAME_PATH
)) {
4057 path
= zfs_strip_path(path
);
4061 * Remove the partition from the path if this is a whole disk.
4063 if (strcmp(type
, VDEV_TYPE_DRAID_SPARE
) != 0 &&
4064 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
, &value
)
4065 == 0 && value
&& !(name_flags
& VDEV_NAME_PATH
)) {
4066 return (zfs_strip_partition(path
));
4072 * If it's a raidz device, we need to stick in the parity level.
4074 if (strcmp(path
, VDEV_TYPE_RAIDZ
) == 0) {
4075 value
= fnvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
);
4076 (void) snprintf(buf
, sizeof (buf
), "%s%llu", path
,
4077 (u_longlong_t
)value
);
4082 * If it's a dRAID device, we add parity, groups, and spares.
4084 if (strcmp(path
, VDEV_TYPE_DRAID
) == 0) {
4085 uint64_t ndata
, nparity
, nspares
;
4089 verify(nvlist_lookup_nvlist_array(nv
,
4090 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) == 0);
4091 nparity
= fnvlist_lookup_uint64(nv
,
4092 ZPOOL_CONFIG_NPARITY
);
4093 ndata
= fnvlist_lookup_uint64(nv
,
4094 ZPOOL_CONFIG_DRAID_NDATA
);
4095 nspares
= fnvlist_lookup_uint64(nv
,
4096 ZPOOL_CONFIG_DRAID_NSPARES
);
4098 path
= zpool_draid_name(buf
, sizeof (buf
), ndata
,
4099 nparity
, nspares
, children
);
4103 * We identify each top-level vdev by using a <type-id>
4104 * naming convention.
4106 if (name_flags
& VDEV_NAME_TYPE_ID
) {
4107 uint64_t id
= fnvlist_lookup_uint64(nv
,
4109 (void) snprintf(tmpbuf
, sizeof (tmpbuf
), "%s-%llu",
4110 path
, (u_longlong_t
)id
);
4115 return (zfs_strdup(hdl
, path
));
4119 zbookmark_mem_compare(const void *a
, const void *b
)
4121 return (memcmp(a
, b
, sizeof (zbookmark_phys_t
)));
4125 * Retrieve the persistent error log, uniquify the members, and return to the
4129 zpool_get_errlog(zpool_handle_t
*zhp
, nvlist_t
**nverrlistp
)
4131 zfs_cmd_t zc
= {"\0"};
4132 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4133 zbookmark_phys_t
*buf
;
4134 uint64_t buflen
= 10000; /* approx. 1MB of RAM */
4136 if (fnvlist_lookup_uint64(zhp
->zpool_config
,
4137 ZPOOL_CONFIG_ERRCOUNT
) == 0)
4141 * Retrieve the raw error list from the kernel. If it doesn't fit,
4142 * allocate a larger buffer and retry.
4144 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
4146 buf
= zfs_alloc(zhp
->zpool_hdl
,
4147 buflen
* sizeof (zbookmark_phys_t
));
4148 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4149 zc
.zc_nvlist_dst_size
= buflen
;
4150 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_ERROR_LOG
,
4153 if (errno
== ENOMEM
) {
4156 return (zpool_standard_error_fmt(hdl
, errno
,
4157 dgettext(TEXT_DOMAIN
, "errors: List of "
4158 "errors unavailable")));
4166 * Sort the resulting bookmarks. This is a little confusing due to the
4167 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
4168 * to first, and 'zc_nvlist_dst_size' indicates the number of bookmarks
4169 * _not_ copied as part of the process. So we point the start of our
4170 * array appropriate and decrement the total number of elements.
4172 zbookmark_phys_t
*zb
= buf
+ zc
.zc_nvlist_dst_size
;
4173 uint64_t zblen
= buflen
- zc
.zc_nvlist_dst_size
;
4175 qsort(zb
, zblen
, sizeof (zbookmark_phys_t
), zbookmark_mem_compare
);
4177 verify(nvlist_alloc(nverrlistp
, 0, KM_SLEEP
) == 0);
4180 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
4182 for (uint64_t i
= 0; i
< zblen
; i
++) {
4185 /* ignoring zb_blkid and zb_level for now */
4186 if (i
> 0 && zb
[i
-1].zb_objset
== zb
[i
].zb_objset
&&
4187 zb
[i
-1].zb_object
== zb
[i
].zb_object
)
4190 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) != 0)
4192 if (nvlist_add_uint64(nv
, ZPOOL_ERR_DATASET
,
4193 zb
[i
].zb_objset
) != 0) {
4197 if (nvlist_add_uint64(nv
, ZPOOL_ERR_OBJECT
,
4198 zb
[i
].zb_object
) != 0) {
4202 if (nvlist_add_nvlist(*nverrlistp
, "ejk", nv
) != 0) {
4214 return (no_memory(zhp
->zpool_hdl
));
4218 * Upgrade a ZFS pool to the latest on-disk version.
4221 zpool_upgrade(zpool_handle_t
*zhp
, uint64_t new_version
)
4223 zfs_cmd_t zc
= {"\0"};
4224 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4226 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
4227 zc
.zc_cookie
= new_version
;
4229 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_UPGRADE
, &zc
) != 0)
4230 return (zpool_standard_error_fmt(hdl
, errno
,
4231 dgettext(TEXT_DOMAIN
, "cannot upgrade '%s'"),
4237 zfs_save_arguments(int argc
, char **argv
, char *string
, int len
)
4241 (void) strlcpy(string
, zfs_basename(argv
[0]), len
);
4242 for (i
= 1; i
< argc
; i
++) {
4243 (void) strlcat(string
, " ", len
);
4244 (void) strlcat(string
, argv
[i
], len
);
4249 zpool_log_history(libzfs_handle_t
*hdl
, const char *message
)
4251 zfs_cmd_t zc
= {"\0"};
4254 args
= fnvlist_alloc();
4255 fnvlist_add_string(args
, "message", message
);
4256 zcmd_write_src_nvlist(hdl
, &zc
, args
);
4257 int err
= zfs_ioctl(hdl
, ZFS_IOC_LOG_HISTORY
, &zc
);
4259 zcmd_free_nvlists(&zc
);
4264 * Perform ioctl to get some command history of a pool.
4266 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
4267 * logical offset of the history buffer to start reading from.
4269 * Upon return, 'off' is the next logical offset to read from and
4270 * 'len' is the actual amount of bytes read into 'buf'.
4273 get_history(zpool_handle_t
*zhp
, char *buf
, uint64_t *off
, uint64_t *len
)
4275 zfs_cmd_t zc
= {"\0"};
4276 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4278 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4280 zc
.zc_history
= (uint64_t)(uintptr_t)buf
;
4281 zc
.zc_history_len
= *len
;
4282 zc
.zc_history_offset
= *off
;
4284 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_GET_HISTORY
, &zc
) != 0) {
4287 return (zfs_error_fmt(hdl
, EZFS_PERM
,
4288 dgettext(TEXT_DOMAIN
,
4289 "cannot show history for pool '%s'"),
4292 return (zfs_error_fmt(hdl
, EZFS_NOHISTORY
,
4293 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
4294 "'%s'"), zhp
->zpool_name
));
4296 return (zfs_error_fmt(hdl
, EZFS_BADVERSION
,
4297 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
4298 "'%s', pool must be upgraded"), zhp
->zpool_name
));
4300 return (zpool_standard_error_fmt(hdl
, errno
,
4301 dgettext(TEXT_DOMAIN
,
4302 "cannot get history for '%s'"), zhp
->zpool_name
));
4306 *len
= zc
.zc_history_len
;
4307 *off
= zc
.zc_history_offset
;
4313 * Retrieve the command history of a pool.
4316 zpool_get_history(zpool_handle_t
*zhp
, nvlist_t
**nvhisp
, uint64_t *off
,
4319 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4321 int buflen
= 128 * 1024;
4322 nvlist_t
**records
= NULL
;
4323 uint_t numrecords
= 0;
4325 uint64_t start
= *off
;
4327 buf
= zfs_alloc(hdl
, buflen
);
4329 /* process about 1MiB a time */
4330 while (*off
- start
< 1024 * 1024) {
4331 uint64_t bytes_read
= buflen
;
4334 if ((err
= get_history(zhp
, buf
, off
, &bytes_read
)) != 0)
4337 /* if nothing else was read in, we're at EOF, just return */
4343 if ((err
= zpool_history_unpack(buf
, bytes_read
,
4344 &leftover
, &records
, &numrecords
)) != 0) {
4345 zpool_standard_error_fmt(hdl
, err
,
4346 dgettext(TEXT_DOMAIN
,
4347 "cannot get history for '%s'"), zhp
->zpool_name
);
4351 if (leftover
== bytes_read
) {
4353 * no progress made, because buffer is not big enough
4354 * to hold this record; resize and retry.
4358 buf
= zfs_alloc(hdl
, buflen
);
4365 *nvhisp
= fnvlist_alloc();
4366 fnvlist_add_nvlist_array(*nvhisp
, ZPOOL_HIST_RECORD
,
4367 (const nvlist_t
**)records
, numrecords
);
4369 for (i
= 0; i
< numrecords
; i
++)
4370 nvlist_free(records
[i
]);
4377 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
4378 * If there is a new event available 'nvp' will contain a newly allocated
4379 * nvlist and 'dropped' will be set to the number of missed events since
4380 * the last call to this function. When 'nvp' is set to NULL it indicates
4381 * no new events are available. In either case the function returns 0 and
4382 * it is up to the caller to free 'nvp'. In the case of a fatal error the
4383 * function will return a non-zero value. When the function is called in
4384 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
4385 * it will not return until a new event is available.
4388 zpool_events_next(libzfs_handle_t
*hdl
, nvlist_t
**nvp
,
4389 int *dropped
, unsigned flags
, int zevent_fd
)
4391 zfs_cmd_t zc
= {"\0"};
4396 zc
.zc_cleanup_fd
= zevent_fd
;
4398 if (flags
& ZEVENT_NONBLOCK
)
4399 zc
.zc_guid
= ZEVENT_NONBLOCK
;
4401 zcmd_alloc_dst_nvlist(hdl
, &zc
, ZEVENT_SIZE
);
4404 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_NEXT
, &zc
) != 0) {
4407 error
= zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
4408 dgettext(TEXT_DOMAIN
, "zfs shutdown"));
4411 /* Blocking error case should not occur */
4412 if (!(flags
& ZEVENT_NONBLOCK
))
4413 error
= zpool_standard_error_fmt(hdl
, errno
,
4414 dgettext(TEXT_DOMAIN
, "cannot get event"));
4418 zcmd_expand_dst_nvlist(hdl
, &zc
);
4421 error
= zpool_standard_error_fmt(hdl
, errno
,
4422 dgettext(TEXT_DOMAIN
, "cannot get event"));
4427 error
= zcmd_read_dst_nvlist(hdl
, &zc
, nvp
);
4431 *dropped
= (int)zc
.zc_cookie
;
4433 zcmd_free_nvlists(&zc
);
4442 zpool_events_clear(libzfs_handle_t
*hdl
, int *count
)
4444 zfs_cmd_t zc
= {"\0"};
4446 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_CLEAR
, &zc
) != 0)
4447 return (zpool_standard_error(hdl
, errno
,
4448 dgettext(TEXT_DOMAIN
, "cannot clear events")));
4451 *count
= (int)zc
.zc_cookie
; /* # of events cleared */
4457 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4458 * the passed zevent_fd file handle. On success zero is returned,
4459 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4462 zpool_events_seek(libzfs_handle_t
*hdl
, uint64_t eid
, int zevent_fd
)
4464 zfs_cmd_t zc
= {"\0"};
4468 zc
.zc_cleanup_fd
= zevent_fd
;
4470 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_SEEK
, &zc
) != 0) {
4473 error
= zfs_error_fmt(hdl
, EZFS_NOENT
,
4474 dgettext(TEXT_DOMAIN
, "cannot get event"));
4478 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
4479 dgettext(TEXT_DOMAIN
, "cannot get event"));
4483 error
= zpool_standard_error_fmt(hdl
, errno
,
4484 dgettext(TEXT_DOMAIN
, "cannot get event"));
4493 zpool_obj_to_path_impl(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4494 char *pathname
, size_t len
, boolean_t always_unmounted
)
4496 zfs_cmd_t zc
= {"\0"};
4497 boolean_t mounted
= B_FALSE
;
4498 char *mntpnt
= NULL
;
4499 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
4502 /* special case for the MOS */
4503 (void) snprintf(pathname
, len
, "<metadata>:<0x%llx>",
4508 /* get the dataset's name */
4509 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4511 if (zfs_ioctl(zhp
->zpool_hdl
,
4512 ZFS_IOC_DSOBJ_TO_DSNAME
, &zc
) != 0) {
4513 /* just write out a path of two object numbers */
4514 (void) snprintf(pathname
, len
, "<0x%llx>:<0x%llx>",
4515 (longlong_t
)dsobj
, (longlong_t
)obj
);
4518 (void) strlcpy(dsname
, zc
.zc_value
, sizeof (dsname
));
4520 /* find out if the dataset is mounted */
4521 mounted
= !always_unmounted
&& is_mounted(zhp
->zpool_hdl
, dsname
,
4524 /* get the corrupted object's path */
4525 (void) strlcpy(zc
.zc_name
, dsname
, sizeof (zc
.zc_name
));
4527 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_OBJ_TO_PATH
,
4530 (void) snprintf(pathname
, len
, "%s%s", mntpnt
,
4533 (void) snprintf(pathname
, len
, "%s:%s",
4534 dsname
, zc
.zc_value
);
4537 (void) snprintf(pathname
, len
, "%s:<0x%llx>", dsname
,
4544 zpool_obj_to_path(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4545 char *pathname
, size_t len
)
4547 zpool_obj_to_path_impl(zhp
, dsobj
, obj
, pathname
, len
, B_FALSE
);
4551 zpool_obj_to_path_ds(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4552 char *pathname
, size_t len
)
4554 zpool_obj_to_path_impl(zhp
, dsobj
, obj
, pathname
, len
, B_TRUE
);
4557 * Wait while the specified activity is in progress in the pool.
4560 zpool_wait(zpool_handle_t
*zhp
, zpool_wait_activity_t activity
)
4564 int error
= zpool_wait_status(zhp
, activity
, &missing
, NULL
);
4567 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, ENOENT
,
4568 dgettext(TEXT_DOMAIN
, "error waiting in pool '%s'"),
4577 * Wait for the given activity and return the status of the wait (whether or not
4578 * any waiting was done) in the 'waited' parameter. Non-existent pools are
4579 * reported via the 'missing' parameter, rather than by printing an error
4580 * message. This is convenient when this function is called in a loop over a
4581 * long period of time (as it is, for example, by zpool's wait cmd). In that
4582 * scenario, a pool being exported or destroyed should be considered a normal
4583 * event, so we don't want to print an error when we find that the pool doesn't
4587 zpool_wait_status(zpool_handle_t
*zhp
, zpool_wait_activity_t activity
,
4588 boolean_t
*missing
, boolean_t
*waited
)
4590 int error
= lzc_wait(zhp
->zpool_name
, activity
, waited
);
4591 *missing
= (error
== ENOENT
);
4596 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
4597 dgettext(TEXT_DOMAIN
, "error waiting in pool '%s'"),
4605 zpool_set_bootenv(zpool_handle_t
*zhp
, const nvlist_t
*envmap
)
4607 int error
= lzc_set_bootenv(zhp
->zpool_name
, envmap
);
4609 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
4610 dgettext(TEXT_DOMAIN
,
4611 "error setting bootenv in pool '%s'"), zhp
->zpool_name
);
4618 zpool_get_bootenv(zpool_handle_t
*zhp
, nvlist_t
**nvlp
)
4624 error
= lzc_get_bootenv(zhp
->zpool_name
, &nvl
);
4626 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
4627 dgettext(TEXT_DOMAIN
,
4628 "error getting bootenv in pool '%s'"), zhp
->zpool_name
);
4637 * Attempt to read and parse feature file(s) (from "compatibility" property).
4638 * Files contain zpool feature names, comma or whitespace-separated.
4639 * Comments (# character to next newline) are discarded.
4642 * compatibility : string containing feature filenames
4643 * features : either NULL or pointer to array of boolean
4644 * report : either NULL or pointer to string buffer
4645 * rlen : length of "report" buffer
4647 * compatibility is NULL (unset), "", "off", "legacy", or list of
4648 * comma-separated filenames. filenames should either be absolute,
4650 * 1) ZPOOL_SYSCONF_COMPAT_D (eg: /etc/zfs/compatibility.d) or
4651 * 2) ZPOOL_DATA_COMPAT_D (eg: /usr/share/zfs/compatibility.d).
4652 * (Unset), "" or "off" => enable all features
4653 * "legacy" => disable all features
4655 * Any feature names read from files which match unames in spa_feature_table
4656 * will have the corresponding boolean set in the features array (if non-NULL).
4657 * If more than one feature set specified, only features present in *all* of
4660 * "report" if not NULL will be populated with a suitable status message.
4663 * ZPOOL_COMPATIBILITY_OK : files read and parsed ok
4664 * ZPOOL_COMPATIBILITY_BADFILE : file too big or not a text file
4665 * ZPOOL_COMPATIBILITY_BADTOKEN : SYSCONF file contains invalid feature name
4666 * ZPOOL_COMPATIBILITY_WARNTOKEN : DATA file contains invalid feature name
4667 * ZPOOL_COMPATIBILITY_NOFILES : no feature files found
4669 zpool_compat_status_t
4670 zpool_load_compat(const char *compat
, boolean_t
*features
, char *report
,
4673 int sdirfd
, ddirfd
, featfd
;
4677 char *file
, *line
, *word
;
4679 char l_compat
[ZFS_MAXPROPLEN
];
4681 boolean_t ret_nofiles
= B_TRUE
;
4682 boolean_t ret_badfile
= B_FALSE
;
4683 boolean_t ret_badtoken
= B_FALSE
;
4684 boolean_t ret_warntoken
= B_FALSE
;
4686 /* special cases (unset), "" and "off" => enable all features */
4687 if (compat
== NULL
|| compat
[0] == '\0' ||
4688 strcmp(compat
, ZPOOL_COMPAT_OFF
) == 0) {
4689 if (features
!= NULL
)
4690 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4691 features
[i
] = B_TRUE
;
4693 strlcpy(report
, gettext("all features enabled"), rlen
);
4694 return (ZPOOL_COMPATIBILITY_OK
);
4697 /* Final special case "legacy" => disable all features */
4698 if (strcmp(compat
, ZPOOL_COMPAT_LEGACY
) == 0) {
4699 if (features
!= NULL
)
4700 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4701 features
[i
] = B_FALSE
;
4703 strlcpy(report
, gettext("all features disabled"), rlen
);
4704 return (ZPOOL_COMPATIBILITY_OK
);
4708 * Start with all true; will be ANDed with results from each file
4710 if (features
!= NULL
)
4711 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4712 features
[i
] = B_TRUE
;
4714 char err_badfile
[ZFS_MAXPROPLEN
] = "";
4715 char err_badtoken
[ZFS_MAXPROPLEN
] = "";
4718 * We ignore errors from the directory open()
4719 * as they're only needed if the filename is relative
4720 * which will be checked during the openat().
4723 /* O_PATH safer than O_RDONLY if system allows it */
4725 #define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_PATH)
4727 #define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_RDONLY)
4730 sdirfd
= open(ZPOOL_SYSCONF_COMPAT_D
, ZC_DIR_FLAGS
);
4731 ddirfd
= open(ZPOOL_DATA_COMPAT_D
, ZC_DIR_FLAGS
);
4733 (void) strlcpy(l_compat
, compat
, ZFS_MAXPROPLEN
);
4735 for (file
= strtok_r(l_compat
, ",", &ps
);
4737 file
= strtok_r(NULL
, ",", &ps
)) {
4739 boolean_t l_features
[SPA_FEATURES
];
4741 enum { Z_SYSCONF
, Z_DATA
} source
;
4743 /* try sysconfdir first, then datadir */
4745 if ((featfd
= openat(sdirfd
, file
, O_RDONLY
| O_CLOEXEC
)) < 0) {
4746 featfd
= openat(ddirfd
, file
, O_RDONLY
| O_CLOEXEC
);
4750 /* File readable and correct size? */
4752 fstat(featfd
, &fs
) < 0 ||
4754 fs
.st_size
> ZPOOL_COMPAT_MAXSIZE
) {
4755 (void) close(featfd
);
4756 strlcat(err_badfile
, file
, ZFS_MAXPROPLEN
);
4757 strlcat(err_badfile
, " ", ZFS_MAXPROPLEN
);
4758 ret_badfile
= B_TRUE
;
4762 /* Prefault the file if system allows */
4763 #if defined(MAP_POPULATE)
4764 #define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_POPULATE)
4765 #elif defined(MAP_PREFAULT_READ)
4766 #define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_PREFAULT_READ)
4768 #define ZC_MMAP_FLAGS (MAP_PRIVATE)
4771 /* private mmap() so we can strtok safely */
4772 fc
= (char *)mmap(NULL
, fs
.st_size
, PROT_READ
| PROT_WRITE
,
4773 ZC_MMAP_FLAGS
, featfd
, 0);
4774 (void) close(featfd
);
4776 /* map ok, and last character == newline? */
4777 if (fc
== MAP_FAILED
|| fc
[fs
.st_size
- 1] != '\n') {
4778 (void) munmap((void *) fc
, fs
.st_size
);
4779 strlcat(err_badfile
, file
, ZFS_MAXPROPLEN
);
4780 strlcat(err_badfile
, " ", ZFS_MAXPROPLEN
);
4781 ret_badfile
= B_TRUE
;
4785 ret_nofiles
= B_FALSE
;
4787 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4788 l_features
[i
] = B_FALSE
;
4790 /* replace final newline with NULL to ensure string ends */
4791 fc
[fs
.st_size
- 1] = '\0';
4793 for (line
= strtok_r(fc
, "\n", &ls
);
4795 line
= strtok_r(NULL
, "\n", &ls
)) {
4796 /* discard comments */
4797 char *r
= strchr(line
, '#');
4801 for (word
= strtok_r(line
, ", \t", &ws
);
4803 word
= strtok_r(NULL
, ", \t", &ws
)) {
4804 /* Find matching feature name */
4806 for (f
= 0; f
< SPA_FEATURES
; f
++) {
4807 zfeature_info_t
*fi
=
4808 &spa_feature_table
[f
];
4809 if (strcmp(word
, fi
->fi_uname
) == 0) {
4810 l_features
[f
] = B_TRUE
;
4814 if (f
< SPA_FEATURES
)
4817 /* found an unrecognized word */
4818 /* lightly sanitize it */
4819 if (strlen(word
) > 32)
4821 for (char *c
= word
; *c
!= '\0'; c
++)
4825 strlcat(err_badtoken
, word
, ZFS_MAXPROPLEN
);
4826 strlcat(err_badtoken
, " ", ZFS_MAXPROPLEN
);
4827 if (source
== Z_SYSCONF
)
4828 ret_badtoken
= B_TRUE
;
4830 ret_warntoken
= B_TRUE
;
4833 (void) munmap((void *) fc
, fs
.st_size
);
4835 if (features
!= NULL
)
4836 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4837 features
[i
] &= l_features
[i
];
4839 (void) close(sdirfd
);
4840 (void) close(ddirfd
);
4842 /* Return the most serious error */
4845 snprintf(report
, rlen
, gettext("could not read/"
4846 "parse feature file(s): %s"), err_badfile
);
4847 return (ZPOOL_COMPATIBILITY_BADFILE
);
4852 gettext("no valid compatibility files specified"),
4854 return (ZPOOL_COMPATIBILITY_NOFILES
);
4858 snprintf(report
, rlen
, gettext("invalid feature "
4859 "name(s) in local compatibility files: %s"),
4861 return (ZPOOL_COMPATIBILITY_BADTOKEN
);
4863 if (ret_warntoken
) {
4865 snprintf(report
, rlen
, gettext("unrecognized feature "
4866 "name(s) in distribution compatibility files: %s"),
4868 return (ZPOOL_COMPATIBILITY_WARNTOKEN
);
4871 strlcpy(report
, gettext("compatibility set ok"), rlen
);
4872 return (ZPOOL_COMPATIBILITY_OK
);
4876 zpool_vdev_guid(zpool_handle_t
*zhp
, const char *vdevname
, uint64_t *vdev_guid
)
4879 boolean_t avail_spare
, l2cache
;
4881 verify(zhp
!= NULL
);
4882 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
4883 char errbuf
[ERRBUFLEN
];
4884 (void) snprintf(errbuf
, sizeof (errbuf
),
4885 dgettext(TEXT_DOMAIN
, "pool is in an unavailable state"));
4886 return (zfs_error(zhp
->zpool_hdl
, EZFS_POOLUNAVAIL
, errbuf
));
4889 if ((tgt
= zpool_find_vdev(zhp
, vdevname
, &avail_spare
, &l2cache
,
4891 char errbuf
[ERRBUFLEN
];
4892 (void) snprintf(errbuf
, sizeof (errbuf
),
4893 dgettext(TEXT_DOMAIN
, "can not find %s in %s"),
4894 vdevname
, zhp
->zpool_name
);
4895 return (zfs_error(zhp
->zpool_hdl
, EZFS_NODEVICE
, errbuf
));
4898 *vdev_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
4903 * Get a vdev property value for 'prop' and return the value in
4904 * a pre-allocated buffer.
4907 zpool_get_vdev_prop_value(nvlist_t
*nvprop
, vdev_prop_t prop
, char *prop_name
,
4908 char *buf
, size_t len
, zprop_source_t
*srctype
, boolean_t literal
)
4913 zprop_source_t src
= ZPROP_SRC_NONE
;
4915 if (prop
== VDEV_PROP_USERPROP
) {
4916 /* user property, prop_name must contain the property name */
4917 assert(prop_name
!= NULL
);
4918 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
4919 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
4920 strval
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
4922 /* user prop not found */
4925 (void) strlcpy(buf
, strval
, len
);
4931 if (prop_name
== NULL
)
4932 prop_name
= (char *)vdev_prop_to_name(prop
);
4934 switch (vdev_prop_get_type(prop
)) {
4935 case PROP_TYPE_STRING
:
4936 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
4937 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
4938 strval
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
4940 src
= ZPROP_SRC_DEFAULT
;
4941 if ((strval
= vdev_prop_default_string(prop
)) == NULL
)
4944 (void) strlcpy(buf
, strval
, len
);
4947 case PROP_TYPE_NUMBER
:
4948 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
4949 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
4950 intval
= fnvlist_lookup_uint64(nv
, ZPROP_VALUE
);
4952 src
= ZPROP_SRC_DEFAULT
;
4953 intval
= vdev_prop_default_numeric(prop
);
4957 case VDEV_PROP_ASIZE
:
4958 case VDEV_PROP_PSIZE
:
4959 case VDEV_PROP_SIZE
:
4960 case VDEV_PROP_BOOTSIZE
:
4961 case VDEV_PROP_ALLOCATED
:
4962 case VDEV_PROP_FREE
:
4963 case VDEV_PROP_READ_ERRORS
:
4964 case VDEV_PROP_WRITE_ERRORS
:
4965 case VDEV_PROP_CHECKSUM_ERRORS
:
4966 case VDEV_PROP_INITIALIZE_ERRORS
:
4967 case VDEV_PROP_OPS_NULL
:
4968 case VDEV_PROP_OPS_READ
:
4969 case VDEV_PROP_OPS_WRITE
:
4970 case VDEV_PROP_OPS_FREE
:
4971 case VDEV_PROP_OPS_CLAIM
:
4972 case VDEV_PROP_OPS_TRIM
:
4973 case VDEV_PROP_BYTES_NULL
:
4974 case VDEV_PROP_BYTES_READ
:
4975 case VDEV_PROP_BYTES_WRITE
:
4976 case VDEV_PROP_BYTES_FREE
:
4977 case VDEV_PROP_BYTES_CLAIM
:
4978 case VDEV_PROP_BYTES_TRIM
:
4980 (void) snprintf(buf
, len
, "%llu",
4981 (u_longlong_t
)intval
);
4983 (void) zfs_nicenum(intval
, buf
, len
);
4986 case VDEV_PROP_EXPANDSZ
:
4988 (void) strlcpy(buf
, "-", len
);
4989 } else if (literal
) {
4990 (void) snprintf(buf
, len
, "%llu",
4991 (u_longlong_t
)intval
);
4993 (void) zfs_nicenum(intval
, buf
, len
);
4996 case VDEV_PROP_CAPACITY
:
4998 (void) snprintf(buf
, len
, "%llu",
4999 (u_longlong_t
)intval
);
5001 (void) snprintf(buf
, len
, "%llu%%",
5002 (u_longlong_t
)intval
);
5005 case VDEV_PROP_CHECKSUM_N
:
5006 case VDEV_PROP_CHECKSUM_T
:
5007 case VDEV_PROP_IO_N
:
5008 case VDEV_PROP_IO_T
:
5009 if (intval
== UINT64_MAX
) {
5010 (void) strlcpy(buf
, "-", len
);
5012 (void) snprintf(buf
, len
, "%llu",
5013 (u_longlong_t
)intval
);
5016 case VDEV_PROP_FRAGMENTATION
:
5017 if (intval
== UINT64_MAX
) {
5018 (void) strlcpy(buf
, "-", len
);
5020 (void) snprintf(buf
, len
, "%llu%%",
5021 (u_longlong_t
)intval
);
5024 case VDEV_PROP_STATE
:
5026 (void) snprintf(buf
, len
, "%llu",
5027 (u_longlong_t
)intval
);
5029 (void) strlcpy(buf
, zpool_state_to_name(intval
,
5030 VDEV_AUX_NONE
), len
);
5034 (void) snprintf(buf
, len
, "%llu",
5035 (u_longlong_t
)intval
);
5039 case PROP_TYPE_INDEX
:
5040 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
5041 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
5042 intval
= fnvlist_lookup_uint64(nv
, ZPROP_VALUE
);
5044 src
= ZPROP_SRC_DEFAULT
;
5045 intval
= vdev_prop_default_numeric(prop
);
5047 if (vdev_prop_index_to_string(prop
, intval
,
5048 (const char **)&strval
) != 0)
5050 (void) strlcpy(buf
, strval
, len
);
5064 * Get a vdev property value for 'prop_name' and return the value in
5065 * a pre-allocated buffer.
5068 zpool_get_vdev_prop(zpool_handle_t
*zhp
, const char *vdevname
, vdev_prop_t prop
,
5069 char *prop_name
, char *buf
, size_t len
, zprop_source_t
*srctype
,
5072 nvlist_t
*reqnvl
, *reqprops
;
5073 nvlist_t
*retprops
= NULL
;
5074 uint64_t vdev_guid
= 0;
5077 if ((ret
= zpool_vdev_guid(zhp
, vdevname
, &vdev_guid
)) != 0)
5080 if (nvlist_alloc(&reqnvl
, NV_UNIQUE_NAME
, 0) != 0)
5081 return (no_memory(zhp
->zpool_hdl
));
5082 if (nvlist_alloc(&reqprops
, NV_UNIQUE_NAME
, 0) != 0)
5083 return (no_memory(zhp
->zpool_hdl
));
5085 fnvlist_add_uint64(reqnvl
, ZPOOL_VDEV_PROPS_GET_VDEV
, vdev_guid
);
5087 if (prop
!= VDEV_PROP_USERPROP
) {
5088 /* prop_name overrides prop value */
5089 if (prop_name
!= NULL
)
5090 prop
= vdev_name_to_prop(prop_name
);
5092 prop_name
= (char *)vdev_prop_to_name(prop
);
5093 assert(prop
< VDEV_NUM_PROPS
);
5096 assert(prop_name
!= NULL
);
5097 if (nvlist_add_uint64(reqprops
, prop_name
, prop
) != 0) {
5098 nvlist_free(reqnvl
);
5099 nvlist_free(reqprops
);
5100 return (no_memory(zhp
->zpool_hdl
));
5103 fnvlist_add_nvlist(reqnvl
, ZPOOL_VDEV_PROPS_GET_PROPS
, reqprops
);
5105 ret
= lzc_get_vdev_prop(zhp
->zpool_name
, reqnvl
, &retprops
);
5108 ret
= zpool_get_vdev_prop_value(retprops
, prop
, prop_name
, buf
,
5109 len
, srctype
, literal
);
5111 char errbuf
[ERRBUFLEN
];
5112 (void) snprintf(errbuf
, sizeof (errbuf
),
5113 dgettext(TEXT_DOMAIN
, "cannot get vdev property %s from"
5114 " %s in %s"), prop_name
, vdevname
, zhp
->zpool_name
);
5115 (void) zpool_standard_error(zhp
->zpool_hdl
, ret
, errbuf
);
5118 nvlist_free(reqnvl
);
5119 nvlist_free(reqprops
);
5120 nvlist_free(retprops
);
5126 * Get all vdev properties
5129 zpool_get_all_vdev_props(zpool_handle_t
*zhp
, const char *vdevname
,
5132 nvlist_t
*nvl
= NULL
;
5133 uint64_t vdev_guid
= 0;
5136 if ((ret
= zpool_vdev_guid(zhp
, vdevname
, &vdev_guid
)) != 0)
5139 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5140 return (no_memory(zhp
->zpool_hdl
));
5142 fnvlist_add_uint64(nvl
, ZPOOL_VDEV_PROPS_GET_VDEV
, vdev_guid
);
5144 ret
= lzc_get_vdev_prop(zhp
->zpool_name
, nvl
, outnvl
);
5149 char errbuf
[ERRBUFLEN
];
5150 (void) snprintf(errbuf
, sizeof (errbuf
),
5151 dgettext(TEXT_DOMAIN
, "cannot get vdev properties for"
5152 " %s in %s"), vdevname
, zhp
->zpool_name
);
5153 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
5163 zpool_set_vdev_prop(zpool_handle_t
*zhp
, const char *vdevname
,
5164 const char *propname
, const char *propval
)
5167 nvlist_t
*nvl
= NULL
;
5168 nvlist_t
*outnvl
= NULL
;
5170 nvlist_t
*realprops
;
5171 prop_flags_t flags
= { 0 };
5175 if ((ret
= zpool_vdev_guid(zhp
, vdevname
, &vdev_guid
)) != 0)
5178 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5179 return (no_memory(zhp
->zpool_hdl
));
5180 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
5181 return (no_memory(zhp
->zpool_hdl
));
5183 fnvlist_add_uint64(nvl
, ZPOOL_VDEV_PROPS_SET_VDEV
, vdev_guid
);
5185 if (nvlist_add_string(props
, propname
, propval
) != 0) {
5187 return (no_memory(zhp
->zpool_hdl
));
5190 char errbuf
[ERRBUFLEN
];
5191 (void) snprintf(errbuf
, sizeof (errbuf
),
5192 dgettext(TEXT_DOMAIN
, "cannot set property %s for %s on %s"),
5193 propname
, vdevname
, zhp
->zpool_name
);
5196 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
5197 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
5198 zhp
->zpool_name
, props
, version
, flags
, errbuf
)) == NULL
) {
5207 fnvlist_add_nvlist(nvl
, ZPOOL_VDEV_PROPS_SET_PROPS
, props
);
5209 ret
= lzc_set_vdev_prop(zhp
->zpool_name
, nvl
, &outnvl
);
5213 nvlist_free(outnvl
);
5216 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);