4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
26 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
39 #include <sys/efi_partition.h>
41 #include <sys/zfs_ioctl.h>
44 #include "zfs_namecheck.h"
46 #include "libzfs_impl.h"
47 #include "zfs_comutil.h"
48 #include "zfeature_common.h"
50 static int read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
);
52 #define DISK_ROOT "/dev/dsk"
53 #define RDISK_ROOT "/dev/rdsk"
54 #define BACKUP_SLICE "s2"
56 typedef struct prop_flags
{
57 int create
:1; /* Validate property on creation */
58 int import
:1; /* Validate property on import */
62 * ====================================================================
63 * zpool property functions
64 * ====================================================================
68 zpool_get_all_props(zpool_handle_t
*zhp
)
71 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
73 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
75 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
78 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_PROPS
, &zc
) != 0) {
79 if (errno
== ENOMEM
) {
80 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
81 zcmd_free_nvlists(&zc
);
85 zcmd_free_nvlists(&zc
);
90 if (zcmd_read_dst_nvlist(hdl
, &zc
, &zhp
->zpool_props
) != 0) {
91 zcmd_free_nvlists(&zc
);
95 zcmd_free_nvlists(&zc
);
101 zpool_props_refresh(zpool_handle_t
*zhp
)
105 old_props
= zhp
->zpool_props
;
107 if (zpool_get_all_props(zhp
) != 0)
110 nvlist_free(old_props
);
115 zpool_get_prop_string(zpool_handle_t
*zhp
, zpool_prop_t prop
,
121 zprop_source_t source
;
123 nvl
= zhp
->zpool_props
;
124 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
125 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &ival
) == 0);
127 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
129 source
= ZPROP_SRC_DEFAULT
;
130 if ((value
= (char *)zpool_prop_default_string(prop
)) == NULL
)
141 zpool_get_prop_int(zpool_handle_t
*zhp
, zpool_prop_t prop
, zprop_source_t
*src
)
145 zprop_source_t source
;
147 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
)) {
149 * zpool_get_all_props() has most likely failed because
150 * the pool is faulted, but if all we need is the top level
151 * vdev's guid then get it from the zhp config nvlist.
153 if ((prop
== ZPOOL_PROP_GUID
) &&
154 (nvlist_lookup_nvlist(zhp
->zpool_config
,
155 ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0) &&
156 (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
)
160 return (zpool_prop_default_numeric(prop
));
163 nvl
= zhp
->zpool_props
;
164 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
165 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &value
) == 0);
167 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
169 source
= ZPROP_SRC_DEFAULT
;
170 value
= zpool_prop_default_numeric(prop
);
180 * Map VDEV STATE to printed strings.
183 zpool_state_to_name(vdev_state_t state
, vdev_aux_t aux
)
186 case VDEV_STATE_CLOSED
:
187 case VDEV_STATE_OFFLINE
:
188 return (gettext("OFFLINE"));
189 case VDEV_STATE_REMOVED
:
190 return (gettext("REMOVED"));
191 case VDEV_STATE_CANT_OPEN
:
192 if (aux
== VDEV_AUX_CORRUPT_DATA
|| aux
== VDEV_AUX_BAD_LOG
)
193 return (gettext("FAULTED"));
194 else if (aux
== VDEV_AUX_SPLIT_POOL
)
195 return (gettext("SPLIT"));
197 return (gettext("UNAVAIL"));
198 case VDEV_STATE_FAULTED
:
199 return (gettext("FAULTED"));
200 case VDEV_STATE_DEGRADED
:
201 return (gettext("DEGRADED"));
202 case VDEV_STATE_HEALTHY
:
203 return (gettext("ONLINE"));
206 return (gettext("UNKNOWN"));
210 * Get a zpool property value for 'prop' and return the value in
211 * a pre-allocated buffer.
214 zpool_get_prop(zpool_handle_t
*zhp
, zpool_prop_t prop
, char *buf
, size_t len
,
215 zprop_source_t
*srctype
, boolean_t literal
)
219 zprop_source_t src
= ZPROP_SRC_NONE
;
224 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
226 case ZPOOL_PROP_NAME
:
227 (void) strlcpy(buf
, zpool_get_name(zhp
), len
);
230 case ZPOOL_PROP_HEALTH
:
231 (void) strlcpy(buf
, "FAULTED", len
);
234 case ZPOOL_PROP_GUID
:
235 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
236 (void) snprintf(buf
, len
, "%llu", intval
);
239 case ZPOOL_PROP_ALTROOT
:
240 case ZPOOL_PROP_CACHEFILE
:
241 case ZPOOL_PROP_COMMENT
:
242 if (zhp
->zpool_props
!= NULL
||
243 zpool_get_all_props(zhp
) == 0) {
245 zpool_get_prop_string(zhp
, prop
, &src
),
251 (void) strlcpy(buf
, "-", len
);
260 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
) &&
261 prop
!= ZPOOL_PROP_NAME
)
264 switch (zpool_prop_get_type(prop
)) {
265 case PROP_TYPE_STRING
:
266 (void) strlcpy(buf
, zpool_get_prop_string(zhp
, prop
, &src
),
270 case PROP_TYPE_NUMBER
:
271 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
274 case ZPOOL_PROP_SIZE
:
275 case ZPOOL_PROP_ALLOCATED
:
276 case ZPOOL_PROP_FREE
:
277 case ZPOOL_PROP_FREEING
:
278 case ZPOOL_PROP_LEAKED
:
280 (void) snprintf(buf
, len
, "%llu",
281 (u_longlong_t
)intval
);
283 (void) zfs_nicenum(intval
, buf
, len
);
286 case ZPOOL_PROP_EXPANDSZ
:
288 (void) strlcpy(buf
, "-", len
);
289 } else if (literal
) {
290 (void) snprintf(buf
, len
, "%llu",
291 (u_longlong_t
)intval
);
293 (void) zfs_nicenum(intval
, buf
, len
);
296 case ZPOOL_PROP_CAPACITY
:
298 (void) snprintf(buf
, len
, "%llu",
299 (u_longlong_t
)intval
);
301 (void) snprintf(buf
, len
, "%llu%%",
302 (u_longlong_t
)intval
);
305 case ZPOOL_PROP_FRAGMENTATION
:
306 if (intval
== UINT64_MAX
) {
307 (void) strlcpy(buf
, "-", len
);
309 (void) snprintf(buf
, len
, "%llu%%",
310 (u_longlong_t
)intval
);
313 case ZPOOL_PROP_DEDUPRATIO
:
314 (void) snprintf(buf
, len
, "%llu.%02llux",
315 (u_longlong_t
)(intval
/ 100),
316 (u_longlong_t
)(intval
% 100));
318 case ZPOOL_PROP_HEALTH
:
319 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
320 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
321 verify(nvlist_lookup_uint64_array(nvroot
,
322 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
325 (void) strlcpy(buf
, zpool_state_to_name(intval
,
328 case ZPOOL_PROP_VERSION
:
329 if (intval
>= SPA_VERSION_FEATURES
) {
330 (void) snprintf(buf
, len
, "-");
335 (void) snprintf(buf
, len
, "%llu", intval
);
339 case PROP_TYPE_INDEX
:
340 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
341 if (zpool_prop_index_to_string(prop
, intval
, &strval
)
344 (void) strlcpy(buf
, strval
, len
);
358 * Check if the bootfs name has the same pool name as it is set to.
359 * Assuming bootfs is a valid dataset name.
362 bootfs_name_valid(const char *pool
, char *bootfs
)
364 int len
= strlen(pool
);
366 if (!zfs_name_valid(bootfs
, ZFS_TYPE_FILESYSTEM
|ZFS_TYPE_SNAPSHOT
))
369 if (strncmp(pool
, bootfs
, len
) == 0 &&
370 (bootfs
[len
] == '/' || bootfs
[len
] == '\0'))
377 zpool_is_bootable(zpool_handle_t
*zhp
)
379 char bootfs
[ZPOOL_MAXNAMELEN
];
381 return (zpool_get_prop(zhp
, ZPOOL_PROP_BOOTFS
, bootfs
,
382 sizeof (bootfs
), NULL
, B_FALSE
) == 0 && strncmp(bootfs
, "-",
383 sizeof (bootfs
)) != 0);
388 * Given an nvlist of zpool properties to be set, validate that they are
389 * correct, and parse any numeric properties (index, boolean, etc) if they are
390 * specified as strings.
393 zpool_valid_proplist(libzfs_handle_t
*hdl
, const char *poolname
,
394 nvlist_t
*props
, uint64_t version
, prop_flags_t flags
, char *errbuf
)
402 struct stat64 statbuf
;
405 if (nvlist_alloc(&retprops
, NV_UNIQUE_NAME
, 0) != 0) {
406 (void) no_memory(hdl
);
411 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
412 const char *propname
= nvpair_name(elem
);
414 prop
= zpool_name_to_prop(propname
);
415 if (prop
== ZPROP_INVAL
&& zpool_prop_feature(propname
)) {
417 char *fname
= strchr(propname
, '@') + 1;
419 err
= zfeature_lookup_name(fname
, NULL
);
421 ASSERT3U(err
, ==, ENOENT
);
422 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
423 "invalid feature '%s'"), fname
);
424 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
428 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
429 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
430 "'%s' must be a string"), propname
);
431 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
435 (void) nvpair_value_string(elem
, &strval
);
436 if (strcmp(strval
, ZFS_FEATURE_ENABLED
) != 0) {
437 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
438 "property '%s' can only be set to "
439 "'enabled'"), propname
);
440 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
444 if (nvlist_add_uint64(retprops
, propname
, 0) != 0) {
445 (void) no_memory(hdl
);
452 * Make sure this property is valid and applies to this type.
454 if (prop
== ZPROP_INVAL
) {
455 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
456 "invalid property '%s'"), propname
);
457 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
461 if (zpool_prop_readonly(prop
)) {
462 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
463 "is readonly"), propname
);
464 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
468 if (zprop_parse_value(hdl
, elem
, prop
, ZFS_TYPE_POOL
, retprops
,
469 &strval
, &intval
, errbuf
) != 0)
473 * Perform additional checking for specific properties.
476 case ZPOOL_PROP_VERSION
:
477 if (intval
< version
||
478 !SPA_VERSION_IS_SUPPORTED(intval
)) {
479 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
480 "property '%s' number %d is invalid."),
482 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
487 case ZPOOL_PROP_BOOTFS
:
488 if (flags
.create
|| flags
.import
) {
489 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
490 "property '%s' cannot be set at creation "
491 "or import time"), propname
);
492 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
496 if (version
< SPA_VERSION_BOOTFS
) {
497 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
498 "pool must be upgraded to support "
499 "'%s' property"), propname
);
500 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
505 * bootfs property value has to be a dataset name and
506 * the dataset has to be in the same pool as it sets to.
508 if (strval
[0] != '\0' && !bootfs_name_valid(poolname
,
510 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
511 "is an invalid name"), strval
);
512 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
516 if ((zhp
= zpool_open_canfail(hdl
, poolname
)) == NULL
) {
517 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
518 "could not open pool '%s'"), poolname
);
519 (void) zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
);
525 case ZPOOL_PROP_ALTROOT
:
526 if (!flags
.create
&& !flags
.import
) {
527 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
528 "property '%s' can only be set during pool "
529 "creation or import"), propname
);
530 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
534 if (strval
[0] != '/') {
535 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
536 "bad alternate root '%s'"), strval
);
537 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
542 case ZPOOL_PROP_CACHEFILE
:
543 if (strval
[0] == '\0')
546 if (strcmp(strval
, "none") == 0)
549 if (strval
[0] != '/') {
550 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
551 "property '%s' must be empty, an "
552 "absolute path, or 'none'"), propname
);
553 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
557 slash
= strrchr(strval
, '/');
559 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
560 strcmp(slash
, "/..") == 0) {
561 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
562 "'%s' is not a valid file"), strval
);
563 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
569 if (strval
[0] != '\0' &&
570 (stat64(strval
, &statbuf
) != 0 ||
571 !S_ISDIR(statbuf
.st_mode
))) {
572 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
573 "'%s' is not a valid directory"),
575 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
582 case ZPOOL_PROP_COMMENT
:
583 for (check
= strval
; *check
!= '\0'; check
++) {
584 if (!isprint(*check
)) {
586 dgettext(TEXT_DOMAIN
,
587 "comment may only have printable "
589 (void) zfs_error(hdl
, EZFS_BADPROP
,
594 if (strlen(strval
) > ZPROP_MAX_COMMENT
) {
595 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
596 "comment must not exceed %d characters"),
598 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
602 case ZPOOL_PROP_READONLY
:
604 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
605 "property '%s' can only be set at "
606 "import time"), propname
);
607 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
616 nvlist_free(retprops
);
621 * Set zpool property : propname=propval.
624 zpool_set_prop(zpool_handle_t
*zhp
, const char *propname
, const char *propval
)
626 zfs_cmd_t zc
= { 0 };
629 nvlist_t
*nvl
= NULL
;
632 prop_flags_t flags
= { 0 };
634 (void) snprintf(errbuf
, sizeof (errbuf
),
635 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
638 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
639 return (no_memory(zhp
->zpool_hdl
));
641 if (nvlist_add_string(nvl
, propname
, propval
) != 0) {
643 return (no_memory(zhp
->zpool_hdl
));
646 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
647 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
648 zhp
->zpool_name
, nvl
, version
, flags
, errbuf
)) == NULL
) {
657 * Execute the corresponding ioctl() to set this property.
659 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
661 if (zcmd_write_src_nvlist(zhp
->zpool_hdl
, &zc
, nvl
) != 0) {
666 ret
= zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_SET_PROPS
, &zc
);
668 zcmd_free_nvlists(&zc
);
672 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
674 (void) zpool_props_refresh(zhp
);
680 zpool_expand_proplist(zpool_handle_t
*zhp
, zprop_list_t
**plp
)
682 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
684 char buf
[ZFS_MAXPROPLEN
];
685 nvlist_t
*features
= NULL
;
687 boolean_t firstexpand
= (NULL
== *plp
);
689 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_POOL
) != 0)
693 while (*last
!= NULL
)
694 last
= &(*last
)->pl_next
;
697 features
= zpool_get_features(zhp
);
699 if ((*plp
)->pl_all
&& firstexpand
) {
700 for (int i
= 0; i
< SPA_FEATURES
; i
++) {
701 zprop_list_t
*entry
= zfs_alloc(hdl
,
702 sizeof (zprop_list_t
));
703 entry
->pl_prop
= ZPROP_INVAL
;
704 entry
->pl_user_prop
= zfs_asprintf(hdl
, "feature@%s",
705 spa_feature_table
[i
].fi_uname
);
706 entry
->pl_width
= strlen(entry
->pl_user_prop
);
707 entry
->pl_all
= B_TRUE
;
710 last
= &entry
->pl_next
;
714 /* add any unsupported features */
715 for (nvpair_t
*nvp
= nvlist_next_nvpair(features
, NULL
);
716 nvp
!= NULL
; nvp
= nvlist_next_nvpair(features
, nvp
)) {
721 if (zfeature_is_supported(nvpair_name(nvp
)))
724 propname
= zfs_asprintf(hdl
, "unsupported@%s",
728 * Before adding the property to the list make sure that no
729 * other pool already added the same property.
733 while (entry
!= NULL
) {
734 if (entry
->pl_user_prop
!= NULL
&&
735 strcmp(propname
, entry
->pl_user_prop
) == 0) {
739 entry
= entry
->pl_next
;
746 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
747 entry
->pl_prop
= ZPROP_INVAL
;
748 entry
->pl_user_prop
= propname
;
749 entry
->pl_width
= strlen(entry
->pl_user_prop
);
750 entry
->pl_all
= B_TRUE
;
753 last
= &entry
->pl_next
;
756 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
761 if (entry
->pl_prop
!= ZPROP_INVAL
&&
762 zpool_get_prop(zhp
, entry
->pl_prop
, buf
, sizeof (buf
),
763 NULL
, B_FALSE
) == 0) {
764 if (strlen(buf
) > entry
->pl_width
)
765 entry
->pl_width
= strlen(buf
);
773 * Get the state for the given feature on the given ZFS pool.
776 zpool_prop_get_feature(zpool_handle_t
*zhp
, const char *propname
, char *buf
,
780 boolean_t found
= B_FALSE
;
781 nvlist_t
*features
= zpool_get_features(zhp
);
783 const char *feature
= strchr(propname
, '@') + 1;
785 supported
= zpool_prop_feature(propname
);
786 ASSERT(supported
|| zfs_prop_unsupported(propname
));
789 * Convert from feature name to feature guid. This conversion is
790 * unecessary for unsupported@... properties because they already
797 ret
= zfeature_lookup_name(feature
, &fid
);
799 (void) strlcpy(buf
, "-", len
);
802 feature
= spa_feature_table
[fid
].fi_guid
;
805 if (nvlist_lookup_uint64(features
, feature
, &refcount
) == 0)
810 (void) strlcpy(buf
, ZFS_FEATURE_DISABLED
, len
);
813 (void) strlcpy(buf
, ZFS_FEATURE_ENABLED
, len
);
815 (void) strlcpy(buf
, ZFS_FEATURE_ACTIVE
, len
);
820 (void) strcpy(buf
, ZFS_UNSUPPORTED_INACTIVE
);
822 (void) strcpy(buf
, ZFS_UNSUPPORTED_READONLY
);
825 (void) strlcpy(buf
, "-", len
);
834 * Don't start the slice at the default block of 34; many storage
835 * devices will use a stripe width of 128k, so start there instead.
837 #define NEW_START_BLOCK 256
840 * Validate the given pool name, optionally putting an extended error message in
844 zpool_name_valid(libzfs_handle_t
*hdl
, boolean_t isopen
, const char *pool
)
850 ret
= pool_namecheck(pool
, &why
, &what
);
853 * The rules for reserved pool names were extended at a later point.
854 * But we need to support users with existing pools that may now be
855 * invalid. So we only check for this expanded set of names during a
856 * create (or import), and only in userland.
858 if (ret
== 0 && !isopen
&&
859 (strncmp(pool
, "mirror", 6) == 0 ||
860 strncmp(pool
, "raidz", 5) == 0 ||
861 strncmp(pool
, "spare", 5) == 0 ||
862 strcmp(pool
, "log") == 0)) {
865 dgettext(TEXT_DOMAIN
, "name is reserved"));
873 case NAME_ERR_TOOLONG
:
875 dgettext(TEXT_DOMAIN
, "name is too long"));
878 case NAME_ERR_INVALCHAR
:
880 dgettext(TEXT_DOMAIN
, "invalid character "
881 "'%c' in pool name"), what
);
884 case NAME_ERR_NOLETTER
:
885 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
886 "name must begin with a letter"));
889 case NAME_ERR_RESERVED
:
890 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
891 "name is reserved"));
894 case NAME_ERR_DISKLIKE
:
895 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
896 "pool name is reserved"));
899 case NAME_ERR_LEADING_SLASH
:
900 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
901 "leading slash in name"));
904 case NAME_ERR_EMPTY_COMPONENT
:
905 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
906 "empty component in name"));
909 case NAME_ERR_TRAILING_SLASH
:
910 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
911 "trailing slash in name"));
914 case NAME_ERR_MULTIPLE_AT
:
915 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
916 "multiple '@' delimiters in name"));
928 * Open a handle to the given pool, even if the pool is currently in the FAULTED
932 zpool_open_canfail(libzfs_handle_t
*hdl
, const char *pool
)
938 * Make sure the pool name is valid.
940 if (!zpool_name_valid(hdl
, B_TRUE
, pool
)) {
941 (void) zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
942 dgettext(TEXT_DOMAIN
, "cannot open '%s'"),
947 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
950 zhp
->zpool_hdl
= hdl
;
951 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
953 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
959 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "no such pool"));
960 (void) zfs_error_fmt(hdl
, EZFS_NOENT
,
961 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), pool
);
970 * Like the above, but silent on error. Used when iterating over pools (because
971 * the configuration cache may be out of date).
974 zpool_open_silent(libzfs_handle_t
*hdl
, const char *pool
, zpool_handle_t
**ret
)
979 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
982 zhp
->zpool_hdl
= hdl
;
983 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
985 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1001 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1005 zpool_open(libzfs_handle_t
*hdl
, const char *pool
)
1007 zpool_handle_t
*zhp
;
1009 if ((zhp
= zpool_open_canfail(hdl
, pool
)) == NULL
)
1012 if (zhp
->zpool_state
== POOL_STATE_UNAVAIL
) {
1013 (void) zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
1014 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), zhp
->zpool_name
);
1023 * Close the handle. Simply frees the memory associated with the handle.
1026 zpool_close(zpool_handle_t
*zhp
)
1028 if (zhp
->zpool_config
)
1029 nvlist_free(zhp
->zpool_config
);
1030 if (zhp
->zpool_old_config
)
1031 nvlist_free(zhp
->zpool_old_config
);
1032 if (zhp
->zpool_props
)
1033 nvlist_free(zhp
->zpool_props
);
1038 * Return the name of the pool.
1041 zpool_get_name(zpool_handle_t
*zhp
)
1043 return (zhp
->zpool_name
);
1048 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1051 zpool_get_state(zpool_handle_t
*zhp
)
1053 return (zhp
->zpool_state
);
1057 * Create the named pool, using the provided vdev list. It is assumed
1058 * that the consumer has already validated the contents of the nvlist, so we
1059 * don't have to worry about error semantics.
1062 zpool_create(libzfs_handle_t
*hdl
, const char *pool
, nvlist_t
*nvroot
,
1063 nvlist_t
*props
, nvlist_t
*fsprops
)
1065 zfs_cmd_t zc
= { 0 };
1066 nvlist_t
*zc_fsprops
= NULL
;
1067 nvlist_t
*zc_props
= NULL
;
1071 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1072 "cannot create '%s'"), pool
);
1074 if (!zpool_name_valid(hdl
, B_FALSE
, pool
))
1075 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
1077 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1081 prop_flags_t flags
= { .create
= B_TRUE
, .import
= B_FALSE
};
1083 if ((zc_props
= zpool_valid_proplist(hdl
, pool
, props
,
1084 SPA_VERSION_1
, flags
, msg
)) == NULL
) {
1093 zoned
= ((nvlist_lookup_string(fsprops
,
1094 zfs_prop_to_name(ZFS_PROP_ZONED
), &zonestr
) == 0) &&
1095 strcmp(zonestr
, "on") == 0);
1097 if ((zc_fsprops
= zfs_valid_proplist(hdl
, ZFS_TYPE_FILESYSTEM
,
1098 fsprops
, zoned
, NULL
, NULL
, msg
)) == NULL
) {
1102 (nvlist_alloc(&zc_props
, NV_UNIQUE_NAME
, 0) != 0)) {
1105 if (nvlist_add_nvlist(zc_props
,
1106 ZPOOL_ROOTFS_PROPS
, zc_fsprops
) != 0) {
1111 if (zc_props
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
1114 (void) strlcpy(zc
.zc_name
, pool
, sizeof (zc
.zc_name
));
1116 if ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_CREATE
, &zc
)) != 0) {
1118 zcmd_free_nvlists(&zc
);
1119 nvlist_free(zc_props
);
1120 nvlist_free(zc_fsprops
);
1125 * This can happen if the user has specified the same
1126 * device multiple times. We can't reliably detect this
1127 * until we try to add it and see we already have a
1130 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1131 "one or more vdevs refer to the same device"));
1132 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1136 * This happens if the record size is smaller or larger
1137 * than the allowed size range, or not a power of 2.
1139 * NOTE: although zfs_valid_proplist is called earlier,
1140 * this case may have slipped through since the
1141 * pool does not exist yet and it is therefore
1142 * impossible to read properties e.g. max blocksize
1145 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1146 "record size invalid"));
1147 return (zfs_error(hdl
, EZFS_BADPROP
, msg
));
1151 * This occurs when one of the devices is below
1152 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1153 * device was the problem device since there's no
1154 * reliable way to determine device size from userland.
1159 zfs_nicenum(SPA_MINDEVSIZE
, buf
, sizeof (buf
));
1161 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1162 "one or more devices is less than the "
1163 "minimum size (%s)"), buf
);
1165 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1168 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1169 "one or more devices is out of space"));
1170 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1173 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1174 "cache device must be a disk or disk slice"));
1175 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1178 return (zpool_standard_error(hdl
, errno
, msg
));
1183 zcmd_free_nvlists(&zc
);
1184 nvlist_free(zc_props
);
1185 nvlist_free(zc_fsprops
);
1190 * Destroy the given pool. It is up to the caller to ensure that there are no
1191 * datasets left in the pool.
1194 zpool_destroy(zpool_handle_t
*zhp
, const char *log_str
)
1196 zfs_cmd_t zc
= { 0 };
1197 zfs_handle_t
*zfp
= NULL
;
1198 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1201 if (zhp
->zpool_state
== POOL_STATE_ACTIVE
&&
1202 (zfp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
1205 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1206 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1208 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_DESTROY
, &zc
) != 0) {
1209 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1210 "cannot destroy '%s'"), zhp
->zpool_name
);
1212 if (errno
== EROFS
) {
1213 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1214 "one or more devices is read only"));
1215 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1217 (void) zpool_standard_error(hdl
, errno
, msg
);
1226 remove_mountpoint(zfp
);
1234 * Add the given vdevs to the pool. The caller must have already performed the
1235 * necessary verification to ensure that the vdev specification is well-formed.
1238 zpool_add(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
1240 zfs_cmd_t zc
= { 0 };
1242 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1244 nvlist_t
**spares
, **l2cache
;
1245 uint_t nspares
, nl2cache
;
1247 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1248 "cannot add to '%s'"), zhp
->zpool_name
);
1250 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1251 SPA_VERSION_SPARES
&&
1252 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1253 &spares
, &nspares
) == 0) {
1254 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1255 "upgraded to add hot spares"));
1256 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1259 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1260 SPA_VERSION_L2CACHE
&&
1261 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1262 &l2cache
, &nl2cache
) == 0) {
1263 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1264 "upgraded to add cache devices"));
1265 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1268 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1270 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1272 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_ADD
, &zc
) != 0) {
1276 * This can happen if the user has specified the same
1277 * device multiple times. We can't reliably detect this
1278 * until we try to add it and see we already have a
1281 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1282 "one or more vdevs refer to the same device"));
1283 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1288 * This occurrs when one of the devices is below
1289 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1290 * device was the problem device since there's no
1291 * reliable way to determine device size from userland.
1296 zfs_nicenum(SPA_MINDEVSIZE
, buf
, sizeof (buf
));
1298 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1299 "device is less than the minimum "
1302 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1306 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1307 "pool must be upgraded to add these vdevs"));
1308 (void) zfs_error(hdl
, EZFS_BADVERSION
, msg
);
1312 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1313 "root pool can not have multiple vdevs"
1314 " or separate logs"));
1315 (void) zfs_error(hdl
, EZFS_POOL_NOTSUP
, msg
);
1319 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1320 "cache device must be a disk or disk slice"));
1321 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1325 (void) zpool_standard_error(hdl
, errno
, msg
);
1333 zcmd_free_nvlists(&zc
);
1339 * Exports the pool from the system. The caller must ensure that there are no
1340 * mounted datasets in the pool.
1343 zpool_export_common(zpool_handle_t
*zhp
, boolean_t force
, boolean_t hardforce
,
1344 const char *log_str
)
1346 zfs_cmd_t zc
= { 0 };
1349 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1350 "cannot export '%s'"), zhp
->zpool_name
);
1352 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1353 zc
.zc_cookie
= force
;
1354 zc
.zc_guid
= hardforce
;
1355 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1357 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_EXPORT
, &zc
) != 0) {
1360 zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
1361 "use '-f' to override the following errors:\n"
1362 "'%s' has an active shared spare which could be"
1363 " used by other pools once '%s' is exported."),
1364 zhp
->zpool_name
, zhp
->zpool_name
);
1365 return (zfs_error(zhp
->zpool_hdl
, EZFS_ACTIVE_SPARE
,
1368 return (zpool_standard_error_fmt(zhp
->zpool_hdl
, errno
,
1377 zpool_export(zpool_handle_t
*zhp
, boolean_t force
, const char *log_str
)
1379 return (zpool_export_common(zhp
, force
, B_FALSE
, log_str
));
1383 zpool_export_force(zpool_handle_t
*zhp
, const char *log_str
)
1385 return (zpool_export_common(zhp
, B_TRUE
, B_TRUE
, log_str
));
1389 zpool_rewind_exclaim(libzfs_handle_t
*hdl
, const char *name
, boolean_t dryrun
,
1392 nvlist_t
*nv
= NULL
;
1398 if (!hdl
->libzfs_printerr
|| config
== NULL
)
1401 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1402 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0) {
1406 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1408 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1410 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1411 strftime(timestr
, 128, 0, &t
) != 0) {
1413 (void) printf(dgettext(TEXT_DOMAIN
,
1414 "Would be able to return %s "
1415 "to its state as of %s.\n"),
1418 (void) printf(dgettext(TEXT_DOMAIN
,
1419 "Pool %s returned to its state as of %s.\n"),
1423 (void) printf(dgettext(TEXT_DOMAIN
,
1424 "%s approximately %lld "),
1425 dryrun
? "Would discard" : "Discarded",
1427 (void) printf(dgettext(TEXT_DOMAIN
,
1428 "minutes of transactions.\n"));
1429 } else if (loss
> 0) {
1430 (void) printf(dgettext(TEXT_DOMAIN
,
1431 "%s approximately %lld "),
1432 dryrun
? "Would discard" : "Discarded", loss
);
1433 (void) printf(dgettext(TEXT_DOMAIN
,
1434 "seconds of transactions.\n"));
1440 zpool_explain_recover(libzfs_handle_t
*hdl
, const char *name
, int reason
,
1443 nvlist_t
*nv
= NULL
;
1445 uint64_t edata
= UINT64_MAX
;
1450 if (!hdl
->libzfs_printerr
)
1454 (void) printf(dgettext(TEXT_DOMAIN
, "action: "));
1456 (void) printf(dgettext(TEXT_DOMAIN
, "\t"));
1458 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1459 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1460 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0 ||
1461 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1464 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1465 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_DATA_ERRORS
,
1468 (void) printf(dgettext(TEXT_DOMAIN
,
1469 "Recovery is possible, but will result in some data loss.\n"));
1471 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1472 strftime(timestr
, 128, 0, &t
) != 0) {
1473 (void) printf(dgettext(TEXT_DOMAIN
,
1474 "\tReturning the pool to its state as of %s\n"
1475 "\tshould correct the problem. "),
1478 (void) printf(dgettext(TEXT_DOMAIN
,
1479 "\tReverting the pool to an earlier state "
1480 "should correct the problem.\n\t"));
1484 (void) printf(dgettext(TEXT_DOMAIN
,
1485 "Approximately %lld minutes of data\n"
1486 "\tmust be discarded, irreversibly. "), (loss
+ 30) / 60);
1487 } else if (loss
> 0) {
1488 (void) printf(dgettext(TEXT_DOMAIN
,
1489 "Approximately %lld seconds of data\n"
1490 "\tmust be discarded, irreversibly. "), loss
);
1492 if (edata
!= 0 && edata
!= UINT64_MAX
) {
1494 (void) printf(dgettext(TEXT_DOMAIN
,
1495 "After rewind, at least\n"
1496 "\tone persistent user-data error will remain. "));
1498 (void) printf(dgettext(TEXT_DOMAIN
,
1499 "After rewind, several\n"
1500 "\tpersistent user-data errors will remain. "));
1503 (void) printf(dgettext(TEXT_DOMAIN
,
1504 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1505 reason
>= 0 ? "clear" : "import", name
);
1507 (void) printf(dgettext(TEXT_DOMAIN
,
1508 "A scrub of the pool\n"
1509 "\tis strongly recommended after recovery.\n"));
1513 (void) printf(dgettext(TEXT_DOMAIN
,
1514 "Destroy and re-create the pool from\n\ta backup source.\n"));
1518 * zpool_import() is a contracted interface. Should be kept the same
1521 * Applications should use zpool_import_props() to import a pool with
1522 * new properties value to be set.
1525 zpool_import(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1528 nvlist_t
*props
= NULL
;
1531 if (altroot
!= NULL
) {
1532 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
1533 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1534 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1538 if (nvlist_add_string(props
,
1539 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), altroot
) != 0 ||
1540 nvlist_add_string(props
,
1541 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
), "none") != 0) {
1543 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1544 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1549 ret
= zpool_import_props(hdl
, config
, newname
, props
,
1557 print_vdev_tree(libzfs_handle_t
*hdl
, const char *name
, nvlist_t
*nv
,
1563 uint64_t is_log
= 0;
1565 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
,
1569 (void) printf("\t%*s%s%s\n", indent
, "", name
,
1570 is_log
? " [log]" : "");
1572 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1573 &child
, &children
) != 0)
1576 for (c
= 0; c
< children
; c
++) {
1577 vname
= zpool_vdev_name(hdl
, NULL
, child
[c
], B_TRUE
);
1578 print_vdev_tree(hdl
, vname
, child
[c
], indent
+ 2);
1584 zpool_print_unsup_feat(nvlist_t
*config
)
1586 nvlist_t
*nvinfo
, *unsup_feat
;
1588 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) ==
1590 verify(nvlist_lookup_nvlist(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
,
1593 for (nvpair_t
*nvp
= nvlist_next_nvpair(unsup_feat
, NULL
); nvp
!= NULL
;
1594 nvp
= nvlist_next_nvpair(unsup_feat
, nvp
)) {
1597 verify(nvpair_type(nvp
) == DATA_TYPE_STRING
);
1598 verify(nvpair_value_string(nvp
, &desc
) == 0);
1600 if (strlen(desc
) > 0)
1601 (void) printf("\t%s (%s)\n", nvpair_name(nvp
), desc
);
1603 (void) printf("\t%s\n", nvpair_name(nvp
));
1608 * Import the given pool using the known configuration and a list of
1609 * properties to be set. The configuration should have come from
1610 * zpool_find_import(). The 'newname' parameters control whether the pool
1611 * is imported with a different name.
1614 zpool_import_props(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1615 nvlist_t
*props
, int flags
)
1617 zfs_cmd_t zc
= { 0 };
1618 zpool_rewind_policy_t policy
;
1619 nvlist_t
*nv
= NULL
;
1620 nvlist_t
*nvinfo
= NULL
;
1621 nvlist_t
*missing
= NULL
;
1628 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1631 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1632 "cannot import pool '%s'"), origname
);
1634 if (newname
!= NULL
) {
1635 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
1636 return (zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1637 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1639 thename
= (char *)newname
;
1644 if (props
!= NULL
) {
1646 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
1648 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
1651 if ((props
= zpool_valid_proplist(hdl
, origname
,
1652 props
, version
, flags
, errbuf
)) == NULL
)
1654 if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
1661 (void) strlcpy(zc
.zc_name
, thename
, sizeof (zc
.zc_name
));
1663 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1666 if (zcmd_write_conf_nvlist(hdl
, &zc
, config
) != 0) {
1667 zcmd_free_nvlists(&zc
);
1670 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zc
.zc_nvlist_conf_size
* 2) != 0) {
1671 zcmd_free_nvlists(&zc
);
1675 zc
.zc_cookie
= flags
;
1676 while ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_IMPORT
, &zc
)) != 0 &&
1678 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
1679 zcmd_free_nvlists(&zc
);
1686 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nv
);
1688 zcmd_free_nvlists(&zc
);
1690 zpool_get_rewind_policy(config
, &policy
);
1696 * Dry-run failed, but we print out what success
1697 * looks like if we found a best txg
1699 if (policy
.zrp_request
& ZPOOL_TRY_REWIND
) {
1700 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1706 if (newname
== NULL
)
1707 (void) snprintf(desc
, sizeof (desc
),
1708 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1711 (void) snprintf(desc
, sizeof (desc
),
1712 dgettext(TEXT_DOMAIN
, "cannot import '%s' as '%s'"),
1717 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
1718 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1719 nvlist_exists(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
)) {
1720 (void) printf(dgettext(TEXT_DOMAIN
, "This "
1721 "pool uses the following feature(s) not "
1722 "supported by this system:\n"));
1723 zpool_print_unsup_feat(nv
);
1724 if (nvlist_exists(nvinfo
,
1725 ZPOOL_CONFIG_CAN_RDONLY
)) {
1726 (void) printf(dgettext(TEXT_DOMAIN
,
1727 "All unsupported features are only "
1728 "required for writing to the pool."
1729 "\nThe pool can be imported using "
1730 "'-o readonly=on'.\n"));
1734 * Unsupported version.
1736 (void) zfs_error(hdl
, EZFS_BADVERSION
, desc
);
1740 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, desc
);
1744 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1745 "one or more devices is read only"));
1746 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
1750 if (nv
&& nvlist_lookup_nvlist(nv
,
1751 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1752 nvlist_lookup_nvlist(nvinfo
,
1753 ZPOOL_CONFIG_MISSING_DEVICES
, &missing
) == 0) {
1754 (void) printf(dgettext(TEXT_DOMAIN
,
1755 "The devices below are missing, use "
1756 "'-m' to import the pool anyway:\n"));
1757 print_vdev_tree(hdl
, NULL
, missing
, 2);
1758 (void) printf("\n");
1760 (void) zpool_standard_error(hdl
, error
, desc
);
1764 (void) zpool_standard_error(hdl
, error
, desc
);
1768 (void) zpool_standard_error(hdl
, error
, desc
);
1769 zpool_explain_recover(hdl
,
1770 newname
? origname
: thename
, -error
, nv
);
1777 zpool_handle_t
*zhp
;
1780 * This should never fail, but play it safe anyway.
1782 if (zpool_open_silent(hdl
, thename
, &zhp
) != 0)
1784 else if (zhp
!= NULL
)
1786 if (policy
.zrp_request
&
1787 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
1788 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1789 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0), nv
);
1802 zpool_scan(zpool_handle_t
*zhp
, pool_scan_func_t func
)
1804 zfs_cmd_t zc
= { 0 };
1806 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1808 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1809 zc
.zc_cookie
= func
;
1811 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_SCAN
, &zc
) == 0 ||
1812 (errno
== ENOENT
&& func
!= POOL_SCAN_NONE
))
1815 if (func
== POOL_SCAN_SCRUB
) {
1816 (void) snprintf(msg
, sizeof (msg
),
1817 dgettext(TEXT_DOMAIN
, "cannot scrub %s"), zc
.zc_name
);
1818 } else if (func
== POOL_SCAN_NONE
) {
1819 (void) snprintf(msg
, sizeof (msg
),
1820 dgettext(TEXT_DOMAIN
, "cannot cancel scrubbing %s"),
1823 assert(!"unexpected result");
1826 if (errno
== EBUSY
) {
1828 pool_scan_stat_t
*ps
= NULL
;
1831 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
1832 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
1833 (void) nvlist_lookup_uint64_array(nvroot
,
1834 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &psc
);
1835 if (ps
&& ps
->pss_func
== POOL_SCAN_SCRUB
)
1836 return (zfs_error(hdl
, EZFS_SCRUBBING
, msg
));
1838 return (zfs_error(hdl
, EZFS_RESILVERING
, msg
));
1839 } else if (errno
== ENOENT
) {
1840 return (zfs_error(hdl
, EZFS_NO_SCRUB
, msg
));
1842 return (zpool_standard_error(hdl
, errno
, msg
));
1847 * This provides a very minimal check whether a given string is likely a
1848 * c#t#d# style string. Users of this are expected to do their own
1849 * verification of the s# part.
1851 #define CTD_CHECK(str) (str && str[0] == 'c' && isdigit(str[1]))
1854 * More elaborate version for ones which may start with "/dev/dsk/"
1858 ctd_check_path(char *str
)
1861 * If it starts with a slash, check the last component.
1863 if (str
&& str
[0] == '/') {
1864 char *tmp
= strrchr(str
, '/');
1867 * If it ends in "/old", check the second-to-last
1868 * component of the string instead.
1870 if (tmp
!= str
&& strcmp(tmp
, "/old") == 0) {
1871 for (tmp
--; *tmp
!= '/'; tmp
--)
1876 return (CTD_CHECK(str
));
1880 * Find a vdev that matches the search criteria specified. We use the
1881 * the nvpair name to determine how we should look for the device.
1882 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1883 * spare; but FALSE if its an INUSE spare.
1886 vdev_to_nvlist_iter(nvlist_t
*nv
, nvlist_t
*search
, boolean_t
*avail_spare
,
1887 boolean_t
*l2cache
, boolean_t
*log
)
1894 nvpair_t
*pair
= nvlist_next_nvpair(search
, NULL
);
1896 /* Nothing to look for */
1897 if (search
== NULL
|| pair
== NULL
)
1900 /* Obtain the key we will use to search */
1901 srchkey
= nvpair_name(pair
);
1903 switch (nvpair_type(pair
)) {
1904 case DATA_TYPE_UINT64
:
1905 if (strcmp(srchkey
, ZPOOL_CONFIG_GUID
) == 0) {
1906 uint64_t srchval
, theguid
;
1908 verify(nvpair_value_uint64(pair
, &srchval
) == 0);
1909 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1911 if (theguid
== srchval
)
1916 case DATA_TYPE_STRING
: {
1917 char *srchval
, *val
;
1919 verify(nvpair_value_string(pair
, &srchval
) == 0);
1920 if (nvlist_lookup_string(nv
, srchkey
, &val
) != 0)
1924 * Search for the requested value. Special cases:
1926 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
1927 * "s0" or "s0/old". The "s0" part is hidden from the user,
1928 * but included in the string, so this matches around it.
1929 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1931 * Otherwise, all other searches are simple string compares.
1933 if (strcmp(srchkey
, ZPOOL_CONFIG_PATH
) == 0 &&
1934 ctd_check_path(val
)) {
1935 uint64_t wholedisk
= 0;
1937 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
1940 int slen
= strlen(srchval
);
1941 int vlen
= strlen(val
);
1943 if (slen
!= vlen
- 2)
1947 * make_leaf_vdev() should only set
1948 * wholedisk for ZPOOL_CONFIG_PATHs which
1949 * will include "/dev/dsk/", giving plenty of
1950 * room for the indices used next.
1955 * strings identical except trailing "s0"
1957 if (strcmp(&val
[vlen
- 2], "s0") == 0 &&
1958 strncmp(srchval
, val
, slen
) == 0)
1962 * strings identical except trailing "s0/old"
1964 if (strcmp(&val
[vlen
- 6], "s0/old") == 0 &&
1965 strcmp(&srchval
[slen
- 4], "/old") == 0 &&
1966 strncmp(srchval
, val
, slen
- 4) == 0)
1971 } else if (strcmp(srchkey
, ZPOOL_CONFIG_TYPE
) == 0 && val
) {
1972 char *type
, *idx
, *end
, *p
;
1973 uint64_t id
, vdev_id
;
1976 * Determine our vdev type, keeping in mind
1977 * that the srchval is composed of a type and
1978 * vdev id pair (i.e. mirror-4).
1980 if ((type
= strdup(srchval
)) == NULL
)
1983 if ((p
= strrchr(type
, '-')) == NULL
) {
1991 * If the types don't match then keep looking.
1993 if (strncmp(val
, type
, strlen(val
)) != 0) {
1998 verify(strncmp(type
, VDEV_TYPE_RAIDZ
,
1999 strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2000 strncmp(type
, VDEV_TYPE_MIRROR
,
2001 strlen(VDEV_TYPE_MIRROR
)) == 0);
2002 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
2006 vdev_id
= strtoull(idx
, &end
, 10);
2013 * Now verify that we have the correct vdev id.
2022 if (strcmp(srchval
, val
) == 0)
2031 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2032 &child
, &children
) != 0)
2035 for (c
= 0; c
< children
; c
++) {
2036 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2037 avail_spare
, l2cache
, NULL
)) != NULL
) {
2039 * The 'is_log' value is only set for the toplevel
2040 * vdev, not the leaf vdevs. So we always lookup the
2041 * log device from the root of the vdev tree (where
2042 * 'log' is non-NULL).
2045 nvlist_lookup_uint64(child
[c
],
2046 ZPOOL_CONFIG_IS_LOG
, &is_log
) == 0 &&
2054 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2055 &child
, &children
) == 0) {
2056 for (c
= 0; c
< children
; c
++) {
2057 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2058 avail_spare
, l2cache
, NULL
)) != NULL
) {
2059 *avail_spare
= B_TRUE
;
2065 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2066 &child
, &children
) == 0) {
2067 for (c
= 0; c
< children
; c
++) {
2068 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2069 avail_spare
, l2cache
, NULL
)) != NULL
) {
2080 * Given a physical path (minus the "/devices" prefix), find the
2084 zpool_find_vdev_by_physpath(zpool_handle_t
*zhp
, const char *ppath
,
2085 boolean_t
*avail_spare
, boolean_t
*l2cache
, boolean_t
*log
)
2087 nvlist_t
*search
, *nvroot
, *ret
;
2089 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2090 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PHYS_PATH
, ppath
) == 0);
2092 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2095 *avail_spare
= B_FALSE
;
2099 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2100 nvlist_free(search
);
2106 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2109 zpool_vdev_is_interior(const char *name
)
2111 if (strncmp(name
, VDEV_TYPE_RAIDZ
, strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2112 strncmp(name
, VDEV_TYPE_MIRROR
, strlen(VDEV_TYPE_MIRROR
)) == 0)
2118 zpool_find_vdev(zpool_handle_t
*zhp
, const char *path
, boolean_t
*avail_spare
,
2119 boolean_t
*l2cache
, boolean_t
*log
)
2121 char buf
[MAXPATHLEN
];
2123 nvlist_t
*nvroot
, *search
, *ret
;
2126 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2128 guid
= strtoull(path
, &end
, 10);
2129 if (guid
!= 0 && *end
== '\0') {
2130 verify(nvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
) == 0);
2131 } else if (zpool_vdev_is_interior(path
)) {
2132 verify(nvlist_add_string(search
, ZPOOL_CONFIG_TYPE
, path
) == 0);
2133 } else if (path
[0] != '/') {
2134 (void) snprintf(buf
, sizeof (buf
), "%s%s", "/dev/dsk/", path
);
2135 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PATH
, buf
) == 0);
2137 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PATH
, path
) == 0);
2140 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2143 *avail_spare
= B_FALSE
;
2147 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2148 nvlist_free(search
);
2154 vdev_online(nvlist_t
*nv
)
2158 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
, &ival
) == 0 ||
2159 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
, &ival
) == 0 ||
2160 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
, &ival
) == 0)
2167 * Helper function for zpool_get_physpaths().
2170 vdev_get_one_physpath(nvlist_t
*config
, char *physpath
, size_t physpath_size
,
2171 size_t *bytes_written
)
2173 size_t bytes_left
, pos
, rsz
;
2177 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PHYS_PATH
,
2179 return (EZFS_NODEVICE
);
2181 pos
= *bytes_written
;
2182 bytes_left
= physpath_size
- pos
;
2183 format
= (pos
== 0) ? "%s" : " %s";
2185 rsz
= snprintf(physpath
+ pos
, bytes_left
, format
, tmppath
);
2186 *bytes_written
+= rsz
;
2188 if (rsz
>= bytes_left
) {
2189 /* if physpath was not copied properly, clear it */
2190 if (bytes_left
!= 0) {
2193 return (EZFS_NOSPC
);
2199 vdev_get_physpaths(nvlist_t
*nv
, char *physpath
, size_t phypath_size
,
2200 size_t *rsz
, boolean_t is_spare
)
2205 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
2206 return (EZFS_INVALCONFIG
);
2208 if (strcmp(type
, VDEV_TYPE_DISK
) == 0) {
2210 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2211 * For a spare vdev, we only want to boot from the active
2216 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
2219 return (EZFS_INVALCONFIG
);
2222 if (vdev_online(nv
)) {
2223 if ((ret
= vdev_get_one_physpath(nv
, physpath
,
2224 phypath_size
, rsz
)) != 0)
2227 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) == 0 ||
2228 strcmp(type
, VDEV_TYPE_REPLACING
) == 0 ||
2229 (is_spare
= (strcmp(type
, VDEV_TYPE_SPARE
) == 0))) {
2234 if (nvlist_lookup_nvlist_array(nv
,
2235 ZPOOL_CONFIG_CHILDREN
, &child
, &count
) != 0)
2236 return (EZFS_INVALCONFIG
);
2238 for (i
= 0; i
< count
; i
++) {
2239 ret
= vdev_get_physpaths(child
[i
], physpath
,
2240 phypath_size
, rsz
, is_spare
);
2241 if (ret
== EZFS_NOSPC
)
2246 return (EZFS_POOL_INVALARG
);
2250 * Get phys_path for a root pool config.
2251 * Return 0 on success; non-zero on failure.
2254 zpool_get_config_physpath(nvlist_t
*config
, char *physpath
, size_t phypath_size
)
2257 nvlist_t
*vdev_root
;
2264 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2266 return (EZFS_INVALCONFIG
);
2268 if (nvlist_lookup_string(vdev_root
, ZPOOL_CONFIG_TYPE
, &type
) != 0 ||
2269 nvlist_lookup_nvlist_array(vdev_root
, ZPOOL_CONFIG_CHILDREN
,
2270 &child
, &count
) != 0)
2271 return (EZFS_INVALCONFIG
);
2274 * root pool can only have a single top-level vdev.
2276 if (strcmp(type
, VDEV_TYPE_ROOT
) != 0 || count
!= 1)
2277 return (EZFS_POOL_INVALARG
);
2279 (void) vdev_get_physpaths(child
[0], physpath
, phypath_size
, &rsz
,
2282 /* No online devices */
2284 return (EZFS_NODEVICE
);
2290 * Get phys_path for a root pool
2291 * Return 0 on success; non-zero on failure.
2294 zpool_get_physpath(zpool_handle_t
*zhp
, char *physpath
, size_t phypath_size
)
2296 return (zpool_get_config_physpath(zhp
->zpool_config
, physpath
,
2301 * If the device has being dynamically expanded then we need to relabel
2302 * the disk to use the new unallocated space.
2305 zpool_relabel_disk(libzfs_handle_t
*hdl
, const char *name
)
2307 char path
[MAXPATHLEN
];
2310 int (*_efi_use_whole_disk
)(int);
2312 if ((_efi_use_whole_disk
= (int (*)(int))dlsym(RTLD_DEFAULT
,
2313 "efi_use_whole_disk")) == NULL
)
2316 (void) snprintf(path
, sizeof (path
), "%s/%s", RDISK_ROOT
, name
);
2318 if ((fd
= open(path
, O_RDWR
| O_NDELAY
)) < 0) {
2319 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2320 "relabel '%s': unable to open device"), name
);
2321 return (zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
));
2325 * It's possible that we might encounter an error if the device
2326 * does not have any unallocated space left. If so, we simply
2327 * ignore that error and continue on.
2329 error
= _efi_use_whole_disk(fd
);
2331 if (error
&& error
!= VT_ENOSPC
) {
2332 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2333 "relabel '%s': unable to read disk capacity"), name
);
2334 return (zfs_error(hdl
, EZFS_NOCAP
, errbuf
));
2340 * Bring the specified vdev online. The 'flags' parameter is a set of the
2341 * ZFS_ONLINE_* flags.
2344 zpool_vdev_online(zpool_handle_t
*zhp
, const char *path
, int flags
,
2345 vdev_state_t
*newstate
)
2347 zfs_cmd_t zc
= { 0 };
2350 boolean_t avail_spare
, l2cache
, islog
;
2351 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2353 if (flags
& ZFS_ONLINE_EXPAND
) {
2354 (void) snprintf(msg
, sizeof (msg
),
2355 dgettext(TEXT_DOMAIN
, "cannot expand %s"), path
);
2357 (void) snprintf(msg
, sizeof (msg
),
2358 dgettext(TEXT_DOMAIN
, "cannot online %s"), path
);
2361 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2362 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2364 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2366 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2369 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2371 if (flags
& ZFS_ONLINE_EXPAND
||
2372 zpool_get_prop_int(zhp
, ZPOOL_PROP_AUTOEXPAND
, NULL
)) {
2373 char *pathname
= NULL
;
2374 uint64_t wholedisk
= 0;
2376 (void) nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_WHOLE_DISK
,
2378 verify(nvlist_lookup_string(tgt
, ZPOOL_CONFIG_PATH
,
2382 * XXX - L2ARC 1.0 devices can't support expansion.
2385 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2386 "cannot expand cache devices"));
2387 return (zfs_error(hdl
, EZFS_VDEVNOTSUP
, msg
));
2391 pathname
+= strlen(DISK_ROOT
) + 1;
2392 (void) zpool_relabel_disk(hdl
, pathname
);
2396 zc
.zc_cookie
= VDEV_STATE_ONLINE
;
2399 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) != 0) {
2400 if (errno
== EINVAL
) {
2401 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "was split "
2402 "from this pool into a new one. Use '%s' "
2403 "instead"), "zpool detach");
2404 return (zfs_error(hdl
, EZFS_POSTSPLIT_ONLINE
, msg
));
2406 return (zpool_standard_error(hdl
, errno
, msg
));
2409 *newstate
= zc
.zc_cookie
;
2414 * Take the specified vdev offline
2417 zpool_vdev_offline(zpool_handle_t
*zhp
, const char *path
, boolean_t istmp
)
2419 zfs_cmd_t zc
= { 0 };
2422 boolean_t avail_spare
, l2cache
;
2423 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2425 (void) snprintf(msg
, sizeof (msg
),
2426 dgettext(TEXT_DOMAIN
, "cannot offline %s"), path
);
2428 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2429 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2431 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2433 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2436 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2438 zc
.zc_cookie
= VDEV_STATE_OFFLINE
;
2439 zc
.zc_obj
= istmp
? ZFS_OFFLINE_TEMPORARY
: 0;
2441 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2448 * There are no other replicas of this device.
2450 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2454 * The log device has unplayed logs
2456 return (zfs_error(hdl
, EZFS_UNPLAYED_LOGS
, msg
));
2459 return (zpool_standard_error(hdl
, errno
, msg
));
2464 * Mark the given vdev faulted.
2467 zpool_vdev_fault(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2469 zfs_cmd_t zc
= { 0 };
2471 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2473 (void) snprintf(msg
, sizeof (msg
),
2474 dgettext(TEXT_DOMAIN
, "cannot fault %llu"), guid
);
2476 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2478 zc
.zc_cookie
= VDEV_STATE_FAULTED
;
2481 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2488 * There are no other replicas of this device.
2490 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2493 return (zpool_standard_error(hdl
, errno
, msg
));
2499 * Mark the given vdev degraded.
2502 zpool_vdev_degrade(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2504 zfs_cmd_t zc
= { 0 };
2506 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2508 (void) snprintf(msg
, sizeof (msg
),
2509 dgettext(TEXT_DOMAIN
, "cannot degrade %llu"), guid
);
2511 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2513 zc
.zc_cookie
= VDEV_STATE_DEGRADED
;
2516 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2519 return (zpool_standard_error(hdl
, errno
, msg
));
2523 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2527 is_replacing_spare(nvlist_t
*search
, nvlist_t
*tgt
, int which
)
2533 if (nvlist_lookup_nvlist_array(search
, ZPOOL_CONFIG_CHILDREN
, &child
,
2535 verify(nvlist_lookup_string(search
, ZPOOL_CONFIG_TYPE
,
2538 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0 &&
2539 children
== 2 && child
[which
] == tgt
)
2542 for (c
= 0; c
< children
; c
++)
2543 if (is_replacing_spare(child
[c
], tgt
, which
))
2551 * Attach new_disk (fully described by nvroot) to old_disk.
2552 * If 'replacing' is specified, the new disk will replace the old one.
2555 zpool_vdev_attach(zpool_handle_t
*zhp
,
2556 const char *old_disk
, const char *new_disk
, nvlist_t
*nvroot
, int replacing
)
2558 zfs_cmd_t zc
= { 0 };
2562 boolean_t avail_spare
, l2cache
, islog
;
2567 nvlist_t
*config_root
;
2568 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2569 boolean_t rootpool
= zpool_is_bootable(zhp
);
2572 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2573 "cannot replace %s with %s"), old_disk
, new_disk
);
2575 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2576 "cannot attach %s to %s"), new_disk
, old_disk
);
2578 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2579 if ((tgt
= zpool_find_vdev(zhp
, old_disk
, &avail_spare
, &l2cache
,
2581 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2584 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2587 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2589 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2590 zc
.zc_cookie
= replacing
;
2592 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
2593 &child
, &children
) != 0 || children
!= 1) {
2594 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2595 "new device must be a single disk"));
2596 return (zfs_error(hdl
, EZFS_INVALCONFIG
, msg
));
2599 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
2600 ZPOOL_CONFIG_VDEV_TREE
, &config_root
) == 0);
2602 if ((newname
= zpool_vdev_name(NULL
, NULL
, child
[0], B_FALSE
)) == NULL
)
2606 * If the target is a hot spare that has been swapped in, we can only
2607 * replace it with another hot spare.
2610 nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_IS_SPARE
, &val
) == 0 &&
2611 (zpool_find_vdev(zhp
, newname
, &avail_spare
, &l2cache
,
2612 NULL
) == NULL
|| !avail_spare
) &&
2613 is_replacing_spare(config_root
, tgt
, 1)) {
2614 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2615 "can only be replaced by another hot spare"));
2617 return (zfs_error(hdl
, EZFS_BADTARGET
, msg
));
2622 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
2625 ret
= zfs_ioctl(hdl
, ZFS_IOC_VDEV_ATTACH
, &zc
);
2627 zcmd_free_nvlists(&zc
);
2632 * XXX need a better way to prevent user from
2633 * booting up a half-baked vdev.
2635 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Make "
2636 "sure to wait until resilver is done "
2637 "before rebooting.\n"));
2645 * Can't attach to or replace this type of vdev.
2648 uint64_t version
= zpool_get_prop_int(zhp
,
2649 ZPOOL_PROP_VERSION
, NULL
);
2652 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2653 "cannot replace a log with a spare"));
2654 else if (version
>= SPA_VERSION_MULTI_REPLACE
)
2655 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2656 "already in replacing/spare config; wait "
2657 "for completion or use 'zpool detach'"));
2659 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2660 "cannot replace a replacing device"));
2662 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2663 "can only attach to mirrors and top-level "
2666 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2671 * The new device must be a single disk.
2673 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2674 "new device must be a single disk"));
2675 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2679 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "%s is busy"),
2681 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2686 * The new device is too small.
2688 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2689 "device is too small"));
2690 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2695 * The new device has a different alignment requirement.
2697 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2698 "devices have different sector alignment"));
2699 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2704 * The resulting top-level vdev spec won't fit in the label.
2706 (void) zfs_error(hdl
, EZFS_DEVOVERFLOW
, msg
);
2710 (void) zpool_standard_error(hdl
, errno
, msg
);
2717 * Detach the specified device.
2720 zpool_vdev_detach(zpool_handle_t
*zhp
, const char *path
)
2722 zfs_cmd_t zc
= { 0 };
2725 boolean_t avail_spare
, l2cache
;
2726 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2728 (void) snprintf(msg
, sizeof (msg
),
2729 dgettext(TEXT_DOMAIN
, "cannot detach %s"), path
);
2731 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2732 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2734 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2737 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2740 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2742 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2744 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_DETACH
, &zc
) == 0)
2751 * Can't detach from this type of vdev.
2753 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "only "
2754 "applicable to mirror and replacing vdevs"));
2755 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2760 * There are no other replicas of this device.
2762 (void) zfs_error(hdl
, EZFS_NOREPLICAS
, msg
);
2766 (void) zpool_standard_error(hdl
, errno
, msg
);
2773 * Find a mirror vdev in the source nvlist.
2775 * The mchild array contains a list of disks in one of the top-level mirrors
2776 * of the source pool. The schild array contains a list of disks that the
2777 * user specified on the command line. We loop over the mchild array to
2778 * see if any entry in the schild array matches.
2780 * If a disk in the mchild array is found in the schild array, we return
2781 * the index of that entry. Otherwise we return -1.
2784 find_vdev_entry(zpool_handle_t
*zhp
, nvlist_t
**mchild
, uint_t mchildren
,
2785 nvlist_t
**schild
, uint_t schildren
)
2789 for (mc
= 0; mc
< mchildren
; mc
++) {
2791 char *mpath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
2792 mchild
[mc
], B_FALSE
);
2794 for (sc
= 0; sc
< schildren
; sc
++) {
2795 char *spath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
2796 schild
[sc
], B_FALSE
);
2797 boolean_t result
= (strcmp(mpath
, spath
) == 0);
2813 * Split a mirror pool. If newroot points to null, then a new nvlist
2814 * is generated and it is the responsibility of the caller to free it.
2817 zpool_vdev_split(zpool_handle_t
*zhp
, char *newname
, nvlist_t
**newroot
,
2818 nvlist_t
*props
, splitflags_t flags
)
2820 zfs_cmd_t zc
= { 0 };
2822 nvlist_t
*tree
, *config
, **child
, **newchild
, *newconfig
= NULL
;
2823 nvlist_t
**varray
= NULL
, *zc_props
= NULL
;
2824 uint_t c
, children
, newchildren
, lastlog
= 0, vcount
, found
= 0;
2825 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2827 boolean_t freelist
= B_FALSE
, memory_err
= B_TRUE
;
2830 (void) snprintf(msg
, sizeof (msg
),
2831 dgettext(TEXT_DOMAIN
, "Unable to split %s"), zhp
->zpool_name
);
2833 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
2834 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
2836 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
2837 (void) fprintf(stderr
, gettext("Internal error: unable to "
2838 "retrieve pool configuration\n"));
2842 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &tree
)
2844 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
, &vers
) == 0);
2847 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
2848 if ((zc_props
= zpool_valid_proplist(hdl
, zhp
->zpool_name
,
2849 props
, vers
, flags
, msg
)) == NULL
)
2853 if (nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
2855 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2856 "Source pool is missing vdev tree"));
2858 nvlist_free(zc_props
);
2862 varray
= zfs_alloc(hdl
, children
* sizeof (nvlist_t
*));
2865 if (*newroot
== NULL
||
2866 nvlist_lookup_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
2867 &newchild
, &newchildren
) != 0)
2870 for (c
= 0; c
< children
; c
++) {
2871 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
2873 nvlist_t
**mchild
, *vdev
;
2878 * Unlike cache & spares, slogs are stored in the
2879 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2881 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2883 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
2885 if (is_log
|| is_hole
) {
2887 * Create a hole vdev and put it in the config.
2889 if (nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) != 0)
2891 if (nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
,
2892 VDEV_TYPE_HOLE
) != 0)
2894 if (nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_HOLE
,
2899 varray
[vcount
++] = vdev
;
2903 verify(nvlist_lookup_string(child
[c
], ZPOOL_CONFIG_TYPE
, &type
)
2905 if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
2906 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2907 "Source pool must be composed only of mirrors\n"));
2908 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2912 verify(nvlist_lookup_nvlist_array(child
[c
],
2913 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
2915 /* find or add an entry for this top-level vdev */
2916 if (newchildren
> 0 &&
2917 (entry
= find_vdev_entry(zhp
, mchild
, mchildren
,
2918 newchild
, newchildren
)) >= 0) {
2919 /* We found a disk that the user specified. */
2920 vdev
= mchild
[entry
];
2923 /* User didn't specify a disk for this vdev. */
2924 vdev
= mchild
[mchildren
- 1];
2927 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
2931 /* did we find every disk the user specified? */
2932 if (found
!= newchildren
) {
2933 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Device list must "
2934 "include at most one disk from each mirror"));
2935 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2939 /* Prepare the nvlist for populating. */
2940 if (*newroot
== NULL
) {
2941 if (nvlist_alloc(newroot
, NV_UNIQUE_NAME
, 0) != 0)
2944 if (nvlist_add_string(*newroot
, ZPOOL_CONFIG_TYPE
,
2945 VDEV_TYPE_ROOT
) != 0)
2948 verify(nvlist_remove_all(*newroot
, ZPOOL_CONFIG_CHILDREN
) == 0);
2951 /* Add all the children we found */
2952 if (nvlist_add_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
, varray
,
2953 lastlog
== 0 ? vcount
: lastlog
) != 0)
2957 * If we're just doing a dry run, exit now with success.
2960 memory_err
= B_FALSE
;
2965 /* now build up the config list & call the ioctl */
2966 if (nvlist_alloc(&newconfig
, NV_UNIQUE_NAME
, 0) != 0)
2969 if (nvlist_add_nvlist(newconfig
,
2970 ZPOOL_CONFIG_VDEV_TREE
, *newroot
) != 0 ||
2971 nvlist_add_string(newconfig
,
2972 ZPOOL_CONFIG_POOL_NAME
, newname
) != 0 ||
2973 nvlist_add_uint64(newconfig
, ZPOOL_CONFIG_VERSION
, vers
) != 0)
2977 * The new pool is automatically part of the namespace unless we
2978 * explicitly export it.
2981 zc
.zc_cookie
= ZPOOL_EXPORT_AFTER_SPLIT
;
2982 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2983 (void) strlcpy(zc
.zc_string
, newname
, sizeof (zc
.zc_string
));
2984 if (zcmd_write_conf_nvlist(hdl
, &zc
, newconfig
) != 0)
2986 if (zc_props
!= NULL
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
2989 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SPLIT
, &zc
) != 0) {
2990 retval
= zpool_standard_error(hdl
, errno
, msg
);
2995 memory_err
= B_FALSE
;
2998 if (varray
!= NULL
) {
3001 for (v
= 0; v
< vcount
; v
++)
3002 nvlist_free(varray
[v
]);
3005 zcmd_free_nvlists(&zc
);
3007 nvlist_free(zc_props
);
3009 nvlist_free(newconfig
);
3011 nvlist_free(*newroot
);
3019 return (no_memory(hdl
));
3025 * Remove the given device. Currently, this is supported only for hot spares
3026 * and level 2 cache devices.
3029 zpool_vdev_remove(zpool_handle_t
*zhp
, const char *path
)
3031 zfs_cmd_t zc
= { 0 };
3034 boolean_t avail_spare
, l2cache
, islog
;
3035 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3038 (void) snprintf(msg
, sizeof (msg
),
3039 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3041 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3042 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3044 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3046 * XXX - this should just go away.
3048 if (!avail_spare
&& !l2cache
&& !islog
) {
3049 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3050 "only inactive hot spares, cache, top-level, "
3051 "or log devices can be removed"));
3052 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3055 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
3056 if (islog
&& version
< SPA_VERSION_HOLES
) {
3057 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3058 "pool must be upgrade to support log removal"));
3059 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
3062 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
3064 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3067 return (zpool_standard_error(hdl
, errno
, msg
));
3071 * Clear the errors for the pool, or the particular device if specified.
3074 zpool_clear(zpool_handle_t
*zhp
, const char *path
, nvlist_t
*rewindnvl
)
3076 zfs_cmd_t zc
= { 0 };
3079 zpool_rewind_policy_t policy
;
3080 boolean_t avail_spare
, l2cache
;
3081 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3082 nvlist_t
*nvi
= NULL
;
3086 (void) snprintf(msg
, sizeof (msg
),
3087 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3090 (void) snprintf(msg
, sizeof (msg
),
3091 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3094 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3096 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
,
3097 &l2cache
, NULL
)) == 0)
3098 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3101 * Don't allow error clearing for hot spares. Do allow
3102 * error clearing for l2cache devices.
3105 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3107 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
,
3111 zpool_get_rewind_policy(rewindnvl
, &policy
);
3112 zc
.zc_cookie
= policy
.zrp_request
;
3114 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zhp
->zpool_config_size
* 2) != 0)
3117 if (zcmd_write_src_nvlist(hdl
, &zc
, rewindnvl
) != 0)
3120 while ((error
= zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
)) != 0 &&
3122 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
3123 zcmd_free_nvlists(&zc
);
3128 if (!error
|| ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) &&
3129 errno
!= EPERM
&& errno
!= EACCES
)) {
3130 if (policy
.zrp_request
&
3131 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
3132 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nvi
);
3133 zpool_rewind_exclaim(hdl
, zc
.zc_name
,
3134 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0),
3138 zcmd_free_nvlists(&zc
);
3142 zcmd_free_nvlists(&zc
);
3143 return (zpool_standard_error(hdl
, errno
, msg
));
3147 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3150 zpool_vdev_clear(zpool_handle_t
*zhp
, uint64_t guid
)
3152 zfs_cmd_t zc
= { 0 };
3154 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3156 (void) snprintf(msg
, sizeof (msg
),
3157 dgettext(TEXT_DOMAIN
, "cannot clear errors for %llx"),
3160 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3162 zc
.zc_cookie
= ZPOOL_NO_REWIND
;
3164 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_CLEAR
, &zc
) == 0)
3167 return (zpool_standard_error(hdl
, errno
, msg
));
3171 * Change the GUID for a pool.
3174 zpool_reguid(zpool_handle_t
*zhp
)
3177 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3178 zfs_cmd_t zc
= { 0 };
3180 (void) snprintf(msg
, sizeof (msg
),
3181 dgettext(TEXT_DOMAIN
, "cannot reguid '%s'"), zhp
->zpool_name
);
3183 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3184 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REGUID
, &zc
) == 0)
3187 return (zpool_standard_error(hdl
, errno
, msg
));
3194 zpool_reopen(zpool_handle_t
*zhp
)
3196 zfs_cmd_t zc
= { 0 };
3198 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3200 (void) snprintf(msg
, sizeof (msg
),
3201 dgettext(TEXT_DOMAIN
, "cannot reopen '%s'"),
3204 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3205 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REOPEN
, &zc
) == 0)
3207 return (zpool_standard_error(hdl
, errno
, msg
));
3211 * Convert from a devid string to a path.
3214 devid_to_path(char *devid_str
)
3219 devid_nmlist_t
*list
= NULL
;
3222 if (devid_str_decode(devid_str
, &devid
, &minor
) != 0)
3225 ret
= devid_deviceid_to_nmlist("/dev", devid
, minor
, &list
);
3227 devid_str_free(minor
);
3234 * In a case the strdup() fails, we will just return NULL below.
3236 path
= strdup(list
[0].devname
);
3238 devid_free_nmlist(list
);
3244 * Convert from a path to a devid string.
3247 path_to_devid(const char *path
)
3253 if ((fd
= open(path
, O_RDONLY
)) < 0)
3258 if (devid_get(fd
, &devid
) == 0) {
3259 if (devid_get_minor_name(fd
, &minor
) == 0)
3260 ret
= devid_str_encode(devid
, minor
);
3262 devid_str_free(minor
);
3271 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3272 * ignore any failure here, since a common case is for an unprivileged user to
3273 * type 'zpool status', and we'll display the correct information anyway.
3276 set_path(zpool_handle_t
*zhp
, nvlist_t
*nv
, const char *path
)
3278 zfs_cmd_t zc
= { 0 };
3280 (void) strncpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3281 (void) strncpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
3282 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
3285 (void) ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_VDEV_SETPATH
, &zc
);
3289 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3290 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3291 * We also check if this is a whole disk, in which case we strip off the
3292 * trailing 's0' slice name.
3294 * This routine is also responsible for identifying when disks have been
3295 * reconfigured in a new location. The kernel will have opened the device by
3296 * devid, but the path will still refer to the old location. To catch this, we
3297 * first do a path -> devid translation (which is fast for the common case). If
3298 * the devid matches, we're done. If not, we do a reverse devid -> path
3299 * translation and issue the appropriate ioctl() to update the path of the vdev.
3300 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3304 zpool_vdev_name(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, nvlist_t
*nv
,
3313 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
3315 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
3317 (void) snprintf(buf
, sizeof (buf
), "%llu",
3318 (u_longlong_t
)value
);
3320 } else if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
3323 * If the device is dead (faulted, offline, etc) then don't
3324 * bother opening it. Otherwise we may be forcing the user to
3325 * open a misbehaving device, which can have undesirable
3328 if ((nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
3329 (uint64_t **)&vs
, &vsc
) != 0 ||
3330 vs
->vs_state
>= VDEV_STATE_DEGRADED
) &&
3332 nvlist_lookup_string(nv
, ZPOOL_CONFIG_DEVID
, &devid
) == 0) {
3334 * Determine if the current path is correct.
3336 char *newdevid
= path_to_devid(path
);
3338 if (newdevid
== NULL
||
3339 strcmp(devid
, newdevid
) != 0) {
3342 if ((newpath
= devid_to_path(devid
)) != NULL
) {
3344 * Update the path appropriately.
3346 set_path(zhp
, nv
, newpath
);
3347 if (nvlist_add_string(nv
,
3348 ZPOOL_CONFIG_PATH
, newpath
) == 0)
3349 verify(nvlist_lookup_string(nv
,
3357 devid_str_free(newdevid
);
3360 if (strncmp(path
, "/dev/dsk/", 9) == 0)
3363 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
3364 &value
) == 0 && value
) {
3365 int pathlen
= strlen(path
);
3366 char *tmp
= zfs_strdup(hdl
, path
);
3369 * If it starts with c#, and ends with "s0", chop
3370 * the "s0" off, or if it ends with "s0/old", remove
3371 * the "s0" from the middle.
3373 if (CTD_CHECK(tmp
)) {
3374 if (strcmp(&tmp
[pathlen
- 2], "s0") == 0) {
3375 tmp
[pathlen
- 2] = '\0';
3376 } else if (pathlen
> 6 &&
3377 strcmp(&tmp
[pathlen
- 6], "s0/old") == 0) {
3378 (void) strcpy(&tmp
[pathlen
- 6],
3385 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &path
) == 0);
3388 * If it's a raidz device, we need to stick in the parity level.
3390 if (strcmp(path
, VDEV_TYPE_RAIDZ
) == 0) {
3391 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
,
3393 (void) snprintf(buf
, sizeof (buf
), "%s%llu", path
,
3394 (u_longlong_t
)value
);
3399 * We identify each top-level vdev by using a <type-id>
3400 * naming convention.
3405 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
3407 (void) snprintf(buf
, sizeof (buf
), "%s-%llu", path
,
3413 return (zfs_strdup(hdl
, path
));
3417 zbookmark_mem_compare(const void *a
, const void *b
)
3419 return (memcmp(a
, b
, sizeof (zbookmark_phys_t
)));
3423 * Retrieve the persistent error log, uniquify the members, and return to the
3427 zpool_get_errlog(zpool_handle_t
*zhp
, nvlist_t
**nverrlistp
)
3429 zfs_cmd_t zc
= { 0 };
3431 zbookmark_phys_t
*zb
= NULL
;
3435 * Retrieve the raw error list from the kernel. If the number of errors
3436 * has increased, allocate more space and continue until we get the
3439 verify(nvlist_lookup_uint64(zhp
->zpool_config
, ZPOOL_CONFIG_ERRCOUNT
,
3443 if ((zc
.zc_nvlist_dst
= (uintptr_t)zfs_alloc(zhp
->zpool_hdl
,
3444 count
* sizeof (zbookmark_phys_t
))) == (uintptr_t)NULL
)
3446 zc
.zc_nvlist_dst_size
= count
;
3447 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3449 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_ERROR_LOG
,
3451 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3452 if (errno
== ENOMEM
) {
3455 count
= zc
.zc_nvlist_dst_size
;
3456 dst
= zfs_alloc(zhp
->zpool_hdl
, count
*
3457 sizeof (zbookmark_phys_t
));
3460 zc
.zc_nvlist_dst
= (uintptr_t)dst
;
3470 * Sort the resulting bookmarks. This is a little confusing due to the
3471 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3472 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3473 * _not_ copied as part of the process. So we point the start of our
3474 * array appropriate and decrement the total number of elements.
3476 zb
= ((zbookmark_phys_t
*)(uintptr_t)zc
.zc_nvlist_dst
) +
3477 zc
.zc_nvlist_dst_size
;
3478 count
-= zc
.zc_nvlist_dst_size
;
3480 qsort(zb
, count
, sizeof (zbookmark_phys_t
), zbookmark_mem_compare
);
3482 verify(nvlist_alloc(nverrlistp
, 0, KM_SLEEP
) == 0);
3485 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3487 for (i
= 0; i
< count
; i
++) {
3490 /* ignoring zb_blkid and zb_level for now */
3491 if (i
> 0 && zb
[i
-1].zb_objset
== zb
[i
].zb_objset
&&
3492 zb
[i
-1].zb_object
== zb
[i
].zb_object
)
3495 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) != 0)
3497 if (nvlist_add_uint64(nv
, ZPOOL_ERR_DATASET
,
3498 zb
[i
].zb_objset
) != 0) {
3502 if (nvlist_add_uint64(nv
, ZPOOL_ERR_OBJECT
,
3503 zb
[i
].zb_object
) != 0) {
3507 if (nvlist_add_nvlist(*nverrlistp
, "ejk", nv
) != 0) {
3514 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3518 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3519 return (no_memory(zhp
->zpool_hdl
));
3523 * Upgrade a ZFS pool to the latest on-disk version.
3526 zpool_upgrade(zpool_handle_t
*zhp
, uint64_t new_version
)
3528 zfs_cmd_t zc
= { 0 };
3529 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3531 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3532 zc
.zc_cookie
= new_version
;
3534 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_UPGRADE
, &zc
) != 0)
3535 return (zpool_standard_error_fmt(hdl
, errno
,
3536 dgettext(TEXT_DOMAIN
, "cannot upgrade '%s'"),
3542 zfs_save_arguments(int argc
, char **argv
, char *string
, int len
)
3544 (void) strlcpy(string
, basename(argv
[0]), len
);
3545 for (int i
= 1; i
< argc
; i
++) {
3546 (void) strlcat(string
, " ", len
);
3547 (void) strlcat(string
, argv
[i
], len
);
3552 zpool_log_history(libzfs_handle_t
*hdl
, const char *message
)
3554 zfs_cmd_t zc
= { 0 };
3558 args
= fnvlist_alloc();
3559 fnvlist_add_string(args
, "message", message
);
3560 err
= zcmd_write_src_nvlist(hdl
, &zc
, args
);
3562 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_LOG_HISTORY
, &zc
);
3564 zcmd_free_nvlists(&zc
);
3569 * Perform ioctl to get some command history of a pool.
3571 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3572 * logical offset of the history buffer to start reading from.
3574 * Upon return, 'off' is the next logical offset to read from and
3575 * 'len' is the actual amount of bytes read into 'buf'.
3578 get_history(zpool_handle_t
*zhp
, char *buf
, uint64_t *off
, uint64_t *len
)
3580 zfs_cmd_t zc
= { 0 };
3581 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3583 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3585 zc
.zc_history
= (uint64_t)(uintptr_t)buf
;
3586 zc
.zc_history_len
= *len
;
3587 zc
.zc_history_offset
= *off
;
3589 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_HISTORY
, &zc
) != 0) {
3592 return (zfs_error_fmt(hdl
, EZFS_PERM
,
3593 dgettext(TEXT_DOMAIN
,
3594 "cannot show history for pool '%s'"),
3597 return (zfs_error_fmt(hdl
, EZFS_NOHISTORY
,
3598 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
3599 "'%s'"), zhp
->zpool_name
));
3601 return (zfs_error_fmt(hdl
, EZFS_BADVERSION
,
3602 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
3603 "'%s', pool must be upgraded"), zhp
->zpool_name
));
3605 return (zpool_standard_error_fmt(hdl
, errno
,
3606 dgettext(TEXT_DOMAIN
,
3607 "cannot get history for '%s'"), zhp
->zpool_name
));
3611 *len
= zc
.zc_history_len
;
3612 *off
= zc
.zc_history_offset
;
3618 * Process the buffer of nvlists, unpacking and storing each nvlist record
3619 * into 'records'. 'leftover' is set to the number of bytes that weren't
3620 * processed as there wasn't a complete record.
3623 zpool_history_unpack(char *buf
, uint64_t bytes_read
, uint64_t *leftover
,
3624 nvlist_t
***records
, uint_t
*numrecords
)
3630 while (bytes_read
> sizeof (reclen
)) {
3632 /* get length of packed record (stored as little endian) */
3633 for (i
= 0, reclen
= 0; i
< sizeof (reclen
); i
++)
3634 reclen
+= (uint64_t)(((uchar_t
*)buf
)[i
]) << (8*i
);
3636 if (bytes_read
< sizeof (reclen
) + reclen
)
3640 if (nvlist_unpack(buf
+ sizeof (reclen
), reclen
, &nv
, 0) != 0)
3642 bytes_read
-= sizeof (reclen
) + reclen
;
3643 buf
+= sizeof (reclen
) + reclen
;
3645 /* add record to nvlist array */
3647 if (ISP2(*numrecords
+ 1)) {
3648 *records
= realloc(*records
,
3649 *numrecords
* 2 * sizeof (nvlist_t
*));
3651 (*records
)[*numrecords
- 1] = nv
;
3654 *leftover
= bytes_read
;
3659 * Retrieve the command history of a pool.
3662 zpool_get_history(zpool_handle_t
*zhp
, nvlist_t
**nvhisp
)
3665 int buflen
= 128 * 1024;
3667 nvlist_t
**records
= NULL
;
3668 uint_t numrecords
= 0;
3671 buf
= malloc(buflen
);
3675 uint64_t bytes_read
= buflen
;
3678 if ((err
= get_history(zhp
, buf
, &off
, &bytes_read
)) != 0)
3681 /* if nothing else was read in, we're at EOF, just return */
3685 if ((err
= zpool_history_unpack(buf
, bytes_read
,
3686 &leftover
, &records
, &numrecords
)) != 0)
3689 if (leftover
== bytes_read
) {
3691 * no progress made, because buffer is not big enough
3692 * to hold this record; resize and retry.
3696 buf
= malloc(buflen
);
3707 verify(nvlist_alloc(nvhisp
, NV_UNIQUE_NAME
, 0) == 0);
3708 verify(nvlist_add_nvlist_array(*nvhisp
, ZPOOL_HIST_RECORD
,
3709 records
, numrecords
) == 0);
3711 for (i
= 0; i
< numrecords
; i
++)
3712 nvlist_free(records
[i
]);
3719 zpool_obj_to_path(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
3720 char *pathname
, size_t len
)
3722 zfs_cmd_t zc
= { 0 };
3723 boolean_t mounted
= B_FALSE
;
3724 char *mntpnt
= NULL
;
3725 char dsname
[MAXNAMELEN
];
3728 /* special case for the MOS */
3729 (void) snprintf(pathname
, len
, "<metadata>:<0x%llx>", obj
);
3733 /* get the dataset's name */
3734 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3736 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
,
3737 ZFS_IOC_DSOBJ_TO_DSNAME
, &zc
) != 0) {
3738 /* just write out a path of two object numbers */
3739 (void) snprintf(pathname
, len
, "<0x%llx>:<0x%llx>",
3743 (void) strlcpy(dsname
, zc
.zc_value
, sizeof (dsname
));
3745 /* find out if the dataset is mounted */
3746 mounted
= is_mounted(zhp
->zpool_hdl
, dsname
, &mntpnt
);
3748 /* get the corrupted object's path */
3749 (void) strlcpy(zc
.zc_name
, dsname
, sizeof (zc
.zc_name
));
3751 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_OBJ_TO_PATH
,
3754 (void) snprintf(pathname
, len
, "%s%s", mntpnt
,
3757 (void) snprintf(pathname
, len
, "%s:%s",
3758 dsname
, zc
.zc_value
);
3761 (void) snprintf(pathname
, len
, "%s:<0x%llx>", dsname
, obj
);
3767 * Read the EFI label from the config, if a label does not exist then
3768 * pass back the error to the caller. If the caller has passed a non-NULL
3769 * diskaddr argument then we set it to the starting address of the EFI
3773 read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
)
3777 char diskname
[MAXPATHLEN
];
3780 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PATH
, &path
) != 0)
3783 (void) snprintf(diskname
, sizeof (diskname
), "%s%s", RDISK_ROOT
,
3784 strrchr(path
, '/'));
3785 if ((fd
= open(diskname
, O_RDONLY
|O_NDELAY
)) >= 0) {
3786 struct dk_gpt
*vtoc
;
3788 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) >= 0) {
3790 *sb
= vtoc
->efi_parts
[0].p_start
;
3799 * determine where a partition starts on a disk in the current
3803 find_start_block(nvlist_t
*config
)
3807 diskaddr_t sb
= MAXOFFSET_T
;
3810 if (nvlist_lookup_nvlist_array(config
,
3811 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) != 0) {
3812 if (nvlist_lookup_uint64(config
,
3813 ZPOOL_CONFIG_WHOLE_DISK
,
3814 &wholedisk
) != 0 || !wholedisk
) {
3815 return (MAXOFFSET_T
);
3817 if (read_efi_label(config
, &sb
) < 0)
3822 for (c
= 0; c
< children
; c
++) {
3823 sb
= find_start_block(child
[c
]);
3824 if (sb
!= MAXOFFSET_T
) {
3828 return (MAXOFFSET_T
);
3832 * Label an individual disk. The name provided is the short name,
3833 * stripped of any leading /dev path.
3836 zpool_label_disk(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, char *name
)
3838 char path
[MAXPATHLEN
];
3839 struct dk_gpt
*vtoc
;
3841 size_t resv
= EFI_MIN_RESV_SIZE
;
3842 uint64_t slice_size
;
3843 diskaddr_t start_block
;
3846 /* prepare an error message just in case */
3847 (void) snprintf(errbuf
, sizeof (errbuf
),
3848 dgettext(TEXT_DOMAIN
, "cannot label '%s'"), name
);
3853 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
3854 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3856 if (zhp
->zpool_start_block
== 0)
3857 start_block
= find_start_block(nvroot
);
3859 start_block
= zhp
->zpool_start_block
;
3860 zhp
->zpool_start_block
= start_block
;
3863 start_block
= NEW_START_BLOCK
;
3866 (void) snprintf(path
, sizeof (path
), "%s/%s%s", RDISK_ROOT
, name
,
3869 if ((fd
= open(path
, O_RDWR
| O_NDELAY
)) < 0) {
3871 * This shouldn't happen. We've long since verified that this
3872 * is a valid device.
3875 dgettext(TEXT_DOMAIN
, "unable to open device"));
3876 return (zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
));
3879 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, &vtoc
) != 0) {
3881 * The only way this can fail is if we run out of memory, or we
3882 * were unable to read the disk's capacity
3884 if (errno
== ENOMEM
)
3885 (void) no_memory(hdl
);
3888 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3889 "unable to read disk capacity"), name
);
3891 return (zfs_error(hdl
, EZFS_NOCAP
, errbuf
));
3894 slice_size
= vtoc
->efi_last_u_lba
+ 1;
3895 slice_size
-= EFI_MIN_RESV_SIZE
;
3896 if (start_block
== MAXOFFSET_T
)
3897 start_block
= NEW_START_BLOCK
;
3898 slice_size
-= start_block
;
3900 vtoc
->efi_parts
[0].p_start
= start_block
;
3901 vtoc
->efi_parts
[0].p_size
= slice_size
;
3904 * Why we use V_USR: V_BACKUP confuses users, and is considered
3905 * disposable by some EFI utilities (since EFI doesn't have a backup
3906 * slice). V_UNASSIGNED is supposed to be used only for zero size
3907 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
3908 * etc. were all pretty specific. V_USR is as close to reality as we
3909 * can get, in the absence of V_OTHER.
3911 vtoc
->efi_parts
[0].p_tag
= V_USR
;
3912 (void) strcpy(vtoc
->efi_parts
[0].p_name
, "zfs");
3914 vtoc
->efi_parts
[8].p_start
= slice_size
+ start_block
;
3915 vtoc
->efi_parts
[8].p_size
= resv
;
3916 vtoc
->efi_parts
[8].p_tag
= V_RESERVED
;
3918 if (efi_write(fd
, vtoc
) != 0) {
3920 * Some block drivers (like pcata) may not support EFI
3921 * GPT labels. Print out a helpful error message dir-
3922 * ecting the user to manually label the disk and give
3928 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3929 "try using fdisk(1M) and then provide a specific slice"));
3930 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
3939 supported_dump_vdev_type(libzfs_handle_t
*hdl
, nvlist_t
*config
, char *errbuf
)
3945 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
3946 if (strcmp(type
, VDEV_TYPE_FILE
) == 0 ||
3947 strcmp(type
, VDEV_TYPE_HOLE
) == 0 ||
3948 strcmp(type
, VDEV_TYPE_MISSING
) == 0) {
3949 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3950 "vdev type '%s' is not supported"), type
);
3951 (void) zfs_error(hdl
, EZFS_VDEVNOTSUP
, errbuf
);
3954 if (nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_CHILDREN
,
3955 &child
, &children
) == 0) {
3956 for (c
= 0; c
< children
; c
++) {
3957 if (!supported_dump_vdev_type(hdl
, child
[c
], errbuf
))
3965 * Check if this zvol is allowable for use as a dump device; zero if
3966 * it is, > 0 if it isn't, < 0 if it isn't a zvol.
3968 * Allowable storage configurations include mirrors, all raidz variants, and
3969 * pools with log, cache, and spare devices. Pools which are backed by files or
3970 * have missing/hole vdevs are not suitable.
3973 zvol_check_dump_config(char *arg
)
3975 zpool_handle_t
*zhp
= NULL
;
3976 nvlist_t
*config
, *nvroot
;
3980 libzfs_handle_t
*hdl
;
3982 char poolname
[ZPOOL_MAXNAMELEN
];
3983 int pathlen
= strlen(ZVOL_FULL_DEV_DIR
);
3986 if (strncmp(arg
, ZVOL_FULL_DEV_DIR
, pathlen
)) {
3990 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3991 "dump is not supported on device '%s'"), arg
);
3993 if ((hdl
= libzfs_init()) == NULL
)
3995 libzfs_print_on_error(hdl
, B_TRUE
);
3997 volname
= arg
+ pathlen
;
3999 /* check the configuration of the pool */
4000 if ((p
= strchr(volname
, '/')) == NULL
) {
4001 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4002 "malformed dataset name"));
4003 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
4005 } else if (p
- volname
>= ZFS_MAXNAMELEN
) {
4006 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4007 "dataset name is too long"));
4008 (void) zfs_error(hdl
, EZFS_NAMETOOLONG
, errbuf
);
4011 (void) strncpy(poolname
, volname
, p
- volname
);
4012 poolname
[p
- volname
] = '\0';
4015 if ((zhp
= zpool_open(hdl
, poolname
)) == NULL
) {
4016 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4017 "could not open pool '%s'"), poolname
);
4018 (void) zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
);
4021 config
= zpool_get_config(zhp
, NULL
);
4022 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4024 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4025 "could not obtain vdev configuration for '%s'"), poolname
);
4026 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, errbuf
);
4030 verify(nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
4031 &top
, &toplevels
) == 0);
4033 if (!supported_dump_vdev_type(hdl
, top
[0], errbuf
)) {