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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2013 by Delphix. All rights reserved.
40 #include <sys/efi_partition.h>
42 #include <sys/zfs_ioctl.h>
45 #include "zfs_namecheck.h"
47 #include "libzfs_impl.h"
48 #include "zfs_comutil.h"
49 #include "zfeature_common.h"
51 static int read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
);
53 typedef struct prop_flags
{
54 int create
:1; /* Validate property on creation */
55 int import
:1; /* Validate property on import */
59 * ====================================================================
60 * zpool property functions
61 * ====================================================================
65 zpool_get_all_props(zpool_handle_t
*zhp
)
67 zfs_cmd_t zc
= {"\0"};
68 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
70 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
72 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
75 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_PROPS
, &zc
) != 0) {
76 if (errno
== ENOMEM
) {
77 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
78 zcmd_free_nvlists(&zc
);
82 zcmd_free_nvlists(&zc
);
87 if (zcmd_read_dst_nvlist(hdl
, &zc
, &zhp
->zpool_props
) != 0) {
88 zcmd_free_nvlists(&zc
);
92 zcmd_free_nvlists(&zc
);
98 zpool_props_refresh(zpool_handle_t
*zhp
)
102 old_props
= zhp
->zpool_props
;
104 if (zpool_get_all_props(zhp
) != 0)
107 nvlist_free(old_props
);
112 zpool_get_prop_string(zpool_handle_t
*zhp
, zpool_prop_t prop
,
118 zprop_source_t source
;
120 nvl
= zhp
->zpool_props
;
121 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
122 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &ival
) == 0);
124 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
126 source
= ZPROP_SRC_DEFAULT
;
127 if ((value
= (char *)zpool_prop_default_string(prop
)) == NULL
)
138 zpool_get_prop_int(zpool_handle_t
*zhp
, zpool_prop_t prop
, zprop_source_t
*src
)
142 zprop_source_t source
;
144 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
)) {
146 * zpool_get_all_props() has most likely failed because
147 * the pool is faulted, but if all we need is the top level
148 * vdev's guid then get it from the zhp config nvlist.
150 if ((prop
== ZPOOL_PROP_GUID
) &&
151 (nvlist_lookup_nvlist(zhp
->zpool_config
,
152 ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0) &&
153 (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
)
157 return (zpool_prop_default_numeric(prop
));
160 nvl
= zhp
->zpool_props
;
161 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
162 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &value
) == 0);
164 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
166 source
= ZPROP_SRC_DEFAULT
;
167 value
= zpool_prop_default_numeric(prop
);
177 * Map VDEV STATE to printed strings.
180 zpool_state_to_name(vdev_state_t state
, vdev_aux_t aux
)
185 case VDEV_STATE_CLOSED
:
186 case VDEV_STATE_OFFLINE
:
187 return (gettext("OFFLINE"));
188 case VDEV_STATE_REMOVED
:
189 return (gettext("REMOVED"));
190 case VDEV_STATE_CANT_OPEN
:
191 if (aux
== VDEV_AUX_CORRUPT_DATA
|| aux
== VDEV_AUX_BAD_LOG
)
192 return (gettext("FAULTED"));
193 else if (aux
== VDEV_AUX_SPLIT_POOL
)
194 return (gettext("SPLIT"));
196 return (gettext("UNAVAIL"));
197 case VDEV_STATE_FAULTED
:
198 return (gettext("FAULTED"));
199 case VDEV_STATE_DEGRADED
:
200 return (gettext("DEGRADED"));
201 case VDEV_STATE_HEALTHY
:
202 return (gettext("ONLINE"));
205 return (gettext("UNKNOWN"));
209 * Map POOL STATE to printed strings.
212 zpool_pool_state_to_name(pool_state_t state
)
217 case POOL_STATE_ACTIVE
:
218 return (gettext("ACTIVE"));
219 case POOL_STATE_EXPORTED
:
220 return (gettext("EXPORTED"));
221 case POOL_STATE_DESTROYED
:
222 return (gettext("DESTROYED"));
223 case POOL_STATE_SPARE
:
224 return (gettext("SPARE"));
225 case POOL_STATE_L2CACHE
:
226 return (gettext("L2CACHE"));
227 case POOL_STATE_UNINITIALIZED
:
228 return (gettext("UNINITIALIZED"));
229 case POOL_STATE_UNAVAIL
:
230 return (gettext("UNAVAIL"));
231 case POOL_STATE_POTENTIALLY_ACTIVE
:
232 return (gettext("POTENTIALLY_ACTIVE"));
235 return (gettext("UNKNOWN"));
239 * API compatibility wrapper around zpool_get_prop_literal
242 zpool_get_prop(zpool_handle_t
*zhp
, zpool_prop_t prop
, char *buf
, size_t len
,
243 zprop_source_t
*srctype
)
245 return (zpool_get_prop_literal(zhp
, prop
, buf
, len
, srctype
, B_FALSE
));
249 * Get a zpool property value for 'prop' and return the value in
250 * a pre-allocated buffer.
253 zpool_get_prop_literal(zpool_handle_t
*zhp
, zpool_prop_t prop
, char *buf
,
254 size_t len
, zprop_source_t
*srctype
, boolean_t literal
)
258 zprop_source_t src
= ZPROP_SRC_NONE
;
263 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
265 case ZPOOL_PROP_NAME
:
266 (void) strlcpy(buf
, zpool_get_name(zhp
), len
);
269 case ZPOOL_PROP_HEALTH
:
270 (void) strlcpy(buf
, "FAULTED", len
);
273 case ZPOOL_PROP_GUID
:
274 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
275 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
278 case ZPOOL_PROP_ALTROOT
:
279 case ZPOOL_PROP_CACHEFILE
:
280 case ZPOOL_PROP_COMMENT
:
281 if (zhp
->zpool_props
!= NULL
||
282 zpool_get_all_props(zhp
) == 0) {
284 zpool_get_prop_string(zhp
, prop
, &src
),
292 (void) strlcpy(buf
, "-", len
);
301 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
) &&
302 prop
!= ZPOOL_PROP_NAME
)
305 switch (zpool_prop_get_type(prop
)) {
306 case PROP_TYPE_STRING
:
307 (void) strlcpy(buf
, zpool_get_prop_string(zhp
, prop
, &src
),
311 case PROP_TYPE_NUMBER
:
312 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
315 case ZPOOL_PROP_SIZE
:
316 case ZPOOL_PROP_ALLOCATED
:
317 case ZPOOL_PROP_FREE
:
318 case ZPOOL_PROP_FREEING
:
319 case ZPOOL_PROP_EXPANDSZ
:
320 case ZPOOL_PROP_ASHIFT
:
322 (void) snprintf(buf
, len
, "%llu",
323 (u_longlong_t
)intval
);
325 (void) zfs_nicenum(intval
, buf
, len
);
328 case ZPOOL_PROP_CAPACITY
:
329 (void) snprintf(buf
, len
, "%llu%%",
330 (u_longlong_t
)intval
);
333 case ZPOOL_PROP_DEDUPRATIO
:
334 (void) snprintf(buf
, len
, "%llu.%02llux",
335 (u_longlong_t
)(intval
/ 100),
336 (u_longlong_t
)(intval
% 100));
339 case ZPOOL_PROP_HEALTH
:
340 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
341 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
342 verify(nvlist_lookup_uint64_array(nvroot
,
343 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
346 (void) strlcpy(buf
, zpool_state_to_name(intval
,
349 case ZPOOL_PROP_VERSION
:
350 if (intval
>= SPA_VERSION_FEATURES
) {
351 (void) snprintf(buf
, len
, "-");
356 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
360 case PROP_TYPE_INDEX
:
361 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
362 if (zpool_prop_index_to_string(prop
, intval
, &strval
)
365 (void) strlcpy(buf
, strval
, len
);
379 * Check if the bootfs name has the same pool name as it is set to.
380 * Assuming bootfs is a valid dataset name.
383 bootfs_name_valid(const char *pool
, char *bootfs
)
385 int len
= strlen(pool
);
387 if (!zfs_name_valid(bootfs
, ZFS_TYPE_FILESYSTEM
|ZFS_TYPE_SNAPSHOT
))
390 if (strncmp(pool
, bootfs
, len
) == 0 &&
391 (bootfs
[len
] == '/' || bootfs
[len
] == '\0'))
397 #if defined(__sun__) || defined(__sun)
399 * Inspect the configuration to determine if any of the devices contain
403 pool_uses_efi(nvlist_t
*config
)
408 if (nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_CHILDREN
,
409 &child
, &children
) != 0)
410 return (read_efi_label(config
, NULL
) >= 0);
412 for (c
= 0; c
< children
; c
++) {
413 if (pool_uses_efi(child
[c
]))
421 zpool_is_bootable(zpool_handle_t
*zhp
)
423 char bootfs
[ZPOOL_MAXNAMELEN
];
425 return (zpool_get_prop(zhp
, ZPOOL_PROP_BOOTFS
, bootfs
,
426 sizeof (bootfs
), NULL
) == 0 && strncmp(bootfs
, "-",
427 sizeof (bootfs
)) != 0);
432 * Given an nvlist of zpool properties to be set, validate that they are
433 * correct, and parse any numeric properties (index, boolean, etc) if they are
434 * specified as strings.
437 zpool_valid_proplist(libzfs_handle_t
*hdl
, const char *poolname
,
438 nvlist_t
*props
, uint64_t version
, prop_flags_t flags
, char *errbuf
)
446 struct stat64 statbuf
;
450 if (nvlist_alloc(&retprops
, NV_UNIQUE_NAME
, 0) != 0) {
451 (void) no_memory(hdl
);
456 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
457 const char *propname
= nvpair_name(elem
);
459 prop
= zpool_name_to_prop(propname
);
460 if (prop
== ZPROP_INVAL
&& zpool_prop_feature(propname
)) {
462 char *fname
= strchr(propname
, '@') + 1;
464 err
= zfeature_lookup_name(fname
, NULL
);
466 ASSERT3U(err
, ==, ENOENT
);
467 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
468 "invalid feature '%s'"), fname
);
469 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
473 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
474 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
475 "'%s' must be a string"), propname
);
476 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
480 (void) nvpair_value_string(elem
, &strval
);
481 if (strcmp(strval
, ZFS_FEATURE_ENABLED
) != 0) {
482 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
483 "property '%s' can only be set to "
484 "'enabled'"), propname
);
485 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
489 if (nvlist_add_uint64(retprops
, propname
, 0) != 0) {
490 (void) no_memory(hdl
);
497 * Make sure this property is valid and applies to this type.
499 if (prop
== ZPROP_INVAL
) {
500 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
501 "invalid property '%s'"), propname
);
502 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
506 if (zpool_prop_readonly(prop
)) {
507 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
508 "is readonly"), propname
);
509 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
513 if (zprop_parse_value(hdl
, elem
, prop
, ZFS_TYPE_POOL
, retprops
,
514 &strval
, &intval
, errbuf
) != 0)
518 * Perform additional checking for specific properties.
523 case ZPOOL_PROP_VERSION
:
524 if (intval
< version
||
525 !SPA_VERSION_IS_SUPPORTED(intval
)) {
526 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
527 "property '%s' number %d is invalid."),
529 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
534 case ZPOOL_PROP_ASHIFT
:
536 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
537 "property '%s' can only be set at "
538 "creation time"), propname
);
539 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
543 if (intval
!= 0 && (intval
< 9 || intval
> 13)) {
544 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
545 "property '%s' number %d is invalid."),
547 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
552 case ZPOOL_PROP_BOOTFS
:
553 if (flags
.create
|| flags
.import
) {
554 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
555 "property '%s' cannot be set at creation "
556 "or import time"), propname
);
557 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
561 if (version
< SPA_VERSION_BOOTFS
) {
562 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
563 "pool must be upgraded to support "
564 "'%s' property"), propname
);
565 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
570 * bootfs property value has to be a dataset name and
571 * the dataset has to be in the same pool as it sets to.
573 if (strval
[0] != '\0' && !bootfs_name_valid(poolname
,
575 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
576 "is an invalid name"), strval
);
577 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
581 if ((zhp
= zpool_open_canfail(hdl
, poolname
)) == NULL
) {
582 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
583 "could not open pool '%s'"), poolname
);
584 (void) zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
);
587 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
588 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
590 #if defined(__sun__) || defined(__sun)
592 * bootfs property cannot be set on a disk which has
595 if (pool_uses_efi(nvroot
)) {
596 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
597 "property '%s' not supported on "
598 "EFI labeled devices"), propname
);
599 (void) zfs_error(hdl
, EZFS_POOL_NOTSUP
, errbuf
);
607 case ZPOOL_PROP_ALTROOT
:
608 if (!flags
.create
&& !flags
.import
) {
609 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
610 "property '%s' can only be set during pool "
611 "creation or import"), propname
);
612 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
616 if (strval
[0] != '/') {
617 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
618 "bad alternate root '%s'"), strval
);
619 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
624 case ZPOOL_PROP_CACHEFILE
:
625 if (strval
[0] == '\0')
628 if (strcmp(strval
, "none") == 0)
631 if (strval
[0] != '/') {
632 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
633 "property '%s' must be empty, an "
634 "absolute path, or 'none'"), propname
);
635 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
639 slash
= strrchr(strval
, '/');
641 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
642 strcmp(slash
, "/..") == 0) {
643 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
644 "'%s' is not a valid file"), strval
);
645 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
651 if (strval
[0] != '\0' &&
652 (stat64(strval
, &statbuf
) != 0 ||
653 !S_ISDIR(statbuf
.st_mode
))) {
654 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
655 "'%s' is not a valid directory"),
657 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
664 case ZPOOL_PROP_COMMENT
:
665 for (check
= strval
; *check
!= '\0'; check
++) {
666 if (!isprint(*check
)) {
668 dgettext(TEXT_DOMAIN
,
669 "comment may only have printable "
671 (void) zfs_error(hdl
, EZFS_BADPROP
,
676 if (strlen(strval
) > ZPROP_MAX_COMMENT
) {
677 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
678 "comment must not exceed %d characters"),
680 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
684 case ZPOOL_PROP_READONLY
:
686 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
687 "property '%s' can only be set at "
688 "import time"), propname
);
689 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
698 nvlist_free(retprops
);
703 * Set zpool property : propname=propval.
706 zpool_set_prop(zpool_handle_t
*zhp
, const char *propname
, const char *propval
)
708 zfs_cmd_t zc
= {"\0"};
711 nvlist_t
*nvl
= NULL
;
714 prop_flags_t flags
= { 0 };
716 (void) snprintf(errbuf
, sizeof (errbuf
),
717 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
720 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
721 return (no_memory(zhp
->zpool_hdl
));
723 if (nvlist_add_string(nvl
, propname
, propval
) != 0) {
725 return (no_memory(zhp
->zpool_hdl
));
728 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
729 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
730 zhp
->zpool_name
, nvl
, version
, flags
, errbuf
)) == NULL
) {
739 * Execute the corresponding ioctl() to set this property.
741 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
743 if (zcmd_write_src_nvlist(zhp
->zpool_hdl
, &zc
, nvl
) != 0) {
748 ret
= zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_SET_PROPS
, &zc
);
750 zcmd_free_nvlists(&zc
);
754 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
756 (void) zpool_props_refresh(zhp
);
762 zpool_expand_proplist(zpool_handle_t
*zhp
, zprop_list_t
**plp
)
764 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
766 char buf
[ZFS_MAXPROPLEN
];
767 nvlist_t
*features
= NULL
;
770 boolean_t firstexpand
= (NULL
== *plp
);
773 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_POOL
) != 0)
777 while (*last
!= NULL
)
778 last
= &(*last
)->pl_next
;
781 features
= zpool_get_features(zhp
);
783 if ((*plp
)->pl_all
&& firstexpand
) {
784 for (i
= 0; i
< SPA_FEATURES
; i
++) {
785 zprop_list_t
*entry
= zfs_alloc(hdl
,
786 sizeof (zprop_list_t
));
787 entry
->pl_prop
= ZPROP_INVAL
;
788 entry
->pl_user_prop
= zfs_asprintf(hdl
, "feature@%s",
789 spa_feature_table
[i
].fi_uname
);
790 entry
->pl_width
= strlen(entry
->pl_user_prop
);
791 entry
->pl_all
= B_TRUE
;
794 last
= &entry
->pl_next
;
798 /* add any unsupported features */
799 for (nvp
= nvlist_next_nvpair(features
, NULL
);
800 nvp
!= NULL
; nvp
= nvlist_next_nvpair(features
, nvp
)) {
805 if (zfeature_is_supported(nvpair_name(nvp
)))
808 propname
= zfs_asprintf(hdl
, "unsupported@%s",
812 * Before adding the property to the list make sure that no
813 * other pool already added the same property.
817 while (entry
!= NULL
) {
818 if (entry
->pl_user_prop
!= NULL
&&
819 strcmp(propname
, entry
->pl_user_prop
) == 0) {
823 entry
= entry
->pl_next
;
830 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
831 entry
->pl_prop
= ZPROP_INVAL
;
832 entry
->pl_user_prop
= propname
;
833 entry
->pl_width
= strlen(entry
->pl_user_prop
);
834 entry
->pl_all
= B_TRUE
;
837 last
= &entry
->pl_next
;
840 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
845 if (entry
->pl_prop
!= ZPROP_INVAL
&&
846 zpool_get_prop(zhp
, entry
->pl_prop
, buf
, sizeof (buf
),
848 if (strlen(buf
) > entry
->pl_width
)
849 entry
->pl_width
= strlen(buf
);
857 * Get the state for the given feature on the given ZFS pool.
860 zpool_prop_get_feature(zpool_handle_t
*zhp
, const char *propname
, char *buf
,
864 boolean_t found
= B_FALSE
;
865 nvlist_t
*features
= zpool_get_features(zhp
);
867 const char *feature
= strchr(propname
, '@') + 1;
869 supported
= zpool_prop_feature(propname
);
870 ASSERT(supported
|| zpool_prop_unsupported(propname
));
873 * Convert from feature name to feature guid. This conversion is
874 * unecessary for unsupported@... properties because they already
881 ret
= zfeature_lookup_name(feature
, &fid
);
883 (void) strlcpy(buf
, "-", len
);
886 feature
= spa_feature_table
[fid
].fi_guid
;
889 if (nvlist_lookup_uint64(features
, feature
, &refcount
) == 0)
894 (void) strlcpy(buf
, ZFS_FEATURE_DISABLED
, len
);
897 (void) strlcpy(buf
, ZFS_FEATURE_ENABLED
, len
);
899 (void) strlcpy(buf
, ZFS_FEATURE_ACTIVE
, len
);
904 (void) strcpy(buf
, ZFS_UNSUPPORTED_INACTIVE
);
906 (void) strcpy(buf
, ZFS_UNSUPPORTED_READONLY
);
909 (void) strlcpy(buf
, "-", len
);
918 * Don't start the slice at the default block of 34; many storage
919 * devices will use a stripe width of 128k, other vendors prefer a 1m
920 * alignment. It is best to play it safe and ensure a 1m alignment
921 * given 512B blocks. When the block size is larger by a power of 2
922 * we will still be 1m aligned. Some devices are sensitive to the
923 * partition ending alignment as well.
925 #define NEW_START_BLOCK 2048
926 #define PARTITION_END_ALIGNMENT 2048
929 * Validate the given pool name, optionally putting an extended error message in
933 zpool_name_valid(libzfs_handle_t
*hdl
, boolean_t isopen
, const char *pool
)
939 ret
= pool_namecheck(pool
, &why
, &what
);
942 * The rules for reserved pool names were extended at a later point.
943 * But we need to support users with existing pools that may now be
944 * invalid. So we only check for this expanded set of names during a
945 * create (or import), and only in userland.
947 if (ret
== 0 && !isopen
&&
948 (strncmp(pool
, "mirror", 6) == 0 ||
949 strncmp(pool
, "raidz", 5) == 0 ||
950 strncmp(pool
, "spare", 5) == 0 ||
951 strcmp(pool
, "log") == 0)) {
954 dgettext(TEXT_DOMAIN
, "name is reserved"));
962 case NAME_ERR_TOOLONG
:
964 dgettext(TEXT_DOMAIN
, "name is too long"));
967 case NAME_ERR_INVALCHAR
:
969 dgettext(TEXT_DOMAIN
, "invalid character "
970 "'%c' in pool name"), what
);
973 case NAME_ERR_NOLETTER
:
974 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
975 "name must begin with a letter"));
978 case NAME_ERR_RESERVED
:
979 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
980 "name is reserved"));
983 case NAME_ERR_DISKLIKE
:
984 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
985 "pool name is reserved"));
988 case NAME_ERR_LEADING_SLASH
:
989 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
990 "leading slash in name"));
993 case NAME_ERR_EMPTY_COMPONENT
:
994 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
995 "empty component in name"));
998 case NAME_ERR_TRAILING_SLASH
:
999 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1000 "trailing slash in name"));
1003 case NAME_ERR_MULTIPLE_AT
:
1004 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1005 "multiple '@' delimiters in name"));
1007 case NAME_ERR_NO_AT
:
1008 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1009 "permission set is missing '@'"));
1020 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1024 zpool_open_canfail(libzfs_handle_t
*hdl
, const char *pool
)
1026 zpool_handle_t
*zhp
;
1030 * Make sure the pool name is valid.
1032 if (!zpool_name_valid(hdl
, B_TRUE
, pool
)) {
1033 (void) zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1034 dgettext(TEXT_DOMAIN
, "cannot open '%s'"),
1039 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
1042 zhp
->zpool_hdl
= hdl
;
1043 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1045 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1051 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "no such pool"));
1052 (void) zfs_error_fmt(hdl
, EZFS_NOENT
,
1053 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), pool
);
1062 * Like the above, but silent on error. Used when iterating over pools (because
1063 * the configuration cache may be out of date).
1066 zpool_open_silent(libzfs_handle_t
*hdl
, const char *pool
, zpool_handle_t
**ret
)
1068 zpool_handle_t
*zhp
;
1071 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
1074 zhp
->zpool_hdl
= hdl
;
1075 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1077 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1093 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1097 zpool_open(libzfs_handle_t
*hdl
, const char *pool
)
1099 zpool_handle_t
*zhp
;
1101 if ((zhp
= zpool_open_canfail(hdl
, pool
)) == NULL
)
1104 if (zhp
->zpool_state
== POOL_STATE_UNAVAIL
) {
1105 (void) zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
1106 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), zhp
->zpool_name
);
1115 * Close the handle. Simply frees the memory associated with the handle.
1118 zpool_close(zpool_handle_t
*zhp
)
1120 if (zhp
->zpool_config
)
1121 nvlist_free(zhp
->zpool_config
);
1122 if (zhp
->zpool_old_config
)
1123 nvlist_free(zhp
->zpool_old_config
);
1124 if (zhp
->zpool_props
)
1125 nvlist_free(zhp
->zpool_props
);
1130 * Return the name of the pool.
1133 zpool_get_name(zpool_handle_t
*zhp
)
1135 return (zhp
->zpool_name
);
1140 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1143 zpool_get_state(zpool_handle_t
*zhp
)
1145 return (zhp
->zpool_state
);
1149 * Create the named pool, using the provided vdev list. It is assumed
1150 * that the consumer has already validated the contents of the nvlist, so we
1151 * don't have to worry about error semantics.
1154 zpool_create(libzfs_handle_t
*hdl
, const char *pool
, nvlist_t
*nvroot
,
1155 nvlist_t
*props
, nvlist_t
*fsprops
)
1157 zfs_cmd_t zc
= {"\0"};
1158 nvlist_t
*zc_fsprops
= NULL
;
1159 nvlist_t
*zc_props
= NULL
;
1163 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1164 "cannot create '%s'"), pool
);
1166 if (!zpool_name_valid(hdl
, B_FALSE
, pool
))
1167 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
1169 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1173 prop_flags_t flags
= { .create
= B_TRUE
, .import
= B_FALSE
};
1175 if ((zc_props
= zpool_valid_proplist(hdl
, pool
, props
,
1176 SPA_VERSION_1
, flags
, msg
)) == NULL
) {
1185 zoned
= ((nvlist_lookup_string(fsprops
,
1186 zfs_prop_to_name(ZFS_PROP_ZONED
), &zonestr
) == 0) &&
1187 strcmp(zonestr
, "on") == 0);
1189 if ((zc_fsprops
= zfs_valid_proplist(hdl
,
1190 ZFS_TYPE_FILESYSTEM
, fsprops
, zoned
, NULL
, msg
)) == NULL
) {
1194 (nvlist_alloc(&zc_props
, NV_UNIQUE_NAME
, 0) != 0)) {
1197 if (nvlist_add_nvlist(zc_props
,
1198 ZPOOL_ROOTFS_PROPS
, zc_fsprops
) != 0) {
1203 if (zc_props
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
1206 (void) strlcpy(zc
.zc_name
, pool
, sizeof (zc
.zc_name
));
1208 if ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_CREATE
, &zc
)) != 0) {
1210 zcmd_free_nvlists(&zc
);
1211 nvlist_free(zc_props
);
1212 nvlist_free(zc_fsprops
);
1217 * This can happen if the user has specified the same
1218 * device multiple times. We can't reliably detect this
1219 * until we try to add it and see we already have a
1220 * label. This can also happen under if the device is
1221 * part of an active md or lvm device.
1223 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1224 "one or more vdevs refer to the same device, or "
1225 "one of\nthe devices is part of an active md or "
1227 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1231 * This occurs when one of the devices is below
1232 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1233 * device was the problem device since there's no
1234 * reliable way to determine device size from userland.
1239 zfs_nicenum(SPA_MINDEVSIZE
, buf
, sizeof (buf
));
1241 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1242 "one or more devices is less than the "
1243 "minimum size (%s)"), buf
);
1245 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1248 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1249 "one or more devices is out of space"));
1250 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1253 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1254 "cache device must be a disk or disk slice"));
1255 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1258 return (zpool_standard_error(hdl
, errno
, msg
));
1263 zcmd_free_nvlists(&zc
);
1264 nvlist_free(zc_props
);
1265 nvlist_free(zc_fsprops
);
1270 * Destroy the given pool. It is up to the caller to ensure that there are no
1271 * datasets left in the pool.
1274 zpool_destroy(zpool_handle_t
*zhp
, const char *log_str
)
1276 zfs_cmd_t zc
= {"\0"};
1277 zfs_handle_t
*zfp
= NULL
;
1278 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1281 if (zhp
->zpool_state
== POOL_STATE_ACTIVE
&&
1282 (zfp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
1285 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1286 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1288 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_DESTROY
, &zc
) != 0) {
1289 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1290 "cannot destroy '%s'"), zhp
->zpool_name
);
1292 if (errno
== EROFS
) {
1293 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1294 "one or more devices is read only"));
1295 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1297 (void) zpool_standard_error(hdl
, errno
, msg
);
1306 remove_mountpoint(zfp
);
1314 * Add the given vdevs to the pool. The caller must have already performed the
1315 * necessary verification to ensure that the vdev specification is well-formed.
1318 zpool_add(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
1320 zfs_cmd_t zc
= {"\0"};
1322 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1324 nvlist_t
**spares
, **l2cache
;
1325 uint_t nspares
, nl2cache
;
1327 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1328 "cannot add to '%s'"), zhp
->zpool_name
);
1330 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1331 SPA_VERSION_SPARES
&&
1332 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1333 &spares
, &nspares
) == 0) {
1334 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1335 "upgraded to add hot spares"));
1336 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1339 #if defined(__sun__) || defined(__sun)
1340 if (zpool_is_bootable(zhp
) && nvlist_lookup_nvlist_array(nvroot
,
1341 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0) {
1344 for (s
= 0; s
< nspares
; s
++) {
1347 if (nvlist_lookup_string(spares
[s
], ZPOOL_CONFIG_PATH
,
1348 &path
) == 0 && pool_uses_efi(spares
[s
])) {
1349 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1350 "device '%s' contains an EFI label and "
1351 "cannot be used on root pools."),
1352 zpool_vdev_name(hdl
, NULL
, spares
[s
],
1354 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, msg
));
1360 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1361 SPA_VERSION_L2CACHE
&&
1362 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1363 &l2cache
, &nl2cache
) == 0) {
1364 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1365 "upgraded to add cache devices"));
1366 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1369 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1371 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1373 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_ADD
, &zc
) != 0) {
1377 * This can happen if the user has specified the same
1378 * device multiple times. We can't reliably detect this
1379 * until we try to add it and see we already have a
1382 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1383 "one or more vdevs refer to the same device"));
1384 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1389 * This occurrs when one of the devices is below
1390 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1391 * device was the problem device since there's no
1392 * reliable way to determine device size from userland.
1397 zfs_nicenum(SPA_MINDEVSIZE
, buf
, sizeof (buf
));
1399 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1400 "device is less than the minimum "
1403 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1407 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1408 "pool must be upgraded to add these vdevs"));
1409 (void) zfs_error(hdl
, EZFS_BADVERSION
, msg
);
1413 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1414 "cache device must be a disk or disk slice"));
1415 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1419 (void) zpool_standard_error(hdl
, errno
, msg
);
1427 zcmd_free_nvlists(&zc
);
1433 * Exports the pool from the system. The caller must ensure that there are no
1434 * mounted datasets in the pool.
1437 zpool_export_common(zpool_handle_t
*zhp
, boolean_t force
, boolean_t hardforce
,
1438 const char *log_str
)
1440 zfs_cmd_t zc
= {"\0"};
1443 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1444 "cannot export '%s'"), zhp
->zpool_name
);
1446 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1447 zc
.zc_cookie
= force
;
1448 zc
.zc_guid
= hardforce
;
1449 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1451 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_EXPORT
, &zc
) != 0) {
1454 zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
1455 "use '-f' to override the following errors:\n"
1456 "'%s' has an active shared spare which could be"
1457 " used by other pools once '%s' is exported."),
1458 zhp
->zpool_name
, zhp
->zpool_name
);
1459 return (zfs_error(zhp
->zpool_hdl
, EZFS_ACTIVE_SPARE
,
1462 return (zpool_standard_error_fmt(zhp
->zpool_hdl
, errno
,
1471 zpool_export(zpool_handle_t
*zhp
, boolean_t force
, const char *log_str
)
1473 return (zpool_export_common(zhp
, force
, B_FALSE
, log_str
));
1477 zpool_export_force(zpool_handle_t
*zhp
, const char *log_str
)
1479 return (zpool_export_common(zhp
, B_TRUE
, B_TRUE
, log_str
));
1483 zpool_rewind_exclaim(libzfs_handle_t
*hdl
, const char *name
, boolean_t dryrun
,
1486 nvlist_t
*nv
= NULL
;
1492 if (!hdl
->libzfs_printerr
|| config
== NULL
)
1495 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1496 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0) {
1500 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1502 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1504 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1505 strftime(timestr
, 128, "%c", &t
) != 0) {
1507 (void) printf(dgettext(TEXT_DOMAIN
,
1508 "Would be able to return %s "
1509 "to its state as of %s.\n"),
1512 (void) printf(dgettext(TEXT_DOMAIN
,
1513 "Pool %s returned to its state as of %s.\n"),
1517 (void) printf(dgettext(TEXT_DOMAIN
,
1518 "%s approximately %lld "),
1519 dryrun
? "Would discard" : "Discarded",
1520 ((longlong_t
)loss
+ 30) / 60);
1521 (void) printf(dgettext(TEXT_DOMAIN
,
1522 "minutes of transactions.\n"));
1523 } else if (loss
> 0) {
1524 (void) printf(dgettext(TEXT_DOMAIN
,
1525 "%s approximately %lld "),
1526 dryrun
? "Would discard" : "Discarded",
1528 (void) printf(dgettext(TEXT_DOMAIN
,
1529 "seconds of transactions.\n"));
1535 zpool_explain_recover(libzfs_handle_t
*hdl
, const char *name
, int reason
,
1538 nvlist_t
*nv
= NULL
;
1540 uint64_t edata
= UINT64_MAX
;
1545 if (!hdl
->libzfs_printerr
)
1549 (void) printf(dgettext(TEXT_DOMAIN
, "action: "));
1551 (void) printf(dgettext(TEXT_DOMAIN
, "\t"));
1553 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1554 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1555 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0 ||
1556 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1559 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1560 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_DATA_ERRORS
,
1563 (void) printf(dgettext(TEXT_DOMAIN
,
1564 "Recovery is possible, but will result in some data loss.\n"));
1566 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1567 strftime(timestr
, 128, "%c", &t
) != 0) {
1568 (void) printf(dgettext(TEXT_DOMAIN
,
1569 "\tReturning the pool to its state as of %s\n"
1570 "\tshould correct the problem. "),
1573 (void) printf(dgettext(TEXT_DOMAIN
,
1574 "\tReverting the pool to an earlier state "
1575 "should correct the problem.\n\t"));
1579 (void) printf(dgettext(TEXT_DOMAIN
,
1580 "Approximately %lld minutes of data\n"
1581 "\tmust be discarded, irreversibly. "),
1582 ((longlong_t
)loss
+ 30) / 60);
1583 } else if (loss
> 0) {
1584 (void) printf(dgettext(TEXT_DOMAIN
,
1585 "Approximately %lld seconds of data\n"
1586 "\tmust be discarded, irreversibly. "),
1589 if (edata
!= 0 && edata
!= UINT64_MAX
) {
1591 (void) printf(dgettext(TEXT_DOMAIN
,
1592 "After rewind, at least\n"
1593 "\tone persistent user-data error will remain. "));
1595 (void) printf(dgettext(TEXT_DOMAIN
,
1596 "After rewind, several\n"
1597 "\tpersistent user-data errors will remain. "));
1600 (void) printf(dgettext(TEXT_DOMAIN
,
1601 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1602 reason
>= 0 ? "clear" : "import", name
);
1604 (void) printf(dgettext(TEXT_DOMAIN
,
1605 "A scrub of the pool\n"
1606 "\tis strongly recommended after recovery.\n"));
1610 (void) printf(dgettext(TEXT_DOMAIN
,
1611 "Destroy and re-create the pool from\n\ta backup source.\n"));
1615 * zpool_import() is a contracted interface. Should be kept the same
1618 * Applications should use zpool_import_props() to import a pool with
1619 * new properties value to be set.
1622 zpool_import(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1625 nvlist_t
*props
= NULL
;
1628 if (altroot
!= NULL
) {
1629 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
1630 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1631 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1635 if (nvlist_add_string(props
,
1636 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), altroot
) != 0 ||
1637 nvlist_add_string(props
,
1638 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
), "none") != 0) {
1640 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1641 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1646 ret
= zpool_import_props(hdl
, config
, newname
, props
,
1654 print_vdev_tree(libzfs_handle_t
*hdl
, const char *name
, nvlist_t
*nv
,
1660 uint64_t is_log
= 0;
1662 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
,
1666 (void) printf("\t%*s%s%s\n", indent
, "", name
,
1667 is_log
? " [log]" : "");
1669 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1670 &child
, &children
) != 0)
1673 for (c
= 0; c
< children
; c
++) {
1674 vname
= zpool_vdev_name(hdl
, NULL
, child
[c
], B_TRUE
);
1675 print_vdev_tree(hdl
, vname
, child
[c
], indent
+ 2);
1681 zpool_print_unsup_feat(nvlist_t
*config
)
1683 nvlist_t
*nvinfo
, *unsup_feat
;
1686 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) ==
1688 verify(nvlist_lookup_nvlist(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
,
1691 for (nvp
= nvlist_next_nvpair(unsup_feat
, NULL
); nvp
!= NULL
;
1692 nvp
= nvlist_next_nvpair(unsup_feat
, nvp
)) {
1695 verify(nvpair_type(nvp
) == DATA_TYPE_STRING
);
1696 verify(nvpair_value_string(nvp
, &desc
) == 0);
1698 if (strlen(desc
) > 0)
1699 (void) printf("\t%s (%s)\n", nvpair_name(nvp
), desc
);
1701 (void) printf("\t%s\n", nvpair_name(nvp
));
1706 * Import the given pool using the known configuration and a list of
1707 * properties to be set. The configuration should have come from
1708 * zpool_find_import(). The 'newname' parameters control whether the pool
1709 * is imported with a different name.
1712 zpool_import_props(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1713 nvlist_t
*props
, int flags
)
1715 zfs_cmd_t zc
= {"\0"};
1716 zpool_rewind_policy_t policy
;
1717 nvlist_t
*nv
= NULL
;
1718 nvlist_t
*nvinfo
= NULL
;
1719 nvlist_t
*missing
= NULL
;
1726 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1729 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1730 "cannot import pool '%s'"), origname
);
1732 if (newname
!= NULL
) {
1733 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
1734 return (zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1735 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1737 thename
= (char *)newname
;
1744 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
1746 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
1749 if ((props
= zpool_valid_proplist(hdl
, origname
,
1750 props
, version
, flags
, errbuf
)) == NULL
) {
1752 } else if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
1758 (void) strlcpy(zc
.zc_name
, thename
, sizeof (zc
.zc_name
));
1760 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1763 if (zcmd_write_conf_nvlist(hdl
, &zc
, config
) != 0) {
1767 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zc
.zc_nvlist_conf_size
* 2) != 0) {
1772 zc
.zc_cookie
= flags
;
1773 while ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_IMPORT
, &zc
)) != 0 &&
1775 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
1776 zcmd_free_nvlists(&zc
);
1783 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nv
);
1784 zpool_get_rewind_policy(config
, &policy
);
1790 * Dry-run failed, but we print out what success
1791 * looks like if we found a best txg
1793 if (policy
.zrp_request
& ZPOOL_TRY_REWIND
) {
1794 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1800 if (newname
== NULL
)
1801 (void) snprintf(desc
, sizeof (desc
),
1802 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1805 (void) snprintf(desc
, sizeof (desc
),
1806 dgettext(TEXT_DOMAIN
, "cannot import '%s' as '%s'"),
1811 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
1812 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1813 nvlist_exists(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
)) {
1814 (void) printf(dgettext(TEXT_DOMAIN
, "This "
1815 "pool uses the following feature(s) not "
1816 "supported by this system:\n"));
1817 zpool_print_unsup_feat(nv
);
1818 if (nvlist_exists(nvinfo
,
1819 ZPOOL_CONFIG_CAN_RDONLY
)) {
1820 (void) printf(dgettext(TEXT_DOMAIN
,
1821 "All unsupported features are only "
1822 "required for writing to the pool."
1823 "\nThe pool can be imported using "
1824 "'-o readonly=on'.\n"));
1828 * Unsupported version.
1830 (void) zfs_error(hdl
, EZFS_BADVERSION
, desc
);
1834 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, desc
);
1838 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1839 "one or more devices is read only"));
1840 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
1844 if (nv
&& nvlist_lookup_nvlist(nv
,
1845 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1846 nvlist_lookup_nvlist(nvinfo
,
1847 ZPOOL_CONFIG_MISSING_DEVICES
, &missing
) == 0) {
1848 (void) printf(dgettext(TEXT_DOMAIN
,
1849 "The devices below are missing, use "
1850 "'-m' to import the pool anyway:\n"));
1851 print_vdev_tree(hdl
, NULL
, missing
, 2);
1852 (void) printf("\n");
1854 (void) zpool_standard_error(hdl
, error
, desc
);
1858 (void) zpool_standard_error(hdl
, error
, desc
);
1862 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1863 "one or more devices are already in use\n"));
1864 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
1868 (void) zpool_standard_error(hdl
, error
, desc
);
1869 zpool_explain_recover(hdl
,
1870 newname
? origname
: thename
, -error
, nv
);
1877 zpool_handle_t
*zhp
;
1880 * This should never fail, but play it safe anyway.
1882 if (zpool_open_silent(hdl
, thename
, &zhp
) != 0)
1884 else if (zhp
!= NULL
)
1886 if (policy
.zrp_request
&
1887 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
1888 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1889 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0), nv
);
1895 zcmd_free_nvlists(&zc
);
1905 zpool_scan(zpool_handle_t
*zhp
, pool_scan_func_t func
)
1907 zfs_cmd_t zc
= {"\0"};
1909 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1911 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1912 zc
.zc_cookie
= func
;
1914 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_SCAN
, &zc
) == 0 ||
1915 (errno
== ENOENT
&& func
!= POOL_SCAN_NONE
))
1918 if (func
== POOL_SCAN_SCRUB
) {
1919 (void) snprintf(msg
, sizeof (msg
),
1920 dgettext(TEXT_DOMAIN
, "cannot scrub %s"), zc
.zc_name
);
1921 } else if (func
== POOL_SCAN_NONE
) {
1922 (void) snprintf(msg
, sizeof (msg
),
1923 dgettext(TEXT_DOMAIN
, "cannot cancel scrubbing %s"),
1926 assert(!"unexpected result");
1929 if (errno
== EBUSY
) {
1931 pool_scan_stat_t
*ps
= NULL
;
1934 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
1935 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
1936 (void) nvlist_lookup_uint64_array(nvroot
,
1937 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &psc
);
1938 if (ps
&& ps
->pss_func
== POOL_SCAN_SCRUB
)
1939 return (zfs_error(hdl
, EZFS_SCRUBBING
, msg
));
1941 return (zfs_error(hdl
, EZFS_RESILVERING
, msg
));
1942 } else if (errno
== ENOENT
) {
1943 return (zfs_error(hdl
, EZFS_NO_SCRUB
, msg
));
1945 return (zpool_standard_error(hdl
, errno
, msg
));
1950 * Find a vdev that matches the search criteria specified. We use the
1951 * the nvpair name to determine how we should look for the device.
1952 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1953 * spare; but FALSE if its an INUSE spare.
1956 vdev_to_nvlist_iter(nvlist_t
*nv
, nvlist_t
*search
, boolean_t
*avail_spare
,
1957 boolean_t
*l2cache
, boolean_t
*log
)
1964 nvpair_t
*pair
= nvlist_next_nvpair(search
, NULL
);
1966 /* Nothing to look for */
1967 if (search
== NULL
|| pair
== NULL
)
1970 /* Obtain the key we will use to search */
1971 srchkey
= nvpair_name(pair
);
1973 switch (nvpair_type(pair
)) {
1974 case DATA_TYPE_UINT64
:
1975 if (strcmp(srchkey
, ZPOOL_CONFIG_GUID
) == 0) {
1976 uint64_t srchval
, theguid
;
1978 verify(nvpair_value_uint64(pair
, &srchval
) == 0);
1979 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1981 if (theguid
== srchval
)
1986 case DATA_TYPE_STRING
: {
1987 char *srchval
, *val
;
1989 verify(nvpair_value_string(pair
, &srchval
) == 0);
1990 if (nvlist_lookup_string(nv
, srchkey
, &val
) != 0)
1994 * Search for the requested value. Special cases:
1996 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
1997 * "-part1", or "p1". The suffix is hidden from the user,
1998 * but included in the string, so this matches around it.
1999 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2000 * is used to check all possible expanded paths.
2001 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2003 * Otherwise, all other searches are simple string compares.
2005 if (strcmp(srchkey
, ZPOOL_CONFIG_PATH
) == 0) {
2006 uint64_t wholedisk
= 0;
2008 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
2010 if (zfs_strcmp_pathname(srchval
, val
, wholedisk
) == 0)
2013 } else if (strcmp(srchkey
, ZPOOL_CONFIG_TYPE
) == 0 && val
) {
2014 char *type
, *idx
, *end
, *p
;
2015 uint64_t id
, vdev_id
;
2018 * Determine our vdev type, keeping in mind
2019 * that the srchval is composed of a type and
2020 * vdev id pair (i.e. mirror-4).
2022 if ((type
= strdup(srchval
)) == NULL
)
2025 if ((p
= strrchr(type
, '-')) == NULL
) {
2033 * If the types don't match then keep looking.
2035 if (strncmp(val
, type
, strlen(val
)) != 0) {
2040 verify(strncmp(type
, VDEV_TYPE_RAIDZ
,
2041 strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2042 strncmp(type
, VDEV_TYPE_MIRROR
,
2043 strlen(VDEV_TYPE_MIRROR
)) == 0);
2044 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
2048 vdev_id
= strtoull(idx
, &end
, 10);
2055 * Now verify that we have the correct vdev id.
2064 if (strcmp(srchval
, val
) == 0)
2073 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2074 &child
, &children
) != 0)
2077 for (c
= 0; c
< children
; c
++) {
2078 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2079 avail_spare
, l2cache
, NULL
)) != NULL
) {
2081 * The 'is_log' value is only set for the toplevel
2082 * vdev, not the leaf vdevs. So we always lookup the
2083 * log device from the root of the vdev tree (where
2084 * 'log' is non-NULL).
2087 nvlist_lookup_uint64(child
[c
],
2088 ZPOOL_CONFIG_IS_LOG
, &is_log
) == 0 &&
2096 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2097 &child
, &children
) == 0) {
2098 for (c
= 0; c
< children
; c
++) {
2099 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2100 avail_spare
, l2cache
, NULL
)) != NULL
) {
2101 *avail_spare
= B_TRUE
;
2107 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2108 &child
, &children
) == 0) {
2109 for (c
= 0; c
< children
; c
++) {
2110 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2111 avail_spare
, l2cache
, NULL
)) != NULL
) {
2122 * Given a physical path (minus the "/devices" prefix), find the
2126 zpool_find_vdev_by_physpath(zpool_handle_t
*zhp
, const char *ppath
,
2127 boolean_t
*avail_spare
, boolean_t
*l2cache
, boolean_t
*log
)
2129 nvlist_t
*search
, *nvroot
, *ret
;
2131 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2132 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PHYS_PATH
, ppath
) == 0);
2134 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2137 *avail_spare
= B_FALSE
;
2141 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2142 nvlist_free(search
);
2148 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2151 zpool_vdev_is_interior(const char *name
)
2153 if (strncmp(name
, VDEV_TYPE_RAIDZ
, strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2154 strncmp(name
, VDEV_TYPE_MIRROR
, strlen(VDEV_TYPE_MIRROR
)) == 0)
2160 zpool_find_vdev(zpool_handle_t
*zhp
, const char *path
, boolean_t
*avail_spare
,
2161 boolean_t
*l2cache
, boolean_t
*log
)
2164 nvlist_t
*nvroot
, *search
, *ret
;
2167 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2169 guid
= strtoull(path
, &end
, 0);
2170 if (guid
!= 0 && *end
== '\0') {
2171 verify(nvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
) == 0);
2172 } else if (zpool_vdev_is_interior(path
)) {
2173 verify(nvlist_add_string(search
, ZPOOL_CONFIG_TYPE
, path
) == 0);
2175 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PATH
, path
) == 0);
2178 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2181 *avail_spare
= B_FALSE
;
2185 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2186 nvlist_free(search
);
2192 vdev_online(nvlist_t
*nv
)
2196 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
, &ival
) == 0 ||
2197 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
, &ival
) == 0 ||
2198 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
, &ival
) == 0)
2205 * Helper function for zpool_get_physpaths().
2208 vdev_get_one_physpath(nvlist_t
*config
, char *physpath
, size_t physpath_size
,
2209 size_t *bytes_written
)
2211 size_t bytes_left
, pos
, rsz
;
2215 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PHYS_PATH
,
2217 return (EZFS_NODEVICE
);
2219 pos
= *bytes_written
;
2220 bytes_left
= physpath_size
- pos
;
2221 format
= (pos
== 0) ? "%s" : " %s";
2223 rsz
= snprintf(physpath
+ pos
, bytes_left
, format
, tmppath
);
2224 *bytes_written
+= rsz
;
2226 if (rsz
>= bytes_left
) {
2227 /* if physpath was not copied properly, clear it */
2228 if (bytes_left
!= 0) {
2231 return (EZFS_NOSPC
);
2237 vdev_get_physpaths(nvlist_t
*nv
, char *physpath
, size_t phypath_size
,
2238 size_t *rsz
, boolean_t is_spare
)
2243 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
2244 return (EZFS_INVALCONFIG
);
2246 if (strcmp(type
, VDEV_TYPE_DISK
) == 0) {
2248 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2249 * For a spare vdev, we only want to boot from the active
2254 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
2257 return (EZFS_INVALCONFIG
);
2260 if (vdev_online(nv
)) {
2261 if ((ret
= vdev_get_one_physpath(nv
, physpath
,
2262 phypath_size
, rsz
)) != 0)
2265 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) == 0 ||
2266 strcmp(type
, VDEV_TYPE_REPLACING
) == 0 ||
2267 (is_spare
= (strcmp(type
, VDEV_TYPE_SPARE
) == 0))) {
2272 if (nvlist_lookup_nvlist_array(nv
,
2273 ZPOOL_CONFIG_CHILDREN
, &child
, &count
) != 0)
2274 return (EZFS_INVALCONFIG
);
2276 for (i
= 0; i
< count
; i
++) {
2277 ret
= vdev_get_physpaths(child
[i
], physpath
,
2278 phypath_size
, rsz
, is_spare
);
2279 if (ret
== EZFS_NOSPC
)
2284 return (EZFS_POOL_INVALARG
);
2288 * Get phys_path for a root pool config.
2289 * Return 0 on success; non-zero on failure.
2292 zpool_get_config_physpath(nvlist_t
*config
, char *physpath
, size_t phypath_size
)
2295 nvlist_t
*vdev_root
;
2302 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2304 return (EZFS_INVALCONFIG
);
2306 if (nvlist_lookup_string(vdev_root
, ZPOOL_CONFIG_TYPE
, &type
) != 0 ||
2307 nvlist_lookup_nvlist_array(vdev_root
, ZPOOL_CONFIG_CHILDREN
,
2308 &child
, &count
) != 0)
2309 return (EZFS_INVALCONFIG
);
2311 #if defined(__sun__) || defined(__sun)
2313 * root pool can not have EFI labeled disks and can only have
2314 * a single top-level vdev.
2316 if (strcmp(type
, VDEV_TYPE_ROOT
) != 0 || count
!= 1 ||
2317 pool_uses_efi(vdev_root
))
2318 return (EZFS_POOL_INVALARG
);
2321 (void) vdev_get_physpaths(child
[0], physpath
, phypath_size
, &rsz
,
2324 /* No online devices */
2326 return (EZFS_NODEVICE
);
2332 * Get phys_path for a root pool
2333 * Return 0 on success; non-zero on failure.
2336 zpool_get_physpath(zpool_handle_t
*zhp
, char *physpath
, size_t phypath_size
)
2338 return (zpool_get_config_physpath(zhp
->zpool_config
, physpath
,
2343 * If the device has being dynamically expanded then we need to relabel
2344 * the disk to use the new unallocated space.
2347 zpool_relabel_disk(libzfs_handle_t
*hdl
, const char *path
, const char *msg
)
2351 if ((fd
= open(path
, O_RDWR
|O_DIRECT
)) < 0) {
2352 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2353 "relabel '%s': unable to open device: %d"), path
, errno
);
2354 return (zfs_error(hdl
, EZFS_OPENFAILED
, msg
));
2358 * It's possible that we might encounter an error if the device
2359 * does not have any unallocated space left. If so, we simply
2360 * ignore that error and continue on.
2362 * Also, we don't call efi_rescan() - that would just return EBUSY.
2363 * The module will do it for us in vdev_disk_open().
2365 error
= efi_use_whole_disk(fd
);
2367 if (error
&& error
!= VT_ENOSPC
) {
2368 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2369 "relabel '%s': unable to read disk capacity"), path
);
2370 return (zfs_error(hdl
, EZFS_NOCAP
, msg
));
2376 * Bring the specified vdev online. The 'flags' parameter is a set of the
2377 * ZFS_ONLINE_* flags.
2380 zpool_vdev_online(zpool_handle_t
*zhp
, const char *path
, int flags
,
2381 vdev_state_t
*newstate
)
2383 zfs_cmd_t zc
= {"\0"};
2386 boolean_t avail_spare
, l2cache
, islog
;
2387 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2390 if (flags
& ZFS_ONLINE_EXPAND
) {
2391 (void) snprintf(msg
, sizeof (msg
),
2392 dgettext(TEXT_DOMAIN
, "cannot expand %s"), path
);
2394 (void) snprintf(msg
, sizeof (msg
),
2395 dgettext(TEXT_DOMAIN
, "cannot online %s"), path
);
2398 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2399 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2401 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2403 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2406 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2408 if (flags
& ZFS_ONLINE_EXPAND
||
2409 zpool_get_prop_int(zhp
, ZPOOL_PROP_AUTOEXPAND
, NULL
)) {
2410 uint64_t wholedisk
= 0;
2412 (void) nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_WHOLE_DISK
,
2416 * XXX - L2ARC 1.0 devices can't support expansion.
2419 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2420 "cannot expand cache devices"));
2421 return (zfs_error(hdl
, EZFS_VDEVNOTSUP
, msg
));
2425 const char *fullpath
= path
;
2426 char buf
[MAXPATHLEN
];
2428 if (path
[0] != '/') {
2429 error
= zfs_resolve_shortname(path
, buf
,
2432 return (zfs_error(hdl
, EZFS_NODEVICE
,
2438 error
= zpool_relabel_disk(hdl
, fullpath
, msg
);
2444 zc
.zc_cookie
= VDEV_STATE_ONLINE
;
2447 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) != 0) {
2448 if (errno
== EINVAL
) {
2449 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "was split "
2450 "from this pool into a new one. Use '%s' "
2451 "instead"), "zpool detach");
2452 return (zfs_error(hdl
, EZFS_POSTSPLIT_ONLINE
, msg
));
2454 return (zpool_standard_error(hdl
, errno
, msg
));
2457 *newstate
= zc
.zc_cookie
;
2462 * Take the specified vdev offline
2465 zpool_vdev_offline(zpool_handle_t
*zhp
, const char *path
, boolean_t istmp
)
2467 zfs_cmd_t zc
= {"\0"};
2470 boolean_t avail_spare
, l2cache
;
2471 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2473 (void) snprintf(msg
, sizeof (msg
),
2474 dgettext(TEXT_DOMAIN
, "cannot offline %s"), path
);
2476 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2477 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2479 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2481 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2484 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2486 zc
.zc_cookie
= VDEV_STATE_OFFLINE
;
2487 zc
.zc_obj
= istmp
? ZFS_OFFLINE_TEMPORARY
: 0;
2489 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2496 * There are no other replicas of this device.
2498 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2502 * The log device has unplayed logs
2504 return (zfs_error(hdl
, EZFS_UNPLAYED_LOGS
, msg
));
2507 return (zpool_standard_error(hdl
, errno
, msg
));
2512 * Mark the given vdev faulted.
2515 zpool_vdev_fault(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2517 zfs_cmd_t zc
= {"\0"};
2519 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2521 (void) snprintf(msg
, sizeof (msg
),
2522 dgettext(TEXT_DOMAIN
, "cannot fault %llu"), (u_longlong_t
)guid
);
2524 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2526 zc
.zc_cookie
= VDEV_STATE_FAULTED
;
2529 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2536 * There are no other replicas of this device.
2538 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2541 return (zpool_standard_error(hdl
, errno
, msg
));
2547 * Mark the given vdev degraded.
2550 zpool_vdev_degrade(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2552 zfs_cmd_t zc
= {"\0"};
2554 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2556 (void) snprintf(msg
, sizeof (msg
),
2557 dgettext(TEXT_DOMAIN
, "cannot degrade %llu"), (u_longlong_t
)guid
);
2559 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2561 zc
.zc_cookie
= VDEV_STATE_DEGRADED
;
2564 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2567 return (zpool_standard_error(hdl
, errno
, msg
));
2571 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2575 is_replacing_spare(nvlist_t
*search
, nvlist_t
*tgt
, int which
)
2581 if (nvlist_lookup_nvlist_array(search
, ZPOOL_CONFIG_CHILDREN
, &child
,
2583 verify(nvlist_lookup_string(search
, ZPOOL_CONFIG_TYPE
,
2586 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0 &&
2587 children
== 2 && child
[which
] == tgt
)
2590 for (c
= 0; c
< children
; c
++)
2591 if (is_replacing_spare(child
[c
], tgt
, which
))
2599 * Attach new_disk (fully described by nvroot) to old_disk.
2600 * If 'replacing' is specified, the new disk will replace the old one.
2603 zpool_vdev_attach(zpool_handle_t
*zhp
,
2604 const char *old_disk
, const char *new_disk
, nvlist_t
*nvroot
, int replacing
)
2606 zfs_cmd_t zc
= {"\0"};
2610 boolean_t avail_spare
, l2cache
, islog
;
2615 nvlist_t
*config_root
;
2616 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2617 boolean_t rootpool
= zpool_is_bootable(zhp
);
2620 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2621 "cannot replace %s with %s"), old_disk
, new_disk
);
2623 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2624 "cannot attach %s to %s"), new_disk
, old_disk
);
2626 #if defined(__sun__) || defined(__sun)
2628 * If this is a root pool, make sure that we're not attaching an
2629 * EFI labeled device.
2631 if (rootpool
&& pool_uses_efi(nvroot
)) {
2632 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2633 "EFI labeled devices are not supported on root pools."));
2634 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, msg
));
2638 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2639 if ((tgt
= zpool_find_vdev(zhp
, old_disk
, &avail_spare
, &l2cache
,
2641 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2644 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2647 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2649 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2650 zc
.zc_cookie
= replacing
;
2652 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
2653 &child
, &children
) != 0 || children
!= 1) {
2654 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2655 "new device must be a single disk"));
2656 return (zfs_error(hdl
, EZFS_INVALCONFIG
, msg
));
2659 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
2660 ZPOOL_CONFIG_VDEV_TREE
, &config_root
) == 0);
2662 if ((newname
= zpool_vdev_name(NULL
, NULL
, child
[0], B_FALSE
)) == NULL
)
2666 * If the target is a hot spare that has been swapped in, we can only
2667 * replace it with another hot spare.
2670 nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_IS_SPARE
, &val
) == 0 &&
2671 (zpool_find_vdev(zhp
, newname
, &avail_spare
, &l2cache
,
2672 NULL
) == NULL
|| !avail_spare
) &&
2673 is_replacing_spare(config_root
, tgt
, 1)) {
2674 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2675 "can only be replaced by another hot spare"));
2677 return (zfs_error(hdl
, EZFS_BADTARGET
, msg
));
2682 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
2685 ret
= zfs_ioctl(hdl
, ZFS_IOC_VDEV_ATTACH
, &zc
);
2687 zcmd_free_nvlists(&zc
);
2692 * XXX need a better way to prevent user from
2693 * booting up a half-baked vdev.
2695 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Make "
2696 "sure to wait until resilver is done "
2697 "before rebooting.\n"));
2705 * Can't attach to or replace this type of vdev.
2708 uint64_t version
= zpool_get_prop_int(zhp
,
2709 ZPOOL_PROP_VERSION
, NULL
);
2712 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2713 "cannot replace a log with a spare"));
2714 else if (version
>= SPA_VERSION_MULTI_REPLACE
)
2715 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2716 "already in replacing/spare config; wait "
2717 "for completion or use 'zpool detach'"));
2719 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2720 "cannot replace a replacing device"));
2722 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2723 "can only attach to mirrors and top-level "
2726 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2731 * The new device must be a single disk.
2733 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2734 "new device must be a single disk"));
2735 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2739 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "%s is busy"),
2741 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2746 * The new device is too small.
2748 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2749 "device is too small"));
2750 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2755 * The new device has a different optimal sector size.
2757 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2758 "new device has a different optimal sector size; use the "
2759 "option '-o ashift=N' to override the optimal size"));
2760 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2765 * The resulting top-level vdev spec won't fit in the label.
2767 (void) zfs_error(hdl
, EZFS_DEVOVERFLOW
, msg
);
2771 (void) zpool_standard_error(hdl
, errno
, msg
);
2778 * Detach the specified device.
2781 zpool_vdev_detach(zpool_handle_t
*zhp
, const char *path
)
2783 zfs_cmd_t zc
= {"\0"};
2786 boolean_t avail_spare
, l2cache
;
2787 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2789 (void) snprintf(msg
, sizeof (msg
),
2790 dgettext(TEXT_DOMAIN
, "cannot detach %s"), path
);
2792 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2793 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2795 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2798 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2801 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2803 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2805 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_DETACH
, &zc
) == 0)
2812 * Can't detach from this type of vdev.
2814 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "only "
2815 "applicable to mirror and replacing vdevs"));
2816 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2821 * There are no other replicas of this device.
2823 (void) zfs_error(hdl
, EZFS_NOREPLICAS
, msg
);
2827 (void) zpool_standard_error(hdl
, errno
, msg
);
2834 * Find a mirror vdev in the source nvlist.
2836 * The mchild array contains a list of disks in one of the top-level mirrors
2837 * of the source pool. The schild array contains a list of disks that the
2838 * user specified on the command line. We loop over the mchild array to
2839 * see if any entry in the schild array matches.
2841 * If a disk in the mchild array is found in the schild array, we return
2842 * the index of that entry. Otherwise we return -1.
2845 find_vdev_entry(zpool_handle_t
*zhp
, nvlist_t
**mchild
, uint_t mchildren
,
2846 nvlist_t
**schild
, uint_t schildren
)
2850 for (mc
= 0; mc
< mchildren
; mc
++) {
2852 char *mpath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
2853 mchild
[mc
], B_FALSE
);
2855 for (sc
= 0; sc
< schildren
; sc
++) {
2856 char *spath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
2857 schild
[sc
], B_FALSE
);
2858 boolean_t result
= (strcmp(mpath
, spath
) == 0);
2874 * Split a mirror pool. If newroot points to null, then a new nvlist
2875 * is generated and it is the responsibility of the caller to free it.
2878 zpool_vdev_split(zpool_handle_t
*zhp
, char *newname
, nvlist_t
**newroot
,
2879 nvlist_t
*props
, splitflags_t flags
)
2881 zfs_cmd_t zc
= {"\0"};
2883 nvlist_t
*tree
, *config
, **child
, **newchild
, *newconfig
= NULL
;
2884 nvlist_t
**varray
= NULL
, *zc_props
= NULL
;
2885 uint_t c
, children
, newchildren
, lastlog
= 0, vcount
, found
= 0;
2886 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2888 boolean_t freelist
= B_FALSE
, memory_err
= B_TRUE
;
2891 (void) snprintf(msg
, sizeof (msg
),
2892 dgettext(TEXT_DOMAIN
, "Unable to split %s"), zhp
->zpool_name
);
2894 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
2895 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
2897 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
2898 (void) fprintf(stderr
, gettext("Internal error: unable to "
2899 "retrieve pool configuration\n"));
2903 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &tree
)
2905 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
, &vers
) == 0);
2908 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
2909 if ((zc_props
= zpool_valid_proplist(hdl
, zhp
->zpool_name
,
2910 props
, vers
, flags
, msg
)) == NULL
)
2914 if (nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
2916 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2917 "Source pool is missing vdev tree"));
2919 nvlist_free(zc_props
);
2923 varray
= zfs_alloc(hdl
, children
* sizeof (nvlist_t
*));
2926 if (*newroot
== NULL
||
2927 nvlist_lookup_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
2928 &newchild
, &newchildren
) != 0)
2931 for (c
= 0; c
< children
; c
++) {
2932 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
2934 nvlist_t
**mchild
, *vdev
;
2939 * Unlike cache & spares, slogs are stored in the
2940 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2942 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2944 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
2946 if (is_log
|| is_hole
) {
2948 * Create a hole vdev and put it in the config.
2950 if (nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) != 0)
2952 if (nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
,
2953 VDEV_TYPE_HOLE
) != 0)
2955 if (nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_HOLE
,
2960 varray
[vcount
++] = vdev
;
2964 verify(nvlist_lookup_string(child
[c
], ZPOOL_CONFIG_TYPE
, &type
)
2966 if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
2967 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2968 "Source pool must be composed only of mirrors\n"));
2969 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2973 verify(nvlist_lookup_nvlist_array(child
[c
],
2974 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
2976 /* find or add an entry for this top-level vdev */
2977 if (newchildren
> 0 &&
2978 (entry
= find_vdev_entry(zhp
, mchild
, mchildren
,
2979 newchild
, newchildren
)) >= 0) {
2980 /* We found a disk that the user specified. */
2981 vdev
= mchild
[entry
];
2984 /* User didn't specify a disk for this vdev. */
2985 vdev
= mchild
[mchildren
- 1];
2988 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
2992 /* did we find every disk the user specified? */
2993 if (found
!= newchildren
) {
2994 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Device list must "
2995 "include at most one disk from each mirror"));
2996 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3000 /* Prepare the nvlist for populating. */
3001 if (*newroot
== NULL
) {
3002 if (nvlist_alloc(newroot
, NV_UNIQUE_NAME
, 0) != 0)
3005 if (nvlist_add_string(*newroot
, ZPOOL_CONFIG_TYPE
,
3006 VDEV_TYPE_ROOT
) != 0)
3009 verify(nvlist_remove_all(*newroot
, ZPOOL_CONFIG_CHILDREN
) == 0);
3012 /* Add all the children we found */
3013 if (nvlist_add_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
, varray
,
3014 lastlog
== 0 ? vcount
: lastlog
) != 0)
3018 * If we're just doing a dry run, exit now with success.
3021 memory_err
= B_FALSE
;
3026 /* now build up the config list & call the ioctl */
3027 if (nvlist_alloc(&newconfig
, NV_UNIQUE_NAME
, 0) != 0)
3030 if (nvlist_add_nvlist(newconfig
,
3031 ZPOOL_CONFIG_VDEV_TREE
, *newroot
) != 0 ||
3032 nvlist_add_string(newconfig
,
3033 ZPOOL_CONFIG_POOL_NAME
, newname
) != 0 ||
3034 nvlist_add_uint64(newconfig
, ZPOOL_CONFIG_VERSION
, vers
) != 0)
3038 * The new pool is automatically part of the namespace unless we
3039 * explicitly export it.
3042 zc
.zc_cookie
= ZPOOL_EXPORT_AFTER_SPLIT
;
3043 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3044 (void) strlcpy(zc
.zc_string
, newname
, sizeof (zc
.zc_string
));
3045 if (zcmd_write_conf_nvlist(hdl
, &zc
, newconfig
) != 0)
3047 if (zc_props
!= NULL
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
3050 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SPLIT
, &zc
) != 0) {
3051 retval
= zpool_standard_error(hdl
, errno
, msg
);
3056 memory_err
= B_FALSE
;
3059 if (varray
!= NULL
) {
3062 for (v
= 0; v
< vcount
; v
++)
3063 nvlist_free(varray
[v
]);
3066 zcmd_free_nvlists(&zc
);
3068 nvlist_free(zc_props
);
3070 nvlist_free(newconfig
);
3072 nvlist_free(*newroot
);
3080 return (no_memory(hdl
));
3086 * Remove the given device. Currently, this is supported only for hot spares
3087 * and level 2 cache devices.
3090 zpool_vdev_remove(zpool_handle_t
*zhp
, const char *path
)
3092 zfs_cmd_t zc
= {"\0"};
3095 boolean_t avail_spare
, l2cache
, islog
;
3096 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3099 (void) snprintf(msg
, sizeof (msg
),
3100 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3102 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3103 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3105 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3107 * XXX - this should just go away.
3109 if (!avail_spare
&& !l2cache
&& !islog
) {
3110 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3111 "only inactive hot spares, cache, top-level, "
3112 "or log devices can be removed"));
3113 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3116 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
3117 if (islog
&& version
< SPA_VERSION_HOLES
) {
3118 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3119 "pool must be upgrade to support log removal"));
3120 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
3123 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
3125 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3128 return (zpool_standard_error(hdl
, errno
, msg
));
3132 * Clear the errors for the pool, or the particular device if specified.
3135 zpool_clear(zpool_handle_t
*zhp
, const char *path
, nvlist_t
*rewindnvl
)
3137 zfs_cmd_t zc
= {"\0"};
3140 zpool_rewind_policy_t policy
;
3141 boolean_t avail_spare
, l2cache
;
3142 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3143 nvlist_t
*nvi
= NULL
;
3147 (void) snprintf(msg
, sizeof (msg
),
3148 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3151 (void) snprintf(msg
, sizeof (msg
),
3152 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3155 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3157 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
,
3158 &l2cache
, NULL
)) == 0)
3159 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3162 * Don't allow error clearing for hot spares. Do allow
3163 * error clearing for l2cache devices.
3166 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3168 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
,
3172 zpool_get_rewind_policy(rewindnvl
, &policy
);
3173 zc
.zc_cookie
= policy
.zrp_request
;
3175 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zhp
->zpool_config_size
* 2) != 0)
3178 if (zcmd_write_src_nvlist(hdl
, &zc
, rewindnvl
) != 0)
3181 while ((error
= zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
)) != 0 &&
3183 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
3184 zcmd_free_nvlists(&zc
);
3189 if (!error
|| ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) &&
3190 errno
!= EPERM
&& errno
!= EACCES
)) {
3191 if (policy
.zrp_request
&
3192 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
3193 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nvi
);
3194 zpool_rewind_exclaim(hdl
, zc
.zc_name
,
3195 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0),
3199 zcmd_free_nvlists(&zc
);
3203 zcmd_free_nvlists(&zc
);
3204 return (zpool_standard_error(hdl
, errno
, msg
));
3208 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3211 zpool_vdev_clear(zpool_handle_t
*zhp
, uint64_t guid
)
3213 zfs_cmd_t zc
= {"\0"};
3215 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3217 (void) snprintf(msg
, sizeof (msg
),
3218 dgettext(TEXT_DOMAIN
, "cannot clear errors for %llx"),
3219 (u_longlong_t
)guid
);
3221 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3223 zc
.zc_cookie
= ZPOOL_NO_REWIND
;
3225 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_CLEAR
, &zc
) == 0)
3228 return (zpool_standard_error(hdl
, errno
, msg
));
3232 * Change the GUID for a pool.
3235 zpool_reguid(zpool_handle_t
*zhp
)
3238 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3239 zfs_cmd_t zc
= {"\0"};
3241 (void) snprintf(msg
, sizeof (msg
),
3242 dgettext(TEXT_DOMAIN
, "cannot reguid '%s'"), zhp
->zpool_name
);
3244 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3245 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REGUID
, &zc
) == 0)
3248 return (zpool_standard_error(hdl
, errno
, msg
));
3255 zpool_reopen(zpool_handle_t
*zhp
)
3257 zfs_cmd_t zc
= {"\0"};
3259 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3261 (void) snprintf(msg
, sizeof (msg
),
3262 dgettext(TEXT_DOMAIN
, "cannot reopen '%s'"),
3265 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3266 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REOPEN
, &zc
) == 0)
3268 return (zpool_standard_error(hdl
, errno
, msg
));
3272 * Convert from a devid string to a path.
3275 devid_to_path(char *devid_str
)
3280 devid_nmlist_t
*list
= NULL
;
3283 if (devid_str_decode(devid_str
, &devid
, &minor
) != 0)
3286 ret
= devid_deviceid_to_nmlist("/dev", devid
, minor
, &list
);
3288 devid_str_free(minor
);
3294 if ((path
= strdup(list
[0].devname
)) == NULL
)
3297 devid_free_nmlist(list
);
3303 * Convert from a path to a devid string.
3306 path_to_devid(const char *path
)
3312 if ((fd
= open(path
, O_RDONLY
)) < 0)
3317 if (devid_get(fd
, &devid
) == 0) {
3318 if (devid_get_minor_name(fd
, &minor
) == 0)
3319 ret
= devid_str_encode(devid
, minor
);
3321 devid_str_free(minor
);
3330 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3331 * ignore any failure here, since a common case is for an unprivileged user to
3332 * type 'zpool status', and we'll display the correct information anyway.
3335 set_path(zpool_handle_t
*zhp
, nvlist_t
*nv
, const char *path
)
3337 zfs_cmd_t zc
= {"\0"};
3339 (void) strncpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3340 (void) strncpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
3341 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
3344 (void) ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_VDEV_SETPATH
, &zc
);
3348 * Remove partition suffix from a vdev path. Partition suffixes may take three
3349 * forms: "-partX", "pX", or "X", where X is a string of digits. The second
3350 * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
3351 * third case only occurs when preceded by a string matching the regular
3352 * expression "^[hs]d[a-z]+", i.e. a scsi or ide disk.
3355 strip_partition(libzfs_handle_t
*hdl
, char *path
)
3357 char *tmp
= zfs_strdup(hdl
, path
);
3358 char *part
= NULL
, *d
= NULL
;
3360 if ((part
= strstr(tmp
, "-part")) && part
!= tmp
) {
3362 } else if ((part
= strrchr(tmp
, 'p')) &&
3363 part
> tmp
+ 1 && isdigit(*(part
-1))) {
3365 } else if ((tmp
[0] == 'h' || tmp
[0] == 's') && tmp
[1] == 'd') {
3366 for (d
= &tmp
[2]; isalpha(*d
); part
= ++d
);
3368 if (part
&& d
&& *d
!= '\0') {
3369 for (; isdigit(*d
); d
++);
3376 #define PATH_BUF_LEN 64
3379 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3380 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3381 * We also check if this is a whole disk, in which case we strip off the
3382 * trailing 's0' slice name.
3384 * This routine is also responsible for identifying when disks have been
3385 * reconfigured in a new location. The kernel will have opened the device by
3386 * devid, but the path will still refer to the old location. To catch this, we
3387 * first do a path -> devid translation (which is fast for the common case). If
3388 * the devid matches, we're done. If not, we do a reverse devid -> path
3389 * translation and issue the appropriate ioctl() to update the path of the vdev.
3390 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3394 zpool_vdev_name(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, nvlist_t
*nv
,
3397 char *path
, *devid
, *type
;
3399 char buf
[PATH_BUF_LEN
];
3400 char tmpbuf
[PATH_BUF_LEN
];
3404 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
3406 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
3408 (void) snprintf(buf
, sizeof (buf
), "%llu",
3409 (u_longlong_t
)value
);
3411 } else if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
3413 * If the device is dead (faulted, offline, etc) then don't
3414 * bother opening it. Otherwise we may be forcing the user to
3415 * open a misbehaving device, which can have undesirable
3418 if ((nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
3419 (uint64_t **)&vs
, &vsc
) != 0 ||
3420 vs
->vs_state
>= VDEV_STATE_DEGRADED
) &&
3422 nvlist_lookup_string(nv
, ZPOOL_CONFIG_DEVID
, &devid
) == 0) {
3424 * Determine if the current path is correct.
3426 char *newdevid
= path_to_devid(path
);
3428 if (newdevid
== NULL
||
3429 strcmp(devid
, newdevid
) != 0) {
3432 if ((newpath
= devid_to_path(devid
)) != NULL
) {
3434 * Update the path appropriately.
3436 set_path(zhp
, nv
, newpath
);
3437 if (nvlist_add_string(nv
,
3438 ZPOOL_CONFIG_PATH
, newpath
) == 0)
3439 verify(nvlist_lookup_string(nv
,
3447 devid_str_free(newdevid
);
3451 * For a block device only use the name.
3453 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
3454 if (strcmp(type
, VDEV_TYPE_DISK
) == 0) {
3455 path
= strrchr(path
, '/');
3460 * Remove the partition from the path it this is a whole disk.
3462 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
3463 &value
) == 0 && value
) {
3464 return (strip_partition(hdl
, path
));
3467 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &path
) == 0);
3470 * If it's a raidz device, we need to stick in the parity level.
3472 if (strcmp(path
, VDEV_TYPE_RAIDZ
) == 0) {
3474 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
,
3476 (void) snprintf(buf
, sizeof (buf
), "%s%llu", path
,
3477 (u_longlong_t
)value
);
3482 * We identify each top-level vdev by using a <type-id>
3483 * naming convention.
3488 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
3490 (void) snprintf(tmpbuf
, sizeof (tmpbuf
), "%s-%llu",
3491 path
, (u_longlong_t
)id
);
3496 return (zfs_strdup(hdl
, path
));
3500 zbookmark_compare(const void *a
, const void *b
)
3502 return (memcmp(a
, b
, sizeof (zbookmark_t
)));
3506 * Retrieve the persistent error log, uniquify the members, and return to the
3510 zpool_get_errlog(zpool_handle_t
*zhp
, nvlist_t
**nverrlistp
)
3512 zfs_cmd_t zc
= {"\0"};
3514 zbookmark_t
*zb
= NULL
;
3518 * Retrieve the raw error list from the kernel. If the number of errors
3519 * has increased, allocate more space and continue until we get the
3522 verify(nvlist_lookup_uint64(zhp
->zpool_config
, ZPOOL_CONFIG_ERRCOUNT
,
3526 if ((zc
.zc_nvlist_dst
= (uintptr_t)zfs_alloc(zhp
->zpool_hdl
,
3527 count
* sizeof (zbookmark_t
))) == (uintptr_t)NULL
)
3529 zc
.zc_nvlist_dst_size
= count
;
3530 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3532 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_ERROR_LOG
,
3534 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3535 if (errno
== ENOMEM
) {
3536 count
= zc
.zc_nvlist_dst_size
;
3537 if ((zc
.zc_nvlist_dst
= (uintptr_t)
3538 zfs_alloc(zhp
->zpool_hdl
, count
*
3539 sizeof (zbookmark_t
))) == (uintptr_t)NULL
)
3550 * Sort the resulting bookmarks. This is a little confusing due to the
3551 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3552 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3553 * _not_ copied as part of the process. So we point the start of our
3554 * array appropriate and decrement the total number of elements.
3556 zb
= ((zbookmark_t
*)(uintptr_t)zc
.zc_nvlist_dst
) +
3557 zc
.zc_nvlist_dst_size
;
3558 count
-= zc
.zc_nvlist_dst_size
;
3560 qsort(zb
, count
, sizeof (zbookmark_t
), zbookmark_compare
);
3562 verify(nvlist_alloc(nverrlistp
, 0, KM_SLEEP
) == 0);
3565 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3567 for (i
= 0; i
< count
; i
++) {
3570 /* ignoring zb_blkid and zb_level for now */
3571 if (i
> 0 && zb
[i
-1].zb_objset
== zb
[i
].zb_objset
&&
3572 zb
[i
-1].zb_object
== zb
[i
].zb_object
)
3575 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) != 0)
3577 if (nvlist_add_uint64(nv
, ZPOOL_ERR_DATASET
,
3578 zb
[i
].zb_objset
) != 0) {
3582 if (nvlist_add_uint64(nv
, ZPOOL_ERR_OBJECT
,
3583 zb
[i
].zb_object
) != 0) {
3587 if (nvlist_add_nvlist(*nverrlistp
, "ejk", nv
) != 0) {
3594 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3598 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3599 return (no_memory(zhp
->zpool_hdl
));
3603 * Upgrade a ZFS pool to the latest on-disk version.
3606 zpool_upgrade(zpool_handle_t
*zhp
, uint64_t new_version
)
3608 zfs_cmd_t zc
= {"\0"};
3609 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3611 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3612 zc
.zc_cookie
= new_version
;
3614 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_UPGRADE
, &zc
) != 0)
3615 return (zpool_standard_error_fmt(hdl
, errno
,
3616 dgettext(TEXT_DOMAIN
, "cannot upgrade '%s'"),
3622 zfs_save_arguments(int argc
, char **argv
, char *string
, int len
)
3626 (void) strlcpy(string
, basename(argv
[0]), len
);
3627 for (i
= 1; i
< argc
; i
++) {
3628 (void) strlcat(string
, " ", len
);
3629 (void) strlcat(string
, argv
[i
], len
);
3634 zpool_log_history(libzfs_handle_t
*hdl
, const char *message
)
3636 zfs_cmd_t zc
= {"\0"};
3640 args
= fnvlist_alloc();
3641 fnvlist_add_string(args
, "message", message
);
3642 err
= zcmd_write_src_nvlist(hdl
, &zc
, args
);
3644 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_LOG_HISTORY
, &zc
);
3646 zcmd_free_nvlists(&zc
);
3651 * Perform ioctl to get some command history of a pool.
3653 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3654 * logical offset of the history buffer to start reading from.
3656 * Upon return, 'off' is the next logical offset to read from and
3657 * 'len' is the actual amount of bytes read into 'buf'.
3660 get_history(zpool_handle_t
*zhp
, char *buf
, uint64_t *off
, uint64_t *len
)
3662 zfs_cmd_t zc
= {"\0"};
3663 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3665 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3667 zc
.zc_history
= (uint64_t)(uintptr_t)buf
;
3668 zc
.zc_history_len
= *len
;
3669 zc
.zc_history_offset
= *off
;
3671 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_HISTORY
, &zc
) != 0) {
3674 return (zfs_error_fmt(hdl
, EZFS_PERM
,
3675 dgettext(TEXT_DOMAIN
,
3676 "cannot show history for pool '%s'"),
3679 return (zfs_error_fmt(hdl
, EZFS_NOHISTORY
,
3680 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
3681 "'%s'"), zhp
->zpool_name
));
3683 return (zfs_error_fmt(hdl
, EZFS_BADVERSION
,
3684 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
3685 "'%s', pool must be upgraded"), zhp
->zpool_name
));
3687 return (zpool_standard_error_fmt(hdl
, errno
,
3688 dgettext(TEXT_DOMAIN
,
3689 "cannot get history for '%s'"), zhp
->zpool_name
));
3693 *len
= zc
.zc_history_len
;
3694 *off
= zc
.zc_history_offset
;
3700 * Process the buffer of nvlists, unpacking and storing each nvlist record
3701 * into 'records'. 'leftover' is set to the number of bytes that weren't
3702 * processed as there wasn't a complete record.
3705 zpool_history_unpack(char *buf
, uint64_t bytes_read
, uint64_t *leftover
,
3706 nvlist_t
***records
, uint_t
*numrecords
)
3712 while (bytes_read
> sizeof (reclen
)) {
3714 /* get length of packed record (stored as little endian) */
3715 for (i
= 0, reclen
= 0; i
< sizeof (reclen
); i
++)
3716 reclen
+= (uint64_t)(((uchar_t
*)buf
)[i
]) << (8*i
);
3718 if (bytes_read
< sizeof (reclen
) + reclen
)
3722 if (nvlist_unpack(buf
+ sizeof (reclen
), reclen
, &nv
, 0) != 0)
3724 bytes_read
-= sizeof (reclen
) + reclen
;
3725 buf
+= sizeof (reclen
) + reclen
;
3727 /* add record to nvlist array */
3729 if (ISP2(*numrecords
+ 1)) {
3730 *records
= realloc(*records
,
3731 *numrecords
* 2 * sizeof (nvlist_t
*));
3733 (*records
)[*numrecords
- 1] = nv
;
3736 *leftover
= bytes_read
;
3740 #define HIS_BUF_LEN (128*1024)
3743 * Retrieve the command history of a pool.
3746 zpool_get_history(zpool_handle_t
*zhp
, nvlist_t
**nvhisp
)
3748 char buf
[HIS_BUF_LEN
];
3750 nvlist_t
**records
= NULL
;
3751 uint_t numrecords
= 0;
3755 uint64_t bytes_read
= sizeof (buf
);
3758 if ((err
= get_history(zhp
, buf
, &off
, &bytes_read
)) != 0)
3761 /* if nothing else was read in, we're at EOF, just return */
3765 if ((err
= zpool_history_unpack(buf
, bytes_read
,
3766 &leftover
, &records
, &numrecords
)) != 0)
3774 verify(nvlist_alloc(nvhisp
, NV_UNIQUE_NAME
, 0) == 0);
3775 verify(nvlist_add_nvlist_array(*nvhisp
, ZPOOL_HIST_RECORD
,
3776 records
, numrecords
) == 0);
3778 for (i
= 0; i
< numrecords
; i
++)
3779 nvlist_free(records
[i
]);
3786 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
3787 * If there is a new event available 'nvp' will contain a newly allocated
3788 * nvlist and 'dropped' will be set to the number of missed events since
3789 * the last call to this function. When 'nvp' is set to NULL it indicates
3790 * no new events are available. In either case the function returns 0 and
3791 * it is up to the caller to free 'nvp'. In the case of a fatal error the
3792 * function will return a non-zero value. When the function is called in
3793 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
3794 * it will not return until a new event is available.
3797 zpool_events_next(libzfs_handle_t
*hdl
, nvlist_t
**nvp
,
3798 int *dropped
, unsigned flags
, int zevent_fd
)
3800 zfs_cmd_t zc
= {"\0"};
3805 zc
.zc_cleanup_fd
= zevent_fd
;
3807 if (flags
& ZEVENT_NONBLOCK
)
3808 zc
.zc_guid
= ZEVENT_NONBLOCK
;
3810 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, ZEVENT_SIZE
) != 0)
3814 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_NEXT
, &zc
) != 0) {
3817 error
= zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
3818 dgettext(TEXT_DOMAIN
, "zfs shutdown"));
3821 /* Blocking error case should not occur */
3822 if (!(flags
& ZEVENT_NONBLOCK
))
3823 error
= zpool_standard_error_fmt(hdl
, errno
,
3824 dgettext(TEXT_DOMAIN
, "cannot get event"));
3828 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
3829 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
3830 dgettext(TEXT_DOMAIN
, "cannot get event"));
3836 error
= zpool_standard_error_fmt(hdl
, errno
,
3837 dgettext(TEXT_DOMAIN
, "cannot get event"));
3842 error
= zcmd_read_dst_nvlist(hdl
, &zc
, nvp
);
3846 *dropped
= (int)zc
.zc_cookie
;
3848 zcmd_free_nvlists(&zc
);
3857 zpool_events_clear(libzfs_handle_t
*hdl
, int *count
)
3859 zfs_cmd_t zc
= {"\0"};
3862 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
3863 "cannot clear events"));
3865 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_CLEAR
, &zc
) != 0)
3866 return (zpool_standard_error_fmt(hdl
, errno
, msg
));
3869 *count
= (int)zc
.zc_cookie
; /* # of events cleared */
3875 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
3876 * the passed zevent_fd file handle. On success zero is returned,
3877 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
3880 zpool_events_seek(libzfs_handle_t
*hdl
, uint64_t eid
, int zevent_fd
)
3882 zfs_cmd_t zc
= {"\0"};
3886 zc
.zc_cleanup_fd
= zevent_fd
;
3888 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_SEEK
, &zc
) != 0) {
3891 error
= zfs_error_fmt(hdl
, EZFS_NOENT
,
3892 dgettext(TEXT_DOMAIN
, "cannot get event"));
3896 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
3897 dgettext(TEXT_DOMAIN
, "cannot get event"));
3901 error
= zpool_standard_error_fmt(hdl
, errno
,
3902 dgettext(TEXT_DOMAIN
, "cannot get event"));
3911 zpool_obj_to_path(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
3912 char *pathname
, size_t len
)
3914 zfs_cmd_t zc
= {"\0"};
3915 boolean_t mounted
= B_FALSE
;
3916 char *mntpnt
= NULL
;
3917 char dsname
[MAXNAMELEN
];
3920 /* special case for the MOS */
3921 (void) snprintf(pathname
, len
, "<metadata>:<0x%llx>",
3926 /* get the dataset's name */
3927 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3929 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
,
3930 ZFS_IOC_DSOBJ_TO_DSNAME
, &zc
) != 0) {
3931 /* just write out a path of two object numbers */
3932 (void) snprintf(pathname
, len
, "<0x%llx>:<0x%llx>",
3933 (longlong_t
)dsobj
, (longlong_t
)obj
);
3936 (void) strlcpy(dsname
, zc
.zc_value
, sizeof (dsname
));
3938 /* find out if the dataset is mounted */
3939 mounted
= is_mounted(zhp
->zpool_hdl
, dsname
, &mntpnt
);
3941 /* get the corrupted object's path */
3942 (void) strlcpy(zc
.zc_name
, dsname
, sizeof (zc
.zc_name
));
3944 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_OBJ_TO_PATH
,
3947 (void) snprintf(pathname
, len
, "%s%s", mntpnt
,
3950 (void) snprintf(pathname
, len
, "%s:%s",
3951 dsname
, zc
.zc_value
);
3954 (void) snprintf(pathname
, len
, "%s:<0x%llx>", dsname
,
3961 * Read the EFI label from the config, if a label does not exist then
3962 * pass back the error to the caller. If the caller has passed a non-NULL
3963 * diskaddr argument then we set it to the starting address of the EFI
3967 read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
)
3971 char diskname
[MAXPATHLEN
];
3974 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PATH
, &path
) != 0)
3977 (void) snprintf(diskname
, sizeof (diskname
), "%s%s", DISK_ROOT
,
3978 strrchr(path
, '/'));
3979 if ((fd
= open(diskname
, O_RDWR
|O_DIRECT
)) >= 0) {
3980 struct dk_gpt
*vtoc
;
3982 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) >= 0) {
3984 *sb
= vtoc
->efi_parts
[0].p_start
;
3993 * determine where a partition starts on a disk in the current
3997 find_start_block(nvlist_t
*config
)
4001 diskaddr_t sb
= MAXOFFSET_T
;
4004 if (nvlist_lookup_nvlist_array(config
,
4005 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) != 0) {
4006 if (nvlist_lookup_uint64(config
,
4007 ZPOOL_CONFIG_WHOLE_DISK
,
4008 &wholedisk
) != 0 || !wholedisk
) {
4009 return (MAXOFFSET_T
);
4011 if (read_efi_label(config
, &sb
) < 0)
4016 for (c
= 0; c
< children
; c
++) {
4017 sb
= find_start_block(child
[c
]);
4018 if (sb
!= MAXOFFSET_T
) {
4022 return (MAXOFFSET_T
);
4026 zpool_label_disk_wait(char *path
, int timeout
)
4028 struct stat64 statbuf
;
4032 * Wait timeout miliseconds for a newly created device to be available
4033 * from the given path. There is a small window when a /dev/ device
4034 * will exist and the udev link will not, so we must wait for the
4035 * symlink. Depending on the udev rules this may take a few seconds.
4037 for (i
= 0; i
< timeout
; i
++) {
4041 if ((stat64(path
, &statbuf
) == 0) && (errno
== 0))
4049 zpool_label_disk_check(char *path
)
4051 struct dk_gpt
*vtoc
;
4054 if ((fd
= open(path
, O_RDWR
|O_DIRECT
)) < 0)
4057 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) != 0) {
4062 if (vtoc
->efi_flags
& EFI_GPT_PRIMARY_CORRUPT
) {
4074 * Label an individual disk. The name provided is the short name,
4075 * stripped of any leading /dev path.
4078 zpool_label_disk(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, char *name
)
4080 char path
[MAXPATHLEN
];
4081 struct dk_gpt
*vtoc
;
4083 size_t resv
= EFI_MIN_RESV_SIZE
;
4084 uint64_t slice_size
;
4085 diskaddr_t start_block
;
4088 /* prepare an error message just in case */
4089 (void) snprintf(errbuf
, sizeof (errbuf
),
4090 dgettext(TEXT_DOMAIN
, "cannot label '%s'"), name
);
4095 #if defined(__sun__) || defined(__sun)
4096 if (zpool_is_bootable(zhp
)) {
4097 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4098 "EFI labeled devices are not supported on root "
4100 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, errbuf
));
4104 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
4105 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
4107 if (zhp
->zpool_start_block
== 0)
4108 start_block
= find_start_block(nvroot
);
4110 start_block
= zhp
->zpool_start_block
;
4111 zhp
->zpool_start_block
= start_block
;
4114 start_block
= NEW_START_BLOCK
;
4117 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4119 if ((fd
= open(path
, O_RDWR
|O_DIRECT
)) < 0) {
4121 * This shouldn't happen. We've long since verified that this
4122 * is a valid device.
4124 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
4125 "label '%s': unable to open device: %d"), path
, errno
);
4126 return (zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
));
4129 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, &vtoc
) != 0) {
4131 * The only way this can fail is if we run out of memory, or we
4132 * were unable to read the disk's capacity
4134 if (errno
== ENOMEM
)
4135 (void) no_memory(hdl
);
4138 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
4139 "label '%s': unable to read disk capacity"), path
);
4141 return (zfs_error(hdl
, EZFS_NOCAP
, errbuf
));
4144 slice_size
= vtoc
->efi_last_u_lba
+ 1;
4145 slice_size
-= EFI_MIN_RESV_SIZE
;
4146 if (start_block
== MAXOFFSET_T
)
4147 start_block
= NEW_START_BLOCK
;
4148 slice_size
-= start_block
;
4149 slice_size
= P2ALIGN(slice_size
, PARTITION_END_ALIGNMENT
);
4151 vtoc
->efi_parts
[0].p_start
= start_block
;
4152 vtoc
->efi_parts
[0].p_size
= slice_size
;
4155 * Why we use V_USR: V_BACKUP confuses users, and is considered
4156 * disposable by some EFI utilities (since EFI doesn't have a backup
4157 * slice). V_UNASSIGNED is supposed to be used only for zero size
4158 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4159 * etc. were all pretty specific. V_USR is as close to reality as we
4160 * can get, in the absence of V_OTHER.
4162 vtoc
->efi_parts
[0].p_tag
= V_USR
;
4163 (void) strcpy(vtoc
->efi_parts
[0].p_name
, "zfs");
4165 vtoc
->efi_parts
[8].p_start
= slice_size
+ start_block
;
4166 vtoc
->efi_parts
[8].p_size
= resv
;
4167 vtoc
->efi_parts
[8].p_tag
= V_RESERVED
;
4169 if ((rval
= efi_write(fd
, vtoc
)) != 0 || (rval
= efi_rescan(fd
)) != 0) {
4171 * Some block drivers (like pcata) may not support EFI
4172 * GPT labels. Print out a helpful error message dir-
4173 * ecting the user to manually label the disk and give
4179 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "try using "
4180 "parted(8) and then provide a specific slice: %d"), rval
);
4181 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4187 /* Wait for the first expected partition to appear. */
4189 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4190 (void) zfs_append_partition(path
, MAXPATHLEN
);
4192 rval
= zpool_label_disk_wait(path
, 3000);
4194 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "failed to "
4195 "detect device partitions on '%s': %d"), path
, rval
);
4196 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4199 /* We can't be to paranoid. Read the label back and verify it. */
4200 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4201 rval
= zpool_label_disk_check(path
);
4203 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "freshly written "
4204 "EFI label on '%s' is damaged. Ensure\nthis device "
4205 "is not in in use, and is functioning properly: %d"),
4207 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));