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 * API compatibility wrapper around zpool_get_prop_literal
242 zpool_get_prop(zpool_handle_t
*zhp
, zpool_prop_t prop
, char *buf
, size_t len
,
243 zprop_source_t
*srctype
)
245 return (zpool_get_prop_literal(zhp
, prop
, buf
, len
, srctype
, B_FALSE
));
249 * Get a zpool property value for 'prop' and return the value in
250 * a pre-allocated buffer.
253 zpool_get_prop_literal(zpool_handle_t
*zhp
, zpool_prop_t prop
, char *buf
,
254 size_t len
, zprop_source_t
*srctype
, boolean_t literal
)
258 zprop_source_t src
= ZPROP_SRC_NONE
;
263 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
265 case ZPOOL_PROP_NAME
:
266 (void) strlcpy(buf
, zpool_get_name(zhp
), len
);
269 case ZPOOL_PROP_HEALTH
:
270 (void) strlcpy(buf
, "FAULTED", len
);
273 case ZPOOL_PROP_GUID
:
274 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
275 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
278 case ZPOOL_PROP_ALTROOT
:
279 case ZPOOL_PROP_CACHEFILE
:
280 case ZPOOL_PROP_COMMENT
:
281 if (zhp
->zpool_props
!= NULL
||
282 zpool_get_all_props(zhp
) == 0) {
284 zpool_get_prop_string(zhp
, prop
, &src
),
292 (void) strlcpy(buf
, "-", len
);
301 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
) &&
302 prop
!= ZPOOL_PROP_NAME
)
305 switch (zpool_prop_get_type(prop
)) {
306 case PROP_TYPE_STRING
:
307 (void) strlcpy(buf
, zpool_get_prop_string(zhp
, prop
, &src
),
311 case PROP_TYPE_NUMBER
:
312 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
315 case ZPOOL_PROP_SIZE
:
316 case ZPOOL_PROP_ALLOCATED
:
317 case ZPOOL_PROP_FREE
:
318 case ZPOOL_PROP_FREEING
:
319 case ZPOOL_PROP_LEAKED
:
320 case ZPOOL_PROP_ASHIFT
:
322 (void) snprintf(buf
, len
, "%llu",
323 (u_longlong_t
)intval
);
325 (void) zfs_nicenum(intval
, buf
, len
);
328 case ZPOOL_PROP_EXPANDSZ
:
330 (void) strlcpy(buf
, "-", len
);
331 } else if (literal
) {
332 (void) snprintf(buf
, len
, "%llu",
333 (u_longlong_t
)intval
);
335 (void) zfs_nicenum(intval
, buf
, len
);
339 case ZPOOL_PROP_CAPACITY
:
340 (void) snprintf(buf
, len
, "%llu%%",
341 (u_longlong_t
)intval
);
344 case ZPOOL_PROP_FRAGMENTATION
:
345 if (intval
== UINT64_MAX
) {
346 (void) strlcpy(buf
, "-", len
);
348 (void) snprintf(buf
, len
, "%llu%%",
349 (u_longlong_t
)intval
);
353 case ZPOOL_PROP_DEDUPRATIO
:
354 (void) snprintf(buf
, len
, "%llu.%02llux",
355 (u_longlong_t
)(intval
/ 100),
356 (u_longlong_t
)(intval
% 100));
359 case ZPOOL_PROP_HEALTH
:
360 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
361 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
362 verify(nvlist_lookup_uint64_array(nvroot
,
363 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
366 (void) strlcpy(buf
, zpool_state_to_name(intval
,
369 case ZPOOL_PROP_VERSION
:
370 if (intval
>= SPA_VERSION_FEATURES
) {
371 (void) snprintf(buf
, len
, "-");
376 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
380 case PROP_TYPE_INDEX
:
381 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
382 if (zpool_prop_index_to_string(prop
, intval
, &strval
)
385 (void) strlcpy(buf
, strval
, len
);
399 * Check if the bootfs name has the same pool name as it is set to.
400 * Assuming bootfs is a valid dataset name.
403 bootfs_name_valid(const char *pool
, char *bootfs
)
405 int len
= strlen(pool
);
407 if (!zfs_name_valid(bootfs
, ZFS_TYPE_FILESYSTEM
|ZFS_TYPE_SNAPSHOT
))
410 if (strncmp(pool
, bootfs
, len
) == 0 &&
411 (bootfs
[len
] == '/' || bootfs
[len
] == '\0'))
417 #if defined(__sun__) || defined(__sun)
419 * Inspect the configuration to determine if any of the devices contain
423 pool_uses_efi(nvlist_t
*config
)
428 if (nvlist_lookup_nvlist_array(config
, ZPOOL_CONFIG_CHILDREN
,
429 &child
, &children
) != 0)
430 return (read_efi_label(config
, NULL
) >= 0);
432 for (c
= 0; c
< children
; c
++) {
433 if (pool_uses_efi(child
[c
]))
441 zpool_is_bootable(zpool_handle_t
*zhp
)
443 char bootfs
[ZPOOL_MAXNAMELEN
];
445 return (zpool_get_prop(zhp
, ZPOOL_PROP_BOOTFS
, bootfs
,
446 sizeof (bootfs
), NULL
) == 0 && strncmp(bootfs
, "-",
447 sizeof (bootfs
)) != 0);
452 * Given an nvlist of zpool properties to be set, validate that they are
453 * correct, and parse any numeric properties (index, boolean, etc) if they are
454 * specified as strings.
457 zpool_valid_proplist(libzfs_handle_t
*hdl
, const char *poolname
,
458 nvlist_t
*props
, uint64_t version
, prop_flags_t flags
, char *errbuf
)
466 struct stat64 statbuf
;
470 if (nvlist_alloc(&retprops
, NV_UNIQUE_NAME
, 0) != 0) {
471 (void) no_memory(hdl
);
476 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
477 const char *propname
= nvpair_name(elem
);
479 prop
= zpool_name_to_prop(propname
);
480 if (prop
== ZPROP_INVAL
&& zpool_prop_feature(propname
)) {
482 char *fname
= strchr(propname
, '@') + 1;
484 err
= zfeature_lookup_name(fname
, NULL
);
486 ASSERT3U(err
, ==, ENOENT
);
487 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
488 "invalid feature '%s'"), fname
);
489 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
493 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
494 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
495 "'%s' must be a string"), propname
);
496 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
500 (void) nvpair_value_string(elem
, &strval
);
501 if (strcmp(strval
, ZFS_FEATURE_ENABLED
) != 0) {
502 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
503 "property '%s' can only be set to "
504 "'enabled'"), propname
);
505 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
509 if (nvlist_add_uint64(retprops
, propname
, 0) != 0) {
510 (void) no_memory(hdl
);
517 * Make sure this property is valid and applies to this type.
519 if (prop
== ZPROP_INVAL
) {
520 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
521 "invalid property '%s'"), propname
);
522 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
526 if (zpool_prop_readonly(prop
)) {
527 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
528 "is readonly"), propname
);
529 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
533 if (zprop_parse_value(hdl
, elem
, prop
, ZFS_TYPE_POOL
, retprops
,
534 &strval
, &intval
, errbuf
) != 0)
538 * Perform additional checking for specific properties.
543 case ZPOOL_PROP_VERSION
:
544 if (intval
< version
||
545 !SPA_VERSION_IS_SUPPORTED(intval
)) {
546 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
547 "property '%s' number %d is invalid."),
549 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
554 case ZPOOL_PROP_ASHIFT
:
556 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
557 "property '%s' can only be set at "
558 "creation time"), propname
);
559 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
563 if (intval
!= 0 && (intval
< 9 || intval
> 13)) {
564 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
565 "property '%s' number %d is invalid."),
567 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
572 case ZPOOL_PROP_BOOTFS
:
573 if (flags
.create
|| flags
.import
) {
574 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
575 "property '%s' cannot be set at creation "
576 "or import time"), propname
);
577 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
581 if (version
< SPA_VERSION_BOOTFS
) {
582 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
583 "pool must be upgraded to support "
584 "'%s' property"), propname
);
585 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
590 * bootfs property value has to be a dataset name and
591 * the dataset has to be in the same pool as it sets to.
593 if (strval
[0] != '\0' && !bootfs_name_valid(poolname
,
595 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
596 "is an invalid name"), strval
);
597 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
601 if ((zhp
= zpool_open_canfail(hdl
, poolname
)) == NULL
) {
602 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
603 "could not open pool '%s'"), poolname
);
604 (void) zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
);
607 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
608 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
610 #if defined(__sun__) || defined(__sun)
612 * bootfs property cannot be set on a disk which has
615 if (pool_uses_efi(nvroot
)) {
616 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
617 "property '%s' not supported on "
618 "EFI labeled devices"), propname
);
619 (void) zfs_error(hdl
, EZFS_POOL_NOTSUP
, errbuf
);
627 case ZPOOL_PROP_ALTROOT
:
628 if (!flags
.create
&& !flags
.import
) {
629 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
630 "property '%s' can only be set during pool "
631 "creation or import"), propname
);
632 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
636 if (strval
[0] != '/') {
637 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
638 "bad alternate root '%s'"), strval
);
639 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
644 case ZPOOL_PROP_CACHEFILE
:
645 if (strval
[0] == '\0')
648 if (strcmp(strval
, "none") == 0)
651 if (strval
[0] != '/') {
652 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
653 "property '%s' must be empty, an "
654 "absolute path, or 'none'"), propname
);
655 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
659 slash
= strrchr(strval
, '/');
661 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
662 strcmp(slash
, "/..") == 0) {
663 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
664 "'%s' is not a valid file"), strval
);
665 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
671 if (strval
[0] != '\0' &&
672 (stat64(strval
, &statbuf
) != 0 ||
673 !S_ISDIR(statbuf
.st_mode
))) {
674 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
675 "'%s' is not a valid directory"),
677 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
684 case ZPOOL_PROP_COMMENT
:
685 for (check
= strval
; *check
!= '\0'; check
++) {
686 if (!isprint(*check
)) {
688 dgettext(TEXT_DOMAIN
,
689 "comment may only have printable "
691 (void) zfs_error(hdl
, EZFS_BADPROP
,
696 if (strlen(strval
) > ZPROP_MAX_COMMENT
) {
697 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
698 "comment must not exceed %d characters"),
700 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
704 case ZPOOL_PROP_READONLY
:
706 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
707 "property '%s' can only be set at "
708 "import time"), propname
);
709 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
713 case ZPOOL_PROP_TNAME
:
715 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
716 "property '%s' can only be set at "
717 "creation time"), propname
);
718 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
727 nvlist_free(retprops
);
732 * Set zpool property : propname=propval.
735 zpool_set_prop(zpool_handle_t
*zhp
, const char *propname
, const char *propval
)
737 zfs_cmd_t zc
= {"\0"};
740 nvlist_t
*nvl
= NULL
;
743 prop_flags_t flags
= { 0 };
745 (void) snprintf(errbuf
, sizeof (errbuf
),
746 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
749 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
750 return (no_memory(zhp
->zpool_hdl
));
752 if (nvlist_add_string(nvl
, propname
, propval
) != 0) {
754 return (no_memory(zhp
->zpool_hdl
));
757 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
758 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
759 zhp
->zpool_name
, nvl
, version
, flags
, errbuf
)) == NULL
) {
768 * Execute the corresponding ioctl() to set this property.
770 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
772 if (zcmd_write_src_nvlist(zhp
->zpool_hdl
, &zc
, nvl
) != 0) {
777 ret
= zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_SET_PROPS
, &zc
);
779 zcmd_free_nvlists(&zc
);
783 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
785 (void) zpool_props_refresh(zhp
);
791 zpool_expand_proplist(zpool_handle_t
*zhp
, zprop_list_t
**plp
)
793 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
795 char buf
[ZFS_MAXPROPLEN
];
796 nvlist_t
*features
= NULL
;
799 boolean_t firstexpand
= (NULL
== *plp
);
802 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_POOL
) != 0)
806 while (*last
!= NULL
)
807 last
= &(*last
)->pl_next
;
810 features
= zpool_get_features(zhp
);
812 if ((*plp
)->pl_all
&& firstexpand
) {
813 for (i
= 0; i
< SPA_FEATURES
; i
++) {
814 zprop_list_t
*entry
= zfs_alloc(hdl
,
815 sizeof (zprop_list_t
));
816 entry
->pl_prop
= ZPROP_INVAL
;
817 entry
->pl_user_prop
= zfs_asprintf(hdl
, "feature@%s",
818 spa_feature_table
[i
].fi_uname
);
819 entry
->pl_width
= strlen(entry
->pl_user_prop
);
820 entry
->pl_all
= B_TRUE
;
823 last
= &entry
->pl_next
;
827 /* add any unsupported features */
828 for (nvp
= nvlist_next_nvpair(features
, NULL
);
829 nvp
!= NULL
; nvp
= nvlist_next_nvpair(features
, nvp
)) {
834 if (zfeature_is_supported(nvpair_name(nvp
)))
837 propname
= zfs_asprintf(hdl
, "unsupported@%s",
841 * Before adding the property to the list make sure that no
842 * other pool already added the same property.
846 while (entry
!= NULL
) {
847 if (entry
->pl_user_prop
!= NULL
&&
848 strcmp(propname
, entry
->pl_user_prop
) == 0) {
852 entry
= entry
->pl_next
;
859 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
860 entry
->pl_prop
= ZPROP_INVAL
;
861 entry
->pl_user_prop
= propname
;
862 entry
->pl_width
= strlen(entry
->pl_user_prop
);
863 entry
->pl_all
= B_TRUE
;
866 last
= &entry
->pl_next
;
869 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
874 if (entry
->pl_prop
!= ZPROP_INVAL
&&
875 zpool_get_prop(zhp
, entry
->pl_prop
, buf
, sizeof (buf
),
877 if (strlen(buf
) > entry
->pl_width
)
878 entry
->pl_width
= strlen(buf
);
886 * Get the state for the given feature on the given ZFS pool.
889 zpool_prop_get_feature(zpool_handle_t
*zhp
, const char *propname
, char *buf
,
893 boolean_t found
= B_FALSE
;
894 nvlist_t
*features
= zpool_get_features(zhp
);
896 const char *feature
= strchr(propname
, '@') + 1;
898 supported
= zpool_prop_feature(propname
);
899 ASSERT(supported
|| zpool_prop_unsupported(propname
));
902 * Convert from feature name to feature guid. This conversion is
903 * unecessary for unsupported@... properties because they already
910 ret
= zfeature_lookup_name(feature
, &fid
);
912 (void) strlcpy(buf
, "-", len
);
915 feature
= spa_feature_table
[fid
].fi_guid
;
918 if (nvlist_lookup_uint64(features
, feature
, &refcount
) == 0)
923 (void) strlcpy(buf
, ZFS_FEATURE_DISABLED
, len
);
926 (void) strlcpy(buf
, ZFS_FEATURE_ENABLED
, len
);
928 (void) strlcpy(buf
, ZFS_FEATURE_ACTIVE
, len
);
933 (void) strcpy(buf
, ZFS_UNSUPPORTED_INACTIVE
);
935 (void) strcpy(buf
, ZFS_UNSUPPORTED_READONLY
);
938 (void) strlcpy(buf
, "-", len
);
947 * Don't start the slice at the default block of 34; many storage
948 * devices will use a stripe width of 128k, other vendors prefer a 1m
949 * alignment. It is best to play it safe and ensure a 1m alignment
950 * given 512B blocks. When the block size is larger by a power of 2
951 * we will still be 1m aligned. Some devices are sensitive to the
952 * partition ending alignment as well.
954 #define NEW_START_BLOCK 2048
955 #define PARTITION_END_ALIGNMENT 2048
958 * Validate the given pool name, optionally putting an extended error message in
962 zpool_name_valid(libzfs_handle_t
*hdl
, boolean_t isopen
, const char *pool
)
968 ret
= pool_namecheck(pool
, &why
, &what
);
971 * The rules for reserved pool names were extended at a later point.
972 * But we need to support users with existing pools that may now be
973 * invalid. So we only check for this expanded set of names during a
974 * create (or import), and only in userland.
976 if (ret
== 0 && !isopen
&&
977 (strncmp(pool
, "mirror", 6) == 0 ||
978 strncmp(pool
, "raidz", 5) == 0 ||
979 strncmp(pool
, "spare", 5) == 0 ||
980 strcmp(pool
, "log") == 0)) {
983 dgettext(TEXT_DOMAIN
, "name is reserved"));
991 case NAME_ERR_TOOLONG
:
993 dgettext(TEXT_DOMAIN
, "name is too long"));
996 case NAME_ERR_INVALCHAR
:
998 dgettext(TEXT_DOMAIN
, "invalid character "
999 "'%c' in pool name"), what
);
1002 case NAME_ERR_NOLETTER
:
1003 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1004 "name must begin with a letter"));
1007 case NAME_ERR_RESERVED
:
1008 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1009 "name is reserved"));
1012 case NAME_ERR_DISKLIKE
:
1013 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1014 "pool name is reserved"));
1017 case NAME_ERR_LEADING_SLASH
:
1018 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1019 "leading slash in name"));
1022 case NAME_ERR_EMPTY_COMPONENT
:
1023 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1024 "empty component in name"));
1027 case NAME_ERR_TRAILING_SLASH
:
1028 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1029 "trailing slash in name"));
1032 case NAME_ERR_MULTIPLE_AT
:
1033 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1034 "multiple '@' delimiters in name"));
1036 case NAME_ERR_NO_AT
:
1037 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1038 "permission set is missing '@'"));
1049 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1053 zpool_open_canfail(libzfs_handle_t
*hdl
, const char *pool
)
1055 zpool_handle_t
*zhp
;
1059 * Make sure the pool name is valid.
1061 if (!zpool_name_valid(hdl
, B_TRUE
, pool
)) {
1062 (void) zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1063 dgettext(TEXT_DOMAIN
, "cannot open '%s'"),
1068 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
1071 zhp
->zpool_hdl
= hdl
;
1072 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1074 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1080 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "no such pool"));
1081 (void) zfs_error_fmt(hdl
, EZFS_NOENT
,
1082 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), pool
);
1091 * Like the above, but silent on error. Used when iterating over pools (because
1092 * the configuration cache may be out of date).
1095 zpool_open_silent(libzfs_handle_t
*hdl
, const char *pool
, zpool_handle_t
**ret
)
1097 zpool_handle_t
*zhp
;
1100 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
1103 zhp
->zpool_hdl
= hdl
;
1104 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1106 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1122 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1126 zpool_open(libzfs_handle_t
*hdl
, const char *pool
)
1128 zpool_handle_t
*zhp
;
1130 if ((zhp
= zpool_open_canfail(hdl
, pool
)) == NULL
)
1133 if (zhp
->zpool_state
== POOL_STATE_UNAVAIL
) {
1134 (void) zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
1135 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), zhp
->zpool_name
);
1144 * Close the handle. Simply frees the memory associated with the handle.
1147 zpool_close(zpool_handle_t
*zhp
)
1149 if (zhp
->zpool_config
)
1150 nvlist_free(zhp
->zpool_config
);
1151 if (zhp
->zpool_old_config
)
1152 nvlist_free(zhp
->zpool_old_config
);
1153 if (zhp
->zpool_props
)
1154 nvlist_free(zhp
->zpool_props
);
1159 * Return the name of the pool.
1162 zpool_get_name(zpool_handle_t
*zhp
)
1164 return (zhp
->zpool_name
);
1169 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1172 zpool_get_state(zpool_handle_t
*zhp
)
1174 return (zhp
->zpool_state
);
1178 * Create the named pool, using the provided vdev list. It is assumed
1179 * that the consumer has already validated the contents of the nvlist, so we
1180 * don't have to worry about error semantics.
1183 zpool_create(libzfs_handle_t
*hdl
, const char *pool
, nvlist_t
*nvroot
,
1184 nvlist_t
*props
, nvlist_t
*fsprops
)
1186 zfs_cmd_t zc
= {"\0"};
1187 nvlist_t
*zc_fsprops
= NULL
;
1188 nvlist_t
*zc_props
= NULL
;
1192 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1193 "cannot create '%s'"), pool
);
1195 if (!zpool_name_valid(hdl
, B_FALSE
, pool
))
1196 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
1198 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1202 prop_flags_t flags
= { .create
= B_TRUE
, .import
= B_FALSE
};
1204 if ((zc_props
= zpool_valid_proplist(hdl
, pool
, props
,
1205 SPA_VERSION_1
, flags
, msg
)) == NULL
) {
1214 zoned
= ((nvlist_lookup_string(fsprops
,
1215 zfs_prop_to_name(ZFS_PROP_ZONED
), &zonestr
) == 0) &&
1216 strcmp(zonestr
, "on") == 0);
1218 if ((zc_fsprops
= zfs_valid_proplist(hdl
, ZFS_TYPE_FILESYSTEM
,
1219 fsprops
, zoned
, NULL
, NULL
, msg
)) == NULL
) {
1223 (nvlist_alloc(&zc_props
, NV_UNIQUE_NAME
, 0) != 0)) {
1226 if (nvlist_add_nvlist(zc_props
,
1227 ZPOOL_ROOTFS_PROPS
, zc_fsprops
) != 0) {
1232 if (zc_props
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
1235 (void) strlcpy(zc
.zc_name
, pool
, sizeof (zc
.zc_name
));
1237 if ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_CREATE
, &zc
)) != 0) {
1239 zcmd_free_nvlists(&zc
);
1240 nvlist_free(zc_props
);
1241 nvlist_free(zc_fsprops
);
1246 * This can happen if the user has specified the same
1247 * device multiple times. We can't reliably detect this
1248 * until we try to add it and see we already have a
1249 * label. This can also happen under if the device is
1250 * part of an active md or lvm device.
1252 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1253 "one or more vdevs refer to the same device, or "
1254 "one of\nthe devices is part of an active md or "
1256 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1260 * This happens if the record size is smaller or larger
1261 * than the allowed size range, or not a power of 2.
1263 * NOTE: although zfs_valid_proplist is called earlier,
1264 * this case may have slipped through since the
1265 * pool does not exist yet and it is therefore
1266 * impossible to read properties e.g. max blocksize
1269 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1270 "record size invalid"));
1271 return (zfs_error(hdl
, EZFS_BADPROP
, msg
));
1275 * This occurs when one of the devices is below
1276 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1277 * device was the problem device since there's no
1278 * reliable way to determine device size from userland.
1283 zfs_nicenum(SPA_MINDEVSIZE
, buf
, sizeof (buf
));
1285 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1286 "one or more devices is less than the "
1287 "minimum size (%s)"), buf
);
1289 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1292 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1293 "one or more devices is out of space"));
1294 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1297 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1298 "cache device must be a disk or disk slice"));
1299 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1302 return (zpool_standard_error(hdl
, errno
, msg
));
1307 zcmd_free_nvlists(&zc
);
1308 nvlist_free(zc_props
);
1309 nvlist_free(zc_fsprops
);
1314 * Destroy the given pool. It is up to the caller to ensure that there are no
1315 * datasets left in the pool.
1318 zpool_destroy(zpool_handle_t
*zhp
, const char *log_str
)
1320 zfs_cmd_t zc
= {"\0"};
1321 zfs_handle_t
*zfp
= NULL
;
1322 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1325 if (zhp
->zpool_state
== POOL_STATE_ACTIVE
&&
1326 (zfp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
1329 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1330 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1332 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_DESTROY
, &zc
) != 0) {
1333 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1334 "cannot destroy '%s'"), zhp
->zpool_name
);
1336 if (errno
== EROFS
) {
1337 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1338 "one or more devices is read only"));
1339 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1341 (void) zpool_standard_error(hdl
, errno
, msg
);
1350 remove_mountpoint(zfp
);
1358 * Add the given vdevs to the pool. The caller must have already performed the
1359 * necessary verification to ensure that the vdev specification is well-formed.
1362 zpool_add(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
1364 zfs_cmd_t zc
= {"\0"};
1366 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1368 nvlist_t
**spares
, **l2cache
;
1369 uint_t nspares
, nl2cache
;
1371 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1372 "cannot add to '%s'"), zhp
->zpool_name
);
1374 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1375 SPA_VERSION_SPARES
&&
1376 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1377 &spares
, &nspares
) == 0) {
1378 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1379 "upgraded to add hot spares"));
1380 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1383 #if defined(__sun__) || defined(__sun)
1384 if (zpool_is_bootable(zhp
) && nvlist_lookup_nvlist_array(nvroot
,
1385 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0) {
1388 for (s
= 0; s
< nspares
; s
++) {
1391 if (nvlist_lookup_string(spares
[s
], ZPOOL_CONFIG_PATH
,
1392 &path
) == 0 && pool_uses_efi(spares
[s
])) {
1393 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1394 "device '%s' contains an EFI label and "
1395 "cannot be used on root pools."),
1396 zpool_vdev_name(hdl
, NULL
, spares
[s
], 0));
1397 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, msg
));
1403 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1404 SPA_VERSION_L2CACHE
&&
1405 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1406 &l2cache
, &nl2cache
) == 0) {
1407 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1408 "upgraded to add cache devices"));
1409 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1412 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1414 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1416 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_ADD
, &zc
) != 0) {
1420 * This can happen if the user has specified the same
1421 * device multiple times. We can't reliably detect this
1422 * until we try to add it and see we already have a
1425 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1426 "one or more vdevs refer to the same device"));
1427 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1432 * This occurrs when one of the devices is below
1433 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1434 * device was the problem device since there's no
1435 * reliable way to determine device size from userland.
1440 zfs_nicenum(SPA_MINDEVSIZE
, buf
, sizeof (buf
));
1442 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1443 "device is less than the minimum "
1446 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1450 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1451 "pool must be upgraded to add these vdevs"));
1452 (void) zfs_error(hdl
, EZFS_BADVERSION
, msg
);
1456 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1457 "cache device must be a disk or disk slice"));
1458 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1462 (void) zpool_standard_error(hdl
, errno
, msg
);
1470 zcmd_free_nvlists(&zc
);
1476 * Exports the pool from the system. The caller must ensure that there are no
1477 * mounted datasets in the pool.
1480 zpool_export_common(zpool_handle_t
*zhp
, boolean_t force
, boolean_t hardforce
,
1481 const char *log_str
)
1483 zfs_cmd_t zc
= {"\0"};
1486 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1487 "cannot export '%s'"), zhp
->zpool_name
);
1489 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1490 zc
.zc_cookie
= force
;
1491 zc
.zc_guid
= hardforce
;
1492 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1494 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_EXPORT
, &zc
) != 0) {
1497 zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
1498 "use '-f' to override the following errors:\n"
1499 "'%s' has an active shared spare which could be"
1500 " used by other pools once '%s' is exported."),
1501 zhp
->zpool_name
, zhp
->zpool_name
);
1502 return (zfs_error(zhp
->zpool_hdl
, EZFS_ACTIVE_SPARE
,
1505 return (zpool_standard_error_fmt(zhp
->zpool_hdl
, errno
,
1514 zpool_export(zpool_handle_t
*zhp
, boolean_t force
, const char *log_str
)
1516 return (zpool_export_common(zhp
, force
, B_FALSE
, log_str
));
1520 zpool_export_force(zpool_handle_t
*zhp
, const char *log_str
)
1522 return (zpool_export_common(zhp
, B_TRUE
, B_TRUE
, log_str
));
1526 zpool_rewind_exclaim(libzfs_handle_t
*hdl
, const char *name
, boolean_t dryrun
,
1529 nvlist_t
*nv
= NULL
;
1535 if (!hdl
->libzfs_printerr
|| config
== NULL
)
1538 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1539 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0) {
1543 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1545 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1547 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1548 strftime(timestr
, 128, "%c", &t
) != 0) {
1550 (void) printf(dgettext(TEXT_DOMAIN
,
1551 "Would be able to return %s "
1552 "to its state as of %s.\n"),
1555 (void) printf(dgettext(TEXT_DOMAIN
,
1556 "Pool %s returned to its state as of %s.\n"),
1560 (void) printf(dgettext(TEXT_DOMAIN
,
1561 "%s approximately %lld "),
1562 dryrun
? "Would discard" : "Discarded",
1563 ((longlong_t
)loss
+ 30) / 60);
1564 (void) printf(dgettext(TEXT_DOMAIN
,
1565 "minutes of transactions.\n"));
1566 } else if (loss
> 0) {
1567 (void) printf(dgettext(TEXT_DOMAIN
,
1568 "%s approximately %lld "),
1569 dryrun
? "Would discard" : "Discarded",
1571 (void) printf(dgettext(TEXT_DOMAIN
,
1572 "seconds of transactions.\n"));
1578 zpool_explain_recover(libzfs_handle_t
*hdl
, const char *name
, int reason
,
1581 nvlist_t
*nv
= NULL
;
1583 uint64_t edata
= UINT64_MAX
;
1588 if (!hdl
->libzfs_printerr
)
1592 (void) printf(dgettext(TEXT_DOMAIN
, "action: "));
1594 (void) printf(dgettext(TEXT_DOMAIN
, "\t"));
1596 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1597 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1598 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0 ||
1599 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1602 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1603 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_DATA_ERRORS
,
1606 (void) printf(dgettext(TEXT_DOMAIN
,
1607 "Recovery is possible, but will result in some data loss.\n"));
1609 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1610 strftime(timestr
, 128, "%c", &t
) != 0) {
1611 (void) printf(dgettext(TEXT_DOMAIN
,
1612 "\tReturning the pool to its state as of %s\n"
1613 "\tshould correct the problem. "),
1616 (void) printf(dgettext(TEXT_DOMAIN
,
1617 "\tReverting the pool to an earlier state "
1618 "should correct the problem.\n\t"));
1622 (void) printf(dgettext(TEXT_DOMAIN
,
1623 "Approximately %lld minutes of data\n"
1624 "\tmust be discarded, irreversibly. "),
1625 ((longlong_t
)loss
+ 30) / 60);
1626 } else if (loss
> 0) {
1627 (void) printf(dgettext(TEXT_DOMAIN
,
1628 "Approximately %lld seconds of data\n"
1629 "\tmust be discarded, irreversibly. "),
1632 if (edata
!= 0 && edata
!= UINT64_MAX
) {
1634 (void) printf(dgettext(TEXT_DOMAIN
,
1635 "After rewind, at least\n"
1636 "\tone persistent user-data error will remain. "));
1638 (void) printf(dgettext(TEXT_DOMAIN
,
1639 "After rewind, several\n"
1640 "\tpersistent user-data errors will remain. "));
1643 (void) printf(dgettext(TEXT_DOMAIN
,
1644 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1645 reason
>= 0 ? "clear" : "import", name
);
1647 (void) printf(dgettext(TEXT_DOMAIN
,
1648 "A scrub of the pool\n"
1649 "\tis strongly recommended after recovery.\n"));
1653 (void) printf(dgettext(TEXT_DOMAIN
,
1654 "Destroy and re-create the pool from\n\ta backup source.\n"));
1658 * zpool_import() is a contracted interface. Should be kept the same
1661 * Applications should use zpool_import_props() to import a pool with
1662 * new properties value to be set.
1665 zpool_import(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1668 nvlist_t
*props
= NULL
;
1671 if (altroot
!= NULL
) {
1672 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
1673 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1674 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1678 if (nvlist_add_string(props
,
1679 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), altroot
) != 0 ||
1680 nvlist_add_string(props
,
1681 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
), "none") != 0) {
1683 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1684 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1689 ret
= zpool_import_props(hdl
, config
, newname
, props
,
1697 print_vdev_tree(libzfs_handle_t
*hdl
, const char *name
, nvlist_t
*nv
,
1703 uint64_t is_log
= 0;
1705 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
,
1709 (void) printf("\t%*s%s%s\n", indent
, "", name
,
1710 is_log
? " [log]" : "");
1712 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1713 &child
, &children
) != 0)
1716 for (c
= 0; c
< children
; c
++) {
1717 vname
= zpool_vdev_name(hdl
, NULL
, child
[c
], VDEV_NAME_TYPE_ID
);
1718 print_vdev_tree(hdl
, vname
, child
[c
], indent
+ 2);
1724 zpool_print_unsup_feat(nvlist_t
*config
)
1726 nvlist_t
*nvinfo
, *unsup_feat
;
1729 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) ==
1731 verify(nvlist_lookup_nvlist(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
,
1734 for (nvp
= nvlist_next_nvpair(unsup_feat
, NULL
); nvp
!= NULL
;
1735 nvp
= nvlist_next_nvpair(unsup_feat
, nvp
)) {
1738 verify(nvpair_type(nvp
) == DATA_TYPE_STRING
);
1739 verify(nvpair_value_string(nvp
, &desc
) == 0);
1741 if (strlen(desc
) > 0)
1742 (void) printf("\t%s (%s)\n", nvpair_name(nvp
), desc
);
1744 (void) printf("\t%s\n", nvpair_name(nvp
));
1749 * Import the given pool using the known configuration and a list of
1750 * properties to be set. The configuration should have come from
1751 * zpool_find_import(). The 'newname' parameters control whether the pool
1752 * is imported with a different name.
1755 zpool_import_props(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1756 nvlist_t
*props
, int flags
)
1758 zfs_cmd_t zc
= {"\0"};
1759 zpool_rewind_policy_t policy
;
1760 nvlist_t
*nv
= NULL
;
1761 nvlist_t
*nvinfo
= NULL
;
1762 nvlist_t
*missing
= NULL
;
1769 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1772 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1773 "cannot import pool '%s'"), origname
);
1775 if (newname
!= NULL
) {
1776 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
1777 return (zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1778 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1780 thename
= (char *)newname
;
1785 if (props
!= NULL
) {
1787 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
1789 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
1792 if ((props
= zpool_valid_proplist(hdl
, origname
,
1793 props
, version
, flags
, errbuf
)) == NULL
)
1795 if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
1802 (void) strlcpy(zc
.zc_name
, thename
, sizeof (zc
.zc_name
));
1804 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1807 if (zcmd_write_conf_nvlist(hdl
, &zc
, config
) != 0) {
1808 zcmd_free_nvlists(&zc
);
1811 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zc
.zc_nvlist_conf_size
* 2) != 0) {
1812 zcmd_free_nvlists(&zc
);
1816 zc
.zc_cookie
= flags
;
1817 while ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_IMPORT
, &zc
)) != 0 &&
1819 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
1820 zcmd_free_nvlists(&zc
);
1827 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nv
);
1829 zcmd_free_nvlists(&zc
);
1831 zpool_get_rewind_policy(config
, &policy
);
1837 * Dry-run failed, but we print out what success
1838 * looks like if we found a best txg
1840 if (policy
.zrp_request
& ZPOOL_TRY_REWIND
) {
1841 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1847 if (newname
== NULL
)
1848 (void) snprintf(desc
, sizeof (desc
),
1849 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1852 (void) snprintf(desc
, sizeof (desc
),
1853 dgettext(TEXT_DOMAIN
, "cannot import '%s' as '%s'"),
1858 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
1859 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1860 nvlist_exists(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
)) {
1861 (void) printf(dgettext(TEXT_DOMAIN
, "This "
1862 "pool uses the following feature(s) not "
1863 "supported by this system:\n"));
1864 zpool_print_unsup_feat(nv
);
1865 if (nvlist_exists(nvinfo
,
1866 ZPOOL_CONFIG_CAN_RDONLY
)) {
1867 (void) printf(dgettext(TEXT_DOMAIN
,
1868 "All unsupported features are only "
1869 "required for writing to the pool."
1870 "\nThe pool can be imported using "
1871 "'-o readonly=on'.\n"));
1875 * Unsupported version.
1877 (void) zfs_error(hdl
, EZFS_BADVERSION
, desc
);
1881 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, desc
);
1885 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1886 "one or more devices is read only"));
1887 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
1891 if (nv
&& nvlist_lookup_nvlist(nv
,
1892 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1893 nvlist_lookup_nvlist(nvinfo
,
1894 ZPOOL_CONFIG_MISSING_DEVICES
, &missing
) == 0) {
1895 (void) printf(dgettext(TEXT_DOMAIN
,
1896 "The devices below are missing, use "
1897 "'-m' to import the pool anyway:\n"));
1898 print_vdev_tree(hdl
, NULL
, missing
, 2);
1899 (void) printf("\n");
1901 (void) zpool_standard_error(hdl
, error
, desc
);
1905 (void) zpool_standard_error(hdl
, error
, desc
);
1909 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1910 "one or more devices are already in use\n"));
1911 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
1915 (void) zpool_standard_error(hdl
, error
, desc
);
1916 zpool_explain_recover(hdl
,
1917 newname
? origname
: thename
, -error
, nv
);
1924 zpool_handle_t
*zhp
;
1927 * This should never fail, but play it safe anyway.
1929 if (zpool_open_silent(hdl
, thename
, &zhp
) != 0)
1931 else if (zhp
!= NULL
)
1933 if (policy
.zrp_request
&
1934 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
1935 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1936 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0), nv
);
1949 zpool_scan(zpool_handle_t
*zhp
, pool_scan_func_t func
)
1951 zfs_cmd_t zc
= {"\0"};
1953 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1955 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1956 zc
.zc_cookie
= func
;
1958 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_SCAN
, &zc
) == 0 ||
1959 (errno
== ENOENT
&& func
!= POOL_SCAN_NONE
))
1962 if (func
== POOL_SCAN_SCRUB
) {
1963 (void) snprintf(msg
, sizeof (msg
),
1964 dgettext(TEXT_DOMAIN
, "cannot scrub %s"), zc
.zc_name
);
1965 } else if (func
== POOL_SCAN_NONE
) {
1966 (void) snprintf(msg
, sizeof (msg
),
1967 dgettext(TEXT_DOMAIN
, "cannot cancel scrubbing %s"),
1970 assert(!"unexpected result");
1973 if (errno
== EBUSY
) {
1975 pool_scan_stat_t
*ps
= NULL
;
1978 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
1979 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
1980 (void) nvlist_lookup_uint64_array(nvroot
,
1981 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &psc
);
1982 if (ps
&& ps
->pss_func
== POOL_SCAN_SCRUB
)
1983 return (zfs_error(hdl
, EZFS_SCRUBBING
, msg
));
1985 return (zfs_error(hdl
, EZFS_RESILVERING
, msg
));
1986 } else if (errno
== ENOENT
) {
1987 return (zfs_error(hdl
, EZFS_NO_SCRUB
, msg
));
1989 return (zpool_standard_error(hdl
, errno
, msg
));
1994 * Find a vdev that matches the search criteria specified. We use the
1995 * the nvpair name to determine how we should look for the device.
1996 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1997 * spare; but FALSE if its an INUSE spare.
2000 vdev_to_nvlist_iter(nvlist_t
*nv
, nvlist_t
*search
, boolean_t
*avail_spare
,
2001 boolean_t
*l2cache
, boolean_t
*log
)
2008 nvpair_t
*pair
= nvlist_next_nvpair(search
, NULL
);
2010 /* Nothing to look for */
2011 if (search
== NULL
|| pair
== NULL
)
2014 /* Obtain the key we will use to search */
2015 srchkey
= nvpair_name(pair
);
2017 switch (nvpair_type(pair
)) {
2018 case DATA_TYPE_UINT64
:
2019 if (strcmp(srchkey
, ZPOOL_CONFIG_GUID
) == 0) {
2020 uint64_t srchval
, theguid
;
2022 verify(nvpair_value_uint64(pair
, &srchval
) == 0);
2023 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
2025 if (theguid
== srchval
)
2030 case DATA_TYPE_STRING
: {
2031 char *srchval
, *val
;
2033 verify(nvpair_value_string(pair
, &srchval
) == 0);
2034 if (nvlist_lookup_string(nv
, srchkey
, &val
) != 0)
2038 * Search for the requested value. Special cases:
2040 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2041 * "-part1", or "p1". The suffix is hidden from the user,
2042 * but included in the string, so this matches around it.
2043 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2044 * is used to check all possible expanded paths.
2045 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2047 * Otherwise, all other searches are simple string compares.
2049 if (strcmp(srchkey
, ZPOOL_CONFIG_PATH
) == 0) {
2050 uint64_t wholedisk
= 0;
2052 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
2054 if (zfs_strcmp_pathname(srchval
, val
, wholedisk
) == 0)
2057 } else if (strcmp(srchkey
, ZPOOL_CONFIG_TYPE
) == 0 && val
) {
2058 char *type
, *idx
, *end
, *p
;
2059 uint64_t id
, vdev_id
;
2062 * Determine our vdev type, keeping in mind
2063 * that the srchval is composed of a type and
2064 * vdev id pair (i.e. mirror-4).
2066 if ((type
= strdup(srchval
)) == NULL
)
2069 if ((p
= strrchr(type
, '-')) == NULL
) {
2077 * If the types don't match then keep looking.
2079 if (strncmp(val
, type
, strlen(val
)) != 0) {
2084 verify(strncmp(type
, VDEV_TYPE_RAIDZ
,
2085 strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2086 strncmp(type
, VDEV_TYPE_MIRROR
,
2087 strlen(VDEV_TYPE_MIRROR
)) == 0);
2088 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
2092 vdev_id
= strtoull(idx
, &end
, 10);
2099 * Now verify that we have the correct vdev id.
2108 if (strcmp(srchval
, val
) == 0)
2117 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2118 &child
, &children
) != 0)
2121 for (c
= 0; c
< children
; c
++) {
2122 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2123 avail_spare
, l2cache
, NULL
)) != NULL
) {
2125 * The 'is_log' value is only set for the toplevel
2126 * vdev, not the leaf vdevs. So we always lookup the
2127 * log device from the root of the vdev tree (where
2128 * 'log' is non-NULL).
2131 nvlist_lookup_uint64(child
[c
],
2132 ZPOOL_CONFIG_IS_LOG
, &is_log
) == 0 &&
2140 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2141 &child
, &children
) == 0) {
2142 for (c
= 0; c
< children
; c
++) {
2143 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2144 avail_spare
, l2cache
, NULL
)) != NULL
) {
2145 *avail_spare
= B_TRUE
;
2151 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2152 &child
, &children
) == 0) {
2153 for (c
= 0; c
< children
; c
++) {
2154 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2155 avail_spare
, l2cache
, NULL
)) != NULL
) {
2166 * Given a physical path (minus the "/devices" prefix), find the
2170 zpool_find_vdev_by_physpath(zpool_handle_t
*zhp
, const char *ppath
,
2171 boolean_t
*avail_spare
, boolean_t
*l2cache
, boolean_t
*log
)
2173 nvlist_t
*search
, *nvroot
, *ret
;
2175 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2176 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PHYS_PATH
, ppath
) == 0);
2178 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2181 *avail_spare
= B_FALSE
;
2185 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2186 nvlist_free(search
);
2192 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2195 zpool_vdev_is_interior(const char *name
)
2197 if (strncmp(name
, VDEV_TYPE_RAIDZ
, strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2198 strncmp(name
, VDEV_TYPE_MIRROR
, strlen(VDEV_TYPE_MIRROR
)) == 0)
2204 zpool_find_vdev(zpool_handle_t
*zhp
, const char *path
, boolean_t
*avail_spare
,
2205 boolean_t
*l2cache
, boolean_t
*log
)
2208 nvlist_t
*nvroot
, *search
, *ret
;
2211 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2213 guid
= strtoull(path
, &end
, 0);
2214 if (guid
!= 0 && *end
== '\0') {
2215 verify(nvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
) == 0);
2216 } else if (zpool_vdev_is_interior(path
)) {
2217 verify(nvlist_add_string(search
, ZPOOL_CONFIG_TYPE
, path
) == 0);
2219 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PATH
, path
) == 0);
2222 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2225 *avail_spare
= B_FALSE
;
2229 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2230 nvlist_free(search
);
2236 vdev_online(nvlist_t
*nv
)
2240 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
, &ival
) == 0 ||
2241 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
, &ival
) == 0 ||
2242 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
, &ival
) == 0)
2249 * Helper function for zpool_get_physpaths().
2252 vdev_get_one_physpath(nvlist_t
*config
, char *physpath
, size_t physpath_size
,
2253 size_t *bytes_written
)
2255 size_t bytes_left
, pos
, rsz
;
2259 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PHYS_PATH
,
2261 return (EZFS_NODEVICE
);
2263 pos
= *bytes_written
;
2264 bytes_left
= physpath_size
- pos
;
2265 format
= (pos
== 0) ? "%s" : " %s";
2267 rsz
= snprintf(physpath
+ pos
, bytes_left
, format
, tmppath
);
2268 *bytes_written
+= rsz
;
2270 if (rsz
>= bytes_left
) {
2271 /* if physpath was not copied properly, clear it */
2272 if (bytes_left
!= 0) {
2275 return (EZFS_NOSPC
);
2281 vdev_get_physpaths(nvlist_t
*nv
, char *physpath
, size_t phypath_size
,
2282 size_t *rsz
, boolean_t is_spare
)
2287 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
2288 return (EZFS_INVALCONFIG
);
2290 if (strcmp(type
, VDEV_TYPE_DISK
) == 0) {
2292 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2293 * For a spare vdev, we only want to boot from the active
2298 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
2301 return (EZFS_INVALCONFIG
);
2304 if (vdev_online(nv
)) {
2305 if ((ret
= vdev_get_one_physpath(nv
, physpath
,
2306 phypath_size
, rsz
)) != 0)
2309 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) == 0 ||
2310 strcmp(type
, VDEV_TYPE_REPLACING
) == 0 ||
2311 (is_spare
= (strcmp(type
, VDEV_TYPE_SPARE
) == 0))) {
2316 if (nvlist_lookup_nvlist_array(nv
,
2317 ZPOOL_CONFIG_CHILDREN
, &child
, &count
) != 0)
2318 return (EZFS_INVALCONFIG
);
2320 for (i
= 0; i
< count
; i
++) {
2321 ret
= vdev_get_physpaths(child
[i
], physpath
,
2322 phypath_size
, rsz
, is_spare
);
2323 if (ret
== EZFS_NOSPC
)
2328 return (EZFS_POOL_INVALARG
);
2332 * Get phys_path for a root pool config.
2333 * Return 0 on success; non-zero on failure.
2336 zpool_get_config_physpath(nvlist_t
*config
, char *physpath
, size_t phypath_size
)
2339 nvlist_t
*vdev_root
;
2346 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2348 return (EZFS_INVALCONFIG
);
2350 if (nvlist_lookup_string(vdev_root
, ZPOOL_CONFIG_TYPE
, &type
) != 0 ||
2351 nvlist_lookup_nvlist_array(vdev_root
, ZPOOL_CONFIG_CHILDREN
,
2352 &child
, &count
) != 0)
2353 return (EZFS_INVALCONFIG
);
2355 #if defined(__sun__) || defined(__sun)
2357 * root pool can not have EFI labeled disks and can only have
2358 * a single top-level vdev.
2360 if (strcmp(type
, VDEV_TYPE_ROOT
) != 0 || count
!= 1 ||
2361 pool_uses_efi(vdev_root
))
2362 return (EZFS_POOL_INVALARG
);
2365 (void) vdev_get_physpaths(child
[0], physpath
, phypath_size
, &rsz
,
2368 /* No online devices */
2370 return (EZFS_NODEVICE
);
2376 * Get phys_path for a root pool
2377 * Return 0 on success; non-zero on failure.
2380 zpool_get_physpath(zpool_handle_t
*zhp
, char *physpath
, size_t phypath_size
)
2382 return (zpool_get_config_physpath(zhp
->zpool_config
, physpath
,
2387 * If the device has being dynamically expanded then we need to relabel
2388 * the disk to use the new unallocated space.
2391 zpool_relabel_disk(libzfs_handle_t
*hdl
, const char *path
, const char *msg
)
2395 if ((fd
= open(path
, O_RDWR
|O_DIRECT
)) < 0) {
2396 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2397 "relabel '%s': unable to open device: %d"), path
, errno
);
2398 return (zfs_error(hdl
, EZFS_OPENFAILED
, msg
));
2402 * It's possible that we might encounter an error if the device
2403 * does not have any unallocated space left. If so, we simply
2404 * ignore that error and continue on.
2406 * Also, we don't call efi_rescan() - that would just return EBUSY.
2407 * The module will do it for us in vdev_disk_open().
2409 error
= efi_use_whole_disk(fd
);
2411 if (error
&& error
!= VT_ENOSPC
) {
2412 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2413 "relabel '%s': unable to read disk capacity"), path
);
2414 return (zfs_error(hdl
, EZFS_NOCAP
, msg
));
2420 * Bring the specified vdev online. The 'flags' parameter is a set of the
2421 * ZFS_ONLINE_* flags.
2424 zpool_vdev_online(zpool_handle_t
*zhp
, const char *path
, int flags
,
2425 vdev_state_t
*newstate
)
2427 zfs_cmd_t zc
= {"\0"};
2430 boolean_t avail_spare
, l2cache
, islog
;
2431 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2434 if (flags
& ZFS_ONLINE_EXPAND
) {
2435 (void) snprintf(msg
, sizeof (msg
),
2436 dgettext(TEXT_DOMAIN
, "cannot expand %s"), path
);
2438 (void) snprintf(msg
, sizeof (msg
),
2439 dgettext(TEXT_DOMAIN
, "cannot online %s"), path
);
2442 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2443 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2445 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2447 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2450 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2452 if (flags
& ZFS_ONLINE_EXPAND
||
2453 zpool_get_prop_int(zhp
, ZPOOL_PROP_AUTOEXPAND
, NULL
)) {
2454 uint64_t wholedisk
= 0;
2456 (void) nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_WHOLE_DISK
,
2460 * XXX - L2ARC 1.0 devices can't support expansion.
2463 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2464 "cannot expand cache devices"));
2465 return (zfs_error(hdl
, EZFS_VDEVNOTSUP
, msg
));
2469 const char *fullpath
= path
;
2470 char buf
[MAXPATHLEN
];
2472 if (path
[0] != '/') {
2473 error
= zfs_resolve_shortname(path
, buf
,
2476 return (zfs_error(hdl
, EZFS_NODEVICE
,
2482 error
= zpool_relabel_disk(hdl
, fullpath
, msg
);
2488 zc
.zc_cookie
= VDEV_STATE_ONLINE
;
2491 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) != 0) {
2492 if (errno
== EINVAL
) {
2493 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "was split "
2494 "from this pool into a new one. Use '%s' "
2495 "instead"), "zpool detach");
2496 return (zfs_error(hdl
, EZFS_POSTSPLIT_ONLINE
, msg
));
2498 return (zpool_standard_error(hdl
, errno
, msg
));
2501 *newstate
= zc
.zc_cookie
;
2506 * Take the specified vdev offline
2509 zpool_vdev_offline(zpool_handle_t
*zhp
, const char *path
, boolean_t istmp
)
2511 zfs_cmd_t zc
= {"\0"};
2514 boolean_t avail_spare
, l2cache
;
2515 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2517 (void) snprintf(msg
, sizeof (msg
),
2518 dgettext(TEXT_DOMAIN
, "cannot offline %s"), path
);
2520 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2521 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2523 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2525 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2528 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2530 zc
.zc_cookie
= VDEV_STATE_OFFLINE
;
2531 zc
.zc_obj
= istmp
? ZFS_OFFLINE_TEMPORARY
: 0;
2533 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2540 * There are no other replicas of this device.
2542 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2546 * The log device has unplayed logs
2548 return (zfs_error(hdl
, EZFS_UNPLAYED_LOGS
, msg
));
2551 return (zpool_standard_error(hdl
, errno
, msg
));
2556 * Mark the given vdev faulted.
2559 zpool_vdev_fault(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2561 zfs_cmd_t zc
= {"\0"};
2563 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2565 (void) snprintf(msg
, sizeof (msg
),
2566 dgettext(TEXT_DOMAIN
, "cannot fault %llu"), (u_longlong_t
)guid
);
2568 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2570 zc
.zc_cookie
= VDEV_STATE_FAULTED
;
2573 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2580 * There are no other replicas of this device.
2582 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2585 return (zpool_standard_error(hdl
, errno
, msg
));
2591 * Mark the given vdev degraded.
2594 zpool_vdev_degrade(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2596 zfs_cmd_t zc
= {"\0"};
2598 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2600 (void) snprintf(msg
, sizeof (msg
),
2601 dgettext(TEXT_DOMAIN
, "cannot degrade %llu"), (u_longlong_t
)guid
);
2603 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2605 zc
.zc_cookie
= VDEV_STATE_DEGRADED
;
2608 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2611 return (zpool_standard_error(hdl
, errno
, msg
));
2615 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2619 is_replacing_spare(nvlist_t
*search
, nvlist_t
*tgt
, int which
)
2625 if (nvlist_lookup_nvlist_array(search
, ZPOOL_CONFIG_CHILDREN
, &child
,
2627 verify(nvlist_lookup_string(search
, ZPOOL_CONFIG_TYPE
,
2630 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0 &&
2631 children
== 2 && child
[which
] == tgt
)
2634 for (c
= 0; c
< children
; c
++)
2635 if (is_replacing_spare(child
[c
], tgt
, which
))
2643 * Attach new_disk (fully described by nvroot) to old_disk.
2644 * If 'replacing' is specified, the new disk will replace the old one.
2647 zpool_vdev_attach(zpool_handle_t
*zhp
,
2648 const char *old_disk
, const char *new_disk
, nvlist_t
*nvroot
, int replacing
)
2650 zfs_cmd_t zc
= {"\0"};
2654 boolean_t avail_spare
, l2cache
, islog
;
2659 nvlist_t
*config_root
;
2660 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2661 boolean_t rootpool
= zpool_is_bootable(zhp
);
2664 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2665 "cannot replace %s with %s"), old_disk
, new_disk
);
2667 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2668 "cannot attach %s to %s"), new_disk
, old_disk
);
2670 #if defined(__sun__) || defined(__sun)
2672 * If this is a root pool, make sure that we're not attaching an
2673 * EFI labeled device.
2675 if (rootpool
&& pool_uses_efi(nvroot
)) {
2676 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2677 "EFI labeled devices are not supported on root pools."));
2678 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, msg
));
2682 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2683 if ((tgt
= zpool_find_vdev(zhp
, old_disk
, &avail_spare
, &l2cache
,
2685 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2688 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2691 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2693 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2694 zc
.zc_cookie
= replacing
;
2696 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
2697 &child
, &children
) != 0 || children
!= 1) {
2698 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2699 "new device must be a single disk"));
2700 return (zfs_error(hdl
, EZFS_INVALCONFIG
, msg
));
2703 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
2704 ZPOOL_CONFIG_VDEV_TREE
, &config_root
) == 0);
2706 if ((newname
= zpool_vdev_name(NULL
, NULL
, child
[0], 0)) == NULL
)
2710 * If the target is a hot spare that has been swapped in, we can only
2711 * replace it with another hot spare.
2714 nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_IS_SPARE
, &val
) == 0 &&
2715 (zpool_find_vdev(zhp
, newname
, &avail_spare
, &l2cache
,
2716 NULL
) == NULL
|| !avail_spare
) &&
2717 is_replacing_spare(config_root
, tgt
, 1)) {
2718 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2719 "can only be replaced by another hot spare"));
2721 return (zfs_error(hdl
, EZFS_BADTARGET
, msg
));
2726 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
2729 ret
= zfs_ioctl(hdl
, ZFS_IOC_VDEV_ATTACH
, &zc
);
2731 zcmd_free_nvlists(&zc
);
2736 * XXX need a better way to prevent user from
2737 * booting up a half-baked vdev.
2739 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Make "
2740 "sure to wait until resilver is done "
2741 "before rebooting.\n"));
2749 * Can't attach to or replace this type of vdev.
2752 uint64_t version
= zpool_get_prop_int(zhp
,
2753 ZPOOL_PROP_VERSION
, NULL
);
2756 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2757 "cannot replace a log with a spare"));
2758 else if (version
>= SPA_VERSION_MULTI_REPLACE
)
2759 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2760 "already in replacing/spare config; wait "
2761 "for completion or use 'zpool detach'"));
2763 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2764 "cannot replace a replacing device"));
2766 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2767 "can only attach to mirrors and top-level "
2770 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2775 * The new device must be a single disk.
2777 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2778 "new device must be a single disk"));
2779 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2783 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "%s is busy"),
2785 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2790 * The new device is too small.
2792 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2793 "device is too small"));
2794 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2799 * The new device has a different optimal sector size.
2801 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2802 "new device has a different optimal sector size; use the "
2803 "option '-o ashift=N' to override the optimal size"));
2804 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2809 * The resulting top-level vdev spec won't fit in the label.
2811 (void) zfs_error(hdl
, EZFS_DEVOVERFLOW
, msg
);
2815 (void) zpool_standard_error(hdl
, errno
, msg
);
2822 * Detach the specified device.
2825 zpool_vdev_detach(zpool_handle_t
*zhp
, const char *path
)
2827 zfs_cmd_t zc
= {"\0"};
2830 boolean_t avail_spare
, l2cache
;
2831 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2833 (void) snprintf(msg
, sizeof (msg
),
2834 dgettext(TEXT_DOMAIN
, "cannot detach %s"), path
);
2836 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2837 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2839 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2842 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2845 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2847 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2849 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_DETACH
, &zc
) == 0)
2856 * Can't detach from this type of vdev.
2858 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "only "
2859 "applicable to mirror and replacing vdevs"));
2860 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2865 * There are no other replicas of this device.
2867 (void) zfs_error(hdl
, EZFS_NOREPLICAS
, msg
);
2871 (void) zpool_standard_error(hdl
, errno
, msg
);
2878 * Find a mirror vdev in the source nvlist.
2880 * The mchild array contains a list of disks in one of the top-level mirrors
2881 * of the source pool. The schild array contains a list of disks that the
2882 * user specified on the command line. We loop over the mchild array to
2883 * see if any entry in the schild array matches.
2885 * If a disk in the mchild array is found in the schild array, we return
2886 * the index of that entry. Otherwise we return -1.
2889 find_vdev_entry(zpool_handle_t
*zhp
, nvlist_t
**mchild
, uint_t mchildren
,
2890 nvlist_t
**schild
, uint_t schildren
)
2894 for (mc
= 0; mc
< mchildren
; mc
++) {
2896 char *mpath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
2899 for (sc
= 0; sc
< schildren
; sc
++) {
2900 char *spath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
2902 boolean_t result
= (strcmp(mpath
, spath
) == 0);
2918 * Split a mirror pool. If newroot points to null, then a new nvlist
2919 * is generated and it is the responsibility of the caller to free it.
2922 zpool_vdev_split(zpool_handle_t
*zhp
, char *newname
, nvlist_t
**newroot
,
2923 nvlist_t
*props
, splitflags_t flags
)
2925 zfs_cmd_t zc
= {"\0"};
2927 nvlist_t
*tree
, *config
, **child
, **newchild
, *newconfig
= NULL
;
2928 nvlist_t
**varray
= NULL
, *zc_props
= NULL
;
2929 uint_t c
, children
, newchildren
, lastlog
= 0, vcount
, found
= 0;
2930 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2932 boolean_t freelist
= B_FALSE
, memory_err
= B_TRUE
;
2935 (void) snprintf(msg
, sizeof (msg
),
2936 dgettext(TEXT_DOMAIN
, "Unable to split %s"), zhp
->zpool_name
);
2938 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
2939 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
2941 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
2942 (void) fprintf(stderr
, gettext("Internal error: unable to "
2943 "retrieve pool configuration\n"));
2947 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &tree
)
2949 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
, &vers
) == 0);
2952 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
2953 if ((zc_props
= zpool_valid_proplist(hdl
, zhp
->zpool_name
,
2954 props
, vers
, flags
, msg
)) == NULL
)
2958 if (nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
2960 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2961 "Source pool is missing vdev tree"));
2963 nvlist_free(zc_props
);
2967 varray
= zfs_alloc(hdl
, children
* sizeof (nvlist_t
*));
2970 if (*newroot
== NULL
||
2971 nvlist_lookup_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
2972 &newchild
, &newchildren
) != 0)
2975 for (c
= 0; c
< children
; c
++) {
2976 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
2978 nvlist_t
**mchild
, *vdev
;
2983 * Unlike cache & spares, slogs are stored in the
2984 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2986 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2988 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
2990 if (is_log
|| is_hole
) {
2992 * Create a hole vdev and put it in the config.
2994 if (nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) != 0)
2996 if (nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
,
2997 VDEV_TYPE_HOLE
) != 0)
2999 if (nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_HOLE
,
3004 varray
[vcount
++] = vdev
;
3008 verify(nvlist_lookup_string(child
[c
], ZPOOL_CONFIG_TYPE
, &type
)
3010 if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
3011 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3012 "Source pool must be composed only of mirrors\n"));
3013 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3017 verify(nvlist_lookup_nvlist_array(child
[c
],
3018 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
3020 /* find or add an entry for this top-level vdev */
3021 if (newchildren
> 0 &&
3022 (entry
= find_vdev_entry(zhp
, mchild
, mchildren
,
3023 newchild
, newchildren
)) >= 0) {
3024 /* We found a disk that the user specified. */
3025 vdev
= mchild
[entry
];
3028 /* User didn't specify a disk for this vdev. */
3029 vdev
= mchild
[mchildren
- 1];
3032 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
3036 /* did we find every disk the user specified? */
3037 if (found
!= newchildren
) {
3038 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Device list must "
3039 "include at most one disk from each mirror"));
3040 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3044 /* Prepare the nvlist for populating. */
3045 if (*newroot
== NULL
) {
3046 if (nvlist_alloc(newroot
, NV_UNIQUE_NAME
, 0) != 0)
3049 if (nvlist_add_string(*newroot
, ZPOOL_CONFIG_TYPE
,
3050 VDEV_TYPE_ROOT
) != 0)
3053 verify(nvlist_remove_all(*newroot
, ZPOOL_CONFIG_CHILDREN
) == 0);
3056 /* Add all the children we found */
3057 if (nvlist_add_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
, varray
,
3058 lastlog
== 0 ? vcount
: lastlog
) != 0)
3062 * If we're just doing a dry run, exit now with success.
3065 memory_err
= B_FALSE
;
3070 /* now build up the config list & call the ioctl */
3071 if (nvlist_alloc(&newconfig
, NV_UNIQUE_NAME
, 0) != 0)
3074 if (nvlist_add_nvlist(newconfig
,
3075 ZPOOL_CONFIG_VDEV_TREE
, *newroot
) != 0 ||
3076 nvlist_add_string(newconfig
,
3077 ZPOOL_CONFIG_POOL_NAME
, newname
) != 0 ||
3078 nvlist_add_uint64(newconfig
, ZPOOL_CONFIG_VERSION
, vers
) != 0)
3082 * The new pool is automatically part of the namespace unless we
3083 * explicitly export it.
3086 zc
.zc_cookie
= ZPOOL_EXPORT_AFTER_SPLIT
;
3087 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3088 (void) strlcpy(zc
.zc_string
, newname
, sizeof (zc
.zc_string
));
3089 if (zcmd_write_conf_nvlist(hdl
, &zc
, newconfig
) != 0)
3091 if (zc_props
!= NULL
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
3094 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SPLIT
, &zc
) != 0) {
3095 retval
= zpool_standard_error(hdl
, errno
, msg
);
3100 memory_err
= B_FALSE
;
3103 if (varray
!= NULL
) {
3106 for (v
= 0; v
< vcount
; v
++)
3107 nvlist_free(varray
[v
]);
3110 zcmd_free_nvlists(&zc
);
3112 nvlist_free(zc_props
);
3114 nvlist_free(newconfig
);
3116 nvlist_free(*newroot
);
3124 return (no_memory(hdl
));
3130 * Remove the given device. Currently, this is supported only for hot spares
3131 * and level 2 cache devices.
3134 zpool_vdev_remove(zpool_handle_t
*zhp
, const char *path
)
3136 zfs_cmd_t zc
= {"\0"};
3139 boolean_t avail_spare
, l2cache
, islog
;
3140 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3143 (void) snprintf(msg
, sizeof (msg
),
3144 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3146 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3147 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3149 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3151 * XXX - this should just go away.
3153 if (!avail_spare
&& !l2cache
&& !islog
) {
3154 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3155 "only inactive hot spares, cache, top-level, "
3156 "or log devices can be removed"));
3157 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3160 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
3161 if (islog
&& version
< SPA_VERSION_HOLES
) {
3162 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3163 "pool must be upgrade to support log removal"));
3164 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
3167 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
3169 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3172 return (zpool_standard_error(hdl
, errno
, msg
));
3176 * Clear the errors for the pool, or the particular device if specified.
3179 zpool_clear(zpool_handle_t
*zhp
, const char *path
, nvlist_t
*rewindnvl
)
3181 zfs_cmd_t zc
= {"\0"};
3184 zpool_rewind_policy_t policy
;
3185 boolean_t avail_spare
, l2cache
;
3186 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3187 nvlist_t
*nvi
= NULL
;
3191 (void) snprintf(msg
, sizeof (msg
),
3192 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3195 (void) snprintf(msg
, sizeof (msg
),
3196 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3199 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3201 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
,
3202 &l2cache
, NULL
)) == 0)
3203 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3206 * Don't allow error clearing for hot spares. Do allow
3207 * error clearing for l2cache devices.
3210 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3212 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
,
3216 zpool_get_rewind_policy(rewindnvl
, &policy
);
3217 zc
.zc_cookie
= policy
.zrp_request
;
3219 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zhp
->zpool_config_size
* 2) != 0)
3222 if (zcmd_write_src_nvlist(hdl
, &zc
, rewindnvl
) != 0)
3225 while ((error
= zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
)) != 0 &&
3227 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
3228 zcmd_free_nvlists(&zc
);
3233 if (!error
|| ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) &&
3234 errno
!= EPERM
&& errno
!= EACCES
)) {
3235 if (policy
.zrp_request
&
3236 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
3237 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nvi
);
3238 zpool_rewind_exclaim(hdl
, zc
.zc_name
,
3239 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0),
3243 zcmd_free_nvlists(&zc
);
3247 zcmd_free_nvlists(&zc
);
3248 return (zpool_standard_error(hdl
, errno
, msg
));
3252 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3255 zpool_vdev_clear(zpool_handle_t
*zhp
, uint64_t guid
)
3257 zfs_cmd_t zc
= {"\0"};
3259 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3261 (void) snprintf(msg
, sizeof (msg
),
3262 dgettext(TEXT_DOMAIN
, "cannot clear errors for %llx"),
3263 (u_longlong_t
)guid
);
3265 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3267 zc
.zc_cookie
= ZPOOL_NO_REWIND
;
3269 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_CLEAR
, &zc
) == 0)
3272 return (zpool_standard_error(hdl
, errno
, msg
));
3276 * Change the GUID for a pool.
3279 zpool_reguid(zpool_handle_t
*zhp
)
3282 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3283 zfs_cmd_t zc
= {"\0"};
3285 (void) snprintf(msg
, sizeof (msg
),
3286 dgettext(TEXT_DOMAIN
, "cannot reguid '%s'"), zhp
->zpool_name
);
3288 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3289 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REGUID
, &zc
) == 0)
3292 return (zpool_standard_error(hdl
, errno
, msg
));
3299 zpool_reopen(zpool_handle_t
*zhp
)
3301 zfs_cmd_t zc
= {"\0"};
3303 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3305 (void) snprintf(msg
, sizeof (msg
),
3306 dgettext(TEXT_DOMAIN
, "cannot reopen '%s'"),
3309 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3310 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REOPEN
, &zc
) == 0)
3312 return (zpool_standard_error(hdl
, errno
, msg
));
3315 #if defined(__sun__) || defined(__sun)
3317 * Convert from a devid string to a path.
3320 devid_to_path(char *devid_str
)
3325 devid_nmlist_t
*list
= NULL
;
3328 if (devid_str_decode(devid_str
, &devid
, &minor
) != 0)
3331 ret
= devid_deviceid_to_nmlist("/dev", devid
, minor
, &list
);
3333 devid_str_free(minor
);
3340 * In a case the strdup() fails, we will just return NULL below.
3342 path
= strdup(list
[0].devname
);
3344 devid_free_nmlist(list
);
3350 * Convert from a path to a devid string.
3353 path_to_devid(const char *path
)
3359 if ((fd
= open(path
, O_RDONLY
)) < 0)
3364 if (devid_get(fd
, &devid
) == 0) {
3365 if (devid_get_minor_name(fd
, &minor
) == 0)
3366 ret
= devid_str_encode(devid
, minor
);
3368 devid_str_free(minor
);
3377 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3378 * ignore any failure here, since a common case is for an unprivileged user to
3379 * type 'zpool status', and we'll display the correct information anyway.
3382 set_path(zpool_handle_t
*zhp
, nvlist_t
*nv
, const char *path
)
3384 zfs_cmd_t zc
= {"\0"};
3386 (void) strncpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3387 (void) strncpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
3388 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
3391 (void) ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_VDEV_SETPATH
, &zc
);
3396 * Remove partition suffix from a vdev path. Partition suffixes may take three
3397 * forms: "-partX", "pX", or "X", where X is a string of digits. The second
3398 * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
3399 * third case only occurs when preceded by a string matching the regular
3400 * expression "^([hsv]|xv)d[a-z]+", i.e. a scsi, ide, virtio or xen disk.
3403 strip_partition(libzfs_handle_t
*hdl
, char *path
)
3405 char *tmp
= zfs_strdup(hdl
, path
);
3406 char *part
= NULL
, *d
= NULL
;
3408 if ((part
= strstr(tmp
, "-part")) && part
!= tmp
) {
3410 } else if ((part
= strrchr(tmp
, 'p')) &&
3411 part
> tmp
+ 1 && isdigit(*(part
-1))) {
3413 } else if ((tmp
[0] == 'h' || tmp
[0] == 's' || tmp
[0] == 'v') &&
3415 for (d
= &tmp
[2]; isalpha(*d
); part
= ++d
);
3416 } else if (strncmp("xvd", tmp
, 3) == 0) {
3417 for (d
= &tmp
[3]; isalpha(*d
); part
= ++d
);
3419 if (part
&& d
&& *d
!= '\0') {
3420 for (; isdigit(*d
); d
++);
3427 #define PATH_BUF_LEN 64
3430 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3431 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3432 * We also check if this is a whole disk, in which case we strip off the
3433 * trailing 's0' slice name.
3435 * This routine is also responsible for identifying when disks have been
3436 * reconfigured in a new location. The kernel will have opened the device by
3437 * devid, but the path will still refer to the old location. To catch this, we
3438 * first do a path -> devid translation (which is fast for the common case). If
3439 * the devid matches, we're done. If not, we do a reverse devid -> path
3440 * translation and issue the appropriate ioctl() to update the path of the vdev.
3441 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3445 zpool_vdev_name(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, nvlist_t
*nv
,
3448 char *path
, *type
, *env
;
3450 char buf
[PATH_BUF_LEN
];
3451 char tmpbuf
[PATH_BUF_LEN
];
3453 env
= getenv("ZPOOL_VDEV_NAME_PATH");
3454 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3455 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3456 name_flags
|= VDEV_NAME_PATH
;
3458 env
= getenv("ZPOOL_VDEV_NAME_GUID");
3459 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3460 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3461 name_flags
|= VDEV_NAME_GUID
;
3463 env
= getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
3464 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3465 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3466 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
3468 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
, &value
) == 0 ||
3469 name_flags
& VDEV_NAME_GUID
) {
3470 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
);
3471 (void) snprintf(buf
, sizeof (buf
), "%llu", (u_longlong_t
)value
);
3473 } else if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
3474 #if defined(__sun__) || defined(__sun)
3476 * Live VDEV path updates to a kernel VDEV during a
3477 * zpool_vdev_name lookup are not supported on Linux.
3484 * If the device is dead (faulted, offline, etc) then don't
3485 * bother opening it. Otherwise we may be forcing the user to
3486 * open a misbehaving device, which can have undesirable
3489 if ((nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
3490 (uint64_t **)&vs
, &vsc
) != 0 ||
3491 vs
->vs_state
>= VDEV_STATE_DEGRADED
) &&
3493 nvlist_lookup_string(nv
, ZPOOL_CONFIG_DEVID
, &devid
) == 0) {
3495 * Determine if the current path is correct.
3497 char *newdevid
= path_to_devid(path
);
3499 if (newdevid
== NULL
||
3500 strcmp(devid
, newdevid
) != 0) {
3503 if ((newpath
= devid_to_path(devid
)) != NULL
) {
3505 * Update the path appropriately.
3507 set_path(zhp
, nv
, newpath
);
3508 if (nvlist_add_string(nv
,
3509 ZPOOL_CONFIG_PATH
, newpath
) == 0)
3510 verify(nvlist_lookup_string(nv
,
3518 devid_str_free(newdevid
);
3522 if (name_flags
& VDEV_NAME_FOLLOW_LINKS
) {
3523 char *rp
= realpath(path
, NULL
);
3525 strlcpy(buf
, rp
, sizeof (buf
));
3532 * For a block device only use the name.
3534 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
3535 if ((strcmp(type
, VDEV_TYPE_DISK
) == 0) &&
3536 !(name_flags
& VDEV_NAME_PATH
)) {
3537 path
= strrchr(path
, '/');
3542 * Remove the partition from the path it this is a whole disk.
3544 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
, &value
)
3545 == 0 && value
&& !(name_flags
& VDEV_NAME_PATH
)) {
3546 return (strip_partition(hdl
, path
));
3549 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &path
) == 0);
3552 * If it's a raidz device, we need to stick in the parity level.
3554 if (strcmp(path
, VDEV_TYPE_RAIDZ
) == 0) {
3556 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
,
3558 (void) snprintf(buf
, sizeof (buf
), "%s%llu", path
,
3559 (u_longlong_t
)value
);
3564 * We identify each top-level vdev by using a <type-id>
3565 * naming convention.
3567 if (name_flags
& VDEV_NAME_TYPE_ID
) {
3570 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
3572 (void) snprintf(tmpbuf
, sizeof (tmpbuf
), "%s-%llu",
3573 path
, (u_longlong_t
)id
);
3578 return (zfs_strdup(hdl
, path
));
3582 zbookmark_mem_compare(const void *a
, const void *b
)
3584 return (memcmp(a
, b
, sizeof (zbookmark_phys_t
)));
3588 * Retrieve the persistent error log, uniquify the members, and return to the
3592 zpool_get_errlog(zpool_handle_t
*zhp
, nvlist_t
**nverrlistp
)
3594 zfs_cmd_t zc
= {"\0"};
3596 zbookmark_phys_t
*zb
= NULL
;
3600 * Retrieve the raw error list from the kernel. If the number of errors
3601 * has increased, allocate more space and continue until we get the
3604 verify(nvlist_lookup_uint64(zhp
->zpool_config
, ZPOOL_CONFIG_ERRCOUNT
,
3608 if ((zc
.zc_nvlist_dst
= (uintptr_t)zfs_alloc(zhp
->zpool_hdl
,
3609 count
* sizeof (zbookmark_phys_t
))) == (uintptr_t)NULL
)
3611 zc
.zc_nvlist_dst_size
= count
;
3612 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3614 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_ERROR_LOG
,
3616 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3617 if (errno
== ENOMEM
) {
3620 count
= zc
.zc_nvlist_dst_size
;
3621 dst
= zfs_alloc(zhp
->zpool_hdl
, count
*
3622 sizeof (zbookmark_phys_t
));
3625 zc
.zc_nvlist_dst
= (uintptr_t)dst
;
3635 * Sort the resulting bookmarks. This is a little confusing due to the
3636 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3637 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3638 * _not_ copied as part of the process. So we point the start of our
3639 * array appropriate and decrement the total number of elements.
3641 zb
= ((zbookmark_phys_t
*)(uintptr_t)zc
.zc_nvlist_dst
) +
3642 zc
.zc_nvlist_dst_size
;
3643 count
-= zc
.zc_nvlist_dst_size
;
3645 qsort(zb
, count
, sizeof (zbookmark_phys_t
), zbookmark_mem_compare
);
3647 verify(nvlist_alloc(nverrlistp
, 0, KM_SLEEP
) == 0);
3650 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3652 for (i
= 0; i
< count
; i
++) {
3655 /* ignoring zb_blkid and zb_level for now */
3656 if (i
> 0 && zb
[i
-1].zb_objset
== zb
[i
].zb_objset
&&
3657 zb
[i
-1].zb_object
== zb
[i
].zb_object
)
3660 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) != 0)
3662 if (nvlist_add_uint64(nv
, ZPOOL_ERR_DATASET
,
3663 zb
[i
].zb_objset
) != 0) {
3667 if (nvlist_add_uint64(nv
, ZPOOL_ERR_OBJECT
,
3668 zb
[i
].zb_object
) != 0) {
3672 if (nvlist_add_nvlist(*nverrlistp
, "ejk", nv
) != 0) {
3679 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3683 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3684 return (no_memory(zhp
->zpool_hdl
));
3688 * Upgrade a ZFS pool to the latest on-disk version.
3691 zpool_upgrade(zpool_handle_t
*zhp
, uint64_t new_version
)
3693 zfs_cmd_t zc
= {"\0"};
3694 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3696 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3697 zc
.zc_cookie
= new_version
;
3699 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_UPGRADE
, &zc
) != 0)
3700 return (zpool_standard_error_fmt(hdl
, errno
,
3701 dgettext(TEXT_DOMAIN
, "cannot upgrade '%s'"),
3707 zfs_save_arguments(int argc
, char **argv
, char *string
, int len
)
3711 (void) strlcpy(string
, basename(argv
[0]), len
);
3712 for (i
= 1; i
< argc
; i
++) {
3713 (void) strlcat(string
, " ", len
);
3714 (void) strlcat(string
, argv
[i
], len
);
3719 zpool_log_history(libzfs_handle_t
*hdl
, const char *message
)
3721 zfs_cmd_t zc
= {"\0"};
3725 args
= fnvlist_alloc();
3726 fnvlist_add_string(args
, "message", message
);
3727 err
= zcmd_write_src_nvlist(hdl
, &zc
, args
);
3729 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_LOG_HISTORY
, &zc
);
3731 zcmd_free_nvlists(&zc
);
3736 * Perform ioctl to get some command history of a pool.
3738 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3739 * logical offset of the history buffer to start reading from.
3741 * Upon return, 'off' is the next logical offset to read from and
3742 * 'len' is the actual amount of bytes read into 'buf'.
3745 get_history(zpool_handle_t
*zhp
, char *buf
, uint64_t *off
, uint64_t *len
)
3747 zfs_cmd_t zc
= {"\0"};
3748 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3750 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3752 zc
.zc_history
= (uint64_t)(uintptr_t)buf
;
3753 zc
.zc_history_len
= *len
;
3754 zc
.zc_history_offset
= *off
;
3756 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_HISTORY
, &zc
) != 0) {
3759 return (zfs_error_fmt(hdl
, EZFS_PERM
,
3760 dgettext(TEXT_DOMAIN
,
3761 "cannot show history for pool '%s'"),
3764 return (zfs_error_fmt(hdl
, EZFS_NOHISTORY
,
3765 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
3766 "'%s'"), zhp
->zpool_name
));
3768 return (zfs_error_fmt(hdl
, EZFS_BADVERSION
,
3769 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
3770 "'%s', pool must be upgraded"), zhp
->zpool_name
));
3772 return (zpool_standard_error_fmt(hdl
, errno
,
3773 dgettext(TEXT_DOMAIN
,
3774 "cannot get history for '%s'"), zhp
->zpool_name
));
3778 *len
= zc
.zc_history_len
;
3779 *off
= zc
.zc_history_offset
;
3785 * Process the buffer of nvlists, unpacking and storing each nvlist record
3786 * into 'records'. 'leftover' is set to the number of bytes that weren't
3787 * processed as there wasn't a complete record.
3790 zpool_history_unpack(char *buf
, uint64_t bytes_read
, uint64_t *leftover
,
3791 nvlist_t
***records
, uint_t
*numrecords
)
3797 while (bytes_read
> sizeof (reclen
)) {
3799 /* get length of packed record (stored as little endian) */
3800 for (i
= 0, reclen
= 0; i
< sizeof (reclen
); i
++)
3801 reclen
+= (uint64_t)(((uchar_t
*)buf
)[i
]) << (8*i
);
3803 if (bytes_read
< sizeof (reclen
) + reclen
)
3807 if (nvlist_unpack(buf
+ sizeof (reclen
), reclen
, &nv
, 0) != 0)
3809 bytes_read
-= sizeof (reclen
) + reclen
;
3810 buf
+= sizeof (reclen
) + reclen
;
3812 /* add record to nvlist array */
3814 if (ISP2(*numrecords
+ 1)) {
3815 *records
= realloc(*records
,
3816 *numrecords
* 2 * sizeof (nvlist_t
*));
3818 (*records
)[*numrecords
- 1] = nv
;
3821 *leftover
= bytes_read
;
3826 * Retrieve the command history of a pool.
3829 zpool_get_history(zpool_handle_t
*zhp
, nvlist_t
**nvhisp
)
3832 int buflen
= 128 * 1024;
3834 nvlist_t
**records
= NULL
;
3835 uint_t numrecords
= 0;
3838 buf
= malloc(buflen
);
3842 uint64_t bytes_read
= buflen
;
3845 if ((err
= get_history(zhp
, buf
, &off
, &bytes_read
)) != 0)
3848 /* if nothing else was read in, we're at EOF, just return */
3852 if ((err
= zpool_history_unpack(buf
, bytes_read
,
3853 &leftover
, &records
, &numrecords
)) != 0)
3856 if (leftover
== bytes_read
) {
3858 * no progress made, because buffer is not big enough
3859 * to hold this record; resize and retry.
3863 buf
= malloc(buflen
);
3874 verify(nvlist_alloc(nvhisp
, NV_UNIQUE_NAME
, 0) == 0);
3875 verify(nvlist_add_nvlist_array(*nvhisp
, ZPOOL_HIST_RECORD
,
3876 records
, numrecords
) == 0);
3878 for (i
= 0; i
< numrecords
; i
++)
3879 nvlist_free(records
[i
]);
3886 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
3887 * If there is a new event available 'nvp' will contain a newly allocated
3888 * nvlist and 'dropped' will be set to the number of missed events since
3889 * the last call to this function. When 'nvp' is set to NULL it indicates
3890 * no new events are available. In either case the function returns 0 and
3891 * it is up to the caller to free 'nvp'. In the case of a fatal error the
3892 * function will return a non-zero value. When the function is called in
3893 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
3894 * it will not return until a new event is available.
3897 zpool_events_next(libzfs_handle_t
*hdl
, nvlist_t
**nvp
,
3898 int *dropped
, unsigned flags
, int zevent_fd
)
3900 zfs_cmd_t zc
= {"\0"};
3905 zc
.zc_cleanup_fd
= zevent_fd
;
3907 if (flags
& ZEVENT_NONBLOCK
)
3908 zc
.zc_guid
= ZEVENT_NONBLOCK
;
3910 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, ZEVENT_SIZE
) != 0)
3914 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_NEXT
, &zc
) != 0) {
3917 error
= zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
3918 dgettext(TEXT_DOMAIN
, "zfs shutdown"));
3921 /* Blocking error case should not occur */
3922 if (!(flags
& ZEVENT_NONBLOCK
))
3923 error
= zpool_standard_error_fmt(hdl
, errno
,
3924 dgettext(TEXT_DOMAIN
, "cannot get event"));
3928 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
3929 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
3930 dgettext(TEXT_DOMAIN
, "cannot get event"));
3936 error
= zpool_standard_error_fmt(hdl
, errno
,
3937 dgettext(TEXT_DOMAIN
, "cannot get event"));
3942 error
= zcmd_read_dst_nvlist(hdl
, &zc
, nvp
);
3946 *dropped
= (int)zc
.zc_cookie
;
3948 zcmd_free_nvlists(&zc
);
3957 zpool_events_clear(libzfs_handle_t
*hdl
, int *count
)
3959 zfs_cmd_t zc
= {"\0"};
3962 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
3963 "cannot clear events"));
3965 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_CLEAR
, &zc
) != 0)
3966 return (zpool_standard_error_fmt(hdl
, errno
, msg
));
3969 *count
= (int)zc
.zc_cookie
; /* # of events cleared */
3975 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
3976 * the passed zevent_fd file handle. On success zero is returned,
3977 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
3980 zpool_events_seek(libzfs_handle_t
*hdl
, uint64_t eid
, int zevent_fd
)
3982 zfs_cmd_t zc
= {"\0"};
3986 zc
.zc_cleanup_fd
= zevent_fd
;
3988 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_SEEK
, &zc
) != 0) {
3991 error
= zfs_error_fmt(hdl
, EZFS_NOENT
,
3992 dgettext(TEXT_DOMAIN
, "cannot get event"));
3996 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
3997 dgettext(TEXT_DOMAIN
, "cannot get event"));
4001 error
= zpool_standard_error_fmt(hdl
, errno
,
4002 dgettext(TEXT_DOMAIN
, "cannot get event"));
4011 zpool_obj_to_path(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4012 char *pathname
, size_t len
)
4014 zfs_cmd_t zc
= {"\0"};
4015 boolean_t mounted
= B_FALSE
;
4016 char *mntpnt
= NULL
;
4017 char dsname
[MAXNAMELEN
];
4020 /* special case for the MOS */
4021 (void) snprintf(pathname
, len
, "<metadata>:<0x%llx>",
4026 /* get the dataset's name */
4027 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4029 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
,
4030 ZFS_IOC_DSOBJ_TO_DSNAME
, &zc
) != 0) {
4031 /* just write out a path of two object numbers */
4032 (void) snprintf(pathname
, len
, "<0x%llx>:<0x%llx>",
4033 (longlong_t
)dsobj
, (longlong_t
)obj
);
4036 (void) strlcpy(dsname
, zc
.zc_value
, sizeof (dsname
));
4038 /* find out if the dataset is mounted */
4039 mounted
= is_mounted(zhp
->zpool_hdl
, dsname
, &mntpnt
);
4041 /* get the corrupted object's path */
4042 (void) strlcpy(zc
.zc_name
, dsname
, sizeof (zc
.zc_name
));
4044 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_OBJ_TO_PATH
,
4047 (void) snprintf(pathname
, len
, "%s%s", mntpnt
,
4050 (void) snprintf(pathname
, len
, "%s:%s",
4051 dsname
, zc
.zc_value
);
4054 (void) snprintf(pathname
, len
, "%s:<0x%llx>", dsname
,
4061 * Read the EFI label from the config, if a label does not exist then
4062 * pass back the error to the caller. If the caller has passed a non-NULL
4063 * diskaddr argument then we set it to the starting address of the EFI
4067 read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
)
4071 char diskname
[MAXPATHLEN
];
4074 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PATH
, &path
) != 0)
4077 (void) snprintf(diskname
, sizeof (diskname
), "%s%s", DISK_ROOT
,
4078 strrchr(path
, '/'));
4079 if ((fd
= open(diskname
, O_RDWR
|O_DIRECT
)) >= 0) {
4080 struct dk_gpt
*vtoc
;
4082 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) >= 0) {
4084 *sb
= vtoc
->efi_parts
[0].p_start
;
4093 * determine where a partition starts on a disk in the current
4097 find_start_block(nvlist_t
*config
)
4101 diskaddr_t sb
= MAXOFFSET_T
;
4104 if (nvlist_lookup_nvlist_array(config
,
4105 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) != 0) {
4106 if (nvlist_lookup_uint64(config
,
4107 ZPOOL_CONFIG_WHOLE_DISK
,
4108 &wholedisk
) != 0 || !wholedisk
) {
4109 return (MAXOFFSET_T
);
4111 if (read_efi_label(config
, &sb
) < 0)
4116 for (c
= 0; c
< children
; c
++) {
4117 sb
= find_start_block(child
[c
]);
4118 if (sb
!= MAXOFFSET_T
) {
4122 return (MAXOFFSET_T
);
4126 zpool_label_disk_check(char *path
)
4128 struct dk_gpt
*vtoc
;
4131 if ((fd
= open(path
, O_RDWR
|O_DIRECT
)) < 0)
4134 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) != 0) {
4139 if (vtoc
->efi_flags
& EFI_GPT_PRIMARY_CORRUPT
) {
4151 * Generate a unique partition name for the ZFS member. Partitions must
4152 * have unique names to ensure udev will be able to create symlinks under
4153 * /dev/disk/by-partlabel/ for all pool members. The partition names are
4154 * of the form <pool>-<unique-id>.
4157 zpool_label_name(char *label_name
, int label_size
)
4162 fd
= open("/dev/urandom", O_RDONLY
);
4164 if (read(fd
, &id
, sizeof (id
)) != sizeof (id
))
4171 id
= (((uint64_t)rand()) << 32) | (uint64_t)rand();
4173 snprintf(label_name
, label_size
, "zfs-%016llx", (u_longlong_t
) id
);
4177 * Label an individual disk. The name provided is the short name,
4178 * stripped of any leading /dev path.
4181 zpool_label_disk(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, char *name
)
4183 char path
[MAXPATHLEN
];
4184 struct dk_gpt
*vtoc
;
4186 size_t resv
= EFI_MIN_RESV_SIZE
;
4187 uint64_t slice_size
;
4188 diskaddr_t start_block
;
4191 /* prepare an error message just in case */
4192 (void) snprintf(errbuf
, sizeof (errbuf
),
4193 dgettext(TEXT_DOMAIN
, "cannot label '%s'"), name
);
4198 #if defined(__sun__) || defined(__sun)
4199 if (zpool_is_bootable(zhp
)) {
4200 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4201 "EFI labeled devices are not supported on root "
4203 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, errbuf
));
4207 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
4208 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
4210 if (zhp
->zpool_start_block
== 0)
4211 start_block
= find_start_block(nvroot
);
4213 start_block
= zhp
->zpool_start_block
;
4214 zhp
->zpool_start_block
= start_block
;
4217 start_block
= NEW_START_BLOCK
;
4220 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4222 if ((fd
= open(path
, O_RDWR
|O_DIRECT
)) < 0) {
4224 * This shouldn't happen. We've long since verified that this
4225 * is a valid device.
4227 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
4228 "label '%s': unable to open device: %d"), path
, errno
);
4229 return (zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
));
4232 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, &vtoc
) != 0) {
4234 * The only way this can fail is if we run out of memory, or we
4235 * were unable to read the disk's capacity
4237 if (errno
== ENOMEM
)
4238 (void) no_memory(hdl
);
4241 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
4242 "label '%s': unable to read disk capacity"), path
);
4244 return (zfs_error(hdl
, EZFS_NOCAP
, errbuf
));
4247 slice_size
= vtoc
->efi_last_u_lba
+ 1;
4248 slice_size
-= EFI_MIN_RESV_SIZE
;
4249 if (start_block
== MAXOFFSET_T
)
4250 start_block
= NEW_START_BLOCK
;
4251 slice_size
-= start_block
;
4252 slice_size
= P2ALIGN(slice_size
, PARTITION_END_ALIGNMENT
);
4254 vtoc
->efi_parts
[0].p_start
= start_block
;
4255 vtoc
->efi_parts
[0].p_size
= slice_size
;
4258 * Why we use V_USR: V_BACKUP confuses users, and is considered
4259 * disposable by some EFI utilities (since EFI doesn't have a backup
4260 * slice). V_UNASSIGNED is supposed to be used only for zero size
4261 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4262 * etc. were all pretty specific. V_USR is as close to reality as we
4263 * can get, in the absence of V_OTHER.
4265 vtoc
->efi_parts
[0].p_tag
= V_USR
;
4266 zpool_label_name(vtoc
->efi_parts
[0].p_name
, EFI_PART_NAME_LEN
);
4268 vtoc
->efi_parts
[8].p_start
= slice_size
+ start_block
;
4269 vtoc
->efi_parts
[8].p_size
= resv
;
4270 vtoc
->efi_parts
[8].p_tag
= V_RESERVED
;
4272 if ((rval
= efi_write(fd
, vtoc
)) != 0 || (rval
= efi_rescan(fd
)) != 0) {
4274 * Some block drivers (like pcata) may not support EFI
4275 * GPT labels. Print out a helpful error message dir-
4276 * ecting the user to manually label the disk and give
4282 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "try using "
4283 "parted(8) and then provide a specific slice: %d"), rval
);
4284 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4290 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4291 (void) zfs_append_partition(path
, MAXPATHLEN
);
4293 /* Wait to udev to signal use the device has settled. */
4294 rval
= zpool_label_disk_wait(path
, DISK_LABEL_WAIT
);
4296 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "failed to "
4297 "detect device partitions on '%s': %d"), path
, rval
);
4298 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4301 /* We can't be to paranoid. Read the label back and verify it. */
4302 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4303 rval
= zpool_label_disk_check(path
);
4305 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "freshly written "
4306 "EFI label on '%s' is damaged. Ensure\nthis device "
4307 "is not in in use, and is functioning properly: %d"),
4309 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));