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, 2014 by Delphix. All rights reserved.
40 #include <sys/efi_partition.h>
42 #include <sys/zfs_ioctl.h>
45 #include "zfs_namecheck.h"
47 #include "libzfs_impl.h"
48 #include "zfs_comutil.h"
49 #include "zfeature_common.h"
51 static int read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
);
53 typedef struct prop_flags
{
54 int create
:1; /* Validate property on creation */
55 int import
:1; /* Validate property on import */
59 * ====================================================================
60 * zpool property functions
61 * ====================================================================
65 zpool_get_all_props(zpool_handle_t
*zhp
)
67 zfs_cmd_t zc
= {"\0"};
68 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
70 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
72 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
75 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_PROPS
, &zc
) != 0) {
76 if (errno
== ENOMEM
) {
77 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
78 zcmd_free_nvlists(&zc
);
82 zcmd_free_nvlists(&zc
);
87 if (zcmd_read_dst_nvlist(hdl
, &zc
, &zhp
->zpool_props
) != 0) {
88 zcmd_free_nvlists(&zc
);
92 zcmd_free_nvlists(&zc
);
98 zpool_props_refresh(zpool_handle_t
*zhp
)
102 old_props
= zhp
->zpool_props
;
104 if (zpool_get_all_props(zhp
) != 0)
107 nvlist_free(old_props
);
112 zpool_get_prop_string(zpool_handle_t
*zhp
, zpool_prop_t prop
,
118 zprop_source_t source
;
120 nvl
= zhp
->zpool_props
;
121 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
122 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &ival
) == 0);
124 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
126 source
= ZPROP_SRC_DEFAULT
;
127 if ((value
= (char *)zpool_prop_default_string(prop
)) == NULL
)
138 zpool_get_prop_int(zpool_handle_t
*zhp
, zpool_prop_t prop
, zprop_source_t
*src
)
142 zprop_source_t source
;
144 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
)) {
146 * zpool_get_all_props() has most likely failed because
147 * the pool is faulted, but if all we need is the top level
148 * vdev's guid then get it from the zhp config nvlist.
150 if ((prop
== ZPOOL_PROP_GUID
) &&
151 (nvlist_lookup_nvlist(zhp
->zpool_config
,
152 ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0) &&
153 (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
)
157 return (zpool_prop_default_numeric(prop
));
160 nvl
= zhp
->zpool_props
;
161 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
162 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &value
) == 0);
164 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
166 source
= ZPROP_SRC_DEFAULT
;
167 value
= zpool_prop_default_numeric(prop
);
177 * Map VDEV STATE to printed strings.
180 zpool_state_to_name(vdev_state_t state
, vdev_aux_t aux
)
185 case VDEV_STATE_CLOSED
:
186 case VDEV_STATE_OFFLINE
:
187 return (gettext("OFFLINE"));
188 case VDEV_STATE_REMOVED
:
189 return (gettext("REMOVED"));
190 case VDEV_STATE_CANT_OPEN
:
191 if (aux
== VDEV_AUX_CORRUPT_DATA
|| aux
== VDEV_AUX_BAD_LOG
)
192 return (gettext("FAULTED"));
193 else if (aux
== VDEV_AUX_SPLIT_POOL
)
194 return (gettext("SPLIT"));
196 return (gettext("UNAVAIL"));
197 case VDEV_STATE_FAULTED
:
198 return (gettext("FAULTED"));
199 case VDEV_STATE_DEGRADED
:
200 return (gettext("DEGRADED"));
201 case VDEV_STATE_HEALTHY
:
202 return (gettext("ONLINE"));
205 return (gettext("UNKNOWN"));
209 * Map POOL STATE to printed strings.
212 zpool_pool_state_to_name(pool_state_t state
)
217 case POOL_STATE_ACTIVE
:
218 return (gettext("ACTIVE"));
219 case POOL_STATE_EXPORTED
:
220 return (gettext("EXPORTED"));
221 case POOL_STATE_DESTROYED
:
222 return (gettext("DESTROYED"));
223 case POOL_STATE_SPARE
:
224 return (gettext("SPARE"));
225 case POOL_STATE_L2CACHE
:
226 return (gettext("L2CACHE"));
227 case POOL_STATE_UNINITIALIZED
:
228 return (gettext("UNINITIALIZED"));
229 case POOL_STATE_UNAVAIL
:
230 return (gettext("UNAVAIL"));
231 case POOL_STATE_POTENTIALLY_ACTIVE
:
232 return (gettext("POTENTIALLY_ACTIVE"));
235 return (gettext("UNKNOWN"));
239 * Get a zpool property value for 'prop' and return the value in
240 * a pre-allocated buffer.
243 zpool_get_prop(zpool_handle_t
*zhp
, zpool_prop_t prop
, char *buf
,
244 size_t len
, zprop_source_t
*srctype
, boolean_t literal
)
248 zprop_source_t src
= ZPROP_SRC_NONE
;
253 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
255 case ZPOOL_PROP_NAME
:
256 (void) strlcpy(buf
, zpool_get_name(zhp
), len
);
259 case ZPOOL_PROP_HEALTH
:
260 (void) strlcpy(buf
, "FAULTED", len
);
263 case ZPOOL_PROP_GUID
:
264 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
265 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
268 case ZPOOL_PROP_ALTROOT
:
269 case ZPOOL_PROP_CACHEFILE
:
270 case ZPOOL_PROP_COMMENT
:
271 if (zhp
->zpool_props
!= NULL
||
272 zpool_get_all_props(zhp
) == 0) {
274 zpool_get_prop_string(zhp
, prop
, &src
),
280 (void) strlcpy(buf
, "-", len
);
289 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
) &&
290 prop
!= ZPOOL_PROP_NAME
)
293 switch (zpool_prop_get_type(prop
)) {
294 case PROP_TYPE_STRING
:
295 (void) strlcpy(buf
, zpool_get_prop_string(zhp
, prop
, &src
),
299 case PROP_TYPE_NUMBER
:
300 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
303 case ZPOOL_PROP_SIZE
:
304 case ZPOOL_PROP_ALLOCATED
:
305 case ZPOOL_PROP_FREE
:
306 case ZPOOL_PROP_FREEING
:
307 case ZPOOL_PROP_LEAKED
:
308 case ZPOOL_PROP_ASHIFT
:
310 (void) snprintf(buf
, len
, "%llu",
311 (u_longlong_t
)intval
);
313 (void) zfs_nicenum(intval
, buf
, len
);
316 case ZPOOL_PROP_EXPANDSZ
:
318 (void) strlcpy(buf
, "-", len
);
319 } else if (literal
) {
320 (void) snprintf(buf
, len
, "%llu",
321 (u_longlong_t
)intval
);
323 (void) zfs_nicenum(intval
, buf
, len
);
327 case ZPOOL_PROP_CAPACITY
:
329 (void) snprintf(buf
, len
, "%llu",
330 (u_longlong_t
)intval
);
332 (void) snprintf(buf
, len
, "%llu%%",
333 (u_longlong_t
)intval
);
337 case ZPOOL_PROP_FRAGMENTATION
:
338 if (intval
== UINT64_MAX
) {
339 (void) strlcpy(buf
, "-", len
);
340 } else if (literal
) {
341 (void) snprintf(buf
, len
, "%llu",
342 (u_longlong_t
)intval
);
344 (void) snprintf(buf
, len
, "%llu%%",
345 (u_longlong_t
)intval
);
349 case ZPOOL_PROP_DEDUPRATIO
:
351 (void) snprintf(buf
, len
, "%llu.%02llu",
352 (u_longlong_t
)(intval
/ 100),
353 (u_longlong_t
)(intval
% 100));
355 (void) snprintf(buf
, len
, "%llu.%02llux",
356 (u_longlong_t
)(intval
/ 100),
357 (u_longlong_t
)(intval
% 100));
360 case ZPOOL_PROP_HEALTH
:
361 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
362 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
363 verify(nvlist_lookup_uint64_array(nvroot
,
364 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
367 (void) strlcpy(buf
, zpool_state_to_name(intval
,
370 case ZPOOL_PROP_VERSION
:
371 if (intval
>= SPA_VERSION_FEATURES
) {
372 (void) snprintf(buf
, len
, "-");
377 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
381 case PROP_TYPE_INDEX
:
382 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
383 if (zpool_prop_index_to_string(prop
, intval
, &strval
)
386 (void) strlcpy(buf
, strval
, len
);
400 * Check if the bootfs name has the same pool name as it is set to.
401 * Assuming bootfs is a valid dataset name.
404 bootfs_name_valid(const char *pool
, char *bootfs
)
406 int len
= strlen(pool
);
408 if (!zfs_name_valid(bootfs
, ZFS_TYPE_FILESYSTEM
|ZFS_TYPE_SNAPSHOT
))
411 if (strncmp(pool
, bootfs
, len
) == 0 &&
412 (bootfs
[len
] == '/' || bootfs
[len
] == '\0'))
418 #if defined(__sun__) || defined(__sun)
420 * Inspect the configuration to determine if any of the devices contain
424 pool_uses_efi(nvlist_t
*config
)
429 if (nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_CHILDREN
,
430 &child
, &children
) != 0)
431 return (read_efi_label(config
, NULL
) >= 0);
433 for (c
= 0; c
< children
; c
++) {
434 if (pool_uses_efi(child
[c
]))
442 zpool_is_bootable(zpool_handle_t
*zhp
)
444 char bootfs
[ZFS_MAX_DATASET_NAME_LEN
];
446 return (zpool_get_prop(zhp
, ZPOOL_PROP_BOOTFS
, bootfs
,
447 sizeof (bootfs
), NULL
, B_FALSE
) == 0 && strncmp(bootfs
, "-",
448 sizeof (bootfs
)) != 0);
453 * Given an nvlist of zpool properties to be set, validate that they are
454 * correct, and parse any numeric properties (index, boolean, etc) if they are
455 * specified as strings.
458 zpool_valid_proplist(libzfs_handle_t
*hdl
, const char *poolname
,
459 nvlist_t
*props
, uint64_t version
, prop_flags_t flags
, char *errbuf
)
467 struct stat64 statbuf
;
471 if (nvlist_alloc(&retprops
, NV_UNIQUE_NAME
, 0) != 0) {
472 (void) no_memory(hdl
);
477 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
478 const char *propname
= nvpair_name(elem
);
480 prop
= zpool_name_to_prop(propname
);
481 if (prop
== ZPROP_INVAL
&& zpool_prop_feature(propname
)) {
483 char *fname
= strchr(propname
, '@') + 1;
485 err
= zfeature_lookup_name(fname
, NULL
);
487 ASSERT3U(err
, ==, ENOENT
);
488 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
489 "invalid feature '%s'"), fname
);
490 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
494 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
495 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
496 "'%s' must be a string"), propname
);
497 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
501 (void) nvpair_value_string(elem
, &strval
);
502 if (strcmp(strval
, ZFS_FEATURE_ENABLED
) != 0 &&
503 strcmp(strval
, ZFS_FEATURE_DISABLED
) != 0) {
504 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
505 "property '%s' can only be set to "
506 "'enabled' or 'disabled'"), propname
);
507 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
511 if (nvlist_add_uint64(retprops
, propname
, 0) != 0) {
512 (void) no_memory(hdl
);
519 * Make sure this property is valid and applies to this type.
521 if (prop
== ZPROP_INVAL
) {
522 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
523 "invalid property '%s'"), propname
);
524 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
528 if (zpool_prop_readonly(prop
)) {
529 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
530 "is readonly"), propname
);
531 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
535 if (zprop_parse_value(hdl
, elem
, prop
, ZFS_TYPE_POOL
, retprops
,
536 &strval
, &intval
, errbuf
) != 0)
540 * Perform additional checking for specific properties.
545 case ZPOOL_PROP_VERSION
:
546 if (intval
< version
||
547 !SPA_VERSION_IS_SUPPORTED(intval
)) {
548 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
549 "property '%s' number %d is invalid."),
551 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
556 case ZPOOL_PROP_ASHIFT
:
558 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
559 "property '%s' can only be set at "
560 "creation time"), propname
);
561 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
565 if (intval
!= 0 && (intval
< 9 || intval
> 13)) {
566 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
567 "property '%s' number %d is invalid."),
569 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
574 case ZPOOL_PROP_BOOTFS
:
575 if (flags
.create
|| flags
.import
) {
576 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
577 "property '%s' cannot be set at creation "
578 "or import time"), propname
);
579 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
583 if (version
< SPA_VERSION_BOOTFS
) {
584 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
585 "pool must be upgraded to support "
586 "'%s' property"), propname
);
587 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
592 * bootfs property value has to be a dataset name and
593 * the dataset has to be in the same pool as it sets to.
595 if (strval
[0] != '\0' && !bootfs_name_valid(poolname
,
597 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
598 "is an invalid name"), strval
);
599 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
603 if ((zhp
= zpool_open_canfail(hdl
, poolname
)) == NULL
) {
604 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
605 "could not open pool '%s'"), poolname
);
606 (void) zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
);
609 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
610 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
612 #if defined(__sun__) || defined(__sun)
614 * bootfs property cannot be set on a disk which has
617 if (pool_uses_efi(nvroot
)) {
618 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
619 "property '%s' not supported on "
620 "EFI labeled devices"), propname
);
621 (void) zfs_error(hdl
, EZFS_POOL_NOTSUP
, errbuf
);
629 case ZPOOL_PROP_ALTROOT
:
630 if (!flags
.create
&& !flags
.import
) {
631 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
632 "property '%s' can only be set during pool "
633 "creation or import"), propname
);
634 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
638 if (strval
[0] != '/') {
639 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
640 "bad alternate root '%s'"), strval
);
641 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
646 case ZPOOL_PROP_CACHEFILE
:
647 if (strval
[0] == '\0')
650 if (strcmp(strval
, "none") == 0)
653 if (strval
[0] != '/') {
654 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
655 "property '%s' must be empty, an "
656 "absolute path, or 'none'"), propname
);
657 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
661 slash
= strrchr(strval
, '/');
663 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
664 strcmp(slash
, "/..") == 0) {
665 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
666 "'%s' is not a valid file"), strval
);
667 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
673 if (strval
[0] != '\0' &&
674 (stat64(strval
, &statbuf
) != 0 ||
675 !S_ISDIR(statbuf
.st_mode
))) {
676 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
677 "'%s' is not a valid directory"),
679 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
686 case ZPOOL_PROP_COMMENT
:
687 for (check
= strval
; *check
!= '\0'; check
++) {
688 if (!isprint(*check
)) {
690 dgettext(TEXT_DOMAIN
,
691 "comment may only have printable "
693 (void) zfs_error(hdl
, EZFS_BADPROP
,
698 if (strlen(strval
) > ZPROP_MAX_COMMENT
) {
699 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
700 "comment must not exceed %d characters"),
702 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
706 case ZPOOL_PROP_READONLY
:
708 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
709 "property '%s' can only be set at "
710 "import time"), propname
);
711 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
715 case ZPOOL_PROP_TNAME
:
717 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
718 "property '%s' can only be set at "
719 "creation time"), propname
);
720 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
729 nvlist_free(retprops
);
734 * Set zpool property : propname=propval.
737 zpool_set_prop(zpool_handle_t
*zhp
, const char *propname
, const char *propval
)
739 zfs_cmd_t zc
= {"\0"};
742 nvlist_t
*nvl
= NULL
;
745 prop_flags_t flags
= { 0 };
747 (void) snprintf(errbuf
, sizeof (errbuf
),
748 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
751 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
752 return (no_memory(zhp
->zpool_hdl
));
754 if (nvlist_add_string(nvl
, propname
, propval
) != 0) {
756 return (no_memory(zhp
->zpool_hdl
));
759 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
760 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
761 zhp
->zpool_name
, nvl
, version
, flags
, errbuf
)) == NULL
) {
770 * Execute the corresponding ioctl() to set this property.
772 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
774 if (zcmd_write_src_nvlist(zhp
->zpool_hdl
, &zc
, nvl
) != 0) {
779 ret
= zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_SET_PROPS
, &zc
);
781 zcmd_free_nvlists(&zc
);
785 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
787 (void) zpool_props_refresh(zhp
);
793 zpool_expand_proplist(zpool_handle_t
*zhp
, zprop_list_t
**plp
)
795 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
797 char buf
[ZFS_MAXPROPLEN
];
798 nvlist_t
*features
= NULL
;
801 boolean_t firstexpand
= (NULL
== *plp
);
804 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_POOL
) != 0)
808 while (*last
!= NULL
)
809 last
= &(*last
)->pl_next
;
812 features
= zpool_get_features(zhp
);
814 if ((*plp
)->pl_all
&& firstexpand
) {
815 for (i
= 0; i
< SPA_FEATURES
; i
++) {
816 zprop_list_t
*entry
= zfs_alloc(hdl
,
817 sizeof (zprop_list_t
));
818 entry
->pl_prop
= ZPROP_INVAL
;
819 entry
->pl_user_prop
= zfs_asprintf(hdl
, "feature@%s",
820 spa_feature_table
[i
].fi_uname
);
821 entry
->pl_width
= strlen(entry
->pl_user_prop
);
822 entry
->pl_all
= B_TRUE
;
825 last
= &entry
->pl_next
;
829 /* add any unsupported features */
830 for (nvp
= nvlist_next_nvpair(features
, NULL
);
831 nvp
!= NULL
; nvp
= nvlist_next_nvpair(features
, nvp
)) {
836 if (zfeature_is_supported(nvpair_name(nvp
)))
839 propname
= zfs_asprintf(hdl
, "unsupported@%s",
843 * Before adding the property to the list make sure that no
844 * other pool already added the same property.
848 while (entry
!= NULL
) {
849 if (entry
->pl_user_prop
!= NULL
&&
850 strcmp(propname
, entry
->pl_user_prop
) == 0) {
854 entry
= entry
->pl_next
;
861 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
862 entry
->pl_prop
= ZPROP_INVAL
;
863 entry
->pl_user_prop
= propname
;
864 entry
->pl_width
= strlen(entry
->pl_user_prop
);
865 entry
->pl_all
= B_TRUE
;
868 last
= &entry
->pl_next
;
871 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
876 if (entry
->pl_prop
!= ZPROP_INVAL
&&
877 zpool_get_prop(zhp
, entry
->pl_prop
, buf
, sizeof (buf
),
878 NULL
, B_FALSE
) == 0) {
879 if (strlen(buf
) > entry
->pl_width
)
880 entry
->pl_width
= strlen(buf
);
888 * Get the state for the given feature on the given ZFS pool.
891 zpool_prop_get_feature(zpool_handle_t
*zhp
, const char *propname
, char *buf
,
895 boolean_t found
= B_FALSE
;
896 nvlist_t
*features
= zpool_get_features(zhp
);
898 const char *feature
= strchr(propname
, '@') + 1;
900 supported
= zpool_prop_feature(propname
);
901 ASSERT(supported
|| zpool_prop_unsupported(propname
));
904 * Convert from feature name to feature guid. This conversion is
905 * unnecessary for unsupported@... properties because they already
912 ret
= zfeature_lookup_name(feature
, &fid
);
914 (void) strlcpy(buf
, "-", len
);
917 feature
= spa_feature_table
[fid
].fi_guid
;
920 if (nvlist_lookup_uint64(features
, feature
, &refcount
) == 0)
925 (void) strlcpy(buf
, ZFS_FEATURE_DISABLED
, len
);
928 (void) strlcpy(buf
, ZFS_FEATURE_ENABLED
, len
);
930 (void) strlcpy(buf
, ZFS_FEATURE_ACTIVE
, len
);
935 (void) strcpy(buf
, ZFS_UNSUPPORTED_INACTIVE
);
937 (void) strcpy(buf
, ZFS_UNSUPPORTED_READONLY
);
940 (void) strlcpy(buf
, "-", len
);
949 * Don't start the slice at the default block of 34; many storage
950 * devices will use a stripe width of 128k, other vendors prefer a 1m
951 * alignment. It is best to play it safe and ensure a 1m alignment
952 * given 512B blocks. When the block size is larger by a power of 2
953 * we will still be 1m aligned. Some devices are sensitive to the
954 * partition ending alignment as well.
956 #define NEW_START_BLOCK 2048
957 #define PARTITION_END_ALIGNMENT 2048
960 * Validate the given pool name, optionally putting an extended error message in
964 zpool_name_valid(libzfs_handle_t
*hdl
, boolean_t isopen
, const char *pool
)
970 ret
= pool_namecheck(pool
, &why
, &what
);
973 * The rules for reserved pool names were extended at a later point.
974 * But we need to support users with existing pools that may now be
975 * invalid. So we only check for this expanded set of names during a
976 * create (or import), and only in userland.
978 if (ret
== 0 && !isopen
&&
979 (strncmp(pool
, "mirror", 6) == 0 ||
980 strncmp(pool
, "raidz", 5) == 0 ||
981 strncmp(pool
, "spare", 5) == 0 ||
982 strcmp(pool
, "log") == 0)) {
985 dgettext(TEXT_DOMAIN
, "name is reserved"));
993 case NAME_ERR_TOOLONG
:
995 dgettext(TEXT_DOMAIN
, "name is too long"));
998 case NAME_ERR_INVALCHAR
:
1000 dgettext(TEXT_DOMAIN
, "invalid character "
1001 "'%c' in pool name"), what
);
1004 case NAME_ERR_NOLETTER
:
1005 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1006 "name must begin with a letter"));
1009 case NAME_ERR_RESERVED
:
1010 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1011 "name is reserved"));
1014 case NAME_ERR_DISKLIKE
:
1015 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1016 "pool name is reserved"));
1019 case NAME_ERR_LEADING_SLASH
:
1020 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1021 "leading slash in name"));
1024 case NAME_ERR_EMPTY_COMPONENT
:
1025 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1026 "empty component in name"));
1029 case NAME_ERR_TRAILING_SLASH
:
1030 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1031 "trailing slash in name"));
1034 case NAME_ERR_MULTIPLE_AT
:
1035 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1036 "multiple '@' delimiters in name"));
1038 case NAME_ERR_NO_AT
:
1039 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1040 "permission set is missing '@'"));
1051 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1055 zpool_open_canfail(libzfs_handle_t
*hdl
, const char *pool
)
1057 zpool_handle_t
*zhp
;
1061 * Make sure the pool name is valid.
1063 if (!zpool_name_valid(hdl
, B_TRUE
, pool
)) {
1064 (void) zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1065 dgettext(TEXT_DOMAIN
, "cannot open '%s'"),
1070 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
1073 zhp
->zpool_hdl
= hdl
;
1074 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1076 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1082 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "no such pool"));
1083 (void) zfs_error_fmt(hdl
, EZFS_NOENT
,
1084 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), pool
);
1093 * Like the above, but silent on error. Used when iterating over pools (because
1094 * the configuration cache may be out of date).
1097 zpool_open_silent(libzfs_handle_t
*hdl
, const char *pool
, zpool_handle_t
**ret
)
1099 zpool_handle_t
*zhp
;
1102 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
1105 zhp
->zpool_hdl
= hdl
;
1106 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1108 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1124 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1128 zpool_open(libzfs_handle_t
*hdl
, const char *pool
)
1130 zpool_handle_t
*zhp
;
1132 if ((zhp
= zpool_open_canfail(hdl
, pool
)) == NULL
)
1135 if (zhp
->zpool_state
== POOL_STATE_UNAVAIL
) {
1136 (void) zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
1137 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), zhp
->zpool_name
);
1146 * Close the handle. Simply frees the memory associated with the handle.
1149 zpool_close(zpool_handle_t
*zhp
)
1151 nvlist_free(zhp
->zpool_config
);
1152 nvlist_free(zhp
->zpool_old_config
);
1153 nvlist_free(zhp
->zpool_props
);
1158 * Return the name of the pool.
1161 zpool_get_name(zpool_handle_t
*zhp
)
1163 return (zhp
->zpool_name
);
1168 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1171 zpool_get_state(zpool_handle_t
*zhp
)
1173 return (zhp
->zpool_state
);
1177 * Create the named pool, using the provided vdev list. It is assumed
1178 * that the consumer has already validated the contents of the nvlist, so we
1179 * don't have to worry about error semantics.
1182 zpool_create(libzfs_handle_t
*hdl
, const char *pool
, nvlist_t
*nvroot
,
1183 nvlist_t
*props
, nvlist_t
*fsprops
)
1185 zfs_cmd_t zc
= {"\0"};
1186 nvlist_t
*zc_fsprops
= NULL
;
1187 nvlist_t
*zc_props
= NULL
;
1191 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1192 "cannot create '%s'"), pool
);
1194 if (!zpool_name_valid(hdl
, B_FALSE
, pool
))
1195 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
1197 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1201 prop_flags_t flags
= { .create
= B_TRUE
, .import
= B_FALSE
};
1203 if ((zc_props
= zpool_valid_proplist(hdl
, pool
, props
,
1204 SPA_VERSION_1
, flags
, msg
)) == NULL
) {
1213 zoned
= ((nvlist_lookup_string(fsprops
,
1214 zfs_prop_to_name(ZFS_PROP_ZONED
), &zonestr
) == 0) &&
1215 strcmp(zonestr
, "on") == 0);
1217 if ((zc_fsprops
= zfs_valid_proplist(hdl
, ZFS_TYPE_FILESYSTEM
,
1218 fsprops
, zoned
, NULL
, NULL
, msg
)) == NULL
) {
1222 (nvlist_alloc(&zc_props
, NV_UNIQUE_NAME
, 0) != 0)) {
1225 if (nvlist_add_nvlist(zc_props
,
1226 ZPOOL_ROOTFS_PROPS
, zc_fsprops
) != 0) {
1231 if (zc_props
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
1234 (void) strlcpy(zc
.zc_name
, pool
, sizeof (zc
.zc_name
));
1236 if ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_CREATE
, &zc
)) != 0) {
1238 zcmd_free_nvlists(&zc
);
1239 nvlist_free(zc_props
);
1240 nvlist_free(zc_fsprops
);
1245 * This can happen if the user has specified the same
1246 * device multiple times. We can't reliably detect this
1247 * until we try to add it and see we already have a
1248 * label. This can also happen under if the device is
1249 * part of an active md or lvm device.
1251 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1252 "one or more vdevs refer to the same device, or "
1253 "one of\nthe devices is part of an active md or "
1255 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1259 * This happens if the record size is smaller or larger
1260 * than the allowed size range, or not a power of 2.
1262 * NOTE: although zfs_valid_proplist is called earlier,
1263 * this case may have slipped through since the
1264 * pool does not exist yet and it is therefore
1265 * impossible to read properties e.g. max blocksize
1268 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1269 "record size invalid"));
1270 return (zfs_error(hdl
, EZFS_BADPROP
, msg
));
1274 * This occurs when one of the devices is below
1275 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1276 * device was the problem device since there's no
1277 * reliable way to determine device size from userland.
1282 zfs_nicenum(SPA_MINDEVSIZE
, buf
, sizeof (buf
));
1284 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1285 "one or more devices is less than the "
1286 "minimum size (%s)"), buf
);
1288 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1291 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1292 "one or more devices is out of space"));
1293 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1296 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1297 "cache device must be a disk or disk slice"));
1298 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1301 return (zpool_standard_error(hdl
, errno
, msg
));
1306 zcmd_free_nvlists(&zc
);
1307 nvlist_free(zc_props
);
1308 nvlist_free(zc_fsprops
);
1313 * Destroy the given pool. It is up to the caller to ensure that there are no
1314 * datasets left in the pool.
1317 zpool_destroy(zpool_handle_t
*zhp
, const char *log_str
)
1319 zfs_cmd_t zc
= {"\0"};
1320 zfs_handle_t
*zfp
= NULL
;
1321 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1324 if (zhp
->zpool_state
== POOL_STATE_ACTIVE
&&
1325 (zfp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
1328 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1329 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1331 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_DESTROY
, &zc
) != 0) {
1332 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1333 "cannot destroy '%s'"), zhp
->zpool_name
);
1335 if (errno
== EROFS
) {
1336 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1337 "one or more devices is read only"));
1338 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1340 (void) zpool_standard_error(hdl
, errno
, msg
);
1349 remove_mountpoint(zfp
);
1357 * Add the given vdevs to the pool. The caller must have already performed the
1358 * necessary verification to ensure that the vdev specification is well-formed.
1361 zpool_add(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
1363 zfs_cmd_t zc
= {"\0"};
1365 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1367 nvlist_t
**spares
, **l2cache
;
1368 uint_t nspares
, nl2cache
;
1370 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1371 "cannot add to '%s'"), zhp
->zpool_name
);
1373 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1374 SPA_VERSION_SPARES
&&
1375 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1376 &spares
, &nspares
) == 0) {
1377 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1378 "upgraded to add hot spares"));
1379 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1382 #if defined(__sun__) || defined(__sun)
1383 if (zpool_is_bootable(zhp
) && nvlist_lookup_nvlist_array(nvroot
,
1384 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0) {
1387 for (s
= 0; s
< nspares
; s
++) {
1390 if (nvlist_lookup_string(spares
[s
], ZPOOL_CONFIG_PATH
,
1391 &path
) == 0 && pool_uses_efi(spares
[s
])) {
1392 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1393 "device '%s' contains an EFI label and "
1394 "cannot be used on root pools."),
1395 zpool_vdev_name(hdl
, NULL
, spares
[s
], 0));
1396 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, msg
));
1402 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1403 SPA_VERSION_L2CACHE
&&
1404 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1405 &l2cache
, &nl2cache
) == 0) {
1406 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1407 "upgraded to add cache devices"));
1408 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1411 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1413 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1415 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_ADD
, &zc
) != 0) {
1419 * This can happen if the user has specified the same
1420 * device multiple times. We can't reliably detect this
1421 * until we try to add it and see we already have a
1424 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1425 "one or more vdevs refer to the same device"));
1426 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1431 * This occurrs when one of the devices is below
1432 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1433 * device was the problem device since there's no
1434 * reliable way to determine device size from userland.
1439 zfs_nicenum(SPA_MINDEVSIZE
, buf
, sizeof (buf
));
1441 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1442 "device is less than the minimum "
1445 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1449 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1450 "pool must be upgraded to add these vdevs"));
1451 (void) zfs_error(hdl
, EZFS_BADVERSION
, msg
);
1455 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1456 "cache device must be a disk or disk slice"));
1457 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1461 (void) zpool_standard_error(hdl
, errno
, msg
);
1469 zcmd_free_nvlists(&zc
);
1475 * Exports the pool from the system. The caller must ensure that there are no
1476 * mounted datasets in the pool.
1479 zpool_export_common(zpool_handle_t
*zhp
, boolean_t force
, boolean_t hardforce
,
1480 const char *log_str
)
1482 zfs_cmd_t zc
= {"\0"};
1485 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1486 "cannot export '%s'"), zhp
->zpool_name
);
1488 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1489 zc
.zc_cookie
= force
;
1490 zc
.zc_guid
= hardforce
;
1491 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1493 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_EXPORT
, &zc
) != 0) {
1496 zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
1497 "use '-f' to override the following errors:\n"
1498 "'%s' has an active shared spare which could be"
1499 " used by other pools once '%s' is exported."),
1500 zhp
->zpool_name
, zhp
->zpool_name
);
1501 return (zfs_error(zhp
->zpool_hdl
, EZFS_ACTIVE_SPARE
,
1504 return (zpool_standard_error_fmt(zhp
->zpool_hdl
, errno
,
1513 zpool_export(zpool_handle_t
*zhp
, boolean_t force
, const char *log_str
)
1515 return (zpool_export_common(zhp
, force
, B_FALSE
, log_str
));
1519 zpool_export_force(zpool_handle_t
*zhp
, const char *log_str
)
1521 return (zpool_export_common(zhp
, B_TRUE
, B_TRUE
, log_str
));
1525 zpool_rewind_exclaim(libzfs_handle_t
*hdl
, const char *name
, boolean_t dryrun
,
1528 nvlist_t
*nv
= NULL
;
1534 if (!hdl
->libzfs_printerr
|| config
== NULL
)
1537 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1538 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0) {
1542 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1544 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1546 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1547 strftime(timestr
, 128, "%c", &t
) != 0) {
1549 (void) printf(dgettext(TEXT_DOMAIN
,
1550 "Would be able to return %s "
1551 "to its state as of %s.\n"),
1554 (void) printf(dgettext(TEXT_DOMAIN
,
1555 "Pool %s returned to its state as of %s.\n"),
1559 (void) printf(dgettext(TEXT_DOMAIN
,
1560 "%s approximately %lld "),
1561 dryrun
? "Would discard" : "Discarded",
1562 ((longlong_t
)loss
+ 30) / 60);
1563 (void) printf(dgettext(TEXT_DOMAIN
,
1564 "minutes of transactions.\n"));
1565 } else if (loss
> 0) {
1566 (void) printf(dgettext(TEXT_DOMAIN
,
1567 "%s approximately %lld "),
1568 dryrun
? "Would discard" : "Discarded",
1570 (void) printf(dgettext(TEXT_DOMAIN
,
1571 "seconds of transactions.\n"));
1577 zpool_explain_recover(libzfs_handle_t
*hdl
, const char *name
, int reason
,
1580 nvlist_t
*nv
= NULL
;
1582 uint64_t edata
= UINT64_MAX
;
1587 if (!hdl
->libzfs_printerr
)
1591 (void) printf(dgettext(TEXT_DOMAIN
, "action: "));
1593 (void) printf(dgettext(TEXT_DOMAIN
, "\t"));
1595 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1596 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1597 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0 ||
1598 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1601 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1602 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_DATA_ERRORS
,
1605 (void) printf(dgettext(TEXT_DOMAIN
,
1606 "Recovery is possible, but will result in some data loss.\n"));
1608 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1609 strftime(timestr
, 128, "%c", &t
) != 0) {
1610 (void) printf(dgettext(TEXT_DOMAIN
,
1611 "\tReturning the pool to its state as of %s\n"
1612 "\tshould correct the problem. "),
1615 (void) printf(dgettext(TEXT_DOMAIN
,
1616 "\tReverting the pool to an earlier state "
1617 "should correct the problem.\n\t"));
1621 (void) printf(dgettext(TEXT_DOMAIN
,
1622 "Approximately %lld minutes of data\n"
1623 "\tmust be discarded, irreversibly. "),
1624 ((longlong_t
)loss
+ 30) / 60);
1625 } else if (loss
> 0) {
1626 (void) printf(dgettext(TEXT_DOMAIN
,
1627 "Approximately %lld seconds of data\n"
1628 "\tmust be discarded, irreversibly. "),
1631 if (edata
!= 0 && edata
!= UINT64_MAX
) {
1633 (void) printf(dgettext(TEXT_DOMAIN
,
1634 "After rewind, at least\n"
1635 "\tone persistent user-data error will remain. "));
1637 (void) printf(dgettext(TEXT_DOMAIN
,
1638 "After rewind, several\n"
1639 "\tpersistent user-data errors will remain. "));
1642 (void) printf(dgettext(TEXT_DOMAIN
,
1643 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1644 reason
>= 0 ? "clear" : "import", name
);
1646 (void) printf(dgettext(TEXT_DOMAIN
,
1647 "A scrub of the pool\n"
1648 "\tis strongly recommended after recovery.\n"));
1652 (void) printf(dgettext(TEXT_DOMAIN
,
1653 "Destroy and re-create the pool from\n\ta backup source.\n"));
1657 * zpool_import() is a contracted interface. Should be kept the same
1660 * Applications should use zpool_import_props() to import a pool with
1661 * new properties value to be set.
1664 zpool_import(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1667 nvlist_t
*props
= NULL
;
1670 if (altroot
!= NULL
) {
1671 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
1672 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1673 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1677 if (nvlist_add_string(props
,
1678 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), altroot
) != 0 ||
1679 nvlist_add_string(props
,
1680 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
), "none") != 0) {
1682 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1683 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1688 ret
= zpool_import_props(hdl
, config
, newname
, props
,
1695 print_vdev_tree(libzfs_handle_t
*hdl
, const char *name
, nvlist_t
*nv
,
1701 uint64_t is_log
= 0;
1703 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
,
1707 (void) printf("\t%*s%s%s\n", indent
, "", name
,
1708 is_log
? " [log]" : "");
1710 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1711 &child
, &children
) != 0)
1714 for (c
= 0; c
< children
; c
++) {
1715 vname
= zpool_vdev_name(hdl
, NULL
, child
[c
], VDEV_NAME_TYPE_ID
);
1716 print_vdev_tree(hdl
, vname
, child
[c
], indent
+ 2);
1722 zpool_print_unsup_feat(nvlist_t
*config
)
1724 nvlist_t
*nvinfo
, *unsup_feat
;
1727 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) ==
1729 verify(nvlist_lookup_nvlist(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
,
1732 for (nvp
= nvlist_next_nvpair(unsup_feat
, NULL
); nvp
!= NULL
;
1733 nvp
= nvlist_next_nvpair(unsup_feat
, nvp
)) {
1736 verify(nvpair_type(nvp
) == DATA_TYPE_STRING
);
1737 verify(nvpair_value_string(nvp
, &desc
) == 0);
1739 if (strlen(desc
) > 0)
1740 (void) printf("\t%s (%s)\n", nvpair_name(nvp
), desc
);
1742 (void) printf("\t%s\n", nvpair_name(nvp
));
1747 * Import the given pool using the known configuration and a list of
1748 * properties to be set. The configuration should have come from
1749 * zpool_find_import(). The 'newname' parameters control whether the pool
1750 * is imported with a different name.
1753 zpool_import_props(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1754 nvlist_t
*props
, int flags
)
1756 zfs_cmd_t zc
= {"\0"};
1757 zpool_rewind_policy_t policy
;
1758 nvlist_t
*nv
= NULL
;
1759 nvlist_t
*nvinfo
= NULL
;
1760 nvlist_t
*missing
= NULL
;
1767 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1770 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1771 "cannot import pool '%s'"), origname
);
1773 if (newname
!= NULL
) {
1774 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
1775 return (zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1776 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1778 thename
= (char *)newname
;
1783 if (props
!= NULL
) {
1785 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
1787 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
1790 if ((props
= zpool_valid_proplist(hdl
, origname
,
1791 props
, version
, flags
, errbuf
)) == NULL
)
1793 if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
1800 (void) strlcpy(zc
.zc_name
, thename
, sizeof (zc
.zc_name
));
1802 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1805 if (zcmd_write_conf_nvlist(hdl
, &zc
, config
) != 0) {
1806 zcmd_free_nvlists(&zc
);
1809 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zc
.zc_nvlist_conf_size
* 2) != 0) {
1810 zcmd_free_nvlists(&zc
);
1814 zc
.zc_cookie
= flags
;
1815 while ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_IMPORT
, &zc
)) != 0 &&
1817 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
1818 zcmd_free_nvlists(&zc
);
1825 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nv
);
1827 zcmd_free_nvlists(&zc
);
1829 zpool_get_rewind_policy(config
, &policy
);
1835 * Dry-run failed, but we print out what success
1836 * looks like if we found a best txg
1838 if (policy
.zrp_request
& ZPOOL_TRY_REWIND
) {
1839 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1845 if (newname
== NULL
)
1846 (void) snprintf(desc
, sizeof (desc
),
1847 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1850 (void) snprintf(desc
, sizeof (desc
),
1851 dgettext(TEXT_DOMAIN
, "cannot import '%s' as '%s'"),
1856 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
1857 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1858 nvlist_exists(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
)) {
1859 (void) printf(dgettext(TEXT_DOMAIN
, "This "
1860 "pool uses the following feature(s) not "
1861 "supported by this system:\n"));
1862 zpool_print_unsup_feat(nv
);
1863 if (nvlist_exists(nvinfo
,
1864 ZPOOL_CONFIG_CAN_RDONLY
)) {
1865 (void) printf(dgettext(TEXT_DOMAIN
,
1866 "All unsupported features are only "
1867 "required for writing to the pool."
1868 "\nThe pool can be imported using "
1869 "'-o readonly=on'.\n"));
1873 * Unsupported version.
1875 (void) zfs_error(hdl
, EZFS_BADVERSION
, desc
);
1879 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, desc
);
1883 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1884 "one or more devices is read only"));
1885 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
1889 if (nv
&& nvlist_lookup_nvlist(nv
,
1890 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1891 nvlist_lookup_nvlist(nvinfo
,
1892 ZPOOL_CONFIG_MISSING_DEVICES
, &missing
) == 0) {
1893 (void) printf(dgettext(TEXT_DOMAIN
,
1894 "The devices below are missing, use "
1895 "'-m' to import the pool anyway:\n"));
1896 print_vdev_tree(hdl
, NULL
, missing
, 2);
1897 (void) printf("\n");
1899 (void) zpool_standard_error(hdl
, error
, desc
);
1903 (void) zpool_standard_error(hdl
, error
, desc
);
1907 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1908 "one or more devices are already in use\n"));
1909 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
1912 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1913 "new name of at least one dataset is longer than "
1914 "the maximum allowable length"));
1915 (void) zfs_error(hdl
, EZFS_NAMETOOLONG
, desc
);
1918 (void) zpool_standard_error(hdl
, error
, desc
);
1919 zpool_explain_recover(hdl
,
1920 newname
? origname
: thename
, -error
, nv
);
1927 zpool_handle_t
*zhp
;
1930 * This should never fail, but play it safe anyway.
1932 if (zpool_open_silent(hdl
, thename
, &zhp
) != 0)
1934 else if (zhp
!= NULL
)
1936 if (policy
.zrp_request
&
1937 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
1938 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1939 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0), nv
);
1952 zpool_scan(zpool_handle_t
*zhp
, pool_scan_func_t func
)
1954 zfs_cmd_t zc
= {"\0"};
1956 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1958 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1959 zc
.zc_cookie
= func
;
1961 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_SCAN
, &zc
) == 0 ||
1962 (errno
== ENOENT
&& func
!= POOL_SCAN_NONE
))
1965 if (func
== POOL_SCAN_SCRUB
) {
1966 (void) snprintf(msg
, sizeof (msg
),
1967 dgettext(TEXT_DOMAIN
, "cannot scrub %s"), zc
.zc_name
);
1968 } else if (func
== POOL_SCAN_NONE
) {
1969 (void) snprintf(msg
, sizeof (msg
),
1970 dgettext(TEXT_DOMAIN
, "cannot cancel scrubbing %s"),
1973 assert(!"unexpected result");
1976 if (errno
== EBUSY
) {
1978 pool_scan_stat_t
*ps
= NULL
;
1981 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
1982 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
1983 (void) nvlist_lookup_uint64_array(nvroot
,
1984 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &psc
);
1985 if (ps
&& ps
->pss_func
== POOL_SCAN_SCRUB
)
1986 return (zfs_error(hdl
, EZFS_SCRUBBING
, msg
));
1988 return (zfs_error(hdl
, EZFS_RESILVERING
, msg
));
1989 } else if (errno
== ENOENT
) {
1990 return (zfs_error(hdl
, EZFS_NO_SCRUB
, msg
));
1992 return (zpool_standard_error(hdl
, errno
, msg
));
1997 * Find a vdev that matches the search criteria specified. We use the
1998 * the nvpair name to determine how we should look for the device.
1999 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2000 * spare; but FALSE if its an INUSE spare.
2003 vdev_to_nvlist_iter(nvlist_t
*nv
, nvlist_t
*search
, boolean_t
*avail_spare
,
2004 boolean_t
*l2cache
, boolean_t
*log
)
2011 nvpair_t
*pair
= nvlist_next_nvpair(search
, NULL
);
2013 /* Nothing to look for */
2014 if (search
== NULL
|| pair
== NULL
)
2017 /* Obtain the key we will use to search */
2018 srchkey
= nvpair_name(pair
);
2020 switch (nvpair_type(pair
)) {
2021 case DATA_TYPE_UINT64
:
2022 if (strcmp(srchkey
, ZPOOL_CONFIG_GUID
) == 0) {
2023 uint64_t srchval
, theguid
;
2025 verify(nvpair_value_uint64(pair
, &srchval
) == 0);
2026 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
2028 if (theguid
== srchval
)
2033 case DATA_TYPE_STRING
: {
2034 char *srchval
, *val
;
2036 verify(nvpair_value_string(pair
, &srchval
) == 0);
2037 if (nvlist_lookup_string(nv
, srchkey
, &val
) != 0)
2041 * Search for the requested value. Special cases:
2043 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2044 * "-part1", or "p1". The suffix is hidden from the user,
2045 * but included in the string, so this matches around it.
2046 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2047 * is used to check all possible expanded paths.
2048 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2050 * Otherwise, all other searches are simple string compares.
2052 if (strcmp(srchkey
, ZPOOL_CONFIG_PATH
) == 0) {
2053 uint64_t wholedisk
= 0;
2055 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
2057 if (zfs_strcmp_pathname(srchval
, val
, wholedisk
) == 0)
2060 } else if (strcmp(srchkey
, ZPOOL_CONFIG_TYPE
) == 0 && val
) {
2061 char *type
, *idx
, *end
, *p
;
2062 uint64_t id
, vdev_id
;
2065 * Determine our vdev type, keeping in mind
2066 * that the srchval is composed of a type and
2067 * vdev id pair (i.e. mirror-4).
2069 if ((type
= strdup(srchval
)) == NULL
)
2072 if ((p
= strrchr(type
, '-')) == NULL
) {
2080 * If the types don't match then keep looking.
2082 if (strncmp(val
, type
, strlen(val
)) != 0) {
2087 verify(strncmp(type
, VDEV_TYPE_RAIDZ
,
2088 strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2089 strncmp(type
, VDEV_TYPE_MIRROR
,
2090 strlen(VDEV_TYPE_MIRROR
)) == 0);
2091 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
2095 vdev_id
= strtoull(idx
, &end
, 10);
2102 * Now verify that we have the correct vdev id.
2111 if (strcmp(srchval
, val
) == 0)
2120 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2121 &child
, &children
) != 0)
2124 for (c
= 0; c
< children
; c
++) {
2125 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2126 avail_spare
, l2cache
, NULL
)) != NULL
) {
2128 * The 'is_log' value is only set for the toplevel
2129 * vdev, not the leaf vdevs. So we always lookup the
2130 * log device from the root of the vdev tree (where
2131 * 'log' is non-NULL).
2134 nvlist_lookup_uint64(child
[c
],
2135 ZPOOL_CONFIG_IS_LOG
, &is_log
) == 0 &&
2143 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2144 &child
, &children
) == 0) {
2145 for (c
= 0; c
< children
; c
++) {
2146 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2147 avail_spare
, l2cache
, NULL
)) != NULL
) {
2148 *avail_spare
= B_TRUE
;
2154 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2155 &child
, &children
) == 0) {
2156 for (c
= 0; c
< children
; c
++) {
2157 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2158 avail_spare
, l2cache
, NULL
)) != NULL
) {
2169 * Given a physical path (minus the "/devices" prefix), find the
2173 zpool_find_vdev_by_physpath(zpool_handle_t
*zhp
, const char *ppath
,
2174 boolean_t
*avail_spare
, boolean_t
*l2cache
, boolean_t
*log
)
2176 nvlist_t
*search
, *nvroot
, *ret
;
2178 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2179 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PHYS_PATH
, ppath
) == 0);
2181 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2184 *avail_spare
= B_FALSE
;
2188 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2189 nvlist_free(search
);
2195 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2198 zpool_vdev_is_interior(const char *name
)
2200 if (strncmp(name
, VDEV_TYPE_RAIDZ
, strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2201 strncmp(name
, VDEV_TYPE_MIRROR
, strlen(VDEV_TYPE_MIRROR
)) == 0)
2207 zpool_find_vdev(zpool_handle_t
*zhp
, const char *path
, boolean_t
*avail_spare
,
2208 boolean_t
*l2cache
, boolean_t
*log
)
2211 nvlist_t
*nvroot
, *search
, *ret
;
2214 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2216 guid
= strtoull(path
, &end
, 0);
2217 if (guid
!= 0 && *end
== '\0') {
2218 verify(nvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
) == 0);
2219 } else if (zpool_vdev_is_interior(path
)) {
2220 verify(nvlist_add_string(search
, ZPOOL_CONFIG_TYPE
, path
) == 0);
2222 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PATH
, path
) == 0);
2225 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2228 *avail_spare
= B_FALSE
;
2232 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2233 nvlist_free(search
);
2239 vdev_online(nvlist_t
*nv
)
2243 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
, &ival
) == 0 ||
2244 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
, &ival
) == 0 ||
2245 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
, &ival
) == 0)
2252 * Helper function for zpool_get_physpaths().
2255 vdev_get_one_physpath(nvlist_t
*config
, char *physpath
, size_t physpath_size
,
2256 size_t *bytes_written
)
2258 size_t bytes_left
, pos
, rsz
;
2262 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PHYS_PATH
,
2264 return (EZFS_NODEVICE
);
2266 pos
= *bytes_written
;
2267 bytes_left
= physpath_size
- pos
;
2268 format
= (pos
== 0) ? "%s" : " %s";
2270 rsz
= snprintf(physpath
+ pos
, bytes_left
, format
, tmppath
);
2271 *bytes_written
+= rsz
;
2273 if (rsz
>= bytes_left
) {
2274 /* if physpath was not copied properly, clear it */
2275 if (bytes_left
!= 0) {
2278 return (EZFS_NOSPC
);
2284 vdev_get_physpaths(nvlist_t
*nv
, char *physpath
, size_t phypath_size
,
2285 size_t *rsz
, boolean_t is_spare
)
2290 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
2291 return (EZFS_INVALCONFIG
);
2293 if (strcmp(type
, VDEV_TYPE_DISK
) == 0) {
2295 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2296 * For a spare vdev, we only want to boot from the active
2301 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
2304 return (EZFS_INVALCONFIG
);
2307 if (vdev_online(nv
)) {
2308 if ((ret
= vdev_get_one_physpath(nv
, physpath
,
2309 phypath_size
, rsz
)) != 0)
2312 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) == 0 ||
2313 strcmp(type
, VDEV_TYPE_RAIDZ
) == 0 ||
2314 strcmp(type
, VDEV_TYPE_REPLACING
) == 0 ||
2315 (is_spare
= (strcmp(type
, VDEV_TYPE_SPARE
) == 0))) {
2320 if (nvlist_lookup_nvlist_array(nv
,
2321 ZPOOL_CONFIG_CHILDREN
, &child
, &count
) != 0)
2322 return (EZFS_INVALCONFIG
);
2324 for (i
= 0; i
< count
; i
++) {
2325 ret
= vdev_get_physpaths(child
[i
], physpath
,
2326 phypath_size
, rsz
, is_spare
);
2327 if (ret
== EZFS_NOSPC
)
2332 return (EZFS_POOL_INVALARG
);
2336 * Get phys_path for a root pool config.
2337 * Return 0 on success; non-zero on failure.
2340 zpool_get_config_physpath(nvlist_t
*config
, char *physpath
, size_t phypath_size
)
2343 nvlist_t
*vdev_root
;
2350 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2352 return (EZFS_INVALCONFIG
);
2354 if (nvlist_lookup_string(vdev_root
, ZPOOL_CONFIG_TYPE
, &type
) != 0 ||
2355 nvlist_lookup_nvlist_array(vdev_root
, ZPOOL_CONFIG_CHILDREN
,
2356 &child
, &count
) != 0)
2357 return (EZFS_INVALCONFIG
);
2359 #if defined(__sun__) || defined(__sun)
2361 * root pool can not have EFI labeled disks and can only have
2362 * a single top-level vdev.
2364 if (strcmp(type
, VDEV_TYPE_ROOT
) != 0 || count
!= 1 ||
2365 pool_uses_efi(vdev_root
))
2366 return (EZFS_POOL_INVALARG
);
2369 (void) vdev_get_physpaths(child
[0], physpath
, phypath_size
, &rsz
,
2372 /* No online devices */
2374 return (EZFS_NODEVICE
);
2380 * Get phys_path for a root pool
2381 * Return 0 on success; non-zero on failure.
2384 zpool_get_physpath(zpool_handle_t
*zhp
, char *physpath
, size_t phypath_size
)
2386 return (zpool_get_config_physpath(zhp
->zpool_config
, physpath
,
2391 * If the device has being dynamically expanded then we need to relabel
2392 * the disk to use the new unallocated space.
2395 zpool_relabel_disk(libzfs_handle_t
*hdl
, const char *path
, const char *msg
)
2399 if ((fd
= open(path
, O_RDWR
|O_DIRECT
)) < 0) {
2400 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2401 "relabel '%s': unable to open device: %d"), path
, errno
);
2402 return (zfs_error(hdl
, EZFS_OPENFAILED
, msg
));
2406 * It's possible that we might encounter an error if the device
2407 * does not have any unallocated space left. If so, we simply
2408 * ignore that error and continue on.
2410 * Also, we don't call efi_rescan() - that would just return EBUSY.
2411 * The module will do it for us in vdev_disk_open().
2413 error
= efi_use_whole_disk(fd
);
2415 if (error
&& error
!= VT_ENOSPC
) {
2416 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2417 "relabel '%s': unable to read disk capacity"), path
);
2418 return (zfs_error(hdl
, EZFS_NOCAP
, msg
));
2424 * Bring the specified vdev online. The 'flags' parameter is a set of the
2425 * ZFS_ONLINE_* flags.
2428 zpool_vdev_online(zpool_handle_t
*zhp
, const char *path
, int flags
,
2429 vdev_state_t
*newstate
)
2431 zfs_cmd_t zc
= {"\0"};
2434 boolean_t avail_spare
, l2cache
, islog
;
2435 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2438 if (flags
& ZFS_ONLINE_EXPAND
) {
2439 (void) snprintf(msg
, sizeof (msg
),
2440 dgettext(TEXT_DOMAIN
, "cannot expand %s"), path
);
2442 (void) snprintf(msg
, sizeof (msg
),
2443 dgettext(TEXT_DOMAIN
, "cannot online %s"), path
);
2446 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2447 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2449 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2451 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2454 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2456 if (flags
& ZFS_ONLINE_EXPAND
||
2457 zpool_get_prop_int(zhp
, ZPOOL_PROP_AUTOEXPAND
, NULL
)) {
2458 uint64_t wholedisk
= 0;
2460 (void) nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_WHOLE_DISK
,
2464 * XXX - L2ARC 1.0 devices can't support expansion.
2467 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2468 "cannot expand cache devices"));
2469 return (zfs_error(hdl
, EZFS_VDEVNOTSUP
, msg
));
2473 const char *fullpath
= path
;
2474 char buf
[MAXPATHLEN
];
2476 if (path
[0] != '/') {
2477 error
= zfs_resolve_shortname(path
, buf
,
2480 return (zfs_error(hdl
, EZFS_NODEVICE
,
2486 error
= zpool_relabel_disk(hdl
, fullpath
, msg
);
2492 zc
.zc_cookie
= VDEV_STATE_ONLINE
;
2495 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) != 0) {
2496 if (errno
== EINVAL
) {
2497 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "was split "
2498 "from this pool into a new one. Use '%s' "
2499 "instead"), "zpool detach");
2500 return (zfs_error(hdl
, EZFS_POSTSPLIT_ONLINE
, msg
));
2502 return (zpool_standard_error(hdl
, errno
, msg
));
2505 *newstate
= zc
.zc_cookie
;
2510 * Take the specified vdev offline
2513 zpool_vdev_offline(zpool_handle_t
*zhp
, const char *path
, boolean_t istmp
)
2515 zfs_cmd_t zc
= {"\0"};
2518 boolean_t avail_spare
, l2cache
;
2519 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2521 (void) snprintf(msg
, sizeof (msg
),
2522 dgettext(TEXT_DOMAIN
, "cannot offline %s"), path
);
2524 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2525 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2527 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2529 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2532 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2534 zc
.zc_cookie
= VDEV_STATE_OFFLINE
;
2535 zc
.zc_obj
= istmp
? ZFS_OFFLINE_TEMPORARY
: 0;
2537 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2544 * There are no other replicas of this device.
2546 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2550 * The log device has unplayed logs
2552 return (zfs_error(hdl
, EZFS_UNPLAYED_LOGS
, msg
));
2555 return (zpool_standard_error(hdl
, errno
, msg
));
2560 * Mark the given vdev faulted.
2563 zpool_vdev_fault(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2565 zfs_cmd_t zc
= {"\0"};
2567 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2569 (void) snprintf(msg
, sizeof (msg
),
2570 dgettext(TEXT_DOMAIN
, "cannot fault %llu"), (u_longlong_t
)guid
);
2572 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2574 zc
.zc_cookie
= VDEV_STATE_FAULTED
;
2577 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2584 * There are no other replicas of this device.
2586 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2589 return (zpool_standard_error(hdl
, errno
, msg
));
2595 * Mark the given vdev degraded.
2598 zpool_vdev_degrade(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2600 zfs_cmd_t zc
= {"\0"};
2602 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2604 (void) snprintf(msg
, sizeof (msg
),
2605 dgettext(TEXT_DOMAIN
, "cannot degrade %llu"), (u_longlong_t
)guid
);
2607 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2609 zc
.zc_cookie
= VDEV_STATE_DEGRADED
;
2612 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2615 return (zpool_standard_error(hdl
, errno
, msg
));
2619 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2623 is_replacing_spare(nvlist_t
*search
, nvlist_t
*tgt
, int which
)
2629 if (nvlist_lookup_nvlist_array(search
, ZPOOL_CONFIG_CHILDREN
, &child
,
2631 verify(nvlist_lookup_string(search
, ZPOOL_CONFIG_TYPE
,
2634 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0 &&
2635 children
== 2 && child
[which
] == tgt
)
2638 for (c
= 0; c
< children
; c
++)
2639 if (is_replacing_spare(child
[c
], tgt
, which
))
2647 * Attach new_disk (fully described by nvroot) to old_disk.
2648 * If 'replacing' is specified, the new disk will replace the old one.
2651 zpool_vdev_attach(zpool_handle_t
*zhp
,
2652 const char *old_disk
, const char *new_disk
, nvlist_t
*nvroot
, int replacing
)
2654 zfs_cmd_t zc
= {"\0"};
2658 boolean_t avail_spare
, l2cache
, islog
;
2663 nvlist_t
*config_root
;
2664 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2665 boolean_t rootpool
= zpool_is_bootable(zhp
);
2668 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2669 "cannot replace %s with %s"), old_disk
, new_disk
);
2671 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2672 "cannot attach %s to %s"), new_disk
, old_disk
);
2674 #if defined(__sun__) || defined(__sun)
2676 * If this is a root pool, make sure that we're not attaching an
2677 * EFI labeled device.
2679 if (rootpool
&& pool_uses_efi(nvroot
)) {
2680 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2681 "EFI labeled devices are not supported on root pools."));
2682 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, msg
));
2686 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2687 if ((tgt
= zpool_find_vdev(zhp
, old_disk
, &avail_spare
, &l2cache
,
2689 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2692 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2695 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2697 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2698 zc
.zc_cookie
= replacing
;
2700 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
2701 &child
, &children
) != 0 || children
!= 1) {
2702 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2703 "new device must be a single disk"));
2704 return (zfs_error(hdl
, EZFS_INVALCONFIG
, msg
));
2707 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
2708 ZPOOL_CONFIG_VDEV_TREE
, &config_root
) == 0);
2710 if ((newname
= zpool_vdev_name(NULL
, NULL
, child
[0], 0)) == NULL
)
2714 * If the target is a hot spare that has been swapped in, we can only
2715 * replace it with another hot spare.
2718 nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_IS_SPARE
, &val
) == 0 &&
2719 (zpool_find_vdev(zhp
, newname
, &avail_spare
, &l2cache
,
2720 NULL
) == NULL
|| !avail_spare
) &&
2721 is_replacing_spare(config_root
, tgt
, 1)) {
2722 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2723 "can only be replaced by another hot spare"));
2725 return (zfs_error(hdl
, EZFS_BADTARGET
, msg
));
2730 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
2733 ret
= zfs_ioctl(hdl
, ZFS_IOC_VDEV_ATTACH
, &zc
);
2735 zcmd_free_nvlists(&zc
);
2740 * XXX need a better way to prevent user from
2741 * booting up a half-baked vdev.
2743 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Make "
2744 "sure to wait until resilver is done "
2745 "before rebooting.\n"));
2753 * Can't attach to or replace this type of vdev.
2756 uint64_t version
= zpool_get_prop_int(zhp
,
2757 ZPOOL_PROP_VERSION
, NULL
);
2760 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2761 "cannot replace a log with a spare"));
2762 else if (version
>= SPA_VERSION_MULTI_REPLACE
)
2763 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2764 "already in replacing/spare config; wait "
2765 "for completion or use 'zpool detach'"));
2767 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2768 "cannot replace a replacing device"));
2770 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2771 "can only attach to mirrors and top-level "
2774 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2779 * The new device must be a single disk.
2781 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2782 "new device must be a single disk"));
2783 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2787 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "%s is busy"),
2789 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2794 * The new device is too small.
2796 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2797 "device is too small"));
2798 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2803 * The new device has a different optimal sector size.
2805 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2806 "new device has a different optimal sector size; use the "
2807 "option '-o ashift=N' to override the optimal size"));
2808 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2813 * The resulting top-level vdev spec won't fit in the label.
2815 (void) zfs_error(hdl
, EZFS_DEVOVERFLOW
, msg
);
2819 (void) zpool_standard_error(hdl
, errno
, msg
);
2826 * Detach the specified device.
2829 zpool_vdev_detach(zpool_handle_t
*zhp
, const char *path
)
2831 zfs_cmd_t zc
= {"\0"};
2834 boolean_t avail_spare
, l2cache
;
2835 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2837 (void) snprintf(msg
, sizeof (msg
),
2838 dgettext(TEXT_DOMAIN
, "cannot detach %s"), path
);
2840 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2841 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2843 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2846 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2849 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2851 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2853 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_DETACH
, &zc
) == 0)
2860 * Can't detach from this type of vdev.
2862 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "only "
2863 "applicable to mirror and replacing vdevs"));
2864 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2869 * There are no other replicas of this device.
2871 (void) zfs_error(hdl
, EZFS_NOREPLICAS
, msg
);
2875 (void) zpool_standard_error(hdl
, errno
, msg
);
2882 * Find a mirror vdev in the source nvlist.
2884 * The mchild array contains a list of disks in one of the top-level mirrors
2885 * of the source pool. The schild array contains a list of disks that the
2886 * user specified on the command line. We loop over the mchild array to
2887 * see if any entry in the schild array matches.
2889 * If a disk in the mchild array is found in the schild array, we return
2890 * the index of that entry. Otherwise we return -1.
2893 find_vdev_entry(zpool_handle_t
*zhp
, nvlist_t
**mchild
, uint_t mchildren
,
2894 nvlist_t
**schild
, uint_t schildren
)
2898 for (mc
= 0; mc
< mchildren
; mc
++) {
2900 char *mpath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
2903 for (sc
= 0; sc
< schildren
; sc
++) {
2904 char *spath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
2906 boolean_t result
= (strcmp(mpath
, spath
) == 0);
2922 * Split a mirror pool. If newroot points to null, then a new nvlist
2923 * is generated and it is the responsibility of the caller to free it.
2926 zpool_vdev_split(zpool_handle_t
*zhp
, char *newname
, nvlist_t
**newroot
,
2927 nvlist_t
*props
, splitflags_t flags
)
2929 zfs_cmd_t zc
= {"\0"};
2931 nvlist_t
*tree
, *config
, **child
, **newchild
, *newconfig
= NULL
;
2932 nvlist_t
**varray
= NULL
, *zc_props
= NULL
;
2933 uint_t c
, children
, newchildren
, lastlog
= 0, vcount
, found
= 0;
2934 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2936 boolean_t freelist
= B_FALSE
, memory_err
= B_TRUE
;
2939 (void) snprintf(msg
, sizeof (msg
),
2940 dgettext(TEXT_DOMAIN
, "Unable to split %s"), zhp
->zpool_name
);
2942 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
2943 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
2945 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
2946 (void) fprintf(stderr
, gettext("Internal error: unable to "
2947 "retrieve pool configuration\n"));
2951 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &tree
)
2953 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
, &vers
) == 0);
2956 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
2957 if ((zc_props
= zpool_valid_proplist(hdl
, zhp
->zpool_name
,
2958 props
, vers
, flags
, msg
)) == NULL
)
2962 if (nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
2964 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2965 "Source pool is missing vdev tree"));
2966 nvlist_free(zc_props
);
2970 varray
= zfs_alloc(hdl
, children
* sizeof (nvlist_t
*));
2973 if (*newroot
== NULL
||
2974 nvlist_lookup_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
2975 &newchild
, &newchildren
) != 0)
2978 for (c
= 0; c
< children
; c
++) {
2979 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
2981 nvlist_t
**mchild
, *vdev
;
2986 * Unlike cache & spares, slogs are stored in the
2987 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2989 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2991 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
2993 if (is_log
|| is_hole
) {
2995 * Create a hole vdev and put it in the config.
2997 if (nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) != 0)
2999 if (nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
,
3000 VDEV_TYPE_HOLE
) != 0)
3002 if (nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_HOLE
,
3007 varray
[vcount
++] = vdev
;
3011 verify(nvlist_lookup_string(child
[c
], ZPOOL_CONFIG_TYPE
, &type
)
3013 if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
3014 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3015 "Source pool must be composed only of mirrors\n"));
3016 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3020 verify(nvlist_lookup_nvlist_array(child
[c
],
3021 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
3023 /* find or add an entry for this top-level vdev */
3024 if (newchildren
> 0 &&
3025 (entry
= find_vdev_entry(zhp
, mchild
, mchildren
,
3026 newchild
, newchildren
)) >= 0) {
3027 /* We found a disk that the user specified. */
3028 vdev
= mchild
[entry
];
3031 /* User didn't specify a disk for this vdev. */
3032 vdev
= mchild
[mchildren
- 1];
3035 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
3039 /* did we find every disk the user specified? */
3040 if (found
!= newchildren
) {
3041 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Device list must "
3042 "include at most one disk from each mirror"));
3043 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3047 /* Prepare the nvlist for populating. */
3048 if (*newroot
== NULL
) {
3049 if (nvlist_alloc(newroot
, NV_UNIQUE_NAME
, 0) != 0)
3052 if (nvlist_add_string(*newroot
, ZPOOL_CONFIG_TYPE
,
3053 VDEV_TYPE_ROOT
) != 0)
3056 verify(nvlist_remove_all(*newroot
, ZPOOL_CONFIG_CHILDREN
) == 0);
3059 /* Add all the children we found */
3060 if (nvlist_add_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
, varray
,
3061 lastlog
== 0 ? vcount
: lastlog
) != 0)
3065 * If we're just doing a dry run, exit now with success.
3068 memory_err
= B_FALSE
;
3073 /* now build up the config list & call the ioctl */
3074 if (nvlist_alloc(&newconfig
, NV_UNIQUE_NAME
, 0) != 0)
3077 if (nvlist_add_nvlist(newconfig
,
3078 ZPOOL_CONFIG_VDEV_TREE
, *newroot
) != 0 ||
3079 nvlist_add_string(newconfig
,
3080 ZPOOL_CONFIG_POOL_NAME
, newname
) != 0 ||
3081 nvlist_add_uint64(newconfig
, ZPOOL_CONFIG_VERSION
, vers
) != 0)
3085 * The new pool is automatically part of the namespace unless we
3086 * explicitly export it.
3089 zc
.zc_cookie
= ZPOOL_EXPORT_AFTER_SPLIT
;
3090 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3091 (void) strlcpy(zc
.zc_string
, newname
, sizeof (zc
.zc_string
));
3092 if (zcmd_write_conf_nvlist(hdl
, &zc
, newconfig
) != 0)
3094 if (zc_props
!= NULL
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
3097 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SPLIT
, &zc
) != 0) {
3098 retval
= zpool_standard_error(hdl
, errno
, msg
);
3103 memory_err
= B_FALSE
;
3106 if (varray
!= NULL
) {
3109 for (v
= 0; v
< vcount
; v
++)
3110 nvlist_free(varray
[v
]);
3113 zcmd_free_nvlists(&zc
);
3114 nvlist_free(zc_props
);
3115 nvlist_free(newconfig
);
3117 nvlist_free(*newroot
);
3125 return (no_memory(hdl
));
3131 * Remove the given device. Currently, this is supported only for hot spares,
3132 * cache, and log devices.
3135 zpool_vdev_remove(zpool_handle_t
*zhp
, const char *path
)
3137 zfs_cmd_t zc
= {"\0"};
3140 boolean_t avail_spare
, l2cache
, islog
;
3141 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3144 (void) snprintf(msg
, sizeof (msg
),
3145 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3147 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3148 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3150 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3152 * XXX - this should just go away.
3154 if (!avail_spare
&& !l2cache
&& !islog
) {
3155 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3156 "only inactive hot spares, cache, "
3157 "or log devices can be removed"));
3158 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3161 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
3162 if (islog
&& version
< SPA_VERSION_HOLES
) {
3163 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3164 "pool must be upgrade to support log removal"));
3165 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
3168 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
3170 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3173 return (zpool_standard_error(hdl
, errno
, msg
));
3177 * Clear the errors for the pool, or the particular device if specified.
3180 zpool_clear(zpool_handle_t
*zhp
, const char *path
, nvlist_t
*rewindnvl
)
3182 zfs_cmd_t zc
= {"\0"};
3185 zpool_rewind_policy_t policy
;
3186 boolean_t avail_spare
, l2cache
;
3187 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3188 nvlist_t
*nvi
= NULL
;
3192 (void) snprintf(msg
, sizeof (msg
),
3193 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3196 (void) snprintf(msg
, sizeof (msg
),
3197 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3200 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3202 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
,
3203 &l2cache
, NULL
)) == 0)
3204 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3207 * Don't allow error clearing for hot spares. Do allow
3208 * error clearing for l2cache devices.
3211 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3213 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
,
3217 zpool_get_rewind_policy(rewindnvl
, &policy
);
3218 zc
.zc_cookie
= policy
.zrp_request
;
3220 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zhp
->zpool_config_size
* 2) != 0)
3223 if (zcmd_write_src_nvlist(hdl
, &zc
, rewindnvl
) != 0)
3226 while ((error
= zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
)) != 0 &&
3228 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
3229 zcmd_free_nvlists(&zc
);
3234 if (!error
|| ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) &&
3235 errno
!= EPERM
&& errno
!= EACCES
)) {
3236 if (policy
.zrp_request
&
3237 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
3238 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nvi
);
3239 zpool_rewind_exclaim(hdl
, zc
.zc_name
,
3240 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0),
3244 zcmd_free_nvlists(&zc
);
3248 zcmd_free_nvlists(&zc
);
3249 return (zpool_standard_error(hdl
, errno
, msg
));
3253 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3256 zpool_vdev_clear(zpool_handle_t
*zhp
, uint64_t guid
)
3258 zfs_cmd_t zc
= {"\0"};
3260 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3262 (void) snprintf(msg
, sizeof (msg
),
3263 dgettext(TEXT_DOMAIN
, "cannot clear errors for %llx"),
3264 (u_longlong_t
)guid
);
3266 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3268 zc
.zc_cookie
= ZPOOL_NO_REWIND
;
3270 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_CLEAR
, &zc
) == 0)
3273 return (zpool_standard_error(hdl
, errno
, msg
));
3277 * Change the GUID for a pool.
3280 zpool_reguid(zpool_handle_t
*zhp
)
3283 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3284 zfs_cmd_t zc
= {"\0"};
3286 (void) snprintf(msg
, sizeof (msg
),
3287 dgettext(TEXT_DOMAIN
, "cannot reguid '%s'"), zhp
->zpool_name
);
3289 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3290 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REGUID
, &zc
) == 0)
3293 return (zpool_standard_error(hdl
, errno
, msg
));
3300 zpool_reopen(zpool_handle_t
*zhp
)
3302 zfs_cmd_t zc
= {"\0"};
3304 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3306 (void) snprintf(msg
, sizeof (msg
),
3307 dgettext(TEXT_DOMAIN
, "cannot reopen '%s'"),
3310 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3311 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REOPEN
, &zc
) == 0)
3313 return (zpool_standard_error(hdl
, errno
, msg
));
3316 #if defined(__sun__) || defined(__sun)
3318 * Convert from a devid string to a path.
3321 devid_to_path(char *devid_str
)
3326 devid_nmlist_t
*list
= NULL
;
3329 if (devid_str_decode(devid_str
, &devid
, &minor
) != 0)
3332 ret
= devid_deviceid_to_nmlist("/dev", devid
, minor
, &list
);
3334 devid_str_free(minor
);
3341 * In a case the strdup() fails, we will just return NULL below.
3343 path
= strdup(list
[0].devname
);
3345 devid_free_nmlist(list
);
3351 * Convert from a path to a devid string.
3354 path_to_devid(const char *path
)
3360 if ((fd
= open(path
, O_RDONLY
)) < 0)
3365 if (devid_get(fd
, &devid
) == 0) {
3366 if (devid_get_minor_name(fd
, &minor
) == 0)
3367 ret
= devid_str_encode(devid
, minor
);
3369 devid_str_free(minor
);
3378 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3379 * ignore any failure here, since a common case is for an unprivileged user to
3380 * type 'zpool status', and we'll display the correct information anyway.
3383 set_path(zpool_handle_t
*zhp
, nvlist_t
*nv
, const char *path
)
3385 zfs_cmd_t zc
= {"\0"};
3387 (void) strncpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3388 (void) strncpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
3389 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
3392 (void) ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_VDEV_SETPATH
, &zc
);
3397 * Remove partition suffix from a vdev path. Partition suffixes may take three
3398 * forms: "-partX", "pX", or "X", where X is a string of digits. The second
3399 * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
3400 * third case only occurs when preceded by a string matching the regular
3401 * expression "^([hsv]|xv)d[a-z]+", i.e. a scsi, ide, virtio or xen disk.
3403 * caller must free the returned string
3406 zfs_strip_partition(char *path
)
3408 char *tmp
= strdup(path
);
3409 char *part
= NULL
, *d
= NULL
;
3413 if ((part
= strstr(tmp
, "-part")) && part
!= tmp
) {
3415 } else if ((part
= strrchr(tmp
, 'p')) &&
3416 part
> tmp
+ 1 && isdigit(*(part
-1))) {
3418 } else if ((tmp
[0] == 'h' || tmp
[0] == 's' || tmp
[0] == 'v') &&
3420 for (d
= &tmp
[2]; isalpha(*d
); part
= ++d
) { }
3421 } else if (strncmp("xvd", tmp
, 3) == 0) {
3422 for (d
= &tmp
[3]; isalpha(*d
); part
= ++d
) { }
3424 if (part
&& d
&& *d
!= '\0') {
3425 for (; isdigit(*d
); d
++) { }
3434 * Same as zfs_strip_partition, but allows "/dev/" to be in the pathname
3439 * Returned string must be freed.
3442 zfs_strip_partition_path(char *path
)
3444 char *newpath
= strdup(path
);
3451 /* Point to "sda1" part of "/dev/sda1" */
3452 sd_offset
= strrchr(newpath
, '/') + 1;
3454 /* Get our new name "sda" */
3455 new_sd
= zfs_strip_partition(sd_offset
);
3461 /* Paste the "sda" where "sda1" was */
3462 strlcpy(sd_offset
, new_sd
, strlen(sd_offset
) + 1);
3464 /* Free temporary "sda" */
3470 #define PATH_BUF_LEN 64
3473 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3474 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3475 * We also check if this is a whole disk, in which case we strip off the
3476 * trailing 's0' slice name.
3478 * This routine is also responsible for identifying when disks have been
3479 * reconfigured in a new location. The kernel will have opened the device by
3480 * devid, but the path will still refer to the old location. To catch this, we
3481 * first do a path -> devid translation (which is fast for the common case). If
3482 * the devid matches, we're done. If not, we do a reverse devid -> path
3483 * translation and issue the appropriate ioctl() to update the path of the vdev.
3484 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3488 zpool_vdev_name(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, nvlist_t
*nv
,
3491 char *path
, *type
, *env
;
3493 char buf
[PATH_BUF_LEN
];
3494 char tmpbuf
[PATH_BUF_LEN
];
3496 env
= getenv("ZPOOL_VDEV_NAME_PATH");
3497 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3498 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3499 name_flags
|= VDEV_NAME_PATH
;
3501 env
= getenv("ZPOOL_VDEV_NAME_GUID");
3502 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3503 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3504 name_flags
|= VDEV_NAME_GUID
;
3506 env
= getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
3507 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3508 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3509 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
3511 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
, &value
) == 0 ||
3512 name_flags
& VDEV_NAME_GUID
) {
3513 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
);
3514 (void) snprintf(buf
, sizeof (buf
), "%llu", (u_longlong_t
)value
);
3516 } else if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
3517 #if defined(__sun__) || defined(__sun)
3519 * Live VDEV path updates to a kernel VDEV during a
3520 * zpool_vdev_name lookup are not supported on Linux.
3527 * If the device is dead (faulted, offline, etc) then don't
3528 * bother opening it. Otherwise we may be forcing the user to
3529 * open a misbehaving device, which can have undesirable
3532 if ((nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
3533 (uint64_t **)&vs
, &vsc
) != 0 ||
3534 vs
->vs_state
>= VDEV_STATE_DEGRADED
) &&
3536 nvlist_lookup_string(nv
, ZPOOL_CONFIG_DEVID
, &devid
) == 0) {
3538 * Determine if the current path is correct.
3540 char *newdevid
= path_to_devid(path
);
3542 if (newdevid
== NULL
||
3543 strcmp(devid
, newdevid
) != 0) {
3546 if ((newpath
= devid_to_path(devid
)) != NULL
) {
3548 * Update the path appropriately.
3550 set_path(zhp
, nv
, newpath
);
3551 if (nvlist_add_string(nv
,
3552 ZPOOL_CONFIG_PATH
, newpath
) == 0)
3553 verify(nvlist_lookup_string(nv
,
3561 devid_str_free(newdevid
);
3565 if (name_flags
& VDEV_NAME_FOLLOW_LINKS
) {
3566 char *rp
= realpath(path
, NULL
);
3568 strlcpy(buf
, rp
, sizeof (buf
));
3575 * For a block device only use the name.
3577 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
3578 if ((strcmp(type
, VDEV_TYPE_DISK
) == 0) &&
3579 !(name_flags
& VDEV_NAME_PATH
)) {
3580 path
= strrchr(path
, '/');
3585 * Remove the partition from the path it this is a whole disk.
3587 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
, &value
)
3588 == 0 && value
&& !(name_flags
& VDEV_NAME_PATH
)) {
3589 return (zfs_strip_partition(path
));
3592 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &path
) == 0);
3595 * If it's a raidz device, we need to stick in the parity level.
3597 if (strcmp(path
, VDEV_TYPE_RAIDZ
) == 0) {
3598 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
,
3600 (void) snprintf(buf
, sizeof (buf
), "%s%llu", path
,
3601 (u_longlong_t
)value
);
3606 * We identify each top-level vdev by using a <type-id>
3607 * naming convention.
3609 if (name_flags
& VDEV_NAME_TYPE_ID
) {
3611 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
3613 (void) snprintf(tmpbuf
, sizeof (tmpbuf
), "%s-%llu",
3614 path
, (u_longlong_t
)id
);
3619 return (zfs_strdup(hdl
, path
));
3623 zbookmark_mem_compare(const void *a
, const void *b
)
3625 return (memcmp(a
, b
, sizeof (zbookmark_phys_t
)));
3629 * Retrieve the persistent error log, uniquify the members, and return to the
3633 zpool_get_errlog(zpool_handle_t
*zhp
, nvlist_t
**nverrlistp
)
3635 zfs_cmd_t zc
= {"\0"};
3637 zbookmark_phys_t
*zb
= NULL
;
3641 * Retrieve the raw error list from the kernel. If the number of errors
3642 * has increased, allocate more space and continue until we get the
3645 verify(nvlist_lookup_uint64(zhp
->zpool_config
, ZPOOL_CONFIG_ERRCOUNT
,
3649 if ((zc
.zc_nvlist_dst
= (uintptr_t)zfs_alloc(zhp
->zpool_hdl
,
3650 count
* sizeof (zbookmark_phys_t
))) == (uintptr_t)NULL
)
3652 zc
.zc_nvlist_dst_size
= count
;
3653 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3655 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_ERROR_LOG
,
3657 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3658 if (errno
== ENOMEM
) {
3661 count
= zc
.zc_nvlist_dst_size
;
3662 dst
= zfs_alloc(zhp
->zpool_hdl
, count
*
3663 sizeof (zbookmark_phys_t
));
3666 zc
.zc_nvlist_dst
= (uintptr_t)dst
;
3676 * Sort the resulting bookmarks. This is a little confusing due to the
3677 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3678 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3679 * _not_ copied as part of the process. So we point the start of our
3680 * array appropriate and decrement the total number of elements.
3682 zb
= ((zbookmark_phys_t
*)(uintptr_t)zc
.zc_nvlist_dst
) +
3683 zc
.zc_nvlist_dst_size
;
3684 count
-= zc
.zc_nvlist_dst_size
;
3686 qsort(zb
, count
, sizeof (zbookmark_phys_t
), zbookmark_mem_compare
);
3688 verify(nvlist_alloc(nverrlistp
, 0, KM_SLEEP
) == 0);
3691 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3693 for (i
= 0; i
< count
; i
++) {
3696 /* ignoring zb_blkid and zb_level for now */
3697 if (i
> 0 && zb
[i
-1].zb_objset
== zb
[i
].zb_objset
&&
3698 zb
[i
-1].zb_object
== zb
[i
].zb_object
)
3701 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) != 0)
3703 if (nvlist_add_uint64(nv
, ZPOOL_ERR_DATASET
,
3704 zb
[i
].zb_objset
) != 0) {
3708 if (nvlist_add_uint64(nv
, ZPOOL_ERR_OBJECT
,
3709 zb
[i
].zb_object
) != 0) {
3713 if (nvlist_add_nvlist(*nverrlistp
, "ejk", nv
) != 0) {
3720 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3724 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3725 return (no_memory(zhp
->zpool_hdl
));
3729 * Upgrade a ZFS pool to the latest on-disk version.
3732 zpool_upgrade(zpool_handle_t
*zhp
, uint64_t new_version
)
3734 zfs_cmd_t zc
= {"\0"};
3735 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3737 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3738 zc
.zc_cookie
= new_version
;
3740 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_UPGRADE
, &zc
) != 0)
3741 return (zpool_standard_error_fmt(hdl
, errno
,
3742 dgettext(TEXT_DOMAIN
, "cannot upgrade '%s'"),
3748 zfs_save_arguments(int argc
, char **argv
, char *string
, int len
)
3752 (void) strlcpy(string
, basename(argv
[0]), len
);
3753 for (i
= 1; i
< argc
; i
++) {
3754 (void) strlcat(string
, " ", len
);
3755 (void) strlcat(string
, argv
[i
], len
);
3760 zpool_log_history(libzfs_handle_t
*hdl
, const char *message
)
3762 zfs_cmd_t zc
= {"\0"};
3766 args
= fnvlist_alloc();
3767 fnvlist_add_string(args
, "message", message
);
3768 err
= zcmd_write_src_nvlist(hdl
, &zc
, args
);
3770 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_LOG_HISTORY
, &zc
);
3772 zcmd_free_nvlists(&zc
);
3777 * Perform ioctl to get some command history of a pool.
3779 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3780 * logical offset of the history buffer to start reading from.
3782 * Upon return, 'off' is the next logical offset to read from and
3783 * 'len' is the actual amount of bytes read into 'buf'.
3786 get_history(zpool_handle_t
*zhp
, char *buf
, uint64_t *off
, uint64_t *len
)
3788 zfs_cmd_t zc
= {"\0"};
3789 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3791 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3793 zc
.zc_history
= (uint64_t)(uintptr_t)buf
;
3794 zc
.zc_history_len
= *len
;
3795 zc
.zc_history_offset
= *off
;
3797 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_HISTORY
, &zc
) != 0) {
3800 return (zfs_error_fmt(hdl
, EZFS_PERM
,
3801 dgettext(TEXT_DOMAIN
,
3802 "cannot show history for pool '%s'"),
3805 return (zfs_error_fmt(hdl
, EZFS_NOHISTORY
,
3806 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
3807 "'%s'"), zhp
->zpool_name
));
3809 return (zfs_error_fmt(hdl
, EZFS_BADVERSION
,
3810 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
3811 "'%s', pool must be upgraded"), zhp
->zpool_name
));
3813 return (zpool_standard_error_fmt(hdl
, errno
,
3814 dgettext(TEXT_DOMAIN
,
3815 "cannot get history for '%s'"), zhp
->zpool_name
));
3819 *len
= zc
.zc_history_len
;
3820 *off
= zc
.zc_history_offset
;
3826 * Process the buffer of nvlists, unpacking and storing each nvlist record
3827 * into 'records'. 'leftover' is set to the number of bytes that weren't
3828 * processed as there wasn't a complete record.
3831 zpool_history_unpack(char *buf
, uint64_t bytes_read
, uint64_t *leftover
,
3832 nvlist_t
***records
, uint_t
*numrecords
)
3838 while (bytes_read
> sizeof (reclen
)) {
3840 /* get length of packed record (stored as little endian) */
3841 for (i
= 0, reclen
= 0; i
< sizeof (reclen
); i
++)
3842 reclen
+= (uint64_t)(((uchar_t
*)buf
)[i
]) << (8*i
);
3844 if (bytes_read
< sizeof (reclen
) + reclen
)
3848 if (nvlist_unpack(buf
+ sizeof (reclen
), reclen
, &nv
, 0) != 0)
3850 bytes_read
-= sizeof (reclen
) + reclen
;
3851 buf
+= sizeof (reclen
) + reclen
;
3853 /* add record to nvlist array */
3855 if (ISP2(*numrecords
+ 1)) {
3856 *records
= realloc(*records
,
3857 *numrecords
* 2 * sizeof (nvlist_t
*));
3859 (*records
)[*numrecords
- 1] = nv
;
3862 *leftover
= bytes_read
;
3867 * Retrieve the command history of a pool.
3870 zpool_get_history(zpool_handle_t
*zhp
, nvlist_t
**nvhisp
)
3873 int buflen
= 128 * 1024;
3875 nvlist_t
**records
= NULL
;
3876 uint_t numrecords
= 0;
3879 buf
= malloc(buflen
);
3883 uint64_t bytes_read
= buflen
;
3886 if ((err
= get_history(zhp
, buf
, &off
, &bytes_read
)) != 0)
3889 /* if nothing else was read in, we're at EOF, just return */
3893 if ((err
= zpool_history_unpack(buf
, bytes_read
,
3894 &leftover
, &records
, &numrecords
)) != 0)
3897 if (leftover
== bytes_read
) {
3899 * no progress made, because buffer is not big enough
3900 * to hold this record; resize and retry.
3904 buf
= malloc(buflen
);
3915 verify(nvlist_alloc(nvhisp
, NV_UNIQUE_NAME
, 0) == 0);
3916 verify(nvlist_add_nvlist_array(*nvhisp
, ZPOOL_HIST_RECORD
,
3917 records
, numrecords
) == 0);
3919 for (i
= 0; i
< numrecords
; i
++)
3920 nvlist_free(records
[i
]);
3927 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
3928 * If there is a new event available 'nvp' will contain a newly allocated
3929 * nvlist and 'dropped' will be set to the number of missed events since
3930 * the last call to this function. When 'nvp' is set to NULL it indicates
3931 * no new events are available. In either case the function returns 0 and
3932 * it is up to the caller to free 'nvp'. In the case of a fatal error the
3933 * function will return a non-zero value. When the function is called in
3934 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
3935 * it will not return until a new event is available.
3938 zpool_events_next(libzfs_handle_t
*hdl
, nvlist_t
**nvp
,
3939 int *dropped
, unsigned flags
, int zevent_fd
)
3941 zfs_cmd_t zc
= {"\0"};
3946 zc
.zc_cleanup_fd
= zevent_fd
;
3948 if (flags
& ZEVENT_NONBLOCK
)
3949 zc
.zc_guid
= ZEVENT_NONBLOCK
;
3951 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, ZEVENT_SIZE
) != 0)
3955 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_NEXT
, &zc
) != 0) {
3958 error
= zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
3959 dgettext(TEXT_DOMAIN
, "zfs shutdown"));
3962 /* Blocking error case should not occur */
3963 if (!(flags
& ZEVENT_NONBLOCK
))
3964 error
= zpool_standard_error_fmt(hdl
, errno
,
3965 dgettext(TEXT_DOMAIN
, "cannot get event"));
3969 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
3970 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
3971 dgettext(TEXT_DOMAIN
, "cannot get event"));
3977 error
= zpool_standard_error_fmt(hdl
, errno
,
3978 dgettext(TEXT_DOMAIN
, "cannot get event"));
3983 error
= zcmd_read_dst_nvlist(hdl
, &zc
, nvp
);
3987 *dropped
= (int)zc
.zc_cookie
;
3989 zcmd_free_nvlists(&zc
);
3998 zpool_events_clear(libzfs_handle_t
*hdl
, int *count
)
4000 zfs_cmd_t zc
= {"\0"};
4003 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
4004 "cannot clear events"));
4006 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_CLEAR
, &zc
) != 0)
4007 return (zpool_standard_error_fmt(hdl
, errno
, msg
));
4010 *count
= (int)zc
.zc_cookie
; /* # of events cleared */
4016 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4017 * the passed zevent_fd file handle. On success zero is returned,
4018 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4021 zpool_events_seek(libzfs_handle_t
*hdl
, uint64_t eid
, int zevent_fd
)
4023 zfs_cmd_t zc
= {"\0"};
4027 zc
.zc_cleanup_fd
= zevent_fd
;
4029 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_SEEK
, &zc
) != 0) {
4032 error
= zfs_error_fmt(hdl
, EZFS_NOENT
,
4033 dgettext(TEXT_DOMAIN
, "cannot get event"));
4037 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
4038 dgettext(TEXT_DOMAIN
, "cannot get event"));
4042 error
= zpool_standard_error_fmt(hdl
, errno
,
4043 dgettext(TEXT_DOMAIN
, "cannot get event"));
4052 zpool_obj_to_path(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4053 char *pathname
, size_t len
)
4055 zfs_cmd_t zc
= {"\0"};
4056 boolean_t mounted
= B_FALSE
;
4057 char *mntpnt
= NULL
;
4058 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
4061 /* special case for the MOS */
4062 (void) snprintf(pathname
, len
, "<metadata>:<0x%llx>",
4067 /* get the dataset's name */
4068 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4070 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
,
4071 ZFS_IOC_DSOBJ_TO_DSNAME
, &zc
) != 0) {
4072 /* just write out a path of two object numbers */
4073 (void) snprintf(pathname
, len
, "<0x%llx>:<0x%llx>",
4074 (longlong_t
)dsobj
, (longlong_t
)obj
);
4077 (void) strlcpy(dsname
, zc
.zc_value
, sizeof (dsname
));
4079 /* find out if the dataset is mounted */
4080 mounted
= is_mounted(zhp
->zpool_hdl
, dsname
, &mntpnt
);
4082 /* get the corrupted object's path */
4083 (void) strlcpy(zc
.zc_name
, dsname
, sizeof (zc
.zc_name
));
4085 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_OBJ_TO_PATH
,
4088 (void) snprintf(pathname
, len
, "%s%s", mntpnt
,
4091 (void) snprintf(pathname
, len
, "%s:%s",
4092 dsname
, zc
.zc_value
);
4095 (void) snprintf(pathname
, len
, "%s:<0x%llx>", dsname
,
4102 * Read the EFI label from the config, if a label does not exist then
4103 * pass back the error to the caller. If the caller has passed a non-NULL
4104 * diskaddr argument then we set it to the starting address of the EFI
4108 read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
)
4112 char diskname
[MAXPATHLEN
];
4115 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PATH
, &path
) != 0)
4118 (void) snprintf(diskname
, sizeof (diskname
), "%s%s", DISK_ROOT
,
4119 strrchr(path
, '/'));
4120 if ((fd
= open(diskname
, O_RDWR
|O_DIRECT
)) >= 0) {
4121 struct dk_gpt
*vtoc
;
4123 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) >= 0) {
4125 *sb
= vtoc
->efi_parts
[0].p_start
;
4134 * determine where a partition starts on a disk in the current
4138 find_start_block(nvlist_t
*config
)
4142 diskaddr_t sb
= MAXOFFSET_T
;
4145 if (nvlist_lookup_nvlist_array(config
,
4146 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) != 0) {
4147 if (nvlist_lookup_uint64(config
,
4148 ZPOOL_CONFIG_WHOLE_DISK
,
4149 &wholedisk
) != 0 || !wholedisk
) {
4150 return (MAXOFFSET_T
);
4152 if (read_efi_label(config
, &sb
) < 0)
4157 for (c
= 0; c
< children
; c
++) {
4158 sb
= find_start_block(child
[c
]);
4159 if (sb
!= MAXOFFSET_T
) {
4163 return (MAXOFFSET_T
);
4167 zpool_label_disk_check(char *path
)
4169 struct dk_gpt
*vtoc
;
4172 if ((fd
= open(path
, O_RDWR
|O_DIRECT
)) < 0)
4175 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) != 0) {
4180 if (vtoc
->efi_flags
& EFI_GPT_PRIMARY_CORRUPT
) {
4192 * Generate a unique partition name for the ZFS member. Partitions must
4193 * have unique names to ensure udev will be able to create symlinks under
4194 * /dev/disk/by-partlabel/ for all pool members. The partition names are
4195 * of the form <pool>-<unique-id>.
4198 zpool_label_name(char *label_name
, int label_size
)
4203 fd
= open("/dev/urandom", O_RDONLY
);
4205 if (read(fd
, &id
, sizeof (id
)) != sizeof (id
))
4212 id
= (((uint64_t)rand()) << 32) | (uint64_t)rand();
4214 snprintf(label_name
, label_size
, "zfs-%016llx", (u_longlong_t
)id
);
4218 * Label an individual disk. The name provided is the short name,
4219 * stripped of any leading /dev path.
4222 zpool_label_disk(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, char *name
)
4224 char path
[MAXPATHLEN
];
4225 struct dk_gpt
*vtoc
;
4227 size_t resv
= EFI_MIN_RESV_SIZE
;
4228 uint64_t slice_size
;
4229 diskaddr_t start_block
;
4232 /* prepare an error message just in case */
4233 (void) snprintf(errbuf
, sizeof (errbuf
),
4234 dgettext(TEXT_DOMAIN
, "cannot label '%s'"), name
);
4239 #if defined(__sun__) || defined(__sun)
4240 if (zpool_is_bootable(zhp
)) {
4241 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4242 "EFI labeled devices are not supported on root "
4244 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, errbuf
));
4248 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
4249 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
4251 if (zhp
->zpool_start_block
== 0)
4252 start_block
= find_start_block(nvroot
);
4254 start_block
= zhp
->zpool_start_block
;
4255 zhp
->zpool_start_block
= start_block
;
4258 start_block
= NEW_START_BLOCK
;
4261 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4263 if ((fd
= open(path
, O_RDWR
|O_DIRECT
|O_EXCL
)) < 0) {
4265 * This shouldn't happen. We've long since verified that this
4266 * is a valid device.
4268 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
4269 "label '%s': unable to open device: %d"), path
, errno
);
4270 return (zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
));
4273 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, &vtoc
) != 0) {
4275 * The only way this can fail is if we run out of memory, or we
4276 * were unable to read the disk's capacity
4278 if (errno
== ENOMEM
)
4279 (void) no_memory(hdl
);
4282 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
4283 "label '%s': unable to read disk capacity"), path
);
4285 return (zfs_error(hdl
, EZFS_NOCAP
, errbuf
));
4288 slice_size
= vtoc
->efi_last_u_lba
+ 1;
4289 slice_size
-= EFI_MIN_RESV_SIZE
;
4290 if (start_block
== MAXOFFSET_T
)
4291 start_block
= NEW_START_BLOCK
;
4292 slice_size
-= start_block
;
4293 slice_size
= P2ALIGN(slice_size
, PARTITION_END_ALIGNMENT
);
4295 vtoc
->efi_parts
[0].p_start
= start_block
;
4296 vtoc
->efi_parts
[0].p_size
= slice_size
;
4299 * Why we use V_USR: V_BACKUP confuses users, and is considered
4300 * disposable by some EFI utilities (since EFI doesn't have a backup
4301 * slice). V_UNASSIGNED is supposed to be used only for zero size
4302 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4303 * etc. were all pretty specific. V_USR is as close to reality as we
4304 * can get, in the absence of V_OTHER.
4306 vtoc
->efi_parts
[0].p_tag
= V_USR
;
4307 zpool_label_name(vtoc
->efi_parts
[0].p_name
, EFI_PART_NAME_LEN
);
4309 vtoc
->efi_parts
[8].p_start
= slice_size
+ start_block
;
4310 vtoc
->efi_parts
[8].p_size
= resv
;
4311 vtoc
->efi_parts
[8].p_tag
= V_RESERVED
;
4313 if ((rval
= efi_write(fd
, vtoc
)) != 0 || (rval
= efi_rescan(fd
)) != 0) {
4315 * Some block drivers (like pcata) may not support EFI
4316 * GPT labels. Print out a helpful error message dir-
4317 * ecting the user to manually label the disk and give
4323 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "try using "
4324 "parted(8) and then provide a specific slice: %d"), rval
);
4325 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4331 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4332 (void) zfs_append_partition(path
, MAXPATHLEN
);
4334 /* Wait to udev to signal use the device has settled. */
4335 rval
= zpool_label_disk_wait(path
, DISK_LABEL_WAIT
);
4337 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "failed to "
4338 "detect device partitions on '%s': %d"), path
, rval
);
4339 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4342 /* We can't be to paranoid. Read the label back and verify it. */
4343 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4344 rval
= zpool_label_disk_check(path
);
4346 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "freshly written "
4347 "EFI label on '%s' is damaged. Ensure\nthis device "
4348 "is not in in use, and is functioning properly: %d"),
4350 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4357 * Allocate and return the underlying device name for a device mapper device.
4358 * If a device mapper device maps to multiple devices, return the first device.
4360 * For example, dm_name = "/dev/dm-0" could return "/dev/sda". Symlinks to a
4361 * DM device (like /dev/disk/by-vdev/A0) are also allowed.
4363 * Returns device name, or NULL on error or no match. If dm_name is not a DM
4364 * device then return NULL.
4366 * NOTE: The returned name string must be *freed*.
4369 dm_get_underlying_path(char *dm_name
)
4379 if (dm_name
== NULL
)
4382 /* dm name may be a symlink (like /dev/disk/by-vdev/A0) */
4383 realp
= realpath(dm_name
, NULL
);
4388 * If they preface 'dev' with a path (like "/dev") then strip it off.
4389 * We just want the 'dm-N' part.
4391 tmp
= strrchr(realp
, '/');
4393 dev_str
= tmp
+ 1; /* +1 since we want the chr after '/' */
4397 size
= asprintf(&tmp
, "/sys/block/%s/slaves/", dev_str
);
4398 if (size
== -1 || !tmp
)
4405 /* Return first sd* entry in /sys/block/dm-N/slaves/ */
4406 while ((ep
= readdir(dp
))) {
4407 if (ep
->d_type
!= DT_DIR
) { /* skip "." and ".." dirs */
4408 size
= asprintf(&path
, "/dev/%s", ep
->d_name
);
4422 * Return 1 if device is a device mapper or multipath device.
4426 zfs_dev_is_dm(char *dev_name
)
4430 tmp
= dm_get_underlying_path(dev_name
);
4439 * Lookup the underlying device for a device name
4441 * Often you'll have a symlink to a device, a partition device,
4442 * or a multipath device, and want to look up the underlying device.
4443 * This function returns the underlying device name. If the device
4444 * name is already the underlying device, then just return the same
4445 * name. If the device is a DM device with multiple underlying devices
4446 * then return the first one.
4450 * 1. /dev/disk/by-id/ata-QEMU_HARDDISK_QM00001 -> ../../sda
4451 * dev_name: /dev/disk/by-id/ata-QEMU_HARDDISK_QM00001
4454 * 2. /dev/mapper/mpatha (made up of /dev/sda and /dev/sdb)
4455 * dev_name: /dev/mapper/mpatha
4456 * returns: /dev/sda (first device)
4458 * 3. /dev/sda (already the underlying device)
4459 * dev_name: /dev/sda
4462 * 4. /dev/dm-3 (mapped to /dev/sda)
4463 * dev_name: /dev/dm-3
4466 * 5. /dev/disk/by-id/scsi-0QEMU_drive-scsi0-0-0-0-part9 -> ../../sdb9
4467 * dev_name: /dev/disk/by-id/scsi-0QEMU_drive-scsi0-0-0-0-part9
4470 * 6. /dev/disk/by-uuid/5df030cf-3cd9-46e4-8e99-3ccb462a4e9a -> ../dev/sda2
4471 * dev_name: /dev/disk/by-uuid/5df030cf-3cd9-46e4-8e99-3ccb462a4e9a
4474 * Returns underlying device name, or NULL on error or no match.
4476 * NOTE: The returned name string must be *freed*.
4479 zfs_get_underlying_path(char *dev_name
)
4484 if (dev_name
== NULL
)
4487 tmp
= dm_get_underlying_path(dev_name
);
4489 /* dev_name not a DM device, so just un-symlinkize it */
4491 tmp
= realpath(dev_name
, NULL
);
4494 name
= zfs_strip_partition_path(tmp
);
4502 * Given a dev name like "sda", return the full enclosure sysfs path to
4503 * the disk. You can also pass in the name with "/dev" prepended
4504 * to it (like /dev/sda).
4506 * For example, disk "sda" in enclosure slot 1:
4508 * returns: "/sys/class/enclosure/1:0:3:0/Slot 1"
4510 * 'dev' must be a non-devicemapper device.
4512 * Returned string must be freed.
4515 zfs_get_enclosure_sysfs_path(char *dev_name
)
4519 char buf
[MAXPATHLEN
];
4527 if (dev_name
== NULL
)
4530 /* If they preface 'dev' with a path (like "/dev") then strip it off */
4531 tmp1
= strrchr(dev_name
, '/');
4533 dev_name
= tmp1
+ 1; /* +1 since we want the chr after '/' */
4535 tmpsize
= asprintf(&tmp1
, "/sys/block/%s/device", dev_name
);
4536 if (tmpsize
== -1 || tmp1
== NULL
) {
4543 tmp1
= NULL
; /* To make free() at the end a NOP */
4548 * Look though all sysfs entries in /sys/block/<dev>/device for
4549 * the enclosure symlink.
4551 while ((ep
= readdir(dp
))) {
4552 /* Ignore everything that's not our enclosure_device link */
4553 if (strstr(ep
->d_name
, "enclosure_device") == NULL
)
4556 if (asprintf(&tmp2
, "%s/%s", tmp1
, ep
->d_name
) == -1 ||
4560 size
= readlink(tmp2
, buf
, sizeof (buf
));
4562 /* Did readlink fail or crop the link name? */
4563 if (size
== -1 || size
>= sizeof (buf
)) {
4565 tmp2
= NULL
; /* To make free() at the end a NOP */
4570 * We got a valid link. readlink() doesn't terminate strings
4571 * so we have to do it.
4576 * Our link will look like:
4578 * "../../../../port-11:1:2/..STUFF../enclosure/1:0:3:0/SLOT 1"
4580 * We want to grab the "enclosure/1:0:3:0/SLOT 1" part
4582 tmp3
= strstr(buf
, "enclosure");
4586 if (asprintf(&path
, "/sys/class/%s", tmp3
) == -1) {
4587 /* If asprintf() fails, 'path' is undefined */