4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27 * Copyright (c) 2018 Datto Inc.
28 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
29 * Copyright (c) 2017, Intel Corporation.
30 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>
31 * Copyright (c) 2021, Colm Buckley <colm@tuatha.org>
32 * Copyright (c) 2021, Klara Inc.
44 #include <sys/efi_partition.h>
45 #include <sys/systeminfo.h>
46 #include <sys/zfs_ioctl.h>
47 #include <sys/zfs_sysfs.h>
48 #include <sys/vdev_disk.h>
49 #include <sys/types.h>
54 #include "zfs_namecheck.h"
56 #include "libzfs_impl.h"
57 #include "zfs_comutil.h"
58 #include "zfeature_common.h"
60 static boolean_t
zpool_vdev_is_interior(const char *name
);
62 typedef struct prop_flags
{
63 unsigned int create
:1; /* Validate property on creation */
64 unsigned int import
:1; /* Validate property on import */
65 unsigned int vdevprop
:1; /* Validate property as a VDEV property */
69 * ====================================================================
70 * zpool property functions
71 * ====================================================================
75 zpool_get_all_props(zpool_handle_t
*zhp
)
77 zfs_cmd_t zc
= {"\0"};
78 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
80 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
82 zcmd_alloc_dst_nvlist(hdl
, &zc
, 0);
84 while (zfs_ioctl(hdl
, ZFS_IOC_POOL_GET_PROPS
, &zc
) != 0) {
86 zcmd_expand_dst_nvlist(hdl
, &zc
);
88 zcmd_free_nvlists(&zc
);
93 if (zcmd_read_dst_nvlist(hdl
, &zc
, &zhp
->zpool_props
) != 0) {
94 zcmd_free_nvlists(&zc
);
98 zcmd_free_nvlists(&zc
);
104 zpool_props_refresh(zpool_handle_t
*zhp
)
108 old_props
= zhp
->zpool_props
;
110 if (zpool_get_all_props(zhp
) != 0)
113 nvlist_free(old_props
);
118 zpool_get_prop_string(zpool_handle_t
*zhp
, zpool_prop_t prop
,
123 zprop_source_t source
;
125 nvl
= zhp
->zpool_props
;
126 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
127 source
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
128 value
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
130 source
= ZPROP_SRC_DEFAULT
;
131 if ((value
= zpool_prop_default_string(prop
)) == NULL
)
142 zpool_get_prop_int(zpool_handle_t
*zhp
, zpool_prop_t prop
, zprop_source_t
*src
)
146 zprop_source_t source
;
148 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
)) {
150 * zpool_get_all_props() has most likely failed because
151 * the pool is faulted, but if all we need is the top level
152 * vdev's guid then get it from the zhp config nvlist.
154 if ((prop
== ZPOOL_PROP_GUID
) &&
155 (nvlist_lookup_nvlist(zhp
->zpool_config
,
156 ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0) &&
157 (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
)
161 return (zpool_prop_default_numeric(prop
));
164 nvl
= zhp
->zpool_props
;
165 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
166 source
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
167 value
= fnvlist_lookup_uint64(nv
, ZPROP_VALUE
);
169 source
= ZPROP_SRC_DEFAULT
;
170 value
= zpool_prop_default_numeric(prop
);
180 * Map VDEV STATE to printed strings.
183 zpool_state_to_name(vdev_state_t state
, vdev_aux_t aux
)
186 case VDEV_STATE_CLOSED
:
187 case VDEV_STATE_OFFLINE
:
188 return (gettext("OFFLINE"));
189 case VDEV_STATE_REMOVED
:
190 return (gettext("REMOVED"));
191 case VDEV_STATE_CANT_OPEN
:
192 if (aux
== VDEV_AUX_CORRUPT_DATA
|| aux
== VDEV_AUX_BAD_LOG
)
193 return (gettext("FAULTED"));
194 else if (aux
== VDEV_AUX_SPLIT_POOL
)
195 return (gettext("SPLIT"));
197 return (gettext("UNAVAIL"));
198 case VDEV_STATE_FAULTED
:
199 return (gettext("FAULTED"));
200 case VDEV_STATE_DEGRADED
:
201 return (gettext("DEGRADED"));
202 case VDEV_STATE_HEALTHY
:
203 return (gettext("ONLINE"));
209 return (gettext("UNKNOWN"));
213 * Map POOL STATE to printed strings.
216 zpool_pool_state_to_name(pool_state_t state
)
221 case POOL_STATE_ACTIVE
:
222 return (gettext("ACTIVE"));
223 case POOL_STATE_EXPORTED
:
224 return (gettext("EXPORTED"));
225 case POOL_STATE_DESTROYED
:
226 return (gettext("DESTROYED"));
227 case POOL_STATE_SPARE
:
228 return (gettext("SPARE"));
229 case POOL_STATE_L2CACHE
:
230 return (gettext("L2CACHE"));
231 case POOL_STATE_UNINITIALIZED
:
232 return (gettext("UNINITIALIZED"));
233 case POOL_STATE_UNAVAIL
:
234 return (gettext("UNAVAIL"));
235 case POOL_STATE_POTENTIALLY_ACTIVE
:
236 return (gettext("POTENTIALLY_ACTIVE"));
239 return (gettext("UNKNOWN"));
243 * Given a pool handle, return the pool health string ("ONLINE", "DEGRADED",
247 zpool_get_state_str(zpool_handle_t
*zhp
)
249 zpool_errata_t errata
;
250 zpool_status_t status
;
253 status
= zpool_get_status(zhp
, NULL
, &errata
);
255 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
256 str
= gettext("FAULTED");
257 } else if (status
== ZPOOL_STATUS_IO_FAILURE_WAIT
||
258 status
== ZPOOL_STATUS_IO_FAILURE_MMP
) {
259 str
= gettext("SUSPENDED");
261 nvlist_t
*nvroot
= fnvlist_lookup_nvlist(
262 zpool_get_config(zhp
, NULL
), ZPOOL_CONFIG_VDEV_TREE
);
264 vdev_stat_t
*vs
= (vdev_stat_t
*)fnvlist_lookup_uint64_array(
265 nvroot
, ZPOOL_CONFIG_VDEV_STATS
, &vsc
);
266 str
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
272 * Get a zpool property value for 'prop' and return the value in
273 * a pre-allocated buffer.
276 zpool_get_prop(zpool_handle_t
*zhp
, zpool_prop_t prop
, char *buf
,
277 size_t len
, zprop_source_t
*srctype
, boolean_t literal
)
281 zprop_source_t src
= ZPROP_SRC_NONE
;
283 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
285 case ZPOOL_PROP_NAME
:
286 (void) strlcpy(buf
, zpool_get_name(zhp
), len
);
289 case ZPOOL_PROP_HEALTH
:
290 (void) strlcpy(buf
, zpool_get_state_str(zhp
), len
);
293 case ZPOOL_PROP_GUID
:
294 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
295 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
298 case ZPOOL_PROP_ALTROOT
:
299 case ZPOOL_PROP_CACHEFILE
:
300 case ZPOOL_PROP_COMMENT
:
301 case ZPOOL_PROP_COMPATIBILITY
:
302 if (zhp
->zpool_props
!= NULL
||
303 zpool_get_all_props(zhp
) == 0) {
305 zpool_get_prop_string(zhp
, prop
, &src
),
311 (void) strlcpy(buf
, "-", len
);
320 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
) &&
321 prop
!= ZPOOL_PROP_NAME
)
324 switch (zpool_prop_get_type(prop
)) {
325 case PROP_TYPE_STRING
:
326 (void) strlcpy(buf
, zpool_get_prop_string(zhp
, prop
, &src
),
330 case PROP_TYPE_NUMBER
:
331 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
334 case ZPOOL_PROP_SIZE
:
335 case ZPOOL_PROP_ALLOCATED
:
336 case ZPOOL_PROP_FREE
:
337 case ZPOOL_PROP_FREEING
:
338 case ZPOOL_PROP_LEAKED
:
339 case ZPOOL_PROP_ASHIFT
:
340 case ZPOOL_PROP_MAXBLOCKSIZE
:
341 case ZPOOL_PROP_MAXDNODESIZE
:
342 case ZPOOL_PROP_BCLONESAVED
:
343 case ZPOOL_PROP_BCLONEUSED
:
345 (void) snprintf(buf
, len
, "%llu",
346 (u_longlong_t
)intval
);
348 (void) zfs_nicenum(intval
, buf
, len
);
351 case ZPOOL_PROP_EXPANDSZ
:
352 case ZPOOL_PROP_CHECKPOINT
:
354 (void) strlcpy(buf
, "-", len
);
355 } else if (literal
) {
356 (void) snprintf(buf
, len
, "%llu",
357 (u_longlong_t
)intval
);
359 (void) zfs_nicebytes(intval
, buf
, len
);
363 case ZPOOL_PROP_CAPACITY
:
365 (void) snprintf(buf
, len
, "%llu",
366 (u_longlong_t
)intval
);
368 (void) snprintf(buf
, len
, "%llu%%",
369 (u_longlong_t
)intval
);
373 case ZPOOL_PROP_FRAGMENTATION
:
374 if (intval
== UINT64_MAX
) {
375 (void) strlcpy(buf
, "-", len
);
376 } else if (literal
) {
377 (void) snprintf(buf
, len
, "%llu",
378 (u_longlong_t
)intval
);
380 (void) snprintf(buf
, len
, "%llu%%",
381 (u_longlong_t
)intval
);
385 case ZPOOL_PROP_BCLONERATIO
:
386 case ZPOOL_PROP_DEDUPRATIO
:
388 (void) snprintf(buf
, len
, "%llu.%02llu",
389 (u_longlong_t
)(intval
/ 100),
390 (u_longlong_t
)(intval
% 100));
392 (void) snprintf(buf
, len
, "%llu.%02llux",
393 (u_longlong_t
)(intval
/ 100),
394 (u_longlong_t
)(intval
% 100));
397 case ZPOOL_PROP_HEALTH
:
398 (void) strlcpy(buf
, zpool_get_state_str(zhp
), len
);
400 case ZPOOL_PROP_VERSION
:
401 if (intval
>= SPA_VERSION_FEATURES
) {
402 (void) snprintf(buf
, len
, "-");
407 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
411 case PROP_TYPE_INDEX
:
412 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
413 if (zpool_prop_index_to_string(prop
, intval
, &strval
)
416 (void) strlcpy(buf
, strval
, len
);
430 * Check if the bootfs name has the same pool name as it is set to.
431 * Assuming bootfs is a valid dataset name.
434 bootfs_name_valid(const char *pool
, const char *bootfs
)
436 int len
= strlen(pool
);
437 if (bootfs
[0] == '\0')
440 if (!zfs_name_valid(bootfs
, ZFS_TYPE_FILESYSTEM
|ZFS_TYPE_SNAPSHOT
))
443 if (strncmp(pool
, bootfs
, len
) == 0 &&
444 (bootfs
[len
] == '/' || bootfs
[len
] == '\0'))
451 * Given an nvlist of zpool properties to be set, validate that they are
452 * correct, and parse any numeric properties (index, boolean, etc) if they are
453 * specified as strings.
456 zpool_valid_proplist(libzfs_handle_t
*hdl
, const char *poolname
,
457 nvlist_t
*props
, uint64_t version
, prop_flags_t flags
, char *errbuf
)
465 struct stat64 statbuf
;
469 if (nvlist_alloc(&retprops
, NV_UNIQUE_NAME
, 0) != 0) {
470 (void) no_memory(hdl
);
475 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
476 const char *propname
= nvpair_name(elem
);
478 if (flags
.vdevprop
&& zpool_prop_vdev(propname
)) {
479 vdev_prop_t vprop
= vdev_name_to_prop(propname
);
481 if (vdev_prop_readonly(vprop
)) {
482 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
483 "is readonly"), propname
);
484 (void) zfs_error(hdl
, EZFS_PROPREADONLY
,
489 if (zprop_parse_value(hdl
, elem
, vprop
, ZFS_TYPE_VDEV
,
490 retprops
, &strval
, &intval
, errbuf
) != 0)
494 } else if (flags
.vdevprop
&& vdev_prop_user(propname
)) {
495 if (nvlist_add_nvpair(retprops
, elem
) != 0) {
496 (void) no_memory(hdl
);
500 } else if (flags
.vdevprop
) {
501 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
502 "invalid property: '%s'"), propname
);
503 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
507 prop
= zpool_name_to_prop(propname
);
508 if (prop
== ZPOOL_PROP_INVAL
&& zpool_prop_feature(propname
)) {
510 char *fname
= strchr(propname
, '@') + 1;
512 err
= zfeature_lookup_name(fname
, NULL
);
514 ASSERT3U(err
, ==, ENOENT
);
515 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
516 "feature '%s' unsupported by kernel"),
518 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
522 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
523 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
524 "'%s' must be a string"), propname
);
525 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
529 (void) nvpair_value_string(elem
, &strval
);
530 if (strcmp(strval
, ZFS_FEATURE_ENABLED
) != 0 &&
531 strcmp(strval
, ZFS_FEATURE_DISABLED
) != 0) {
532 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
533 "property '%s' can only be set to "
534 "'enabled' or 'disabled'"), propname
);
535 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
540 strcmp(strval
, ZFS_FEATURE_DISABLED
) == 0) {
541 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
542 "property '%s' can only be set to "
543 "'disabled' at creation time"), propname
);
544 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
548 if (nvlist_add_uint64(retprops
, propname
, 0) != 0) {
549 (void) no_memory(hdl
);
556 * Make sure this property is valid and applies to this type.
558 if (prop
== ZPOOL_PROP_INVAL
) {
559 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
560 "invalid property '%s'"), propname
);
561 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
565 if (zpool_prop_readonly(prop
)) {
566 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
567 "is readonly"), propname
);
568 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
572 if (!flags
.create
&& zpool_prop_setonce(prop
)) {
573 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
574 "property '%s' can only be set at "
575 "creation time"), propname
);
576 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
580 if (zprop_parse_value(hdl
, elem
, prop
, ZFS_TYPE_POOL
, retprops
,
581 &strval
, &intval
, errbuf
) != 0)
585 * Perform additional checking for specific properties.
588 case ZPOOL_PROP_VERSION
:
589 if (intval
< version
||
590 !SPA_VERSION_IS_SUPPORTED(intval
)) {
591 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
592 "property '%s' number %llu is invalid."),
593 propname
, (unsigned long long)intval
);
594 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
599 case ZPOOL_PROP_ASHIFT
:
601 (intval
< ASHIFT_MIN
|| intval
> ASHIFT_MAX
)) {
602 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
603 "property '%s' number %llu is invalid, "
604 "only values between %" PRId32
" and %"
605 PRId32
" are allowed."),
606 propname
, (unsigned long long)intval
,
607 ASHIFT_MIN
, ASHIFT_MAX
);
608 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
613 case ZPOOL_PROP_BOOTFS
:
614 if (flags
.create
|| flags
.import
) {
615 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
616 "property '%s' cannot be set at creation "
617 "or import time"), propname
);
618 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
622 if (version
< SPA_VERSION_BOOTFS
) {
623 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
624 "pool must be upgraded to support "
625 "'%s' property"), propname
);
626 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
631 * bootfs property value has to be a dataset name and
632 * the dataset has to be in the same pool as it sets to.
634 if (!bootfs_name_valid(poolname
, strval
)) {
635 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
636 "is an invalid name"), strval
);
637 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
641 if ((zhp
= zpool_open_canfail(hdl
, poolname
)) == NULL
) {
642 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
643 "could not open pool '%s'"), poolname
);
644 (void) zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
);
650 case ZPOOL_PROP_ALTROOT
:
651 if (!flags
.create
&& !flags
.import
) {
652 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
653 "property '%s' can only be set during pool "
654 "creation or import"), propname
);
655 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
659 if (strval
[0] != '/') {
660 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
661 "bad alternate root '%s'"), strval
);
662 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
667 case ZPOOL_PROP_CACHEFILE
:
668 if (strval
[0] == '\0')
671 if (strcmp(strval
, "none") == 0)
674 if (strval
[0] != '/') {
675 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
676 "property '%s' must be empty, an "
677 "absolute path, or 'none'"), propname
);
678 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
682 slash
= strrchr(strval
, '/');
684 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
685 strcmp(slash
, "/..") == 0) {
686 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
687 "'%s' is not a valid file"), strval
);
688 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
694 if (strval
[0] != '\0' &&
695 (stat64(strval
, &statbuf
) != 0 ||
696 !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"};
783 char errbuf
[ERRBUFLEN
];
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 zcmd_write_src_nvlist(zhp
->zpool_hdl
, &zc
, nvl
);
818 ret
= zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_SET_PROPS
, &zc
);
820 zcmd_free_nvlists(&zc
);
824 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
826 (void) zpool_props_refresh(zhp
);
832 zpool_expand_proplist(zpool_handle_t
*zhp
, zprop_list_t
**plp
,
833 zfs_type_t type
, boolean_t literal
)
835 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
837 char buf
[ZFS_MAXPROPLEN
];
838 nvlist_t
*features
= NULL
;
841 boolean_t firstexpand
= (NULL
== *plp
);
844 if (zprop_expand_list(hdl
, plp
, type
) != 0)
847 if (type
== ZFS_TYPE_VDEV
)
851 while (*last
!= NULL
)
852 last
= &(*last
)->pl_next
;
855 features
= zpool_get_features(zhp
);
857 if ((*plp
)->pl_all
&& firstexpand
) {
858 for (i
= 0; i
< SPA_FEATURES
; i
++) {
859 zprop_list_t
*entry
= zfs_alloc(hdl
,
860 sizeof (zprop_list_t
));
861 entry
->pl_prop
= ZPROP_USERPROP
;
862 entry
->pl_user_prop
= zfs_asprintf(hdl
, "feature@%s",
863 spa_feature_table
[i
].fi_uname
);
864 entry
->pl_width
= strlen(entry
->pl_user_prop
);
865 entry
->pl_all
= B_TRUE
;
868 last
= &entry
->pl_next
;
872 /* add any unsupported features */
873 for (nvp
= nvlist_next_nvpair(features
, NULL
);
874 nvp
!= NULL
; nvp
= nvlist_next_nvpair(features
, nvp
)) {
879 if (zfeature_is_supported(nvpair_name(nvp
)))
882 propname
= zfs_asprintf(hdl
, "unsupported@%s",
886 * Before adding the property to the list make sure that no
887 * other pool already added the same property.
891 while (entry
!= NULL
) {
892 if (entry
->pl_user_prop
!= NULL
&&
893 strcmp(propname
, entry
->pl_user_prop
) == 0) {
897 entry
= entry
->pl_next
;
904 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
905 entry
->pl_prop
= ZPROP_USERPROP
;
906 entry
->pl_user_prop
= propname
;
907 entry
->pl_width
= strlen(entry
->pl_user_prop
);
908 entry
->pl_all
= B_TRUE
;
911 last
= &entry
->pl_next
;
914 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
915 if (entry
->pl_fixed
&& !literal
)
918 if (entry
->pl_prop
!= ZPROP_USERPROP
&&
919 zpool_get_prop(zhp
, entry
->pl_prop
, buf
, sizeof (buf
),
920 NULL
, literal
) == 0) {
921 if (strlen(buf
) > entry
->pl_width
)
922 entry
->pl_width
= strlen(buf
);
930 vdev_expand_proplist(zpool_handle_t
*zhp
, const char *vdevname
,
934 char buf
[ZFS_MAXPROPLEN
];
937 nvpair_t
*elem
= NULL
;
938 nvlist_t
*vprops
= NULL
;
939 nvlist_t
*propval
= NULL
;
940 const char *propname
;
944 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
948 if (zpool_get_vdev_prop(zhp
, vdevname
, entry
->pl_prop
,
949 entry
->pl_user_prop
, buf
, sizeof (buf
), NULL
,
951 if (strlen(buf
) > entry
->pl_width
)
952 entry
->pl_width
= strlen(buf
);
954 if (entry
->pl_prop
== VDEV_PROP_NAME
&&
955 strlen(vdevname
) > entry
->pl_width
)
956 entry
->pl_width
= strlen(vdevname
);
959 /* Handle the all properties case */
961 if (*last
!= NULL
&& (*last
)->pl_all
== B_TRUE
) {
962 while (*last
!= NULL
)
963 last
= &(*last
)->pl_next
;
965 err
= zpool_get_all_vdev_props(zhp
, vdevname
, &vprops
);
969 while ((elem
= nvlist_next_nvpair(vprops
, elem
)) != NULL
) {
970 propname
= nvpair_name(elem
);
972 /* Skip properties that are not user defined */
973 if ((prop
= vdev_name_to_prop(propname
)) !=
977 if (nvpair_value_nvlist(elem
, &propval
) != 0)
980 strval
= fnvlist_lookup_string(propval
, ZPROP_VALUE
);
982 entry
= zfs_alloc(zhp
->zpool_hdl
,
983 sizeof (zprop_list_t
));
984 entry
->pl_prop
= prop
;
985 entry
->pl_user_prop
= zfs_strdup(zhp
->zpool_hdl
,
987 entry
->pl_width
= strlen(strval
);
988 entry
->pl_all
= B_TRUE
;
990 last
= &entry
->pl_next
;
998 * Get the state for the given feature on the given ZFS pool.
1001 zpool_prop_get_feature(zpool_handle_t
*zhp
, const char *propname
, char *buf
,
1005 boolean_t found
= B_FALSE
;
1006 nvlist_t
*features
= zpool_get_features(zhp
);
1007 boolean_t supported
;
1008 const char *feature
= strchr(propname
, '@') + 1;
1010 supported
= zpool_prop_feature(propname
);
1011 ASSERT(supported
|| zpool_prop_unsupported(propname
));
1014 * Convert from feature name to feature guid. This conversion is
1015 * unnecessary for unsupported@... properties because they already
1022 ret
= zfeature_lookup_name(feature
, &fid
);
1024 (void) strlcpy(buf
, "-", len
);
1027 feature
= spa_feature_table
[fid
].fi_guid
;
1030 if (nvlist_lookup_uint64(features
, feature
, &refcount
) == 0)
1035 (void) strlcpy(buf
, ZFS_FEATURE_DISABLED
, len
);
1038 (void) strlcpy(buf
, ZFS_FEATURE_ENABLED
, len
);
1040 (void) strlcpy(buf
, ZFS_FEATURE_ACTIVE
, len
);
1044 if (refcount
== 0) {
1045 (void) strcpy(buf
, ZFS_UNSUPPORTED_INACTIVE
);
1047 (void) strcpy(buf
, ZFS_UNSUPPORTED_READONLY
);
1050 (void) strlcpy(buf
, "-", len
);
1059 * Validate the given pool name, optionally putting an extended error message in
1063 zpool_name_valid(libzfs_handle_t
*hdl
, boolean_t isopen
, const char *pool
)
1065 namecheck_err_t why
;
1069 ret
= pool_namecheck(pool
, &why
, &what
);
1072 * The rules for reserved pool names were extended at a later point.
1073 * But we need to support users with existing pools that may now be
1074 * invalid. So we only check for this expanded set of names during a
1075 * create (or import), and only in userland.
1077 if (ret
== 0 && !isopen
&&
1078 (strncmp(pool
, "mirror", 6) == 0 ||
1079 strncmp(pool
, "raidz", 5) == 0 ||
1080 strncmp(pool
, "draid", 5) == 0 ||
1081 strncmp(pool
, "spare", 5) == 0 ||
1082 strcmp(pool
, "log") == 0)) {
1085 dgettext(TEXT_DOMAIN
, "name is reserved"));
1093 case NAME_ERR_TOOLONG
:
1095 dgettext(TEXT_DOMAIN
, "name is too long"));
1098 case NAME_ERR_INVALCHAR
:
1100 dgettext(TEXT_DOMAIN
, "invalid character "
1101 "'%c' in pool name"), what
);
1104 case NAME_ERR_NOLETTER
:
1105 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1106 "name must begin with a letter"));
1109 case NAME_ERR_RESERVED
:
1110 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1111 "name is reserved"));
1114 case NAME_ERR_DISKLIKE
:
1115 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1116 "pool name is reserved"));
1119 case NAME_ERR_LEADING_SLASH
:
1120 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1121 "leading slash in name"));
1124 case NAME_ERR_EMPTY_COMPONENT
:
1125 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1126 "empty component in name"));
1129 case NAME_ERR_TRAILING_SLASH
:
1130 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1131 "trailing slash in name"));
1134 case NAME_ERR_MULTIPLE_DELIMITERS
:
1135 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1136 "multiple '@' and/or '#' delimiters in "
1140 case NAME_ERR_NO_AT
:
1141 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1142 "permission set is missing '@'"));
1146 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1147 "(%d) not defined"), why
);
1158 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1162 zpool_open_canfail(libzfs_handle_t
*hdl
, const char *pool
)
1164 zpool_handle_t
*zhp
;
1168 * Make sure the pool name is valid.
1170 if (!zpool_name_valid(hdl
, B_TRUE
, pool
)) {
1171 (void) zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1172 dgettext(TEXT_DOMAIN
, "cannot open '%s'"),
1177 zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
));
1179 zhp
->zpool_hdl
= hdl
;
1180 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1182 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1188 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "no such pool"));
1189 (void) zfs_error_fmt(hdl
, EZFS_NOENT
,
1190 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), pool
);
1199 * Like the above, but silent on error. Used when iterating over pools (because
1200 * the configuration cache may be out of date).
1203 zpool_open_silent(libzfs_handle_t
*hdl
, const char *pool
, zpool_handle_t
**ret
)
1205 zpool_handle_t
*zhp
;
1208 zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
));
1210 zhp
->zpool_hdl
= hdl
;
1211 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1213 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1229 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1233 zpool_open(libzfs_handle_t
*hdl
, const char *pool
)
1235 zpool_handle_t
*zhp
;
1237 if ((zhp
= zpool_open_canfail(hdl
, pool
)) == NULL
)
1240 if (zhp
->zpool_state
== POOL_STATE_UNAVAIL
) {
1241 (void) zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
1242 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), zhp
->zpool_name
);
1251 * Close the handle. Simply frees the memory associated with the handle.
1254 zpool_close(zpool_handle_t
*zhp
)
1256 nvlist_free(zhp
->zpool_config
);
1257 nvlist_free(zhp
->zpool_old_config
);
1258 nvlist_free(zhp
->zpool_props
);
1263 * Return the name of the pool.
1266 zpool_get_name(zpool_handle_t
*zhp
)
1268 return (zhp
->zpool_name
);
1273 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1276 zpool_get_state(zpool_handle_t
*zhp
)
1278 return (zhp
->zpool_state
);
1282 * Check if vdev list contains a special vdev
1285 zpool_has_special_vdev(nvlist_t
*nvroot
)
1290 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
, &child
,
1292 for (uint_t c
= 0; c
< children
; c
++) {
1295 if (nvlist_lookup_string(child
[c
],
1296 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
) == 0 &&
1297 strcmp(bias
, VDEV_ALLOC_BIAS_SPECIAL
) == 0) {
1306 * Check if vdev list contains a dRAID vdev
1309 zpool_has_draid_vdev(nvlist_t
*nvroot
)
1314 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
1315 &child
, &children
) == 0) {
1316 for (uint_t c
= 0; c
< children
; c
++) {
1319 if (nvlist_lookup_string(child
[c
],
1320 ZPOOL_CONFIG_TYPE
, &type
) == 0 &&
1321 strcmp(type
, VDEV_TYPE_DRAID
) == 0) {
1330 * Output a dRAID top-level vdev name in to the provided buffer.
1333 zpool_draid_name(char *name
, int len
, uint64_t data
, uint64_t parity
,
1334 uint64_t spares
, uint64_t children
)
1336 snprintf(name
, len
, "%s%llu:%llud:%lluc:%llus",
1337 VDEV_TYPE_DRAID
, (u_longlong_t
)parity
, (u_longlong_t
)data
,
1338 (u_longlong_t
)children
, (u_longlong_t
)spares
);
1344 * Return B_TRUE if the provided name is a dRAID spare name.
1347 zpool_is_draid_spare(const char *name
)
1349 uint64_t spare_id
, parity
, vdev_id
;
1351 if (sscanf(name
, VDEV_TYPE_DRAID
"%llu-%llu-%llu",
1352 (u_longlong_t
*)&parity
, (u_longlong_t
*)&vdev_id
,
1353 (u_longlong_t
*)&spare_id
) == 3) {
1361 * Create the named pool, using the provided vdev list. It is assumed
1362 * that the consumer has already validated the contents of the nvlist, so we
1363 * don't have to worry about error semantics.
1366 zpool_create(libzfs_handle_t
*hdl
, const char *pool
, nvlist_t
*nvroot
,
1367 nvlist_t
*props
, nvlist_t
*fsprops
)
1369 zfs_cmd_t zc
= {"\0"};
1370 nvlist_t
*zc_fsprops
= NULL
;
1371 nvlist_t
*zc_props
= NULL
;
1372 nvlist_t
*hidden_args
= NULL
;
1373 uint8_t *wkeydata
= NULL
;
1375 char errbuf
[ERRBUFLEN
];
1378 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1379 "cannot create '%s'"), pool
);
1381 if (!zpool_name_valid(hdl
, B_FALSE
, pool
))
1382 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
1384 zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
);
1387 prop_flags_t flags
= { .create
= B_TRUE
, .import
= B_FALSE
};
1389 if ((zc_props
= zpool_valid_proplist(hdl
, pool
, props
,
1390 SPA_VERSION_1
, flags
, errbuf
)) == NULL
) {
1399 zoned
= ((nvlist_lookup_string(fsprops
,
1400 zfs_prop_to_name(ZFS_PROP_ZONED
), &zonestr
) == 0) &&
1401 strcmp(zonestr
, "on") == 0);
1403 if ((zc_fsprops
= zfs_valid_proplist(hdl
, ZFS_TYPE_FILESYSTEM
,
1404 fsprops
, zoned
, NULL
, NULL
, B_TRUE
, errbuf
)) == NULL
) {
1408 if (nvlist_exists(zc_fsprops
,
1409 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS
)) &&
1410 !zpool_has_special_vdev(nvroot
)) {
1411 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1412 "%s property requires a special vdev"),
1413 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS
));
1414 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
1419 (nvlist_alloc(&zc_props
, NV_UNIQUE_NAME
, 0) != 0)) {
1422 if (zfs_crypto_create(hdl
, NULL
, zc_fsprops
, props
, B_TRUE
,
1423 &wkeydata
, &wkeylen
) != 0) {
1424 zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
);
1427 if (nvlist_add_nvlist(zc_props
,
1428 ZPOOL_ROOTFS_PROPS
, zc_fsprops
) != 0) {
1431 if (wkeydata
!= NULL
) {
1432 if (nvlist_alloc(&hidden_args
, NV_UNIQUE_NAME
, 0) != 0)
1435 if (nvlist_add_uint8_array(hidden_args
, "wkeydata",
1436 wkeydata
, wkeylen
) != 0)
1439 if (nvlist_add_nvlist(zc_props
, ZPOOL_HIDDEN_ARGS
,
1446 zcmd_write_src_nvlist(hdl
, &zc
, zc_props
);
1448 (void) strlcpy(zc
.zc_name
, pool
, sizeof (zc
.zc_name
));
1450 if ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_CREATE
, &zc
)) != 0) {
1452 zcmd_free_nvlists(&zc
);
1453 nvlist_free(zc_props
);
1454 nvlist_free(zc_fsprops
);
1455 nvlist_free(hidden_args
);
1456 if (wkeydata
!= NULL
)
1462 * This can happen if the user has specified the same
1463 * device multiple times. We can't reliably detect this
1464 * until we try to add it and see we already have a
1465 * label. This can also happen under if the device is
1466 * part of an active md or lvm device.
1468 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1469 "one or more vdevs refer to the same device, or "
1470 "one of\nthe devices is part of an active md or "
1472 return (zfs_error(hdl
, EZFS_BADDEV
, errbuf
));
1476 * This happens if the record size is smaller or larger
1477 * than the allowed size range, or not a power of 2.
1479 * NOTE: although zfs_valid_proplist is called earlier,
1480 * this case may have slipped through since the
1481 * pool does not exist yet and it is therefore
1482 * impossible to read properties e.g. max blocksize
1485 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1486 "record size invalid"));
1487 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1491 * This occurs when one of the devices is below
1492 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1493 * device was the problem device since there's no
1494 * reliable way to determine device size from userland.
1499 zfs_nicebytes(SPA_MINDEVSIZE
, buf
,
1502 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1503 "one or more devices is less than the "
1504 "minimum size (%s)"), buf
);
1506 return (zfs_error(hdl
, EZFS_BADDEV
, errbuf
));
1509 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1510 "one or more devices is out of space"));
1511 return (zfs_error(hdl
, EZFS_BADDEV
, errbuf
));
1514 if (zpool_has_draid_vdev(nvroot
) &&
1515 zfeature_lookup_name("draid", NULL
) != 0) {
1516 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1517 "dRAID vdevs are unsupported by the "
1519 return (zfs_error(hdl
, EZFS_BADDEV
, errbuf
));
1521 return (zpool_standard_error(hdl
, errno
,
1526 return (zpool_standard_error(hdl
, errno
, errbuf
));
1531 zcmd_free_nvlists(&zc
);
1532 nvlist_free(zc_props
);
1533 nvlist_free(zc_fsprops
);
1534 nvlist_free(hidden_args
);
1535 if (wkeydata
!= NULL
)
1541 * Destroy the given pool. It is up to the caller to ensure that there are no
1542 * datasets left in the pool.
1545 zpool_destroy(zpool_handle_t
*zhp
, const char *log_str
)
1547 zfs_cmd_t zc
= {"\0"};
1548 zfs_handle_t
*zfp
= NULL
;
1549 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1550 char errbuf
[ERRBUFLEN
];
1552 if (zhp
->zpool_state
== POOL_STATE_ACTIVE
&&
1553 (zfp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
1556 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1557 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1559 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_DESTROY
, &zc
) != 0) {
1560 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1561 "cannot destroy '%s'"), zhp
->zpool_name
);
1563 if (errno
== EROFS
) {
1564 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1565 "one or more devices is read only"));
1566 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
1568 (void) zpool_standard_error(hdl
, errno
, errbuf
);
1577 remove_mountpoint(zfp
);
1585 * Create a checkpoint in the given pool.
1588 zpool_checkpoint(zpool_handle_t
*zhp
)
1590 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1591 char errbuf
[ERRBUFLEN
];
1594 error
= lzc_pool_checkpoint(zhp
->zpool_name
);
1596 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1597 "cannot checkpoint '%s'"), zhp
->zpool_name
);
1598 (void) zpool_standard_error(hdl
, error
, errbuf
);
1606 * Discard the checkpoint from the given pool.
1609 zpool_discard_checkpoint(zpool_handle_t
*zhp
)
1611 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1612 char errbuf
[ERRBUFLEN
];
1615 error
= lzc_pool_checkpoint_discard(zhp
->zpool_name
);
1617 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1618 "cannot discard checkpoint in '%s'"), zhp
->zpool_name
);
1619 (void) zpool_standard_error(hdl
, error
, errbuf
);
1627 * Add the given vdevs to the pool. The caller must have already performed the
1628 * necessary verification to ensure that the vdev specification is well-formed.
1631 zpool_add(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
1633 zfs_cmd_t zc
= {"\0"};
1635 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1636 char errbuf
[ERRBUFLEN
];
1637 nvlist_t
**spares
, **l2cache
;
1638 uint_t nspares
, nl2cache
;
1640 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1641 "cannot add to '%s'"), zhp
->zpool_name
);
1643 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1644 SPA_VERSION_SPARES
&&
1645 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1646 &spares
, &nspares
) == 0) {
1647 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1648 "upgraded to add hot spares"));
1649 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
1652 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1653 SPA_VERSION_L2CACHE
&&
1654 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1655 &l2cache
, &nl2cache
) == 0) {
1656 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1657 "upgraded to add cache devices"));
1658 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
1661 zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
);
1662 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1664 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_ADD
, &zc
) != 0) {
1668 * This can happen if the user has specified the same
1669 * device multiple times. We can't reliably detect this
1670 * until we try to add it and see we already have a
1673 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1674 "one or more vdevs refer to the same device"));
1675 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
1680 if (zpool_has_draid_vdev(nvroot
) &&
1681 zfeature_lookup_name("draid", NULL
) != 0) {
1682 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1683 "dRAID vdevs are unsupported by the "
1686 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1687 "invalid config; a pool with removing/"
1688 "removed vdevs does not support adding "
1689 "raidz or dRAID vdevs"));
1692 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
1697 * This occurs when one of the devices is below
1698 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1699 * device was the problem device since there's no
1700 * reliable way to determine device size from userland.
1705 zfs_nicebytes(SPA_MINDEVSIZE
, buf
,
1708 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1709 "device is less than the minimum "
1712 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
1716 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1717 "pool must be upgraded to add these vdevs"));
1718 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
1722 (void) zpool_standard_error(hdl
, errno
, errbuf
);
1730 zcmd_free_nvlists(&zc
);
1736 * Exports the pool from the system. The caller must ensure that there are no
1737 * mounted datasets in the pool.
1740 zpool_export_common(zpool_handle_t
*zhp
, boolean_t force
, boolean_t hardforce
,
1741 const char *log_str
)
1743 zfs_cmd_t zc
= {"\0"};
1745 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1746 zc
.zc_cookie
= force
;
1747 zc
.zc_guid
= hardforce
;
1748 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1750 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_EXPORT
, &zc
) != 0) {
1753 zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
1754 "use '-f' to override the following errors:\n"
1755 "'%s' has an active shared spare which could be"
1756 " used by other pools once '%s' is exported."),
1757 zhp
->zpool_name
, zhp
->zpool_name
);
1758 return (zfs_error_fmt(zhp
->zpool_hdl
, EZFS_ACTIVE_SPARE
,
1759 dgettext(TEXT_DOMAIN
, "cannot export '%s'"),
1762 return (zpool_standard_error_fmt(zhp
->zpool_hdl
, errno
,
1763 dgettext(TEXT_DOMAIN
, "cannot export '%s'"),
1772 zpool_export(zpool_handle_t
*zhp
, boolean_t force
, const char *log_str
)
1774 return (zpool_export_common(zhp
, force
, B_FALSE
, log_str
));
1778 zpool_export_force(zpool_handle_t
*zhp
, const char *log_str
)
1780 return (zpool_export_common(zhp
, B_TRUE
, B_TRUE
, log_str
));
1784 zpool_rewind_exclaim(libzfs_handle_t
*hdl
, const char *name
, boolean_t dryrun
,
1787 nvlist_t
*nv
= NULL
;
1793 if (!hdl
->libzfs_printerr
|| config
== NULL
)
1796 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1797 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0) {
1801 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1803 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1805 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1806 strftime(timestr
, 128, "%c", &t
) != 0) {
1808 (void) printf(dgettext(TEXT_DOMAIN
,
1809 "Would be able to return %s "
1810 "to its state as of %s.\n"),
1813 (void) printf(dgettext(TEXT_DOMAIN
,
1814 "Pool %s returned to its state as of %s.\n"),
1818 (void) printf(dgettext(TEXT_DOMAIN
,
1819 "%s approximately %lld "),
1820 dryrun
? "Would discard" : "Discarded",
1821 ((longlong_t
)loss
+ 30) / 60);
1822 (void) printf(dgettext(TEXT_DOMAIN
,
1823 "minutes of transactions.\n"));
1824 } else if (loss
> 0) {
1825 (void) printf(dgettext(TEXT_DOMAIN
,
1826 "%s approximately %lld "),
1827 dryrun
? "Would discard" : "Discarded",
1829 (void) printf(dgettext(TEXT_DOMAIN
,
1830 "seconds of transactions.\n"));
1836 zpool_explain_recover(libzfs_handle_t
*hdl
, const char *name
, int reason
,
1839 nvlist_t
*nv
= NULL
;
1841 uint64_t edata
= UINT64_MAX
;
1846 if (!hdl
->libzfs_printerr
)
1850 (void) printf(dgettext(TEXT_DOMAIN
, "action: "));
1852 (void) printf(dgettext(TEXT_DOMAIN
, "\t"));
1854 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1855 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1856 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0 ||
1857 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1860 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1861 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_DATA_ERRORS
,
1864 (void) printf(dgettext(TEXT_DOMAIN
,
1865 "Recovery is possible, but will result in some data loss.\n"));
1867 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1868 strftime(timestr
, 128, "%c", &t
) != 0) {
1869 (void) printf(dgettext(TEXT_DOMAIN
,
1870 "\tReturning the pool to its state as of %s\n"
1871 "\tshould correct the problem. "),
1874 (void) printf(dgettext(TEXT_DOMAIN
,
1875 "\tReverting the pool to an earlier state "
1876 "should correct the problem.\n\t"));
1880 (void) printf(dgettext(TEXT_DOMAIN
,
1881 "Approximately %lld minutes of data\n"
1882 "\tmust be discarded, irreversibly. "),
1883 ((longlong_t
)loss
+ 30) / 60);
1884 } else if (loss
> 0) {
1885 (void) printf(dgettext(TEXT_DOMAIN
,
1886 "Approximately %lld seconds of data\n"
1887 "\tmust be discarded, irreversibly. "),
1890 if (edata
!= 0 && edata
!= UINT64_MAX
) {
1892 (void) printf(dgettext(TEXT_DOMAIN
,
1893 "After rewind, at least\n"
1894 "\tone persistent user-data error will remain. "));
1896 (void) printf(dgettext(TEXT_DOMAIN
,
1897 "After rewind, several\n"
1898 "\tpersistent user-data errors will remain. "));
1901 (void) printf(dgettext(TEXT_DOMAIN
,
1902 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1903 reason
>= 0 ? "clear" : "import", name
);
1905 (void) printf(dgettext(TEXT_DOMAIN
,
1906 "A scrub of the pool\n"
1907 "\tis strongly recommended after recovery.\n"));
1911 (void) printf(dgettext(TEXT_DOMAIN
,
1912 "Destroy and re-create the pool from\n\ta backup source.\n"));
1916 * zpool_import() is a contracted interface. Should be kept the same
1919 * Applications should use zpool_import_props() to import a pool with
1920 * new properties value to be set.
1923 zpool_import(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1926 nvlist_t
*props
= NULL
;
1929 if (altroot
!= NULL
) {
1930 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
1931 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1932 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1936 if (nvlist_add_string(props
,
1937 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), altroot
) != 0 ||
1938 nvlist_add_string(props
,
1939 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
), "none") != 0) {
1941 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1942 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1947 ret
= zpool_import_props(hdl
, config
, newname
, props
,
1954 print_vdev_tree(libzfs_handle_t
*hdl
, const char *name
, nvlist_t
*nv
,
1960 uint64_t is_log
= 0;
1962 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
,
1966 (void) printf("\t%*s%s%s\n", indent
, "", name
,
1967 is_log
? " [log]" : "");
1969 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1970 &child
, &children
) != 0)
1973 for (c
= 0; c
< children
; c
++) {
1974 vname
= zpool_vdev_name(hdl
, NULL
, child
[c
], VDEV_NAME_TYPE_ID
);
1975 print_vdev_tree(hdl
, vname
, child
[c
], indent
+ 2);
1981 zpool_print_unsup_feat(nvlist_t
*config
)
1983 nvlist_t
*nvinfo
, *unsup_feat
;
1985 nvinfo
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
);
1986 unsup_feat
= fnvlist_lookup_nvlist(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
);
1988 for (nvpair_t
*nvp
= nvlist_next_nvpair(unsup_feat
, NULL
);
1989 nvp
!= NULL
; nvp
= nvlist_next_nvpair(unsup_feat
, nvp
)) {
1990 char *desc
= fnvpair_value_string(nvp
);
1991 if (strlen(desc
) > 0)
1992 (void) printf("\t%s (%s)\n", nvpair_name(nvp
), desc
);
1994 (void) printf("\t%s\n", nvpair_name(nvp
));
1999 * Import the given pool using the known configuration and a list of
2000 * properties to be set. The configuration should have come from
2001 * zpool_find_import(). The 'newname' parameters control whether the pool
2002 * is imported with a different name.
2005 zpool_import_props(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
2006 nvlist_t
*props
, int flags
)
2008 zfs_cmd_t zc
= {"\0"};
2009 zpool_load_policy_t policy
;
2010 nvlist_t
*nv
= NULL
;
2011 nvlist_t
*nvinfo
= NULL
;
2012 nvlist_t
*missing
= NULL
;
2013 const char *thename
;
2017 char errbuf
[ERRBUFLEN
];
2019 origname
= fnvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
);
2021 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2022 "cannot import pool '%s'"), origname
);
2024 if (newname
!= NULL
) {
2025 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
2026 return (zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
2027 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
2034 if (props
!= NULL
) {
2036 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
2038 version
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
2040 if ((props
= zpool_valid_proplist(hdl
, origname
,
2041 props
, version
, flags
, errbuf
)) == NULL
)
2043 zcmd_write_src_nvlist(hdl
, &zc
, props
);
2047 (void) strlcpy(zc
.zc_name
, thename
, sizeof (zc
.zc_name
));
2049 zc
.zc_guid
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
);
2051 zcmd_write_conf_nvlist(hdl
, &zc
, config
);
2052 zcmd_alloc_dst_nvlist(hdl
, &zc
, zc
.zc_nvlist_conf_size
* 2);
2054 zc
.zc_cookie
= flags
;
2055 while ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_IMPORT
, &zc
)) != 0 &&
2057 zcmd_expand_dst_nvlist(hdl
, &zc
);
2061 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nv
);
2063 zcmd_free_nvlists(&zc
);
2065 zpool_get_load_policy(config
, &policy
);
2072 * Dry-run failed, but we print out what success
2073 * looks like if we found a best txg
2075 if (policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) {
2076 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
2082 if (newname
== NULL
)
2083 (void) snprintf(desc
, sizeof (desc
),
2084 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
2087 (void) snprintf(desc
, sizeof (desc
),
2088 dgettext(TEXT_DOMAIN
, "cannot import '%s' as '%s'"),
2093 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
2094 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
2095 nvlist_exists(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
)) {
2096 (void) printf(dgettext(TEXT_DOMAIN
, "This "
2097 "pool uses the following feature(s) not "
2098 "supported by this system:\n"));
2099 zpool_print_unsup_feat(nv
);
2100 if (nvlist_exists(nvinfo
,
2101 ZPOOL_CONFIG_CAN_RDONLY
)) {
2102 (void) printf(dgettext(TEXT_DOMAIN
,
2103 "All unsupported features are only "
2104 "required for writing to the pool."
2105 "\nThe pool can be imported using "
2106 "'-o readonly=on'.\n"));
2110 * Unsupported version.
2112 (void) zfs_error(hdl
, EZFS_BADVERSION
, desc
);
2116 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
2117 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0) {
2118 const char *hostname
= "<unknown>";
2119 uint64_t hostid
= 0;
2120 mmp_state_t mmp_state
;
2122 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
2123 ZPOOL_CONFIG_MMP_STATE
);
2125 if (nvlist_exists(nvinfo
,
2126 ZPOOL_CONFIG_MMP_HOSTNAME
))
2127 hostname
= fnvlist_lookup_string(nvinfo
,
2128 ZPOOL_CONFIG_MMP_HOSTNAME
);
2130 if (nvlist_exists(nvinfo
,
2131 ZPOOL_CONFIG_MMP_HOSTID
))
2132 hostid
= fnvlist_lookup_uint64(nvinfo
,
2133 ZPOOL_CONFIG_MMP_HOSTID
);
2135 if (mmp_state
== MMP_STATE_ACTIVE
) {
2136 (void) snprintf(aux
, sizeof (aux
),
2137 dgettext(TEXT_DOMAIN
, "pool is imp"
2138 "orted on host '%s' (hostid=%lx).\n"
2139 "Export the pool on the other "
2140 "system, then run 'zpool import'."),
2141 hostname
, (unsigned long) hostid
);
2142 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
2143 (void) snprintf(aux
, sizeof (aux
),
2144 dgettext(TEXT_DOMAIN
, "pool has "
2145 "the multihost property on and "
2146 "the\nsystem's hostid is not set. "
2147 "Set a unique system hostid with "
2148 "the zgenhostid(8) command.\n"));
2151 (void) zfs_error_aux(hdl
, "%s", aux
);
2153 (void) zfs_error(hdl
, EZFS_ACTIVE_POOL
, desc
);
2157 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, desc
);
2161 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2162 "one or more devices is read only"));
2163 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
2167 if (nv
&& nvlist_lookup_nvlist(nv
,
2168 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
2169 nvlist_lookup_nvlist(nvinfo
,
2170 ZPOOL_CONFIG_MISSING_DEVICES
, &missing
) == 0) {
2171 (void) printf(dgettext(TEXT_DOMAIN
,
2172 "The devices below are missing or "
2173 "corrupted, use '-m' to import the pool "
2175 print_vdev_tree(hdl
, NULL
, missing
, 2);
2176 (void) printf("\n");
2178 (void) zpool_standard_error(hdl
, error
, desc
);
2182 (void) zpool_standard_error(hdl
, error
, desc
);
2186 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2187 "one or more devices are already in use\n"));
2188 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
2191 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2192 "new name of at least one dataset is longer than "
2193 "the maximum allowable length"));
2194 (void) zfs_error(hdl
, EZFS_NAMETOOLONG
, desc
);
2197 (void) zpool_standard_error(hdl
, error
, desc
);
2198 zpool_explain_recover(hdl
,
2199 newname
? origname
: thename
, -error
, nv
);
2206 zpool_handle_t
*zhp
;
2209 * This should never fail, but play it safe anyway.
2211 if (zpool_open_silent(hdl
, thename
, &zhp
) != 0)
2213 else if (zhp
!= NULL
)
2215 if (policy
.zlp_rewind
&
2216 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
2217 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
2218 ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) != 0), nv
);
2227 * Translate vdev names to guids. If a vdev_path is determined to be
2228 * unsuitable then a vd_errlist is allocated and the vdev path and errno
2232 zpool_translate_vdev_guids(zpool_handle_t
*zhp
, nvlist_t
*vds
,
2233 nvlist_t
*vdev_guids
, nvlist_t
*guids_to_paths
, nvlist_t
**vd_errlist
)
2235 nvlist_t
*errlist
= NULL
;
2238 for (nvpair_t
*elem
= nvlist_next_nvpair(vds
, NULL
); elem
!= NULL
;
2239 elem
= nvlist_next_nvpair(vds
, elem
)) {
2240 boolean_t spare
, cache
;
2242 char *vd_path
= nvpair_name(elem
);
2243 nvlist_t
*tgt
= zpool_find_vdev(zhp
, vd_path
, &spare
, &cache
,
2246 if ((tgt
== NULL
) || cache
|| spare
) {
2247 if (errlist
== NULL
) {
2248 errlist
= fnvlist_alloc();
2252 uint64_t err
= (tgt
== NULL
) ? EZFS_NODEVICE
:
2253 (spare
? EZFS_ISSPARE
: EZFS_ISL2CACHE
);
2254 fnvlist_add_int64(errlist
, vd_path
, err
);
2258 uint64_t guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
2259 fnvlist_add_uint64(vdev_guids
, vd_path
, guid
);
2261 char msg
[MAXNAMELEN
];
2262 (void) snprintf(msg
, sizeof (msg
), "%llu", (u_longlong_t
)guid
);
2263 fnvlist_add_string(guids_to_paths
, msg
, vd_path
);
2267 verify(errlist
!= NULL
);
2268 if (vd_errlist
!= NULL
)
2269 *vd_errlist
= errlist
;
2271 fnvlist_free(errlist
);
2278 xlate_init_err(int err
)
2282 return (EZFS_NODEVICE
);
2285 return (EZFS_BADDEV
);
2287 return (EZFS_INITIALIZING
);
2289 return (EZFS_NO_INITIALIZE
);
2295 * Begin, suspend, or cancel the initialization (initializing of all free
2296 * blocks) for the given vdevs in the given pool.
2299 zpool_initialize_impl(zpool_handle_t
*zhp
, pool_initialize_func_t cmd_type
,
2300 nvlist_t
*vds
, boolean_t wait
)
2304 nvlist_t
*vdev_guids
= fnvlist_alloc();
2305 nvlist_t
*guids_to_paths
= fnvlist_alloc();
2306 nvlist_t
*vd_errlist
= NULL
;
2310 err
= zpool_translate_vdev_guids(zhp
, vds
, vdev_guids
,
2311 guids_to_paths
, &vd_errlist
);
2314 verify(vd_errlist
!= NULL
);
2318 err
= lzc_initialize(zhp
->zpool_name
, cmd_type
,
2319 vdev_guids
, &errlist
);
2322 if (errlist
!= NULL
) {
2323 vd_errlist
= fnvlist_lookup_nvlist(errlist
,
2324 ZPOOL_INITIALIZE_VDEVS
);
2327 (void) zpool_standard_error(zhp
->zpool_hdl
, err
,
2328 dgettext(TEXT_DOMAIN
, "operation failed"));
2333 for (elem
= nvlist_next_nvpair(vdev_guids
, NULL
); elem
!= NULL
;
2334 elem
= nvlist_next_nvpair(vdev_guids
, elem
)) {
2336 uint64_t guid
= fnvpair_value_uint64(elem
);
2338 err
= lzc_wait_tag(zhp
->zpool_name
,
2339 ZPOOL_WAIT_INITIALIZE
, guid
, NULL
);
2341 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
,
2342 err
, dgettext(TEXT_DOMAIN
, "error "
2343 "waiting for '%s' to initialize"),
2353 for (elem
= nvlist_next_nvpair(vd_errlist
, NULL
); elem
!= NULL
;
2354 elem
= nvlist_next_nvpair(vd_errlist
, elem
)) {
2355 int64_t vd_error
= xlate_init_err(fnvpair_value_int64(elem
));
2358 if (nvlist_lookup_string(guids_to_paths
, nvpair_name(elem
),
2360 path
= nvpair_name(elem
);
2362 (void) zfs_error_fmt(zhp
->zpool_hdl
, vd_error
,
2363 "cannot initialize '%s'", path
);
2367 fnvlist_free(vdev_guids
);
2368 fnvlist_free(guids_to_paths
);
2370 if (vd_errlist
!= NULL
)
2371 fnvlist_free(vd_errlist
);
2373 return (err
== 0 ? 0 : -1);
2377 zpool_initialize(zpool_handle_t
*zhp
, pool_initialize_func_t cmd_type
,
2380 return (zpool_initialize_impl(zhp
, cmd_type
, vds
, B_FALSE
));
2384 zpool_initialize_wait(zpool_handle_t
*zhp
, pool_initialize_func_t cmd_type
,
2387 return (zpool_initialize_impl(zhp
, cmd_type
, vds
, B_TRUE
));
2391 xlate_trim_err(int err
)
2395 return (EZFS_NODEVICE
);
2398 return (EZFS_BADDEV
);
2400 return (EZFS_TRIMMING
);
2402 return (EZFS_NO_TRIM
);
2404 return (EZFS_TRIM_NOTSUP
);
2410 zpool_trim_wait(zpool_handle_t
*zhp
, nvlist_t
*vdev_guids
)
2415 for (elem
= nvlist_next_nvpair(vdev_guids
, NULL
); elem
!= NULL
;
2416 elem
= nvlist_next_nvpair(vdev_guids
, elem
)) {
2418 uint64_t guid
= fnvpair_value_uint64(elem
);
2420 err
= lzc_wait_tag(zhp
->zpool_name
,
2421 ZPOOL_WAIT_TRIM
, guid
, NULL
);
2423 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
,
2424 err
, dgettext(TEXT_DOMAIN
, "error "
2425 "waiting to trim '%s'"), nvpair_name(elem
));
2434 * Check errlist and report any errors, omitting ones which should be
2435 * suppressed. Returns B_TRUE if any errors were reported.
2438 check_trim_errs(zpool_handle_t
*zhp
, trimflags_t
*trim_flags
,
2439 nvlist_t
*guids_to_paths
, nvlist_t
*vds
, nvlist_t
*errlist
)
2442 boolean_t reported_errs
= B_FALSE
;
2444 int num_suppressed_errs
= 0;
2446 for (elem
= nvlist_next_nvpair(vds
, NULL
);
2447 elem
!= NULL
; elem
= nvlist_next_nvpair(vds
, elem
)) {
2451 for (elem
= nvlist_next_nvpair(errlist
, NULL
);
2452 elem
!= NULL
; elem
= nvlist_next_nvpair(errlist
, elem
)) {
2453 int64_t vd_error
= xlate_trim_err(fnvpair_value_int64(elem
));
2457 * If only the pool was specified, and it was not a secure
2458 * trim then suppress warnings for individual vdevs which
2459 * do not support trimming.
2461 if (vd_error
== EZFS_TRIM_NOTSUP
&&
2462 trim_flags
->fullpool
&&
2463 !trim_flags
->secure
) {
2464 num_suppressed_errs
++;
2468 reported_errs
= B_TRUE
;
2469 if (nvlist_lookup_string(guids_to_paths
, nvpair_name(elem
),
2471 path
= nvpair_name(elem
);
2473 (void) zfs_error_fmt(zhp
->zpool_hdl
, vd_error
,
2474 "cannot trim '%s'", path
);
2477 if (num_suppressed_errs
== num_vds
) {
2478 (void) zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
2479 "no devices in pool support trim operations"));
2480 (void) (zfs_error(zhp
->zpool_hdl
, EZFS_TRIM_NOTSUP
,
2481 dgettext(TEXT_DOMAIN
, "cannot trim")));
2482 reported_errs
= B_TRUE
;
2485 return (reported_errs
);
2489 * Begin, suspend, or cancel the TRIM (discarding of all free blocks) for
2490 * the given vdevs in the given pool.
2493 zpool_trim(zpool_handle_t
*zhp
, pool_trim_func_t cmd_type
, nvlist_t
*vds
,
2494 trimflags_t
*trim_flags
)
2499 nvlist_t
*vdev_guids
= fnvlist_alloc();
2500 nvlist_t
*guids_to_paths
= fnvlist_alloc();
2501 nvlist_t
*errlist
= NULL
;
2503 err
= zpool_translate_vdev_guids(zhp
, vds
, vdev_guids
,
2504 guids_to_paths
, &errlist
);
2506 check_trim_errs(zhp
, trim_flags
, guids_to_paths
, vds
, errlist
);
2511 err
= lzc_trim(zhp
->zpool_name
, cmd_type
, trim_flags
->rate
,
2512 trim_flags
->secure
, vdev_guids
, &errlist
);
2514 nvlist_t
*vd_errlist
;
2515 if (errlist
!= NULL
&& nvlist_lookup_nvlist(errlist
,
2516 ZPOOL_TRIM_VDEVS
, &vd_errlist
) == 0) {
2517 if (check_trim_errs(zhp
, trim_flags
, guids_to_paths
,
2523 char errbuf
[ERRBUFLEN
];
2525 (void) snprintf(errbuf
, sizeof (errbuf
),
2526 dgettext(TEXT_DOMAIN
, "operation failed"));
2527 zpool_standard_error(zhp
->zpool_hdl
, err
, errbuf
);
2534 if (trim_flags
->wait
)
2535 retval
= zpool_trim_wait(zhp
, vdev_guids
);
2538 if (errlist
!= NULL
)
2539 fnvlist_free(errlist
);
2540 fnvlist_free(vdev_guids
);
2541 fnvlist_free(guids_to_paths
);
2549 zpool_scan(zpool_handle_t
*zhp
, pool_scan_func_t func
, pool_scrub_cmd_t cmd
)
2551 zfs_cmd_t zc
= {"\0"};
2552 char errbuf
[ERRBUFLEN
];
2554 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2556 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2557 zc
.zc_cookie
= func
;
2560 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_SCAN
, &zc
) == 0)
2565 /* ECANCELED on a scrub means we resumed a paused scrub */
2566 if (err
== ECANCELED
&& func
== POOL_SCAN_SCRUB
&&
2567 cmd
== POOL_SCRUB_NORMAL
)
2570 if (err
== ENOENT
&& func
!= POOL_SCAN_NONE
&& cmd
== POOL_SCRUB_NORMAL
)
2573 if (func
== POOL_SCAN_SCRUB
) {
2574 if (cmd
== POOL_SCRUB_PAUSE
) {
2575 (void) snprintf(errbuf
, sizeof (errbuf
),
2576 dgettext(TEXT_DOMAIN
, "cannot pause scrubbing %s"),
2579 assert(cmd
== POOL_SCRUB_NORMAL
);
2580 (void) snprintf(errbuf
, sizeof (errbuf
),
2581 dgettext(TEXT_DOMAIN
, "cannot scrub %s"),
2584 } else if (func
== POOL_SCAN_RESILVER
) {
2585 assert(cmd
== POOL_SCRUB_NORMAL
);
2586 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2587 "cannot restart resilver on %s"), zc
.zc_name
);
2588 } else if (func
== POOL_SCAN_NONE
) {
2589 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2590 "cannot cancel scrubbing %s"), zc
.zc_name
);
2592 assert(!"unexpected result");
2597 pool_scan_stat_t
*ps
= NULL
;
2600 nvroot
= fnvlist_lookup_nvlist(zhp
->zpool_config
,
2601 ZPOOL_CONFIG_VDEV_TREE
);
2602 (void) nvlist_lookup_uint64_array(nvroot
,
2603 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &psc
);
2604 if (ps
&& ps
->pss_func
== POOL_SCAN_SCRUB
&&
2605 ps
->pss_state
== DSS_SCANNING
) {
2606 if (cmd
== POOL_SCRUB_PAUSE
)
2607 return (zfs_error(hdl
, EZFS_SCRUB_PAUSED
,
2610 return (zfs_error(hdl
, EZFS_SCRUBBING
, errbuf
));
2612 return (zfs_error(hdl
, EZFS_RESILVERING
, errbuf
));
2614 } else if (err
== ENOENT
) {
2615 return (zfs_error(hdl
, EZFS_NO_SCRUB
, errbuf
));
2616 } else if (err
== ENOTSUP
&& func
== POOL_SCAN_RESILVER
) {
2617 return (zfs_error(hdl
, EZFS_NO_RESILVER_DEFER
, errbuf
));
2619 return (zpool_standard_error(hdl
, err
, errbuf
));
2624 * Find a vdev that matches the search criteria specified. We use the
2625 * the nvpair name to determine how we should look for the device.
2626 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2627 * spare; but FALSE if its an INUSE spare.
2630 vdev_to_nvlist_iter(nvlist_t
*nv
, nvlist_t
*search
, boolean_t
*avail_spare
,
2631 boolean_t
*l2cache
, boolean_t
*log
)
2638 nvpair_t
*pair
= nvlist_next_nvpair(search
, NULL
);
2640 /* Nothing to look for */
2641 if (search
== NULL
|| pair
== NULL
)
2644 /* Obtain the key we will use to search */
2645 srchkey
= nvpair_name(pair
);
2647 switch (nvpair_type(pair
)) {
2648 case DATA_TYPE_UINT64
:
2649 if (strcmp(srchkey
, ZPOOL_CONFIG_GUID
) == 0) {
2650 uint64_t srchval
= fnvpair_value_uint64(pair
);
2651 uint64_t theguid
= fnvlist_lookup_uint64(nv
,
2653 if (theguid
== srchval
)
2658 case DATA_TYPE_STRING
: {
2659 char *srchval
, *val
;
2661 srchval
= fnvpair_value_string(pair
);
2662 if (nvlist_lookup_string(nv
, srchkey
, &val
) != 0)
2666 * Search for the requested value. Special cases:
2668 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2669 * "-part1", or "p1". The suffix is hidden from the user,
2670 * but included in the string, so this matches around it.
2671 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2672 * is used to check all possible expanded paths.
2673 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2675 * Otherwise, all other searches are simple string compares.
2677 if (strcmp(srchkey
, ZPOOL_CONFIG_PATH
) == 0) {
2678 uint64_t wholedisk
= 0;
2680 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
2682 if (zfs_strcmp_pathname(srchval
, val
, wholedisk
) == 0)
2685 } else if (strcmp(srchkey
, ZPOOL_CONFIG_TYPE
) == 0) {
2686 char *type
, *idx
, *end
, *p
;
2687 uint64_t id
, vdev_id
;
2690 * Determine our vdev type, keeping in mind
2691 * that the srchval is composed of a type and
2692 * vdev id pair (i.e. mirror-4).
2694 if ((type
= strdup(srchval
)) == NULL
)
2697 if ((p
= strrchr(type
, '-')) == NULL
) {
2705 * If the types don't match then keep looking.
2707 if (strncmp(val
, type
, strlen(val
)) != 0) {
2712 verify(zpool_vdev_is_interior(type
));
2714 id
= fnvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
);
2716 vdev_id
= strtoull(idx
, &end
, 10);
2719 * If we are looking for a raidz and a parity is
2720 * specified, make sure it matches.
2722 int rzlen
= strlen(VDEV_TYPE_RAIDZ
);
2723 assert(rzlen
== strlen(VDEV_TYPE_DRAID
));
2724 int typlen
= strlen(type
);
2725 if ((strncmp(type
, VDEV_TYPE_RAIDZ
, rzlen
) == 0 ||
2726 strncmp(type
, VDEV_TYPE_DRAID
, rzlen
) == 0) &&
2728 uint64_t vdev_parity
;
2729 int parity
= *(type
+ rzlen
) - '0';
2731 if (parity
<= 0 || parity
> 3 ||
2732 (typlen
- rzlen
) != 1) {
2734 * Nonsense parity specified, can
2740 vdev_parity
= fnvlist_lookup_uint64(nv
,
2741 ZPOOL_CONFIG_NPARITY
);
2742 if ((int)vdev_parity
!= parity
) {
2753 * Now verify that we have the correct vdev id.
2762 if (strcmp(srchval
, val
) == 0)
2771 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2772 &child
, &children
) != 0)
2775 for (c
= 0; c
< children
; c
++) {
2776 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2777 avail_spare
, l2cache
, NULL
)) != NULL
) {
2779 * The 'is_log' value is only set for the toplevel
2780 * vdev, not the leaf vdevs. So we always lookup the
2781 * log device from the root of the vdev tree (where
2782 * 'log' is non-NULL).
2785 nvlist_lookup_uint64(child
[c
],
2786 ZPOOL_CONFIG_IS_LOG
, &is_log
) == 0 &&
2794 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2795 &child
, &children
) == 0) {
2796 for (c
= 0; c
< children
; c
++) {
2797 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2798 avail_spare
, l2cache
, NULL
)) != NULL
) {
2799 *avail_spare
= B_TRUE
;
2805 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2806 &child
, &children
) == 0) {
2807 for (c
= 0; c
< children
; c
++) {
2808 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2809 avail_spare
, l2cache
, NULL
)) != NULL
) {
2820 * Given a physical path or guid, find the associated vdev.
2823 zpool_find_vdev_by_physpath(zpool_handle_t
*zhp
, const char *ppath
,
2824 boolean_t
*avail_spare
, boolean_t
*l2cache
, boolean_t
*log
)
2826 nvlist_t
*search
, *nvroot
, *ret
;
2830 search
= fnvlist_alloc();
2832 guid
= strtoull(ppath
, &end
, 0);
2833 if (guid
!= 0 && *end
== '\0') {
2834 fnvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
);
2836 fnvlist_add_string(search
, ZPOOL_CONFIG_PHYS_PATH
, ppath
);
2839 nvroot
= fnvlist_lookup_nvlist(zhp
->zpool_config
,
2840 ZPOOL_CONFIG_VDEV_TREE
);
2842 *avail_spare
= B_FALSE
;
2846 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2847 fnvlist_free(search
);
2853 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2856 zpool_vdev_is_interior(const char *name
)
2858 if (strncmp(name
, VDEV_TYPE_RAIDZ
, strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2859 strncmp(name
, VDEV_TYPE_SPARE
, strlen(VDEV_TYPE_SPARE
)) == 0 ||
2861 VDEV_TYPE_REPLACING
, strlen(VDEV_TYPE_REPLACING
)) == 0 ||
2862 strncmp(name
, VDEV_TYPE_MIRROR
, strlen(VDEV_TYPE_MIRROR
)) == 0)
2865 if (strncmp(name
, VDEV_TYPE_DRAID
, strlen(VDEV_TYPE_DRAID
)) == 0 &&
2866 !zpool_is_draid_spare(name
))
2873 zpool_find_vdev(zpool_handle_t
*zhp
, const char *path
, boolean_t
*avail_spare
,
2874 boolean_t
*l2cache
, boolean_t
*log
)
2877 nvlist_t
*nvroot
, *search
, *ret
;
2880 search
= fnvlist_alloc();
2882 guid
= strtoull(path
, &end
, 0);
2883 if (guid
!= 0 && *end
== '\0') {
2884 fnvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
);
2885 } else if (zpool_vdev_is_interior(path
)) {
2886 fnvlist_add_string(search
, ZPOOL_CONFIG_TYPE
, path
);
2888 fnvlist_add_string(search
, ZPOOL_CONFIG_PATH
, path
);
2891 nvroot
= fnvlist_lookup_nvlist(zhp
->zpool_config
,
2892 ZPOOL_CONFIG_VDEV_TREE
);
2894 *avail_spare
= B_FALSE
;
2898 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2899 fnvlist_free(search
);
2905 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
2907 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
2908 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
2912 zpool_vdev_path_to_guid_impl(zpool_handle_t
*zhp
, const char *path
,
2913 boolean_t
*is_spare
, boolean_t
*is_l2cache
, boolean_t
*is_log
)
2915 boolean_t spare
= B_FALSE
, l2cache
= B_FALSE
, log
= B_FALSE
;
2918 if ((tgt
= zpool_find_vdev(zhp
, path
, &spare
, &l2cache
,
2922 if (is_spare
!= NULL
)
2924 if (is_l2cache
!= NULL
)
2925 *is_l2cache
= l2cache
;
2929 return (fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
));
2932 /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
2934 zpool_vdev_path_to_guid(zpool_handle_t
*zhp
, const char *path
)
2936 return (zpool_vdev_path_to_guid_impl(zhp
, path
, NULL
, NULL
, NULL
));
2940 * Bring the specified vdev online. The 'flags' parameter is a set of the
2941 * ZFS_ONLINE_* flags.
2944 zpool_vdev_online(zpool_handle_t
*zhp
, const char *path
, int flags
,
2945 vdev_state_t
*newstate
)
2947 zfs_cmd_t zc
= {"\0"};
2948 char errbuf
[ERRBUFLEN
];
2950 boolean_t avail_spare
, l2cache
, islog
;
2951 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2953 if (flags
& ZFS_ONLINE_EXPAND
) {
2954 (void) snprintf(errbuf
, sizeof (errbuf
),
2955 dgettext(TEXT_DOMAIN
, "cannot expand %s"), path
);
2957 (void) snprintf(errbuf
, sizeof (errbuf
),
2958 dgettext(TEXT_DOMAIN
, "cannot online %s"), path
);
2961 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2962 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2964 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
2966 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
2968 if (!(flags
& ZFS_ONLINE_SPARE
) && avail_spare
)
2969 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
2973 if ((flags
& ZFS_ONLINE_EXPAND
||
2974 zpool_get_prop_int(zhp
, ZPOOL_PROP_AUTOEXPAND
, NULL
)) &&
2975 nvlist_lookup_string(tgt
, ZPOOL_CONFIG_PATH
, &pathname
) == 0) {
2976 uint64_t wholedisk
= 0;
2978 (void) nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_WHOLE_DISK
,
2982 * XXX - L2ARC 1.0 devices can't support expansion.
2985 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2986 "cannot expand cache devices"));
2987 return (zfs_error(hdl
, EZFS_VDEVNOTSUP
, errbuf
));
2991 const char *fullpath
= path
;
2992 char buf
[MAXPATHLEN
];
2995 if (path
[0] != '/') {
2996 error
= zfs_resolve_shortname(path
, buf
,
2999 return (zfs_error(hdl
, EZFS_NODEVICE
,
3005 error
= zpool_relabel_disk(hdl
, fullpath
, errbuf
);
3012 zc
.zc_cookie
= VDEV_STATE_ONLINE
;
3015 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) != 0) {
3016 if (errno
== EINVAL
) {
3017 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "was split "
3018 "from this pool into a new one. Use '%s' "
3019 "instead"), "zpool detach");
3020 return (zfs_error(hdl
, EZFS_POSTSPLIT_ONLINE
, errbuf
));
3022 return (zpool_standard_error(hdl
, errno
, errbuf
));
3025 *newstate
= zc
.zc_cookie
;
3030 * Take the specified vdev offline
3033 zpool_vdev_offline(zpool_handle_t
*zhp
, const char *path
, boolean_t istmp
)
3035 zfs_cmd_t zc
= {"\0"};
3036 char errbuf
[ERRBUFLEN
];
3038 boolean_t avail_spare
, l2cache
;
3039 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3041 (void) snprintf(errbuf
, sizeof (errbuf
),
3042 dgettext(TEXT_DOMAIN
, "cannot offline %s"), path
);
3044 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3045 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3047 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3049 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3052 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
3054 zc
.zc_cookie
= VDEV_STATE_OFFLINE
;
3055 zc
.zc_obj
= istmp
? ZFS_OFFLINE_TEMPORARY
: 0;
3057 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3064 * There are no other replicas of this device.
3066 return (zfs_error(hdl
, EZFS_NOREPLICAS
, errbuf
));
3070 * The log device has unplayed logs
3072 return (zfs_error(hdl
, EZFS_UNPLAYED_LOGS
, errbuf
));
3075 return (zpool_standard_error(hdl
, errno
, errbuf
));
3080 * Remove the specified vdev asynchronously from the configuration, so
3081 * that it may come ONLINE if reinserted. This is called from zed on
3082 * Udev remove event.
3083 * Note: We also have a similar function zpool_vdev_remove() that
3084 * removes the vdev from the pool.
3087 zpool_vdev_remove_wanted(zpool_handle_t
*zhp
, const char *path
)
3089 zfs_cmd_t zc
= {"\0"};
3090 char errbuf
[ERRBUFLEN
];
3092 boolean_t avail_spare
, l2cache
;
3093 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3095 (void) snprintf(errbuf
, sizeof (errbuf
),
3096 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3098 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3099 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3101 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3103 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3105 zc
.zc_cookie
= VDEV_STATE_REMOVED
;
3107 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3110 return (zpool_standard_error(hdl
, errno
, errbuf
));
3114 * Mark the given vdev faulted.
3117 zpool_vdev_fault(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
3119 zfs_cmd_t zc
= {"\0"};
3120 char errbuf
[ERRBUFLEN
];
3121 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3123 (void) snprintf(errbuf
, sizeof (errbuf
),
3124 dgettext(TEXT_DOMAIN
, "cannot fault %llu"), (u_longlong_t
)guid
);
3126 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3128 zc
.zc_cookie
= VDEV_STATE_FAULTED
;
3131 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3138 * There are no other replicas of this device.
3140 return (zfs_error(hdl
, EZFS_NOREPLICAS
, errbuf
));
3143 return (zpool_standard_error(hdl
, errno
, errbuf
));
3149 * Mark the given vdev degraded.
3152 zpool_vdev_degrade(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
3154 zfs_cmd_t zc
= {"\0"};
3155 char errbuf
[ERRBUFLEN
];
3156 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3158 (void) snprintf(errbuf
, sizeof (errbuf
),
3159 dgettext(TEXT_DOMAIN
, "cannot degrade %llu"), (u_longlong_t
)guid
);
3161 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3163 zc
.zc_cookie
= VDEV_STATE_DEGRADED
;
3166 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3169 return (zpool_standard_error(hdl
, errno
, errbuf
));
3173 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
3177 is_replacing_spare(nvlist_t
*search
, nvlist_t
*tgt
, int which
)
3182 if (nvlist_lookup_nvlist_array(search
, ZPOOL_CONFIG_CHILDREN
, &child
,
3184 char *type
= fnvlist_lookup_string(search
, ZPOOL_CONFIG_TYPE
);
3185 if ((strcmp(type
, VDEV_TYPE_SPARE
) == 0 ||
3186 strcmp(type
, VDEV_TYPE_DRAID_SPARE
) == 0) &&
3187 children
== 2 && child
[which
] == tgt
)
3190 for (c
= 0; c
< children
; c
++)
3191 if (is_replacing_spare(child
[c
], tgt
, which
))
3199 * Attach new_disk (fully described by nvroot) to old_disk.
3200 * If 'replacing' is specified, the new disk will replace the old one.
3203 zpool_vdev_attach(zpool_handle_t
*zhp
, const char *old_disk
,
3204 const char *new_disk
, nvlist_t
*nvroot
, int replacing
, boolean_t rebuild
)
3206 zfs_cmd_t zc
= {"\0"};
3207 char errbuf
[ERRBUFLEN
];
3210 boolean_t avail_spare
, l2cache
, islog
;
3215 nvlist_t
*config_root
;
3216 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3219 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3220 "cannot replace %s with %s"), old_disk
, new_disk
);
3222 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3223 "cannot attach %s to %s"), new_disk
, old_disk
);
3225 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3226 if ((tgt
= zpool_find_vdev(zhp
, old_disk
, &avail_spare
, &l2cache
,
3228 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3231 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
3234 return (zfs_error(hdl
, EZFS_ISL2CACHE
, errbuf
));
3236 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3237 zc
.zc_cookie
= replacing
;
3238 zc
.zc_simple
= rebuild
;
3241 zfeature_lookup_guid("org.openzfs:device_rebuild", NULL
) != 0) {
3242 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3243 "the loaded zfs module doesn't support device rebuilds"));
3244 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, errbuf
));
3247 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
3248 &child
, &children
) != 0 || children
!= 1) {
3249 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3250 "new device must be a single disk"));
3251 return (zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
));
3254 config_root
= fnvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
3255 ZPOOL_CONFIG_VDEV_TREE
);
3257 if ((newname
= zpool_vdev_name(NULL
, NULL
, child
[0], 0)) == NULL
)
3261 * If the target is a hot spare that has been swapped in, we can only
3262 * replace it with another hot spare.
3265 nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_IS_SPARE
, &val
) == 0 &&
3266 (zpool_find_vdev(zhp
, newname
, &avail_spare
, &l2cache
,
3267 NULL
) == NULL
|| !avail_spare
) &&
3268 is_replacing_spare(config_root
, tgt
, 1)) {
3269 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3270 "can only be replaced by another hot spare"));
3272 return (zfs_error(hdl
, EZFS_BADTARGET
, errbuf
));
3277 zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
);
3279 ret
= zfs_ioctl(hdl
, ZFS_IOC_VDEV_ATTACH
, &zc
);
3281 zcmd_free_nvlists(&zc
);
3289 * Can't attach to or replace this type of vdev.
3292 uint64_t version
= zpool_get_prop_int(zhp
,
3293 ZPOOL_PROP_VERSION
, NULL
);
3296 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3297 "cannot replace a log with a spare"));
3298 } else if (rebuild
) {
3299 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3300 "only mirror and dRAID vdevs support "
3301 "sequential reconstruction"));
3302 } else if (zpool_is_draid_spare(new_disk
)) {
3303 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3304 "dRAID spares can only replace child "
3305 "devices in their parent's dRAID vdev"));
3306 } else if (version
>= SPA_VERSION_MULTI_REPLACE
) {
3307 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3308 "already in replacing/spare config; wait "
3309 "for completion or use 'zpool detach'"));
3311 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3312 "cannot replace a replacing device"));
3315 char status
[64] = {0};
3316 zpool_prop_get_feature(zhp
,
3317 "feature@device_rebuild", status
, 63);
3319 strncmp(status
, ZFS_FEATURE_DISABLED
, 64) == 0) {
3320 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3321 "device_rebuild feature must be enabled "
3322 "in order to use sequential "
3325 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3326 "can only attach to mirrors and top-level "
3330 (void) zfs_error(hdl
, EZFS_BADTARGET
, errbuf
);
3335 * The new device must be a single disk.
3337 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3338 "new device must be a single disk"));
3339 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
);
3343 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "%s is busy, "
3344 "or device removal is in progress"),
3346 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
3351 * The new device is too small.
3353 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3354 "device is too small"));
3355 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
3360 * The new device has a different optimal sector size.
3362 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3363 "new device has a different optimal sector size; use the "
3364 "option '-o ashift=N' to override the optimal size"));
3365 (void) zfs_error(hdl
, EZFS_BADDEV
, errbuf
);
3370 * The resulting top-level vdev spec won't fit in the label.
3372 (void) zfs_error(hdl
, EZFS_DEVOVERFLOW
, errbuf
);
3376 (void) zpool_standard_error(hdl
, errno
, errbuf
);
3383 * Detach the specified device.
3386 zpool_vdev_detach(zpool_handle_t
*zhp
, const char *path
)
3388 zfs_cmd_t zc
= {"\0"};
3389 char errbuf
[ERRBUFLEN
];
3391 boolean_t avail_spare
, l2cache
;
3392 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3394 (void) snprintf(errbuf
, sizeof (errbuf
),
3395 dgettext(TEXT_DOMAIN
, "cannot detach %s"), path
);
3397 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3398 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3400 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3403 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
3406 return (zfs_error(hdl
, EZFS_ISL2CACHE
, errbuf
));
3408 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3410 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_DETACH
, &zc
) == 0)
3417 * Can't detach from this type of vdev.
3419 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "only "
3420 "applicable to mirror and replacing vdevs"));
3421 (void) zfs_error(hdl
, EZFS_BADTARGET
, errbuf
);
3426 * There are no other replicas of this device.
3428 (void) zfs_error(hdl
, EZFS_NOREPLICAS
, errbuf
);
3432 (void) zpool_standard_error(hdl
, errno
, errbuf
);
3439 * Find a mirror vdev in the source nvlist.
3441 * The mchild array contains a list of disks in one of the top-level mirrors
3442 * of the source pool. The schild array contains a list of disks that the
3443 * user specified on the command line. We loop over the mchild array to
3444 * see if any entry in the schild array matches.
3446 * If a disk in the mchild array is found in the schild array, we return
3447 * the index of that entry. Otherwise we return -1.
3450 find_vdev_entry(zpool_handle_t
*zhp
, nvlist_t
**mchild
, uint_t mchildren
,
3451 nvlist_t
**schild
, uint_t schildren
)
3455 for (mc
= 0; mc
< mchildren
; mc
++) {
3457 char *mpath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
3460 for (sc
= 0; sc
< schildren
; sc
++) {
3461 char *spath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
3463 boolean_t result
= (strcmp(mpath
, spath
) == 0);
3479 * Split a mirror pool. If newroot points to null, then a new nvlist
3480 * is generated and it is the responsibility of the caller to free it.
3483 zpool_vdev_split(zpool_handle_t
*zhp
, char *newname
, nvlist_t
**newroot
,
3484 nvlist_t
*props
, splitflags_t flags
)
3486 zfs_cmd_t zc
= {"\0"};
3487 char errbuf
[ERRBUFLEN
], *bias
;
3488 nvlist_t
*tree
, *config
, **child
, **newchild
, *newconfig
= NULL
;
3489 nvlist_t
**varray
= NULL
, *zc_props
= NULL
;
3490 uint_t c
, children
, newchildren
, lastlog
= 0, vcount
, found
= 0;
3491 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3492 uint64_t vers
, readonly
= B_FALSE
;
3493 boolean_t freelist
= B_FALSE
, memory_err
= B_TRUE
;
3496 (void) snprintf(errbuf
, sizeof (errbuf
),
3497 dgettext(TEXT_DOMAIN
, "Unable to split %s"), zhp
->zpool_name
);
3499 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
3500 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3502 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
3503 (void) fprintf(stderr
, gettext("Internal error: unable to "
3504 "retrieve pool configuration\n"));
3508 tree
= fnvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
);
3509 vers
= fnvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
);
3512 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
3513 if ((zc_props
= zpool_valid_proplist(hdl
, zhp
->zpool_name
,
3514 props
, vers
, flags
, errbuf
)) == NULL
)
3516 (void) nvlist_lookup_uint64(zc_props
,
3517 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
3519 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3520 "property %s can only be set at import time"),
3521 zpool_prop_to_name(ZPOOL_PROP_READONLY
));
3526 if (nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
3528 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3529 "Source pool is missing vdev tree"));
3530 nvlist_free(zc_props
);
3534 varray
= zfs_alloc(hdl
, children
* sizeof (nvlist_t
*));
3537 if (*newroot
== NULL
||
3538 nvlist_lookup_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
3539 &newchild
, &newchildren
) != 0)
3542 for (c
= 0; c
< children
; c
++) {
3543 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
3544 boolean_t is_special
= B_FALSE
, is_dedup
= B_FALSE
;
3546 nvlist_t
**mchild
, *vdev
;
3551 * Unlike cache & spares, slogs are stored in the
3552 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3554 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
3556 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
3558 if (is_log
|| is_hole
) {
3560 * Create a hole vdev and put it in the config.
3562 if (nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) != 0)
3564 if (nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
,
3565 VDEV_TYPE_HOLE
) != 0)
3567 if (nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_HOLE
,
3572 varray
[vcount
++] = vdev
;
3576 type
= fnvlist_lookup_string(child
[c
], ZPOOL_CONFIG_TYPE
);
3578 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0) {
3580 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
3583 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
3584 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3585 "Source pool must be composed only of mirrors\n"));
3586 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
);
3590 if (nvlist_lookup_string(child
[c
],
3591 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
) == 0) {
3592 if (strcmp(bias
, VDEV_ALLOC_BIAS_SPECIAL
) == 0)
3593 is_special
= B_TRUE
;
3594 else if (strcmp(bias
, VDEV_ALLOC_BIAS_DEDUP
) == 0)
3597 verify(nvlist_lookup_nvlist_array(child
[c
],
3598 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
3600 /* find or add an entry for this top-level vdev */
3601 if (newchildren
> 0 &&
3602 (entry
= find_vdev_entry(zhp
, mchild
, mchildren
,
3603 newchild
, newchildren
)) >= 0) {
3604 /* We found a disk that the user specified. */
3605 vdev
= mchild
[entry
];
3608 /* User didn't specify a disk for this vdev. */
3609 vdev
= mchild
[mchildren
- 1];
3612 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
3615 if (flags
.dryrun
!= 0) {
3616 if (is_dedup
== B_TRUE
) {
3617 if (nvlist_add_string(varray
[vcount
- 1],
3618 ZPOOL_CONFIG_ALLOCATION_BIAS
,
3619 VDEV_ALLOC_BIAS_DEDUP
) != 0)
3621 } else if (is_special
== B_TRUE
) {
3622 if (nvlist_add_string(varray
[vcount
- 1],
3623 ZPOOL_CONFIG_ALLOCATION_BIAS
,
3624 VDEV_ALLOC_BIAS_SPECIAL
) != 0)
3630 /* did we find every disk the user specified? */
3631 if (found
!= newchildren
) {
3632 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Device list must "
3633 "include at most one disk from each mirror"));
3634 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
);
3638 /* Prepare the nvlist for populating. */
3639 if (*newroot
== NULL
) {
3640 if (nvlist_alloc(newroot
, NV_UNIQUE_NAME
, 0) != 0)
3643 if (nvlist_add_string(*newroot
, ZPOOL_CONFIG_TYPE
,
3644 VDEV_TYPE_ROOT
) != 0)
3647 verify(nvlist_remove_all(*newroot
, ZPOOL_CONFIG_CHILDREN
) == 0);
3650 /* Add all the children we found */
3651 if (nvlist_add_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
3652 (const nvlist_t
**)varray
, lastlog
== 0 ? vcount
: lastlog
) != 0)
3656 * If we're just doing a dry run, exit now with success.
3659 memory_err
= B_FALSE
;
3664 /* now build up the config list & call the ioctl */
3665 if (nvlist_alloc(&newconfig
, NV_UNIQUE_NAME
, 0) != 0)
3668 if (nvlist_add_nvlist(newconfig
,
3669 ZPOOL_CONFIG_VDEV_TREE
, *newroot
) != 0 ||
3670 nvlist_add_string(newconfig
,
3671 ZPOOL_CONFIG_POOL_NAME
, newname
) != 0 ||
3672 nvlist_add_uint64(newconfig
, ZPOOL_CONFIG_VERSION
, vers
) != 0)
3676 * The new pool is automatically part of the namespace unless we
3677 * explicitly export it.
3680 zc
.zc_cookie
= ZPOOL_EXPORT_AFTER_SPLIT
;
3681 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3682 (void) strlcpy(zc
.zc_string
, newname
, sizeof (zc
.zc_string
));
3683 zcmd_write_conf_nvlist(hdl
, &zc
, newconfig
);
3684 if (zc_props
!= NULL
)
3685 zcmd_write_src_nvlist(hdl
, &zc
, zc_props
);
3687 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SPLIT
, &zc
) != 0) {
3688 retval
= zpool_standard_error(hdl
, errno
, errbuf
);
3693 memory_err
= B_FALSE
;
3696 if (varray
!= NULL
) {
3699 for (v
= 0; v
< vcount
; v
++)
3700 nvlist_free(varray
[v
]);
3703 zcmd_free_nvlists(&zc
);
3704 nvlist_free(zc_props
);
3705 nvlist_free(newconfig
);
3707 nvlist_free(*newroot
);
3715 return (no_memory(hdl
));
3721 * Remove the given device.
3724 zpool_vdev_remove(zpool_handle_t
*zhp
, const char *path
)
3726 zfs_cmd_t zc
= {"\0"};
3727 char errbuf
[ERRBUFLEN
];
3729 boolean_t avail_spare
, l2cache
, islog
;
3730 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3733 (void) snprintf(errbuf
, sizeof (errbuf
),
3734 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3736 if (zpool_is_draid_spare(path
)) {
3737 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3738 "dRAID spares cannot be removed"));
3739 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3742 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3743 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3745 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3747 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
3748 if (islog
&& version
< SPA_VERSION_HOLES
) {
3749 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3750 "pool must be upgraded to support log removal"));
3751 return (zfs_error(hdl
, EZFS_BADVERSION
, errbuf
));
3754 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3756 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3762 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3763 "invalid config; all top-level vdevs must "
3764 "have the same sector size and not be raidz."));
3765 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
);
3770 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3771 "Mount encrypted datasets to replay logs."));
3773 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3774 "Pool busy; removal may already be in progress"));
3776 (void) zfs_error(hdl
, EZFS_BUSY
, errbuf
);
3781 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3782 "Mount encrypted datasets to replay logs."));
3783 (void) zfs_error(hdl
, EZFS_BUSY
, errbuf
);
3785 (void) zpool_standard_error(hdl
, errno
, errbuf
);
3790 (void) zpool_standard_error(hdl
, errno
, errbuf
);
3796 zpool_vdev_remove_cancel(zpool_handle_t
*zhp
)
3798 zfs_cmd_t zc
= {{0}};
3799 char errbuf
[ERRBUFLEN
];
3800 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3802 (void) snprintf(errbuf
, sizeof (errbuf
),
3803 dgettext(TEXT_DOMAIN
, "cannot cancel removal"));
3805 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3808 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3811 return (zpool_standard_error(hdl
, errno
, errbuf
));
3815 zpool_vdev_indirect_size(zpool_handle_t
*zhp
, const char *path
,
3818 char errbuf
[ERRBUFLEN
];
3820 boolean_t avail_spare
, l2cache
, islog
;
3821 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3823 (void) snprintf(errbuf
, sizeof (errbuf
),
3824 dgettext(TEXT_DOMAIN
, "cannot determine indirect size of %s"),
3827 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3829 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3831 if (avail_spare
|| l2cache
|| islog
) {
3836 if (nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_INDIRECT_SIZE
, sizep
) != 0) {
3837 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3838 "indirect size not available"));
3839 return (zfs_error(hdl
, EINVAL
, errbuf
));
3845 * Clear the errors for the pool, or the particular device if specified.
3848 zpool_clear(zpool_handle_t
*zhp
, const char *path
, nvlist_t
*rewindnvl
)
3850 zfs_cmd_t zc
= {"\0"};
3851 char errbuf
[ERRBUFLEN
];
3853 zpool_load_policy_t policy
;
3854 boolean_t avail_spare
, l2cache
;
3855 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3856 nvlist_t
*nvi
= NULL
;
3860 (void) snprintf(errbuf
, sizeof (errbuf
),
3861 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3864 (void) snprintf(errbuf
, sizeof (errbuf
),
3865 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3868 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3870 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
,
3871 &l2cache
, NULL
)) == NULL
)
3872 return (zfs_error(hdl
, EZFS_NODEVICE
, errbuf
));
3875 * Don't allow error clearing for hot spares. Do allow
3876 * error clearing for l2cache devices.
3879 return (zfs_error(hdl
, EZFS_ISSPARE
, errbuf
));
3881 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3884 zpool_get_load_policy(rewindnvl
, &policy
);
3885 zc
.zc_cookie
= policy
.zlp_rewind
;
3887 zcmd_alloc_dst_nvlist(hdl
, &zc
, zhp
->zpool_config_size
* 2);
3888 zcmd_write_src_nvlist(hdl
, &zc
, rewindnvl
);
3890 while ((error
= zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
)) != 0 &&
3892 zcmd_expand_dst_nvlist(hdl
, &zc
);
3894 if (!error
|| ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) &&
3895 errno
!= EPERM
&& errno
!= EACCES
)) {
3896 if (policy
.zlp_rewind
&
3897 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
3898 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nvi
);
3899 zpool_rewind_exclaim(hdl
, zc
.zc_name
,
3900 ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) != 0),
3904 zcmd_free_nvlists(&zc
);
3908 zcmd_free_nvlists(&zc
);
3909 return (zpool_standard_error(hdl
, errno
, errbuf
));
3913 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3916 zpool_vdev_clear(zpool_handle_t
*zhp
, uint64_t guid
)
3918 zfs_cmd_t zc
= {"\0"};
3919 char errbuf
[ERRBUFLEN
];
3920 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3922 (void) snprintf(errbuf
, sizeof (errbuf
),
3923 dgettext(TEXT_DOMAIN
, "cannot clear errors for %llx"),
3924 (u_longlong_t
)guid
);
3926 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3928 zc
.zc_cookie
= ZPOOL_NO_REWIND
;
3930 if (zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
) == 0)
3933 return (zpool_standard_error(hdl
, errno
, errbuf
));
3937 * Change the GUID for a pool.
3940 zpool_reguid(zpool_handle_t
*zhp
)
3942 char errbuf
[ERRBUFLEN
];
3943 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3944 zfs_cmd_t zc
= {"\0"};
3946 (void) snprintf(errbuf
, sizeof (errbuf
),
3947 dgettext(TEXT_DOMAIN
, "cannot reguid '%s'"), zhp
->zpool_name
);
3949 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3950 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REGUID
, &zc
) == 0)
3953 return (zpool_standard_error(hdl
, errno
, errbuf
));
3960 zpool_reopen_one(zpool_handle_t
*zhp
, void *data
)
3962 libzfs_handle_t
*hdl
= zpool_get_handle(zhp
);
3963 const char *pool_name
= zpool_get_name(zhp
);
3964 boolean_t
*scrub_restart
= data
;
3967 error
= lzc_reopen(pool_name
, *scrub_restart
);
3969 return (zpool_standard_error_fmt(hdl
, error
,
3970 dgettext(TEXT_DOMAIN
, "cannot reopen '%s'"), pool_name
));
3976 /* call into libzfs_core to execute the sync IOCTL per pool */
3978 zpool_sync_one(zpool_handle_t
*zhp
, void *data
)
3981 libzfs_handle_t
*hdl
= zpool_get_handle(zhp
);
3982 const char *pool_name
= zpool_get_name(zhp
);
3983 boolean_t
*force
= data
;
3984 nvlist_t
*innvl
= fnvlist_alloc();
3986 fnvlist_add_boolean_value(innvl
, "force", *force
);
3987 if ((ret
= lzc_sync(pool_name
, innvl
, NULL
)) != 0) {
3989 return (zpool_standard_error_fmt(hdl
, ret
,
3990 dgettext(TEXT_DOMAIN
, "sync '%s' failed"), pool_name
));
3997 #define PATH_BUF_LEN 64
4000 * Given a vdev, return the name to display in iostat. If the vdev has a path,
4001 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
4002 * We also check if this is a whole disk, in which case we strip off the
4003 * trailing 's0' slice name.
4005 * This routine is also responsible for identifying when disks have been
4006 * reconfigured in a new location. The kernel will have opened the device by
4007 * devid, but the path will still refer to the old location. To catch this, we
4008 * first do a path -> devid translation (which is fast for the common case). If
4009 * the devid matches, we're done. If not, we do a reverse devid -> path
4010 * translation and issue the appropriate ioctl() to update the path of the vdev.
4011 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
4015 zpool_vdev_name(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, nvlist_t
*nv
,
4021 char buf
[PATH_BUF_LEN
];
4022 char tmpbuf
[PATH_BUF_LEN
* 2];
4025 * vdev_name will be "root"/"root-0" for the root vdev, but it is the
4026 * zpool name that will be displayed to the user.
4028 type
= fnvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
);
4029 if (zhp
!= NULL
&& strcmp(type
, "root") == 0)
4030 return (zfs_strdup(hdl
, zpool_get_name(zhp
)));
4032 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_PATH"))
4033 name_flags
|= VDEV_NAME_PATH
;
4034 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_GUID"))
4035 name_flags
|= VDEV_NAME_GUID
;
4036 if (libzfs_envvar_is_set("ZPOOL_VDEV_NAME_FOLLOW_LINKS"))
4037 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
4039 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
, &value
) == 0 ||
4040 name_flags
& VDEV_NAME_GUID
) {
4041 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
);
4042 (void) snprintf(buf
, sizeof (buf
), "%llu", (u_longlong_t
)value
);
4044 } else if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &tpath
) == 0) {
4047 if (name_flags
& VDEV_NAME_FOLLOW_LINKS
) {
4048 char *rp
= realpath(path
, NULL
);
4050 strlcpy(buf
, rp
, sizeof (buf
));
4057 * For a block device only use the name.
4059 if ((strcmp(type
, VDEV_TYPE_DISK
) == 0) &&
4060 !(name_flags
& VDEV_NAME_PATH
)) {
4061 path
= zfs_strip_path(path
);
4065 * Remove the partition from the path if this is a whole disk.
4067 if (strcmp(type
, VDEV_TYPE_DRAID_SPARE
) != 0 &&
4068 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
, &value
)
4069 == 0 && value
&& !(name_flags
& VDEV_NAME_PATH
)) {
4070 return (zfs_strip_partition(path
));
4076 * If it's a raidz device, we need to stick in the parity level.
4078 if (strcmp(path
, VDEV_TYPE_RAIDZ
) == 0) {
4079 value
= fnvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
);
4080 (void) snprintf(buf
, sizeof (buf
), "%s%llu", path
,
4081 (u_longlong_t
)value
);
4086 * If it's a dRAID device, we add parity, groups, and spares.
4088 if (strcmp(path
, VDEV_TYPE_DRAID
) == 0) {
4089 uint64_t ndata
, nparity
, nspares
;
4093 verify(nvlist_lookup_nvlist_array(nv
,
4094 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) == 0);
4095 nparity
= fnvlist_lookup_uint64(nv
,
4096 ZPOOL_CONFIG_NPARITY
);
4097 ndata
= fnvlist_lookup_uint64(nv
,
4098 ZPOOL_CONFIG_DRAID_NDATA
);
4099 nspares
= fnvlist_lookup_uint64(nv
,
4100 ZPOOL_CONFIG_DRAID_NSPARES
);
4102 path
= zpool_draid_name(buf
, sizeof (buf
), ndata
,
4103 nparity
, nspares
, children
);
4107 * We identify each top-level vdev by using a <type-id>
4108 * naming convention.
4110 if (name_flags
& VDEV_NAME_TYPE_ID
) {
4111 uint64_t id
= fnvlist_lookup_uint64(nv
,
4113 (void) snprintf(tmpbuf
, sizeof (tmpbuf
), "%s-%llu",
4114 path
, (u_longlong_t
)id
);
4119 return (zfs_strdup(hdl
, path
));
4123 zbookmark_mem_compare(const void *a
, const void *b
)
4125 return (memcmp(a
, b
, sizeof (zbookmark_phys_t
)));
4129 * Retrieve the persistent error log, uniquify the members, and return to the
4133 zpool_get_errlog(zpool_handle_t
*zhp
, nvlist_t
**nverrlistp
)
4135 zfs_cmd_t zc
= {"\0"};
4136 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4137 zbookmark_phys_t
*buf
;
4138 uint64_t buflen
= 10000; /* approx. 1MB of RAM */
4140 if (fnvlist_lookup_uint64(zhp
->zpool_config
,
4141 ZPOOL_CONFIG_ERRCOUNT
) == 0)
4145 * Retrieve the raw error list from the kernel. If it doesn't fit,
4146 * allocate a larger buffer and retry.
4148 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
4150 buf
= zfs_alloc(zhp
->zpool_hdl
,
4151 buflen
* sizeof (zbookmark_phys_t
));
4152 zc
.zc_nvlist_dst
= (uintptr_t)buf
;
4153 zc
.zc_nvlist_dst_size
= buflen
;
4154 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_ERROR_LOG
,
4157 if (errno
== ENOMEM
) {
4160 return (zpool_standard_error_fmt(hdl
, errno
,
4161 dgettext(TEXT_DOMAIN
, "errors: List of "
4162 "errors unavailable")));
4170 * Sort the resulting bookmarks. This is a little confusing due to the
4171 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
4172 * to first, and 'zc_nvlist_dst_size' indicates the number of bookmarks
4173 * _not_ copied as part of the process. So we point the start of our
4174 * array appropriate and decrement the total number of elements.
4176 zbookmark_phys_t
*zb
= buf
+ zc
.zc_nvlist_dst_size
;
4177 uint64_t zblen
= buflen
- zc
.zc_nvlist_dst_size
;
4179 qsort(zb
, zblen
, sizeof (zbookmark_phys_t
), zbookmark_mem_compare
);
4181 verify(nvlist_alloc(nverrlistp
, 0, KM_SLEEP
) == 0);
4184 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
4186 for (uint64_t i
= 0; i
< zblen
; i
++) {
4189 /* ignoring zb_blkid and zb_level for now */
4190 if (i
> 0 && zb
[i
-1].zb_objset
== zb
[i
].zb_objset
&&
4191 zb
[i
-1].zb_object
== zb
[i
].zb_object
)
4194 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) != 0)
4196 if (nvlist_add_uint64(nv
, ZPOOL_ERR_DATASET
,
4197 zb
[i
].zb_objset
) != 0) {
4201 if (nvlist_add_uint64(nv
, ZPOOL_ERR_OBJECT
,
4202 zb
[i
].zb_object
) != 0) {
4206 if (nvlist_add_nvlist(*nverrlistp
, "ejk", nv
) != 0) {
4218 return (no_memory(zhp
->zpool_hdl
));
4222 * Upgrade a ZFS pool to the latest on-disk version.
4225 zpool_upgrade(zpool_handle_t
*zhp
, uint64_t new_version
)
4227 zfs_cmd_t zc
= {"\0"};
4228 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4230 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
4231 zc
.zc_cookie
= new_version
;
4233 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_UPGRADE
, &zc
) != 0)
4234 return (zpool_standard_error_fmt(hdl
, errno
,
4235 dgettext(TEXT_DOMAIN
, "cannot upgrade '%s'"),
4241 zfs_save_arguments(int argc
, char **argv
, char *string
, int len
)
4245 (void) strlcpy(string
, zfs_basename(argv
[0]), len
);
4246 for (i
= 1; i
< argc
; i
++) {
4247 (void) strlcat(string
, " ", len
);
4248 (void) strlcat(string
, argv
[i
], len
);
4253 zpool_log_history(libzfs_handle_t
*hdl
, const char *message
)
4255 zfs_cmd_t zc
= {"\0"};
4258 args
= fnvlist_alloc();
4259 fnvlist_add_string(args
, "message", message
);
4260 zcmd_write_src_nvlist(hdl
, &zc
, args
);
4261 int err
= zfs_ioctl(hdl
, ZFS_IOC_LOG_HISTORY
, &zc
);
4263 zcmd_free_nvlists(&zc
);
4268 * Perform ioctl to get some command history of a pool.
4270 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
4271 * logical offset of the history buffer to start reading from.
4273 * Upon return, 'off' is the next logical offset to read from and
4274 * 'len' is the actual amount of bytes read into 'buf'.
4277 get_history(zpool_handle_t
*zhp
, char *buf
, uint64_t *off
, uint64_t *len
)
4279 zfs_cmd_t zc
= {"\0"};
4280 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4282 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4284 zc
.zc_history
= (uint64_t)(uintptr_t)buf
;
4285 zc
.zc_history_len
= *len
;
4286 zc
.zc_history_offset
= *off
;
4288 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_GET_HISTORY
, &zc
) != 0) {
4291 return (zfs_error_fmt(hdl
, EZFS_PERM
,
4292 dgettext(TEXT_DOMAIN
,
4293 "cannot show history for pool '%s'"),
4296 return (zfs_error_fmt(hdl
, EZFS_NOHISTORY
,
4297 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
4298 "'%s'"), zhp
->zpool_name
));
4300 return (zfs_error_fmt(hdl
, EZFS_BADVERSION
,
4301 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
4302 "'%s', pool must be upgraded"), zhp
->zpool_name
));
4304 return (zpool_standard_error_fmt(hdl
, errno
,
4305 dgettext(TEXT_DOMAIN
,
4306 "cannot get history for '%s'"), zhp
->zpool_name
));
4310 *len
= zc
.zc_history_len
;
4311 *off
= zc
.zc_history_offset
;
4317 * Retrieve the command history of a pool.
4320 zpool_get_history(zpool_handle_t
*zhp
, nvlist_t
**nvhisp
, uint64_t *off
,
4323 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4325 int buflen
= 128 * 1024;
4326 nvlist_t
**records
= NULL
;
4327 uint_t numrecords
= 0;
4329 uint64_t start
= *off
;
4331 buf
= zfs_alloc(hdl
, buflen
);
4333 /* process about 1MiB a time */
4334 while (*off
- start
< 1024 * 1024) {
4335 uint64_t bytes_read
= buflen
;
4338 if ((err
= get_history(zhp
, buf
, off
, &bytes_read
)) != 0)
4341 /* if nothing else was read in, we're at EOF, just return */
4347 if ((err
= zpool_history_unpack(buf
, bytes_read
,
4348 &leftover
, &records
, &numrecords
)) != 0) {
4349 zpool_standard_error_fmt(hdl
, err
,
4350 dgettext(TEXT_DOMAIN
,
4351 "cannot get history for '%s'"), zhp
->zpool_name
);
4355 if (leftover
== bytes_read
) {
4357 * no progress made, because buffer is not big enough
4358 * to hold this record; resize and retry.
4362 buf
= zfs_alloc(hdl
, buflen
);
4369 *nvhisp
= fnvlist_alloc();
4370 fnvlist_add_nvlist_array(*nvhisp
, ZPOOL_HIST_RECORD
,
4371 (const nvlist_t
**)records
, numrecords
);
4373 for (i
= 0; i
< numrecords
; i
++)
4374 nvlist_free(records
[i
]);
4381 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
4382 * If there is a new event available 'nvp' will contain a newly allocated
4383 * nvlist and 'dropped' will be set to the number of missed events since
4384 * the last call to this function. When 'nvp' is set to NULL it indicates
4385 * no new events are available. In either case the function returns 0 and
4386 * it is up to the caller to free 'nvp'. In the case of a fatal error the
4387 * function will return a non-zero value. When the function is called in
4388 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
4389 * it will not return until a new event is available.
4392 zpool_events_next(libzfs_handle_t
*hdl
, nvlist_t
**nvp
,
4393 int *dropped
, unsigned flags
, int zevent_fd
)
4395 zfs_cmd_t zc
= {"\0"};
4400 zc
.zc_cleanup_fd
= zevent_fd
;
4402 if (flags
& ZEVENT_NONBLOCK
)
4403 zc
.zc_guid
= ZEVENT_NONBLOCK
;
4405 zcmd_alloc_dst_nvlist(hdl
, &zc
, ZEVENT_SIZE
);
4408 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_NEXT
, &zc
) != 0) {
4411 error
= zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
4412 dgettext(TEXT_DOMAIN
, "zfs shutdown"));
4415 /* Blocking error case should not occur */
4416 if (!(flags
& ZEVENT_NONBLOCK
))
4417 error
= zpool_standard_error_fmt(hdl
, errno
,
4418 dgettext(TEXT_DOMAIN
, "cannot get event"));
4422 zcmd_expand_dst_nvlist(hdl
, &zc
);
4425 error
= zpool_standard_error_fmt(hdl
, errno
,
4426 dgettext(TEXT_DOMAIN
, "cannot get event"));
4431 error
= zcmd_read_dst_nvlist(hdl
, &zc
, nvp
);
4435 *dropped
= (int)zc
.zc_cookie
;
4437 zcmd_free_nvlists(&zc
);
4446 zpool_events_clear(libzfs_handle_t
*hdl
, int *count
)
4448 zfs_cmd_t zc
= {"\0"};
4450 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_CLEAR
, &zc
) != 0)
4451 return (zpool_standard_error(hdl
, errno
,
4452 dgettext(TEXT_DOMAIN
, "cannot clear events")));
4455 *count
= (int)zc
.zc_cookie
; /* # of events cleared */
4461 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4462 * the passed zevent_fd file handle. On success zero is returned,
4463 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4466 zpool_events_seek(libzfs_handle_t
*hdl
, uint64_t eid
, int zevent_fd
)
4468 zfs_cmd_t zc
= {"\0"};
4472 zc
.zc_cleanup_fd
= zevent_fd
;
4474 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_SEEK
, &zc
) != 0) {
4477 error
= zfs_error_fmt(hdl
, EZFS_NOENT
,
4478 dgettext(TEXT_DOMAIN
, "cannot get event"));
4482 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
4483 dgettext(TEXT_DOMAIN
, "cannot get event"));
4487 error
= zpool_standard_error_fmt(hdl
, errno
,
4488 dgettext(TEXT_DOMAIN
, "cannot get event"));
4497 zpool_obj_to_path_impl(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4498 char *pathname
, size_t len
, boolean_t always_unmounted
)
4500 zfs_cmd_t zc
= {"\0"};
4501 boolean_t mounted
= B_FALSE
;
4502 char *mntpnt
= NULL
;
4503 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
4506 /* special case for the MOS */
4507 (void) snprintf(pathname
, len
, "<metadata>:<0x%llx>",
4512 /* get the dataset's name */
4513 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4515 if (zfs_ioctl(zhp
->zpool_hdl
,
4516 ZFS_IOC_DSOBJ_TO_DSNAME
, &zc
) != 0) {
4517 /* just write out a path of two object numbers */
4518 (void) snprintf(pathname
, len
, "<0x%llx>:<0x%llx>",
4519 (longlong_t
)dsobj
, (longlong_t
)obj
);
4522 (void) strlcpy(dsname
, zc
.zc_value
, sizeof (dsname
));
4524 /* find out if the dataset is mounted */
4525 mounted
= !always_unmounted
&& is_mounted(zhp
->zpool_hdl
, dsname
,
4528 /* get the corrupted object's path */
4529 (void) strlcpy(zc
.zc_name
, dsname
, sizeof (zc
.zc_name
));
4531 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_OBJ_TO_PATH
,
4534 (void) snprintf(pathname
, len
, "%s%s", mntpnt
,
4537 (void) snprintf(pathname
, len
, "%s:%s",
4538 dsname
, zc
.zc_value
);
4541 (void) snprintf(pathname
, len
, "%s:<0x%llx>", dsname
,
4548 zpool_obj_to_path(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4549 char *pathname
, size_t len
)
4551 zpool_obj_to_path_impl(zhp
, dsobj
, obj
, pathname
, len
, B_FALSE
);
4555 zpool_obj_to_path_ds(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4556 char *pathname
, size_t len
)
4558 zpool_obj_to_path_impl(zhp
, dsobj
, obj
, pathname
, len
, B_TRUE
);
4561 * Wait while the specified activity is in progress in the pool.
4564 zpool_wait(zpool_handle_t
*zhp
, zpool_wait_activity_t activity
)
4568 int error
= zpool_wait_status(zhp
, activity
, &missing
, NULL
);
4571 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, ENOENT
,
4572 dgettext(TEXT_DOMAIN
, "error waiting in pool '%s'"),
4581 * Wait for the given activity and return the status of the wait (whether or not
4582 * any waiting was done) in the 'waited' parameter. Non-existent pools are
4583 * reported via the 'missing' parameter, rather than by printing an error
4584 * message. This is convenient when this function is called in a loop over a
4585 * long period of time (as it is, for example, by zpool's wait cmd). In that
4586 * scenario, a pool being exported or destroyed should be considered a normal
4587 * event, so we don't want to print an error when we find that the pool doesn't
4591 zpool_wait_status(zpool_handle_t
*zhp
, zpool_wait_activity_t activity
,
4592 boolean_t
*missing
, boolean_t
*waited
)
4594 int error
= lzc_wait(zhp
->zpool_name
, activity
, waited
);
4595 *missing
= (error
== ENOENT
);
4600 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
4601 dgettext(TEXT_DOMAIN
, "error waiting in pool '%s'"),
4609 zpool_set_bootenv(zpool_handle_t
*zhp
, const nvlist_t
*envmap
)
4611 int error
= lzc_set_bootenv(zhp
->zpool_name
, envmap
);
4613 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
4614 dgettext(TEXT_DOMAIN
,
4615 "error setting bootenv in pool '%s'"), zhp
->zpool_name
);
4622 zpool_get_bootenv(zpool_handle_t
*zhp
, nvlist_t
**nvlp
)
4628 error
= lzc_get_bootenv(zhp
->zpool_name
, &nvl
);
4630 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
4631 dgettext(TEXT_DOMAIN
,
4632 "error getting bootenv in pool '%s'"), zhp
->zpool_name
);
4641 * Attempt to read and parse feature file(s) (from "compatibility" property).
4642 * Files contain zpool feature names, comma or whitespace-separated.
4643 * Comments (# character to next newline) are discarded.
4646 * compatibility : string containing feature filenames
4647 * features : either NULL or pointer to array of boolean
4648 * report : either NULL or pointer to string buffer
4649 * rlen : length of "report" buffer
4651 * compatibility is NULL (unset), "", "off", "legacy", or list of
4652 * comma-separated filenames. filenames should either be absolute,
4654 * 1) ZPOOL_SYSCONF_COMPAT_D (eg: /etc/zfs/compatibility.d) or
4655 * 2) ZPOOL_DATA_COMPAT_D (eg: /usr/share/zfs/compatibility.d).
4656 * (Unset), "" or "off" => enable all features
4657 * "legacy" => disable all features
4659 * Any feature names read from files which match unames in spa_feature_table
4660 * will have the corresponding boolean set in the features array (if non-NULL).
4661 * If more than one feature set specified, only features present in *all* of
4664 * "report" if not NULL will be populated with a suitable status message.
4667 * ZPOOL_COMPATIBILITY_OK : files read and parsed ok
4668 * ZPOOL_COMPATIBILITY_BADFILE : file too big or not a text file
4669 * ZPOOL_COMPATIBILITY_BADTOKEN : SYSCONF file contains invalid feature name
4670 * ZPOOL_COMPATIBILITY_WARNTOKEN : DATA file contains invalid feature name
4671 * ZPOOL_COMPATIBILITY_NOFILES : no feature files found
4673 zpool_compat_status_t
4674 zpool_load_compat(const char *compat
, boolean_t
*features
, char *report
,
4677 int sdirfd
, ddirfd
, featfd
;
4681 char *file
, *line
, *word
;
4683 char l_compat
[ZFS_MAXPROPLEN
];
4685 boolean_t ret_nofiles
= B_TRUE
;
4686 boolean_t ret_badfile
= B_FALSE
;
4687 boolean_t ret_badtoken
= B_FALSE
;
4688 boolean_t ret_warntoken
= B_FALSE
;
4690 /* special cases (unset), "" and "off" => enable all features */
4691 if (compat
== NULL
|| compat
[0] == '\0' ||
4692 strcmp(compat
, ZPOOL_COMPAT_OFF
) == 0) {
4693 if (features
!= NULL
)
4694 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4695 features
[i
] = B_TRUE
;
4697 strlcpy(report
, gettext("all features enabled"), rlen
);
4698 return (ZPOOL_COMPATIBILITY_OK
);
4701 /* Final special case "legacy" => disable all features */
4702 if (strcmp(compat
, ZPOOL_COMPAT_LEGACY
) == 0) {
4703 if (features
!= NULL
)
4704 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4705 features
[i
] = B_FALSE
;
4707 strlcpy(report
, gettext("all features disabled"), rlen
);
4708 return (ZPOOL_COMPATIBILITY_OK
);
4712 * Start with all true; will be ANDed with results from each file
4714 if (features
!= NULL
)
4715 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4716 features
[i
] = B_TRUE
;
4718 char err_badfile
[ZFS_MAXPROPLEN
] = "";
4719 char err_badtoken
[ZFS_MAXPROPLEN
] = "";
4722 * We ignore errors from the directory open()
4723 * as they're only needed if the filename is relative
4724 * which will be checked during the openat().
4727 /* O_PATH safer than O_RDONLY if system allows it */
4729 #define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_PATH)
4731 #define ZC_DIR_FLAGS (O_DIRECTORY | O_CLOEXEC | O_RDONLY)
4734 sdirfd
= open(ZPOOL_SYSCONF_COMPAT_D
, ZC_DIR_FLAGS
);
4735 ddirfd
= open(ZPOOL_DATA_COMPAT_D
, ZC_DIR_FLAGS
);
4737 (void) strlcpy(l_compat
, compat
, ZFS_MAXPROPLEN
);
4739 for (file
= strtok_r(l_compat
, ",", &ps
);
4741 file
= strtok_r(NULL
, ",", &ps
)) {
4743 boolean_t l_features
[SPA_FEATURES
];
4745 enum { Z_SYSCONF
, Z_DATA
} source
;
4747 /* try sysconfdir first, then datadir */
4749 if ((featfd
= openat(sdirfd
, file
, O_RDONLY
| O_CLOEXEC
)) < 0) {
4750 featfd
= openat(ddirfd
, file
, O_RDONLY
| O_CLOEXEC
);
4754 /* File readable and correct size? */
4756 fstat(featfd
, &fs
) < 0 ||
4758 fs
.st_size
> ZPOOL_COMPAT_MAXSIZE
) {
4759 (void) close(featfd
);
4760 strlcat(err_badfile
, file
, ZFS_MAXPROPLEN
);
4761 strlcat(err_badfile
, " ", ZFS_MAXPROPLEN
);
4762 ret_badfile
= B_TRUE
;
4766 /* Prefault the file if system allows */
4767 #if defined(MAP_POPULATE)
4768 #define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_POPULATE)
4769 #elif defined(MAP_PREFAULT_READ)
4770 #define ZC_MMAP_FLAGS (MAP_PRIVATE | MAP_PREFAULT_READ)
4772 #define ZC_MMAP_FLAGS (MAP_PRIVATE)
4775 /* private mmap() so we can strtok safely */
4776 fc
= (char *)mmap(NULL
, fs
.st_size
, PROT_READ
| PROT_WRITE
,
4777 ZC_MMAP_FLAGS
, featfd
, 0);
4778 (void) close(featfd
);
4780 /* map ok, and last character == newline? */
4781 if (fc
== MAP_FAILED
|| fc
[fs
.st_size
- 1] != '\n') {
4782 (void) munmap((void *) fc
, fs
.st_size
);
4783 strlcat(err_badfile
, file
, ZFS_MAXPROPLEN
);
4784 strlcat(err_badfile
, " ", ZFS_MAXPROPLEN
);
4785 ret_badfile
= B_TRUE
;
4789 ret_nofiles
= B_FALSE
;
4791 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4792 l_features
[i
] = B_FALSE
;
4794 /* replace final newline with NULL to ensure string ends */
4795 fc
[fs
.st_size
- 1] = '\0';
4797 for (line
= strtok_r(fc
, "\n", &ls
);
4799 line
= strtok_r(NULL
, "\n", &ls
)) {
4800 /* discard comments */
4801 char *r
= strchr(line
, '#');
4805 for (word
= strtok_r(line
, ", \t", &ws
);
4807 word
= strtok_r(NULL
, ", \t", &ws
)) {
4808 /* Find matching feature name */
4810 for (f
= 0; f
< SPA_FEATURES
; f
++) {
4811 zfeature_info_t
*fi
=
4812 &spa_feature_table
[f
];
4813 if (strcmp(word
, fi
->fi_uname
) == 0) {
4814 l_features
[f
] = B_TRUE
;
4818 if (f
< SPA_FEATURES
)
4821 /* found an unrecognized word */
4822 /* lightly sanitize it */
4823 if (strlen(word
) > 32)
4825 for (char *c
= word
; *c
!= '\0'; c
++)
4829 strlcat(err_badtoken
, word
, ZFS_MAXPROPLEN
);
4830 strlcat(err_badtoken
, " ", ZFS_MAXPROPLEN
);
4831 if (source
== Z_SYSCONF
)
4832 ret_badtoken
= B_TRUE
;
4834 ret_warntoken
= B_TRUE
;
4837 (void) munmap((void *) fc
, fs
.st_size
);
4839 if (features
!= NULL
)
4840 for (uint_t i
= 0; i
< SPA_FEATURES
; i
++)
4841 features
[i
] &= l_features
[i
];
4843 (void) close(sdirfd
);
4844 (void) close(ddirfd
);
4846 /* Return the most serious error */
4849 snprintf(report
, rlen
, gettext("could not read/"
4850 "parse feature file(s): %s"), err_badfile
);
4851 return (ZPOOL_COMPATIBILITY_BADFILE
);
4856 gettext("no valid compatibility files specified"),
4858 return (ZPOOL_COMPATIBILITY_NOFILES
);
4862 snprintf(report
, rlen
, gettext("invalid feature "
4863 "name(s) in local compatibility files: %s"),
4865 return (ZPOOL_COMPATIBILITY_BADTOKEN
);
4867 if (ret_warntoken
) {
4869 snprintf(report
, rlen
, gettext("unrecognized feature "
4870 "name(s) in distribution compatibility files: %s"),
4872 return (ZPOOL_COMPATIBILITY_WARNTOKEN
);
4875 strlcpy(report
, gettext("compatibility set ok"), rlen
);
4876 return (ZPOOL_COMPATIBILITY_OK
);
4880 zpool_vdev_guid(zpool_handle_t
*zhp
, const char *vdevname
, uint64_t *vdev_guid
)
4883 boolean_t avail_spare
, l2cache
;
4885 verify(zhp
!= NULL
);
4886 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
4887 char errbuf
[ERRBUFLEN
];
4888 (void) snprintf(errbuf
, sizeof (errbuf
),
4889 dgettext(TEXT_DOMAIN
, "pool is in an unavailable state"));
4890 return (zfs_error(zhp
->zpool_hdl
, EZFS_POOLUNAVAIL
, errbuf
));
4893 if ((tgt
= zpool_find_vdev(zhp
, vdevname
, &avail_spare
, &l2cache
,
4895 char errbuf
[ERRBUFLEN
];
4896 (void) snprintf(errbuf
, sizeof (errbuf
),
4897 dgettext(TEXT_DOMAIN
, "can not find %s in %s"),
4898 vdevname
, zhp
->zpool_name
);
4899 return (zfs_error(zhp
->zpool_hdl
, EZFS_NODEVICE
, errbuf
));
4902 *vdev_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
4907 * Get a vdev property value for 'prop' and return the value in
4908 * a pre-allocated buffer.
4911 zpool_get_vdev_prop_value(nvlist_t
*nvprop
, vdev_prop_t prop
, char *prop_name
,
4912 char *buf
, size_t len
, zprop_source_t
*srctype
, boolean_t literal
)
4917 zprop_source_t src
= ZPROP_SRC_NONE
;
4919 if (prop
== VDEV_PROP_USERPROP
) {
4920 /* user property, prop_name must contain the property name */
4921 assert(prop_name
!= NULL
);
4922 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
4923 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
4924 strval
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
4926 /* user prop not found */
4929 (void) strlcpy(buf
, strval
, len
);
4935 if (prop_name
== NULL
)
4936 prop_name
= (char *)vdev_prop_to_name(prop
);
4938 switch (vdev_prop_get_type(prop
)) {
4939 case PROP_TYPE_STRING
:
4940 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
4941 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
4942 strval
= fnvlist_lookup_string(nv
, ZPROP_VALUE
);
4944 src
= ZPROP_SRC_DEFAULT
;
4945 if ((strval
= vdev_prop_default_string(prop
)) == NULL
)
4948 (void) strlcpy(buf
, strval
, len
);
4951 case PROP_TYPE_NUMBER
:
4952 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
4953 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
4954 intval
= fnvlist_lookup_uint64(nv
, ZPROP_VALUE
);
4956 src
= ZPROP_SRC_DEFAULT
;
4957 intval
= vdev_prop_default_numeric(prop
);
4961 case VDEV_PROP_ASIZE
:
4962 case VDEV_PROP_PSIZE
:
4963 case VDEV_PROP_SIZE
:
4964 case VDEV_PROP_BOOTSIZE
:
4965 case VDEV_PROP_ALLOCATED
:
4966 case VDEV_PROP_FREE
:
4967 case VDEV_PROP_READ_ERRORS
:
4968 case VDEV_PROP_WRITE_ERRORS
:
4969 case VDEV_PROP_CHECKSUM_ERRORS
:
4970 case VDEV_PROP_INITIALIZE_ERRORS
:
4971 case VDEV_PROP_OPS_NULL
:
4972 case VDEV_PROP_OPS_READ
:
4973 case VDEV_PROP_OPS_WRITE
:
4974 case VDEV_PROP_OPS_FREE
:
4975 case VDEV_PROP_OPS_CLAIM
:
4976 case VDEV_PROP_OPS_TRIM
:
4977 case VDEV_PROP_BYTES_NULL
:
4978 case VDEV_PROP_BYTES_READ
:
4979 case VDEV_PROP_BYTES_WRITE
:
4980 case VDEV_PROP_BYTES_FREE
:
4981 case VDEV_PROP_BYTES_CLAIM
:
4982 case VDEV_PROP_BYTES_TRIM
:
4984 (void) snprintf(buf
, len
, "%llu",
4985 (u_longlong_t
)intval
);
4987 (void) zfs_nicenum(intval
, buf
, len
);
4990 case VDEV_PROP_EXPANDSZ
:
4992 (void) strlcpy(buf
, "-", len
);
4993 } else if (literal
) {
4994 (void) snprintf(buf
, len
, "%llu",
4995 (u_longlong_t
)intval
);
4997 (void) zfs_nicenum(intval
, buf
, len
);
5000 case VDEV_PROP_CAPACITY
:
5002 (void) snprintf(buf
, len
, "%llu",
5003 (u_longlong_t
)intval
);
5005 (void) snprintf(buf
, len
, "%llu%%",
5006 (u_longlong_t
)intval
);
5009 case VDEV_PROP_CHECKSUM_N
:
5010 case VDEV_PROP_CHECKSUM_T
:
5011 case VDEV_PROP_IO_N
:
5012 case VDEV_PROP_IO_T
:
5013 if (intval
== UINT64_MAX
) {
5014 (void) strlcpy(buf
, "-", len
);
5016 (void) snprintf(buf
, len
, "%llu",
5017 (u_longlong_t
)intval
);
5020 case VDEV_PROP_FRAGMENTATION
:
5021 if (intval
== UINT64_MAX
) {
5022 (void) strlcpy(buf
, "-", len
);
5024 (void) snprintf(buf
, len
, "%llu%%",
5025 (u_longlong_t
)intval
);
5028 case VDEV_PROP_STATE
:
5030 (void) snprintf(buf
, len
, "%llu",
5031 (u_longlong_t
)intval
);
5033 (void) strlcpy(buf
, zpool_state_to_name(intval
,
5034 VDEV_AUX_NONE
), len
);
5038 (void) snprintf(buf
, len
, "%llu",
5039 (u_longlong_t
)intval
);
5043 case PROP_TYPE_INDEX
:
5044 if (nvlist_lookup_nvlist(nvprop
, prop_name
, &nv
) == 0) {
5045 src
= fnvlist_lookup_uint64(nv
, ZPROP_SOURCE
);
5046 intval
= fnvlist_lookup_uint64(nv
, ZPROP_VALUE
);
5048 src
= ZPROP_SRC_DEFAULT
;
5049 intval
= vdev_prop_default_numeric(prop
);
5051 if (vdev_prop_index_to_string(prop
, intval
,
5052 (const char **)&strval
) != 0)
5054 (void) strlcpy(buf
, strval
, len
);
5068 * Get a vdev property value for 'prop_name' and return the value in
5069 * a pre-allocated buffer.
5072 zpool_get_vdev_prop(zpool_handle_t
*zhp
, const char *vdevname
, vdev_prop_t prop
,
5073 char *prop_name
, char *buf
, size_t len
, zprop_source_t
*srctype
,
5076 nvlist_t
*reqnvl
, *reqprops
;
5077 nvlist_t
*retprops
= NULL
;
5078 uint64_t vdev_guid
= 0;
5081 if ((ret
= zpool_vdev_guid(zhp
, vdevname
, &vdev_guid
)) != 0)
5084 if (nvlist_alloc(&reqnvl
, NV_UNIQUE_NAME
, 0) != 0)
5085 return (no_memory(zhp
->zpool_hdl
));
5086 if (nvlist_alloc(&reqprops
, NV_UNIQUE_NAME
, 0) != 0)
5087 return (no_memory(zhp
->zpool_hdl
));
5089 fnvlist_add_uint64(reqnvl
, ZPOOL_VDEV_PROPS_GET_VDEV
, vdev_guid
);
5091 if (prop
!= VDEV_PROP_USERPROP
) {
5092 /* prop_name overrides prop value */
5093 if (prop_name
!= NULL
)
5094 prop
= vdev_name_to_prop(prop_name
);
5096 prop_name
= (char *)vdev_prop_to_name(prop
);
5097 assert(prop
< VDEV_NUM_PROPS
);
5100 assert(prop_name
!= NULL
);
5101 if (nvlist_add_uint64(reqprops
, prop_name
, prop
) != 0) {
5102 nvlist_free(reqnvl
);
5103 nvlist_free(reqprops
);
5104 return (no_memory(zhp
->zpool_hdl
));
5107 fnvlist_add_nvlist(reqnvl
, ZPOOL_VDEV_PROPS_GET_PROPS
, reqprops
);
5109 ret
= lzc_get_vdev_prop(zhp
->zpool_name
, reqnvl
, &retprops
);
5112 ret
= zpool_get_vdev_prop_value(retprops
, prop
, prop_name
, buf
,
5113 len
, srctype
, literal
);
5115 char errbuf
[ERRBUFLEN
];
5116 (void) snprintf(errbuf
, sizeof (errbuf
),
5117 dgettext(TEXT_DOMAIN
, "cannot get vdev property %s from"
5118 " %s in %s"), prop_name
, vdevname
, zhp
->zpool_name
);
5119 (void) zpool_standard_error(zhp
->zpool_hdl
, ret
, errbuf
);
5122 nvlist_free(reqnvl
);
5123 nvlist_free(reqprops
);
5124 nvlist_free(retprops
);
5130 * Get all vdev properties
5133 zpool_get_all_vdev_props(zpool_handle_t
*zhp
, const char *vdevname
,
5136 nvlist_t
*nvl
= NULL
;
5137 uint64_t vdev_guid
= 0;
5140 if ((ret
= zpool_vdev_guid(zhp
, vdevname
, &vdev_guid
)) != 0)
5143 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5144 return (no_memory(zhp
->zpool_hdl
));
5146 fnvlist_add_uint64(nvl
, ZPOOL_VDEV_PROPS_GET_VDEV
, vdev_guid
);
5148 ret
= lzc_get_vdev_prop(zhp
->zpool_name
, nvl
, outnvl
);
5153 char errbuf
[ERRBUFLEN
];
5154 (void) snprintf(errbuf
, sizeof (errbuf
),
5155 dgettext(TEXT_DOMAIN
, "cannot get vdev properties for"
5156 " %s in %s"), vdevname
, zhp
->zpool_name
);
5157 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
5167 zpool_set_vdev_prop(zpool_handle_t
*zhp
, const char *vdevname
,
5168 const char *propname
, const char *propval
)
5171 nvlist_t
*nvl
= NULL
;
5172 nvlist_t
*outnvl
= NULL
;
5174 nvlist_t
*realprops
;
5175 prop_flags_t flags
= { 0 };
5179 if ((ret
= zpool_vdev_guid(zhp
, vdevname
, &vdev_guid
)) != 0)
5182 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
5183 return (no_memory(zhp
->zpool_hdl
));
5184 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0)
5185 return (no_memory(zhp
->zpool_hdl
));
5187 fnvlist_add_uint64(nvl
, ZPOOL_VDEV_PROPS_SET_VDEV
, vdev_guid
);
5189 if (nvlist_add_string(props
, propname
, propval
) != 0) {
5191 return (no_memory(zhp
->zpool_hdl
));
5194 char errbuf
[ERRBUFLEN
];
5195 (void) snprintf(errbuf
, sizeof (errbuf
),
5196 dgettext(TEXT_DOMAIN
, "cannot set property %s for %s on %s"),
5197 propname
, vdevname
, zhp
->zpool_name
);
5200 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
5201 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
5202 zhp
->zpool_name
, props
, version
, flags
, errbuf
)) == NULL
) {
5211 fnvlist_add_nvlist(nvl
, ZPOOL_VDEV_PROPS_SET_PROPS
, props
);
5213 ret
= lzc_set_vdev_prop(zhp
->zpool_name
, nvl
, &outnvl
);
5217 nvlist_free(outnvl
);
5220 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);