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>
41 #include <sys/efi_partition.h>
43 #include <sys/zfs_ioctl.h>
46 #include "zfs_namecheck.h"
48 #include "libzfs_impl.h"
49 #include "zfs_comutil.h"
50 #include "zfeature_common.h"
52 static int read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
);
54 typedef struct prop_flags
{
55 int create
:1; /* Validate property on creation */
56 int import
:1; /* Validate property on import */
60 * ====================================================================
61 * zpool property functions
62 * ====================================================================
66 zpool_get_all_props(zpool_handle_t
*zhp
)
68 zfs_cmd_t zc
= {"\0"};
69 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
71 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
73 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
76 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_PROPS
, &zc
) != 0) {
77 if (errno
== ENOMEM
) {
78 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
79 zcmd_free_nvlists(&zc
);
83 zcmd_free_nvlists(&zc
);
88 if (zcmd_read_dst_nvlist(hdl
, &zc
, &zhp
->zpool_props
) != 0) {
89 zcmd_free_nvlists(&zc
);
93 zcmd_free_nvlists(&zc
);
99 zpool_props_refresh(zpool_handle_t
*zhp
)
103 old_props
= zhp
->zpool_props
;
105 if (zpool_get_all_props(zhp
) != 0)
108 nvlist_free(old_props
);
113 zpool_get_prop_string(zpool_handle_t
*zhp
, zpool_prop_t prop
,
119 zprop_source_t source
;
121 nvl
= zhp
->zpool_props
;
122 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
123 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &ival
) == 0);
125 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
127 source
= ZPROP_SRC_DEFAULT
;
128 if ((value
= (char *)zpool_prop_default_string(prop
)) == NULL
)
139 zpool_get_prop_int(zpool_handle_t
*zhp
, zpool_prop_t prop
, zprop_source_t
*src
)
143 zprop_source_t source
;
145 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
)) {
147 * zpool_get_all_props() has most likely failed because
148 * the pool is faulted, but if all we need is the top level
149 * vdev's guid then get it from the zhp config nvlist.
151 if ((prop
== ZPOOL_PROP_GUID
) &&
152 (nvlist_lookup_nvlist(zhp
->zpool_config
,
153 ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0) &&
154 (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
)
158 return (zpool_prop_default_numeric(prop
));
161 nvl
= zhp
->zpool_props
;
162 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
163 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &value
) == 0);
165 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
167 source
= ZPROP_SRC_DEFAULT
;
168 value
= zpool_prop_default_numeric(prop
);
178 * Map VDEV STATE to printed strings.
181 zpool_state_to_name(vdev_state_t state
, vdev_aux_t aux
)
184 case VDEV_STATE_CLOSED
:
185 case VDEV_STATE_OFFLINE
:
186 return (gettext("OFFLINE"));
187 case VDEV_STATE_REMOVED
:
188 return (gettext("REMOVED"));
189 case VDEV_STATE_CANT_OPEN
:
190 if (aux
== VDEV_AUX_CORRUPT_DATA
|| aux
== VDEV_AUX_BAD_LOG
)
191 return (gettext("FAULTED"));
192 else if (aux
== VDEV_AUX_SPLIT_POOL
)
193 return (gettext("SPLIT"));
195 return (gettext("UNAVAIL"));
196 case VDEV_STATE_FAULTED
:
197 return (gettext("FAULTED"));
198 case VDEV_STATE_DEGRADED
:
199 return (gettext("DEGRADED"));
200 case VDEV_STATE_HEALTHY
:
201 return (gettext("ONLINE"));
207 return (gettext("UNKNOWN"));
211 * Map POOL STATE to printed strings.
214 zpool_pool_state_to_name(pool_state_t state
)
219 case POOL_STATE_ACTIVE
:
220 return (gettext("ACTIVE"));
221 case POOL_STATE_EXPORTED
:
222 return (gettext("EXPORTED"));
223 case POOL_STATE_DESTROYED
:
224 return (gettext("DESTROYED"));
225 case POOL_STATE_SPARE
:
226 return (gettext("SPARE"));
227 case POOL_STATE_L2CACHE
:
228 return (gettext("L2CACHE"));
229 case POOL_STATE_UNINITIALIZED
:
230 return (gettext("UNINITIALIZED"));
231 case POOL_STATE_UNAVAIL
:
232 return (gettext("UNAVAIL"));
233 case POOL_STATE_POTENTIALLY_ACTIVE
:
234 return (gettext("POTENTIALLY_ACTIVE"));
237 return (gettext("UNKNOWN"));
241 * Get a zpool property value for 'prop' and return the value in
242 * a pre-allocated buffer.
245 zpool_get_prop(zpool_handle_t
*zhp
, zpool_prop_t prop
, char *buf
,
246 size_t len
, zprop_source_t
*srctype
, boolean_t literal
)
250 zprop_source_t src
= ZPROP_SRC_NONE
;
255 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
257 case ZPOOL_PROP_NAME
:
258 (void) strlcpy(buf
, zpool_get_name(zhp
), len
);
261 case ZPOOL_PROP_HEALTH
:
262 (void) strlcpy(buf
, "FAULTED", len
);
265 case ZPOOL_PROP_GUID
:
266 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
267 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
270 case ZPOOL_PROP_ALTROOT
:
271 case ZPOOL_PROP_CACHEFILE
:
272 case ZPOOL_PROP_COMMENT
:
273 if (zhp
->zpool_props
!= NULL
||
274 zpool_get_all_props(zhp
) == 0) {
276 zpool_get_prop_string(zhp
, prop
, &src
),
282 (void) strlcpy(buf
, "-", len
);
291 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
) &&
292 prop
!= ZPOOL_PROP_NAME
)
295 switch (zpool_prop_get_type(prop
)) {
296 case PROP_TYPE_STRING
:
297 (void) strlcpy(buf
, zpool_get_prop_string(zhp
, prop
, &src
),
301 case PROP_TYPE_NUMBER
:
302 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
305 case ZPOOL_PROP_SIZE
:
306 case ZPOOL_PROP_ALLOCATED
:
307 case ZPOOL_PROP_FREE
:
308 case ZPOOL_PROP_FREEING
:
309 case ZPOOL_PROP_LEAKED
:
310 case ZPOOL_PROP_ASHIFT
:
312 (void) snprintf(buf
, len
, "%llu",
313 (u_longlong_t
)intval
);
315 (void) zfs_nicenum(intval
, buf
, len
);
318 case ZPOOL_PROP_EXPANDSZ
:
320 (void) strlcpy(buf
, "-", len
);
321 } else if (literal
) {
322 (void) snprintf(buf
, len
, "%llu",
323 (u_longlong_t
)intval
);
325 (void) zfs_nicebytes(intval
, buf
, len
);
329 case ZPOOL_PROP_CAPACITY
:
331 (void) snprintf(buf
, len
, "%llu",
332 (u_longlong_t
)intval
);
334 (void) snprintf(buf
, len
, "%llu%%",
335 (u_longlong_t
)intval
);
339 case ZPOOL_PROP_FRAGMENTATION
:
340 if (intval
== UINT64_MAX
) {
341 (void) strlcpy(buf
, "-", len
);
342 } else if (literal
) {
343 (void) snprintf(buf
, len
, "%llu",
344 (u_longlong_t
)intval
);
346 (void) snprintf(buf
, len
, "%llu%%",
347 (u_longlong_t
)intval
);
351 case ZPOOL_PROP_DEDUPRATIO
:
353 (void) snprintf(buf
, len
, "%llu.%02llu",
354 (u_longlong_t
)(intval
/ 100),
355 (u_longlong_t
)(intval
% 100));
357 (void) snprintf(buf
, len
, "%llu.%02llux",
358 (u_longlong_t
)(intval
/ 100),
359 (u_longlong_t
)(intval
% 100));
362 case ZPOOL_PROP_HEALTH
:
363 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
364 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
365 verify(nvlist_lookup_uint64_array(nvroot
,
366 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
369 (void) strlcpy(buf
, zpool_state_to_name(intval
,
372 case ZPOOL_PROP_VERSION
:
373 if (intval
>= SPA_VERSION_FEATURES
) {
374 (void) snprintf(buf
, len
, "-");
379 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
383 case PROP_TYPE_INDEX
:
384 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
385 if (zpool_prop_index_to_string(prop
, intval
, &strval
)
388 (void) strlcpy(buf
, strval
, len
);
402 * Check if the bootfs name has the same pool name as it is set to.
403 * Assuming bootfs is a valid dataset name.
406 bootfs_name_valid(const char *pool
, char *bootfs
)
408 int len
= strlen(pool
);
410 if (!zfs_name_valid(bootfs
, ZFS_TYPE_FILESYSTEM
|ZFS_TYPE_SNAPSHOT
))
413 if (strncmp(pool
, bootfs
, len
) == 0 &&
414 (bootfs
[len
] == '/' || bootfs
[len
] == '\0'))
421 zpool_is_bootable(zpool_handle_t
*zhp
)
423 char bootfs
[ZFS_MAX_DATASET_NAME_LEN
];
425 return (zpool_get_prop(zhp
, ZPOOL_PROP_BOOTFS
, bootfs
,
426 sizeof (bootfs
), NULL
, B_FALSE
) == 0 && strncmp(bootfs
, "-",
427 sizeof (bootfs
)) != 0);
432 * Given an nvlist of zpool properties to be set, validate that they are
433 * correct, and parse any numeric properties (index, boolean, etc) if they are
434 * specified as strings.
437 zpool_valid_proplist(libzfs_handle_t
*hdl
, const char *poolname
,
438 nvlist_t
*props
, uint64_t version
, prop_flags_t flags
, char *errbuf
)
446 struct stat64 statbuf
;
449 if (nvlist_alloc(&retprops
, NV_UNIQUE_NAME
, 0) != 0) {
450 (void) no_memory(hdl
);
455 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
456 const char *propname
= nvpair_name(elem
);
458 prop
= zpool_name_to_prop(propname
);
459 if (prop
== ZPROP_INVAL
&& zpool_prop_feature(propname
)) {
461 char *fname
= strchr(propname
, '@') + 1;
463 err
= zfeature_lookup_name(fname
, NULL
);
465 ASSERT3U(err
, ==, ENOENT
);
466 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
467 "invalid feature '%s'"), fname
);
468 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
472 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
473 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
474 "'%s' must be a string"), propname
);
475 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
479 (void) nvpair_value_string(elem
, &strval
);
480 if (strcmp(strval
, ZFS_FEATURE_ENABLED
) != 0 &&
481 strcmp(strval
, ZFS_FEATURE_DISABLED
) != 0) {
482 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
483 "property '%s' can only be set to "
484 "'enabled' or 'disabled'"), propname
);
485 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
489 if (nvlist_add_uint64(retprops
, propname
, 0) != 0) {
490 (void) no_memory(hdl
);
497 * Make sure this property is valid and applies to this type.
499 if (prop
== ZPROP_INVAL
) {
500 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
501 "invalid property '%s'"), propname
);
502 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
506 if (zpool_prop_readonly(prop
)) {
507 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
508 "is readonly"), propname
);
509 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
513 if (zprop_parse_value(hdl
, elem
, prop
, ZFS_TYPE_POOL
, retprops
,
514 &strval
, &intval
, errbuf
) != 0)
518 * Perform additional checking for specific properties.
521 case ZPOOL_PROP_VERSION
:
522 if (intval
< version
||
523 !SPA_VERSION_IS_SUPPORTED(intval
)) {
524 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
525 "property '%s' number %d is invalid."),
527 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
532 case ZPOOL_PROP_ASHIFT
:
534 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
535 "property '%s' can only be set at "
536 "creation time"), propname
);
537 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
542 (intval
< ASHIFT_MIN
|| intval
> ASHIFT_MAX
)) {
543 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
544 "invalid '%s=%d' property: only values "
545 "between %" PRId32
" and %" PRId32
" "
547 propname
, intval
, ASHIFT_MIN
, ASHIFT_MAX
);
548 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
553 case ZPOOL_PROP_BOOTFS
:
554 if (flags
.create
|| flags
.import
) {
555 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
556 "property '%s' cannot be set at creation "
557 "or import time"), propname
);
558 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
562 if (version
< SPA_VERSION_BOOTFS
) {
563 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
564 "pool must be upgraded to support "
565 "'%s' property"), propname
);
566 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
571 * bootfs property value has to be a dataset name and
572 * the dataset has to be in the same pool as it sets to.
574 if (strval
[0] != '\0' && !bootfs_name_valid(poolname
,
576 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
577 "is an invalid name"), strval
);
578 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
582 if ((zhp
= zpool_open_canfail(hdl
, poolname
)) == NULL
) {
583 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
584 "could not open pool '%s'"), poolname
);
585 (void) zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
);
591 case ZPOOL_PROP_ALTROOT
:
592 if (!flags
.create
&& !flags
.import
) {
593 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
594 "property '%s' can only be set during pool "
595 "creation or import"), propname
);
596 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
600 if (strval
[0] != '/') {
601 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
602 "bad alternate root '%s'"), strval
);
603 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
608 case ZPOOL_PROP_CACHEFILE
:
609 if (strval
[0] == '\0')
612 if (strcmp(strval
, "none") == 0)
615 if (strval
[0] != '/') {
616 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
617 "property '%s' must be empty, an "
618 "absolute path, or 'none'"), propname
);
619 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
623 slash
= strrchr(strval
, '/');
625 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
626 strcmp(slash
, "/..") == 0) {
627 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
628 "'%s' is not a valid file"), strval
);
629 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
635 if (strval
[0] != '\0' &&
636 (stat64(strval
, &statbuf
) != 0 ||
637 !S_ISDIR(statbuf
.st_mode
))) {
638 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
639 "'%s' is not a valid directory"),
641 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
648 case ZPOOL_PROP_COMMENT
:
649 for (check
= strval
; *check
!= '\0'; check
++) {
650 if (!isprint(*check
)) {
652 dgettext(TEXT_DOMAIN
,
653 "comment may only have printable "
655 (void) zfs_error(hdl
, EZFS_BADPROP
,
660 if (strlen(strval
) > ZPROP_MAX_COMMENT
) {
661 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
662 "comment must not exceed %d characters"),
664 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
668 case ZPOOL_PROP_READONLY
:
670 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
671 "property '%s' can only be set at "
672 "import time"), propname
);
673 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
677 case ZPOOL_PROP_TNAME
:
679 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
680 "property '%s' can only be set at "
681 "creation time"), propname
);
682 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
688 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
689 "property '%s'(%d) not defined"), propname
, prop
);
696 nvlist_free(retprops
);
701 * Set zpool property : propname=propval.
704 zpool_set_prop(zpool_handle_t
*zhp
, const char *propname
, const char *propval
)
706 zfs_cmd_t zc
= {"\0"};
709 nvlist_t
*nvl
= NULL
;
712 prop_flags_t flags
= { 0 };
714 (void) snprintf(errbuf
, sizeof (errbuf
),
715 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
718 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
719 return (no_memory(zhp
->zpool_hdl
));
721 if (nvlist_add_string(nvl
, propname
, propval
) != 0) {
723 return (no_memory(zhp
->zpool_hdl
));
726 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
727 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
728 zhp
->zpool_name
, nvl
, version
, flags
, errbuf
)) == NULL
) {
737 * Execute the corresponding ioctl() to set this property.
739 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
741 if (zcmd_write_src_nvlist(zhp
->zpool_hdl
, &zc
, nvl
) != 0) {
746 ret
= zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_SET_PROPS
, &zc
);
748 zcmd_free_nvlists(&zc
);
752 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
754 (void) zpool_props_refresh(zhp
);
760 zpool_expand_proplist(zpool_handle_t
*zhp
, zprop_list_t
**plp
)
762 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
764 char buf
[ZFS_MAXPROPLEN
];
765 nvlist_t
*features
= NULL
;
768 boolean_t firstexpand
= (NULL
== *plp
);
771 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_POOL
) != 0)
775 while (*last
!= NULL
)
776 last
= &(*last
)->pl_next
;
779 features
= zpool_get_features(zhp
);
781 if ((*plp
)->pl_all
&& firstexpand
) {
782 for (i
= 0; i
< SPA_FEATURES
; i
++) {
783 zprop_list_t
*entry
= zfs_alloc(hdl
,
784 sizeof (zprop_list_t
));
785 entry
->pl_prop
= ZPROP_INVAL
;
786 entry
->pl_user_prop
= zfs_asprintf(hdl
, "feature@%s",
787 spa_feature_table
[i
].fi_uname
);
788 entry
->pl_width
= strlen(entry
->pl_user_prop
);
789 entry
->pl_all
= B_TRUE
;
792 last
= &entry
->pl_next
;
796 /* add any unsupported features */
797 for (nvp
= nvlist_next_nvpair(features
, NULL
);
798 nvp
!= NULL
; nvp
= nvlist_next_nvpair(features
, nvp
)) {
803 if (zfeature_is_supported(nvpair_name(nvp
)))
806 propname
= zfs_asprintf(hdl
, "unsupported@%s",
810 * Before adding the property to the list make sure that no
811 * other pool already added the same property.
815 while (entry
!= NULL
) {
816 if (entry
->pl_user_prop
!= NULL
&&
817 strcmp(propname
, entry
->pl_user_prop
) == 0) {
821 entry
= entry
->pl_next
;
828 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
829 entry
->pl_prop
= ZPROP_INVAL
;
830 entry
->pl_user_prop
= propname
;
831 entry
->pl_width
= strlen(entry
->pl_user_prop
);
832 entry
->pl_all
= B_TRUE
;
835 last
= &entry
->pl_next
;
838 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
843 if (entry
->pl_prop
!= ZPROP_INVAL
&&
844 zpool_get_prop(zhp
, entry
->pl_prop
, buf
, sizeof (buf
),
845 NULL
, B_FALSE
) == 0) {
846 if (strlen(buf
) > entry
->pl_width
)
847 entry
->pl_width
= strlen(buf
);
855 * Get the state for the given feature on the given ZFS pool.
858 zpool_prop_get_feature(zpool_handle_t
*zhp
, const char *propname
, char *buf
,
862 boolean_t found
= B_FALSE
;
863 nvlist_t
*features
= zpool_get_features(zhp
);
865 const char *feature
= strchr(propname
, '@') + 1;
867 supported
= zpool_prop_feature(propname
);
868 ASSERT(supported
|| zpool_prop_unsupported(propname
));
871 * Convert from feature name to feature guid. This conversion is
872 * unnecessary for unsupported@... properties because they already
879 ret
= zfeature_lookup_name(feature
, &fid
);
881 (void) strlcpy(buf
, "-", len
);
884 feature
= spa_feature_table
[fid
].fi_guid
;
887 if (nvlist_lookup_uint64(features
, feature
, &refcount
) == 0)
892 (void) strlcpy(buf
, ZFS_FEATURE_DISABLED
, len
);
895 (void) strlcpy(buf
, ZFS_FEATURE_ENABLED
, len
);
897 (void) strlcpy(buf
, ZFS_FEATURE_ACTIVE
, len
);
902 (void) strcpy(buf
, ZFS_UNSUPPORTED_INACTIVE
);
904 (void) strcpy(buf
, ZFS_UNSUPPORTED_READONLY
);
907 (void) strlcpy(buf
, "-", len
);
916 * Don't start the slice at the default block of 34; many storage
917 * devices will use a stripe width of 128k, other vendors prefer a 1m
918 * alignment. It is best to play it safe and ensure a 1m alignment
919 * given 512B blocks. When the block size is larger by a power of 2
920 * we will still be 1m aligned. Some devices are sensitive to the
921 * partition ending alignment as well.
923 #define NEW_START_BLOCK 2048
924 #define PARTITION_END_ALIGNMENT 2048
927 * Validate the given pool name, optionally putting an extended error message in
931 zpool_name_valid(libzfs_handle_t
*hdl
, boolean_t isopen
, const char *pool
)
937 ret
= pool_namecheck(pool
, &why
, &what
);
940 * The rules for reserved pool names were extended at a later point.
941 * But we need to support users with existing pools that may now be
942 * invalid. So we only check for this expanded set of names during a
943 * create (or import), and only in userland.
945 if (ret
== 0 && !isopen
&&
946 (strncmp(pool
, "mirror", 6) == 0 ||
947 strncmp(pool
, "raidz", 5) == 0 ||
948 strncmp(pool
, "spare", 5) == 0 ||
949 strcmp(pool
, "log") == 0)) {
952 dgettext(TEXT_DOMAIN
, "name is reserved"));
960 case NAME_ERR_TOOLONG
:
962 dgettext(TEXT_DOMAIN
, "name is too long"));
965 case NAME_ERR_INVALCHAR
:
967 dgettext(TEXT_DOMAIN
, "invalid character "
968 "'%c' in pool name"), what
);
971 case NAME_ERR_NOLETTER
:
972 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
973 "name must begin with a letter"));
976 case NAME_ERR_RESERVED
:
977 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
978 "name is reserved"));
981 case NAME_ERR_DISKLIKE
:
982 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
983 "pool name is reserved"));
986 case NAME_ERR_LEADING_SLASH
:
987 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
988 "leading slash in name"));
991 case NAME_ERR_EMPTY_COMPONENT
:
992 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
993 "empty component in name"));
996 case NAME_ERR_TRAILING_SLASH
:
997 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
998 "trailing slash in name"));
1001 case NAME_ERR_MULTIPLE_DELIMITERS
:
1002 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1003 "multiple '@' and/or '#' delimiters in "
1007 case NAME_ERR_NO_AT
:
1008 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1009 "permission set is missing '@'"));
1013 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1014 "(%d) not defined"), why
);
1025 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1029 zpool_open_canfail(libzfs_handle_t
*hdl
, const char *pool
)
1031 zpool_handle_t
*zhp
;
1035 * Make sure the pool name is valid.
1037 if (!zpool_name_valid(hdl
, B_TRUE
, pool
)) {
1038 (void) zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1039 dgettext(TEXT_DOMAIN
, "cannot open '%s'"),
1044 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
1047 zhp
->zpool_hdl
= hdl
;
1048 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1050 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1056 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "no such pool"));
1057 (void) zfs_error_fmt(hdl
, EZFS_NOENT
,
1058 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), pool
);
1067 * Like the above, but silent on error. Used when iterating over pools (because
1068 * the configuration cache may be out of date).
1071 zpool_open_silent(libzfs_handle_t
*hdl
, const char *pool
, zpool_handle_t
**ret
)
1073 zpool_handle_t
*zhp
;
1076 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
1079 zhp
->zpool_hdl
= hdl
;
1080 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1082 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1098 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1102 zpool_open(libzfs_handle_t
*hdl
, const char *pool
)
1104 zpool_handle_t
*zhp
;
1106 if ((zhp
= zpool_open_canfail(hdl
, pool
)) == NULL
)
1109 if (zhp
->zpool_state
== POOL_STATE_UNAVAIL
) {
1110 (void) zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
1111 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), zhp
->zpool_name
);
1120 * Close the handle. Simply frees the memory associated with the handle.
1123 zpool_close(zpool_handle_t
*zhp
)
1125 nvlist_free(zhp
->zpool_config
);
1126 nvlist_free(zhp
->zpool_old_config
);
1127 nvlist_free(zhp
->zpool_props
);
1132 * Return the name of the pool.
1135 zpool_get_name(zpool_handle_t
*zhp
)
1137 return (zhp
->zpool_name
);
1142 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1145 zpool_get_state(zpool_handle_t
*zhp
)
1147 return (zhp
->zpool_state
);
1151 * Create the named pool, using the provided vdev list. It is assumed
1152 * that the consumer has already validated the contents of the nvlist, so we
1153 * don't have to worry about error semantics.
1156 zpool_create(libzfs_handle_t
*hdl
, const char *pool
, nvlist_t
*nvroot
,
1157 nvlist_t
*props
, nvlist_t
*fsprops
)
1159 zfs_cmd_t zc
= {"\0"};
1160 nvlist_t
*zc_fsprops
= NULL
;
1161 nvlist_t
*zc_props
= NULL
;
1165 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1166 "cannot create '%s'"), pool
);
1168 if (!zpool_name_valid(hdl
, B_FALSE
, pool
))
1169 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
1171 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1175 prop_flags_t flags
= { .create
= B_TRUE
, .import
= B_FALSE
};
1177 if ((zc_props
= zpool_valid_proplist(hdl
, pool
, props
,
1178 SPA_VERSION_1
, flags
, msg
)) == NULL
) {
1187 zoned
= ((nvlist_lookup_string(fsprops
,
1188 zfs_prop_to_name(ZFS_PROP_ZONED
), &zonestr
) == 0) &&
1189 strcmp(zonestr
, "on") == 0);
1191 if ((zc_fsprops
= zfs_valid_proplist(hdl
, ZFS_TYPE_FILESYSTEM
,
1192 fsprops
, zoned
, NULL
, NULL
, msg
)) == NULL
) {
1196 (nvlist_alloc(&zc_props
, NV_UNIQUE_NAME
, 0) != 0)) {
1199 if (nvlist_add_nvlist(zc_props
,
1200 ZPOOL_ROOTFS_PROPS
, zc_fsprops
) != 0) {
1205 if (zc_props
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
1208 (void) strlcpy(zc
.zc_name
, pool
, sizeof (zc
.zc_name
));
1210 if ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_CREATE
, &zc
)) != 0) {
1212 zcmd_free_nvlists(&zc
);
1213 nvlist_free(zc_props
);
1214 nvlist_free(zc_fsprops
);
1219 * This can happen if the user has specified the same
1220 * device multiple times. We can't reliably detect this
1221 * until we try to add it and see we already have a
1222 * label. This can also happen under if the device is
1223 * part of an active md or lvm device.
1225 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1226 "one or more vdevs refer to the same device, or "
1227 "one of\nthe devices is part of an active md or "
1229 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1233 * This happens if the record size is smaller or larger
1234 * than the allowed size range, or not a power of 2.
1236 * NOTE: although zfs_valid_proplist is called earlier,
1237 * this case may have slipped through since the
1238 * pool does not exist yet and it is therefore
1239 * impossible to read properties e.g. max blocksize
1242 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1243 "record size invalid"));
1244 return (zfs_error(hdl
, EZFS_BADPROP
, msg
));
1248 * This occurs when one of the devices is below
1249 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1250 * device was the problem device since there's no
1251 * reliable way to determine device size from userland.
1256 zfs_nicebytes(SPA_MINDEVSIZE
, buf
,
1259 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1260 "one or more devices is less than the "
1261 "minimum size (%s)"), buf
);
1263 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1266 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1267 "one or more devices is out of space"));
1268 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1271 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1272 "cache device must be a disk or disk slice"));
1273 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1276 return (zpool_standard_error(hdl
, errno
, msg
));
1281 zcmd_free_nvlists(&zc
);
1282 nvlist_free(zc_props
);
1283 nvlist_free(zc_fsprops
);
1288 * Destroy the given pool. It is up to the caller to ensure that there are no
1289 * datasets left in the pool.
1292 zpool_destroy(zpool_handle_t
*zhp
, const char *log_str
)
1294 zfs_cmd_t zc
= {"\0"};
1295 zfs_handle_t
*zfp
= NULL
;
1296 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1299 if (zhp
->zpool_state
== POOL_STATE_ACTIVE
&&
1300 (zfp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
1303 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1304 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1306 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_DESTROY
, &zc
) != 0) {
1307 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1308 "cannot destroy '%s'"), zhp
->zpool_name
);
1310 if (errno
== EROFS
) {
1311 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1312 "one or more devices is read only"));
1313 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1315 (void) zpool_standard_error(hdl
, errno
, msg
);
1324 remove_mountpoint(zfp
);
1332 * Add the given vdevs to the pool. The caller must have already performed the
1333 * necessary verification to ensure that the vdev specification is well-formed.
1336 zpool_add(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
1338 zfs_cmd_t zc
= {"\0"};
1340 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1342 nvlist_t
**spares
, **l2cache
;
1343 uint_t nspares
, nl2cache
;
1345 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1346 "cannot add to '%s'"), zhp
->zpool_name
);
1348 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1349 SPA_VERSION_SPARES
&&
1350 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1351 &spares
, &nspares
) == 0) {
1352 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1353 "upgraded to add hot spares"));
1354 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1357 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1358 SPA_VERSION_L2CACHE
&&
1359 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1360 &l2cache
, &nl2cache
) == 0) {
1361 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1362 "upgraded to add cache devices"));
1363 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1366 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1368 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1370 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_ADD
, &zc
) != 0) {
1374 * This can happen if the user has specified the same
1375 * device multiple times. We can't reliably detect this
1376 * until we try to add it and see we already have a
1379 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1380 "one or more vdevs refer to the same device"));
1381 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1386 * This occurrs when one of the devices is below
1387 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1388 * device was the problem device since there's no
1389 * reliable way to determine device size from userland.
1394 zfs_nicebytes(SPA_MINDEVSIZE
, buf
,
1397 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1398 "device is less than the minimum "
1401 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1405 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1406 "pool must be upgraded to add these vdevs"));
1407 (void) zfs_error(hdl
, EZFS_BADVERSION
, msg
);
1411 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1412 "cache device must be a disk or disk slice"));
1413 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1417 (void) zpool_standard_error(hdl
, errno
, msg
);
1425 zcmd_free_nvlists(&zc
);
1431 * Exports the pool from the system. The caller must ensure that there are no
1432 * mounted datasets in the pool.
1435 zpool_export_common(zpool_handle_t
*zhp
, boolean_t force
, boolean_t hardforce
,
1436 const char *log_str
)
1438 zfs_cmd_t zc
= {"\0"};
1441 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1442 "cannot export '%s'"), zhp
->zpool_name
);
1444 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1445 zc
.zc_cookie
= force
;
1446 zc
.zc_guid
= hardforce
;
1447 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1449 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_EXPORT
, &zc
) != 0) {
1452 zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
1453 "use '-f' to override the following errors:\n"
1454 "'%s' has an active shared spare which could be"
1455 " used by other pools once '%s' is exported."),
1456 zhp
->zpool_name
, zhp
->zpool_name
);
1457 return (zfs_error(zhp
->zpool_hdl
, EZFS_ACTIVE_SPARE
,
1460 return (zpool_standard_error_fmt(zhp
->zpool_hdl
, errno
,
1469 zpool_export(zpool_handle_t
*zhp
, boolean_t force
, const char *log_str
)
1471 return (zpool_export_common(zhp
, force
, B_FALSE
, log_str
));
1475 zpool_export_force(zpool_handle_t
*zhp
, const char *log_str
)
1477 return (zpool_export_common(zhp
, B_TRUE
, B_TRUE
, log_str
));
1481 zpool_rewind_exclaim(libzfs_handle_t
*hdl
, const char *name
, boolean_t dryrun
,
1484 nvlist_t
*nv
= NULL
;
1490 if (!hdl
->libzfs_printerr
|| config
== NULL
)
1493 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1494 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0) {
1498 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1500 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1502 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1503 strftime(timestr
, 128, "%c", &t
) != 0) {
1505 (void) printf(dgettext(TEXT_DOMAIN
,
1506 "Would be able to return %s "
1507 "to its state as of %s.\n"),
1510 (void) printf(dgettext(TEXT_DOMAIN
,
1511 "Pool %s returned to its state as of %s.\n"),
1515 (void) printf(dgettext(TEXT_DOMAIN
,
1516 "%s approximately %lld "),
1517 dryrun
? "Would discard" : "Discarded",
1518 ((longlong_t
)loss
+ 30) / 60);
1519 (void) printf(dgettext(TEXT_DOMAIN
,
1520 "minutes of transactions.\n"));
1521 } else if (loss
> 0) {
1522 (void) printf(dgettext(TEXT_DOMAIN
,
1523 "%s approximately %lld "),
1524 dryrun
? "Would discard" : "Discarded",
1526 (void) printf(dgettext(TEXT_DOMAIN
,
1527 "seconds of transactions.\n"));
1533 zpool_explain_recover(libzfs_handle_t
*hdl
, const char *name
, int reason
,
1536 nvlist_t
*nv
= NULL
;
1538 uint64_t edata
= UINT64_MAX
;
1543 if (!hdl
->libzfs_printerr
)
1547 (void) printf(dgettext(TEXT_DOMAIN
, "action: "));
1549 (void) printf(dgettext(TEXT_DOMAIN
, "\t"));
1551 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1552 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1553 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0 ||
1554 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1557 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1558 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_DATA_ERRORS
,
1561 (void) printf(dgettext(TEXT_DOMAIN
,
1562 "Recovery is possible, but will result in some data loss.\n"));
1564 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1565 strftime(timestr
, 128, "%c", &t
) != 0) {
1566 (void) printf(dgettext(TEXT_DOMAIN
,
1567 "\tReturning the pool to its state as of %s\n"
1568 "\tshould correct the problem. "),
1571 (void) printf(dgettext(TEXT_DOMAIN
,
1572 "\tReverting the pool to an earlier state "
1573 "should correct the problem.\n\t"));
1577 (void) printf(dgettext(TEXT_DOMAIN
,
1578 "Approximately %lld minutes of data\n"
1579 "\tmust be discarded, irreversibly. "),
1580 ((longlong_t
)loss
+ 30) / 60);
1581 } else if (loss
> 0) {
1582 (void) printf(dgettext(TEXT_DOMAIN
,
1583 "Approximately %lld seconds of data\n"
1584 "\tmust be discarded, irreversibly. "),
1587 if (edata
!= 0 && edata
!= UINT64_MAX
) {
1589 (void) printf(dgettext(TEXT_DOMAIN
,
1590 "After rewind, at least\n"
1591 "\tone persistent user-data error will remain. "));
1593 (void) printf(dgettext(TEXT_DOMAIN
,
1594 "After rewind, several\n"
1595 "\tpersistent user-data errors will remain. "));
1598 (void) printf(dgettext(TEXT_DOMAIN
,
1599 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1600 reason
>= 0 ? "clear" : "import", name
);
1602 (void) printf(dgettext(TEXT_DOMAIN
,
1603 "A scrub of the pool\n"
1604 "\tis strongly recommended after recovery.\n"));
1608 (void) printf(dgettext(TEXT_DOMAIN
,
1609 "Destroy and re-create the pool from\n\ta backup source.\n"));
1613 * zpool_import() is a contracted interface. Should be kept the same
1616 * Applications should use zpool_import_props() to import a pool with
1617 * new properties value to be set.
1620 zpool_import(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1623 nvlist_t
*props
= NULL
;
1626 if (altroot
!= NULL
) {
1627 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
1628 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1629 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1633 if (nvlist_add_string(props
,
1634 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), altroot
) != 0 ||
1635 nvlist_add_string(props
,
1636 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
), "none") != 0) {
1638 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1639 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1644 ret
= zpool_import_props(hdl
, config
, newname
, props
,
1651 print_vdev_tree(libzfs_handle_t
*hdl
, const char *name
, nvlist_t
*nv
,
1657 uint64_t is_log
= 0;
1659 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
,
1663 (void) printf("\t%*s%s%s\n", indent
, "", name
,
1664 is_log
? " [log]" : "");
1666 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1667 &child
, &children
) != 0)
1670 for (c
= 0; c
< children
; c
++) {
1671 vname
= zpool_vdev_name(hdl
, NULL
, child
[c
], VDEV_NAME_TYPE_ID
);
1672 print_vdev_tree(hdl
, vname
, child
[c
], indent
+ 2);
1678 zpool_print_unsup_feat(nvlist_t
*config
)
1680 nvlist_t
*nvinfo
, *unsup_feat
;
1683 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) ==
1685 verify(nvlist_lookup_nvlist(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
,
1688 for (nvp
= nvlist_next_nvpair(unsup_feat
, NULL
); nvp
!= NULL
;
1689 nvp
= nvlist_next_nvpair(unsup_feat
, nvp
)) {
1692 verify(nvpair_type(nvp
) == DATA_TYPE_STRING
);
1693 verify(nvpair_value_string(nvp
, &desc
) == 0);
1695 if (strlen(desc
) > 0)
1696 (void) printf("\t%s (%s)\n", nvpair_name(nvp
), desc
);
1698 (void) printf("\t%s\n", nvpair_name(nvp
));
1703 * Import the given pool using the known configuration and a list of
1704 * properties to be set. The configuration should have come from
1705 * zpool_find_import(). The 'newname' parameters control whether the pool
1706 * is imported with a different name.
1709 zpool_import_props(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1710 nvlist_t
*props
, int flags
)
1712 zfs_cmd_t zc
= {"\0"};
1713 zpool_rewind_policy_t policy
;
1714 nvlist_t
*nv
= NULL
;
1715 nvlist_t
*nvinfo
= NULL
;
1716 nvlist_t
*missing
= NULL
;
1723 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1726 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1727 "cannot import pool '%s'"), origname
);
1729 if (newname
!= NULL
) {
1730 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
1731 return (zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1732 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1734 thename
= (char *)newname
;
1739 if (props
!= NULL
) {
1741 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
1743 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
1746 if ((props
= zpool_valid_proplist(hdl
, origname
,
1747 props
, version
, flags
, errbuf
)) == NULL
)
1749 if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
1756 (void) strlcpy(zc
.zc_name
, thename
, sizeof (zc
.zc_name
));
1758 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1761 if (zcmd_write_conf_nvlist(hdl
, &zc
, config
) != 0) {
1762 zcmd_free_nvlists(&zc
);
1765 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zc
.zc_nvlist_conf_size
* 2) != 0) {
1766 zcmd_free_nvlists(&zc
);
1770 zc
.zc_cookie
= flags
;
1771 while ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_IMPORT
, &zc
)) != 0 &&
1773 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
1774 zcmd_free_nvlists(&zc
);
1781 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nv
);
1783 zcmd_free_nvlists(&zc
);
1785 zpool_get_rewind_policy(config
, &policy
);
1791 * Dry-run failed, but we print out what success
1792 * looks like if we found a best txg
1794 if (policy
.zrp_request
& ZPOOL_TRY_REWIND
) {
1795 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1801 if (newname
== NULL
)
1802 (void) snprintf(desc
, sizeof (desc
),
1803 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1806 (void) snprintf(desc
, sizeof (desc
),
1807 dgettext(TEXT_DOMAIN
, "cannot import '%s' as '%s'"),
1812 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
1813 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1814 nvlist_exists(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
)) {
1815 (void) printf(dgettext(TEXT_DOMAIN
, "This "
1816 "pool uses the following feature(s) not "
1817 "supported by this system:\n"));
1818 zpool_print_unsup_feat(nv
);
1819 if (nvlist_exists(nvinfo
,
1820 ZPOOL_CONFIG_CAN_RDONLY
)) {
1821 (void) printf(dgettext(TEXT_DOMAIN
,
1822 "All unsupported features are only "
1823 "required for writing to the pool."
1824 "\nThe pool can be imported using "
1825 "'-o readonly=on'.\n"));
1829 * Unsupported version.
1831 (void) zfs_error(hdl
, EZFS_BADVERSION
, desc
);
1835 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, desc
);
1839 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1840 "one or more devices is read only"));
1841 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
1845 if (nv
&& nvlist_lookup_nvlist(nv
,
1846 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1847 nvlist_lookup_nvlist(nvinfo
,
1848 ZPOOL_CONFIG_MISSING_DEVICES
, &missing
) == 0) {
1849 (void) printf(dgettext(TEXT_DOMAIN
,
1850 "The devices below are missing, use "
1851 "'-m' to import the pool anyway:\n"));
1852 print_vdev_tree(hdl
, NULL
, missing
, 2);
1853 (void) printf("\n");
1855 (void) zpool_standard_error(hdl
, error
, desc
);
1859 (void) zpool_standard_error(hdl
, error
, desc
);
1863 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1864 "one or more devices are already in use\n"));
1865 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
1868 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1869 "new name of at least one dataset is longer than "
1870 "the maximum allowable length"));
1871 (void) zfs_error(hdl
, EZFS_NAMETOOLONG
, desc
);
1874 (void) zpool_standard_error(hdl
, error
, desc
);
1875 zpool_explain_recover(hdl
,
1876 newname
? origname
: thename
, -error
, nv
);
1883 zpool_handle_t
*zhp
;
1886 * This should never fail, but play it safe anyway.
1888 if (zpool_open_silent(hdl
, thename
, &zhp
) != 0)
1890 else if (zhp
!= NULL
)
1892 if (policy
.zrp_request
&
1893 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
1894 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1895 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0), nv
);
1908 zpool_scan(zpool_handle_t
*zhp
, pool_scan_func_t func
)
1910 zfs_cmd_t zc
= {"\0"};
1912 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1914 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1915 zc
.zc_cookie
= func
;
1917 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_SCAN
, &zc
) == 0 ||
1918 (errno
== ENOENT
&& func
!= POOL_SCAN_NONE
))
1921 if (func
== POOL_SCAN_SCRUB
) {
1922 (void) snprintf(msg
, sizeof (msg
),
1923 dgettext(TEXT_DOMAIN
, "cannot scrub %s"), zc
.zc_name
);
1924 } else if (func
== POOL_SCAN_NONE
) {
1925 (void) snprintf(msg
, sizeof (msg
),
1926 dgettext(TEXT_DOMAIN
, "cannot cancel scrubbing %s"),
1929 assert(!"unexpected result");
1932 if (errno
== EBUSY
) {
1934 pool_scan_stat_t
*ps
= NULL
;
1937 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
1938 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
1939 (void) nvlist_lookup_uint64_array(nvroot
,
1940 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &psc
);
1941 if (ps
&& ps
->pss_func
== POOL_SCAN_SCRUB
)
1942 return (zfs_error(hdl
, EZFS_SCRUBBING
, msg
));
1944 return (zfs_error(hdl
, EZFS_RESILVERING
, msg
));
1945 } else if (errno
== ENOENT
) {
1946 return (zfs_error(hdl
, EZFS_NO_SCRUB
, msg
));
1948 return (zpool_standard_error(hdl
, errno
, msg
));
1953 * Find a vdev that matches the search criteria specified. We use the
1954 * the nvpair name to determine how we should look for the device.
1955 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
1956 * spare; but FALSE if its an INUSE spare.
1959 vdev_to_nvlist_iter(nvlist_t
*nv
, nvlist_t
*search
, boolean_t
*avail_spare
,
1960 boolean_t
*l2cache
, boolean_t
*log
)
1967 nvpair_t
*pair
= nvlist_next_nvpair(search
, NULL
);
1969 /* Nothing to look for */
1970 if (search
== NULL
|| pair
== NULL
)
1973 /* Obtain the key we will use to search */
1974 srchkey
= nvpair_name(pair
);
1976 switch (nvpair_type(pair
)) {
1977 case DATA_TYPE_UINT64
:
1978 if (strcmp(srchkey
, ZPOOL_CONFIG_GUID
) == 0) {
1979 uint64_t srchval
, theguid
;
1981 verify(nvpair_value_uint64(pair
, &srchval
) == 0);
1982 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
1984 if (theguid
== srchval
)
1989 case DATA_TYPE_STRING
: {
1990 char *srchval
, *val
;
1992 verify(nvpair_value_string(pair
, &srchval
) == 0);
1993 if (nvlist_lookup_string(nv
, srchkey
, &val
) != 0)
1997 * Search for the requested value. Special cases:
1999 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2000 * "-part1", or "p1". The suffix is hidden from the user,
2001 * but included in the string, so this matches around it.
2002 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2003 * is used to check all possible expanded paths.
2004 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2006 * Otherwise, all other searches are simple string compares.
2008 if (strcmp(srchkey
, ZPOOL_CONFIG_PATH
) == 0) {
2009 uint64_t wholedisk
= 0;
2011 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
2013 if (zfs_strcmp_pathname(srchval
, val
, wholedisk
) == 0)
2016 } else if (strcmp(srchkey
, ZPOOL_CONFIG_TYPE
) == 0 && val
) {
2017 char *type
, *idx
, *end
, *p
;
2018 uint64_t id
, vdev_id
;
2021 * Determine our vdev type, keeping in mind
2022 * that the srchval is composed of a type and
2023 * vdev id pair (i.e. mirror-4).
2025 if ((type
= strdup(srchval
)) == NULL
)
2028 if ((p
= strrchr(type
, '-')) == NULL
) {
2036 * If the types don't match then keep looking.
2038 if (strncmp(val
, type
, strlen(val
)) != 0) {
2043 verify(strncmp(type
, VDEV_TYPE_RAIDZ
,
2044 strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2045 strncmp(type
, VDEV_TYPE_MIRROR
,
2046 strlen(VDEV_TYPE_MIRROR
)) == 0);
2047 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
2051 vdev_id
= strtoull(idx
, &end
, 10);
2058 * Now verify that we have the correct vdev id.
2067 if (strcmp(srchval
, val
) == 0)
2076 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2077 &child
, &children
) != 0)
2080 for (c
= 0; c
< children
; c
++) {
2081 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2082 avail_spare
, l2cache
, NULL
)) != NULL
) {
2084 * The 'is_log' value is only set for the toplevel
2085 * vdev, not the leaf vdevs. So we always lookup the
2086 * log device from the root of the vdev tree (where
2087 * 'log' is non-NULL).
2090 nvlist_lookup_uint64(child
[c
],
2091 ZPOOL_CONFIG_IS_LOG
, &is_log
) == 0 &&
2099 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2100 &child
, &children
) == 0) {
2101 for (c
= 0; c
< children
; c
++) {
2102 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2103 avail_spare
, l2cache
, NULL
)) != NULL
) {
2104 *avail_spare
= B_TRUE
;
2110 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2111 &child
, &children
) == 0) {
2112 for (c
= 0; c
< children
; c
++) {
2113 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2114 avail_spare
, l2cache
, NULL
)) != NULL
) {
2125 * Given a physical path (minus the "/devices" prefix), find the
2129 zpool_find_vdev_by_physpath(zpool_handle_t
*zhp
, const char *ppath
,
2130 boolean_t
*avail_spare
, boolean_t
*l2cache
, boolean_t
*log
)
2132 nvlist_t
*search
, *nvroot
, *ret
;
2134 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2135 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PHYS_PATH
, ppath
) == 0);
2137 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2140 *avail_spare
= B_FALSE
;
2144 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2145 nvlist_free(search
);
2151 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2154 zpool_vdev_is_interior(const char *name
)
2156 if (strncmp(name
, VDEV_TYPE_RAIDZ
, strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2157 strncmp(name
, VDEV_TYPE_MIRROR
, strlen(VDEV_TYPE_MIRROR
)) == 0)
2163 zpool_find_vdev(zpool_handle_t
*zhp
, const char *path
, boolean_t
*avail_spare
,
2164 boolean_t
*l2cache
, boolean_t
*log
)
2167 nvlist_t
*nvroot
, *search
, *ret
;
2170 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2172 guid
= strtoull(path
, &end
, 0);
2173 if (guid
!= 0 && *end
== '\0') {
2174 verify(nvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
) == 0);
2175 } else if (zpool_vdev_is_interior(path
)) {
2176 verify(nvlist_add_string(search
, ZPOOL_CONFIG_TYPE
, path
) == 0);
2178 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PATH
, path
) == 0);
2181 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2184 *avail_spare
= B_FALSE
;
2188 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2189 nvlist_free(search
);
2195 vdev_online(nvlist_t
*nv
)
2199 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
, &ival
) == 0 ||
2200 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
, &ival
) == 0 ||
2201 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
, &ival
) == 0)
2208 * Helper function for zpool_get_physpaths().
2211 vdev_get_one_physpath(nvlist_t
*config
, char *physpath
, size_t physpath_size
,
2212 size_t *bytes_written
)
2214 size_t bytes_left
, pos
, rsz
;
2218 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PHYS_PATH
,
2220 return (EZFS_NODEVICE
);
2222 pos
= *bytes_written
;
2223 bytes_left
= physpath_size
- pos
;
2224 format
= (pos
== 0) ? "%s" : " %s";
2226 rsz
= snprintf(physpath
+ pos
, bytes_left
, format
, tmppath
);
2227 *bytes_written
+= rsz
;
2229 if (rsz
>= bytes_left
) {
2230 /* if physpath was not copied properly, clear it */
2231 if (bytes_left
!= 0) {
2234 return (EZFS_NOSPC
);
2240 vdev_get_physpaths(nvlist_t
*nv
, char *physpath
, size_t phypath_size
,
2241 size_t *rsz
, boolean_t is_spare
)
2246 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
2247 return (EZFS_INVALCONFIG
);
2249 if (strcmp(type
, VDEV_TYPE_DISK
) == 0) {
2251 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2252 * For a spare vdev, we only want to boot from the active
2257 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
2260 return (EZFS_INVALCONFIG
);
2263 if (vdev_online(nv
)) {
2264 if ((ret
= vdev_get_one_physpath(nv
, physpath
,
2265 phypath_size
, rsz
)) != 0)
2268 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) == 0 ||
2269 strcmp(type
, VDEV_TYPE_RAIDZ
) == 0 ||
2270 strcmp(type
, VDEV_TYPE_REPLACING
) == 0 ||
2271 (is_spare
= (strcmp(type
, VDEV_TYPE_SPARE
) == 0))) {
2276 if (nvlist_lookup_nvlist_array(nv
,
2277 ZPOOL_CONFIG_CHILDREN
, &child
, &count
) != 0)
2278 return (EZFS_INVALCONFIG
);
2280 for (i
= 0; i
< count
; i
++) {
2281 ret
= vdev_get_physpaths(child
[i
], physpath
,
2282 phypath_size
, rsz
, is_spare
);
2283 if (ret
== EZFS_NOSPC
)
2288 return (EZFS_POOL_INVALARG
);
2292 * Get phys_path for a root pool config.
2293 * Return 0 on success; non-zero on failure.
2296 zpool_get_config_physpath(nvlist_t
*config
, char *physpath
, size_t phypath_size
)
2299 nvlist_t
*vdev_root
;
2306 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2308 return (EZFS_INVALCONFIG
);
2310 if (nvlist_lookup_string(vdev_root
, ZPOOL_CONFIG_TYPE
, &type
) != 0 ||
2311 nvlist_lookup_nvlist_array(vdev_root
, ZPOOL_CONFIG_CHILDREN
,
2312 &child
, &count
) != 0)
2313 return (EZFS_INVALCONFIG
);
2316 * root pool can only have a single top-level vdev.
2318 if (strcmp(type
, VDEV_TYPE_ROOT
) != 0 || count
!= 1)
2319 return (EZFS_POOL_INVALARG
);
2321 (void) vdev_get_physpaths(child
[0], physpath
, phypath_size
, &rsz
,
2324 /* No online devices */
2326 return (EZFS_NODEVICE
);
2332 * Get phys_path for a root pool
2333 * Return 0 on success; non-zero on failure.
2336 zpool_get_physpath(zpool_handle_t
*zhp
, char *physpath
, size_t phypath_size
)
2338 return (zpool_get_config_physpath(zhp
->zpool_config
, physpath
,
2343 * If the device has being dynamically expanded then we need to relabel
2344 * the disk to use the new unallocated space.
2347 zpool_relabel_disk(libzfs_handle_t
*hdl
, const char *path
, const char *msg
)
2351 if ((fd
= open(path
, O_RDWR
|O_DIRECT
)) < 0) {
2352 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2353 "relabel '%s': unable to open device: %d"), path
, errno
);
2354 return (zfs_error(hdl
, EZFS_OPENFAILED
, msg
));
2358 * It's possible that we might encounter an error if the device
2359 * does not have any unallocated space left. If so, we simply
2360 * ignore that error and continue on.
2362 * Also, we don't call efi_rescan() - that would just return EBUSY.
2363 * The module will do it for us in vdev_disk_open().
2365 error
= efi_use_whole_disk(fd
);
2367 /* Flush the buffers to disk and invalidate the page cache. */
2369 (void) ioctl(fd
, BLKFLSBUF
);
2372 if (error
&& error
!= VT_ENOSPC
) {
2373 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2374 "relabel '%s': unable to read disk capacity"), path
);
2375 return (zfs_error(hdl
, EZFS_NOCAP
, msg
));
2382 * Bring the specified vdev online. The 'flags' parameter is a set of the
2383 * ZFS_ONLINE_* flags.
2386 zpool_vdev_online(zpool_handle_t
*zhp
, const char *path
, int flags
,
2387 vdev_state_t
*newstate
)
2389 zfs_cmd_t zc
= {"\0"};
2392 boolean_t avail_spare
, l2cache
, islog
;
2393 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2396 if (flags
& ZFS_ONLINE_EXPAND
) {
2397 (void) snprintf(msg
, sizeof (msg
),
2398 dgettext(TEXT_DOMAIN
, "cannot expand %s"), path
);
2400 (void) snprintf(msg
, sizeof (msg
),
2401 dgettext(TEXT_DOMAIN
, "cannot online %s"), path
);
2404 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2405 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2407 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2409 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2412 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2414 if (flags
& ZFS_ONLINE_EXPAND
||
2415 zpool_get_prop_int(zhp
, ZPOOL_PROP_AUTOEXPAND
, NULL
)) {
2416 uint64_t wholedisk
= 0;
2418 (void) nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_WHOLE_DISK
,
2422 * XXX - L2ARC 1.0 devices can't support expansion.
2425 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2426 "cannot expand cache devices"));
2427 return (zfs_error(hdl
, EZFS_VDEVNOTSUP
, msg
));
2431 const char *fullpath
= path
;
2432 char buf
[MAXPATHLEN
];
2434 if (path
[0] != '/') {
2435 error
= zfs_resolve_shortname(path
, buf
,
2438 return (zfs_error(hdl
, EZFS_NODEVICE
,
2444 error
= zpool_relabel_disk(hdl
, fullpath
, msg
);
2450 zc
.zc_cookie
= VDEV_STATE_ONLINE
;
2453 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) != 0) {
2454 if (errno
== EINVAL
) {
2455 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "was split "
2456 "from this pool into a new one. Use '%s' "
2457 "instead"), "zpool detach");
2458 return (zfs_error(hdl
, EZFS_POSTSPLIT_ONLINE
, msg
));
2460 return (zpool_standard_error(hdl
, errno
, msg
));
2463 *newstate
= zc
.zc_cookie
;
2468 * Take the specified vdev offline
2471 zpool_vdev_offline(zpool_handle_t
*zhp
, const char *path
, boolean_t istmp
)
2473 zfs_cmd_t zc
= {"\0"};
2476 boolean_t avail_spare
, l2cache
;
2477 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2479 (void) snprintf(msg
, sizeof (msg
),
2480 dgettext(TEXT_DOMAIN
, "cannot offline %s"), path
);
2482 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2483 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2485 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2487 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2490 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2492 zc
.zc_cookie
= VDEV_STATE_OFFLINE
;
2493 zc
.zc_obj
= istmp
? ZFS_OFFLINE_TEMPORARY
: 0;
2495 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2502 * There are no other replicas of this device.
2504 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2508 * The log device has unplayed logs
2510 return (zfs_error(hdl
, EZFS_UNPLAYED_LOGS
, msg
));
2513 return (zpool_standard_error(hdl
, errno
, msg
));
2518 * Mark the given vdev faulted.
2521 zpool_vdev_fault(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2523 zfs_cmd_t zc
= {"\0"};
2525 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2527 (void) snprintf(msg
, sizeof (msg
),
2528 dgettext(TEXT_DOMAIN
, "cannot fault %llu"), (u_longlong_t
)guid
);
2530 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2532 zc
.zc_cookie
= VDEV_STATE_FAULTED
;
2535 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2542 * There are no other replicas of this device.
2544 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2547 return (zpool_standard_error(hdl
, errno
, msg
));
2553 * Mark the given vdev degraded.
2556 zpool_vdev_degrade(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2558 zfs_cmd_t zc
= {"\0"};
2560 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2562 (void) snprintf(msg
, sizeof (msg
),
2563 dgettext(TEXT_DOMAIN
, "cannot degrade %llu"), (u_longlong_t
)guid
);
2565 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2567 zc
.zc_cookie
= VDEV_STATE_DEGRADED
;
2570 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2573 return (zpool_standard_error(hdl
, errno
, msg
));
2577 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2581 is_replacing_spare(nvlist_t
*search
, nvlist_t
*tgt
, int which
)
2587 if (nvlist_lookup_nvlist_array(search
, ZPOOL_CONFIG_CHILDREN
, &child
,
2589 verify(nvlist_lookup_string(search
, ZPOOL_CONFIG_TYPE
,
2592 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0 &&
2593 children
== 2 && child
[which
] == tgt
)
2596 for (c
= 0; c
< children
; c
++)
2597 if (is_replacing_spare(child
[c
], tgt
, which
))
2605 * Attach new_disk (fully described by nvroot) to old_disk.
2606 * If 'replacing' is specified, the new disk will replace the old one.
2609 zpool_vdev_attach(zpool_handle_t
*zhp
,
2610 const char *old_disk
, const char *new_disk
, nvlist_t
*nvroot
, int replacing
)
2612 zfs_cmd_t zc
= {"\0"};
2616 boolean_t avail_spare
, l2cache
, islog
;
2621 nvlist_t
*config_root
;
2622 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2623 boolean_t rootpool
= zpool_is_bootable(zhp
);
2626 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2627 "cannot replace %s with %s"), old_disk
, new_disk
);
2629 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2630 "cannot attach %s to %s"), new_disk
, old_disk
);
2632 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2633 if ((tgt
= zpool_find_vdev(zhp
, old_disk
, &avail_spare
, &l2cache
,
2635 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2638 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2641 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2643 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2644 zc
.zc_cookie
= replacing
;
2646 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
2647 &child
, &children
) != 0 || children
!= 1) {
2648 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2649 "new device must be a single disk"));
2650 return (zfs_error(hdl
, EZFS_INVALCONFIG
, msg
));
2653 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
2654 ZPOOL_CONFIG_VDEV_TREE
, &config_root
) == 0);
2656 if ((newname
= zpool_vdev_name(NULL
, NULL
, child
[0], 0)) == NULL
)
2660 * If the target is a hot spare that has been swapped in, we can only
2661 * replace it with another hot spare.
2664 nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_IS_SPARE
, &val
) == 0 &&
2665 (zpool_find_vdev(zhp
, newname
, &avail_spare
, &l2cache
,
2666 NULL
) == NULL
|| !avail_spare
) &&
2667 is_replacing_spare(config_root
, tgt
, 1)) {
2668 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2669 "can only be replaced by another hot spare"));
2671 return (zfs_error(hdl
, EZFS_BADTARGET
, msg
));
2676 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
2679 ret
= zfs_ioctl(hdl
, ZFS_IOC_VDEV_ATTACH
, &zc
);
2681 zcmd_free_nvlists(&zc
);
2686 * XXX need a better way to prevent user from
2687 * booting up a half-baked vdev.
2689 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Make "
2690 "sure to wait until resilver is done "
2691 "before rebooting.\n"));
2699 * Can't attach to or replace this type of vdev.
2702 uint64_t version
= zpool_get_prop_int(zhp
,
2703 ZPOOL_PROP_VERSION
, NULL
);
2706 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2707 "cannot replace a log with a spare"));
2708 else if (version
>= SPA_VERSION_MULTI_REPLACE
)
2709 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2710 "already in replacing/spare config; wait "
2711 "for completion or use 'zpool detach'"));
2713 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2714 "cannot replace a replacing device"));
2716 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2717 "can only attach to mirrors and top-level "
2720 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2725 * The new device must be a single disk.
2727 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2728 "new device must be a single disk"));
2729 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2733 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "%s is busy"),
2735 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2740 * The new device is too small.
2742 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2743 "device is too small"));
2744 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2749 * The new device has a different optimal sector size.
2751 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2752 "new device has a different optimal sector size; use the "
2753 "option '-o ashift=N' to override the optimal size"));
2754 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2759 * The resulting top-level vdev spec won't fit in the label.
2761 (void) zfs_error(hdl
, EZFS_DEVOVERFLOW
, msg
);
2765 (void) zpool_standard_error(hdl
, errno
, msg
);
2772 * Detach the specified device.
2775 zpool_vdev_detach(zpool_handle_t
*zhp
, const char *path
)
2777 zfs_cmd_t zc
= {"\0"};
2780 boolean_t avail_spare
, l2cache
;
2781 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2783 (void) snprintf(msg
, sizeof (msg
),
2784 dgettext(TEXT_DOMAIN
, "cannot detach %s"), path
);
2786 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2787 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2789 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2792 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2795 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2797 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2799 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_DETACH
, &zc
) == 0)
2806 * Can't detach from this type of vdev.
2808 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "only "
2809 "applicable to mirror and replacing vdevs"));
2810 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2815 * There are no other replicas of this device.
2817 (void) zfs_error(hdl
, EZFS_NOREPLICAS
, msg
);
2821 (void) zpool_standard_error(hdl
, errno
, msg
);
2828 * Find a mirror vdev in the source nvlist.
2830 * The mchild array contains a list of disks in one of the top-level mirrors
2831 * of the source pool. The schild array contains a list of disks that the
2832 * user specified on the command line. We loop over the mchild array to
2833 * see if any entry in the schild array matches.
2835 * If a disk in the mchild array is found in the schild array, we return
2836 * the index of that entry. Otherwise we return -1.
2839 find_vdev_entry(zpool_handle_t
*zhp
, nvlist_t
**mchild
, uint_t mchildren
,
2840 nvlist_t
**schild
, uint_t schildren
)
2844 for (mc
= 0; mc
< mchildren
; mc
++) {
2846 char *mpath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
2849 for (sc
= 0; sc
< schildren
; sc
++) {
2850 char *spath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
2852 boolean_t result
= (strcmp(mpath
, spath
) == 0);
2868 * Split a mirror pool. If newroot points to null, then a new nvlist
2869 * is generated and it is the responsibility of the caller to free it.
2872 zpool_vdev_split(zpool_handle_t
*zhp
, char *newname
, nvlist_t
**newroot
,
2873 nvlist_t
*props
, splitflags_t flags
)
2875 zfs_cmd_t zc
= {"\0"};
2877 nvlist_t
*tree
, *config
, **child
, **newchild
, *newconfig
= NULL
;
2878 nvlist_t
**varray
= NULL
, *zc_props
= NULL
;
2879 uint_t c
, children
, newchildren
, lastlog
= 0, vcount
, found
= 0;
2880 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2882 boolean_t freelist
= B_FALSE
, memory_err
= B_TRUE
;
2885 (void) snprintf(msg
, sizeof (msg
),
2886 dgettext(TEXT_DOMAIN
, "Unable to split %s"), zhp
->zpool_name
);
2888 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
2889 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
2891 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
2892 (void) fprintf(stderr
, gettext("Internal error: unable to "
2893 "retrieve pool configuration\n"));
2897 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &tree
)
2899 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
, &vers
) == 0);
2902 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
2903 if ((zc_props
= zpool_valid_proplist(hdl
, zhp
->zpool_name
,
2904 props
, vers
, flags
, msg
)) == NULL
)
2908 if (nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
2910 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2911 "Source pool is missing vdev tree"));
2912 nvlist_free(zc_props
);
2916 varray
= zfs_alloc(hdl
, children
* sizeof (nvlist_t
*));
2919 if (*newroot
== NULL
||
2920 nvlist_lookup_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
2921 &newchild
, &newchildren
) != 0)
2924 for (c
= 0; c
< children
; c
++) {
2925 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
2927 nvlist_t
**mchild
, *vdev
;
2932 * Unlike cache & spares, slogs are stored in the
2933 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
2935 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
2937 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
2939 if (is_log
|| is_hole
) {
2941 * Create a hole vdev and put it in the config.
2943 if (nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) != 0)
2945 if (nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
,
2946 VDEV_TYPE_HOLE
) != 0)
2948 if (nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_HOLE
,
2953 varray
[vcount
++] = vdev
;
2957 verify(nvlist_lookup_string(child
[c
], ZPOOL_CONFIG_TYPE
, &type
)
2959 if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
2960 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2961 "Source pool must be composed only of mirrors\n"));
2962 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2966 verify(nvlist_lookup_nvlist_array(child
[c
],
2967 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
2969 /* find or add an entry for this top-level vdev */
2970 if (newchildren
> 0 &&
2971 (entry
= find_vdev_entry(zhp
, mchild
, mchildren
,
2972 newchild
, newchildren
)) >= 0) {
2973 /* We found a disk that the user specified. */
2974 vdev
= mchild
[entry
];
2977 /* User didn't specify a disk for this vdev. */
2978 vdev
= mchild
[mchildren
- 1];
2981 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
2985 /* did we find every disk the user specified? */
2986 if (found
!= newchildren
) {
2987 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Device list must "
2988 "include at most one disk from each mirror"));
2989 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2993 /* Prepare the nvlist for populating. */
2994 if (*newroot
== NULL
) {
2995 if (nvlist_alloc(newroot
, NV_UNIQUE_NAME
, 0) != 0)
2998 if (nvlist_add_string(*newroot
, ZPOOL_CONFIG_TYPE
,
2999 VDEV_TYPE_ROOT
) != 0)
3002 verify(nvlist_remove_all(*newroot
, ZPOOL_CONFIG_CHILDREN
) == 0);
3005 /* Add all the children we found */
3006 if (nvlist_add_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
, varray
,
3007 lastlog
== 0 ? vcount
: lastlog
) != 0)
3011 * If we're just doing a dry run, exit now with success.
3014 memory_err
= B_FALSE
;
3019 /* now build up the config list & call the ioctl */
3020 if (nvlist_alloc(&newconfig
, NV_UNIQUE_NAME
, 0) != 0)
3023 if (nvlist_add_nvlist(newconfig
,
3024 ZPOOL_CONFIG_VDEV_TREE
, *newroot
) != 0 ||
3025 nvlist_add_string(newconfig
,
3026 ZPOOL_CONFIG_POOL_NAME
, newname
) != 0 ||
3027 nvlist_add_uint64(newconfig
, ZPOOL_CONFIG_VERSION
, vers
) != 0)
3031 * The new pool is automatically part of the namespace unless we
3032 * explicitly export it.
3035 zc
.zc_cookie
= ZPOOL_EXPORT_AFTER_SPLIT
;
3036 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3037 (void) strlcpy(zc
.zc_string
, newname
, sizeof (zc
.zc_string
));
3038 if (zcmd_write_conf_nvlist(hdl
, &zc
, newconfig
) != 0)
3040 if (zc_props
!= NULL
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
3043 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SPLIT
, &zc
) != 0) {
3044 retval
= zpool_standard_error(hdl
, errno
, msg
);
3049 memory_err
= B_FALSE
;
3052 if (varray
!= NULL
) {
3055 for (v
= 0; v
< vcount
; v
++)
3056 nvlist_free(varray
[v
]);
3059 zcmd_free_nvlists(&zc
);
3060 nvlist_free(zc_props
);
3061 nvlist_free(newconfig
);
3063 nvlist_free(*newroot
);
3071 return (no_memory(hdl
));
3077 * Remove the given device. Currently, this is supported only for hot spares,
3078 * cache, and log devices.
3081 zpool_vdev_remove(zpool_handle_t
*zhp
, const char *path
)
3083 zfs_cmd_t zc
= {"\0"};
3086 boolean_t avail_spare
, l2cache
, islog
;
3087 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3090 (void) snprintf(msg
, sizeof (msg
),
3091 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3093 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3094 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3096 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3098 * XXX - this should just go away.
3100 if (!avail_spare
&& !l2cache
&& !islog
) {
3101 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3102 "only inactive hot spares, cache, "
3103 "or log devices can be removed"));
3104 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3107 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
3108 if (islog
&& version
< SPA_VERSION_HOLES
) {
3109 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3110 "pool must be upgrade to support log removal"));
3111 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
3114 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
3116 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3119 return (zpool_standard_error(hdl
, errno
, msg
));
3123 * Clear the errors for the pool, or the particular device if specified.
3126 zpool_clear(zpool_handle_t
*zhp
, const char *path
, nvlist_t
*rewindnvl
)
3128 zfs_cmd_t zc
= {"\0"};
3131 zpool_rewind_policy_t policy
;
3132 boolean_t avail_spare
, l2cache
;
3133 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3134 nvlist_t
*nvi
= NULL
;
3138 (void) snprintf(msg
, sizeof (msg
),
3139 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3142 (void) snprintf(msg
, sizeof (msg
),
3143 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3146 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3148 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
,
3149 &l2cache
, NULL
)) == 0)
3150 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3153 * Don't allow error clearing for hot spares. Do allow
3154 * error clearing for l2cache devices.
3157 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3159 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
,
3163 zpool_get_rewind_policy(rewindnvl
, &policy
);
3164 zc
.zc_cookie
= policy
.zrp_request
;
3166 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zhp
->zpool_config_size
* 2) != 0)
3169 if (zcmd_write_src_nvlist(hdl
, &zc
, rewindnvl
) != 0)
3172 while ((error
= zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
)) != 0 &&
3174 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
3175 zcmd_free_nvlists(&zc
);
3180 if (!error
|| ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) &&
3181 errno
!= EPERM
&& errno
!= EACCES
)) {
3182 if (policy
.zrp_request
&
3183 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
3184 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nvi
);
3185 zpool_rewind_exclaim(hdl
, zc
.zc_name
,
3186 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0),
3190 zcmd_free_nvlists(&zc
);
3194 zcmd_free_nvlists(&zc
);
3195 return (zpool_standard_error(hdl
, errno
, msg
));
3199 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3202 zpool_vdev_clear(zpool_handle_t
*zhp
, uint64_t guid
)
3204 zfs_cmd_t zc
= {"\0"};
3206 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3208 (void) snprintf(msg
, sizeof (msg
),
3209 dgettext(TEXT_DOMAIN
, "cannot clear errors for %llx"),
3210 (u_longlong_t
)guid
);
3212 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3214 zc
.zc_cookie
= ZPOOL_NO_REWIND
;
3216 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_CLEAR
, &zc
) == 0)
3219 return (zpool_standard_error(hdl
, errno
, msg
));
3223 * Change the GUID for a pool.
3226 zpool_reguid(zpool_handle_t
*zhp
)
3229 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3230 zfs_cmd_t zc
= {"\0"};
3232 (void) snprintf(msg
, sizeof (msg
),
3233 dgettext(TEXT_DOMAIN
, "cannot reguid '%s'"), zhp
->zpool_name
);
3235 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3236 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REGUID
, &zc
) == 0)
3239 return (zpool_standard_error(hdl
, errno
, msg
));
3246 zpool_reopen(zpool_handle_t
*zhp
)
3248 zfs_cmd_t zc
= {"\0"};
3250 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3252 (void) snprintf(msg
, sizeof (msg
),
3253 dgettext(TEXT_DOMAIN
, "cannot reopen '%s'"),
3256 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3257 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REOPEN
, &zc
) == 0)
3259 return (zpool_standard_error(hdl
, errno
, msg
));
3262 #if defined(__sun__) || defined(__sun)
3264 * Convert from a devid string to a path.
3267 devid_to_path(char *devid_str
)
3272 devid_nmlist_t
*list
= NULL
;
3275 if (devid_str_decode(devid_str
, &devid
, &minor
) != 0)
3278 ret
= devid_deviceid_to_nmlist("/dev", devid
, minor
, &list
);
3280 devid_str_free(minor
);
3287 * In a case the strdup() fails, we will just return NULL below.
3289 path
= strdup(list
[0].devname
);
3291 devid_free_nmlist(list
);
3297 * Convert from a path to a devid string.
3300 path_to_devid(const char *path
)
3306 if ((fd
= open(path
, O_RDONLY
)) < 0)
3311 if (devid_get(fd
, &devid
) == 0) {
3312 if (devid_get_minor_name(fd
, &minor
) == 0)
3313 ret
= devid_str_encode(devid
, minor
);
3315 devid_str_free(minor
);
3324 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3325 * ignore any failure here, since a common case is for an unprivileged user to
3326 * type 'zpool status', and we'll display the correct information anyway.
3329 set_path(zpool_handle_t
*zhp
, nvlist_t
*nv
, const char *path
)
3331 zfs_cmd_t zc
= {"\0"};
3333 (void) strncpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3334 (void) strncpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
3335 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
3338 (void) ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_VDEV_SETPATH
, &zc
);
3343 * Remove partition suffix from a vdev path. Partition suffixes may take three
3344 * forms: "-partX", "pX", or "X", where X is a string of digits. The second
3345 * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
3346 * third case only occurs when preceded by a string matching the regular
3347 * expression "^([hsv]|xv)d[a-z]+", i.e. a scsi, ide, virtio or xen disk.
3349 * caller must free the returned string
3352 zfs_strip_partition(char *path
)
3354 char *tmp
= strdup(path
);
3355 char *part
= NULL
, *d
= NULL
;
3359 if ((part
= strstr(tmp
, "-part")) && part
!= tmp
) {
3361 } else if ((part
= strrchr(tmp
, 'p')) &&
3362 part
> tmp
+ 1 && isdigit(*(part
-1))) {
3364 } else if ((tmp
[0] == 'h' || tmp
[0] == 's' || tmp
[0] == 'v') &&
3366 for (d
= &tmp
[2]; isalpha(*d
); part
= ++d
) { }
3367 } else if (strncmp("xvd", tmp
, 3) == 0) {
3368 for (d
= &tmp
[3]; isalpha(*d
); part
= ++d
) { }
3370 if (part
&& d
&& *d
!= '\0') {
3371 for (; isdigit(*d
); d
++) { }
3380 * Same as zfs_strip_partition, but allows "/dev/" to be in the pathname
3385 * Returned string must be freed.
3388 zfs_strip_partition_path(char *path
)
3390 char *newpath
= strdup(path
);
3397 /* Point to "sda1" part of "/dev/sda1" */
3398 sd_offset
= strrchr(newpath
, '/') + 1;
3400 /* Get our new name "sda" */
3401 new_sd
= zfs_strip_partition(sd_offset
);
3407 /* Paste the "sda" where "sda1" was */
3408 strlcpy(sd_offset
, new_sd
, strlen(sd_offset
) + 1);
3410 /* Free temporary "sda" */
3416 #define PATH_BUF_LEN 64
3419 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3420 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3421 * We also check if this is a whole disk, in which case we strip off the
3422 * trailing 's0' slice name.
3424 * This routine is also responsible for identifying when disks have been
3425 * reconfigured in a new location. The kernel will have opened the device by
3426 * devid, but the path will still refer to the old location. To catch this, we
3427 * first do a path -> devid translation (which is fast for the common case). If
3428 * the devid matches, we're done. If not, we do a reverse devid -> path
3429 * translation and issue the appropriate ioctl() to update the path of the vdev.
3430 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3434 zpool_vdev_name(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, nvlist_t
*nv
,
3437 char *path
, *type
, *env
;
3439 char buf
[PATH_BUF_LEN
];
3440 char tmpbuf
[PATH_BUF_LEN
];
3442 env
= getenv("ZPOOL_VDEV_NAME_PATH");
3443 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3444 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3445 name_flags
|= VDEV_NAME_PATH
;
3447 env
= getenv("ZPOOL_VDEV_NAME_GUID");
3448 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3449 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3450 name_flags
|= VDEV_NAME_GUID
;
3452 env
= getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
3453 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3454 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3455 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
3457 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
, &value
) == 0 ||
3458 name_flags
& VDEV_NAME_GUID
) {
3459 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
);
3460 (void) snprintf(buf
, sizeof (buf
), "%llu", (u_longlong_t
)value
);
3462 } else if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
3463 #if defined(__sun__) || defined(__sun)
3465 * Live VDEV path updates to a kernel VDEV during a
3466 * zpool_vdev_name lookup are not supported on Linux.
3473 * If the device is dead (faulted, offline, etc) then don't
3474 * bother opening it. Otherwise we may be forcing the user to
3475 * open a misbehaving device, which can have undesirable
3478 if ((nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
3479 (uint64_t **)&vs
, &vsc
) != 0 ||
3480 vs
->vs_state
>= VDEV_STATE_DEGRADED
) &&
3482 nvlist_lookup_string(nv
, ZPOOL_CONFIG_DEVID
, &devid
) == 0) {
3484 * Determine if the current path is correct.
3486 char *newdevid
= path_to_devid(path
);
3488 if (newdevid
== NULL
||
3489 strcmp(devid
, newdevid
) != 0) {
3492 if ((newpath
= devid_to_path(devid
)) != NULL
) {
3494 * Update the path appropriately.
3496 set_path(zhp
, nv
, newpath
);
3497 if (nvlist_add_string(nv
,
3498 ZPOOL_CONFIG_PATH
, newpath
) == 0)
3499 verify(nvlist_lookup_string(nv
,
3507 devid_str_free(newdevid
);
3511 if (name_flags
& VDEV_NAME_FOLLOW_LINKS
) {
3512 char *rp
= realpath(path
, NULL
);
3514 strlcpy(buf
, rp
, sizeof (buf
));
3521 * For a block device only use the name.
3523 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
3524 if ((strcmp(type
, VDEV_TYPE_DISK
) == 0) &&
3525 !(name_flags
& VDEV_NAME_PATH
)) {
3526 path
= strrchr(path
, '/');
3531 * Remove the partition from the path it this is a whole disk.
3533 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
, &value
)
3534 == 0 && value
&& !(name_flags
& VDEV_NAME_PATH
)) {
3535 return (zfs_strip_partition(path
));
3538 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &path
) == 0);
3541 * If it's a raidz device, we need to stick in the parity level.
3543 if (strcmp(path
, VDEV_TYPE_RAIDZ
) == 0) {
3544 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
,
3546 (void) snprintf(buf
, sizeof (buf
), "%s%llu", path
,
3547 (u_longlong_t
)value
);
3552 * We identify each top-level vdev by using a <type-id>
3553 * naming convention.
3555 if (name_flags
& VDEV_NAME_TYPE_ID
) {
3557 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
3559 (void) snprintf(tmpbuf
, sizeof (tmpbuf
), "%s-%llu",
3560 path
, (u_longlong_t
)id
);
3565 return (zfs_strdup(hdl
, path
));
3569 zbookmark_mem_compare(const void *a
, const void *b
)
3571 return (memcmp(a
, b
, sizeof (zbookmark_phys_t
)));
3575 * Retrieve the persistent error log, uniquify the members, and return to the
3579 zpool_get_errlog(zpool_handle_t
*zhp
, nvlist_t
**nverrlistp
)
3581 zfs_cmd_t zc
= {"\0"};
3582 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3584 zbookmark_phys_t
*zb
= NULL
;
3588 * Retrieve the raw error list from the kernel. If the number of errors
3589 * has increased, allocate more space and continue until we get the
3592 verify(nvlist_lookup_uint64(zhp
->zpool_config
, ZPOOL_CONFIG_ERRCOUNT
,
3596 zc
.zc_nvlist_dst
= (uintptr_t)zfs_alloc(zhp
->zpool_hdl
,
3597 count
* sizeof (zbookmark_phys_t
));
3598 zc
.zc_nvlist_dst_size
= count
;
3599 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3601 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_ERROR_LOG
,
3603 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3604 if (errno
== ENOMEM
) {
3607 count
= zc
.zc_nvlist_dst_size
;
3608 dst
= zfs_alloc(zhp
->zpool_hdl
, count
*
3609 sizeof (zbookmark_phys_t
));
3610 zc
.zc_nvlist_dst
= (uintptr_t)dst
;
3612 return (zpool_standard_error_fmt(hdl
, errno
,
3613 dgettext(TEXT_DOMAIN
, "errors: List of "
3614 "errors unavailable")));
3622 * Sort the resulting bookmarks. This is a little confusing due to the
3623 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3624 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3625 * _not_ copied as part of the process. So we point the start of our
3626 * array appropriate and decrement the total number of elements.
3628 zb
= ((zbookmark_phys_t
*)(uintptr_t)zc
.zc_nvlist_dst
) +
3629 zc
.zc_nvlist_dst_size
;
3630 count
-= zc
.zc_nvlist_dst_size
;
3632 qsort(zb
, count
, sizeof (zbookmark_phys_t
), zbookmark_mem_compare
);
3634 verify(nvlist_alloc(nverrlistp
, 0, KM_SLEEP
) == 0);
3637 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3639 for (i
= 0; i
< count
; i
++) {
3642 /* ignoring zb_blkid and zb_level for now */
3643 if (i
> 0 && zb
[i
-1].zb_objset
== zb
[i
].zb_objset
&&
3644 zb
[i
-1].zb_object
== zb
[i
].zb_object
)
3647 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) != 0)
3649 if (nvlist_add_uint64(nv
, ZPOOL_ERR_DATASET
,
3650 zb
[i
].zb_objset
) != 0) {
3654 if (nvlist_add_uint64(nv
, ZPOOL_ERR_OBJECT
,
3655 zb
[i
].zb_object
) != 0) {
3659 if (nvlist_add_nvlist(*nverrlistp
, "ejk", nv
) != 0) {
3666 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3670 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3671 return (no_memory(zhp
->zpool_hdl
));
3675 * Upgrade a ZFS pool to the latest on-disk version.
3678 zpool_upgrade(zpool_handle_t
*zhp
, uint64_t new_version
)
3680 zfs_cmd_t zc
= {"\0"};
3681 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3683 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3684 zc
.zc_cookie
= new_version
;
3686 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_UPGRADE
, &zc
) != 0)
3687 return (zpool_standard_error_fmt(hdl
, errno
,
3688 dgettext(TEXT_DOMAIN
, "cannot upgrade '%s'"),
3694 zfs_save_arguments(int argc
, char **argv
, char *string
, int len
)
3698 (void) strlcpy(string
, basename(argv
[0]), len
);
3699 for (i
= 1; i
< argc
; i
++) {
3700 (void) strlcat(string
, " ", len
);
3701 (void) strlcat(string
, argv
[i
], len
);
3706 zpool_log_history(libzfs_handle_t
*hdl
, const char *message
)
3708 zfs_cmd_t zc
= {"\0"};
3712 args
= fnvlist_alloc();
3713 fnvlist_add_string(args
, "message", message
);
3714 err
= zcmd_write_src_nvlist(hdl
, &zc
, args
);
3716 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_LOG_HISTORY
, &zc
);
3718 zcmd_free_nvlists(&zc
);
3723 * Perform ioctl to get some command history of a pool.
3725 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3726 * logical offset of the history buffer to start reading from.
3728 * Upon return, 'off' is the next logical offset to read from and
3729 * 'len' is the actual amount of bytes read into 'buf'.
3732 get_history(zpool_handle_t
*zhp
, char *buf
, uint64_t *off
, uint64_t *len
)
3734 zfs_cmd_t zc
= {"\0"};
3735 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3737 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3739 zc
.zc_history
= (uint64_t)(uintptr_t)buf
;
3740 zc
.zc_history_len
= *len
;
3741 zc
.zc_history_offset
= *off
;
3743 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_HISTORY
, &zc
) != 0) {
3746 return (zfs_error_fmt(hdl
, EZFS_PERM
,
3747 dgettext(TEXT_DOMAIN
,
3748 "cannot show history for pool '%s'"),
3751 return (zfs_error_fmt(hdl
, EZFS_NOHISTORY
,
3752 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
3753 "'%s'"), zhp
->zpool_name
));
3755 return (zfs_error_fmt(hdl
, EZFS_BADVERSION
,
3756 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
3757 "'%s', pool must be upgraded"), zhp
->zpool_name
));
3759 return (zpool_standard_error_fmt(hdl
, errno
,
3760 dgettext(TEXT_DOMAIN
,
3761 "cannot get history for '%s'"), zhp
->zpool_name
));
3765 *len
= zc
.zc_history_len
;
3766 *off
= zc
.zc_history_offset
;
3772 * Process the buffer of nvlists, unpacking and storing each nvlist record
3773 * into 'records'. 'leftover' is set to the number of bytes that weren't
3774 * processed as there wasn't a complete record.
3777 zpool_history_unpack(char *buf
, uint64_t bytes_read
, uint64_t *leftover
,
3778 nvlist_t
***records
, uint_t
*numrecords
)
3785 while (bytes_read
> sizeof (reclen
)) {
3787 /* get length of packed record (stored as little endian) */
3788 for (i
= 0, reclen
= 0; i
< sizeof (reclen
); i
++)
3789 reclen
+= (uint64_t)(((uchar_t
*)buf
)[i
]) << (8*i
);
3791 if (bytes_read
< sizeof (reclen
) + reclen
)
3795 if (nvlist_unpack(buf
+ sizeof (reclen
), reclen
, &nv
, 0) != 0)
3797 bytes_read
-= sizeof (reclen
) + reclen
;
3798 buf
+= sizeof (reclen
) + reclen
;
3800 /* add record to nvlist array */
3802 if (ISP2(*numrecords
+ 1)) {
3803 tmp
= realloc(*records
,
3804 *numrecords
* 2 * sizeof (nvlist_t
*));
3812 (*records
)[*numrecords
- 1] = nv
;
3815 *leftover
= bytes_read
;
3820 * Retrieve the command history of a pool.
3823 zpool_get_history(zpool_handle_t
*zhp
, nvlist_t
**nvhisp
)
3826 int buflen
= 128 * 1024;
3828 nvlist_t
**records
= NULL
;
3829 uint_t numrecords
= 0;
3832 buf
= malloc(buflen
);
3836 uint64_t bytes_read
= buflen
;
3839 if ((err
= get_history(zhp
, buf
, &off
, &bytes_read
)) != 0)
3842 /* if nothing else was read in, we're at EOF, just return */
3846 if ((err
= zpool_history_unpack(buf
, bytes_read
,
3847 &leftover
, &records
, &numrecords
)) != 0)
3850 if (leftover
== bytes_read
) {
3852 * no progress made, because buffer is not big enough
3853 * to hold this record; resize and retry.
3857 buf
= malloc(buflen
);
3868 verify(nvlist_alloc(nvhisp
, NV_UNIQUE_NAME
, 0) == 0);
3869 verify(nvlist_add_nvlist_array(*nvhisp
, ZPOOL_HIST_RECORD
,
3870 records
, numrecords
) == 0);
3872 for (i
= 0; i
< numrecords
; i
++)
3873 nvlist_free(records
[i
]);
3880 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
3881 * If there is a new event available 'nvp' will contain a newly allocated
3882 * nvlist and 'dropped' will be set to the number of missed events since
3883 * the last call to this function. When 'nvp' is set to NULL it indicates
3884 * no new events are available. In either case the function returns 0 and
3885 * it is up to the caller to free 'nvp'. In the case of a fatal error the
3886 * function will return a non-zero value. When the function is called in
3887 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
3888 * it will not return until a new event is available.
3891 zpool_events_next(libzfs_handle_t
*hdl
, nvlist_t
**nvp
,
3892 int *dropped
, unsigned flags
, int zevent_fd
)
3894 zfs_cmd_t zc
= {"\0"};
3899 zc
.zc_cleanup_fd
= zevent_fd
;
3901 if (flags
& ZEVENT_NONBLOCK
)
3902 zc
.zc_guid
= ZEVENT_NONBLOCK
;
3904 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, ZEVENT_SIZE
) != 0)
3908 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_NEXT
, &zc
) != 0) {
3911 error
= zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
3912 dgettext(TEXT_DOMAIN
, "zfs shutdown"));
3915 /* Blocking error case should not occur */
3916 if (!(flags
& ZEVENT_NONBLOCK
))
3917 error
= zpool_standard_error_fmt(hdl
, errno
,
3918 dgettext(TEXT_DOMAIN
, "cannot get event"));
3922 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
3923 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
3924 dgettext(TEXT_DOMAIN
, "cannot get event"));
3930 error
= zpool_standard_error_fmt(hdl
, errno
,
3931 dgettext(TEXT_DOMAIN
, "cannot get event"));
3936 error
= zcmd_read_dst_nvlist(hdl
, &zc
, nvp
);
3940 *dropped
= (int)zc
.zc_cookie
;
3942 zcmd_free_nvlists(&zc
);
3951 zpool_events_clear(libzfs_handle_t
*hdl
, int *count
)
3953 zfs_cmd_t zc
= {"\0"};
3956 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
3957 "cannot clear events"));
3959 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_CLEAR
, &zc
) != 0)
3960 return (zpool_standard_error_fmt(hdl
, errno
, msg
));
3963 *count
= (int)zc
.zc_cookie
; /* # of events cleared */
3969 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
3970 * the passed zevent_fd file handle. On success zero is returned,
3971 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
3974 zpool_events_seek(libzfs_handle_t
*hdl
, uint64_t eid
, int zevent_fd
)
3976 zfs_cmd_t zc
= {"\0"};
3980 zc
.zc_cleanup_fd
= zevent_fd
;
3982 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_SEEK
, &zc
) != 0) {
3985 error
= zfs_error_fmt(hdl
, EZFS_NOENT
,
3986 dgettext(TEXT_DOMAIN
, "cannot get event"));
3990 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
3991 dgettext(TEXT_DOMAIN
, "cannot get event"));
3995 error
= zpool_standard_error_fmt(hdl
, errno
,
3996 dgettext(TEXT_DOMAIN
, "cannot get event"));
4005 zpool_obj_to_path(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4006 char *pathname
, size_t len
)
4008 zfs_cmd_t zc
= {"\0"};
4009 boolean_t mounted
= B_FALSE
;
4010 char *mntpnt
= NULL
;
4011 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
4014 /* special case for the MOS */
4015 (void) snprintf(pathname
, len
, "<metadata>:<0x%llx>",
4020 /* get the dataset's name */
4021 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4023 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
,
4024 ZFS_IOC_DSOBJ_TO_DSNAME
, &zc
) != 0) {
4025 /* just write out a path of two object numbers */
4026 (void) snprintf(pathname
, len
, "<0x%llx>:<0x%llx>",
4027 (longlong_t
)dsobj
, (longlong_t
)obj
);
4030 (void) strlcpy(dsname
, zc
.zc_value
, sizeof (dsname
));
4032 /* find out if the dataset is mounted */
4033 mounted
= is_mounted(zhp
->zpool_hdl
, dsname
, &mntpnt
);
4035 /* get the corrupted object's path */
4036 (void) strlcpy(zc
.zc_name
, dsname
, sizeof (zc
.zc_name
));
4038 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_OBJ_TO_PATH
,
4041 (void) snprintf(pathname
, len
, "%s%s", mntpnt
,
4044 (void) snprintf(pathname
, len
, "%s:%s",
4045 dsname
, zc
.zc_value
);
4048 (void) snprintf(pathname
, len
, "%s:<0x%llx>", dsname
,
4055 * Read the EFI label from the config, if a label does not exist then
4056 * pass back the error to the caller. If the caller has passed a non-NULL
4057 * diskaddr argument then we set it to the starting address of the EFI
4061 read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
)
4065 char diskname
[MAXPATHLEN
];
4068 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PATH
, &path
) != 0)
4071 (void) snprintf(diskname
, sizeof (diskname
), "%s%s", DISK_ROOT
,
4072 strrchr(path
, '/'));
4073 if ((fd
= open(diskname
, O_RDONLY
|O_DIRECT
)) >= 0) {
4074 struct dk_gpt
*vtoc
;
4076 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) >= 0) {
4078 *sb
= vtoc
->efi_parts
[0].p_start
;
4087 * determine where a partition starts on a disk in the current
4091 find_start_block(nvlist_t
*config
)
4095 diskaddr_t sb
= MAXOFFSET_T
;
4098 if (nvlist_lookup_nvlist_array(config
,
4099 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) != 0) {
4100 if (nvlist_lookup_uint64(config
,
4101 ZPOOL_CONFIG_WHOLE_DISK
,
4102 &wholedisk
) != 0 || !wholedisk
) {
4103 return (MAXOFFSET_T
);
4105 if (read_efi_label(config
, &sb
) < 0)
4110 for (c
= 0; c
< children
; c
++) {
4111 sb
= find_start_block(child
[c
]);
4112 if (sb
!= MAXOFFSET_T
) {
4116 return (MAXOFFSET_T
);
4120 zpool_label_disk_check(char *path
)
4122 struct dk_gpt
*vtoc
;
4125 if ((fd
= open(path
, O_RDONLY
|O_DIRECT
)) < 0)
4128 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) != 0) {
4133 if (vtoc
->efi_flags
& EFI_GPT_PRIMARY_CORRUPT
) {
4145 * Generate a unique partition name for the ZFS member. Partitions must
4146 * have unique names to ensure udev will be able to create symlinks under
4147 * /dev/disk/by-partlabel/ for all pool members. The partition names are
4148 * of the form <pool>-<unique-id>.
4151 zpool_label_name(char *label_name
, int label_size
)
4156 fd
= open("/dev/urandom", O_RDONLY
);
4158 if (read(fd
, &id
, sizeof (id
)) != sizeof (id
))
4165 id
= (((uint64_t)rand()) << 32) | (uint64_t)rand();
4167 snprintf(label_name
, label_size
, "zfs-%016llx", (u_longlong_t
)id
);
4171 * Label an individual disk. The name provided is the short name,
4172 * stripped of any leading /dev path.
4175 zpool_label_disk(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, char *name
)
4177 char path
[MAXPATHLEN
];
4178 struct dk_gpt
*vtoc
;
4180 size_t resv
= EFI_MIN_RESV_SIZE
;
4181 uint64_t slice_size
;
4182 diskaddr_t start_block
;
4185 /* prepare an error message just in case */
4186 (void) snprintf(errbuf
, sizeof (errbuf
),
4187 dgettext(TEXT_DOMAIN
, "cannot label '%s'"), name
);
4192 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
4193 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
4195 if (zhp
->zpool_start_block
== 0)
4196 start_block
= find_start_block(nvroot
);
4198 start_block
= zhp
->zpool_start_block
;
4199 zhp
->zpool_start_block
= start_block
;
4202 start_block
= NEW_START_BLOCK
;
4205 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4207 if ((fd
= open(path
, O_RDWR
|O_DIRECT
|O_EXCL
)) < 0) {
4209 * This shouldn't happen. We've long since verified that this
4210 * is a valid device.
4212 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
4213 "label '%s': unable to open device: %d"), path
, errno
);
4214 return (zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
));
4217 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, &vtoc
) != 0) {
4219 * The only way this can fail is if we run out of memory, or we
4220 * were unable to read the disk's capacity
4222 if (errno
== ENOMEM
)
4223 (void) no_memory(hdl
);
4226 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
4227 "label '%s': unable to read disk capacity"), path
);
4229 return (zfs_error(hdl
, EZFS_NOCAP
, errbuf
));
4232 slice_size
= vtoc
->efi_last_u_lba
+ 1;
4233 slice_size
-= EFI_MIN_RESV_SIZE
;
4234 if (start_block
== MAXOFFSET_T
)
4235 start_block
= NEW_START_BLOCK
;
4236 slice_size
-= start_block
;
4237 slice_size
= P2ALIGN(slice_size
, PARTITION_END_ALIGNMENT
);
4239 vtoc
->efi_parts
[0].p_start
= start_block
;
4240 vtoc
->efi_parts
[0].p_size
= slice_size
;
4243 * Why we use V_USR: V_BACKUP confuses users, and is considered
4244 * disposable by some EFI utilities (since EFI doesn't have a backup
4245 * slice). V_UNASSIGNED is supposed to be used only for zero size
4246 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4247 * etc. were all pretty specific. V_USR is as close to reality as we
4248 * can get, in the absence of V_OTHER.
4250 vtoc
->efi_parts
[0].p_tag
= V_USR
;
4251 zpool_label_name(vtoc
->efi_parts
[0].p_name
, EFI_PART_NAME_LEN
);
4253 vtoc
->efi_parts
[8].p_start
= slice_size
+ start_block
;
4254 vtoc
->efi_parts
[8].p_size
= resv
;
4255 vtoc
->efi_parts
[8].p_tag
= V_RESERVED
;
4257 rval
= efi_write(fd
, vtoc
);
4259 /* Flush the buffers to disk and invalidate the page cache. */
4261 (void) ioctl(fd
, BLKFLSBUF
);
4264 rval
= efi_rescan(fd
);
4267 * Some block drivers (like pcata) may not support EFI GPT labels.
4268 * Print out a helpful error message directing the user to manually
4269 * label the disk and give a specific slice.
4275 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "try using "
4276 "parted(8) and then provide a specific slice: %d"), rval
);
4277 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4283 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4284 (void) zfs_append_partition(path
, MAXPATHLEN
);
4286 /* Wait to udev to signal use the device has settled. */
4287 rval
= zpool_label_disk_wait(path
, DISK_LABEL_WAIT
);
4289 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "failed to "
4290 "detect device partitions on '%s': %d"), path
, rval
);
4291 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4294 /* We can't be to paranoid. Read the label back and verify it. */
4295 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4296 rval
= zpool_label_disk_check(path
);
4298 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "freshly written "
4299 "EFI label on '%s' is damaged. Ensure\nthis device "
4300 "is not in in use, and is functioning properly: %d"),
4302 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4309 * Allocate and return the underlying device name for a device mapper device.
4310 * If a device mapper device maps to multiple devices, return the first device.
4312 * For example, dm_name = "/dev/dm-0" could return "/dev/sda". Symlinks to a
4313 * DM device (like /dev/disk/by-vdev/A0) are also allowed.
4315 * Returns device name, or NULL on error or no match. If dm_name is not a DM
4316 * device then return NULL.
4318 * NOTE: The returned name string must be *freed*.
4321 dm_get_underlying_path(char *dm_name
)
4331 if (dm_name
== NULL
)
4334 /* dm name may be a symlink (like /dev/disk/by-vdev/A0) */
4335 realp
= realpath(dm_name
, NULL
);
4340 * If they preface 'dev' with a path (like "/dev") then strip it off.
4341 * We just want the 'dm-N' part.
4343 tmp
= strrchr(realp
, '/');
4345 dev_str
= tmp
+ 1; /* +1 since we want the chr after '/' */
4349 size
= asprintf(&tmp
, "/sys/block/%s/slaves/", dev_str
);
4350 if (size
== -1 || !tmp
)
4357 /* Return first sd* entry in /sys/block/dm-N/slaves/ */
4358 while ((ep
= readdir(dp
))) {
4359 if (ep
->d_type
!= DT_DIR
) { /* skip "." and ".." dirs */
4360 size
= asprintf(&path
, "/dev/%s", ep
->d_name
);
4374 * Return 1 if device is a device mapper or multipath device.
4378 zfs_dev_is_dm(char *dev_name
)
4382 tmp
= dm_get_underlying_path(dev_name
);
4391 * By "whole disk" we mean an entire physical disk (something we can
4392 * label, toggle the write cache on, etc.) as opposed to the full
4393 * capacity of a pseudo-device such as lofi or did. We act as if we
4394 * are labeling the disk, which should be a pretty good test of whether
4395 * it's a viable device or not. Returns B_TRUE if it is and B_FALSE if
4399 zfs_dev_is_whole_disk(char *dev_name
)
4401 struct dk_gpt
*label
;
4404 if ((fd
= open(dev_name
, O_RDONLY
| O_DIRECT
)) < 0)
4407 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, &label
) != 0) {
4419 * Lookup the underlying device for a device name
4421 * Often you'll have a symlink to a device, a partition device,
4422 * or a multipath device, and want to look up the underlying device.
4423 * This function returns the underlying device name. If the device
4424 * name is already the underlying device, then just return the same
4425 * name. If the device is a DM device with multiple underlying devices
4426 * then return the first one.
4430 * 1. /dev/disk/by-id/ata-QEMU_HARDDISK_QM00001 -> ../../sda
4431 * dev_name: /dev/disk/by-id/ata-QEMU_HARDDISK_QM00001
4434 * 2. /dev/mapper/mpatha (made up of /dev/sda and /dev/sdb)
4435 * dev_name: /dev/mapper/mpatha
4436 * returns: /dev/sda (first device)
4438 * 3. /dev/sda (already the underlying device)
4439 * dev_name: /dev/sda
4442 * 4. /dev/dm-3 (mapped to /dev/sda)
4443 * dev_name: /dev/dm-3
4446 * 5. /dev/disk/by-id/scsi-0QEMU_drive-scsi0-0-0-0-part9 -> ../../sdb9
4447 * dev_name: /dev/disk/by-id/scsi-0QEMU_drive-scsi0-0-0-0-part9
4450 * 6. /dev/disk/by-uuid/5df030cf-3cd9-46e4-8e99-3ccb462a4e9a -> ../dev/sda2
4451 * dev_name: /dev/disk/by-uuid/5df030cf-3cd9-46e4-8e99-3ccb462a4e9a
4454 * Returns underlying device name, or NULL on error or no match.
4456 * NOTE: The returned name string must be *freed*.
4459 zfs_get_underlying_path(char *dev_name
)
4464 if (dev_name
== NULL
)
4467 tmp
= dm_get_underlying_path(dev_name
);
4469 /* dev_name not a DM device, so just un-symlinkize it */
4471 tmp
= realpath(dev_name
, NULL
);
4474 name
= zfs_strip_partition_path(tmp
);
4482 * Given a dev name like "sda", return the full enclosure sysfs path to
4483 * the disk. You can also pass in the name with "/dev" prepended
4484 * to it (like /dev/sda).
4486 * For example, disk "sda" in enclosure slot 1:
4488 * returns: "/sys/class/enclosure/1:0:3:0/Slot 1"
4490 * 'dev' must be a non-devicemapper device.
4492 * Returned string must be freed.
4495 zfs_get_enclosure_sysfs_path(char *dev_name
)
4499 char buf
[MAXPATHLEN
];
4507 if (dev_name
== NULL
)
4510 /* If they preface 'dev' with a path (like "/dev") then strip it off */
4511 tmp1
= strrchr(dev_name
, '/');
4513 dev_name
= tmp1
+ 1; /* +1 since we want the chr after '/' */
4515 tmpsize
= asprintf(&tmp1
, "/sys/block/%s/device", dev_name
);
4516 if (tmpsize
== -1 || tmp1
== NULL
) {
4523 tmp1
= NULL
; /* To make free() at the end a NOP */
4528 * Look though all sysfs entries in /sys/block/<dev>/device for
4529 * the enclosure symlink.
4531 while ((ep
= readdir(dp
))) {
4532 /* Ignore everything that's not our enclosure_device link */
4533 if (strstr(ep
->d_name
, "enclosure_device") == NULL
)
4536 if (asprintf(&tmp2
, "%s/%s", tmp1
, ep
->d_name
) == -1 ||
4540 size
= readlink(tmp2
, buf
, sizeof (buf
));
4542 /* Did readlink fail or crop the link name? */
4543 if (size
== -1 || size
>= sizeof (buf
)) {
4545 tmp2
= NULL
; /* To make free() at the end a NOP */
4550 * We got a valid link. readlink() doesn't terminate strings
4551 * so we have to do it.
4556 * Our link will look like:
4558 * "../../../../port-11:1:2/..STUFF../enclosure/1:0:3:0/SLOT 1"
4560 * We want to grab the "enclosure/1:0:3:0/SLOT 1" part
4562 tmp3
= strstr(buf
, "enclosure");
4566 if (asprintf(&path
, "/sys/class/%s", tmp3
) == -1) {
4567 /* If asprintf() fails, 'path' is undefined */