4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 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
,
1219 ZFS_TYPE_FILESYSTEM
, fsprops
, zoned
, 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 occurs when one of the devices is below
1261 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1262 * device was the problem device since there's no
1263 * reliable way to determine device size from userland.
1268 zfs_nicenum(SPA_MINDEVSIZE
, buf
, sizeof (buf
));
1270 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1271 "one or more devices is less than the "
1272 "minimum size (%s)"), buf
);
1274 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1277 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1278 "one or more devices is out of space"));
1279 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1282 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1283 "cache device must be a disk or disk slice"));
1284 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1287 return (zpool_standard_error(hdl
, errno
, msg
));
1292 zcmd_free_nvlists(&zc
);
1293 nvlist_free(zc_props
);
1294 nvlist_free(zc_fsprops
);
1299 * Destroy the given pool. It is up to the caller to ensure that there are no
1300 * datasets left in the pool.
1303 zpool_destroy(zpool_handle_t
*zhp
, const char *log_str
)
1305 zfs_cmd_t zc
= {"\0"};
1306 zfs_handle_t
*zfp
= NULL
;
1307 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1310 if (zhp
->zpool_state
== POOL_STATE_ACTIVE
&&
1311 (zfp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
1314 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1315 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1317 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_DESTROY
, &zc
) != 0) {
1318 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1319 "cannot destroy '%s'"), zhp
->zpool_name
);
1321 if (errno
== EROFS
) {
1322 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1323 "one or more devices is read only"));
1324 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1326 (void) zpool_standard_error(hdl
, errno
, msg
);
1335 remove_mountpoint(zfp
);
1343 * Add the given vdevs to the pool. The caller must have already performed the
1344 * necessary verification to ensure that the vdev specification is well-formed.
1347 zpool_add(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
1349 zfs_cmd_t zc
= {"\0"};
1351 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1353 nvlist_t
**spares
, **l2cache
;
1354 uint_t nspares
, nl2cache
;
1356 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1357 "cannot add to '%s'"), zhp
->zpool_name
);
1359 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1360 SPA_VERSION_SPARES
&&
1361 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1362 &spares
, &nspares
) == 0) {
1363 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1364 "upgraded to add hot spares"));
1365 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1368 #if defined(__sun__) || defined(__sun)
1369 if (zpool_is_bootable(zhp
) && nvlist_lookup_nvlist_array(nvroot
,
1370 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0) {
1373 for (s
= 0; s
< nspares
; s
++) {
1376 if (nvlist_lookup_string(spares
[s
], ZPOOL_CONFIG_PATH
,
1377 &path
) == 0 && pool_uses_efi(spares
[s
])) {
1378 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1379 "device '%s' contains an EFI label and "
1380 "cannot be used on root pools."),
1381 zpool_vdev_name(hdl
, NULL
, spares
[s
], 0));
1382 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, msg
));
1388 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1389 SPA_VERSION_L2CACHE
&&
1390 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1391 &l2cache
, &nl2cache
) == 0) {
1392 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1393 "upgraded to add cache devices"));
1394 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1397 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1399 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1401 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_ADD
, &zc
) != 0) {
1405 * This can happen if the user has specified the same
1406 * device multiple times. We can't reliably detect this
1407 * until we try to add it and see we already have a
1410 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1411 "one or more vdevs refer to the same device"));
1412 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1417 * This occurrs when one of the devices is below
1418 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1419 * device was the problem device since there's no
1420 * reliable way to determine device size from userland.
1425 zfs_nicenum(SPA_MINDEVSIZE
, buf
, sizeof (buf
));
1427 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1428 "device is less than the minimum "
1431 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1435 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1436 "pool must be upgraded to add these vdevs"));
1437 (void) zfs_error(hdl
, EZFS_BADVERSION
, msg
);
1441 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1442 "cache device must be a disk or disk slice"));
1443 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1447 (void) zpool_standard_error(hdl
, errno
, msg
);
1455 zcmd_free_nvlists(&zc
);
1461 * Exports the pool from the system. The caller must ensure that there are no
1462 * mounted datasets in the pool.
1465 zpool_export_common(zpool_handle_t
*zhp
, boolean_t force
, boolean_t hardforce
,
1466 const char *log_str
)
1468 zfs_cmd_t zc
= {"\0"};
1471 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1472 "cannot export '%s'"), zhp
->zpool_name
);
1474 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1475 zc
.zc_cookie
= force
;
1476 zc
.zc_guid
= hardforce
;
1477 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1479 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_EXPORT
, &zc
) != 0) {
1482 zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
1483 "use '-f' to override the following errors:\n"
1484 "'%s' has an active shared spare which could be"
1485 " used by other pools once '%s' is exported."),
1486 zhp
->zpool_name
, zhp
->zpool_name
);
1487 return (zfs_error(zhp
->zpool_hdl
, EZFS_ACTIVE_SPARE
,
1490 return (zpool_standard_error_fmt(zhp
->zpool_hdl
, errno
,
1499 zpool_export(zpool_handle_t
*zhp
, boolean_t force
, const char *log_str
)
1501 return (zpool_export_common(zhp
, force
, B_FALSE
, log_str
));
1505 zpool_export_force(zpool_handle_t
*zhp
, const char *log_str
)
1507 return (zpool_export_common(zhp
, B_TRUE
, B_TRUE
, log_str
));
1511 zpool_rewind_exclaim(libzfs_handle_t
*hdl
, const char *name
, boolean_t dryrun
,
1514 nvlist_t
*nv
= NULL
;
1520 if (!hdl
->libzfs_printerr
|| config
== NULL
)
1523 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1524 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0) {
1528 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1530 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1532 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1533 strftime(timestr
, 128, "%c", &t
) != 0) {
1535 (void) printf(dgettext(TEXT_DOMAIN
,
1536 "Would be able to return %s "
1537 "to its state as of %s.\n"),
1540 (void) printf(dgettext(TEXT_DOMAIN
,
1541 "Pool %s returned to its state as of %s.\n"),
1545 (void) printf(dgettext(TEXT_DOMAIN
,
1546 "%s approximately %lld "),
1547 dryrun
? "Would discard" : "Discarded",
1548 ((longlong_t
)loss
+ 30) / 60);
1549 (void) printf(dgettext(TEXT_DOMAIN
,
1550 "minutes of transactions.\n"));
1551 } else if (loss
> 0) {
1552 (void) printf(dgettext(TEXT_DOMAIN
,
1553 "%s approximately %lld "),
1554 dryrun
? "Would discard" : "Discarded",
1556 (void) printf(dgettext(TEXT_DOMAIN
,
1557 "seconds of transactions.\n"));
1563 zpool_explain_recover(libzfs_handle_t
*hdl
, const char *name
, int reason
,
1566 nvlist_t
*nv
= NULL
;
1568 uint64_t edata
= UINT64_MAX
;
1573 if (!hdl
->libzfs_printerr
)
1577 (void) printf(dgettext(TEXT_DOMAIN
, "action: "));
1579 (void) printf(dgettext(TEXT_DOMAIN
, "\t"));
1581 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1582 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1583 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0 ||
1584 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1587 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1588 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_DATA_ERRORS
,
1591 (void) printf(dgettext(TEXT_DOMAIN
,
1592 "Recovery is possible, but will result in some data loss.\n"));
1594 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1595 strftime(timestr
, 128, "%c", &t
) != 0) {
1596 (void) printf(dgettext(TEXT_DOMAIN
,
1597 "\tReturning the pool to its state as of %s\n"
1598 "\tshould correct the problem. "),
1601 (void) printf(dgettext(TEXT_DOMAIN
,
1602 "\tReverting the pool to an earlier state "
1603 "should correct the problem.\n\t"));
1607 (void) printf(dgettext(TEXT_DOMAIN
,
1608 "Approximately %lld minutes of data\n"
1609 "\tmust be discarded, irreversibly. "),
1610 ((longlong_t
)loss
+ 30) / 60);
1611 } else if (loss
> 0) {
1612 (void) printf(dgettext(TEXT_DOMAIN
,
1613 "Approximately %lld seconds of data\n"
1614 "\tmust be discarded, irreversibly. "),
1617 if (edata
!= 0 && edata
!= UINT64_MAX
) {
1619 (void) printf(dgettext(TEXT_DOMAIN
,
1620 "After rewind, at least\n"
1621 "\tone persistent user-data error will remain. "));
1623 (void) printf(dgettext(TEXT_DOMAIN
,
1624 "After rewind, several\n"
1625 "\tpersistent user-data errors will remain. "));
1628 (void) printf(dgettext(TEXT_DOMAIN
,
1629 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1630 reason
>= 0 ? "clear" : "import", name
);
1632 (void) printf(dgettext(TEXT_DOMAIN
,
1633 "A scrub of the pool\n"
1634 "\tis strongly recommended after recovery.\n"));
1638 (void) printf(dgettext(TEXT_DOMAIN
,
1639 "Destroy and re-create the pool from\n\ta backup source.\n"));
1643 * zpool_import() is a contracted interface. Should be kept the same
1646 * Applications should use zpool_import_props() to import a pool with
1647 * new properties value to be set.
1650 zpool_import(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1653 nvlist_t
*props
= NULL
;
1656 if (altroot
!= NULL
) {
1657 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
1658 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1659 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1663 if (nvlist_add_string(props
,
1664 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), altroot
) != 0 ||
1665 nvlist_add_string(props
,
1666 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
), "none") != 0) {
1668 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1669 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1674 ret
= zpool_import_props(hdl
, config
, newname
, props
,
1682 print_vdev_tree(libzfs_handle_t
*hdl
, const char *name
, nvlist_t
*nv
,
1688 uint64_t is_log
= 0;
1690 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
,
1694 (void) printf("\t%*s%s%s\n", indent
, "", name
,
1695 is_log
? " [log]" : "");
1697 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1698 &child
, &children
) != 0)
1701 for (c
= 0; c
< children
; c
++) {
1702 vname
= zpool_vdev_name(hdl
, NULL
, child
[c
], VDEV_NAME_TYPE_ID
);
1703 print_vdev_tree(hdl
, vname
, child
[c
], indent
+ 2);
1709 zpool_print_unsup_feat(nvlist_t
*config
)
1711 nvlist_t
*nvinfo
, *unsup_feat
;
1714 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) ==
1716 verify(nvlist_lookup_nvlist(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
,
1719 for (nvp
= nvlist_next_nvpair(unsup_feat
, NULL
); nvp
!= NULL
;
1720 nvp
= nvlist_next_nvpair(unsup_feat
, nvp
)) {
1723 verify(nvpair_type(nvp
) == DATA_TYPE_STRING
);
1724 verify(nvpair_value_string(nvp
, &desc
) == 0);
1726 if (strlen(desc
) > 0)
1727 (void) printf("\t%s (%s)\n", nvpair_name(nvp
), desc
);
1729 (void) printf("\t%s\n", nvpair_name(nvp
));
1734 * Import the given pool using the known configuration and a list of
1735 * properties to be set. The configuration should have come from
1736 * zpool_find_import(). The 'newname' parameters control whether the pool
1737 * is imported with a different name.
1740 zpool_import_props(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1741 nvlist_t
*props
, int flags
)
1743 zfs_cmd_t zc
= {"\0"};
1744 zpool_rewind_policy_t policy
;
1745 nvlist_t
*nv
= NULL
;
1746 nvlist_t
*nvinfo
= NULL
;
1747 nvlist_t
*missing
= NULL
;
1754 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1757 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1758 "cannot import pool '%s'"), origname
);
1760 if (newname
!= NULL
) {
1761 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
1762 return (zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1763 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1765 thename
= (char *)newname
;
1772 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
1774 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
1777 if ((props
= zpool_valid_proplist(hdl
, origname
,
1778 props
, version
, flags
, errbuf
)) == NULL
) {
1780 } else if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
1786 (void) strlcpy(zc
.zc_name
, thename
, sizeof (zc
.zc_name
));
1788 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1791 if (zcmd_write_conf_nvlist(hdl
, &zc
, config
) != 0) {
1795 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zc
.zc_nvlist_conf_size
* 2) != 0) {
1800 zc
.zc_cookie
= flags
;
1801 while ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_IMPORT
, &zc
)) != 0 &&
1803 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
1804 zcmd_free_nvlists(&zc
);
1811 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nv
);
1812 zpool_get_rewind_policy(config
, &policy
);
1818 * Dry-run failed, but we print out what success
1819 * looks like if we found a best txg
1821 if (policy
.zrp_request
& ZPOOL_TRY_REWIND
) {
1822 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1828 if (newname
== NULL
)
1829 (void) snprintf(desc
, sizeof (desc
),
1830 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1833 (void) snprintf(desc
, sizeof (desc
),
1834 dgettext(TEXT_DOMAIN
, "cannot import '%s' as '%s'"),
1839 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
1840 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1841 nvlist_exists(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
)) {
1842 (void) printf(dgettext(TEXT_DOMAIN
, "This "
1843 "pool uses the following feature(s) not "
1844 "supported by this system:\n"));
1845 zpool_print_unsup_feat(nv
);
1846 if (nvlist_exists(nvinfo
,
1847 ZPOOL_CONFIG_CAN_RDONLY
)) {
1848 (void) printf(dgettext(TEXT_DOMAIN
,
1849 "All unsupported features are only "
1850 "required for writing to the pool."
1851 "\nThe pool can be imported using "
1852 "'-o readonly=on'.\n"));
1856 * Unsupported version.
1858 (void) zfs_error(hdl
, EZFS_BADVERSION
, desc
);
1862 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, desc
);
1866 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1867 "one or more devices is read only"));
1868 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
1872 if (nv
&& nvlist_lookup_nvlist(nv
,
1873 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1874 nvlist_lookup_nvlist(nvinfo
,
1875 ZPOOL_CONFIG_MISSING_DEVICES
, &missing
) == 0) {
1876 (void) printf(dgettext(TEXT_DOMAIN
,
1877 "The devices below are missing, use "
1878 "'-m' to import the pool anyway:\n"));
1879 print_vdev_tree(hdl
, NULL
, missing
, 2);
1880 (void) printf("\n");
1882 (void) zpool_standard_error(hdl
, error
, desc
);
1886 (void) zpool_standard_error(hdl
, error
, desc
);
1890 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1891 "one or more devices are already in use\n"));
1892 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
1895 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1896 "new name of at least one dataset is longer than "
1897 "the maximum allowable length"));
1898 (void) zfs_error(hdl
, EZFS_NAMETOOLONG
, desc
);
1901 (void) zpool_standard_error(hdl
, error
, desc
);
1902 zpool_explain_recover(hdl
,
1903 newname
? origname
: thename
, -error
, nv
);
1910 zpool_handle_t
*zhp
;
1913 * This should never fail, but play it safe anyway.
1915 if (zpool_open_silent(hdl
, thename
, &zhp
) != 0)
1917 else if (zhp
!= NULL
)
1919 if (policy
.zrp_request
&
1920 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
1921 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1922 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0), nv
);
1928 zcmd_free_nvlists(&zc
);
1938 zpool_scan(zpool_handle_t
*zhp
, pool_scan_func_t func
)
1940 zfs_cmd_t zc
= {"\0"};
1942 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1944 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1945 zc
.zc_cookie
= func
;
1947 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_SCAN
, &zc
) == 0 ||
1948 (errno
== ENOENT
&& func
!= POOL_SCAN_NONE
))
1951 if (func
== POOL_SCAN_SCRUB
) {
1952 (void) snprintf(msg
, sizeof (msg
),
1953 dgettext(TEXT_DOMAIN
, "cannot scrub %s"), zc
.zc_name
);
1954 } else if (func
== POOL_SCAN_NONE
) {
1955 (void) snprintf(msg
, sizeof (msg
),
1956 dgettext(TEXT_DOMAIN
, "cannot cancel scrubbing %s"),
1959 assert(!"unexpected result");
1962 if (errno
== EBUSY
) {
1964 pool_scan_stat_t
*ps
= NULL
;
1967 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
1968 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
1969 (void) nvlist_lookup_uint64_array(nvroot
,
1970 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &psc
);
1971 if (ps
&& ps
->pss_func
== POOL_SCAN_SCRUB
)
1972 return (zfs_error(hdl
, EZFS_SCRUBBING
, msg
));
1974 return (zfs_error(hdl
, EZFS_RESILVERING
, msg
));
1975 } else if (errno
== ENOENT
) {
1976 return (zfs_error(hdl
, EZFS_NO_SCRUB
, msg
));
1978 return (zpool_standard_error(hdl
, errno
, msg
));
1983 * Find a vdev that matches the search criteria specified. We use the
1984 * the nvpair name to determine how we should look for the device.
1985 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1986 * spare; but FALSE if its an INUSE spare.
1989 vdev_to_nvlist_iter(nvlist_t
*nv
, nvlist_t
*search
, boolean_t
*avail_spare
,
1990 boolean_t
*l2cache
, boolean_t
*log
)
1997 nvpair_t
*pair
= nvlist_next_nvpair(search
, NULL
);
1999 /* Nothing to look for */
2000 if (search
== NULL
|| pair
== NULL
)
2003 /* Obtain the key we will use to search */
2004 srchkey
= nvpair_name(pair
);
2006 switch (nvpair_type(pair
)) {
2007 case DATA_TYPE_UINT64
:
2008 if (strcmp(srchkey
, ZPOOL_CONFIG_GUID
) == 0) {
2009 uint64_t srchval
, theguid
;
2011 verify(nvpair_value_uint64(pair
, &srchval
) == 0);
2012 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
2014 if (theguid
== srchval
)
2019 case DATA_TYPE_STRING
: {
2020 char *srchval
, *val
;
2022 verify(nvpair_value_string(pair
, &srchval
) == 0);
2023 if (nvlist_lookup_string(nv
, srchkey
, &val
) != 0)
2027 * Search for the requested value. Special cases:
2029 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2030 * "-part1", or "p1". The suffix is hidden from the user,
2031 * but included in the string, so this matches around it.
2032 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2033 * is used to check all possible expanded paths.
2034 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2036 * Otherwise, all other searches are simple string compares.
2038 if (strcmp(srchkey
, ZPOOL_CONFIG_PATH
) == 0) {
2039 uint64_t wholedisk
= 0;
2041 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
2043 if (zfs_strcmp_pathname(srchval
, val
, wholedisk
) == 0)
2046 } else if (strcmp(srchkey
, ZPOOL_CONFIG_TYPE
) == 0 && val
) {
2047 char *type
, *idx
, *end
, *p
;
2048 uint64_t id
, vdev_id
;
2051 * Determine our vdev type, keeping in mind
2052 * that the srchval is composed of a type and
2053 * vdev id pair (i.e. mirror-4).
2055 if ((type
= strdup(srchval
)) == NULL
)
2058 if ((p
= strrchr(type
, '-')) == NULL
) {
2066 * If the types don't match then keep looking.
2068 if (strncmp(val
, type
, strlen(val
)) != 0) {
2073 verify(strncmp(type
, VDEV_TYPE_RAIDZ
,
2074 strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2075 strncmp(type
, VDEV_TYPE_MIRROR
,
2076 strlen(VDEV_TYPE_MIRROR
)) == 0);
2077 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
2081 vdev_id
= strtoull(idx
, &end
, 10);
2088 * Now verify that we have the correct vdev id.
2097 if (strcmp(srchval
, val
) == 0)
2106 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2107 &child
, &children
) != 0)
2110 for (c
= 0; c
< children
; c
++) {
2111 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2112 avail_spare
, l2cache
, NULL
)) != NULL
) {
2114 * The 'is_log' value is only set for the toplevel
2115 * vdev, not the leaf vdevs. So we always lookup the
2116 * log device from the root of the vdev tree (where
2117 * 'log' is non-NULL).
2120 nvlist_lookup_uint64(child
[c
],
2121 ZPOOL_CONFIG_IS_LOG
, &is_log
) == 0 &&
2129 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2130 &child
, &children
) == 0) {
2131 for (c
= 0; c
< children
; c
++) {
2132 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2133 avail_spare
, l2cache
, NULL
)) != NULL
) {
2134 *avail_spare
= B_TRUE
;
2140 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
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
) {
2155 * Given a physical path (minus the "/devices" prefix), find the
2159 zpool_find_vdev_by_physpath(zpool_handle_t
*zhp
, const char *ppath
,
2160 boolean_t
*avail_spare
, boolean_t
*l2cache
, boolean_t
*log
)
2162 nvlist_t
*search
, *nvroot
, *ret
;
2164 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2165 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PHYS_PATH
, ppath
) == 0);
2167 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2170 *avail_spare
= B_FALSE
;
2174 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2175 nvlist_free(search
);
2181 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2184 zpool_vdev_is_interior(const char *name
)
2186 if (strncmp(name
, VDEV_TYPE_RAIDZ
, strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2187 strncmp(name
, VDEV_TYPE_MIRROR
, strlen(VDEV_TYPE_MIRROR
)) == 0)
2193 zpool_find_vdev(zpool_handle_t
*zhp
, const char *path
, boolean_t
*avail_spare
,
2194 boolean_t
*l2cache
, boolean_t
*log
)
2197 nvlist_t
*nvroot
, *search
, *ret
;
2200 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2202 guid
= strtoull(path
, &end
, 0);
2203 if (guid
!= 0 && *end
== '\0') {
2204 verify(nvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
) == 0);
2205 } else if (zpool_vdev_is_interior(path
)) {
2206 verify(nvlist_add_string(search
, ZPOOL_CONFIG_TYPE
, path
) == 0);
2208 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PATH
, path
) == 0);
2211 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2214 *avail_spare
= B_FALSE
;
2218 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2219 nvlist_free(search
);
2225 vdev_online(nvlist_t
*nv
)
2229 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
, &ival
) == 0 ||
2230 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
, &ival
) == 0 ||
2231 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
, &ival
) == 0)
2238 * Helper function for zpool_get_physpaths().
2241 vdev_get_one_physpath(nvlist_t
*config
, char *physpath
, size_t physpath_size
,
2242 size_t *bytes_written
)
2244 size_t bytes_left
, pos
, rsz
;
2248 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PHYS_PATH
,
2250 return (EZFS_NODEVICE
);
2252 pos
= *bytes_written
;
2253 bytes_left
= physpath_size
- pos
;
2254 format
= (pos
== 0) ? "%s" : " %s";
2256 rsz
= snprintf(physpath
+ pos
, bytes_left
, format
, tmppath
);
2257 *bytes_written
+= rsz
;
2259 if (rsz
>= bytes_left
) {
2260 /* if physpath was not copied properly, clear it */
2261 if (bytes_left
!= 0) {
2264 return (EZFS_NOSPC
);
2270 vdev_get_physpaths(nvlist_t
*nv
, char *physpath
, size_t phypath_size
,
2271 size_t *rsz
, boolean_t is_spare
)
2276 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
2277 return (EZFS_INVALCONFIG
);
2279 if (strcmp(type
, VDEV_TYPE_DISK
) == 0) {
2281 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2282 * For a spare vdev, we only want to boot from the active
2287 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
2290 return (EZFS_INVALCONFIG
);
2293 if (vdev_online(nv
)) {
2294 if ((ret
= vdev_get_one_physpath(nv
, physpath
,
2295 phypath_size
, rsz
)) != 0)
2298 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) == 0 ||
2299 strcmp(type
, VDEV_TYPE_REPLACING
) == 0 ||
2300 (is_spare
= (strcmp(type
, VDEV_TYPE_SPARE
) == 0))) {
2305 if (nvlist_lookup_nvlist_array(nv
,
2306 ZPOOL_CONFIG_CHILDREN
, &child
, &count
) != 0)
2307 return (EZFS_INVALCONFIG
);
2309 for (i
= 0; i
< count
; i
++) {
2310 ret
= vdev_get_physpaths(child
[i
], physpath
,
2311 phypath_size
, rsz
, is_spare
);
2312 if (ret
== EZFS_NOSPC
)
2317 return (EZFS_POOL_INVALARG
);
2321 * Get phys_path for a root pool config.
2322 * Return 0 on success; non-zero on failure.
2325 zpool_get_config_physpath(nvlist_t
*config
, char *physpath
, size_t phypath_size
)
2328 nvlist_t
*vdev_root
;
2335 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2337 return (EZFS_INVALCONFIG
);
2339 if (nvlist_lookup_string(vdev_root
, ZPOOL_CONFIG_TYPE
, &type
) != 0 ||
2340 nvlist_lookup_nvlist_array(vdev_root
, ZPOOL_CONFIG_CHILDREN
,
2341 &child
, &count
) != 0)
2342 return (EZFS_INVALCONFIG
);
2344 #if defined(__sun__) || defined(__sun)
2346 * root pool can not have EFI labeled disks and can only have
2347 * a single top-level vdev.
2349 if (strcmp(type
, VDEV_TYPE_ROOT
) != 0 || count
!= 1 ||
2350 pool_uses_efi(vdev_root
))
2351 return (EZFS_POOL_INVALARG
);
2354 (void) vdev_get_physpaths(child
[0], physpath
, phypath_size
, &rsz
,
2357 /* No online devices */
2359 return (EZFS_NODEVICE
);
2365 * Get phys_path for a root pool
2366 * Return 0 on success; non-zero on failure.
2369 zpool_get_physpath(zpool_handle_t
*zhp
, char *physpath
, size_t phypath_size
)
2371 return (zpool_get_config_physpath(zhp
->zpool_config
, physpath
,
2376 * If the device has being dynamically expanded then we need to relabel
2377 * the disk to use the new unallocated space.
2380 zpool_relabel_disk(libzfs_handle_t
*hdl
, const char *path
, const char *msg
)
2384 if ((fd
= open(path
, O_RDWR
|O_DIRECT
)) < 0) {
2385 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2386 "relabel '%s': unable to open device: %d"), path
, errno
);
2387 return (zfs_error(hdl
, EZFS_OPENFAILED
, msg
));
2391 * It's possible that we might encounter an error if the device
2392 * does not have any unallocated space left. If so, we simply
2393 * ignore that error and continue on.
2395 * Also, we don't call efi_rescan() - that would just return EBUSY.
2396 * The module will do it for us in vdev_disk_open().
2398 error
= efi_use_whole_disk(fd
);
2400 if (error
&& error
!= VT_ENOSPC
) {
2401 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2402 "relabel '%s': unable to read disk capacity"), path
);
2403 return (zfs_error(hdl
, EZFS_NOCAP
, msg
));
2409 * Bring the specified vdev online. The 'flags' parameter is a set of the
2410 * ZFS_ONLINE_* flags.
2413 zpool_vdev_online(zpool_handle_t
*zhp
, const char *path
, int flags
,
2414 vdev_state_t
*newstate
)
2416 zfs_cmd_t zc
= {"\0"};
2419 boolean_t avail_spare
, l2cache
, islog
;
2420 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2423 if (flags
& ZFS_ONLINE_EXPAND
) {
2424 (void) snprintf(msg
, sizeof (msg
),
2425 dgettext(TEXT_DOMAIN
, "cannot expand %s"), path
);
2427 (void) snprintf(msg
, sizeof (msg
),
2428 dgettext(TEXT_DOMAIN
, "cannot online %s"), path
);
2431 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2432 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2434 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2436 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2439 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2441 if (flags
& ZFS_ONLINE_EXPAND
||
2442 zpool_get_prop_int(zhp
, ZPOOL_PROP_AUTOEXPAND
, NULL
)) {
2443 uint64_t wholedisk
= 0;
2445 (void) nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_WHOLE_DISK
,
2449 * XXX - L2ARC 1.0 devices can't support expansion.
2452 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2453 "cannot expand cache devices"));
2454 return (zfs_error(hdl
, EZFS_VDEVNOTSUP
, msg
));
2458 const char *fullpath
= path
;
2459 char buf
[MAXPATHLEN
];
2461 if (path
[0] != '/') {
2462 error
= zfs_resolve_shortname(path
, buf
,
2465 return (zfs_error(hdl
, EZFS_NODEVICE
,
2471 error
= zpool_relabel_disk(hdl
, fullpath
, msg
);
2477 zc
.zc_cookie
= VDEV_STATE_ONLINE
;
2480 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) != 0) {
2481 if (errno
== EINVAL
) {
2482 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "was split "
2483 "from this pool into a new one. Use '%s' "
2484 "instead"), "zpool detach");
2485 return (zfs_error(hdl
, EZFS_POSTSPLIT_ONLINE
, msg
));
2487 return (zpool_standard_error(hdl
, errno
, msg
));
2490 *newstate
= zc
.zc_cookie
;
2495 * Take the specified vdev offline
2498 zpool_vdev_offline(zpool_handle_t
*zhp
, const char *path
, boolean_t istmp
)
2500 zfs_cmd_t zc
= {"\0"};
2503 boolean_t avail_spare
, l2cache
;
2504 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2506 (void) snprintf(msg
, sizeof (msg
),
2507 dgettext(TEXT_DOMAIN
, "cannot offline %s"), path
);
2509 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2510 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2512 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2514 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2517 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2519 zc
.zc_cookie
= VDEV_STATE_OFFLINE
;
2520 zc
.zc_obj
= istmp
? ZFS_OFFLINE_TEMPORARY
: 0;
2522 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2529 * There are no other replicas of this device.
2531 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2535 * The log device has unplayed logs
2537 return (zfs_error(hdl
, EZFS_UNPLAYED_LOGS
, msg
));
2540 return (zpool_standard_error(hdl
, errno
, msg
));
2545 * Mark the given vdev faulted.
2548 zpool_vdev_fault(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2550 zfs_cmd_t zc
= {"\0"};
2552 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2554 (void) snprintf(msg
, sizeof (msg
),
2555 dgettext(TEXT_DOMAIN
, "cannot fault %llu"), (u_longlong_t
)guid
);
2557 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2559 zc
.zc_cookie
= VDEV_STATE_FAULTED
;
2562 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2569 * There are no other replicas of this device.
2571 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2574 return (zpool_standard_error(hdl
, errno
, msg
));
2580 * Mark the given vdev degraded.
2583 zpool_vdev_degrade(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2585 zfs_cmd_t zc
= {"\0"};
2587 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2589 (void) snprintf(msg
, sizeof (msg
),
2590 dgettext(TEXT_DOMAIN
, "cannot degrade %llu"), (u_longlong_t
)guid
);
2592 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2594 zc
.zc_cookie
= VDEV_STATE_DEGRADED
;
2597 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2600 return (zpool_standard_error(hdl
, errno
, msg
));
2604 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2608 is_replacing_spare(nvlist_t
*search
, nvlist_t
*tgt
, int which
)
2614 if (nvlist_lookup_nvlist_array(search
, ZPOOL_CONFIG_CHILDREN
, &child
,
2616 verify(nvlist_lookup_string(search
, ZPOOL_CONFIG_TYPE
,
2619 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0 &&
2620 children
== 2 && child
[which
] == tgt
)
2623 for (c
= 0; c
< children
; c
++)
2624 if (is_replacing_spare(child
[c
], tgt
, which
))
2632 * Attach new_disk (fully described by nvroot) to old_disk.
2633 * If 'replacing' is specified, the new disk will replace the old one.
2636 zpool_vdev_attach(zpool_handle_t
*zhp
,
2637 const char *old_disk
, const char *new_disk
, nvlist_t
*nvroot
, int replacing
)
2639 zfs_cmd_t zc
= {"\0"};
2643 boolean_t avail_spare
, l2cache
, islog
;
2648 nvlist_t
*config_root
;
2649 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2650 boolean_t rootpool
= zpool_is_bootable(zhp
);
2653 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2654 "cannot replace %s with %s"), old_disk
, new_disk
);
2656 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2657 "cannot attach %s to %s"), new_disk
, old_disk
);
2659 #if defined(__sun__) || defined(__sun)
2661 * If this is a root pool, make sure that we're not attaching an
2662 * EFI labeled device.
2664 if (rootpool
&& pool_uses_efi(nvroot
)) {
2665 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2666 "EFI labeled devices are not supported on root pools."));
2667 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, msg
));
2671 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2672 if ((tgt
= zpool_find_vdev(zhp
, old_disk
, &avail_spare
, &l2cache
,
2674 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2677 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2680 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2682 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2683 zc
.zc_cookie
= replacing
;
2685 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
2686 &child
, &children
) != 0 || children
!= 1) {
2687 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2688 "new device must be a single disk"));
2689 return (zfs_error(hdl
, EZFS_INVALCONFIG
, msg
));
2692 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
2693 ZPOOL_CONFIG_VDEV_TREE
, &config_root
) == 0);
2695 if ((newname
= zpool_vdev_name(NULL
, NULL
, child
[0], 0)) == NULL
)
2699 * If the target is a hot spare that has been swapped in, we can only
2700 * replace it with another hot spare.
2703 nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_IS_SPARE
, &val
) == 0 &&
2704 (zpool_find_vdev(zhp
, newname
, &avail_spare
, &l2cache
,
2705 NULL
) == NULL
|| !avail_spare
) &&
2706 is_replacing_spare(config_root
, tgt
, 1)) {
2707 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2708 "can only be replaced by another hot spare"));
2710 return (zfs_error(hdl
, EZFS_BADTARGET
, msg
));
2715 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
2718 ret
= zfs_ioctl(hdl
, ZFS_IOC_VDEV_ATTACH
, &zc
);
2720 zcmd_free_nvlists(&zc
);
2725 * XXX need a better way to prevent user from
2726 * booting up a half-baked vdev.
2728 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Make "
2729 "sure to wait until resilver is done "
2730 "before rebooting.\n"));
2738 * Can't attach to or replace this type of vdev.
2741 uint64_t version
= zpool_get_prop_int(zhp
,
2742 ZPOOL_PROP_VERSION
, NULL
);
2745 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2746 "cannot replace a log with a spare"));
2747 else if (version
>= SPA_VERSION_MULTI_REPLACE
)
2748 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2749 "already in replacing/spare config; wait "
2750 "for completion or use 'zpool detach'"));
2752 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2753 "cannot replace a replacing device"));
2755 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2756 "can only attach to mirrors and top-level "
2759 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2764 * The new device must be a single disk.
2766 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2767 "new device must be a single disk"));
2768 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2772 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "%s is busy"),
2774 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2779 * The new device is too small.
2781 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2782 "device is too small"));
2783 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2788 * The new device has a different optimal sector size.
2790 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2791 "new device has a different optimal sector size; use the "
2792 "option '-o ashift=N' to override the optimal size"));
2793 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2798 * The resulting top-level vdev spec won't fit in the label.
2800 (void) zfs_error(hdl
, EZFS_DEVOVERFLOW
, msg
);
2804 (void) zpool_standard_error(hdl
, errno
, msg
);
2811 * Detach the specified device.
2814 zpool_vdev_detach(zpool_handle_t
*zhp
, const char *path
)
2816 zfs_cmd_t zc
= {"\0"};
2819 boolean_t avail_spare
, l2cache
;
2820 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2822 (void) snprintf(msg
, sizeof (msg
),
2823 dgettext(TEXT_DOMAIN
, "cannot detach %s"), path
);
2825 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2826 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2828 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2831 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2834 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2836 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2838 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_DETACH
, &zc
) == 0)
2845 * Can't detach from this type of vdev.
2847 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "only "
2848 "applicable to mirror and replacing vdevs"));
2849 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2854 * There are no other replicas of this device.
2856 (void) zfs_error(hdl
, EZFS_NOREPLICAS
, msg
);
2860 (void) zpool_standard_error(hdl
, errno
, msg
);
2867 * Find a mirror vdev in the source nvlist.
2869 * The mchild array contains a list of disks in one of the top-level mirrors
2870 * of the source pool. The schild array contains a list of disks that the
2871 * user specified on the command line. We loop over the mchild array to
2872 * see if any entry in the schild array matches.
2874 * If a disk in the mchild array is found in the schild array, we return
2875 * the index of that entry. Otherwise we return -1.
2878 find_vdev_entry(zpool_handle_t
*zhp
, nvlist_t
**mchild
, uint_t mchildren
,
2879 nvlist_t
**schild
, uint_t schildren
)
2883 for (mc
= 0; mc
< mchildren
; mc
++) {
2885 char *mpath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
2888 for (sc
= 0; sc
< schildren
; sc
++) {
2889 char *spath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
2891 boolean_t result
= (strcmp(mpath
, spath
) == 0);
2907 * Split a mirror pool. If newroot points to null, then a new nvlist
2908 * is generated and it is the responsibility of the caller to free it.
2911 zpool_vdev_split(zpool_handle_t
*zhp
, char *newname
, nvlist_t
**newroot
,
2912 nvlist_t
*props
, splitflags_t flags
)
2914 zfs_cmd_t zc
= {"\0"};
2916 nvlist_t
*tree
, *config
, **child
, **newchild
, *newconfig
= NULL
;
2917 nvlist_t
**varray
= NULL
, *zc_props
= NULL
;
2918 uint_t c
, children
, newchildren
, lastlog
= 0, vcount
, found
= 0;
2919 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2921 boolean_t freelist
= B_FALSE
, memory_err
= B_TRUE
;
2924 (void) snprintf(msg
, sizeof (msg
),
2925 dgettext(TEXT_DOMAIN
, "Unable to split %s"), zhp
->zpool_name
);
2927 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
2928 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
2930 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
2931 (void) fprintf(stderr
, gettext("Internal error: unable to "
2932 "retrieve pool configuration\n"));
2936 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &tree
)
2938 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
, &vers
) == 0);
2941 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
2942 if ((zc_props
= zpool_valid_proplist(hdl
, zhp
->zpool_name
,
2943 props
, vers
, flags
, msg
)) == NULL
)
2947 if (nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
2949 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2950 "Source pool is missing vdev tree"));
2952 nvlist_free(zc_props
);
2956 varray
= zfs_alloc(hdl
, children
* sizeof (nvlist_t
*));
2959 if (*newroot
== NULL
||
2960 nvlist_lookup_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
2961 &newchild
, &newchildren
) != 0)
2964 for (c
= 0; c
< children
; c
++) {
2965 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
2967 nvlist_t
**mchild
, *vdev
;
2972 * Unlike cache & spares, slogs are stored in the
2973 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2975 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2977 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
2979 if (is_log
|| is_hole
) {
2981 * Create a hole vdev and put it in the config.
2983 if (nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) != 0)
2985 if (nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
,
2986 VDEV_TYPE_HOLE
) != 0)
2988 if (nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_HOLE
,
2993 varray
[vcount
++] = vdev
;
2997 verify(nvlist_lookup_string(child
[c
], ZPOOL_CONFIG_TYPE
, &type
)
2999 if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
3000 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3001 "Source pool must be composed only of mirrors\n"));
3002 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3006 verify(nvlist_lookup_nvlist_array(child
[c
],
3007 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
3009 /* find or add an entry for this top-level vdev */
3010 if (newchildren
> 0 &&
3011 (entry
= find_vdev_entry(zhp
, mchild
, mchildren
,
3012 newchild
, newchildren
)) >= 0) {
3013 /* We found a disk that the user specified. */
3014 vdev
= mchild
[entry
];
3017 /* User didn't specify a disk for this vdev. */
3018 vdev
= mchild
[mchildren
- 1];
3021 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
3025 /* did we find every disk the user specified? */
3026 if (found
!= newchildren
) {
3027 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Device list must "
3028 "include at most one disk from each mirror"));
3029 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3033 /* Prepare the nvlist for populating. */
3034 if (*newroot
== NULL
) {
3035 if (nvlist_alloc(newroot
, NV_UNIQUE_NAME
, 0) != 0)
3038 if (nvlist_add_string(*newroot
, ZPOOL_CONFIG_TYPE
,
3039 VDEV_TYPE_ROOT
) != 0)
3042 verify(nvlist_remove_all(*newroot
, ZPOOL_CONFIG_CHILDREN
) == 0);
3045 /* Add all the children we found */
3046 if (nvlist_add_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
, varray
,
3047 lastlog
== 0 ? vcount
: lastlog
) != 0)
3051 * If we're just doing a dry run, exit now with success.
3054 memory_err
= B_FALSE
;
3059 /* now build up the config list & call the ioctl */
3060 if (nvlist_alloc(&newconfig
, NV_UNIQUE_NAME
, 0) != 0)
3063 if (nvlist_add_nvlist(newconfig
,
3064 ZPOOL_CONFIG_VDEV_TREE
, *newroot
) != 0 ||
3065 nvlist_add_string(newconfig
,
3066 ZPOOL_CONFIG_POOL_NAME
, newname
) != 0 ||
3067 nvlist_add_uint64(newconfig
, ZPOOL_CONFIG_VERSION
, vers
) != 0)
3071 * The new pool is automatically part of the namespace unless we
3072 * explicitly export it.
3075 zc
.zc_cookie
= ZPOOL_EXPORT_AFTER_SPLIT
;
3076 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3077 (void) strlcpy(zc
.zc_string
, newname
, sizeof (zc
.zc_string
));
3078 if (zcmd_write_conf_nvlist(hdl
, &zc
, newconfig
) != 0)
3080 if (zc_props
!= NULL
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
3083 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SPLIT
, &zc
) != 0) {
3084 retval
= zpool_standard_error(hdl
, errno
, msg
);
3089 memory_err
= B_FALSE
;
3092 if (varray
!= NULL
) {
3095 for (v
= 0; v
< vcount
; v
++)
3096 nvlist_free(varray
[v
]);
3099 zcmd_free_nvlists(&zc
);
3101 nvlist_free(zc_props
);
3103 nvlist_free(newconfig
);
3105 nvlist_free(*newroot
);
3113 return (no_memory(hdl
));
3119 * Remove the given device. Currently, this is supported only for hot spares
3120 * and level 2 cache devices.
3123 zpool_vdev_remove(zpool_handle_t
*zhp
, const char *path
)
3125 zfs_cmd_t zc
= {"\0"};
3128 boolean_t avail_spare
, l2cache
, islog
;
3129 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3132 (void) snprintf(msg
, sizeof (msg
),
3133 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3135 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3136 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3138 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3140 * XXX - this should just go away.
3142 if (!avail_spare
&& !l2cache
&& !islog
) {
3143 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3144 "only inactive hot spares, cache, top-level, "
3145 "or log devices can be removed"));
3146 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3149 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
3150 if (islog
&& version
< SPA_VERSION_HOLES
) {
3151 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3152 "pool must be upgrade to support log removal"));
3153 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
3156 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
3158 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3161 return (zpool_standard_error(hdl
, errno
, msg
));
3165 * Clear the errors for the pool, or the particular device if specified.
3168 zpool_clear(zpool_handle_t
*zhp
, const char *path
, nvlist_t
*rewindnvl
)
3170 zfs_cmd_t zc
= {"\0"};
3173 zpool_rewind_policy_t policy
;
3174 boolean_t avail_spare
, l2cache
;
3175 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3176 nvlist_t
*nvi
= NULL
;
3180 (void) snprintf(msg
, sizeof (msg
),
3181 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3184 (void) snprintf(msg
, sizeof (msg
),
3185 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3188 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3190 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
,
3191 &l2cache
, NULL
)) == 0)
3192 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3195 * Don't allow error clearing for hot spares. Do allow
3196 * error clearing for l2cache devices.
3199 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3201 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
,
3205 zpool_get_rewind_policy(rewindnvl
, &policy
);
3206 zc
.zc_cookie
= policy
.zrp_request
;
3208 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zhp
->zpool_config_size
* 2) != 0)
3211 if (zcmd_write_src_nvlist(hdl
, &zc
, rewindnvl
) != 0)
3214 while ((error
= zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
)) != 0 &&
3216 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
3217 zcmd_free_nvlists(&zc
);
3222 if (!error
|| ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) &&
3223 errno
!= EPERM
&& errno
!= EACCES
)) {
3224 if (policy
.zrp_request
&
3225 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
3226 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nvi
);
3227 zpool_rewind_exclaim(hdl
, zc
.zc_name
,
3228 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0),
3232 zcmd_free_nvlists(&zc
);
3236 zcmd_free_nvlists(&zc
);
3237 return (zpool_standard_error(hdl
, errno
, msg
));
3241 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3244 zpool_vdev_clear(zpool_handle_t
*zhp
, uint64_t guid
)
3246 zfs_cmd_t zc
= {"\0"};
3248 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3250 (void) snprintf(msg
, sizeof (msg
),
3251 dgettext(TEXT_DOMAIN
, "cannot clear errors for %llx"),
3252 (u_longlong_t
)guid
);
3254 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3256 zc
.zc_cookie
= ZPOOL_NO_REWIND
;
3258 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_CLEAR
, &zc
) == 0)
3261 return (zpool_standard_error(hdl
, errno
, msg
));
3265 * Change the GUID for a pool.
3268 zpool_reguid(zpool_handle_t
*zhp
)
3271 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3272 zfs_cmd_t zc
= {"\0"};
3274 (void) snprintf(msg
, sizeof (msg
),
3275 dgettext(TEXT_DOMAIN
, "cannot reguid '%s'"), zhp
->zpool_name
);
3277 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3278 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REGUID
, &zc
) == 0)
3281 return (zpool_standard_error(hdl
, errno
, msg
));
3288 zpool_reopen(zpool_handle_t
*zhp
)
3290 zfs_cmd_t zc
= {"\0"};
3292 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3294 (void) snprintf(msg
, sizeof (msg
),
3295 dgettext(TEXT_DOMAIN
, "cannot reopen '%s'"),
3298 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3299 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REOPEN
, &zc
) == 0)
3301 return (zpool_standard_error(hdl
, errno
, msg
));
3305 * Convert from a devid string to a path.
3308 devid_to_path(char *devid_str
)
3313 devid_nmlist_t
*list
= NULL
;
3316 if (devid_str_decode(devid_str
, &devid
, &minor
) != 0)
3319 ret
= devid_deviceid_to_nmlist("/dev", devid
, minor
, &list
);
3321 devid_str_free(minor
);
3327 if ((path
= strdup(list
[0].devname
)) == NULL
)
3330 devid_free_nmlist(list
);
3336 * Convert from a path to a devid string.
3339 path_to_devid(const char *path
)
3345 if ((fd
= open(path
, O_RDONLY
)) < 0)
3350 if (devid_get(fd
, &devid
) == 0) {
3351 if (devid_get_minor_name(fd
, &minor
) == 0)
3352 ret
= devid_str_encode(devid
, minor
);
3354 devid_str_free(minor
);
3363 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3364 * ignore any failure here, since a common case is for an unprivileged user to
3365 * type 'zpool status', and we'll display the correct information anyway.
3368 set_path(zpool_handle_t
*zhp
, nvlist_t
*nv
, const char *path
)
3370 zfs_cmd_t zc
= {"\0"};
3372 (void) strncpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3373 (void) strncpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
3374 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
3377 (void) ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_VDEV_SETPATH
, &zc
);
3381 * Remove partition suffix from a vdev path. Partition suffixes may take three
3382 * forms: "-partX", "pX", or "X", where X is a string of digits. The second
3383 * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
3384 * third case only occurs when preceded by a string matching the regular
3385 * expression "^([hsv]|xv)d[a-z]+", i.e. a scsi, ide, virtio or xen disk.
3388 strip_partition(libzfs_handle_t
*hdl
, char *path
)
3390 char *tmp
= zfs_strdup(hdl
, path
);
3391 char *part
= NULL
, *d
= NULL
;
3393 if ((part
= strstr(tmp
, "-part")) && part
!= tmp
) {
3395 } else if ((part
= strrchr(tmp
, 'p')) &&
3396 part
> tmp
+ 1 && isdigit(*(part
-1))) {
3398 } else if ((tmp
[0] == 'h' || tmp
[0] == 's' || tmp
[0] == 'v') &&
3400 for (d
= &tmp
[2]; isalpha(*d
); part
= ++d
);
3401 } else if (strncmp("xvd", tmp
, 3) == 0) {
3402 for (d
= &tmp
[3]; isalpha(*d
); part
= ++d
);
3404 if (part
&& d
&& *d
!= '\0') {
3405 for (; isdigit(*d
); d
++);
3412 #define PATH_BUF_LEN 64
3415 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3416 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3417 * We also check if this is a whole disk, in which case we strip off the
3418 * trailing 's0' slice name.
3420 * This routine is also responsible for identifying when disks have been
3421 * reconfigured in a new location. The kernel will have opened the device by
3422 * devid, but the path will still refer to the old location. To catch this, we
3423 * first do a path -> devid translation (which is fast for the common case). If
3424 * the devid matches, we're done. If not, we do a reverse devid -> path
3425 * translation and issue the appropriate ioctl() to update the path of the vdev.
3426 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3430 zpool_vdev_name(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, nvlist_t
*nv
,
3433 char *path
, *devid
, *type
, *env
;
3435 char buf
[PATH_BUF_LEN
];
3436 char tmpbuf
[PATH_BUF_LEN
];
3440 env
= getenv("ZPOOL_VDEV_NAME_PATH");
3441 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3442 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3443 name_flags
|= VDEV_NAME_PATH
;
3445 env
= getenv("ZPOOL_VDEV_NAME_GUID");
3446 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3447 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3448 name_flags
|= VDEV_NAME_GUID
;
3450 env
= getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
3451 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3452 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3453 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
3455 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
, &value
) == 0 ||
3456 name_flags
& VDEV_NAME_GUID
) {
3457 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
);
3458 (void) snprintf(buf
, sizeof (buf
), "%llu", (u_longlong_t
)value
);
3460 } else if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
3462 * If the device is dead (faulted, offline, etc) then don't
3463 * bother opening it. Otherwise we may be forcing the user to
3464 * open a misbehaving device, which can have undesirable
3467 if ((nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
3468 (uint64_t **)&vs
, &vsc
) != 0 ||
3469 vs
->vs_state
>= VDEV_STATE_DEGRADED
) &&
3471 nvlist_lookup_string(nv
, ZPOOL_CONFIG_DEVID
, &devid
) == 0) {
3473 * Determine if the current path is correct.
3475 char *newdevid
= path_to_devid(path
);
3477 if (newdevid
== NULL
||
3478 strcmp(devid
, newdevid
) != 0) {
3481 if ((newpath
= devid_to_path(devid
)) != NULL
) {
3483 * Update the path appropriately.
3485 set_path(zhp
, nv
, newpath
);
3486 if (nvlist_add_string(nv
,
3487 ZPOOL_CONFIG_PATH
, newpath
) == 0)
3488 verify(nvlist_lookup_string(nv
,
3496 devid_str_free(newdevid
);
3499 if (name_flags
& VDEV_NAME_FOLLOW_LINKS
) {
3500 char *rp
= realpath(path
, NULL
);
3502 strlcpy(buf
, rp
, sizeof (buf
));
3509 * For a block device only use the name.
3511 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
3512 if ((strcmp(type
, VDEV_TYPE_DISK
) == 0) &&
3513 !(name_flags
& VDEV_NAME_PATH
)) {
3514 path
= strrchr(path
, '/');
3519 * Remove the partition from the path it this is a whole disk.
3521 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
, &value
)
3522 == 0 && value
&& !(name_flags
& VDEV_NAME_PATH
)) {
3523 return (strip_partition(hdl
, path
));
3526 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &path
) == 0);
3529 * If it's a raidz device, we need to stick in the parity level.
3531 if (strcmp(path
, VDEV_TYPE_RAIDZ
) == 0) {
3533 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
,
3535 (void) snprintf(buf
, sizeof (buf
), "%s%llu", path
,
3536 (u_longlong_t
)value
);
3541 * We identify each top-level vdev by using a <type-id>
3542 * naming convention.
3544 if (name_flags
& VDEV_NAME_TYPE_ID
) {
3547 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
3549 (void) snprintf(tmpbuf
, sizeof (tmpbuf
), "%s-%llu",
3550 path
, (u_longlong_t
)id
);
3555 return (zfs_strdup(hdl
, path
));
3559 zbookmark_compare(const void *a
, const void *b
)
3561 return (memcmp(a
, b
, sizeof (zbookmark_phys_t
)));
3565 * Retrieve the persistent error log, uniquify the members, and return to the
3569 zpool_get_errlog(zpool_handle_t
*zhp
, nvlist_t
**nverrlistp
)
3571 zfs_cmd_t zc
= {"\0"};
3573 zbookmark_phys_t
*zb
= NULL
;
3577 * Retrieve the raw error list from the kernel. If the number of errors
3578 * has increased, allocate more space and continue until we get the
3581 verify(nvlist_lookup_uint64(zhp
->zpool_config
, ZPOOL_CONFIG_ERRCOUNT
,
3585 if ((zc
.zc_nvlist_dst
= (uintptr_t)zfs_alloc(zhp
->zpool_hdl
,
3586 count
* sizeof (zbookmark_phys_t
))) == (uintptr_t)NULL
)
3588 zc
.zc_nvlist_dst_size
= count
;
3589 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3591 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_ERROR_LOG
,
3593 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3594 if (errno
== ENOMEM
) {
3597 count
= zc
.zc_nvlist_dst_size
;
3598 dst
= zfs_alloc(zhp
->zpool_hdl
, count
*
3599 sizeof (zbookmark_phys_t
));
3602 zc
.zc_nvlist_dst
= (uintptr_t)dst
;
3612 * Sort the resulting bookmarks. This is a little confusing due to the
3613 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3614 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3615 * _not_ copied as part of the process. So we point the start of our
3616 * array appropriate and decrement the total number of elements.
3618 zb
= ((zbookmark_phys_t
*)(uintptr_t)zc
.zc_nvlist_dst
) +
3619 zc
.zc_nvlist_dst_size
;
3620 count
-= zc
.zc_nvlist_dst_size
;
3622 qsort(zb
, count
, sizeof (zbookmark_phys_t
), zbookmark_compare
);
3624 verify(nvlist_alloc(nverrlistp
, 0, KM_SLEEP
) == 0);
3627 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3629 for (i
= 0; i
< count
; i
++) {
3632 /* ignoring zb_blkid and zb_level for now */
3633 if (i
> 0 && zb
[i
-1].zb_objset
== zb
[i
].zb_objset
&&
3634 zb
[i
-1].zb_object
== zb
[i
].zb_object
)
3637 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) != 0)
3639 if (nvlist_add_uint64(nv
, ZPOOL_ERR_DATASET
,
3640 zb
[i
].zb_objset
) != 0) {
3644 if (nvlist_add_uint64(nv
, ZPOOL_ERR_OBJECT
,
3645 zb
[i
].zb_object
) != 0) {
3649 if (nvlist_add_nvlist(*nverrlistp
, "ejk", nv
) != 0) {
3656 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3660 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3661 return (no_memory(zhp
->zpool_hdl
));
3665 * Upgrade a ZFS pool to the latest on-disk version.
3668 zpool_upgrade(zpool_handle_t
*zhp
, uint64_t new_version
)
3670 zfs_cmd_t zc
= {"\0"};
3671 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3673 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3674 zc
.zc_cookie
= new_version
;
3676 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_UPGRADE
, &zc
) != 0)
3677 return (zpool_standard_error_fmt(hdl
, errno
,
3678 dgettext(TEXT_DOMAIN
, "cannot upgrade '%s'"),
3684 zfs_save_arguments(int argc
, char **argv
, char *string
, int len
)
3688 (void) strlcpy(string
, basename(argv
[0]), len
);
3689 for (i
= 1; i
< argc
; i
++) {
3690 (void) strlcat(string
, " ", len
);
3691 (void) strlcat(string
, argv
[i
], len
);
3696 zpool_log_history(libzfs_handle_t
*hdl
, const char *message
)
3698 zfs_cmd_t zc
= {"\0"};
3702 args
= fnvlist_alloc();
3703 fnvlist_add_string(args
, "message", message
);
3704 err
= zcmd_write_src_nvlist(hdl
, &zc
, args
);
3706 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_LOG_HISTORY
, &zc
);
3708 zcmd_free_nvlists(&zc
);
3713 * Perform ioctl to get some command history of a pool.
3715 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3716 * logical offset of the history buffer to start reading from.
3718 * Upon return, 'off' is the next logical offset to read from and
3719 * 'len' is the actual amount of bytes read into 'buf'.
3722 get_history(zpool_handle_t
*zhp
, char *buf
, uint64_t *off
, uint64_t *len
)
3724 zfs_cmd_t zc
= {"\0"};
3725 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3727 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3729 zc
.zc_history
= (uint64_t)(uintptr_t)buf
;
3730 zc
.zc_history_len
= *len
;
3731 zc
.zc_history_offset
= *off
;
3733 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_HISTORY
, &zc
) != 0) {
3736 return (zfs_error_fmt(hdl
, EZFS_PERM
,
3737 dgettext(TEXT_DOMAIN
,
3738 "cannot show history for pool '%s'"),
3741 return (zfs_error_fmt(hdl
, EZFS_NOHISTORY
,
3742 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
3743 "'%s'"), zhp
->zpool_name
));
3745 return (zfs_error_fmt(hdl
, EZFS_BADVERSION
,
3746 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
3747 "'%s', pool must be upgraded"), zhp
->zpool_name
));
3749 return (zpool_standard_error_fmt(hdl
, errno
,
3750 dgettext(TEXT_DOMAIN
,
3751 "cannot get history for '%s'"), zhp
->zpool_name
));
3755 *len
= zc
.zc_history_len
;
3756 *off
= zc
.zc_history_offset
;
3762 * Process the buffer of nvlists, unpacking and storing each nvlist record
3763 * into 'records'. 'leftover' is set to the number of bytes that weren't
3764 * processed as there wasn't a complete record.
3767 zpool_history_unpack(char *buf
, uint64_t bytes_read
, uint64_t *leftover
,
3768 nvlist_t
***records
, uint_t
*numrecords
)
3774 while (bytes_read
> sizeof (reclen
)) {
3776 /* get length of packed record (stored as little endian) */
3777 for (i
= 0, reclen
= 0; i
< sizeof (reclen
); i
++)
3778 reclen
+= (uint64_t)(((uchar_t
*)buf
)[i
]) << (8*i
);
3780 if (bytes_read
< sizeof (reclen
) + reclen
)
3784 if (nvlist_unpack(buf
+ sizeof (reclen
), reclen
, &nv
, 0) != 0)
3786 bytes_read
-= sizeof (reclen
) + reclen
;
3787 buf
+= sizeof (reclen
) + reclen
;
3789 /* add record to nvlist array */
3791 if (ISP2(*numrecords
+ 1)) {
3792 *records
= realloc(*records
,
3793 *numrecords
* 2 * sizeof (nvlist_t
*));
3795 (*records
)[*numrecords
- 1] = nv
;
3798 *leftover
= bytes_read
;
3803 * Retrieve the command history of a pool.
3806 zpool_get_history(zpool_handle_t
*zhp
, nvlist_t
**nvhisp
)
3809 int buflen
= 128 * 1024;
3811 nvlist_t
**records
= NULL
;
3812 uint_t numrecords
= 0;
3815 buf
= malloc(buflen
);
3819 uint64_t bytes_read
= buflen
;
3822 if ((err
= get_history(zhp
, buf
, &off
, &bytes_read
)) != 0)
3825 /* if nothing else was read in, we're at EOF, just return */
3829 if ((err
= zpool_history_unpack(buf
, bytes_read
,
3830 &leftover
, &records
, &numrecords
)) != 0)
3833 if (leftover
== bytes_read
) {
3835 * no progress made, because buffer is not big enough
3836 * to hold this record; resize and retry.
3840 buf
= malloc(buflen
);
3851 verify(nvlist_alloc(nvhisp
, NV_UNIQUE_NAME
, 0) == 0);
3852 verify(nvlist_add_nvlist_array(*nvhisp
, ZPOOL_HIST_RECORD
,
3853 records
, numrecords
) == 0);
3855 for (i
= 0; i
< numrecords
; i
++)
3856 nvlist_free(records
[i
]);
3863 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
3864 * If there is a new event available 'nvp' will contain a newly allocated
3865 * nvlist and 'dropped' will be set to the number of missed events since
3866 * the last call to this function. When 'nvp' is set to NULL it indicates
3867 * no new events are available. In either case the function returns 0 and
3868 * it is up to the caller to free 'nvp'. In the case of a fatal error the
3869 * function will return a non-zero value. When the function is called in
3870 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
3871 * it will not return until a new event is available.
3874 zpool_events_next(libzfs_handle_t
*hdl
, nvlist_t
**nvp
,
3875 int *dropped
, unsigned flags
, int zevent_fd
)
3877 zfs_cmd_t zc
= {"\0"};
3882 zc
.zc_cleanup_fd
= zevent_fd
;
3884 if (flags
& ZEVENT_NONBLOCK
)
3885 zc
.zc_guid
= ZEVENT_NONBLOCK
;
3887 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, ZEVENT_SIZE
) != 0)
3891 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_NEXT
, &zc
) != 0) {
3894 error
= zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
3895 dgettext(TEXT_DOMAIN
, "zfs shutdown"));
3898 /* Blocking error case should not occur */
3899 if (!(flags
& ZEVENT_NONBLOCK
))
3900 error
= zpool_standard_error_fmt(hdl
, errno
,
3901 dgettext(TEXT_DOMAIN
, "cannot get event"));
3905 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
3906 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
3907 dgettext(TEXT_DOMAIN
, "cannot get event"));
3913 error
= zpool_standard_error_fmt(hdl
, errno
,
3914 dgettext(TEXT_DOMAIN
, "cannot get event"));
3919 error
= zcmd_read_dst_nvlist(hdl
, &zc
, nvp
);
3923 *dropped
= (int)zc
.zc_cookie
;
3925 zcmd_free_nvlists(&zc
);
3934 zpool_events_clear(libzfs_handle_t
*hdl
, int *count
)
3936 zfs_cmd_t zc
= {"\0"};
3939 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
3940 "cannot clear events"));
3942 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_CLEAR
, &zc
) != 0)
3943 return (zpool_standard_error_fmt(hdl
, errno
, msg
));
3946 *count
= (int)zc
.zc_cookie
; /* # of events cleared */
3952 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
3953 * the passed zevent_fd file handle. On success zero is returned,
3954 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
3957 zpool_events_seek(libzfs_handle_t
*hdl
, uint64_t eid
, int zevent_fd
)
3959 zfs_cmd_t zc
= {"\0"};
3963 zc
.zc_cleanup_fd
= zevent_fd
;
3965 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_SEEK
, &zc
) != 0) {
3968 error
= zfs_error_fmt(hdl
, EZFS_NOENT
,
3969 dgettext(TEXT_DOMAIN
, "cannot get event"));
3973 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
3974 dgettext(TEXT_DOMAIN
, "cannot get event"));
3978 error
= zpool_standard_error_fmt(hdl
, errno
,
3979 dgettext(TEXT_DOMAIN
, "cannot get event"));
3988 zpool_obj_to_path(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
3989 char *pathname
, size_t len
)
3991 zfs_cmd_t zc
= {"\0"};
3992 boolean_t mounted
= B_FALSE
;
3993 char *mntpnt
= NULL
;
3994 char dsname
[MAXNAMELEN
];
3997 /* special case for the MOS */
3998 (void) snprintf(pathname
, len
, "<metadata>:<0x%llx>",
4003 /* get the dataset's name */
4004 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4006 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
,
4007 ZFS_IOC_DSOBJ_TO_DSNAME
, &zc
) != 0) {
4008 /* just write out a path of two object numbers */
4009 (void) snprintf(pathname
, len
, "<0x%llx>:<0x%llx>",
4010 (longlong_t
)dsobj
, (longlong_t
)obj
);
4013 (void) strlcpy(dsname
, zc
.zc_value
, sizeof (dsname
));
4015 /* find out if the dataset is mounted */
4016 mounted
= is_mounted(zhp
->zpool_hdl
, dsname
, &mntpnt
);
4018 /* get the corrupted object's path */
4019 (void) strlcpy(zc
.zc_name
, dsname
, sizeof (zc
.zc_name
));
4021 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_OBJ_TO_PATH
,
4024 (void) snprintf(pathname
, len
, "%s%s", mntpnt
,
4027 (void) snprintf(pathname
, len
, "%s:%s",
4028 dsname
, zc
.zc_value
);
4031 (void) snprintf(pathname
, len
, "%s:<0x%llx>", dsname
,
4038 * Read the EFI label from the config, if a label does not exist then
4039 * pass back the error to the caller. If the caller has passed a non-NULL
4040 * diskaddr argument then we set it to the starting address of the EFI
4044 read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
)
4048 char diskname
[MAXPATHLEN
];
4051 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PATH
, &path
) != 0)
4054 (void) snprintf(diskname
, sizeof (diskname
), "%s%s", DISK_ROOT
,
4055 strrchr(path
, '/'));
4056 if ((fd
= open(diskname
, O_RDWR
|O_DIRECT
)) >= 0) {
4057 struct dk_gpt
*vtoc
;
4059 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) >= 0) {
4061 *sb
= vtoc
->efi_parts
[0].p_start
;
4070 * determine where a partition starts on a disk in the current
4074 find_start_block(nvlist_t
*config
)
4078 diskaddr_t sb
= MAXOFFSET_T
;
4081 if (nvlist_lookup_nvlist_array(config
,
4082 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) != 0) {
4083 if (nvlist_lookup_uint64(config
,
4084 ZPOOL_CONFIG_WHOLE_DISK
,
4085 &wholedisk
) != 0 || !wholedisk
) {
4086 return (MAXOFFSET_T
);
4088 if (read_efi_label(config
, &sb
) < 0)
4093 for (c
= 0; c
< children
; c
++) {
4094 sb
= find_start_block(child
[c
]);
4095 if (sb
!= MAXOFFSET_T
) {
4099 return (MAXOFFSET_T
);
4103 zpool_label_disk_check(char *path
)
4105 struct dk_gpt
*vtoc
;
4108 if ((fd
= open(path
, O_RDWR
|O_DIRECT
)) < 0)
4111 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) != 0) {
4116 if (vtoc
->efi_flags
& EFI_GPT_PRIMARY_CORRUPT
) {
4128 * Generate a unique partition name for the ZFS member. Partitions must
4129 * have unique names to ensure udev will be able to create symlinks under
4130 * /dev/disk/by-partlabel/ for all pool members. The partition names are
4131 * of the form <pool>-<unique-id>.
4134 zpool_label_name(char *label_name
, int label_size
)
4139 fd
= open("/dev/urandom", O_RDONLY
);
4141 if (read(fd
, &id
, sizeof (id
)) != sizeof (id
))
4148 id
= (((uint64_t)rand()) << 32) | (uint64_t)rand();
4150 snprintf(label_name
, label_size
, "zfs-%016llx", (u_longlong_t
) id
);
4154 * Label an individual disk. The name provided is the short name,
4155 * stripped of any leading /dev path.
4158 zpool_label_disk(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, char *name
)
4160 char path
[MAXPATHLEN
];
4161 struct dk_gpt
*vtoc
;
4163 size_t resv
= EFI_MIN_RESV_SIZE
;
4164 uint64_t slice_size
;
4165 diskaddr_t start_block
;
4168 /* prepare an error message just in case */
4169 (void) snprintf(errbuf
, sizeof (errbuf
),
4170 dgettext(TEXT_DOMAIN
, "cannot label '%s'"), name
);
4175 #if defined(__sun__) || defined(__sun)
4176 if (zpool_is_bootable(zhp
)) {
4177 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4178 "EFI labeled devices are not supported on root "
4180 return (zfs_error(hdl
, EZFS_POOL_NOTSUP
, errbuf
));
4184 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
4185 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
4187 if (zhp
->zpool_start_block
== 0)
4188 start_block
= find_start_block(nvroot
);
4190 start_block
= zhp
->zpool_start_block
;
4191 zhp
->zpool_start_block
= start_block
;
4194 start_block
= NEW_START_BLOCK
;
4197 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4199 if ((fd
= open(path
, O_RDWR
|O_DIRECT
)) < 0) {
4201 * This shouldn't happen. We've long since verified that this
4202 * is a valid device.
4204 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
4205 "label '%s': unable to open device: %d"), path
, errno
);
4206 return (zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
));
4209 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, &vtoc
) != 0) {
4211 * The only way this can fail is if we run out of memory, or we
4212 * were unable to read the disk's capacity
4214 if (errno
== ENOMEM
)
4215 (void) no_memory(hdl
);
4218 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
4219 "label '%s': unable to read disk capacity"), path
);
4221 return (zfs_error(hdl
, EZFS_NOCAP
, errbuf
));
4224 slice_size
= vtoc
->efi_last_u_lba
+ 1;
4225 slice_size
-= EFI_MIN_RESV_SIZE
;
4226 if (start_block
== MAXOFFSET_T
)
4227 start_block
= NEW_START_BLOCK
;
4228 slice_size
-= start_block
;
4229 slice_size
= P2ALIGN(slice_size
, PARTITION_END_ALIGNMENT
);
4231 vtoc
->efi_parts
[0].p_start
= start_block
;
4232 vtoc
->efi_parts
[0].p_size
= slice_size
;
4235 * Why we use V_USR: V_BACKUP confuses users, and is considered
4236 * disposable by some EFI utilities (since EFI doesn't have a backup
4237 * slice). V_UNASSIGNED is supposed to be used only for zero size
4238 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4239 * etc. were all pretty specific. V_USR is as close to reality as we
4240 * can get, in the absence of V_OTHER.
4242 vtoc
->efi_parts
[0].p_tag
= V_USR
;
4243 zpool_label_name(vtoc
->efi_parts
[0].p_name
, EFI_PART_NAME_LEN
);
4245 vtoc
->efi_parts
[8].p_start
= slice_size
+ start_block
;
4246 vtoc
->efi_parts
[8].p_size
= resv
;
4247 vtoc
->efi_parts
[8].p_tag
= V_RESERVED
;
4249 if ((rval
= efi_write(fd
, vtoc
)) != 0 || (rval
= efi_rescan(fd
)) != 0) {
4251 * Some block drivers (like pcata) may not support EFI
4252 * GPT labels. Print out a helpful error message dir-
4253 * ecting the user to manually label the disk and give
4259 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "try using "
4260 "parted(8) and then provide a specific slice: %d"), rval
);
4261 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4267 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4268 (void) zfs_append_partition(path
, MAXPATHLEN
);
4270 /* Wait to udev to signal use the device has settled. */
4271 rval
= zpool_label_disk_wait(path
, DISK_LABEL_WAIT
);
4273 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "failed to "
4274 "detect device partitions on '%s': %d"), path
, rval
);
4275 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4278 /* We can't be to paranoid. Read the label back and verify it. */
4279 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4280 rval
= zpool_label_disk_check(path
);
4282 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "freshly written "
4283 "EFI label on '%s' is damaged. Ensure\nthis device "
4284 "is not in in use, and is functioning properly: %d"),
4286 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));