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) 2012 by Delphix. All rights reserved.
38 #include <sys/efi_partition.h>
40 #include <sys/zfs_ioctl.h>
43 #include "zfs_namecheck.h"
45 #include "libzfs_impl.h"
46 #include "zfs_comutil.h"
47 #include "zfeature_common.h"
49 static int read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
);
51 #define DISK_ROOT "/dev/dsk"
52 #define RDISK_ROOT "/dev/rdsk"
53 #define BACKUP_SLICE "s2"
55 typedef struct prop_flags
{
56 int create
:1; /* Validate property on creation */
57 int import
:1; /* Validate property on import */
61 * ====================================================================
62 * zpool property functions
63 * ====================================================================
67 zpool_get_all_props(zpool_handle_t
*zhp
)
70 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
72 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
74 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
77 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_PROPS
, &zc
) != 0) {
78 if (errno
== ENOMEM
) {
79 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
80 zcmd_free_nvlists(&zc
);
84 zcmd_free_nvlists(&zc
);
89 if (zcmd_read_dst_nvlist(hdl
, &zc
, &zhp
->zpool_props
) != 0) {
90 zcmd_free_nvlists(&zc
);
94 zcmd_free_nvlists(&zc
);
100 zpool_props_refresh(zpool_handle_t
*zhp
)
104 old_props
= zhp
->zpool_props
;
106 if (zpool_get_all_props(zhp
) != 0)
109 nvlist_free(old_props
);
114 zpool_get_prop_string(zpool_handle_t
*zhp
, zpool_prop_t prop
,
120 zprop_source_t source
;
122 nvl
= zhp
->zpool_props
;
123 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
124 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &ival
) == 0);
126 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
128 source
= ZPROP_SRC_DEFAULT
;
129 if ((value
= (char *)zpool_prop_default_string(prop
)) == NULL
)
140 zpool_get_prop_int(zpool_handle_t
*zhp
, zpool_prop_t prop
, zprop_source_t
*src
)
144 zprop_source_t source
;
146 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
)) {
148 * zpool_get_all_props() has most likely failed because
149 * the pool is faulted, but if all we need is the top level
150 * vdev's guid then get it from the zhp config nvlist.
152 if ((prop
== ZPOOL_PROP_GUID
) &&
153 (nvlist_lookup_nvlist(zhp
->zpool_config
,
154 ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0) &&
155 (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
)
159 return (zpool_prop_default_numeric(prop
));
162 nvl
= zhp
->zpool_props
;
163 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
164 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &value
) == 0);
166 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
168 source
= ZPROP_SRC_DEFAULT
;
169 value
= zpool_prop_default_numeric(prop
);
179 * Map VDEV STATE to printed strings.
182 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 * Get a zpool property value for 'prop' and return the value in
210 * a pre-allocated buffer.
213 zpool_get_prop(zpool_handle_t
*zhp
, zpool_prop_t prop
, char *buf
, size_t len
,
214 zprop_source_t
*srctype
)
218 zprop_source_t src
= ZPROP_SRC_NONE
;
223 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
225 case ZPOOL_PROP_NAME
:
226 (void) strlcpy(buf
, zpool_get_name(zhp
), len
);
229 case ZPOOL_PROP_HEALTH
:
230 (void) strlcpy(buf
, "FAULTED", len
);
233 case ZPOOL_PROP_GUID
:
234 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
235 (void) snprintf(buf
, len
, "%llu", intval
);
238 case ZPOOL_PROP_ALTROOT
:
239 case ZPOOL_PROP_CACHEFILE
:
240 case ZPOOL_PROP_COMMENT
:
241 if (zhp
->zpool_props
!= NULL
||
242 zpool_get_all_props(zhp
) == 0) {
244 zpool_get_prop_string(zhp
, prop
, &src
),
252 (void) strlcpy(buf
, "-", len
);
261 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
) &&
262 prop
!= ZPOOL_PROP_NAME
)
265 switch (zpool_prop_get_type(prop
)) {
266 case PROP_TYPE_STRING
:
267 (void) strlcpy(buf
, zpool_get_prop_string(zhp
, prop
, &src
),
271 case PROP_TYPE_NUMBER
:
272 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
275 case ZPOOL_PROP_SIZE
:
276 case ZPOOL_PROP_ALLOCATED
:
277 case ZPOOL_PROP_FREE
:
278 case ZPOOL_PROP_FREEING
:
279 case ZPOOL_PROP_EXPANDSZ
:
280 (void) zfs_nicenum(intval
, buf
, len
);
283 case ZPOOL_PROP_CAPACITY
:
284 (void) snprintf(buf
, len
, "%llu%%",
285 (u_longlong_t
)intval
);
288 case ZPOOL_PROP_DEDUPRATIO
:
289 (void) snprintf(buf
, len
, "%llu.%02llux",
290 (u_longlong_t
)(intval
/ 100),
291 (u_longlong_t
)(intval
% 100));
294 case ZPOOL_PROP_HEALTH
:
295 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
296 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
297 verify(nvlist_lookup_uint64_array(nvroot
,
298 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
301 (void) strlcpy(buf
, zpool_state_to_name(intval
,
304 case ZPOOL_PROP_VERSION
:
305 if (intval
>= SPA_VERSION_FEATURES
) {
306 (void) snprintf(buf
, len
, "-");
311 (void) snprintf(buf
, len
, "%llu", intval
);
315 case PROP_TYPE_INDEX
:
316 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
317 if (zpool_prop_index_to_string(prop
, intval
, &strval
)
320 (void) strlcpy(buf
, strval
, len
);
334 * Check if the bootfs name has the same pool name as it is set to.
335 * Assuming bootfs is a valid dataset name.
338 bootfs_name_valid(const char *pool
, char *bootfs
)
340 int len
= strlen(pool
);
342 if (!zfs_name_valid(bootfs
, ZFS_TYPE_FILESYSTEM
|ZFS_TYPE_SNAPSHOT
))
345 if (strncmp(pool
, bootfs
, len
) == 0 &&
346 (bootfs
[len
] == '/' || bootfs
[len
] == '\0'))
353 * Inspect the configuration to determine if any of the devices contain
357 pool_uses_efi(nvlist_t
*config
)
362 if (nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_CHILDREN
,
363 &child
, &children
) != 0)
364 return (read_efi_label(config
, NULL
) >= 0);
366 for (c
= 0; c
< children
; c
++) {
367 if (pool_uses_efi(child
[c
]))
374 zpool_is_bootable(zpool_handle_t
*zhp
)
376 char bootfs
[ZPOOL_MAXNAMELEN
];
378 return (zpool_get_prop(zhp
, ZPOOL_PROP_BOOTFS
, bootfs
,
379 sizeof (bootfs
), NULL
) == 0 && strncmp(bootfs
, "-",
380 sizeof (bootfs
)) != 0);
385 * Given an nvlist of zpool properties to be set, validate that they are
386 * correct, and parse any numeric properties (index, boolean, etc) if they are
387 * specified as strings.
390 zpool_valid_proplist(libzfs_handle_t
*hdl
, const char *poolname
,
391 nvlist_t
*props
, uint64_t version
, prop_flags_t flags
, char *errbuf
)
399 struct stat64 statbuf
;
403 if (nvlist_alloc(&retprops
, NV_UNIQUE_NAME
, 0) != 0) {
404 (void) no_memory(hdl
);
409 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
410 const char *propname
= nvpair_name(elem
);
412 prop
= zpool_name_to_prop(propname
);
413 if (prop
== ZPROP_INVAL
&& zpool_prop_feature(propname
)) {
415 zfeature_info_t
*feature
;
416 char *fname
= strchr(propname
, '@') + 1;
418 err
= zfeature_lookup_name(fname
, &feature
);
420 ASSERT3U(err
, ==, ENOENT
);
421 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
422 "invalid feature '%s'"), fname
);
423 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
427 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
428 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
429 "'%s' must be a string"), propname
);
430 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
434 (void) nvpair_value_string(elem
, &strval
);
435 if (strcmp(strval
, ZFS_FEATURE_ENABLED
) != 0) {
436 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
437 "property '%s' can only be set to "
438 "'enabled'"), propname
);
439 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
443 if (nvlist_add_uint64(retprops
, propname
, 0) != 0) {
444 (void) no_memory(hdl
);
451 * Make sure this property is valid and applies to this type.
453 if (prop
== ZPROP_INVAL
) {
454 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
455 "invalid property '%s'"), propname
);
456 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
460 if (zpool_prop_readonly(prop
)) {
461 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
462 "is readonly"), propname
);
463 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
467 if (zprop_parse_value(hdl
, elem
, prop
, ZFS_TYPE_POOL
, retprops
,
468 &strval
, &intval
, errbuf
) != 0)
472 * Perform additional checking for specific properties.
475 case ZPOOL_PROP_VERSION
:
476 if (intval
< version
||
477 !SPA_VERSION_IS_SUPPORTED(intval
)) {
478 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
479 "property '%s' number %d is invalid."),
481 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
486 case ZPOOL_PROP_BOOTFS
:
487 if (flags
.create
|| flags
.import
) {
488 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
489 "property '%s' cannot be set at creation "
490 "or import time"), propname
);
491 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
495 if (version
< SPA_VERSION_BOOTFS
) {
496 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
497 "pool must be upgraded to support "
498 "'%s' property"), propname
);
499 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
504 * bootfs property value has to be a dataset name and
505 * the dataset has to be in the same pool as it sets to.
507 if (strval
[0] != '\0' && !bootfs_name_valid(poolname
,
509 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
510 "is an invalid name"), strval
);
511 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
515 if ((zhp
= zpool_open_canfail(hdl
, poolname
)) == NULL
) {
516 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
517 "could not open pool '%s'"), poolname
);
518 (void) zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
);
521 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
522 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
525 * bootfs property cannot be set on a disk which has
528 if (pool_uses_efi(nvroot
)) {
529 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
530 "property '%s' not supported on "
531 "EFI labeled devices"), propname
);
532 (void) zfs_error(hdl
, EZFS_POOL_NOTSUP
, errbuf
);
539 case ZPOOL_PROP_ALTROOT
:
540 if (!flags
.create
&& !flags
.import
) {
541 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
542 "property '%s' can only be set during pool "
543 "creation or import"), propname
);
544 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
548 if (strval
[0] != '/') {
549 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
550 "bad alternate root '%s'"), strval
);
551 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
556 case ZPOOL_PROP_CACHEFILE
:
557 if (strval
[0] == '\0')
560 if (strcmp(strval
, "none") == 0)
563 if (strval
[0] != '/') {
564 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
565 "property '%s' must be empty, an "
566 "absolute path, or 'none'"), propname
);
567 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
571 slash
= strrchr(strval
, '/');
573 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
574 strcmp(slash
, "/..") == 0) {
575 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
576 "'%s' is not a valid file"), strval
);
577 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
583 if (strval
[0] != '\0' &&
584 (stat64(strval
, &statbuf
) != 0 ||
585 !S_ISDIR(statbuf
.st_mode
))) {
586 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
587 "'%s' is not a valid directory"),
589 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
596 case ZPOOL_PROP_COMMENT
:
597 for (check
= strval
; *check
!= '\0'; check
++) {
598 if (!isprint(*check
)) {
600 dgettext(TEXT_DOMAIN
,
601 "comment may only have printable "
603 (void) zfs_error(hdl
, EZFS_BADPROP
,
608 if (strlen(strval
) > ZPROP_MAX_COMMENT
) {
609 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
610 "comment must not exceed %d characters"),
612 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
616 case ZPOOL_PROP_READONLY
:
618 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
619 "property '%s' can only be set at "
620 "import time"), propname
);
621 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
630 nvlist_free(retprops
);
635 * Set zpool property : propname=propval.
638 zpool_set_prop(zpool_handle_t
*zhp
, const char *propname
, const char *propval
)
640 zfs_cmd_t zc
= { 0 };
643 nvlist_t
*nvl
= NULL
;
646 prop_flags_t flags
= { 0 };
648 (void) snprintf(errbuf
, sizeof (errbuf
),
649 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
652 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
653 return (no_memory(zhp
->zpool_hdl
));
655 if (nvlist_add_string(nvl
, propname
, propval
) != 0) {
657 return (no_memory(zhp
->zpool_hdl
));
660 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
661 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
662 zhp
->zpool_name
, nvl
, version
, flags
, errbuf
)) == NULL
) {
671 * Execute the corresponding ioctl() to set this property.
673 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
675 if (zcmd_write_src_nvlist(zhp
->zpool_hdl
, &zc
, nvl
) != 0) {
680 ret
= zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_SET_PROPS
, &zc
);
682 zcmd_free_nvlists(&zc
);
686 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
688 (void) zpool_props_refresh(zhp
);
694 zpool_expand_proplist(zpool_handle_t
*zhp
, zprop_list_t
**plp
)
696 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
698 char buf
[ZFS_MAXPROPLEN
];
699 nvlist_t
*features
= NULL
;
701 boolean_t firstexpand
= (NULL
== *plp
);
703 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_POOL
) != 0)
707 while (*last
!= NULL
)
708 last
= &(*last
)->pl_next
;
711 features
= zpool_get_features(zhp
);
713 if ((*plp
)->pl_all
&& firstexpand
) {
714 for (int i
= 0; i
< SPA_FEATURES
; i
++) {
715 zprop_list_t
*entry
= zfs_alloc(hdl
,
716 sizeof (zprop_list_t
));
717 entry
->pl_prop
= ZPROP_INVAL
;
718 entry
->pl_user_prop
= zfs_asprintf(hdl
, "feature@%s",
719 spa_feature_table
[i
].fi_uname
);
720 entry
->pl_width
= strlen(entry
->pl_user_prop
);
721 entry
->pl_all
= B_TRUE
;
724 last
= &entry
->pl_next
;
728 /* add any unsupported features */
729 for (nvpair_t
*nvp
= nvlist_next_nvpair(features
, NULL
);
730 nvp
!= NULL
; nvp
= nvlist_next_nvpair(features
, nvp
)) {
735 if (zfeature_is_supported(nvpair_name(nvp
)))
738 propname
= zfs_asprintf(hdl
, "unsupported@%s",
742 * Before adding the property to the list make sure that no
743 * other pool already added the same property.
747 while (entry
!= NULL
) {
748 if (entry
->pl_user_prop
!= NULL
&&
749 strcmp(propname
, entry
->pl_user_prop
) == 0) {
753 entry
= entry
->pl_next
;
760 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
761 entry
->pl_prop
= ZPROP_INVAL
;
762 entry
->pl_user_prop
= propname
;
763 entry
->pl_width
= strlen(entry
->pl_user_prop
);
764 entry
->pl_all
= B_TRUE
;
767 last
= &entry
->pl_next
;
770 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
775 if (entry
->pl_prop
!= ZPROP_INVAL
&&
776 zpool_get_prop(zhp
, entry
->pl_prop
, buf
, sizeof (buf
),
778 if (strlen(buf
) > entry
->pl_width
)
779 entry
->pl_width
= strlen(buf
);
787 * Get the state for the given feature on the given ZFS pool.
790 zpool_prop_get_feature(zpool_handle_t
*zhp
, const char *propname
, char *buf
,
794 boolean_t found
= B_FALSE
;
795 nvlist_t
*features
= zpool_get_features(zhp
);
797 const char *feature
= strchr(propname
, '@') + 1;
799 supported
= zpool_prop_feature(propname
);
800 ASSERT(supported
|| zfs_prop_unsupported(propname
));
803 * Convert from feature name to feature guid. This conversion is
804 * unecessary for unsupported@... properties because they already
811 ret
= zfeature_lookup_name(feature
, &fi
);
813 (void) strlcpy(buf
, "-", len
);
816 feature
= fi
->fi_guid
;
819 if (nvlist_lookup_uint64(features
, feature
, &refcount
) == 0)
824 (void) strlcpy(buf
, ZFS_FEATURE_DISABLED
, len
);
827 (void) strlcpy(buf
, ZFS_FEATURE_ENABLED
, len
);
829 (void) strlcpy(buf
, ZFS_FEATURE_ACTIVE
, len
);
834 (void) strcpy(buf
, ZFS_UNSUPPORTED_INACTIVE
);
836 (void) strcpy(buf
, ZFS_UNSUPPORTED_READONLY
);
839 (void) strlcpy(buf
, "-", len
);
848 * Don't start the slice at the default block of 34; many storage
849 * devices will use a stripe width of 128k, so start there instead.
851 #define NEW_START_BLOCK 256
854 * Validate the given pool name, optionally putting an extended error message in
858 zpool_name_valid(libzfs_handle_t
*hdl
, boolean_t isopen
, const char *pool
)
864 ret
= pool_namecheck(pool
, &why
, &what
);
867 * The rules for reserved pool names were extended at a later point.
868 * But we need to support users with existing pools that may now be
869 * invalid. So we only check for this expanded set of names during a
870 * create (or import), and only in userland.
872 if (ret
== 0 && !isopen
&&
873 (strncmp(pool
, "mirror", 6) == 0 ||
874 strncmp(pool
, "raidz", 5) == 0 ||
875 strncmp(pool
, "spare", 5) == 0 ||
876 strcmp(pool
, "log") == 0)) {
879 dgettext(TEXT_DOMAIN
, "name is reserved"));
887 case NAME_ERR_TOOLONG
:
889 dgettext(TEXT_DOMAIN
, "name is too long"));
892 case NAME_ERR_INVALCHAR
:
894 dgettext(TEXT_DOMAIN
, "invalid character "
895 "'%c' in pool name"), what
);
898 case NAME_ERR_NOLETTER
:
899 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
900 "name must begin with a letter"));
903 case NAME_ERR_RESERVED
:
904 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
905 "name is reserved"));
908 case NAME_ERR_DISKLIKE
:
909 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
910 "pool name is reserved"));
913 case NAME_ERR_LEADING_SLASH
:
914 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
915 "leading slash in name"));
918 case NAME_ERR_EMPTY_COMPONENT
:
919 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
920 "empty component in name"));
923 case NAME_ERR_TRAILING_SLASH
:
924 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
925 "trailing slash in name"));
928 case NAME_ERR_MULTIPLE_AT
:
929 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
930 "multiple '@' delimiters in name"));
942 * Open a handle to the given pool, even if the pool is currently in the FAULTED
946 zpool_open_canfail(libzfs_handle_t
*hdl
, const char *pool
)
952 * Make sure the pool name is valid.
954 if (!zpool_name_valid(hdl
, B_TRUE
, pool
)) {
955 (void) zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
956 dgettext(TEXT_DOMAIN
, "cannot open '%s'"),
961 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
964 zhp
->zpool_hdl
= hdl
;
965 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
967 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
973 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "no such pool"));
974 (void) zfs_error_fmt(hdl
, EZFS_NOENT
,
975 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), pool
);
984 * Like the above, but silent on error. Used when iterating over pools (because
985 * the configuration cache may be out of date).
988 zpool_open_silent(libzfs_handle_t
*hdl
, const char *pool
, zpool_handle_t
**ret
)
993 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
996 zhp
->zpool_hdl
= hdl
;
997 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
999 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1015 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1019 zpool_open(libzfs_handle_t
*hdl
, const char *pool
)
1021 zpool_handle_t
*zhp
;
1023 if ((zhp
= zpool_open_canfail(hdl
, pool
)) == NULL
)
1026 if (zhp
->zpool_state
== POOL_STATE_UNAVAIL
) {
1027 (void) zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
1028 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), zhp
->zpool_name
);
1037 * Close the handle. Simply frees the memory associated with the handle.
1040 zpool_close(zpool_handle_t
*zhp
)
1042 if (zhp
->zpool_config
)
1043 nvlist_free(zhp
->zpool_config
);
1044 if (zhp
->zpool_old_config
)
1045 nvlist_free(zhp
->zpool_old_config
);
1046 if (zhp
->zpool_props
)
1047 nvlist_free(zhp
->zpool_props
);
1052 * Return the name of the pool.
1055 zpool_get_name(zpool_handle_t
*zhp
)
1057 return (zhp
->zpool_name
);
1062 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1065 zpool_get_state(zpool_handle_t
*zhp
)
1067 return (zhp
->zpool_state
);
1071 * Create the named pool, using the provided vdev list. It is assumed
1072 * that the consumer has already validated the contents of the nvlist, so we
1073 * don't have to worry about error semantics.
1076 zpool_create(libzfs_handle_t
*hdl
, const char *pool
, nvlist_t
*nvroot
,
1077 nvlist_t
*props
, nvlist_t
*fsprops
)
1079 zfs_cmd_t zc
= { 0 };
1080 nvlist_t
*zc_fsprops
= NULL
;
1081 nvlist_t
*zc_props
= NULL
;
1085 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1086 "cannot create '%s'"), pool
);
1088 if (!zpool_name_valid(hdl
, B_FALSE
, pool
))
1089 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
1091 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1095 prop_flags_t flags
= { .create
= B_TRUE
, .import
= B_FALSE
};
1097 if ((zc_props
= zpool_valid_proplist(hdl
, pool
, props
,
1098 SPA_VERSION_1
, flags
, msg
)) == NULL
) {
1107 zoned
= ((nvlist_lookup_string(fsprops
,
1108 zfs_prop_to_name(ZFS_PROP_ZONED
), &zonestr
) == 0) &&
1109 strcmp(zonestr
, "on") == 0);
1111 if ((zc_fsprops
= zfs_valid_proplist(hdl
,
1112 ZFS_TYPE_FILESYSTEM
, fsprops
, zoned
, NULL
, msg
)) == NULL
) {
1116 (nvlist_alloc(&zc_props
, NV_UNIQUE_NAME
, 0) != 0)) {
1119 if (nvlist_add_nvlist(zc_props
,
1120 ZPOOL_ROOTFS_PROPS
, zc_fsprops
) != 0) {
1125 if (zc_props
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
1128 (void) strlcpy(zc
.zc_name
, pool
, sizeof (zc
.zc_name
));
1130 if ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_CREATE
, &zc
)) != 0) {
1132 zcmd_free_nvlists(&zc
);
1133 nvlist_free(zc_props
);
1134 nvlist_free(zc_fsprops
);
1139 * This can happen if the user has specified the same
1140 * device multiple times. We can't reliably detect this
1141 * until we try to add it and see we already have a
1144 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1145 "one or more vdevs refer to the same device"));
1146 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1150 * This occurs when one of the devices is below
1151 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1152 * device was the problem device since there's no
1153 * reliable way to determine device size from userland.
1158 zfs_nicenum(SPA_MINDEVSIZE
, buf
, sizeof (buf
));
1160 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1161 "one or more devices is less than the "
1162 "minimum size (%s)"), buf
);
1164 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1167 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1168 "one or more devices is out of space"));
1169 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1172 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1173 "cache device must be a disk or disk slice"));
1174 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1177 return (zpool_standard_error(hdl
, errno
, msg
));
1182 zcmd_free_nvlists(&zc
);
1183 nvlist_free(zc_props
);
1184 nvlist_free(zc_fsprops
);
1189 * Destroy the given pool. It is up to the caller to ensure that there are no
1190 * datasets left in the pool.
1193 zpool_destroy(zpool_handle_t
*zhp
, const char *log_str
)
1195 zfs_cmd_t zc
= { 0 };
1196 zfs_handle_t
*zfp
= NULL
;
1197 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1200 if (zhp
->zpool_state
== POOL_STATE_ACTIVE
&&
1201 (zfp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
1204 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1205 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1207 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_DESTROY
, &zc
) != 0) {
1208 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1209 "cannot destroy '%s'"), zhp
->zpool_name
);
1211 if (errno
== EROFS
) {
1212 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1213 "one or more devices is read only"));
1214 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1216 (void) zpool_standard_error(hdl
, errno
, msg
);
1225 remove_mountpoint(zfp
);
1233 * Add the given vdevs to the pool. The caller must have already performed the
1234 * necessary verification to ensure that the vdev specification is well-formed.
1237 zpool_add(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
1239 zfs_cmd_t zc
= { 0 };
1241 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1243 nvlist_t
**spares
, **l2cache
;
1244 uint_t nspares
, nl2cache
;
1246 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1247 "cannot add to '%s'"), zhp
->zpool_name
);
1249 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1250 SPA_VERSION_SPARES
&&
1251 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1252 &spares
, &nspares
) == 0) {
1253 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1254 "upgraded to add hot spares"));
1255 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1258 if (zpool_is_bootable(zhp
) && nvlist_lookup_nvlist_array(nvroot
,
1259 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0) {
1262 for (s
= 0; s
< nspares
; s
++) {
1265 if (nvlist_lookup_string(spares
[s
], ZPOOL_CONFIG_PATH
,
1266 &path
) == 0 && pool_uses_efi(spares
[s
])) {
1267 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1268 "device '%s' contains an EFI label and "
1269 "cannot be used on root pools."),
1270 zpool_vdev_name(hdl
, NULL
, spares
[s
],
1272 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, msg
));
1277 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1278 SPA_VERSION_L2CACHE
&&
1279 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1280 &l2cache
, &nl2cache
) == 0) {
1281 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1282 "upgraded to add cache devices"));
1283 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1286 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1288 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1290 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_ADD
, &zc
) != 0) {
1294 * This can happen if the user has specified the same
1295 * device multiple times. We can't reliably detect this
1296 * until we try to add it and see we already have a
1299 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1300 "one or more vdevs refer to the same device"));
1301 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1306 * This occurrs when one of the devices is below
1307 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1308 * device was the problem device since there's no
1309 * reliable way to determine device size from userland.
1314 zfs_nicenum(SPA_MINDEVSIZE
, buf
, sizeof (buf
));
1316 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1317 "device is less than the minimum "
1320 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1324 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1325 "pool must be upgraded to add these vdevs"));
1326 (void) zfs_error(hdl
, EZFS_BADVERSION
, msg
);
1330 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1331 "root pool can not have multiple vdevs"
1332 " or separate logs"));
1333 (void) zfs_error(hdl
, EZFS_POOL_NOTSUP
, msg
);
1337 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1338 "cache device must be a disk or disk slice"));
1339 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1343 (void) zpool_standard_error(hdl
, errno
, msg
);
1351 zcmd_free_nvlists(&zc
);
1357 * Exports the pool from the system. The caller must ensure that there are no
1358 * mounted datasets in the pool.
1361 zpool_export_common(zpool_handle_t
*zhp
, boolean_t force
, boolean_t hardforce
,
1362 const char *log_str
)
1364 zfs_cmd_t zc
= { 0 };
1367 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1368 "cannot export '%s'"), zhp
->zpool_name
);
1370 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1371 zc
.zc_cookie
= force
;
1372 zc
.zc_guid
= hardforce
;
1373 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1375 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_EXPORT
, &zc
) != 0) {
1378 zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
1379 "use '-f' to override the following errors:\n"
1380 "'%s' has an active shared spare which could be"
1381 " used by other pools once '%s' is exported."),
1382 zhp
->zpool_name
, zhp
->zpool_name
);
1383 return (zfs_error(zhp
->zpool_hdl
, EZFS_ACTIVE_SPARE
,
1386 return (zpool_standard_error_fmt(zhp
->zpool_hdl
, errno
,
1395 zpool_export(zpool_handle_t
*zhp
, boolean_t force
, const char *log_str
)
1397 return (zpool_export_common(zhp
, force
, B_FALSE
, log_str
));
1401 zpool_export_force(zpool_handle_t
*zhp
, const char *log_str
)
1403 return (zpool_export_common(zhp
, B_TRUE
, B_TRUE
, log_str
));
1407 zpool_rewind_exclaim(libzfs_handle_t
*hdl
, const char *name
, boolean_t dryrun
,
1410 nvlist_t
*nv
= NULL
;
1416 if (!hdl
->libzfs_printerr
|| config
== NULL
)
1419 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1420 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0) {
1424 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1426 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1428 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1429 strftime(timestr
, 128, 0, &t
) != 0) {
1431 (void) printf(dgettext(TEXT_DOMAIN
,
1432 "Would be able to return %s "
1433 "to its state as of %s.\n"),
1436 (void) printf(dgettext(TEXT_DOMAIN
,
1437 "Pool %s returned to its state as of %s.\n"),
1441 (void) printf(dgettext(TEXT_DOMAIN
,
1442 "%s approximately %lld "),
1443 dryrun
? "Would discard" : "Discarded",
1445 (void) printf(dgettext(TEXT_DOMAIN
,
1446 "minutes of transactions.\n"));
1447 } else if (loss
> 0) {
1448 (void) printf(dgettext(TEXT_DOMAIN
,
1449 "%s approximately %lld "),
1450 dryrun
? "Would discard" : "Discarded", loss
);
1451 (void) printf(dgettext(TEXT_DOMAIN
,
1452 "seconds of transactions.\n"));
1458 zpool_explain_recover(libzfs_handle_t
*hdl
, const char *name
, int reason
,
1461 nvlist_t
*nv
= NULL
;
1463 uint64_t edata
= UINT64_MAX
;
1468 if (!hdl
->libzfs_printerr
)
1472 (void) printf(dgettext(TEXT_DOMAIN
, "action: "));
1474 (void) printf(dgettext(TEXT_DOMAIN
, "\t"));
1476 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1477 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1478 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0 ||
1479 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1482 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1483 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_DATA_ERRORS
,
1486 (void) printf(dgettext(TEXT_DOMAIN
,
1487 "Recovery is possible, but will result in some data loss.\n"));
1489 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1490 strftime(timestr
, 128, 0, &t
) != 0) {
1491 (void) printf(dgettext(TEXT_DOMAIN
,
1492 "\tReturning the pool to its state as of %s\n"
1493 "\tshould correct the problem. "),
1496 (void) printf(dgettext(TEXT_DOMAIN
,
1497 "\tReverting the pool to an earlier state "
1498 "should correct the problem.\n\t"));
1502 (void) printf(dgettext(TEXT_DOMAIN
,
1503 "Approximately %lld minutes of data\n"
1504 "\tmust be discarded, irreversibly. "), (loss
+ 30) / 60);
1505 } else if (loss
> 0) {
1506 (void) printf(dgettext(TEXT_DOMAIN
,
1507 "Approximately %lld seconds of data\n"
1508 "\tmust be discarded, irreversibly. "), loss
);
1510 if (edata
!= 0 && edata
!= UINT64_MAX
) {
1512 (void) printf(dgettext(TEXT_DOMAIN
,
1513 "After rewind, at least\n"
1514 "\tone persistent user-data error will remain. "));
1516 (void) printf(dgettext(TEXT_DOMAIN
,
1517 "After rewind, several\n"
1518 "\tpersistent user-data errors will remain. "));
1521 (void) printf(dgettext(TEXT_DOMAIN
,
1522 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1523 reason
>= 0 ? "clear" : "import", name
);
1525 (void) printf(dgettext(TEXT_DOMAIN
,
1526 "A scrub of the pool\n"
1527 "\tis strongly recommended after recovery.\n"));
1531 (void) printf(dgettext(TEXT_DOMAIN
,
1532 "Destroy and re-create the pool from\n\ta backup source.\n"));
1536 * zpool_import() is a contracted interface. Should be kept the same
1539 * Applications should use zpool_import_props() to import a pool with
1540 * new properties value to be set.
1543 zpool_import(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1546 nvlist_t
*props
= NULL
;
1549 if (altroot
!= NULL
) {
1550 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
1551 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1552 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1556 if (nvlist_add_string(props
,
1557 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), altroot
) != 0 ||
1558 nvlist_add_string(props
,
1559 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
), "none") != 0) {
1561 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1562 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1567 ret
= zpool_import_props(hdl
, config
, newname
, props
,
1575 print_vdev_tree(libzfs_handle_t
*hdl
, const char *name
, nvlist_t
*nv
,
1581 uint64_t is_log
= 0;
1583 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
,
1587 (void) printf("\t%*s%s%s\n", indent
, "", name
,
1588 is_log
? " [log]" : "");
1590 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1591 &child
, &children
) != 0)
1594 for (c
= 0; c
< children
; c
++) {
1595 vname
= zpool_vdev_name(hdl
, NULL
, child
[c
], B_TRUE
);
1596 print_vdev_tree(hdl
, vname
, child
[c
], indent
+ 2);
1602 zpool_print_unsup_feat(nvlist_t
*config
)
1604 nvlist_t
*nvinfo
, *unsup_feat
;
1606 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) ==
1608 verify(nvlist_lookup_nvlist(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
,
1611 for (nvpair_t
*nvp
= nvlist_next_nvpair(unsup_feat
, NULL
); nvp
!= NULL
;
1612 nvp
= nvlist_next_nvpair(unsup_feat
, nvp
)) {
1615 verify(nvpair_type(nvp
) == DATA_TYPE_STRING
);
1616 verify(nvpair_value_string(nvp
, &desc
) == 0);
1618 if (strlen(desc
) > 0)
1619 (void) printf("\t%s (%s)\n", nvpair_name(nvp
), desc
);
1621 (void) printf("\t%s\n", nvpair_name(nvp
));
1626 * Import the given pool using the known configuration and a list of
1627 * properties to be set. The configuration should have come from
1628 * zpool_find_import(). The 'newname' parameters control whether the pool
1629 * is imported with a different name.
1632 zpool_import_props(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1633 nvlist_t
*props
, int flags
)
1635 zfs_cmd_t zc
= { 0 };
1636 zpool_rewind_policy_t policy
;
1637 nvlist_t
*nv
= NULL
;
1638 nvlist_t
*nvinfo
= NULL
;
1639 nvlist_t
*missing
= NULL
;
1646 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1649 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1650 "cannot import pool '%s'"), origname
);
1652 if (newname
!= NULL
) {
1653 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
1654 return (zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1655 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1657 thename
= (char *)newname
;
1664 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
1666 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
1669 if ((props
= zpool_valid_proplist(hdl
, origname
,
1670 props
, version
, flags
, errbuf
)) == NULL
) {
1672 } else if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
1678 (void) strlcpy(zc
.zc_name
, thename
, sizeof (zc
.zc_name
));
1680 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1683 if (zcmd_write_conf_nvlist(hdl
, &zc
, config
) != 0) {
1687 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zc
.zc_nvlist_conf_size
* 2) != 0) {
1692 zc
.zc_cookie
= flags
;
1693 while ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_IMPORT
, &zc
)) != 0 &&
1695 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
1696 zcmd_free_nvlists(&zc
);
1703 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nv
);
1704 zpool_get_rewind_policy(config
, &policy
);
1710 * Dry-run failed, but we print out what success
1711 * looks like if we found a best txg
1713 if (policy
.zrp_request
& ZPOOL_TRY_REWIND
) {
1714 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1720 if (newname
== NULL
)
1721 (void) snprintf(desc
, sizeof (desc
),
1722 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1725 (void) snprintf(desc
, sizeof (desc
),
1726 dgettext(TEXT_DOMAIN
, "cannot import '%s' as '%s'"),
1731 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
1732 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1733 nvlist_exists(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
)) {
1734 (void) printf(dgettext(TEXT_DOMAIN
, "This "
1735 "pool uses the following feature(s) not "
1736 "supported by this system:\n"));
1737 zpool_print_unsup_feat(nv
);
1738 if (nvlist_exists(nvinfo
,
1739 ZPOOL_CONFIG_CAN_RDONLY
)) {
1740 (void) printf(dgettext(TEXT_DOMAIN
,
1741 "All unsupported features are only "
1742 "required for writing to the pool."
1743 "\nThe pool can be imported using "
1744 "'-o readonly=on'.\n"));
1748 * Unsupported version.
1750 (void) zfs_error(hdl
, EZFS_BADVERSION
, desc
);
1754 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, desc
);
1758 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1759 "one or more devices is read only"));
1760 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
1764 if (nv
&& nvlist_lookup_nvlist(nv
,
1765 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1766 nvlist_lookup_nvlist(nvinfo
,
1767 ZPOOL_CONFIG_MISSING_DEVICES
, &missing
) == 0) {
1768 (void) printf(dgettext(TEXT_DOMAIN
,
1769 "The devices below are missing, use "
1770 "'-m' to import the pool anyway:\n"));
1771 print_vdev_tree(hdl
, NULL
, missing
, 2);
1772 (void) printf("\n");
1774 (void) zpool_standard_error(hdl
, error
, desc
);
1778 (void) zpool_standard_error(hdl
, error
, desc
);
1782 (void) zpool_standard_error(hdl
, error
, desc
);
1783 zpool_explain_recover(hdl
,
1784 newname
? origname
: thename
, -error
, nv
);
1791 zpool_handle_t
*zhp
;
1794 * This should never fail, but play it safe anyway.
1796 if (zpool_open_silent(hdl
, thename
, &zhp
) != 0)
1798 else if (zhp
!= NULL
)
1800 if (policy
.zrp_request
&
1801 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
1802 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1803 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0), nv
);
1809 zcmd_free_nvlists(&zc
);
1819 zpool_scan(zpool_handle_t
*zhp
, pool_scan_func_t func
)
1821 zfs_cmd_t zc
= { 0 };
1823 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1825 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1826 zc
.zc_cookie
= func
;
1828 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_SCAN
, &zc
) == 0 ||
1829 (errno
== ENOENT
&& func
!= POOL_SCAN_NONE
))
1832 if (func
== POOL_SCAN_SCRUB
) {
1833 (void) snprintf(msg
, sizeof (msg
),
1834 dgettext(TEXT_DOMAIN
, "cannot scrub %s"), zc
.zc_name
);
1835 } else if (func
== POOL_SCAN_NONE
) {
1836 (void) snprintf(msg
, sizeof (msg
),
1837 dgettext(TEXT_DOMAIN
, "cannot cancel scrubbing %s"),
1840 assert(!"unexpected result");
1843 if (errno
== EBUSY
) {
1845 pool_scan_stat_t
*ps
= NULL
;
1848 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
1849 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
1850 (void) nvlist_lookup_uint64_array(nvroot
,
1851 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &psc
);
1852 if (ps
&& ps
->pss_func
== POOL_SCAN_SCRUB
)
1853 return (zfs_error(hdl
, EZFS_SCRUBBING
, msg
));
1855 return (zfs_error(hdl
, EZFS_RESILVERING
, msg
));
1856 } else if (errno
== ENOENT
) {
1857 return (zfs_error(hdl
, EZFS_NO_SCRUB
, msg
));
1859 return (zpool_standard_error(hdl
, errno
, msg
));
1864 * This provides a very minimal check whether a given string is likely a
1865 * c#t#d# style string. Users of this are expected to do their own
1866 * verification of the s# part.
1868 #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1]))
1871 * More elaborate version for ones which may start with "/dev/dsk/"
1875 ctd_check_path(char *str
) {
1877 * If it starts with a slash, check the last component.
1879 if (str
&& str
[0] == '/') {
1880 char *tmp
= strrchr(str
, '/');
1883 * If it ends in "/old", check the second-to-last
1884 * component of the string instead.
1886 if (tmp
!= str
&& strcmp(tmp
, "/old") == 0) {
1887 for (tmp
--; *tmp
!= '/'; tmp
--)
1892 return (CTD_CHECK(str
));
1896 * Find a vdev that matches the search criteria specified. We use the
1897 * the nvpair name to determine how we should look for the device.
1898 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1899 * spare; but FALSE if its an INUSE spare.
1902 vdev_to_nvlist_iter(nvlist_t
*nv
, nvlist_t
*search
, boolean_t
*avail_spare
,
1903 boolean_t
*l2cache
, boolean_t
*log
)
1910 nvpair_t
*pair
= nvlist_next_nvpair(search
, NULL
);
1912 /* Nothing to look for */
1913 if (search
== NULL
|| pair
== NULL
)
1916 /* Obtain the key we will use to search */
1917 srchkey
= nvpair_name(pair
);
1919 switch (nvpair_type(pair
)) {
1920 case DATA_TYPE_UINT64
:
1921 if (strcmp(srchkey
, ZPOOL_CONFIG_GUID
) == 0) {
1922 uint64_t srchval
, theguid
;
1924 verify(nvpair_value_uint64(pair
, &srchval
) == 0);
1925 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1927 if (theguid
== srchval
)
1932 case DATA_TYPE_STRING
: {
1933 char *srchval
, *val
;
1935 verify(nvpair_value_string(pair
, &srchval
) == 0);
1936 if (nvlist_lookup_string(nv
, srchkey
, &val
) != 0)
1940 * Search for the requested value. Special cases:
1942 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
1943 * "s0" or "s0/old". The "s0" part is hidden from the user,
1944 * but included in the string, so this matches around it.
1945 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1947 * Otherwise, all other searches are simple string compares.
1949 if (strcmp(srchkey
, ZPOOL_CONFIG_PATH
) == 0 &&
1950 ctd_check_path(val
)) {
1951 uint64_t wholedisk
= 0;
1953 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
1956 int slen
= strlen(srchval
);
1957 int vlen
= strlen(val
);
1959 if (slen
!= vlen
- 2)
1963 * make_leaf_vdev() should only set
1964 * wholedisk for ZPOOL_CONFIG_PATHs which
1965 * will include "/dev/dsk/", giving plenty of
1966 * room for the indices used next.
1971 * strings identical except trailing "s0"
1973 if (strcmp(&val
[vlen
- 2], "s0") == 0 &&
1974 strncmp(srchval
, val
, slen
) == 0)
1978 * strings identical except trailing "s0/old"
1980 if (strcmp(&val
[vlen
- 6], "s0/old") == 0 &&
1981 strcmp(&srchval
[slen
- 4], "/old") == 0 &&
1982 strncmp(srchval
, val
, slen
- 4) == 0)
1987 } else if (strcmp(srchkey
, ZPOOL_CONFIG_TYPE
) == 0 && val
) {
1988 char *type
, *idx
, *end
, *p
;
1989 uint64_t id
, vdev_id
;
1992 * Determine our vdev type, keeping in mind
1993 * that the srchval is composed of a type and
1994 * vdev id pair (i.e. mirror-4).
1996 if ((type
= strdup(srchval
)) == NULL
)
1999 if ((p
= strrchr(type
, '-')) == NULL
) {
2007 * If the types don't match then keep looking.
2009 if (strncmp(val
, type
, strlen(val
)) != 0) {
2014 verify(strncmp(type
, VDEV_TYPE_RAIDZ
,
2015 strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2016 strncmp(type
, VDEV_TYPE_MIRROR
,
2017 strlen(VDEV_TYPE_MIRROR
)) == 0);
2018 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
2022 vdev_id
= strtoull(idx
, &end
, 10);
2029 * Now verify that we have the correct vdev id.
2038 if (strcmp(srchval
, val
) == 0)
2047 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2048 &child
, &children
) != 0)
2051 for (c
= 0; c
< children
; c
++) {
2052 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2053 avail_spare
, l2cache
, NULL
)) != NULL
) {
2055 * The 'is_log' value is only set for the toplevel
2056 * vdev, not the leaf vdevs. So we always lookup the
2057 * log device from the root of the vdev tree (where
2058 * 'log' is non-NULL).
2061 nvlist_lookup_uint64(child
[c
],
2062 ZPOOL_CONFIG_IS_LOG
, &is_log
) == 0 &&
2070 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2071 &child
, &children
) == 0) {
2072 for (c
= 0; c
< children
; c
++) {
2073 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2074 avail_spare
, l2cache
, NULL
)) != NULL
) {
2075 *avail_spare
= B_TRUE
;
2081 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2082 &child
, &children
) == 0) {
2083 for (c
= 0; c
< children
; c
++) {
2084 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2085 avail_spare
, l2cache
, NULL
)) != NULL
) {
2096 * Given a physical path (minus the "/devices" prefix), find the
2100 zpool_find_vdev_by_physpath(zpool_handle_t
*zhp
, const char *ppath
,
2101 boolean_t
*avail_spare
, boolean_t
*l2cache
, boolean_t
*log
)
2103 nvlist_t
*search
, *nvroot
, *ret
;
2105 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2106 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PHYS_PATH
, ppath
) == 0);
2108 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2111 *avail_spare
= B_FALSE
;
2115 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2116 nvlist_free(search
);
2122 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2125 zpool_vdev_is_interior(const char *name
)
2127 if (strncmp(name
, VDEV_TYPE_RAIDZ
, strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2128 strncmp(name
, VDEV_TYPE_MIRROR
, strlen(VDEV_TYPE_MIRROR
)) == 0)
2134 zpool_find_vdev(zpool_handle_t
*zhp
, const char *path
, boolean_t
*avail_spare
,
2135 boolean_t
*l2cache
, boolean_t
*log
)
2137 char buf
[MAXPATHLEN
];
2139 nvlist_t
*nvroot
, *search
, *ret
;
2142 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2144 guid
= strtoull(path
, &end
, 10);
2145 if (guid
!= 0 && *end
== '\0') {
2146 verify(nvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
) == 0);
2147 } else if (zpool_vdev_is_interior(path
)) {
2148 verify(nvlist_add_string(search
, ZPOOL_CONFIG_TYPE
, path
) == 0);
2149 } else if (path
[0] != '/') {
2150 (void) snprintf(buf
, sizeof (buf
), "%s%s", "/dev/dsk/", path
);
2151 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PATH
, buf
) == 0);
2153 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PATH
, path
) == 0);
2156 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2159 *avail_spare
= B_FALSE
;
2163 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2164 nvlist_free(search
);
2170 vdev_online(nvlist_t
*nv
)
2174 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
, &ival
) == 0 ||
2175 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
, &ival
) == 0 ||
2176 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
, &ival
) == 0)
2183 * Helper function for zpool_get_physpaths().
2186 vdev_get_one_physpath(nvlist_t
*config
, char *physpath
, size_t physpath_size
,
2187 size_t *bytes_written
)
2189 size_t bytes_left
, pos
, rsz
;
2193 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PHYS_PATH
,
2195 return (EZFS_NODEVICE
);
2197 pos
= *bytes_written
;
2198 bytes_left
= physpath_size
- pos
;
2199 format
= (pos
== 0) ? "%s" : " %s";
2201 rsz
= snprintf(physpath
+ pos
, bytes_left
, format
, tmppath
);
2202 *bytes_written
+= rsz
;
2204 if (rsz
>= bytes_left
) {
2205 /* if physpath was not copied properly, clear it */
2206 if (bytes_left
!= 0) {
2209 return (EZFS_NOSPC
);
2215 vdev_get_physpaths(nvlist_t
*nv
, char *physpath
, size_t phypath_size
,
2216 size_t *rsz
, boolean_t is_spare
)
2221 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
2222 return (EZFS_INVALCONFIG
);
2224 if (strcmp(type
, VDEV_TYPE_DISK
) == 0) {
2226 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2227 * For a spare vdev, we only want to boot from the active
2232 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
2235 return (EZFS_INVALCONFIG
);
2238 if (vdev_online(nv
)) {
2239 if ((ret
= vdev_get_one_physpath(nv
, physpath
,
2240 phypath_size
, rsz
)) != 0)
2243 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) == 0 ||
2244 strcmp(type
, VDEV_TYPE_REPLACING
) == 0 ||
2245 (is_spare
= (strcmp(type
, VDEV_TYPE_SPARE
) == 0))) {
2250 if (nvlist_lookup_nvlist_array(nv
,
2251 ZPOOL_CONFIG_CHILDREN
, &child
, &count
) != 0)
2252 return (EZFS_INVALCONFIG
);
2254 for (i
= 0; i
< count
; i
++) {
2255 ret
= vdev_get_physpaths(child
[i
], physpath
,
2256 phypath_size
, rsz
, is_spare
);
2257 if (ret
== EZFS_NOSPC
)
2262 return (EZFS_POOL_INVALARG
);
2266 * Get phys_path for a root pool config.
2267 * Return 0 on success; non-zero on failure.
2270 zpool_get_config_physpath(nvlist_t
*config
, char *physpath
, size_t phypath_size
)
2273 nvlist_t
*vdev_root
;
2280 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2282 return (EZFS_INVALCONFIG
);
2284 if (nvlist_lookup_string(vdev_root
, ZPOOL_CONFIG_TYPE
, &type
) != 0 ||
2285 nvlist_lookup_nvlist_array(vdev_root
, ZPOOL_CONFIG_CHILDREN
,
2286 &child
, &count
) != 0)
2287 return (EZFS_INVALCONFIG
);
2290 * root pool can not have EFI labeled disks and can only have
2291 * a single top-level vdev.
2293 if (strcmp(type
, VDEV_TYPE_ROOT
) != 0 || count
!= 1 ||
2294 pool_uses_efi(vdev_root
))
2295 return (EZFS_POOL_INVALARG
);
2297 (void) vdev_get_physpaths(child
[0], physpath
, phypath_size
, &rsz
,
2300 /* No online devices */
2302 return (EZFS_NODEVICE
);
2308 * Get phys_path for a root pool
2309 * Return 0 on success; non-zero on failure.
2312 zpool_get_physpath(zpool_handle_t
*zhp
, char *physpath
, size_t phypath_size
)
2314 return (zpool_get_config_physpath(zhp
->zpool_config
, physpath
,
2319 * If the device has being dynamically expanded then we need to relabel
2320 * the disk to use the new unallocated space.
2323 zpool_relabel_disk(libzfs_handle_t
*hdl
, const char *name
)
2325 char path
[MAXPATHLEN
];
2328 int (*_efi_use_whole_disk
)(int);
2330 if ((_efi_use_whole_disk
= (int (*)(int))dlsym(RTLD_DEFAULT
,
2331 "efi_use_whole_disk")) == NULL
)
2334 (void) snprintf(path
, sizeof (path
), "%s/%s", RDISK_ROOT
, name
);
2336 if ((fd
= open(path
, O_RDWR
| O_NDELAY
)) < 0) {
2337 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2338 "relabel '%s': unable to open device"), name
);
2339 return (zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
));
2343 * It's possible that we might encounter an error if the device
2344 * does not have any unallocated space left. If so, we simply
2345 * ignore that error and continue on.
2347 error
= _efi_use_whole_disk(fd
);
2349 if (error
&& error
!= VT_ENOSPC
) {
2350 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2351 "relabel '%s': unable to read disk capacity"), name
);
2352 return (zfs_error(hdl
, EZFS_NOCAP
, errbuf
));
2358 * Bring the specified vdev online. The 'flags' parameter is a set of the
2359 * ZFS_ONLINE_* flags.
2362 zpool_vdev_online(zpool_handle_t
*zhp
, const char *path
, int flags
,
2363 vdev_state_t
*newstate
)
2365 zfs_cmd_t zc
= { 0 };
2368 boolean_t avail_spare
, l2cache
, islog
;
2369 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2371 if (flags
& ZFS_ONLINE_EXPAND
) {
2372 (void) snprintf(msg
, sizeof (msg
),
2373 dgettext(TEXT_DOMAIN
, "cannot expand %s"), path
);
2375 (void) snprintf(msg
, sizeof (msg
),
2376 dgettext(TEXT_DOMAIN
, "cannot online %s"), path
);
2379 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2380 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2382 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2384 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2387 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2389 if (flags
& ZFS_ONLINE_EXPAND
||
2390 zpool_get_prop_int(zhp
, ZPOOL_PROP_AUTOEXPAND
, NULL
)) {
2391 char *pathname
= NULL
;
2392 uint64_t wholedisk
= 0;
2394 (void) nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_WHOLE_DISK
,
2396 verify(nvlist_lookup_string(tgt
, ZPOOL_CONFIG_PATH
,
2400 * XXX - L2ARC 1.0 devices can't support expansion.
2403 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2404 "cannot expand cache devices"));
2405 return (zfs_error(hdl
, EZFS_VDEVNOTSUP
, msg
));
2409 pathname
+= strlen(DISK_ROOT
) + 1;
2410 (void) zpool_relabel_disk(hdl
, pathname
);
2414 zc
.zc_cookie
= VDEV_STATE_ONLINE
;
2417 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) != 0) {
2418 if (errno
== EINVAL
) {
2419 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "was split "
2420 "from this pool into a new one. Use '%s' "
2421 "instead"), "zpool detach");
2422 return (zfs_error(hdl
, EZFS_POSTSPLIT_ONLINE
, msg
));
2424 return (zpool_standard_error(hdl
, errno
, msg
));
2427 *newstate
= zc
.zc_cookie
;
2432 * Take the specified vdev offline
2435 zpool_vdev_offline(zpool_handle_t
*zhp
, const char *path
, boolean_t istmp
)
2437 zfs_cmd_t zc
= { 0 };
2440 boolean_t avail_spare
, l2cache
;
2441 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2443 (void) snprintf(msg
, sizeof (msg
),
2444 dgettext(TEXT_DOMAIN
, "cannot offline %s"), path
);
2446 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2447 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2449 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2451 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2454 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2456 zc
.zc_cookie
= VDEV_STATE_OFFLINE
;
2457 zc
.zc_obj
= istmp
? ZFS_OFFLINE_TEMPORARY
: 0;
2459 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2466 * There are no other replicas of this device.
2468 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2472 * The log device has unplayed logs
2474 return (zfs_error(hdl
, EZFS_UNPLAYED_LOGS
, msg
));
2477 return (zpool_standard_error(hdl
, errno
, msg
));
2482 * Mark the given vdev faulted.
2485 zpool_vdev_fault(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2487 zfs_cmd_t zc
= { 0 };
2489 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2491 (void) snprintf(msg
, sizeof (msg
),
2492 dgettext(TEXT_DOMAIN
, "cannot fault %llu"), guid
);
2494 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2496 zc
.zc_cookie
= VDEV_STATE_FAULTED
;
2499 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2506 * There are no other replicas of this device.
2508 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2511 return (zpool_standard_error(hdl
, errno
, msg
));
2517 * Mark the given vdev degraded.
2520 zpool_vdev_degrade(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2522 zfs_cmd_t zc
= { 0 };
2524 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2526 (void) snprintf(msg
, sizeof (msg
),
2527 dgettext(TEXT_DOMAIN
, "cannot degrade %llu"), guid
);
2529 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2531 zc
.zc_cookie
= VDEV_STATE_DEGRADED
;
2534 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2537 return (zpool_standard_error(hdl
, errno
, msg
));
2541 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2545 is_replacing_spare(nvlist_t
*search
, nvlist_t
*tgt
, int which
)
2551 if (nvlist_lookup_nvlist_array(search
, ZPOOL_CONFIG_CHILDREN
, &child
,
2553 verify(nvlist_lookup_string(search
, ZPOOL_CONFIG_TYPE
,
2556 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0 &&
2557 children
== 2 && child
[which
] == tgt
)
2560 for (c
= 0; c
< children
; c
++)
2561 if (is_replacing_spare(child
[c
], tgt
, which
))
2569 * Attach new_disk (fully described by nvroot) to old_disk.
2570 * If 'replacing' is specified, the new disk will replace the old one.
2573 zpool_vdev_attach(zpool_handle_t
*zhp
,
2574 const char *old_disk
, const char *new_disk
, nvlist_t
*nvroot
, int replacing
)
2576 zfs_cmd_t zc
= { 0 };
2580 boolean_t avail_spare
, l2cache
, islog
;
2585 nvlist_t
*config_root
;
2586 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2587 boolean_t rootpool
= zpool_is_bootable(zhp
);
2590 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2591 "cannot replace %s with %s"), old_disk
, new_disk
);
2593 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2594 "cannot attach %s to %s"), new_disk
, old_disk
);
2597 * If this is a root pool, make sure that we're not attaching an
2598 * EFI labeled device.
2600 if (rootpool
&& pool_uses_efi(nvroot
)) {
2601 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2602 "EFI labeled devices are not supported on root pools."));
2603 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, msg
));
2606 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2607 if ((tgt
= zpool_find_vdev(zhp
, old_disk
, &avail_spare
, &l2cache
,
2609 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2612 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2615 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2617 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2618 zc
.zc_cookie
= replacing
;
2620 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
2621 &child
, &children
) != 0 || children
!= 1) {
2622 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2623 "new device must be a single disk"));
2624 return (zfs_error(hdl
, EZFS_INVALCONFIG
, msg
));
2627 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
2628 ZPOOL_CONFIG_VDEV_TREE
, &config_root
) == 0);
2630 if ((newname
= zpool_vdev_name(NULL
, NULL
, child
[0], B_FALSE
)) == NULL
)
2634 * If the target is a hot spare that has been swapped in, we can only
2635 * replace it with another hot spare.
2638 nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_IS_SPARE
, &val
) == 0 &&
2639 (zpool_find_vdev(zhp
, newname
, &avail_spare
, &l2cache
,
2640 NULL
) == NULL
|| !avail_spare
) &&
2641 is_replacing_spare(config_root
, tgt
, 1)) {
2642 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2643 "can only be replaced by another hot spare"));
2645 return (zfs_error(hdl
, EZFS_BADTARGET
, msg
));
2650 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
2653 ret
= zfs_ioctl(hdl
, ZFS_IOC_VDEV_ATTACH
, &zc
);
2655 zcmd_free_nvlists(&zc
);
2660 * XXX need a better way to prevent user from
2661 * booting up a half-baked vdev.
2663 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Make "
2664 "sure to wait until resilver is done "
2665 "before rebooting.\n"));
2673 * Can't attach to or replace this type of vdev.
2676 uint64_t version
= zpool_get_prop_int(zhp
,
2677 ZPOOL_PROP_VERSION
, NULL
);
2680 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2681 "cannot replace a log with a spare"));
2682 else if (version
>= SPA_VERSION_MULTI_REPLACE
)
2683 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2684 "already in replacing/spare config; wait "
2685 "for completion or use 'zpool detach'"));
2687 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2688 "cannot replace a replacing device"));
2690 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2691 "can only attach to mirrors and top-level "
2694 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2699 * The new device must be a single disk.
2701 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2702 "new device must be a single disk"));
2703 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2707 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "%s is busy"),
2709 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2714 * The new device is too small.
2716 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2717 "device is too small"));
2718 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2723 * The new device has a different alignment requirement.
2725 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2726 "devices have different sector alignment"));
2727 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2732 * The resulting top-level vdev spec won't fit in the label.
2734 (void) zfs_error(hdl
, EZFS_DEVOVERFLOW
, msg
);
2738 (void) zpool_standard_error(hdl
, errno
, msg
);
2745 * Detach the specified device.
2748 zpool_vdev_detach(zpool_handle_t
*zhp
, const char *path
)
2750 zfs_cmd_t zc
= { 0 };
2753 boolean_t avail_spare
, l2cache
;
2754 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2756 (void) snprintf(msg
, sizeof (msg
),
2757 dgettext(TEXT_DOMAIN
, "cannot detach %s"), path
);
2759 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2760 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2762 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2765 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2768 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2770 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2772 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_DETACH
, &zc
) == 0)
2779 * Can't detach from this type of vdev.
2781 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "only "
2782 "applicable to mirror and replacing vdevs"));
2783 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2788 * There are no other replicas of this device.
2790 (void) zfs_error(hdl
, EZFS_NOREPLICAS
, msg
);
2794 (void) zpool_standard_error(hdl
, errno
, msg
);
2801 * Find a mirror vdev in the source nvlist.
2803 * The mchild array contains a list of disks in one of the top-level mirrors
2804 * of the source pool. The schild array contains a list of disks that the
2805 * user specified on the command line. We loop over the mchild array to
2806 * see if any entry in the schild array matches.
2808 * If a disk in the mchild array is found in the schild array, we return
2809 * the index of that entry. Otherwise we return -1.
2812 find_vdev_entry(zpool_handle_t
*zhp
, nvlist_t
**mchild
, uint_t mchildren
,
2813 nvlist_t
**schild
, uint_t schildren
)
2817 for (mc
= 0; mc
< mchildren
; mc
++) {
2819 char *mpath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
2820 mchild
[mc
], B_FALSE
);
2822 for (sc
= 0; sc
< schildren
; sc
++) {
2823 char *spath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
2824 schild
[sc
], B_FALSE
);
2825 boolean_t result
= (strcmp(mpath
, spath
) == 0);
2841 * Split a mirror pool. If newroot points to null, then a new nvlist
2842 * is generated and it is the responsibility of the caller to free it.
2845 zpool_vdev_split(zpool_handle_t
*zhp
, char *newname
, nvlist_t
**newroot
,
2846 nvlist_t
*props
, splitflags_t flags
)
2848 zfs_cmd_t zc
= { 0 };
2850 nvlist_t
*tree
, *config
, **child
, **newchild
, *newconfig
= NULL
;
2851 nvlist_t
**varray
= NULL
, *zc_props
= NULL
;
2852 uint_t c
, children
, newchildren
, lastlog
= 0, vcount
, found
= 0;
2853 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2855 boolean_t freelist
= B_FALSE
, memory_err
= B_TRUE
;
2858 (void) snprintf(msg
, sizeof (msg
),
2859 dgettext(TEXT_DOMAIN
, "Unable to split %s"), zhp
->zpool_name
);
2861 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
2862 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
2864 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
2865 (void) fprintf(stderr
, gettext("Internal error: unable to "
2866 "retrieve pool configuration\n"));
2870 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &tree
)
2872 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
, &vers
) == 0);
2875 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
2876 if ((zc_props
= zpool_valid_proplist(hdl
, zhp
->zpool_name
,
2877 props
, vers
, flags
, msg
)) == NULL
)
2881 if (nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
2883 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2884 "Source pool is missing vdev tree"));
2886 nvlist_free(zc_props
);
2890 varray
= zfs_alloc(hdl
, children
* sizeof (nvlist_t
*));
2893 if (*newroot
== NULL
||
2894 nvlist_lookup_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
2895 &newchild
, &newchildren
) != 0)
2898 for (c
= 0; c
< children
; c
++) {
2899 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
2901 nvlist_t
**mchild
, *vdev
;
2906 * Unlike cache & spares, slogs are stored in the
2907 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2909 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2911 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
2913 if (is_log
|| is_hole
) {
2915 * Create a hole vdev and put it in the config.
2917 if (nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) != 0)
2919 if (nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
,
2920 VDEV_TYPE_HOLE
) != 0)
2922 if (nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_HOLE
,
2927 varray
[vcount
++] = vdev
;
2931 verify(nvlist_lookup_string(child
[c
], ZPOOL_CONFIG_TYPE
, &type
)
2933 if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
2934 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2935 "Source pool must be composed only of mirrors\n"));
2936 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2940 verify(nvlist_lookup_nvlist_array(child
[c
],
2941 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
2943 /* find or add an entry for this top-level vdev */
2944 if (newchildren
> 0 &&
2945 (entry
= find_vdev_entry(zhp
, mchild
, mchildren
,
2946 newchild
, newchildren
)) >= 0) {
2947 /* We found a disk that the user specified. */
2948 vdev
= mchild
[entry
];
2951 /* User didn't specify a disk for this vdev. */
2952 vdev
= mchild
[mchildren
- 1];
2955 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
2959 /* did we find every disk the user specified? */
2960 if (found
!= newchildren
) {
2961 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Device list must "
2962 "include at most one disk from each mirror"));
2963 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2967 /* Prepare the nvlist for populating. */
2968 if (*newroot
== NULL
) {
2969 if (nvlist_alloc(newroot
, NV_UNIQUE_NAME
, 0) != 0)
2972 if (nvlist_add_string(*newroot
, ZPOOL_CONFIG_TYPE
,
2973 VDEV_TYPE_ROOT
) != 0)
2976 verify(nvlist_remove_all(*newroot
, ZPOOL_CONFIG_CHILDREN
) == 0);
2979 /* Add all the children we found */
2980 if (nvlist_add_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
, varray
,
2981 lastlog
== 0 ? vcount
: lastlog
) != 0)
2985 * If we're just doing a dry run, exit now with success.
2988 memory_err
= B_FALSE
;
2993 /* now build up the config list & call the ioctl */
2994 if (nvlist_alloc(&newconfig
, NV_UNIQUE_NAME
, 0) != 0)
2997 if (nvlist_add_nvlist(newconfig
,
2998 ZPOOL_CONFIG_VDEV_TREE
, *newroot
) != 0 ||
2999 nvlist_add_string(newconfig
,
3000 ZPOOL_CONFIG_POOL_NAME
, newname
) != 0 ||
3001 nvlist_add_uint64(newconfig
, ZPOOL_CONFIG_VERSION
, vers
) != 0)
3005 * The new pool is automatically part of the namespace unless we
3006 * explicitly export it.
3009 zc
.zc_cookie
= ZPOOL_EXPORT_AFTER_SPLIT
;
3010 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3011 (void) strlcpy(zc
.zc_string
, newname
, sizeof (zc
.zc_string
));
3012 if (zcmd_write_conf_nvlist(hdl
, &zc
, newconfig
) != 0)
3014 if (zc_props
!= NULL
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
3017 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SPLIT
, &zc
) != 0) {
3018 retval
= zpool_standard_error(hdl
, errno
, msg
);
3023 memory_err
= B_FALSE
;
3026 if (varray
!= NULL
) {
3029 for (v
= 0; v
< vcount
; v
++)
3030 nvlist_free(varray
[v
]);
3033 zcmd_free_nvlists(&zc
);
3035 nvlist_free(zc_props
);
3037 nvlist_free(newconfig
);
3039 nvlist_free(*newroot
);
3047 return (no_memory(hdl
));
3053 * Remove the given device. Currently, this is supported only for hot spares
3054 * and level 2 cache devices.
3057 zpool_vdev_remove(zpool_handle_t
*zhp
, const char *path
)
3059 zfs_cmd_t zc
= { 0 };
3062 boolean_t avail_spare
, l2cache
, islog
;
3063 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3066 (void) snprintf(msg
, sizeof (msg
),
3067 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3069 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3070 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3072 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3074 * XXX - this should just go away.
3076 if (!avail_spare
&& !l2cache
&& !islog
) {
3077 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3078 "only inactive hot spares, cache, top-level, "
3079 "or log devices can be removed"));
3080 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3083 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
3084 if (islog
&& version
< SPA_VERSION_HOLES
) {
3085 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3086 "pool must be upgrade to support log removal"));
3087 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
3090 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
3092 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3095 return (zpool_standard_error(hdl
, errno
, msg
));
3099 * Clear the errors for the pool, or the particular device if specified.
3102 zpool_clear(zpool_handle_t
*zhp
, const char *path
, nvlist_t
*rewindnvl
)
3104 zfs_cmd_t zc
= { 0 };
3107 zpool_rewind_policy_t policy
;
3108 boolean_t avail_spare
, l2cache
;
3109 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3110 nvlist_t
*nvi
= NULL
;
3114 (void) snprintf(msg
, sizeof (msg
),
3115 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3118 (void) snprintf(msg
, sizeof (msg
),
3119 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3122 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3124 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
,
3125 &l2cache
, NULL
)) == 0)
3126 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3129 * Don't allow error clearing for hot spares. Do allow
3130 * error clearing for l2cache devices.
3133 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3135 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
,
3139 zpool_get_rewind_policy(rewindnvl
, &policy
);
3140 zc
.zc_cookie
= policy
.zrp_request
;
3142 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zhp
->zpool_config_size
* 2) != 0)
3145 if (zcmd_write_src_nvlist(hdl
, &zc
, rewindnvl
) != 0)
3148 while ((error
= zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
)) != 0 &&
3150 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
3151 zcmd_free_nvlists(&zc
);
3156 if (!error
|| ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) &&
3157 errno
!= EPERM
&& errno
!= EACCES
)) {
3158 if (policy
.zrp_request
&
3159 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
3160 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nvi
);
3161 zpool_rewind_exclaim(hdl
, zc
.zc_name
,
3162 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0),
3166 zcmd_free_nvlists(&zc
);
3170 zcmd_free_nvlists(&zc
);
3171 return (zpool_standard_error(hdl
, errno
, msg
));
3175 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3178 zpool_vdev_clear(zpool_handle_t
*zhp
, uint64_t guid
)
3180 zfs_cmd_t zc
= { 0 };
3182 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3184 (void) snprintf(msg
, sizeof (msg
),
3185 dgettext(TEXT_DOMAIN
, "cannot clear errors for %llx"),
3188 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3190 zc
.zc_cookie
= ZPOOL_NO_REWIND
;
3192 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_CLEAR
, &zc
) == 0)
3195 return (zpool_standard_error(hdl
, errno
, msg
));
3199 * Change the GUID for a pool.
3202 zpool_reguid(zpool_handle_t
*zhp
)
3205 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3206 zfs_cmd_t zc
= { 0 };
3208 (void) snprintf(msg
, sizeof (msg
),
3209 dgettext(TEXT_DOMAIN
, "cannot reguid '%s'"), zhp
->zpool_name
);
3211 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3212 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REGUID
, &zc
) == 0)
3215 return (zpool_standard_error(hdl
, errno
, msg
));
3222 zpool_reopen(zpool_handle_t
*zhp
)
3224 zfs_cmd_t zc
= { 0 };
3226 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3228 (void) snprintf(msg
, sizeof (msg
),
3229 dgettext(TEXT_DOMAIN
, "cannot reopen '%s'"),
3232 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3233 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REOPEN
, &zc
) == 0)
3235 return (zpool_standard_error(hdl
, errno
, msg
));
3239 * Convert from a devid string to a path.
3242 devid_to_path(char *devid_str
)
3247 devid_nmlist_t
*list
= NULL
;
3250 if (devid_str_decode(devid_str
, &devid
, &minor
) != 0)
3253 ret
= devid_deviceid_to_nmlist("/dev", devid
, minor
, &list
);
3255 devid_str_free(minor
);
3261 if ((path
= strdup(list
[0].devname
)) == NULL
)
3264 devid_free_nmlist(list
);
3270 * Convert from a path to a devid string.
3273 path_to_devid(const char *path
)
3279 if ((fd
= open(path
, O_RDONLY
)) < 0)
3284 if (devid_get(fd
, &devid
) == 0) {
3285 if (devid_get_minor_name(fd
, &minor
) == 0)
3286 ret
= devid_str_encode(devid
, minor
);
3288 devid_str_free(minor
);
3297 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3298 * ignore any failure here, since a common case is for an unprivileged user to
3299 * type 'zpool status', and we'll display the correct information anyway.
3302 set_path(zpool_handle_t
*zhp
, nvlist_t
*nv
, const char *path
)
3304 zfs_cmd_t zc
= { 0 };
3306 (void) strncpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3307 (void) strncpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
3308 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
3311 (void) ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_VDEV_SETPATH
, &zc
);
3315 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3316 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3317 * We also check if this is a whole disk, in which case we strip off the
3318 * trailing 's0' slice name.
3320 * This routine is also responsible for identifying when disks have been
3321 * reconfigured in a new location. The kernel will have opened the device by
3322 * devid, but the path will still refer to the old location. To catch this, we
3323 * first do a path -> devid translation (which is fast for the common case). If
3324 * the devid matches, we're done. If not, we do a reverse devid -> path
3325 * translation and issue the appropriate ioctl() to update the path of the vdev.
3326 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3330 zpool_vdev_name(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, nvlist_t
*nv
,
3339 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
3341 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
3343 (void) snprintf(buf
, sizeof (buf
), "%llu",
3344 (u_longlong_t
)value
);
3346 } else if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
3349 * If the device is dead (faulted, offline, etc) then don't
3350 * bother opening it. Otherwise we may be forcing the user to
3351 * open a misbehaving device, which can have undesirable
3354 if ((nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
3355 (uint64_t **)&vs
, &vsc
) != 0 ||
3356 vs
->vs_state
>= VDEV_STATE_DEGRADED
) &&
3358 nvlist_lookup_string(nv
, ZPOOL_CONFIG_DEVID
, &devid
) == 0) {
3360 * Determine if the current path is correct.
3362 char *newdevid
= path_to_devid(path
);
3364 if (newdevid
== NULL
||
3365 strcmp(devid
, newdevid
) != 0) {
3368 if ((newpath
= devid_to_path(devid
)) != NULL
) {
3370 * Update the path appropriately.
3372 set_path(zhp
, nv
, newpath
);
3373 if (nvlist_add_string(nv
,
3374 ZPOOL_CONFIG_PATH
, newpath
) == 0)
3375 verify(nvlist_lookup_string(nv
,
3383 devid_str_free(newdevid
);
3386 if (strncmp(path
, "/dev/dsk/", 9) == 0)
3389 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
3390 &value
) == 0 && value
) {
3391 int pathlen
= strlen(path
);
3392 char *tmp
= zfs_strdup(hdl
, path
);
3395 * If it starts with c#, and ends with "s0", chop
3396 * the "s0" off, or if it ends with "s0/old", remove
3397 * the "s0" from the middle.
3399 if (CTD_CHECK(tmp
)) {
3400 if (strcmp(&tmp
[pathlen
- 2], "s0") == 0) {
3401 tmp
[pathlen
- 2] = '\0';
3402 } else if (pathlen
> 6 &&
3403 strcmp(&tmp
[pathlen
- 6], "s0/old") == 0) {
3404 (void) strcpy(&tmp
[pathlen
- 6],
3411 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &path
) == 0);
3414 * If it's a raidz device, we need to stick in the parity level.
3416 if (strcmp(path
, VDEV_TYPE_RAIDZ
) == 0) {
3417 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
,
3419 (void) snprintf(buf
, sizeof (buf
), "%s%llu", path
,
3420 (u_longlong_t
)value
);
3425 * We identify each top-level vdev by using a <type-id>
3426 * naming convention.
3431 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
3433 (void) snprintf(buf
, sizeof (buf
), "%s-%llu", path
,
3439 return (zfs_strdup(hdl
, path
));
3443 zbookmark_compare(const void *a
, const void *b
)
3445 return (memcmp(a
, b
, sizeof (zbookmark_t
)));
3449 * Retrieve the persistent error log, uniquify the members, and return to the
3453 zpool_get_errlog(zpool_handle_t
*zhp
, nvlist_t
**nverrlistp
)
3455 zfs_cmd_t zc
= { 0 };
3457 zbookmark_t
*zb
= NULL
;
3461 * Retrieve the raw error list from the kernel. If the number of errors
3462 * has increased, allocate more space and continue until we get the
3465 verify(nvlist_lookup_uint64(zhp
->zpool_config
, ZPOOL_CONFIG_ERRCOUNT
,
3469 if ((zc
.zc_nvlist_dst
= (uintptr_t)zfs_alloc(zhp
->zpool_hdl
,
3470 count
* sizeof (zbookmark_t
))) == (uintptr_t)NULL
)
3472 zc
.zc_nvlist_dst_size
= count
;
3473 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3475 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_ERROR_LOG
,
3477 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3478 if (errno
== ENOMEM
) {
3479 count
= zc
.zc_nvlist_dst_size
;
3480 if ((zc
.zc_nvlist_dst
= (uintptr_t)
3481 zfs_alloc(zhp
->zpool_hdl
, count
*
3482 sizeof (zbookmark_t
))) == (uintptr_t)NULL
)
3493 * Sort the resulting bookmarks. This is a little confusing due to the
3494 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3495 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3496 * _not_ copied as part of the process. So we point the start of our
3497 * array appropriate and decrement the total number of elements.
3499 zb
= ((zbookmark_t
*)(uintptr_t)zc
.zc_nvlist_dst
) +
3500 zc
.zc_nvlist_dst_size
;
3501 count
-= zc
.zc_nvlist_dst_size
;
3503 qsort(zb
, count
, sizeof (zbookmark_t
), zbookmark_compare
);
3505 verify(nvlist_alloc(nverrlistp
, 0, KM_SLEEP
) == 0);
3508 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3510 for (i
= 0; i
< count
; i
++) {
3513 /* ignoring zb_blkid and zb_level for now */
3514 if (i
> 0 && zb
[i
-1].zb_objset
== zb
[i
].zb_objset
&&
3515 zb
[i
-1].zb_object
== zb
[i
].zb_object
)
3518 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) != 0)
3520 if (nvlist_add_uint64(nv
, ZPOOL_ERR_DATASET
,
3521 zb
[i
].zb_objset
) != 0) {
3525 if (nvlist_add_uint64(nv
, ZPOOL_ERR_OBJECT
,
3526 zb
[i
].zb_object
) != 0) {
3530 if (nvlist_add_nvlist(*nverrlistp
, "ejk", nv
) != 0) {
3537 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3541 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3542 return (no_memory(zhp
->zpool_hdl
));
3546 * Upgrade a ZFS pool to the latest on-disk version.
3549 zpool_upgrade(zpool_handle_t
*zhp
, uint64_t new_version
)
3551 zfs_cmd_t zc
= { 0 };
3552 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3554 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3555 zc
.zc_cookie
= new_version
;
3557 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_UPGRADE
, &zc
) != 0)
3558 return (zpool_standard_error_fmt(hdl
, errno
,
3559 dgettext(TEXT_DOMAIN
, "cannot upgrade '%s'"),
3565 zfs_save_arguments(int argc
, char **argv
, char *string
, int len
)
3567 (void) strlcpy(string
, basename(argv
[0]), len
);
3568 for (int i
= 1; i
< argc
; i
++) {
3569 (void) strlcat(string
, " ", len
);
3570 (void) strlcat(string
, argv
[i
], len
);
3575 zpool_log_history(libzfs_handle_t
*hdl
, const char *message
)
3577 zfs_cmd_t zc
= { 0 };
3581 args
= fnvlist_alloc();
3582 fnvlist_add_string(args
, "message", message
);
3583 err
= zcmd_write_src_nvlist(hdl
, &zc
, args
);
3585 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_LOG_HISTORY
, &zc
);
3587 zcmd_free_nvlists(&zc
);
3592 * Perform ioctl to get some command history of a pool.
3594 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3595 * logical offset of the history buffer to start reading from.
3597 * Upon return, 'off' is the next logical offset to read from and
3598 * 'len' is the actual amount of bytes read into 'buf'.
3601 get_history(zpool_handle_t
*zhp
, char *buf
, uint64_t *off
, uint64_t *len
)
3603 zfs_cmd_t zc
= { 0 };
3604 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3606 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3608 zc
.zc_history
= (uint64_t)(uintptr_t)buf
;
3609 zc
.zc_history_len
= *len
;
3610 zc
.zc_history_offset
= *off
;
3612 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_HISTORY
, &zc
) != 0) {
3615 return (zfs_error_fmt(hdl
, EZFS_PERM
,
3616 dgettext(TEXT_DOMAIN
,
3617 "cannot show history for pool '%s'"),
3620 return (zfs_error_fmt(hdl
, EZFS_NOHISTORY
,
3621 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
3622 "'%s'"), zhp
->zpool_name
));
3624 return (zfs_error_fmt(hdl
, EZFS_BADVERSION
,
3625 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
3626 "'%s', pool must be upgraded"), zhp
->zpool_name
));
3628 return (zpool_standard_error_fmt(hdl
, errno
,
3629 dgettext(TEXT_DOMAIN
,
3630 "cannot get history for '%s'"), zhp
->zpool_name
));
3634 *len
= zc
.zc_history_len
;
3635 *off
= zc
.zc_history_offset
;
3641 * Process the buffer of nvlists, unpacking and storing each nvlist record
3642 * into 'records'. 'leftover' is set to the number of bytes that weren't
3643 * processed as there wasn't a complete record.
3646 zpool_history_unpack(char *buf
, uint64_t bytes_read
, uint64_t *leftover
,
3647 nvlist_t
***records
, uint_t
*numrecords
)
3653 while (bytes_read
> sizeof (reclen
)) {
3655 /* get length of packed record (stored as little endian) */
3656 for (i
= 0, reclen
= 0; i
< sizeof (reclen
); i
++)
3657 reclen
+= (uint64_t)(((uchar_t
*)buf
)[i
]) << (8*i
);
3659 if (bytes_read
< sizeof (reclen
) + reclen
)
3663 if (nvlist_unpack(buf
+ sizeof (reclen
), reclen
, &nv
, 0) != 0)
3665 bytes_read
-= sizeof (reclen
) + reclen
;
3666 buf
+= sizeof (reclen
) + reclen
;
3668 /* add record to nvlist array */
3670 if (ISP2(*numrecords
+ 1)) {
3671 *records
= realloc(*records
,
3672 *numrecords
* 2 * sizeof (nvlist_t
*));
3674 (*records
)[*numrecords
- 1] = nv
;
3677 *leftover
= bytes_read
;
3681 #define HIS_BUF_LEN (128*1024)
3684 * Retrieve the command history of a pool.
3687 zpool_get_history(zpool_handle_t
*zhp
, nvlist_t
**nvhisp
)
3689 char buf
[HIS_BUF_LEN
];
3691 nvlist_t
**records
= NULL
;
3692 uint_t numrecords
= 0;
3696 uint64_t bytes_read
= sizeof (buf
);
3699 if ((err
= get_history(zhp
, buf
, &off
, &bytes_read
)) != 0)
3702 /* if nothing else was read in, we're at EOF, just return */
3706 if ((err
= zpool_history_unpack(buf
, bytes_read
,
3707 &leftover
, &records
, &numrecords
)) != 0)
3715 verify(nvlist_alloc(nvhisp
, NV_UNIQUE_NAME
, 0) == 0);
3716 verify(nvlist_add_nvlist_array(*nvhisp
, ZPOOL_HIST_RECORD
,
3717 records
, numrecords
) == 0);
3719 for (i
= 0; i
< numrecords
; i
++)
3720 nvlist_free(records
[i
]);
3727 zpool_obj_to_path(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
3728 char *pathname
, size_t len
)
3730 zfs_cmd_t zc
= { 0 };
3731 boolean_t mounted
= B_FALSE
;
3732 char *mntpnt
= NULL
;
3733 char dsname
[MAXNAMELEN
];
3736 /* special case for the MOS */
3737 (void) snprintf(pathname
, len
, "<metadata>:<0x%llx>", obj
);
3741 /* get the dataset's name */
3742 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3744 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
,
3745 ZFS_IOC_DSOBJ_TO_DSNAME
, &zc
) != 0) {
3746 /* just write out a path of two object numbers */
3747 (void) snprintf(pathname
, len
, "<0x%llx>:<0x%llx>",
3751 (void) strlcpy(dsname
, zc
.zc_value
, sizeof (dsname
));
3753 /* find out if the dataset is mounted */
3754 mounted
= is_mounted(zhp
->zpool_hdl
, dsname
, &mntpnt
);
3756 /* get the corrupted object's path */
3757 (void) strlcpy(zc
.zc_name
, dsname
, sizeof (zc
.zc_name
));
3759 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_OBJ_TO_PATH
,
3762 (void) snprintf(pathname
, len
, "%s%s", mntpnt
,
3765 (void) snprintf(pathname
, len
, "%s:%s",
3766 dsname
, zc
.zc_value
);
3769 (void) snprintf(pathname
, len
, "%s:<0x%llx>", dsname
, obj
);
3775 * Read the EFI label from the config, if a label does not exist then
3776 * pass back the error to the caller. If the caller has passed a non-NULL
3777 * diskaddr argument then we set it to the starting address of the EFI
3781 read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
)
3785 char diskname
[MAXPATHLEN
];
3788 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PATH
, &path
) != 0)
3791 (void) snprintf(diskname
, sizeof (diskname
), "%s%s", RDISK_ROOT
,
3792 strrchr(path
, '/'));
3793 if ((fd
= open(diskname
, O_RDONLY
|O_NDELAY
)) >= 0) {
3794 struct dk_gpt
*vtoc
;
3796 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) >= 0) {
3798 *sb
= vtoc
->efi_parts
[0].p_start
;
3807 * determine where a partition starts on a disk in the current
3811 find_start_block(nvlist_t
*config
)
3815 diskaddr_t sb
= MAXOFFSET_T
;
3818 if (nvlist_lookup_nvlist_array(config
,
3819 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) != 0) {
3820 if (nvlist_lookup_uint64(config
,
3821 ZPOOL_CONFIG_WHOLE_DISK
,
3822 &wholedisk
) != 0 || !wholedisk
) {
3823 return (MAXOFFSET_T
);
3825 if (read_efi_label(config
, &sb
) < 0)
3830 for (c
= 0; c
< children
; c
++) {
3831 sb
= find_start_block(child
[c
]);
3832 if (sb
!= MAXOFFSET_T
) {
3836 return (MAXOFFSET_T
);
3840 * Label an individual disk. The name provided is the short name,
3841 * stripped of any leading /dev path.
3844 zpool_label_disk(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, char *name
)
3846 char path
[MAXPATHLEN
];
3847 struct dk_gpt
*vtoc
;
3849 size_t resv
= EFI_MIN_RESV_SIZE
;
3850 uint64_t slice_size
;
3851 diskaddr_t start_block
;
3854 /* prepare an error message just in case */
3855 (void) snprintf(errbuf
, sizeof (errbuf
),
3856 dgettext(TEXT_DOMAIN
, "cannot label '%s'"), name
);
3861 if (zpool_is_bootable(zhp
)) {
3862 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3863 "EFI labeled devices are not supported on root "
3865 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, errbuf
));
3868 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
3869 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3871 if (zhp
->zpool_start_block
== 0)
3872 start_block
= find_start_block(nvroot
);
3874 start_block
= zhp
->zpool_start_block
;
3875 zhp
->zpool_start_block
= start_block
;
3878 start_block
= NEW_START_BLOCK
;
3881 (void) snprintf(path
, sizeof (path
), "%s/%s%s", RDISK_ROOT
, name
,
3884 if ((fd
= open(path
, O_RDWR
| O_NDELAY
)) < 0) {
3886 * This shouldn't happen. We've long since verified that this
3887 * is a valid device.
3890 dgettext(TEXT_DOMAIN
, "unable to open device"));
3891 return (zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
));
3894 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, &vtoc
) != 0) {
3896 * The only way this can fail is if we run out of memory, or we
3897 * were unable to read the disk's capacity
3899 if (errno
== ENOMEM
)
3900 (void) no_memory(hdl
);
3903 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3904 "unable to read disk capacity"), name
);
3906 return (zfs_error(hdl
, EZFS_NOCAP
, errbuf
));
3909 slice_size
= vtoc
->efi_last_u_lba
+ 1;
3910 slice_size
-= EFI_MIN_RESV_SIZE
;
3911 if (start_block
== MAXOFFSET_T
)
3912 start_block
= NEW_START_BLOCK
;
3913 slice_size
-= start_block
;
3915 vtoc
->efi_parts
[0].p_start
= start_block
;
3916 vtoc
->efi_parts
[0].p_size
= slice_size
;
3919 * Why we use V_USR: V_BACKUP confuses users, and is considered
3920 * disposable by some EFI utilities (since EFI doesn't have a backup
3921 * slice). V_UNASSIGNED is supposed to be used only for zero size
3922 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
3923 * etc. were all pretty specific. V_USR is as close to reality as we
3924 * can get, in the absence of V_OTHER.
3926 vtoc
->efi_parts
[0].p_tag
= V_USR
;
3927 (void) strcpy(vtoc
->efi_parts
[0].p_name
, "zfs");
3929 vtoc
->efi_parts
[8].p_start
= slice_size
+ start_block
;
3930 vtoc
->efi_parts
[8].p_size
= resv
;
3931 vtoc
->efi_parts
[8].p_tag
= V_RESERVED
;
3933 if (efi_write(fd
, vtoc
) != 0) {
3935 * Some block drivers (like pcata) may not support EFI
3936 * GPT labels. Print out a helpful error message dir-
3937 * ecting the user to manually label the disk and give
3943 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3944 "try using fdisk(1M) and then provide a specific slice"));
3945 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
3954 supported_dump_vdev_type(libzfs_handle_t
*hdl
, nvlist_t
*config
, char *errbuf
)
3960 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
3961 if (strcmp(type
, VDEV_TYPE_RAIDZ
) == 0 ||
3962 strcmp(type
, VDEV_TYPE_FILE
) == 0 ||
3963 strcmp(type
, VDEV_TYPE_LOG
) == 0 ||
3964 strcmp(type
, VDEV_TYPE_HOLE
) == 0 ||
3965 strcmp(type
, VDEV_TYPE_MISSING
) == 0) {
3966 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3967 "vdev type '%s' is not supported"), type
);
3968 (void) zfs_error(hdl
, EZFS_VDEVNOTSUP
, errbuf
);
3971 if (nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_CHILDREN
,
3972 &child
, &children
) == 0) {
3973 for (c
= 0; c
< children
; c
++) {
3974 if (!supported_dump_vdev_type(hdl
, child
[c
], errbuf
))
3982 * check if this zvol is allowable for use as a dump device; zero if
3983 * it is, > 0 if it isn't, < 0 if it isn't a zvol
3986 zvol_check_dump_config(char *arg
)
3988 zpool_handle_t
*zhp
= NULL
;
3989 nvlist_t
*config
, *nvroot
;
3993 libzfs_handle_t
*hdl
;
3995 char poolname
[ZPOOL_MAXNAMELEN
];
3996 int pathlen
= strlen(ZVOL_FULL_DEV_DIR
);
3999 if (strncmp(arg
, ZVOL_FULL_DEV_DIR
, pathlen
)) {
4003 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4004 "dump is not supported on device '%s'"), arg
);
4006 if ((hdl
= libzfs_init()) == NULL
)
4008 libzfs_print_on_error(hdl
, B_TRUE
);
4010 volname
= arg
+ pathlen
;
4012 /* check the configuration of the pool */
4013 if ((p
= strchr(volname
, '/')) == NULL
) {
4014 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4015 "malformed dataset name"));
4016 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
4018 } else if (p
- volname
>= ZFS_MAXNAMELEN
) {
4019 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4020 "dataset name is too long"));
4021 (void) zfs_error(hdl
, EZFS_NAMETOOLONG
, errbuf
);
4024 (void) strncpy(poolname
, volname
, p
- volname
);
4025 poolname
[p
- volname
] = '\0';
4028 if ((zhp
= zpool_open(hdl
, poolname
)) == NULL
) {
4029 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4030 "could not open pool '%s'"), poolname
);
4031 (void) zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
);
4034 config
= zpool_get_config(zhp
, NULL
);
4035 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4037 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4038 "could not obtain vdev configuration for '%s'"), poolname
);
4039 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
);
4043 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
4044 &top
, &toplevels
) == 0);
4045 if (toplevels
!= 1) {
4046 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4047 "'%s' has multiple top level vdevs"), poolname
);
4048 (void) zfs_error(hdl
, EZFS_DEVOVERFLOW
, errbuf
);
4052 if (!supported_dump_vdev_type(hdl
, top
[0], errbuf
)) {