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 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
85 while (zfs_ioctl(hdl
, ZFS_IOC_POOL_GET_PROPS
, &zc
) != 0) {
86 if (errno
== ENOMEM
) {
87 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
88 zcmd_free_nvlists(&zc
);
92 zcmd_free_nvlists(&zc
);
97 if (zcmd_read_dst_nvlist(hdl
, &zc
, &zhp
->zpool_props
) != 0) {
98 zcmd_free_nvlists(&zc
);
102 zcmd_free_nvlists(&zc
);
108 zpool_props_refresh(zpool_handle_t
*zhp
)
112 old_props
= zhp
->zpool_props
;
114 if (zpool_get_all_props(zhp
) != 0)
117 nvlist_free(old_props
);
122 zpool_get_prop_string(zpool_handle_t
*zhp
, zpool_prop_t prop
,
127 zprop_source_t source
;
129 nvl
= zhp
->zpool_props
;
130 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
131 source
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
132 value
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
134 source
= ZPROP_SRC_DEFAULT
;
135 if ((value
= (char *)zpool_prop_default_string(prop
)) == NULL
)
146 zpool_get_prop_int(zpool_handle_t
*zhp
, zpool_prop_t prop
, zprop_source_t
*src
)
150 zprop_source_t source
;
152 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
)) {
154 * zpool_get_all_props() has most likely failed because
155 * the pool is faulted, but if all we need is the top level
156 * vdev's guid then get it from the zhp config nvlist.
158 if ((prop
== ZPOOL_PROP_GUID
) &&
159 (nvlist_lookup_nvlist(zhp
->zpool_config
,
160 ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0) &&
161 (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
)
165 return (zpool_prop_default_numeric(prop
));
168 nvl
= zhp
->zpool_props
;
169 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
170 source
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
171 value
= fnvlist_lookup_uint64(nv
, ZPROP_VALUE
);
173 source
= ZPROP_SRC_DEFAULT
;
174 value
= zpool_prop_default_numeric(prop
);
184 * Map VDEV STATE to printed strings.
187 zpool_state_to_name(vdev_state_t state
, vdev_aux_t aux
)
190 case VDEV_STATE_CLOSED
:
191 case VDEV_STATE_OFFLINE
:
192 return (gettext("OFFLINE"));
193 case VDEV_STATE_REMOVED
:
194 return (gettext("REMOVED"));
195 case VDEV_STATE_CANT_OPEN
:
196 if (aux
== VDEV_AUX_CORRUPT_DATA
|| aux
== VDEV_AUX_BAD_LOG
)
197 return (gettext("FAULTED"));
198 else if (aux
== VDEV_AUX_SPLIT_POOL
)
199 return (gettext("SPLIT"));
201 return (gettext("UNAVAIL"));
202 case VDEV_STATE_FAULTED
:
203 return (gettext("FAULTED"));
204 case VDEV_STATE_DEGRADED
:
205 return (gettext("DEGRADED"));
206 case VDEV_STATE_HEALTHY
:
207 return (gettext("ONLINE"));
213 return (gettext("UNKNOWN"));
217 * Map POOL STATE to printed strings.
220 zpool_pool_state_to_name(pool_state_t state
)
225 case POOL_STATE_ACTIVE
:
226 return (gettext("ACTIVE"));
227 case POOL_STATE_EXPORTED
:
228 return (gettext("EXPORTED"));
229 case POOL_STATE_DESTROYED
:
230 return (gettext("DESTROYED"));
231 case POOL_STATE_SPARE
:
232 return (gettext("SPARE"));
233 case POOL_STATE_L2CACHE
:
234 return (gettext("L2CACHE"));
235 case POOL_STATE_UNINITIALIZED
:
236 return (gettext("UNINITIALIZED"));
237 case POOL_STATE_UNAVAIL
:
238 return (gettext("UNAVAIL"));
239 case POOL_STATE_POTENTIALLY_ACTIVE
:
240 return (gettext("POTENTIALLY_ACTIVE"));
243 return (gettext("UNKNOWN"));
247 * Given a pool handle, return the pool health string ("ONLINE", "DEGRADED",
251 zpool_get_state_str(zpool_handle_t
*zhp
)
253 zpool_errata_t errata
;
254 zpool_status_t status
;
257 status
= zpool_get_status(zhp
, NULL
, &errata
);
259 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
260 str
= gettext("FAULTED");
261 } else if (status
== ZPOOL_STATUS_IO_FAILURE_WAIT
||
262 status
== ZPOOL_STATUS_IO_FAILURE_MMP
) {
263 str
= gettext("SUSPENDED");
265 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(
266 zpool_get_config(zhp
, NULL
), ZPOOL_CONFIG_VDEV_TREE
);
268 vdev_stat_t
*vs
= (vdev_stat_t
*)fnvlist_lookup_uint64_array(
269 nvroot
, ZPOOL_CONFIG_VDEV_STATS
, &vsc
);
270 str
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
276 * Get a zpool property value for 'prop' and return the value in
277 * a pre-allocated buffer.
280 zpool_get_prop(zpool_handle_t
*zhp
, zpool_prop_t prop
, char *buf
,
281 size_t len
, zprop_source_t
*srctype
, boolean_t literal
)
285 zprop_source_t src
= ZPROP_SRC_NONE
;
287 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
289 case ZPOOL_PROP_NAME
:
290 (void) strlcpy(buf
, zpool_get_name(zhp
), len
);
293 case ZPOOL_PROP_HEALTH
:
294 (void) strlcpy(buf
, zpool_get_state_str(zhp
), len
);
297 case ZPOOL_PROP_GUID
:
298 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
299 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
302 case ZPOOL_PROP_ALTROOT
:
303 case ZPOOL_PROP_CACHEFILE
:
304 case ZPOOL_PROP_COMMENT
:
305 case ZPOOL_PROP_COMPATIBILITY
:
306 if (zhp
->zpool_props
!= NULL
||
307 zpool_get_all_props(zhp
) == 0) {
309 zpool_get_prop_string(zhp
, prop
, &src
),
315 (void) strlcpy(buf
, "-", len
);
324 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
) &&
325 prop
!= ZPOOL_PROP_NAME
)
328 switch (zpool_prop_get_type(prop
)) {
329 case PROP_TYPE_STRING
:
330 (void) strlcpy(buf
, zpool_get_prop_string(zhp
, prop
, &src
),
334 case PROP_TYPE_NUMBER
:
335 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
338 case ZPOOL_PROP_SIZE
:
339 case ZPOOL_PROP_ALLOCATED
:
340 case ZPOOL_PROP_FREE
:
341 case ZPOOL_PROP_FREEING
:
342 case ZPOOL_PROP_LEAKED
:
343 case ZPOOL_PROP_ASHIFT
:
344 case ZPOOL_PROP_MAXBLOCKSIZE
:
345 case ZPOOL_PROP_MAXDNODESIZE
:
347 (void) snprintf(buf
, len
, "%llu",
348 (u_longlong_t
)intval
);
350 (void) zfs_nicenum(intval
, buf
, len
);
353 case ZPOOL_PROP_EXPANDSZ
:
354 case ZPOOL_PROP_CHECKPOINT
:
356 (void) strlcpy(buf
, "-", len
);
357 } else if (literal
) {
358 (void) snprintf(buf
, len
, "%llu",
359 (u_longlong_t
)intval
);
361 (void) zfs_nicebytes(intval
, buf
, len
);
365 case ZPOOL_PROP_CAPACITY
:
367 (void) snprintf(buf
, len
, "%llu",
368 (u_longlong_t
)intval
);
370 (void) snprintf(buf
, len
, "%llu%%",
371 (u_longlong_t
)intval
);
375 case ZPOOL_PROP_FRAGMENTATION
:
376 if (intval
== UINT64_MAX
) {
377 (void) strlcpy(buf
, "-", len
);
378 } else if (literal
) {
379 (void) snprintf(buf
, len
, "%llu",
380 (u_longlong_t
)intval
);
382 (void) snprintf(buf
, len
, "%llu%%",
383 (u_longlong_t
)intval
);
387 case ZPOOL_PROP_DEDUPRATIO
:
389 (void) snprintf(buf
, len
, "%llu.%02llu",
390 (u_longlong_t
)(intval
/ 100),
391 (u_longlong_t
)(intval
% 100));
393 (void) snprintf(buf
, len
, "%llu.%02llux",
394 (u_longlong_t
)(intval
/ 100),
395 (u_longlong_t
)(intval
% 100));
398 case ZPOOL_PROP_HEALTH
:
399 (void) strlcpy(buf
, zpool_get_state_str(zhp
), len
);
401 case ZPOOL_PROP_VERSION
:
402 if (intval
>= SPA_VERSION_FEATURES
) {
403 (void) snprintf(buf
, len
, "-");
408 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
412 case PROP_TYPE_INDEX
:
413 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
414 if (zpool_prop_index_to_string(prop
, intval
, &strval
)
417 (void) strlcpy(buf
, strval
, len
);
431 * Check if the bootfs name has the same pool name as it is set to.
432 * Assuming bootfs is a valid dataset name.
435 bootfs_name_valid(const char *pool
, const char *bootfs
)
437 int len
= strlen(pool
);
438 if (bootfs
[0] == '\0')
441 if (!zfs_name_valid(bootfs
, ZFS_TYPE_FILESYSTEM
|ZFS_TYPE_SNAPSHOT
))
444 if (strncmp(pool
, bootfs
, len
) == 0 &&
445 (bootfs
[len
] == '/' || bootfs
[len
] == '\0'))
452 * Given an nvlist of zpool properties to be set, validate that they are
453 * correct, and parse any numeric properties (index, boolean, etc) if they are
454 * specified as strings.
457 zpool_valid_proplist(libzfs_handle_t
*hdl
, const char *poolname
,
458 nvlist_t
*props
, uint64_t version
, prop_flags_t flags
, char *errbuf
)
466 struct stat64 statbuf
;
470 if (nvlist_alloc(&retprops
, NV_UNIQUE_NAME
, 0) != 0) {
471 (void) no_memory(hdl
);
476 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
477 const char *propname
= nvpair_name(elem
);
479 if (flags
.vdevprop
&& zpool_prop_vdev(propname
)) {
480 vdev_prop_t vprop
= vdev_name_to_prop(propname
);
482 if (vdev_prop_readonly(vprop
)) {
483 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
484 "is readonly"), propname
);
485 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
490 if (zprop_parse_value(hdl
, elem
, vprop
, ZFS_TYPE_VDEV
,
491 retprops
, &strval
, &intval
, errbuf
) != 0)
495 } else if (flags
.vdevprop
&& vdev_prop_user(propname
)) {
496 if (nvlist_add_nvpair(retprops
, elem
) != 0) {
497 (void) no_memory(hdl
);
501 } else if (flags
.vdevprop
) {
502 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
503 "invalid property: '%s'"), propname
);
504 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
508 prop
= zpool_name_to_prop(propname
);
509 if (prop
== ZPOOL_PROP_INVAL
&& zpool_prop_feature(propname
)) {
511 char *fname
= strchr(propname
, '@') + 1;
513 err
= zfeature_lookup_name(fname
, NULL
);
515 ASSERT3U(err
, ==, ENOENT
);
516 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
517 "feature '%s' unsupported by kernel"),
519 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
523 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
524 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
525 "'%s' must be a string"), propname
);
526 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
530 (void) nvpair_value_string(elem
, &strval
);
531 if (strcmp(strval
, ZFS_FEATURE_ENABLED
) != 0 &&
532 strcmp(strval
, ZFS_FEATURE_DISABLED
) != 0) {
533 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
534 "property '%s' can only be set to "
535 "'enabled' or 'disabled'"), propname
);
536 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
541 strcmp(strval
, ZFS_FEATURE_DISABLED
) == 0) {
542 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
543 "property '%s' can only be set to "
544 "'disabled' at creation time"), propname
);
545 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
549 if (nvlist_add_uint64(retprops
, propname
, 0) != 0) {
550 (void) no_memory(hdl
);
557 * Make sure this property is valid and applies to this type.
559 if (prop
== ZPOOL_PROP_INVAL
) {
560 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
561 "invalid property '%s'"), propname
);
562 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
566 if (zpool_prop_readonly(prop
)) {
567 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
568 "is readonly"), propname
);
569 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
573 if (!flags
.create
&& zpool_prop_setonce(prop
)) {
574 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
575 "property '%s' can only be set at "
576 "creation time"), propname
);
577 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
581 if (zprop_parse_value(hdl
, elem
, prop
, ZFS_TYPE_POOL
, retprops
,
582 &strval
, &intval
, errbuf
) != 0)
586 * Perform additional checking for specific properties.
589 case ZPOOL_PROP_VERSION
:
590 if (intval
< version
||
591 !SPA_VERSION_IS_SUPPORTED(intval
)) {
592 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
593 "property '%s' number %llu is invalid."),
594 propname
, (unsigned long long)intval
);
595 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
600 case ZPOOL_PROP_ASHIFT
:
602 (intval
< ASHIFT_MIN
|| intval
> ASHIFT_MAX
)) {
603 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
604 "property '%s' number %llu is invalid, "
605 "only values between %" PRId32
" and %"
606 PRId32
" are allowed."),
607 propname
, (unsigned long long)intval
,
608 ASHIFT_MIN
, ASHIFT_MAX
);
609 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
614 case ZPOOL_PROP_BOOTFS
:
615 if (flags
.create
|| flags
.import
) {
616 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
617 "property '%s' cannot be set at creation "
618 "or import time"), propname
);
619 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
623 if (version
< SPA_VERSION_BOOTFS
) {
624 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
625 "pool must be upgraded to support "
626 "'%s' property"), propname
);
627 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
632 * bootfs property value has to be a dataset name and
633 * the dataset has to be in the same pool as it sets to.
635 if (!bootfs_name_valid(poolname
, strval
)) {
636 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
637 "is an invalid name"), strval
);
638 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
642 if ((zhp
= zpool_open_canfail(hdl
, poolname
)) == NULL
) {
643 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
644 "could not open pool '%s'"), poolname
);
645 (void) zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
);
651 case ZPOOL_PROP_ALTROOT
:
652 if (!flags
.create
&& !flags
.import
) {
653 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
654 "property '%s' can only be set during pool "
655 "creation or import"), propname
);
656 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
660 if (strval
[0] != '/') {
661 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
662 "bad alternate root '%s'"), strval
);
663 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
668 case ZPOOL_PROP_CACHEFILE
:
669 if (strval
[0] == '\0')
672 if (strcmp(strval
, "none") == 0)
675 if (strval
[0] != '/') {
676 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
677 "property '%s' must be empty, an "
678 "absolute path, or 'none'"), propname
);
679 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
683 slash
= strrchr(strval
, '/');
685 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
686 strcmp(slash
, "/..") == 0) {
687 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
688 "'%s' is not a valid file"), strval
);
689 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
695 if (strval
[0] != '\0' &&
696 (stat64(strval
, &statbuf
) != 0 ||
697 !S_ISDIR(statbuf
.st_mode
))) {
698 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
699 "'%s' is not a valid directory"),
701 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
708 case ZPOOL_PROP_COMPATIBILITY
:
709 switch (zpool_load_compat(strval
, NULL
, report
, 1024)) {
710 case ZPOOL_COMPATIBILITY_OK
:
711 case ZPOOL_COMPATIBILITY_WARNTOKEN
:
713 case ZPOOL_COMPATIBILITY_BADFILE
:
714 case ZPOOL_COMPATIBILITY_BADTOKEN
:
715 case ZPOOL_COMPATIBILITY_NOFILES
:
716 zfs_error_aux(hdl
, "%s", report
);
717 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
722 case ZPOOL_PROP_COMMENT
:
723 for (check
= strval
; *check
!= '\0'; check
++) {
724 if (!isprint(*check
)) {
726 dgettext(TEXT_DOMAIN
,
727 "comment may only have printable "
729 (void) zfs_error(hdl
, EZFS_BADPROP
,
734 if (strlen(strval
) > ZPROP_MAX_COMMENT
) {
735 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
736 "comment must not exceed %d characters"),
738 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
742 case ZPOOL_PROP_READONLY
:
744 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
745 "property '%s' can only be set at "
746 "import time"), propname
);
747 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
751 case ZPOOL_PROP_MULTIHOST
:
752 if (get_system_hostid() == 0) {
753 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
754 "requires a non-zero system hostid"));
755 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
759 case ZPOOL_PROP_DEDUPDITTO
:
760 printf("Note: property '%s' no longer has "
761 "any effect\n", propname
);
771 nvlist_free(retprops
);
776 * Set zpool property : propname=propval.
779 zpool_set_prop(zpool_handle_t
*zhp
, const char *propname
, const char *propval
)
781 zfs_cmd_t zc
= {"\0"};
784 nvlist_t
*nvl
= NULL
;
787 prop_flags_t flags
= { 0 };
789 (void) snprintf(errbuf
, sizeof (errbuf
),
790 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
793 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
794 return (no_memory(zhp
->zpool_hdl
));
796 if (nvlist_add_string(nvl
, propname
, propval
) != 0) {
798 return (no_memory(zhp
->zpool_hdl
));
801 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
802 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
803 zhp
->zpool_name
, nvl
, version
, flags
, errbuf
)) == NULL
) {
812 * Execute the corresponding ioctl() to set this property.
814 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
816 if (zcmd_write_src_nvlist(zhp
->zpool_hdl
, &zc
, nvl
) != 0) {
821 ret
= zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_SET_PROPS
, &zc
);
823 zcmd_free_nvlists(&zc
);
827 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
829 (void) zpool_props_refresh(zhp
);
835 zpool_expand_proplist(zpool_handle_t
*zhp
, zprop_list_t
**plp
,
836 zfs_type_t type
, boolean_t literal
)
838 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
840 char buf
[ZFS_MAXPROPLEN
];
841 nvlist_t
*features
= NULL
;
844 boolean_t firstexpand
= (NULL
== *plp
);
847 if (zprop_expand_list(hdl
, plp
, type
) != 0)
850 if (type
== ZFS_TYPE_VDEV
)
854 while (*last
!= NULL
)
855 last
= &(*last
)->pl_next
;
858 features
= zpool_get_features(zhp
);
860 if ((*plp
)->pl_all
&& firstexpand
) {
861 for (i
= 0; i
< SPA_FEATURES
; i
++) {
862 zprop_list_t
*entry
= zfs_alloc(hdl
,
863 sizeof (zprop_list_t
));
864 entry
->pl_prop
= ZPROP_INVAL
;
865 entry
->pl_user_prop
= zfs_asprintf(hdl
, "feature@%s",
866 spa_feature_table
[i
].fi_uname
);
867 entry
->pl_width
= strlen(entry
->pl_user_prop
);
868 entry
->pl_all
= B_TRUE
;
871 last
= &entry
->pl_next
;
875 /* add any unsupported features */
876 for (nvp
= nvlist_next_nvpair(features
, NULL
);
877 nvp
!= NULL
; nvp
= nvlist_next_nvpair(features
, nvp
)) {
882 if (zfeature_is_supported(nvpair_name(nvp
)))
885 propname
= zfs_asprintf(hdl
, "unsupported@%s",
889 * Before adding the property to the list make sure that no
890 * other pool already added the same property.
894 while (entry
!= NULL
) {
895 if (entry
->pl_user_prop
!= NULL
&&
896 strcmp(propname
, entry
->pl_user_prop
) == 0) {
900 entry
= entry
->pl_next
;
907 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
908 entry
->pl_prop
= ZPROP_INVAL
;
909 entry
->pl_user_prop
= propname
;
910 entry
->pl_width
= strlen(entry
->pl_user_prop
);
911 entry
->pl_all
= B_TRUE
;
914 last
= &entry
->pl_next
;
917 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
918 if (entry
->pl_fixed
&& !literal
)
921 if (entry
->pl_prop
!= ZPROP_INVAL
&&
922 zpool_get_prop(zhp
, entry
->pl_prop
, buf
, sizeof (buf
),
923 NULL
, literal
) == 0) {
924 if (strlen(buf
) > entry
->pl_width
)
925 entry
->pl_width
= strlen(buf
);
933 vdev_expand_proplist(zpool_handle_t
*zhp
, const char *vdevname
,
937 char buf
[ZFS_MAXPROPLEN
];
940 nvpair_t
*elem
= NULL
;
941 nvlist_t
*vprops
= NULL
;
942 nvlist_t
*propval
= NULL
;
943 const char *propname
;
947 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
951 if (zpool_get_vdev_prop(zhp
, vdevname
, entry
->pl_prop
,
952 entry
->pl_user_prop
, buf
, sizeof (buf
), NULL
,
954 if (strlen(buf
) > entry
->pl_width
)
955 entry
->pl_width
= strlen(buf
);
957 if (entry
->pl_prop
== VDEV_PROP_NAME
&&
958 strlen(vdevname
) > entry
->pl_width
)
959 entry
->pl_width
= strlen(vdevname
);
962 /* Handle the all properties case */
964 if (*last
!= NULL
&& (*last
)->pl_all
== B_TRUE
) {
965 while (*last
!= NULL
)
966 last
= &(*last
)->pl_next
;
968 err
= zpool_get_all_vdev_props(zhp
, vdevname
, &vprops
);
972 while ((elem
= nvlist_next_nvpair(vprops
, elem
)) != NULL
) {
973 propname
= nvpair_name(elem
);
975 /* Skip properties that are not user defined */
976 if ((prop
= vdev_name_to_prop(propname
)) !=
980 if (nvpair_value_nvlist(elem
, &propval
) != 0)
983 strval
= fnvlist_lookup_string(propval
, ZPROP_VALUE
);
985 if ((entry
= zfs_alloc(zhp
->zpool_hdl
,
986 sizeof (zprop_list_t
))) == NULL
)
989 entry
->pl_prop
= prop
;
990 entry
->pl_user_prop
= zfs_strdup(zhp
->zpool_hdl
,
992 entry
->pl_width
= strlen(strval
);
993 entry
->pl_all
= B_TRUE
;
995 last
= &entry
->pl_next
;
1003 * Get the state for the given feature on the given ZFS pool.
1006 zpool_prop_get_feature(zpool_handle_t
*zhp
, const char *propname
, char *buf
,
1010 boolean_t found
= B_FALSE
;
1011 nvlist_t
*features
= zpool_get_features(zhp
);
1012 boolean_t supported
;
1013 const char *feature
= strchr(propname
, '@') + 1;
1015 supported
= zpool_prop_feature(propname
);
1016 ASSERT(supported
|| zpool_prop_unsupported(propname
));
1019 * Convert from feature name to feature guid. This conversion is
1020 * unnecessary for unsupported@... properties because they already
1027 ret
= zfeature_lookup_name(feature
, &fid
);
1029 (void) strlcpy(buf
, "-", len
);
1032 feature
= spa_feature_table
[fid
].fi_guid
;
1035 if (nvlist_lookup_uint64(features
, feature
, &refcount
) == 0)
1040 (void) strlcpy(buf
, ZFS_FEATURE_DISABLED
, len
);
1043 (void) strlcpy(buf
, ZFS_FEATURE_ENABLED
, len
);
1045 (void) strlcpy(buf
, ZFS_FEATURE_ACTIVE
, len
);
1049 if (refcount
== 0) {
1050 (void) strcpy(buf
, ZFS_UNSUPPORTED_INACTIVE
);
1052 (void) strcpy(buf
, ZFS_UNSUPPORTED_READONLY
);
1055 (void) strlcpy(buf
, "-", len
);
1064 * Validate the given pool name, optionally putting an extended error message in
1068 zpool_name_valid(libzfs_handle_t
*hdl
, boolean_t isopen
, const char *pool
)
1070 namecheck_err_t why
;
1074 ret
= pool_namecheck(pool
, &why
, &what
);
1077 * The rules for reserved pool names were extended at a later point.
1078 * But we need to support users with existing pools that may now be
1079 * invalid. So we only check for this expanded set of names during a
1080 * create (or import), and only in userland.
1082 if (ret
== 0 && !isopen
&&
1083 (strncmp(pool
, "mirror", 6) == 0 ||
1084 strncmp(pool
, "raidz", 5) == 0 ||
1085 strncmp(pool
, "draid", 5) == 0 ||
1086 strncmp(pool
, "spare", 5) == 0 ||
1087 strcmp(pool
, "log") == 0)) {
1090 dgettext(TEXT_DOMAIN
, "name is reserved"));
1098 case NAME_ERR_TOOLONG
:
1100 dgettext(TEXT_DOMAIN
, "name is too long"));
1103 case NAME_ERR_INVALCHAR
:
1105 dgettext(TEXT_DOMAIN
, "invalid character "
1106 "'%c' in pool name"), what
);
1109 case NAME_ERR_NOLETTER
:
1110 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1111 "name must begin with a letter"));
1114 case NAME_ERR_RESERVED
:
1115 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1116 "name is reserved"));
1119 case NAME_ERR_DISKLIKE
:
1120 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1121 "pool name is reserved"));
1124 case NAME_ERR_LEADING_SLASH
:
1125 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1126 "leading slash in name"));
1129 case NAME_ERR_EMPTY_COMPONENT
:
1130 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1131 "empty component in name"));
1134 case NAME_ERR_TRAILING_SLASH
:
1135 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1136 "trailing slash in name"));
1139 case NAME_ERR_MULTIPLE_DELIMITERS
:
1140 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1141 "multiple '@' and/or '#' delimiters in "
1145 case NAME_ERR_NO_AT
:
1146 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1147 "permission set is missing '@'"));
1151 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1152 "(%d) not defined"), why
);
1163 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1167 zpool_open_canfail(libzfs_handle_t
*hdl
, const char *pool
)
1169 zpool_handle_t
*zhp
;
1173 * Make sure the pool name is valid.
1175 if (!zpool_name_valid(hdl
, B_TRUE
, pool
)) {
1176 (void) zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1177 dgettext(TEXT_DOMAIN
, "cannot open '%s'"),
1182 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
1185 zhp
->zpool_hdl
= hdl
;
1186 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1188 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1194 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "no such pool"));
1195 (void) zfs_error_fmt(hdl
, EZFS_NOENT
,
1196 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), pool
);
1205 * Like the above, but silent on error. Used when iterating over pools (because
1206 * the configuration cache may be out of date).
1209 zpool_open_silent(libzfs_handle_t
*hdl
, const char *pool
, zpool_handle_t
**ret
)
1211 zpool_handle_t
*zhp
;
1214 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
1217 zhp
->zpool_hdl
= hdl
;
1218 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1220 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1236 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1240 zpool_open(libzfs_handle_t
*hdl
, const char *pool
)
1242 zpool_handle_t
*zhp
;
1244 if ((zhp
= zpool_open_canfail(hdl
, pool
)) == NULL
)
1247 if (zhp
->zpool_state
== POOL_STATE_UNAVAIL
) {
1248 (void) zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
1249 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), zhp
->zpool_name
);
1258 * Close the handle. Simply frees the memory associated with the handle.
1261 zpool_close(zpool_handle_t
*zhp
)
1263 nvlist_free(zhp
->zpool_config
);
1264 nvlist_free(zhp
->zpool_old_config
);
1265 nvlist_free(zhp
->zpool_props
);
1270 * Return the name of the pool.
1273 zpool_get_name(zpool_handle_t
*zhp
)
1275 return (zhp
->zpool_name
);
1280 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1283 zpool_get_state(zpool_handle_t
*zhp
)
1285 return (zhp
->zpool_state
);
1289 * Check if vdev list contains a special vdev
1292 zpool_has_special_vdev(nvlist_t
*nvroot
)
1297 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
, &child
,
1299 for (uint_t c
= 0; c
< children
; c
++) {
1302 if (nvlist_lookup_string(child
[c
],
1303 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
) == 0 &&
1304 strcmp(bias
, VDEV_ALLOC_BIAS_SPECIAL
) == 0) {
1313 * Check if vdev list contains a dRAID vdev
1316 zpool_has_draid_vdev(nvlist_t
*nvroot
)
1321 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
1322 &child
, &children
) == 0) {
1323 for (uint_t c
= 0; c
< children
; c
++) {
1326 if (nvlist_lookup_string(child
[c
],
1327 ZPOOL_CONFIG_TYPE
, &type
) == 0 &&
1328 strcmp(type
, VDEV_TYPE_DRAID
) == 0) {
1337 * Output a dRAID top-level vdev name in to the provided buffer.
1340 zpool_draid_name(char *name
, int len
, uint64_t data
, uint64_t parity
,
1341 uint64_t spares
, uint64_t children
)
1343 snprintf(name
, len
, "%s%llu:%llud:%lluc:%llus",
1344 VDEV_TYPE_DRAID
, (u_longlong_t
)parity
, (u_longlong_t
)data
,
1345 (u_longlong_t
)children
, (u_longlong_t
)spares
);
1351 * Return B_TRUE if the provided name is a dRAID spare name.
1354 zpool_is_draid_spare(const char *name
)
1356 uint64_t spare_id
, parity
, vdev_id
;
1358 if (sscanf(name
, VDEV_TYPE_DRAID
"%llu-%llu-%llu",
1359 (u_longlong_t
*)&parity
, (u_longlong_t
*)&vdev_id
,
1360 (u_longlong_t
*)&spare_id
) == 3) {
1368 * Create the named pool, using the provided vdev list. It is assumed
1369 * that the consumer has already validated the contents of the nvlist, so we
1370 * don't have to worry about error semantics.
1373 zpool_create(libzfs_handle_t
*hdl
, const char *pool
, nvlist_t
*nvroot
,
1374 nvlist_t
*props
, nvlist_t
*fsprops
)
1376 zfs_cmd_t zc
= {"\0"};
1377 nvlist_t
*zc_fsprops
= NULL
;
1378 nvlist_t
*zc_props
= NULL
;
1379 nvlist_t
*hidden_args
= NULL
;
1380 uint8_t *wkeydata
= NULL
;
1385 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1386 "cannot create '%s'"), pool
);
1388 if (!zpool_name_valid(hdl
, B_FALSE
, pool
))
1389 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
1391 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1395 prop_flags_t flags
= { .create
= B_TRUE
, .import
= B_FALSE
};
1397 if ((zc_props
= zpool_valid_proplist(hdl
, pool
, props
,
1398 SPA_VERSION_1
, flags
, msg
)) == NULL
) {
1407 zoned
= ((nvlist_lookup_string(fsprops
,
1408 zfs_prop_to_name(ZFS_PROP_ZONED
), &zonestr
) == 0) &&
1409 strcmp(zonestr
, "on") == 0);
1411 if ((zc_fsprops
= zfs_valid_proplist(hdl
, ZFS_TYPE_FILESYSTEM
,
1412 fsprops
, zoned
, NULL
, NULL
, B_TRUE
, msg
)) == NULL
) {
1416 if (nvlist_exists(zc_fsprops
,
1417 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS
)) &&
1418 !zpool_has_special_vdev(nvroot
)) {
1419 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1420 "%s property requires a special vdev"),
1421 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS
));
1422 (void) zfs_error(hdl
, EZFS_BADPROP
, msg
);
1427 (nvlist_alloc(&zc_props
, NV_UNIQUE_NAME
, 0) != 0)) {
1430 if (zfs_crypto_create(hdl
, NULL
, zc_fsprops
, props
, B_TRUE
,
1431 &wkeydata
, &wkeylen
) != 0) {
1432 zfs_error(hdl
, EZFS_CRYPTOFAILED
, msg
);
1435 if (nvlist_add_nvlist(zc_props
,
1436 ZPOOL_ROOTFS_PROPS
, zc_fsprops
) != 0) {
1439 if (wkeydata
!= NULL
) {
1440 if (nvlist_alloc(&hidden_args
, NV_UNIQUE_NAME
, 0) != 0)
1443 if (nvlist_add_uint8_array(hidden_args
, "wkeydata",
1444 wkeydata
, wkeylen
) != 0)
1447 if (nvlist_add_nvlist(zc_props
, ZPOOL_HIDDEN_ARGS
,
1453 if (zc_props
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
1456 (void) strlcpy(zc
.zc_name
, pool
, sizeof (zc
.zc_name
));
1458 if ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_CREATE
, &zc
)) != 0) {
1460 zcmd_free_nvlists(&zc
);
1461 nvlist_free(zc_props
);
1462 nvlist_free(zc_fsprops
);
1463 nvlist_free(hidden_args
);
1464 if (wkeydata
!= NULL
)
1470 * This can happen if the user has specified the same
1471 * device multiple times. We can't reliably detect this
1472 * until we try to add it and see we already have a
1473 * label. This can also happen under if the device is
1474 * part of an active md or lvm device.
1476 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1477 "one or more vdevs refer to the same device, or "
1478 "one of\nthe devices is part of an active md or "
1480 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1484 * This happens if the record size is smaller or larger
1485 * than the allowed size range, or not a power of 2.
1487 * NOTE: although zfs_valid_proplist is called earlier,
1488 * this case may have slipped through since the
1489 * pool does not exist yet and it is therefore
1490 * impossible to read properties e.g. max blocksize
1493 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1494 "record size invalid"));
1495 return (zfs_error(hdl
, EZFS_BADPROP
, msg
));
1499 * This occurs when one of the devices is below
1500 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1501 * device was the problem device since there's no
1502 * reliable way to determine device size from userland.
1507 zfs_nicebytes(SPA_MINDEVSIZE
, buf
,
1510 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1511 "one or more devices is less than the "
1512 "minimum size (%s)"), buf
);
1514 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1517 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1518 "one or more devices is out of space"));
1519 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1522 if (zpool_has_draid_vdev(nvroot
) &&
1523 zfeature_lookup_name("draid", NULL
) != 0) {
1524 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1525 "dRAID vdevs are unsupported by the "
1527 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1529 return (zpool_standard_error(hdl
, errno
, msg
));
1533 return (zpool_standard_error(hdl
, errno
, msg
));
1538 zcmd_free_nvlists(&zc
);
1539 nvlist_free(zc_props
);
1540 nvlist_free(zc_fsprops
);
1541 nvlist_free(hidden_args
);
1542 if (wkeydata
!= NULL
)
1548 * Destroy the given pool. It is up to the caller to ensure that there are no
1549 * datasets left in the pool.
1552 zpool_destroy(zpool_handle_t
*zhp
, const char *log_str
)
1554 zfs_cmd_t zc
= {"\0"};
1555 zfs_handle_t
*zfp
= NULL
;
1556 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1559 if (zhp
->zpool_state
== POOL_STATE_ACTIVE
&&
1560 (zfp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
1563 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1564 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1566 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_DESTROY
, &zc
) != 0) {
1567 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1568 "cannot destroy '%s'"), zhp
->zpool_name
);
1570 if (errno
== EROFS
) {
1571 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1572 "one or more devices is read only"));
1573 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1575 (void) zpool_standard_error(hdl
, errno
, msg
);
1584 remove_mountpoint(zfp
);
1592 * Create a checkpoint in the given pool.
1595 zpool_checkpoint(zpool_handle_t
*zhp
)
1597 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1601 error
= lzc_pool_checkpoint(zhp
->zpool_name
);
1603 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1604 "cannot checkpoint '%s'"), zhp
->zpool_name
);
1605 (void) zpool_standard_error(hdl
, error
, msg
);
1613 * Discard the checkpoint from the given pool.
1616 zpool_discard_checkpoint(zpool_handle_t
*zhp
)
1618 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1622 error
= lzc_pool_checkpoint_discard(zhp
->zpool_name
);
1624 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1625 "cannot discard checkpoint in '%s'"), zhp
->zpool_name
);
1626 (void) zpool_standard_error(hdl
, error
, msg
);
1634 * Add the given vdevs to the pool. The caller must have already performed the
1635 * necessary verification to ensure that the vdev specification is well-formed.
1638 zpool_add(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
1640 zfs_cmd_t zc
= {"\0"};
1642 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1644 nvlist_t
**spares
, **l2cache
;
1645 uint_t nspares
, nl2cache
;
1647 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1648 "cannot add to '%s'"), zhp
->zpool_name
);
1650 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1651 SPA_VERSION_SPARES
&&
1652 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1653 &spares
, &nspares
) == 0) {
1654 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1655 "upgraded to add hot spares"));
1656 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1659 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1660 SPA_VERSION_L2CACHE
&&
1661 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1662 &l2cache
, &nl2cache
) == 0) {
1663 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1664 "upgraded to add cache devices"));
1665 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1668 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1670 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1672 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_ADD
, &zc
) != 0) {
1676 * This can happen if the user has specified the same
1677 * device multiple times. We can't reliably detect this
1678 * until we try to add it and see we already have a
1681 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1682 "one or more vdevs refer to the same device"));
1683 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1688 if (zpool_has_draid_vdev(nvroot
) &&
1689 zfeature_lookup_name("draid", NULL
) != 0) {
1690 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1691 "dRAID vdevs are unsupported by the "
1694 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1695 "invalid config; a pool with removing/"
1696 "removed vdevs does not support adding "
1697 "raidz or dRAID vdevs"));
1700 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1705 * This occurs when one of the devices is below
1706 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1707 * device was the problem device since there's no
1708 * reliable way to determine device size from userland.
1713 zfs_nicebytes(SPA_MINDEVSIZE
, buf
,
1716 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1717 "device is less than the minimum "
1720 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1724 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1725 "pool must be upgraded to add these vdevs"));
1726 (void) zfs_error(hdl
, EZFS_BADVERSION
, msg
);
1730 (void) zpool_standard_error(hdl
, errno
, msg
);
1738 zcmd_free_nvlists(&zc
);
1744 * Exports the pool from the system. The caller must ensure that there are no
1745 * mounted datasets in the pool.
1748 zpool_export_common(zpool_handle_t
*zhp
, boolean_t force
, boolean_t hardforce
,
1749 const char *log_str
)
1751 zfs_cmd_t zc
= {"\0"};
1753 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1754 zc
.zc_cookie
= force
;
1755 zc
.zc_guid
= hardforce
;
1756 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1758 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_EXPORT
, &zc
) != 0) {
1761 zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
1762 "use '-f' to override the following errors:\n"
1763 "'%s' has an active shared spare which could be"
1764 " used by other pools once '%s' is exported."),
1765 zhp
->zpool_name
, zhp
->zpool_name
);
1766 return (zfs_error_fmt(zhp
->zpool_hdl
, EZFS_ACTIVE_SPARE
,
1767 dgettext(TEXT_DOMAIN
, "cannot export '%s'"),
1770 return (zpool_standard_error_fmt(zhp
->zpool_hdl
, errno
,
1771 dgettext(TEXT_DOMAIN
, "cannot export '%s'"),
1780 zpool_export(zpool_handle_t
*zhp
, boolean_t force
, const char *log_str
)
1782 return (zpool_export_common(zhp
, force
, B_FALSE
, log_str
));
1786 zpool_export_force(zpool_handle_t
*zhp
, const char *log_str
)
1788 return (zpool_export_common(zhp
, B_TRUE
, B_TRUE
, log_str
));
1792 zpool_rewind_exclaim(libzfs_handle_t
*hdl
, const char *name
, boolean_t dryrun
,
1795 nvlist_t
*nv
= NULL
;
1801 if (!hdl
->libzfs_printerr
|| config
== NULL
)
1804 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1805 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0) {
1809 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1811 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1813 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1814 strftime(timestr
, 128, "%c", &t
) != 0) {
1816 (void) printf(dgettext(TEXT_DOMAIN
,
1817 "Would be able to return %s "
1818 "to its state as of %s.\n"),
1821 (void) printf(dgettext(TEXT_DOMAIN
,
1822 "Pool %s returned to its state as of %s.\n"),
1826 (void) printf(dgettext(TEXT_DOMAIN
,
1827 "%s approximately %lld "),
1828 dryrun
? "Would discard" : "Discarded",
1829 ((longlong_t
)loss
+ 30) / 60);
1830 (void) printf(dgettext(TEXT_DOMAIN
,
1831 "minutes of transactions.\n"));
1832 } else if (loss
> 0) {
1833 (void) printf(dgettext(TEXT_DOMAIN
,
1834 "%s approximately %lld "),
1835 dryrun
? "Would discard" : "Discarded",
1837 (void) printf(dgettext(TEXT_DOMAIN
,
1838 "seconds of transactions.\n"));
1844 zpool_explain_recover(libzfs_handle_t
*hdl
, const char *name
, int reason
,
1847 nvlist_t
*nv
= NULL
;
1849 uint64_t edata
= UINT64_MAX
;
1854 if (!hdl
->libzfs_printerr
)
1858 (void) printf(dgettext(TEXT_DOMAIN
, "action: "));
1860 (void) printf(dgettext(TEXT_DOMAIN
, "\t"));
1862 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1863 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1864 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0 ||
1865 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1868 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1869 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_DATA_ERRORS
,
1872 (void) printf(dgettext(TEXT_DOMAIN
,
1873 "Recovery is possible, but will result in some data loss.\n"));
1875 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1876 strftime(timestr
, 128, "%c", &t
) != 0) {
1877 (void) printf(dgettext(TEXT_DOMAIN
,
1878 "\tReturning the pool to its state as of %s\n"
1879 "\tshould correct the problem. "),
1882 (void) printf(dgettext(TEXT_DOMAIN
,
1883 "\tReverting the pool to an earlier state "
1884 "should correct the problem.\n\t"));
1888 (void) printf(dgettext(TEXT_DOMAIN
,
1889 "Approximately %lld minutes of data\n"
1890 "\tmust be discarded, irreversibly. "),
1891 ((longlong_t
)loss
+ 30) / 60);
1892 } else if (loss
> 0) {
1893 (void) printf(dgettext(TEXT_DOMAIN
,
1894 "Approximately %lld seconds of data\n"
1895 "\tmust be discarded, irreversibly. "),
1898 if (edata
!= 0 && edata
!= UINT64_MAX
) {
1900 (void) printf(dgettext(TEXT_DOMAIN
,
1901 "After rewind, at least\n"
1902 "\tone persistent user-data error will remain. "));
1904 (void) printf(dgettext(TEXT_DOMAIN
,
1905 "After rewind, several\n"
1906 "\tpersistent user-data errors will remain. "));
1909 (void) printf(dgettext(TEXT_DOMAIN
,
1910 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1911 reason
>= 0 ? "clear" : "import", name
);
1913 (void) printf(dgettext(TEXT_DOMAIN
,
1914 "A scrub of the pool\n"
1915 "\tis strongly recommended after recovery.\n"));
1919 (void) printf(dgettext(TEXT_DOMAIN
,
1920 "Destroy and re-create the pool from\n\ta backup source.\n"));
1924 * zpool_import() is a contracted interface. Should be kept the same
1927 * Applications should use zpool_import_props() to import a pool with
1928 * new properties value to be set.
1931 zpool_import(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1934 nvlist_t
*props
= NULL
;
1937 if (altroot
!= NULL
) {
1938 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
1939 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1940 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1944 if (nvlist_add_string(props
,
1945 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), altroot
) != 0 ||
1946 nvlist_add_string(props
,
1947 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
), "none") != 0) {
1949 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1950 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1955 ret
= zpool_import_props(hdl
, config
, newname
, props
,
1962 print_vdev_tree(libzfs_handle_t
*hdl
, const char *name
, nvlist_t
*nv
,
1968 uint64_t is_log
= 0;
1970 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
,
1974 (void) printf("\t%*s%s%s\n", indent
, "", name
,
1975 is_log
? " [log]" : "");
1977 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1978 &child
, &children
) != 0)
1981 for (c
= 0; c
< children
; c
++) {
1982 vname
= zpool_vdev_name(hdl
, NULL
, child
[c
], VDEV_NAME_TYPE_ID
);
1983 print_vdev_tree(hdl
, vname
, child
[c
], indent
+ 2);
1989 zpool_print_unsup_feat(nvlist_t
*config
)
1991 nvlist_t
*nvinfo
, *unsup_feat
;
1993 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
1994 unsup_feat
= fnvlist_lookup_nvlist(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
);
1996 for (nvpair_t
*nvp
= nvlist_next_nvpair(unsup_feat
, NULL
);
1997 nvp
!= NULL
; nvp
= nvlist_next_nvpair(unsup_feat
, nvp
)) {
1998 char *desc
= fnvpair_value_string(nvp
);
1999 if (strlen(desc
) > 0)
2000 (void) printf("\t%s (%s)\n", nvpair_name(nvp
), desc
);
2002 (void) printf("\t%s\n", nvpair_name(nvp
));
2007 * Import the given pool using the known configuration and a list of
2008 * properties to be set. The configuration should have come from
2009 * zpool_find_import(). The 'newname' parameters control whether the pool
2010 * is imported with a different name.
2013 zpool_import_props(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
2014 nvlist_t
*props
, int flags
)
2016 zfs_cmd_t zc
= {"\0"};
2017 zpool_load_policy_t policy
;
2018 nvlist_t
*nv
= NULL
;
2019 nvlist_t
*nvinfo
= NULL
;
2020 nvlist_t
*missing
= NULL
;
2021 const char *thename
;
2027 origname
= fnvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
);
2029 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2030 "cannot import pool '%s'"), origname
);
2032 if (newname
!= NULL
) {
2033 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
2034 return (zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
2035 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
2042 if (props
!= NULL
) {
2044 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
2046 version
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
2048 if ((props
= zpool_valid_proplist(hdl
, origname
,
2049 props
, version
, flags
, errbuf
)) == NULL
)
2051 if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
2058 (void) strlcpy(zc
.zc_name
, thename
, sizeof (zc
.zc_name
));
2060 zc
.zc_guid
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
);
2062 if (zcmd_write_conf_nvlist(hdl
, &zc
, config
) != 0) {
2063 zcmd_free_nvlists(&zc
);
2066 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zc
.zc_nvlist_conf_size
* 2) != 0) {
2067 zcmd_free_nvlists(&zc
);
2071 zc
.zc_cookie
= flags
;
2072 while ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_IMPORT
, &zc
)) != 0 &&
2074 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
2075 zcmd_free_nvlists(&zc
);
2082 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nv
);
2084 zcmd_free_nvlists(&zc
);
2086 zpool_get_load_policy(config
, &policy
);
2093 * Dry-run failed, but we print out what success
2094 * looks like if we found a best txg
2096 if (policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) {
2097 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
2103 if (newname
== NULL
)
2104 (void) snprintf(desc
, sizeof (desc
),
2105 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
2108 (void) snprintf(desc
, sizeof (desc
),
2109 dgettext(TEXT_DOMAIN
, "cannot import '%s' as '%s'"),
2114 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
2115 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
2116 nvlist_exists(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
)) {
2117 (void) printf(dgettext(TEXT_DOMAIN
, "This "
2118 "pool uses the following feature(s) not "
2119 "supported by this system:\n"));
2120 zpool_print_unsup_feat(nv
);
2121 if (nvlist_exists(nvinfo
,
2122 ZPOOL_CONFIG_CAN_RDONLY
)) {
2123 (void) printf(dgettext(TEXT_DOMAIN
,
2124 "All unsupported features are only "
2125 "required for writing to the pool."
2126 "\nThe pool can be imported using "
2127 "'-o readonly=on'.\n"));
2131 * Unsupported version.
2133 (void) zfs_error(hdl
, EZFS_BADVERSION
, desc
);
2137 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
2138 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0) {
2139 char *hostname
= "<unknown>";
2140 uint64_t hostid
= 0;
2141 mmp_state_t mmp_state
;
2143 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
2144 ZPOOL_CONFIG_MMP_STATE
);
2146 if (nvlist_exists(nvinfo
,
2147 ZPOOL_CONFIG_MMP_HOSTNAME
))
2148 hostname
= fnvlist_lookup_string(nvinfo
,
2149 ZPOOL_CONFIG_MMP_HOSTNAME
);
2151 if (nvlist_exists(nvinfo
,
2152 ZPOOL_CONFIG_MMP_HOSTID
))
2153 hostid
= fnvlist_lookup_uint64(nvinfo
,
2154 ZPOOL_CONFIG_MMP_HOSTID
);
2156 if (mmp_state
== MMP_STATE_ACTIVE
) {
2157 (void) snprintf(aux
, sizeof (aux
),
2158 dgettext(TEXT_DOMAIN
, "pool is imp"
2159 "orted on host '%s' (hostid=%lx).\n"
2160 "Export the pool on the other "
2161 "system, then run 'zpool import'."),
2162 hostname
, (unsigned long) hostid
);
2163 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
2164 (void) snprintf(aux
, sizeof (aux
),
2165 dgettext(TEXT_DOMAIN
, "pool has "
2166 "the multihost property on and "
2167 "the\nsystem's hostid is not set. "
2168 "Set a unique system hostid with "
2169 "the zgenhostid(8) command.\n"));
2172 (void) zfs_error_aux(hdl
, "%s", aux
);
2174 (void) zfs_error(hdl
, EZFS_ACTIVE_POOL
, desc
);
2178 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, desc
);
2182 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2183 "one or more devices is read only"));
2184 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
2188 if (nv
&& nvlist_lookup_nvlist(nv
,
2189 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
2190 nvlist_lookup_nvlist(nvinfo
,
2191 ZPOOL_CONFIG_MISSING_DEVICES
, &missing
) == 0) {
2192 (void) printf(dgettext(TEXT_DOMAIN
,
2193 "The devices below are missing or "
2194 "corrupted, use '-m' to import the pool "
2196 print_vdev_tree(hdl
, NULL
, missing
, 2);
2197 (void) printf("\n");
2199 (void) zpool_standard_error(hdl
, error
, desc
);
2203 (void) zpool_standard_error(hdl
, error
, desc
);
2207 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2208 "one or more devices are already in use\n"));
2209 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
2212 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2213 "new name of at least one dataset is longer than "
2214 "the maximum allowable length"));
2215 (void) zfs_error(hdl
, EZFS_NAMETOOLONG
, desc
);
2218 (void) zpool_standard_error(hdl
, error
, desc
);
2219 zpool_explain_recover(hdl
,
2220 newname
? origname
: thename
, -error
, nv
);
2227 zpool_handle_t
*zhp
;
2230 * This should never fail, but play it safe anyway.
2232 if (zpool_open_silent(hdl
, thename
, &zhp
) != 0)
2234 else if (zhp
!= NULL
)
2236 if (policy
.zlp_rewind
&
2237 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
2238 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
2239 ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) != 0), nv
);
2249 * Translate vdev names to guids. If a vdev_path is determined to be
2250 * unsuitable then a vd_errlist is allocated and the vdev path and errno
2254 zpool_translate_vdev_guids(zpool_handle_t
*zhp
, nvlist_t
*vds
,
2255 nvlist_t
*vdev_guids
, nvlist_t
*guids_to_paths
, nvlist_t
**vd_errlist
)
2257 nvlist_t
*errlist
= NULL
;
2260 for (nvpair_t
*elem
= nvlist_next_nvpair(vds
, NULL
); elem
!= NULL
;
2261 elem
= nvlist_next_nvpair(vds
, elem
)) {
2262 boolean_t spare
, cache
;
2264 char *vd_path
= nvpair_name(elem
);
2265 nvlist_t
*tgt
= zpool_find_vdev(zhp
, vd_path
, &spare
, &cache
,
2268 if ((tgt
== NULL
) || cache
|| spare
) {
2269 if (errlist
== NULL
) {
2270 errlist
= fnvlist_alloc();
2274 uint64_t err
= (tgt
== NULL
) ? EZFS_NODEVICE
:
2275 (spare
? EZFS_ISSPARE
: EZFS_ISL2CACHE
);
2276 fnvlist_add_int64(errlist
, vd_path
, err
);
2280 uint64_t guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
2281 fnvlist_add_uint64(vdev_guids
, vd_path
, guid
);
2283 char msg
[MAXNAMELEN
];
2284 (void) snprintf(msg
, sizeof (msg
), "%llu", (u_longlong_t
)guid
);
2285 fnvlist_add_string(guids_to_paths
, msg
, vd_path
);
2289 verify(errlist
!= NULL
);
2290 if (vd_errlist
!= NULL
)
2291 *vd_errlist
= errlist
;
2293 fnvlist_free(errlist
);
2300 xlate_init_err(int err
)
2304 return (EZFS_NODEVICE
);
2307 return (EZFS_BADDEV
);
2309 return (EZFS_INITIALIZING
);
2311 return (EZFS_NO_INITIALIZE
);
2317 * Begin, suspend, or cancel the initialization (initializing of all free
2318 * blocks) for the given vdevs in the given pool.
2321 zpool_initialize_impl(zpool_handle_t
*zhp
, pool_initialize_func_t cmd_type
,
2322 nvlist_t
*vds
, boolean_t wait
)
2326 nvlist_t
*vdev_guids
= fnvlist_alloc();
2327 nvlist_t
*guids_to_paths
= fnvlist_alloc();
2328 nvlist_t
*vd_errlist
= NULL
;
2332 err
= zpool_translate_vdev_guids(zhp
, vds
, vdev_guids
,
2333 guids_to_paths
, &vd_errlist
);
2336 verify(vd_errlist
!= NULL
);
2340 err
= lzc_initialize(zhp
->zpool_name
, cmd_type
,
2341 vdev_guids
, &errlist
);
2344 if (errlist
!= NULL
) {
2345 vd_errlist
= fnvlist_lookup_nvlist(errlist
,
2346 ZPOOL_INITIALIZE_VDEVS
);
2349 (void) zpool_standard_error(zhp
->zpool_hdl
, err
,
2350 dgettext(TEXT_DOMAIN
, "operation failed"));
2355 for (elem
= nvlist_next_nvpair(vdev_guids
, NULL
); elem
!= NULL
;
2356 elem
= nvlist_next_nvpair(vdev_guids
, elem
)) {
2358 uint64_t guid
= fnvpair_value_uint64(elem
);
2360 err
= lzc_wait_tag(zhp
->zpool_name
,
2361 ZPOOL_WAIT_INITIALIZE
, guid
, NULL
);
2363 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
,
2364 err
, dgettext(TEXT_DOMAIN
, "error "
2365 "waiting for '%s' to initialize"),
2375 for (elem
= nvlist_next_nvpair(vd_errlist
, NULL
); elem
!= NULL
;
2376 elem
= nvlist_next_nvpair(vd_errlist
, elem
)) {
2377 int64_t vd_error
= xlate_init_err(fnvpair_value_int64(elem
));
2380 if (nvlist_lookup_string(guids_to_paths
, nvpair_name(elem
),
2382 path
= nvpair_name(elem
);
2384 (void) zfs_error_fmt(zhp
->zpool_hdl
, vd_error
,
2385 "cannot initialize '%s'", path
);
2389 fnvlist_free(vdev_guids
);
2390 fnvlist_free(guids_to_paths
);
2392 if (vd_errlist
!= NULL
)
2393 fnvlist_free(vd_errlist
);
2395 return (err
== 0 ? 0 : -1);
2399 zpool_initialize(zpool_handle_t
*zhp
, pool_initialize_func_t cmd_type
,
2402 return (zpool_initialize_impl(zhp
, cmd_type
, vds
, B_FALSE
));
2406 zpool_initialize_wait(zpool_handle_t
*zhp
, pool_initialize_func_t cmd_type
,
2409 return (zpool_initialize_impl(zhp
, cmd_type
, vds
, B_TRUE
));
2413 xlate_trim_err(int err
)
2417 return (EZFS_NODEVICE
);
2420 return (EZFS_BADDEV
);
2422 return (EZFS_TRIMMING
);
2424 return (EZFS_NO_TRIM
);
2426 return (EZFS_TRIM_NOTSUP
);
2432 zpool_trim_wait(zpool_handle_t
*zhp
, nvlist_t
*vdev_guids
)
2437 for (elem
= nvlist_next_nvpair(vdev_guids
, NULL
); elem
!= NULL
;
2438 elem
= nvlist_next_nvpair(vdev_guids
, elem
)) {
2440 uint64_t guid
= fnvpair_value_uint64(elem
);
2442 err
= lzc_wait_tag(zhp
->zpool_name
,
2443 ZPOOL_WAIT_TRIM
, guid
, NULL
);
2445 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
,
2446 err
, dgettext(TEXT_DOMAIN
, "error "
2447 "waiting to trim '%s'"), nvpair_name(elem
));
2456 * Check errlist and report any errors, omitting ones which should be
2457 * suppressed. Returns B_TRUE if any errors were reported.
2460 check_trim_errs(zpool_handle_t
*zhp
, trimflags_t
*trim_flags
,
2461 nvlist_t
*guids_to_paths
, nvlist_t
*vds
, nvlist_t
*errlist
)
2464 boolean_t reported_errs
= B_FALSE
;
2466 int num_suppressed_errs
= 0;
2468 for (elem
= nvlist_next_nvpair(vds
, NULL
);
2469 elem
!= NULL
; elem
= nvlist_next_nvpair(vds
, elem
)) {
2473 for (elem
= nvlist_next_nvpair(errlist
, NULL
);
2474 elem
!= NULL
; elem
= nvlist_next_nvpair(errlist
, elem
)) {
2475 int64_t vd_error
= xlate_trim_err(fnvpair_value_int64(elem
));
2479 * If only the pool was specified, and it was not a secure
2480 * trim then suppress warnings for individual vdevs which
2481 * do not support trimming.
2483 if (vd_error
== EZFS_TRIM_NOTSUP
&&
2484 trim_flags
->fullpool
&&
2485 !trim_flags
->secure
) {
2486 num_suppressed_errs
++;
2490 reported_errs
= B_TRUE
;
2491 if (nvlist_lookup_string(guids_to_paths
, nvpair_name(elem
),
2493 path
= nvpair_name(elem
);
2495 (void) zfs_error_fmt(zhp
->zpool_hdl
, vd_error
,
2496 "cannot trim '%s'", path
);
2499 if (num_suppressed_errs
== num_vds
) {
2500 (void) zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
2501 "no devices in pool support trim operations"));
2502 (void) (zfs_error(zhp
->zpool_hdl
, EZFS_TRIM_NOTSUP
,
2503 dgettext(TEXT_DOMAIN
, "cannot trim")));
2504 reported_errs
= B_TRUE
;
2507 return (reported_errs
);
2511 * Begin, suspend, or cancel the TRIM (discarding of all free blocks) for
2512 * the given vdevs in the given pool.
2515 zpool_trim(zpool_handle_t
*zhp
, pool_trim_func_t cmd_type
, nvlist_t
*vds
,
2516 trimflags_t
*trim_flags
)
2521 nvlist_t
*vdev_guids
= fnvlist_alloc();
2522 nvlist_t
*guids_to_paths
= fnvlist_alloc();
2523 nvlist_t
*errlist
= NULL
;
2525 err
= zpool_translate_vdev_guids(zhp
, vds
, vdev_guids
,
2526 guids_to_paths
, &errlist
);
2528 check_trim_errs(zhp
, trim_flags
, guids_to_paths
, vds
, errlist
);
2533 err
= lzc_trim(zhp
->zpool_name
, cmd_type
, trim_flags
->rate
,
2534 trim_flags
->secure
, vdev_guids
, &errlist
);
2536 nvlist_t
*vd_errlist
;
2537 if (errlist
!= NULL
&& nvlist_lookup_nvlist(errlist
,
2538 ZPOOL_TRIM_VDEVS
, &vd_errlist
) == 0) {
2539 if (check_trim_errs(zhp
, trim_flags
, guids_to_paths
,
2547 (void) snprintf(msg
, sizeof (msg
),
2548 dgettext(TEXT_DOMAIN
, "operation failed"));
2549 zpool_standard_error(zhp
->zpool_hdl
, err
, msg
);
2556 if (trim_flags
->wait
)
2557 retval
= zpool_trim_wait(zhp
, vdev_guids
);
2560 if (errlist
!= NULL
)
2561 fnvlist_free(errlist
);
2562 fnvlist_free(vdev_guids
);
2563 fnvlist_free(guids_to_paths
);
2571 zpool_scan(zpool_handle_t
*zhp
, pool_scan_func_t func
, pool_scrub_cmd_t cmd
)
2573 zfs_cmd_t zc
= {"\0"};
2576 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2578 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2579 zc
.zc_cookie
= func
;
2582 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_SCAN
, &zc
) == 0)
2587 /* ECANCELED on a scrub means we resumed a paused scrub */
2588 if (err
== ECANCELED
&& func
== POOL_SCAN_SCRUB
&&
2589 cmd
== POOL_SCRUB_NORMAL
)
2592 if (err
== ENOENT
&& func
!= POOL_SCAN_NONE
&& cmd
== POOL_SCRUB_NORMAL
)
2595 if (func
== POOL_SCAN_SCRUB
) {
2596 if (cmd
== POOL_SCRUB_PAUSE
) {
2597 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2598 "cannot pause scrubbing %s"), zc
.zc_name
);
2600 assert(cmd
== POOL_SCRUB_NORMAL
);
2601 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2602 "cannot scrub %s"), zc
.zc_name
);
2604 } else if (func
== POOL_SCAN_RESILVER
) {
2605 assert(cmd
== POOL_SCRUB_NORMAL
);
2606 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2607 "cannot restart resilver on %s"), zc
.zc_name
);
2608 } else if (func
== POOL_SCAN_NONE
) {
2609 (void) snprintf(msg
, sizeof (msg
),
2610 dgettext(TEXT_DOMAIN
, "cannot cancel scrubbing %s"),
2613 assert(!"unexpected result");
2618 pool_scan_stat_t
*ps
= NULL
;
2621 nvroot
= fnvlist_lookup_nvlist(zhp
->zpool_config
,
2622 ZPOOL_CONFIG_VDEV_TREE
);
2623 (void) nvlist_lookup_uint64_array(nvroot
,
2624 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &psc
);
2625 if (ps
&& ps
->pss_func
== POOL_SCAN_SCRUB
&&
2626 ps
->pss_state
== DSS_SCANNING
) {
2627 if (cmd
== POOL_SCRUB_PAUSE
)
2628 return (zfs_error(hdl
, EZFS_SCRUB_PAUSED
, msg
));
2630 return (zfs_error(hdl
, EZFS_SCRUBBING
, msg
));
2632 return (zfs_error(hdl
, EZFS_RESILVERING
, msg
));
2634 } else if (err
== ENOENT
) {
2635 return (zfs_error(hdl
, EZFS_NO_SCRUB
, msg
));
2636 } else if (err
== ENOTSUP
&& func
== POOL_SCAN_RESILVER
) {
2637 return (zfs_error(hdl
, EZFS_NO_RESILVER_DEFER
, msg
));
2639 return (zpool_standard_error(hdl
, err
, msg
));
2644 * Find a vdev that matches the search criteria specified. We use the
2645 * the nvpair name to determine how we should look for the device.
2646 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2647 * spare; but FALSE if its an INUSE spare.
2650 vdev_to_nvlist_iter(nvlist_t
*nv
, nvlist_t
*search
, boolean_t
*avail_spare
,
2651 boolean_t
*l2cache
, boolean_t
*log
)
2658 nvpair_t
*pair
= nvlist_next_nvpair(search
, NULL
);
2660 /* Nothing to look for */
2661 if (search
== NULL
|| pair
== NULL
)
2664 /* Obtain the key we will use to search */
2665 srchkey
= nvpair_name(pair
);
2667 switch (nvpair_type(pair
)) {
2668 case DATA_TYPE_UINT64
:
2669 if (strcmp(srchkey
, ZPOOL_CONFIG_GUID
) == 0) {
2670 uint64_t srchval
= fnvpair_value_uint64(pair
);
2671 uint64_t theguid
= fnvlist_lookup_uint64(nv
,
2673 if (theguid
== srchval
)
2678 case DATA_TYPE_STRING
: {
2679 char *srchval
, *val
;
2681 srchval
= fnvpair_value_string(pair
);
2682 if (nvlist_lookup_string(nv
, srchkey
, &val
) != 0)
2686 * Search for the requested value. Special cases:
2688 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2689 * "-part1", or "p1". The suffix is hidden from the user,
2690 * but included in the string, so this matches around it.
2691 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2692 * is used to check all possible expanded paths.
2693 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2695 * Otherwise, all other searches are simple string compares.
2697 if (strcmp(srchkey
, ZPOOL_CONFIG_PATH
) == 0) {
2698 uint64_t wholedisk
= 0;
2700 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
2702 if (zfs_strcmp_pathname(srchval
, val
, wholedisk
) == 0)
2705 } else if (strcmp(srchkey
, ZPOOL_CONFIG_TYPE
) == 0 && val
) {
2706 char *type
, *idx
, *end
, *p
;
2707 uint64_t id
, vdev_id
;
2710 * Determine our vdev type, keeping in mind
2711 * that the srchval is composed of a type and
2712 * vdev id pair (i.e. mirror-4).
2714 if ((type
= strdup(srchval
)) == NULL
)
2717 if ((p
= strrchr(type
, '-')) == NULL
) {
2725 * If the types don't match then keep looking.
2727 if (strncmp(val
, type
, strlen(val
)) != 0) {
2732 verify(zpool_vdev_is_interior(type
));
2734 id
= fnvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
);
2736 vdev_id
= strtoull(idx
, &end
, 10);
2739 * If we are looking for a raidz and a parity is
2740 * specified, make sure it matches.
2742 int rzlen
= strlen(VDEV_TYPE_RAIDZ
);
2743 assert(rzlen
== strlen(VDEV_TYPE_DRAID
));
2744 int typlen
= strlen(type
);
2745 if ((strncmp(type
, VDEV_TYPE_RAIDZ
, rzlen
) == 0 ||
2746 strncmp(type
, VDEV_TYPE_DRAID
, rzlen
) == 0) &&
2748 uint64_t vdev_parity
;
2749 int parity
= *(type
+ rzlen
) - '0';
2751 if (parity
<= 0 || parity
> 3 ||
2752 (typlen
- rzlen
) != 1) {
2754 * Nonsense parity specified, can
2760 vdev_parity
= fnvlist_lookup_uint64(nv
,
2761 ZPOOL_CONFIG_NPARITY
);
2762 if ((int)vdev_parity
!= parity
) {
2773 * Now verify that we have the correct vdev id.
2782 if (strcmp(srchval
, val
) == 0)
2791 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2792 &child
, &children
) != 0)
2795 for (c
= 0; c
< children
; c
++) {
2796 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2797 avail_spare
, l2cache
, NULL
)) != NULL
) {
2799 * The 'is_log' value is only set for the toplevel
2800 * vdev, not the leaf vdevs. So we always lookup the
2801 * log device from the root of the vdev tree (where
2802 * 'log' is non-NULL).
2805 nvlist_lookup_uint64(child
[c
],
2806 ZPOOL_CONFIG_IS_LOG
, &is_log
) == 0 &&
2814 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2815 &child
, &children
) == 0) {
2816 for (c
= 0; c
< children
; c
++) {
2817 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2818 avail_spare
, l2cache
, NULL
)) != NULL
) {
2819 *avail_spare
= B_TRUE
;
2825 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2826 &child
, &children
) == 0) {
2827 for (c
= 0; c
< children
; c
++) {
2828 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2829 avail_spare
, l2cache
, NULL
)) != NULL
) {
2840 * Given a physical path or guid, find the associated vdev.
2843 zpool_find_vdev_by_physpath(zpool_handle_t
*zhp
, const char *ppath
,
2844 boolean_t
*avail_spare
, boolean_t
*l2cache
, boolean_t
*log
)
2846 nvlist_t
*search
, *nvroot
, *ret
;
2850 search
= fnvlist_alloc();
2852 guid
= strtoull(ppath
, &end
, 0);
2853 if (guid
!= 0 && *end
== '\0') {
2854 fnvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
);
2856 fnvlist_add_string(search
, ZPOOL_CONFIG_PHYS_PATH
, ppath
);
2859 nvroot
= fnvlist_lookup_nvlist(zhp
->zpool_config
,
2860 ZPOOL_CONFIG_VDEV_TREE
);
2862 *avail_spare
= B_FALSE
;
2866 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2867 fnvlist_free(search
);
2873 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2876 zpool_vdev_is_interior(const char *name
)
2878 if (strncmp(name
, VDEV_TYPE_RAIDZ
, strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2879 strncmp(name
, VDEV_TYPE_SPARE
, strlen(VDEV_TYPE_SPARE
)) == 0 ||
2881 VDEV_TYPE_REPLACING
, strlen(VDEV_TYPE_REPLACING
)) == 0 ||
2882 strncmp(name
, VDEV_TYPE_MIRROR
, strlen(VDEV_TYPE_MIRROR
)) == 0)
2885 if (strncmp(name
, VDEV_TYPE_DRAID
, strlen(VDEV_TYPE_DRAID
)) == 0 &&
2886 !zpool_is_draid_spare(name
))
2893 zpool_find_vdev(zpool_handle_t
*zhp
, const char *path
, boolean_t
*avail_spare
,
2894 boolean_t
*l2cache
, boolean_t
*log
)
2897 nvlist_t
*nvroot
, *search
, *ret
;
2900 search
= fnvlist_alloc();
2902 guid
= strtoull(path
, &end
, 0);
2903 if (guid
!= 0 && *end
== '\0') {
2904 fnvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
);
2905 } else if (zpool_vdev_is_interior(path
)) {
2906 fnvlist_add_string(search
, ZPOOL_CONFIG_TYPE
, path
);
2908 fnvlist_add_string(search
, ZPOOL_CONFIG_PATH
, path
);
2911 nvroot
= fnvlist_lookup_nvlist(zhp
->zpool_config
,
2912 ZPOOL_CONFIG_VDEV_TREE
);
2914 *avail_spare
= B_FALSE
;
2918 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2919 fnvlist_free(search
);
2925 vdev_is_online(nvlist_t
*nv
)
2929 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
, &ival
) == 0 ||
2930 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
, &ival
) == 0 ||
2931 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
, &ival
) == 0)
2938 * Helper function for zpool_get_physpaths().
2941 vdev_get_one_physpath(nvlist_t
*config
, char *physpath
, size_t physpath_size
,
2942 size_t *bytes_written
)
2944 size_t bytes_left
, pos
, rsz
;
2948 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PHYS_PATH
,
2950 return (EZFS_NODEVICE
);
2952 pos
= *bytes_written
;
2953 bytes_left
= physpath_size
- pos
;
2954 format
= (pos
== 0) ? "%s" : " %s";
2956 rsz
= snprintf(physpath
+ pos
, bytes_left
, format
, tmppath
);
2957 *bytes_written
+= rsz
;
2959 if (rsz
>= bytes_left
) {
2960 /* if physpath was not copied properly, clear it */
2961 if (bytes_left
!= 0) {
2964 return (EZFS_NOSPC
);
2970 vdev_get_physpaths(nvlist_t
*nv
, char *physpath
, size_t phypath_size
,
2971 size_t *rsz
, boolean_t is_spare
)
2976 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
2977 return (EZFS_INVALCONFIG
);
2979 if (strcmp(type
, VDEV_TYPE_DISK
) == 0) {
2981 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2982 * For a spare vdev, we only want to boot from the active
2987 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
2990 return (EZFS_INVALCONFIG
);
2993 if (vdev_is_online(nv
)) {
2994 if ((ret
= vdev_get_one_physpath(nv
, physpath
,
2995 phypath_size
, rsz
)) != 0)
2998 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) == 0 ||
2999 strcmp(type
, VDEV_TYPE_RAIDZ
) == 0 ||
3000 strcmp(type
, VDEV_TYPE_REPLACING
) == 0 ||
3001 (is_spare
= (strcmp(type
, VDEV_TYPE_SPARE
) == 0))) {
3006 if (nvlist_lookup_nvlist_array(nv
,
3007 ZPOOL_CONFIG_CHILDREN
, &child
, &count
) != 0)
3008 return (EZFS_INVALCONFIG
);
3010 for (i
= 0; i
< count
; i
++) {
3011 ret
= vdev_get_physpaths(child
[i
], physpath
,
3012 phypath_size
, rsz
, is_spare
);
3013 if (ret
== EZFS_NOSPC
)
3018 return (EZFS_POOL_INVALARG
);
3022 * Get phys_path for a root pool config.
3023 * Return 0 on success; non-zero on failure.
3026 zpool_get_config_physpath(nvlist_t
*config
, char *physpath
, size_t phypath_size
)
3029 nvlist_t
*vdev_root
;
3036 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3038 return (EZFS_INVALCONFIG
);
3040 if (nvlist_lookup_string(vdev_root
, ZPOOL_CONFIG_TYPE
, &type
) != 0 ||
3041 nvlist_lookup_nvlist_array(vdev_root
, ZPOOL_CONFIG_CHILDREN
,
3042 &child
, &count
) != 0)
3043 return (EZFS_INVALCONFIG
);
3046 * root pool can only have a single top-level vdev.
3048 if (strcmp(type
, VDEV_TYPE_ROOT
) != 0 || count
!= 1)
3049 return (EZFS_POOL_INVALARG
);
3051 (void) vdev_get_physpaths(child
[0], physpath
, phypath_size
, &rsz
,
3054 /* No online devices */
3056 return (EZFS_NODEVICE
);
3062 * Get phys_path for a root pool
3063 * Return 0 on success; non-zero on failure.
3066 zpool_get_physpath(zpool_handle_t
*zhp
, char *physpath
, size_t phypath_size
)
3068 return (zpool_get_config_physpath(zhp
->zpool_config
, physpath
,
3073 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
3075 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
3076 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
3080 zpool_vdev_path_to_guid_impl(zpool_handle_t
*zhp
, const char *path
,
3081 boolean_t
*is_spare
, boolean_t
*is_l2cache
, boolean_t
*is_log
)
3083 boolean_t spare
= B_FALSE
, l2cache
= B_FALSE
, log
= B_FALSE
;
3086 if ((tgt
= zpool_find_vdev(zhp
, path
, &spare
, &l2cache
,
3090 if (is_spare
!= NULL
)
3092 if (is_l2cache
!= NULL
)
3093 *is_l2cache
= l2cache
;
3097 return (fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
));
3100 /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
3102 zpool_vdev_path_to_guid(zpool_handle_t
*zhp
, const char *path
)
3104 return (zpool_vdev_path_to_guid_impl(zhp
, path
, NULL
, NULL
, NULL
));
3108 * Bring the specified vdev online. The 'flags' parameter is a set of the
3109 * ZFS_ONLINE_* flags.
3112 zpool_vdev_online(zpool_handle_t
*zhp
, const char *path
, int flags
,
3113 vdev_state_t
*newstate
)
3115 zfs_cmd_t zc
= {"\0"};
3118 boolean_t avail_spare
, l2cache
, islog
;
3119 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3121 if (flags
& ZFS_ONLINE_EXPAND
) {
3122 (void) snprintf(msg
, sizeof (msg
),
3123 dgettext(TEXT_DOMAIN
, "cannot expand %s"), path
);
3125 (void) snprintf(msg
, sizeof (msg
),
3126 dgettext(TEXT_DOMAIN
, "cannot online %s"), path
);
3129 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3130 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3132 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3134 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3137 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3141 if ((flags
& ZFS_ONLINE_EXPAND
||
3142 zpool_get_prop_int(zhp
, ZPOOL_PROP_AUTOEXPAND
, NULL
)) &&
3143 nvlist_lookup_string(tgt
, ZPOOL_CONFIG_PATH
, &pathname
) == 0) {
3144 uint64_t wholedisk
= 0;
3146 (void) nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_WHOLE_DISK
,
3150 * XXX - L2ARC 1.0 devices can't support expansion.
3153 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3154 "cannot expand cache devices"));
3155 return (zfs_error(hdl
, EZFS_VDEVNOTSUP
, msg
));
3159 const char *fullpath
= path
;
3160 char buf
[MAXPATHLEN
];
3163 if (path
[0] != '/') {
3164 error
= zfs_resolve_shortname(path
, buf
,
3167 return (zfs_error(hdl
, EZFS_NODEVICE
,
3173 error
= zpool_relabel_disk(hdl
, fullpath
, msg
);
3180 zc
.zc_cookie
= VDEV_STATE_ONLINE
;
3183 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) != 0) {
3184 if (errno
== EINVAL
) {
3185 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "was split "
3186 "from this pool into a new one. Use '%s' "
3187 "instead"), "zpool detach");
3188 return (zfs_error(hdl
, EZFS_POSTSPLIT_ONLINE
, msg
));
3190 return (zpool_standard_error(hdl
, errno
, msg
));
3193 *newstate
= zc
.zc_cookie
;
3198 * Take the specified vdev offline
3201 zpool_vdev_offline(zpool_handle_t
*zhp
, const char *path
, boolean_t istmp
)
3203 zfs_cmd_t zc
= {"\0"};
3206 boolean_t avail_spare
, l2cache
;
3207 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3209 (void) snprintf(msg
, sizeof (msg
),
3210 dgettext(TEXT_DOMAIN
, "cannot offline %s"), path
);
3212 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3213 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3215 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3217 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3220 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3222 zc
.zc_cookie
= VDEV_STATE_OFFLINE
;
3223 zc
.zc_obj
= istmp
? ZFS_OFFLINE_TEMPORARY
: 0;
3225 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3232 * There are no other replicas of this device.
3234 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
3238 * The log device has unplayed logs
3240 return (zfs_error(hdl
, EZFS_UNPLAYED_LOGS
, msg
));
3243 return (zpool_standard_error(hdl
, errno
, msg
));
3248 * Mark the given vdev faulted.
3251 zpool_vdev_fault(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
3253 zfs_cmd_t zc
= {"\0"};
3255 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3257 (void) snprintf(msg
, sizeof (msg
),
3258 dgettext(TEXT_DOMAIN
, "cannot fault %llu"), (u_longlong_t
)guid
);
3260 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3262 zc
.zc_cookie
= VDEV_STATE_FAULTED
;
3265 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3272 * There are no other replicas of this device.
3274 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
3277 return (zpool_standard_error(hdl
, errno
, msg
));
3283 * Mark the given vdev degraded.
3286 zpool_vdev_degrade(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
3288 zfs_cmd_t zc
= {"\0"};
3290 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3292 (void) snprintf(msg
, sizeof (msg
),
3293 dgettext(TEXT_DOMAIN
, "cannot degrade %llu"), (u_longlong_t
)guid
);
3295 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3297 zc
.zc_cookie
= VDEV_STATE_DEGRADED
;
3300 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3303 return (zpool_standard_error(hdl
, errno
, msg
));
3307 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
3311 is_replacing_spare(nvlist_t
*search
, nvlist_t
*tgt
, int which
)
3316 if (nvlist_lookup_nvlist_array(search
, ZPOOL_CONFIG_CHILDREN
, &child
,
3318 char *type
= fnvlist_lookup_string(search
, ZPOOL_CONFIG_TYPE
);
3319 if ((strcmp(type
, VDEV_TYPE_SPARE
) == 0 ||
3320 strcmp(type
, VDEV_TYPE_DRAID_SPARE
) == 0) &&
3321 children
== 2 && child
[which
] == tgt
)
3324 for (c
= 0; c
< children
; c
++)
3325 if (is_replacing_spare(child
[c
], tgt
, which
))
3333 * Attach new_disk (fully described by nvroot) to old_disk.
3334 * If 'replacing' is specified, the new disk will replace the old one.
3337 zpool_vdev_attach(zpool_handle_t
*zhp
, const char *old_disk
,
3338 const char *new_disk
, nvlist_t
*nvroot
, int replacing
, boolean_t rebuild
)
3340 zfs_cmd_t zc
= {"\0"};
3344 boolean_t avail_spare
, l2cache
, islog
;
3349 nvlist_t
*config_root
;
3350 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3353 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
3354 "cannot replace %s with %s"), old_disk
, new_disk
);
3356 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
3357 "cannot attach %s to %s"), new_disk
, old_disk
);
3359 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3360 if ((tgt
= zpool_find_vdev(zhp
, old_disk
, &avail_spare
, &l2cache
,
3362 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3365 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3368 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
3370 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3371 zc
.zc_cookie
= replacing
;
3372 zc
.zc_simple
= rebuild
;
3375 zfeature_lookup_guid("org.openzfs:device_rebuild", NULL
) != 0) {
3376 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3377 "the loaded zfs module doesn't support device rebuilds"));
3378 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, msg
));
3381 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
3382 &child
, &children
) != 0 || children
!= 1) {
3383 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3384 "new device must be a single disk"));
3385 return (zfs_error(hdl
, EZFS_INVALCONFIG
, msg
));
3388 config_root
= fnvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
3389 ZPOOL_CONFIG_VDEV_TREE
);
3391 if ((newname
= zpool_vdev_name(NULL
, NULL
, child
[0], 0)) == NULL
)
3395 * If the target is a hot spare that has been swapped in, we can only
3396 * replace it with another hot spare.
3399 nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_IS_SPARE
, &val
) == 0 &&
3400 (zpool_find_vdev(zhp
, newname
, &avail_spare
, &l2cache
,
3401 NULL
) == NULL
|| !avail_spare
) &&
3402 is_replacing_spare(config_root
, tgt
, 1)) {
3403 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3404 "can only be replaced by another hot spare"));
3406 return (zfs_error(hdl
, EZFS_BADTARGET
, msg
));
3411 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
3414 ret
= zfs_ioctl(hdl
, ZFS_IOC_VDEV_ATTACH
, &zc
);
3416 zcmd_free_nvlists(&zc
);
3424 * Can't attach to or replace this type of vdev.
3427 uint64_t version
= zpool_get_prop_int(zhp
,
3428 ZPOOL_PROP_VERSION
, NULL
);
3431 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3432 "cannot replace a log with a spare"));
3433 } else if (rebuild
) {
3434 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3435 "only mirror and dRAID vdevs support "
3436 "sequential reconstruction"));
3437 } else if (zpool_is_draid_spare(new_disk
)) {
3438 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3439 "dRAID spares can only replace child "
3440 "devices in their parent's dRAID vdev"));
3441 } else if (version
>= SPA_VERSION_MULTI_REPLACE
) {
3442 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3443 "already in replacing/spare config; wait "
3444 "for completion or use 'zpool detach'"));
3446 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3447 "cannot replace a replacing device"));
3450 char status
[64] = {0};
3451 zpool_prop_get_feature(zhp
,
3452 "feature@device_rebuild", status
, 63);
3454 strncmp(status
, ZFS_FEATURE_DISABLED
, 64) == 0) {
3455 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3456 "device_rebuild feature must be enabled "
3457 "in order to use sequential "
3460 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3461 "can only attach to mirrors and top-level "
3465 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
3470 * The new device must be a single disk.
3472 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3473 "new device must be a single disk"));
3474 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3478 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "%s is busy, "
3479 "or device removal is in progress"),
3481 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
3486 * The new device is too small.
3488 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3489 "device is too small"));
3490 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
3495 * The new device has a different optimal sector size.
3497 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3498 "new device has a different optimal sector size; use the "
3499 "option '-o ashift=N' to override the optimal size"));
3500 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
3505 * The resulting top-level vdev spec won't fit in the label.
3507 (void) zfs_error(hdl
, EZFS_DEVOVERFLOW
, msg
);
3511 (void) zpool_standard_error(hdl
, errno
, msg
);
3518 * Detach the specified device.
3521 zpool_vdev_detach(zpool_handle_t
*zhp
, const char *path
)
3523 zfs_cmd_t zc
= {"\0"};
3526 boolean_t avail_spare
, l2cache
;
3527 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3529 (void) snprintf(msg
, sizeof (msg
),
3530 dgettext(TEXT_DOMAIN
, "cannot detach %s"), path
);
3532 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3533 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3535 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3538 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3541 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
3543 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3545 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_DETACH
, &zc
) == 0)
3552 * Can't detach from this type of vdev.
3554 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "only "
3555 "applicable to mirror and replacing vdevs"));
3556 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
3561 * There are no other replicas of this device.
3563 (void) zfs_error(hdl
, EZFS_NOREPLICAS
, msg
);
3567 (void) zpool_standard_error(hdl
, errno
, msg
);
3574 * Find a mirror vdev in the source nvlist.
3576 * The mchild array contains a list of disks in one of the top-level mirrors
3577 * of the source pool. The schild array contains a list of disks that the
3578 * user specified on the command line. We loop over the mchild array to
3579 * see if any entry in the schild array matches.
3581 * If a disk in the mchild array is found in the schild array, we return
3582 * the index of that entry. Otherwise we return -1.
3585 find_vdev_entry(zpool_handle_t
*zhp
, nvlist_t
**mchild
, uint_t mchildren
,
3586 nvlist_t
**schild
, uint_t schildren
)
3590 for (mc
= 0; mc
< mchildren
; mc
++) {
3592 char *mpath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
3595 for (sc
= 0; sc
< schildren
; sc
++) {
3596 char *spath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
3598 boolean_t result
= (strcmp(mpath
, spath
) == 0);
3614 * Split a mirror pool. If newroot points to null, then a new nvlist
3615 * is generated and it is the responsibility of the caller to free it.
3618 zpool_vdev_split(zpool_handle_t
*zhp
, char *newname
, nvlist_t
**newroot
,
3619 nvlist_t
*props
, splitflags_t flags
)
3621 zfs_cmd_t zc
= {"\0"};
3622 char msg
[1024], *bias
;
3623 nvlist_t
*tree
, *config
, **child
, **newchild
, *newconfig
= NULL
;
3624 nvlist_t
**varray
= NULL
, *zc_props
= NULL
;
3625 uint_t c
, children
, newchildren
, lastlog
= 0, vcount
, found
= 0;
3626 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3627 uint64_t vers
, readonly
= B_FALSE
;
3628 boolean_t freelist
= B_FALSE
, memory_err
= B_TRUE
;
3631 (void) snprintf(msg
, sizeof (msg
),
3632 dgettext(TEXT_DOMAIN
, "Unable to split %s"), zhp
->zpool_name
);
3634 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
3635 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
3637 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
3638 (void) fprintf(stderr
, gettext("Internal error: unable to "
3639 "retrieve pool configuration\n"));
3643 tree
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
3644 vers
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
3647 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
3648 if ((zc_props
= zpool_valid_proplist(hdl
, zhp
->zpool_name
,
3649 props
, vers
, flags
, msg
)) == NULL
)
3651 (void) nvlist_lookup_uint64(zc_props
,
3652 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
3654 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3655 "property %s can only be set at import time"),
3656 zpool_prop_to_name(ZPOOL_PROP_READONLY
));
3661 if (nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
3663 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3664 "Source pool is missing vdev tree"));
3665 nvlist_free(zc_props
);
3669 varray
= zfs_alloc(hdl
, children
* sizeof (nvlist_t
*));
3672 if (*newroot
== NULL
||
3673 nvlist_lookup_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
3674 &newchild
, &newchildren
) != 0)
3677 for (c
= 0; c
< children
; c
++) {
3678 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
3679 boolean_t is_special
= B_FALSE
, is_dedup
= B_FALSE
;
3681 nvlist_t
**mchild
, *vdev
;
3686 * Unlike cache & spares, slogs are stored in the
3687 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3689 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
3691 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
3693 if (is_log
|| is_hole
) {
3695 * Create a hole vdev and put it in the config.
3697 if (nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) != 0)
3699 if (nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
,
3700 VDEV_TYPE_HOLE
) != 0)
3702 if (nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_HOLE
,
3707 varray
[vcount
++] = vdev
;
3711 type
= fnvlist_lookup_string(child
[c
], ZPOOL_CONFIG_TYPE
);
3713 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0) {
3715 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
3718 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
3719 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3720 "Source pool must be composed only of mirrors\n"));
3721 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3725 if (nvlist_lookup_string(child
[c
],
3726 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
) == 0) {
3727 if (strcmp(bias
, VDEV_ALLOC_BIAS_SPECIAL
) == 0)
3728 is_special
= B_TRUE
;
3729 else if (strcmp(bias
, VDEV_ALLOC_BIAS_DEDUP
) == 0)
3732 verify(nvlist_lookup_nvlist_array(child
[c
],
3733 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
3735 /* find or add an entry for this top-level vdev */
3736 if (newchildren
> 0 &&
3737 (entry
= find_vdev_entry(zhp
, mchild
, mchildren
,
3738 newchild
, newchildren
)) >= 0) {
3739 /* We found a disk that the user specified. */
3740 vdev
= mchild
[entry
];
3743 /* User didn't specify a disk for this vdev. */
3744 vdev
= mchild
[mchildren
- 1];
3747 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
3750 if (flags
.dryrun
!= 0) {
3751 if (is_dedup
== B_TRUE
) {
3752 if (nvlist_add_string(varray
[vcount
- 1],
3753 ZPOOL_CONFIG_ALLOCATION_BIAS
,
3754 VDEV_ALLOC_BIAS_DEDUP
) != 0)
3756 } else if (is_special
== B_TRUE
) {
3757 if (nvlist_add_string(varray
[vcount
- 1],
3758 ZPOOL_CONFIG_ALLOCATION_BIAS
,
3759 VDEV_ALLOC_BIAS_SPECIAL
) != 0)
3765 /* did we find every disk the user specified? */
3766 if (found
!= newchildren
) {
3767 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Device list must "
3768 "include at most one disk from each mirror"));
3769 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3773 /* Prepare the nvlist for populating. */
3774 if (*newroot
== NULL
) {
3775 if (nvlist_alloc(newroot
, NV_UNIQUE_NAME
, 0) != 0)
3778 if (nvlist_add_string(*newroot
, ZPOOL_CONFIG_TYPE
,
3779 VDEV_TYPE_ROOT
) != 0)
3782 verify(nvlist_remove_all(*newroot
, ZPOOL_CONFIG_CHILDREN
) == 0);
3785 /* Add all the children we found */
3786 if (nvlist_add_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
3787 (const nvlist_t
**)varray
, lastlog
== 0 ? vcount
: lastlog
) != 0)
3791 * If we're just doing a dry run, exit now with success.
3794 memory_err
= B_FALSE
;
3799 /* now build up the config list & call the ioctl */
3800 if (nvlist_alloc(&newconfig
, NV_UNIQUE_NAME
, 0) != 0)
3803 if (nvlist_add_nvlist(newconfig
,
3804 ZPOOL_CONFIG_VDEV_TREE
, *newroot
) != 0 ||
3805 nvlist_add_string(newconfig
,
3806 ZPOOL_CONFIG_POOL_NAME
, newname
) != 0 ||
3807 nvlist_add_uint64(newconfig
, ZPOOL_CONFIG_VERSION
, vers
) != 0)
3811 * The new pool is automatically part of the namespace unless we
3812 * explicitly export it.
3815 zc
.zc_cookie
= ZPOOL_EXPORT_AFTER_SPLIT
;
3816 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3817 (void) strlcpy(zc
.zc_string
, newname
, sizeof (zc
.zc_string
));
3818 if (zcmd_write_conf_nvlist(hdl
, &zc
, newconfig
) != 0)
3820 if (zc_props
!= NULL
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
3823 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SPLIT
, &zc
) != 0) {
3824 retval
= zpool_standard_error(hdl
, errno
, msg
);
3829 memory_err
= B_FALSE
;
3832 if (varray
!= NULL
) {
3835 for (v
= 0; v
< vcount
; v
++)
3836 nvlist_free(varray
[v
]);
3839 zcmd_free_nvlists(&zc
);
3840 nvlist_free(zc_props
);
3841 nvlist_free(newconfig
);
3843 nvlist_free(*newroot
);
3851 return (no_memory(hdl
));
3857 * Remove the given device.
3860 zpool_vdev_remove(zpool_handle_t
*zhp
, const char *path
)
3862 zfs_cmd_t zc
= {"\0"};
3865 boolean_t avail_spare
, l2cache
, islog
;
3866 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3869 (void) snprintf(msg
, sizeof (msg
),
3870 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3872 if (zpool_is_draid_spare(path
)) {
3873 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3874 "dRAID spares cannot be removed"));
3875 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3878 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3879 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3881 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3883 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
3884 if (islog
&& version
< SPA_VERSION_HOLES
) {
3885 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3886 "pool must be upgraded to support log removal"));
3887 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
3890 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3892 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3898 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3899 "invalid config; all top-level vdevs must "
3900 "have the same sector size and not be raidz."));
3901 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3906 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3907 "Mount encrypted datasets to replay logs."));
3909 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3910 "Pool busy; removal may already be in progress"));
3912 (void) zfs_error(hdl
, EZFS_BUSY
, msg
);
3917 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3918 "Mount encrypted datasets to replay logs."));
3919 (void) zfs_error(hdl
, EZFS_BUSY
, msg
);
3921 (void) zpool_standard_error(hdl
, errno
, msg
);
3926 (void) zpool_standard_error(hdl
, errno
, msg
);
3932 zpool_vdev_remove_cancel(zpool_handle_t
*zhp
)
3934 zfs_cmd_t zc
= {{0}};
3936 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3938 (void) snprintf(msg
, sizeof (msg
),
3939 dgettext(TEXT_DOMAIN
, "cannot cancel removal"));
3941 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3944 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3947 return (zpool_standard_error(hdl
, errno
, msg
));
3951 zpool_vdev_indirect_size(zpool_handle_t
*zhp
, const char *path
,
3956 boolean_t avail_spare
, l2cache
, islog
;
3957 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3959 (void) snprintf(msg
, sizeof (msg
),
3960 dgettext(TEXT_DOMAIN
, "cannot determine indirect size of %s"),
3963 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3965 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3967 if (avail_spare
|| l2cache
|| islog
) {
3972 if (nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_INDIRECT_SIZE
, sizep
) != 0) {
3973 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3974 "indirect size not available"));
3975 return (zfs_error(hdl
, EINVAL
, msg
));
3981 * Clear the errors for the pool, or the particular device if specified.
3984 zpool_clear(zpool_handle_t
*zhp
, const char *path
, nvlist_t
*rewindnvl
)
3986 zfs_cmd_t zc
= {"\0"};
3989 zpool_load_policy_t policy
;
3990 boolean_t avail_spare
, l2cache
;
3991 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3992 nvlist_t
*nvi
= NULL
;
3996 (void) snprintf(msg
, sizeof (msg
),
3997 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
4000 (void) snprintf(msg
, sizeof (msg
),
4001 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
4004 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4006 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
,
4007 &l2cache
, NULL
)) == NULL
)
4008 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
4011 * Don't allow error clearing for hot spares. Do allow
4012 * error clearing for l2cache devices.
4015 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
4017 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
4020 zpool_get_load_policy(rewindnvl
, &policy
);
4021 zc
.zc_cookie
= policy
.zlp_rewind
;
4023 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zhp
->zpool_config_size
* 2) != 0)
4026 if (zcmd_write_src_nvlist(hdl
, &zc
, rewindnvl
) != 0)
4029 while ((error
= zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
)) != 0 &&
4031 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
4032 zcmd_free_nvlists(&zc
);
4037 if (!error
|| ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) &&
4038 errno
!= EPERM
&& errno
!= EACCES
)) {
4039 if (policy
.zlp_rewind
&
4040 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
4041 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nvi
);
4042 zpool_rewind_exclaim(hdl
, zc
.zc_name
,
4043 ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) != 0),
4047 zcmd_free_nvlists(&zc
);
4051 zcmd_free_nvlists(&zc
);
4052 return (zpool_standard_error(hdl
, errno
, msg
));
4056 * Similar to zpool_clear(), but takes a GUID (used by fmd).
4059 zpool_vdev_clear(zpool_handle_t
*zhp
, uint64_t guid
)
4061 zfs_cmd_t zc
= {"\0"};
4063 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4065 (void) snprintf(msg
, sizeof (msg
),
4066 dgettext(TEXT_DOMAIN
, "cannot clear errors for %llx"),
4067 (u_longlong_t
)guid
);
4069 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4071 zc
.zc_cookie
= ZPOOL_NO_REWIND
;
4073 if (zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
) == 0)
4076 return (zpool_standard_error(hdl
, errno
, msg
));
4080 * Change the GUID for a pool.
4083 zpool_reguid(zpool_handle_t
*zhp
)
4086 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4087 zfs_cmd_t zc
= {"\0"};
4089 (void) snprintf(msg
, sizeof (msg
),
4090 dgettext(TEXT_DOMAIN
, "cannot reguid '%s'"), zhp
->zpool_name
);
4092 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4093 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REGUID
, &zc
) == 0)
4096 return (zpool_standard_error(hdl
, errno
, msg
));
4103 zpool_reopen_one(zpool_handle_t
*zhp
, void *data
)
4105 libzfs_handle_t
*hdl
= zpool_get_handle(zhp
);
4106 const char *pool_name
= zpool_get_name(zhp
);
4107 boolean_t
*scrub_restart
= data
;
4110 error
= lzc_reopen(pool_name
, *scrub_restart
);
4112 return (zpool_standard_error_fmt(hdl
, error
,
4113 dgettext(TEXT_DOMAIN
, "cannot reopen '%s'"), pool_name
));
4119 /* call into libzfs_core to execute the sync IOCTL per pool */
4121 zpool_sync_one(zpool_handle_t
*zhp
, void *data
)
4124 libzfs_handle_t
*hdl
= zpool_get_handle(zhp
);
4125 const char *pool_name
= zpool_get_name(zhp
);
4126 boolean_t
*force
= data
;
4127 nvlist_t
*innvl
= fnvlist_alloc();
4129 fnvlist_add_boolean_value(innvl
, "force", *force
);
4130 if ((ret
= lzc_sync(pool_name
, innvl
, NULL
)) != 0) {
4132 return (zpool_standard_error_fmt(hdl
, ret
,
4133 dgettext(TEXT_DOMAIN
, "sync '%s' failed"), pool_name
));
4140 #define PATH_BUF_LEN 64
4143 * Given a vdev, return the name to display in iostat. If the vdev has a path,
4144 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
4145 * We also check if this is a whole disk, in which case we strip off the
4146 * trailing 's0' slice name.
4148 * This routine is also responsible for identifying when disks have been
4149 * reconfigured in a new location. The kernel will have opened the device by
4150 * devid, but the path will still refer to the old location. To catch this, we
4151 * first do a path -> devid translation (which is fast for the common case). If
4152 * the devid matches, we're done. If not, we do a reverse devid -> path
4153 * translation and issue the appropriate ioctl() to update the path of the vdev.
4154 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
4158 zpool_vdev_name(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, nvlist_t
*nv
,
4161 char *path
, *type
, *env
;
4163 char buf
[PATH_BUF_LEN
];
4164 char tmpbuf
[PATH_BUF_LEN
];
4167 * vdev_name will be "root"/"root-0" for the root vdev, but it is the
4168 * zpool name that will be displayed to the user.
4170 type
= fnvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
);
4171 if (zhp
!= NULL
&& strcmp(type
, "root") == 0)
4172 return (zfs_strdup(hdl
, zpool_get_name(zhp
)));
4174 env
= getenv("ZPOOL_VDEV_NAME_PATH");
4175 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
4176 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
4177 name_flags
|= VDEV_NAME_PATH
;
4179 env
= getenv("ZPOOL_VDEV_NAME_GUID");
4180 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
4181 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
4182 name_flags
|= VDEV_NAME_GUID
;
4184 env
= getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
4185 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
4186 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
4187 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
4189 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
, &value
) == 0 ||
4190 name_flags
& VDEV_NAME_GUID
) {
4191 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
);
4192 (void) snprintf(buf
, sizeof (buf
), "%llu", (u_longlong_t
)value
);
4194 } else if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
4195 if (name_flags
& VDEV_NAME_FOLLOW_LINKS
) {
4196 char *rp
= realpath(path
, NULL
);
4198 strlcpy(buf
, rp
, sizeof (buf
));
4205 * For a block device only use the name.
4207 if ((strcmp(type
, VDEV_TYPE_DISK
) == 0) &&
4208 !(name_flags
& VDEV_NAME_PATH
)) {
4209 path
= zfs_strip_path(path
);
4213 * Remove the partition from the path if this is a whole disk.
4215 if (strcmp(type
, VDEV_TYPE_DRAID_SPARE
) != 0 &&
4216 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
, &value
)
4217 == 0 && value
&& !(name_flags
& VDEV_NAME_PATH
)) {
4218 return (zfs_strip_partition(path
));
4224 * If it's a raidz device, we need to stick in the parity level.
4226 if (strcmp(path
, VDEV_TYPE_RAIDZ
) == 0) {
4227 value
= fnvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
);
4228 (void) snprintf(buf
, sizeof (buf
), "%s%llu", path
,
4229 (u_longlong_t
)value
);
4234 * If it's a dRAID device, we add parity, groups, and spares.
4236 if (strcmp(path
, VDEV_TYPE_DRAID
) == 0) {
4237 uint64_t ndata
, nparity
, nspares
;
4241 verify(nvlist_lookup_nvlist_array(nv
,
4242 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) == 0);
4243 nparity
= fnvlist_lookup_uint64(nv
,
4244 ZPOOL_CONFIG_NPARITY
);
4245 ndata
= fnvlist_lookup_uint64(nv
,
4246 ZPOOL_CONFIG_DRAID_NDATA
);
4247 nspares
= fnvlist_lookup_uint64(nv
,
4248 ZPOOL_CONFIG_DRAID_NSPARES
);
4250 path
= zpool_draid_name(buf
, sizeof (buf
), ndata
,
4251 nparity
, nspares
, children
);
4255 * We identify each top-level vdev by using a <type-id>
4256 * naming convention.
4258 if (name_flags
& VDEV_NAME_TYPE_ID
) {
4259 uint64_t id
= fnvlist_lookup_uint64(nv
,
4261 (void) snprintf(tmpbuf
, sizeof (tmpbuf
), "%s-%llu",
4262 path
, (u_longlong_t
)id
);
4267 return (zfs_strdup(hdl
, path
));
4271 zbookmark_mem_compare(const void *a
, const void *b
)
4273 return (memcmp(a
, b
, sizeof (zbookmark_phys_t
)));
4277 * Retrieve the persistent error log, uniquify the members, and return to the
4281 zpool_get_errlog(zpool_handle_t
*zhp
, nvlist_t
**nverrlistp
)
4283 zfs_cmd_t zc
= {"\0"};
4284 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4286 zbookmark_phys_t
*zb
= NULL
;
4290 * Retrieve the raw error list from the kernel. If the number of errors
4291 * has increased, allocate more space and continue until we get the
4294 count
= fnvlist_lookup_uint64(zhp
->zpool_config
, ZPOOL_CONFIG_ERRCOUNT
);
4297 zc
.zc_nvlist_dst
= (uintptr_t)zfs_alloc(zhp
->zpool_hdl
,
4298 count
* sizeof (zbookmark_phys_t
));
4299 zc
.zc_nvlist_dst_size
= count
;
4300 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
4302 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_ERROR_LOG
,
4304 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
4305 if (errno
== ENOMEM
) {
4308 count
= zc
.zc_nvlist_dst_size
;
4309 dst
= zfs_alloc(zhp
->zpool_hdl
, count
*
4310 sizeof (zbookmark_phys_t
));
4311 zc
.zc_nvlist_dst
= (uintptr_t)dst
;
4313 return (zpool_standard_error_fmt(hdl
, errno
,
4314 dgettext(TEXT_DOMAIN
, "errors: List of "
4315 "errors unavailable")));
4323 * Sort the resulting bookmarks. This is a little confusing due to the
4324 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
4325 * to first, and 'zc_nvlist_dst_size' indicates the number of bookmarks
4326 * _not_ copied as part of the process. So we point the start of our
4327 * array appropriate and decrement the total number of elements.
4329 zb
= ((zbookmark_phys_t
*)(uintptr_t)zc
.zc_nvlist_dst
) +
4330 zc
.zc_nvlist_dst_size
;
4331 count
-= zc
.zc_nvlist_dst_size
;
4333 qsort(zb
, count
, sizeof (zbookmark_phys_t
), zbookmark_mem_compare
);
4335 verify(nvlist_alloc(nverrlistp
, 0, KM_SLEEP
) == 0);
4338 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
4340 for (i
= 0; i
< count
; i
++) {
4343 /* ignoring zb_blkid and zb_level for now */
4344 if (i
> 0 && zb
[i
-1].zb_objset
== zb
[i
].zb_objset
&&
4345 zb
[i
-1].zb_object
== zb
[i
].zb_object
)
4348 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) != 0)
4350 if (nvlist_add_uint64(nv
, ZPOOL_ERR_DATASET
,
4351 zb
[i
].zb_objset
) != 0) {
4355 if (nvlist_add_uint64(nv
, ZPOOL_ERR_OBJECT
,
4356 zb
[i
].zb_object
) != 0) {
4360 if (nvlist_add_nvlist(*nverrlistp
, "ejk", nv
) != 0) {
4367 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
4371 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
4372 return (no_memory(zhp
->zpool_hdl
));
4376 * Upgrade a ZFS pool to the latest on-disk version.
4379 zpool_upgrade(zpool_handle_t
*zhp
, uint64_t new_version
)
4381 zfs_cmd_t zc
= {"\0"};
4382 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4384 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
4385 zc
.zc_cookie
= new_version
;
4387 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_UPGRADE
, &zc
) != 0)
4388 return (zpool_standard_error_fmt(hdl
, errno
,
4389 dgettext(TEXT_DOMAIN
, "cannot upgrade '%s'"),
4395 zfs_save_arguments(int argc
, char **argv
, char *string
, int len
)
4399 (void) strlcpy(string
, zfs_basename(argv
[0]), len
);
4400 for (i
= 1; i
< argc
; i
++) {
4401 (void) strlcat(string
, " ", len
);
4402 (void) strlcat(string
, argv
[i
], len
);
4407 zpool_log_history(libzfs_handle_t
*hdl
, const char *message
)
4409 zfs_cmd_t zc
= {"\0"};
4413 args
= fnvlist_alloc();
4414 fnvlist_add_string(args
, "message", message
);
4415 err
= zcmd_write_src_nvlist(hdl
, &zc
, args
);
4417 err
= zfs_ioctl(hdl
, ZFS_IOC_LOG_HISTORY
, &zc
);
4419 zcmd_free_nvlists(&zc
);
4424 * Perform ioctl to get some command history of a pool.
4426 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
4427 * logical offset of the history buffer to start reading from.
4429 * Upon return, 'off' is the next logical offset to read from and
4430 * 'len' is the actual amount of bytes read into 'buf'.
4433 get_history(zpool_handle_t
*zhp
, char *buf
, uint64_t *off
, uint64_t *len
)
4435 zfs_cmd_t zc
= {"\0"};
4436 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4438 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4440 zc
.zc_history
= (uint64_t)(uintptr_t)buf
;
4441 zc
.zc_history_len
= *len
;
4442 zc
.zc_history_offset
= *off
;
4444 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_GET_HISTORY
, &zc
) != 0) {
4447 return (zfs_error_fmt(hdl
, EZFS_PERM
,
4448 dgettext(TEXT_DOMAIN
,
4449 "cannot show history for pool '%s'"),
4452 return (zfs_error_fmt(hdl
, EZFS_NOHISTORY
,
4453 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
4454 "'%s'"), zhp
->zpool_name
));
4456 return (zfs_error_fmt(hdl
, EZFS_BADVERSION
,
4457 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
4458 "'%s', pool must be upgraded"), zhp
->zpool_name
));
4460 return (zpool_standard_error_fmt(hdl
, errno
,
4461 dgettext(TEXT_DOMAIN
,
4462 "cannot get history for '%s'"), zhp
->zpool_name
));
4466 *len
= zc
.zc_history_len
;
4467 *off
= zc
.zc_history_offset
;
4473 * Retrieve the command history of a pool.
4476 zpool_get_history(zpool_handle_t
*zhp
, nvlist_t
**nvhisp
, uint64_t *off
,
4480 int buflen
= 128 * 1024;
4481 nvlist_t
**records
= NULL
;
4482 uint_t numrecords
= 0;
4484 uint64_t start
= *off
;
4486 buf
= malloc(buflen
);
4489 /* process about 1MB a time */
4490 while (*off
- start
< 1024 * 1024) {
4491 uint64_t bytes_read
= buflen
;
4494 if ((err
= get_history(zhp
, buf
, off
, &bytes_read
)) != 0)
4497 /* if nothing else was read in, we're at EOF, just return */
4503 if ((err
= zpool_history_unpack(buf
, bytes_read
,
4504 &leftover
, &records
, &numrecords
)) != 0)
4507 if (leftover
== bytes_read
) {
4509 * no progress made, because buffer is not big enough
4510 * to hold this record; resize and retry.
4514 buf
= malloc(buflen
);
4523 *nvhisp
= fnvlist_alloc();
4524 fnvlist_add_nvlist_array(*nvhisp
, ZPOOL_HIST_RECORD
,
4525 (const nvlist_t
**)records
, numrecords
);
4527 for (i
= 0; i
< numrecords
; i
++)
4528 nvlist_free(records
[i
]);
4535 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
4536 * If there is a new event available 'nvp' will contain a newly allocated
4537 * nvlist and 'dropped' will be set to the number of missed events since
4538 * the last call to this function. When 'nvp' is set to NULL it indicates
4539 * no new events are available. In either case the function returns 0 and
4540 * it is up to the caller to free 'nvp'. In the case of a fatal error the
4541 * function will return a non-zero value. When the function is called in
4542 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
4543 * it will not return until a new event is available.
4546 zpool_events_next(libzfs_handle_t
*hdl
, nvlist_t
**nvp
,
4547 int *dropped
, unsigned flags
, int zevent_fd
)
4549 zfs_cmd_t zc
= {"\0"};
4554 zc
.zc_cleanup_fd
= zevent_fd
;
4556 if (flags
& ZEVENT_NONBLOCK
)
4557 zc
.zc_guid
= ZEVENT_NONBLOCK
;
4559 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, ZEVENT_SIZE
) != 0)
4563 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_NEXT
, &zc
) != 0) {
4566 error
= zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
4567 dgettext(TEXT_DOMAIN
, "zfs shutdown"));
4570 /* Blocking error case should not occur */
4571 if (!(flags
& ZEVENT_NONBLOCK
))
4572 error
= zpool_standard_error_fmt(hdl
, errno
,
4573 dgettext(TEXT_DOMAIN
, "cannot get event"));
4577 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
4578 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
4579 dgettext(TEXT_DOMAIN
, "cannot get event"));
4585 error
= zpool_standard_error_fmt(hdl
, errno
,
4586 dgettext(TEXT_DOMAIN
, "cannot get event"));
4591 error
= zcmd_read_dst_nvlist(hdl
, &zc
, nvp
);
4595 *dropped
= (int)zc
.zc_cookie
;
4597 zcmd_free_nvlists(&zc
);
4606 zpool_events_clear(libzfs_handle_t
*hdl
, int *count
)
4608 zfs_cmd_t zc
= {"\0"};
4610 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_CLEAR
, &zc
) != 0)
4611 return (zpool_standard_error(hdl
, errno
,
4612 dgettext(TEXT_DOMAIN
, "cannot clear events")));
4615 *count
= (int)zc
.zc_cookie
; /* # of events cleared */
4621 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4622 * the passed zevent_fd file handle. On success zero is returned,
4623 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4626 zpool_events_seek(libzfs_handle_t
*hdl
, uint64_t eid
, int zevent_fd
)
4628 zfs_cmd_t zc
= {"\0"};
4632 zc
.zc_cleanup_fd
= zevent_fd
;
4634 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_SEEK
, &zc
) != 0) {
4637 error
= zfs_error_fmt(hdl
, EZFS_NOENT
,
4638 dgettext(TEXT_DOMAIN
, "cannot get event"));
4642 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
4643 dgettext(TEXT_DOMAIN
, "cannot get event"));
4647 error
= zpool_standard_error_fmt(hdl
, errno
,
4648 dgettext(TEXT_DOMAIN
, "cannot get event"));
4657 zpool_obj_to_path_impl(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4658 char *pathname
, size_t len
, boolean_t always_unmounted
)
4660 zfs_cmd_t zc
= {"\0"};
4661 boolean_t mounted
= B_FALSE
;
4662 char *mntpnt
= NULL
;
4663 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
4666 /* special case for the MOS */
4667 (void) snprintf(pathname
, len
, "<metadata>:<0x%llx>",
4672 /* get the dataset's name */
4673 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4675 if (zfs_ioctl(zhp
->zpool_hdl
,
4676 ZFS_IOC_DSOBJ_TO_DSNAME
, &zc
) != 0) {
4677 /* just write out a path of two object numbers */
4678 (void) snprintf(pathname
, len
, "<0x%llx>:<0x%llx>",
4679 (longlong_t
)dsobj
, (longlong_t
)obj
);
4682 (void) strlcpy(dsname
, zc
.zc_value
, sizeof (dsname
));
4684 /* find out if the dataset is mounted */
4685 mounted
= !always_unmounted
&& is_mounted(zhp
->zpool_hdl
, dsname
,
4688 /* get the corrupted object's path */
4689 (void) strlcpy(zc
.zc_name
, dsname
, sizeof (zc
.zc_name
));
4691 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_OBJ_TO_PATH
,
4694 (void) snprintf(pathname
, len
, "%s%s", mntpnt
,
4697 (void) snprintf(pathname
, len
, "%s:%s",
4698 dsname
, zc
.zc_value
);
4701 (void) snprintf(pathname
, len
, "%s:<0x%llx>", dsname
,
4708 zpool_obj_to_path(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4709 char *pathname
, size_t len
)
4711 zpool_obj_to_path_impl(zhp
, dsobj
, obj
, pathname
, len
, B_FALSE
);
4715 zpool_obj_to_path_ds(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4716 char *pathname
, size_t len
)
4718 zpool_obj_to_path_impl(zhp
, dsobj
, obj
, pathname
, len
, B_TRUE
);
4721 * Wait while the specified activity is in progress in the pool.
4724 zpool_wait(zpool_handle_t
*zhp
, zpool_wait_activity_t activity
)
4728 int error
= zpool_wait_status(zhp
, activity
, &missing
, NULL
);
4731 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, ENOENT
,
4732 dgettext(TEXT_DOMAIN
, "error waiting in pool '%s'"),
4741 * Wait for the given activity and return the status of the wait (whether or not
4742 * any waiting was done) in the 'waited' parameter. Non-existent pools are
4743 * reported via the 'missing' parameter, rather than by printing an error
4744 * message. This is convenient when this function is called in a loop over a
4745 * long period of time (as it is, for example, by zpool's wait cmd). In that
4746 * scenario, a pool being exported or destroyed should be considered a normal
4747 * event, so we don't want to print an error when we find that the pool doesn't
4751 zpool_wait_status(zpool_handle_t
*zhp
, zpool_wait_activity_t activity
,
4752 boolean_t
*missing
, boolean_t
*waited
)
4754 int error
= lzc_wait(zhp
->zpool_name
, activity
, waited
);
4755 *missing
= (error
== ENOENT
);
4760 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
4761 dgettext(TEXT_DOMAIN
, "error waiting in pool '%s'"),
4769 zpool_set_bootenv(zpool_handle_t
*zhp
, const nvlist_t
*envmap
)
4771 int error
= lzc_set_bootenv(zhp
->zpool_name
, envmap
);
4773 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
4774 dgettext(TEXT_DOMAIN
,
4775 "error setting bootenv in pool '%s'"), zhp
->zpool_name
);
4782 zpool_get_bootenv(zpool_handle_t
*zhp
, nvlist_t
**nvlp
)
4788 error
= lzc_get_bootenv(zhp
->zpool_name
, &nvl
);
4790 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
4791 dgettext(TEXT_DOMAIN
,
4792 "error getting bootenv in pool '%s'"), zhp
->zpool_name
);
4801 * Attempt to read and parse feature file(s) (from "compatibility" property).
4802 * Files contain zpool feature names, comma or whitespace-separated.
4803 * Comments (# character to next newline) are discarded.
4806 * compatibility : string containing feature filenames
4807 * features : either NULL or pointer to array of boolean
4808 * report : either NULL or pointer to string buffer
4809 * rlen : length of "report" buffer
4811 * compatibility is NULL (unset), "", "off", "legacy", or list of
4812 * comma-separated filenames. filenames should either be absolute,
4814 * 1) ZPOOL_SYSCONF_COMPAT_D (eg: /etc/zfs/compatibility.d) or
4815 * 2) ZPOOL_DATA_COMPAT_D (eg: /usr/share/zfs/compatibility.d).
4816 * (Unset), "" or "off" => enable all features
4817 * "legacy" => disable all features
4819 * Any feature names read from files which match unames in spa_feature_table
4820 * will have the corresponding boolean set in the features array (if non-NULL).
4821 * If more than one feature set specified, only features present in *all* of
4824 * "report" if not NULL will be populated with a suitable status message.
4827 * ZPOOL_COMPATIBILITY_OK : files read and parsed ok
4828 * ZPOOL_COMPATIBILITY_BADFILE : file too big or not a text file
4829 * ZPOOL_COMPATIBILITY_BADTOKEN : SYSCONF file contains invalid feature name
4830 * ZPOOL_COMPATIBILITY_WARNTOKEN : DATA file contains invalid feature name
4831 * ZPOOL_COMPATIBILITY_NOFILES : no feature files found
4833 zpool_compat_status_t
4834 zpool_load_compat(const char *compat
, boolean_t
*features
, char *report
,
4837 int sdirfd
, ddirfd
, featfd
;
4841 char *file
, *line
, *word
;
4843 char l_compat
[ZFS_MAXPROPLEN
];
4845 boolean_t ret_nofiles
= B_TRUE
;
4846 boolean_t ret_badfile
= B_FALSE
;
4847 boolean_t ret_badtoken
= B_FALSE
;
4848 boolean_t ret_warntoken
= B_FALSE
;
4850 /* special cases (unset), "" and "off" => enable all features */
4851 if (compat
== NULL
|| compat
[0] == '\0' ||
4852 strcmp(compat
, ZPOOL_COMPAT_OFF
) == 0) {
4853 if (features
!= NULL
)
4854 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4855 features
[i
] = B_TRUE
;
4857 strlcpy(report
, gettext("all features enabled"), rlen
);
4858 return (ZPOOL_COMPATIBILITY_OK
);
4861 /* Final special case "legacy" => disable all features */
4862 if (strcmp(compat
, ZPOOL_COMPAT_LEGACY
) == 0) {
4863 if (features
!= NULL
)
4864 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4865 features
[i
] = B_FALSE
;
4867 strlcpy(report
, gettext("all features disabled"), rlen
);
4868 return (ZPOOL_COMPATIBILITY_OK
);
4872 * Start with all true; will be ANDed with results from each file
4874 if (features
!= NULL
)
4875 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4876 features
[i
] = B_TRUE
;
4878 char err_badfile
[1024] = "";
4879 char err_badtoken
[1024] = "";
4882 * We ignore errors from the directory open()
4883 * as they're only needed if the filename is relative
4884 * which will be checked during the openat().
4887 /* O_PATH safer than O_RDONLY if system allows it */
4889 #define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_PATH)
4891 #define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_RDONLY)
4894 sdirfd
= open(ZPOOL_SYSCONF_COMPAT_D
, ZC_DIR_FLAGS
);
4895 ddirfd
= open(ZPOOL_DATA_COMPAT_D
, ZC_DIR_FLAGS
);
4897 (void) strlcpy(l_compat
, compat
, ZFS_MAXPROPLEN
);
4899 for (file
= strtok_r(l_compat
, ",", &ps
);
4901 file
= strtok_r(NULL
, ",", &ps
)) {
4903 boolean_t l_features
[SPA_FEATURES
];
4905 enum { Z_SYSCONF
, Z_DATA
} source
;
4907 /* try sysconfdir first, then datadir */
4909 if ((featfd
= openat(sdirfd
, file
, O_RDONLY
| O_CLOEXEC
)) < 0) {
4910 featfd
= openat(ddirfd
, file
, O_RDONLY
| O_CLOEXEC
);
4914 /* File readable and correct size? */
4916 fstat(featfd
, &fs
) < 0 ||
4918 fs
.st_size
> ZPOOL_COMPAT_MAXSIZE
) {
4919 (void) close(featfd
);
4920 strlcat(err_badfile
, file
, ZFS_MAXPROPLEN
);
4921 strlcat(err_badfile
, " ", ZFS_MAXPROPLEN
);
4922 ret_badfile
= B_TRUE
;
4926 /* Prefault the file if system allows */
4927 #if defined(MAP_POPULATE)
4928 #define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_POPULATE)
4929 #elif defined(MAP_PREFAULT_READ)
4930 #define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_PREFAULT_READ)
4932 #define ZC_MMAP_FLAGS (MAP_PRIVATE)
4935 /* private mmap() so we can strtok safely */
4936 fc
= (char *)mmap(NULL
, fs
.st_size
, PROT_READ
| PROT_WRITE
,
4937 ZC_MMAP_FLAGS
, featfd
, 0);
4938 (void) close(featfd
);
4940 /* map ok, and last character == newline? */
4941 if (fc
== MAP_FAILED
|| fc
[fs
.st_size
- 1] != '\n') {
4942 (void) munmap((void *) fc
, fs
.st_size
);
4943 strlcat(err_badfile
, file
, ZFS_MAXPROPLEN
);
4944 strlcat(err_badfile
, " ", ZFS_MAXPROPLEN
);
4945 ret_badfile
= B_TRUE
;
4949 ret_nofiles
= B_FALSE
;
4951 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4952 l_features
[i
] = B_FALSE
;
4954 /* replace final newline with NULL to ensure string ends */
4955 fc
[fs
.st_size
- 1] = '\0';
4957 for (line
= strtok_r(fc
, "\n", &ls
);
4959 line
= strtok_r(NULL
, "\n", &ls
)) {
4960 /* discard comments */
4961 char *r
= strchr(line
, '#');
4965 for (word
= strtok_r(line
, ", \t", &ws
);
4967 word
= strtok_r(NULL
, ", \t", &ws
)) {
4968 /* Find matching feature name */
4970 for (f
= 0; f
< SPA_FEATURES
; f
++) {
4971 zfeature_info_t
*fi
=
4972 &spa_feature_table
[f
];
4973 if (strcmp(word
, fi
->fi_uname
) == 0) {
4974 l_features
[f
] = B_TRUE
;
4978 if (f
< SPA_FEATURES
)
4981 /* found an unrecognized word */
4982 /* lightly sanitize it */
4983 if (strlen(word
) > 32)
4985 for (char *c
= word
; *c
!= '\0'; c
++)
4989 strlcat(err_badtoken
, word
, ZFS_MAXPROPLEN
);
4990 strlcat(err_badtoken
, " ", ZFS_MAXPROPLEN
);
4991 if (source
== Z_SYSCONF
)
4992 ret_badtoken
= B_TRUE
;
4994 ret_warntoken
= B_TRUE
;
4997 (void) munmap((void *) fc
, fs
.st_size
);
4999 if (features
!= NULL
)
5000 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
5001 features
[i
] &= l_features
[i
];
5003 (void) close(sdirfd
);
5004 (void) close(ddirfd
);
5006 /* Return the most serious error */
5009 snprintf(report
, rlen
, gettext("could not read/"
5010 "parse feature file(s): %s"), err_badfile
);
5011 return (ZPOOL_COMPATIBILITY_BADFILE
);
5016 gettext("no valid compatibility files specified"),
5018 return (ZPOOL_COMPATIBILITY_NOFILES
);
5022 snprintf(report
, rlen
, gettext("invalid feature "
5023 "name(s) in local compatibility files: %s"),
5025 return (ZPOOL_COMPATIBILITY_BADTOKEN
);
5027 if (ret_warntoken
) {
5029 snprintf(report
, rlen
, gettext("unrecognized feature "
5030 "name(s) in distribution compatibility files: %s"),
5032 return (ZPOOL_COMPATIBILITY_WARNTOKEN
);
5035 strlcpy(report
, gettext("compatibility set ok"), rlen
);
5036 return (ZPOOL_COMPATIBILITY_OK
);
5040 zpool_vdev_guid(zpool_handle_t
*zhp
, const char *vdevname
, uint64_t *vdev_guid
)
5043 boolean_t avail_spare
, l2cache
;
5045 verify(zhp
!= NULL
);
5046 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
5048 (void) snprintf(errbuf
, sizeof (errbuf
),
5049 dgettext(TEXT_DOMAIN
, "pool is in an unavailable state"));
5050 return (zfs_error(zhp
->zpool_hdl
, EZFS_POOLUNAVAIL
, errbuf
));
5053 if ((tgt
= zpool_find_vdev(zhp
, vdevname
, &avail_spare
, &l2cache
,
5056 (void) snprintf(errbuf
, sizeof (errbuf
),
5057 dgettext(TEXT_DOMAIN
, "can not find %s in %s"),
5058 vdevname
, zhp
->zpool_name
);
5059 return (zfs_error(zhp
->zpool_hdl
, EZFS_NODEVICE
, errbuf
));
5062 *vdev_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
5067 * Get a vdev property value for 'prop' and return the value in
5068 * a pre-allocated buffer.
5071 zpool_get_vdev_prop_value(nvlist_t
*nvprop
, vdev_prop_t prop
, char *prop_name
,
5072 char *buf
, size_t len
, zprop_source_t
*srctype
, boolean_t literal
)
5077 zprop_source_t src
= ZPROP_SRC_NONE
;
5079 if (prop
== VDEV_PROP_USER
) {
5080 /* user property, prop_name must contain the property name */
5081 assert(prop_name
!= NULL
);
5082 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
5083 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
5084 strval
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
5086 /* user prop not found */
5089 (void) strlcpy(buf
, strval
, len
);
5095 if (prop_name
== NULL
)
5096 prop_name
= (char *)vdev_prop_to_name(prop
);
5098 switch (vdev_prop_get_type(prop
)) {
5099 case PROP_TYPE_STRING
:
5100 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
5101 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
5102 strval
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
5104 src
= ZPROP_SRC_DEFAULT
;
5105 if ((strval
= (char *)vdev_prop_default_string(prop
))
5109 (void) strlcpy(buf
, strval
, len
);
5112 case PROP_TYPE_NUMBER
:
5113 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
5114 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
5115 intval
= fnvlist_lookup_uint64(nv
, ZPROP_VALUE
);
5117 src
= ZPROP_SRC_DEFAULT
;
5118 intval
= vdev_prop_default_numeric(prop
);
5122 case VDEV_PROP_ASIZE
:
5123 case VDEV_PROP_PSIZE
:
5124 case VDEV_PROP_SIZE
:
5125 case VDEV_PROP_BOOTSIZE
:
5126 case VDEV_PROP_ALLOCATED
:
5127 case VDEV_PROP_FREE
:
5128 case VDEV_PROP_READ_ERRORS
:
5129 case VDEV_PROP_WRITE_ERRORS
:
5130 case VDEV_PROP_CHECKSUM_ERRORS
:
5131 case VDEV_PROP_INITIALIZE_ERRORS
:
5132 case VDEV_PROP_OPS_NULL
:
5133 case VDEV_PROP_OPS_READ
:
5134 case VDEV_PROP_OPS_WRITE
:
5135 case VDEV_PROP_OPS_FREE
:
5136 case VDEV_PROP_OPS_CLAIM
:
5137 case VDEV_PROP_OPS_TRIM
:
5138 case VDEV_PROP_BYTES_NULL
:
5139 case VDEV_PROP_BYTES_READ
:
5140 case VDEV_PROP_BYTES_WRITE
:
5141 case VDEV_PROP_BYTES_FREE
:
5142 case VDEV_PROP_BYTES_CLAIM
:
5143 case VDEV_PROP_BYTES_TRIM
:
5145 (void) snprintf(buf
, len
, "%llu",
5146 (u_longlong_t
)intval
);
5148 (void) zfs_nicenum(intval
, buf
, len
);
5151 case VDEV_PROP_EXPANDSZ
:
5153 (void) strlcpy(buf
, "-", len
);
5154 } else if (literal
) {
5155 (void) snprintf(buf
, len
, "%llu",
5156 (u_longlong_t
)intval
);
5158 (void) zfs_nicenum(intval
, buf
, len
);
5161 case VDEV_PROP_CAPACITY
:
5163 (void) snprintf(buf
, len
, "%llu",
5164 (u_longlong_t
)intval
);
5166 (void) snprintf(buf
, len
, "%llu%%",
5167 (u_longlong_t
)intval
);
5170 case VDEV_PROP_FRAGMENTATION
:
5171 if (intval
== UINT64_MAX
) {
5172 (void) strlcpy(buf
, "-", len
);
5174 (void) snprintf(buf
, len
, "%llu%%",
5175 (u_longlong_t
)intval
);
5178 case VDEV_PROP_STATE
:
5180 (void) snprintf(buf
, len
, "%llu",
5181 (u_longlong_t
)intval
);
5183 (void) strlcpy(buf
, zpool_state_to_name(intval
,
5184 VDEV_AUX_NONE
), len
);
5188 (void) snprintf(buf
, len
, "%llu",
5189 (u_longlong_t
)intval
);
5193 case PROP_TYPE_INDEX
:
5194 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
5195 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
5196 intval
= fnvlist_lookup_uint64(nv
, ZPROP_VALUE
);
5198 src
= ZPROP_SRC_DEFAULT
;
5199 intval
= vdev_prop_default_numeric(prop
);
5201 if (vdev_prop_index_to_string(prop
, intval
,
5202 (const char **)&strval
) != 0)
5204 (void) strlcpy(buf
, strval
, len
);
5218 * Get a vdev property value for 'prop_name' and return the value in
5219 * a pre-allocated buffer.
5222 zpool_get_vdev_prop(zpool_handle_t
*zhp
, const char *vdevname
, vdev_prop_t prop
,
5223 char *prop_name
, char *buf
, size_t len
, zprop_source_t
*srctype
,
5226 nvlist_t
*reqnvl
, *reqprops
;
5227 nvlist_t
*retprops
= NULL
;
5231 if ((ret
= zpool_vdev_guid(zhp
, vdevname
, &vdev_guid
)) != 0)
5234 if (nvlist_alloc(&reqnvl
, NV_UNIQUE_NAME
, 0) != 0)
5235 return (no_memory(zhp
->zpool_hdl
));
5236 if (nvlist_alloc(&reqprops
, NV_UNIQUE_NAME
, 0) != 0)
5237 return (no_memory(zhp
->zpool_hdl
));
5239 fnvlist_add_uint64(reqnvl
, ZPOOL_VDEV_PROPS_GET_VDEV
, vdev_guid
);
5241 if (prop
!= VDEV_PROP_USER
) {
5242 /* prop_name overrides prop value */
5243 if (prop_name
!= NULL
)
5244 prop
= vdev_name_to_prop(prop_name
);
5246 prop_name
= (char *)vdev_prop_to_name(prop
);
5247 assert(prop
< VDEV_NUM_PROPS
);
5250 assert(prop_name
!= NULL
);
5251 if (nvlist_add_uint64(reqprops
, prop_name
, prop
) != 0) {
5252 nvlist_free(reqnvl
);
5253 nvlist_free(reqprops
);
5254 return (no_memory(zhp
->zpool_hdl
));
5257 fnvlist_add_nvlist(reqnvl
, ZPOOL_VDEV_PROPS_GET_PROPS
, reqprops
);
5259 ret
= lzc_get_vdev_prop(zhp
->zpool_name
, reqnvl
, &retprops
);
5262 ret
= zpool_get_vdev_prop_value(retprops
, prop
, prop_name
, buf
,
5263 len
, srctype
, literal
);
5266 (void) snprintf(errbuf
, sizeof (errbuf
),
5267 dgettext(TEXT_DOMAIN
, "cannot get vdev property %s from"
5268 " %s in %s"), prop_name
, vdevname
, zhp
->zpool_name
);
5269 (void) zpool_standard_error(zhp
->zpool_hdl
, ret
, errbuf
);
5272 nvlist_free(reqnvl
);
5273 nvlist_free(reqprops
);
5274 nvlist_free(retprops
);
5280 * Get all vdev properties
5283 zpool_get_all_vdev_props(zpool_handle_t
*zhp
, const char *vdevname
,
5286 nvlist_t
*nvl
= NULL
;
5290 if ((ret
= zpool_vdev_guid(zhp
, vdevname
, &vdev_guid
)) != 0)
5293 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5294 return (no_memory(zhp
->zpool_hdl
));
5296 fnvlist_add_uint64(nvl
, ZPOOL_VDEV_PROPS_GET_VDEV
, vdev_guid
);
5298 ret
= lzc_get_vdev_prop(zhp
->zpool_name
, nvl
, outnvl
);
5304 (void) snprintf(errbuf
, sizeof (errbuf
),
5305 dgettext(TEXT_DOMAIN
, "cannot get vdev properties for"
5306 " %s in %s"), vdevname
, zhp
->zpool_name
);
5307 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
5317 zpool_set_vdev_prop(zpool_handle_t
*zhp
, const char *vdevname
,
5318 const char *propname
, const char *propval
)
5321 nvlist_t
*nvl
= NULL
;
5322 nvlist_t
*outnvl
= NULL
;
5324 nvlist_t
*realprops
;
5325 prop_flags_t flags
= { 0 };
5329 if ((ret
= zpool_vdev_guid(zhp
, vdevname
, &vdev_guid
)) != 0)
5332 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5333 return (no_memory(zhp
->zpool_hdl
));
5334 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
5335 return (no_memory(zhp
->zpool_hdl
));
5337 fnvlist_add_uint64(nvl
, ZPOOL_VDEV_PROPS_SET_VDEV
, vdev_guid
);
5339 if (nvlist_add_string(props
, propname
, propval
) != 0) {
5341 return (no_memory(zhp
->zpool_hdl
));
5345 (void) snprintf(errbuf
, sizeof (errbuf
),
5346 dgettext(TEXT_DOMAIN
, "cannot set property %s for %s on %s"),
5347 propname
, vdevname
, zhp
->zpool_name
);
5350 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
5351 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
5352 zhp
->zpool_name
, props
, version
, flags
, errbuf
)) == NULL
) {
5361 fnvlist_add_nvlist(nvl
, ZPOOL_VDEV_PROPS_SET_PROPS
, props
);
5363 ret
= lzc_set_vdev_prop(zhp
->zpool_name
, nvl
, &outnvl
);
5367 nvlist_free(outnvl
);
5370 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);