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, 2018 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>
43 #include <sys/efi_partition.h>
44 #include <sys/systeminfo.h>
46 #include <sys/zfs_ioctl.h>
47 #include <sys/vdev_disk.h>
51 #include "zfs_namecheck.h"
53 #include "libzfs_impl.h"
54 #include "zfs_comutil.h"
55 #include "zfeature_common.h"
57 static int read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
);
58 static boolean_t
zpool_vdev_is_interior(const char *name
);
60 typedef struct prop_flags
{
61 int create
:1; /* Validate property on creation */
62 int import
:1; /* Validate property on import */
66 * ====================================================================
67 * zpool property functions
68 * ====================================================================
72 zpool_get_all_props(zpool_handle_t
*zhp
)
74 zfs_cmd_t zc
= {"\0"};
75 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
77 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
79 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
82 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_PROPS
, &zc
) != 0) {
83 if (errno
== ENOMEM
) {
84 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
85 zcmd_free_nvlists(&zc
);
89 zcmd_free_nvlists(&zc
);
94 if (zcmd_read_dst_nvlist(hdl
, &zc
, &zhp
->zpool_props
) != 0) {
95 zcmd_free_nvlists(&zc
);
99 zcmd_free_nvlists(&zc
);
105 zpool_props_refresh(zpool_handle_t
*zhp
)
109 old_props
= zhp
->zpool_props
;
111 if (zpool_get_all_props(zhp
) != 0)
114 nvlist_free(old_props
);
119 zpool_get_prop_string(zpool_handle_t
*zhp
, zpool_prop_t prop
,
125 zprop_source_t source
;
127 nvl
= zhp
->zpool_props
;
128 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
129 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &ival
) == 0);
131 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
133 source
= ZPROP_SRC_DEFAULT
;
134 if ((value
= (char *)zpool_prop_default_string(prop
)) == NULL
)
145 zpool_get_prop_int(zpool_handle_t
*zhp
, zpool_prop_t prop
, zprop_source_t
*src
)
149 zprop_source_t source
;
151 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
)) {
153 * zpool_get_all_props() has most likely failed because
154 * the pool is faulted, but if all we need is the top level
155 * vdev's guid then get it from the zhp config nvlist.
157 if ((prop
== ZPOOL_PROP_GUID
) &&
158 (nvlist_lookup_nvlist(zhp
->zpool_config
,
159 ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0) &&
160 (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
)
164 return (zpool_prop_default_numeric(prop
));
167 nvl
= zhp
->zpool_props
;
168 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
169 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &value
) == 0);
171 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
173 source
= ZPROP_SRC_DEFAULT
;
174 value
= zpool_prop_default_numeric(prop
);
184 * Map VDEV STATE to printed strings.
187 zpool_state_to_name(vdev_state_t state
, vdev_aux_t aux
)
190 case VDEV_STATE_CLOSED
:
191 case VDEV_STATE_OFFLINE
:
192 return (gettext("OFFLINE"));
193 case VDEV_STATE_REMOVED
:
194 return (gettext("REMOVED"));
195 case VDEV_STATE_CANT_OPEN
:
196 if (aux
== VDEV_AUX_CORRUPT_DATA
|| aux
== VDEV_AUX_BAD_LOG
)
197 return (gettext("FAULTED"));
198 else if (aux
== VDEV_AUX_SPLIT_POOL
)
199 return (gettext("SPLIT"));
201 return (gettext("UNAVAIL"));
202 case VDEV_STATE_FAULTED
:
203 return (gettext("FAULTED"));
204 case VDEV_STATE_DEGRADED
:
205 return (gettext("DEGRADED"));
206 case VDEV_STATE_HEALTHY
:
207 return (gettext("ONLINE"));
213 return (gettext("UNKNOWN"));
217 * Map POOL STATE to printed strings.
220 zpool_pool_state_to_name(pool_state_t state
)
225 case POOL_STATE_ACTIVE
:
226 return (gettext("ACTIVE"));
227 case POOL_STATE_EXPORTED
:
228 return (gettext("EXPORTED"));
229 case POOL_STATE_DESTROYED
:
230 return (gettext("DESTROYED"));
231 case POOL_STATE_SPARE
:
232 return (gettext("SPARE"));
233 case POOL_STATE_L2CACHE
:
234 return (gettext("L2CACHE"));
235 case POOL_STATE_UNINITIALIZED
:
236 return (gettext("UNINITIALIZED"));
237 case POOL_STATE_UNAVAIL
:
238 return (gettext("UNAVAIL"));
239 case POOL_STATE_POTENTIALLY_ACTIVE
:
240 return (gettext("POTENTIALLY_ACTIVE"));
243 return (gettext("UNKNOWN"));
247 * Given a pool handle, return the pool health string ("ONLINE", "DEGRADED",
251 zpool_get_state_str(zpool_handle_t
*zhp
)
253 zpool_errata_t errata
;
254 zpool_status_t status
;
260 status
= zpool_get_status(zhp
, NULL
, &errata
);
262 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
263 str
= gettext("FAULTED");
264 } else if (status
== ZPOOL_STATUS_IO_FAILURE_WAIT
||
265 status
== ZPOOL_STATUS_IO_FAILURE_MMP
) {
266 str
= gettext("SUSPENDED");
268 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
269 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
270 verify(nvlist_lookup_uint64_array(nvroot
,
271 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
273 str
= zpool_state_to_name(vs
->vs_state
, vs
->vs_aux
);
279 * Get a zpool property value for 'prop' and return the value in
280 * a pre-allocated buffer.
283 zpool_get_prop(zpool_handle_t
*zhp
, zpool_prop_t prop
, char *buf
,
284 size_t len
, zprop_source_t
*srctype
, boolean_t literal
)
288 zprop_source_t src
= ZPROP_SRC_NONE
;
290 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
292 case ZPOOL_PROP_NAME
:
293 (void) strlcpy(buf
, zpool_get_name(zhp
), len
);
296 case ZPOOL_PROP_HEALTH
:
297 (void) strlcpy(buf
, zpool_get_state_str(zhp
), len
);
300 case ZPOOL_PROP_GUID
:
301 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
302 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
305 case ZPOOL_PROP_ALTROOT
:
306 case ZPOOL_PROP_CACHEFILE
:
307 case ZPOOL_PROP_COMMENT
:
308 if (zhp
->zpool_props
!= NULL
||
309 zpool_get_all_props(zhp
) == 0) {
311 zpool_get_prop_string(zhp
, prop
, &src
),
317 (void) strlcpy(buf
, "-", len
);
326 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
) &&
327 prop
!= ZPOOL_PROP_NAME
)
330 switch (zpool_prop_get_type(prop
)) {
331 case PROP_TYPE_STRING
:
332 (void) strlcpy(buf
, zpool_get_prop_string(zhp
, prop
, &src
),
336 case PROP_TYPE_NUMBER
:
337 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
340 case ZPOOL_PROP_SIZE
:
341 case ZPOOL_PROP_ALLOCATED
:
342 case ZPOOL_PROP_FREE
:
343 case ZPOOL_PROP_FREEING
:
344 case ZPOOL_PROP_LEAKED
:
345 case ZPOOL_PROP_ASHIFT
:
347 (void) snprintf(buf
, len
, "%llu",
348 (u_longlong_t
)intval
);
350 (void) zfs_nicenum(intval
, buf
, len
);
353 case ZPOOL_PROP_EXPANDSZ
:
354 case ZPOOL_PROP_CHECKPOINT
:
356 (void) strlcpy(buf
, "-", len
);
357 } else if (literal
) {
358 (void) snprintf(buf
, len
, "%llu",
359 (u_longlong_t
)intval
);
361 (void) zfs_nicebytes(intval
, buf
, len
);
365 case ZPOOL_PROP_CAPACITY
:
367 (void) snprintf(buf
, len
, "%llu",
368 (u_longlong_t
)intval
);
370 (void) snprintf(buf
, len
, "%llu%%",
371 (u_longlong_t
)intval
);
375 case ZPOOL_PROP_FRAGMENTATION
:
376 if (intval
== UINT64_MAX
) {
377 (void) strlcpy(buf
, "-", len
);
378 } else if (literal
) {
379 (void) snprintf(buf
, len
, "%llu",
380 (u_longlong_t
)intval
);
382 (void) snprintf(buf
, len
, "%llu%%",
383 (u_longlong_t
)intval
);
387 case ZPOOL_PROP_DEDUPRATIO
:
389 (void) snprintf(buf
, len
, "%llu.%02llu",
390 (u_longlong_t
)(intval
/ 100),
391 (u_longlong_t
)(intval
% 100));
393 (void) snprintf(buf
, len
, "%llu.%02llux",
394 (u_longlong_t
)(intval
/ 100),
395 (u_longlong_t
)(intval
% 100));
398 case ZPOOL_PROP_HEALTH
:
399 (void) strlcpy(buf
, zpool_get_state_str(zhp
), len
);
401 case ZPOOL_PROP_VERSION
:
402 if (intval
>= SPA_VERSION_FEATURES
) {
403 (void) snprintf(buf
, len
, "-");
408 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
412 case PROP_TYPE_INDEX
:
413 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
414 if (zpool_prop_index_to_string(prop
, intval
, &strval
)
417 (void) strlcpy(buf
, strval
, len
);
431 * Check if the bootfs name has the same pool name as it is set to.
432 * Assuming bootfs is a valid dataset name.
435 bootfs_name_valid(const char *pool
, char *bootfs
)
437 int len
= strlen(pool
);
438 if (bootfs
[0] == '\0')
441 if (!zfs_name_valid(bootfs
, ZFS_TYPE_FILESYSTEM
|ZFS_TYPE_SNAPSHOT
))
444 if (strncmp(pool
, bootfs
, len
) == 0 &&
445 (bootfs
[len
] == '/' || bootfs
[len
] == '\0'))
452 zpool_is_bootable(zpool_handle_t
*zhp
)
454 char bootfs
[ZFS_MAX_DATASET_NAME_LEN
];
456 return (zpool_get_prop(zhp
, ZPOOL_PROP_BOOTFS
, bootfs
,
457 sizeof (bootfs
), NULL
, B_FALSE
) == 0 && strncmp(bootfs
, "-",
458 sizeof (bootfs
)) != 0);
463 * Given an nvlist of zpool properties to be set, validate that they are
464 * correct, and parse any numeric properties (index, boolean, etc) if they are
465 * specified as strings.
468 zpool_valid_proplist(libzfs_handle_t
*hdl
, const char *poolname
,
469 nvlist_t
*props
, uint64_t version
, prop_flags_t flags
, char *errbuf
)
477 struct stat64 statbuf
;
480 if (nvlist_alloc(&retprops
, NV_UNIQUE_NAME
, 0) != 0) {
481 (void) no_memory(hdl
);
486 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
487 const char *propname
= nvpair_name(elem
);
489 prop
= zpool_name_to_prop(propname
);
490 if (prop
== ZPOOL_PROP_INVAL
&& zpool_prop_feature(propname
)) {
492 char *fname
= strchr(propname
, '@') + 1;
494 err
= zfeature_lookup_name(fname
, NULL
);
496 ASSERT3U(err
, ==, ENOENT
);
497 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
498 "invalid feature '%s'"), fname
);
499 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
503 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
504 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
505 "'%s' must be a string"), propname
);
506 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
510 (void) nvpair_value_string(elem
, &strval
);
511 if (strcmp(strval
, ZFS_FEATURE_ENABLED
) != 0 &&
512 strcmp(strval
, ZFS_FEATURE_DISABLED
) != 0) {
513 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
514 "property '%s' can only be set to "
515 "'enabled' or 'disabled'"), propname
);
516 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
521 strcmp(strval
, ZFS_FEATURE_DISABLED
) == 0) {
522 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
523 "property '%s' can only be set to "
524 "'disabled' at creation time"), propname
);
525 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
529 if (nvlist_add_uint64(retprops
, propname
, 0) != 0) {
530 (void) no_memory(hdl
);
537 * Make sure this property is valid and applies to this type.
539 if (prop
== ZPOOL_PROP_INVAL
) {
540 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
541 "invalid property '%s'"), propname
);
542 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
546 if (zpool_prop_readonly(prop
)) {
547 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
548 "is readonly"), propname
);
549 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
553 if (!flags
.create
&& zpool_prop_setonce(prop
)) {
554 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
555 "property '%s' can only be set at "
556 "creation time"), propname
);
557 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
561 if (zprop_parse_value(hdl
, elem
, prop
, ZFS_TYPE_POOL
, retprops
,
562 &strval
, &intval
, errbuf
) != 0)
566 * Perform additional checking for specific properties.
569 case ZPOOL_PROP_VERSION
:
570 if (intval
< version
||
571 !SPA_VERSION_IS_SUPPORTED(intval
)) {
572 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
573 "property '%s' number %d is invalid."),
575 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
580 case ZPOOL_PROP_ASHIFT
:
582 (intval
< ASHIFT_MIN
|| intval
> ASHIFT_MAX
)) {
583 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
584 "property '%s' number %d is invalid, only "
585 "values between %" PRId32
" and "
586 "%" PRId32
" are allowed."),
587 propname
, intval
, ASHIFT_MIN
, ASHIFT_MAX
);
588 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
593 case ZPOOL_PROP_BOOTFS
:
594 if (flags
.create
|| flags
.import
) {
595 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
596 "property '%s' cannot be set at creation "
597 "or import time"), propname
);
598 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
602 if (version
< SPA_VERSION_BOOTFS
) {
603 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
604 "pool must be upgraded to support "
605 "'%s' property"), propname
);
606 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
611 * bootfs property value has to be a dataset name and
612 * the dataset has to be in the same pool as it sets to.
614 if (!bootfs_name_valid(poolname
, strval
)) {
615 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
616 "is an invalid name"), strval
);
617 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
621 if ((zhp
= zpool_open_canfail(hdl
, poolname
)) == NULL
) {
622 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
623 "could not open pool '%s'"), poolname
);
624 (void) zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
);
630 case ZPOOL_PROP_ALTROOT
:
631 if (!flags
.create
&& !flags
.import
) {
632 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
633 "property '%s' can only be set during pool "
634 "creation or import"), propname
);
635 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
639 if (strval
[0] != '/') {
640 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
641 "bad alternate root '%s'"), strval
);
642 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
647 case ZPOOL_PROP_CACHEFILE
:
648 if (strval
[0] == '\0')
651 if (strcmp(strval
, "none") == 0)
654 if (strval
[0] != '/') {
655 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
656 "property '%s' must be empty, an "
657 "absolute path, or 'none'"), propname
);
658 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
662 slash
= strrchr(strval
, '/');
664 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
665 strcmp(slash
, "/..") == 0) {
666 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
667 "'%s' is not a valid file"), strval
);
668 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
674 if (strval
[0] != '\0' &&
675 (stat64(strval
, &statbuf
) != 0 ||
676 !S_ISDIR(statbuf
.st_mode
))) {
677 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
678 "'%s' is not a valid directory"),
680 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
687 case ZPOOL_PROP_COMMENT
:
688 for (check
= strval
; *check
!= '\0'; check
++) {
689 if (!isprint(*check
)) {
691 dgettext(TEXT_DOMAIN
,
692 "comment may only have printable "
694 (void) zfs_error(hdl
, EZFS_BADPROP
,
699 if (strlen(strval
) > ZPROP_MAX_COMMENT
) {
700 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
701 "comment must not exceed %d characters"),
703 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
707 case ZPOOL_PROP_READONLY
:
709 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
710 "property '%s' can only be set at "
711 "import time"), propname
);
712 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
716 case ZPOOL_PROP_MULTIHOST
:
717 if (get_system_hostid() == 0) {
718 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
719 "requires a non-zero system hostid"));
720 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
724 case ZPOOL_PROP_DEDUPDITTO
:
725 if (intval
< ZIO_DEDUPDITTO_MIN
&& intval
!= 0) {
726 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
727 "property '%s' value %d is invalid; only "
728 "values of 0 or >= %" PRId32
" are allowed "
729 "for this property."),
730 propname
, intval
, ZIO_DEDUPDITTO_MIN
);
731 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
737 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
738 "property '%s'(%d) not defined"), propname
, prop
);
745 nvlist_free(retprops
);
750 * Set zpool property : propname=propval.
753 zpool_set_prop(zpool_handle_t
*zhp
, const char *propname
, const char *propval
)
755 zfs_cmd_t zc
= {"\0"};
758 nvlist_t
*nvl
= NULL
;
761 prop_flags_t flags
= { 0 };
763 (void) snprintf(errbuf
, sizeof (errbuf
),
764 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
767 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
768 return (no_memory(zhp
->zpool_hdl
));
770 if (nvlist_add_string(nvl
, propname
, propval
) != 0) {
772 return (no_memory(zhp
->zpool_hdl
));
775 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
776 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
777 zhp
->zpool_name
, nvl
, version
, flags
, errbuf
)) == NULL
) {
786 * Execute the corresponding ioctl() to set this property.
788 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
790 if (zcmd_write_src_nvlist(zhp
->zpool_hdl
, &zc
, nvl
) != 0) {
795 ret
= zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_SET_PROPS
, &zc
);
797 zcmd_free_nvlists(&zc
);
801 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
803 (void) zpool_props_refresh(zhp
);
809 zpool_expand_proplist(zpool_handle_t
*zhp
, zprop_list_t
**plp
)
811 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
813 char buf
[ZFS_MAXPROPLEN
];
814 nvlist_t
*features
= NULL
;
817 boolean_t firstexpand
= (NULL
== *plp
);
820 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_POOL
) != 0)
824 while (*last
!= NULL
)
825 last
= &(*last
)->pl_next
;
828 features
= zpool_get_features(zhp
);
830 if ((*plp
)->pl_all
&& firstexpand
) {
831 for (i
= 0; i
< SPA_FEATURES
; i
++) {
832 zprop_list_t
*entry
= zfs_alloc(hdl
,
833 sizeof (zprop_list_t
));
834 entry
->pl_prop
= ZPROP_INVAL
;
835 entry
->pl_user_prop
= zfs_asprintf(hdl
, "feature@%s",
836 spa_feature_table
[i
].fi_uname
);
837 entry
->pl_width
= strlen(entry
->pl_user_prop
);
838 entry
->pl_all
= B_TRUE
;
841 last
= &entry
->pl_next
;
845 /* add any unsupported features */
846 for (nvp
= nvlist_next_nvpair(features
, NULL
);
847 nvp
!= NULL
; nvp
= nvlist_next_nvpair(features
, nvp
)) {
852 if (zfeature_is_supported(nvpair_name(nvp
)))
855 propname
= zfs_asprintf(hdl
, "unsupported@%s",
859 * Before adding the property to the list make sure that no
860 * other pool already added the same property.
864 while (entry
!= NULL
) {
865 if (entry
->pl_user_prop
!= NULL
&&
866 strcmp(propname
, entry
->pl_user_prop
) == 0) {
870 entry
= entry
->pl_next
;
877 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
878 entry
->pl_prop
= ZPROP_INVAL
;
879 entry
->pl_user_prop
= propname
;
880 entry
->pl_width
= strlen(entry
->pl_user_prop
);
881 entry
->pl_all
= B_TRUE
;
884 last
= &entry
->pl_next
;
887 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
892 if (entry
->pl_prop
!= ZPROP_INVAL
&&
893 zpool_get_prop(zhp
, entry
->pl_prop
, buf
, sizeof (buf
),
894 NULL
, B_FALSE
) == 0) {
895 if (strlen(buf
) > entry
->pl_width
)
896 entry
->pl_width
= strlen(buf
);
904 * Get the state for the given feature on the given ZFS pool.
907 zpool_prop_get_feature(zpool_handle_t
*zhp
, const char *propname
, char *buf
,
911 boolean_t found
= B_FALSE
;
912 nvlist_t
*features
= zpool_get_features(zhp
);
914 const char *feature
= strchr(propname
, '@') + 1;
916 supported
= zpool_prop_feature(propname
);
917 ASSERT(supported
|| zpool_prop_unsupported(propname
));
920 * Convert from feature name to feature guid. This conversion is
921 * unnecessary for unsupported@... properties because they already
928 ret
= zfeature_lookup_name(feature
, &fid
);
930 (void) strlcpy(buf
, "-", len
);
933 feature
= spa_feature_table
[fid
].fi_guid
;
936 if (nvlist_lookup_uint64(features
, feature
, &refcount
) == 0)
941 (void) strlcpy(buf
, ZFS_FEATURE_DISABLED
, len
);
944 (void) strlcpy(buf
, ZFS_FEATURE_ENABLED
, len
);
946 (void) strlcpy(buf
, ZFS_FEATURE_ACTIVE
, len
);
951 (void) strcpy(buf
, ZFS_UNSUPPORTED_INACTIVE
);
953 (void) strcpy(buf
, ZFS_UNSUPPORTED_READONLY
);
956 (void) strlcpy(buf
, "-", len
);
965 * Validate the given pool name, optionally putting an extended error message in
969 zpool_name_valid(libzfs_handle_t
*hdl
, boolean_t isopen
, const char *pool
)
975 ret
= pool_namecheck(pool
, &why
, &what
);
978 * The rules for reserved pool names were extended at a later point.
979 * But we need to support users with existing pools that may now be
980 * invalid. So we only check for this expanded set of names during a
981 * create (or import), and only in userland.
983 if (ret
== 0 && !isopen
&&
984 (strncmp(pool
, "mirror", 6) == 0 ||
985 strncmp(pool
, "raidz", 5) == 0 ||
986 strncmp(pool
, "spare", 5) == 0 ||
987 strcmp(pool
, "log") == 0)) {
990 dgettext(TEXT_DOMAIN
, "name is reserved"));
998 case NAME_ERR_TOOLONG
:
1000 dgettext(TEXT_DOMAIN
, "name is too long"));
1003 case NAME_ERR_INVALCHAR
:
1005 dgettext(TEXT_DOMAIN
, "invalid character "
1006 "'%c' in pool name"), what
);
1009 case NAME_ERR_NOLETTER
:
1010 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1011 "name must begin with a letter"));
1014 case NAME_ERR_RESERVED
:
1015 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1016 "name is reserved"));
1019 case NAME_ERR_DISKLIKE
:
1020 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1021 "pool name is reserved"));
1024 case NAME_ERR_LEADING_SLASH
:
1025 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1026 "leading slash in name"));
1029 case NAME_ERR_EMPTY_COMPONENT
:
1030 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1031 "empty component in name"));
1034 case NAME_ERR_TRAILING_SLASH
:
1035 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1036 "trailing slash in name"));
1039 case NAME_ERR_MULTIPLE_DELIMITERS
:
1040 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1041 "multiple '@' and/or '#' delimiters in "
1045 case NAME_ERR_NO_AT
:
1046 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1047 "permission set is missing '@'"));
1051 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1052 "(%d) not defined"), why
);
1063 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1067 zpool_open_canfail(libzfs_handle_t
*hdl
, const char *pool
)
1069 zpool_handle_t
*zhp
;
1073 * Make sure the pool name is valid.
1075 if (!zpool_name_valid(hdl
, B_TRUE
, pool
)) {
1076 (void) zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1077 dgettext(TEXT_DOMAIN
, "cannot open '%s'"),
1082 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
1085 zhp
->zpool_hdl
= hdl
;
1086 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1088 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1094 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "no such pool"));
1095 (void) zfs_error_fmt(hdl
, EZFS_NOENT
,
1096 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), pool
);
1105 * Like the above, but silent on error. Used when iterating over pools (because
1106 * the configuration cache may be out of date).
1109 zpool_open_silent(libzfs_handle_t
*hdl
, const char *pool
, zpool_handle_t
**ret
)
1111 zpool_handle_t
*zhp
;
1114 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
1117 zhp
->zpool_hdl
= hdl
;
1118 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1120 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1136 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1140 zpool_open(libzfs_handle_t
*hdl
, const char *pool
)
1142 zpool_handle_t
*zhp
;
1144 if ((zhp
= zpool_open_canfail(hdl
, pool
)) == NULL
)
1147 if (zhp
->zpool_state
== POOL_STATE_UNAVAIL
) {
1148 (void) zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
1149 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), zhp
->zpool_name
);
1158 * Close the handle. Simply frees the memory associated with the handle.
1161 zpool_close(zpool_handle_t
*zhp
)
1163 nvlist_free(zhp
->zpool_config
);
1164 nvlist_free(zhp
->zpool_old_config
);
1165 nvlist_free(zhp
->zpool_props
);
1170 * Return the name of the pool.
1173 zpool_get_name(zpool_handle_t
*zhp
)
1175 return (zhp
->zpool_name
);
1180 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1183 zpool_get_state(zpool_handle_t
*zhp
)
1185 return (zhp
->zpool_state
);
1189 * Check if vdev list contains a special vdev
1192 zpool_has_special_vdev(nvlist_t
*nvroot
)
1197 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
, &child
,
1199 for (uint_t c
= 0; c
< children
; c
++) {
1202 if (nvlist_lookup_string(child
[c
],
1203 ZPOOL_CONFIG_ALLOCATION_BIAS
, &bias
) == 0 &&
1204 strcmp(bias
, VDEV_ALLOC_BIAS_SPECIAL
) == 0) {
1213 * Create the named pool, using the provided vdev list. It is assumed
1214 * that the consumer has already validated the contents of the nvlist, so we
1215 * don't have to worry about error semantics.
1218 zpool_create(libzfs_handle_t
*hdl
, const char *pool
, nvlist_t
*nvroot
,
1219 nvlist_t
*props
, nvlist_t
*fsprops
)
1221 zfs_cmd_t zc
= {"\0"};
1222 nvlist_t
*zc_fsprops
= NULL
;
1223 nvlist_t
*zc_props
= NULL
;
1224 nvlist_t
*hidden_args
= NULL
;
1225 uint8_t *wkeydata
= NULL
;
1230 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1231 "cannot create '%s'"), pool
);
1233 if (!zpool_name_valid(hdl
, B_FALSE
, pool
))
1234 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
1236 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1240 prop_flags_t flags
= { .create
= B_TRUE
, .import
= B_FALSE
};
1242 if ((zc_props
= zpool_valid_proplist(hdl
, pool
, props
,
1243 SPA_VERSION_1
, flags
, msg
)) == NULL
) {
1252 zoned
= ((nvlist_lookup_string(fsprops
,
1253 zfs_prop_to_name(ZFS_PROP_ZONED
), &zonestr
) == 0) &&
1254 strcmp(zonestr
, "on") == 0);
1256 if ((zc_fsprops
= zfs_valid_proplist(hdl
, ZFS_TYPE_FILESYSTEM
,
1257 fsprops
, zoned
, NULL
, NULL
, B_TRUE
, msg
)) == NULL
) {
1261 if (nvlist_exists(zc_fsprops
,
1262 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS
)) &&
1263 !zpool_has_special_vdev(nvroot
)) {
1264 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1265 "%s property requires a special vdev"),
1266 zfs_prop_to_name(ZFS_PROP_SPECIAL_SMALL_BLOCKS
));
1267 (void) zfs_error(hdl
, EZFS_BADPROP
, msg
);
1272 (nvlist_alloc(&zc_props
, NV_UNIQUE_NAME
, 0) != 0)) {
1275 if (zfs_crypto_create(hdl
, NULL
, zc_fsprops
, props
, B_TRUE
,
1276 &wkeydata
, &wkeylen
) != 0) {
1277 zfs_error(hdl
, EZFS_CRYPTOFAILED
, msg
);
1280 if (nvlist_add_nvlist(zc_props
,
1281 ZPOOL_ROOTFS_PROPS
, zc_fsprops
) != 0) {
1284 if (wkeydata
!= NULL
) {
1285 if (nvlist_alloc(&hidden_args
, NV_UNIQUE_NAME
, 0) != 0)
1288 if (nvlist_add_uint8_array(hidden_args
, "wkeydata",
1289 wkeydata
, wkeylen
) != 0)
1292 if (nvlist_add_nvlist(zc_props
, ZPOOL_HIDDEN_ARGS
,
1298 if (zc_props
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
1301 (void) strlcpy(zc
.zc_name
, pool
, sizeof (zc
.zc_name
));
1303 if ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_CREATE
, &zc
)) != 0) {
1305 zcmd_free_nvlists(&zc
);
1306 nvlist_free(zc_props
);
1307 nvlist_free(zc_fsprops
);
1308 nvlist_free(hidden_args
);
1309 if (wkeydata
!= NULL
)
1315 * This can happen if the user has specified the same
1316 * device multiple times. We can't reliably detect this
1317 * until we try to add it and see we already have a
1318 * label. This can also happen under if the device is
1319 * part of an active md or lvm device.
1321 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1322 "one or more vdevs refer to the same device, or "
1323 "one of\nthe devices is part of an active md or "
1325 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1329 * This happens if the record size is smaller or larger
1330 * than the allowed size range, or not a power of 2.
1332 * NOTE: although zfs_valid_proplist is called earlier,
1333 * this case may have slipped through since the
1334 * pool does not exist yet and it is therefore
1335 * impossible to read properties e.g. max blocksize
1338 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1339 "record size invalid"));
1340 return (zfs_error(hdl
, EZFS_BADPROP
, msg
));
1344 * This occurs when one of the devices is below
1345 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1346 * device was the problem device since there's no
1347 * reliable way to determine device size from userland.
1352 zfs_nicebytes(SPA_MINDEVSIZE
, buf
,
1355 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1356 "one or more devices is less than the "
1357 "minimum size (%s)"), buf
);
1359 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1362 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1363 "one or more devices is out of space"));
1364 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1367 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1368 "cache device must be a disk or disk slice"));
1369 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1372 return (zpool_standard_error(hdl
, errno
, msg
));
1377 zcmd_free_nvlists(&zc
);
1378 nvlist_free(zc_props
);
1379 nvlist_free(zc_fsprops
);
1380 nvlist_free(hidden_args
);
1381 if (wkeydata
!= NULL
)
1387 * Destroy the given pool. It is up to the caller to ensure that there are no
1388 * datasets left in the pool.
1391 zpool_destroy(zpool_handle_t
*zhp
, const char *log_str
)
1393 zfs_cmd_t zc
= {"\0"};
1394 zfs_handle_t
*zfp
= NULL
;
1395 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1398 if (zhp
->zpool_state
== POOL_STATE_ACTIVE
&&
1399 (zfp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
1402 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1403 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1405 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_DESTROY
, &zc
) != 0) {
1406 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1407 "cannot destroy '%s'"), zhp
->zpool_name
);
1409 if (errno
== EROFS
) {
1410 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1411 "one or more devices is read only"));
1412 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1414 (void) zpool_standard_error(hdl
, errno
, msg
);
1423 remove_mountpoint(zfp
);
1431 * Create a checkpoint in the given pool.
1434 zpool_checkpoint(zpool_handle_t
*zhp
)
1436 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1440 error
= lzc_pool_checkpoint(zhp
->zpool_name
);
1442 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1443 "cannot checkpoint '%s'"), zhp
->zpool_name
);
1444 (void) zpool_standard_error(hdl
, error
, msg
);
1452 * Discard the checkpoint from the given pool.
1455 zpool_discard_checkpoint(zpool_handle_t
*zhp
)
1457 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1461 error
= lzc_pool_checkpoint_discard(zhp
->zpool_name
);
1463 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1464 "cannot discard checkpoint in '%s'"), zhp
->zpool_name
);
1465 (void) zpool_standard_error(hdl
, error
, msg
);
1473 * Add the given vdevs to the pool. The caller must have already performed the
1474 * necessary verification to ensure that the vdev specification is well-formed.
1477 zpool_add(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
1479 zfs_cmd_t zc
= {"\0"};
1481 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1483 nvlist_t
**spares
, **l2cache
;
1484 uint_t nspares
, nl2cache
;
1486 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1487 "cannot add to '%s'"), zhp
->zpool_name
);
1489 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1490 SPA_VERSION_SPARES
&&
1491 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1492 &spares
, &nspares
) == 0) {
1493 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1494 "upgraded to add hot spares"));
1495 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1498 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1499 SPA_VERSION_L2CACHE
&&
1500 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1501 &l2cache
, &nl2cache
) == 0) {
1502 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1503 "upgraded to add cache devices"));
1504 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1507 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1509 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1511 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_ADD
, &zc
) != 0) {
1515 * This can happen if the user has specified the same
1516 * device multiple times. We can't reliably detect this
1517 * until we try to add it and see we already have a
1520 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1521 "one or more vdevs refer to the same device"));
1522 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1526 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1527 "invalid config; a pool with removing/removed "
1528 "vdevs does not support adding raidz vdevs"));
1529 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1534 * This occurrs when one of the devices is below
1535 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1536 * device was the problem device since there's no
1537 * reliable way to determine device size from userland.
1542 zfs_nicebytes(SPA_MINDEVSIZE
, buf
,
1545 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1546 "device is less than the minimum "
1549 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1553 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1554 "pool must be upgraded to add these vdevs"));
1555 (void) zfs_error(hdl
, EZFS_BADVERSION
, msg
);
1559 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1560 "cache device must be a disk or disk slice"));
1561 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1565 (void) zpool_standard_error(hdl
, errno
, msg
);
1573 zcmd_free_nvlists(&zc
);
1579 * Exports the pool from the system. The caller must ensure that there are no
1580 * mounted datasets in the pool.
1583 zpool_export_common(zpool_handle_t
*zhp
, boolean_t force
, boolean_t hardforce
,
1584 const char *log_str
)
1586 zfs_cmd_t zc
= {"\0"};
1589 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1590 "cannot export '%s'"), zhp
->zpool_name
);
1592 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1593 zc
.zc_cookie
= force
;
1594 zc
.zc_guid
= hardforce
;
1595 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1597 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_EXPORT
, &zc
) != 0) {
1600 zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
1601 "use '-f' to override the following errors:\n"
1602 "'%s' has an active shared spare which could be"
1603 " used by other pools once '%s' is exported."),
1604 zhp
->zpool_name
, zhp
->zpool_name
);
1605 return (zfs_error(zhp
->zpool_hdl
, EZFS_ACTIVE_SPARE
,
1608 return (zpool_standard_error_fmt(zhp
->zpool_hdl
, errno
,
1617 zpool_export(zpool_handle_t
*zhp
, boolean_t force
, const char *log_str
)
1619 return (zpool_export_common(zhp
, force
, B_FALSE
, log_str
));
1623 zpool_export_force(zpool_handle_t
*zhp
, const char *log_str
)
1625 return (zpool_export_common(zhp
, B_TRUE
, B_TRUE
, log_str
));
1629 zpool_rewind_exclaim(libzfs_handle_t
*hdl
, const char *name
, boolean_t dryrun
,
1632 nvlist_t
*nv
= NULL
;
1638 if (!hdl
->libzfs_printerr
|| config
== NULL
)
1641 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1642 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0) {
1646 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1648 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1650 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1651 strftime(timestr
, 128, "%c", &t
) != 0) {
1653 (void) printf(dgettext(TEXT_DOMAIN
,
1654 "Would be able to return %s "
1655 "to its state as of %s.\n"),
1658 (void) printf(dgettext(TEXT_DOMAIN
,
1659 "Pool %s returned to its state as of %s.\n"),
1663 (void) printf(dgettext(TEXT_DOMAIN
,
1664 "%s approximately %lld "),
1665 dryrun
? "Would discard" : "Discarded",
1666 ((longlong_t
)loss
+ 30) / 60);
1667 (void) printf(dgettext(TEXT_DOMAIN
,
1668 "minutes of transactions.\n"));
1669 } else if (loss
> 0) {
1670 (void) printf(dgettext(TEXT_DOMAIN
,
1671 "%s approximately %lld "),
1672 dryrun
? "Would discard" : "Discarded",
1674 (void) printf(dgettext(TEXT_DOMAIN
,
1675 "seconds of transactions.\n"));
1681 zpool_explain_recover(libzfs_handle_t
*hdl
, const char *name
, int reason
,
1684 nvlist_t
*nv
= NULL
;
1686 uint64_t edata
= UINT64_MAX
;
1691 if (!hdl
->libzfs_printerr
)
1695 (void) printf(dgettext(TEXT_DOMAIN
, "action: "));
1697 (void) printf(dgettext(TEXT_DOMAIN
, "\t"));
1699 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1700 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1701 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0 ||
1702 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1705 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1706 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_DATA_ERRORS
,
1709 (void) printf(dgettext(TEXT_DOMAIN
,
1710 "Recovery is possible, but will result in some data loss.\n"));
1712 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1713 strftime(timestr
, 128, "%c", &t
) != 0) {
1714 (void) printf(dgettext(TEXT_DOMAIN
,
1715 "\tReturning the pool to its state as of %s\n"
1716 "\tshould correct the problem. "),
1719 (void) printf(dgettext(TEXT_DOMAIN
,
1720 "\tReverting the pool to an earlier state "
1721 "should correct the problem.\n\t"));
1725 (void) printf(dgettext(TEXT_DOMAIN
,
1726 "Approximately %lld minutes of data\n"
1727 "\tmust be discarded, irreversibly. "),
1728 ((longlong_t
)loss
+ 30) / 60);
1729 } else if (loss
> 0) {
1730 (void) printf(dgettext(TEXT_DOMAIN
,
1731 "Approximately %lld seconds of data\n"
1732 "\tmust be discarded, irreversibly. "),
1735 if (edata
!= 0 && edata
!= UINT64_MAX
) {
1737 (void) printf(dgettext(TEXT_DOMAIN
,
1738 "After rewind, at least\n"
1739 "\tone persistent user-data error will remain. "));
1741 (void) printf(dgettext(TEXT_DOMAIN
,
1742 "After rewind, several\n"
1743 "\tpersistent user-data errors will remain. "));
1746 (void) printf(dgettext(TEXT_DOMAIN
,
1747 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1748 reason
>= 0 ? "clear" : "import", name
);
1750 (void) printf(dgettext(TEXT_DOMAIN
,
1751 "A scrub of the pool\n"
1752 "\tis strongly recommended after recovery.\n"));
1756 (void) printf(dgettext(TEXT_DOMAIN
,
1757 "Destroy and re-create the pool from\n\ta backup source.\n"));
1761 * zpool_import() is a contracted interface. Should be kept the same
1764 * Applications should use zpool_import_props() to import a pool with
1765 * new properties value to be set.
1768 zpool_import(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1771 nvlist_t
*props
= NULL
;
1774 if (altroot
!= NULL
) {
1775 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
1776 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1777 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1781 if (nvlist_add_string(props
,
1782 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), altroot
) != 0 ||
1783 nvlist_add_string(props
,
1784 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
), "none") != 0) {
1786 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1787 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1792 ret
= zpool_import_props(hdl
, config
, newname
, props
,
1799 print_vdev_tree(libzfs_handle_t
*hdl
, const char *name
, nvlist_t
*nv
,
1805 uint64_t is_log
= 0;
1807 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
,
1811 (void) printf("\t%*s%s%s\n", indent
, "", name
,
1812 is_log
? " [log]" : "");
1814 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1815 &child
, &children
) != 0)
1818 for (c
= 0; c
< children
; c
++) {
1819 vname
= zpool_vdev_name(hdl
, NULL
, child
[c
], VDEV_NAME_TYPE_ID
);
1820 print_vdev_tree(hdl
, vname
, child
[c
], indent
+ 2);
1826 zpool_print_unsup_feat(nvlist_t
*config
)
1828 nvlist_t
*nvinfo
, *unsup_feat
;
1831 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) ==
1833 verify(nvlist_lookup_nvlist(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
,
1836 for (nvp
= nvlist_next_nvpair(unsup_feat
, NULL
); nvp
!= NULL
;
1837 nvp
= nvlist_next_nvpair(unsup_feat
, nvp
)) {
1840 verify(nvpair_type(nvp
) == DATA_TYPE_STRING
);
1841 verify(nvpair_value_string(nvp
, &desc
) == 0);
1843 if (strlen(desc
) > 0)
1844 (void) printf("\t%s (%s)\n", nvpair_name(nvp
), desc
);
1846 (void) printf("\t%s\n", nvpair_name(nvp
));
1851 * Import the given pool using the known configuration and a list of
1852 * properties to be set. The configuration should have come from
1853 * zpool_find_import(). The 'newname' parameters control whether the pool
1854 * is imported with a different name.
1857 zpool_import_props(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1858 nvlist_t
*props
, int flags
)
1860 zfs_cmd_t zc
= {"\0"};
1861 zpool_load_policy_t policy
;
1862 nvlist_t
*nv
= NULL
;
1863 nvlist_t
*nvinfo
= NULL
;
1864 nvlist_t
*missing
= NULL
;
1871 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1874 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1875 "cannot import pool '%s'"), origname
);
1877 if (newname
!= NULL
) {
1878 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
1879 return (zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1880 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1882 thename
= (char *)newname
;
1887 if (props
!= NULL
) {
1889 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
1891 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
1894 if ((props
= zpool_valid_proplist(hdl
, origname
,
1895 props
, version
, flags
, errbuf
)) == NULL
)
1897 if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
1904 (void) strlcpy(zc
.zc_name
, thename
, sizeof (zc
.zc_name
));
1906 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1909 if (zcmd_write_conf_nvlist(hdl
, &zc
, config
) != 0) {
1910 zcmd_free_nvlists(&zc
);
1913 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zc
.zc_nvlist_conf_size
* 2) != 0) {
1914 zcmd_free_nvlists(&zc
);
1918 zc
.zc_cookie
= flags
;
1919 while ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_IMPORT
, &zc
)) != 0 &&
1921 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
1922 zcmd_free_nvlists(&zc
);
1929 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nv
);
1931 zcmd_free_nvlists(&zc
);
1933 zpool_get_load_policy(config
, &policy
);
1940 * Dry-run failed, but we print out what success
1941 * looks like if we found a best txg
1943 if (policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) {
1944 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1950 if (newname
== NULL
)
1951 (void) snprintf(desc
, sizeof (desc
),
1952 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1955 (void) snprintf(desc
, sizeof (desc
),
1956 dgettext(TEXT_DOMAIN
, "cannot import '%s' as '%s'"),
1961 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
1962 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1963 nvlist_exists(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
)) {
1964 (void) printf(dgettext(TEXT_DOMAIN
, "This "
1965 "pool uses the following feature(s) not "
1966 "supported by this system:\n"));
1967 zpool_print_unsup_feat(nv
);
1968 if (nvlist_exists(nvinfo
,
1969 ZPOOL_CONFIG_CAN_RDONLY
)) {
1970 (void) printf(dgettext(TEXT_DOMAIN
,
1971 "All unsupported features are only "
1972 "required for writing to the pool."
1973 "\nThe pool can be imported using "
1974 "'-o readonly=on'.\n"));
1978 * Unsupported version.
1980 (void) zfs_error(hdl
, EZFS_BADVERSION
, desc
);
1984 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
1985 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0) {
1986 char *hostname
= "<unknown>";
1987 uint64_t hostid
= 0;
1988 mmp_state_t mmp_state
;
1990 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
1991 ZPOOL_CONFIG_MMP_STATE
);
1993 if (nvlist_exists(nvinfo
,
1994 ZPOOL_CONFIG_MMP_HOSTNAME
))
1995 hostname
= fnvlist_lookup_string(nvinfo
,
1996 ZPOOL_CONFIG_MMP_HOSTNAME
);
1998 if (nvlist_exists(nvinfo
,
1999 ZPOOL_CONFIG_MMP_HOSTID
))
2000 hostid
= fnvlist_lookup_uint64(nvinfo
,
2001 ZPOOL_CONFIG_MMP_HOSTID
);
2003 if (mmp_state
== MMP_STATE_ACTIVE
) {
2004 (void) snprintf(aux
, sizeof (aux
),
2005 dgettext(TEXT_DOMAIN
, "pool is imp"
2006 "orted on host '%s' (hostid=%lx).\n"
2007 "Export the pool on the other "
2008 "system, then run 'zpool import'."),
2009 hostname
, (unsigned long) hostid
);
2010 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
2011 (void) snprintf(aux
, sizeof (aux
),
2012 dgettext(TEXT_DOMAIN
, "pool has "
2013 "the multihost property on and "
2014 "the\nsystem's hostid is not set. "
2015 "Set a unique system hostid with "
2016 "the zgenhostid(8) command.\n"));
2019 (void) zfs_error_aux(hdl
, aux
);
2021 (void) zfs_error(hdl
, EZFS_ACTIVE_POOL
, desc
);
2025 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, desc
);
2029 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2030 "one or more devices is read only"));
2031 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
2035 if (nv
&& nvlist_lookup_nvlist(nv
,
2036 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
2037 nvlist_lookup_nvlist(nvinfo
,
2038 ZPOOL_CONFIG_MISSING_DEVICES
, &missing
) == 0) {
2039 (void) printf(dgettext(TEXT_DOMAIN
,
2040 "The devices below are missing or "
2041 "corrupted, use '-m' to import the pool "
2043 print_vdev_tree(hdl
, NULL
, missing
, 2);
2044 (void) printf("\n");
2046 (void) zpool_standard_error(hdl
, error
, desc
);
2050 (void) zpool_standard_error(hdl
, error
, desc
);
2054 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2055 "one or more devices are already in use\n"));
2056 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
2059 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2060 "new name of at least one dataset is longer than "
2061 "the maximum allowable length"));
2062 (void) zfs_error(hdl
, EZFS_NAMETOOLONG
, desc
);
2065 (void) zpool_standard_error(hdl
, error
, desc
);
2066 zpool_explain_recover(hdl
,
2067 newname
? origname
: thename
, -error
, nv
);
2074 zpool_handle_t
*zhp
;
2077 * This should never fail, but play it safe anyway.
2079 if (zpool_open_silent(hdl
, thename
, &zhp
) != 0)
2081 else if (zhp
!= NULL
)
2083 if (policy
.zlp_rewind
&
2084 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
2085 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
2086 ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) != 0), nv
);
2096 * Translate vdev names to guids. If a vdev_path is determined to be
2097 * unsuitable then a vd_errlist is allocated and the vdev path and errno
2101 zpool_translate_vdev_guids(zpool_handle_t
*zhp
, nvlist_t
*vds
,
2102 nvlist_t
*vdev_guids
, nvlist_t
*guids_to_paths
, nvlist_t
**vd_errlist
)
2104 nvlist_t
*errlist
= NULL
;
2107 for (nvpair_t
*elem
= nvlist_next_nvpair(vds
, NULL
); elem
!= NULL
;
2108 elem
= nvlist_next_nvpair(vds
, elem
)) {
2109 boolean_t spare
, cache
;
2111 char *vd_path
= nvpair_name(elem
);
2112 nvlist_t
*tgt
= zpool_find_vdev(zhp
, vd_path
, &spare
, &cache
,
2115 if ((tgt
== NULL
) || cache
|| spare
) {
2116 if (errlist
== NULL
) {
2117 errlist
= fnvlist_alloc();
2121 uint64_t err
= (tgt
== NULL
) ? EZFS_NODEVICE
:
2122 (spare
? EZFS_ISSPARE
: EZFS_ISL2CACHE
);
2123 fnvlist_add_int64(errlist
, vd_path
, err
);
2127 uint64_t guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
2128 fnvlist_add_uint64(vdev_guids
, vd_path
, guid
);
2130 char msg
[MAXNAMELEN
];
2131 (void) snprintf(msg
, sizeof (msg
), "%llu", (u_longlong_t
)guid
);
2132 fnvlist_add_string(guids_to_paths
, msg
, vd_path
);
2136 verify(errlist
!= NULL
);
2137 if (vd_errlist
!= NULL
)
2138 *vd_errlist
= errlist
;
2140 fnvlist_free(errlist
);
2147 xlate_init_err(int err
)
2151 return (EZFS_NODEVICE
);
2154 return (EZFS_BADDEV
);
2156 return (EZFS_INITIALIZING
);
2158 return (EZFS_NO_INITIALIZE
);
2164 * Begin, suspend, or cancel the initialization (initializing of all free
2165 * blocks) for the given vdevs in the given pool.
2168 zpool_initialize(zpool_handle_t
*zhp
, pool_initialize_func_t cmd_type
,
2174 nvlist_t
*vdev_guids
= fnvlist_alloc();
2175 nvlist_t
*guids_to_paths
= fnvlist_alloc();
2176 nvlist_t
*vd_errlist
= NULL
;
2180 err
= zpool_translate_vdev_guids(zhp
, vds
, vdev_guids
,
2181 guids_to_paths
, &vd_errlist
);
2184 err
= lzc_initialize(zhp
->zpool_name
, cmd_type
,
2185 vdev_guids
, &errlist
);
2187 fnvlist_free(vdev_guids
);
2188 fnvlist_free(guids_to_paths
);
2192 if (errlist
!= NULL
) {
2193 vd_errlist
= fnvlist_lookup_nvlist(errlist
,
2194 ZPOOL_INITIALIZE_VDEVS
);
2197 (void) snprintf(msg
, sizeof (msg
),
2198 dgettext(TEXT_DOMAIN
, "operation failed"));
2200 verify(vd_errlist
!= NULL
);
2203 for (elem
= nvlist_next_nvpair(vd_errlist
, NULL
); elem
!= NULL
;
2204 elem
= nvlist_next_nvpair(vd_errlist
, elem
)) {
2205 int64_t vd_error
= xlate_init_err(fnvpair_value_int64(elem
));
2208 if (nvlist_lookup_string(guids_to_paths
, nvpair_name(elem
),
2210 path
= nvpair_name(elem
);
2212 (void) zfs_error_fmt(zhp
->zpool_hdl
, vd_error
,
2213 "cannot initialize '%s'", path
);
2216 fnvlist_free(vdev_guids
);
2217 fnvlist_free(guids_to_paths
);
2219 if (vd_errlist
!= NULL
) {
2220 fnvlist_free(vd_errlist
);
2224 return (zpool_standard_error(zhp
->zpool_hdl
, err
, msg
));
2228 xlate_trim_err(int err
)
2232 return (EZFS_NODEVICE
);
2235 return (EZFS_BADDEV
);
2237 return (EZFS_TRIMMING
);
2239 return (EZFS_NO_TRIM
);
2241 return (EZFS_TRIM_NOTSUP
);
2247 * Begin, suspend, or cancel the TRIM (discarding of all free blocks) for
2248 * the given vdevs in the given pool.
2251 zpool_trim(zpool_handle_t
*zhp
, pool_trim_func_t cmd_type
, nvlist_t
*vds
,
2252 trimflags_t
*trim_flags
)
2257 nvlist_t
*vdev_guids
= fnvlist_alloc();
2258 nvlist_t
*guids_to_paths
= fnvlist_alloc();
2259 nvlist_t
*vd_errlist
= NULL
;
2263 err
= zpool_translate_vdev_guids(zhp
, vds
, vdev_guids
,
2264 guids_to_paths
, &vd_errlist
);
2266 err
= lzc_trim(zhp
->zpool_name
, cmd_type
, trim_flags
->rate
,
2267 trim_flags
->secure
, vdev_guids
, &errlist
);
2269 fnvlist_free(vdev_guids
);
2270 fnvlist_free(guids_to_paths
);
2274 if (errlist
!= NULL
) {
2275 vd_errlist
= fnvlist_lookup_nvlist(errlist
,
2279 (void) snprintf(msg
, sizeof (msg
),
2280 dgettext(TEXT_DOMAIN
, "operation failed"));
2282 verify(vd_errlist
!= NULL
);
2285 for (elem
= nvlist_next_nvpair(vd_errlist
, NULL
);
2286 elem
!= NULL
; elem
= nvlist_next_nvpair(vd_errlist
, elem
)) {
2287 int64_t vd_error
= xlate_trim_err(fnvpair_value_int64(elem
));
2291 * If only the pool was specified, and it was not a secure
2292 * trim then suppress warnings for individual vdevs which
2293 * do not support trimming.
2295 if (vd_error
== EZFS_TRIM_NOTSUP
&&
2296 trim_flags
->fullpool
&&
2297 !trim_flags
->secure
) {
2301 if (nvlist_lookup_string(guids_to_paths
, nvpair_name(elem
),
2303 path
= nvpair_name(elem
);
2305 (void) zfs_error_fmt(zhp
->zpool_hdl
, vd_error
,
2306 "cannot trim '%s'", path
);
2309 fnvlist_free(vdev_guids
);
2310 fnvlist_free(guids_to_paths
);
2312 if (vd_errlist
!= NULL
) {
2313 fnvlist_free(vd_errlist
);
2317 return (zpool_standard_error(zhp
->zpool_hdl
, err
, msg
));
2324 zpool_scan(zpool_handle_t
*zhp
, pool_scan_func_t func
, pool_scrub_cmd_t cmd
)
2326 zfs_cmd_t zc
= {"\0"};
2329 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2331 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2332 zc
.zc_cookie
= func
;
2335 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_SCAN
, &zc
) == 0)
2340 /* ECANCELED on a scrub means we resumed a paused scrub */
2341 if (err
== ECANCELED
&& func
== POOL_SCAN_SCRUB
&&
2342 cmd
== POOL_SCRUB_NORMAL
)
2345 if (err
== ENOENT
&& func
!= POOL_SCAN_NONE
&& cmd
== POOL_SCRUB_NORMAL
)
2348 if (func
== POOL_SCAN_SCRUB
) {
2349 if (cmd
== POOL_SCRUB_PAUSE
) {
2350 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2351 "cannot pause scrubbing %s"), zc
.zc_name
);
2353 assert(cmd
== POOL_SCRUB_NORMAL
);
2354 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2355 "cannot scrub %s"), zc
.zc_name
);
2357 } else if (func
== POOL_SCAN_RESILVER
) {
2358 assert(cmd
== POOL_SCRUB_NORMAL
);
2359 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2360 "cannot restart resilver on %s"), zc
.zc_name
);
2361 } else if (func
== POOL_SCAN_NONE
) {
2362 (void) snprintf(msg
, sizeof (msg
),
2363 dgettext(TEXT_DOMAIN
, "cannot cancel scrubbing %s"),
2366 assert(!"unexpected result");
2371 pool_scan_stat_t
*ps
= NULL
;
2374 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
2375 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
2376 (void) nvlist_lookup_uint64_array(nvroot
,
2377 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &psc
);
2378 if (ps
&& ps
->pss_func
== POOL_SCAN_SCRUB
) {
2379 if (cmd
== POOL_SCRUB_PAUSE
)
2380 return (zfs_error(hdl
, EZFS_SCRUB_PAUSED
, msg
));
2382 return (zfs_error(hdl
, EZFS_SCRUBBING
, msg
));
2384 return (zfs_error(hdl
, EZFS_RESILVERING
, msg
));
2386 } else if (err
== ENOENT
) {
2387 return (zfs_error(hdl
, EZFS_NO_SCRUB
, msg
));
2388 } else if (err
== ENOTSUP
&& func
== POOL_SCAN_RESILVER
) {
2389 return (zfs_error(hdl
, EZFS_NO_RESILVER_DEFER
, msg
));
2391 return (zpool_standard_error(hdl
, err
, msg
));
2396 * Find a vdev that matches the search criteria specified. We use the
2397 * the nvpair name to determine how we should look for the device.
2398 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2399 * spare; but FALSE if its an INUSE spare.
2402 vdev_to_nvlist_iter(nvlist_t
*nv
, nvlist_t
*search
, boolean_t
*avail_spare
,
2403 boolean_t
*l2cache
, boolean_t
*log
)
2410 nvpair_t
*pair
= nvlist_next_nvpair(search
, NULL
);
2412 /* Nothing to look for */
2413 if (search
== NULL
|| pair
== NULL
)
2416 /* Obtain the key we will use to search */
2417 srchkey
= nvpair_name(pair
);
2419 switch (nvpair_type(pair
)) {
2420 case DATA_TYPE_UINT64
:
2421 if (strcmp(srchkey
, ZPOOL_CONFIG_GUID
) == 0) {
2422 uint64_t srchval
, theguid
;
2424 verify(nvpair_value_uint64(pair
, &srchval
) == 0);
2425 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
2427 if (theguid
== srchval
)
2432 case DATA_TYPE_STRING
: {
2433 char *srchval
, *val
;
2435 verify(nvpair_value_string(pair
, &srchval
) == 0);
2436 if (nvlist_lookup_string(nv
, srchkey
, &val
) != 0)
2440 * Search for the requested value. Special cases:
2442 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2443 * "-part1", or "p1". The suffix is hidden from the user,
2444 * but included in the string, so this matches around it.
2445 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2446 * is used to check all possible expanded paths.
2447 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2449 * Otherwise, all other searches are simple string compares.
2451 if (strcmp(srchkey
, ZPOOL_CONFIG_PATH
) == 0) {
2452 uint64_t wholedisk
= 0;
2454 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
2456 if (zfs_strcmp_pathname(srchval
, val
, wholedisk
) == 0)
2459 } else if (strcmp(srchkey
, ZPOOL_CONFIG_TYPE
) == 0 && val
) {
2460 char *type
, *idx
, *end
, *p
;
2461 uint64_t id
, vdev_id
;
2464 * Determine our vdev type, keeping in mind
2465 * that the srchval is composed of a type and
2466 * vdev id pair (i.e. mirror-4).
2468 if ((type
= strdup(srchval
)) == NULL
)
2471 if ((p
= strrchr(type
, '-')) == NULL
) {
2479 * If the types don't match then keep looking.
2481 if (strncmp(val
, type
, strlen(val
)) != 0) {
2486 verify(zpool_vdev_is_interior(type
));
2487 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
2491 vdev_id
= strtoull(idx
, &end
, 10);
2498 * Now verify that we have the correct vdev id.
2507 if (strcmp(srchval
, val
) == 0)
2516 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2517 &child
, &children
) != 0)
2520 for (c
= 0; c
< children
; c
++) {
2521 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2522 avail_spare
, l2cache
, NULL
)) != NULL
) {
2524 * The 'is_log' value is only set for the toplevel
2525 * vdev, not the leaf vdevs. So we always lookup the
2526 * log device from the root of the vdev tree (where
2527 * 'log' is non-NULL).
2530 nvlist_lookup_uint64(child
[c
],
2531 ZPOOL_CONFIG_IS_LOG
, &is_log
) == 0 &&
2539 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2540 &child
, &children
) == 0) {
2541 for (c
= 0; c
< children
; c
++) {
2542 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2543 avail_spare
, l2cache
, NULL
)) != NULL
) {
2544 *avail_spare
= B_TRUE
;
2550 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2551 &child
, &children
) == 0) {
2552 for (c
= 0; c
< children
; c
++) {
2553 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2554 avail_spare
, l2cache
, NULL
)) != NULL
) {
2565 * Given a physical path or guid, find the associated vdev.
2568 zpool_find_vdev_by_physpath(zpool_handle_t
*zhp
, const char *ppath
,
2569 boolean_t
*avail_spare
, boolean_t
*l2cache
, boolean_t
*log
)
2571 nvlist_t
*search
, *nvroot
, *ret
;
2575 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2577 guid
= strtoull(ppath
, &end
, 0);
2578 if (guid
!= 0 && *end
== '\0') {
2579 verify(nvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
) == 0);
2581 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PHYS_PATH
,
2585 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2588 *avail_spare
= B_FALSE
;
2592 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2593 nvlist_free(search
);
2599 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2602 zpool_vdev_is_interior(const char *name
)
2604 if (strncmp(name
, VDEV_TYPE_RAIDZ
, strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2605 strncmp(name
, VDEV_TYPE_SPARE
, strlen(VDEV_TYPE_SPARE
)) == 0 ||
2607 VDEV_TYPE_REPLACING
, strlen(VDEV_TYPE_REPLACING
)) == 0 ||
2608 strncmp(name
, VDEV_TYPE_MIRROR
, strlen(VDEV_TYPE_MIRROR
)) == 0)
2614 zpool_find_vdev(zpool_handle_t
*zhp
, const char *path
, boolean_t
*avail_spare
,
2615 boolean_t
*l2cache
, boolean_t
*log
)
2618 nvlist_t
*nvroot
, *search
, *ret
;
2621 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2623 guid
= strtoull(path
, &end
, 0);
2624 if (guid
!= 0 && *end
== '\0') {
2625 verify(nvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
) == 0);
2626 } else if (zpool_vdev_is_interior(path
)) {
2627 verify(nvlist_add_string(search
, ZPOOL_CONFIG_TYPE
, path
) == 0);
2629 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PATH
, path
) == 0);
2632 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2635 *avail_spare
= B_FALSE
;
2639 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2640 nvlist_free(search
);
2646 vdev_is_online(nvlist_t
*nv
)
2650 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
, &ival
) == 0 ||
2651 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
, &ival
) == 0 ||
2652 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
, &ival
) == 0)
2659 * Helper function for zpool_get_physpaths().
2662 vdev_get_one_physpath(nvlist_t
*config
, char *physpath
, size_t physpath_size
,
2663 size_t *bytes_written
)
2665 size_t bytes_left
, pos
, rsz
;
2669 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PHYS_PATH
,
2671 return (EZFS_NODEVICE
);
2673 pos
= *bytes_written
;
2674 bytes_left
= physpath_size
- pos
;
2675 format
= (pos
== 0) ? "%s" : " %s";
2677 rsz
= snprintf(physpath
+ pos
, bytes_left
, format
, tmppath
);
2678 *bytes_written
+= rsz
;
2680 if (rsz
>= bytes_left
) {
2681 /* if physpath was not copied properly, clear it */
2682 if (bytes_left
!= 0) {
2685 return (EZFS_NOSPC
);
2691 vdev_get_physpaths(nvlist_t
*nv
, char *physpath
, size_t phypath_size
,
2692 size_t *rsz
, boolean_t is_spare
)
2697 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
2698 return (EZFS_INVALCONFIG
);
2700 if (strcmp(type
, VDEV_TYPE_DISK
) == 0) {
2702 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2703 * For a spare vdev, we only want to boot from the active
2708 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
2711 return (EZFS_INVALCONFIG
);
2714 if (vdev_is_online(nv
)) {
2715 if ((ret
= vdev_get_one_physpath(nv
, physpath
,
2716 phypath_size
, rsz
)) != 0)
2719 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) == 0 ||
2720 strcmp(type
, VDEV_TYPE_RAIDZ
) == 0 ||
2721 strcmp(type
, VDEV_TYPE_REPLACING
) == 0 ||
2722 (is_spare
= (strcmp(type
, VDEV_TYPE_SPARE
) == 0))) {
2727 if (nvlist_lookup_nvlist_array(nv
,
2728 ZPOOL_CONFIG_CHILDREN
, &child
, &count
) != 0)
2729 return (EZFS_INVALCONFIG
);
2731 for (i
= 0; i
< count
; i
++) {
2732 ret
= vdev_get_physpaths(child
[i
], physpath
,
2733 phypath_size
, rsz
, is_spare
);
2734 if (ret
== EZFS_NOSPC
)
2739 return (EZFS_POOL_INVALARG
);
2743 * Get phys_path for a root pool config.
2744 * Return 0 on success; non-zero on failure.
2747 zpool_get_config_physpath(nvlist_t
*config
, char *physpath
, size_t phypath_size
)
2750 nvlist_t
*vdev_root
;
2757 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2759 return (EZFS_INVALCONFIG
);
2761 if (nvlist_lookup_string(vdev_root
, ZPOOL_CONFIG_TYPE
, &type
) != 0 ||
2762 nvlist_lookup_nvlist_array(vdev_root
, ZPOOL_CONFIG_CHILDREN
,
2763 &child
, &count
) != 0)
2764 return (EZFS_INVALCONFIG
);
2767 * root pool can only have a single top-level vdev.
2769 if (strcmp(type
, VDEV_TYPE_ROOT
) != 0 || count
!= 1)
2770 return (EZFS_POOL_INVALARG
);
2772 (void) vdev_get_physpaths(child
[0], physpath
, phypath_size
, &rsz
,
2775 /* No online devices */
2777 return (EZFS_NODEVICE
);
2783 * Get phys_path for a root pool
2784 * Return 0 on success; non-zero on failure.
2787 zpool_get_physpath(zpool_handle_t
*zhp
, char *physpath
, size_t phypath_size
)
2789 return (zpool_get_config_physpath(zhp
->zpool_config
, physpath
,
2794 * If the device has being dynamically expanded then we need to relabel
2795 * the disk to use the new unallocated space.
2798 zpool_relabel_disk(libzfs_handle_t
*hdl
, const char *path
, const char *msg
)
2802 if ((fd
= open(path
, O_RDWR
|O_DIRECT
)) < 0) {
2803 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2804 "relabel '%s': unable to open device: %d"), path
, errno
);
2805 return (zfs_error(hdl
, EZFS_OPENFAILED
, msg
));
2809 * It's possible that we might encounter an error if the device
2810 * does not have any unallocated space left. If so, we simply
2811 * ignore that error and continue on.
2813 * Also, we don't call efi_rescan() - that would just return EBUSY.
2814 * The module will do it for us in vdev_disk_open().
2816 error
= efi_use_whole_disk(fd
);
2818 /* Flush the buffers to disk and invalidate the page cache. */
2820 (void) ioctl(fd
, BLKFLSBUF
);
2823 if (error
&& error
!= VT_ENOSPC
) {
2824 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2825 "relabel '%s': unable to read disk capacity"), path
);
2826 return (zfs_error(hdl
, EZFS_NOCAP
, msg
));
2833 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
2835 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
2836 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
2840 zpool_vdev_path_to_guid_impl(zpool_handle_t
*zhp
, const char *path
,
2841 boolean_t
*is_spare
, boolean_t
*is_l2cache
, boolean_t
*is_log
)
2844 boolean_t spare
= B_FALSE
, l2cache
= B_FALSE
, log
= B_FALSE
;
2847 if ((tgt
= zpool_find_vdev(zhp
, path
, &spare
, &l2cache
,
2851 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &guid
) == 0);
2852 if (is_spare
!= NULL
)
2854 if (is_l2cache
!= NULL
)
2855 *is_l2cache
= l2cache
;
2862 /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
2864 zpool_vdev_path_to_guid(zpool_handle_t
*zhp
, const char *path
)
2866 return (zpool_vdev_path_to_guid_impl(zhp
, path
, NULL
, NULL
, NULL
));
2870 * Bring the specified vdev online. The 'flags' parameter is a set of the
2871 * ZFS_ONLINE_* flags.
2874 zpool_vdev_online(zpool_handle_t
*zhp
, const char *path
, int flags
,
2875 vdev_state_t
*newstate
)
2877 zfs_cmd_t zc
= {"\0"};
2881 boolean_t avail_spare
, l2cache
, islog
;
2882 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2885 if (flags
& ZFS_ONLINE_EXPAND
) {
2886 (void) snprintf(msg
, sizeof (msg
),
2887 dgettext(TEXT_DOMAIN
, "cannot expand %s"), path
);
2889 (void) snprintf(msg
, sizeof (msg
),
2890 dgettext(TEXT_DOMAIN
, "cannot online %s"), path
);
2893 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2894 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2896 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2898 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2901 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2903 if ((flags
& ZFS_ONLINE_EXPAND
||
2904 zpool_get_prop_int(zhp
, ZPOOL_PROP_AUTOEXPAND
, NULL
)) &&
2905 nvlist_lookup_string(tgt
, ZPOOL_CONFIG_PATH
, &pathname
) == 0) {
2906 uint64_t wholedisk
= 0;
2908 (void) nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_WHOLE_DISK
,
2912 * XXX - L2ARC 1.0 devices can't support expansion.
2915 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2916 "cannot expand cache devices"));
2917 return (zfs_error(hdl
, EZFS_VDEVNOTSUP
, msg
));
2921 const char *fullpath
= path
;
2922 char buf
[MAXPATHLEN
];
2924 if (path
[0] != '/') {
2925 error
= zfs_resolve_shortname(path
, buf
,
2928 return (zfs_error(hdl
, EZFS_NODEVICE
,
2934 error
= zpool_relabel_disk(hdl
, fullpath
, msg
);
2940 zc
.zc_cookie
= VDEV_STATE_ONLINE
;
2943 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) != 0) {
2944 if (errno
== EINVAL
) {
2945 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "was split "
2946 "from this pool into a new one. Use '%s' "
2947 "instead"), "zpool detach");
2948 return (zfs_error(hdl
, EZFS_POSTSPLIT_ONLINE
, msg
));
2950 return (zpool_standard_error(hdl
, errno
, msg
));
2953 *newstate
= zc
.zc_cookie
;
2958 * Take the specified vdev offline
2961 zpool_vdev_offline(zpool_handle_t
*zhp
, const char *path
, boolean_t istmp
)
2963 zfs_cmd_t zc
= {"\0"};
2966 boolean_t avail_spare
, l2cache
;
2967 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2969 (void) snprintf(msg
, sizeof (msg
),
2970 dgettext(TEXT_DOMAIN
, "cannot offline %s"), path
);
2972 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2973 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2975 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2977 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2980 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2982 zc
.zc_cookie
= VDEV_STATE_OFFLINE
;
2983 zc
.zc_obj
= istmp
? ZFS_OFFLINE_TEMPORARY
: 0;
2985 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2992 * There are no other replicas of this device.
2994 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2998 * The log device has unplayed logs
3000 return (zfs_error(hdl
, EZFS_UNPLAYED_LOGS
, msg
));
3003 return (zpool_standard_error(hdl
, errno
, msg
));
3008 * Mark the given vdev faulted.
3011 zpool_vdev_fault(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
3013 zfs_cmd_t zc
= {"\0"};
3015 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3017 (void) snprintf(msg
, sizeof (msg
),
3018 dgettext(TEXT_DOMAIN
, "cannot fault %llu"), (u_longlong_t
)guid
);
3020 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3022 zc
.zc_cookie
= VDEV_STATE_FAULTED
;
3025 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3032 * There are no other replicas of this device.
3034 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
3037 return (zpool_standard_error(hdl
, errno
, msg
));
3043 * Mark the given vdev degraded.
3046 zpool_vdev_degrade(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
3048 zfs_cmd_t zc
= {"\0"};
3050 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3052 (void) snprintf(msg
, sizeof (msg
),
3053 dgettext(TEXT_DOMAIN
, "cannot degrade %llu"), (u_longlong_t
)guid
);
3055 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3057 zc
.zc_cookie
= VDEV_STATE_DEGRADED
;
3060 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
3063 return (zpool_standard_error(hdl
, errno
, msg
));
3067 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
3071 is_replacing_spare(nvlist_t
*search
, nvlist_t
*tgt
, int which
)
3077 if (nvlist_lookup_nvlist_array(search
, ZPOOL_CONFIG_CHILDREN
, &child
,
3079 verify(nvlist_lookup_string(search
, ZPOOL_CONFIG_TYPE
,
3082 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0 &&
3083 children
== 2 && child
[which
] == tgt
)
3086 for (c
= 0; c
< children
; c
++)
3087 if (is_replacing_spare(child
[c
], tgt
, which
))
3095 * Attach new_disk (fully described by nvroot) to old_disk.
3096 * If 'replacing' is specified, the new disk will replace the old one.
3099 zpool_vdev_attach(zpool_handle_t
*zhp
,
3100 const char *old_disk
, const char *new_disk
, nvlist_t
*nvroot
, int replacing
)
3102 zfs_cmd_t zc
= {"\0"};
3106 boolean_t avail_spare
, l2cache
, islog
;
3111 nvlist_t
*config_root
;
3112 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3113 boolean_t rootpool
= zpool_is_bootable(zhp
);
3116 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
3117 "cannot replace %s with %s"), old_disk
, new_disk
);
3119 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
3120 "cannot attach %s to %s"), new_disk
, old_disk
);
3122 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3123 if ((tgt
= zpool_find_vdev(zhp
, old_disk
, &avail_spare
, &l2cache
,
3125 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3128 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3131 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
3133 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
3134 zc
.zc_cookie
= replacing
;
3136 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
3137 &child
, &children
) != 0 || children
!= 1) {
3138 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3139 "new device must be a single disk"));
3140 return (zfs_error(hdl
, EZFS_INVALCONFIG
, msg
));
3143 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
3144 ZPOOL_CONFIG_VDEV_TREE
, &config_root
) == 0);
3146 if ((newname
= zpool_vdev_name(NULL
, NULL
, child
[0], 0)) == NULL
)
3150 * If the target is a hot spare that has been swapped in, we can only
3151 * replace it with another hot spare.
3154 nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_IS_SPARE
, &val
) == 0 &&
3155 (zpool_find_vdev(zhp
, newname
, &avail_spare
, &l2cache
,
3156 NULL
) == NULL
|| !avail_spare
) &&
3157 is_replacing_spare(config_root
, tgt
, 1)) {
3158 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3159 "can only be replaced by another hot spare"));
3161 return (zfs_error(hdl
, EZFS_BADTARGET
, msg
));
3166 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
3169 ret
= zfs_ioctl(hdl
, ZFS_IOC_VDEV_ATTACH
, &zc
);
3171 zcmd_free_nvlists(&zc
);
3176 * XXX need a better way to prevent user from
3177 * booting up a half-baked vdev.
3179 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Make "
3180 "sure to wait until resilver is done "
3181 "before rebooting.\n"));
3189 * Can't attach to or replace this type of vdev.
3192 uint64_t version
= zpool_get_prop_int(zhp
,
3193 ZPOOL_PROP_VERSION
, NULL
);
3196 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3197 "cannot replace a log with a spare"));
3198 else if (version
>= SPA_VERSION_MULTI_REPLACE
)
3199 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3200 "already in replacing/spare config; wait "
3201 "for completion or use 'zpool detach'"));
3203 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3204 "cannot replace a replacing device"));
3206 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3207 "can only attach to mirrors and top-level "
3210 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
3215 * The new device must be a single disk.
3217 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3218 "new device must be a single disk"));
3219 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3223 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "%s is busy, "
3224 "or device removal is in progress"),
3226 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
3231 * The new device is too small.
3233 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3234 "device is too small"));
3235 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
3240 * The new device has a different optimal sector size.
3242 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3243 "new device has a different optimal sector size; use the "
3244 "option '-o ashift=N' to override the optimal size"));
3245 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
3250 * The resulting top-level vdev spec won't fit in the label.
3252 (void) zfs_error(hdl
, EZFS_DEVOVERFLOW
, msg
);
3256 (void) zpool_standard_error(hdl
, errno
, msg
);
3263 * Detach the specified device.
3266 zpool_vdev_detach(zpool_handle_t
*zhp
, const char *path
)
3268 zfs_cmd_t zc
= {"\0"};
3271 boolean_t avail_spare
, l2cache
;
3272 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3274 (void) snprintf(msg
, sizeof (msg
),
3275 dgettext(TEXT_DOMAIN
, "cannot detach %s"), path
);
3277 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3278 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3280 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3283 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3286 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
3288 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
3290 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_DETACH
, &zc
) == 0)
3297 * Can't detach from this type of vdev.
3299 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "only "
3300 "applicable to mirror and replacing vdevs"));
3301 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
3306 * There are no other replicas of this device.
3308 (void) zfs_error(hdl
, EZFS_NOREPLICAS
, msg
);
3312 (void) zpool_standard_error(hdl
, errno
, msg
);
3319 * Find a mirror vdev in the source nvlist.
3321 * The mchild array contains a list of disks in one of the top-level mirrors
3322 * of the source pool. The schild array contains a list of disks that the
3323 * user specified on the command line. We loop over the mchild array to
3324 * see if any entry in the schild array matches.
3326 * If a disk in the mchild array is found in the schild array, we return
3327 * the index of that entry. Otherwise we return -1.
3330 find_vdev_entry(zpool_handle_t
*zhp
, nvlist_t
**mchild
, uint_t mchildren
,
3331 nvlist_t
**schild
, uint_t schildren
)
3335 for (mc
= 0; mc
< mchildren
; mc
++) {
3337 char *mpath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
3340 for (sc
= 0; sc
< schildren
; sc
++) {
3341 char *spath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
3343 boolean_t result
= (strcmp(mpath
, spath
) == 0);
3359 * Split a mirror pool. If newroot points to null, then a new nvlist
3360 * is generated and it is the responsibility of the caller to free it.
3363 zpool_vdev_split(zpool_handle_t
*zhp
, char *newname
, nvlist_t
**newroot
,
3364 nvlist_t
*props
, splitflags_t flags
)
3366 zfs_cmd_t zc
= {"\0"};
3368 nvlist_t
*tree
, *config
, **child
, **newchild
, *newconfig
= NULL
;
3369 nvlist_t
**varray
= NULL
, *zc_props
= NULL
;
3370 uint_t c
, children
, newchildren
, lastlog
= 0, vcount
, found
= 0;
3371 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3372 uint64_t vers
, readonly
= B_FALSE
;
3373 boolean_t freelist
= B_FALSE
, memory_err
= B_TRUE
;
3376 (void) snprintf(msg
, sizeof (msg
),
3377 dgettext(TEXT_DOMAIN
, "Unable to split %s"), zhp
->zpool_name
);
3379 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
3380 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
3382 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
3383 (void) fprintf(stderr
, gettext("Internal error: unable to "
3384 "retrieve pool configuration\n"));
3388 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &tree
)
3390 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
, &vers
) == 0);
3393 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
3394 if ((zc_props
= zpool_valid_proplist(hdl
, zhp
->zpool_name
,
3395 props
, vers
, flags
, msg
)) == NULL
)
3397 (void) nvlist_lookup_uint64(zc_props
,
3398 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
3400 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3401 "property %s can only be set at import time"),
3402 zpool_prop_to_name(ZPOOL_PROP_READONLY
));
3407 if (nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
3409 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3410 "Source pool is missing vdev tree"));
3411 nvlist_free(zc_props
);
3415 varray
= zfs_alloc(hdl
, children
* sizeof (nvlist_t
*));
3418 if (*newroot
== NULL
||
3419 nvlist_lookup_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
3420 &newchild
, &newchildren
) != 0)
3423 for (c
= 0; c
< children
; c
++) {
3424 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
3426 nvlist_t
**mchild
, *vdev
;
3431 * Unlike cache & spares, slogs are stored in the
3432 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3434 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
3436 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
3438 if (is_log
|| is_hole
) {
3440 * Create a hole vdev and put it in the config.
3442 if (nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) != 0)
3444 if (nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
,
3445 VDEV_TYPE_HOLE
) != 0)
3447 if (nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_HOLE
,
3452 varray
[vcount
++] = vdev
;
3456 verify(nvlist_lookup_string(child
[c
], ZPOOL_CONFIG_TYPE
, &type
)
3458 if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
3459 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3460 "Source pool must be composed only of mirrors\n"));
3461 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3465 verify(nvlist_lookup_nvlist_array(child
[c
],
3466 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
3468 /* find or add an entry for this top-level vdev */
3469 if (newchildren
> 0 &&
3470 (entry
= find_vdev_entry(zhp
, mchild
, mchildren
,
3471 newchild
, newchildren
)) >= 0) {
3472 /* We found a disk that the user specified. */
3473 vdev
= mchild
[entry
];
3476 /* User didn't specify a disk for this vdev. */
3477 vdev
= mchild
[mchildren
- 1];
3480 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
3484 /* did we find every disk the user specified? */
3485 if (found
!= newchildren
) {
3486 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Device list must "
3487 "include at most one disk from each mirror"));
3488 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3492 /* Prepare the nvlist for populating. */
3493 if (*newroot
== NULL
) {
3494 if (nvlist_alloc(newroot
, NV_UNIQUE_NAME
, 0) != 0)
3497 if (nvlist_add_string(*newroot
, ZPOOL_CONFIG_TYPE
,
3498 VDEV_TYPE_ROOT
) != 0)
3501 verify(nvlist_remove_all(*newroot
, ZPOOL_CONFIG_CHILDREN
) == 0);
3504 /* Add all the children we found */
3505 if (nvlist_add_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
, varray
,
3506 lastlog
== 0 ? vcount
: lastlog
) != 0)
3510 * If we're just doing a dry run, exit now with success.
3513 memory_err
= B_FALSE
;
3518 /* now build up the config list & call the ioctl */
3519 if (nvlist_alloc(&newconfig
, NV_UNIQUE_NAME
, 0) != 0)
3522 if (nvlist_add_nvlist(newconfig
,
3523 ZPOOL_CONFIG_VDEV_TREE
, *newroot
) != 0 ||
3524 nvlist_add_string(newconfig
,
3525 ZPOOL_CONFIG_POOL_NAME
, newname
) != 0 ||
3526 nvlist_add_uint64(newconfig
, ZPOOL_CONFIG_VERSION
, vers
) != 0)
3530 * The new pool is automatically part of the namespace unless we
3531 * explicitly export it.
3534 zc
.zc_cookie
= ZPOOL_EXPORT_AFTER_SPLIT
;
3535 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3536 (void) strlcpy(zc
.zc_string
, newname
, sizeof (zc
.zc_string
));
3537 if (zcmd_write_conf_nvlist(hdl
, &zc
, newconfig
) != 0)
3539 if (zc_props
!= NULL
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
3542 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SPLIT
, &zc
) != 0) {
3543 retval
= zpool_standard_error(hdl
, errno
, msg
);
3548 memory_err
= B_FALSE
;
3551 if (varray
!= NULL
) {
3554 for (v
= 0; v
< vcount
; v
++)
3555 nvlist_free(varray
[v
]);
3558 zcmd_free_nvlists(&zc
);
3559 nvlist_free(zc_props
);
3560 nvlist_free(newconfig
);
3562 nvlist_free(*newroot
);
3570 return (no_memory(hdl
));
3576 * Remove the given device.
3579 zpool_vdev_remove(zpool_handle_t
*zhp
, const char *path
)
3581 zfs_cmd_t zc
= {"\0"};
3584 boolean_t avail_spare
, l2cache
, islog
;
3585 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3588 (void) snprintf(msg
, sizeof (msg
),
3589 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3591 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3592 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3594 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3596 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
3597 if (islog
&& version
< SPA_VERSION_HOLES
) {
3598 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3599 "pool must be upgraded to support log removal"));
3600 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
3603 if (!islog
&& !avail_spare
&& !l2cache
&& zpool_is_bootable(zhp
)) {
3604 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3605 "root pool can not have removed devices, "
3606 "because GRUB does not understand them"));
3607 return (zfs_error(hdl
, EINVAL
, msg
));
3610 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3612 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3618 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3619 "invalid config; all top-level vdevs must "
3620 "have the same sector size and not be raidz."));
3621 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3626 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3627 "Mount encrypted datasets to replay logs."));
3629 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3630 "Pool busy; removal may already be in progress"));
3632 (void) zfs_error(hdl
, EZFS_BUSY
, msg
);
3637 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3638 "Mount encrypted datasets to replay logs."));
3639 (void) zfs_error(hdl
, EZFS_BUSY
, msg
);
3641 (void) zpool_standard_error(hdl
, errno
, msg
);
3646 (void) zpool_standard_error(hdl
, errno
, msg
);
3652 zpool_vdev_remove_cancel(zpool_handle_t
*zhp
)
3656 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3658 (void) snprintf(msg
, sizeof (msg
),
3659 dgettext(TEXT_DOMAIN
, "cannot cancel removal"));
3661 bzero(&zc
, sizeof (zc
));
3662 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3665 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3668 return (zpool_standard_error(hdl
, errno
, msg
));
3672 zpool_vdev_indirect_size(zpool_handle_t
*zhp
, const char *path
,
3677 boolean_t avail_spare
, l2cache
, islog
;
3678 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3680 (void) snprintf(msg
, sizeof (msg
),
3681 dgettext(TEXT_DOMAIN
, "cannot determine indirect size of %s"),
3684 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3686 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3688 if (avail_spare
|| l2cache
|| islog
) {
3693 if (nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_INDIRECT_SIZE
, sizep
) != 0) {
3694 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3695 "indirect size not available"));
3696 return (zfs_error(hdl
, EINVAL
, msg
));
3702 * Clear the errors for the pool, or the particular device if specified.
3705 zpool_clear(zpool_handle_t
*zhp
, const char *path
, nvlist_t
*rewindnvl
)
3707 zfs_cmd_t zc
= {"\0"};
3710 zpool_load_policy_t policy
;
3711 boolean_t avail_spare
, l2cache
;
3712 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3713 nvlist_t
*nvi
= NULL
;
3717 (void) snprintf(msg
, sizeof (msg
),
3718 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3721 (void) snprintf(msg
, sizeof (msg
),
3722 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3725 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3727 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
,
3728 &l2cache
, NULL
)) == NULL
)
3729 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3732 * Don't allow error clearing for hot spares. Do allow
3733 * error clearing for l2cache devices.
3736 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3738 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
,
3742 zpool_get_load_policy(rewindnvl
, &policy
);
3743 zc
.zc_cookie
= policy
.zlp_rewind
;
3745 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zhp
->zpool_config_size
* 2) != 0)
3748 if (zcmd_write_src_nvlist(hdl
, &zc
, rewindnvl
) != 0)
3751 while ((error
= zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
)) != 0 &&
3753 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
3754 zcmd_free_nvlists(&zc
);
3759 if (!error
|| ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) &&
3760 errno
!= EPERM
&& errno
!= EACCES
)) {
3761 if (policy
.zlp_rewind
&
3762 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
3763 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nvi
);
3764 zpool_rewind_exclaim(hdl
, zc
.zc_name
,
3765 ((policy
.zlp_rewind
& ZPOOL_TRY_REWIND
) != 0),
3769 zcmd_free_nvlists(&zc
);
3773 zcmd_free_nvlists(&zc
);
3774 return (zpool_standard_error(hdl
, errno
, msg
));
3778 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3781 zpool_vdev_clear(zpool_handle_t
*zhp
, uint64_t guid
)
3783 zfs_cmd_t zc
= {"\0"};
3785 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3787 (void) snprintf(msg
, sizeof (msg
),
3788 dgettext(TEXT_DOMAIN
, "cannot clear errors for %llx"),
3789 (u_longlong_t
)guid
);
3791 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3793 zc
.zc_cookie
= ZPOOL_NO_REWIND
;
3795 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_CLEAR
, &zc
) == 0)
3798 return (zpool_standard_error(hdl
, errno
, msg
));
3802 * Change the GUID for a pool.
3805 zpool_reguid(zpool_handle_t
*zhp
)
3808 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3809 zfs_cmd_t zc
= {"\0"};
3811 (void) snprintf(msg
, sizeof (msg
),
3812 dgettext(TEXT_DOMAIN
, "cannot reguid '%s'"), zhp
->zpool_name
);
3814 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3815 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REGUID
, &zc
) == 0)
3818 return (zpool_standard_error(hdl
, errno
, msg
));
3825 zpool_reopen_one(zpool_handle_t
*zhp
, void *data
)
3827 libzfs_handle_t
*hdl
= zpool_get_handle(zhp
);
3828 const char *pool_name
= zpool_get_name(zhp
);
3829 boolean_t
*scrub_restart
= data
;
3832 error
= lzc_reopen(pool_name
, *scrub_restart
);
3834 return (zpool_standard_error_fmt(hdl
, error
,
3835 dgettext(TEXT_DOMAIN
, "cannot reopen '%s'"), pool_name
));
3841 /* call into libzfs_core to execute the sync IOCTL per pool */
3843 zpool_sync_one(zpool_handle_t
*zhp
, void *data
)
3846 libzfs_handle_t
*hdl
= zpool_get_handle(zhp
);
3847 const char *pool_name
= zpool_get_name(zhp
);
3848 boolean_t
*force
= data
;
3849 nvlist_t
*innvl
= fnvlist_alloc();
3851 fnvlist_add_boolean_value(innvl
, "force", *force
);
3852 if ((ret
= lzc_sync(pool_name
, innvl
, NULL
)) != 0) {
3854 return (zpool_standard_error_fmt(hdl
, ret
,
3855 dgettext(TEXT_DOMAIN
, "sync '%s' failed"), pool_name
));
3862 #if defined(__sun__) || defined(__sun)
3864 * Convert from a devid string to a path.
3867 devid_to_path(char *devid_str
)
3872 devid_nmlist_t
*list
= NULL
;
3875 if (devid_str_decode(devid_str
, &devid
, &minor
) != 0)
3878 ret
= devid_deviceid_to_nmlist("/dev", devid
, minor
, &list
);
3880 devid_str_free(minor
);
3887 * In a case the strdup() fails, we will just return NULL below.
3889 path
= strdup(list
[0].devname
);
3891 devid_free_nmlist(list
);
3897 * Convert from a path to a devid string.
3900 path_to_devid(const char *path
)
3906 if ((fd
= open(path
, O_RDONLY
)) < 0)
3911 if (devid_get(fd
, &devid
) == 0) {
3912 if (devid_get_minor_name(fd
, &minor
) == 0)
3913 ret
= devid_str_encode(devid
, minor
);
3915 devid_str_free(minor
);
3924 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3925 * ignore any failure here, since a common case is for an unprivileged user to
3926 * type 'zpool status', and we'll display the correct information anyway.
3929 set_path(zpool_handle_t
*zhp
, nvlist_t
*nv
, const char *path
)
3931 zfs_cmd_t zc
= {"\0"};
3933 (void) strncpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3934 (void) strncpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
3935 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
3938 (void) ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_VDEV_SETPATH
, &zc
);
3942 #define PATH_BUF_LEN 64
3945 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3946 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3947 * We also check if this is a whole disk, in which case we strip off the
3948 * trailing 's0' slice name.
3950 * This routine is also responsible for identifying when disks have been
3951 * reconfigured in a new location. The kernel will have opened the device by
3952 * devid, but the path will still refer to the old location. To catch this, we
3953 * first do a path -> devid translation (which is fast for the common case). If
3954 * the devid matches, we're done. If not, we do a reverse devid -> path
3955 * translation and issue the appropriate ioctl() to update the path of the vdev.
3956 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3960 zpool_vdev_name(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, nvlist_t
*nv
,
3963 char *path
, *type
, *env
;
3965 char buf
[PATH_BUF_LEN
];
3966 char tmpbuf
[PATH_BUF_LEN
];
3969 * vdev_name will be "root"/"root-0" for the root vdev, but it is the
3970 * zpool name that will be displayed to the user.
3972 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
3973 if (zhp
!= NULL
&& strcmp(type
, "root") == 0)
3974 return (zfs_strdup(hdl
, zpool_get_name(zhp
)));
3976 env
= getenv("ZPOOL_VDEV_NAME_PATH");
3977 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3978 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3979 name_flags
|= VDEV_NAME_PATH
;
3981 env
= getenv("ZPOOL_VDEV_NAME_GUID");
3982 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3983 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3984 name_flags
|= VDEV_NAME_GUID
;
3986 env
= getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
3987 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3988 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3989 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
3991 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
, &value
) == 0 ||
3992 name_flags
& VDEV_NAME_GUID
) {
3993 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
);
3994 (void) snprintf(buf
, sizeof (buf
), "%llu", (u_longlong_t
)value
);
3996 } else if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
3997 #if defined(__sun__) || defined(__sun)
3999 * Live VDEV path updates to a kernel VDEV during a
4000 * zpool_vdev_name lookup are not supported on Linux.
4007 * If the device is dead (faulted, offline, etc) then don't
4008 * bother opening it. Otherwise we may be forcing the user to
4009 * open a misbehaving device, which can have undesirable
4012 if ((nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
4013 (uint64_t **)&vs
, &vsc
) != 0 ||
4014 vs
->vs_state
>= VDEV_STATE_DEGRADED
) &&
4016 nvlist_lookup_string(nv
, ZPOOL_CONFIG_DEVID
, &devid
) == 0) {
4018 * Determine if the current path is correct.
4020 char *newdevid
= path_to_devid(path
);
4022 if (newdevid
== NULL
||
4023 strcmp(devid
, newdevid
) != 0) {
4026 if ((newpath
= devid_to_path(devid
)) != NULL
) {
4028 * Update the path appropriately.
4030 set_path(zhp
, nv
, newpath
);
4031 if (nvlist_add_string(nv
,
4032 ZPOOL_CONFIG_PATH
, newpath
) == 0)
4033 verify(nvlist_lookup_string(nv
,
4041 devid_str_free(newdevid
);
4045 if (name_flags
& VDEV_NAME_FOLLOW_LINKS
) {
4046 char *rp
= realpath(path
, NULL
);
4048 strlcpy(buf
, rp
, sizeof (buf
));
4055 * For a block device only use the name.
4057 if ((strcmp(type
, VDEV_TYPE_DISK
) == 0) &&
4058 !(name_flags
& VDEV_NAME_PATH
)) {
4059 path
= strrchr(path
, '/');
4064 * Remove the partition from the path it this is a whole disk.
4066 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
, &value
)
4067 == 0 && value
&& !(name_flags
& VDEV_NAME_PATH
)) {
4068 return (zfs_strip_partition(path
));
4074 * If it's a raidz device, we need to stick in the parity level.
4076 if (strcmp(path
, VDEV_TYPE_RAIDZ
) == 0) {
4077 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
,
4079 (void) snprintf(buf
, sizeof (buf
), "%s%llu", path
,
4080 (u_longlong_t
)value
);
4085 * We identify each top-level vdev by using a <type-id>
4086 * naming convention.
4088 if (name_flags
& VDEV_NAME_TYPE_ID
) {
4090 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
4092 (void) snprintf(tmpbuf
, sizeof (tmpbuf
), "%s-%llu",
4093 path
, (u_longlong_t
)id
);
4098 return (zfs_strdup(hdl
, path
));
4102 zbookmark_mem_compare(const void *a
, const void *b
)
4104 return (memcmp(a
, b
, sizeof (zbookmark_phys_t
)));
4108 * Retrieve the persistent error log, uniquify the members, and return to the
4112 zpool_get_errlog(zpool_handle_t
*zhp
, nvlist_t
**nverrlistp
)
4114 zfs_cmd_t zc
= {"\0"};
4115 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4117 zbookmark_phys_t
*zb
= NULL
;
4121 * Retrieve the raw error list from the kernel. If the number of errors
4122 * has increased, allocate more space and continue until we get the
4125 verify(nvlist_lookup_uint64(zhp
->zpool_config
, ZPOOL_CONFIG_ERRCOUNT
,
4129 zc
.zc_nvlist_dst
= (uintptr_t)zfs_alloc(zhp
->zpool_hdl
,
4130 count
* sizeof (zbookmark_phys_t
));
4131 zc
.zc_nvlist_dst_size
= count
;
4132 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
4134 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_ERROR_LOG
,
4136 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
4137 if (errno
== ENOMEM
) {
4140 count
= zc
.zc_nvlist_dst_size
;
4141 dst
= zfs_alloc(zhp
->zpool_hdl
, count
*
4142 sizeof (zbookmark_phys_t
));
4143 zc
.zc_nvlist_dst
= (uintptr_t)dst
;
4145 return (zpool_standard_error_fmt(hdl
, errno
,
4146 dgettext(TEXT_DOMAIN
, "errors: List of "
4147 "errors unavailable")));
4155 * Sort the resulting bookmarks. This is a little confusing due to the
4156 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
4157 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
4158 * _not_ copied as part of the process. So we point the start of our
4159 * array appropriate and decrement the total number of elements.
4161 zb
= ((zbookmark_phys_t
*)(uintptr_t)zc
.zc_nvlist_dst
) +
4162 zc
.zc_nvlist_dst_size
;
4163 count
-= zc
.zc_nvlist_dst_size
;
4165 qsort(zb
, count
, sizeof (zbookmark_phys_t
), zbookmark_mem_compare
);
4167 verify(nvlist_alloc(nverrlistp
, 0, KM_SLEEP
) == 0);
4170 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
4172 for (i
= 0; i
< count
; i
++) {
4175 /* ignoring zb_blkid and zb_level for now */
4176 if (i
> 0 && zb
[i
-1].zb_objset
== zb
[i
].zb_objset
&&
4177 zb
[i
-1].zb_object
== zb
[i
].zb_object
)
4180 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) != 0)
4182 if (nvlist_add_uint64(nv
, ZPOOL_ERR_DATASET
,
4183 zb
[i
].zb_objset
) != 0) {
4187 if (nvlist_add_uint64(nv
, ZPOOL_ERR_OBJECT
,
4188 zb
[i
].zb_object
) != 0) {
4192 if (nvlist_add_nvlist(*nverrlistp
, "ejk", nv
) != 0) {
4199 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
4203 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
4204 return (no_memory(zhp
->zpool_hdl
));
4208 * Upgrade a ZFS pool to the latest on-disk version.
4211 zpool_upgrade(zpool_handle_t
*zhp
, uint64_t new_version
)
4213 zfs_cmd_t zc
= {"\0"};
4214 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4216 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
4217 zc
.zc_cookie
= new_version
;
4219 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_UPGRADE
, &zc
) != 0)
4220 return (zpool_standard_error_fmt(hdl
, errno
,
4221 dgettext(TEXT_DOMAIN
, "cannot upgrade '%s'"),
4227 zfs_save_arguments(int argc
, char **argv
, char *string
, int len
)
4231 (void) strlcpy(string
, basename(argv
[0]), len
);
4232 for (i
= 1; i
< argc
; i
++) {
4233 (void) strlcat(string
, " ", len
);
4234 (void) strlcat(string
, argv
[i
], len
);
4239 zpool_log_history(libzfs_handle_t
*hdl
, const char *message
)
4241 zfs_cmd_t zc
= {"\0"};
4245 args
= fnvlist_alloc();
4246 fnvlist_add_string(args
, "message", message
);
4247 err
= zcmd_write_src_nvlist(hdl
, &zc
, args
);
4249 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_LOG_HISTORY
, &zc
);
4251 zcmd_free_nvlists(&zc
);
4256 * Perform ioctl to get some command history of a pool.
4258 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
4259 * logical offset of the history buffer to start reading from.
4261 * Upon return, 'off' is the next logical offset to read from and
4262 * 'len' is the actual amount of bytes read into 'buf'.
4265 get_history(zpool_handle_t
*zhp
, char *buf
, uint64_t *off
, uint64_t *len
)
4267 zfs_cmd_t zc
= {"\0"};
4268 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
4270 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4272 zc
.zc_history
= (uint64_t)(uintptr_t)buf
;
4273 zc
.zc_history_len
= *len
;
4274 zc
.zc_history_offset
= *off
;
4276 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_HISTORY
, &zc
) != 0) {
4279 return (zfs_error_fmt(hdl
, EZFS_PERM
,
4280 dgettext(TEXT_DOMAIN
,
4281 "cannot show history for pool '%s'"),
4284 return (zfs_error_fmt(hdl
, EZFS_NOHISTORY
,
4285 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
4286 "'%s'"), zhp
->zpool_name
));
4288 return (zfs_error_fmt(hdl
, EZFS_BADVERSION
,
4289 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
4290 "'%s', pool must be upgraded"), zhp
->zpool_name
));
4292 return (zpool_standard_error_fmt(hdl
, errno
,
4293 dgettext(TEXT_DOMAIN
,
4294 "cannot get history for '%s'"), zhp
->zpool_name
));
4298 *len
= zc
.zc_history_len
;
4299 *off
= zc
.zc_history_offset
;
4305 * Retrieve the command history of a pool.
4308 zpool_get_history(zpool_handle_t
*zhp
, nvlist_t
**nvhisp
)
4311 int buflen
= 128 * 1024;
4313 nvlist_t
**records
= NULL
;
4314 uint_t numrecords
= 0;
4317 buf
= malloc(buflen
);
4321 uint64_t bytes_read
= buflen
;
4324 if ((err
= get_history(zhp
, buf
, &off
, &bytes_read
)) != 0)
4327 /* if nothing else was read in, we're at EOF, just return */
4331 if ((err
= zpool_history_unpack(buf
, bytes_read
,
4332 &leftover
, &records
, &numrecords
)) != 0)
4335 if (leftover
== bytes_read
) {
4337 * no progress made, because buffer is not big enough
4338 * to hold this record; resize and retry.
4342 buf
= malloc(buflen
);
4353 verify(nvlist_alloc(nvhisp
, NV_UNIQUE_NAME
, 0) == 0);
4354 verify(nvlist_add_nvlist_array(*nvhisp
, ZPOOL_HIST_RECORD
,
4355 records
, numrecords
) == 0);
4357 for (i
= 0; i
< numrecords
; i
++)
4358 nvlist_free(records
[i
]);
4365 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
4366 * If there is a new event available 'nvp' will contain a newly allocated
4367 * nvlist and 'dropped' will be set to the number of missed events since
4368 * the last call to this function. When 'nvp' is set to NULL it indicates
4369 * no new events are available. In either case the function returns 0 and
4370 * it is up to the caller to free 'nvp'. In the case of a fatal error the
4371 * function will return a non-zero value. When the function is called in
4372 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
4373 * it will not return until a new event is available.
4376 zpool_events_next(libzfs_handle_t
*hdl
, nvlist_t
**nvp
,
4377 int *dropped
, unsigned flags
, int zevent_fd
)
4379 zfs_cmd_t zc
= {"\0"};
4384 zc
.zc_cleanup_fd
= zevent_fd
;
4386 if (flags
& ZEVENT_NONBLOCK
)
4387 zc
.zc_guid
= ZEVENT_NONBLOCK
;
4389 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, ZEVENT_SIZE
) != 0)
4393 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_NEXT
, &zc
) != 0) {
4396 error
= zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
4397 dgettext(TEXT_DOMAIN
, "zfs shutdown"));
4400 /* Blocking error case should not occur */
4401 if (!(flags
& ZEVENT_NONBLOCK
))
4402 error
= zpool_standard_error_fmt(hdl
, errno
,
4403 dgettext(TEXT_DOMAIN
, "cannot get event"));
4407 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
4408 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
4409 dgettext(TEXT_DOMAIN
, "cannot get event"));
4415 error
= zpool_standard_error_fmt(hdl
, errno
,
4416 dgettext(TEXT_DOMAIN
, "cannot get event"));
4421 error
= zcmd_read_dst_nvlist(hdl
, &zc
, nvp
);
4425 *dropped
= (int)zc
.zc_cookie
;
4427 zcmd_free_nvlists(&zc
);
4436 zpool_events_clear(libzfs_handle_t
*hdl
, int *count
)
4438 zfs_cmd_t zc
= {"\0"};
4441 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
4442 "cannot clear events"));
4444 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_CLEAR
, &zc
) != 0)
4445 return (zpool_standard_error_fmt(hdl
, errno
, msg
));
4448 *count
= (int)zc
.zc_cookie
; /* # of events cleared */
4454 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4455 * the passed zevent_fd file handle. On success zero is returned,
4456 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4459 zpool_events_seek(libzfs_handle_t
*hdl
, uint64_t eid
, int zevent_fd
)
4461 zfs_cmd_t zc
= {"\0"};
4465 zc
.zc_cleanup_fd
= zevent_fd
;
4467 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_SEEK
, &zc
) != 0) {
4470 error
= zfs_error_fmt(hdl
, EZFS_NOENT
,
4471 dgettext(TEXT_DOMAIN
, "cannot get event"));
4475 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
4476 dgettext(TEXT_DOMAIN
, "cannot get event"));
4480 error
= zpool_standard_error_fmt(hdl
, errno
,
4481 dgettext(TEXT_DOMAIN
, "cannot get event"));
4490 zpool_obj_to_path(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4491 char *pathname
, size_t len
)
4493 zfs_cmd_t zc
= {"\0"};
4494 boolean_t mounted
= B_FALSE
;
4495 char *mntpnt
= NULL
;
4496 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
4499 /* special case for the MOS */
4500 (void) snprintf(pathname
, len
, "<metadata>:<0x%llx>",
4505 /* get the dataset's name */
4506 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4508 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
,
4509 ZFS_IOC_DSOBJ_TO_DSNAME
, &zc
) != 0) {
4510 /* just write out a path of two object numbers */
4511 (void) snprintf(pathname
, len
, "<0x%llx>:<0x%llx>",
4512 (longlong_t
)dsobj
, (longlong_t
)obj
);
4515 (void) strlcpy(dsname
, zc
.zc_value
, sizeof (dsname
));
4517 /* find out if the dataset is mounted */
4518 mounted
= is_mounted(zhp
->zpool_hdl
, dsname
, &mntpnt
);
4520 /* get the corrupted object's path */
4521 (void) strlcpy(zc
.zc_name
, dsname
, sizeof (zc
.zc_name
));
4523 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_OBJ_TO_PATH
,
4526 (void) snprintf(pathname
, len
, "%s%s", mntpnt
,
4529 (void) snprintf(pathname
, len
, "%s:%s",
4530 dsname
, zc
.zc_value
);
4533 (void) snprintf(pathname
, len
, "%s:<0x%llx>", dsname
,
4540 * Read the EFI label from the config, if a label does not exist then
4541 * pass back the error to the caller. If the caller has passed a non-NULL
4542 * diskaddr argument then we set it to the starting address of the EFI
4546 read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
)
4550 char diskname
[MAXPATHLEN
];
4553 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PATH
, &path
) != 0)
4556 (void) snprintf(diskname
, sizeof (diskname
), "%s%s", DISK_ROOT
,
4557 strrchr(path
, '/'));
4558 if ((fd
= open(diskname
, O_RDONLY
|O_DIRECT
)) >= 0) {
4559 struct dk_gpt
*vtoc
;
4561 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) >= 0) {
4563 *sb
= vtoc
->efi_parts
[0].p_start
;
4572 * determine where a partition starts on a disk in the current
4576 find_start_block(nvlist_t
*config
)
4580 diskaddr_t sb
= MAXOFFSET_T
;
4583 if (nvlist_lookup_nvlist_array(config
,
4584 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) != 0) {
4585 if (nvlist_lookup_uint64(config
,
4586 ZPOOL_CONFIG_WHOLE_DISK
,
4587 &wholedisk
) != 0 || !wholedisk
) {
4588 return (MAXOFFSET_T
);
4590 if (read_efi_label(config
, &sb
) < 0)
4595 for (c
= 0; c
< children
; c
++) {
4596 sb
= find_start_block(child
[c
]);
4597 if (sb
!= MAXOFFSET_T
) {
4601 return (MAXOFFSET_T
);
4605 zpool_label_disk_check(char *path
)
4607 struct dk_gpt
*vtoc
;
4610 if ((fd
= open(path
, O_RDONLY
|O_DIRECT
)) < 0)
4613 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) != 0) {
4618 if (vtoc
->efi_flags
& EFI_GPT_PRIMARY_CORRUPT
) {
4630 * Generate a unique partition name for the ZFS member. Partitions must
4631 * have unique names to ensure udev will be able to create symlinks under
4632 * /dev/disk/by-partlabel/ for all pool members. The partition names are
4633 * of the form <pool>-<unique-id>.
4636 zpool_label_name(char *label_name
, int label_size
)
4641 fd
= open("/dev/urandom", O_RDONLY
);
4643 if (read(fd
, &id
, sizeof (id
)) != sizeof (id
))
4650 id
= (((uint64_t)rand()) << 32) | (uint64_t)rand();
4652 snprintf(label_name
, label_size
, "zfs-%016llx", (u_longlong_t
)id
);
4656 * Label an individual disk. The name provided is the short name,
4657 * stripped of any leading /dev path.
4660 zpool_label_disk(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, char *name
)
4662 char path
[MAXPATHLEN
];
4663 struct dk_gpt
*vtoc
;
4665 size_t resv
= EFI_MIN_RESV_SIZE
;
4666 uint64_t slice_size
;
4667 diskaddr_t start_block
;
4670 /* prepare an error message just in case */
4671 (void) snprintf(errbuf
, sizeof (errbuf
),
4672 dgettext(TEXT_DOMAIN
, "cannot label '%s'"), name
);
4677 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
4678 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
4680 if (zhp
->zpool_start_block
== 0)
4681 start_block
= find_start_block(nvroot
);
4683 start_block
= zhp
->zpool_start_block
;
4684 zhp
->zpool_start_block
= start_block
;
4687 start_block
= NEW_START_BLOCK
;
4690 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4692 if ((fd
= open(path
, O_RDWR
|O_DIRECT
|O_EXCL
)) < 0) {
4694 * This shouldn't happen. We've long since verified that this
4695 * is a valid device.
4697 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
4698 "label '%s': unable to open device: %d"), path
, errno
);
4699 return (zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
));
4702 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, &vtoc
) != 0) {
4704 * The only way this can fail is if we run out of memory, or we
4705 * were unable to read the disk's capacity
4707 if (errno
== ENOMEM
)
4708 (void) no_memory(hdl
);
4711 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
4712 "label '%s': unable to read disk capacity"), path
);
4714 return (zfs_error(hdl
, EZFS_NOCAP
, errbuf
));
4717 slice_size
= vtoc
->efi_last_u_lba
+ 1;
4718 slice_size
-= EFI_MIN_RESV_SIZE
;
4719 if (start_block
== MAXOFFSET_T
)
4720 start_block
= NEW_START_BLOCK
;
4721 slice_size
-= start_block
;
4722 slice_size
= P2ALIGN(slice_size
, PARTITION_END_ALIGNMENT
);
4724 vtoc
->efi_parts
[0].p_start
= start_block
;
4725 vtoc
->efi_parts
[0].p_size
= slice_size
;
4728 * Why we use V_USR: V_BACKUP confuses users, and is considered
4729 * disposable by some EFI utilities (since EFI doesn't have a backup
4730 * slice). V_UNASSIGNED is supposed to be used only for zero size
4731 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4732 * etc. were all pretty specific. V_USR is as close to reality as we
4733 * can get, in the absence of V_OTHER.
4735 vtoc
->efi_parts
[0].p_tag
= V_USR
;
4736 zpool_label_name(vtoc
->efi_parts
[0].p_name
, EFI_PART_NAME_LEN
);
4738 vtoc
->efi_parts
[8].p_start
= slice_size
+ start_block
;
4739 vtoc
->efi_parts
[8].p_size
= resv
;
4740 vtoc
->efi_parts
[8].p_tag
= V_RESERVED
;
4742 rval
= efi_write(fd
, vtoc
);
4744 /* Flush the buffers to disk and invalidate the page cache. */
4746 (void) ioctl(fd
, BLKFLSBUF
);
4749 rval
= efi_rescan(fd
);
4752 * Some block drivers (like pcata) may not support EFI GPT labels.
4753 * Print out a helpful error message directing the user to manually
4754 * label the disk and give a specific slice.
4760 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "try using "
4761 "parted(8) and then provide a specific slice: %d"), rval
);
4762 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4768 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4769 (void) zfs_append_partition(path
, MAXPATHLEN
);
4771 /* Wait to udev to signal use the device has settled. */
4772 rval
= zpool_label_disk_wait(path
, DISK_LABEL_WAIT
);
4774 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "failed to "
4775 "detect device partitions on '%s': %d"), path
, rval
);
4776 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4779 /* We can't be to paranoid. Read the label back and verify it. */
4780 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4781 rval
= zpool_label_disk_check(path
);
4783 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "freshly written "
4784 "EFI label on '%s' is damaged. Ensure\nthis device "
4785 "is not in in use, and is functioning properly: %d"),
4787 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));