4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
40 #include <sys/efi_partition.h>
42 #include <sys/zfs_ioctl.h>
45 #include "zfs_namecheck.h"
47 #include "libzfs_impl.h"
48 #include "zfs_comutil.h"
49 #include "zfeature_common.h"
51 static int read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
);
53 typedef struct prop_flags
{
54 int create
:1; /* Validate property on creation */
55 int import
:1; /* Validate property on import */
59 * ====================================================================
60 * zpool property functions
61 * ====================================================================
65 zpool_get_all_props(zpool_handle_t
*zhp
)
67 zfs_cmd_t zc
= {"\0"};
68 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
70 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
72 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
75 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_PROPS
, &zc
) != 0) {
76 if (errno
== ENOMEM
) {
77 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
78 zcmd_free_nvlists(&zc
);
82 zcmd_free_nvlists(&zc
);
87 if (zcmd_read_dst_nvlist(hdl
, &zc
, &zhp
->zpool_props
) != 0) {
88 zcmd_free_nvlists(&zc
);
92 zcmd_free_nvlists(&zc
);
98 zpool_props_refresh(zpool_handle_t
*zhp
)
102 old_props
= zhp
->zpool_props
;
104 if (zpool_get_all_props(zhp
) != 0)
107 nvlist_free(old_props
);
112 zpool_get_prop_string(zpool_handle_t
*zhp
, zpool_prop_t prop
,
118 zprop_source_t source
;
120 nvl
= zhp
->zpool_props
;
121 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
122 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &ival
) == 0);
124 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
126 source
= ZPROP_SRC_DEFAULT
;
127 if ((value
= (char *)zpool_prop_default_string(prop
)) == NULL
)
138 zpool_get_prop_int(zpool_handle_t
*zhp
, zpool_prop_t prop
, zprop_source_t
*src
)
142 zprop_source_t source
;
144 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
)) {
146 * zpool_get_all_props() has most likely failed because
147 * the pool is faulted, but if all we need is the top level
148 * vdev's guid then get it from the zhp config nvlist.
150 if ((prop
== ZPOOL_PROP_GUID
) &&
151 (nvlist_lookup_nvlist(zhp
->zpool_config
,
152 ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0) &&
153 (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
)
157 return (zpool_prop_default_numeric(prop
));
160 nvl
= zhp
->zpool_props
;
161 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
162 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &value
) == 0);
164 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
166 source
= ZPROP_SRC_DEFAULT
;
167 value
= zpool_prop_default_numeric(prop
);
177 * Map VDEV STATE to printed strings.
180 zpool_state_to_name(vdev_state_t state
, vdev_aux_t aux
)
185 case VDEV_STATE_CLOSED
:
186 case VDEV_STATE_OFFLINE
:
187 return (gettext("OFFLINE"));
188 case VDEV_STATE_REMOVED
:
189 return (gettext("REMOVED"));
190 case VDEV_STATE_CANT_OPEN
:
191 if (aux
== VDEV_AUX_CORRUPT_DATA
|| aux
== VDEV_AUX_BAD_LOG
)
192 return (gettext("FAULTED"));
193 else if (aux
== VDEV_AUX_SPLIT_POOL
)
194 return (gettext("SPLIT"));
196 return (gettext("UNAVAIL"));
197 case VDEV_STATE_FAULTED
:
198 return (gettext("FAULTED"));
199 case VDEV_STATE_DEGRADED
:
200 return (gettext("DEGRADED"));
201 case VDEV_STATE_HEALTHY
:
202 return (gettext("ONLINE"));
205 return (gettext("UNKNOWN"));
209 * Map POOL STATE to printed strings.
212 zpool_pool_state_to_name(pool_state_t state
)
217 case POOL_STATE_ACTIVE
:
218 return (gettext("ACTIVE"));
219 case POOL_STATE_EXPORTED
:
220 return (gettext("EXPORTED"));
221 case POOL_STATE_DESTROYED
:
222 return (gettext("DESTROYED"));
223 case POOL_STATE_SPARE
:
224 return (gettext("SPARE"));
225 case POOL_STATE_L2CACHE
:
226 return (gettext("L2CACHE"));
227 case POOL_STATE_UNINITIALIZED
:
228 return (gettext("UNINITIALIZED"));
229 case POOL_STATE_UNAVAIL
:
230 return (gettext("UNAVAIL"));
231 case POOL_STATE_POTENTIALLY_ACTIVE
:
232 return (gettext("POTENTIALLY_ACTIVE"));
235 return (gettext("UNKNOWN"));
239 * Get a zpool property value for 'prop' and return the value in
240 * a pre-allocated buffer.
243 zpool_get_prop(zpool_handle_t
*zhp
, zpool_prop_t prop
, char *buf
,
244 size_t len
, zprop_source_t
*srctype
, boolean_t literal
)
248 zprop_source_t src
= ZPROP_SRC_NONE
;
253 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
255 case ZPOOL_PROP_NAME
:
256 (void) strlcpy(buf
, zpool_get_name(zhp
), len
);
259 case ZPOOL_PROP_HEALTH
:
260 (void) strlcpy(buf
, "FAULTED", len
);
263 case ZPOOL_PROP_GUID
:
264 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
265 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
268 case ZPOOL_PROP_ALTROOT
:
269 case ZPOOL_PROP_CACHEFILE
:
270 case ZPOOL_PROP_COMMENT
:
271 if (zhp
->zpool_props
!= NULL
||
272 zpool_get_all_props(zhp
) == 0) {
274 zpool_get_prop_string(zhp
, prop
, &src
),
280 (void) strlcpy(buf
, "-", len
);
289 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
) &&
290 prop
!= ZPOOL_PROP_NAME
)
293 switch (zpool_prop_get_type(prop
)) {
294 case PROP_TYPE_STRING
:
295 (void) strlcpy(buf
, zpool_get_prop_string(zhp
, prop
, &src
),
299 case PROP_TYPE_NUMBER
:
300 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
303 case ZPOOL_PROP_SIZE
:
304 case ZPOOL_PROP_ALLOCATED
:
305 case ZPOOL_PROP_FREE
:
306 case ZPOOL_PROP_FREEING
:
307 case ZPOOL_PROP_LEAKED
:
308 case ZPOOL_PROP_ASHIFT
:
310 (void) snprintf(buf
, len
, "%llu",
311 (u_longlong_t
)intval
);
313 (void) zfs_nicenum(intval
, buf
, len
);
316 case ZPOOL_PROP_EXPANDSZ
:
318 (void) strlcpy(buf
, "-", len
);
319 } else if (literal
) {
320 (void) snprintf(buf
, len
, "%llu",
321 (u_longlong_t
)intval
);
323 (void) zfs_nicenum(intval
, buf
, len
);
327 case ZPOOL_PROP_CAPACITY
:
329 (void) snprintf(buf
, len
, "%llu",
330 (u_longlong_t
)intval
);
332 (void) snprintf(buf
, len
, "%llu%%",
333 (u_longlong_t
)intval
);
337 case ZPOOL_PROP_FRAGMENTATION
:
338 if (intval
== UINT64_MAX
) {
339 (void) strlcpy(buf
, "-", len
);
340 } else if (literal
) {
341 (void) snprintf(buf
, len
, "%llu",
342 (u_longlong_t
)intval
);
344 (void) snprintf(buf
, len
, "%llu%%",
345 (u_longlong_t
)intval
);
349 case ZPOOL_PROP_DEDUPRATIO
:
351 (void) snprintf(buf
, len
, "%llu.%02llu",
352 (u_longlong_t
)(intval
/ 100),
353 (u_longlong_t
)(intval
% 100));
355 (void) snprintf(buf
, len
, "%llu.%02llux",
356 (u_longlong_t
)(intval
/ 100),
357 (u_longlong_t
)(intval
% 100));
360 case ZPOOL_PROP_HEALTH
:
361 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
362 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
363 verify(nvlist_lookup_uint64_array(nvroot
,
364 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
367 (void) strlcpy(buf
, zpool_state_to_name(intval
,
370 case ZPOOL_PROP_VERSION
:
371 if (intval
>= SPA_VERSION_FEATURES
) {
372 (void) snprintf(buf
, len
, "-");
377 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
381 case PROP_TYPE_INDEX
:
382 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
383 if (zpool_prop_index_to_string(prop
, intval
, &strval
)
386 (void) strlcpy(buf
, strval
, len
);
400 * Check if the bootfs name has the same pool name as it is set to.
401 * Assuming bootfs is a valid dataset name.
404 bootfs_name_valid(const char *pool
, char *bootfs
)
406 int len
= strlen(pool
);
408 if (!zfs_name_valid(bootfs
, ZFS_TYPE_FILESYSTEM
|ZFS_TYPE_SNAPSHOT
))
411 if (strncmp(pool
, bootfs
, len
) == 0 &&
412 (bootfs
[len
] == '/' || bootfs
[len
] == '\0'))
419 zpool_is_bootable(zpool_handle_t
*zhp
)
421 char bootfs
[ZFS_MAX_DATASET_NAME_LEN
];
423 return (zpool_get_prop(zhp
, ZPOOL_PROP_BOOTFS
, bootfs
,
424 sizeof (bootfs
), NULL
, B_FALSE
) == 0 && strncmp(bootfs
, "-",
425 sizeof (bootfs
)) != 0);
430 * Given an nvlist of zpool properties to be set, validate that they are
431 * correct, and parse any numeric properties (index, boolean, etc) if they are
432 * specified as strings.
435 zpool_valid_proplist(libzfs_handle_t
*hdl
, const char *poolname
,
436 nvlist_t
*props
, uint64_t version
, prop_flags_t flags
, char *errbuf
)
444 struct stat64 statbuf
;
447 if (nvlist_alloc(&retprops
, NV_UNIQUE_NAME
, 0) != 0) {
448 (void) no_memory(hdl
);
453 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
454 const char *propname
= nvpair_name(elem
);
456 prop
= zpool_name_to_prop(propname
);
457 if (prop
== ZPROP_INVAL
&& zpool_prop_feature(propname
)) {
459 char *fname
= strchr(propname
, '@') + 1;
461 err
= zfeature_lookup_name(fname
, NULL
);
463 ASSERT3U(err
, ==, ENOENT
);
464 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
465 "invalid feature '%s'"), fname
);
466 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
470 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
471 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
472 "'%s' must be a string"), propname
);
473 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
477 (void) nvpair_value_string(elem
, &strval
);
478 if (strcmp(strval
, ZFS_FEATURE_ENABLED
) != 0 &&
479 strcmp(strval
, ZFS_FEATURE_DISABLED
) != 0) {
480 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
481 "property '%s' can only be set to "
482 "'enabled' or 'disabled'"), propname
);
483 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
487 if (nvlist_add_uint64(retprops
, propname
, 0) != 0) {
488 (void) no_memory(hdl
);
495 * Make sure this property is valid and applies to this type.
497 if (prop
== ZPROP_INVAL
) {
498 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
499 "invalid property '%s'"), propname
);
500 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
504 if (zpool_prop_readonly(prop
)) {
505 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
506 "is readonly"), propname
);
507 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
511 if (zprop_parse_value(hdl
, elem
, prop
, ZFS_TYPE_POOL
, retprops
,
512 &strval
, &intval
, errbuf
) != 0)
516 * Perform additional checking for specific properties.
521 case ZPOOL_PROP_VERSION
:
522 if (intval
< version
||
523 !SPA_VERSION_IS_SUPPORTED(intval
)) {
524 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
525 "property '%s' number %d is invalid."),
527 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
532 case ZPOOL_PROP_ASHIFT
:
534 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
535 "property '%s' can only be set at "
536 "creation time"), propname
);
537 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
541 if (intval
!= 0 && (intval
< 9 || intval
> 13)) {
542 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
543 "property '%s' number %d is invalid."),
545 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
550 case ZPOOL_PROP_BOOTFS
:
551 if (flags
.create
|| flags
.import
) {
552 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
553 "property '%s' cannot be set at creation "
554 "or import time"), propname
);
555 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
559 if (version
< SPA_VERSION_BOOTFS
) {
560 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
561 "pool must be upgraded to support "
562 "'%s' property"), propname
);
563 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
568 * bootfs property value has to be a dataset name and
569 * the dataset has to be in the same pool as it sets to.
571 if (strval
[0] != '\0' && !bootfs_name_valid(poolname
,
573 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
574 "is an invalid name"), strval
);
575 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
579 if ((zhp
= zpool_open_canfail(hdl
, poolname
)) == NULL
) {
580 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
581 "could not open pool '%s'"), poolname
);
582 (void) zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
);
588 case ZPOOL_PROP_ALTROOT
:
589 if (!flags
.create
&& !flags
.import
) {
590 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
591 "property '%s' can only be set during pool "
592 "creation or import"), propname
);
593 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
597 if (strval
[0] != '/') {
598 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
599 "bad alternate root '%s'"), strval
);
600 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
605 case ZPOOL_PROP_CACHEFILE
:
606 if (strval
[0] == '\0')
609 if (strcmp(strval
, "none") == 0)
612 if (strval
[0] != '/') {
613 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
614 "property '%s' must be empty, an "
615 "absolute path, or 'none'"), propname
);
616 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
620 slash
= strrchr(strval
, '/');
622 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
623 strcmp(slash
, "/..") == 0) {
624 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
625 "'%s' is not a valid file"), strval
);
626 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
632 if (strval
[0] != '\0' &&
633 (stat64(strval
, &statbuf
) != 0 ||
634 !S_ISDIR(statbuf
.st_mode
))) {
635 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
636 "'%s' is not a valid directory"),
638 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
645 case ZPOOL_PROP_COMMENT
:
646 for (check
= strval
; *check
!= '\0'; check
++) {
647 if (!isprint(*check
)) {
649 dgettext(TEXT_DOMAIN
,
650 "comment may only have printable "
652 (void) zfs_error(hdl
, EZFS_BADPROP
,
657 if (strlen(strval
) > ZPROP_MAX_COMMENT
) {
658 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
659 "comment must not exceed %d characters"),
661 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
665 case ZPOOL_PROP_READONLY
:
667 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
668 "property '%s' can only be set at "
669 "import time"), propname
);
670 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
674 case ZPOOL_PROP_TNAME
:
676 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
677 "property '%s' can only be set at "
678 "creation time"), propname
);
679 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
688 nvlist_free(retprops
);
693 * Set zpool property : propname=propval.
696 zpool_set_prop(zpool_handle_t
*zhp
, const char *propname
, const char *propval
)
698 zfs_cmd_t zc
= {"\0"};
701 nvlist_t
*nvl
= NULL
;
704 prop_flags_t flags
= { 0 };
706 (void) snprintf(errbuf
, sizeof (errbuf
),
707 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
710 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
711 return (no_memory(zhp
->zpool_hdl
));
713 if (nvlist_add_string(nvl
, propname
, propval
) != 0) {
715 return (no_memory(zhp
->zpool_hdl
));
718 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
719 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
720 zhp
->zpool_name
, nvl
, version
, flags
, errbuf
)) == NULL
) {
729 * Execute the corresponding ioctl() to set this property.
731 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
733 if (zcmd_write_src_nvlist(zhp
->zpool_hdl
, &zc
, nvl
) != 0) {
738 ret
= zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_SET_PROPS
, &zc
);
740 zcmd_free_nvlists(&zc
);
744 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
746 (void) zpool_props_refresh(zhp
);
752 zpool_expand_proplist(zpool_handle_t
*zhp
, zprop_list_t
**plp
)
754 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
756 char buf
[ZFS_MAXPROPLEN
];
757 nvlist_t
*features
= NULL
;
760 boolean_t firstexpand
= (NULL
== *plp
);
763 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_POOL
) != 0)
767 while (*last
!= NULL
)
768 last
= &(*last
)->pl_next
;
771 features
= zpool_get_features(zhp
);
773 if ((*plp
)->pl_all
&& firstexpand
) {
774 for (i
= 0; i
< SPA_FEATURES
; i
++) {
775 zprop_list_t
*entry
= zfs_alloc(hdl
,
776 sizeof (zprop_list_t
));
777 entry
->pl_prop
= ZPROP_INVAL
;
778 entry
->pl_user_prop
= zfs_asprintf(hdl
, "feature@%s",
779 spa_feature_table
[i
].fi_uname
);
780 entry
->pl_width
= strlen(entry
->pl_user_prop
);
781 entry
->pl_all
= B_TRUE
;
784 last
= &entry
->pl_next
;
788 /* add any unsupported features */
789 for (nvp
= nvlist_next_nvpair(features
, NULL
);
790 nvp
!= NULL
; nvp
= nvlist_next_nvpair(features
, nvp
)) {
795 if (zfeature_is_supported(nvpair_name(nvp
)))
798 propname
= zfs_asprintf(hdl
, "unsupported@%s",
802 * Before adding the property to the list make sure that no
803 * other pool already added the same property.
807 while (entry
!= NULL
) {
808 if (entry
->pl_user_prop
!= NULL
&&
809 strcmp(propname
, entry
->pl_user_prop
) == 0) {
813 entry
= entry
->pl_next
;
820 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
821 entry
->pl_prop
= ZPROP_INVAL
;
822 entry
->pl_user_prop
= propname
;
823 entry
->pl_width
= strlen(entry
->pl_user_prop
);
824 entry
->pl_all
= B_TRUE
;
827 last
= &entry
->pl_next
;
830 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
835 if (entry
->pl_prop
!= ZPROP_INVAL
&&
836 zpool_get_prop(zhp
, entry
->pl_prop
, buf
, sizeof (buf
),
837 NULL
, B_FALSE
) == 0) {
838 if (strlen(buf
) > entry
->pl_width
)
839 entry
->pl_width
= strlen(buf
);
847 * Get the state for the given feature on the given ZFS pool.
850 zpool_prop_get_feature(zpool_handle_t
*zhp
, const char *propname
, char *buf
,
854 boolean_t found
= B_FALSE
;
855 nvlist_t
*features
= zpool_get_features(zhp
);
857 const char *feature
= strchr(propname
, '@') + 1;
859 supported
= zpool_prop_feature(propname
);
860 ASSERT(supported
|| zpool_prop_unsupported(propname
));
863 * Convert from feature name to feature guid. This conversion is
864 * unnecessary for unsupported@... properties because they already
871 ret
= zfeature_lookup_name(feature
, &fid
);
873 (void) strlcpy(buf
, "-", len
);
876 feature
= spa_feature_table
[fid
].fi_guid
;
879 if (nvlist_lookup_uint64(features
, feature
, &refcount
) == 0)
884 (void) strlcpy(buf
, ZFS_FEATURE_DISABLED
, len
);
887 (void) strlcpy(buf
, ZFS_FEATURE_ENABLED
, len
);
889 (void) strlcpy(buf
, ZFS_FEATURE_ACTIVE
, len
);
894 (void) strcpy(buf
, ZFS_UNSUPPORTED_INACTIVE
);
896 (void) strcpy(buf
, ZFS_UNSUPPORTED_READONLY
);
899 (void) strlcpy(buf
, "-", len
);
908 * Don't start the slice at the default block of 34; many storage
909 * devices will use a stripe width of 128k, other vendors prefer a 1m
910 * alignment. It is best to play it safe and ensure a 1m alignment
911 * given 512B blocks. When the block size is larger by a power of 2
912 * we will still be 1m aligned. Some devices are sensitive to the
913 * partition ending alignment as well.
915 #define NEW_START_BLOCK 2048
916 #define PARTITION_END_ALIGNMENT 2048
919 * Validate the given pool name, optionally putting an extended error message in
923 zpool_name_valid(libzfs_handle_t
*hdl
, boolean_t isopen
, const char *pool
)
929 ret
= pool_namecheck(pool
, &why
, &what
);
932 * The rules for reserved pool names were extended at a later point.
933 * But we need to support users with existing pools that may now be
934 * invalid. So we only check for this expanded set of names during a
935 * create (or import), and only in userland.
937 if (ret
== 0 && !isopen
&&
938 (strncmp(pool
, "mirror", 6) == 0 ||
939 strncmp(pool
, "raidz", 5) == 0 ||
940 strncmp(pool
, "spare", 5) == 0 ||
941 strcmp(pool
, "log") == 0)) {
944 dgettext(TEXT_DOMAIN
, "name is reserved"));
952 case NAME_ERR_TOOLONG
:
954 dgettext(TEXT_DOMAIN
, "name is too long"));
957 case NAME_ERR_INVALCHAR
:
959 dgettext(TEXT_DOMAIN
, "invalid character "
960 "'%c' in pool name"), what
);
963 case NAME_ERR_NOLETTER
:
964 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
965 "name must begin with a letter"));
968 case NAME_ERR_RESERVED
:
969 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
970 "name is reserved"));
973 case NAME_ERR_DISKLIKE
:
974 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
975 "pool name is reserved"));
978 case NAME_ERR_LEADING_SLASH
:
979 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
980 "leading slash in name"));
983 case NAME_ERR_EMPTY_COMPONENT
:
984 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
985 "empty component in name"));
988 case NAME_ERR_TRAILING_SLASH
:
989 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
990 "trailing slash in name"));
993 case NAME_ERR_MULTIPLE_DELIMITERS
:
994 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
995 "multiple '@' and/or '#' delimiters in "
999 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1000 "permission set is missing '@'"));
1011 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1015 zpool_open_canfail(libzfs_handle_t
*hdl
, const char *pool
)
1017 zpool_handle_t
*zhp
;
1021 * Make sure the pool name is valid.
1023 if (!zpool_name_valid(hdl
, B_TRUE
, pool
)) {
1024 (void) zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1025 dgettext(TEXT_DOMAIN
, "cannot open '%s'"),
1030 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
1033 zhp
->zpool_hdl
= hdl
;
1034 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1036 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1042 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "no such pool"));
1043 (void) zfs_error_fmt(hdl
, EZFS_NOENT
,
1044 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), pool
);
1053 * Like the above, but silent on error. Used when iterating over pools (because
1054 * the configuration cache may be out of date).
1057 zpool_open_silent(libzfs_handle_t
*hdl
, const char *pool
, zpool_handle_t
**ret
)
1059 zpool_handle_t
*zhp
;
1062 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
1065 zhp
->zpool_hdl
= hdl
;
1066 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1068 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1084 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1088 zpool_open(libzfs_handle_t
*hdl
, const char *pool
)
1090 zpool_handle_t
*zhp
;
1092 if ((zhp
= zpool_open_canfail(hdl
, pool
)) == NULL
)
1095 if (zhp
->zpool_state
== POOL_STATE_UNAVAIL
) {
1096 (void) zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
1097 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), zhp
->zpool_name
);
1106 * Close the handle. Simply frees the memory associated with the handle.
1109 zpool_close(zpool_handle_t
*zhp
)
1111 nvlist_free(zhp
->zpool_config
);
1112 nvlist_free(zhp
->zpool_old_config
);
1113 nvlist_free(zhp
->zpool_props
);
1118 * Return the name of the pool.
1121 zpool_get_name(zpool_handle_t
*zhp
)
1123 return (zhp
->zpool_name
);
1128 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1131 zpool_get_state(zpool_handle_t
*zhp
)
1133 return (zhp
->zpool_state
);
1137 * Create the named pool, using the provided vdev list. It is assumed
1138 * that the consumer has already validated the contents of the nvlist, so we
1139 * don't have to worry about error semantics.
1142 zpool_create(libzfs_handle_t
*hdl
, const char *pool
, nvlist_t
*nvroot
,
1143 nvlist_t
*props
, nvlist_t
*fsprops
)
1145 zfs_cmd_t zc
= {"\0"};
1146 nvlist_t
*zc_fsprops
= NULL
;
1147 nvlist_t
*zc_props
= NULL
;
1151 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1152 "cannot create '%s'"), pool
);
1154 if (!zpool_name_valid(hdl
, B_FALSE
, pool
))
1155 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
1157 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1161 prop_flags_t flags
= { .create
= B_TRUE
, .import
= B_FALSE
};
1163 if ((zc_props
= zpool_valid_proplist(hdl
, pool
, props
,
1164 SPA_VERSION_1
, flags
, msg
)) == NULL
) {
1173 zoned
= ((nvlist_lookup_string(fsprops
,
1174 zfs_prop_to_name(ZFS_PROP_ZONED
), &zonestr
) == 0) &&
1175 strcmp(zonestr
, "on") == 0);
1177 if ((zc_fsprops
= zfs_valid_proplist(hdl
, ZFS_TYPE_FILESYSTEM
,
1178 fsprops
, zoned
, NULL
, NULL
, msg
)) == NULL
) {
1182 (nvlist_alloc(&zc_props
, NV_UNIQUE_NAME
, 0) != 0)) {
1185 if (nvlist_add_nvlist(zc_props
,
1186 ZPOOL_ROOTFS_PROPS
, zc_fsprops
) != 0) {
1191 if (zc_props
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
1194 (void) strlcpy(zc
.zc_name
, pool
, sizeof (zc
.zc_name
));
1196 if ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_CREATE
, &zc
)) != 0) {
1198 zcmd_free_nvlists(&zc
);
1199 nvlist_free(zc_props
);
1200 nvlist_free(zc_fsprops
);
1205 * This can happen if the user has specified the same
1206 * device multiple times. We can't reliably detect this
1207 * until we try to add it and see we already have a
1208 * label. This can also happen under if the device is
1209 * part of an active md or lvm device.
1211 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1212 "one or more vdevs refer to the same device, or "
1213 "one of\nthe devices is part of an active md or "
1215 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1219 * This happens if the record size is smaller or larger
1220 * than the allowed size range, or not a power of 2.
1222 * NOTE: although zfs_valid_proplist is called earlier,
1223 * this case may have slipped through since the
1224 * pool does not exist yet and it is therefore
1225 * impossible to read properties e.g. max blocksize
1228 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1229 "record size invalid"));
1230 return (zfs_error(hdl
, EZFS_BADPROP
, msg
));
1234 * This occurs when one of the devices is below
1235 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1236 * device was the problem device since there's no
1237 * reliable way to determine device size from userland.
1242 zfs_nicenum(SPA_MINDEVSIZE
, buf
, sizeof (buf
));
1244 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1245 "one or more devices is less than the "
1246 "minimum size (%s)"), buf
);
1248 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1251 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1252 "one or more devices is out of space"));
1253 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1256 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1257 "cache device must be a disk or disk slice"));
1258 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1261 return (zpool_standard_error(hdl
, errno
, msg
));
1266 zcmd_free_nvlists(&zc
);
1267 nvlist_free(zc_props
);
1268 nvlist_free(zc_fsprops
);
1273 * Destroy the given pool. It is up to the caller to ensure that there are no
1274 * datasets left in the pool.
1277 zpool_destroy(zpool_handle_t
*zhp
, const char *log_str
)
1279 zfs_cmd_t zc
= {"\0"};
1280 zfs_handle_t
*zfp
= NULL
;
1281 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1284 if (zhp
->zpool_state
== POOL_STATE_ACTIVE
&&
1285 (zfp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
1288 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1289 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1291 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_DESTROY
, &zc
) != 0) {
1292 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1293 "cannot destroy '%s'"), zhp
->zpool_name
);
1295 if (errno
== EROFS
) {
1296 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1297 "one or more devices is read only"));
1298 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1300 (void) zpool_standard_error(hdl
, errno
, msg
);
1309 remove_mountpoint(zfp
);
1317 * Add the given vdevs to the pool. The caller must have already performed the
1318 * necessary verification to ensure that the vdev specification is well-formed.
1321 zpool_add(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
1323 zfs_cmd_t zc
= {"\0"};
1325 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1327 nvlist_t
**spares
, **l2cache
;
1328 uint_t nspares
, nl2cache
;
1330 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1331 "cannot add to '%s'"), zhp
->zpool_name
);
1333 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1334 SPA_VERSION_SPARES
&&
1335 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1336 &spares
, &nspares
) == 0) {
1337 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1338 "upgraded to add hot spares"));
1339 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1342 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1343 SPA_VERSION_L2CACHE
&&
1344 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1345 &l2cache
, &nl2cache
) == 0) {
1346 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1347 "upgraded to add cache devices"));
1348 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1351 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1353 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1355 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_ADD
, &zc
) != 0) {
1359 * This can happen if the user has specified the same
1360 * device multiple times. We can't reliably detect this
1361 * until we try to add it and see we already have a
1364 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1365 "one or more vdevs refer to the same device"));
1366 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1371 * This occurrs when one of the devices is below
1372 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1373 * device was the problem device since there's no
1374 * reliable way to determine device size from userland.
1379 zfs_nicenum(SPA_MINDEVSIZE
, buf
, sizeof (buf
));
1381 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1382 "device is less than the minimum "
1385 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1389 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1390 "pool must be upgraded to add these vdevs"));
1391 (void) zfs_error(hdl
, EZFS_BADVERSION
, msg
);
1395 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1396 "cache device must be a disk or disk slice"));
1397 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1401 (void) zpool_standard_error(hdl
, errno
, msg
);
1409 zcmd_free_nvlists(&zc
);
1415 * Exports the pool from the system. The caller must ensure that there are no
1416 * mounted datasets in the pool.
1419 zpool_export_common(zpool_handle_t
*zhp
, boolean_t force
, boolean_t hardforce
,
1420 const char *log_str
)
1422 zfs_cmd_t zc
= {"\0"};
1425 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1426 "cannot export '%s'"), zhp
->zpool_name
);
1428 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1429 zc
.zc_cookie
= force
;
1430 zc
.zc_guid
= hardforce
;
1431 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1433 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_EXPORT
, &zc
) != 0) {
1436 zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
1437 "use '-f' to override the following errors:\n"
1438 "'%s' has an active shared spare which could be"
1439 " used by other pools once '%s' is exported."),
1440 zhp
->zpool_name
, zhp
->zpool_name
);
1441 return (zfs_error(zhp
->zpool_hdl
, EZFS_ACTIVE_SPARE
,
1444 return (zpool_standard_error_fmt(zhp
->zpool_hdl
, errno
,
1453 zpool_export(zpool_handle_t
*zhp
, boolean_t force
, const char *log_str
)
1455 return (zpool_export_common(zhp
, force
, B_FALSE
, log_str
));
1459 zpool_export_force(zpool_handle_t
*zhp
, const char *log_str
)
1461 return (zpool_export_common(zhp
, B_TRUE
, B_TRUE
, log_str
));
1465 zpool_rewind_exclaim(libzfs_handle_t
*hdl
, const char *name
, boolean_t dryrun
,
1468 nvlist_t
*nv
= NULL
;
1474 if (!hdl
->libzfs_printerr
|| config
== NULL
)
1477 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1478 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0) {
1482 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1484 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1486 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1487 strftime(timestr
, 128, "%c", &t
) != 0) {
1489 (void) printf(dgettext(TEXT_DOMAIN
,
1490 "Would be able to return %s "
1491 "to its state as of %s.\n"),
1494 (void) printf(dgettext(TEXT_DOMAIN
,
1495 "Pool %s returned to its state as of %s.\n"),
1499 (void) printf(dgettext(TEXT_DOMAIN
,
1500 "%s approximately %lld "),
1501 dryrun
? "Would discard" : "Discarded",
1502 ((longlong_t
)loss
+ 30) / 60);
1503 (void) printf(dgettext(TEXT_DOMAIN
,
1504 "minutes of transactions.\n"));
1505 } else if (loss
> 0) {
1506 (void) printf(dgettext(TEXT_DOMAIN
,
1507 "%s approximately %lld "),
1508 dryrun
? "Would discard" : "Discarded",
1510 (void) printf(dgettext(TEXT_DOMAIN
,
1511 "seconds of transactions.\n"));
1517 zpool_explain_recover(libzfs_handle_t
*hdl
, const char *name
, int reason
,
1520 nvlist_t
*nv
= NULL
;
1522 uint64_t edata
= UINT64_MAX
;
1527 if (!hdl
->libzfs_printerr
)
1531 (void) printf(dgettext(TEXT_DOMAIN
, "action: "));
1533 (void) printf(dgettext(TEXT_DOMAIN
, "\t"));
1535 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1536 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1537 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0 ||
1538 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1541 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1542 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_DATA_ERRORS
,
1545 (void) printf(dgettext(TEXT_DOMAIN
,
1546 "Recovery is possible, but will result in some data loss.\n"));
1548 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1549 strftime(timestr
, 128, "%c", &t
) != 0) {
1550 (void) printf(dgettext(TEXT_DOMAIN
,
1551 "\tReturning the pool to its state as of %s\n"
1552 "\tshould correct the problem. "),
1555 (void) printf(dgettext(TEXT_DOMAIN
,
1556 "\tReverting the pool to an earlier state "
1557 "should correct the problem.\n\t"));
1561 (void) printf(dgettext(TEXT_DOMAIN
,
1562 "Approximately %lld minutes of data\n"
1563 "\tmust be discarded, irreversibly. "),
1564 ((longlong_t
)loss
+ 30) / 60);
1565 } else if (loss
> 0) {
1566 (void) printf(dgettext(TEXT_DOMAIN
,
1567 "Approximately %lld seconds of data\n"
1568 "\tmust be discarded, irreversibly. "),
1571 if (edata
!= 0 && edata
!= UINT64_MAX
) {
1573 (void) printf(dgettext(TEXT_DOMAIN
,
1574 "After rewind, at least\n"
1575 "\tone persistent user-data error will remain. "));
1577 (void) printf(dgettext(TEXT_DOMAIN
,
1578 "After rewind, several\n"
1579 "\tpersistent user-data errors will remain. "));
1582 (void) printf(dgettext(TEXT_DOMAIN
,
1583 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1584 reason
>= 0 ? "clear" : "import", name
);
1586 (void) printf(dgettext(TEXT_DOMAIN
,
1587 "A scrub of the pool\n"
1588 "\tis strongly recommended after recovery.\n"));
1592 (void) printf(dgettext(TEXT_DOMAIN
,
1593 "Destroy and re-create the pool from\n\ta backup source.\n"));
1597 * zpool_import() is a contracted interface. Should be kept the same
1600 * Applications should use zpool_import_props() to import a pool with
1601 * new properties value to be set.
1604 zpool_import(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1607 nvlist_t
*props
= NULL
;
1610 if (altroot
!= NULL
) {
1611 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
1612 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1613 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1617 if (nvlist_add_string(props
,
1618 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), altroot
) != 0 ||
1619 nvlist_add_string(props
,
1620 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
), "none") != 0) {
1622 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1623 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1628 ret
= zpool_import_props(hdl
, config
, newname
, props
,
1635 print_vdev_tree(libzfs_handle_t
*hdl
, const char *name
, nvlist_t
*nv
,
1641 uint64_t is_log
= 0;
1643 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
,
1647 (void) printf("\t%*s%s%s\n", indent
, "", name
,
1648 is_log
? " [log]" : "");
1650 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1651 &child
, &children
) != 0)
1654 for (c
= 0; c
< children
; c
++) {
1655 vname
= zpool_vdev_name(hdl
, NULL
, child
[c
], VDEV_NAME_TYPE_ID
);
1656 print_vdev_tree(hdl
, vname
, child
[c
], indent
+ 2);
1662 zpool_print_unsup_feat(nvlist_t
*config
)
1664 nvlist_t
*nvinfo
, *unsup_feat
;
1667 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) ==
1669 verify(nvlist_lookup_nvlist(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
,
1672 for (nvp
= nvlist_next_nvpair(unsup_feat
, NULL
); nvp
!= NULL
;
1673 nvp
= nvlist_next_nvpair(unsup_feat
, nvp
)) {
1676 verify(nvpair_type(nvp
) == DATA_TYPE_STRING
);
1677 verify(nvpair_value_string(nvp
, &desc
) == 0);
1679 if (strlen(desc
) > 0)
1680 (void) printf("\t%s (%s)\n", nvpair_name(nvp
), desc
);
1682 (void) printf("\t%s\n", nvpair_name(nvp
));
1687 * Import the given pool using the known configuration and a list of
1688 * properties to be set. The configuration should have come from
1689 * zpool_find_import(). The 'newname' parameters control whether the pool
1690 * is imported with a different name.
1693 zpool_import_props(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1694 nvlist_t
*props
, int flags
)
1696 zfs_cmd_t zc
= {"\0"};
1697 zpool_rewind_policy_t policy
;
1698 nvlist_t
*nv
= NULL
;
1699 nvlist_t
*nvinfo
= NULL
;
1700 nvlist_t
*missing
= NULL
;
1707 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1710 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1711 "cannot import pool '%s'"), origname
);
1713 if (newname
!= NULL
) {
1714 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
1715 return (zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1716 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1718 thename
= (char *)newname
;
1723 if (props
!= NULL
) {
1725 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
1727 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
1730 if ((props
= zpool_valid_proplist(hdl
, origname
,
1731 props
, version
, flags
, errbuf
)) == NULL
)
1733 if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
1740 (void) strlcpy(zc
.zc_name
, thename
, sizeof (zc
.zc_name
));
1742 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1745 if (zcmd_write_conf_nvlist(hdl
, &zc
, config
) != 0) {
1746 zcmd_free_nvlists(&zc
);
1749 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zc
.zc_nvlist_conf_size
* 2) != 0) {
1750 zcmd_free_nvlists(&zc
);
1754 zc
.zc_cookie
= flags
;
1755 while ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_IMPORT
, &zc
)) != 0 &&
1757 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
1758 zcmd_free_nvlists(&zc
);
1765 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nv
);
1767 zcmd_free_nvlists(&zc
);
1769 zpool_get_rewind_policy(config
, &policy
);
1775 * Dry-run failed, but we print out what success
1776 * looks like if we found a best txg
1778 if (policy
.zrp_request
& ZPOOL_TRY_REWIND
) {
1779 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1785 if (newname
== NULL
)
1786 (void) snprintf(desc
, sizeof (desc
),
1787 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1790 (void) snprintf(desc
, sizeof (desc
),
1791 dgettext(TEXT_DOMAIN
, "cannot import '%s' as '%s'"),
1796 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
1797 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1798 nvlist_exists(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
)) {
1799 (void) printf(dgettext(TEXT_DOMAIN
, "This "
1800 "pool uses the following feature(s) not "
1801 "supported by this system:\n"));
1802 zpool_print_unsup_feat(nv
);
1803 if (nvlist_exists(nvinfo
,
1804 ZPOOL_CONFIG_CAN_RDONLY
)) {
1805 (void) printf(dgettext(TEXT_DOMAIN
,
1806 "All unsupported features are only "
1807 "required for writing to the pool."
1808 "\nThe pool can be imported using "
1809 "'-o readonly=on'.\n"));
1813 * Unsupported version.
1815 (void) zfs_error(hdl
, EZFS_BADVERSION
, desc
);
1819 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, desc
);
1823 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1824 "one or more devices is read only"));
1825 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
1829 if (nv
&& nvlist_lookup_nvlist(nv
,
1830 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1831 nvlist_lookup_nvlist(nvinfo
,
1832 ZPOOL_CONFIG_MISSING_DEVICES
, &missing
) == 0) {
1833 (void) printf(dgettext(TEXT_DOMAIN
,
1834 "The devices below are missing, use "
1835 "'-m' to import the pool anyway:\n"));
1836 print_vdev_tree(hdl
, NULL
, missing
, 2);
1837 (void) printf("\n");
1839 (void) zpool_standard_error(hdl
, error
, desc
);
1843 (void) zpool_standard_error(hdl
, error
, desc
);
1847 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1848 "one or more devices are already in use\n"));
1849 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
1852 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1853 "new name of at least one dataset is longer than "
1854 "the maximum allowable length"));
1855 (void) zfs_error(hdl
, EZFS_NAMETOOLONG
, desc
);
1858 (void) zpool_standard_error(hdl
, error
, desc
);
1859 zpool_explain_recover(hdl
,
1860 newname
? origname
: thename
, -error
, nv
);
1867 zpool_handle_t
*zhp
;
1870 * This should never fail, but play it safe anyway.
1872 if (zpool_open_silent(hdl
, thename
, &zhp
) != 0)
1874 else if (zhp
!= NULL
)
1876 if (policy
.zrp_request
&
1877 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
1878 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1879 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0), nv
);
1892 zpool_scan(zpool_handle_t
*zhp
, pool_scan_func_t func
)
1894 zfs_cmd_t zc
= {"\0"};
1896 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1898 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1899 zc
.zc_cookie
= func
;
1901 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_SCAN
, &zc
) == 0 ||
1902 (errno
== ENOENT
&& func
!= POOL_SCAN_NONE
))
1905 if (func
== POOL_SCAN_SCRUB
) {
1906 (void) snprintf(msg
, sizeof (msg
),
1907 dgettext(TEXT_DOMAIN
, "cannot scrub %s"), zc
.zc_name
);
1908 } else if (func
== POOL_SCAN_NONE
) {
1909 (void) snprintf(msg
, sizeof (msg
),
1910 dgettext(TEXT_DOMAIN
, "cannot cancel scrubbing %s"),
1913 assert(!"unexpected result");
1916 if (errno
== EBUSY
) {
1918 pool_scan_stat_t
*ps
= NULL
;
1921 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
1922 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
1923 (void) nvlist_lookup_uint64_array(nvroot
,
1924 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &psc
);
1925 if (ps
&& ps
->pss_func
== POOL_SCAN_SCRUB
)
1926 return (zfs_error(hdl
, EZFS_SCRUBBING
, msg
));
1928 return (zfs_error(hdl
, EZFS_RESILVERING
, msg
));
1929 } else if (errno
== ENOENT
) {
1930 return (zfs_error(hdl
, EZFS_NO_SCRUB
, msg
));
1932 return (zpool_standard_error(hdl
, errno
, msg
));
1937 * Find a vdev that matches the search criteria specified. We use the
1938 * the nvpair name to determine how we should look for the device.
1939 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1940 * spare; but FALSE if its an INUSE spare.
1943 vdev_to_nvlist_iter(nvlist_t
*nv
, nvlist_t
*search
, boolean_t
*avail_spare
,
1944 boolean_t
*l2cache
, boolean_t
*log
)
1951 nvpair_t
*pair
= nvlist_next_nvpair(search
, NULL
);
1953 /* Nothing to look for */
1954 if (search
== NULL
|| pair
== NULL
)
1957 /* Obtain the key we will use to search */
1958 srchkey
= nvpair_name(pair
);
1960 switch (nvpair_type(pair
)) {
1961 case DATA_TYPE_UINT64
:
1962 if (strcmp(srchkey
, ZPOOL_CONFIG_GUID
) == 0) {
1963 uint64_t srchval
, theguid
;
1965 verify(nvpair_value_uint64(pair
, &srchval
) == 0);
1966 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1968 if (theguid
== srchval
)
1973 case DATA_TYPE_STRING
: {
1974 char *srchval
, *val
;
1976 verify(nvpair_value_string(pair
, &srchval
) == 0);
1977 if (nvlist_lookup_string(nv
, srchkey
, &val
) != 0)
1981 * Search for the requested value. Special cases:
1983 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
1984 * "-part1", or "p1". The suffix is hidden from the user,
1985 * but included in the string, so this matches around it.
1986 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
1987 * is used to check all possible expanded paths.
1988 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1990 * Otherwise, all other searches are simple string compares.
1992 if (strcmp(srchkey
, ZPOOL_CONFIG_PATH
) == 0) {
1993 uint64_t wholedisk
= 0;
1995 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
1997 if (zfs_strcmp_pathname(srchval
, val
, wholedisk
) == 0)
2000 } else if (strcmp(srchkey
, ZPOOL_CONFIG_TYPE
) == 0 && val
) {
2001 char *type
, *idx
, *end
, *p
;
2002 uint64_t id
, vdev_id
;
2005 * Determine our vdev type, keeping in mind
2006 * that the srchval is composed of a type and
2007 * vdev id pair (i.e. mirror-4).
2009 if ((type
= strdup(srchval
)) == NULL
)
2012 if ((p
= strrchr(type
, '-')) == NULL
) {
2020 * If the types don't match then keep looking.
2022 if (strncmp(val
, type
, strlen(val
)) != 0) {
2027 verify(strncmp(type
, VDEV_TYPE_RAIDZ
,
2028 strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2029 strncmp(type
, VDEV_TYPE_MIRROR
,
2030 strlen(VDEV_TYPE_MIRROR
)) == 0);
2031 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
2035 vdev_id
= strtoull(idx
, &end
, 10);
2042 * Now verify that we have the correct vdev id.
2051 if (strcmp(srchval
, val
) == 0)
2060 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2061 &child
, &children
) != 0)
2064 for (c
= 0; c
< children
; c
++) {
2065 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2066 avail_spare
, l2cache
, NULL
)) != NULL
) {
2068 * The 'is_log' value is only set for the toplevel
2069 * vdev, not the leaf vdevs. So we always lookup the
2070 * log device from the root of the vdev tree (where
2071 * 'log' is non-NULL).
2074 nvlist_lookup_uint64(child
[c
],
2075 ZPOOL_CONFIG_IS_LOG
, &is_log
) == 0 &&
2083 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2084 &child
, &children
) == 0) {
2085 for (c
= 0; c
< children
; c
++) {
2086 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2087 avail_spare
, l2cache
, NULL
)) != NULL
) {
2088 *avail_spare
= B_TRUE
;
2094 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2095 &child
, &children
) == 0) {
2096 for (c
= 0; c
< children
; c
++) {
2097 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2098 avail_spare
, l2cache
, NULL
)) != NULL
) {
2109 * Given a physical path (minus the "/devices" prefix), find the
2113 zpool_find_vdev_by_physpath(zpool_handle_t
*zhp
, const char *ppath
,
2114 boolean_t
*avail_spare
, boolean_t
*l2cache
, boolean_t
*log
)
2116 nvlist_t
*search
, *nvroot
, *ret
;
2118 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2119 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PHYS_PATH
, ppath
) == 0);
2121 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2124 *avail_spare
= B_FALSE
;
2128 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2129 nvlist_free(search
);
2135 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2138 zpool_vdev_is_interior(const char *name
)
2140 if (strncmp(name
, VDEV_TYPE_RAIDZ
, strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2141 strncmp(name
, VDEV_TYPE_MIRROR
, strlen(VDEV_TYPE_MIRROR
)) == 0)
2147 zpool_find_vdev(zpool_handle_t
*zhp
, const char *path
, boolean_t
*avail_spare
,
2148 boolean_t
*l2cache
, boolean_t
*log
)
2151 nvlist_t
*nvroot
, *search
, *ret
;
2154 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2156 guid
= strtoull(path
, &end
, 0);
2157 if (guid
!= 0 && *end
== '\0') {
2158 verify(nvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
) == 0);
2159 } else if (zpool_vdev_is_interior(path
)) {
2160 verify(nvlist_add_string(search
, ZPOOL_CONFIG_TYPE
, path
) == 0);
2162 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PATH
, path
) == 0);
2165 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2168 *avail_spare
= B_FALSE
;
2172 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2173 nvlist_free(search
);
2179 vdev_online(nvlist_t
*nv
)
2183 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
, &ival
) == 0 ||
2184 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
, &ival
) == 0 ||
2185 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
, &ival
) == 0)
2192 * Helper function for zpool_get_physpaths().
2195 vdev_get_one_physpath(nvlist_t
*config
, char *physpath
, size_t physpath_size
,
2196 size_t *bytes_written
)
2198 size_t bytes_left
, pos
, rsz
;
2202 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PHYS_PATH
,
2204 return (EZFS_NODEVICE
);
2206 pos
= *bytes_written
;
2207 bytes_left
= physpath_size
- pos
;
2208 format
= (pos
== 0) ? "%s" : " %s";
2210 rsz
= snprintf(physpath
+ pos
, bytes_left
, format
, tmppath
);
2211 *bytes_written
+= rsz
;
2213 if (rsz
>= bytes_left
) {
2214 /* if physpath was not copied properly, clear it */
2215 if (bytes_left
!= 0) {
2218 return (EZFS_NOSPC
);
2224 vdev_get_physpaths(nvlist_t
*nv
, char *physpath
, size_t phypath_size
,
2225 size_t *rsz
, boolean_t is_spare
)
2230 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
2231 return (EZFS_INVALCONFIG
);
2233 if (strcmp(type
, VDEV_TYPE_DISK
) == 0) {
2235 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2236 * For a spare vdev, we only want to boot from the active
2241 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
2244 return (EZFS_INVALCONFIG
);
2247 if (vdev_online(nv
)) {
2248 if ((ret
= vdev_get_one_physpath(nv
, physpath
,
2249 phypath_size
, rsz
)) != 0)
2252 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) == 0 ||
2253 strcmp(type
, VDEV_TYPE_RAIDZ
) == 0 ||
2254 strcmp(type
, VDEV_TYPE_REPLACING
) == 0 ||
2255 (is_spare
= (strcmp(type
, VDEV_TYPE_SPARE
) == 0))) {
2260 if (nvlist_lookup_nvlist_array(nv
,
2261 ZPOOL_CONFIG_CHILDREN
, &child
, &count
) != 0)
2262 return (EZFS_INVALCONFIG
);
2264 for (i
= 0; i
< count
; i
++) {
2265 ret
= vdev_get_physpaths(child
[i
], physpath
,
2266 phypath_size
, rsz
, is_spare
);
2267 if (ret
== EZFS_NOSPC
)
2272 return (EZFS_POOL_INVALARG
);
2276 * Get phys_path for a root pool config.
2277 * Return 0 on success; non-zero on failure.
2280 zpool_get_config_physpath(nvlist_t
*config
, char *physpath
, size_t phypath_size
)
2283 nvlist_t
*vdev_root
;
2290 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2292 return (EZFS_INVALCONFIG
);
2294 if (nvlist_lookup_string(vdev_root
, ZPOOL_CONFIG_TYPE
, &type
) != 0 ||
2295 nvlist_lookup_nvlist_array(vdev_root
, ZPOOL_CONFIG_CHILDREN
,
2296 &child
, &count
) != 0)
2297 return (EZFS_INVALCONFIG
);
2300 * root pool can only have a single top-level vdev.
2302 if (strcmp(type
, VDEV_TYPE_ROOT
) != 0 || count
!= 1)
2303 return (EZFS_POOL_INVALARG
);
2305 (void) vdev_get_physpaths(child
[0], physpath
, phypath_size
, &rsz
,
2308 /* No online devices */
2310 return (EZFS_NODEVICE
);
2316 * Get phys_path for a root pool
2317 * Return 0 on success; non-zero on failure.
2320 zpool_get_physpath(zpool_handle_t
*zhp
, char *physpath
, size_t phypath_size
)
2322 return (zpool_get_config_physpath(zhp
->zpool_config
, physpath
,
2327 * If the device has being dynamically expanded then we need to relabel
2328 * the disk to use the new unallocated space.
2331 zpool_relabel_disk(libzfs_handle_t
*hdl
, const char *path
, const char *msg
)
2335 if ((fd
= open(path
, O_RDWR
|O_DIRECT
)) < 0) {
2336 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2337 "relabel '%s': unable to open device: %d"), path
, errno
);
2338 return (zfs_error(hdl
, EZFS_OPENFAILED
, msg
));
2342 * It's possible that we might encounter an error if the device
2343 * does not have any unallocated space left. If so, we simply
2344 * ignore that error and continue on.
2346 * Also, we don't call efi_rescan() - that would just return EBUSY.
2347 * The module will do it for us in vdev_disk_open().
2349 error
= efi_use_whole_disk(fd
);
2351 if (error
&& error
!= VT_ENOSPC
) {
2352 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2353 "relabel '%s': unable to read disk capacity"), path
);
2354 return (zfs_error(hdl
, EZFS_NOCAP
, msg
));
2360 * Bring the specified vdev online. The 'flags' parameter is a set of the
2361 * ZFS_ONLINE_* flags.
2364 zpool_vdev_online(zpool_handle_t
*zhp
, const char *path
, int flags
,
2365 vdev_state_t
*newstate
)
2367 zfs_cmd_t zc
= {"\0"};
2370 boolean_t avail_spare
, l2cache
, islog
;
2371 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2374 if (flags
& ZFS_ONLINE_EXPAND
) {
2375 (void) snprintf(msg
, sizeof (msg
),
2376 dgettext(TEXT_DOMAIN
, "cannot expand %s"), path
);
2378 (void) snprintf(msg
, sizeof (msg
),
2379 dgettext(TEXT_DOMAIN
, "cannot online %s"), path
);
2382 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2383 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2385 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2387 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2390 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2392 if (flags
& ZFS_ONLINE_EXPAND
||
2393 zpool_get_prop_int(zhp
, ZPOOL_PROP_AUTOEXPAND
, NULL
)) {
2394 uint64_t wholedisk
= 0;
2396 (void) nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_WHOLE_DISK
,
2400 * XXX - L2ARC 1.0 devices can't support expansion.
2403 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2404 "cannot expand cache devices"));
2405 return (zfs_error(hdl
, EZFS_VDEVNOTSUP
, msg
));
2409 const char *fullpath
= path
;
2410 char buf
[MAXPATHLEN
];
2412 if (path
[0] != '/') {
2413 error
= zfs_resolve_shortname(path
, buf
,
2416 return (zfs_error(hdl
, EZFS_NODEVICE
,
2422 error
= zpool_relabel_disk(hdl
, fullpath
, msg
);
2428 zc
.zc_cookie
= VDEV_STATE_ONLINE
;
2431 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) != 0) {
2432 if (errno
== EINVAL
) {
2433 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "was split "
2434 "from this pool into a new one. Use '%s' "
2435 "instead"), "zpool detach");
2436 return (zfs_error(hdl
, EZFS_POSTSPLIT_ONLINE
, msg
));
2438 return (zpool_standard_error(hdl
, errno
, msg
));
2441 *newstate
= zc
.zc_cookie
;
2446 * Take the specified vdev offline
2449 zpool_vdev_offline(zpool_handle_t
*zhp
, const char *path
, boolean_t istmp
)
2451 zfs_cmd_t zc
= {"\0"};
2454 boolean_t avail_spare
, l2cache
;
2455 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2457 (void) snprintf(msg
, sizeof (msg
),
2458 dgettext(TEXT_DOMAIN
, "cannot offline %s"), path
);
2460 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2461 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2463 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2465 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2468 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2470 zc
.zc_cookie
= VDEV_STATE_OFFLINE
;
2471 zc
.zc_obj
= istmp
? ZFS_OFFLINE_TEMPORARY
: 0;
2473 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2480 * There are no other replicas of this device.
2482 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2486 * The log device has unplayed logs
2488 return (zfs_error(hdl
, EZFS_UNPLAYED_LOGS
, msg
));
2491 return (zpool_standard_error(hdl
, errno
, msg
));
2496 * Mark the given vdev faulted.
2499 zpool_vdev_fault(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2501 zfs_cmd_t zc
= {"\0"};
2503 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2505 (void) snprintf(msg
, sizeof (msg
),
2506 dgettext(TEXT_DOMAIN
, "cannot fault %llu"), (u_longlong_t
)guid
);
2508 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2510 zc
.zc_cookie
= VDEV_STATE_FAULTED
;
2513 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2520 * There are no other replicas of this device.
2522 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2525 return (zpool_standard_error(hdl
, errno
, msg
));
2531 * Mark the given vdev degraded.
2534 zpool_vdev_degrade(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2536 zfs_cmd_t zc
= {"\0"};
2538 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2540 (void) snprintf(msg
, sizeof (msg
),
2541 dgettext(TEXT_DOMAIN
, "cannot degrade %llu"), (u_longlong_t
)guid
);
2543 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2545 zc
.zc_cookie
= VDEV_STATE_DEGRADED
;
2548 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2551 return (zpool_standard_error(hdl
, errno
, msg
));
2555 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2559 is_replacing_spare(nvlist_t
*search
, nvlist_t
*tgt
, int which
)
2565 if (nvlist_lookup_nvlist_array(search
, ZPOOL_CONFIG_CHILDREN
, &child
,
2567 verify(nvlist_lookup_string(search
, ZPOOL_CONFIG_TYPE
,
2570 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0 &&
2571 children
== 2 && child
[which
] == tgt
)
2574 for (c
= 0; c
< children
; c
++)
2575 if (is_replacing_spare(child
[c
], tgt
, which
))
2583 * Attach new_disk (fully described by nvroot) to old_disk.
2584 * If 'replacing' is specified, the new disk will replace the old one.
2587 zpool_vdev_attach(zpool_handle_t
*zhp
,
2588 const char *old_disk
, const char *new_disk
, nvlist_t
*nvroot
, int replacing
)
2590 zfs_cmd_t zc
= {"\0"};
2594 boolean_t avail_spare
, l2cache
, islog
;
2599 nvlist_t
*config_root
;
2600 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2601 boolean_t rootpool
= zpool_is_bootable(zhp
);
2604 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2605 "cannot replace %s with %s"), old_disk
, new_disk
);
2607 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2608 "cannot attach %s to %s"), new_disk
, old_disk
);
2610 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2611 if ((tgt
= zpool_find_vdev(zhp
, old_disk
, &avail_spare
, &l2cache
,
2613 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2616 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2619 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2621 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2622 zc
.zc_cookie
= replacing
;
2624 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
2625 &child
, &children
) != 0 || children
!= 1) {
2626 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2627 "new device must be a single disk"));
2628 return (zfs_error(hdl
, EZFS_INVALCONFIG
, msg
));
2631 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
2632 ZPOOL_CONFIG_VDEV_TREE
, &config_root
) == 0);
2634 if ((newname
= zpool_vdev_name(NULL
, NULL
, child
[0], 0)) == NULL
)
2638 * If the target is a hot spare that has been swapped in, we can only
2639 * replace it with another hot spare.
2642 nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_IS_SPARE
, &val
) == 0 &&
2643 (zpool_find_vdev(zhp
, newname
, &avail_spare
, &l2cache
,
2644 NULL
) == NULL
|| !avail_spare
) &&
2645 is_replacing_spare(config_root
, tgt
, 1)) {
2646 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2647 "can only be replaced by another hot spare"));
2649 return (zfs_error(hdl
, EZFS_BADTARGET
, msg
));
2654 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
2657 ret
= zfs_ioctl(hdl
, ZFS_IOC_VDEV_ATTACH
, &zc
);
2659 zcmd_free_nvlists(&zc
);
2664 * XXX need a better way to prevent user from
2665 * booting up a half-baked vdev.
2667 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Make "
2668 "sure to wait until resilver is done "
2669 "before rebooting.\n"));
2677 * Can't attach to or replace this type of vdev.
2680 uint64_t version
= zpool_get_prop_int(zhp
,
2681 ZPOOL_PROP_VERSION
, NULL
);
2684 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2685 "cannot replace a log with a spare"));
2686 else if (version
>= SPA_VERSION_MULTI_REPLACE
)
2687 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2688 "already in replacing/spare config; wait "
2689 "for completion or use 'zpool detach'"));
2691 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2692 "cannot replace a replacing device"));
2694 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2695 "can only attach to mirrors and top-level "
2698 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2703 * The new device must be a single disk.
2705 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2706 "new device must be a single disk"));
2707 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2711 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "%s is busy"),
2713 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2718 * The new device is too small.
2720 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2721 "device is too small"));
2722 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2727 * The new device has a different optimal sector size.
2729 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2730 "new device has a different optimal sector size; use the "
2731 "option '-o ashift=N' to override the optimal size"));
2732 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2737 * The resulting top-level vdev spec won't fit in the label.
2739 (void) zfs_error(hdl
, EZFS_DEVOVERFLOW
, msg
);
2743 (void) zpool_standard_error(hdl
, errno
, msg
);
2750 * Detach the specified device.
2753 zpool_vdev_detach(zpool_handle_t
*zhp
, const char *path
)
2755 zfs_cmd_t zc
= {"\0"};
2758 boolean_t avail_spare
, l2cache
;
2759 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2761 (void) snprintf(msg
, sizeof (msg
),
2762 dgettext(TEXT_DOMAIN
, "cannot detach %s"), path
);
2764 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2765 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2767 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2770 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2773 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2775 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2777 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_DETACH
, &zc
) == 0)
2784 * Can't detach from this type of vdev.
2786 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "only "
2787 "applicable to mirror and replacing vdevs"));
2788 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2793 * There are no other replicas of this device.
2795 (void) zfs_error(hdl
, EZFS_NOREPLICAS
, msg
);
2799 (void) zpool_standard_error(hdl
, errno
, msg
);
2806 * Find a mirror vdev in the source nvlist.
2808 * The mchild array contains a list of disks in one of the top-level mirrors
2809 * of the source pool. The schild array contains a list of disks that the
2810 * user specified on the command line. We loop over the mchild array to
2811 * see if any entry in the schild array matches.
2813 * If a disk in the mchild array is found in the schild array, we return
2814 * the index of that entry. Otherwise we return -1.
2817 find_vdev_entry(zpool_handle_t
*zhp
, nvlist_t
**mchild
, uint_t mchildren
,
2818 nvlist_t
**schild
, uint_t schildren
)
2822 for (mc
= 0; mc
< mchildren
; mc
++) {
2824 char *mpath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
2827 for (sc
= 0; sc
< schildren
; sc
++) {
2828 char *spath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
2830 boolean_t result
= (strcmp(mpath
, spath
) == 0);
2846 * Split a mirror pool. If newroot points to null, then a new nvlist
2847 * is generated and it is the responsibility of the caller to free it.
2850 zpool_vdev_split(zpool_handle_t
*zhp
, char *newname
, nvlist_t
**newroot
,
2851 nvlist_t
*props
, splitflags_t flags
)
2853 zfs_cmd_t zc
= {"\0"};
2855 nvlist_t
*tree
, *config
, **child
, **newchild
, *newconfig
= NULL
;
2856 nvlist_t
**varray
= NULL
, *zc_props
= NULL
;
2857 uint_t c
, children
, newchildren
, lastlog
= 0, vcount
, found
= 0;
2858 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2860 boolean_t freelist
= B_FALSE
, memory_err
= B_TRUE
;
2863 (void) snprintf(msg
, sizeof (msg
),
2864 dgettext(TEXT_DOMAIN
, "Unable to split %s"), zhp
->zpool_name
);
2866 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
2867 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
2869 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
2870 (void) fprintf(stderr
, gettext("Internal error: unable to "
2871 "retrieve pool configuration\n"));
2875 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &tree
)
2877 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
, &vers
) == 0);
2880 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
2881 if ((zc_props
= zpool_valid_proplist(hdl
, zhp
->zpool_name
,
2882 props
, vers
, flags
, msg
)) == NULL
)
2886 if (nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
2888 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2889 "Source pool is missing vdev tree"));
2890 nvlist_free(zc_props
);
2894 varray
= zfs_alloc(hdl
, children
* sizeof (nvlist_t
*));
2897 if (*newroot
== NULL
||
2898 nvlist_lookup_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
2899 &newchild
, &newchildren
) != 0)
2902 for (c
= 0; c
< children
; c
++) {
2903 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
2905 nvlist_t
**mchild
, *vdev
;
2910 * Unlike cache & spares, slogs are stored in the
2911 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2913 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2915 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
2917 if (is_log
|| is_hole
) {
2919 * Create a hole vdev and put it in the config.
2921 if (nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) != 0)
2923 if (nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
,
2924 VDEV_TYPE_HOLE
) != 0)
2926 if (nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_HOLE
,
2931 varray
[vcount
++] = vdev
;
2935 verify(nvlist_lookup_string(child
[c
], ZPOOL_CONFIG_TYPE
, &type
)
2937 if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
2938 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2939 "Source pool must be composed only of mirrors\n"));
2940 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2944 verify(nvlist_lookup_nvlist_array(child
[c
],
2945 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
2947 /* find or add an entry for this top-level vdev */
2948 if (newchildren
> 0 &&
2949 (entry
= find_vdev_entry(zhp
, mchild
, mchildren
,
2950 newchild
, newchildren
)) >= 0) {
2951 /* We found a disk that the user specified. */
2952 vdev
= mchild
[entry
];
2955 /* User didn't specify a disk for this vdev. */
2956 vdev
= mchild
[mchildren
- 1];
2959 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
2963 /* did we find every disk the user specified? */
2964 if (found
!= newchildren
) {
2965 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Device list must "
2966 "include at most one disk from each mirror"));
2967 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2971 /* Prepare the nvlist for populating. */
2972 if (*newroot
== NULL
) {
2973 if (nvlist_alloc(newroot
, NV_UNIQUE_NAME
, 0) != 0)
2976 if (nvlist_add_string(*newroot
, ZPOOL_CONFIG_TYPE
,
2977 VDEV_TYPE_ROOT
) != 0)
2980 verify(nvlist_remove_all(*newroot
, ZPOOL_CONFIG_CHILDREN
) == 0);
2983 /* Add all the children we found */
2984 if (nvlist_add_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
, varray
,
2985 lastlog
== 0 ? vcount
: lastlog
) != 0)
2989 * If we're just doing a dry run, exit now with success.
2992 memory_err
= B_FALSE
;
2997 /* now build up the config list & call the ioctl */
2998 if (nvlist_alloc(&newconfig
, NV_UNIQUE_NAME
, 0) != 0)
3001 if (nvlist_add_nvlist(newconfig
,
3002 ZPOOL_CONFIG_VDEV_TREE
, *newroot
) != 0 ||
3003 nvlist_add_string(newconfig
,
3004 ZPOOL_CONFIG_POOL_NAME
, newname
) != 0 ||
3005 nvlist_add_uint64(newconfig
, ZPOOL_CONFIG_VERSION
, vers
) != 0)
3009 * The new pool is automatically part of the namespace unless we
3010 * explicitly export it.
3013 zc
.zc_cookie
= ZPOOL_EXPORT_AFTER_SPLIT
;
3014 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3015 (void) strlcpy(zc
.zc_string
, newname
, sizeof (zc
.zc_string
));
3016 if (zcmd_write_conf_nvlist(hdl
, &zc
, newconfig
) != 0)
3018 if (zc_props
!= NULL
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
3021 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SPLIT
, &zc
) != 0) {
3022 retval
= zpool_standard_error(hdl
, errno
, msg
);
3027 memory_err
= B_FALSE
;
3030 if (varray
!= NULL
) {
3033 for (v
= 0; v
< vcount
; v
++)
3034 nvlist_free(varray
[v
]);
3037 zcmd_free_nvlists(&zc
);
3038 nvlist_free(zc_props
);
3039 nvlist_free(newconfig
);
3041 nvlist_free(*newroot
);
3049 return (no_memory(hdl
));
3055 * Remove the given device. Currently, this is supported only for hot spares,
3056 * cache, and log devices.
3059 zpool_vdev_remove(zpool_handle_t
*zhp
, const char *path
)
3061 zfs_cmd_t zc
= {"\0"};
3064 boolean_t avail_spare
, l2cache
, islog
;
3065 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3068 (void) snprintf(msg
, sizeof (msg
),
3069 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3071 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3072 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3074 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3076 * XXX - this should just go away.
3078 if (!avail_spare
&& !l2cache
&& !islog
) {
3079 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3080 "only inactive hot spares, cache, "
3081 "or log devices can be removed"));
3082 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3085 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
3086 if (islog
&& version
< SPA_VERSION_HOLES
) {
3087 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3088 "pool must be upgrade to support log removal"));
3089 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
3092 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
3094 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3097 return (zpool_standard_error(hdl
, errno
, msg
));
3101 * Clear the errors for the pool, or the particular device if specified.
3104 zpool_clear(zpool_handle_t
*zhp
, const char *path
, nvlist_t
*rewindnvl
)
3106 zfs_cmd_t zc
= {"\0"};
3109 zpool_rewind_policy_t policy
;
3110 boolean_t avail_spare
, l2cache
;
3111 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3112 nvlist_t
*nvi
= NULL
;
3116 (void) snprintf(msg
, sizeof (msg
),
3117 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3120 (void) snprintf(msg
, sizeof (msg
),
3121 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3124 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3126 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
,
3127 &l2cache
, NULL
)) == 0)
3128 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3131 * Don't allow error clearing for hot spares. Do allow
3132 * error clearing for l2cache devices.
3135 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3137 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
,
3141 zpool_get_rewind_policy(rewindnvl
, &policy
);
3142 zc
.zc_cookie
= policy
.zrp_request
;
3144 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zhp
->zpool_config_size
* 2) != 0)
3147 if (zcmd_write_src_nvlist(hdl
, &zc
, rewindnvl
) != 0)
3150 while ((error
= zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
)) != 0 &&
3152 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
3153 zcmd_free_nvlists(&zc
);
3158 if (!error
|| ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) &&
3159 errno
!= EPERM
&& errno
!= EACCES
)) {
3160 if (policy
.zrp_request
&
3161 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
3162 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nvi
);
3163 zpool_rewind_exclaim(hdl
, zc
.zc_name
,
3164 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0),
3168 zcmd_free_nvlists(&zc
);
3172 zcmd_free_nvlists(&zc
);
3173 return (zpool_standard_error(hdl
, errno
, msg
));
3177 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3180 zpool_vdev_clear(zpool_handle_t
*zhp
, uint64_t guid
)
3182 zfs_cmd_t zc
= {"\0"};
3184 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3186 (void) snprintf(msg
, sizeof (msg
),
3187 dgettext(TEXT_DOMAIN
, "cannot clear errors for %llx"),
3188 (u_longlong_t
)guid
);
3190 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3192 zc
.zc_cookie
= ZPOOL_NO_REWIND
;
3194 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_CLEAR
, &zc
) == 0)
3197 return (zpool_standard_error(hdl
, errno
, msg
));
3201 * Change the GUID for a pool.
3204 zpool_reguid(zpool_handle_t
*zhp
)
3207 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3208 zfs_cmd_t zc
= {"\0"};
3210 (void) snprintf(msg
, sizeof (msg
),
3211 dgettext(TEXT_DOMAIN
, "cannot reguid '%s'"), zhp
->zpool_name
);
3213 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3214 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REGUID
, &zc
) == 0)
3217 return (zpool_standard_error(hdl
, errno
, msg
));
3224 zpool_reopen(zpool_handle_t
*zhp
)
3226 zfs_cmd_t zc
= {"\0"};
3228 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3230 (void) snprintf(msg
, sizeof (msg
),
3231 dgettext(TEXT_DOMAIN
, "cannot reopen '%s'"),
3234 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3235 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REOPEN
, &zc
) == 0)
3237 return (zpool_standard_error(hdl
, errno
, msg
));
3240 #if defined(__sun__) || defined(__sun)
3242 * Convert from a devid string to a path.
3245 devid_to_path(char *devid_str
)
3250 devid_nmlist_t
*list
= NULL
;
3253 if (devid_str_decode(devid_str
, &devid
, &minor
) != 0)
3256 ret
= devid_deviceid_to_nmlist("/dev", devid
, minor
, &list
);
3258 devid_str_free(minor
);
3265 * In a case the strdup() fails, we will just return NULL below.
3267 path
= strdup(list
[0].devname
);
3269 devid_free_nmlist(list
);
3275 * Convert from a path to a devid string.
3278 path_to_devid(const char *path
)
3284 if ((fd
= open(path
, O_RDONLY
)) < 0)
3289 if (devid_get(fd
, &devid
) == 0) {
3290 if (devid_get_minor_name(fd
, &minor
) == 0)
3291 ret
= devid_str_encode(devid
, minor
);
3293 devid_str_free(minor
);
3302 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3303 * ignore any failure here, since a common case is for an unprivileged user to
3304 * type 'zpool status', and we'll display the correct information anyway.
3307 set_path(zpool_handle_t
*zhp
, nvlist_t
*nv
, const char *path
)
3309 zfs_cmd_t zc
= {"\0"};
3311 (void) strncpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3312 (void) strncpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
3313 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
3316 (void) ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_VDEV_SETPATH
, &zc
);
3321 * Remove partition suffix from a vdev path. Partition suffixes may take three
3322 * forms: "-partX", "pX", or "X", where X is a string of digits. The second
3323 * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
3324 * third case only occurs when preceded by a string matching the regular
3325 * expression "^([hsv]|xv)d[a-z]+", i.e. a scsi, ide, virtio or xen disk.
3327 * caller must free the returned string
3330 zfs_strip_partition(char *path
)
3332 char *tmp
= strdup(path
);
3333 char *part
= NULL
, *d
= NULL
;
3337 if ((part
= strstr(tmp
, "-part")) && part
!= tmp
) {
3339 } else if ((part
= strrchr(tmp
, 'p')) &&
3340 part
> tmp
+ 1 && isdigit(*(part
-1))) {
3342 } else if ((tmp
[0] == 'h' || tmp
[0] == 's' || tmp
[0] == 'v') &&
3344 for (d
= &tmp
[2]; isalpha(*d
); part
= ++d
) { }
3345 } else if (strncmp("xvd", tmp
, 3) == 0) {
3346 for (d
= &tmp
[3]; isalpha(*d
); part
= ++d
) { }
3348 if (part
&& d
&& *d
!= '\0') {
3349 for (; isdigit(*d
); d
++) { }
3358 * Same as zfs_strip_partition, but allows "/dev/" to be in the pathname
3363 * Returned string must be freed.
3366 zfs_strip_partition_path(char *path
)
3368 char *newpath
= strdup(path
);
3375 /* Point to "sda1" part of "/dev/sda1" */
3376 sd_offset
= strrchr(newpath
, '/') + 1;
3378 /* Get our new name "sda" */
3379 new_sd
= zfs_strip_partition(sd_offset
);
3385 /* Paste the "sda" where "sda1" was */
3386 strlcpy(sd_offset
, new_sd
, strlen(sd_offset
) + 1);
3388 /* Free temporary "sda" */
3394 #define PATH_BUF_LEN 64
3397 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3398 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3399 * We also check if this is a whole disk, in which case we strip off the
3400 * trailing 's0' slice name.
3402 * This routine is also responsible for identifying when disks have been
3403 * reconfigured in a new location. The kernel will have opened the device by
3404 * devid, but the path will still refer to the old location. To catch this, we
3405 * first do a path -> devid translation (which is fast for the common case). If
3406 * the devid matches, we're done. If not, we do a reverse devid -> path
3407 * translation and issue the appropriate ioctl() to update the path of the vdev.
3408 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3412 zpool_vdev_name(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, nvlist_t
*nv
,
3415 char *path
, *type
, *env
;
3417 char buf
[PATH_BUF_LEN
];
3418 char tmpbuf
[PATH_BUF_LEN
];
3420 env
= getenv("ZPOOL_VDEV_NAME_PATH");
3421 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3422 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3423 name_flags
|= VDEV_NAME_PATH
;
3425 env
= getenv("ZPOOL_VDEV_NAME_GUID");
3426 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3427 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3428 name_flags
|= VDEV_NAME_GUID
;
3430 env
= getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
3431 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3432 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3433 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
3435 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
, &value
) == 0 ||
3436 name_flags
& VDEV_NAME_GUID
) {
3437 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
);
3438 (void) snprintf(buf
, sizeof (buf
), "%llu", (u_longlong_t
)value
);
3440 } else if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
3441 #if defined(__sun__) || defined(__sun)
3443 * Live VDEV path updates to a kernel VDEV during a
3444 * zpool_vdev_name lookup are not supported on Linux.
3451 * If the device is dead (faulted, offline, etc) then don't
3452 * bother opening it. Otherwise we may be forcing the user to
3453 * open a misbehaving device, which can have undesirable
3456 if ((nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
3457 (uint64_t **)&vs
, &vsc
) != 0 ||
3458 vs
->vs_state
>= VDEV_STATE_DEGRADED
) &&
3460 nvlist_lookup_string(nv
, ZPOOL_CONFIG_DEVID
, &devid
) == 0) {
3462 * Determine if the current path is correct.
3464 char *newdevid
= path_to_devid(path
);
3466 if (newdevid
== NULL
||
3467 strcmp(devid
, newdevid
) != 0) {
3470 if ((newpath
= devid_to_path(devid
)) != NULL
) {
3472 * Update the path appropriately.
3474 set_path(zhp
, nv
, newpath
);
3475 if (nvlist_add_string(nv
,
3476 ZPOOL_CONFIG_PATH
, newpath
) == 0)
3477 verify(nvlist_lookup_string(nv
,
3485 devid_str_free(newdevid
);
3489 if (name_flags
& VDEV_NAME_FOLLOW_LINKS
) {
3490 char *rp
= realpath(path
, NULL
);
3492 strlcpy(buf
, rp
, sizeof (buf
));
3499 * For a block device only use the name.
3501 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
3502 if ((strcmp(type
, VDEV_TYPE_DISK
) == 0) &&
3503 !(name_flags
& VDEV_NAME_PATH
)) {
3504 path
= strrchr(path
, '/');
3509 * Remove the partition from the path it this is a whole disk.
3511 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
, &value
)
3512 == 0 && value
&& !(name_flags
& VDEV_NAME_PATH
)) {
3513 return (zfs_strip_partition(path
));
3516 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &path
) == 0);
3519 * If it's a raidz device, we need to stick in the parity level.
3521 if (strcmp(path
, VDEV_TYPE_RAIDZ
) == 0) {
3522 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
,
3524 (void) snprintf(buf
, sizeof (buf
), "%s%llu", path
,
3525 (u_longlong_t
)value
);
3530 * We identify each top-level vdev by using a <type-id>
3531 * naming convention.
3533 if (name_flags
& VDEV_NAME_TYPE_ID
) {
3535 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
3537 (void) snprintf(tmpbuf
, sizeof (tmpbuf
), "%s-%llu",
3538 path
, (u_longlong_t
)id
);
3543 return (zfs_strdup(hdl
, path
));
3547 zbookmark_mem_compare(const void *a
, const void *b
)
3549 return (memcmp(a
, b
, sizeof (zbookmark_phys_t
)));
3553 * Retrieve the persistent error log, uniquify the members, and return to the
3557 zpool_get_errlog(zpool_handle_t
*zhp
, nvlist_t
**nverrlistp
)
3559 zfs_cmd_t zc
= {"\0"};
3561 zbookmark_phys_t
*zb
= NULL
;
3565 * Retrieve the raw error list from the kernel. If the number of errors
3566 * has increased, allocate more space and continue until we get the
3569 verify(nvlist_lookup_uint64(zhp
->zpool_config
, ZPOOL_CONFIG_ERRCOUNT
,
3573 if ((zc
.zc_nvlist_dst
= (uintptr_t)zfs_alloc(zhp
->zpool_hdl
,
3574 count
* sizeof (zbookmark_phys_t
))) == (uintptr_t)NULL
)
3576 zc
.zc_nvlist_dst_size
= count
;
3577 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3579 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_ERROR_LOG
,
3581 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3582 if (errno
== ENOMEM
) {
3585 count
= zc
.zc_nvlist_dst_size
;
3586 dst
= zfs_alloc(zhp
->zpool_hdl
, count
*
3587 sizeof (zbookmark_phys_t
));
3590 zc
.zc_nvlist_dst
= (uintptr_t)dst
;
3600 * Sort the resulting bookmarks. This is a little confusing due to the
3601 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3602 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3603 * _not_ copied as part of the process. So we point the start of our
3604 * array appropriate and decrement the total number of elements.
3606 zb
= ((zbookmark_phys_t
*)(uintptr_t)zc
.zc_nvlist_dst
) +
3607 zc
.zc_nvlist_dst_size
;
3608 count
-= zc
.zc_nvlist_dst_size
;
3610 qsort(zb
, count
, sizeof (zbookmark_phys_t
), zbookmark_mem_compare
);
3612 verify(nvlist_alloc(nverrlistp
, 0, KM_SLEEP
) == 0);
3615 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3617 for (i
= 0; i
< count
; i
++) {
3620 /* ignoring zb_blkid and zb_level for now */
3621 if (i
> 0 && zb
[i
-1].zb_objset
== zb
[i
].zb_objset
&&
3622 zb
[i
-1].zb_object
== zb
[i
].zb_object
)
3625 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) != 0)
3627 if (nvlist_add_uint64(nv
, ZPOOL_ERR_DATASET
,
3628 zb
[i
].zb_objset
) != 0) {
3632 if (nvlist_add_uint64(nv
, ZPOOL_ERR_OBJECT
,
3633 zb
[i
].zb_object
) != 0) {
3637 if (nvlist_add_nvlist(*nverrlistp
, "ejk", nv
) != 0) {
3644 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3648 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3649 return (no_memory(zhp
->zpool_hdl
));
3653 * Upgrade a ZFS pool to the latest on-disk version.
3656 zpool_upgrade(zpool_handle_t
*zhp
, uint64_t new_version
)
3658 zfs_cmd_t zc
= {"\0"};
3659 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3661 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3662 zc
.zc_cookie
= new_version
;
3664 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_UPGRADE
, &zc
) != 0)
3665 return (zpool_standard_error_fmt(hdl
, errno
,
3666 dgettext(TEXT_DOMAIN
, "cannot upgrade '%s'"),
3672 zfs_save_arguments(int argc
, char **argv
, char *string
, int len
)
3676 (void) strlcpy(string
, basename(argv
[0]), len
);
3677 for (i
= 1; i
< argc
; i
++) {
3678 (void) strlcat(string
, " ", len
);
3679 (void) strlcat(string
, argv
[i
], len
);
3684 zpool_log_history(libzfs_handle_t
*hdl
, const char *message
)
3686 zfs_cmd_t zc
= {"\0"};
3690 args
= fnvlist_alloc();
3691 fnvlist_add_string(args
, "message", message
);
3692 err
= zcmd_write_src_nvlist(hdl
, &zc
, args
);
3694 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_LOG_HISTORY
, &zc
);
3696 zcmd_free_nvlists(&zc
);
3701 * Perform ioctl to get some command history of a pool.
3703 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3704 * logical offset of the history buffer to start reading from.
3706 * Upon return, 'off' is the next logical offset to read from and
3707 * 'len' is the actual amount of bytes read into 'buf'.
3710 get_history(zpool_handle_t
*zhp
, char *buf
, uint64_t *off
, uint64_t *len
)
3712 zfs_cmd_t zc
= {"\0"};
3713 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3715 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3717 zc
.zc_history
= (uint64_t)(uintptr_t)buf
;
3718 zc
.zc_history_len
= *len
;
3719 zc
.zc_history_offset
= *off
;
3721 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_HISTORY
, &zc
) != 0) {
3724 return (zfs_error_fmt(hdl
, EZFS_PERM
,
3725 dgettext(TEXT_DOMAIN
,
3726 "cannot show history for pool '%s'"),
3729 return (zfs_error_fmt(hdl
, EZFS_NOHISTORY
,
3730 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
3731 "'%s'"), zhp
->zpool_name
));
3733 return (zfs_error_fmt(hdl
, EZFS_BADVERSION
,
3734 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
3735 "'%s', pool must be upgraded"), zhp
->zpool_name
));
3737 return (zpool_standard_error_fmt(hdl
, errno
,
3738 dgettext(TEXT_DOMAIN
,
3739 "cannot get history for '%s'"), zhp
->zpool_name
));
3743 *len
= zc
.zc_history_len
;
3744 *off
= zc
.zc_history_offset
;
3750 * Process the buffer of nvlists, unpacking and storing each nvlist record
3751 * into 'records'. 'leftover' is set to the number of bytes that weren't
3752 * processed as there wasn't a complete record.
3755 zpool_history_unpack(char *buf
, uint64_t bytes_read
, uint64_t *leftover
,
3756 nvlist_t
***records
, uint_t
*numrecords
)
3763 while (bytes_read
> sizeof (reclen
)) {
3765 /* get length of packed record (stored as little endian) */
3766 for (i
= 0, reclen
= 0; i
< sizeof (reclen
); i
++)
3767 reclen
+= (uint64_t)(((uchar_t
*)buf
)[i
]) << (8*i
);
3769 if (bytes_read
< sizeof (reclen
) + reclen
)
3773 if (nvlist_unpack(buf
+ sizeof (reclen
), reclen
, &nv
, 0) != 0)
3775 bytes_read
-= sizeof (reclen
) + reclen
;
3776 buf
+= sizeof (reclen
) + reclen
;
3778 /* add record to nvlist array */
3780 if (ISP2(*numrecords
+ 1)) {
3781 tmp
= realloc(*records
,
3782 *numrecords
* 2 * sizeof (nvlist_t
*));
3790 (*records
)[*numrecords
- 1] = nv
;
3793 *leftover
= bytes_read
;
3798 * Retrieve the command history of a pool.
3801 zpool_get_history(zpool_handle_t
*zhp
, nvlist_t
**nvhisp
)
3804 int buflen
= 128 * 1024;
3806 nvlist_t
**records
= NULL
;
3807 uint_t numrecords
= 0;
3810 buf
= malloc(buflen
);
3814 uint64_t bytes_read
= buflen
;
3817 if ((err
= get_history(zhp
, buf
, &off
, &bytes_read
)) != 0)
3820 /* if nothing else was read in, we're at EOF, just return */
3824 if ((err
= zpool_history_unpack(buf
, bytes_read
,
3825 &leftover
, &records
, &numrecords
)) != 0)
3828 if (leftover
== bytes_read
) {
3830 * no progress made, because buffer is not big enough
3831 * to hold this record; resize and retry.
3835 buf
= malloc(buflen
);
3846 verify(nvlist_alloc(nvhisp
, NV_UNIQUE_NAME
, 0) == 0);
3847 verify(nvlist_add_nvlist_array(*nvhisp
, ZPOOL_HIST_RECORD
,
3848 records
, numrecords
) == 0);
3850 for (i
= 0; i
< numrecords
; i
++)
3851 nvlist_free(records
[i
]);
3858 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
3859 * If there is a new event available 'nvp' will contain a newly allocated
3860 * nvlist and 'dropped' will be set to the number of missed events since
3861 * the last call to this function. When 'nvp' is set to NULL it indicates
3862 * no new events are available. In either case the function returns 0 and
3863 * it is up to the caller to free 'nvp'. In the case of a fatal error the
3864 * function will return a non-zero value. When the function is called in
3865 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
3866 * it will not return until a new event is available.
3869 zpool_events_next(libzfs_handle_t
*hdl
, nvlist_t
**nvp
,
3870 int *dropped
, unsigned flags
, int zevent_fd
)
3872 zfs_cmd_t zc
= {"\0"};
3877 zc
.zc_cleanup_fd
= zevent_fd
;
3879 if (flags
& ZEVENT_NONBLOCK
)
3880 zc
.zc_guid
= ZEVENT_NONBLOCK
;
3882 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, ZEVENT_SIZE
) != 0)
3886 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_NEXT
, &zc
) != 0) {
3889 error
= zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
3890 dgettext(TEXT_DOMAIN
, "zfs shutdown"));
3893 /* Blocking error case should not occur */
3894 if (!(flags
& ZEVENT_NONBLOCK
))
3895 error
= zpool_standard_error_fmt(hdl
, errno
,
3896 dgettext(TEXT_DOMAIN
, "cannot get event"));
3900 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
3901 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
3902 dgettext(TEXT_DOMAIN
, "cannot get event"));
3908 error
= zpool_standard_error_fmt(hdl
, errno
,
3909 dgettext(TEXT_DOMAIN
, "cannot get event"));
3914 error
= zcmd_read_dst_nvlist(hdl
, &zc
, nvp
);
3918 *dropped
= (int)zc
.zc_cookie
;
3920 zcmd_free_nvlists(&zc
);
3929 zpool_events_clear(libzfs_handle_t
*hdl
, int *count
)
3931 zfs_cmd_t zc
= {"\0"};
3934 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
3935 "cannot clear events"));
3937 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_CLEAR
, &zc
) != 0)
3938 return (zpool_standard_error_fmt(hdl
, errno
, msg
));
3941 *count
= (int)zc
.zc_cookie
; /* # of events cleared */
3947 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
3948 * the passed zevent_fd file handle. On success zero is returned,
3949 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
3952 zpool_events_seek(libzfs_handle_t
*hdl
, uint64_t eid
, int zevent_fd
)
3954 zfs_cmd_t zc
= {"\0"};
3958 zc
.zc_cleanup_fd
= zevent_fd
;
3960 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_SEEK
, &zc
) != 0) {
3963 error
= zfs_error_fmt(hdl
, EZFS_NOENT
,
3964 dgettext(TEXT_DOMAIN
, "cannot get event"));
3968 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
3969 dgettext(TEXT_DOMAIN
, "cannot get event"));
3973 error
= zpool_standard_error_fmt(hdl
, errno
,
3974 dgettext(TEXT_DOMAIN
, "cannot get event"));
3983 zpool_obj_to_path(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
3984 char *pathname
, size_t len
)
3986 zfs_cmd_t zc
= {"\0"};
3987 boolean_t mounted
= B_FALSE
;
3988 char *mntpnt
= NULL
;
3989 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
3992 /* special case for the MOS */
3993 (void) snprintf(pathname
, len
, "<metadata>:<0x%llx>",
3998 /* get the dataset's name */
3999 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4001 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
,
4002 ZFS_IOC_DSOBJ_TO_DSNAME
, &zc
) != 0) {
4003 /* just write out a path of two object numbers */
4004 (void) snprintf(pathname
, len
, "<0x%llx>:<0x%llx>",
4005 (longlong_t
)dsobj
, (longlong_t
)obj
);
4008 (void) strlcpy(dsname
, zc
.zc_value
, sizeof (dsname
));
4010 /* find out if the dataset is mounted */
4011 mounted
= is_mounted(zhp
->zpool_hdl
, dsname
, &mntpnt
);
4013 /* get the corrupted object's path */
4014 (void) strlcpy(zc
.zc_name
, dsname
, sizeof (zc
.zc_name
));
4016 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_OBJ_TO_PATH
,
4019 (void) snprintf(pathname
, len
, "%s%s", mntpnt
,
4022 (void) snprintf(pathname
, len
, "%s:%s",
4023 dsname
, zc
.zc_value
);
4026 (void) snprintf(pathname
, len
, "%s:<0x%llx>", dsname
,
4033 * Read the EFI label from the config, if a label does not exist then
4034 * pass back the error to the caller. If the caller has passed a non-NULL
4035 * diskaddr argument then we set it to the starting address of the EFI
4039 read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
)
4043 char diskname
[MAXPATHLEN
];
4046 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PATH
, &path
) != 0)
4049 (void) snprintf(diskname
, sizeof (diskname
), "%s%s", DISK_ROOT
,
4050 strrchr(path
, '/'));
4051 if ((fd
= open(diskname
, O_RDWR
|O_DIRECT
)) >= 0) {
4052 struct dk_gpt
*vtoc
;
4054 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) >= 0) {
4056 *sb
= vtoc
->efi_parts
[0].p_start
;
4065 * determine where a partition starts on a disk in the current
4069 find_start_block(nvlist_t
*config
)
4073 diskaddr_t sb
= MAXOFFSET_T
;
4076 if (nvlist_lookup_nvlist_array(config
,
4077 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) != 0) {
4078 if (nvlist_lookup_uint64(config
,
4079 ZPOOL_CONFIG_WHOLE_DISK
,
4080 &wholedisk
) != 0 || !wholedisk
) {
4081 return (MAXOFFSET_T
);
4083 if (read_efi_label(config
, &sb
) < 0)
4088 for (c
= 0; c
< children
; c
++) {
4089 sb
= find_start_block(child
[c
]);
4090 if (sb
!= MAXOFFSET_T
) {
4094 return (MAXOFFSET_T
);
4098 zpool_label_disk_check(char *path
)
4100 struct dk_gpt
*vtoc
;
4103 if ((fd
= open(path
, O_RDWR
|O_DIRECT
)) < 0)
4106 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) != 0) {
4111 if (vtoc
->efi_flags
& EFI_GPT_PRIMARY_CORRUPT
) {
4123 * Generate a unique partition name for the ZFS member. Partitions must
4124 * have unique names to ensure udev will be able to create symlinks under
4125 * /dev/disk/by-partlabel/ for all pool members. The partition names are
4126 * of the form <pool>-<unique-id>.
4129 zpool_label_name(char *label_name
, int label_size
)
4134 fd
= open("/dev/urandom", O_RDONLY
);
4136 if (read(fd
, &id
, sizeof (id
)) != sizeof (id
))
4143 id
= (((uint64_t)rand()) << 32) | (uint64_t)rand();
4145 snprintf(label_name
, label_size
, "zfs-%016llx", (u_longlong_t
)id
);
4149 * Label an individual disk. The name provided is the short name,
4150 * stripped of any leading /dev path.
4153 zpool_label_disk(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, char *name
)
4155 char path
[MAXPATHLEN
];
4156 struct dk_gpt
*vtoc
;
4158 size_t resv
= EFI_MIN_RESV_SIZE
;
4159 uint64_t slice_size
;
4160 diskaddr_t start_block
;
4163 /* prepare an error message just in case */
4164 (void) snprintf(errbuf
, sizeof (errbuf
),
4165 dgettext(TEXT_DOMAIN
, "cannot label '%s'"), name
);
4170 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
4171 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
4173 if (zhp
->zpool_start_block
== 0)
4174 start_block
= find_start_block(nvroot
);
4176 start_block
= zhp
->zpool_start_block
;
4177 zhp
->zpool_start_block
= start_block
;
4180 start_block
= NEW_START_BLOCK
;
4183 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4185 if ((fd
= open(path
, O_RDWR
|O_DIRECT
|O_EXCL
)) < 0) {
4187 * This shouldn't happen. We've long since verified that this
4188 * is a valid device.
4190 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
4191 "label '%s': unable to open device: %d"), path
, errno
);
4192 return (zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
));
4195 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, &vtoc
) != 0) {
4197 * The only way this can fail is if we run out of memory, or we
4198 * were unable to read the disk's capacity
4200 if (errno
== ENOMEM
)
4201 (void) no_memory(hdl
);
4204 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
4205 "label '%s': unable to read disk capacity"), path
);
4207 return (zfs_error(hdl
, EZFS_NOCAP
, errbuf
));
4210 slice_size
= vtoc
->efi_last_u_lba
+ 1;
4211 slice_size
-= EFI_MIN_RESV_SIZE
;
4212 if (start_block
== MAXOFFSET_T
)
4213 start_block
= NEW_START_BLOCK
;
4214 slice_size
-= start_block
;
4215 slice_size
= P2ALIGN(slice_size
, PARTITION_END_ALIGNMENT
);
4217 vtoc
->efi_parts
[0].p_start
= start_block
;
4218 vtoc
->efi_parts
[0].p_size
= slice_size
;
4221 * Why we use V_USR: V_BACKUP confuses users, and is considered
4222 * disposable by some EFI utilities (since EFI doesn't have a backup
4223 * slice). V_UNASSIGNED is supposed to be used only for zero size
4224 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4225 * etc. were all pretty specific. V_USR is as close to reality as we
4226 * can get, in the absence of V_OTHER.
4228 vtoc
->efi_parts
[0].p_tag
= V_USR
;
4229 zpool_label_name(vtoc
->efi_parts
[0].p_name
, EFI_PART_NAME_LEN
);
4231 vtoc
->efi_parts
[8].p_start
= slice_size
+ start_block
;
4232 vtoc
->efi_parts
[8].p_size
= resv
;
4233 vtoc
->efi_parts
[8].p_tag
= V_RESERVED
;
4235 if ((rval
= efi_write(fd
, vtoc
)) != 0 || (rval
= efi_rescan(fd
)) != 0) {
4237 * Some block drivers (like pcata) may not support EFI
4238 * GPT labels. Print out a helpful error message dir-
4239 * ecting the user to manually label the disk and give
4245 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "try using "
4246 "parted(8) and then provide a specific slice: %d"), rval
);
4247 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4253 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4254 (void) zfs_append_partition(path
, MAXPATHLEN
);
4256 /* Wait to udev to signal use the device has settled. */
4257 rval
= zpool_label_disk_wait(path
, DISK_LABEL_WAIT
);
4259 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "failed to "
4260 "detect device partitions on '%s': %d"), path
, rval
);
4261 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4264 /* We can't be to paranoid. Read the label back and verify it. */
4265 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4266 rval
= zpool_label_disk_check(path
);
4268 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "freshly written "
4269 "EFI label on '%s' is damaged. Ensure\nthis device "
4270 "is not in in use, and is functioning properly: %d"),
4272 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4279 * Allocate and return the underlying device name for a device mapper device.
4280 * If a device mapper device maps to multiple devices, return the first device.
4282 * For example, dm_name = "/dev/dm-0" could return "/dev/sda". Symlinks to a
4283 * DM device (like /dev/disk/by-vdev/A0) are also allowed.
4285 * Returns device name, or NULL on error or no match. If dm_name is not a DM
4286 * device then return NULL.
4288 * NOTE: The returned name string must be *freed*.
4291 dm_get_underlying_path(char *dm_name
)
4301 if (dm_name
== NULL
)
4304 /* dm name may be a symlink (like /dev/disk/by-vdev/A0) */
4305 realp
= realpath(dm_name
, NULL
);
4310 * If they preface 'dev' with a path (like "/dev") then strip it off.
4311 * We just want the 'dm-N' part.
4313 tmp
= strrchr(realp
, '/');
4315 dev_str
= tmp
+ 1; /* +1 since we want the chr after '/' */
4319 size
= asprintf(&tmp
, "/sys/block/%s/slaves/", dev_str
);
4320 if (size
== -1 || !tmp
)
4327 /* Return first sd* entry in /sys/block/dm-N/slaves/ */
4328 while ((ep
= readdir(dp
))) {
4329 if (ep
->d_type
!= DT_DIR
) { /* skip "." and ".." dirs */
4330 size
= asprintf(&path
, "/dev/%s", ep
->d_name
);
4344 * Return 1 if device is a device mapper or multipath device.
4348 zfs_dev_is_dm(char *dev_name
)
4352 tmp
= dm_get_underlying_path(dev_name
);
4361 * Lookup the underlying device for a device name
4363 * Often you'll have a symlink to a device, a partition device,
4364 * or a multipath device, and want to look up the underlying device.
4365 * This function returns the underlying device name. If the device
4366 * name is already the underlying device, then just return the same
4367 * name. If the device is a DM device with multiple underlying devices
4368 * then return the first one.
4372 * 1. /dev/disk/by-id/ata-QEMU_HARDDISK_QM00001 -> ../../sda
4373 * dev_name: /dev/disk/by-id/ata-QEMU_HARDDISK_QM00001
4376 * 2. /dev/mapper/mpatha (made up of /dev/sda and /dev/sdb)
4377 * dev_name: /dev/mapper/mpatha
4378 * returns: /dev/sda (first device)
4380 * 3. /dev/sda (already the underlying device)
4381 * dev_name: /dev/sda
4384 * 4. /dev/dm-3 (mapped to /dev/sda)
4385 * dev_name: /dev/dm-3
4388 * 5. /dev/disk/by-id/scsi-0QEMU_drive-scsi0-0-0-0-part9 -> ../../sdb9
4389 * dev_name: /dev/disk/by-id/scsi-0QEMU_drive-scsi0-0-0-0-part9
4392 * 6. /dev/disk/by-uuid/5df030cf-3cd9-46e4-8e99-3ccb462a4e9a -> ../dev/sda2
4393 * dev_name: /dev/disk/by-uuid/5df030cf-3cd9-46e4-8e99-3ccb462a4e9a
4396 * Returns underlying device name, or NULL on error or no match.
4398 * NOTE: The returned name string must be *freed*.
4401 zfs_get_underlying_path(char *dev_name
)
4406 if (dev_name
== NULL
)
4409 tmp
= dm_get_underlying_path(dev_name
);
4411 /* dev_name not a DM device, so just un-symlinkize it */
4413 tmp
= realpath(dev_name
, NULL
);
4416 name
= zfs_strip_partition_path(tmp
);
4424 * Given a dev name like "sda", return the full enclosure sysfs path to
4425 * the disk. You can also pass in the name with "/dev" prepended
4426 * to it (like /dev/sda).
4428 * For example, disk "sda" in enclosure slot 1:
4430 * returns: "/sys/class/enclosure/1:0:3:0/Slot 1"
4432 * 'dev' must be a non-devicemapper device.
4434 * Returned string must be freed.
4437 zfs_get_enclosure_sysfs_path(char *dev_name
)
4441 char buf
[MAXPATHLEN
];
4449 if (dev_name
== NULL
)
4452 /* If they preface 'dev' with a path (like "/dev") then strip it off */
4453 tmp1
= strrchr(dev_name
, '/');
4455 dev_name
= tmp1
+ 1; /* +1 since we want the chr after '/' */
4457 tmpsize
= asprintf(&tmp1
, "/sys/block/%s/device", dev_name
);
4458 if (tmpsize
== -1 || tmp1
== NULL
) {
4465 tmp1
= NULL
; /* To make free() at the end a NOP */
4470 * Look though all sysfs entries in /sys/block/<dev>/device for
4471 * the enclosure symlink.
4473 while ((ep
= readdir(dp
))) {
4474 /* Ignore everything that's not our enclosure_device link */
4475 if (strstr(ep
->d_name
, "enclosure_device") == NULL
)
4478 if (asprintf(&tmp2
, "%s/%s", tmp1
, ep
->d_name
) == -1 ||
4482 size
= readlink(tmp2
, buf
, sizeof (buf
));
4484 /* Did readlink fail or crop the link name? */
4485 if (size
== -1 || size
>= sizeof (buf
)) {
4487 tmp2
= NULL
; /* To make free() at the end a NOP */
4492 * We got a valid link. readlink() doesn't terminate strings
4493 * so we have to do it.
4498 * Our link will look like:
4500 * "../../../../port-11:1:2/..STUFF../enclosure/1:0:3:0/SLOT 1"
4502 * We want to grab the "enclosure/1:0:3:0/SLOT 1" part
4504 tmp3
= strstr(buf
, "enclosure");
4508 if (asprintf(&path
, "/sys/class/%s", tmp3
) == -1) {
4509 /* If asprintf() fails, 'path' is undefined */