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.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27 * Copyright (c) 2017 Datto Inc.
42 #include <sys/efi_partition.h>
44 #include <sys/zfs_ioctl.h>
47 #include "zfs_namecheck.h"
49 #include "libzfs_impl.h"
50 #include "zfs_comutil.h"
51 #include "zfeature_common.h"
53 static int read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
);
55 typedef struct prop_flags
{
56 int create
:1; /* Validate property on creation */
57 int import
:1; /* Validate property on import */
61 * ====================================================================
62 * zpool property functions
63 * ====================================================================
67 zpool_get_all_props(zpool_handle_t
*zhp
)
69 zfs_cmd_t zc
= {"\0"};
70 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
72 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
74 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
77 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_PROPS
, &zc
) != 0) {
78 if (errno
== ENOMEM
) {
79 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
80 zcmd_free_nvlists(&zc
);
84 zcmd_free_nvlists(&zc
);
89 if (zcmd_read_dst_nvlist(hdl
, &zc
, &zhp
->zpool_props
) != 0) {
90 zcmd_free_nvlists(&zc
);
94 zcmd_free_nvlists(&zc
);
100 zpool_props_refresh(zpool_handle_t
*zhp
)
104 old_props
= zhp
->zpool_props
;
106 if (zpool_get_all_props(zhp
) != 0)
109 nvlist_free(old_props
);
114 zpool_get_prop_string(zpool_handle_t
*zhp
, zpool_prop_t prop
,
120 zprop_source_t source
;
122 nvl
= zhp
->zpool_props
;
123 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
124 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &ival
) == 0);
126 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
128 source
= ZPROP_SRC_DEFAULT
;
129 if ((value
= (char *)zpool_prop_default_string(prop
)) == NULL
)
140 zpool_get_prop_int(zpool_handle_t
*zhp
, zpool_prop_t prop
, zprop_source_t
*src
)
144 zprop_source_t source
;
146 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
)) {
148 * zpool_get_all_props() has most likely failed because
149 * the pool is faulted, but if all we need is the top level
150 * vdev's guid then get it from the zhp config nvlist.
152 if ((prop
== ZPOOL_PROP_GUID
) &&
153 (nvlist_lookup_nvlist(zhp
->zpool_config
,
154 ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0) &&
155 (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
)
159 return (zpool_prop_default_numeric(prop
));
162 nvl
= zhp
->zpool_props
;
163 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
164 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &value
) == 0);
166 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
168 source
= ZPROP_SRC_DEFAULT
;
169 value
= zpool_prop_default_numeric(prop
);
179 * Map VDEV STATE to printed strings.
182 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"));
208 return (gettext("UNKNOWN"));
212 * Map POOL STATE to printed strings.
215 zpool_pool_state_to_name(pool_state_t state
)
220 case POOL_STATE_ACTIVE
:
221 return (gettext("ACTIVE"));
222 case POOL_STATE_EXPORTED
:
223 return (gettext("EXPORTED"));
224 case POOL_STATE_DESTROYED
:
225 return (gettext("DESTROYED"));
226 case POOL_STATE_SPARE
:
227 return (gettext("SPARE"));
228 case POOL_STATE_L2CACHE
:
229 return (gettext("L2CACHE"));
230 case POOL_STATE_UNINITIALIZED
:
231 return (gettext("UNINITIALIZED"));
232 case POOL_STATE_UNAVAIL
:
233 return (gettext("UNAVAIL"));
234 case POOL_STATE_POTENTIALLY_ACTIVE
:
235 return (gettext("POTENTIALLY_ACTIVE"));
238 return (gettext("UNKNOWN"));
242 * Get a zpool property value for 'prop' and return the value in
243 * a pre-allocated buffer.
246 zpool_get_prop(zpool_handle_t
*zhp
, zpool_prop_t prop
, char *buf
,
247 size_t len
, zprop_source_t
*srctype
, boolean_t literal
)
251 zprop_source_t src
= ZPROP_SRC_NONE
;
256 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
258 case ZPOOL_PROP_NAME
:
259 (void) strlcpy(buf
, zpool_get_name(zhp
), len
);
262 case ZPOOL_PROP_HEALTH
:
263 (void) strlcpy(buf
, "FAULTED", len
);
266 case ZPOOL_PROP_GUID
:
267 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
268 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
271 case ZPOOL_PROP_ALTROOT
:
272 case ZPOOL_PROP_CACHEFILE
:
273 case ZPOOL_PROP_COMMENT
:
274 if (zhp
->zpool_props
!= NULL
||
275 zpool_get_all_props(zhp
) == 0) {
277 zpool_get_prop_string(zhp
, prop
, &src
),
283 (void) strlcpy(buf
, "-", len
);
292 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
) &&
293 prop
!= ZPOOL_PROP_NAME
)
296 switch (zpool_prop_get_type(prop
)) {
297 case PROP_TYPE_STRING
:
298 (void) strlcpy(buf
, zpool_get_prop_string(zhp
, prop
, &src
),
302 case PROP_TYPE_NUMBER
:
303 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
306 case ZPOOL_PROP_SIZE
:
307 case ZPOOL_PROP_ALLOCATED
:
308 case ZPOOL_PROP_FREE
:
309 case ZPOOL_PROP_FREEING
:
310 case ZPOOL_PROP_LEAKED
:
311 case ZPOOL_PROP_ASHIFT
:
313 (void) snprintf(buf
, len
, "%llu",
314 (u_longlong_t
)intval
);
316 (void) zfs_nicenum(intval
, buf
, len
);
319 case ZPOOL_PROP_EXPANDSZ
:
321 (void) strlcpy(buf
, "-", len
);
322 } else if (literal
) {
323 (void) snprintf(buf
, len
, "%llu",
324 (u_longlong_t
)intval
);
326 (void) zfs_nicebytes(intval
, buf
, len
);
330 case ZPOOL_PROP_CAPACITY
:
332 (void) snprintf(buf
, len
, "%llu",
333 (u_longlong_t
)intval
);
335 (void) snprintf(buf
, len
, "%llu%%",
336 (u_longlong_t
)intval
);
340 case ZPOOL_PROP_FRAGMENTATION
:
341 if (intval
== UINT64_MAX
) {
342 (void) strlcpy(buf
, "-", len
);
343 } else if (literal
) {
344 (void) snprintf(buf
, len
, "%llu",
345 (u_longlong_t
)intval
);
347 (void) snprintf(buf
, len
, "%llu%%",
348 (u_longlong_t
)intval
);
352 case ZPOOL_PROP_DEDUPRATIO
:
354 (void) snprintf(buf
, len
, "%llu.%02llu",
355 (u_longlong_t
)(intval
/ 100),
356 (u_longlong_t
)(intval
% 100));
358 (void) snprintf(buf
, len
, "%llu.%02llux",
359 (u_longlong_t
)(intval
/ 100),
360 (u_longlong_t
)(intval
% 100));
363 case ZPOOL_PROP_HEALTH
:
364 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
365 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
366 verify(nvlist_lookup_uint64_array(nvroot
,
367 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
370 (void) strlcpy(buf
, zpool_state_to_name(intval
,
373 case ZPOOL_PROP_VERSION
:
374 if (intval
>= SPA_VERSION_FEATURES
) {
375 (void) snprintf(buf
, len
, "-");
380 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
384 case PROP_TYPE_INDEX
:
385 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
386 if (zpool_prop_index_to_string(prop
, intval
, &strval
)
389 (void) strlcpy(buf
, strval
, len
);
403 * Check if the bootfs name has the same pool name as it is set to.
404 * Assuming bootfs is a valid dataset name.
407 bootfs_name_valid(const char *pool
, char *bootfs
)
409 int len
= strlen(pool
);
411 if (!zfs_name_valid(bootfs
, ZFS_TYPE_FILESYSTEM
|ZFS_TYPE_SNAPSHOT
))
414 if (strncmp(pool
, bootfs
, len
) == 0 &&
415 (bootfs
[len
] == '/' || bootfs
[len
] == '\0'))
422 zpool_is_bootable(zpool_handle_t
*zhp
)
424 char bootfs
[ZFS_MAX_DATASET_NAME_LEN
];
426 return (zpool_get_prop(zhp
, ZPOOL_PROP_BOOTFS
, bootfs
,
427 sizeof (bootfs
), NULL
, B_FALSE
) == 0 && strncmp(bootfs
, "-",
428 sizeof (bootfs
)) != 0);
433 * Given an nvlist of zpool properties to be set, validate that they are
434 * correct, and parse any numeric properties (index, boolean, etc) if they are
435 * specified as strings.
438 zpool_valid_proplist(libzfs_handle_t
*hdl
, const char *poolname
,
439 nvlist_t
*props
, uint64_t version
, prop_flags_t flags
, char *errbuf
)
447 struct stat64 statbuf
;
450 if (nvlist_alloc(&retprops
, NV_UNIQUE_NAME
, 0) != 0) {
451 (void) no_memory(hdl
);
456 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
457 const char *propname
= nvpair_name(elem
);
459 prop
= zpool_name_to_prop(propname
);
460 if (prop
== ZPROP_INVAL
&& zpool_prop_feature(propname
)) {
462 char *fname
= strchr(propname
, '@') + 1;
464 err
= zfeature_lookup_name(fname
, NULL
);
466 ASSERT3U(err
, ==, ENOENT
);
467 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
468 "invalid feature '%s'"), fname
);
469 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
473 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
474 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
475 "'%s' must be a string"), propname
);
476 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
480 (void) nvpair_value_string(elem
, &strval
);
481 if (strcmp(strval
, ZFS_FEATURE_ENABLED
) != 0 &&
482 strcmp(strval
, ZFS_FEATURE_DISABLED
) != 0) {
483 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
484 "property '%s' can only be set to "
485 "'enabled' or 'disabled'"), propname
);
486 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
490 if (nvlist_add_uint64(retprops
, propname
, 0) != 0) {
491 (void) no_memory(hdl
);
498 * Make sure this property is valid and applies to this type.
500 if (prop
== ZPROP_INVAL
) {
501 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
502 "invalid property '%s'"), propname
);
503 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
507 if (zpool_prop_readonly(prop
)) {
508 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
509 "is readonly"), propname
);
510 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
514 if (zprop_parse_value(hdl
, elem
, prop
, ZFS_TYPE_POOL
, retprops
,
515 &strval
, &intval
, errbuf
) != 0)
519 * Perform additional checking for specific properties.
522 case ZPOOL_PROP_VERSION
:
523 if (intval
< version
||
524 !SPA_VERSION_IS_SUPPORTED(intval
)) {
525 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
526 "property '%s' number %d is invalid."),
528 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
533 case ZPOOL_PROP_ASHIFT
:
535 (intval
< ASHIFT_MIN
|| intval
> ASHIFT_MAX
)) {
536 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
537 "invalid '%s=%d' property: only values "
538 "between %" PRId32
" and %" PRId32
" "
540 propname
, intval
, ASHIFT_MIN
, ASHIFT_MAX
);
541 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
546 case ZPOOL_PROP_BOOTFS
:
547 if (flags
.create
|| flags
.import
) {
548 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
549 "property '%s' cannot be set at creation "
550 "or import time"), propname
);
551 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
555 if (version
< SPA_VERSION_BOOTFS
) {
556 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
557 "pool must be upgraded to support "
558 "'%s' property"), propname
);
559 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
564 * bootfs property value has to be a dataset name and
565 * the dataset has to be in the same pool as it sets to.
567 if (strval
[0] != '\0' && !bootfs_name_valid(poolname
,
569 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
570 "is an invalid name"), strval
);
571 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
575 if ((zhp
= zpool_open_canfail(hdl
, poolname
)) == NULL
) {
576 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
577 "could not open pool '%s'"), poolname
);
578 (void) zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
);
584 case ZPOOL_PROP_ALTROOT
:
585 if (!flags
.create
&& !flags
.import
) {
586 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
587 "property '%s' can only be set during pool "
588 "creation or import"), propname
);
589 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
593 if (strval
[0] != '/') {
594 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
595 "bad alternate root '%s'"), strval
);
596 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
601 case ZPOOL_PROP_CACHEFILE
:
602 if (strval
[0] == '\0')
605 if (strcmp(strval
, "none") == 0)
608 if (strval
[0] != '/') {
609 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
610 "property '%s' must be empty, an "
611 "absolute path, or 'none'"), propname
);
612 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
616 slash
= strrchr(strval
, '/');
618 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
619 strcmp(slash
, "/..") == 0) {
620 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
621 "'%s' is not a valid file"), strval
);
622 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
628 if (strval
[0] != '\0' &&
629 (stat64(strval
, &statbuf
) != 0 ||
630 !S_ISDIR(statbuf
.st_mode
))) {
631 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
632 "'%s' is not a valid directory"),
634 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
641 case ZPOOL_PROP_COMMENT
:
642 for (check
= strval
; *check
!= '\0'; check
++) {
643 if (!isprint(*check
)) {
645 dgettext(TEXT_DOMAIN
,
646 "comment may only have printable "
648 (void) zfs_error(hdl
, EZFS_BADPROP
,
653 if (strlen(strval
) > ZPROP_MAX_COMMENT
) {
654 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
655 "comment must not exceed %d characters"),
657 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
661 case ZPOOL_PROP_READONLY
:
663 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
664 "property '%s' can only be set at "
665 "import time"), propname
);
666 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
670 case ZPOOL_PROP_TNAME
:
672 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
673 "property '%s' can only be set at "
674 "creation time"), propname
);
675 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
681 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
682 "property '%s'(%d) not defined"), propname
, prop
);
689 nvlist_free(retprops
);
694 * Set zpool property : propname=propval.
697 zpool_set_prop(zpool_handle_t
*zhp
, const char *propname
, const char *propval
)
699 zfs_cmd_t zc
= {"\0"};
702 nvlist_t
*nvl
= NULL
;
705 prop_flags_t flags
= { 0 };
707 (void) snprintf(errbuf
, sizeof (errbuf
),
708 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
711 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
712 return (no_memory(zhp
->zpool_hdl
));
714 if (nvlist_add_string(nvl
, propname
, propval
) != 0) {
716 return (no_memory(zhp
->zpool_hdl
));
719 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
720 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
721 zhp
->zpool_name
, nvl
, version
, flags
, errbuf
)) == NULL
) {
730 * Execute the corresponding ioctl() to set this property.
732 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
734 if (zcmd_write_src_nvlist(zhp
->zpool_hdl
, &zc
, nvl
) != 0) {
739 ret
= zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_SET_PROPS
, &zc
);
741 zcmd_free_nvlists(&zc
);
745 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
747 (void) zpool_props_refresh(zhp
);
753 zpool_expand_proplist(zpool_handle_t
*zhp
, zprop_list_t
**plp
)
755 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
757 char buf
[ZFS_MAXPROPLEN
];
758 nvlist_t
*features
= NULL
;
761 boolean_t firstexpand
= (NULL
== *plp
);
764 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_POOL
) != 0)
768 while (*last
!= NULL
)
769 last
= &(*last
)->pl_next
;
772 features
= zpool_get_features(zhp
);
774 if ((*plp
)->pl_all
&& firstexpand
) {
775 for (i
= 0; i
< SPA_FEATURES
; i
++) {
776 zprop_list_t
*entry
= zfs_alloc(hdl
,
777 sizeof (zprop_list_t
));
778 entry
->pl_prop
= ZPROP_INVAL
;
779 entry
->pl_user_prop
= zfs_asprintf(hdl
, "feature@%s",
780 spa_feature_table
[i
].fi_uname
);
781 entry
->pl_width
= strlen(entry
->pl_user_prop
);
782 entry
->pl_all
= B_TRUE
;
785 last
= &entry
->pl_next
;
789 /* add any unsupported features */
790 for (nvp
= nvlist_next_nvpair(features
, NULL
);
791 nvp
!= NULL
; nvp
= nvlist_next_nvpair(features
, nvp
)) {
796 if (zfeature_is_supported(nvpair_name(nvp
)))
799 propname
= zfs_asprintf(hdl
, "unsupported@%s",
803 * Before adding the property to the list make sure that no
804 * other pool already added the same property.
808 while (entry
!= NULL
) {
809 if (entry
->pl_user_prop
!= NULL
&&
810 strcmp(propname
, entry
->pl_user_prop
) == 0) {
814 entry
= entry
->pl_next
;
821 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
822 entry
->pl_prop
= ZPROP_INVAL
;
823 entry
->pl_user_prop
= propname
;
824 entry
->pl_width
= strlen(entry
->pl_user_prop
);
825 entry
->pl_all
= B_TRUE
;
828 last
= &entry
->pl_next
;
831 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
836 if (entry
->pl_prop
!= ZPROP_INVAL
&&
837 zpool_get_prop(zhp
, entry
->pl_prop
, buf
, sizeof (buf
),
838 NULL
, B_FALSE
) == 0) {
839 if (strlen(buf
) > entry
->pl_width
)
840 entry
->pl_width
= strlen(buf
);
848 * Get the state for the given feature on the given ZFS pool.
851 zpool_prop_get_feature(zpool_handle_t
*zhp
, const char *propname
, char *buf
,
855 boolean_t found
= B_FALSE
;
856 nvlist_t
*features
= zpool_get_features(zhp
);
858 const char *feature
= strchr(propname
, '@') + 1;
860 supported
= zpool_prop_feature(propname
);
861 ASSERT(supported
|| zpool_prop_unsupported(propname
));
864 * Convert from feature name to feature guid. This conversion is
865 * unnecessary for unsupported@... properties because they already
872 ret
= zfeature_lookup_name(feature
, &fid
);
874 (void) strlcpy(buf
, "-", len
);
877 feature
= spa_feature_table
[fid
].fi_guid
;
880 if (nvlist_lookup_uint64(features
, feature
, &refcount
) == 0)
885 (void) strlcpy(buf
, ZFS_FEATURE_DISABLED
, len
);
888 (void) strlcpy(buf
, ZFS_FEATURE_ENABLED
, len
);
890 (void) strlcpy(buf
, ZFS_FEATURE_ACTIVE
, len
);
895 (void) strcpy(buf
, ZFS_UNSUPPORTED_INACTIVE
);
897 (void) strcpy(buf
, ZFS_UNSUPPORTED_READONLY
);
900 (void) strlcpy(buf
, "-", len
);
909 * Don't start the slice at the default block of 34; many storage
910 * devices will use a stripe width of 128k, other vendors prefer a 1m
911 * alignment. It is best to play it safe and ensure a 1m alignment
912 * given 512B blocks. When the block size is larger by a power of 2
913 * we will still be 1m aligned. Some devices are sensitive to the
914 * partition ending alignment as well.
916 #define NEW_START_BLOCK 2048
917 #define PARTITION_END_ALIGNMENT 2048
920 * Validate the given pool name, optionally putting an extended error message in
924 zpool_name_valid(libzfs_handle_t
*hdl
, boolean_t isopen
, const char *pool
)
930 ret
= pool_namecheck(pool
, &why
, &what
);
933 * The rules for reserved pool names were extended at a later point.
934 * But we need to support users with existing pools that may now be
935 * invalid. So we only check for this expanded set of names during a
936 * create (or import), and only in userland.
938 if (ret
== 0 && !isopen
&&
939 (strncmp(pool
, "mirror", 6) == 0 ||
940 strncmp(pool
, "raidz", 5) == 0 ||
941 strncmp(pool
, "spare", 5) == 0 ||
942 strcmp(pool
, "log") == 0)) {
945 dgettext(TEXT_DOMAIN
, "name is reserved"));
953 case NAME_ERR_TOOLONG
:
955 dgettext(TEXT_DOMAIN
, "name is too long"));
958 case NAME_ERR_INVALCHAR
:
960 dgettext(TEXT_DOMAIN
, "invalid character "
961 "'%c' in pool name"), what
);
964 case NAME_ERR_NOLETTER
:
965 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
966 "name must begin with a letter"));
969 case NAME_ERR_RESERVED
:
970 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
971 "name is reserved"));
974 case NAME_ERR_DISKLIKE
:
975 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
976 "pool name is reserved"));
979 case NAME_ERR_LEADING_SLASH
:
980 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
981 "leading slash in name"));
984 case NAME_ERR_EMPTY_COMPONENT
:
985 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
986 "empty component in name"));
989 case NAME_ERR_TRAILING_SLASH
:
990 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
991 "trailing slash in name"));
994 case NAME_ERR_MULTIPLE_DELIMITERS
:
995 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
996 "multiple '@' and/or '#' delimiters in "
1000 case NAME_ERR_NO_AT
:
1001 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1002 "permission set is missing '@'"));
1006 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1007 "(%d) not defined"), why
);
1018 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1022 zpool_open_canfail(libzfs_handle_t
*hdl
, const char *pool
)
1024 zpool_handle_t
*zhp
;
1028 * Make sure the pool name is valid.
1030 if (!zpool_name_valid(hdl
, B_TRUE
, pool
)) {
1031 (void) zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1032 dgettext(TEXT_DOMAIN
, "cannot open '%s'"),
1037 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
1040 zhp
->zpool_hdl
= hdl
;
1041 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1043 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1049 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "no such pool"));
1050 (void) zfs_error_fmt(hdl
, EZFS_NOENT
,
1051 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), pool
);
1060 * Like the above, but silent on error. Used when iterating over pools (because
1061 * the configuration cache may be out of date).
1064 zpool_open_silent(libzfs_handle_t
*hdl
, const char *pool
, zpool_handle_t
**ret
)
1066 zpool_handle_t
*zhp
;
1069 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
1072 zhp
->zpool_hdl
= hdl
;
1073 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1075 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1091 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1095 zpool_open(libzfs_handle_t
*hdl
, const char *pool
)
1097 zpool_handle_t
*zhp
;
1099 if ((zhp
= zpool_open_canfail(hdl
, pool
)) == NULL
)
1102 if (zhp
->zpool_state
== POOL_STATE_UNAVAIL
) {
1103 (void) zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
1104 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), zhp
->zpool_name
);
1113 * Close the handle. Simply frees the memory associated with the handle.
1116 zpool_close(zpool_handle_t
*zhp
)
1118 nvlist_free(zhp
->zpool_config
);
1119 nvlist_free(zhp
->zpool_old_config
);
1120 nvlist_free(zhp
->zpool_props
);
1125 * Return the name of the pool.
1128 zpool_get_name(zpool_handle_t
*zhp
)
1130 return (zhp
->zpool_name
);
1135 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1138 zpool_get_state(zpool_handle_t
*zhp
)
1140 return (zhp
->zpool_state
);
1144 * Create the named pool, using the provided vdev list. It is assumed
1145 * that the consumer has already validated the contents of the nvlist, so we
1146 * don't have to worry about error semantics.
1149 zpool_create(libzfs_handle_t
*hdl
, const char *pool
, nvlist_t
*nvroot
,
1150 nvlist_t
*props
, nvlist_t
*fsprops
)
1152 zfs_cmd_t zc
= {"\0"};
1153 nvlist_t
*zc_fsprops
= NULL
;
1154 nvlist_t
*zc_props
= NULL
;
1158 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1159 "cannot create '%s'"), pool
);
1161 if (!zpool_name_valid(hdl
, B_FALSE
, pool
))
1162 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
1164 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1168 prop_flags_t flags
= { .create
= B_TRUE
, .import
= B_FALSE
};
1170 if ((zc_props
= zpool_valid_proplist(hdl
, pool
, props
,
1171 SPA_VERSION_1
, flags
, msg
)) == NULL
) {
1180 zoned
= ((nvlist_lookup_string(fsprops
,
1181 zfs_prop_to_name(ZFS_PROP_ZONED
), &zonestr
) == 0) &&
1182 strcmp(zonestr
, "on") == 0);
1184 if ((zc_fsprops
= zfs_valid_proplist(hdl
, ZFS_TYPE_FILESYSTEM
,
1185 fsprops
, zoned
, NULL
, NULL
, msg
)) == NULL
) {
1189 (nvlist_alloc(&zc_props
, NV_UNIQUE_NAME
, 0) != 0)) {
1192 if (nvlist_add_nvlist(zc_props
,
1193 ZPOOL_ROOTFS_PROPS
, zc_fsprops
) != 0) {
1198 if (zc_props
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
1201 (void) strlcpy(zc
.zc_name
, pool
, sizeof (zc
.zc_name
));
1203 if ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_CREATE
, &zc
)) != 0) {
1205 zcmd_free_nvlists(&zc
);
1206 nvlist_free(zc_props
);
1207 nvlist_free(zc_fsprops
);
1212 * This can happen if the user has specified the same
1213 * device multiple times. We can't reliably detect this
1214 * until we try to add it and see we already have a
1215 * label. This can also happen under if the device is
1216 * part of an active md or lvm device.
1218 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1219 "one or more vdevs refer to the same device, or "
1220 "one of\nthe devices is part of an active md or "
1222 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1226 * This happens if the record size is smaller or larger
1227 * than the allowed size range, or not a power of 2.
1229 * NOTE: although zfs_valid_proplist is called earlier,
1230 * this case may have slipped through since the
1231 * pool does not exist yet and it is therefore
1232 * impossible to read properties e.g. max blocksize
1235 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1236 "record size invalid"));
1237 return (zfs_error(hdl
, EZFS_BADPROP
, msg
));
1241 * This occurs when one of the devices is below
1242 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1243 * device was the problem device since there's no
1244 * reliable way to determine device size from userland.
1249 zfs_nicebytes(SPA_MINDEVSIZE
, buf
,
1252 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1253 "one or more devices is less than the "
1254 "minimum size (%s)"), buf
);
1256 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1259 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1260 "one or more devices is out of space"));
1261 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1264 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1265 "cache device must be a disk or disk slice"));
1266 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1269 return (zpool_standard_error(hdl
, errno
, msg
));
1274 zcmd_free_nvlists(&zc
);
1275 nvlist_free(zc_props
);
1276 nvlist_free(zc_fsprops
);
1281 * Destroy the given pool. It is up to the caller to ensure that there are no
1282 * datasets left in the pool.
1285 zpool_destroy(zpool_handle_t
*zhp
, const char *log_str
)
1287 zfs_cmd_t zc
= {"\0"};
1288 zfs_handle_t
*zfp
= NULL
;
1289 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1292 if (zhp
->zpool_state
== POOL_STATE_ACTIVE
&&
1293 (zfp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
1296 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1297 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1299 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_DESTROY
, &zc
) != 0) {
1300 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1301 "cannot destroy '%s'"), zhp
->zpool_name
);
1303 if (errno
== EROFS
) {
1304 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1305 "one or more devices is read only"));
1306 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1308 (void) zpool_standard_error(hdl
, errno
, msg
);
1317 remove_mountpoint(zfp
);
1325 * Add the given vdevs to the pool. The caller must have already performed the
1326 * necessary verification to ensure that the vdev specification is well-formed.
1329 zpool_add(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
1331 zfs_cmd_t zc
= {"\0"};
1333 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1335 nvlist_t
**spares
, **l2cache
;
1336 uint_t nspares
, nl2cache
;
1338 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1339 "cannot add to '%s'"), zhp
->zpool_name
);
1341 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1342 SPA_VERSION_SPARES
&&
1343 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1344 &spares
, &nspares
) == 0) {
1345 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1346 "upgraded to add hot spares"));
1347 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1350 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1351 SPA_VERSION_L2CACHE
&&
1352 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1353 &l2cache
, &nl2cache
) == 0) {
1354 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1355 "upgraded to add cache devices"));
1356 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1359 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1361 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1363 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_ADD
, &zc
) != 0) {
1367 * This can happen if the user has specified the same
1368 * device multiple times. We can't reliably detect this
1369 * until we try to add it and see we already have a
1372 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1373 "one or more vdevs refer to the same device"));
1374 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1379 * This occurrs when one of the devices is below
1380 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1381 * device was the problem device since there's no
1382 * reliable way to determine device size from userland.
1387 zfs_nicebytes(SPA_MINDEVSIZE
, buf
,
1390 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1391 "device is less than the minimum "
1394 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1398 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1399 "pool must be upgraded to add these vdevs"));
1400 (void) zfs_error(hdl
, EZFS_BADVERSION
, msg
);
1404 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1405 "cache device must be a disk or disk slice"));
1406 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1410 (void) zpool_standard_error(hdl
, errno
, msg
);
1418 zcmd_free_nvlists(&zc
);
1424 * Exports the pool from the system. The caller must ensure that there are no
1425 * mounted datasets in the pool.
1428 zpool_export_common(zpool_handle_t
*zhp
, boolean_t force
, boolean_t hardforce
,
1429 const char *log_str
)
1431 zfs_cmd_t zc
= {"\0"};
1434 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1435 "cannot export '%s'"), zhp
->zpool_name
);
1437 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1438 zc
.zc_cookie
= force
;
1439 zc
.zc_guid
= hardforce
;
1440 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1442 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_EXPORT
, &zc
) != 0) {
1445 zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
1446 "use '-f' to override the following errors:\n"
1447 "'%s' has an active shared spare which could be"
1448 " used by other pools once '%s' is exported."),
1449 zhp
->zpool_name
, zhp
->zpool_name
);
1450 return (zfs_error(zhp
->zpool_hdl
, EZFS_ACTIVE_SPARE
,
1453 return (zpool_standard_error_fmt(zhp
->zpool_hdl
, errno
,
1462 zpool_export(zpool_handle_t
*zhp
, boolean_t force
, const char *log_str
)
1464 return (zpool_export_common(zhp
, force
, B_FALSE
, log_str
));
1468 zpool_export_force(zpool_handle_t
*zhp
, const char *log_str
)
1470 return (zpool_export_common(zhp
, B_TRUE
, B_TRUE
, log_str
));
1474 zpool_rewind_exclaim(libzfs_handle_t
*hdl
, const char *name
, boolean_t dryrun
,
1477 nvlist_t
*nv
= NULL
;
1483 if (!hdl
->libzfs_printerr
|| config
== NULL
)
1486 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1487 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0) {
1491 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1493 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1495 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1496 strftime(timestr
, 128, "%c", &t
) != 0) {
1498 (void) printf(dgettext(TEXT_DOMAIN
,
1499 "Would be able to return %s "
1500 "to its state as of %s.\n"),
1503 (void) printf(dgettext(TEXT_DOMAIN
,
1504 "Pool %s returned to its state as of %s.\n"),
1508 (void) printf(dgettext(TEXT_DOMAIN
,
1509 "%s approximately %lld "),
1510 dryrun
? "Would discard" : "Discarded",
1511 ((longlong_t
)loss
+ 30) / 60);
1512 (void) printf(dgettext(TEXT_DOMAIN
,
1513 "minutes of transactions.\n"));
1514 } else if (loss
> 0) {
1515 (void) printf(dgettext(TEXT_DOMAIN
,
1516 "%s approximately %lld "),
1517 dryrun
? "Would discard" : "Discarded",
1519 (void) printf(dgettext(TEXT_DOMAIN
,
1520 "seconds of transactions.\n"));
1526 zpool_explain_recover(libzfs_handle_t
*hdl
, const char *name
, int reason
,
1529 nvlist_t
*nv
= NULL
;
1531 uint64_t edata
= UINT64_MAX
;
1536 if (!hdl
->libzfs_printerr
)
1540 (void) printf(dgettext(TEXT_DOMAIN
, "action: "));
1542 (void) printf(dgettext(TEXT_DOMAIN
, "\t"));
1544 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1545 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1546 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0 ||
1547 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1550 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1551 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_DATA_ERRORS
,
1554 (void) printf(dgettext(TEXT_DOMAIN
,
1555 "Recovery is possible, but will result in some data loss.\n"));
1557 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1558 strftime(timestr
, 128, "%c", &t
) != 0) {
1559 (void) printf(dgettext(TEXT_DOMAIN
,
1560 "\tReturning the pool to its state as of %s\n"
1561 "\tshould correct the problem. "),
1564 (void) printf(dgettext(TEXT_DOMAIN
,
1565 "\tReverting the pool to an earlier state "
1566 "should correct the problem.\n\t"));
1570 (void) printf(dgettext(TEXT_DOMAIN
,
1571 "Approximately %lld minutes of data\n"
1572 "\tmust be discarded, irreversibly. "),
1573 ((longlong_t
)loss
+ 30) / 60);
1574 } else if (loss
> 0) {
1575 (void) printf(dgettext(TEXT_DOMAIN
,
1576 "Approximately %lld seconds of data\n"
1577 "\tmust be discarded, irreversibly. "),
1580 if (edata
!= 0 && edata
!= UINT64_MAX
) {
1582 (void) printf(dgettext(TEXT_DOMAIN
,
1583 "After rewind, at least\n"
1584 "\tone persistent user-data error will remain. "));
1586 (void) printf(dgettext(TEXT_DOMAIN
,
1587 "After rewind, several\n"
1588 "\tpersistent user-data errors will remain. "));
1591 (void) printf(dgettext(TEXT_DOMAIN
,
1592 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1593 reason
>= 0 ? "clear" : "import", name
);
1595 (void) printf(dgettext(TEXT_DOMAIN
,
1596 "A scrub of the pool\n"
1597 "\tis strongly recommended after recovery.\n"));
1601 (void) printf(dgettext(TEXT_DOMAIN
,
1602 "Destroy and re-create the pool from\n\ta backup source.\n"));
1606 * zpool_import() is a contracted interface. Should be kept the same
1609 * Applications should use zpool_import_props() to import a pool with
1610 * new properties value to be set.
1613 zpool_import(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1616 nvlist_t
*props
= NULL
;
1619 if (altroot
!= NULL
) {
1620 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
1621 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1622 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1626 if (nvlist_add_string(props
,
1627 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), altroot
) != 0 ||
1628 nvlist_add_string(props
,
1629 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
), "none") != 0) {
1631 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1632 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1637 ret
= zpool_import_props(hdl
, config
, newname
, props
,
1644 print_vdev_tree(libzfs_handle_t
*hdl
, const char *name
, nvlist_t
*nv
,
1650 uint64_t is_log
= 0;
1652 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
,
1656 (void) printf("\t%*s%s%s\n", indent
, "", name
,
1657 is_log
? " [log]" : "");
1659 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1660 &child
, &children
) != 0)
1663 for (c
= 0; c
< children
; c
++) {
1664 vname
= zpool_vdev_name(hdl
, NULL
, child
[c
], VDEV_NAME_TYPE_ID
);
1665 print_vdev_tree(hdl
, vname
, child
[c
], indent
+ 2);
1671 zpool_print_unsup_feat(nvlist_t
*config
)
1673 nvlist_t
*nvinfo
, *unsup_feat
;
1676 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) ==
1678 verify(nvlist_lookup_nvlist(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
,
1681 for (nvp
= nvlist_next_nvpair(unsup_feat
, NULL
); nvp
!= NULL
;
1682 nvp
= nvlist_next_nvpair(unsup_feat
, nvp
)) {
1685 verify(nvpair_type(nvp
) == DATA_TYPE_STRING
);
1686 verify(nvpair_value_string(nvp
, &desc
) == 0);
1688 if (strlen(desc
) > 0)
1689 (void) printf("\t%s (%s)\n", nvpair_name(nvp
), desc
);
1691 (void) printf("\t%s\n", nvpair_name(nvp
));
1696 * Import the given pool using the known configuration and a list of
1697 * properties to be set. The configuration should have come from
1698 * zpool_find_import(). The 'newname' parameters control whether the pool
1699 * is imported with a different name.
1702 zpool_import_props(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1703 nvlist_t
*props
, int flags
)
1705 zfs_cmd_t zc
= {"\0"};
1706 zpool_rewind_policy_t policy
;
1707 nvlist_t
*nv
= NULL
;
1708 nvlist_t
*nvinfo
= NULL
;
1709 nvlist_t
*missing
= NULL
;
1716 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1719 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1720 "cannot import pool '%s'"), origname
);
1722 if (newname
!= NULL
) {
1723 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
1724 return (zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1725 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1727 thename
= (char *)newname
;
1732 if (props
!= NULL
) {
1734 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
1736 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
1739 if ((props
= zpool_valid_proplist(hdl
, origname
,
1740 props
, version
, flags
, errbuf
)) == NULL
)
1742 if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
1749 (void) strlcpy(zc
.zc_name
, thename
, sizeof (zc
.zc_name
));
1751 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1754 if (zcmd_write_conf_nvlist(hdl
, &zc
, config
) != 0) {
1755 zcmd_free_nvlists(&zc
);
1758 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zc
.zc_nvlist_conf_size
* 2) != 0) {
1759 zcmd_free_nvlists(&zc
);
1763 zc
.zc_cookie
= flags
;
1764 while ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_IMPORT
, &zc
)) != 0 &&
1766 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
1767 zcmd_free_nvlists(&zc
);
1774 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nv
);
1776 zcmd_free_nvlists(&zc
);
1778 zpool_get_rewind_policy(config
, &policy
);
1784 * Dry-run failed, but we print out what success
1785 * looks like if we found a best txg
1787 if (policy
.zrp_request
& ZPOOL_TRY_REWIND
) {
1788 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1794 if (newname
== NULL
)
1795 (void) snprintf(desc
, sizeof (desc
),
1796 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1799 (void) snprintf(desc
, sizeof (desc
),
1800 dgettext(TEXT_DOMAIN
, "cannot import '%s' as '%s'"),
1805 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
1806 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1807 nvlist_exists(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
)) {
1808 (void) printf(dgettext(TEXT_DOMAIN
, "This "
1809 "pool uses the following feature(s) not "
1810 "supported by this system:\n"));
1811 zpool_print_unsup_feat(nv
);
1812 if (nvlist_exists(nvinfo
,
1813 ZPOOL_CONFIG_CAN_RDONLY
)) {
1814 (void) printf(dgettext(TEXT_DOMAIN
,
1815 "All unsupported features are only "
1816 "required for writing to the pool."
1817 "\nThe pool can be imported using "
1818 "'-o readonly=on'.\n"));
1822 * Unsupported version.
1824 (void) zfs_error(hdl
, EZFS_BADVERSION
, desc
);
1828 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, desc
);
1832 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1833 "one or more devices is read only"));
1834 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
1838 if (nv
&& nvlist_lookup_nvlist(nv
,
1839 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1840 nvlist_lookup_nvlist(nvinfo
,
1841 ZPOOL_CONFIG_MISSING_DEVICES
, &missing
) == 0) {
1842 (void) printf(dgettext(TEXT_DOMAIN
,
1843 "The devices below are missing, use "
1844 "'-m' to import the pool anyway:\n"));
1845 print_vdev_tree(hdl
, NULL
, missing
, 2);
1846 (void) printf("\n");
1848 (void) zpool_standard_error(hdl
, error
, desc
);
1852 (void) zpool_standard_error(hdl
, error
, desc
);
1856 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1857 "one or more devices are already in use\n"));
1858 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
1861 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1862 "new name of at least one dataset is longer than "
1863 "the maximum allowable length"));
1864 (void) zfs_error(hdl
, EZFS_NAMETOOLONG
, desc
);
1867 (void) zpool_standard_error(hdl
, error
, desc
);
1868 zpool_explain_recover(hdl
,
1869 newname
? origname
: thename
, -error
, nv
);
1876 zpool_handle_t
*zhp
;
1879 * This should never fail, but play it safe anyway.
1881 if (zpool_open_silent(hdl
, thename
, &zhp
) != 0)
1883 else if (zhp
!= NULL
)
1885 if (policy
.zrp_request
&
1886 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
1887 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1888 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0), nv
);
1901 zpool_scan(zpool_handle_t
*zhp
, pool_scan_func_t func
)
1903 zfs_cmd_t zc
= {"\0"};
1905 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1907 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1908 zc
.zc_cookie
= func
;
1910 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_SCAN
, &zc
) == 0 ||
1911 (errno
== ENOENT
&& func
!= POOL_SCAN_NONE
))
1914 if (func
== POOL_SCAN_SCRUB
) {
1915 (void) snprintf(msg
, sizeof (msg
),
1916 dgettext(TEXT_DOMAIN
, "cannot scrub %s"), zc
.zc_name
);
1917 } else if (func
== POOL_SCAN_NONE
) {
1918 (void) snprintf(msg
, sizeof (msg
),
1919 dgettext(TEXT_DOMAIN
, "cannot cancel scrubbing %s"),
1922 assert(!"unexpected result");
1925 if (errno
== EBUSY
) {
1927 pool_scan_stat_t
*ps
= NULL
;
1930 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
1931 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
1932 (void) nvlist_lookup_uint64_array(nvroot
,
1933 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &psc
);
1934 if (ps
&& ps
->pss_func
== POOL_SCAN_SCRUB
)
1935 return (zfs_error(hdl
, EZFS_SCRUBBING
, msg
));
1937 return (zfs_error(hdl
, EZFS_RESILVERING
, msg
));
1938 } else if (errno
== ENOENT
) {
1939 return (zfs_error(hdl
, EZFS_NO_SCRUB
, msg
));
1941 return (zpool_standard_error(hdl
, errno
, msg
));
1946 * Find a vdev that matches the search criteria specified. We use the
1947 * the nvpair name to determine how we should look for the device.
1948 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1949 * spare; but FALSE if its an INUSE spare.
1952 vdev_to_nvlist_iter(nvlist_t
*nv
, nvlist_t
*search
, boolean_t
*avail_spare
,
1953 boolean_t
*l2cache
, boolean_t
*log
)
1960 nvpair_t
*pair
= nvlist_next_nvpair(search
, NULL
);
1962 /* Nothing to look for */
1963 if (search
== NULL
|| pair
== NULL
)
1966 /* Obtain the key we will use to search */
1967 srchkey
= nvpair_name(pair
);
1969 switch (nvpair_type(pair
)) {
1970 case DATA_TYPE_UINT64
:
1971 if (strcmp(srchkey
, ZPOOL_CONFIG_GUID
) == 0) {
1972 uint64_t srchval
, theguid
;
1974 verify(nvpair_value_uint64(pair
, &srchval
) == 0);
1975 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1977 if (theguid
== srchval
)
1982 case DATA_TYPE_STRING
: {
1983 char *srchval
, *val
;
1985 verify(nvpair_value_string(pair
, &srchval
) == 0);
1986 if (nvlist_lookup_string(nv
, srchkey
, &val
) != 0)
1990 * Search for the requested value. Special cases:
1992 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
1993 * "-part1", or "p1". The suffix is hidden from the user,
1994 * but included in the string, so this matches around it.
1995 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
1996 * is used to check all possible expanded paths.
1997 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
1999 * Otherwise, all other searches are simple string compares.
2001 if (strcmp(srchkey
, ZPOOL_CONFIG_PATH
) == 0) {
2002 uint64_t wholedisk
= 0;
2004 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
2006 if (zfs_strcmp_pathname(srchval
, val
, wholedisk
) == 0)
2009 } else if (strcmp(srchkey
, ZPOOL_CONFIG_TYPE
) == 0 && val
) {
2010 char *type
, *idx
, *end
, *p
;
2011 uint64_t id
, vdev_id
;
2014 * Determine our vdev type, keeping in mind
2015 * that the srchval is composed of a type and
2016 * vdev id pair (i.e. mirror-4).
2018 if ((type
= strdup(srchval
)) == NULL
)
2021 if ((p
= strrchr(type
, '-')) == NULL
) {
2029 * If the types don't match then keep looking.
2031 if (strncmp(val
, type
, strlen(val
)) != 0) {
2036 verify(strncmp(type
, VDEV_TYPE_RAIDZ
,
2037 strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2038 strncmp(type
, VDEV_TYPE_MIRROR
,
2039 strlen(VDEV_TYPE_MIRROR
)) == 0);
2040 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
2044 vdev_id
= strtoull(idx
, &end
, 10);
2051 * Now verify that we have the correct vdev id.
2060 if (strcmp(srchval
, val
) == 0)
2069 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2070 &child
, &children
) != 0)
2073 for (c
= 0; c
< children
; c
++) {
2074 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2075 avail_spare
, l2cache
, NULL
)) != NULL
) {
2077 * The 'is_log' value is only set for the toplevel
2078 * vdev, not the leaf vdevs. So we always lookup the
2079 * log device from the root of the vdev tree (where
2080 * 'log' is non-NULL).
2083 nvlist_lookup_uint64(child
[c
],
2084 ZPOOL_CONFIG_IS_LOG
, &is_log
) == 0 &&
2092 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2093 &child
, &children
) == 0) {
2094 for (c
= 0; c
< children
; c
++) {
2095 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2096 avail_spare
, l2cache
, NULL
)) != NULL
) {
2097 *avail_spare
= B_TRUE
;
2103 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2104 &child
, &children
) == 0) {
2105 for (c
= 0; c
< children
; c
++) {
2106 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2107 avail_spare
, l2cache
, NULL
)) != NULL
) {
2118 * Given a physical path (minus the "/devices" prefix), find the
2122 zpool_find_vdev_by_physpath(zpool_handle_t
*zhp
, const char *ppath
,
2123 boolean_t
*avail_spare
, boolean_t
*l2cache
, boolean_t
*log
)
2125 nvlist_t
*search
, *nvroot
, *ret
;
2127 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2128 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PHYS_PATH
, ppath
) == 0);
2130 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2133 *avail_spare
= B_FALSE
;
2137 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2138 nvlist_free(search
);
2144 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2147 zpool_vdev_is_interior(const char *name
)
2149 if (strncmp(name
, VDEV_TYPE_RAIDZ
, strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2150 strncmp(name
, VDEV_TYPE_MIRROR
, strlen(VDEV_TYPE_MIRROR
)) == 0)
2156 zpool_find_vdev(zpool_handle_t
*zhp
, const char *path
, boolean_t
*avail_spare
,
2157 boolean_t
*l2cache
, boolean_t
*log
)
2160 nvlist_t
*nvroot
, *search
, *ret
;
2163 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2165 guid
= strtoull(path
, &end
, 0);
2166 if (guid
!= 0 && *end
== '\0') {
2167 verify(nvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
) == 0);
2168 } else if (zpool_vdev_is_interior(path
)) {
2169 verify(nvlist_add_string(search
, ZPOOL_CONFIG_TYPE
, path
) == 0);
2171 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PATH
, path
) == 0);
2174 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2177 *avail_spare
= B_FALSE
;
2181 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2182 nvlist_free(search
);
2188 vdev_online(nvlist_t
*nv
)
2192 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
, &ival
) == 0 ||
2193 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
, &ival
) == 0 ||
2194 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
, &ival
) == 0)
2201 * Helper function for zpool_get_physpaths().
2204 vdev_get_one_physpath(nvlist_t
*config
, char *physpath
, size_t physpath_size
,
2205 size_t *bytes_written
)
2207 size_t bytes_left
, pos
, rsz
;
2211 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PHYS_PATH
,
2213 return (EZFS_NODEVICE
);
2215 pos
= *bytes_written
;
2216 bytes_left
= physpath_size
- pos
;
2217 format
= (pos
== 0) ? "%s" : " %s";
2219 rsz
= snprintf(physpath
+ pos
, bytes_left
, format
, tmppath
);
2220 *bytes_written
+= rsz
;
2222 if (rsz
>= bytes_left
) {
2223 /* if physpath was not copied properly, clear it */
2224 if (bytes_left
!= 0) {
2227 return (EZFS_NOSPC
);
2233 vdev_get_physpaths(nvlist_t
*nv
, char *physpath
, size_t phypath_size
,
2234 size_t *rsz
, boolean_t is_spare
)
2239 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
2240 return (EZFS_INVALCONFIG
);
2242 if (strcmp(type
, VDEV_TYPE_DISK
) == 0) {
2244 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2245 * For a spare vdev, we only want to boot from the active
2250 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
2253 return (EZFS_INVALCONFIG
);
2256 if (vdev_online(nv
)) {
2257 if ((ret
= vdev_get_one_physpath(nv
, physpath
,
2258 phypath_size
, rsz
)) != 0)
2261 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) == 0 ||
2262 strcmp(type
, VDEV_TYPE_RAIDZ
) == 0 ||
2263 strcmp(type
, VDEV_TYPE_REPLACING
) == 0 ||
2264 (is_spare
= (strcmp(type
, VDEV_TYPE_SPARE
) == 0))) {
2269 if (nvlist_lookup_nvlist_array(nv
,
2270 ZPOOL_CONFIG_CHILDREN
, &child
, &count
) != 0)
2271 return (EZFS_INVALCONFIG
);
2273 for (i
= 0; i
< count
; i
++) {
2274 ret
= vdev_get_physpaths(child
[i
], physpath
,
2275 phypath_size
, rsz
, is_spare
);
2276 if (ret
== EZFS_NOSPC
)
2281 return (EZFS_POOL_INVALARG
);
2285 * Get phys_path for a root pool config.
2286 * Return 0 on success; non-zero on failure.
2289 zpool_get_config_physpath(nvlist_t
*config
, char *physpath
, size_t phypath_size
)
2292 nvlist_t
*vdev_root
;
2299 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2301 return (EZFS_INVALCONFIG
);
2303 if (nvlist_lookup_string(vdev_root
, ZPOOL_CONFIG_TYPE
, &type
) != 0 ||
2304 nvlist_lookup_nvlist_array(vdev_root
, ZPOOL_CONFIG_CHILDREN
,
2305 &child
, &count
) != 0)
2306 return (EZFS_INVALCONFIG
);
2309 * root pool can only have a single top-level vdev.
2311 if (strcmp(type
, VDEV_TYPE_ROOT
) != 0 || count
!= 1)
2312 return (EZFS_POOL_INVALARG
);
2314 (void) vdev_get_physpaths(child
[0], physpath
, phypath_size
, &rsz
,
2317 /* No online devices */
2319 return (EZFS_NODEVICE
);
2325 * Get phys_path for a root pool
2326 * Return 0 on success; non-zero on failure.
2329 zpool_get_physpath(zpool_handle_t
*zhp
, char *physpath
, size_t phypath_size
)
2331 return (zpool_get_config_physpath(zhp
->zpool_config
, physpath
,
2336 * If the device has being dynamically expanded then we need to relabel
2337 * the disk to use the new unallocated space.
2340 zpool_relabel_disk(libzfs_handle_t
*hdl
, const char *path
, const char *msg
)
2344 if ((fd
= open(path
, O_RDWR
|O_DIRECT
)) < 0) {
2345 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2346 "relabel '%s': unable to open device: %d"), path
, errno
);
2347 return (zfs_error(hdl
, EZFS_OPENFAILED
, msg
));
2351 * It's possible that we might encounter an error if the device
2352 * does not have any unallocated space left. If so, we simply
2353 * ignore that error and continue on.
2355 * Also, we don't call efi_rescan() - that would just return EBUSY.
2356 * The module will do it for us in vdev_disk_open().
2358 error
= efi_use_whole_disk(fd
);
2360 /* Flush the buffers to disk and invalidate the page cache. */
2362 (void) ioctl(fd
, BLKFLSBUF
);
2365 if (error
&& error
!= VT_ENOSPC
) {
2366 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2367 "relabel '%s': unable to read disk capacity"), path
);
2368 return (zfs_error(hdl
, EZFS_NOCAP
, msg
));
2375 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
2377 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
2378 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
2382 zpool_vdev_path_to_guid_impl(zpool_handle_t
*zhp
, const char *path
,
2383 boolean_t
*is_spare
, boolean_t
*is_l2cache
, boolean_t
*is_log
)
2386 boolean_t spare
= B_FALSE
, l2cache
= B_FALSE
, log
= B_FALSE
;
2389 if ((tgt
= zpool_find_vdev(zhp
, path
, &spare
, &l2cache
,
2393 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &guid
) == 0);
2394 if (is_spare
!= NULL
)
2396 if (is_l2cache
!= NULL
)
2397 *is_l2cache
= l2cache
;
2404 /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
2406 zpool_vdev_path_to_guid(zpool_handle_t
*zhp
, const char *path
)
2408 return (zpool_vdev_path_to_guid_impl(zhp
, path
, NULL
, NULL
, NULL
));
2412 * Bring the specified vdev online. The 'flags' parameter is a set of the
2413 * ZFS_ONLINE_* flags.
2416 zpool_vdev_online(zpool_handle_t
*zhp
, const char *path
, int flags
,
2417 vdev_state_t
*newstate
)
2419 zfs_cmd_t zc
= {"\0"};
2422 boolean_t avail_spare
, l2cache
, islog
;
2423 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2426 if (flags
& ZFS_ONLINE_EXPAND
) {
2427 (void) snprintf(msg
, sizeof (msg
),
2428 dgettext(TEXT_DOMAIN
, "cannot expand %s"), path
);
2430 (void) snprintf(msg
, sizeof (msg
),
2431 dgettext(TEXT_DOMAIN
, "cannot online %s"), path
);
2434 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2435 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2437 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2439 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2442 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2444 if (flags
& ZFS_ONLINE_EXPAND
||
2445 zpool_get_prop_int(zhp
, ZPOOL_PROP_AUTOEXPAND
, NULL
)) {
2446 uint64_t wholedisk
= 0;
2448 (void) nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_WHOLE_DISK
,
2452 * XXX - L2ARC 1.0 devices can't support expansion.
2455 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2456 "cannot expand cache devices"));
2457 return (zfs_error(hdl
, EZFS_VDEVNOTSUP
, msg
));
2461 const char *fullpath
= path
;
2462 char buf
[MAXPATHLEN
];
2464 if (path
[0] != '/') {
2465 error
= zfs_resolve_shortname(path
, buf
,
2468 return (zfs_error(hdl
, EZFS_NODEVICE
,
2474 error
= zpool_relabel_disk(hdl
, fullpath
, msg
);
2480 zc
.zc_cookie
= VDEV_STATE_ONLINE
;
2483 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) != 0) {
2484 if (errno
== EINVAL
) {
2485 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "was split "
2486 "from this pool into a new one. Use '%s' "
2487 "instead"), "zpool detach");
2488 return (zfs_error(hdl
, EZFS_POSTSPLIT_ONLINE
, msg
));
2490 return (zpool_standard_error(hdl
, errno
, msg
));
2493 *newstate
= zc
.zc_cookie
;
2498 * Take the specified vdev offline
2501 zpool_vdev_offline(zpool_handle_t
*zhp
, const char *path
, boolean_t istmp
)
2503 zfs_cmd_t zc
= {"\0"};
2506 boolean_t avail_spare
, l2cache
;
2507 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2509 (void) snprintf(msg
, sizeof (msg
),
2510 dgettext(TEXT_DOMAIN
, "cannot offline %s"), path
);
2512 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2513 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2515 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2517 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2520 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2522 zc
.zc_cookie
= VDEV_STATE_OFFLINE
;
2523 zc
.zc_obj
= istmp
? ZFS_OFFLINE_TEMPORARY
: 0;
2525 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2532 * There are no other replicas of this device.
2534 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2538 * The log device has unplayed logs
2540 return (zfs_error(hdl
, EZFS_UNPLAYED_LOGS
, msg
));
2543 return (zpool_standard_error(hdl
, errno
, msg
));
2548 * Mark the given vdev faulted.
2551 zpool_vdev_fault(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2553 zfs_cmd_t zc
= {"\0"};
2555 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2557 (void) snprintf(msg
, sizeof (msg
),
2558 dgettext(TEXT_DOMAIN
, "cannot fault %llu"), (u_longlong_t
)guid
);
2560 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2562 zc
.zc_cookie
= VDEV_STATE_FAULTED
;
2565 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2572 * There are no other replicas of this device.
2574 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2577 return (zpool_standard_error(hdl
, errno
, msg
));
2583 * Mark the given vdev degraded.
2586 zpool_vdev_degrade(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2588 zfs_cmd_t zc
= {"\0"};
2590 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2592 (void) snprintf(msg
, sizeof (msg
),
2593 dgettext(TEXT_DOMAIN
, "cannot degrade %llu"), (u_longlong_t
)guid
);
2595 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2597 zc
.zc_cookie
= VDEV_STATE_DEGRADED
;
2600 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2603 return (zpool_standard_error(hdl
, errno
, msg
));
2607 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2611 is_replacing_spare(nvlist_t
*search
, nvlist_t
*tgt
, int which
)
2617 if (nvlist_lookup_nvlist_array(search
, ZPOOL_CONFIG_CHILDREN
, &child
,
2619 verify(nvlist_lookup_string(search
, ZPOOL_CONFIG_TYPE
,
2622 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0 &&
2623 children
== 2 && child
[which
] == tgt
)
2626 for (c
= 0; c
< children
; c
++)
2627 if (is_replacing_spare(child
[c
], tgt
, which
))
2635 * Attach new_disk (fully described by nvroot) to old_disk.
2636 * If 'replacing' is specified, the new disk will replace the old one.
2639 zpool_vdev_attach(zpool_handle_t
*zhp
,
2640 const char *old_disk
, const char *new_disk
, nvlist_t
*nvroot
, int replacing
)
2642 zfs_cmd_t zc
= {"\0"};
2646 boolean_t avail_spare
, l2cache
, islog
;
2651 nvlist_t
*config_root
;
2652 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2653 boolean_t rootpool
= zpool_is_bootable(zhp
);
2656 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2657 "cannot replace %s with %s"), old_disk
, new_disk
);
2659 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2660 "cannot attach %s to %s"), new_disk
, old_disk
);
2662 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2663 if ((tgt
= zpool_find_vdev(zhp
, old_disk
, &avail_spare
, &l2cache
,
2665 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2668 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2671 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2673 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2674 zc
.zc_cookie
= replacing
;
2676 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
2677 &child
, &children
) != 0 || children
!= 1) {
2678 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2679 "new device must be a single disk"));
2680 return (zfs_error(hdl
, EZFS_INVALCONFIG
, msg
));
2683 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
2684 ZPOOL_CONFIG_VDEV_TREE
, &config_root
) == 0);
2686 if ((newname
= zpool_vdev_name(NULL
, NULL
, child
[0], 0)) == NULL
)
2690 * If the target is a hot spare that has been swapped in, we can only
2691 * replace it with another hot spare.
2694 nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_IS_SPARE
, &val
) == 0 &&
2695 (zpool_find_vdev(zhp
, newname
, &avail_spare
, &l2cache
,
2696 NULL
) == NULL
|| !avail_spare
) &&
2697 is_replacing_spare(config_root
, tgt
, 1)) {
2698 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2699 "can only be replaced by another hot spare"));
2701 return (zfs_error(hdl
, EZFS_BADTARGET
, msg
));
2706 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
2709 ret
= zfs_ioctl(hdl
, ZFS_IOC_VDEV_ATTACH
, &zc
);
2711 zcmd_free_nvlists(&zc
);
2716 * XXX need a better way to prevent user from
2717 * booting up a half-baked vdev.
2719 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Make "
2720 "sure to wait until resilver is done "
2721 "before rebooting.\n"));
2729 * Can't attach to or replace this type of vdev.
2732 uint64_t version
= zpool_get_prop_int(zhp
,
2733 ZPOOL_PROP_VERSION
, NULL
);
2736 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2737 "cannot replace a log with a spare"));
2738 else if (version
>= SPA_VERSION_MULTI_REPLACE
)
2739 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2740 "already in replacing/spare config; wait "
2741 "for completion or use 'zpool detach'"));
2743 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2744 "cannot replace a replacing device"));
2746 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2747 "can only attach to mirrors and top-level "
2750 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2755 * The new device must be a single disk.
2757 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2758 "new device must be a single disk"));
2759 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2763 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "%s is busy"),
2765 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2770 * The new device is too small.
2772 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2773 "device is too small"));
2774 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2779 * The new device has a different optimal sector size.
2781 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2782 "new device has a different optimal sector size; use the "
2783 "option '-o ashift=N' to override the optimal size"));
2784 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2789 * The resulting top-level vdev spec won't fit in the label.
2791 (void) zfs_error(hdl
, EZFS_DEVOVERFLOW
, msg
);
2795 (void) zpool_standard_error(hdl
, errno
, msg
);
2802 * Detach the specified device.
2805 zpool_vdev_detach(zpool_handle_t
*zhp
, const char *path
)
2807 zfs_cmd_t zc
= {"\0"};
2810 boolean_t avail_spare
, l2cache
;
2811 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2813 (void) snprintf(msg
, sizeof (msg
),
2814 dgettext(TEXT_DOMAIN
, "cannot detach %s"), path
);
2816 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2817 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2819 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2822 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2825 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2827 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2829 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_DETACH
, &zc
) == 0)
2836 * Can't detach from this type of vdev.
2838 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "only "
2839 "applicable to mirror and replacing vdevs"));
2840 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2845 * There are no other replicas of this device.
2847 (void) zfs_error(hdl
, EZFS_NOREPLICAS
, msg
);
2851 (void) zpool_standard_error(hdl
, errno
, msg
);
2858 * Find a mirror vdev in the source nvlist.
2860 * The mchild array contains a list of disks in one of the top-level mirrors
2861 * of the source pool. The schild array contains a list of disks that the
2862 * user specified on the command line. We loop over the mchild array to
2863 * see if any entry in the schild array matches.
2865 * If a disk in the mchild array is found in the schild array, we return
2866 * the index of that entry. Otherwise we return -1.
2869 find_vdev_entry(zpool_handle_t
*zhp
, nvlist_t
**mchild
, uint_t mchildren
,
2870 nvlist_t
**schild
, uint_t schildren
)
2874 for (mc
= 0; mc
< mchildren
; mc
++) {
2876 char *mpath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
2879 for (sc
= 0; sc
< schildren
; sc
++) {
2880 char *spath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
2882 boolean_t result
= (strcmp(mpath
, spath
) == 0);
2898 * Split a mirror pool. If newroot points to null, then a new nvlist
2899 * is generated and it is the responsibility of the caller to free it.
2902 zpool_vdev_split(zpool_handle_t
*zhp
, char *newname
, nvlist_t
**newroot
,
2903 nvlist_t
*props
, splitflags_t flags
)
2905 zfs_cmd_t zc
= {"\0"};
2907 nvlist_t
*tree
, *config
, **child
, **newchild
, *newconfig
= NULL
;
2908 nvlist_t
**varray
= NULL
, *zc_props
= NULL
;
2909 uint_t c
, children
, newchildren
, lastlog
= 0, vcount
, found
= 0;
2910 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2912 boolean_t freelist
= B_FALSE
, memory_err
= B_TRUE
;
2915 (void) snprintf(msg
, sizeof (msg
),
2916 dgettext(TEXT_DOMAIN
, "Unable to split %s"), zhp
->zpool_name
);
2918 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
2919 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
2921 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
2922 (void) fprintf(stderr
, gettext("Internal error: unable to "
2923 "retrieve pool configuration\n"));
2927 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &tree
)
2929 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
, &vers
) == 0);
2932 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
2933 if ((zc_props
= zpool_valid_proplist(hdl
, zhp
->zpool_name
,
2934 props
, vers
, flags
, msg
)) == NULL
)
2938 if (nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
2940 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2941 "Source pool is missing vdev tree"));
2942 nvlist_free(zc_props
);
2946 varray
= zfs_alloc(hdl
, children
* sizeof (nvlist_t
*));
2949 if (*newroot
== NULL
||
2950 nvlist_lookup_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
2951 &newchild
, &newchildren
) != 0)
2954 for (c
= 0; c
< children
; c
++) {
2955 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
2957 nvlist_t
**mchild
, *vdev
;
2962 * Unlike cache & spares, slogs are stored in the
2963 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2965 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2967 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
2969 if (is_log
|| is_hole
) {
2971 * Create a hole vdev and put it in the config.
2973 if (nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) != 0)
2975 if (nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
,
2976 VDEV_TYPE_HOLE
) != 0)
2978 if (nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_HOLE
,
2983 varray
[vcount
++] = vdev
;
2987 verify(nvlist_lookup_string(child
[c
], ZPOOL_CONFIG_TYPE
, &type
)
2989 if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
2990 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2991 "Source pool must be composed only of mirrors\n"));
2992 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2996 verify(nvlist_lookup_nvlist_array(child
[c
],
2997 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
2999 /* find or add an entry for this top-level vdev */
3000 if (newchildren
> 0 &&
3001 (entry
= find_vdev_entry(zhp
, mchild
, mchildren
,
3002 newchild
, newchildren
)) >= 0) {
3003 /* We found a disk that the user specified. */
3004 vdev
= mchild
[entry
];
3007 /* User didn't specify a disk for this vdev. */
3008 vdev
= mchild
[mchildren
- 1];
3011 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
3015 /* did we find every disk the user specified? */
3016 if (found
!= newchildren
) {
3017 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Device list must "
3018 "include at most one disk from each mirror"));
3019 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3023 /* Prepare the nvlist for populating. */
3024 if (*newroot
== NULL
) {
3025 if (nvlist_alloc(newroot
, NV_UNIQUE_NAME
, 0) != 0)
3028 if (nvlist_add_string(*newroot
, ZPOOL_CONFIG_TYPE
,
3029 VDEV_TYPE_ROOT
) != 0)
3032 verify(nvlist_remove_all(*newroot
, ZPOOL_CONFIG_CHILDREN
) == 0);
3035 /* Add all the children we found */
3036 if (nvlist_add_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
, varray
,
3037 lastlog
== 0 ? vcount
: lastlog
) != 0)
3041 * If we're just doing a dry run, exit now with success.
3044 memory_err
= B_FALSE
;
3049 /* now build up the config list & call the ioctl */
3050 if (nvlist_alloc(&newconfig
, NV_UNIQUE_NAME
, 0) != 0)
3053 if (nvlist_add_nvlist(newconfig
,
3054 ZPOOL_CONFIG_VDEV_TREE
, *newroot
) != 0 ||
3055 nvlist_add_string(newconfig
,
3056 ZPOOL_CONFIG_POOL_NAME
, newname
) != 0 ||
3057 nvlist_add_uint64(newconfig
, ZPOOL_CONFIG_VERSION
, vers
) != 0)
3061 * The new pool is automatically part of the namespace unless we
3062 * explicitly export it.
3065 zc
.zc_cookie
= ZPOOL_EXPORT_AFTER_SPLIT
;
3066 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3067 (void) strlcpy(zc
.zc_string
, newname
, sizeof (zc
.zc_string
));
3068 if (zcmd_write_conf_nvlist(hdl
, &zc
, newconfig
) != 0)
3070 if (zc_props
!= NULL
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
3073 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SPLIT
, &zc
) != 0) {
3074 retval
= zpool_standard_error(hdl
, errno
, msg
);
3079 memory_err
= B_FALSE
;
3082 if (varray
!= NULL
) {
3085 for (v
= 0; v
< vcount
; v
++)
3086 nvlist_free(varray
[v
]);
3089 zcmd_free_nvlists(&zc
);
3090 nvlist_free(zc_props
);
3091 nvlist_free(newconfig
);
3093 nvlist_free(*newroot
);
3101 return (no_memory(hdl
));
3107 * Remove the given device. Currently, this is supported only for hot spares,
3108 * cache, and log devices.
3111 zpool_vdev_remove(zpool_handle_t
*zhp
, const char *path
)
3113 zfs_cmd_t zc
= {"\0"};
3116 boolean_t avail_spare
, l2cache
, islog
;
3117 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3120 (void) snprintf(msg
, sizeof (msg
),
3121 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3123 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3124 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3126 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3128 * XXX - this should just go away.
3130 if (!avail_spare
&& !l2cache
&& !islog
) {
3131 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3132 "only inactive hot spares, cache, "
3133 "or log devices can be removed"));
3134 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3137 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
3138 if (islog
&& version
< SPA_VERSION_HOLES
) {
3139 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3140 "pool must be upgrade to support log removal"));
3141 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
3144 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
3146 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3149 return (zpool_standard_error(hdl
, errno
, msg
));
3153 * Clear the errors for the pool, or the particular device if specified.
3156 zpool_clear(zpool_handle_t
*zhp
, const char *path
, nvlist_t
*rewindnvl
)
3158 zfs_cmd_t zc
= {"\0"};
3161 zpool_rewind_policy_t policy
;
3162 boolean_t avail_spare
, l2cache
;
3163 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3164 nvlist_t
*nvi
= NULL
;
3168 (void) snprintf(msg
, sizeof (msg
),
3169 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3172 (void) snprintf(msg
, sizeof (msg
),
3173 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3176 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3178 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
,
3179 &l2cache
, NULL
)) == 0)
3180 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3183 * Don't allow error clearing for hot spares. Do allow
3184 * error clearing for l2cache devices.
3187 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3189 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
,
3193 zpool_get_rewind_policy(rewindnvl
, &policy
);
3194 zc
.zc_cookie
= policy
.zrp_request
;
3196 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zhp
->zpool_config_size
* 2) != 0)
3199 if (zcmd_write_src_nvlist(hdl
, &zc
, rewindnvl
) != 0)
3202 while ((error
= zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
)) != 0 &&
3204 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
3205 zcmd_free_nvlists(&zc
);
3210 if (!error
|| ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) &&
3211 errno
!= EPERM
&& errno
!= EACCES
)) {
3212 if (policy
.zrp_request
&
3213 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
3214 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nvi
);
3215 zpool_rewind_exclaim(hdl
, zc
.zc_name
,
3216 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0),
3220 zcmd_free_nvlists(&zc
);
3224 zcmd_free_nvlists(&zc
);
3225 return (zpool_standard_error(hdl
, errno
, msg
));
3229 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3232 zpool_vdev_clear(zpool_handle_t
*zhp
, uint64_t guid
)
3234 zfs_cmd_t zc
= {"\0"};
3236 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3238 (void) snprintf(msg
, sizeof (msg
),
3239 dgettext(TEXT_DOMAIN
, "cannot clear errors for %llx"),
3240 (u_longlong_t
)guid
);
3242 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3244 zc
.zc_cookie
= ZPOOL_NO_REWIND
;
3246 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_CLEAR
, &zc
) == 0)
3249 return (zpool_standard_error(hdl
, errno
, msg
));
3253 * Change the GUID for a pool.
3256 zpool_reguid(zpool_handle_t
*zhp
)
3259 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3260 zfs_cmd_t zc
= {"\0"};
3262 (void) snprintf(msg
, sizeof (msg
),
3263 dgettext(TEXT_DOMAIN
, "cannot reguid '%s'"), zhp
->zpool_name
);
3265 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3266 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REGUID
, &zc
) == 0)
3269 return (zpool_standard_error(hdl
, errno
, msg
));
3276 zpool_reopen(zpool_handle_t
*zhp
)
3278 zfs_cmd_t zc
= {"\0"};
3280 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3282 (void) snprintf(msg
, sizeof (msg
),
3283 dgettext(TEXT_DOMAIN
, "cannot reopen '%s'"),
3286 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3287 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REOPEN
, &zc
) == 0)
3289 return (zpool_standard_error(hdl
, errno
, msg
));
3292 /* call into libzfs_core to execute the sync IOCTL per pool */
3294 zpool_sync_one(zpool_handle_t
*zhp
, void *data
)
3297 libzfs_handle_t
*hdl
= zpool_get_handle(zhp
);
3298 const char *pool_name
= zpool_get_name(zhp
);
3299 boolean_t
*force
= data
;
3300 nvlist_t
*innvl
= fnvlist_alloc();
3302 fnvlist_add_boolean_value(innvl
, "force", *force
);
3303 if ((ret
= lzc_sync(pool_name
, innvl
, NULL
)) != 0) {
3305 return (zpool_standard_error_fmt(hdl
, ret
,
3306 dgettext(TEXT_DOMAIN
, "sync '%s' failed"), pool_name
));
3313 #if defined(__sun__) || defined(__sun)
3315 * Convert from a devid string to a path.
3318 devid_to_path(char *devid_str
)
3323 devid_nmlist_t
*list
= NULL
;
3326 if (devid_str_decode(devid_str
, &devid
, &minor
) != 0)
3329 ret
= devid_deviceid_to_nmlist("/dev", devid
, minor
, &list
);
3331 devid_str_free(minor
);
3338 * In a case the strdup() fails, we will just return NULL below.
3340 path
= strdup(list
[0].devname
);
3342 devid_free_nmlist(list
);
3348 * Convert from a path to a devid string.
3351 path_to_devid(const char *path
)
3357 if ((fd
= open(path
, O_RDONLY
)) < 0)
3362 if (devid_get(fd
, &devid
) == 0) {
3363 if (devid_get_minor_name(fd
, &minor
) == 0)
3364 ret
= devid_str_encode(devid
, minor
);
3366 devid_str_free(minor
);
3375 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3376 * ignore any failure here, since a common case is for an unprivileged user to
3377 * type 'zpool status', and we'll display the correct information anyway.
3380 set_path(zpool_handle_t
*zhp
, nvlist_t
*nv
, const char *path
)
3382 zfs_cmd_t zc
= {"\0"};
3384 (void) strncpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3385 (void) strncpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
3386 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
3389 (void) ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_VDEV_SETPATH
, &zc
);
3394 * Remove partition suffix from a vdev path. Partition suffixes may take three
3395 * forms: "-partX", "pX", or "X", where X is a string of digits. The second
3396 * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
3397 * third case only occurs when preceded by a string matching the regular
3398 * expression "^([hsv]|xv)d[a-z]+", i.e. a scsi, ide, virtio or xen disk.
3400 * caller must free the returned string
3403 zfs_strip_partition(char *path
)
3405 char *tmp
= strdup(path
);
3406 char *part
= NULL
, *d
= NULL
;
3410 if ((part
= strstr(tmp
, "-part")) && part
!= tmp
) {
3412 } else if ((part
= strrchr(tmp
, 'p')) &&
3413 part
> tmp
+ 1 && isdigit(*(part
-1))) {
3415 } else if ((tmp
[0] == 'h' || tmp
[0] == 's' || tmp
[0] == 'v') &&
3417 for (d
= &tmp
[2]; isalpha(*d
); part
= ++d
) { }
3418 } else if (strncmp("xvd", tmp
, 3) == 0) {
3419 for (d
= &tmp
[3]; isalpha(*d
); part
= ++d
) { }
3421 if (part
&& d
&& *d
!= '\0') {
3422 for (; isdigit(*d
); d
++) { }
3431 * Same as zfs_strip_partition, but allows "/dev/" to be in the pathname
3436 * Returned string must be freed.
3439 zfs_strip_partition_path(char *path
)
3441 char *newpath
= strdup(path
);
3448 /* Point to "sda1" part of "/dev/sda1" */
3449 sd_offset
= strrchr(newpath
, '/') + 1;
3451 /* Get our new name "sda" */
3452 new_sd
= zfs_strip_partition(sd_offset
);
3458 /* Paste the "sda" where "sda1" was */
3459 strlcpy(sd_offset
, new_sd
, strlen(sd_offset
) + 1);
3461 /* Free temporary "sda" */
3467 #define PATH_BUF_LEN 64
3470 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3471 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3472 * We also check if this is a whole disk, in which case we strip off the
3473 * trailing 's0' slice name.
3475 * This routine is also responsible for identifying when disks have been
3476 * reconfigured in a new location. The kernel will have opened the device by
3477 * devid, but the path will still refer to the old location. To catch this, we
3478 * first do a path -> devid translation (which is fast for the common case). If
3479 * the devid matches, we're done. If not, we do a reverse devid -> path
3480 * translation and issue the appropriate ioctl() to update the path of the vdev.
3481 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3485 zpool_vdev_name(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, nvlist_t
*nv
,
3488 char *path
, *type
, *env
;
3490 char buf
[PATH_BUF_LEN
];
3491 char tmpbuf
[PATH_BUF_LEN
];
3493 env
= getenv("ZPOOL_VDEV_NAME_PATH");
3494 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3495 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3496 name_flags
|= VDEV_NAME_PATH
;
3498 env
= getenv("ZPOOL_VDEV_NAME_GUID");
3499 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3500 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3501 name_flags
|= VDEV_NAME_GUID
;
3503 env
= getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
3504 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3505 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3506 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
3508 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
, &value
) == 0 ||
3509 name_flags
& VDEV_NAME_GUID
) {
3510 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
);
3511 (void) snprintf(buf
, sizeof (buf
), "%llu", (u_longlong_t
)value
);
3513 } else if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
3514 #if defined(__sun__) || defined(__sun)
3516 * Live VDEV path updates to a kernel VDEV during a
3517 * zpool_vdev_name lookup are not supported on Linux.
3524 * If the device is dead (faulted, offline, etc) then don't
3525 * bother opening it. Otherwise we may be forcing the user to
3526 * open a misbehaving device, which can have undesirable
3529 if ((nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
3530 (uint64_t **)&vs
, &vsc
) != 0 ||
3531 vs
->vs_state
>= VDEV_STATE_DEGRADED
) &&
3533 nvlist_lookup_string(nv
, ZPOOL_CONFIG_DEVID
, &devid
) == 0) {
3535 * Determine if the current path is correct.
3537 char *newdevid
= path_to_devid(path
);
3539 if (newdevid
== NULL
||
3540 strcmp(devid
, newdevid
) != 0) {
3543 if ((newpath
= devid_to_path(devid
)) != NULL
) {
3545 * Update the path appropriately.
3547 set_path(zhp
, nv
, newpath
);
3548 if (nvlist_add_string(nv
,
3549 ZPOOL_CONFIG_PATH
, newpath
) == 0)
3550 verify(nvlist_lookup_string(nv
,
3558 devid_str_free(newdevid
);
3562 if (name_flags
& VDEV_NAME_FOLLOW_LINKS
) {
3563 char *rp
= realpath(path
, NULL
);
3565 strlcpy(buf
, rp
, sizeof (buf
));
3572 * For a block device only use the name.
3574 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
3575 if ((strcmp(type
, VDEV_TYPE_DISK
) == 0) &&
3576 !(name_flags
& VDEV_NAME_PATH
)) {
3577 path
= strrchr(path
, '/');
3582 * Remove the partition from the path it this is a whole disk.
3584 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
, &value
)
3585 == 0 && value
&& !(name_flags
& VDEV_NAME_PATH
)) {
3586 return (zfs_strip_partition(path
));
3589 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &path
) == 0);
3592 * If it's a raidz device, we need to stick in the parity level.
3594 if (strcmp(path
, VDEV_TYPE_RAIDZ
) == 0) {
3595 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
,
3597 (void) snprintf(buf
, sizeof (buf
), "%s%llu", path
,
3598 (u_longlong_t
)value
);
3603 * We identify each top-level vdev by using a <type-id>
3604 * naming convention.
3606 if (name_flags
& VDEV_NAME_TYPE_ID
) {
3608 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
3610 (void) snprintf(tmpbuf
, sizeof (tmpbuf
), "%s-%llu",
3611 path
, (u_longlong_t
)id
);
3616 return (zfs_strdup(hdl
, path
));
3620 zbookmark_mem_compare(const void *a
, const void *b
)
3622 return (memcmp(a
, b
, sizeof (zbookmark_phys_t
)));
3626 * Retrieve the persistent error log, uniquify the members, and return to the
3630 zpool_get_errlog(zpool_handle_t
*zhp
, nvlist_t
**nverrlistp
)
3632 zfs_cmd_t zc
= {"\0"};
3633 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3635 zbookmark_phys_t
*zb
= NULL
;
3639 * Retrieve the raw error list from the kernel. If the number of errors
3640 * has increased, allocate more space and continue until we get the
3643 verify(nvlist_lookup_uint64(zhp
->zpool_config
, ZPOOL_CONFIG_ERRCOUNT
,
3647 zc
.zc_nvlist_dst
= (uintptr_t)zfs_alloc(zhp
->zpool_hdl
,
3648 count
* sizeof (zbookmark_phys_t
));
3649 zc
.zc_nvlist_dst_size
= count
;
3650 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3652 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_ERROR_LOG
,
3654 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3655 if (errno
== ENOMEM
) {
3658 count
= zc
.zc_nvlist_dst_size
;
3659 dst
= zfs_alloc(zhp
->zpool_hdl
, count
*
3660 sizeof (zbookmark_phys_t
));
3661 zc
.zc_nvlist_dst
= (uintptr_t)dst
;
3663 return (zpool_standard_error_fmt(hdl
, errno
,
3664 dgettext(TEXT_DOMAIN
, "errors: List of "
3665 "errors unavailable")));
3673 * Sort the resulting bookmarks. This is a little confusing due to the
3674 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3675 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3676 * _not_ copied as part of the process. So we point the start of our
3677 * array appropriate and decrement the total number of elements.
3679 zb
= ((zbookmark_phys_t
*)(uintptr_t)zc
.zc_nvlist_dst
) +
3680 zc
.zc_nvlist_dst_size
;
3681 count
-= zc
.zc_nvlist_dst_size
;
3683 qsort(zb
, count
, sizeof (zbookmark_phys_t
), zbookmark_mem_compare
);
3685 verify(nvlist_alloc(nverrlistp
, 0, KM_SLEEP
) == 0);
3688 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3690 for (i
= 0; i
< count
; i
++) {
3693 /* ignoring zb_blkid and zb_level for now */
3694 if (i
> 0 && zb
[i
-1].zb_objset
== zb
[i
].zb_objset
&&
3695 zb
[i
-1].zb_object
== zb
[i
].zb_object
)
3698 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) != 0)
3700 if (nvlist_add_uint64(nv
, ZPOOL_ERR_DATASET
,
3701 zb
[i
].zb_objset
) != 0) {
3705 if (nvlist_add_uint64(nv
, ZPOOL_ERR_OBJECT
,
3706 zb
[i
].zb_object
) != 0) {
3710 if (nvlist_add_nvlist(*nverrlistp
, "ejk", nv
) != 0) {
3717 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3721 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3722 return (no_memory(zhp
->zpool_hdl
));
3726 * Upgrade a ZFS pool to the latest on-disk version.
3729 zpool_upgrade(zpool_handle_t
*zhp
, uint64_t new_version
)
3731 zfs_cmd_t zc
= {"\0"};
3732 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3734 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3735 zc
.zc_cookie
= new_version
;
3737 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_UPGRADE
, &zc
) != 0)
3738 return (zpool_standard_error_fmt(hdl
, errno
,
3739 dgettext(TEXT_DOMAIN
, "cannot upgrade '%s'"),
3745 zfs_save_arguments(int argc
, char **argv
, char *string
, int len
)
3749 (void) strlcpy(string
, basename(argv
[0]), len
);
3750 for (i
= 1; i
< argc
; i
++) {
3751 (void) strlcat(string
, " ", len
);
3752 (void) strlcat(string
, argv
[i
], len
);
3757 zpool_log_history(libzfs_handle_t
*hdl
, const char *message
)
3759 zfs_cmd_t zc
= {"\0"};
3763 args
= fnvlist_alloc();
3764 fnvlist_add_string(args
, "message", message
);
3765 err
= zcmd_write_src_nvlist(hdl
, &zc
, args
);
3767 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_LOG_HISTORY
, &zc
);
3769 zcmd_free_nvlists(&zc
);
3774 * Perform ioctl to get some command history of a pool.
3776 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3777 * logical offset of the history buffer to start reading from.
3779 * Upon return, 'off' is the next logical offset to read from and
3780 * 'len' is the actual amount of bytes read into 'buf'.
3783 get_history(zpool_handle_t
*zhp
, char *buf
, uint64_t *off
, uint64_t *len
)
3785 zfs_cmd_t zc
= {"\0"};
3786 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3788 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3790 zc
.zc_history
= (uint64_t)(uintptr_t)buf
;
3791 zc
.zc_history_len
= *len
;
3792 zc
.zc_history_offset
= *off
;
3794 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_HISTORY
, &zc
) != 0) {
3797 return (zfs_error_fmt(hdl
, EZFS_PERM
,
3798 dgettext(TEXT_DOMAIN
,
3799 "cannot show history for pool '%s'"),
3802 return (zfs_error_fmt(hdl
, EZFS_NOHISTORY
,
3803 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
3804 "'%s'"), zhp
->zpool_name
));
3806 return (zfs_error_fmt(hdl
, EZFS_BADVERSION
,
3807 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
3808 "'%s', pool must be upgraded"), zhp
->zpool_name
));
3810 return (zpool_standard_error_fmt(hdl
, errno
,
3811 dgettext(TEXT_DOMAIN
,
3812 "cannot get history for '%s'"), zhp
->zpool_name
));
3816 *len
= zc
.zc_history_len
;
3817 *off
= zc
.zc_history_offset
;
3823 * Process the buffer of nvlists, unpacking and storing each nvlist record
3824 * into 'records'. 'leftover' is set to the number of bytes that weren't
3825 * processed as there wasn't a complete record.
3828 zpool_history_unpack(char *buf
, uint64_t bytes_read
, uint64_t *leftover
,
3829 nvlist_t
***records
, uint_t
*numrecords
)
3836 while (bytes_read
> sizeof (reclen
)) {
3838 /* get length of packed record (stored as little endian) */
3839 for (i
= 0, reclen
= 0; i
< sizeof (reclen
); i
++)
3840 reclen
+= (uint64_t)(((uchar_t
*)buf
)[i
]) << (8*i
);
3842 if (bytes_read
< sizeof (reclen
) + reclen
)
3846 if (nvlist_unpack(buf
+ sizeof (reclen
), reclen
, &nv
, 0) != 0)
3848 bytes_read
-= sizeof (reclen
) + reclen
;
3849 buf
+= sizeof (reclen
) + reclen
;
3851 /* add record to nvlist array */
3853 if (ISP2(*numrecords
+ 1)) {
3854 tmp
= realloc(*records
,
3855 *numrecords
* 2 * sizeof (nvlist_t
*));
3863 (*records
)[*numrecords
- 1] = nv
;
3866 *leftover
= bytes_read
;
3871 * Retrieve the command history of a pool.
3874 zpool_get_history(zpool_handle_t
*zhp
, nvlist_t
**nvhisp
)
3877 int buflen
= 128 * 1024;
3879 nvlist_t
**records
= NULL
;
3880 uint_t numrecords
= 0;
3883 buf
= malloc(buflen
);
3887 uint64_t bytes_read
= buflen
;
3890 if ((err
= get_history(zhp
, buf
, &off
, &bytes_read
)) != 0)
3893 /* if nothing else was read in, we're at EOF, just return */
3897 if ((err
= zpool_history_unpack(buf
, bytes_read
,
3898 &leftover
, &records
, &numrecords
)) != 0)
3901 if (leftover
== bytes_read
) {
3903 * no progress made, because buffer is not big enough
3904 * to hold this record; resize and retry.
3908 buf
= malloc(buflen
);
3919 verify(nvlist_alloc(nvhisp
, NV_UNIQUE_NAME
, 0) == 0);
3920 verify(nvlist_add_nvlist_array(*nvhisp
, ZPOOL_HIST_RECORD
,
3921 records
, numrecords
) == 0);
3923 for (i
= 0; i
< numrecords
; i
++)
3924 nvlist_free(records
[i
]);
3931 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
3932 * If there is a new event available 'nvp' will contain a newly allocated
3933 * nvlist and 'dropped' will be set to the number of missed events since
3934 * the last call to this function. When 'nvp' is set to NULL it indicates
3935 * no new events are available. In either case the function returns 0 and
3936 * it is up to the caller to free 'nvp'. In the case of a fatal error the
3937 * function will return a non-zero value. When the function is called in
3938 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
3939 * it will not return until a new event is available.
3942 zpool_events_next(libzfs_handle_t
*hdl
, nvlist_t
**nvp
,
3943 int *dropped
, unsigned flags
, int zevent_fd
)
3945 zfs_cmd_t zc
= {"\0"};
3950 zc
.zc_cleanup_fd
= zevent_fd
;
3952 if (flags
& ZEVENT_NONBLOCK
)
3953 zc
.zc_guid
= ZEVENT_NONBLOCK
;
3955 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, ZEVENT_SIZE
) != 0)
3959 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_NEXT
, &zc
) != 0) {
3962 error
= zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
3963 dgettext(TEXT_DOMAIN
, "zfs shutdown"));
3966 /* Blocking error case should not occur */
3967 if (!(flags
& ZEVENT_NONBLOCK
))
3968 error
= zpool_standard_error_fmt(hdl
, errno
,
3969 dgettext(TEXT_DOMAIN
, "cannot get event"));
3973 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
3974 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
3975 dgettext(TEXT_DOMAIN
, "cannot get event"));
3981 error
= zpool_standard_error_fmt(hdl
, errno
,
3982 dgettext(TEXT_DOMAIN
, "cannot get event"));
3987 error
= zcmd_read_dst_nvlist(hdl
, &zc
, nvp
);
3991 *dropped
= (int)zc
.zc_cookie
;
3993 zcmd_free_nvlists(&zc
);
4002 zpool_events_clear(libzfs_handle_t
*hdl
, int *count
)
4004 zfs_cmd_t zc
= {"\0"};
4007 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
4008 "cannot clear events"));
4010 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_CLEAR
, &zc
) != 0)
4011 return (zpool_standard_error_fmt(hdl
, errno
, msg
));
4014 *count
= (int)zc
.zc_cookie
; /* # of events cleared */
4020 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4021 * the passed zevent_fd file handle. On success zero is returned,
4022 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4025 zpool_events_seek(libzfs_handle_t
*hdl
, uint64_t eid
, int zevent_fd
)
4027 zfs_cmd_t zc
= {"\0"};
4031 zc
.zc_cleanup_fd
= zevent_fd
;
4033 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_SEEK
, &zc
) != 0) {
4036 error
= zfs_error_fmt(hdl
, EZFS_NOENT
,
4037 dgettext(TEXT_DOMAIN
, "cannot get event"));
4041 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
4042 dgettext(TEXT_DOMAIN
, "cannot get event"));
4046 error
= zpool_standard_error_fmt(hdl
, errno
,
4047 dgettext(TEXT_DOMAIN
, "cannot get event"));
4056 zpool_obj_to_path(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4057 char *pathname
, size_t len
)
4059 zfs_cmd_t zc
= {"\0"};
4060 boolean_t mounted
= B_FALSE
;
4061 char *mntpnt
= NULL
;
4062 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
4065 /* special case for the MOS */
4066 (void) snprintf(pathname
, len
, "<metadata>:<0x%llx>",
4071 /* get the dataset's name */
4072 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4074 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
,
4075 ZFS_IOC_DSOBJ_TO_DSNAME
, &zc
) != 0) {
4076 /* just write out a path of two object numbers */
4077 (void) snprintf(pathname
, len
, "<0x%llx>:<0x%llx>",
4078 (longlong_t
)dsobj
, (longlong_t
)obj
);
4081 (void) strlcpy(dsname
, zc
.zc_value
, sizeof (dsname
));
4083 /* find out if the dataset is mounted */
4084 mounted
= is_mounted(zhp
->zpool_hdl
, dsname
, &mntpnt
);
4086 /* get the corrupted object's path */
4087 (void) strlcpy(zc
.zc_name
, dsname
, sizeof (zc
.zc_name
));
4089 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_OBJ_TO_PATH
,
4092 (void) snprintf(pathname
, len
, "%s%s", mntpnt
,
4095 (void) snprintf(pathname
, len
, "%s:%s",
4096 dsname
, zc
.zc_value
);
4099 (void) snprintf(pathname
, len
, "%s:<0x%llx>", dsname
,
4106 * Read the EFI label from the config, if a label does not exist then
4107 * pass back the error to the caller. If the caller has passed a non-NULL
4108 * diskaddr argument then we set it to the starting address of the EFI
4112 read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
)
4116 char diskname
[MAXPATHLEN
];
4119 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PATH
, &path
) != 0)
4122 (void) snprintf(diskname
, sizeof (diskname
), "%s%s", DISK_ROOT
,
4123 strrchr(path
, '/'));
4124 if ((fd
= open(diskname
, O_RDONLY
|O_DIRECT
)) >= 0) {
4125 struct dk_gpt
*vtoc
;
4127 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) >= 0) {
4129 *sb
= vtoc
->efi_parts
[0].p_start
;
4138 * determine where a partition starts on a disk in the current
4142 find_start_block(nvlist_t
*config
)
4146 diskaddr_t sb
= MAXOFFSET_T
;
4149 if (nvlist_lookup_nvlist_array(config
,
4150 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) != 0) {
4151 if (nvlist_lookup_uint64(config
,
4152 ZPOOL_CONFIG_WHOLE_DISK
,
4153 &wholedisk
) != 0 || !wholedisk
) {
4154 return (MAXOFFSET_T
);
4156 if (read_efi_label(config
, &sb
) < 0)
4161 for (c
= 0; c
< children
; c
++) {
4162 sb
= find_start_block(child
[c
]);
4163 if (sb
!= MAXOFFSET_T
) {
4167 return (MAXOFFSET_T
);
4171 zpool_label_disk_check(char *path
)
4173 struct dk_gpt
*vtoc
;
4176 if ((fd
= open(path
, O_RDONLY
|O_DIRECT
)) < 0)
4179 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) != 0) {
4184 if (vtoc
->efi_flags
& EFI_GPT_PRIMARY_CORRUPT
) {
4196 * Generate a unique partition name for the ZFS member. Partitions must
4197 * have unique names to ensure udev will be able to create symlinks under
4198 * /dev/disk/by-partlabel/ for all pool members. The partition names are
4199 * of the form <pool>-<unique-id>.
4202 zpool_label_name(char *label_name
, int label_size
)
4207 fd
= open("/dev/urandom", O_RDONLY
);
4209 if (read(fd
, &id
, sizeof (id
)) != sizeof (id
))
4216 id
= (((uint64_t)rand()) << 32) | (uint64_t)rand();
4218 snprintf(label_name
, label_size
, "zfs-%016llx", (u_longlong_t
)id
);
4222 * Label an individual disk. The name provided is the short name,
4223 * stripped of any leading /dev path.
4226 zpool_label_disk(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, char *name
)
4228 char path
[MAXPATHLEN
];
4229 struct dk_gpt
*vtoc
;
4231 size_t resv
= EFI_MIN_RESV_SIZE
;
4232 uint64_t slice_size
;
4233 diskaddr_t start_block
;
4236 /* prepare an error message just in case */
4237 (void) snprintf(errbuf
, sizeof (errbuf
),
4238 dgettext(TEXT_DOMAIN
, "cannot label '%s'"), name
);
4243 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
4244 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
4246 if (zhp
->zpool_start_block
== 0)
4247 start_block
= find_start_block(nvroot
);
4249 start_block
= zhp
->zpool_start_block
;
4250 zhp
->zpool_start_block
= start_block
;
4253 start_block
= NEW_START_BLOCK
;
4256 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4258 if ((fd
= open(path
, O_RDWR
|O_DIRECT
|O_EXCL
)) < 0) {
4260 * This shouldn't happen. We've long since verified that this
4261 * is a valid device.
4263 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
4264 "label '%s': unable to open device: %d"), path
, errno
);
4265 return (zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
));
4268 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, &vtoc
) != 0) {
4270 * The only way this can fail is if we run out of memory, or we
4271 * were unable to read the disk's capacity
4273 if (errno
== ENOMEM
)
4274 (void) no_memory(hdl
);
4277 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
4278 "label '%s': unable to read disk capacity"), path
);
4280 return (zfs_error(hdl
, EZFS_NOCAP
, errbuf
));
4283 slice_size
= vtoc
->efi_last_u_lba
+ 1;
4284 slice_size
-= EFI_MIN_RESV_SIZE
;
4285 if (start_block
== MAXOFFSET_T
)
4286 start_block
= NEW_START_BLOCK
;
4287 slice_size
-= start_block
;
4288 slice_size
= P2ALIGN(slice_size
, PARTITION_END_ALIGNMENT
);
4290 vtoc
->efi_parts
[0].p_start
= start_block
;
4291 vtoc
->efi_parts
[0].p_size
= slice_size
;
4294 * Why we use V_USR: V_BACKUP confuses users, and is considered
4295 * disposable by some EFI utilities (since EFI doesn't have a backup
4296 * slice). V_UNASSIGNED is supposed to be used only for zero size
4297 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4298 * etc. were all pretty specific. V_USR is as close to reality as we
4299 * can get, in the absence of V_OTHER.
4301 vtoc
->efi_parts
[0].p_tag
= V_USR
;
4302 zpool_label_name(vtoc
->efi_parts
[0].p_name
, EFI_PART_NAME_LEN
);
4304 vtoc
->efi_parts
[8].p_start
= slice_size
+ start_block
;
4305 vtoc
->efi_parts
[8].p_size
= resv
;
4306 vtoc
->efi_parts
[8].p_tag
= V_RESERVED
;
4308 rval
= efi_write(fd
, vtoc
);
4310 /* Flush the buffers to disk and invalidate the page cache. */
4312 (void) ioctl(fd
, BLKFLSBUF
);
4315 rval
= efi_rescan(fd
);
4318 * Some block drivers (like pcata) may not support EFI GPT labels.
4319 * Print out a helpful error message directing the user to manually
4320 * label the disk and give a specific slice.
4326 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "try using "
4327 "parted(8) and then provide a specific slice: %d"), rval
);
4328 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4334 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4335 (void) zfs_append_partition(path
, MAXPATHLEN
);
4337 /* Wait to udev to signal use the device has settled. */
4338 rval
= zpool_label_disk_wait(path
, DISK_LABEL_WAIT
);
4340 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "failed to "
4341 "detect device partitions on '%s': %d"), path
, rval
);
4342 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4345 /* We can't be to paranoid. Read the label back and verify it. */
4346 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4347 rval
= zpool_label_disk_check(path
);
4349 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "freshly written "
4350 "EFI label on '%s' is damaged. Ensure\nthis device "
4351 "is not in in use, and is functioning properly: %d"),
4353 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4360 * Allocate and return the underlying device name for a device mapper device.
4361 * If a device mapper device maps to multiple devices, return the first device.
4363 * For example, dm_name = "/dev/dm-0" could return "/dev/sda". Symlinks to a
4364 * DM device (like /dev/disk/by-vdev/A0) are also allowed.
4366 * Returns device name, or NULL on error or no match. If dm_name is not a DM
4367 * device then return NULL.
4369 * NOTE: The returned name string must be *freed*.
4372 dm_get_underlying_path(char *dm_name
)
4382 if (dm_name
== NULL
)
4385 /* dm name may be a symlink (like /dev/disk/by-vdev/A0) */
4386 realp
= realpath(dm_name
, NULL
);
4391 * If they preface 'dev' with a path (like "/dev") then strip it off.
4392 * We just want the 'dm-N' part.
4394 tmp
= strrchr(realp
, '/');
4396 dev_str
= tmp
+ 1; /* +1 since we want the chr after '/' */
4400 size
= asprintf(&tmp
, "/sys/block/%s/slaves/", dev_str
);
4401 if (size
== -1 || !tmp
)
4408 /* Return first sd* entry in /sys/block/dm-N/slaves/ */
4409 while ((ep
= readdir(dp
))) {
4410 if (ep
->d_type
!= DT_DIR
) { /* skip "." and ".." dirs */
4411 size
= asprintf(&path
, "/dev/%s", ep
->d_name
);
4425 * Return 1 if device is a device mapper or multipath device.
4429 zfs_dev_is_dm(char *dev_name
)
4433 tmp
= dm_get_underlying_path(dev_name
);
4442 * By "whole disk" we mean an entire physical disk (something we can
4443 * label, toggle the write cache on, etc.) as opposed to the full
4444 * capacity of a pseudo-device such as lofi or did. We act as if we
4445 * are labeling the disk, which should be a pretty good test of whether
4446 * it's a viable device or not. Returns B_TRUE if it is and B_FALSE if
4450 zfs_dev_is_whole_disk(char *dev_name
)
4452 struct dk_gpt
*label
;
4455 if ((fd
= open(dev_name
, O_RDONLY
| O_DIRECT
)) < 0)
4458 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, &label
) != 0) {
4470 * Lookup the underlying device for a device name
4472 * Often you'll have a symlink to a device, a partition device,
4473 * or a multipath device, and want to look up the underlying device.
4474 * This function returns the underlying device name. If the device
4475 * name is already the underlying device, then just return the same
4476 * name. If the device is a DM device with multiple underlying devices
4477 * then return the first one.
4481 * 1. /dev/disk/by-id/ata-QEMU_HARDDISK_QM00001 -> ../../sda
4482 * dev_name: /dev/disk/by-id/ata-QEMU_HARDDISK_QM00001
4485 * 2. /dev/mapper/mpatha (made up of /dev/sda and /dev/sdb)
4486 * dev_name: /dev/mapper/mpatha
4487 * returns: /dev/sda (first device)
4489 * 3. /dev/sda (already the underlying device)
4490 * dev_name: /dev/sda
4493 * 4. /dev/dm-3 (mapped to /dev/sda)
4494 * dev_name: /dev/dm-3
4497 * 5. /dev/disk/by-id/scsi-0QEMU_drive-scsi0-0-0-0-part9 -> ../../sdb9
4498 * dev_name: /dev/disk/by-id/scsi-0QEMU_drive-scsi0-0-0-0-part9
4501 * 6. /dev/disk/by-uuid/5df030cf-3cd9-46e4-8e99-3ccb462a4e9a -> ../dev/sda2
4502 * dev_name: /dev/disk/by-uuid/5df030cf-3cd9-46e4-8e99-3ccb462a4e9a
4505 * Returns underlying device name, or NULL on error or no match.
4507 * NOTE: The returned name string must be *freed*.
4510 zfs_get_underlying_path(char *dev_name
)
4515 if (dev_name
== NULL
)
4518 tmp
= dm_get_underlying_path(dev_name
);
4520 /* dev_name not a DM device, so just un-symlinkize it */
4522 tmp
= realpath(dev_name
, NULL
);
4525 name
= zfs_strip_partition_path(tmp
);
4533 * Given a dev name like "sda", return the full enclosure sysfs path to
4534 * the disk. You can also pass in the name with "/dev" prepended
4535 * to it (like /dev/sda).
4537 * For example, disk "sda" in enclosure slot 1:
4539 * returns: "/sys/class/enclosure/1:0:3:0/Slot 1"
4541 * 'dev' must be a non-devicemapper device.
4543 * Returned string must be freed.
4546 zfs_get_enclosure_sysfs_path(char *dev_name
)
4550 char buf
[MAXPATHLEN
];
4558 if (dev_name
== NULL
)
4561 /* If they preface 'dev' with a path (like "/dev") then strip it off */
4562 tmp1
= strrchr(dev_name
, '/');
4564 dev_name
= tmp1
+ 1; /* +1 since we want the chr after '/' */
4566 tmpsize
= asprintf(&tmp1
, "/sys/block/%s/device", dev_name
);
4567 if (tmpsize
== -1 || tmp1
== NULL
) {
4574 tmp1
= NULL
; /* To make free() at the end a NOP */
4579 * Look though all sysfs entries in /sys/block/<dev>/device for
4580 * the enclosure symlink.
4582 while ((ep
= readdir(dp
))) {
4583 /* Ignore everything that's not our enclosure_device link */
4584 if (strstr(ep
->d_name
, "enclosure_device") == NULL
)
4587 if (asprintf(&tmp2
, "%s/%s", tmp1
, ep
->d_name
) == -1 ||
4591 size
= readlink(tmp2
, buf
, sizeof (buf
));
4593 /* Did readlink fail or crop the link name? */
4594 if (size
== -1 || size
>= sizeof (buf
)) {
4596 tmp2
= NULL
; /* To make free() at the end a NOP */
4601 * We got a valid link. readlink() doesn't terminate strings
4602 * so we have to do it.
4607 * Our link will look like:
4609 * "../../../../port-11:1:2/..STUFF../enclosure/1:0:3:0/SLOT 1"
4611 * We want to grab the "enclosure/1:0:3:0/SLOT 1" part
4613 tmp3
= strstr(buf
, "enclosure");
4617 if (asprintf(&path
, "/sys/class/%s", tmp3
) == -1) {
4618 /* If asprintf() fails, 'path' is undefined */