4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27 * Copyright (c) 2018 Datto Inc.
28 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
29 * Copyright (c) 2017, Intel Corporation.
30 * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>
42 #include <sys/efi_partition.h>
43 #include <sys/systeminfo.h>
44 #include <sys/zfs_ioctl.h>
45 #include <sys/zfs_sysfs.h>
46 #include <sys/vdev_disk.h>
49 #include "zfs_namecheck.h"
51 #include "libzfs_impl.h"
52 #include "zfs_comutil.h"
53 #include "zfeature_common.h"
55 static boolean_t
zpool_vdev_is_interior(const char *name
);
57 typedef struct prop_flags
{
58 int create
:1; /* Validate property on creation */
59 int import
:1; /* Validate property on import */
63 * ====================================================================
64 * zpool property functions
65 * ====================================================================
69 zpool_get_all_props(zpool_handle_t
*zhp
)
71 zfs_cmd_t zc
= {"\0"};
72 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
74 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
76 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
79 while (zfs_ioctl(hdl
, ZFS_IOC_POOL_GET_PROPS
, &zc
) != 0) {
80 if (errno
== ENOMEM
) {
81 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
82 zcmd_free_nvlists(&zc
);
86 zcmd_free_nvlists(&zc
);
91 if (zcmd_read_dst_nvlist(hdl
, &zc
, &zhp
->zpool_props
) != 0) {
92 zcmd_free_nvlists(&zc
);
96 zcmd_free_nvlists(&zc
);
102 zpool_props_refresh(zpool_handle_t
*zhp
)
106 old_props
= zhp
->zpool_props
;
108 if (zpool_get_all_props(zhp
) != 0)
111 nvlist_free(old_props
);
116 zpool_get_prop_string(zpool_handle_t
*zhp
, zpool_prop_t prop
,
122 zprop_source_t source
;
124 nvl
= zhp
->zpool_props
;
125 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
126 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &ival
) == 0);
128 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
130 source
= ZPROP_SRC_DEFAULT
;
131 if ((value
= (char *)zpool_prop_default_string(prop
)) == NULL
)
142 zpool_get_prop_int(zpool_handle_t
*zhp
, zpool_prop_t prop
, zprop_source_t
*src
)
146 zprop_source_t source
;
148 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
)) {
150 * zpool_get_all_props() has most likely failed because
151 * the pool is faulted, but if all we need is the top level
152 * vdev's guid then get it from the zhp config nvlist.
154 if ((prop
== ZPOOL_PROP_GUID
) &&
155 (nvlist_lookup_nvlist(zhp
->zpool_config
,
156 ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0) &&
157 (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
)
161 return (zpool_prop_default_numeric(prop
));
164 nvl
= zhp
->zpool_props
;
165 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
166 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &value
) == 0);
168 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
170 source
= ZPROP_SRC_DEFAULT
;
171 value
= zpool_prop_default_numeric(prop
);
181 * Map VDEV STATE to printed strings.
184 zpool_state_to_name(vdev_state_t state
, vdev_aux_t aux
)
187 case VDEV_STATE_CLOSED
:
188 case VDEV_STATE_OFFLINE
:
189 return (gettext("OFFLINE"));
190 case VDEV_STATE_REMOVED
:
191 return (gettext("REMOVED"));
192 case VDEV_STATE_CANT_OPEN
:
193 if (aux
== VDEV_AUX_CORRUPT_DATA
|| aux
== VDEV_AUX_BAD_LOG
)
194 return (gettext("FAULTED"));
195 else if (aux
== VDEV_AUX_SPLIT_POOL
)
196 return (gettext("SPLIT"));
198 return (gettext("UNAVAIL"));
199 case VDEV_STATE_FAULTED
:
200 return (gettext("FAULTED"));
201 case VDEV_STATE_DEGRADED
:
202 return (gettext("DEGRADED"));
203 case VDEV_STATE_HEALTHY
:
204 return (gettext("ONLINE"));
210 return (gettext("UNKNOWN"));
214 * Map POOL STATE to printed strings.
217 zpool_pool_state_to_name(pool_state_t state
)
222 case POOL_STATE_ACTIVE
:
223 return (gettext("ACTIVE"));
224 case POOL_STATE_EXPORTED
:
225 return (gettext("EXPORTED"));
226 case POOL_STATE_DESTROYED
:
227 return (gettext("DESTROYED"));
228 case POOL_STATE_SPARE
:
229 return (gettext("SPARE"));
230 case POOL_STATE_L2CACHE
:
231 return (gettext("L2CACHE"));
232 case POOL_STATE_UNINITIALIZED
:
233 return (gettext("UNINITIALIZED"));
234 case POOL_STATE_UNAVAIL
:
235 return (gettext("UNAVAIL"));
236 case POOL_STATE_POTENTIALLY_ACTIVE
:
237 return (gettext("POTENTIALLY_ACTIVE"));
240 return (gettext("UNKNOWN"));
244 * Given a pool handle, return the pool health string ("ONLINE", "DEGRADED",
248 zpool_get_state_str(zpool_handle_t
*zhp
)
250 zpool_errata_t errata
;
251 zpool_status_t status
;
257 status
= zpool_get_status(zhp
, NULL
, &errata
);
259 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
260 str
= gettext("FAULTED");
261 } else if (status
== ZPOOL_STATUS_IO_FAILURE_WAIT
||
262 status
== ZPOOL_STATUS_IO_FAILURE_MMP
) {
263 str
= gettext("SUSPENDED");
265 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
266 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
267 verify(nvlist_lookup_uint64_array(nvroot
,
268 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
270 str
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
276 * Get a zpool property value for 'prop' and return the value in
277 * a pre-allocated buffer.
280 zpool_get_prop(zpool_handle_t
*zhp
, zpool_prop_t prop
, char *buf
,
281 size_t len
, zprop_source_t
*srctype
, boolean_t literal
)
285 zprop_source_t src
= ZPROP_SRC_NONE
;
287 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
289 case ZPOOL_PROP_NAME
:
290 (void) strlcpy(buf
, zpool_get_name(zhp
), len
);
293 case ZPOOL_PROP_HEALTH
:
294 (void) strlcpy(buf
, zpool_get_state_str(zhp
), len
);
297 case ZPOOL_PROP_GUID
:
298 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
299 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
302 case ZPOOL_PROP_ALTROOT
:
303 case ZPOOL_PROP_CACHEFILE
:
304 case ZPOOL_PROP_COMMENT
:
305 if (zhp
->zpool_props
!= NULL
||
306 zpool_get_all_props(zhp
) == 0) {
308 zpool_get_prop_string(zhp
, prop
, &src
),
314 (void) strlcpy(buf
, "-", len
);
323 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
) &&
324 prop
!= ZPOOL_PROP_NAME
)
327 switch (zpool_prop_get_type(prop
)) {
328 case PROP_TYPE_STRING
:
329 (void) strlcpy(buf
, zpool_get_prop_string(zhp
, prop
, &src
),
333 case PROP_TYPE_NUMBER
:
334 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
337 case ZPOOL_PROP_SIZE
:
338 case ZPOOL_PROP_ALLOCATED
:
339 case ZPOOL_PROP_FREE
:
340 case ZPOOL_PROP_FREEING
:
341 case ZPOOL_PROP_LEAKED
:
342 case ZPOOL_PROP_ASHIFT
:
344 (void) snprintf(buf
, len
, "%llu",
345 (u_longlong_t
)intval
);
347 (void) zfs_nicenum(intval
, buf
, len
);
350 case ZPOOL_PROP_EXPANDSZ
:
351 case ZPOOL_PROP_CHECKPOINT
:
353 (void) strlcpy(buf
, "-", len
);
354 } else if (literal
) {
355 (void) snprintf(buf
, len
, "%llu",
356 (u_longlong_t
)intval
);
358 (void) zfs_nicebytes(intval
, buf
, len
);
362 case ZPOOL_PROP_CAPACITY
:
364 (void) snprintf(buf
, len
, "%llu",
365 (u_longlong_t
)intval
);
367 (void) snprintf(buf
, len
, "%llu%%",
368 (u_longlong_t
)intval
);
372 case ZPOOL_PROP_FRAGMENTATION
:
373 if (intval
== UINT64_MAX
) {
374 (void) strlcpy(buf
, "-", len
);
375 } else if (literal
) {
376 (void) snprintf(buf
, len
, "%llu",
377 (u_longlong_t
)intval
);
379 (void) snprintf(buf
, len
, "%llu%%",
380 (u_longlong_t
)intval
);
384 case ZPOOL_PROP_DEDUPRATIO
:
386 (void) snprintf(buf
, len
, "%llu.%02llu",
387 (u_longlong_t
)(intval
/ 100),
388 (u_longlong_t
)(intval
% 100));
390 (void) snprintf(buf
, len
, "%llu.%02llux",
391 (u_longlong_t
)(intval
/ 100),
392 (u_longlong_t
)(intval
% 100));
395 case ZPOOL_PROP_HEALTH
:
396 (void) strlcpy(buf
, zpool_get_state_str(zhp
), len
);
398 case ZPOOL_PROP_VERSION
:
399 if (intval
>= SPA_VERSION_FEATURES
) {
400 (void) snprintf(buf
, len
, "-");
405 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
409 case PROP_TYPE_INDEX
:
410 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
411 if (zpool_prop_index_to_string(prop
, intval
, &strval
)
414 (void) strlcpy(buf
, strval
, len
);
428 * Check if the bootfs name has the same pool name as it is set to.
429 * Assuming bootfs is a valid dataset name.
432 bootfs_name_valid(const char *pool
, const char *bootfs
)
434 int len
= strlen(pool
);
435 if (bootfs
[0] == '\0')
438 if (!zfs_name_valid(bootfs
, ZFS_TYPE_FILESYSTEM
|ZFS_TYPE_SNAPSHOT
))
441 if (strncmp(pool
, bootfs
, len
) == 0 &&
442 (bootfs
[len
] == '/' || bootfs
[len
] == '\0'))
449 * Given an nvlist of zpool properties to be set, validate that they are
450 * correct, and parse any numeric properties (index, boolean, etc) if they are
451 * specified as strings.
454 zpool_valid_proplist(libzfs_handle_t
*hdl
, const char *poolname
,
455 nvlist_t
*props
, uint64_t version
, prop_flags_t flags
, char *errbuf
)
463 struct stat64 statbuf
;
466 if (nvlist_alloc(&retprops
, NV_UNIQUE_NAME
, 0) != 0) {
467 (void) no_memory(hdl
);
472 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
473 const char *propname
= nvpair_name(elem
);
475 prop
= zpool_name_to_prop(propname
);
476 if (prop
== ZPOOL_PROP_INVAL
&& zpool_prop_feature(propname
)) {
478 char *fname
= strchr(propname
, '@') + 1;
480 err
= zfeature_lookup_name(fname
, NULL
);
482 ASSERT3U(err
, ==, ENOENT
);
483 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
484 "feature '%s' unsupported by kernel"),
486 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
490 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
491 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
492 "'%s' must be a string"), propname
);
493 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
497 (void) nvpair_value_string(elem
, &strval
);
498 if (strcmp(strval
, ZFS_FEATURE_ENABLED
) != 0 &&
499 strcmp(strval
, ZFS_FEATURE_DISABLED
) != 0) {
500 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
501 "property '%s' can only be set to "
502 "'enabled' or 'disabled'"), propname
);
503 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
508 strcmp(strval
, ZFS_FEATURE_DISABLED
) == 0) {
509 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
510 "property '%s' can only be set to "
511 "'disabled' at creation time"), propname
);
512 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
516 if (nvlist_add_uint64(retprops
, propname
, 0) != 0) {
517 (void) no_memory(hdl
);
524 * Make sure this property is valid and applies to this type.
526 if (prop
== ZPOOL_PROP_INVAL
) {
527 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
528 "invalid property '%s'"), propname
);
529 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
533 if (zpool_prop_readonly(prop
)) {
534 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
535 "is readonly"), propname
);
536 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
540 if (!flags
.create
&& zpool_prop_setonce(prop
)) {
541 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
542 "property '%s' can only be set at "
543 "creation time"), propname
);
544 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
548 if (zprop_parse_value(hdl
, elem
, prop
, ZFS_TYPE_POOL
, retprops
,
549 &strval
, &intval
, errbuf
) != 0)
553 * Perform additional checking for specific properties.
556 case ZPOOL_PROP_VERSION
:
557 if (intval
< version
||
558 !SPA_VERSION_IS_SUPPORTED(intval
)) {
559 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
560 "property '%s' number %d is invalid."),
562 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
567 case ZPOOL_PROP_ASHIFT
:
569 (intval
< ASHIFT_MIN
|| intval
> ASHIFT_MAX
)) {
570 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
571 "property '%s' number %d is invalid, only "
572 "values between %" PRId32
" and "
573 "%" PRId32
" are allowed."),
574 propname
, intval
, ASHIFT_MIN
, ASHIFT_MAX
);
575 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
580 case ZPOOL_PROP_BOOTFS
:
581 if (flags
.create
|| flags
.import
) {
582 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
583 "property '%s' cannot be set at creation "
584 "or import time"), propname
);
585 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
589 if (version
< SPA_VERSION_BOOTFS
) {
590 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
591 "pool must be upgraded to support "
592 "'%s' property"), propname
);
593 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
598 * bootfs property value has to be a dataset name and
599 * the dataset has to be in the same pool as it sets to.
601 if (!bootfs_name_valid(poolname
, strval
)) {
602 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
603 "is an invalid name"), strval
);
604 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
608 if ((zhp
= zpool_open_canfail(hdl
, poolname
)) == NULL
) {
609 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
610 "could not open pool '%s'"), poolname
);
611 (void) zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
);
617 case ZPOOL_PROP_ALTROOT
:
618 if (!flags
.create
&& !flags
.import
) {
619 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
620 "property '%s' can only be set during pool "
621 "creation or import"), propname
);
622 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
626 if (strval
[0] != '/') {
627 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
628 "bad alternate root '%s'"), strval
);
629 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
634 case ZPOOL_PROP_CACHEFILE
:
635 if (strval
[0] == '\0')
638 if (strcmp(strval
, "none") == 0)
641 if (strval
[0] != '/') {
642 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
643 "property '%s' must be empty, an "
644 "absolute path, or 'none'"), propname
);
645 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
649 slash
= strrchr(strval
, '/');
651 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
652 strcmp(slash
, "/..") == 0) {
653 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
654 "'%s' is not a valid file"), strval
);
655 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
661 if (strval
[0] != '\0' &&
662 (stat64(strval
, &statbuf
) != 0 ||
663 !S_ISDIR(statbuf
.st_mode
))) {
664 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
665 "'%s' is not a valid directory"),
667 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
674 case ZPOOL_PROP_COMMENT
:
675 for (check
= strval
; *check
!= '\0'; check
++) {
676 if (!isprint(*check
)) {
678 dgettext(TEXT_DOMAIN
,
679 "comment may only have printable "
681 (void) zfs_error(hdl
, EZFS_BADPROP
,
686 if (strlen(strval
) > ZPROP_MAX_COMMENT
) {
687 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
688 "comment must not exceed %d characters"),
690 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
694 case ZPOOL_PROP_READONLY
:
696 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
697 "property '%s' can only be set at "
698 "import time"), propname
);
699 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
703 case ZPOOL_PROP_MULTIHOST
:
704 if (get_system_hostid() == 0) {
705 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
706 "requires a non-zero system hostid"));
707 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
711 case ZPOOL_PROP_DEDUPDITTO
:
712 printf("Note: property '%s' no longer has "
713 "any effect\n", propname
);
723 nvlist_free(retprops
);
728 * Set zpool property : propname=propval.
731 zpool_set_prop(zpool_handle_t
*zhp
, const char *propname
, const char *propval
)
733 zfs_cmd_t zc
= {"\0"};
736 nvlist_t
*nvl
= NULL
;
739 prop_flags_t flags
= { 0 };
741 (void) snprintf(errbuf
, sizeof (errbuf
),
742 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
745 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
746 return (no_memory(zhp
->zpool_hdl
));
748 if (nvlist_add_string(nvl
, propname
, propval
) != 0) {
750 return (no_memory(zhp
->zpool_hdl
));
753 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
754 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
755 zhp
->zpool_name
, nvl
, version
, flags
, errbuf
)) == NULL
) {
764 * Execute the corresponding ioctl() to set this property.
766 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
768 if (zcmd_write_src_nvlist(zhp
->zpool_hdl
, &zc
, nvl
) != 0) {
773 ret
= zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_SET_PROPS
, &zc
);
775 zcmd_free_nvlists(&zc
);
779 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
781 (void) zpool_props_refresh(zhp
);
787 zpool_expand_proplist(zpool_handle_t
*zhp
, zprop_list_t
**plp
)
789 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
791 char buf
[ZFS_MAXPROPLEN
];
792 nvlist_t
*features
= NULL
;
795 boolean_t firstexpand
= (NULL
== *plp
);
798 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_POOL
) != 0)
802 while (*last
!= NULL
)
803 last
= &(*last
)->pl_next
;
806 features
= zpool_get_features(zhp
);
808 if ((*plp
)->pl_all
&& firstexpand
) {
809 for (i
= 0; i
< SPA_FEATURES
; i
++) {
810 zprop_list_t
*entry
= zfs_alloc(hdl
,
811 sizeof (zprop_list_t
));
812 entry
->pl_prop
= ZPROP_INVAL
;
813 entry
->pl_user_prop
= zfs_asprintf(hdl
, "feature@%s",
814 spa_feature_table
[i
].fi_uname
);
815 entry
->pl_width
= strlen(entry
->pl_user_prop
);
816 entry
->pl_all
= B_TRUE
;
819 last
= &entry
->pl_next
;
823 /* add any unsupported features */
824 for (nvp
= nvlist_next_nvpair(features
, NULL
);
825 nvp
!= NULL
; nvp
= nvlist_next_nvpair(features
, nvp
)) {
830 if (zfeature_is_supported(nvpair_name(nvp
)))
833 propname
= zfs_asprintf(hdl
, "unsupported@%s",
837 * Before adding the property to the list make sure that no
838 * other pool already added the same property.
842 while (entry
!= NULL
) {
843 if (entry
->pl_user_prop
!= NULL
&&
844 strcmp(propname
, entry
->pl_user_prop
) == 0) {
848 entry
= entry
->pl_next
;
855 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
856 entry
->pl_prop
= ZPROP_INVAL
;
857 entry
->pl_user_prop
= propname
;
858 entry
->pl_width
= strlen(entry
->pl_user_prop
);
859 entry
->pl_all
= B_TRUE
;
862 last
= &entry
->pl_next
;
865 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
870 if (entry
->pl_prop
!= ZPROP_INVAL
&&
871 zpool_get_prop(zhp
, entry
->pl_prop
, buf
, sizeof (buf
),
872 NULL
, B_FALSE
) == 0) {
873 if (strlen(buf
) > entry
->pl_width
)
874 entry
->pl_width
= strlen(buf
);
882 * Get the state for the given feature on the given ZFS pool.
885 zpool_prop_get_feature(zpool_handle_t
*zhp
, const char *propname
, char *buf
,
889 boolean_t found
= B_FALSE
;
890 nvlist_t
*features
= zpool_get_features(zhp
);
892 const char *feature
= strchr(propname
, '@') + 1;
894 supported
= zpool_prop_feature(propname
);
895 ASSERT(supported
|| zpool_prop_unsupported(propname
));
898 * Convert from feature name to feature guid. This conversion is
899 * unnecessary for unsupported@... properties because they already
906 ret
= zfeature_lookup_name(feature
, &fid
);
908 (void) strlcpy(buf
, "-", len
);
911 feature
= spa_feature_table
[fid
].fi_guid
;
914 if (nvlist_lookup_uint64(features
, feature
, &refcount
) == 0)
919 (void) strlcpy(buf
, ZFS_FEATURE_DISABLED
, len
);
922 (void) strlcpy(buf
, ZFS_FEATURE_ENABLED
, len
);
924 (void) strlcpy(buf
, ZFS_FEATURE_ACTIVE
, len
);
929 (void) strcpy(buf
, ZFS_UNSUPPORTED_INACTIVE
);
931 (void) strcpy(buf
, ZFS_UNSUPPORTED_READONLY
);
934 (void) strlcpy(buf
, "-", len
);
943 * Validate the given pool name, optionally putting an extended error message in
947 zpool_name_valid(libzfs_handle_t
*hdl
, boolean_t isopen
, const char *pool
)
953 ret
= pool_namecheck(pool
, &why
, &what
);
956 * The rules for reserved pool names were extended at a later point.
957 * But we need to support users with existing pools that may now be
958 * invalid. So we only check for this expanded set of names during a
959 * create (or import), and only in userland.
961 if (ret
== 0 && !isopen
&&
962 (strncmp(pool
, "mirror", 6) == 0 ||
963 strncmp(pool
, "raidz", 5) == 0 ||
964 strncmp(pool
, "draid", 5) == 0 ||
965 strncmp(pool
, "spare", 5) == 0 ||
966 strcmp(pool
, "log") == 0)) {
969 dgettext(TEXT_DOMAIN
, "name is reserved"));
977 case NAME_ERR_TOOLONG
:
979 dgettext(TEXT_DOMAIN
, "name is too long"));
982 case NAME_ERR_INVALCHAR
:
984 dgettext(TEXT_DOMAIN
, "invalid character "
985 "'%c' in pool name"), what
);
988 case NAME_ERR_NOLETTER
:
989 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
990 "name must begin with a letter"));
993 case NAME_ERR_RESERVED
:
994 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
995 "name is reserved"));
998 case NAME_ERR_DISKLIKE
:
999 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1000 "pool name is reserved"));
1003 case NAME_ERR_LEADING_SLASH
:
1004 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1005 "leading slash in name"));
1008 case NAME_ERR_EMPTY_COMPONENT
:
1009 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1010 "empty component in name"));
1013 case NAME_ERR_TRAILING_SLASH
:
1014 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1015 "trailing slash in name"));
1018 case NAME_ERR_MULTIPLE_DELIMITERS
:
1019 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1020 "multiple '@' and/or '#' delimiters in "
1024 case NAME_ERR_NO_AT
:
1025 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1026 "permission set is missing '@'"));
1030 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1031 "(%d) not defined"), why
);
1042 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1046 zpool_open_canfail(libzfs_handle_t
*hdl
, const char *pool
)
1048 zpool_handle_t
*zhp
;
1052 * Make sure the pool name is valid.
1054 if (!zpool_name_valid(hdl
, B_TRUE
, pool
)) {
1055 (void) zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1056 dgettext(TEXT_DOMAIN
, "cannot open '%s'"),
1061 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
1064 zhp
->zpool_hdl
= hdl
;
1065 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1067 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1073 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "no such pool"));
1074 (void) zfs_error_fmt(hdl
, EZFS_NOENT
,
1075 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), pool
);
1084 * Like the above, but silent on error. Used when iterating over pools (because
1085 * the configuration cache may be out of date).
1088 zpool_open_silent(libzfs_handle_t
*hdl
, const char *pool
, zpool_handle_t
**ret
)
1090 zpool_handle_t
*zhp
;
1093 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
1096 zhp
->zpool_hdl
= hdl
;
1097 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1099 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1115 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1119 zpool_open(libzfs_handle_t
*hdl
, const char *pool
)
1121 zpool_handle_t
*zhp
;
1123 if ((zhp
= zpool_open_canfail(hdl
, pool
)) == NULL
)
1126 if (zhp
->zpool_state
== POOL_STATE_UNAVAIL
) {
1127 (void) zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
1128 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), zhp
->zpool_name
);
1137 * Close the handle. Simply frees the memory associated with the handle.
1140 zpool_close(zpool_handle_t
*zhp
)
1142 nvlist_free(zhp
->zpool_config
);
1143 nvlist_free(zhp
->zpool_old_config
);
1144 nvlist_free(zhp
->zpool_props
);
1149 * Return the name of the pool.
1152 zpool_get_name(zpool_handle_t
*zhp
)
1154 return (zhp
->zpool_name
);
1159 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1162 zpool_get_state(zpool_handle_t
*zhp
)
1164 return (zhp
->zpool_state
);
1168 * Check if vdev list contains a special vdev
1171 zpool_has_special_vdev(nvlist_t
*nvroot
)
1176 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
, &child
,
1178 for (uint_t c
= 0; c
< children
; c
++) {
1181 if (nvlist_lookup_string(child
[c
],
1182 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
) == 0 &&
1183 strcmp(bias
, VDEV_ALLOC_BIAS_SPECIAL
) == 0) {
1192 * Output a dRAID top-level vdev name in to the provided buffer.
1195 zpool_draid_name(char *name
, int len
, uint64_t data
, uint64_t parity
,
1196 uint64_t spares
, uint64_t children
)
1198 snprintf(name
, len
, "%s%llu:%llud:%lluc:%llus",
1199 VDEV_TYPE_DRAID
, (u_longlong_t
)parity
, (u_longlong_t
)data
,
1200 (u_longlong_t
)children
, (u_longlong_t
)spares
);
1206 * Return B_TRUE if the provided name is a dRAID spare name.
1209 zpool_is_draid_spare(const char *name
)
1211 uint64_t spare_id
, parity
, vdev_id
;
1213 if (sscanf(name
, VDEV_TYPE_DRAID
"%llu-%llu-%llu",
1214 (u_longlong_t
*)&parity
, (u_longlong_t
*)&vdev_id
,
1215 (u_longlong_t
*)&spare_id
) == 3) {
1223 * Create the named pool, using the provided vdev list. It is assumed
1224 * that the consumer has already validated the contents of the nvlist, so we
1225 * don't have to worry about error semantics.
1228 zpool_create(libzfs_handle_t
*hdl
, const char *pool
, nvlist_t
*nvroot
,
1229 nvlist_t
*props
, nvlist_t
*fsprops
)
1231 zfs_cmd_t zc
= {"\0"};
1232 nvlist_t
*zc_fsprops
= NULL
;
1233 nvlist_t
*zc_props
= NULL
;
1234 nvlist_t
*hidden_args
= NULL
;
1235 uint8_t *wkeydata
= NULL
;
1240 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1241 "cannot create '%s'"), pool
);
1243 if (!zpool_name_valid(hdl
, B_FALSE
, pool
))
1244 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
1246 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1250 prop_flags_t flags
= { .create
= B_TRUE
, .import
= B_FALSE
};
1252 if ((zc_props
= zpool_valid_proplist(hdl
, pool
, props
,
1253 SPA_VERSION_1
, flags
, msg
)) == NULL
) {
1262 zoned
= ((nvlist_lookup_string(fsprops
,
1263 zfs_prop_to_name(ZFS_PROP_ZONED
), &zonestr
) == 0) &&
1264 strcmp(zonestr
, "on") == 0);
1266 if ((zc_fsprops
= zfs_valid_proplist(hdl
, ZFS_TYPE_FILESYSTEM
,
1267 fsprops
, zoned
, NULL
, NULL
, B_TRUE
, msg
)) == NULL
) {
1271 if (nvlist_exists(zc_fsprops
,
1272 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS
)) &&
1273 !zpool_has_special_vdev(nvroot
)) {
1274 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1275 "%s property requires a special vdev"),
1276 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS
));
1277 (void) zfs_error(hdl
, EZFS_BADPROP
, msg
);
1282 (nvlist_alloc(&zc_props
, NV_UNIQUE_NAME
, 0) != 0)) {
1285 if (zfs_crypto_create(hdl
, NULL
, zc_fsprops
, props
, B_TRUE
,
1286 &wkeydata
, &wkeylen
) != 0) {
1287 zfs_error(hdl
, EZFS_CRYPTOFAILED
, msg
);
1290 if (nvlist_add_nvlist(zc_props
,
1291 ZPOOL_ROOTFS_PROPS
, zc_fsprops
) != 0) {
1294 if (wkeydata
!= NULL
) {
1295 if (nvlist_alloc(&hidden_args
, NV_UNIQUE_NAME
, 0) != 0)
1298 if (nvlist_add_uint8_array(hidden_args
, "wkeydata",
1299 wkeydata
, wkeylen
) != 0)
1302 if (nvlist_add_nvlist(zc_props
, ZPOOL_HIDDEN_ARGS
,
1308 if (zc_props
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
1311 (void) strlcpy(zc
.zc_name
, pool
, sizeof (zc
.zc_name
));
1313 if ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_CREATE
, &zc
)) != 0) {
1315 zcmd_free_nvlists(&zc
);
1316 nvlist_free(zc_props
);
1317 nvlist_free(zc_fsprops
);
1318 nvlist_free(hidden_args
);
1319 if (wkeydata
!= NULL
)
1325 * This can happen if the user has specified the same
1326 * device multiple times. We can't reliably detect this
1327 * until we try to add it and see we already have a
1328 * label. This can also happen under if the device is
1329 * part of an active md or lvm device.
1331 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1332 "one or more vdevs refer to the same device, or "
1333 "one of\nthe devices is part of an active md or "
1335 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1339 * This happens if the record size is smaller or larger
1340 * than the allowed size range, or not a power of 2.
1342 * NOTE: although zfs_valid_proplist is called earlier,
1343 * this case may have slipped through since the
1344 * pool does not exist yet and it is therefore
1345 * impossible to read properties e.g. max blocksize
1348 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1349 "record size invalid"));
1350 return (zfs_error(hdl
, EZFS_BADPROP
, msg
));
1354 * This occurs when one of the devices is below
1355 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1356 * device was the problem device since there's no
1357 * reliable way to determine device size from userland.
1362 zfs_nicebytes(SPA_MINDEVSIZE
, buf
,
1365 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1366 "one or more devices is less than the "
1367 "minimum size (%s)"), buf
);
1369 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1372 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1373 "one or more devices is out of space"));
1374 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1377 return (zpool_standard_error(hdl
, errno
, msg
));
1382 zcmd_free_nvlists(&zc
);
1383 nvlist_free(zc_props
);
1384 nvlist_free(zc_fsprops
);
1385 nvlist_free(hidden_args
);
1386 if (wkeydata
!= NULL
)
1392 * Destroy the given pool. It is up to the caller to ensure that there are no
1393 * datasets left in the pool.
1396 zpool_destroy(zpool_handle_t
*zhp
, const char *log_str
)
1398 zfs_cmd_t zc
= {"\0"};
1399 zfs_handle_t
*zfp
= NULL
;
1400 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1403 if (zhp
->zpool_state
== POOL_STATE_ACTIVE
&&
1404 (zfp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
1407 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1408 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1410 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_DESTROY
, &zc
) != 0) {
1411 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1412 "cannot destroy '%s'"), zhp
->zpool_name
);
1414 if (errno
== EROFS
) {
1415 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1416 "one or more devices is read only"));
1417 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1419 (void) zpool_standard_error(hdl
, errno
, msg
);
1428 remove_mountpoint(zfp
);
1436 * Create a checkpoint in the given pool.
1439 zpool_checkpoint(zpool_handle_t
*zhp
)
1441 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1445 error
= lzc_pool_checkpoint(zhp
->zpool_name
);
1447 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1448 "cannot checkpoint '%s'"), zhp
->zpool_name
);
1449 (void) zpool_standard_error(hdl
, error
, msg
);
1457 * Discard the checkpoint from the given pool.
1460 zpool_discard_checkpoint(zpool_handle_t
*zhp
)
1462 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1466 error
= lzc_pool_checkpoint_discard(zhp
->zpool_name
);
1468 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1469 "cannot discard checkpoint in '%s'"), zhp
->zpool_name
);
1470 (void) zpool_standard_error(hdl
, error
, msg
);
1478 * Add the given vdevs to the pool. The caller must have already performed the
1479 * necessary verification to ensure that the vdev specification is well-formed.
1482 zpool_add(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
1484 zfs_cmd_t zc
= {"\0"};
1486 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1488 nvlist_t
**spares
, **l2cache
;
1489 uint_t nspares
, nl2cache
;
1491 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1492 "cannot add to '%s'"), zhp
->zpool_name
);
1494 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1495 SPA_VERSION_SPARES
&&
1496 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1497 &spares
, &nspares
) == 0) {
1498 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1499 "upgraded to add hot spares"));
1500 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1503 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1504 SPA_VERSION_L2CACHE
&&
1505 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1506 &l2cache
, &nl2cache
) == 0) {
1507 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1508 "upgraded to add cache devices"));
1509 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1512 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1514 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1516 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_ADD
, &zc
) != 0) {
1520 * This can happen if the user has specified the same
1521 * device multiple times. We can't reliably detect this
1522 * until we try to add it and see we already have a
1525 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1526 "one or more vdevs refer to the same device"));
1527 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1531 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1532 "invalid config; a pool with removing/removed "
1533 "vdevs does not support adding raidz vdevs"));
1534 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1539 * This occurs when one of the devices is below
1540 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1541 * device was the problem device since there's no
1542 * reliable way to determine device size from userland.
1547 zfs_nicebytes(SPA_MINDEVSIZE
, buf
,
1550 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1551 "device is less than the minimum "
1554 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1558 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1559 "pool must be upgraded to add these vdevs"));
1560 (void) zfs_error(hdl
, EZFS_BADVERSION
, msg
);
1564 (void) zpool_standard_error(hdl
, errno
, msg
);
1572 zcmd_free_nvlists(&zc
);
1578 * Exports the pool from the system. The caller must ensure that there are no
1579 * mounted datasets in the pool.
1582 zpool_export_common(zpool_handle_t
*zhp
, boolean_t force
, boolean_t hardforce
,
1583 const char *log_str
)
1585 zfs_cmd_t zc
= {"\0"};
1588 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1589 "cannot export '%s'"), zhp
->zpool_name
);
1591 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1592 zc
.zc_cookie
= force
;
1593 zc
.zc_guid
= hardforce
;
1594 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1596 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_EXPORT
, &zc
) != 0) {
1599 zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
1600 "use '-f' to override the following errors:\n"
1601 "'%s' has an active shared spare which could be"
1602 " used by other pools once '%s' is exported."),
1603 zhp
->zpool_name
, zhp
->zpool_name
);
1604 return (zfs_error(zhp
->zpool_hdl
, EZFS_ACTIVE_SPARE
,
1607 return (zpool_standard_error_fmt(zhp
->zpool_hdl
, errno
,
1616 zpool_export(zpool_handle_t
*zhp
, boolean_t force
, const char *log_str
)
1618 return (zpool_export_common(zhp
, force
, B_FALSE
, log_str
));
1622 zpool_export_force(zpool_handle_t
*zhp
, const char *log_str
)
1624 return (zpool_export_common(zhp
, B_TRUE
, B_TRUE
, log_str
));
1628 zpool_rewind_exclaim(libzfs_handle_t
*hdl
, const char *name
, boolean_t dryrun
,
1631 nvlist_t
*nv
= NULL
;
1637 if (!hdl
->libzfs_printerr
|| config
== NULL
)
1640 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1641 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0) {
1645 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1647 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1649 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1650 strftime(timestr
, 128, "%c", &t
) != 0) {
1652 (void) printf(dgettext(TEXT_DOMAIN
,
1653 "Would be able to return %s "
1654 "to its state as of %s.\n"),
1657 (void) printf(dgettext(TEXT_DOMAIN
,
1658 "Pool %s returned to its state as of %s.\n"),
1662 (void) printf(dgettext(TEXT_DOMAIN
,
1663 "%s approximately %lld "),
1664 dryrun
? "Would discard" : "Discarded",
1665 ((longlong_t
)loss
+ 30) / 60);
1666 (void) printf(dgettext(TEXT_DOMAIN
,
1667 "minutes of transactions.\n"));
1668 } else if (loss
> 0) {
1669 (void) printf(dgettext(TEXT_DOMAIN
,
1670 "%s approximately %lld "),
1671 dryrun
? "Would discard" : "Discarded",
1673 (void) printf(dgettext(TEXT_DOMAIN
,
1674 "seconds of transactions.\n"));
1680 zpool_explain_recover(libzfs_handle_t
*hdl
, const char *name
, int reason
,
1683 nvlist_t
*nv
= NULL
;
1685 uint64_t edata
= UINT64_MAX
;
1690 if (!hdl
->libzfs_printerr
)
1694 (void) printf(dgettext(TEXT_DOMAIN
, "action: "));
1696 (void) printf(dgettext(TEXT_DOMAIN
, "\t"));
1698 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1699 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1700 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0 ||
1701 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1704 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1705 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_DATA_ERRORS
,
1708 (void) printf(dgettext(TEXT_DOMAIN
,
1709 "Recovery is possible, but will result in some data loss.\n"));
1711 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1712 strftime(timestr
, 128, "%c", &t
) != 0) {
1713 (void) printf(dgettext(TEXT_DOMAIN
,
1714 "\tReturning the pool to its state as of %s\n"
1715 "\tshould correct the problem. "),
1718 (void) printf(dgettext(TEXT_DOMAIN
,
1719 "\tReverting the pool to an earlier state "
1720 "should correct the problem.\n\t"));
1724 (void) printf(dgettext(TEXT_DOMAIN
,
1725 "Approximately %lld minutes of data\n"
1726 "\tmust be discarded, irreversibly. "),
1727 ((longlong_t
)loss
+ 30) / 60);
1728 } else if (loss
> 0) {
1729 (void) printf(dgettext(TEXT_DOMAIN
,
1730 "Approximately %lld seconds of data\n"
1731 "\tmust be discarded, irreversibly. "),
1734 if (edata
!= 0 && edata
!= UINT64_MAX
) {
1736 (void) printf(dgettext(TEXT_DOMAIN
,
1737 "After rewind, at least\n"
1738 "\tone persistent user-data error will remain. "));
1740 (void) printf(dgettext(TEXT_DOMAIN
,
1741 "After rewind, several\n"
1742 "\tpersistent user-data errors will remain. "));
1745 (void) printf(dgettext(TEXT_DOMAIN
,
1746 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1747 reason
>= 0 ? "clear" : "import", name
);
1749 (void) printf(dgettext(TEXT_DOMAIN
,
1750 "A scrub of the pool\n"
1751 "\tis strongly recommended after recovery.\n"));
1755 (void) printf(dgettext(TEXT_DOMAIN
,
1756 "Destroy and re-create the pool from\n\ta backup source.\n"));
1760 * zpool_import() is a contracted interface. Should be kept the same
1763 * Applications should use zpool_import_props() to import a pool with
1764 * new properties value to be set.
1767 zpool_import(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1770 nvlist_t
*props
= NULL
;
1773 if (altroot
!= NULL
) {
1774 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
1775 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1776 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1780 if (nvlist_add_string(props
,
1781 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), altroot
) != 0 ||
1782 nvlist_add_string(props
,
1783 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
), "none") != 0) {
1785 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1786 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1791 ret
= zpool_import_props(hdl
, config
, newname
, props
,
1798 print_vdev_tree(libzfs_handle_t
*hdl
, const char *name
, nvlist_t
*nv
,
1804 uint64_t is_log
= 0;
1806 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
,
1810 (void) printf("\t%*s%s%s\n", indent
, "", name
,
1811 is_log
? " [log]" : "");
1813 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1814 &child
, &children
) != 0)
1817 for (c
= 0; c
< children
; c
++) {
1818 vname
= zpool_vdev_name(hdl
, NULL
, child
[c
], VDEV_NAME_TYPE_ID
);
1819 print_vdev_tree(hdl
, vname
, child
[c
], indent
+ 2);
1825 zpool_print_unsup_feat(nvlist_t
*config
)
1827 nvlist_t
*nvinfo
, *unsup_feat
;
1830 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) ==
1832 verify(nvlist_lookup_nvlist(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
,
1835 for (nvp
= nvlist_next_nvpair(unsup_feat
, NULL
); nvp
!= NULL
;
1836 nvp
= nvlist_next_nvpair(unsup_feat
, nvp
)) {
1839 verify(nvpair_type(nvp
) == DATA_TYPE_STRING
);
1840 verify(nvpair_value_string(nvp
, &desc
) == 0);
1842 if (strlen(desc
) > 0)
1843 (void) printf("\t%s (%s)\n", nvpair_name(nvp
), desc
);
1845 (void) printf("\t%s\n", nvpair_name(nvp
));
1850 * Import the given pool using the known configuration and a list of
1851 * properties to be set. The configuration should have come from
1852 * zpool_find_import(). The 'newname' parameters control whether the pool
1853 * is imported with a different name.
1856 zpool_import_props(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1857 nvlist_t
*props
, int flags
)
1859 zfs_cmd_t zc
= {"\0"};
1860 zpool_load_policy_t policy
;
1861 nvlist_t
*nv
= NULL
;
1862 nvlist_t
*nvinfo
= NULL
;
1863 nvlist_t
*missing
= NULL
;
1870 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1873 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1874 "cannot import pool '%s'"), origname
);
1876 if (newname
!= NULL
) {
1877 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
1878 return (zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1879 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1881 thename
= (char *)newname
;
1886 if (props
!= NULL
) {
1888 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
1890 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
1893 if ((props
= zpool_valid_proplist(hdl
, origname
,
1894 props
, version
, flags
, errbuf
)) == NULL
)
1896 if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
1903 (void) strlcpy(zc
.zc_name
, thename
, sizeof (zc
.zc_name
));
1905 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1908 if (zcmd_write_conf_nvlist(hdl
, &zc
, config
) != 0) {
1909 zcmd_free_nvlists(&zc
);
1912 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zc
.zc_nvlist_conf_size
* 2) != 0) {
1913 zcmd_free_nvlists(&zc
);
1917 zc
.zc_cookie
= flags
;
1918 while ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_IMPORT
, &zc
)) != 0 &&
1920 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
1921 zcmd_free_nvlists(&zc
);
1928 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nv
);
1930 zcmd_free_nvlists(&zc
);
1932 zpool_get_load_policy(config
, &policy
);
1939 * Dry-run failed, but we print out what success
1940 * looks like if we found a best txg
1942 if (policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) {
1943 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1949 if (newname
== NULL
)
1950 (void) snprintf(desc
, sizeof (desc
),
1951 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1954 (void) snprintf(desc
, sizeof (desc
),
1955 dgettext(TEXT_DOMAIN
, "cannot import '%s' as '%s'"),
1960 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
1961 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1962 nvlist_exists(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
)) {
1963 (void) printf(dgettext(TEXT_DOMAIN
, "This "
1964 "pool uses the following feature(s) not "
1965 "supported by this system:\n"));
1966 zpool_print_unsup_feat(nv
);
1967 if (nvlist_exists(nvinfo
,
1968 ZPOOL_CONFIG_CAN_RDONLY
)) {
1969 (void) printf(dgettext(TEXT_DOMAIN
,
1970 "All unsupported features are only "
1971 "required for writing to the pool."
1972 "\nThe pool can be imported using "
1973 "'-o readonly=on'.\n"));
1977 * Unsupported version.
1979 (void) zfs_error(hdl
, EZFS_BADVERSION
, desc
);
1983 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
1984 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0) {
1985 char *hostname
= "<unknown>";
1986 uint64_t hostid
= 0;
1987 mmp_state_t mmp_state
;
1989 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
1990 ZPOOL_CONFIG_MMP_STATE
);
1992 if (nvlist_exists(nvinfo
,
1993 ZPOOL_CONFIG_MMP_HOSTNAME
))
1994 hostname
= fnvlist_lookup_string(nvinfo
,
1995 ZPOOL_CONFIG_MMP_HOSTNAME
);
1997 if (nvlist_exists(nvinfo
,
1998 ZPOOL_CONFIG_MMP_HOSTID
))
1999 hostid
= fnvlist_lookup_uint64(nvinfo
,
2000 ZPOOL_CONFIG_MMP_HOSTID
);
2002 if (mmp_state
== MMP_STATE_ACTIVE
) {
2003 (void) snprintf(aux
, sizeof (aux
),
2004 dgettext(TEXT_DOMAIN
, "pool is imp"
2005 "orted on host '%s' (hostid=%lx).\n"
2006 "Export the pool on the other "
2007 "system, then run 'zpool import'."),
2008 hostname
, (unsigned long) hostid
);
2009 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
2010 (void) snprintf(aux
, sizeof (aux
),
2011 dgettext(TEXT_DOMAIN
, "pool has "
2012 "the multihost property on and "
2013 "the\nsystem's hostid is not set. "
2014 "Set a unique system hostid with "
2015 "the zgenhostid(8) command.\n"));
2018 (void) zfs_error_aux(hdl
, aux
);
2020 (void) zfs_error(hdl
, EZFS_ACTIVE_POOL
, desc
);
2024 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, desc
);
2028 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2029 "one or more devices is read only"));
2030 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
2034 if (nv
&& nvlist_lookup_nvlist(nv
,
2035 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
2036 nvlist_lookup_nvlist(nvinfo
,
2037 ZPOOL_CONFIG_MISSING_DEVICES
, &missing
) == 0) {
2038 (void) printf(dgettext(TEXT_DOMAIN
,
2039 "The devices below are missing or "
2040 "corrupted, use '-m' to import the pool "
2042 print_vdev_tree(hdl
, NULL
, missing
, 2);
2043 (void) printf("\n");
2045 (void) zpool_standard_error(hdl
, error
, desc
);
2049 (void) zpool_standard_error(hdl
, error
, desc
);
2053 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2054 "one or more devices are already in use\n"));
2055 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
2058 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2059 "new name of at least one dataset is longer than "
2060 "the maximum allowable length"));
2061 (void) zfs_error(hdl
, EZFS_NAMETOOLONG
, desc
);
2064 (void) zpool_standard_error(hdl
, error
, desc
);
2065 zpool_explain_recover(hdl
,
2066 newname
? origname
: thename
, -error
, nv
);
2073 zpool_handle_t
*zhp
;
2076 * This should never fail, but play it safe anyway.
2078 if (zpool_open_silent(hdl
, thename
, &zhp
) != 0)
2080 else if (zhp
!= NULL
)
2082 if (policy
.zlp_rewind
&
2083 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
2084 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
2085 ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) != 0), nv
);
2095 * Translate vdev names to guids. If a vdev_path is determined to be
2096 * unsuitable then a vd_errlist is allocated and the vdev path and errno
2100 zpool_translate_vdev_guids(zpool_handle_t
*zhp
, nvlist_t
*vds
,
2101 nvlist_t
*vdev_guids
, nvlist_t
*guids_to_paths
, nvlist_t
**vd_errlist
)
2103 nvlist_t
*errlist
= NULL
;
2106 for (nvpair_t
*elem
= nvlist_next_nvpair(vds
, NULL
); elem
!= NULL
;
2107 elem
= nvlist_next_nvpair(vds
, elem
)) {
2108 boolean_t spare
, cache
;
2110 char *vd_path
= nvpair_name(elem
);
2111 nvlist_t
*tgt
= zpool_find_vdev(zhp
, vd_path
, &spare
, &cache
,
2114 if ((tgt
== NULL
) || cache
|| spare
) {
2115 if (errlist
== NULL
) {
2116 errlist
= fnvlist_alloc();
2120 uint64_t err
= (tgt
== NULL
) ? EZFS_NODEVICE
:
2121 (spare
? EZFS_ISSPARE
: EZFS_ISL2CACHE
);
2122 fnvlist_add_int64(errlist
, vd_path
, err
);
2126 uint64_t guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
2127 fnvlist_add_uint64(vdev_guids
, vd_path
, guid
);
2129 char msg
[MAXNAMELEN
];
2130 (void) snprintf(msg
, sizeof (msg
), "%llu", (u_longlong_t
)guid
);
2131 fnvlist_add_string(guids_to_paths
, msg
, vd_path
);
2135 verify(errlist
!= NULL
);
2136 if (vd_errlist
!= NULL
)
2137 *vd_errlist
= errlist
;
2139 fnvlist_free(errlist
);
2146 xlate_init_err(int err
)
2150 return (EZFS_NODEVICE
);
2153 return (EZFS_BADDEV
);
2155 return (EZFS_INITIALIZING
);
2157 return (EZFS_NO_INITIALIZE
);
2163 * Begin, suspend, or cancel the initialization (initializing of all free
2164 * blocks) for the given vdevs in the given pool.
2167 zpool_initialize_impl(zpool_handle_t
*zhp
, pool_initialize_func_t cmd_type
,
2168 nvlist_t
*vds
, boolean_t wait
)
2172 nvlist_t
*vdev_guids
= fnvlist_alloc();
2173 nvlist_t
*guids_to_paths
= fnvlist_alloc();
2174 nvlist_t
*vd_errlist
= NULL
;
2178 err
= zpool_translate_vdev_guids(zhp
, vds
, vdev_guids
,
2179 guids_to_paths
, &vd_errlist
);
2182 verify(vd_errlist
!= NULL
);
2186 err
= lzc_initialize(zhp
->zpool_name
, cmd_type
,
2187 vdev_guids
, &errlist
);
2190 if (errlist
!= NULL
) {
2191 vd_errlist
= fnvlist_lookup_nvlist(errlist
,
2192 ZPOOL_INITIALIZE_VDEVS
);
2195 (void) zpool_standard_error(zhp
->zpool_hdl
, err
,
2196 dgettext(TEXT_DOMAIN
, "operation failed"));
2201 for (elem
= nvlist_next_nvpair(vdev_guids
, NULL
); elem
!= NULL
;
2202 elem
= nvlist_next_nvpair(vdev_guids
, elem
)) {
2204 uint64_t guid
= fnvpair_value_uint64(elem
);
2206 err
= lzc_wait_tag(zhp
->zpool_name
,
2207 ZPOOL_WAIT_INITIALIZE
, guid
, NULL
);
2209 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
,
2210 err
, dgettext(TEXT_DOMAIN
, "error "
2211 "waiting for '%s' to initialize"),
2221 for (elem
= nvlist_next_nvpair(vd_errlist
, NULL
); elem
!= NULL
;
2222 elem
= nvlist_next_nvpair(vd_errlist
, elem
)) {
2223 int64_t vd_error
= xlate_init_err(fnvpair_value_int64(elem
));
2226 if (nvlist_lookup_string(guids_to_paths
, nvpair_name(elem
),
2228 path
= nvpair_name(elem
);
2230 (void) zfs_error_fmt(zhp
->zpool_hdl
, vd_error
,
2231 "cannot initialize '%s'", path
);
2235 fnvlist_free(vdev_guids
);
2236 fnvlist_free(guids_to_paths
);
2238 if (vd_errlist
!= NULL
)
2239 fnvlist_free(vd_errlist
);
2241 return (err
== 0 ? 0 : -1);
2245 zpool_initialize(zpool_handle_t
*zhp
, pool_initialize_func_t cmd_type
,
2248 return (zpool_initialize_impl(zhp
, cmd_type
, vds
, B_FALSE
));
2252 zpool_initialize_wait(zpool_handle_t
*zhp
, pool_initialize_func_t cmd_type
,
2255 return (zpool_initialize_impl(zhp
, cmd_type
, vds
, B_TRUE
));
2259 xlate_trim_err(int err
)
2263 return (EZFS_NODEVICE
);
2266 return (EZFS_BADDEV
);
2268 return (EZFS_TRIMMING
);
2270 return (EZFS_NO_TRIM
);
2272 return (EZFS_TRIM_NOTSUP
);
2278 zpool_trim_wait(zpool_handle_t
*zhp
, nvlist_t
*vdev_guids
)
2283 for (elem
= nvlist_next_nvpair(vdev_guids
, NULL
); elem
!= NULL
;
2284 elem
= nvlist_next_nvpair(vdev_guids
, elem
)) {
2286 uint64_t guid
= fnvpair_value_uint64(elem
);
2288 err
= lzc_wait_tag(zhp
->zpool_name
,
2289 ZPOOL_WAIT_TRIM
, guid
, NULL
);
2291 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
,
2292 err
, dgettext(TEXT_DOMAIN
, "error "
2293 "waiting to trim '%s'"), nvpair_name(elem
));
2302 * Check errlist and report any errors, omitting ones which should be
2303 * suppressed. Returns B_TRUE if any errors were reported.
2306 check_trim_errs(zpool_handle_t
*zhp
, trimflags_t
*trim_flags
,
2307 nvlist_t
*guids_to_paths
, nvlist_t
*vds
, nvlist_t
*errlist
)
2310 boolean_t reported_errs
= B_FALSE
;
2312 int num_suppressed_errs
= 0;
2314 for (elem
= nvlist_next_nvpair(vds
, NULL
);
2315 elem
!= NULL
; elem
= nvlist_next_nvpair(vds
, elem
)) {
2319 for (elem
= nvlist_next_nvpair(errlist
, NULL
);
2320 elem
!= NULL
; elem
= nvlist_next_nvpair(errlist
, elem
)) {
2321 int64_t vd_error
= xlate_trim_err(fnvpair_value_int64(elem
));
2325 * If only the pool was specified, and it was not a secure
2326 * trim then suppress warnings for individual vdevs which
2327 * do not support trimming.
2329 if (vd_error
== EZFS_TRIM_NOTSUP
&&
2330 trim_flags
->fullpool
&&
2331 !trim_flags
->secure
) {
2332 num_suppressed_errs
++;
2336 reported_errs
= B_TRUE
;
2337 if (nvlist_lookup_string(guids_to_paths
, nvpair_name(elem
),
2339 path
= nvpair_name(elem
);
2341 (void) zfs_error_fmt(zhp
->zpool_hdl
, vd_error
,
2342 "cannot trim '%s'", path
);
2345 if (num_suppressed_errs
== num_vds
) {
2346 (void) zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
2347 "no devices in pool support trim operations"));
2348 (void) (zfs_error(zhp
->zpool_hdl
, EZFS_TRIM_NOTSUP
,
2349 dgettext(TEXT_DOMAIN
, "cannot trim")));
2350 reported_errs
= B_TRUE
;
2353 return (reported_errs
);
2357 * Begin, suspend, or cancel the TRIM (discarding of all free blocks) for
2358 * the given vdevs in the given pool.
2361 zpool_trim(zpool_handle_t
*zhp
, pool_trim_func_t cmd_type
, nvlist_t
*vds
,
2362 trimflags_t
*trim_flags
)
2367 nvlist_t
*vdev_guids
= fnvlist_alloc();
2368 nvlist_t
*guids_to_paths
= fnvlist_alloc();
2369 nvlist_t
*errlist
= NULL
;
2371 err
= zpool_translate_vdev_guids(zhp
, vds
, vdev_guids
,
2372 guids_to_paths
, &errlist
);
2374 check_trim_errs(zhp
, trim_flags
, guids_to_paths
, vds
, errlist
);
2379 err
= lzc_trim(zhp
->zpool_name
, cmd_type
, trim_flags
->rate
,
2380 trim_flags
->secure
, vdev_guids
, &errlist
);
2382 nvlist_t
*vd_errlist
;
2383 if (errlist
!= NULL
&& nvlist_lookup_nvlist(errlist
,
2384 ZPOOL_TRIM_VDEVS
, &vd_errlist
) == 0) {
2385 if (check_trim_errs(zhp
, trim_flags
, guids_to_paths
,
2393 (void) snprintf(msg
, sizeof (msg
),
2394 dgettext(TEXT_DOMAIN
, "operation failed"));
2395 zpool_standard_error(zhp
->zpool_hdl
, err
, msg
);
2402 if (trim_flags
->wait
)
2403 retval
= zpool_trim_wait(zhp
, vdev_guids
);
2406 if (errlist
!= NULL
)
2407 fnvlist_free(errlist
);
2408 fnvlist_free(vdev_guids
);
2409 fnvlist_free(guids_to_paths
);
2417 zpool_scan(zpool_handle_t
*zhp
, pool_scan_func_t func
, pool_scrub_cmd_t cmd
)
2419 zfs_cmd_t zc
= {"\0"};
2422 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2424 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2425 zc
.zc_cookie
= func
;
2428 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_SCAN
, &zc
) == 0)
2433 /* ECANCELED on a scrub means we resumed a paused scrub */
2434 if (err
== ECANCELED
&& func
== POOL_SCAN_SCRUB
&&
2435 cmd
== POOL_SCRUB_NORMAL
)
2438 if (err
== ENOENT
&& func
!= POOL_SCAN_NONE
&& cmd
== POOL_SCRUB_NORMAL
)
2441 if (func
== POOL_SCAN_SCRUB
) {
2442 if (cmd
== POOL_SCRUB_PAUSE
) {
2443 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2444 "cannot pause scrubbing %s"), zc
.zc_name
);
2446 assert(cmd
== POOL_SCRUB_NORMAL
);
2447 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2448 "cannot scrub %s"), zc
.zc_name
);
2450 } else if (func
== POOL_SCAN_RESILVER
) {
2451 assert(cmd
== POOL_SCRUB_NORMAL
);
2452 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2453 "cannot restart resilver on %s"), zc
.zc_name
);
2454 } else if (func
== POOL_SCAN_NONE
) {
2455 (void) snprintf(msg
, sizeof (msg
),
2456 dgettext(TEXT_DOMAIN
, "cannot cancel scrubbing %s"),
2459 assert(!"unexpected result");
2464 pool_scan_stat_t
*ps
= NULL
;
2467 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
2468 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
2469 (void) nvlist_lookup_uint64_array(nvroot
,
2470 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &psc
);
2471 if (ps
&& ps
->pss_func
== POOL_SCAN_SCRUB
&&
2472 ps
->pss_state
== DSS_SCANNING
) {
2473 if (cmd
== POOL_SCRUB_PAUSE
)
2474 return (zfs_error(hdl
, EZFS_SCRUB_PAUSED
, msg
));
2476 return (zfs_error(hdl
, EZFS_SCRUBBING
, msg
));
2478 return (zfs_error(hdl
, EZFS_RESILVERING
, msg
));
2480 } else if (err
== ENOENT
) {
2481 return (zfs_error(hdl
, EZFS_NO_SCRUB
, msg
));
2482 } else if (err
== ENOTSUP
&& func
== POOL_SCAN_RESILVER
) {
2483 return (zfs_error(hdl
, EZFS_NO_RESILVER_DEFER
, msg
));
2485 return (zpool_standard_error(hdl
, err
, msg
));
2490 * Find a vdev that matches the search criteria specified. We use the
2491 * the nvpair name to determine how we should look for the device.
2492 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2493 * spare; but FALSE if its an INUSE spare.
2496 vdev_to_nvlist_iter(nvlist_t
*nv
, nvlist_t
*search
, boolean_t
*avail_spare
,
2497 boolean_t
*l2cache
, boolean_t
*log
)
2504 nvpair_t
*pair
= nvlist_next_nvpair(search
, NULL
);
2506 /* Nothing to look for */
2507 if (search
== NULL
|| pair
== NULL
)
2510 /* Obtain the key we will use to search */
2511 srchkey
= nvpair_name(pair
);
2513 switch (nvpair_type(pair
)) {
2514 case DATA_TYPE_UINT64
:
2515 if (strcmp(srchkey
, ZPOOL_CONFIG_GUID
) == 0) {
2516 uint64_t srchval
, theguid
;
2518 verify(nvpair_value_uint64(pair
, &srchval
) == 0);
2519 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
2521 if (theguid
== srchval
)
2526 case DATA_TYPE_STRING
: {
2527 char *srchval
, *val
;
2529 verify(nvpair_value_string(pair
, &srchval
) == 0);
2530 if (nvlist_lookup_string(nv
, srchkey
, &val
) != 0)
2534 * Search for the requested value. Special cases:
2536 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2537 * "-part1", or "p1". The suffix is hidden from the user,
2538 * but included in the string, so this matches around it.
2539 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2540 * is used to check all possible expanded paths.
2541 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2543 * Otherwise, all other searches are simple string compares.
2545 if (strcmp(srchkey
, ZPOOL_CONFIG_PATH
) == 0) {
2546 uint64_t wholedisk
= 0;
2548 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
2550 if (zfs_strcmp_pathname(srchval
, val
, wholedisk
) == 0)
2553 } else if (strcmp(srchkey
, ZPOOL_CONFIG_TYPE
) == 0 && val
) {
2554 char *type
, *idx
, *end
, *p
;
2555 uint64_t id
, vdev_id
;
2558 * Determine our vdev type, keeping in mind
2559 * that the srchval is composed of a type and
2560 * vdev id pair (i.e. mirror-4).
2562 if ((type
= strdup(srchval
)) == NULL
)
2565 if ((p
= strrchr(type
, '-')) == NULL
) {
2573 * If the types don't match then keep looking.
2575 if (strncmp(val
, type
, strlen(val
)) != 0) {
2580 verify(zpool_vdev_is_interior(type
));
2581 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
2585 vdev_id
= strtoull(idx
, &end
, 10);
2592 * Now verify that we have the correct vdev id.
2601 if (strcmp(srchval
, val
) == 0)
2610 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2611 &child
, &children
) != 0)
2614 for (c
= 0; c
< children
; c
++) {
2615 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2616 avail_spare
, l2cache
, NULL
)) != NULL
) {
2618 * The 'is_log' value is only set for the toplevel
2619 * vdev, not the leaf vdevs. So we always lookup the
2620 * log device from the root of the vdev tree (where
2621 * 'log' is non-NULL).
2624 nvlist_lookup_uint64(child
[c
],
2625 ZPOOL_CONFIG_IS_LOG
, &is_log
) == 0 &&
2633 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2634 &child
, &children
) == 0) {
2635 for (c
= 0; c
< children
; c
++) {
2636 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2637 avail_spare
, l2cache
, NULL
)) != NULL
) {
2638 *avail_spare
= B_TRUE
;
2644 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2645 &child
, &children
) == 0) {
2646 for (c
= 0; c
< children
; c
++) {
2647 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2648 avail_spare
, l2cache
, NULL
)) != NULL
) {
2659 * Given a physical path or guid, find the associated vdev.
2662 zpool_find_vdev_by_physpath(zpool_handle_t
*zhp
, const char *ppath
,
2663 boolean_t
*avail_spare
, boolean_t
*l2cache
, boolean_t
*log
)
2665 nvlist_t
*search
, *nvroot
, *ret
;
2669 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2671 guid
= strtoull(ppath
, &end
, 0);
2672 if (guid
!= 0 && *end
== '\0') {
2673 verify(nvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
) == 0);
2675 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PHYS_PATH
,
2679 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2682 *avail_spare
= B_FALSE
;
2686 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2687 nvlist_free(search
);
2693 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2696 zpool_vdev_is_interior(const char *name
)
2698 if (strncmp(name
, VDEV_TYPE_RAIDZ
, strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2699 strncmp(name
, VDEV_TYPE_SPARE
, strlen(VDEV_TYPE_SPARE
)) == 0 ||
2701 VDEV_TYPE_REPLACING
, strlen(VDEV_TYPE_REPLACING
)) == 0 ||
2702 strncmp(name
, VDEV_TYPE_MIRROR
, strlen(VDEV_TYPE_MIRROR
)) == 0)
2705 if (strncmp(name
, VDEV_TYPE_DRAID
, strlen(VDEV_TYPE_DRAID
)) == 0 &&
2706 !zpool_is_draid_spare(name
))
2713 zpool_find_vdev(zpool_handle_t
*zhp
, const char *path
, boolean_t
*avail_spare
,
2714 boolean_t
*l2cache
, boolean_t
*log
)
2717 nvlist_t
*nvroot
, *search
, *ret
;
2720 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2722 guid
= strtoull(path
, &end
, 0);
2723 if (guid
!= 0 && *end
== '\0') {
2724 verify(nvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
) == 0);
2725 } else if (zpool_vdev_is_interior(path
)) {
2726 verify(nvlist_add_string(search
, ZPOOL_CONFIG_TYPE
, path
) == 0);
2728 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PATH
, path
) == 0);
2731 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2734 *avail_spare
= B_FALSE
;
2738 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2739 nvlist_free(search
);
2745 vdev_is_online(nvlist_t
*nv
)
2749 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
, &ival
) == 0 ||
2750 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
, &ival
) == 0 ||
2751 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
, &ival
) == 0)
2758 * Helper function for zpool_get_physpaths().
2761 vdev_get_one_physpath(nvlist_t
*config
, char *physpath
, size_t physpath_size
,
2762 size_t *bytes_written
)
2764 size_t bytes_left
, pos
, rsz
;
2768 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PHYS_PATH
,
2770 return (EZFS_NODEVICE
);
2772 pos
= *bytes_written
;
2773 bytes_left
= physpath_size
- pos
;
2774 format
= (pos
== 0) ? "%s" : " %s";
2776 rsz
= snprintf(physpath
+ pos
, bytes_left
, format
, tmppath
);
2777 *bytes_written
+= rsz
;
2779 if (rsz
>= bytes_left
) {
2780 /* if physpath was not copied properly, clear it */
2781 if (bytes_left
!= 0) {
2784 return (EZFS_NOSPC
);
2790 vdev_get_physpaths(nvlist_t
*nv
, char *physpath
, size_t phypath_size
,
2791 size_t *rsz
, boolean_t is_spare
)
2796 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
2797 return (EZFS_INVALCONFIG
);
2799 if (strcmp(type
, VDEV_TYPE_DISK
) == 0) {
2801 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2802 * For a spare vdev, we only want to boot from the active
2807 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
2810 return (EZFS_INVALCONFIG
);
2813 if (vdev_is_online(nv
)) {
2814 if ((ret
= vdev_get_one_physpath(nv
, physpath
,
2815 phypath_size
, rsz
)) != 0)
2818 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) == 0 ||
2819 strcmp(type
, VDEV_TYPE_RAIDZ
) == 0 ||
2820 strcmp(type
, VDEV_TYPE_REPLACING
) == 0 ||
2821 (is_spare
= (strcmp(type
, VDEV_TYPE_SPARE
) == 0))) {
2826 if (nvlist_lookup_nvlist_array(nv
,
2827 ZPOOL_CONFIG_CHILDREN
, &child
, &count
) != 0)
2828 return (EZFS_INVALCONFIG
);
2830 for (i
= 0; i
< count
; i
++) {
2831 ret
= vdev_get_physpaths(child
[i
], physpath
,
2832 phypath_size
, rsz
, is_spare
);
2833 if (ret
== EZFS_NOSPC
)
2838 return (EZFS_POOL_INVALARG
);
2842 * Get phys_path for a root pool config.
2843 * Return 0 on success; non-zero on failure.
2846 zpool_get_config_physpath(nvlist_t
*config
, char *physpath
, size_t phypath_size
)
2849 nvlist_t
*vdev_root
;
2856 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2858 return (EZFS_INVALCONFIG
);
2860 if (nvlist_lookup_string(vdev_root
, ZPOOL_CONFIG_TYPE
, &type
) != 0 ||
2861 nvlist_lookup_nvlist_array(vdev_root
, ZPOOL_CONFIG_CHILDREN
,
2862 &child
, &count
) != 0)
2863 return (EZFS_INVALCONFIG
);
2866 * root pool can only have a single top-level vdev.
2868 if (strcmp(type
, VDEV_TYPE_ROOT
) != 0 || count
!= 1)
2869 return (EZFS_POOL_INVALARG
);
2871 (void) vdev_get_physpaths(child
[0], physpath
, phypath_size
, &rsz
,
2874 /* No online devices */
2876 return (EZFS_NODEVICE
);
2882 * Get phys_path for a root pool
2883 * Return 0 on success; non-zero on failure.
2886 zpool_get_physpath(zpool_handle_t
*zhp
, char *physpath
, size_t phypath_size
)
2888 return (zpool_get_config_physpath(zhp
->zpool_config
, physpath
,
2893 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
2895 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
2896 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
2900 zpool_vdev_path_to_guid_impl(zpool_handle_t
*zhp
, const char *path
,
2901 boolean_t
*is_spare
, boolean_t
*is_l2cache
, boolean_t
*is_log
)
2904 boolean_t spare
= B_FALSE
, l2cache
= B_FALSE
, log
= B_FALSE
;
2907 if ((tgt
= zpool_find_vdev(zhp
, path
, &spare
, &l2cache
,
2911 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &guid
) == 0);
2912 if (is_spare
!= NULL
)
2914 if (is_l2cache
!= NULL
)
2915 *is_l2cache
= l2cache
;
2922 /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
2924 zpool_vdev_path_to_guid(zpool_handle_t
*zhp
, const char *path
)
2926 return (zpool_vdev_path_to_guid_impl(zhp
, path
, NULL
, NULL
, NULL
));
2930 * Bring the specified vdev online. The 'flags' parameter is a set of the
2931 * ZFS_ONLINE_* flags.
2934 zpool_vdev_online(zpool_handle_t
*zhp
, const char *path
, int flags
,
2935 vdev_state_t
*newstate
)
2937 zfs_cmd_t zc
= {"\0"};
2941 boolean_t avail_spare
, l2cache
, islog
;
2942 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2945 if (flags
& ZFS_ONLINE_EXPAND
) {
2946 (void) snprintf(msg
, sizeof (msg
),
2947 dgettext(TEXT_DOMAIN
, "cannot expand %s"), path
);
2949 (void) snprintf(msg
, sizeof (msg
),
2950 dgettext(TEXT_DOMAIN
, "cannot online %s"), path
);
2953 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2954 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2956 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2958 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2961 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2963 if ((flags
& ZFS_ONLINE_EXPAND
||
2964 zpool_get_prop_int(zhp
, ZPOOL_PROP_AUTOEXPAND
, NULL
)) &&
2965 nvlist_lookup_string(tgt
, ZPOOL_CONFIG_PATH
, &pathname
) == 0) {
2966 uint64_t wholedisk
= 0;
2968 (void) nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_WHOLE_DISK
,
2972 * XXX - L2ARC 1.0 devices can't support expansion.
2975 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2976 "cannot expand cache devices"));
2977 return (zfs_error(hdl
, EZFS_VDEVNOTSUP
, msg
));
2981 const char *fullpath
= path
;
2982 char buf
[MAXPATHLEN
];
2984 if (path
[0] != '/') {
2985 error
= zfs_resolve_shortname(path
, buf
,
2988 return (zfs_error(hdl
, EZFS_NODEVICE
,
2994 error
= zpool_relabel_disk(hdl
, fullpath
, msg
);
3000 zc
.zc_cookie
= VDEV_STATE_ONLINE
;
3003 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) != 0) {
3004 if (errno
== EINVAL
) {
3005 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "was split "
3006 "from this pool into a new one. Use '%s' "
3007 "instead"), "zpool detach");
3008 return (zfs_error(hdl
, EZFS_POSTSPLIT_ONLINE
, msg
));
3010 return (zpool_standard_error(hdl
, errno
, msg
));
3013 *newstate
= zc
.zc_cookie
;
3018 * Take the specified vdev offline
3021 zpool_vdev_offline(zpool_handle_t
*zhp
, const char *path
, boolean_t istmp
)
3023 zfs_cmd_t zc
= {"\0"};
3026 boolean_t avail_spare
, l2cache
;
3027 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3029 (void) snprintf(msg
, sizeof (msg
),
3030 dgettext(TEXT_DOMAIN
, "cannot offline %s"), path
);
3032 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3033 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3035 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3037 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
3040 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3042 zc
.zc_cookie
= VDEV_STATE_OFFLINE
;
3043 zc
.zc_obj
= istmp
? ZFS_OFFLINE_TEMPORARY
: 0;
3045 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3052 * There are no other replicas of this device.
3054 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
3058 * The log device has unplayed logs
3060 return (zfs_error(hdl
, EZFS_UNPLAYED_LOGS
, msg
));
3063 return (zpool_standard_error(hdl
, errno
, msg
));
3068 * Mark the given vdev faulted.
3071 zpool_vdev_fault(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
3073 zfs_cmd_t zc
= {"\0"};
3075 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3077 (void) snprintf(msg
, sizeof (msg
),
3078 dgettext(TEXT_DOMAIN
, "cannot fault %llu"), (u_longlong_t
)guid
);
3080 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3082 zc
.zc_cookie
= VDEV_STATE_FAULTED
;
3085 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3092 * There are no other replicas of this device.
3094 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
3097 return (zpool_standard_error(hdl
, errno
, msg
));
3103 * Mark the given vdev degraded.
3106 zpool_vdev_degrade(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
3108 zfs_cmd_t zc
= {"\0"};
3110 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3112 (void) snprintf(msg
, sizeof (msg
),
3113 dgettext(TEXT_DOMAIN
, "cannot degrade %llu"), (u_longlong_t
)guid
);
3115 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3117 zc
.zc_cookie
= VDEV_STATE_DEGRADED
;
3120 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3123 return (zpool_standard_error(hdl
, errno
, msg
));
3127 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
3131 is_replacing_spare(nvlist_t
*search
, nvlist_t
*tgt
, int which
)
3137 if (nvlist_lookup_nvlist_array(search
, ZPOOL_CONFIG_CHILDREN
, &child
,
3139 verify(nvlist_lookup_string(search
, ZPOOL_CONFIG_TYPE
,
3142 if ((strcmp(type
, VDEV_TYPE_SPARE
) == 0 ||
3143 strcmp(type
, VDEV_TYPE_DRAID_SPARE
) == 0) &&
3144 children
== 2 && child
[which
] == tgt
)
3147 for (c
= 0; c
< children
; c
++)
3148 if (is_replacing_spare(child
[c
], tgt
, which
))
3156 * Attach new_disk (fully described by nvroot) to old_disk.
3157 * If 'replacing' is specified, the new disk will replace the old one.
3160 zpool_vdev_attach(zpool_handle_t
*zhp
, const char *old_disk
,
3161 const char *new_disk
, nvlist_t
*nvroot
, int replacing
, boolean_t rebuild
)
3163 zfs_cmd_t zc
= {"\0"};
3167 boolean_t avail_spare
, l2cache
, islog
;
3172 nvlist_t
*config_root
;
3173 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3176 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
3177 "cannot replace %s with %s"), old_disk
, new_disk
);
3179 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
3180 "cannot attach %s to %s"), new_disk
, old_disk
);
3182 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3183 if ((tgt
= zpool_find_vdev(zhp
, old_disk
, &avail_spare
, &l2cache
,
3185 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3188 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3191 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
3193 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
3194 zc
.zc_cookie
= replacing
;
3195 zc
.zc_simple
= rebuild
;
3198 zfeature_lookup_guid("org.openzfs:device_rebuild", NULL
) != 0) {
3199 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3200 "the loaded zfs module doesn't support device rebuilds"));
3201 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, msg
));
3204 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
3205 &child
, &children
) != 0 || children
!= 1) {
3206 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3207 "new device must be a single disk"));
3208 return (zfs_error(hdl
, EZFS_INVALCONFIG
, msg
));
3211 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
3212 ZPOOL_CONFIG_VDEV_TREE
, &config_root
) == 0);
3214 if ((newname
= zpool_vdev_name(NULL
, NULL
, child
[0], 0)) == NULL
)
3218 * If the target is a hot spare that has been swapped in, we can only
3219 * replace it with another hot spare.
3222 nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_IS_SPARE
, &val
) == 0 &&
3223 (zpool_find_vdev(zhp
, newname
, &avail_spare
, &l2cache
,
3224 NULL
) == NULL
|| !avail_spare
) &&
3225 is_replacing_spare(config_root
, tgt
, 1)) {
3226 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3227 "can only be replaced by another hot spare"));
3229 return (zfs_error(hdl
, EZFS_BADTARGET
, msg
));
3234 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
3237 ret
= zfs_ioctl(hdl
, ZFS_IOC_VDEV_ATTACH
, &zc
);
3239 zcmd_free_nvlists(&zc
);
3247 * Can't attach to or replace this type of vdev.
3250 uint64_t version
= zpool_get_prop_int(zhp
,
3251 ZPOOL_PROP_VERSION
, NULL
);
3254 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3255 "cannot replace a log with a spare"));
3256 } else if (rebuild
) {
3257 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3258 "only mirror and dRAID vdevs support "
3259 "sequential reconstruction"));
3260 } else if (zpool_is_draid_spare(new_disk
)) {
3261 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3262 "dRAID spares can only replace child "
3263 "devices in their parent's dRAID vdev"));
3264 } else if (version
>= SPA_VERSION_MULTI_REPLACE
) {
3265 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3266 "already in replacing/spare config; wait "
3267 "for completion or use 'zpool detach'"));
3269 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3270 "cannot replace a replacing device"));
3273 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3274 "can only attach to mirrors and top-level "
3277 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
3282 * The new device must be a single disk.
3284 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3285 "new device must be a single disk"));
3286 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3290 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "%s is busy, "
3291 "or device removal is in progress"),
3293 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
3298 * The new device is too small.
3300 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3301 "device is too small"));
3302 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
3307 * The new device has a different optimal sector size.
3309 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3310 "new device has a different optimal sector size; use the "
3311 "option '-o ashift=N' to override the optimal size"));
3312 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
3317 * The resulting top-level vdev spec won't fit in the label.
3319 (void) zfs_error(hdl
, EZFS_DEVOVERFLOW
, msg
);
3323 (void) zpool_standard_error(hdl
, errno
, msg
);
3330 * Detach the specified device.
3333 zpool_vdev_detach(zpool_handle_t
*zhp
, const char *path
)
3335 zfs_cmd_t zc
= {"\0"};
3338 boolean_t avail_spare
, l2cache
;
3339 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3341 (void) snprintf(msg
, sizeof (msg
),
3342 dgettext(TEXT_DOMAIN
, "cannot detach %s"), path
);
3344 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3345 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3347 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3350 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3353 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
3355 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
3357 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_DETACH
, &zc
) == 0)
3364 * Can't detach from this type of vdev.
3366 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "only "
3367 "applicable to mirror and replacing vdevs"));
3368 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
3373 * There are no other replicas of this device.
3375 (void) zfs_error(hdl
, EZFS_NOREPLICAS
, msg
);
3379 (void) zpool_standard_error(hdl
, errno
, msg
);
3386 * Find a mirror vdev in the source nvlist.
3388 * The mchild array contains a list of disks in one of the top-level mirrors
3389 * of the source pool. The schild array contains a list of disks that the
3390 * user specified on the command line. We loop over the mchild array to
3391 * see if any entry in the schild array matches.
3393 * If a disk in the mchild array is found in the schild array, we return
3394 * the index of that entry. Otherwise we return -1.
3397 find_vdev_entry(zpool_handle_t
*zhp
, nvlist_t
**mchild
, uint_t mchildren
,
3398 nvlist_t
**schild
, uint_t schildren
)
3402 for (mc
= 0; mc
< mchildren
; mc
++) {
3404 char *mpath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
3407 for (sc
= 0; sc
< schildren
; sc
++) {
3408 char *spath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
3410 boolean_t result
= (strcmp(mpath
, spath
) == 0);
3426 * Split a mirror pool. If newroot points to null, then a new nvlist
3427 * is generated and it is the responsibility of the caller to free it.
3430 zpool_vdev_split(zpool_handle_t
*zhp
, char *newname
, nvlist_t
**newroot
,
3431 nvlist_t
*props
, splitflags_t flags
)
3433 zfs_cmd_t zc
= {"\0"};
3435 nvlist_t
*tree
, *config
, **child
, **newchild
, *newconfig
= NULL
;
3436 nvlist_t
**varray
= NULL
, *zc_props
= NULL
;
3437 uint_t c
, children
, newchildren
, lastlog
= 0, vcount
, found
= 0;
3438 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3439 uint64_t vers
, readonly
= B_FALSE
;
3440 boolean_t freelist
= B_FALSE
, memory_err
= B_TRUE
;
3443 (void) snprintf(msg
, sizeof (msg
),
3444 dgettext(TEXT_DOMAIN
, "Unable to split %s"), zhp
->zpool_name
);
3446 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
3447 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
3449 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
3450 (void) fprintf(stderr
, gettext("Internal error: unable to "
3451 "retrieve pool configuration\n"));
3455 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &tree
)
3457 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
, &vers
) == 0);
3460 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
3461 if ((zc_props
= zpool_valid_proplist(hdl
, zhp
->zpool_name
,
3462 props
, vers
, flags
, msg
)) == NULL
)
3464 (void) nvlist_lookup_uint64(zc_props
,
3465 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
3467 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3468 "property %s can only be set at import time"),
3469 zpool_prop_to_name(ZPOOL_PROP_READONLY
));
3474 if (nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
3476 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3477 "Source pool is missing vdev tree"));
3478 nvlist_free(zc_props
);
3482 varray
= zfs_alloc(hdl
, children
* sizeof (nvlist_t
*));
3485 if (*newroot
== NULL
||
3486 nvlist_lookup_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
3487 &newchild
, &newchildren
) != 0)
3490 for (c
= 0; c
< children
; c
++) {
3491 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
3493 nvlist_t
**mchild
, *vdev
;
3498 * Unlike cache & spares, slogs are stored in the
3499 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3501 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
3503 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
3505 if (is_log
|| is_hole
) {
3507 * Create a hole vdev and put it in the config.
3509 if (nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) != 0)
3511 if (nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
,
3512 VDEV_TYPE_HOLE
) != 0)
3514 if (nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_HOLE
,
3519 varray
[vcount
++] = vdev
;
3523 verify(nvlist_lookup_string(child
[c
], ZPOOL_CONFIG_TYPE
, &type
)
3526 if (strcmp(type
, VDEV_TYPE_INDIRECT
) == 0) {
3528 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
3531 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
3532 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3533 "Source pool must be composed only of mirrors\n"));
3534 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3538 verify(nvlist_lookup_nvlist_array(child
[c
],
3539 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
3541 /* find or add an entry for this top-level vdev */
3542 if (newchildren
> 0 &&
3543 (entry
= find_vdev_entry(zhp
, mchild
, mchildren
,
3544 newchild
, newchildren
)) >= 0) {
3545 /* We found a disk that the user specified. */
3546 vdev
= mchild
[entry
];
3549 /* User didn't specify a disk for this vdev. */
3550 vdev
= mchild
[mchildren
- 1];
3553 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
3557 /* did we find every disk the user specified? */
3558 if (found
!= newchildren
) {
3559 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Device list must "
3560 "include at most one disk from each mirror"));
3561 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3565 /* Prepare the nvlist for populating. */
3566 if (*newroot
== NULL
) {
3567 if (nvlist_alloc(newroot
, NV_UNIQUE_NAME
, 0) != 0)
3570 if (nvlist_add_string(*newroot
, ZPOOL_CONFIG_TYPE
,
3571 VDEV_TYPE_ROOT
) != 0)
3574 verify(nvlist_remove_all(*newroot
, ZPOOL_CONFIG_CHILDREN
) == 0);
3577 /* Add all the children we found */
3578 if (nvlist_add_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
, varray
,
3579 lastlog
== 0 ? vcount
: lastlog
) != 0)
3583 * If we're just doing a dry run, exit now with success.
3586 memory_err
= B_FALSE
;
3591 /* now build up the config list & call the ioctl */
3592 if (nvlist_alloc(&newconfig
, NV_UNIQUE_NAME
, 0) != 0)
3595 if (nvlist_add_nvlist(newconfig
,
3596 ZPOOL_CONFIG_VDEV_TREE
, *newroot
) != 0 ||
3597 nvlist_add_string(newconfig
,
3598 ZPOOL_CONFIG_POOL_NAME
, newname
) != 0 ||
3599 nvlist_add_uint64(newconfig
, ZPOOL_CONFIG_VERSION
, vers
) != 0)
3603 * The new pool is automatically part of the namespace unless we
3604 * explicitly export it.
3607 zc
.zc_cookie
= ZPOOL_EXPORT_AFTER_SPLIT
;
3608 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3609 (void) strlcpy(zc
.zc_string
, newname
, sizeof (zc
.zc_string
));
3610 if (zcmd_write_conf_nvlist(hdl
, &zc
, newconfig
) != 0)
3612 if (zc_props
!= NULL
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
3615 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SPLIT
, &zc
) != 0) {
3616 retval
= zpool_standard_error(hdl
, errno
, msg
);
3621 memory_err
= B_FALSE
;
3624 if (varray
!= NULL
) {
3627 for (v
= 0; v
< vcount
; v
++)
3628 nvlist_free(varray
[v
]);
3631 zcmd_free_nvlists(&zc
);
3632 nvlist_free(zc_props
);
3633 nvlist_free(newconfig
);
3635 nvlist_free(*newroot
);
3643 return (no_memory(hdl
));
3649 * Remove the given device.
3652 zpool_vdev_remove(zpool_handle_t
*zhp
, const char *path
)
3654 zfs_cmd_t zc
= {"\0"};
3657 boolean_t avail_spare
, l2cache
, islog
;
3658 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3661 (void) snprintf(msg
, sizeof (msg
),
3662 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3664 if (zpool_is_draid_spare(path
)) {
3665 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3666 "dRAID spares cannot be removed"));
3667 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3670 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3671 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3673 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3675 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
3676 if (islog
&& version
< SPA_VERSION_HOLES
) {
3677 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3678 "pool must be upgraded to support log removal"));
3679 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
3682 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3684 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3690 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3691 "invalid config; all top-level vdevs must "
3692 "have the same sector size and not be raidz."));
3693 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3698 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3699 "Mount encrypted datasets to replay logs."));
3701 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3702 "Pool busy; removal may already be in progress"));
3704 (void) zfs_error(hdl
, EZFS_BUSY
, msg
);
3709 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3710 "Mount encrypted datasets to replay logs."));
3711 (void) zfs_error(hdl
, EZFS_BUSY
, msg
);
3713 (void) zpool_standard_error(hdl
, errno
, msg
);
3718 (void) zpool_standard_error(hdl
, errno
, msg
);
3724 zpool_vdev_remove_cancel(zpool_handle_t
*zhp
)
3728 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3730 (void) snprintf(msg
, sizeof (msg
),
3731 dgettext(TEXT_DOMAIN
, "cannot cancel removal"));
3733 bzero(&zc
, sizeof (zc
));
3734 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3737 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3740 return (zpool_standard_error(hdl
, errno
, msg
));
3744 zpool_vdev_indirect_size(zpool_handle_t
*zhp
, const char *path
,
3749 boolean_t avail_spare
, l2cache
, islog
;
3750 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3752 (void) snprintf(msg
, sizeof (msg
),
3753 dgettext(TEXT_DOMAIN
, "cannot determine indirect size of %s"),
3756 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3758 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3760 if (avail_spare
|| l2cache
|| islog
) {
3765 if (nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_INDIRECT_SIZE
, sizep
) != 0) {
3766 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3767 "indirect size not available"));
3768 return (zfs_error(hdl
, EINVAL
, msg
));
3774 * Clear the errors for the pool, or the particular device if specified.
3777 zpool_clear(zpool_handle_t
*zhp
, const char *path
, nvlist_t
*rewindnvl
)
3779 zfs_cmd_t zc
= {"\0"};
3782 zpool_load_policy_t policy
;
3783 boolean_t avail_spare
, l2cache
;
3784 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3785 nvlist_t
*nvi
= NULL
;
3789 (void) snprintf(msg
, sizeof (msg
),
3790 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3793 (void) snprintf(msg
, sizeof (msg
),
3794 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3797 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3799 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
,
3800 &l2cache
, NULL
)) == NULL
)
3801 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3804 * Don't allow error clearing for hot spares. Do allow
3805 * error clearing for l2cache devices.
3808 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3810 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
,
3814 zpool_get_load_policy(rewindnvl
, &policy
);
3815 zc
.zc_cookie
= policy
.zlp_rewind
;
3817 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zhp
->zpool_config_size
* 2) != 0)
3820 if (zcmd_write_src_nvlist(hdl
, &zc
, rewindnvl
) != 0)
3823 while ((error
= zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
)) != 0 &&
3825 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
3826 zcmd_free_nvlists(&zc
);
3831 if (!error
|| ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) &&
3832 errno
!= EPERM
&& errno
!= EACCES
)) {
3833 if (policy
.zlp_rewind
&
3834 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
3835 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nvi
);
3836 zpool_rewind_exclaim(hdl
, zc
.zc_name
,
3837 ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) != 0),
3841 zcmd_free_nvlists(&zc
);
3845 zcmd_free_nvlists(&zc
);
3846 return (zpool_standard_error(hdl
, errno
, msg
));
3850 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3853 zpool_vdev_clear(zpool_handle_t
*zhp
, uint64_t guid
)
3855 zfs_cmd_t zc
= {"\0"};
3857 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3859 (void) snprintf(msg
, sizeof (msg
),
3860 dgettext(TEXT_DOMAIN
, "cannot clear errors for %llx"),
3861 (u_longlong_t
)guid
);
3863 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3865 zc
.zc_cookie
= ZPOOL_NO_REWIND
;
3867 if (zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
) == 0)
3870 return (zpool_standard_error(hdl
, errno
, msg
));
3874 * Change the GUID for a pool.
3877 zpool_reguid(zpool_handle_t
*zhp
)
3880 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3881 zfs_cmd_t zc
= {"\0"};
3883 (void) snprintf(msg
, sizeof (msg
),
3884 dgettext(TEXT_DOMAIN
, "cannot reguid '%s'"), zhp
->zpool_name
);
3886 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3887 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REGUID
, &zc
) == 0)
3890 return (zpool_standard_error(hdl
, errno
, msg
));
3897 zpool_reopen_one(zpool_handle_t
*zhp
, void *data
)
3899 libzfs_handle_t
*hdl
= zpool_get_handle(zhp
);
3900 const char *pool_name
= zpool_get_name(zhp
);
3901 boolean_t
*scrub_restart
= data
;
3904 error
= lzc_reopen(pool_name
, *scrub_restart
);
3906 return (zpool_standard_error_fmt(hdl
, error
,
3907 dgettext(TEXT_DOMAIN
, "cannot reopen '%s'"), pool_name
));
3913 /* call into libzfs_core to execute the sync IOCTL per pool */
3915 zpool_sync_one(zpool_handle_t
*zhp
, void *data
)
3918 libzfs_handle_t
*hdl
= zpool_get_handle(zhp
);
3919 const char *pool_name
= zpool_get_name(zhp
);
3920 boolean_t
*force
= data
;
3921 nvlist_t
*innvl
= fnvlist_alloc();
3923 fnvlist_add_boolean_value(innvl
, "force", *force
);
3924 if ((ret
= lzc_sync(pool_name
, innvl
, NULL
)) != 0) {
3926 return (zpool_standard_error_fmt(hdl
, ret
,
3927 dgettext(TEXT_DOMAIN
, "sync '%s' failed"), pool_name
));
3934 #define PATH_BUF_LEN 64
3937 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3938 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3939 * We also check if this is a whole disk, in which case we strip off the
3940 * trailing 's0' slice name.
3942 * This routine is also responsible for identifying when disks have been
3943 * reconfigured in a new location. The kernel will have opened the device by
3944 * devid, but the path will still refer to the old location. To catch this, we
3945 * first do a path -> devid translation (which is fast for the common case). If
3946 * the devid matches, we're done. If not, we do a reverse devid -> path
3947 * translation and issue the appropriate ioctl() to update the path of the vdev.
3948 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3952 zpool_vdev_name(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, nvlist_t
*nv
,
3955 char *path
, *type
, *env
;
3957 char buf
[PATH_BUF_LEN
];
3958 char tmpbuf
[PATH_BUF_LEN
];
3961 * vdev_name will be "root"/"root-0" for the root vdev, but it is the
3962 * zpool name that will be displayed to the user.
3964 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
3965 if (zhp
!= NULL
&& strcmp(type
, "root") == 0)
3966 return (zfs_strdup(hdl
, zpool_get_name(zhp
)));
3968 env
= getenv("ZPOOL_VDEV_NAME_PATH");
3969 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3970 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3971 name_flags
|= VDEV_NAME_PATH
;
3973 env
= getenv("ZPOOL_VDEV_NAME_GUID");
3974 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3975 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3976 name_flags
|= VDEV_NAME_GUID
;
3978 env
= getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
3979 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3980 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3981 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
3983 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
, &value
) == 0 ||
3984 name_flags
& VDEV_NAME_GUID
) {
3985 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
);
3986 (void) snprintf(buf
, sizeof (buf
), "%llu", (u_longlong_t
)value
);
3988 } else if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
3989 if (name_flags
& VDEV_NAME_FOLLOW_LINKS
) {
3990 char *rp
= realpath(path
, NULL
);
3992 strlcpy(buf
, rp
, sizeof (buf
));
3999 * For a block device only use the name.
4001 if ((strcmp(type
, VDEV_TYPE_DISK
) == 0) &&
4002 !(name_flags
& VDEV_NAME_PATH
)) {
4003 path
= zfs_strip_path(path
);
4007 * Remove the partition from the path if this is a whole disk.
4009 if (strcmp(type
, VDEV_TYPE_DRAID_SPARE
) != 0 &&
4010 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
, &value
)
4011 == 0 && value
&& !(name_flags
& VDEV_NAME_PATH
)) {
4012 return (zfs_strip_partition(path
));
4018 * If it's a raidz device, we need to stick in the parity level.
4020 if (strcmp(path
, VDEV_TYPE_RAIDZ
) == 0) {
4021 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
,
4023 (void) snprintf(buf
, sizeof (buf
), "%s%llu", path
,
4024 (u_longlong_t
)value
);
4029 * If it's a dRAID device, we add parity, groups, and spares.
4031 if (strcmp(path
, VDEV_TYPE_DRAID
) == 0) {
4032 uint64_t ndata
, nparity
, nspares
;
4036 verify(nvlist_lookup_nvlist_array(nv
,
4037 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) == 0);
4038 verify(nvlist_lookup_uint64(nv
,
4039 ZPOOL_CONFIG_NPARITY
, &nparity
) == 0);
4040 verify(nvlist_lookup_uint64(nv
,
4041 ZPOOL_CONFIG_DRAID_NDATA
, &ndata
) == 0);
4042 verify(nvlist_lookup_uint64(nv
,
4043 ZPOOL_CONFIG_DRAID_NSPARES
, &nspares
) == 0);
4045 path
= zpool_draid_name(buf
, sizeof (buf
), ndata
,
4046 nparity
, nspares
, children
);
4050 * We identify each top-level vdev by using a <type-id>
4051 * naming convention.
4053 if (name_flags
& VDEV_NAME_TYPE_ID
) {
4055 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
4057 (void) snprintf(tmpbuf
, sizeof (tmpbuf
), "%s-%llu",
4058 path
, (u_longlong_t
)id
);
4063 return (zfs_strdup(hdl
, path
));
4067 zbookmark_mem_compare(const void *a
, const void *b
)
4069 return (memcmp(a
, b
, sizeof (zbookmark_phys_t
)));
4073 * Retrieve the persistent error log, uniquify the members, and return to the
4077 zpool_get_errlog(zpool_handle_t
*zhp
, nvlist_t
**nverrlistp
)
4079 zfs_cmd_t zc
= {"\0"};
4080 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4082 zbookmark_phys_t
*zb
= NULL
;
4086 * Retrieve the raw error list from the kernel. If the number of errors
4087 * has increased, allocate more space and continue until we get the
4090 verify(nvlist_lookup_uint64(zhp
->zpool_config
, ZPOOL_CONFIG_ERRCOUNT
,
4094 zc
.zc_nvlist_dst
= (uintptr_t)zfs_alloc(zhp
->zpool_hdl
,
4095 count
* sizeof (zbookmark_phys_t
));
4096 zc
.zc_nvlist_dst_size
= count
;
4097 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
4099 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_ERROR_LOG
,
4101 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
4102 if (errno
== ENOMEM
) {
4105 count
= zc
.zc_nvlist_dst_size
;
4106 dst
= zfs_alloc(zhp
->zpool_hdl
, count
*
4107 sizeof (zbookmark_phys_t
));
4108 zc
.zc_nvlist_dst
= (uintptr_t)dst
;
4110 return (zpool_standard_error_fmt(hdl
, errno
,
4111 dgettext(TEXT_DOMAIN
, "errors: List of "
4112 "errors unavailable")));
4120 * Sort the resulting bookmarks. This is a little confusing due to the
4121 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
4122 * to first, and 'zc_nvlist_dst_size' indicates the number of bookmarks
4123 * _not_ copied as part of the process. So we point the start of our
4124 * array appropriate and decrement the total number of elements.
4126 zb
= ((zbookmark_phys_t
*)(uintptr_t)zc
.zc_nvlist_dst
) +
4127 zc
.zc_nvlist_dst_size
;
4128 count
-= zc
.zc_nvlist_dst_size
;
4130 qsort(zb
, count
, sizeof (zbookmark_phys_t
), zbookmark_mem_compare
);
4132 verify(nvlist_alloc(nverrlistp
, 0, KM_SLEEP
) == 0);
4135 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
4137 for (i
= 0; i
< count
; i
++) {
4140 /* ignoring zb_blkid and zb_level for now */
4141 if (i
> 0 && zb
[i
-1].zb_objset
== zb
[i
].zb_objset
&&
4142 zb
[i
-1].zb_object
== zb
[i
].zb_object
)
4145 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) != 0)
4147 if (nvlist_add_uint64(nv
, ZPOOL_ERR_DATASET
,
4148 zb
[i
].zb_objset
) != 0) {
4152 if (nvlist_add_uint64(nv
, ZPOOL_ERR_OBJECT
,
4153 zb
[i
].zb_object
) != 0) {
4157 if (nvlist_add_nvlist(*nverrlistp
, "ejk", nv
) != 0) {
4164 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
4168 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
4169 return (no_memory(zhp
->zpool_hdl
));
4173 * Upgrade a ZFS pool to the latest on-disk version.
4176 zpool_upgrade(zpool_handle_t
*zhp
, uint64_t new_version
)
4178 zfs_cmd_t zc
= {"\0"};
4179 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4181 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
4182 zc
.zc_cookie
= new_version
;
4184 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_UPGRADE
, &zc
) != 0)
4185 return (zpool_standard_error_fmt(hdl
, errno
,
4186 dgettext(TEXT_DOMAIN
, "cannot upgrade '%s'"),
4192 zfs_save_arguments(int argc
, char **argv
, char *string
, int len
)
4196 (void) strlcpy(string
, basename(argv
[0]), len
);
4197 for (i
= 1; i
< argc
; i
++) {
4198 (void) strlcat(string
, " ", len
);
4199 (void) strlcat(string
, argv
[i
], len
);
4204 zpool_log_history(libzfs_handle_t
*hdl
, const char *message
)
4206 zfs_cmd_t zc
= {"\0"};
4210 args
= fnvlist_alloc();
4211 fnvlist_add_string(args
, "message", message
);
4212 err
= zcmd_write_src_nvlist(hdl
, &zc
, args
);
4214 err
= zfs_ioctl(hdl
, ZFS_IOC_LOG_HISTORY
, &zc
);
4216 zcmd_free_nvlists(&zc
);
4221 * Perform ioctl to get some command history of a pool.
4223 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
4224 * logical offset of the history buffer to start reading from.
4226 * Upon return, 'off' is the next logical offset to read from and
4227 * 'len' is the actual amount of bytes read into 'buf'.
4230 get_history(zpool_handle_t
*zhp
, char *buf
, uint64_t *off
, uint64_t *len
)
4232 zfs_cmd_t zc
= {"\0"};
4233 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4235 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4237 zc
.zc_history
= (uint64_t)(uintptr_t)buf
;
4238 zc
.zc_history_len
= *len
;
4239 zc
.zc_history_offset
= *off
;
4241 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_GET_HISTORY
, &zc
) != 0) {
4244 return (zfs_error_fmt(hdl
, EZFS_PERM
,
4245 dgettext(TEXT_DOMAIN
,
4246 "cannot show history for pool '%s'"),
4249 return (zfs_error_fmt(hdl
, EZFS_NOHISTORY
,
4250 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
4251 "'%s'"), zhp
->zpool_name
));
4253 return (zfs_error_fmt(hdl
, EZFS_BADVERSION
,
4254 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
4255 "'%s', pool must be upgraded"), zhp
->zpool_name
));
4257 return (zpool_standard_error_fmt(hdl
, errno
,
4258 dgettext(TEXT_DOMAIN
,
4259 "cannot get history for '%s'"), zhp
->zpool_name
));
4263 *len
= zc
.zc_history_len
;
4264 *off
= zc
.zc_history_offset
;
4270 * Retrieve the command history of a pool.
4273 zpool_get_history(zpool_handle_t
*zhp
, nvlist_t
**nvhisp
, uint64_t *off
,
4277 int buflen
= 128 * 1024;
4278 nvlist_t
**records
= NULL
;
4279 uint_t numrecords
= 0;
4281 uint64_t start
= *off
;
4283 buf
= malloc(buflen
);
4286 /* process about 1MB a time */
4287 while (*off
- start
< 1024 * 1024) {
4288 uint64_t bytes_read
= buflen
;
4291 if ((err
= get_history(zhp
, buf
, off
, &bytes_read
)) != 0)
4294 /* if nothing else was read in, we're at EOF, just return */
4300 if ((err
= zpool_history_unpack(buf
, bytes_read
,
4301 &leftover
, &records
, &numrecords
)) != 0)
4304 if (leftover
== bytes_read
) {
4306 * no progress made, because buffer is not big enough
4307 * to hold this record; resize and retry.
4311 buf
= malloc(buflen
);
4320 verify(nvlist_alloc(nvhisp
, NV_UNIQUE_NAME
, 0) == 0);
4321 verify(nvlist_add_nvlist_array(*nvhisp
, ZPOOL_HIST_RECORD
,
4322 records
, numrecords
) == 0);
4324 for (i
= 0; i
< numrecords
; i
++)
4325 nvlist_free(records
[i
]);
4332 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
4333 * If there is a new event available 'nvp' will contain a newly allocated
4334 * nvlist and 'dropped' will be set to the number of missed events since
4335 * the last call to this function. When 'nvp' is set to NULL it indicates
4336 * no new events are available. In either case the function returns 0 and
4337 * it is up to the caller to free 'nvp'. In the case of a fatal error the
4338 * function will return a non-zero value. When the function is called in
4339 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
4340 * it will not return until a new event is available.
4343 zpool_events_next(libzfs_handle_t
*hdl
, nvlist_t
**nvp
,
4344 int *dropped
, unsigned flags
, int zevent_fd
)
4346 zfs_cmd_t zc
= {"\0"};
4351 zc
.zc_cleanup_fd
= zevent_fd
;
4353 if (flags
& ZEVENT_NONBLOCK
)
4354 zc
.zc_guid
= ZEVENT_NONBLOCK
;
4356 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, ZEVENT_SIZE
) != 0)
4360 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_NEXT
, &zc
) != 0) {
4363 error
= zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
4364 dgettext(TEXT_DOMAIN
, "zfs shutdown"));
4367 /* Blocking error case should not occur */
4368 if (!(flags
& ZEVENT_NONBLOCK
))
4369 error
= zpool_standard_error_fmt(hdl
, errno
,
4370 dgettext(TEXT_DOMAIN
, "cannot get event"));
4374 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
4375 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
4376 dgettext(TEXT_DOMAIN
, "cannot get event"));
4382 error
= zpool_standard_error_fmt(hdl
, errno
,
4383 dgettext(TEXT_DOMAIN
, "cannot get event"));
4388 error
= zcmd_read_dst_nvlist(hdl
, &zc
, nvp
);
4392 *dropped
= (int)zc
.zc_cookie
;
4394 zcmd_free_nvlists(&zc
);
4403 zpool_events_clear(libzfs_handle_t
*hdl
, int *count
)
4405 zfs_cmd_t zc
= {"\0"};
4408 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
4409 "cannot clear events"));
4411 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_CLEAR
, &zc
) != 0)
4412 return (zpool_standard_error_fmt(hdl
, errno
, msg
));
4415 *count
= (int)zc
.zc_cookie
; /* # of events cleared */
4421 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4422 * the passed zevent_fd file handle. On success zero is returned,
4423 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4426 zpool_events_seek(libzfs_handle_t
*hdl
, uint64_t eid
, int zevent_fd
)
4428 zfs_cmd_t zc
= {"\0"};
4432 zc
.zc_cleanup_fd
= zevent_fd
;
4434 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_SEEK
, &zc
) != 0) {
4437 error
= zfs_error_fmt(hdl
, EZFS_NOENT
,
4438 dgettext(TEXT_DOMAIN
, "cannot get event"));
4442 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
4443 dgettext(TEXT_DOMAIN
, "cannot get event"));
4447 error
= zpool_standard_error_fmt(hdl
, errno
,
4448 dgettext(TEXT_DOMAIN
, "cannot get event"));
4457 zpool_obj_to_path_impl(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4458 char *pathname
, size_t len
, boolean_t always_unmounted
)
4460 zfs_cmd_t zc
= {"\0"};
4461 boolean_t mounted
= B_FALSE
;
4462 char *mntpnt
= NULL
;
4463 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
4466 /* special case for the MOS */
4467 (void) snprintf(pathname
, len
, "<metadata>:<0x%llx>",
4472 /* get the dataset's name */
4473 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4475 if (zfs_ioctl(zhp
->zpool_hdl
,
4476 ZFS_IOC_DSOBJ_TO_DSNAME
, &zc
) != 0) {
4477 /* just write out a path of two object numbers */
4478 (void) snprintf(pathname
, len
, "<0x%llx>:<0x%llx>",
4479 (longlong_t
)dsobj
, (longlong_t
)obj
);
4482 (void) strlcpy(dsname
, zc
.zc_value
, sizeof (dsname
));
4484 /* find out if the dataset is mounted */
4485 mounted
= !always_unmounted
&& is_mounted(zhp
->zpool_hdl
, dsname
,
4488 /* get the corrupted object's path */
4489 (void) strlcpy(zc
.zc_name
, dsname
, sizeof (zc
.zc_name
));
4491 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_OBJ_TO_PATH
,
4494 (void) snprintf(pathname
, len
, "%s%s", mntpnt
,
4497 (void) snprintf(pathname
, len
, "%s:%s",
4498 dsname
, zc
.zc_value
);
4501 (void) snprintf(pathname
, len
, "%s:<0x%llx>", dsname
,
4508 zpool_obj_to_path(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4509 char *pathname
, size_t len
)
4511 zpool_obj_to_path_impl(zhp
, dsobj
, obj
, pathname
, len
, B_FALSE
);
4515 zpool_obj_to_path_ds(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4516 char *pathname
, size_t len
)
4518 zpool_obj_to_path_impl(zhp
, dsobj
, obj
, pathname
, len
, B_TRUE
);
4521 * Wait while the specified activity is in progress in the pool.
4524 zpool_wait(zpool_handle_t
*zhp
, zpool_wait_activity_t activity
)
4528 int error
= zpool_wait_status(zhp
, activity
, &missing
, NULL
);
4531 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, ENOENT
,
4532 dgettext(TEXT_DOMAIN
, "error waiting in pool '%s'"),
4541 * Wait for the given activity and return the status of the wait (whether or not
4542 * any waiting was done) in the 'waited' parameter. Non-existent pools are
4543 * reported via the 'missing' parameter, rather than by printing an error
4544 * message. This is convenient when this function is called in a loop over a
4545 * long period of time (as it is, for example, by zpool's wait cmd). In that
4546 * scenario, a pool being exported or destroyed should be considered a normal
4547 * event, so we don't want to print an error when we find that the pool doesn't
4551 zpool_wait_status(zpool_handle_t
*zhp
, zpool_wait_activity_t activity
,
4552 boolean_t
*missing
, boolean_t
*waited
)
4554 int error
= lzc_wait(zhp
->zpool_name
, activity
, waited
);
4555 *missing
= (error
== ENOENT
);
4560 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
4561 dgettext(TEXT_DOMAIN
, "error waiting in pool '%s'"),
4569 zpool_set_bootenv(zpool_handle_t
*zhp
, const nvlist_t
*envmap
)
4571 int error
= lzc_set_bootenv(zhp
->zpool_name
, envmap
);
4573 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
4574 dgettext(TEXT_DOMAIN
,
4575 "error setting bootenv in pool '%s'"), zhp
->zpool_name
);
4582 zpool_get_bootenv(zpool_handle_t
*zhp
, nvlist_t
**nvlp
)
4588 error
= lzc_get_bootenv(zhp
->zpool_name
, &nvl
);
4590 (void) zpool_standard_error_fmt(zhp
->zpool_hdl
, error
,
4591 dgettext(TEXT_DOMAIN
,
4592 "error getting bootenv in pool '%s'"), zhp
->zpool_name
);