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.
37 #include <sys/efi_partition.h>
39 #include <sys/zfs_ioctl.h>
42 #include "zfs_namecheck.h"
44 #include "libzfs_impl.h"
45 #include "zfs_comutil.h"
47 static int read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
);
49 typedef struct prop_flags
{
50 int create
:1; /* Validate property on creation */
51 int import
:1; /* Validate property on import */
55 * ====================================================================
56 * zpool property functions
57 * ====================================================================
61 zpool_get_all_props(zpool_handle_t
*zhp
)
63 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
64 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
66 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
68 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
71 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_PROPS
, &zc
) != 0) {
72 if (errno
== ENOMEM
) {
73 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
74 zcmd_free_nvlists(&zc
);
78 zcmd_free_nvlists(&zc
);
83 if (zcmd_read_dst_nvlist(hdl
, &zc
, &zhp
->zpool_props
) != 0) {
84 zcmd_free_nvlists(&zc
);
88 zcmd_free_nvlists(&zc
);
94 zpool_props_refresh(zpool_handle_t
*zhp
)
98 old_props
= zhp
->zpool_props
;
100 if (zpool_get_all_props(zhp
) != 0)
103 nvlist_free(old_props
);
108 zpool_get_prop_string(zpool_handle_t
*zhp
, zpool_prop_t prop
,
114 zprop_source_t source
;
116 nvl
= zhp
->zpool_props
;
117 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
118 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &ival
) == 0);
120 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
122 source
= ZPROP_SRC_DEFAULT
;
123 if ((value
= (char *)zpool_prop_default_string(prop
)) == NULL
)
134 zpool_get_prop_int(zpool_handle_t
*zhp
, zpool_prop_t prop
, zprop_source_t
*src
)
138 zprop_source_t source
;
140 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
)) {
142 * zpool_get_all_props() has most likely failed because
143 * the pool is faulted, but if all we need is the top level
144 * vdev's guid then get it from the zhp config nvlist.
146 if ((prop
== ZPOOL_PROP_GUID
) &&
147 (nvlist_lookup_nvlist(zhp
->zpool_config
,
148 ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0) &&
149 (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
)
153 return (zpool_prop_default_numeric(prop
));
156 nvl
= zhp
->zpool_props
;
157 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
158 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &value
) == 0);
160 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
162 source
= ZPROP_SRC_DEFAULT
;
163 value
= zpool_prop_default_numeric(prop
);
173 * Map VDEV STATE to printed strings.
176 zpool_state_to_name(vdev_state_t state
, vdev_aux_t aux
)
181 case VDEV_STATE_CLOSED
:
182 case VDEV_STATE_OFFLINE
:
183 return (gettext("OFFLINE"));
184 case VDEV_STATE_REMOVED
:
185 return (gettext("REMOVED"));
186 case VDEV_STATE_CANT_OPEN
:
187 if (aux
== VDEV_AUX_CORRUPT_DATA
|| aux
== VDEV_AUX_BAD_LOG
)
188 return (gettext("FAULTED"));
189 else if (aux
== VDEV_AUX_SPLIT_POOL
)
190 return (gettext("SPLIT"));
192 return (gettext("UNAVAIL"));
193 case VDEV_STATE_FAULTED
:
194 return (gettext("FAULTED"));
195 case VDEV_STATE_DEGRADED
:
196 return (gettext("DEGRADED"));
197 case VDEV_STATE_HEALTHY
:
198 return (gettext("ONLINE"));
201 return (gettext("UNKNOWN"));
205 * Get a zpool property value for 'prop' and return the value in
206 * a pre-allocated buffer.
209 zpool_get_prop(zpool_handle_t
*zhp
, zpool_prop_t prop
, char *buf
, size_t len
,
210 zprop_source_t
*srctype
)
214 zprop_source_t src
= ZPROP_SRC_NONE
;
219 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
221 case ZPOOL_PROP_NAME
:
222 (void) strlcpy(buf
, zpool_get_name(zhp
), len
);
225 case ZPOOL_PROP_HEALTH
:
226 (void) strlcpy(buf
, "FAULTED", len
);
229 case ZPOOL_PROP_GUID
:
230 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
231 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
234 case ZPOOL_PROP_ALTROOT
:
235 case ZPOOL_PROP_CACHEFILE
:
236 if (zhp
->zpool_props
!= NULL
||
237 zpool_get_all_props(zhp
) == 0) {
239 zpool_get_prop_string(zhp
, prop
, &src
),
247 (void) strlcpy(buf
, "-", len
);
256 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
) &&
257 prop
!= ZPOOL_PROP_NAME
)
260 switch (zpool_prop_get_type(prop
)) {
261 case PROP_TYPE_STRING
:
262 (void) strlcpy(buf
, zpool_get_prop_string(zhp
, prop
, &src
),
266 case PROP_TYPE_NUMBER
:
267 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
270 case ZPOOL_PROP_SIZE
:
271 case ZPOOL_PROP_ALLOCATED
:
272 case ZPOOL_PROP_FREE
:
273 (void) zfs_nicenum(intval
, buf
, len
);
276 case ZPOOL_PROP_CAPACITY
:
277 (void) snprintf(buf
, len
, "%llu%%",
278 (u_longlong_t
)intval
);
281 case ZPOOL_PROP_DEDUPRATIO
:
282 (void) snprintf(buf
, len
, "%llu.%02llux",
283 (u_longlong_t
)(intval
/ 100),
284 (u_longlong_t
)(intval
% 100));
287 case ZPOOL_PROP_HEALTH
:
288 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
289 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
290 verify(nvlist_lookup_uint64_array(nvroot
,
291 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
294 (void) strlcpy(buf
, zpool_state_to_name(intval
,
298 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
302 case PROP_TYPE_INDEX
:
303 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
304 if (zpool_prop_index_to_string(prop
, intval
, &strval
)
307 (void) strlcpy(buf
, strval
, len
);
321 * Check if the bootfs name has the same pool name as it is set to.
322 * Assuming bootfs is a valid dataset name.
325 bootfs_name_valid(const char *pool
, char *bootfs
)
327 int len
= strlen(pool
);
329 if (!zfs_name_valid(bootfs
, ZFS_TYPE_FILESYSTEM
|ZFS_TYPE_SNAPSHOT
))
332 if (strncmp(pool
, bootfs
, len
) == 0 &&
333 (bootfs
[len
] == '/' || bootfs
[len
] == '\0'))
340 * Inspect the configuration to determine if any of the devices contain
344 pool_uses_efi(nvlist_t
*config
)
349 if (nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_CHILDREN
,
350 &child
, &children
) != 0)
351 return (read_efi_label(config
, NULL
) >= 0);
353 for (c
= 0; c
< children
; c
++) {
354 if (pool_uses_efi(child
[c
]))
361 pool_is_bootable(zpool_handle_t
*zhp
)
363 char bootfs
[ZPOOL_MAXNAMELEN
];
365 return (zpool_get_prop(zhp
, ZPOOL_PROP_BOOTFS
, bootfs
,
366 sizeof (bootfs
), NULL
) == 0 && strncmp(bootfs
, "-",
367 sizeof (bootfs
)) != 0);
372 * Given an nvlist of zpool properties to be set, validate that they are
373 * correct, and parse any numeric properties (index, boolean, etc) if they are
374 * specified as strings.
377 zpool_valid_proplist(libzfs_handle_t
*hdl
, const char *poolname
,
378 nvlist_t
*props
, uint64_t version
, prop_flags_t flags
, char *errbuf
)
386 struct stat64 statbuf
;
390 if (nvlist_alloc(&retprops
, NV_UNIQUE_NAME
, 0) != 0) {
391 (void) no_memory(hdl
);
396 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
397 const char *propname
= nvpair_name(elem
);
400 * Make sure this property is valid and applies to this type.
402 if ((prop
= zpool_name_to_prop(propname
)) == ZPROP_INVAL
) {
403 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
404 "invalid property '%s'"), propname
);
405 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
409 if (zpool_prop_readonly(prop
)) {
410 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
411 "is readonly"), propname
);
412 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
416 if (zprop_parse_value(hdl
, elem
, prop
, ZFS_TYPE_POOL
, retprops
,
417 &strval
, &intval
, errbuf
) != 0)
421 * Perform additional checking for specific properties.
426 case ZPOOL_PROP_VERSION
:
427 if (intval
< version
|| intval
> SPA_VERSION
) {
428 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
429 "property '%s' number %d is invalid."),
431 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
436 case ZPOOL_PROP_BOOTFS
:
437 if (flags
.create
|| flags
.import
) {
438 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
439 "property '%s' cannot be set at creation "
440 "or import time"), propname
);
441 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
445 if (version
< SPA_VERSION_BOOTFS
) {
446 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
447 "pool must be upgraded to support "
448 "'%s' property"), propname
);
449 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
454 * bootfs property value has to be a dataset name and
455 * the dataset has to be in the same pool as it sets to.
457 if (strval
[0] != '\0' && !bootfs_name_valid(poolname
,
459 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
460 "is an invalid name"), strval
);
461 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
465 if ((zhp
= zpool_open_canfail(hdl
, poolname
)) == NULL
) {
466 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
467 "could not open pool '%s'"), poolname
);
468 (void) zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
);
471 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
472 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
475 * bootfs property cannot be set on a disk which has
478 if (pool_uses_efi(nvroot
)) {
479 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
480 "property '%s' not supported on "
481 "EFI labeled devices"), propname
);
482 (void) zfs_error(hdl
, EZFS_POOL_NOTSUP
, errbuf
);
489 case ZPOOL_PROP_ALTROOT
:
490 if (!flags
.create
&& !flags
.import
) {
491 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
492 "property '%s' can only be set during pool "
493 "creation or import"), propname
);
494 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
498 if (strval
[0] != '/') {
499 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
500 "bad alternate root '%s'"), strval
);
501 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
506 case ZPOOL_PROP_CACHEFILE
:
507 if (strval
[0] == '\0')
510 if (strcmp(strval
, "none") == 0)
513 if (strval
[0] != '/') {
514 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
515 "property '%s' must be empty, an "
516 "absolute path, or 'none'"), propname
);
517 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
521 slash
= strrchr(strval
, '/');
523 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
524 strcmp(slash
, "/..") == 0) {
525 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
526 "'%s' is not a valid file"), strval
);
527 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
533 if (strval
[0] != '\0' &&
534 (stat64(strval
, &statbuf
) != 0 ||
535 !S_ISDIR(statbuf
.st_mode
))) {
536 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
537 "'%s' is not a valid directory"),
539 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
546 case ZPOOL_PROP_READONLY
:
548 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
549 "property '%s' can only be set at "
550 "import time"), propname
);
551 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
560 nvlist_free(retprops
);
565 * Set zpool property : propname=propval.
568 zpool_set_prop(zpool_handle_t
*zhp
, const char *propname
, const char *propval
)
570 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
573 nvlist_t
*nvl
= NULL
;
576 prop_flags_t flags
= { 0 };
578 (void) snprintf(errbuf
, sizeof (errbuf
),
579 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
582 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
583 return (no_memory(zhp
->zpool_hdl
));
585 if (nvlist_add_string(nvl
, propname
, propval
) != 0) {
587 return (no_memory(zhp
->zpool_hdl
));
590 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
591 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
592 zhp
->zpool_name
, nvl
, version
, flags
, errbuf
)) == NULL
) {
601 * Execute the corresponding ioctl() to set this property.
603 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
605 if (zcmd_write_src_nvlist(zhp
->zpool_hdl
, &zc
, nvl
) != 0) {
610 ret
= zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_SET_PROPS
, &zc
);
612 zcmd_free_nvlists(&zc
);
616 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
618 (void) zpool_props_refresh(zhp
);
624 zpool_expand_proplist(zpool_handle_t
*zhp
, zprop_list_t
**plp
)
626 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
628 char buf
[ZFS_MAXPROPLEN
];
630 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_POOL
) != 0)
633 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
638 if (entry
->pl_prop
!= ZPROP_INVAL
&&
639 zpool_get_prop(zhp
, entry
->pl_prop
, buf
, sizeof (buf
),
641 if (strlen(buf
) > entry
->pl_width
)
642 entry
->pl_width
= strlen(buf
);
651 * Don't start the slice at the default block of 34; many storage
652 * devices will use a stripe width of 128k, other vendors prefer a 1m
653 * alignment. It is best to play it safe and ensure a 1m alignment
654 * give 512b blocks. When the block size is larger by a power of 2
655 * we will still be 1m aligned.
657 #define NEW_START_BLOCK 2048
660 * Validate the given pool name, optionally putting an extended error message in
664 zpool_name_valid(libzfs_handle_t
*hdl
, boolean_t isopen
, const char *pool
)
670 ret
= pool_namecheck(pool
, &why
, &what
);
673 * The rules for reserved pool names were extended at a later point.
674 * But we need to support users with existing pools that may now be
675 * invalid. So we only check for this expanded set of names during a
676 * create (or import), and only in userland.
678 if (ret
== 0 && !isopen
&&
679 (strncmp(pool
, "mirror", 6) == 0 ||
680 strncmp(pool
, "raidz", 5) == 0 ||
681 strncmp(pool
, "spare", 5) == 0 ||
682 strcmp(pool
, "log") == 0)) {
685 dgettext(TEXT_DOMAIN
, "name is reserved"));
693 case NAME_ERR_TOOLONG
:
695 dgettext(TEXT_DOMAIN
, "name is too long"));
698 case NAME_ERR_INVALCHAR
:
700 dgettext(TEXT_DOMAIN
, "invalid character "
701 "'%c' in pool name"), what
);
704 case NAME_ERR_NOLETTER
:
705 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
706 "name must begin with a letter"));
709 case NAME_ERR_RESERVED
:
710 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
711 "name is reserved"));
714 case NAME_ERR_DISKLIKE
:
715 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
716 "pool name is reserved"));
719 case NAME_ERR_LEADING_SLASH
:
720 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
721 "leading slash in name"));
724 case NAME_ERR_EMPTY_COMPONENT
:
725 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
726 "empty component in name"));
729 case NAME_ERR_TRAILING_SLASH
:
730 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
731 "trailing slash in name"));
734 case NAME_ERR_MULTIPLE_AT
:
735 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
736 "multiple '@' delimiters in name"));
739 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
740 "permission set is missing '@'"));
751 * Open a handle to the given pool, even if the pool is currently in the FAULTED
755 zpool_open_canfail(libzfs_handle_t
*hdl
, const char *pool
)
761 * Make sure the pool name is valid.
763 if (!zpool_name_valid(hdl
, B_TRUE
, pool
)) {
764 (void) zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
765 dgettext(TEXT_DOMAIN
, "cannot open '%s'"),
770 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
773 zhp
->zpool_hdl
= hdl
;
774 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
776 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
782 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "no such pool"));
783 (void) zfs_error_fmt(hdl
, EZFS_NOENT
,
784 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), pool
);
793 * Like the above, but silent on error. Used when iterating over pools (because
794 * the configuration cache may be out of date).
797 zpool_open_silent(libzfs_handle_t
*hdl
, const char *pool
, zpool_handle_t
**ret
)
802 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
805 zhp
->zpool_hdl
= hdl
;
806 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
808 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
824 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
828 zpool_open(libzfs_handle_t
*hdl
, const char *pool
)
832 if ((zhp
= zpool_open_canfail(hdl
, pool
)) == NULL
)
835 if (zhp
->zpool_state
== POOL_STATE_UNAVAIL
) {
836 (void) zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
837 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), zhp
->zpool_name
);
846 * Close the handle. Simply frees the memory associated with the handle.
849 zpool_close(zpool_handle_t
*zhp
)
851 if (zhp
->zpool_config
)
852 nvlist_free(zhp
->zpool_config
);
853 if (zhp
->zpool_old_config
)
854 nvlist_free(zhp
->zpool_old_config
);
855 if (zhp
->zpool_props
)
856 nvlist_free(zhp
->zpool_props
);
861 * Return the name of the pool.
864 zpool_get_name(zpool_handle_t
*zhp
)
866 return (zhp
->zpool_name
);
871 * Return the state of the pool (ACTIVE or UNAVAILABLE)
874 zpool_get_state(zpool_handle_t
*zhp
)
876 return (zhp
->zpool_state
);
880 * Create the named pool, using the provided vdev list. It is assumed
881 * that the consumer has already validated the contents of the nvlist, so we
882 * don't have to worry about error semantics.
885 zpool_create(libzfs_handle_t
*hdl
, const char *pool
, nvlist_t
*nvroot
,
886 nvlist_t
*props
, nvlist_t
*fsprops
)
888 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
889 nvlist_t
*zc_fsprops
= NULL
;
890 nvlist_t
*zc_props
= NULL
;
895 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
896 "cannot create '%s'"), pool
);
898 if (!zpool_name_valid(hdl
, B_FALSE
, pool
))
899 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
901 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
905 prop_flags_t flags
= { .create
= B_TRUE
, .import
= B_FALSE
};
907 if ((zc_props
= zpool_valid_proplist(hdl
, pool
, props
,
908 SPA_VERSION_1
, flags
, msg
)) == NULL
) {
917 zoned
= ((nvlist_lookup_string(fsprops
,
918 zfs_prop_to_name(ZFS_PROP_ZONED
), &zonestr
) == 0) &&
919 strcmp(zonestr
, "on") == 0);
921 if ((zc_fsprops
= zfs_valid_proplist(hdl
,
922 ZFS_TYPE_FILESYSTEM
, fsprops
, zoned
, NULL
, msg
)) == NULL
) {
926 (nvlist_alloc(&zc_props
, NV_UNIQUE_NAME
, 0) != 0)) {
929 if (nvlist_add_nvlist(zc_props
,
930 ZPOOL_ROOTFS_PROPS
, zc_fsprops
) != 0) {
935 if (zc_props
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
938 (void) strlcpy(zc
.zc_name
, pool
, sizeof (zc
.zc_name
));
940 if ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_CREATE
, &zc
)) != 0) {
942 zcmd_free_nvlists(&zc
);
943 nvlist_free(zc_props
);
944 nvlist_free(zc_fsprops
);
949 * This can happen if the user has specified the same
950 * device multiple times. We can't reliably detect this
951 * until we try to add it and see we already have a
952 * label. This can also happen under if the device is
953 * part of an active md or lvm device.
955 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
956 "one or more vdevs refer to the same device, or one of\n"
957 "the devices is part of an active md or lvm device"));
958 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
962 * This occurs when one of the devices is below
963 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
964 * device was the problem device since there's no
965 * reliable way to determine device size from userland.
970 zfs_nicenum(SPA_MINDEVSIZE
, buf
, sizeof (buf
));
972 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
973 "one or more devices is less than the "
974 "minimum size (%s)"), buf
);
976 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
979 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
980 "one or more devices is out of space"));
981 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
984 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
985 "cache device must be a disk or disk slice"));
986 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
989 return (zpool_standard_error(hdl
, errno
, msg
));
994 * If this is an alternate root pool, then we automatically set the
995 * mountpoint of the root dataset to be '/'.
997 if (nvlist_lookup_string(props
, zpool_prop_to_name(ZPOOL_PROP_ALTROOT
),
1001 verify((zhp
= zfs_open(hdl
, pool
, ZFS_TYPE_DATASET
)) != NULL
);
1002 verify(zfs_prop_set(zhp
, zfs_prop_to_name(ZFS_PROP_MOUNTPOINT
),
1009 zcmd_free_nvlists(&zc
);
1010 nvlist_free(zc_props
);
1011 nvlist_free(zc_fsprops
);
1016 * Destroy the given pool. It is up to the caller to ensure that there are no
1017 * datasets left in the pool.
1020 zpool_destroy(zpool_handle_t
*zhp
)
1022 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
1023 zfs_handle_t
*zfp
= NULL
;
1024 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1027 if (zhp
->zpool_state
== POOL_STATE_ACTIVE
&&
1028 (zfp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
1031 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1033 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_DESTROY
, &zc
) != 0) {
1034 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1035 "cannot destroy '%s'"), zhp
->zpool_name
);
1037 if (errno
== EROFS
) {
1038 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1039 "one or more devices is read only"));
1040 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1042 (void) zpool_standard_error(hdl
, errno
, msg
);
1051 remove_mountpoint(zfp
);
1059 * Add the given vdevs to the pool. The caller must have already performed the
1060 * necessary verification to ensure that the vdev specification is well-formed.
1063 zpool_add(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
1065 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
1067 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1069 nvlist_t
**spares
, **l2cache
;
1070 uint_t nspares
, nl2cache
;
1072 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1073 "cannot add to '%s'"), zhp
->zpool_name
);
1075 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1076 SPA_VERSION_SPARES
&&
1077 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1078 &spares
, &nspares
) == 0) {
1079 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1080 "upgraded to add hot spares"));
1081 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1084 if (pool_is_bootable(zhp
) && nvlist_lookup_nvlist_array(nvroot
,
1085 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0) {
1088 for (s
= 0; s
< nspares
; s
++) {
1091 if (nvlist_lookup_string(spares
[s
], ZPOOL_CONFIG_PATH
,
1092 &path
) == 0 && pool_uses_efi(spares
[s
])) {
1093 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1094 "device '%s' contains an EFI label and "
1095 "cannot be used on root pools."),
1096 zpool_vdev_name(hdl
, NULL
, spares
[s
],
1098 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, msg
));
1103 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1104 SPA_VERSION_L2CACHE
&&
1105 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1106 &l2cache
, &nl2cache
) == 0) {
1107 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1108 "upgraded to add cache devices"));
1109 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1112 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1114 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1116 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_ADD
, &zc
) != 0) {
1120 * This can happen if the user has specified the same
1121 * device multiple times. We can't reliably detect this
1122 * until we try to add it and see we already have a
1125 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1126 "one or more vdevs refer to the same device"));
1127 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1132 * This occurrs when one of the devices is below
1133 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1134 * device was the problem device since there's no
1135 * reliable way to determine device size from userland.
1140 zfs_nicenum(SPA_MINDEVSIZE
, buf
, sizeof (buf
));
1142 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1143 "device is less than the minimum "
1146 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1150 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1151 "pool must be upgraded to add these vdevs"));
1152 (void) zfs_error(hdl
, EZFS_BADVERSION
, msg
);
1156 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1157 "root pool can not have multiple vdevs"
1158 " or separate logs"));
1159 (void) zfs_error(hdl
, EZFS_POOL_NOTSUP
, msg
);
1163 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1164 "cache device must be a disk or disk slice"));
1165 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1169 (void) zpool_standard_error(hdl
, errno
, msg
);
1177 zcmd_free_nvlists(&zc
);
1183 * Exports the pool from the system. The caller must ensure that there are no
1184 * mounted datasets in the pool.
1187 zpool_export_common(zpool_handle_t
*zhp
, boolean_t force
, boolean_t hardforce
)
1189 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
1192 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1193 "cannot export '%s'"), zhp
->zpool_name
);
1195 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1196 zc
.zc_cookie
= force
;
1197 zc
.zc_guid
= hardforce
;
1199 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_EXPORT
, &zc
) != 0) {
1202 zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
1203 "use '-f' to override the following errors:\n"
1204 "'%s' has an active shared spare which could be"
1205 " used by other pools once '%s' is exported."),
1206 zhp
->zpool_name
, zhp
->zpool_name
);
1207 return (zfs_error(zhp
->zpool_hdl
, EZFS_ACTIVE_SPARE
,
1210 return (zpool_standard_error_fmt(zhp
->zpool_hdl
, errno
,
1219 zpool_export(zpool_handle_t
*zhp
, boolean_t force
)
1221 return (zpool_export_common(zhp
, force
, B_FALSE
));
1225 zpool_export_force(zpool_handle_t
*zhp
)
1227 return (zpool_export_common(zhp
, B_TRUE
, B_TRUE
));
1231 zpool_rewind_exclaim(libzfs_handle_t
*hdl
, const char *name
, boolean_t dryrun
,
1234 nvlist_t
*nv
= NULL
;
1240 if (!hdl
->libzfs_printerr
|| config
== NULL
)
1243 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0)
1246 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1248 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1250 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1251 strftime(timestr
, 128, "%c", &t
) != 0) {
1253 (void) printf(dgettext(TEXT_DOMAIN
,
1254 "Would be able to return %s "
1255 "to its state as of %s.\n"),
1258 (void) printf(dgettext(TEXT_DOMAIN
,
1259 "Pool %s returned to its state as of %s.\n"),
1263 (void) printf(dgettext(TEXT_DOMAIN
,
1264 "%s approximately %lld "),
1265 dryrun
? "Would discard" : "Discarded",
1266 ((longlong_t
)loss
+ 30) / 60);
1267 (void) printf(dgettext(TEXT_DOMAIN
,
1268 "minutes of transactions.\n"));
1269 } else if (loss
> 0) {
1270 (void) printf(dgettext(TEXT_DOMAIN
,
1271 "%s approximately %lld "),
1272 dryrun
? "Would discard" : "Discarded",
1274 (void) printf(dgettext(TEXT_DOMAIN
,
1275 "seconds of transactions.\n"));
1281 zpool_explain_recover(libzfs_handle_t
*hdl
, const char *name
, int reason
,
1284 nvlist_t
*nv
= NULL
;
1286 uint64_t edata
= UINT64_MAX
;
1291 if (!hdl
->libzfs_printerr
)
1295 (void) printf(dgettext(TEXT_DOMAIN
, "action: "));
1297 (void) printf(dgettext(TEXT_DOMAIN
, "\t"));
1299 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1300 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1301 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1304 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1305 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_DATA_ERRORS
,
1308 (void) printf(dgettext(TEXT_DOMAIN
,
1309 "Recovery is possible, but will result in some data loss.\n"));
1311 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1312 strftime(timestr
, 128, "%c", &t
) != 0) {
1313 (void) printf(dgettext(TEXT_DOMAIN
,
1314 "\tReturning the pool to its state as of %s\n"
1315 "\tshould correct the problem. "),
1318 (void) printf(dgettext(TEXT_DOMAIN
,
1319 "\tReverting the pool to an earlier state "
1320 "should correct the problem.\n\t"));
1324 (void) printf(dgettext(TEXT_DOMAIN
,
1325 "Approximately %lld minutes of data\n"
1326 "\tmust be discarded, irreversibly. "),
1327 ((longlong_t
)loss
+ 30) / 60);
1328 } else if (loss
> 0) {
1329 (void) printf(dgettext(TEXT_DOMAIN
,
1330 "Approximately %lld seconds of data\n"
1331 "\tmust be discarded, irreversibly. "),
1334 if (edata
!= 0 && edata
!= UINT64_MAX
) {
1336 (void) printf(dgettext(TEXT_DOMAIN
,
1337 "After rewind, at least\n"
1338 "\tone persistent user-data error will remain. "));
1340 (void) printf(dgettext(TEXT_DOMAIN
,
1341 "After rewind, several\n"
1342 "\tpersistent user-data errors will remain. "));
1345 (void) printf(dgettext(TEXT_DOMAIN
,
1346 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1347 reason
>= 0 ? "clear" : "import", name
);
1349 (void) printf(dgettext(TEXT_DOMAIN
,
1350 "A scrub of the pool\n"
1351 "\tis strongly recommended after recovery.\n"));
1355 (void) printf(dgettext(TEXT_DOMAIN
,
1356 "Destroy and re-create the pool from\n\ta backup source.\n"));
1360 * zpool_import() is a contracted interface. Should be kept the same
1363 * Applications should use zpool_import_props() to import a pool with
1364 * new properties value to be set.
1367 zpool_import(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1370 nvlist_t
*props
= NULL
;
1373 if (altroot
!= NULL
) {
1374 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
1375 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1376 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1380 if (nvlist_add_string(props
,
1381 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), altroot
) != 0 ||
1382 nvlist_add_string(props
,
1383 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
), "none") != 0) {
1385 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1386 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1391 ret
= zpool_import_props(hdl
, config
, newname
, props
,
1399 print_vdev_tree(libzfs_handle_t
*hdl
, const char *name
, nvlist_t
*nv
,
1405 uint64_t is_log
= 0;
1407 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
,
1411 (void) printf("\t%*s%s%s\n", indent
, "", name
,
1412 is_log
? " [log]" : "");
1414 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1415 &child
, &children
) != 0)
1418 for (c
= 0; c
< children
; c
++) {
1419 vname
= zpool_vdev_name(hdl
, NULL
, child
[c
], B_TRUE
);
1420 print_vdev_tree(hdl
, vname
, child
[c
], indent
+ 2);
1426 * Import the given pool using the known configuration and a list of
1427 * properties to be set. The configuration should have come from
1428 * zpool_find_import(). The 'newname' parameters control whether the pool
1429 * is imported with a different name.
1432 zpool_import_props(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1433 nvlist_t
*props
, int flags
)
1435 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
1436 zpool_rewind_policy_t policy
;
1437 nvlist_t
*nv
= NULL
;
1438 nvlist_t
*nvinfo
= NULL
;
1439 nvlist_t
*missing
= NULL
;
1446 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1449 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1450 "cannot import pool '%s'"), origname
);
1452 if (newname
!= NULL
) {
1453 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
1454 return (zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1455 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1457 thename
= (char *)newname
;
1464 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
1466 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
1469 if ((props
= zpool_valid_proplist(hdl
, origname
,
1470 props
, version
, flags
, errbuf
)) == NULL
) {
1472 } else if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
1478 (void) strlcpy(zc
.zc_name
, thename
, sizeof (zc
.zc_name
));
1480 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1483 if (zcmd_write_conf_nvlist(hdl
, &zc
, config
) != 0) {
1487 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zc
.zc_nvlist_conf_size
* 2) != 0) {
1492 zc
.zc_cookie
= flags
;
1493 while ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_IMPORT
, &zc
)) != 0 &&
1495 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
1496 zcmd_free_nvlists(&zc
);
1503 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nv
);
1504 zpool_get_rewind_policy(config
, &policy
);
1510 * Dry-run failed, but we print out what success
1511 * looks like if we found a best txg
1513 if (policy
.zrp_request
& ZPOOL_TRY_REWIND
) {
1514 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1520 if (newname
== NULL
)
1521 (void) snprintf(desc
, sizeof (desc
),
1522 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1525 (void) snprintf(desc
, sizeof (desc
),
1526 dgettext(TEXT_DOMAIN
, "cannot import '%s' as '%s'"),
1532 * Unsupported version.
1534 (void) zfs_error(hdl
, EZFS_BADVERSION
, desc
);
1538 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, desc
);
1542 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1543 "one or more devices is read only"));
1544 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
1548 if (nv
&& nvlist_lookup_nvlist(nv
,
1549 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1550 nvlist_lookup_nvlist(nvinfo
,
1551 ZPOOL_CONFIG_MISSING_DEVICES
, &missing
) == 0) {
1552 (void) printf(dgettext(TEXT_DOMAIN
,
1553 "The devices below are missing, use "
1554 "'-m' to import the pool anyway:\n"));
1555 print_vdev_tree(hdl
, NULL
, missing
, 2);
1556 (void) printf("\n");
1558 (void) zpool_standard_error(hdl
, error
, desc
);
1562 (void) zpool_standard_error(hdl
, error
, desc
);
1566 (void) zpool_standard_error(hdl
, error
, desc
);
1567 zpool_explain_recover(hdl
,
1568 newname
? origname
: thename
, -error
, nv
);
1575 zpool_handle_t
*zhp
;
1578 * This should never fail, but play it safe anyway.
1580 if (zpool_open_silent(hdl
, thename
, &zhp
) != 0)
1582 else if (zhp
!= NULL
)
1584 if (policy
.zrp_request
&
1585 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
1586 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1587 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0), nv
);
1593 zcmd_free_nvlists(&zc
);
1603 zpool_scan(zpool_handle_t
*zhp
, pool_scan_func_t func
)
1605 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
1607 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1609 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1610 zc
.zc_cookie
= func
;
1612 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_SCAN
, &zc
) == 0 ||
1613 (errno
== ENOENT
&& func
!= POOL_SCAN_NONE
))
1616 if (func
== POOL_SCAN_SCRUB
) {
1617 (void) snprintf(msg
, sizeof (msg
),
1618 dgettext(TEXT_DOMAIN
, "cannot scrub %s"), zc
.zc_name
);
1619 } else if (func
== POOL_SCAN_NONE
) {
1620 (void) snprintf(msg
, sizeof (msg
),
1621 dgettext(TEXT_DOMAIN
, "cannot cancel scrubbing %s"),
1624 assert(!"unexpected result");
1627 if (errno
== EBUSY
) {
1629 pool_scan_stat_t
*ps
= NULL
;
1632 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
1633 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
1634 (void) nvlist_lookup_uint64_array(nvroot
,
1635 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &psc
);
1636 if (ps
&& ps
->pss_func
== POOL_SCAN_SCRUB
)
1637 return (zfs_error(hdl
, EZFS_SCRUBBING
, msg
));
1639 return (zfs_error(hdl
, EZFS_RESILVERING
, msg
));
1640 } else if (errno
== ENOENT
) {
1641 return (zfs_error(hdl
, EZFS_NO_SCRUB
, msg
));
1643 return (zpool_standard_error(hdl
, errno
, msg
));
1648 * Find a vdev that matches the search criteria specified. We use the
1649 * the nvpair name to determine how we should look for the device.
1650 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1651 * spare; but FALSE if its an INUSE spare.
1654 vdev_to_nvlist_iter(nvlist_t
*nv
, nvlist_t
*search
, boolean_t
*avail_spare
,
1655 boolean_t
*l2cache
, boolean_t
*log
)
1662 nvpair_t
*pair
= nvlist_next_nvpair(search
, NULL
);
1664 /* Nothing to look for */
1665 if (search
== NULL
|| pair
== NULL
)
1668 /* Obtain the key we will use to search */
1669 srchkey
= nvpair_name(pair
);
1671 switch (nvpair_type(pair
)) {
1672 case DATA_TYPE_UINT64
:
1673 if (strcmp(srchkey
, ZPOOL_CONFIG_GUID
) == 0) {
1674 uint64_t srchval
, theguid
;
1676 verify(nvpair_value_uint64(pair
, &srchval
) == 0);
1677 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1679 if (theguid
== srchval
)
1684 case DATA_TYPE_STRING
: {
1685 char *srchval
, *val
;
1687 verify(nvpair_value_string(pair
, &srchval
) == 0);
1688 if (nvlist_lookup_string(nv
, srchkey
, &val
) != 0)
1692 * Search for the requested value. Special cases:
1694 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in with a
1695 * partition suffix "1", "-part1", or "p1". The suffix is hidden
1696 * from the user, but included in the string, so this matches around
1698 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1700 * Otherwise, all other searches are simple string compares.
1702 if (strcmp(srchkey
, ZPOOL_CONFIG_PATH
) == 0) {
1703 uint64_t wholedisk
= 0;
1705 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
1708 char buf
[MAXPATHLEN
];
1710 zfs_append_partition(srchval
, buf
, sizeof (buf
));
1711 if (strcmp(val
, buf
) == 0)
1716 } else if (strcmp(srchkey
, ZPOOL_CONFIG_TYPE
) == 0 && val
) {
1717 char *type
, *idx
, *end
, *p
;
1718 uint64_t id
, vdev_id
;
1721 * Determine our vdev type, keeping in mind
1722 * that the srchval is composed of a type and
1723 * vdev id pair (i.e. mirror-4).
1725 if ((type
= strdup(srchval
)) == NULL
)
1728 if ((p
= strrchr(type
, '-')) == NULL
) {
1736 * If the types don't match then keep looking.
1738 if (strncmp(val
, type
, strlen(val
)) != 0) {
1743 verify(strncmp(type
, VDEV_TYPE_RAIDZ
,
1744 strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
1745 strncmp(type
, VDEV_TYPE_MIRROR
,
1746 strlen(VDEV_TYPE_MIRROR
)) == 0);
1747 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
1751 vdev_id
= strtoull(idx
, &end
, 10);
1758 * Now verify that we have the correct vdev id.
1767 if (strcmp(srchval
, val
) == 0)
1776 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1777 &child
, &children
) != 0)
1780 for (c
= 0; c
< children
; c
++) {
1781 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
1782 avail_spare
, l2cache
, NULL
)) != NULL
) {
1784 * The 'is_log' value is only set for the toplevel
1785 * vdev, not the leaf vdevs. So we always lookup the
1786 * log device from the root of the vdev tree (where
1787 * 'log' is non-NULL).
1790 nvlist_lookup_uint64(child
[c
],
1791 ZPOOL_CONFIG_IS_LOG
, &is_log
) == 0 &&
1799 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
1800 &child
, &children
) == 0) {
1801 for (c
= 0; c
< children
; c
++) {
1802 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
1803 avail_spare
, l2cache
, NULL
)) != NULL
) {
1804 *avail_spare
= B_TRUE
;
1810 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
1811 &child
, &children
) == 0) {
1812 for (c
= 0; c
< children
; c
++) {
1813 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
1814 avail_spare
, l2cache
, NULL
)) != NULL
) {
1825 * Given a physical path (minus the "/devices" prefix), find the
1829 zpool_find_vdev_by_physpath(zpool_handle_t
*zhp
, const char *ppath
,
1830 boolean_t
*avail_spare
, boolean_t
*l2cache
, boolean_t
*log
)
1832 nvlist_t
*search
, *nvroot
, *ret
;
1834 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1835 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PHYS_PATH
, ppath
) == 0);
1837 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
1840 *avail_spare
= B_FALSE
;
1844 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
1845 nvlist_free(search
);
1851 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
1854 zpool_vdev_is_interior(const char *name
)
1856 if (strncmp(name
, VDEV_TYPE_RAIDZ
, strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
1857 strncmp(name
, VDEV_TYPE_MIRROR
, strlen(VDEV_TYPE_MIRROR
)) == 0)
1863 zpool_find_vdev(zpool_handle_t
*zhp
, const char *path
, boolean_t
*avail_spare
,
1864 boolean_t
*l2cache
, boolean_t
*log
)
1866 char buf
[MAXPATHLEN
];
1868 nvlist_t
*nvroot
, *search
, *ret
;
1871 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1873 guid
= strtoull(path
, &end
, 10);
1874 if (guid
!= 0 && *end
== '\0') {
1875 verify(nvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
) == 0);
1876 } else if (zpool_vdev_is_interior(path
)) {
1877 verify(nvlist_add_string(search
, ZPOOL_CONFIG_TYPE
, path
) == 0);
1878 } else if (path
[0] != '/') {
1879 if (zfs_resolve_shortname(path
, buf
, sizeof (buf
)) < 0) {
1880 nvlist_free(search
);
1883 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PATH
, buf
) == 0);
1885 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PATH
, path
) == 0);
1888 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
1891 *avail_spare
= B_FALSE
;
1895 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
1896 nvlist_free(search
);
1902 vdev_online(nvlist_t
*nv
)
1906 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
, &ival
) == 0 ||
1907 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
, &ival
) == 0 ||
1908 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
, &ival
) == 0)
1915 * Helper function for zpool_get_physpaths().
1918 vdev_get_one_physpath(nvlist_t
*config
, char *physpath
, size_t physpath_size
,
1919 size_t *bytes_written
)
1921 size_t bytes_left
, pos
, rsz
;
1925 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PHYS_PATH
,
1927 return (EZFS_NODEVICE
);
1929 pos
= *bytes_written
;
1930 bytes_left
= physpath_size
- pos
;
1931 format
= (pos
== 0) ? "%s" : " %s";
1933 rsz
= snprintf(physpath
+ pos
, bytes_left
, format
, tmppath
);
1934 *bytes_written
+= rsz
;
1936 if (rsz
>= bytes_left
) {
1937 /* if physpath was not copied properly, clear it */
1938 if (bytes_left
!= 0) {
1941 return (EZFS_NOSPC
);
1947 vdev_get_physpaths(nvlist_t
*nv
, char *physpath
, size_t phypath_size
,
1948 size_t *rsz
, boolean_t is_spare
)
1953 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
1954 return (EZFS_INVALCONFIG
);
1956 if (strcmp(type
, VDEV_TYPE_DISK
) == 0) {
1958 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
1959 * For a spare vdev, we only want to boot from the active
1964 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
1967 return (EZFS_INVALCONFIG
);
1970 if (vdev_online(nv
)) {
1971 if ((ret
= vdev_get_one_physpath(nv
, physpath
,
1972 phypath_size
, rsz
)) != 0)
1975 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) == 0 ||
1976 strcmp(type
, VDEV_TYPE_REPLACING
) == 0 ||
1977 (is_spare
= (strcmp(type
, VDEV_TYPE_SPARE
) == 0))) {
1982 if (nvlist_lookup_nvlist_array(nv
,
1983 ZPOOL_CONFIG_CHILDREN
, &child
, &count
) != 0)
1984 return (EZFS_INVALCONFIG
);
1986 for (i
= 0; i
< count
; i
++) {
1987 ret
= vdev_get_physpaths(child
[i
], physpath
,
1988 phypath_size
, rsz
, is_spare
);
1989 if (ret
== EZFS_NOSPC
)
1994 return (EZFS_POOL_INVALARG
);
1998 * Get phys_path for a root pool config.
1999 * Return 0 on success; non-zero on failure.
2002 zpool_get_config_physpath(nvlist_t
*config
, char *physpath
, size_t phypath_size
)
2005 nvlist_t
*vdev_root
;
2012 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2014 return (EZFS_INVALCONFIG
);
2016 if (nvlist_lookup_string(vdev_root
, ZPOOL_CONFIG_TYPE
, &type
) != 0 ||
2017 nvlist_lookup_nvlist_array(vdev_root
, ZPOOL_CONFIG_CHILDREN
,
2018 &child
, &count
) != 0)
2019 return (EZFS_INVALCONFIG
);
2022 * root pool can not have EFI labeled disks and can only have
2023 * a single top-level vdev.
2025 if (strcmp(type
, VDEV_TYPE_ROOT
) != 0 || count
!= 1 ||
2026 pool_uses_efi(vdev_root
))
2027 return (EZFS_POOL_INVALARG
);
2029 (void) vdev_get_physpaths(child
[0], physpath
, phypath_size
, &rsz
,
2032 /* No online devices */
2034 return (EZFS_NODEVICE
);
2040 * Get phys_path for a root pool
2041 * Return 0 on success; non-zero on failure.
2044 zpool_get_physpath(zpool_handle_t
*zhp
, char *physpath
, size_t phypath_size
)
2046 return (zpool_get_config_physpath(zhp
->zpool_config
, physpath
,
2051 * If the device has being dynamically expanded then we need to relabel
2052 * the disk to use the new unallocated space.
2055 zpool_relabel_disk(libzfs_handle_t
*hdl
, const char *path
)
2060 if ((fd
= open(path
, O_RDWR
|O_DIRECT
)) < 0) {
2061 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2062 "relabel '%s': unable to open device"), path
);
2063 return (zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
));
2067 * It's possible that we might encounter an error if the device
2068 * does not have any unallocated space left. If so, we simply
2069 * ignore that error and continue on.
2071 error
= efi_use_whole_disk(fd
);
2073 if (error
&& error
!= VT_ENOSPC
) {
2074 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2075 "relabel '%s': unable to read disk capacity"), path
);
2076 return (zfs_error(hdl
, EZFS_NOCAP
, errbuf
));
2082 * Bring the specified vdev online. The 'flags' parameter is a set of the
2083 * ZFS_ONLINE_* flags.
2086 zpool_vdev_online(zpool_handle_t
*zhp
, const char *path
, int flags
,
2087 vdev_state_t
*newstate
)
2089 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2092 boolean_t avail_spare
, l2cache
, islog
;
2093 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2095 if (flags
& ZFS_ONLINE_EXPAND
) {
2096 (void) snprintf(msg
, sizeof (msg
),
2097 dgettext(TEXT_DOMAIN
, "cannot expand %s"), path
);
2099 (void) snprintf(msg
, sizeof (msg
),
2100 dgettext(TEXT_DOMAIN
, "cannot online %s"), path
);
2103 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2104 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2106 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2108 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2111 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2113 if (flags
& ZFS_ONLINE_EXPAND
||
2114 zpool_get_prop_int(zhp
, ZPOOL_PROP_AUTOEXPAND
, NULL
)) {
2115 char *pathname
= NULL
;
2116 uint64_t wholedisk
= 0;
2118 (void) nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_WHOLE_DISK
,
2120 verify(nvlist_lookup_string(tgt
, ZPOOL_CONFIG_PATH
,
2124 * XXX - L2ARC 1.0 devices can't support expansion.
2127 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2128 "cannot expand cache devices"));
2129 return (zfs_error(hdl
, EZFS_VDEVNOTSUP
, msg
));
2133 pathname
+= strlen(DISK_ROOT
) + 1;
2134 (void) zpool_relabel_disk(hdl
, pathname
);
2138 zc
.zc_cookie
= VDEV_STATE_ONLINE
;
2141 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) != 0) {
2142 if (errno
== EINVAL
) {
2143 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "was split "
2144 "from this pool into a new one. Use '%s' "
2145 "instead"), "zpool detach");
2146 return (zfs_error(hdl
, EZFS_POSTSPLIT_ONLINE
, msg
));
2148 return (zpool_standard_error(hdl
, errno
, msg
));
2151 *newstate
= zc
.zc_cookie
;
2156 * Take the specified vdev offline
2159 zpool_vdev_offline(zpool_handle_t
*zhp
, const char *path
, boolean_t istmp
)
2161 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2164 boolean_t avail_spare
, l2cache
;
2165 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2167 (void) snprintf(msg
, sizeof (msg
),
2168 dgettext(TEXT_DOMAIN
, "cannot offline %s"), path
);
2170 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2171 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2173 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2175 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2178 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2180 zc
.zc_cookie
= VDEV_STATE_OFFLINE
;
2181 zc
.zc_obj
= istmp
? ZFS_OFFLINE_TEMPORARY
: 0;
2183 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2190 * There are no other replicas of this device.
2192 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2196 * The log device has unplayed logs
2198 return (zfs_error(hdl
, EZFS_UNPLAYED_LOGS
, msg
));
2201 return (zpool_standard_error(hdl
, errno
, msg
));
2206 * Mark the given vdev faulted.
2209 zpool_vdev_fault(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2211 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2213 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2215 (void) snprintf(msg
, sizeof (msg
),
2216 dgettext(TEXT_DOMAIN
, "cannot fault %llu"), (u_longlong_t
)guid
);
2218 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2220 zc
.zc_cookie
= VDEV_STATE_FAULTED
;
2223 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2230 * There are no other replicas of this device.
2232 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2235 return (zpool_standard_error(hdl
, errno
, msg
));
2241 * Mark the given vdev degraded.
2244 zpool_vdev_degrade(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2246 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2248 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2250 (void) snprintf(msg
, sizeof (msg
),
2251 dgettext(TEXT_DOMAIN
, "cannot degrade %llu"), (u_longlong_t
)guid
);
2253 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2255 zc
.zc_cookie
= VDEV_STATE_DEGRADED
;
2258 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2261 return (zpool_standard_error(hdl
, errno
, msg
));
2265 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2269 is_replacing_spare(nvlist_t
*search
, nvlist_t
*tgt
, int which
)
2275 if (nvlist_lookup_nvlist_array(search
, ZPOOL_CONFIG_CHILDREN
, &child
,
2277 verify(nvlist_lookup_string(search
, ZPOOL_CONFIG_TYPE
,
2280 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0 &&
2281 children
== 2 && child
[which
] == tgt
)
2284 for (c
= 0; c
< children
; c
++)
2285 if (is_replacing_spare(child
[c
], tgt
, which
))
2293 * Attach new_disk (fully described by nvroot) to old_disk.
2294 * If 'replacing' is specified, the new disk will replace the old one.
2297 zpool_vdev_attach(zpool_handle_t
*zhp
,
2298 const char *old_disk
, const char *new_disk
, nvlist_t
*nvroot
, int replacing
)
2300 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2304 boolean_t avail_spare
, l2cache
, islog
;
2309 nvlist_t
*config_root
;
2310 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2311 boolean_t rootpool
= pool_is_bootable(zhp
);
2314 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2315 "cannot replace %s with %s"), old_disk
, new_disk
);
2317 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2318 "cannot attach %s to %s"), new_disk
, old_disk
);
2321 * If this is a root pool, make sure that we're not attaching an
2322 * EFI labeled device.
2324 if (rootpool
&& pool_uses_efi(nvroot
)) {
2325 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2326 "EFI labeled devices are not supported on root pools."));
2327 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, msg
));
2330 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2331 if ((tgt
= zpool_find_vdev(zhp
, old_disk
, &avail_spare
, &l2cache
,
2333 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2336 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2339 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2341 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2342 zc
.zc_cookie
= replacing
;
2344 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
2345 &child
, &children
) != 0 || children
!= 1) {
2346 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2347 "new device must be a single disk"));
2348 return (zfs_error(hdl
, EZFS_INVALCONFIG
, msg
));
2351 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
2352 ZPOOL_CONFIG_VDEV_TREE
, &config_root
) == 0);
2354 if ((newname
= zpool_vdev_name(NULL
, NULL
, child
[0], B_FALSE
)) == NULL
)
2358 * If the target is a hot spare that has been swapped in, we can only
2359 * replace it with another hot spare.
2362 nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_IS_SPARE
, &val
) == 0 &&
2363 (zpool_find_vdev(zhp
, newname
, &avail_spare
, &l2cache
,
2364 NULL
) == NULL
|| !avail_spare
) &&
2365 is_replacing_spare(config_root
, tgt
, 1)) {
2366 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2367 "can only be replaced by another hot spare"));
2369 return (zfs_error(hdl
, EZFS_BADTARGET
, msg
));
2374 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
2377 ret
= zfs_ioctl(hdl
, ZFS_IOC_VDEV_ATTACH
, &zc
);
2379 zcmd_free_nvlists(&zc
);
2384 * XXX need a better way to prevent user from
2385 * booting up a half-baked vdev.
2387 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Make "
2388 "sure to wait until resilver is done "
2389 "before rebooting.\n"));
2397 * Can't attach to or replace this type of vdev.
2400 uint64_t version
= zpool_get_prop_int(zhp
,
2401 ZPOOL_PROP_VERSION
, NULL
);
2404 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2405 "cannot replace a log with a spare"));
2406 else if (version
>= SPA_VERSION_MULTI_REPLACE
)
2407 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2408 "already in replacing/spare config; wait "
2409 "for completion or use 'zpool detach'"));
2411 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2412 "cannot replace a replacing device"));
2414 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2415 "can only attach to mirrors and top-level "
2418 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2423 * The new device must be a single disk.
2425 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2426 "new device must be a single disk"));
2427 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2431 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "%s is busy"),
2433 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2438 * The new device is too small.
2440 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2441 "device is too small"));
2442 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2447 * The new device has a different alignment requirement.
2449 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2450 "devices have different sector alignment"));
2451 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2456 * The resulting top-level vdev spec won't fit in the label.
2458 (void) zfs_error(hdl
, EZFS_DEVOVERFLOW
, msg
);
2462 (void) zpool_standard_error(hdl
, errno
, msg
);
2469 * Detach the specified device.
2472 zpool_vdev_detach(zpool_handle_t
*zhp
, const char *path
)
2474 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2477 boolean_t avail_spare
, l2cache
;
2478 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2480 (void) snprintf(msg
, sizeof (msg
),
2481 dgettext(TEXT_DOMAIN
, "cannot detach %s"), path
);
2483 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2484 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2486 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2489 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2492 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2494 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2496 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_DETACH
, &zc
) == 0)
2503 * Can't detach from this type of vdev.
2505 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "only "
2506 "applicable to mirror and replacing vdevs"));
2507 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2512 * There are no other replicas of this device.
2514 (void) zfs_error(hdl
, EZFS_NOREPLICAS
, msg
);
2518 (void) zpool_standard_error(hdl
, errno
, msg
);
2525 * Find a mirror vdev in the source nvlist.
2527 * The mchild array contains a list of disks in one of the top-level mirrors
2528 * of the source pool. The schild array contains a list of disks that the
2529 * user specified on the command line. We loop over the mchild array to
2530 * see if any entry in the schild array matches.
2532 * If a disk in the mchild array is found in the schild array, we return
2533 * the index of that entry. Otherwise we return -1.
2536 find_vdev_entry(zpool_handle_t
*zhp
, nvlist_t
**mchild
, uint_t mchildren
,
2537 nvlist_t
**schild
, uint_t schildren
)
2541 for (mc
= 0; mc
< mchildren
; mc
++) {
2543 char *mpath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
2544 mchild
[mc
], B_FALSE
);
2546 for (sc
= 0; sc
< schildren
; sc
++) {
2547 char *spath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
2548 schild
[sc
], B_FALSE
);
2549 boolean_t result
= (strcmp(mpath
, spath
) == 0);
2565 * Split a mirror pool. If newroot points to null, then a new nvlist
2566 * is generated and it is the responsibility of the caller to free it.
2569 zpool_vdev_split(zpool_handle_t
*zhp
, char *newname
, nvlist_t
**newroot
,
2570 nvlist_t
*props
, splitflags_t flags
)
2572 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2574 nvlist_t
*tree
, *config
, **child
, **newchild
, *newconfig
= NULL
;
2575 nvlist_t
**varray
= NULL
, *zc_props
= NULL
;
2576 uint_t c
, children
, newchildren
, lastlog
= 0, vcount
, found
= 0;
2577 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2579 boolean_t freelist
= B_FALSE
, memory_err
= B_TRUE
;
2582 (void) snprintf(msg
, sizeof (msg
),
2583 dgettext(TEXT_DOMAIN
, "Unable to split %s"), zhp
->zpool_name
);
2585 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
2586 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
2588 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
2589 (void) fprintf(stderr
, gettext("Internal error: unable to "
2590 "retrieve pool configuration\n"));
2594 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &tree
)
2596 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
, &vers
) == 0);
2599 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
2600 if ((zc_props
= zpool_valid_proplist(hdl
, zhp
->zpool_name
,
2601 props
, vers
, flags
, msg
)) == NULL
)
2605 if (nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
2607 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2608 "Source pool is missing vdev tree"));
2610 nvlist_free(zc_props
);
2614 varray
= zfs_alloc(hdl
, children
* sizeof (nvlist_t
*));
2617 if (*newroot
== NULL
||
2618 nvlist_lookup_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
2619 &newchild
, &newchildren
) != 0)
2622 for (c
= 0; c
< children
; c
++) {
2623 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
2625 nvlist_t
**mchild
, *vdev
;
2630 * Unlike cache & spares, slogs are stored in the
2631 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2633 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2635 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
2637 if (is_log
|| is_hole
) {
2639 * Create a hole vdev and put it in the config.
2641 if (nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) != 0)
2643 if (nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
,
2644 VDEV_TYPE_HOLE
) != 0)
2646 if (nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_HOLE
,
2651 varray
[vcount
++] = vdev
;
2655 verify(nvlist_lookup_string(child
[c
], ZPOOL_CONFIG_TYPE
, &type
)
2657 if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
2658 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2659 "Source pool must be composed only of mirrors\n"));
2660 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2664 verify(nvlist_lookup_nvlist_array(child
[c
],
2665 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
2667 /* find or add an entry for this top-level vdev */
2668 if (newchildren
> 0 &&
2669 (entry
= find_vdev_entry(zhp
, mchild
, mchildren
,
2670 newchild
, newchildren
)) >= 0) {
2671 /* We found a disk that the user specified. */
2672 vdev
= mchild
[entry
];
2675 /* User didn't specify a disk for this vdev. */
2676 vdev
= mchild
[mchildren
- 1];
2679 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
2683 /* did we find every disk the user specified? */
2684 if (found
!= newchildren
) {
2685 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Device list must "
2686 "include at most one disk from each mirror"));
2687 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2691 /* Prepare the nvlist for populating. */
2692 if (*newroot
== NULL
) {
2693 if (nvlist_alloc(newroot
, NV_UNIQUE_NAME
, 0) != 0)
2696 if (nvlist_add_string(*newroot
, ZPOOL_CONFIG_TYPE
,
2697 VDEV_TYPE_ROOT
) != 0)
2700 verify(nvlist_remove_all(*newroot
, ZPOOL_CONFIG_CHILDREN
) == 0);
2703 /* Add all the children we found */
2704 if (nvlist_add_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
, varray
,
2705 lastlog
== 0 ? vcount
: lastlog
) != 0)
2709 * If we're just doing a dry run, exit now with success.
2712 memory_err
= B_FALSE
;
2717 /* now build up the config list & call the ioctl */
2718 if (nvlist_alloc(&newconfig
, NV_UNIQUE_NAME
, 0) != 0)
2721 if (nvlist_add_nvlist(newconfig
,
2722 ZPOOL_CONFIG_VDEV_TREE
, *newroot
) != 0 ||
2723 nvlist_add_string(newconfig
,
2724 ZPOOL_CONFIG_POOL_NAME
, newname
) != 0 ||
2725 nvlist_add_uint64(newconfig
, ZPOOL_CONFIG_VERSION
, vers
) != 0)
2729 * The new pool is automatically part of the namespace unless we
2730 * explicitly export it.
2733 zc
.zc_cookie
= ZPOOL_EXPORT_AFTER_SPLIT
;
2734 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2735 (void) strlcpy(zc
.zc_string
, newname
, sizeof (zc
.zc_string
));
2736 if (zcmd_write_conf_nvlist(hdl
, &zc
, newconfig
) != 0)
2738 if (zc_props
!= NULL
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
2741 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SPLIT
, &zc
) != 0) {
2742 retval
= zpool_standard_error(hdl
, errno
, msg
);
2747 memory_err
= B_FALSE
;
2750 if (varray
!= NULL
) {
2753 for (v
= 0; v
< vcount
; v
++)
2754 nvlist_free(varray
[v
]);
2757 zcmd_free_nvlists(&zc
);
2759 nvlist_free(zc_props
);
2761 nvlist_free(newconfig
);
2763 nvlist_free(*newroot
);
2771 return (no_memory(hdl
));
2777 * Remove the given device. Currently, this is supported only for hot spares
2778 * and level 2 cache devices.
2781 zpool_vdev_remove(zpool_handle_t
*zhp
, const char *path
)
2783 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2786 boolean_t avail_spare
, l2cache
, islog
;
2787 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2790 (void) snprintf(msg
, sizeof (msg
),
2791 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
2793 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2794 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2796 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2798 * XXX - this should just go away.
2800 if (!avail_spare
&& !l2cache
&& !islog
) {
2801 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2802 "only inactive hot spares, cache, top-level, "
2803 "or log devices can be removed"));
2804 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2807 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
2808 if (islog
&& version
< SPA_VERSION_HOLES
) {
2809 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2810 "pool must be upgrade to support log removal"));
2811 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
2814 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2816 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
2819 return (zpool_standard_error(hdl
, errno
, msg
));
2823 * Clear the errors for the pool, or the particular device if specified.
2826 zpool_clear(zpool_handle_t
*zhp
, const char *path
, nvlist_t
*rewindnvl
)
2828 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2831 zpool_rewind_policy_t policy
;
2832 boolean_t avail_spare
, l2cache
;
2833 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2834 nvlist_t
*nvi
= NULL
;
2838 (void) snprintf(msg
, sizeof (msg
),
2839 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
2842 (void) snprintf(msg
, sizeof (msg
),
2843 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
2846 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2848 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
,
2849 &l2cache
, NULL
)) == 0)
2850 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2853 * Don't allow error clearing for hot spares. Do allow
2854 * error clearing for l2cache devices.
2857 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2859 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
,
2863 zpool_get_rewind_policy(rewindnvl
, &policy
);
2864 zc
.zc_cookie
= policy
.zrp_request
;
2866 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zhp
->zpool_config_size
* 2) != 0)
2869 if (zcmd_write_src_nvlist(hdl
, &zc
, rewindnvl
) != 0)
2872 while ((error
= zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
)) != 0 &&
2874 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
2875 zcmd_free_nvlists(&zc
);
2880 if (!error
|| ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) &&
2881 errno
!= EPERM
&& errno
!= EACCES
)) {
2882 if (policy
.zrp_request
&
2883 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
2884 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nvi
);
2885 zpool_rewind_exclaim(hdl
, zc
.zc_name
,
2886 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0),
2890 zcmd_free_nvlists(&zc
);
2894 zcmd_free_nvlists(&zc
);
2895 return (zpool_standard_error(hdl
, errno
, msg
));
2899 * Similar to zpool_clear(), but takes a GUID (used by fmd).
2902 zpool_vdev_clear(zpool_handle_t
*zhp
, uint64_t guid
)
2904 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2906 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2908 (void) snprintf(msg
, sizeof (msg
),
2909 dgettext(TEXT_DOMAIN
, "cannot clear errors for %llx"),
2910 (u_longlong_t
)guid
);
2912 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2914 zc
.zc_cookie
= ZPOOL_NO_REWIND
;
2916 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_CLEAR
, &zc
) == 0)
2919 return (zpool_standard_error(hdl
, errno
, msg
));
2923 * Convert from a devid string to a path.
2926 devid_to_path(char *devid_str
)
2931 devid_nmlist_t
*list
= NULL
;
2934 if (devid_str_decode(devid_str
, &devid
, &minor
) != 0)
2937 ret
= devid_deviceid_to_nmlist("/dev", devid
, minor
, &list
);
2939 devid_str_free(minor
);
2945 if ((path
= strdup(list
[0].devname
)) == NULL
)
2948 devid_free_nmlist(list
);
2954 * Convert from a path to a devid string.
2957 path_to_devid(const char *path
)
2963 if ((fd
= open(path
, O_RDONLY
)) < 0)
2968 if (devid_get(fd
, &devid
) == 0) {
2969 if (devid_get_minor_name(fd
, &minor
) == 0)
2970 ret
= devid_str_encode(devid
, minor
);
2972 devid_str_free(minor
);
2981 * Issue the necessary ioctl() to update the stored path value for the vdev. We
2982 * ignore any failure here, since a common case is for an unprivileged user to
2983 * type 'zpool status', and we'll display the correct information anyway.
2986 set_path(zpool_handle_t
*zhp
, nvlist_t
*nv
, const char *path
)
2988 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2990 (void) strncpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2991 (void) strncpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
2992 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
2995 (void) ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_VDEV_SETPATH
, &zc
);
2999 * Remove partition suffix from a vdev path. Partition suffixes may take three
3000 * forms: "-partX", "pX", or "X", where X is a string of digits. The second
3001 * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
3002 * third case only occurs when preceded by a string matching the regular
3003 * expression "^[hs]d[a-z]+", i.e. a scsi or ide disk.
3006 strip_partition(libzfs_handle_t
*hdl
, char *path
)
3008 char *tmp
= zfs_strdup(hdl
, path
);
3009 char *part
= NULL
, *d
= NULL
;
3011 if ((part
= strstr(tmp
, "-part")) && part
!= tmp
) {
3013 } else if ((part
= strrchr(tmp
, 'p')) &&
3014 part
> tmp
+ 1 && isdigit(*(part
-1))) {
3016 } else if ((tmp
[0] == 'h' || tmp
[0] == 's') && tmp
[1] == 'd') {
3017 for (d
= &tmp
[2]; isalpha(*d
); part
= ++d
);
3019 if (part
&& d
&& *d
!= '\0') {
3020 for (; isdigit(*d
); d
++);
3027 #define PATH_BUF_LEN 64
3030 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3031 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3032 * We also check if this is a whole disk, in which case we strip off the
3033 * trailing 's0' slice name.
3035 * This routine is also responsible for identifying when disks have been
3036 * reconfigured in a new location. The kernel will have opened the device by
3037 * devid, but the path will still refer to the old location. To catch this, we
3038 * first do a path -> devid translation (which is fast for the common case). If
3039 * the devid matches, we're done. If not, we do a reverse devid -> path
3040 * translation and issue the appropriate ioctl() to update the path of the vdev.
3041 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3045 zpool_vdev_name(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, nvlist_t
*nv
,
3048 char *path
, *devid
, *type
;
3050 char buf
[PATH_BUF_LEN
];
3054 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
,
3056 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
3058 (void) snprintf(buf
, sizeof (buf
), "%llu",
3059 (u_longlong_t
)value
);
3061 } else if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
3063 * If the device is dead (faulted, offline, etc) then don't
3064 * bother opening it. Otherwise we may be forcing the user to
3065 * open a misbehaving device, which can have undesirable
3068 if ((nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
3069 (uint64_t **)&vs
, &vsc
) != 0 ||
3070 vs
->vs_state
>= VDEV_STATE_DEGRADED
) &&
3072 nvlist_lookup_string(nv
, ZPOOL_CONFIG_DEVID
, &devid
) == 0) {
3074 * Determine if the current path is correct.
3076 char *newdevid
= path_to_devid(path
);
3078 if (newdevid
== NULL
||
3079 strcmp(devid
, newdevid
) != 0) {
3082 if ((newpath
= devid_to_path(devid
)) != NULL
) {
3084 * Update the path appropriately.
3086 set_path(zhp
, nv
, newpath
);
3087 if (nvlist_add_string(nv
,
3088 ZPOOL_CONFIG_PATH
, newpath
) == 0)
3089 verify(nvlist_lookup_string(nv
,
3097 devid_str_free(newdevid
);
3101 * For a block device only use the name.
3103 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
3104 if (strcmp(type
, VDEV_TYPE_DISK
) == 0) {
3105 path
= strrchr(path
, '/');
3110 * Remove the partition from the path it this is a whole disk.
3112 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
3113 &value
) == 0 && value
) {
3114 return strip_partition(hdl
, path
);
3117 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &path
) == 0);
3120 * If it's a raidz device, we need to stick in the parity level.
3122 if (strcmp(path
, VDEV_TYPE_RAIDZ
) == 0) {
3123 char tmpbuf
[PATH_BUF_LEN
];
3125 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
,
3127 (void) snprintf(tmpbuf
, sizeof (tmpbuf
), "%s%llu", path
,
3128 (u_longlong_t
)value
);
3133 * We identify each top-level vdev by using a <type-id>
3134 * naming convention.
3139 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
3141 (void) snprintf(buf
, sizeof (buf
), "%s-%llu", path
,
3147 return (zfs_strdup(hdl
, path
));
3151 zbookmark_compare(const void *a
, const void *b
)
3153 return (memcmp(a
, b
, sizeof (zbookmark_t
)));
3157 * Retrieve the persistent error log, uniquify the members, and return to the
3161 zpool_get_errlog(zpool_handle_t
*zhp
, nvlist_t
**nverrlistp
)
3163 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3165 zbookmark_t
*zb
= NULL
;
3169 * Retrieve the raw error list from the kernel. If the number of errors
3170 * has increased, allocate more space and continue until we get the
3173 verify(nvlist_lookup_uint64(zhp
->zpool_config
, ZPOOL_CONFIG_ERRCOUNT
,
3177 if ((zc
.zc_nvlist_dst
= (uintptr_t)zfs_alloc(zhp
->zpool_hdl
,
3178 count
* sizeof (zbookmark_t
))) == (uintptr_t)NULL
)
3180 zc
.zc_nvlist_dst_size
= count
;
3181 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3183 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_ERROR_LOG
,
3185 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3186 if (errno
== ENOMEM
) {
3187 count
= zc
.zc_nvlist_dst_size
;
3188 if ((zc
.zc_nvlist_dst
= (uintptr_t)
3189 zfs_alloc(zhp
->zpool_hdl
, count
*
3190 sizeof (zbookmark_t
))) == (uintptr_t)NULL
)
3201 * Sort the resulting bookmarks. This is a little confusing due to the
3202 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3203 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3204 * _not_ copied as part of the process. So we point the start of our
3205 * array appropriate and decrement the total number of elements.
3207 zb
= ((zbookmark_t
*)(uintptr_t)zc
.zc_nvlist_dst
) +
3208 zc
.zc_nvlist_dst_size
;
3209 count
-= zc
.zc_nvlist_dst_size
;
3211 qsort(zb
, count
, sizeof (zbookmark_t
), zbookmark_compare
);
3213 verify(nvlist_alloc(nverrlistp
, 0, KM_SLEEP
) == 0);
3216 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3218 for (i
= 0; i
< count
; i
++) {
3221 /* ignoring zb_blkid and zb_level for now */
3222 if (i
> 0 && zb
[i
-1].zb_objset
== zb
[i
].zb_objset
&&
3223 zb
[i
-1].zb_object
== zb
[i
].zb_object
)
3226 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) != 0)
3228 if (nvlist_add_uint64(nv
, ZPOOL_ERR_DATASET
,
3229 zb
[i
].zb_objset
) != 0) {
3233 if (nvlist_add_uint64(nv
, ZPOOL_ERR_OBJECT
,
3234 zb
[i
].zb_object
) != 0) {
3238 if (nvlist_add_nvlist(*nverrlistp
, "ejk", nv
) != 0) {
3245 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3249 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3250 return (no_memory(zhp
->zpool_hdl
));
3254 * Upgrade a ZFS pool to the latest on-disk version.
3257 zpool_upgrade(zpool_handle_t
*zhp
, uint64_t new_version
)
3259 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3260 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3262 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3263 zc
.zc_cookie
= new_version
;
3265 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_UPGRADE
, &zc
) != 0)
3266 return (zpool_standard_error_fmt(hdl
, errno
,
3267 dgettext(TEXT_DOMAIN
, "cannot upgrade '%s'"),
3273 zpool_set_history_str(const char *subcommand
, int argc
, char **argv
,
3278 (void) strlcpy(history_str
, subcommand
, HIS_MAX_RECORD_LEN
);
3279 for (i
= 1; i
< argc
; i
++) {
3280 if (strlen(history_str
) + 1 + strlen(argv
[i
]) >
3283 (void) strlcat(history_str
, " ", HIS_MAX_RECORD_LEN
);
3284 (void) strlcat(history_str
, argv
[i
], HIS_MAX_RECORD_LEN
);
3289 * Stage command history for logging.
3292 zpool_stage_history(libzfs_handle_t
*hdl
, const char *history_str
)
3294 if (history_str
== NULL
)
3297 if (strlen(history_str
) > HIS_MAX_RECORD_LEN
)
3300 if (hdl
->libzfs_log_str
!= NULL
)
3301 free(hdl
->libzfs_log_str
);
3303 if ((hdl
->libzfs_log_str
= strdup(history_str
)) == NULL
)
3304 return (no_memory(hdl
));
3310 * Perform ioctl to get some command history of a pool.
3312 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3313 * logical offset of the history buffer to start reading from.
3315 * Upon return, 'off' is the next logical offset to read from and
3316 * 'len' is the actual amount of bytes read into 'buf'.
3319 get_history(zpool_handle_t
*zhp
, char *buf
, uint64_t *off
, uint64_t *len
)
3321 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3322 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3324 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3326 zc
.zc_history
= (uint64_t)(uintptr_t)buf
;
3327 zc
.zc_history_len
= *len
;
3328 zc
.zc_history_offset
= *off
;
3330 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_HISTORY
, &zc
) != 0) {
3333 return (zfs_error_fmt(hdl
, EZFS_PERM
,
3334 dgettext(TEXT_DOMAIN
,
3335 "cannot show history for pool '%s'"),
3338 return (zfs_error_fmt(hdl
, EZFS_NOHISTORY
,
3339 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
3340 "'%s'"), zhp
->zpool_name
));
3342 return (zfs_error_fmt(hdl
, EZFS_BADVERSION
,
3343 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
3344 "'%s', pool must be upgraded"), zhp
->zpool_name
));
3346 return (zpool_standard_error_fmt(hdl
, errno
,
3347 dgettext(TEXT_DOMAIN
,
3348 "cannot get history for '%s'"), zhp
->zpool_name
));
3352 *len
= zc
.zc_history_len
;
3353 *off
= zc
.zc_history_offset
;
3359 * Process the buffer of nvlists, unpacking and storing each nvlist record
3360 * into 'records'. 'leftover' is set to the number of bytes that weren't
3361 * processed as there wasn't a complete record.
3364 zpool_history_unpack(char *buf
, uint64_t bytes_read
, uint64_t *leftover
,
3365 nvlist_t
***records
, uint_t
*numrecords
)
3371 while (bytes_read
> sizeof (reclen
)) {
3373 /* get length of packed record (stored as little endian) */
3374 for (i
= 0, reclen
= 0; i
< sizeof (reclen
); i
++)
3375 reclen
+= (uint64_t)(((uchar_t
*)buf
)[i
]) << (8*i
);
3377 if (bytes_read
< sizeof (reclen
) + reclen
)
3381 if (nvlist_unpack(buf
+ sizeof (reclen
), reclen
, &nv
, 0) != 0)
3383 bytes_read
-= sizeof (reclen
) + reclen
;
3384 buf
+= sizeof (reclen
) + reclen
;
3386 /* add record to nvlist array */
3388 if (ISP2(*numrecords
+ 1)) {
3389 *records
= realloc(*records
,
3390 *numrecords
* 2 * sizeof (nvlist_t
*));
3392 (*records
)[*numrecords
- 1] = nv
;
3395 *leftover
= bytes_read
;
3399 #define HIS_BUF_LEN (128*1024)
3402 * Retrieve the command history of a pool.
3405 zpool_get_history(zpool_handle_t
*zhp
, nvlist_t
**nvhisp
)
3407 char buf
[HIS_BUF_LEN
];
3409 nvlist_t
**records
= NULL
;
3410 uint_t numrecords
= 0;
3414 uint64_t bytes_read
= sizeof (buf
);
3417 if ((err
= get_history(zhp
, buf
, &off
, &bytes_read
)) != 0)
3420 /* if nothing else was read in, we're at EOF, just return */
3424 if ((err
= zpool_history_unpack(buf
, bytes_read
,
3425 &leftover
, &records
, &numrecords
)) != 0)
3433 verify(nvlist_alloc(nvhisp
, NV_UNIQUE_NAME
, 0) == 0);
3434 verify(nvlist_add_nvlist_array(*nvhisp
, ZPOOL_HIST_RECORD
,
3435 records
, numrecords
) == 0);
3437 for (i
= 0; i
< numrecords
; i
++)
3438 nvlist_free(records
[i
]);
3445 * Retrieve the next event. If there is a new event available 'nvp' will
3446 * contain a newly allocated nvlist and 'dropped' will be set to the number
3447 * of missed events since the last call to this function. When 'nvp' is
3448 * set to NULL it indicates no new events are available. In either case
3449 * the function returns 0 and it is up to the caller to free 'nvp'. In
3450 * the case of a fatal error the function will return a non-zero value.
3451 * When the function is called in blocking mode it will not return until
3452 * a new event is available.
3455 zpool_events_next(libzfs_handle_t
*hdl
, nvlist_t
**nvp
,
3456 int *dropped
, int block
, int cleanup_fd
)
3458 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3463 zc
.zc_cleanup_fd
= cleanup_fd
;
3466 zc
.zc_guid
= ZEVENT_NONBLOCK
;
3468 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, ZEVENT_SIZE
) != 0)
3472 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_NEXT
, &zc
) != 0) {
3475 error
= zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
3476 dgettext(TEXT_DOMAIN
, "zfs shutdown"));
3479 /* Blocking error case should not occur */
3481 error
= zpool_standard_error_fmt(hdl
, errno
,
3482 dgettext(TEXT_DOMAIN
, "cannot get event"));
3486 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
3487 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
3488 dgettext(TEXT_DOMAIN
, "cannot get event"));
3494 error
= zpool_standard_error_fmt(hdl
, errno
,
3495 dgettext(TEXT_DOMAIN
, "cannot get event"));
3500 error
= zcmd_read_dst_nvlist(hdl
, &zc
, nvp
);
3504 *dropped
= (int)zc
.zc_cookie
;
3506 zcmd_free_nvlists(&zc
);
3515 zpool_events_clear(libzfs_handle_t
*hdl
, int *count
)
3517 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3520 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
3521 "cannot clear events"));
3523 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_CLEAR
, &zc
) != 0)
3524 return (zpool_standard_error_fmt(hdl
, errno
, msg
));
3527 *count
= (int)zc
.zc_cookie
; /* # of events cleared */
3533 zpool_obj_to_path(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
3534 char *pathname
, size_t len
)
3536 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
3537 boolean_t mounted
= B_FALSE
;
3538 char *mntpnt
= NULL
;
3539 char dsname
[MAXNAMELEN
];
3542 /* special case for the MOS */
3543 (void) snprintf(pathname
, len
, "<metadata>:<0x%llx>", (longlong_t
)obj
);
3547 /* get the dataset's name */
3548 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3550 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
,
3551 ZFS_IOC_DSOBJ_TO_DSNAME
, &zc
) != 0) {
3552 /* just write out a path of two object numbers */
3553 (void) snprintf(pathname
, len
, "<0x%llx>:<0x%llx>",
3554 (longlong_t
)dsobj
, (longlong_t
)obj
);
3557 (void) strlcpy(dsname
, zc
.zc_value
, sizeof (dsname
));
3559 /* find out if the dataset is mounted */
3560 mounted
= is_mounted(zhp
->zpool_hdl
, dsname
, &mntpnt
);
3562 /* get the corrupted object's path */
3563 (void) strlcpy(zc
.zc_name
, dsname
, sizeof (zc
.zc_name
));
3565 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_OBJ_TO_PATH
,
3568 (void) snprintf(pathname
, len
, "%s%s", mntpnt
,
3571 (void) snprintf(pathname
, len
, "%s:%s",
3572 dsname
, zc
.zc_value
);
3575 (void) snprintf(pathname
, len
, "%s:<0x%llx>", dsname
, (longlong_t
)obj
);
3581 * Read the EFI label from the config, if a label does not exist then
3582 * pass back the error to the caller. If the caller has passed a non-NULL
3583 * diskaddr argument then we set it to the starting address of the EFI
3587 read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
)
3591 char diskname
[MAXPATHLEN
];
3594 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PATH
, &path
) != 0)
3597 (void) snprintf(diskname
, sizeof (diskname
), "%s%s", RDISK_ROOT
,
3598 strrchr(path
, '/'));
3599 if ((fd
= open(diskname
, O_RDWR
|O_DIRECT
)) >= 0) {
3600 struct dk_gpt
*vtoc
;
3602 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) >= 0) {
3604 *sb
= vtoc
->efi_parts
[0].p_start
;
3613 * determine where a partition starts on a disk in the current
3617 find_start_block(nvlist_t
*config
)
3621 diskaddr_t sb
= MAXOFFSET_T
;
3624 if (nvlist_lookup_nvlist_array(config
,
3625 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) != 0) {
3626 if (nvlist_lookup_uint64(config
,
3627 ZPOOL_CONFIG_WHOLE_DISK
,
3628 &wholedisk
) != 0 || !wholedisk
) {
3629 return (MAXOFFSET_T
);
3631 if (read_efi_label(config
, &sb
) < 0)
3636 for (c
= 0; c
< children
; c
++) {
3637 sb
= find_start_block(child
[c
]);
3638 if (sb
!= MAXOFFSET_T
) {
3642 return (MAXOFFSET_T
);
3646 zpool_label_disk_wait(char *path
, int timeout
)
3648 struct stat64 statbuf
;
3652 * Wait timeout miliseconds for a newly created device to be available
3653 * from the given path. There is a small window when a /dev/ device
3654 * will exist and the udev link will not, so we must wait for the
3655 * symlink. Depending on the udev rules this may take a few seconds.
3657 for (i
= 0; i
< timeout
; i
++) {
3661 if ((stat64(path
, &statbuf
) == 0) && (errno
== 0))
3669 zpool_label_disk_check(char *path
)
3671 struct dk_gpt
*vtoc
;
3674 if ((fd
= open(path
, O_RDWR
|O_DIRECT
)) < 0)
3677 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) != 0) {
3682 if (vtoc
->efi_flags
& EFI_GPT_PRIMARY_CORRUPT
) {
3694 * Label an individual disk. The name provided is the short name,
3695 * stripped of any leading /dev path.
3698 zpool_label_disk(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, char *name
)
3700 char path
[MAXPATHLEN
];
3701 struct dk_gpt
*vtoc
;
3703 size_t resv
= EFI_MIN_RESV_SIZE
;
3704 uint64_t slice_size
;
3705 diskaddr_t start_block
;
3708 /* prepare an error message just in case */
3709 (void) snprintf(errbuf
, sizeof (errbuf
),
3710 dgettext(TEXT_DOMAIN
, "cannot label '%s'"), name
);
3715 if (pool_is_bootable(zhp
)) {
3716 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3717 "EFI labeled devices are not supported on root "
3719 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, errbuf
));
3722 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
3723 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3725 if (zhp
->zpool_start_block
== 0)
3726 start_block
= find_start_block(nvroot
);
3728 start_block
= zhp
->zpool_start_block
;
3729 zhp
->zpool_start_block
= start_block
;
3732 start_block
= NEW_START_BLOCK
;
3735 (void) snprintf(path
, sizeof (path
), "%s/%s%s", RDISK_ROOT
, name
,
3738 if ((fd
= open(path
, O_RDWR
|O_DIRECT
)) < 0) {
3740 * This shouldn't happen. We've long since verified that this
3741 * is a valid device.
3743 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3744 "unable to open device '%s': %d"), path
, errno
);
3745 return (zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
));
3748 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, &vtoc
) != 0) {
3750 * The only way this can fail is if we run out of memory, or we
3751 * were unable to read the disk's capacity
3753 if (errno
== ENOMEM
)
3754 (void) no_memory(hdl
);
3757 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3758 "unable to read disk capacity"), name
);
3760 return (zfs_error(hdl
, EZFS_NOCAP
, errbuf
));
3763 slice_size
= vtoc
->efi_last_u_lba
+ 1;
3764 slice_size
-= EFI_MIN_RESV_SIZE
;
3765 if (start_block
== MAXOFFSET_T
)
3766 start_block
= NEW_START_BLOCK
;
3767 slice_size
-= start_block
;
3769 vtoc
->efi_parts
[0].p_start
= start_block
;
3770 vtoc
->efi_parts
[0].p_size
= slice_size
;
3773 * Why we use V_USR: V_BACKUP confuses users, and is considered
3774 * disposable by some EFI utilities (since EFI doesn't have a backup
3775 * slice). V_UNASSIGNED is supposed to be used only for zero size
3776 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
3777 * etc. were all pretty specific. V_USR is as close to reality as we
3778 * can get, in the absence of V_OTHER.
3780 vtoc
->efi_parts
[0].p_tag
= V_USR
;
3781 (void) strcpy(vtoc
->efi_parts
[0].p_name
, "zfs");
3783 vtoc
->efi_parts
[8].p_start
= slice_size
+ start_block
;
3784 vtoc
->efi_parts
[8].p_size
= resv
;
3785 vtoc
->efi_parts
[8].p_tag
= V_RESERVED
;
3787 if ((rval
= efi_write(fd
, vtoc
)) != 0) {
3789 * Some block drivers (like pcata) may not support EFI
3790 * GPT labels. Print out a helpful error message dir-
3791 * ecting the user to manually label the disk and give
3797 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "try using "
3798 "parted(8) and then provide a specific slice: %d"), rval
);
3799 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
3805 /* Wait for the first expected slice to appear. */
3806 (void) snprintf(path
, sizeof (path
), "%s/%s%s%s", DISK_ROOT
, name
,
3807 isdigit(name
[strlen(name
)-1]) ? "p" : "", FIRST_SLICE
);
3808 rval
= zpool_label_disk_wait(path
, 3000);
3810 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "failed to "
3811 "detect device partitions on '%s': %d"), path
, rval
);
3812 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
3815 /* We can't be to paranoid. Read the label back and verify it. */
3816 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
3817 rval
= zpool_label_disk_check(path
);
3819 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "freshly written "
3820 "EFI label on '%s' is damaged. Ensure\nthis device "
3821 "is not in in use, and is functioning properly: %d"),
3823 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));