4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2011, 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 int create
:1; /* Validate property on creation */
64 int import
:1; /* Validate property on import */
65 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
= (char *)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_INVAL
;
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_INVAL
;
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_INVAL
&&
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
;
1374 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1375 "cannot create '%s'"), pool
);
1377 if (!zpool_name_valid(hdl
, B_FALSE
, pool
))
1378 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
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
, msg
)) == 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
, msg
)) == 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
, msg
);
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
, msg
);
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
, msg
));
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
, msg
));
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
, msg
));
1505 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1506 "one or more devices is out of space"));
1507 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
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
, msg
));
1517 return (zpool_standard_error(hdl
, errno
, msg
));
1521 return (zpool_standard_error(hdl
, errno
, msg
));
1526 zcmd_free_nvlists(&zc
);
1527 nvlist_free(zc_props
);
1528 nvlist_free(zc_fsprops
);
1529 nvlist_free(hidden_args
);
1530 if (wkeydata
!= NULL
)
1536 * Destroy the given pool. It is up to the caller to ensure that there are no
1537 * datasets left in the pool.
1540 zpool_destroy(zpool_handle_t
*zhp
, const char *log_str
)
1542 zfs_cmd_t zc
= {"\0"};
1543 zfs_handle_t
*zfp
= NULL
;
1544 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1547 if (zhp
->zpool_state
== POOL_STATE_ACTIVE
&&
1548 (zfp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
1551 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1552 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1554 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_DESTROY
, &zc
) != 0) {
1555 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1556 "cannot destroy '%s'"), zhp
->zpool_name
);
1558 if (errno
== EROFS
) {
1559 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1560 "one or more devices is read only"));
1561 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1563 (void) zpool_standard_error(hdl
, errno
, msg
);
1572 remove_mountpoint(zfp
);
1580 * Create a checkpoint in the given pool.
1583 zpool_checkpoint(zpool_handle_t
*zhp
)
1585 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1589 error
= lzc_pool_checkpoint(zhp
->zpool_name
);
1591 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1592 "cannot checkpoint '%s'"), zhp
->zpool_name
);
1593 (void) zpool_standard_error(hdl
, error
, msg
);
1601 * Discard the checkpoint from the given pool.
1604 zpool_discard_checkpoint(zpool_handle_t
*zhp
)
1606 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1610 error
= lzc_pool_checkpoint_discard(zhp
->zpool_name
);
1612 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1613 "cannot discard checkpoint in '%s'"), zhp
->zpool_name
);
1614 (void) zpool_standard_error(hdl
, error
, msg
);
1622 * Add the given vdevs to the pool. The caller must have already performed the
1623 * necessary verification to ensure that the vdev specification is well-formed.
1626 zpool_add(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
1628 zfs_cmd_t zc
= {"\0"};
1630 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1632 nvlist_t
**spares
, **l2cache
;
1633 uint_t nspares
, nl2cache
;
1635 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1636 "cannot add to '%s'"), zhp
->zpool_name
);
1638 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1639 SPA_VERSION_SPARES
&&
1640 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1641 &spares
, &nspares
) == 0) {
1642 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1643 "upgraded to add hot spares"));
1644 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1647 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1648 SPA_VERSION_L2CACHE
&&
1649 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1650 &l2cache
, &nl2cache
) == 0) {
1651 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1652 "upgraded to add cache devices"));
1653 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1656 zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
);
1657 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1659 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_ADD
, &zc
) != 0) {
1663 * This can happen if the user has specified the same
1664 * device multiple times. We can't reliably detect this
1665 * until we try to add it and see we already have a
1668 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1669 "one or more vdevs refer to the same device"));
1670 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1675 if (zpool_has_draid_vdev(nvroot
) &&
1676 zfeature_lookup_name("draid", NULL
) != 0) {
1677 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1678 "dRAID vdevs are unsupported by the "
1681 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1682 "invalid config; a pool with removing/"
1683 "removed vdevs does not support adding "
1684 "raidz or dRAID vdevs"));
1687 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1692 * This occurs when one of the devices is below
1693 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1694 * device was the problem device since there's no
1695 * reliable way to determine device size from userland.
1700 zfs_nicebytes(SPA_MINDEVSIZE
, buf
,
1703 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1704 "device is less than the minimum "
1707 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1711 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1712 "pool must be upgraded to add these vdevs"));
1713 (void) zfs_error(hdl
, EZFS_BADVERSION
, msg
);
1717 (void) zpool_standard_error(hdl
, errno
, msg
);
1725 zcmd_free_nvlists(&zc
);
1731 * Exports the pool from the system. The caller must ensure that there are no
1732 * mounted datasets in the pool.
1735 zpool_export_common(zpool_handle_t
*zhp
, boolean_t force
, boolean_t hardforce
,
1736 const char *log_str
)
1738 zfs_cmd_t zc
= {"\0"};
1740 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1741 zc
.zc_cookie
= force
;
1742 zc
.zc_guid
= hardforce
;
1743 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1745 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_EXPORT
, &zc
) != 0) {
1748 zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
1749 "use '-f' to override the following errors:\n"
1750 "'%s' has an active shared spare which could be"
1751 " used by other pools once '%s' is exported."),
1752 zhp
->zpool_name
, zhp
->zpool_name
);
1753 return (zfs_error_fmt(zhp
->zpool_hdl
, EZFS_ACTIVE_SPARE
,
1754 dgettext(TEXT_DOMAIN
, "cannot export '%s'"),
1757 return (zpool_standard_error_fmt(zhp
->zpool_hdl
, errno
,
1758 dgettext(TEXT_DOMAIN
, "cannot export '%s'"),
1767 zpool_export(zpool_handle_t
*zhp
, boolean_t force
, const char *log_str
)
1769 return (zpool_export_common(zhp
, force
, B_FALSE
, log_str
));
1773 zpool_export_force(zpool_handle_t
*zhp
, const char *log_str
)
1775 return (zpool_export_common(zhp
, B_TRUE
, B_TRUE
, log_str
));
1779 zpool_rewind_exclaim(libzfs_handle_t
*hdl
, const char *name
, boolean_t dryrun
,
1782 nvlist_t
*nv
= NULL
;
1788 if (!hdl
->libzfs_printerr
|| config
== NULL
)
1791 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1792 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0) {
1796 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1798 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1800 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1801 strftime(timestr
, 128, "%c", &t
) != 0) {
1803 (void) printf(dgettext(TEXT_DOMAIN
,
1804 "Would be able to return %s "
1805 "to its state as of %s.\n"),
1808 (void) printf(dgettext(TEXT_DOMAIN
,
1809 "Pool %s returned to its state as of %s.\n"),
1813 (void) printf(dgettext(TEXT_DOMAIN
,
1814 "%s approximately %lld "),
1815 dryrun
? "Would discard" : "Discarded",
1816 ((longlong_t
)loss
+ 30) / 60);
1817 (void) printf(dgettext(TEXT_DOMAIN
,
1818 "minutes of transactions.\n"));
1819 } else if (loss
> 0) {
1820 (void) printf(dgettext(TEXT_DOMAIN
,
1821 "%s approximately %lld "),
1822 dryrun
? "Would discard" : "Discarded",
1824 (void) printf(dgettext(TEXT_DOMAIN
,
1825 "seconds of transactions.\n"));
1831 zpool_explain_recover(libzfs_handle_t
*hdl
, const char *name
, int reason
,
1834 nvlist_t
*nv
= NULL
;
1836 uint64_t edata
= UINT64_MAX
;
1841 if (!hdl
->libzfs_printerr
)
1845 (void) printf(dgettext(TEXT_DOMAIN
, "action: "));
1847 (void) printf(dgettext(TEXT_DOMAIN
, "\t"));
1849 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1850 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1851 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0 ||
1852 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1855 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1856 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_DATA_ERRORS
,
1859 (void) printf(dgettext(TEXT_DOMAIN
,
1860 "Recovery is possible, but will result in some data loss.\n"));
1862 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1863 strftime(timestr
, 128, "%c", &t
) != 0) {
1864 (void) printf(dgettext(TEXT_DOMAIN
,
1865 "\tReturning the pool to its state as of %s\n"
1866 "\tshould correct the problem. "),
1869 (void) printf(dgettext(TEXT_DOMAIN
,
1870 "\tReverting the pool to an earlier state "
1871 "should correct the problem.\n\t"));
1875 (void) printf(dgettext(TEXT_DOMAIN
,
1876 "Approximately %lld minutes of data\n"
1877 "\tmust be discarded, irreversibly. "),
1878 ((longlong_t
)loss
+ 30) / 60);
1879 } else if (loss
> 0) {
1880 (void) printf(dgettext(TEXT_DOMAIN
,
1881 "Approximately %lld seconds of data\n"
1882 "\tmust be discarded, irreversibly. "),
1885 if (edata
!= 0 && edata
!= UINT64_MAX
) {
1887 (void) printf(dgettext(TEXT_DOMAIN
,
1888 "After rewind, at least\n"
1889 "\tone persistent user-data error will remain. "));
1891 (void) printf(dgettext(TEXT_DOMAIN
,
1892 "After rewind, several\n"
1893 "\tpersistent user-data errors will remain. "));
1896 (void) printf(dgettext(TEXT_DOMAIN
,
1897 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1898 reason
>= 0 ? "clear" : "import", name
);
1900 (void) printf(dgettext(TEXT_DOMAIN
,
1901 "A scrub of the pool\n"
1902 "\tis strongly recommended after recovery.\n"));
1906 (void) printf(dgettext(TEXT_DOMAIN
,
1907 "Destroy and re-create the pool from\n\ta backup source.\n"));
1911 * zpool_import() is a contracted interface. Should be kept the same
1914 * Applications should use zpool_import_props() to import a pool with
1915 * new properties value to be set.
1918 zpool_import(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1921 nvlist_t
*props
= NULL
;
1924 if (altroot
!= NULL
) {
1925 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
1926 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1927 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1931 if (nvlist_add_string(props
,
1932 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), altroot
) != 0 ||
1933 nvlist_add_string(props
,
1934 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
), "none") != 0) {
1936 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1937 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1942 ret
= zpool_import_props(hdl
, config
, newname
, props
,
1949 print_vdev_tree(libzfs_handle_t
*hdl
, const char *name
, nvlist_t
*nv
,
1955 uint64_t is_log
= 0;
1957 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
,
1961 (void) printf("\t%*s%s%s\n", indent
, "", name
,
1962 is_log
? " [log]" : "");
1964 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1965 &child
, &children
) != 0)
1968 for (c
= 0; c
< children
; c
++) {
1969 vname
= zpool_vdev_name(hdl
, NULL
, child
[c
], VDEV_NAME_TYPE_ID
);
1970 print_vdev_tree(hdl
, vname
, child
[c
], indent
+ 2);
1976 zpool_print_unsup_feat(nvlist_t
*config
)
1978 nvlist_t
*nvinfo
, *unsup_feat
;
1980 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
1981 unsup_feat
= fnvlist_lookup_nvlist(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
);
1983 for (nvpair_t
*nvp
= nvlist_next_nvpair(unsup_feat
, NULL
);
1984 nvp
!= NULL
; nvp
= nvlist_next_nvpair(unsup_feat
, nvp
)) {
1985 char *desc
= fnvpair_value_string(nvp
);
1986 if (strlen(desc
) > 0)
1987 (void) printf("\t%s (%s)\n", nvpair_name(nvp
), desc
);
1989 (void) printf("\t%s\n", nvpair_name(nvp
));
1994 * Import the given pool using the known configuration and a list of
1995 * properties to be set. The configuration should have come from
1996 * zpool_find_import(). The 'newname' parameters control whether the pool
1997 * is imported with a different name.
2000 zpool_import_props(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
2001 nvlist_t
*props
, int flags
)
2003 zfs_cmd_t zc
= {"\0"};
2004 zpool_load_policy_t policy
;
2005 nvlist_t
*nv
= NULL
;
2006 nvlist_t
*nvinfo
= NULL
;
2007 nvlist_t
*missing
= NULL
;
2008 const char *thename
;
2012 char errbuf
[ERRBUFLEN
];
2014 origname
= fnvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
);
2016 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2017 "cannot import pool '%s'"), origname
);
2019 if (newname
!= NULL
) {
2020 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
2021 return (zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
2022 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
2029 if (props
!= NULL
) {
2031 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
2033 version
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
2035 if ((props
= zpool_valid_proplist(hdl
, origname
,
2036 props
, version
, flags
, errbuf
)) == NULL
)
2038 zcmd_write_src_nvlist(hdl
, &zc
, props
);
2042 (void) strlcpy(zc
.zc_name
, thename
, sizeof (zc
.zc_name
));
2044 zc
.zc_guid
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
);
2046 zcmd_write_conf_nvlist(hdl
, &zc
, config
);
2047 zcmd_alloc_dst_nvlist(hdl
, &zc
, zc
.zc_nvlist_conf_size
* 2);
2049 zc
.zc_cookie
= flags
;
2050 while ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_IMPORT
, &zc
)) != 0 &&
2052 zcmd_expand_dst_nvlist(hdl
, &zc
);
2056 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nv
);
2058 zcmd_free_nvlists(&zc
);
2060 zpool_get_load_policy(config
, &policy
);
2067 * Dry-run failed, but we print out what success
2068 * looks like if we found a best txg
2070 if (policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) {
2071 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
2077 if (newname
== NULL
)
2078 (void) snprintf(desc
, sizeof (desc
),
2079 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
2082 (void) snprintf(desc
, sizeof (desc
),
2083 dgettext(TEXT_DOMAIN
, "cannot import '%s' as '%s'"),
2088 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
2089 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
2090 nvlist_exists(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
)) {
2091 (void) printf(dgettext(TEXT_DOMAIN
, "This "
2092 "pool uses the following feature(s) not "
2093 "supported by this system:\n"));
2094 zpool_print_unsup_feat(nv
);
2095 if (nvlist_exists(nvinfo
,
2096 ZPOOL_CONFIG_CAN_RDONLY
)) {
2097 (void) printf(dgettext(TEXT_DOMAIN
,
2098 "All unsupported features are only "
2099 "required for writing to the pool."
2100 "\nThe pool can be imported using "
2101 "'-o readonly=on'.\n"));
2105 * Unsupported version.
2107 (void) zfs_error(hdl
, EZFS_BADVERSION
, desc
);
2111 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
2112 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0) {
2113 char *hostname
= "<unknown>";
2114 uint64_t hostid
= 0;
2115 mmp_state_t mmp_state
;
2117 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
2118 ZPOOL_CONFIG_MMP_STATE
);
2120 if (nvlist_exists(nvinfo
,
2121 ZPOOL_CONFIG_MMP_HOSTNAME
))
2122 hostname
= fnvlist_lookup_string(nvinfo
,
2123 ZPOOL_CONFIG_MMP_HOSTNAME
);
2125 if (nvlist_exists(nvinfo
,
2126 ZPOOL_CONFIG_MMP_HOSTID
))
2127 hostid
= fnvlist_lookup_uint64(nvinfo
,
2128 ZPOOL_CONFIG_MMP_HOSTID
);
2130 if (mmp_state
== MMP_STATE_ACTIVE
) {
2131 (void) snprintf(aux
, sizeof (aux
),
2132 dgettext(TEXT_DOMAIN
, "pool is imp"
2133 "orted on host '%s' (hostid=%lx).\n"
2134 "Export the pool on the other "
2135 "system, then run 'zpool import'."),
2136 hostname
, (unsigned long) hostid
);
2137 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
2138 (void) snprintf(aux
, sizeof (aux
),
2139 dgettext(TEXT_DOMAIN
, "pool has "
2140 "the multihost property on and "
2141 "the\nsystem's hostid is not set. "
2142 "Set a unique system hostid with "
2143 "the zgenhostid(8) command.\n"));
2146 (void) zfs_error_aux(hdl
, "%s", aux
);
2148 (void) zfs_error(hdl
, EZFS_ACTIVE_POOL
, desc
);
2152 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, desc
);
2156 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2157 "one or more devices is read only"));
2158 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
2162 if (nv
&& nvlist_lookup_nvlist(nv
,
2163 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
2164 nvlist_lookup_nvlist(nvinfo
,
2165 ZPOOL_CONFIG_MISSING_DEVICES
, &missing
) == 0) {
2166 (void) printf(dgettext(TEXT_DOMAIN
,
2167 "The devices below are missing or "
2168 "corrupted, use '-m' to import the pool "
2170 print_vdev_tree(hdl
, NULL
, missing
, 2);
2171 (void) printf("\n");
2173 (void) zpool_standard_error(hdl
, error
, desc
);
2177 (void) zpool_standard_error(hdl
, error
, desc
);
2181 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2182 "one or more devices are already in use\n"));
2183 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
2186 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2187 "new name of at least one dataset is longer than "
2188 "the maximum allowable length"));
2189 (void) zfs_error(hdl
, EZFS_NAMETOOLONG
, desc
);
2192 (void) zpool_standard_error(hdl
, error
, desc
);
2193 zpool_explain_recover(hdl
,
2194 newname
? origname
: thename
, -error
, nv
);
2201 zpool_handle_t
*zhp
;
2204 * This should never fail, but play it safe anyway.
2206 if (zpool_open_silent(hdl
, thename
, &zhp
) != 0)
2208 else if (zhp
!= NULL
)
2210 if (policy
.zlp_rewind
&
2211 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
2212 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
2213 ((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
,
2521 (void) snprintf(msg
, sizeof (msg
),
2522 dgettext(TEXT_DOMAIN
, "operation failed"));
2523 zpool_standard_error(zhp
->zpool_hdl
, err
, msg
);
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"};
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(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2572 "cannot pause scrubbing %s"), zc
.zc_name
);
2574 assert(cmd
== POOL_SCRUB_NORMAL
);
2575 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2576 "cannot scrub %s"), zc
.zc_name
);
2578 } else if (func
== POOL_SCAN_RESILVER
) {
2579 assert(cmd
== POOL_SCRUB_NORMAL
);
2580 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2581 "cannot restart resilver on %s"), zc
.zc_name
);
2582 } else if (func
== POOL_SCAN_NONE
) {
2583 (void) snprintf(msg
, sizeof (msg
),
2584 dgettext(TEXT_DOMAIN
, "cannot cancel scrubbing %s"),
2587 assert(!"unexpected result");
2592 pool_scan_stat_t
*ps
= NULL
;
2595 nvroot
= fnvlist_lookup_nvlist(zhp
->zpool_config
,
2596 ZPOOL_CONFIG_VDEV_TREE
);
2597 (void) nvlist_lookup_uint64_array(nvroot
,
2598 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &psc
);
2599 if (ps
&& ps
->pss_func
== POOL_SCAN_SCRUB
&&
2600 ps
->pss_state
== DSS_SCANNING
) {
2601 if (cmd
== POOL_SCRUB_PAUSE
)
2602 return (zfs_error(hdl
, EZFS_SCRUB_PAUSED
, msg
));
2604 return (zfs_error(hdl
, EZFS_SCRUBBING
, msg
));
2606 return (zfs_error(hdl
, EZFS_RESILVERING
, msg
));
2608 } else if (err
== ENOENT
) {
2609 return (zfs_error(hdl
, EZFS_NO_SCRUB
, msg
));
2610 } else if (err
== ENOTSUP
&& func
== POOL_SCAN_RESILVER
) {
2611 return (zfs_error(hdl
, EZFS_NO_RESILVER_DEFER
, msg
));
2613 return (zpool_standard_error(hdl
, err
, msg
));
2618 * Find a vdev that matches the search criteria specified. We use the
2619 * the nvpair name to determine how we should look for the device.
2620 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2621 * spare; but FALSE if its an INUSE spare.
2624 vdev_to_nvlist_iter(nvlist_t
*nv
, nvlist_t
*search
, boolean_t
*avail_spare
,
2625 boolean_t
*l2cache
, boolean_t
*log
)
2632 nvpair_t
*pair
= nvlist_next_nvpair(search
, NULL
);
2634 /* Nothing to look for */
2635 if (search
== NULL
|| pair
== NULL
)
2638 /* Obtain the key we will use to search */
2639 srchkey
= nvpair_name(pair
);
2641 switch (nvpair_type(pair
)) {
2642 case DATA_TYPE_UINT64
:
2643 if (strcmp(srchkey
, ZPOOL_CONFIG_GUID
) == 0) {
2644 uint64_t srchval
= fnvpair_value_uint64(pair
);
2645 uint64_t theguid
= fnvlist_lookup_uint64(nv
,
2647 if (theguid
== srchval
)
2652 case DATA_TYPE_STRING
: {
2653 char *srchval
, *val
;
2655 srchval
= fnvpair_value_string(pair
);
2656 if (nvlist_lookup_string(nv
, srchkey
, &val
) != 0)
2660 * Search for the requested value. Special cases:
2662 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2663 * "-part1", or "p1". The suffix is hidden from the user,
2664 * but included in the string, so this matches around it.
2665 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2666 * is used to check all possible expanded paths.
2667 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2669 * Otherwise, all other searches are simple string compares.
2671 if (strcmp(srchkey
, ZPOOL_CONFIG_PATH
) == 0) {
2672 uint64_t wholedisk
= 0;
2674 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
2676 if (zfs_strcmp_pathname(srchval
, val
, wholedisk
) == 0)
2679 } else if (strcmp(srchkey
, ZPOOL_CONFIG_TYPE
) == 0 && val
) {
2680 char *type
, *idx
, *end
, *p
;
2681 uint64_t id
, vdev_id
;
2684 * Determine our vdev type, keeping in mind
2685 * that the srchval is composed of a type and
2686 * vdev id pair (i.e. mirror-4).
2688 if ((type
= strdup(srchval
)) == NULL
)
2691 if ((p
= strrchr(type
, '-')) == NULL
) {
2699 * If the types don't match then keep looking.
2701 if (strncmp(val
, type
, strlen(val
)) != 0) {
2706 verify(zpool_vdev_is_interior(type
));
2708 id
= fnvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
);
2710 vdev_id
= strtoull(idx
, &end
, 10);
2713 * If we are looking for a raidz and a parity is
2714 * specified, make sure it matches.
2716 int rzlen
= strlen(VDEV_TYPE_RAIDZ
);
2717 assert(rzlen
== strlen(VDEV_TYPE_DRAID
));
2718 int typlen
= strlen(type
);
2719 if ((strncmp(type
, VDEV_TYPE_RAIDZ
, rzlen
) == 0 ||
2720 strncmp(type
, VDEV_TYPE_DRAID
, rzlen
) == 0) &&
2722 uint64_t vdev_parity
;
2723 int parity
= *(type
+ rzlen
) - '0';
2725 if (parity
<= 0 || parity
> 3 ||
2726 (typlen
- rzlen
) != 1) {
2728 * Nonsense parity specified, can
2734 vdev_parity
= fnvlist_lookup_uint64(nv
,
2735 ZPOOL_CONFIG_NPARITY
);
2736 if ((int)vdev_parity
!= parity
) {
2747 * Now verify that we have the correct vdev id.
2756 if (strcmp(srchval
, val
) == 0)
2765 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2766 &child
, &children
) != 0)
2769 for (c
= 0; c
< children
; c
++) {
2770 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2771 avail_spare
, l2cache
, NULL
)) != NULL
) {
2773 * The 'is_log' value is only set for the toplevel
2774 * vdev, not the leaf vdevs. So we always lookup the
2775 * log device from the root of the vdev tree (where
2776 * 'log' is non-NULL).
2779 nvlist_lookup_uint64(child
[c
],
2780 ZPOOL_CONFIG_IS_LOG
, &is_log
) == 0 &&
2788 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2789 &child
, &children
) == 0) {
2790 for (c
= 0; c
< children
; c
++) {
2791 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2792 avail_spare
, l2cache
, NULL
)) != NULL
) {
2793 *avail_spare
= B_TRUE
;
2799 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2800 &child
, &children
) == 0) {
2801 for (c
= 0; c
< children
; c
++) {
2802 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2803 avail_spare
, l2cache
, NULL
)) != NULL
) {
2814 * Given a physical path or guid, find the associated vdev.
2817 zpool_find_vdev_by_physpath(zpool_handle_t
*zhp
, const char *ppath
,
2818 boolean_t
*avail_spare
, boolean_t
*l2cache
, boolean_t
*log
)
2820 nvlist_t
*search
, *nvroot
, *ret
;
2824 search
= fnvlist_alloc();
2826 guid
= strtoull(ppath
, &end
, 0);
2827 if (guid
!= 0 && *end
== '\0') {
2828 fnvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
);
2830 fnvlist_add_string(search
, ZPOOL_CONFIG_PHYS_PATH
, ppath
);
2833 nvroot
= fnvlist_lookup_nvlist(zhp
->zpool_config
,
2834 ZPOOL_CONFIG_VDEV_TREE
);
2836 *avail_spare
= B_FALSE
;
2840 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2841 fnvlist_free(search
);
2847 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2850 zpool_vdev_is_interior(const char *name
)
2852 if (strncmp(name
, VDEV_TYPE_RAIDZ
, strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2853 strncmp(name
, VDEV_TYPE_SPARE
, strlen(VDEV_TYPE_SPARE
)) == 0 ||
2855 VDEV_TYPE_REPLACING
, strlen(VDEV_TYPE_REPLACING
)) == 0 ||
2856 strncmp(name
, VDEV_TYPE_MIRROR
, strlen(VDEV_TYPE_MIRROR
)) == 0)
2859 if (strncmp(name
, VDEV_TYPE_DRAID
, strlen(VDEV_TYPE_DRAID
)) == 0 &&
2860 !zpool_is_draid_spare(name
))
2867 zpool_find_vdev(zpool_handle_t
*zhp
, const char *path
, boolean_t
*avail_spare
,
2868 boolean_t
*l2cache
, boolean_t
*log
)
2871 nvlist_t
*nvroot
, *search
, *ret
;
2874 search
= fnvlist_alloc();
2876 guid
= strtoull(path
, &end
, 0);
2877 if (guid
!= 0 && *end
== '\0') {
2878 fnvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
);
2879 } else if (zpool_vdev_is_interior(path
)) {
2880 fnvlist_add_string(search
, ZPOOL_CONFIG_TYPE
, path
);
2882 fnvlist_add_string(search
, ZPOOL_CONFIG_PATH
, path
);
2885 nvroot
= fnvlist_lookup_nvlist(zhp
->zpool_config
,
2886 ZPOOL_CONFIG_VDEV_TREE
);
2888 *avail_spare
= B_FALSE
;
2892 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2893 fnvlist_free(search
);
2899 vdev_is_online(nvlist_t
*nv
)
2903 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
, &ival
) == 0 ||
2904 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
, &ival
) == 0 ||
2905 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
, &ival
) == 0)
2912 * Helper function for zpool_get_physpaths().
2915 vdev_get_one_physpath(nvlist_t
*config
, char *physpath
, size_t physpath_size
,
2916 size_t *bytes_written
)
2918 size_t bytes_left
, pos
, rsz
;
2922 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PHYS_PATH
,
2924 return (EZFS_NODEVICE
);
2926 pos
= *bytes_written
;
2927 bytes_left
= physpath_size
- pos
;
2928 format
= (pos
== 0) ? "%s" : " %s";
2930 rsz
= snprintf(physpath
+ pos
, bytes_left
, format
, tmppath
);
2931 *bytes_written
+= rsz
;
2933 if (rsz
>= bytes_left
) {
2934 /* if physpath was not copied properly, clear it */
2935 if (bytes_left
!= 0) {
2938 return (EZFS_NOSPC
);
2944 vdev_get_physpaths(nvlist_t
*nv
, char *physpath
, size_t phypath_size
,
2945 size_t *rsz
, boolean_t is_spare
)
2950 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
2951 return (EZFS_INVALCONFIG
);
2953 if (strcmp(type
, VDEV_TYPE_DISK
) == 0) {
2955 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2956 * For a spare vdev, we only want to boot from the active
2961 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
2964 return (EZFS_INVALCONFIG
);
2967 if (vdev_is_online(nv
)) {
2968 if ((ret
= vdev_get_one_physpath(nv
, physpath
,
2969 phypath_size
, rsz
)) != 0)
2972 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) == 0 ||
2973 strcmp(type
, VDEV_TYPE_RAIDZ
) == 0 ||
2974 strcmp(type
, VDEV_TYPE_REPLACING
) == 0 ||
2975 (is_spare
= (strcmp(type
, VDEV_TYPE_SPARE
) == 0))) {
2980 if (nvlist_lookup_nvlist_array(nv
,
2981 ZPOOL_CONFIG_CHILDREN
, &child
, &count
) != 0)
2982 return (EZFS_INVALCONFIG
);
2984 for (i
= 0; i
< count
; i
++) {
2985 ret
= vdev_get_physpaths(child
[i
], physpath
,
2986 phypath_size
, rsz
, is_spare
);
2987 if (ret
== EZFS_NOSPC
)
2992 return (EZFS_POOL_INVALARG
);
2996 * Get phys_path for a root pool config.
2997 * Return 0 on success; non-zero on failure.
3000 zpool_get_config_physpath(nvlist_t
*config
, char *physpath
, size_t phypath_size
)
3003 nvlist_t
*vdev_root
;
3010 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3012 return (EZFS_INVALCONFIG
);
3014 if (nvlist_lookup_string(vdev_root
, ZPOOL_CONFIG_TYPE
, &type
) != 0 ||
3015 nvlist_lookup_nvlist_array(vdev_root
, ZPOOL_CONFIG_CHILDREN
,
3016 &child
, &count
) != 0)
3017 return (EZFS_INVALCONFIG
);
3020 * root pool can only have a single top-level vdev.
3022 if (strcmp(type
, VDEV_TYPE_ROOT
) != 0 || count
!= 1)
3023 return (EZFS_POOL_INVALARG
);
3025 (void) vdev_get_physpaths(child
[0], physpath
, phypath_size
, &rsz
,
3028 /* No online devices */
3030 return (EZFS_NODEVICE
);
3036 * Get phys_path for a root pool
3037 * Return 0 on success; non-zero on failure.
3040 zpool_get_physpath(zpool_handle_t
*zhp
, char *physpath
, size_t phypath_size
)
3042 return (zpool_get_config_physpath(zhp
->zpool_config
, physpath
,
3047 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
3049 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
3050 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
3054 zpool_vdev_path_to_guid_impl(zpool_handle_t
*zhp
, const char *path
,
3055 boolean_t
*is_spare
, boolean_t
*is_l2cache
, boolean_t
*is_log
)
3057 boolean_t spare
= B_FALSE
, l2cache
= B_FALSE
, log
= B_FALSE
;
3060 if ((tgt
= zpool_find_vdev(zhp
, path
, &spare
, &l2cache
,
3064 if (is_spare
!= NULL
)
3066 if (is_l2cache
!= NULL
)
3067 *is_l2cache
= l2cache
;
3071 return (fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
));
3074 /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
3076 zpool_vdev_path_to_guid(zpool_handle_t
*zhp
, const char *path
)
3078 return (zpool_vdev_path_to_guid_impl(zhp
, path
, NULL
, NULL
, NULL
));
3082 * Bring the specified vdev online. The 'flags' parameter is a set of the
3083 * ZFS_ONLINE_* flags.
3086 zpool_vdev_online(zpool_handle_t
*zhp
, const char *path
, int flags
,
3087 vdev_state_t
*newstate
)
3089 zfs_cmd_t zc
= {"\0"};
3092 boolean_t avail_spare
, l2cache
, islog
;
3093 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3095 if (flags
& ZFS_ONLINE_EXPAND
) {
3096 (void) snprintf(msg
, sizeof (msg
),
3097 dgettext(TEXT_DOMAIN
, "cannot expand %s"), path
);
3099 (void) snprintf(msg
, sizeof (msg
),
3100 dgettext(TEXT_DOMAIN
, "cannot online %s"), path
);
3103 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3104 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3106 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3108 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3111 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3115 if ((flags
& ZFS_ONLINE_EXPAND
||
3116 zpool_get_prop_int(zhp
, ZPOOL_PROP_AUTOEXPAND
, NULL
)) &&
3117 nvlist_lookup_string(tgt
, ZPOOL_CONFIG_PATH
, &pathname
) == 0) {
3118 uint64_t wholedisk
= 0;
3120 (void) nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_WHOLE_DISK
,
3124 * XXX - L2ARC 1.0 devices can't support expansion.
3127 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3128 "cannot expand cache devices"));
3129 return (zfs_error(hdl
, EZFS_VDEVNOTSUP
, msg
));
3133 const char *fullpath
= path
;
3134 char buf
[MAXPATHLEN
];
3137 if (path
[0] != '/') {
3138 error
= zfs_resolve_shortname(path
, buf
,
3141 return (zfs_error(hdl
, EZFS_NODEVICE
,
3147 error
= zpool_relabel_disk(hdl
, fullpath
, msg
);
3154 zc
.zc_cookie
= VDEV_STATE_ONLINE
;
3157 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) != 0) {
3158 if (errno
== EINVAL
) {
3159 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "was split "
3160 "from this pool into a new one. Use '%s' "
3161 "instead"), "zpool detach");
3162 return (zfs_error(hdl
, EZFS_POSTSPLIT_ONLINE
, msg
));
3164 return (zpool_standard_error(hdl
, errno
, msg
));
3167 *newstate
= zc
.zc_cookie
;
3172 * Take the specified vdev offline
3175 zpool_vdev_offline(zpool_handle_t
*zhp
, const char *path
, boolean_t istmp
)
3177 zfs_cmd_t zc
= {"\0"};
3180 boolean_t avail_spare
, l2cache
;
3181 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3183 (void) snprintf(msg
, sizeof (msg
),
3184 dgettext(TEXT_DOMAIN
, "cannot offline %s"), path
);
3186 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3187 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3189 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3191 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3194 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3196 zc
.zc_cookie
= VDEV_STATE_OFFLINE
;
3197 zc
.zc_obj
= istmp
? ZFS_OFFLINE_TEMPORARY
: 0;
3199 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3206 * There are no other replicas of this device.
3208 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
3212 * The log device has unplayed logs
3214 return (zfs_error(hdl
, EZFS_UNPLAYED_LOGS
, msg
));
3217 return (zpool_standard_error(hdl
, errno
, msg
));
3222 * Mark the given vdev faulted.
3225 zpool_vdev_fault(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
3227 zfs_cmd_t zc
= {"\0"};
3229 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3231 (void) snprintf(msg
, sizeof (msg
),
3232 dgettext(TEXT_DOMAIN
, "cannot fault %llu"), (u_longlong_t
)guid
);
3234 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3236 zc
.zc_cookie
= VDEV_STATE_FAULTED
;
3239 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3246 * There are no other replicas of this device.
3248 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
3251 return (zpool_standard_error(hdl
, errno
, msg
));
3257 * Mark the given vdev degraded.
3260 zpool_vdev_degrade(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
3262 zfs_cmd_t zc
= {"\0"};
3264 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3266 (void) snprintf(msg
, sizeof (msg
),
3267 dgettext(TEXT_DOMAIN
, "cannot degrade %llu"), (u_longlong_t
)guid
);
3269 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3271 zc
.zc_cookie
= VDEV_STATE_DEGRADED
;
3274 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3277 return (zpool_standard_error(hdl
, errno
, msg
));
3281 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
3285 is_replacing_spare(nvlist_t
*search
, nvlist_t
*tgt
, int which
)
3290 if (nvlist_lookup_nvlist_array(search
, ZPOOL_CONFIG_CHILDREN
, &child
,
3292 char *type
= fnvlist_lookup_string(search
, ZPOOL_CONFIG_TYPE
);
3293 if ((strcmp(type
, VDEV_TYPE_SPARE
) == 0 ||
3294 strcmp(type
, VDEV_TYPE_DRAID_SPARE
) == 0) &&
3295 children
== 2 && child
[which
] == tgt
)
3298 for (c
= 0; c
< children
; c
++)
3299 if (is_replacing_spare(child
[c
], tgt
, which
))
3307 * Attach new_disk (fully described by nvroot) to old_disk.
3308 * If 'replacing' is specified, the new disk will replace the old one.
3311 zpool_vdev_attach(zpool_handle_t
*zhp
, const char *old_disk
,
3312 const char *new_disk
, nvlist_t
*nvroot
, int replacing
, boolean_t rebuild
)
3314 zfs_cmd_t zc
= {"\0"};
3318 boolean_t avail_spare
, l2cache
, islog
;
3323 nvlist_t
*config_root
;
3324 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3327 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
3328 "cannot replace %s with %s"), old_disk
, new_disk
);
3330 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
3331 "cannot attach %s to %s"), new_disk
, old_disk
);
3333 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3334 if ((tgt
= zpool_find_vdev(zhp
, old_disk
, &avail_spare
, &l2cache
,
3336 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3339 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3342 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
3344 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3345 zc
.zc_cookie
= replacing
;
3346 zc
.zc_simple
= rebuild
;
3349 zfeature_lookup_guid("org.openzfs:device_rebuild", NULL
) != 0) {
3350 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3351 "the loaded zfs module doesn't support device rebuilds"));
3352 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, msg
));
3355 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
3356 &child
, &children
) != 0 || children
!= 1) {
3357 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3358 "new device must be a single disk"));
3359 return (zfs_error(hdl
, EZFS_INVALCONFIG
, msg
));
3362 config_root
= fnvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
3363 ZPOOL_CONFIG_VDEV_TREE
);
3365 if ((newname
= zpool_vdev_name(NULL
, NULL
, child
[0], 0)) == NULL
)
3369 * If the target is a hot spare that has been swapped in, we can only
3370 * replace it with another hot spare.
3373 nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_IS_SPARE
, &val
) == 0 &&
3374 (zpool_find_vdev(zhp
, newname
, &avail_spare
, &l2cache
,
3375 NULL
) == NULL
|| !avail_spare
) &&
3376 is_replacing_spare(config_root
, tgt
, 1)) {
3377 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3378 "can only be replaced by another hot spare"));
3380 return (zfs_error(hdl
, EZFS_BADTARGET
, msg
));
3385 zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
);
3387 ret
= zfs_ioctl(hdl
, ZFS_IOC_VDEV_ATTACH
, &zc
);
3389 zcmd_free_nvlists(&zc
);
3397 * Can't attach to or replace this type of vdev.
3400 uint64_t version
= zpool_get_prop_int(zhp
,
3401 ZPOOL_PROP_VERSION
, NULL
);
3404 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3405 "cannot replace a log with a spare"));
3406 } else if (rebuild
) {
3407 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3408 "only mirror and dRAID vdevs support "
3409 "sequential reconstruction"));
3410 } else if (zpool_is_draid_spare(new_disk
)) {
3411 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3412 "dRAID spares can only replace child "
3413 "devices in their parent's dRAID vdev"));
3414 } else if (version
>= SPA_VERSION_MULTI_REPLACE
) {
3415 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3416 "already in replacing/spare config; wait "
3417 "for completion or use 'zpool detach'"));
3419 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3420 "cannot replace a replacing device"));
3423 char status
[64] = {0};
3424 zpool_prop_get_feature(zhp
,
3425 "feature@device_rebuild", status
, 63);
3427 strncmp(status
, ZFS_FEATURE_DISABLED
, 64) == 0) {
3428 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3429 "device_rebuild feature must be enabled "
3430 "in order to use sequential "
3433 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3434 "can only attach to mirrors and top-level "
3438 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
3443 * The new device must be a single disk.
3445 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3446 "new device must be a single disk"));
3447 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3451 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "%s is busy, "
3452 "or device removal is in progress"),
3454 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
3459 * The new device is too small.
3461 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3462 "device is too small"));
3463 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
3468 * The new device has a different optimal sector size.
3470 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3471 "new device has a different optimal sector size; use the "
3472 "option '-o ashift=N' to override the optimal size"));
3473 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
3478 * The resulting top-level vdev spec won't fit in the label.
3480 (void) zfs_error(hdl
, EZFS_DEVOVERFLOW
, msg
);
3484 (void) zpool_standard_error(hdl
, errno
, msg
);
3491 * Detach the specified device.
3494 zpool_vdev_detach(zpool_handle_t
*zhp
, const char *path
)
3496 zfs_cmd_t zc
= {"\0"};
3499 boolean_t avail_spare
, l2cache
;
3500 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3502 (void) snprintf(msg
, sizeof (msg
),
3503 dgettext(TEXT_DOMAIN
, "cannot detach %s"), path
);
3505 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3506 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3508 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3511 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3514 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
3516 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3518 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_DETACH
, &zc
) == 0)
3525 * Can't detach from this type of vdev.
3527 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "only "
3528 "applicable to mirror and replacing vdevs"));
3529 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
3534 * There are no other replicas of this device.
3536 (void) zfs_error(hdl
, EZFS_NOREPLICAS
, msg
);
3540 (void) zpool_standard_error(hdl
, errno
, msg
);
3547 * Find a mirror vdev in the source nvlist.
3549 * The mchild array contains a list of disks in one of the top-level mirrors
3550 * of the source pool. The schild array contains a list of disks that the
3551 * user specified on the command line. We loop over the mchild array to
3552 * see if any entry in the schild array matches.
3554 * If a disk in the mchild array is found in the schild array, we return
3555 * the index of that entry. Otherwise we return -1.
3558 find_vdev_entry(zpool_handle_t
*zhp
, nvlist_t
**mchild
, uint_t mchildren
,
3559 nvlist_t
**schild
, uint_t schildren
)
3563 for (mc
= 0; mc
< mchildren
; mc
++) {
3565 char *mpath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
3568 for (sc
= 0; sc
< schildren
; sc
++) {
3569 char *spath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
3571 boolean_t result
= (strcmp(mpath
, spath
) == 0);
3587 * Split a mirror pool. If newroot points to null, then a new nvlist
3588 * is generated and it is the responsibility of the caller to free it.
3591 zpool_vdev_split(zpool_handle_t
*zhp
, char *newname
, nvlist_t
**newroot
,
3592 nvlist_t
*props
, splitflags_t flags
)
3594 zfs_cmd_t zc
= {"\0"};
3595 char msg
[1024], *bias
;
3596 nvlist_t
*tree
, *config
, **child
, **newchild
, *newconfig
= NULL
;
3597 nvlist_t
**varray
= NULL
, *zc_props
= NULL
;
3598 uint_t c
, children
, newchildren
, lastlog
= 0, vcount
, found
= 0;
3599 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3600 uint64_t vers
, readonly
= B_FALSE
;
3601 boolean_t freelist
= B_FALSE
, memory_err
= B_TRUE
;
3604 (void) snprintf(msg
, sizeof (msg
),
3605 dgettext(TEXT_DOMAIN
, "Unable to split %s"), zhp
->zpool_name
);
3607 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
3608 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
3610 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
3611 (void) fprintf(stderr
, gettext("Internal error: unable to "
3612 "retrieve pool configuration\n"));
3616 tree
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
3617 vers
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
3620 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
3621 if ((zc_props
= zpool_valid_proplist(hdl
, zhp
->zpool_name
,
3622 props
, vers
, flags
, msg
)) == NULL
)
3624 (void) nvlist_lookup_uint64(zc_props
,
3625 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
3627 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3628 "property %s can only be set at import time"),
3629 zpool_prop_to_name(ZPOOL_PROP_READONLY
));
3634 if (nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
3636 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3637 "Source pool is missing vdev tree"));
3638 nvlist_free(zc_props
);
3642 varray
= zfs_alloc(hdl
, children
* sizeof (nvlist_t
*));
3645 if (*newroot
== NULL
||
3646 nvlist_lookup_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
3647 &newchild
, &newchildren
) != 0)
3650 for (c
= 0; c
< children
; c
++) {
3651 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
3652 boolean_t is_special
= B_FALSE
, is_dedup
= B_FALSE
;
3654 nvlist_t
**mchild
, *vdev
;
3659 * Unlike cache & spares, slogs are stored in the
3660 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3662 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
3664 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
3666 if (is_log
|| is_hole
) {
3668 * Create a hole vdev and put it in the config.
3670 if (nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) != 0)
3672 if (nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
,
3673 VDEV_TYPE_HOLE
) != 0)
3675 if (nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_HOLE
,
3680 varray
[vcount
++] = vdev
;
3684 type
= fnvlist_lookup_string(child
[c
], ZPOOL_CONFIG_TYPE
);
3686 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0) {
3688 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
3691 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
3692 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3693 "Source pool must be composed only of mirrors\n"));
3694 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3698 if (nvlist_lookup_string(child
[c
],
3699 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
) == 0) {
3700 if (strcmp(bias
, VDEV_ALLOC_BIAS_SPECIAL
) == 0)
3701 is_special
= B_TRUE
;
3702 else if (strcmp(bias
, VDEV_ALLOC_BIAS_DEDUP
) == 0)
3705 verify(nvlist_lookup_nvlist_array(child
[c
],
3706 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
3708 /* find or add an entry for this top-level vdev */
3709 if (newchildren
> 0 &&
3710 (entry
= find_vdev_entry(zhp
, mchild
, mchildren
,
3711 newchild
, newchildren
)) >= 0) {
3712 /* We found a disk that the user specified. */
3713 vdev
= mchild
[entry
];
3716 /* User didn't specify a disk for this vdev. */
3717 vdev
= mchild
[mchildren
- 1];
3720 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
3723 if (flags
.dryrun
!= 0) {
3724 if (is_dedup
== B_TRUE
) {
3725 if (nvlist_add_string(varray
[vcount
- 1],
3726 ZPOOL_CONFIG_ALLOCATION_BIAS
,
3727 VDEV_ALLOC_BIAS_DEDUP
) != 0)
3729 } else if (is_special
== B_TRUE
) {
3730 if (nvlist_add_string(varray
[vcount
- 1],
3731 ZPOOL_CONFIG_ALLOCATION_BIAS
,
3732 VDEV_ALLOC_BIAS_SPECIAL
) != 0)
3738 /* did we find every disk the user specified? */
3739 if (found
!= newchildren
) {
3740 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Device list must "
3741 "include at most one disk from each mirror"));
3742 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3746 /* Prepare the nvlist for populating. */
3747 if (*newroot
== NULL
) {
3748 if (nvlist_alloc(newroot
, NV_UNIQUE_NAME
, 0) != 0)
3751 if (nvlist_add_string(*newroot
, ZPOOL_CONFIG_TYPE
,
3752 VDEV_TYPE_ROOT
) != 0)
3755 verify(nvlist_remove_all(*newroot
, ZPOOL_CONFIG_CHILDREN
) == 0);
3758 /* Add all the children we found */
3759 if (nvlist_add_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
3760 (const nvlist_t
**)varray
, lastlog
== 0 ? vcount
: lastlog
) != 0)
3764 * If we're just doing a dry run, exit now with success.
3767 memory_err
= B_FALSE
;
3772 /* now build up the config list & call the ioctl */
3773 if (nvlist_alloc(&newconfig
, NV_UNIQUE_NAME
, 0) != 0)
3776 if (nvlist_add_nvlist(newconfig
,
3777 ZPOOL_CONFIG_VDEV_TREE
, *newroot
) != 0 ||
3778 nvlist_add_string(newconfig
,
3779 ZPOOL_CONFIG_POOL_NAME
, newname
) != 0 ||
3780 nvlist_add_uint64(newconfig
, ZPOOL_CONFIG_VERSION
, vers
) != 0)
3784 * The new pool is automatically part of the namespace unless we
3785 * explicitly export it.
3788 zc
.zc_cookie
= ZPOOL_EXPORT_AFTER_SPLIT
;
3789 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3790 (void) strlcpy(zc
.zc_string
, newname
, sizeof (zc
.zc_string
));
3791 zcmd_write_conf_nvlist(hdl
, &zc
, newconfig
);
3792 if (zc_props
!= NULL
)
3793 zcmd_write_src_nvlist(hdl
, &zc
, zc_props
);
3795 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SPLIT
, &zc
) != 0) {
3796 retval
= zpool_standard_error(hdl
, errno
, msg
);
3801 memory_err
= B_FALSE
;
3804 if (varray
!= NULL
) {
3807 for (v
= 0; v
< vcount
; v
++)
3808 nvlist_free(varray
[v
]);
3811 zcmd_free_nvlists(&zc
);
3812 nvlist_free(zc_props
);
3813 nvlist_free(newconfig
);
3815 nvlist_free(*newroot
);
3823 return (no_memory(hdl
));
3829 * Remove the given device.
3832 zpool_vdev_remove(zpool_handle_t
*zhp
, const char *path
)
3834 zfs_cmd_t zc
= {"\0"};
3837 boolean_t avail_spare
, l2cache
, islog
;
3838 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3841 (void) snprintf(msg
, sizeof (msg
),
3842 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3844 if (zpool_is_draid_spare(path
)) {
3845 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3846 "dRAID spares cannot be removed"));
3847 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3850 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3851 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3853 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3855 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
3856 if (islog
&& version
< SPA_VERSION_HOLES
) {
3857 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3858 "pool must be upgraded to support log removal"));
3859 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
3862 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3864 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3870 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3871 "invalid config; all top-level vdevs must "
3872 "have the same sector size and not be raidz."));
3873 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3878 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3879 "Mount encrypted datasets to replay logs."));
3881 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3882 "Pool busy; removal may already be in progress"));
3884 (void) zfs_error(hdl
, EZFS_BUSY
, msg
);
3889 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3890 "Mount encrypted datasets to replay logs."));
3891 (void) zfs_error(hdl
, EZFS_BUSY
, msg
);
3893 (void) zpool_standard_error(hdl
, errno
, msg
);
3898 (void) zpool_standard_error(hdl
, errno
, msg
);
3904 zpool_vdev_remove_cancel(zpool_handle_t
*zhp
)
3906 zfs_cmd_t zc
= {{0}};
3908 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3910 (void) snprintf(msg
, sizeof (msg
),
3911 dgettext(TEXT_DOMAIN
, "cannot cancel removal"));
3913 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3916 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3919 return (zpool_standard_error(hdl
, errno
, msg
));
3923 zpool_vdev_indirect_size(zpool_handle_t
*zhp
, const char *path
,
3928 boolean_t avail_spare
, l2cache
, islog
;
3929 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3931 (void) snprintf(msg
, sizeof (msg
),
3932 dgettext(TEXT_DOMAIN
, "cannot determine indirect size of %s"),
3935 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3937 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3939 if (avail_spare
|| l2cache
|| islog
) {
3944 if (nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_INDIRECT_SIZE
, sizep
) != 0) {
3945 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3946 "indirect size not available"));
3947 return (zfs_error(hdl
, EINVAL
, msg
));
3953 * Clear the errors for the pool, or the particular device if specified.
3956 zpool_clear(zpool_handle_t
*zhp
, const char *path
, nvlist_t
*rewindnvl
)
3958 zfs_cmd_t zc
= {"\0"};
3961 zpool_load_policy_t policy
;
3962 boolean_t avail_spare
, l2cache
;
3963 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3964 nvlist_t
*nvi
= NULL
;
3968 (void) snprintf(msg
, sizeof (msg
),
3969 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3972 (void) snprintf(msg
, sizeof (msg
),
3973 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3976 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3978 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
,
3979 &l2cache
, NULL
)) == NULL
)
3980 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3983 * Don't allow error clearing for hot spares. Do allow
3984 * error clearing for l2cache devices.
3987 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3989 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3992 zpool_get_load_policy(rewindnvl
, &policy
);
3993 zc
.zc_cookie
= policy
.zlp_rewind
;
3995 zcmd_alloc_dst_nvlist(hdl
, &zc
, zhp
->zpool_config_size
* 2);
3996 zcmd_write_src_nvlist(hdl
, &zc
, rewindnvl
);
3998 while ((error
= zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
)) != 0 &&
4000 zcmd_expand_dst_nvlist(hdl
, &zc
);
4002 if (!error
|| ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) &&
4003 errno
!= EPERM
&& errno
!= EACCES
)) {
4004 if (policy
.zlp_rewind
&
4005 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
4006 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nvi
);
4007 zpool_rewind_exclaim(hdl
, zc
.zc_name
,
4008 ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) != 0),
4012 zcmd_free_nvlists(&zc
);
4016 zcmd_free_nvlists(&zc
);
4017 return (zpool_standard_error(hdl
, errno
, msg
));
4021 * Similar to zpool_clear(), but takes a GUID (used by fmd).
4024 zpool_vdev_clear(zpool_handle_t
*zhp
, uint64_t guid
)
4026 zfs_cmd_t zc
= {"\0"};
4028 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4030 (void) snprintf(msg
, sizeof (msg
),
4031 dgettext(TEXT_DOMAIN
, "cannot clear errors for %llx"),
4032 (u_longlong_t
)guid
);
4034 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4036 zc
.zc_cookie
= ZPOOL_NO_REWIND
;
4038 if (zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
) == 0)
4041 return (zpool_standard_error(hdl
, errno
, msg
));
4045 * Change the GUID for a pool.
4048 zpool_reguid(zpool_handle_t
*zhp
)
4051 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4052 zfs_cmd_t zc
= {"\0"};
4054 (void) snprintf(msg
, sizeof (msg
),
4055 dgettext(TEXT_DOMAIN
, "cannot reguid '%s'"), zhp
->zpool_name
);
4057 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4058 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REGUID
, &zc
) == 0)
4061 return (zpool_standard_error(hdl
, errno
, msg
));
4068 zpool_reopen_one(zpool_handle_t
*zhp
, void *data
)
4070 libzfs_handle_t
*hdl
= zpool_get_handle(zhp
);
4071 const char *pool_name
= zpool_get_name(zhp
);
4072 boolean_t
*scrub_restart
= data
;
4075 error
= lzc_reopen(pool_name
, *scrub_restart
);
4077 return (zpool_standard_error_fmt(hdl
, error
,
4078 dgettext(TEXT_DOMAIN
, "cannot reopen '%s'"), pool_name
));
4084 /* call into libzfs_core to execute the sync IOCTL per pool */
4086 zpool_sync_one(zpool_handle_t
*zhp
, void *data
)
4089 libzfs_handle_t
*hdl
= zpool_get_handle(zhp
);
4090 const char *pool_name
= zpool_get_name(zhp
);
4091 boolean_t
*force
= data
;
4092 nvlist_t
*innvl
= fnvlist_alloc();
4094 fnvlist_add_boolean_value(innvl
, "force", *force
);
4095 if ((ret
= lzc_sync(pool_name
, innvl
, NULL
)) != 0) {
4097 return (zpool_standard_error_fmt(hdl
, ret
,
4098 dgettext(TEXT_DOMAIN
, "sync '%s' failed"), pool_name
));
4105 #define PATH_BUF_LEN 64
4108 * Given a vdev, return the name to display in iostat. If the vdev has a path,
4109 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
4110 * We also check if this is a whole disk, in which case we strip off the
4111 * trailing 's0' slice name.
4113 * This routine is also responsible for identifying when disks have been
4114 * reconfigured in a new location. The kernel will have opened the device by
4115 * devid, but the path will still refer to the old location. To catch this, we
4116 * first do a path -> devid translation (which is fast for the common case). If
4117 * the devid matches, we're done. If not, we do a reverse devid -> path
4118 * translation and issue the appropriate ioctl() to update the path of the vdev.
4119 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
4123 zpool_vdev_name(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, nvlist_t
*nv
,
4129 char buf
[PATH_BUF_LEN
];
4130 char tmpbuf
[PATH_BUF_LEN
* 2];
4133 * vdev_name will be "root"/"root-0" for the root vdev, but it is the
4134 * zpool name that will be displayed to the user.
4136 type
= fnvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
);
4137 if (zhp
!= NULL
&& strcmp(type
, "root") == 0)
4138 return (zfs_strdup(hdl
, zpool_get_name(zhp
)));
4140 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_PATH"))
4141 name_flags
|= VDEV_NAME_PATH
;
4142 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_GUID"))
4143 name_flags
|= VDEV_NAME_GUID
;
4144 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_FOLLOW_LINKS"))
4145 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
4147 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
, &value
) == 0 ||
4148 name_flags
& VDEV_NAME_GUID
) {
4149 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
);
4150 (void) snprintf(buf
, sizeof (buf
), "%llu", (u_longlong_t
)value
);
4152 } else if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &tpath
) == 0) {
4155 if (name_flags
& VDEV_NAME_FOLLOW_LINKS
) {
4156 char *rp
= realpath(path
, NULL
);
4158 strlcpy(buf
, rp
, sizeof (buf
));
4165 * For a block device only use the name.
4167 if ((strcmp(type
, VDEV_TYPE_DISK
) == 0) &&
4168 !(name_flags
& VDEV_NAME_PATH
)) {
4169 path
= zfs_strip_path(path
);
4173 * Remove the partition from the path if this is a whole disk.
4175 if (strcmp(type
, VDEV_TYPE_DRAID_SPARE
) != 0 &&
4176 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
, &value
)
4177 == 0 && value
&& !(name_flags
& VDEV_NAME_PATH
)) {
4178 return (zfs_strip_partition(path
));
4184 * If it's a raidz device, we need to stick in the parity level.
4186 if (strcmp(path
, VDEV_TYPE_RAIDZ
) == 0) {
4187 value
= fnvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
);
4188 (void) snprintf(buf
, sizeof (buf
), "%s%llu", path
,
4189 (u_longlong_t
)value
);
4194 * If it's a dRAID device, we add parity, groups, and spares.
4196 if (strcmp(path
, VDEV_TYPE_DRAID
) == 0) {
4197 uint64_t ndata
, nparity
, nspares
;
4201 verify(nvlist_lookup_nvlist_array(nv
,
4202 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) == 0);
4203 nparity
= fnvlist_lookup_uint64(nv
,
4204 ZPOOL_CONFIG_NPARITY
);
4205 ndata
= fnvlist_lookup_uint64(nv
,
4206 ZPOOL_CONFIG_DRAID_NDATA
);
4207 nspares
= fnvlist_lookup_uint64(nv
,
4208 ZPOOL_CONFIG_DRAID_NSPARES
);
4210 path
= zpool_draid_name(buf
, sizeof (buf
), ndata
,
4211 nparity
, nspares
, children
);
4215 * We identify each top-level vdev by using a <type-id>
4216 * naming convention.
4218 if (name_flags
& VDEV_NAME_TYPE_ID
) {
4219 uint64_t id
= fnvlist_lookup_uint64(nv
,
4221 (void) snprintf(tmpbuf
, sizeof (tmpbuf
), "%s-%llu",
4222 path
, (u_longlong_t
)id
);
4227 return (zfs_strdup(hdl
, path
));
4231 zbookmark_mem_compare(const void *a
, const void *b
)
4233 return (memcmp(a
, b
, sizeof (zbookmark_phys_t
)));
4237 * Retrieve the persistent error log, uniquify the members, and return to the
4241 zpool_get_errlog(zpool_handle_t
*zhp
, nvlist_t
**nverrlistp
)
4243 zfs_cmd_t zc
= {"\0"};
4244 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4246 zbookmark_phys_t
*zb
= NULL
;
4250 * Retrieve the raw error list from the kernel. If the number of errors
4251 * has increased, allocate more space and continue until we get the
4254 count
= fnvlist_lookup_uint64(zhp
->zpool_config
, ZPOOL_CONFIG_ERRCOUNT
);
4257 zc
.zc_nvlist_dst
= (uintptr_t)zfs_alloc(zhp
->zpool_hdl
,
4258 count
* sizeof (zbookmark_phys_t
));
4259 zc
.zc_nvlist_dst_size
= count
;
4260 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
4262 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_ERROR_LOG
,
4264 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
4265 if (errno
== ENOMEM
) {
4268 count
= zc
.zc_nvlist_dst_size
;
4269 dst
= zfs_alloc(zhp
->zpool_hdl
, count
*
4270 sizeof (zbookmark_phys_t
));
4271 zc
.zc_nvlist_dst
= (uintptr_t)dst
;
4273 return (zpool_standard_error_fmt(hdl
, errno
,
4274 dgettext(TEXT_DOMAIN
, "errors: List of "
4275 "errors unavailable")));
4283 * Sort the resulting bookmarks. This is a little confusing due to the
4284 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
4285 * to first, and 'zc_nvlist_dst_size' indicates the number of bookmarks
4286 * _not_ copied as part of the process. So we point the start of our
4287 * array appropriate and decrement the total number of elements.
4289 zb
= ((zbookmark_phys_t
*)(uintptr_t)zc
.zc_nvlist_dst
) +
4290 zc
.zc_nvlist_dst_size
;
4291 count
-= zc
.zc_nvlist_dst_size
;
4293 qsort(zb
, count
, sizeof (zbookmark_phys_t
), zbookmark_mem_compare
);
4295 verify(nvlist_alloc(nverrlistp
, 0, KM_SLEEP
) == 0);
4298 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
4300 for (i
= 0; i
< count
; i
++) {
4303 /* ignoring zb_blkid and zb_level for now */
4304 if (i
> 0 && zb
[i
-1].zb_objset
== zb
[i
].zb_objset
&&
4305 zb
[i
-1].zb_object
== zb
[i
].zb_object
)
4308 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) != 0)
4310 if (nvlist_add_uint64(nv
, ZPOOL_ERR_DATASET
,
4311 zb
[i
].zb_objset
) != 0) {
4315 if (nvlist_add_uint64(nv
, ZPOOL_ERR_OBJECT
,
4316 zb
[i
].zb_object
) != 0) {
4320 if (nvlist_add_nvlist(*nverrlistp
, "ejk", nv
) != 0) {
4327 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
4331 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
4332 return (no_memory(zhp
->zpool_hdl
));
4336 * Upgrade a ZFS pool to the latest on-disk version.
4339 zpool_upgrade(zpool_handle_t
*zhp
, uint64_t new_version
)
4341 zfs_cmd_t zc
= {"\0"};
4342 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4344 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
4345 zc
.zc_cookie
= new_version
;
4347 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_UPGRADE
, &zc
) != 0)
4348 return (zpool_standard_error_fmt(hdl
, errno
,
4349 dgettext(TEXT_DOMAIN
, "cannot upgrade '%s'"),
4355 zfs_save_arguments(int argc
, char **argv
, char *string
, int len
)
4359 (void) strlcpy(string
, zfs_basename(argv
[0]), len
);
4360 for (i
= 1; i
< argc
; i
++) {
4361 (void) strlcat(string
, " ", len
);
4362 (void) strlcat(string
, argv
[i
], len
);
4367 zpool_log_history(libzfs_handle_t
*hdl
, const char *message
)
4369 zfs_cmd_t zc
= {"\0"};
4372 args
= fnvlist_alloc();
4373 fnvlist_add_string(args
, "message", message
);
4374 zcmd_write_src_nvlist(hdl
, &zc
, args
);
4375 int err
= zfs_ioctl(hdl
, ZFS_IOC_LOG_HISTORY
, &zc
);
4377 zcmd_free_nvlists(&zc
);
4382 * Perform ioctl to get some command history of a pool.
4384 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
4385 * logical offset of the history buffer to start reading from.
4387 * Upon return, 'off' is the next logical offset to read from and
4388 * 'len' is the actual amount of bytes read into 'buf'.
4391 get_history(zpool_handle_t
*zhp
, char *buf
, uint64_t *off
, uint64_t *len
)
4393 zfs_cmd_t zc
= {"\0"};
4394 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4396 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4398 zc
.zc_history
= (uint64_t)(uintptr_t)buf
;
4399 zc
.zc_history_len
= *len
;
4400 zc
.zc_history_offset
= *off
;
4402 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_GET_HISTORY
, &zc
) != 0) {
4405 return (zfs_error_fmt(hdl
, EZFS_PERM
,
4406 dgettext(TEXT_DOMAIN
,
4407 "cannot show history for pool '%s'"),
4410 return (zfs_error_fmt(hdl
, EZFS_NOHISTORY
,
4411 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
4412 "'%s'"), zhp
->zpool_name
));
4414 return (zfs_error_fmt(hdl
, EZFS_BADVERSION
,
4415 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
4416 "'%s', pool must be upgraded"), zhp
->zpool_name
));
4418 return (zpool_standard_error_fmt(hdl
, errno
,
4419 dgettext(TEXT_DOMAIN
,
4420 "cannot get history for '%s'"), zhp
->zpool_name
));
4424 *len
= zc
.zc_history_len
;
4425 *off
= zc
.zc_history_offset
;
4431 * Retrieve the command history of a pool.
4434 zpool_get_history(zpool_handle_t
*zhp
, nvlist_t
**nvhisp
, uint64_t *off
,
4437 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4439 int buflen
= 128 * 1024;
4440 nvlist_t
**records
= NULL
;
4441 uint_t numrecords
= 0;
4443 uint64_t start
= *off
;
4445 buf
= zfs_alloc(hdl
, buflen
);
4447 /* process about 1MiB a time */
4448 while (*off
- start
< 1024 * 1024) {
4449 uint64_t bytes_read
= buflen
;
4452 if ((err
= get_history(zhp
, buf
, off
, &bytes_read
)) != 0)
4455 /* if nothing else was read in, we're at EOF, just return */
4461 if ((err
= zpool_history_unpack(buf
, bytes_read
,
4462 &leftover
, &records
, &numrecords
)) != 0) {
4463 zpool_standard_error_fmt(hdl
, err
,
4464 dgettext(TEXT_DOMAIN
,
4465 "cannot get history for '%s'"), zhp
->zpool_name
);
4469 if (leftover
== bytes_read
) {
4471 * no progress made, because buffer is not big enough
4472 * to hold this record; resize and retry.
4476 buf
= zfs_alloc(hdl
, buflen
);
4483 *nvhisp
= fnvlist_alloc();
4484 fnvlist_add_nvlist_array(*nvhisp
, ZPOOL_HIST_RECORD
,
4485 (const nvlist_t
**)records
, numrecords
);
4487 for (i
= 0; i
< numrecords
; i
++)
4488 nvlist_free(records
[i
]);
4495 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
4496 * If there is a new event available 'nvp' will contain a newly allocated
4497 * nvlist and 'dropped' will be set to the number of missed events since
4498 * the last call to this function. When 'nvp' is set to NULL it indicates
4499 * no new events are available. In either case the function returns 0 and
4500 * it is up to the caller to free 'nvp'. In the case of a fatal error the
4501 * function will return a non-zero value. When the function is called in
4502 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
4503 * it will not return until a new event is available.
4506 zpool_events_next(libzfs_handle_t
*hdl
, nvlist_t
**nvp
,
4507 int *dropped
, unsigned flags
, int zevent_fd
)
4509 zfs_cmd_t zc
= {"\0"};
4514 zc
.zc_cleanup_fd
= zevent_fd
;
4516 if (flags
& ZEVENT_NONBLOCK
)
4517 zc
.zc_guid
= ZEVENT_NONBLOCK
;
4519 zcmd_alloc_dst_nvlist(hdl
, &zc
, ZEVENT_SIZE
);
4522 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_NEXT
, &zc
) != 0) {
4525 error
= zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
4526 dgettext(TEXT_DOMAIN
, "zfs shutdown"));
4529 /* Blocking error case should not occur */
4530 if (!(flags
& ZEVENT_NONBLOCK
))
4531 error
= zpool_standard_error_fmt(hdl
, errno
,
4532 dgettext(TEXT_DOMAIN
, "cannot get event"));
4536 zcmd_expand_dst_nvlist(hdl
, &zc
);
4539 error
= zpool_standard_error_fmt(hdl
, errno
,
4540 dgettext(TEXT_DOMAIN
, "cannot get event"));
4545 error
= zcmd_read_dst_nvlist(hdl
, &zc
, nvp
);
4549 *dropped
= (int)zc
.zc_cookie
;
4551 zcmd_free_nvlists(&zc
);
4560 zpool_events_clear(libzfs_handle_t
*hdl
, int *count
)
4562 zfs_cmd_t zc
= {"\0"};
4564 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_CLEAR
, &zc
) != 0)
4565 return (zpool_standard_error(hdl
, errno
,
4566 dgettext(TEXT_DOMAIN
, "cannot clear events")));
4569 *count
= (int)zc
.zc_cookie
; /* # of events cleared */
4575 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4576 * the passed zevent_fd file handle. On success zero is returned,
4577 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4580 zpool_events_seek(libzfs_handle_t
*hdl
, uint64_t eid
, int zevent_fd
)
4582 zfs_cmd_t zc
= {"\0"};
4586 zc
.zc_cleanup_fd
= zevent_fd
;
4588 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_SEEK
, &zc
) != 0) {
4591 error
= zfs_error_fmt(hdl
, EZFS_NOENT
,
4592 dgettext(TEXT_DOMAIN
, "cannot get event"));
4596 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
4597 dgettext(TEXT_DOMAIN
, "cannot get event"));
4601 error
= zpool_standard_error_fmt(hdl
, errno
,
4602 dgettext(TEXT_DOMAIN
, "cannot get event"));
4611 zpool_obj_to_path_impl(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4612 char *pathname
, size_t len
, boolean_t always_unmounted
)
4614 zfs_cmd_t zc
= {"\0"};
4615 boolean_t mounted
= B_FALSE
;
4616 char *mntpnt
= NULL
;
4617 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
4620 /* special case for the MOS */
4621 (void) snprintf(pathname
, len
, "<metadata>:<0x%llx>",
4626 /* get the dataset's name */
4627 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4629 if (zfs_ioctl(zhp
->zpool_hdl
,
4630 ZFS_IOC_DSOBJ_TO_DSNAME
, &zc
) != 0) {
4631 /* just write out a path of two object numbers */
4632 (void) snprintf(pathname
, len
, "<0x%llx>:<0x%llx>",
4633 (longlong_t
)dsobj
, (longlong_t
)obj
);
4636 (void) strlcpy(dsname
, zc
.zc_value
, sizeof (dsname
));
4638 /* find out if the dataset is mounted */
4639 mounted
= !always_unmounted
&& is_mounted(zhp
->zpool_hdl
, dsname
,
4642 /* get the corrupted object's path */
4643 (void) strlcpy(zc
.zc_name
, dsname
, sizeof (zc
.zc_name
));
4645 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_OBJ_TO_PATH
,
4648 (void) snprintf(pathname
, len
, "%s%s", mntpnt
,
4651 (void) snprintf(pathname
, len
, "%s:%s",
4652 dsname
, zc
.zc_value
);
4655 (void) snprintf(pathname
, len
, "%s:<0x%llx>", dsname
,
4662 zpool_obj_to_path(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4663 char *pathname
, size_t len
)
4665 zpool_obj_to_path_impl(zhp
, dsobj
, obj
, pathname
, len
, B_FALSE
);
4669 zpool_obj_to_path_ds(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4670 char *pathname
, size_t len
)
4672 zpool_obj_to_path_impl(zhp
, dsobj
, obj
, pathname
, len
, B_TRUE
);
4675 * Wait while the specified activity is in progress in the pool.
4678 zpool_wait(zpool_handle_t
*zhp
, zpool_wait_activity_t activity
)
4682 int error
= zpool_wait_status(zhp
, activity
, &missing
, NULL
);
4685 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, ENOENT
,
4686 dgettext(TEXT_DOMAIN
, "error waiting in pool '%s'"),
4695 * Wait for the given activity and return the status of the wait (whether or not
4696 * any waiting was done) in the 'waited' parameter. Non-existent pools are
4697 * reported via the 'missing' parameter, rather than by printing an error
4698 * message. This is convenient when this function is called in a loop over a
4699 * long period of time (as it is, for example, by zpool's wait cmd). In that
4700 * scenario, a pool being exported or destroyed should be considered a normal
4701 * event, so we don't want to print an error when we find that the pool doesn't
4705 zpool_wait_status(zpool_handle_t
*zhp
, zpool_wait_activity_t activity
,
4706 boolean_t
*missing
, boolean_t
*waited
)
4708 int error
= lzc_wait(zhp
->zpool_name
, activity
, waited
);
4709 *missing
= (error
== ENOENT
);
4714 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
4715 dgettext(TEXT_DOMAIN
, "error waiting in pool '%s'"),
4723 zpool_set_bootenv(zpool_handle_t
*zhp
, const nvlist_t
*envmap
)
4725 int error
= lzc_set_bootenv(zhp
->zpool_name
, envmap
);
4727 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
4728 dgettext(TEXT_DOMAIN
,
4729 "error setting bootenv in pool '%s'"), zhp
->zpool_name
);
4736 zpool_get_bootenv(zpool_handle_t
*zhp
, nvlist_t
**nvlp
)
4742 error
= lzc_get_bootenv(zhp
->zpool_name
, &nvl
);
4744 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
4745 dgettext(TEXT_DOMAIN
,
4746 "error getting bootenv in pool '%s'"), zhp
->zpool_name
);
4755 * Attempt to read and parse feature file(s) (from "compatibility" property).
4756 * Files contain zpool feature names, comma or whitespace-separated.
4757 * Comments (# character to next newline) are discarded.
4760 * compatibility : string containing feature filenames
4761 * features : either NULL or pointer to array of boolean
4762 * report : either NULL or pointer to string buffer
4763 * rlen : length of "report" buffer
4765 * compatibility is NULL (unset), "", "off", "legacy", or list of
4766 * comma-separated filenames. filenames should either be absolute,
4768 * 1) ZPOOL_SYSCONF_COMPAT_D (eg: /etc/zfs/compatibility.d) or
4769 * 2) ZPOOL_DATA_COMPAT_D (eg: /usr/share/zfs/compatibility.d).
4770 * (Unset), "" or "off" => enable all features
4771 * "legacy" => disable all features
4773 * Any feature names read from files which match unames in spa_feature_table
4774 * will have the corresponding boolean set in the features array (if non-NULL).
4775 * If more than one feature set specified, only features present in *all* of
4778 * "report" if not NULL will be populated with a suitable status message.
4781 * ZPOOL_COMPATIBILITY_OK : files read and parsed ok
4782 * ZPOOL_COMPATIBILITY_BADFILE : file too big or not a text file
4783 * ZPOOL_COMPATIBILITY_BADTOKEN : SYSCONF file contains invalid feature name
4784 * ZPOOL_COMPATIBILITY_WARNTOKEN : DATA file contains invalid feature name
4785 * ZPOOL_COMPATIBILITY_NOFILES : no feature files found
4787 zpool_compat_status_t
4788 zpool_load_compat(const char *compat
, boolean_t
*features
, char *report
,
4791 int sdirfd
, ddirfd
, featfd
;
4795 char *file
, *line
, *word
;
4797 char l_compat
[ZFS_MAXPROPLEN
];
4799 boolean_t ret_nofiles
= B_TRUE
;
4800 boolean_t ret_badfile
= B_FALSE
;
4801 boolean_t ret_badtoken
= B_FALSE
;
4802 boolean_t ret_warntoken
= B_FALSE
;
4804 /* special cases (unset), "" and "off" => enable all features */
4805 if (compat
== NULL
|| compat
[0] == '\0' ||
4806 strcmp(compat
, ZPOOL_COMPAT_OFF
) == 0) {
4807 if (features
!= NULL
)
4808 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4809 features
[i
] = B_TRUE
;
4811 strlcpy(report
, gettext("all features enabled"), rlen
);
4812 return (ZPOOL_COMPATIBILITY_OK
);
4815 /* Final special case "legacy" => disable all features */
4816 if (strcmp(compat
, ZPOOL_COMPAT_LEGACY
) == 0) {
4817 if (features
!= NULL
)
4818 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4819 features
[i
] = B_FALSE
;
4821 strlcpy(report
, gettext("all features disabled"), rlen
);
4822 return (ZPOOL_COMPATIBILITY_OK
);
4826 * Start with all true; will be ANDed with results from each file
4828 if (features
!= NULL
)
4829 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4830 features
[i
] = B_TRUE
;
4832 char err_badfile
[1024] = "";
4833 char err_badtoken
[1024] = "";
4836 * We ignore errors from the directory open()
4837 * as they're only needed if the filename is relative
4838 * which will be checked during the openat().
4841 /* O_PATH safer than O_RDONLY if system allows it */
4843 #define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_PATH)
4845 #define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_RDONLY)
4848 sdirfd
= open(ZPOOL_SYSCONF_COMPAT_D
, ZC_DIR_FLAGS
);
4849 ddirfd
= open(ZPOOL_DATA_COMPAT_D
, ZC_DIR_FLAGS
);
4851 (void) strlcpy(l_compat
, compat
, ZFS_MAXPROPLEN
);
4853 for (file
= strtok_r(l_compat
, ",", &ps
);
4855 file
= strtok_r(NULL
, ",", &ps
)) {
4857 boolean_t l_features
[SPA_FEATURES
];
4859 enum { Z_SYSCONF
, Z_DATA
} source
;
4861 /* try sysconfdir first, then datadir */
4863 if ((featfd
= openat(sdirfd
, file
, O_RDONLY
| O_CLOEXEC
)) < 0) {
4864 featfd
= openat(ddirfd
, file
, O_RDONLY
| O_CLOEXEC
);
4868 /* File readable and correct size? */
4870 fstat(featfd
, &fs
) < 0 ||
4872 fs
.st_size
> ZPOOL_COMPAT_MAXSIZE
) {
4873 (void) close(featfd
);
4874 strlcat(err_badfile
, file
, ZFS_MAXPROPLEN
);
4875 strlcat(err_badfile
, " ", ZFS_MAXPROPLEN
);
4876 ret_badfile
= B_TRUE
;
4880 /* Prefault the file if system allows */
4881 #if defined(MAP_POPULATE)
4882 #define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_POPULATE)
4883 #elif defined(MAP_PREFAULT_READ)
4884 #define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_PREFAULT_READ)
4886 #define ZC_MMAP_FLAGS (MAP_PRIVATE)
4889 /* private mmap() so we can strtok safely */
4890 fc
= (char *)mmap(NULL
, fs
.st_size
, PROT_READ
| PROT_WRITE
,
4891 ZC_MMAP_FLAGS
, featfd
, 0);
4892 (void) close(featfd
);
4894 /* map ok, and last character == newline? */
4895 if (fc
== MAP_FAILED
|| fc
[fs
.st_size
- 1] != '\n') {
4896 (void) munmap((void *) fc
, fs
.st_size
);
4897 strlcat(err_badfile
, file
, ZFS_MAXPROPLEN
);
4898 strlcat(err_badfile
, " ", ZFS_MAXPROPLEN
);
4899 ret_badfile
= B_TRUE
;
4903 ret_nofiles
= B_FALSE
;
4905 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4906 l_features
[i
] = B_FALSE
;
4908 /* replace final newline with NULL to ensure string ends */
4909 fc
[fs
.st_size
- 1] = '\0';
4911 for (line
= strtok_r(fc
, "\n", &ls
);
4913 line
= strtok_r(NULL
, "\n", &ls
)) {
4914 /* discard comments */
4915 char *r
= strchr(line
, '#');
4919 for (word
= strtok_r(line
, ", \t", &ws
);
4921 word
= strtok_r(NULL
, ", \t", &ws
)) {
4922 /* Find matching feature name */
4924 for (f
= 0; f
< SPA_FEATURES
; f
++) {
4925 zfeature_info_t
*fi
=
4926 &spa_feature_table
[f
];
4927 if (strcmp(word
, fi
->fi_uname
) == 0) {
4928 l_features
[f
] = B_TRUE
;
4932 if (f
< SPA_FEATURES
)
4935 /* found an unrecognized word */
4936 /* lightly sanitize it */
4937 if (strlen(word
) > 32)
4939 for (char *c
= word
; *c
!= '\0'; c
++)
4943 strlcat(err_badtoken
, word
, ZFS_MAXPROPLEN
);
4944 strlcat(err_badtoken
, " ", ZFS_MAXPROPLEN
);
4945 if (source
== Z_SYSCONF
)
4946 ret_badtoken
= B_TRUE
;
4948 ret_warntoken
= B_TRUE
;
4951 (void) munmap((void *) fc
, fs
.st_size
);
4953 if (features
!= NULL
)
4954 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4955 features
[i
] &= l_features
[i
];
4957 (void) close(sdirfd
);
4958 (void) close(ddirfd
);
4960 /* Return the most serious error */
4963 snprintf(report
, rlen
, gettext("could not read/"
4964 "parse feature file(s): %s"), err_badfile
);
4965 return (ZPOOL_COMPATIBILITY_BADFILE
);
4970 gettext("no valid compatibility files specified"),
4972 return (ZPOOL_COMPATIBILITY_NOFILES
);
4976 snprintf(report
, rlen
, gettext("invalid feature "
4977 "name(s) in local compatibility files: %s"),
4979 return (ZPOOL_COMPATIBILITY_BADTOKEN
);
4981 if (ret_warntoken
) {
4983 snprintf(report
, rlen
, gettext("unrecognized feature "
4984 "name(s) in distribution compatibility files: %s"),
4986 return (ZPOOL_COMPATIBILITY_WARNTOKEN
);
4989 strlcpy(report
, gettext("compatibility set ok"), rlen
);
4990 return (ZPOOL_COMPATIBILITY_OK
);
4994 zpool_vdev_guid(zpool_handle_t
*zhp
, const char *vdevname
, uint64_t *vdev_guid
)
4997 boolean_t avail_spare
, l2cache
;
4999 verify(zhp
!= NULL
);
5000 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
5001 char errbuf
[ERRBUFLEN
];
5002 (void) snprintf(errbuf
, sizeof (errbuf
),
5003 dgettext(TEXT_DOMAIN
, "pool is in an unavailable state"));
5004 return (zfs_error(zhp
->zpool_hdl
, EZFS_POOLUNAVAIL
, errbuf
));
5007 if ((tgt
= zpool_find_vdev(zhp
, vdevname
, &avail_spare
, &l2cache
,
5009 char errbuf
[ERRBUFLEN
];
5010 (void) snprintf(errbuf
, sizeof (errbuf
),
5011 dgettext(TEXT_DOMAIN
, "can not find %s in %s"),
5012 vdevname
, zhp
->zpool_name
);
5013 return (zfs_error(zhp
->zpool_hdl
, EZFS_NODEVICE
, errbuf
));
5016 *vdev_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
5021 * Get a vdev property value for 'prop' and return the value in
5022 * a pre-allocated buffer.
5025 zpool_get_vdev_prop_value(nvlist_t
*nvprop
, vdev_prop_t prop
, char *prop_name
,
5026 char *buf
, size_t len
, zprop_source_t
*srctype
, boolean_t literal
)
5031 zprop_source_t src
= ZPROP_SRC_NONE
;
5033 if (prop
== VDEV_PROP_USER
) {
5034 /* user property, prop_name must contain the property name */
5035 assert(prop_name
!= NULL
);
5036 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
5037 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
5038 strval
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
5040 /* user prop not found */
5043 (void) strlcpy(buf
, strval
, len
);
5049 if (prop_name
== NULL
)
5050 prop_name
= (char *)vdev_prop_to_name(prop
);
5052 switch (vdev_prop_get_type(prop
)) {
5053 case PROP_TYPE_STRING
:
5054 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
5055 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
5056 strval
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
5058 src
= ZPROP_SRC_DEFAULT
;
5059 if ((strval
= (char *)vdev_prop_default_string(prop
))
5063 (void) strlcpy(buf
, strval
, len
);
5066 case PROP_TYPE_NUMBER
:
5067 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
5068 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
5069 intval
= fnvlist_lookup_uint64(nv
, ZPROP_VALUE
);
5071 src
= ZPROP_SRC_DEFAULT
;
5072 intval
= vdev_prop_default_numeric(prop
);
5076 case VDEV_PROP_ASIZE
:
5077 case VDEV_PROP_PSIZE
:
5078 case VDEV_PROP_SIZE
:
5079 case VDEV_PROP_BOOTSIZE
:
5080 case VDEV_PROP_ALLOCATED
:
5081 case VDEV_PROP_FREE
:
5082 case VDEV_PROP_READ_ERRORS
:
5083 case VDEV_PROP_WRITE_ERRORS
:
5084 case VDEV_PROP_CHECKSUM_ERRORS
:
5085 case VDEV_PROP_INITIALIZE_ERRORS
:
5086 case VDEV_PROP_OPS_NULL
:
5087 case VDEV_PROP_OPS_READ
:
5088 case VDEV_PROP_OPS_WRITE
:
5089 case VDEV_PROP_OPS_FREE
:
5090 case VDEV_PROP_OPS_CLAIM
:
5091 case VDEV_PROP_OPS_TRIM
:
5092 case VDEV_PROP_BYTES_NULL
:
5093 case VDEV_PROP_BYTES_READ
:
5094 case VDEV_PROP_BYTES_WRITE
:
5095 case VDEV_PROP_BYTES_FREE
:
5096 case VDEV_PROP_BYTES_CLAIM
:
5097 case VDEV_PROP_BYTES_TRIM
:
5099 (void) snprintf(buf
, len
, "%llu",
5100 (u_longlong_t
)intval
);
5102 (void) zfs_nicenum(intval
, buf
, len
);
5105 case VDEV_PROP_EXPANDSZ
:
5107 (void) strlcpy(buf
, "-", len
);
5108 } else if (literal
) {
5109 (void) snprintf(buf
, len
, "%llu",
5110 (u_longlong_t
)intval
);
5112 (void) zfs_nicenum(intval
, buf
, len
);
5115 case VDEV_PROP_CAPACITY
:
5117 (void) snprintf(buf
, len
, "%llu",
5118 (u_longlong_t
)intval
);
5120 (void) snprintf(buf
, len
, "%llu%%",
5121 (u_longlong_t
)intval
);
5124 case VDEV_PROP_FRAGMENTATION
:
5125 if (intval
== UINT64_MAX
) {
5126 (void) strlcpy(buf
, "-", len
);
5128 (void) snprintf(buf
, len
, "%llu%%",
5129 (u_longlong_t
)intval
);
5132 case VDEV_PROP_STATE
:
5134 (void) snprintf(buf
, len
, "%llu",
5135 (u_longlong_t
)intval
);
5137 (void) strlcpy(buf
, zpool_state_to_name(intval
,
5138 VDEV_AUX_NONE
), len
);
5142 (void) snprintf(buf
, len
, "%llu",
5143 (u_longlong_t
)intval
);
5147 case PROP_TYPE_INDEX
:
5148 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
5149 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
5150 intval
= fnvlist_lookup_uint64(nv
, ZPROP_VALUE
);
5152 src
= ZPROP_SRC_DEFAULT
;
5153 intval
= vdev_prop_default_numeric(prop
);
5155 if (vdev_prop_index_to_string(prop
, intval
,
5156 (const char **)&strval
) != 0)
5158 (void) strlcpy(buf
, strval
, len
);
5172 * Get a vdev property value for 'prop_name' and return the value in
5173 * a pre-allocated buffer.
5176 zpool_get_vdev_prop(zpool_handle_t
*zhp
, const char *vdevname
, vdev_prop_t prop
,
5177 char *prop_name
, char *buf
, size_t len
, zprop_source_t
*srctype
,
5180 nvlist_t
*reqnvl
, *reqprops
;
5181 nvlist_t
*retprops
= NULL
;
5182 uint64_t vdev_guid
= 0;
5185 if ((ret
= zpool_vdev_guid(zhp
, vdevname
, &vdev_guid
)) != 0)
5188 if (nvlist_alloc(&reqnvl
, NV_UNIQUE_NAME
, 0) != 0)
5189 return (no_memory(zhp
->zpool_hdl
));
5190 if (nvlist_alloc(&reqprops
, NV_UNIQUE_NAME
, 0) != 0)
5191 return (no_memory(zhp
->zpool_hdl
));
5193 fnvlist_add_uint64(reqnvl
, ZPOOL_VDEV_PROPS_GET_VDEV
, vdev_guid
);
5195 if (prop
!= VDEV_PROP_USER
) {
5196 /* prop_name overrides prop value */
5197 if (prop_name
!= NULL
)
5198 prop
= vdev_name_to_prop(prop_name
);
5200 prop_name
= (char *)vdev_prop_to_name(prop
);
5201 assert(prop
< VDEV_NUM_PROPS
);
5204 assert(prop_name
!= NULL
);
5205 if (nvlist_add_uint64(reqprops
, prop_name
, prop
) != 0) {
5206 nvlist_free(reqnvl
);
5207 nvlist_free(reqprops
);
5208 return (no_memory(zhp
->zpool_hdl
));
5211 fnvlist_add_nvlist(reqnvl
, ZPOOL_VDEV_PROPS_GET_PROPS
, reqprops
);
5213 ret
= lzc_get_vdev_prop(zhp
->zpool_name
, reqnvl
, &retprops
);
5216 ret
= zpool_get_vdev_prop_value(retprops
, prop
, prop_name
, buf
,
5217 len
, srctype
, literal
);
5219 char errbuf
[ERRBUFLEN
];
5220 (void) snprintf(errbuf
, sizeof (errbuf
),
5221 dgettext(TEXT_DOMAIN
, "cannot get vdev property %s from"
5222 " %s in %s"), prop_name
, vdevname
, zhp
->zpool_name
);
5223 (void) zpool_standard_error(zhp
->zpool_hdl
, ret
, errbuf
);
5226 nvlist_free(reqnvl
);
5227 nvlist_free(reqprops
);
5228 nvlist_free(retprops
);
5234 * Get all vdev properties
5237 zpool_get_all_vdev_props(zpool_handle_t
*zhp
, const char *vdevname
,
5240 nvlist_t
*nvl
= NULL
;
5241 uint64_t vdev_guid
= 0;
5244 if ((ret
= zpool_vdev_guid(zhp
, vdevname
, &vdev_guid
)) != 0)
5247 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5248 return (no_memory(zhp
->zpool_hdl
));
5250 fnvlist_add_uint64(nvl
, ZPOOL_VDEV_PROPS_GET_VDEV
, vdev_guid
);
5252 ret
= lzc_get_vdev_prop(zhp
->zpool_name
, nvl
, outnvl
);
5257 char errbuf
[ERRBUFLEN
];
5258 (void) snprintf(errbuf
, sizeof (errbuf
),
5259 dgettext(TEXT_DOMAIN
, "cannot get vdev properties for"
5260 " %s in %s"), vdevname
, zhp
->zpool_name
);
5261 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
5271 zpool_set_vdev_prop(zpool_handle_t
*zhp
, const char *vdevname
,
5272 const char *propname
, const char *propval
)
5275 nvlist_t
*nvl
= NULL
;
5276 nvlist_t
*outnvl
= NULL
;
5278 nvlist_t
*realprops
;
5279 prop_flags_t flags
= { 0 };
5283 if ((ret
= zpool_vdev_guid(zhp
, vdevname
, &vdev_guid
)) != 0)
5286 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5287 return (no_memory(zhp
->zpool_hdl
));
5288 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
5289 return (no_memory(zhp
->zpool_hdl
));
5291 fnvlist_add_uint64(nvl
, ZPOOL_VDEV_PROPS_SET_VDEV
, vdev_guid
);
5293 if (nvlist_add_string(props
, propname
, propval
) != 0) {
5295 return (no_memory(zhp
->zpool_hdl
));
5298 char errbuf
[ERRBUFLEN
];
5299 (void) snprintf(errbuf
, sizeof (errbuf
),
5300 dgettext(TEXT_DOMAIN
, "cannot set property %s for %s on %s"),
5301 propname
, vdevname
, zhp
->zpool_name
);
5304 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
5305 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
5306 zhp
->zpool_name
, props
, version
, flags
, errbuf
)) == NULL
) {
5315 fnvlist_add_nvlist(nvl
, ZPOOL_VDEV_PROPS_SET_PROPS
, props
);
5317 ret
= lzc_set_vdev_prop(zhp
->zpool_name
, nvl
, &outnvl
);
5321 nvlist_free(outnvl
);
5324 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);