4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27 * Copyright (c) 2017 Datto Inc.
42 #include <sys/efi_partition.h>
44 #include <sys/zfs_ioctl.h>
47 #include "zfs_namecheck.h"
49 #include "libzfs_impl.h"
50 #include "zfs_comutil.h"
51 #include "zfeature_common.h"
53 static int read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
);
55 typedef struct prop_flags
{
56 int create
:1; /* Validate property on creation */
57 int import
:1; /* Validate property on import */
61 * ====================================================================
62 * zpool property functions
63 * ====================================================================
67 zpool_get_all_props(zpool_handle_t
*zhp
)
69 zfs_cmd_t zc
= {"\0"};
70 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
72 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
74 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
77 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_PROPS
, &zc
) != 0) {
78 if (errno
== ENOMEM
) {
79 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
80 zcmd_free_nvlists(&zc
);
84 zcmd_free_nvlists(&zc
);
89 if (zcmd_read_dst_nvlist(hdl
, &zc
, &zhp
->zpool_props
) != 0) {
90 zcmd_free_nvlists(&zc
);
94 zcmd_free_nvlists(&zc
);
100 zpool_props_refresh(zpool_handle_t
*zhp
)
104 old_props
= zhp
->zpool_props
;
106 if (zpool_get_all_props(zhp
) != 0)
109 nvlist_free(old_props
);
114 zpool_get_prop_string(zpool_handle_t
*zhp
, zpool_prop_t prop
,
120 zprop_source_t source
;
122 nvl
= zhp
->zpool_props
;
123 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
124 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &ival
) == 0);
126 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
128 source
= ZPROP_SRC_DEFAULT
;
129 if ((value
= (char *)zpool_prop_default_string(prop
)) == NULL
)
140 zpool_get_prop_int(zpool_handle_t
*zhp
, zpool_prop_t prop
, zprop_source_t
*src
)
144 zprop_source_t source
;
146 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
)) {
148 * zpool_get_all_props() has most likely failed because
149 * the pool is faulted, but if all we need is the top level
150 * vdev's guid then get it from the zhp config nvlist.
152 if ((prop
== ZPOOL_PROP_GUID
) &&
153 (nvlist_lookup_nvlist(zhp
->zpool_config
,
154 ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0) &&
155 (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
)
159 return (zpool_prop_default_numeric(prop
));
162 nvl
= zhp
->zpool_props
;
163 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
164 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &value
) == 0);
166 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
168 source
= ZPROP_SRC_DEFAULT
;
169 value
= zpool_prop_default_numeric(prop
);
179 * Map VDEV STATE to printed strings.
182 zpool_state_to_name(vdev_state_t state
, vdev_aux_t aux
)
185 case VDEV_STATE_CLOSED
:
186 case VDEV_STATE_OFFLINE
:
187 return (gettext("OFFLINE"));
188 case VDEV_STATE_REMOVED
:
189 return (gettext("REMOVED"));
190 case VDEV_STATE_CANT_OPEN
:
191 if (aux
== VDEV_AUX_CORRUPT_DATA
|| aux
== VDEV_AUX_BAD_LOG
)
192 return (gettext("FAULTED"));
193 else if (aux
== VDEV_AUX_SPLIT_POOL
)
194 return (gettext("SPLIT"));
196 return (gettext("UNAVAIL"));
197 case VDEV_STATE_FAULTED
:
198 return (gettext("FAULTED"));
199 case VDEV_STATE_DEGRADED
:
200 return (gettext("DEGRADED"));
201 case VDEV_STATE_HEALTHY
:
202 return (gettext("ONLINE"));
208 return (gettext("UNKNOWN"));
212 * Map POOL STATE to printed strings.
215 zpool_pool_state_to_name(pool_state_t state
)
220 case POOL_STATE_ACTIVE
:
221 return (gettext("ACTIVE"));
222 case POOL_STATE_EXPORTED
:
223 return (gettext("EXPORTED"));
224 case POOL_STATE_DESTROYED
:
225 return (gettext("DESTROYED"));
226 case POOL_STATE_SPARE
:
227 return (gettext("SPARE"));
228 case POOL_STATE_L2CACHE
:
229 return (gettext("L2CACHE"));
230 case POOL_STATE_UNINITIALIZED
:
231 return (gettext("UNINITIALIZED"));
232 case POOL_STATE_UNAVAIL
:
233 return (gettext("UNAVAIL"));
234 case POOL_STATE_POTENTIALLY_ACTIVE
:
235 return (gettext("POTENTIALLY_ACTIVE"));
238 return (gettext("UNKNOWN"));
242 * Get a zpool property value for 'prop' and return the value in
243 * a pre-allocated buffer.
246 zpool_get_prop(zpool_handle_t
*zhp
, zpool_prop_t prop
, char *buf
,
247 size_t len
, zprop_source_t
*srctype
, boolean_t literal
)
251 zprop_source_t src
= ZPROP_SRC_NONE
;
256 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
258 case ZPOOL_PROP_NAME
:
259 (void) strlcpy(buf
, zpool_get_name(zhp
), len
);
262 case ZPOOL_PROP_HEALTH
:
263 (void) strlcpy(buf
, "FAULTED", len
);
266 case ZPOOL_PROP_GUID
:
267 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
268 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
271 case ZPOOL_PROP_ALTROOT
:
272 case ZPOOL_PROP_CACHEFILE
:
273 case ZPOOL_PROP_COMMENT
:
274 if (zhp
->zpool_props
!= NULL
||
275 zpool_get_all_props(zhp
) == 0) {
277 zpool_get_prop_string(zhp
, prop
, &src
),
283 (void) strlcpy(buf
, "-", len
);
292 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
) &&
293 prop
!= ZPOOL_PROP_NAME
)
296 switch (zpool_prop_get_type(prop
)) {
297 case PROP_TYPE_STRING
:
298 (void) strlcpy(buf
, zpool_get_prop_string(zhp
, prop
, &src
),
302 case PROP_TYPE_NUMBER
:
303 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
306 case ZPOOL_PROP_SIZE
:
307 case ZPOOL_PROP_ALLOCATED
:
308 case ZPOOL_PROP_FREE
:
309 case ZPOOL_PROP_FREEING
:
310 case ZPOOL_PROP_LEAKED
:
311 case ZPOOL_PROP_ASHIFT
:
313 (void) snprintf(buf
, len
, "%llu",
314 (u_longlong_t
)intval
);
316 (void) zfs_nicenum(intval
, buf
, len
);
319 case ZPOOL_PROP_EXPANDSZ
:
321 (void) strlcpy(buf
, "-", len
);
322 } else if (literal
) {
323 (void) snprintf(buf
, len
, "%llu",
324 (u_longlong_t
)intval
);
326 (void) zfs_nicebytes(intval
, buf
, len
);
330 case ZPOOL_PROP_CAPACITY
:
332 (void) snprintf(buf
, len
, "%llu",
333 (u_longlong_t
)intval
);
335 (void) snprintf(buf
, len
, "%llu%%",
336 (u_longlong_t
)intval
);
340 case ZPOOL_PROP_FRAGMENTATION
:
341 if (intval
== UINT64_MAX
) {
342 (void) strlcpy(buf
, "-", len
);
343 } else if (literal
) {
344 (void) snprintf(buf
, len
, "%llu",
345 (u_longlong_t
)intval
);
347 (void) snprintf(buf
, len
, "%llu%%",
348 (u_longlong_t
)intval
);
352 case ZPOOL_PROP_DEDUPRATIO
:
354 (void) snprintf(buf
, len
, "%llu.%02llu",
355 (u_longlong_t
)(intval
/ 100),
356 (u_longlong_t
)(intval
% 100));
358 (void) snprintf(buf
, len
, "%llu.%02llux",
359 (u_longlong_t
)(intval
/ 100),
360 (u_longlong_t
)(intval
% 100));
363 case ZPOOL_PROP_HEALTH
:
364 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
365 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
366 verify(nvlist_lookup_uint64_array(nvroot
,
367 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
370 (void) strlcpy(buf
, zpool_state_to_name(intval
,
373 case ZPOOL_PROP_VERSION
:
374 if (intval
>= SPA_VERSION_FEATURES
) {
375 (void) snprintf(buf
, len
, "-");
380 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
384 case PROP_TYPE_INDEX
:
385 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
386 if (zpool_prop_index_to_string(prop
, intval
, &strval
)
389 (void) strlcpy(buf
, strval
, len
);
403 * Check if the bootfs name has the same pool name as it is set to.
404 * Assuming bootfs is a valid dataset name.
407 bootfs_name_valid(const char *pool
, char *bootfs
)
409 int len
= strlen(pool
);
411 if (!zfs_name_valid(bootfs
, ZFS_TYPE_FILESYSTEM
|ZFS_TYPE_SNAPSHOT
))
414 if (strncmp(pool
, bootfs
, len
) == 0 &&
415 (bootfs
[len
] == '/' || bootfs
[len
] == '\0'))
422 zpool_is_bootable(zpool_handle_t
*zhp
)
424 char bootfs
[ZFS_MAX_DATASET_NAME_LEN
];
426 return (zpool_get_prop(zhp
, ZPOOL_PROP_BOOTFS
, bootfs
,
427 sizeof (bootfs
), NULL
, B_FALSE
) == 0 && strncmp(bootfs
, "-",
428 sizeof (bootfs
)) != 0);
433 * Given an nvlist of zpool properties to be set, validate that they are
434 * correct, and parse any numeric properties (index, boolean, etc) if they are
435 * specified as strings.
438 zpool_valid_proplist(libzfs_handle_t
*hdl
, const char *poolname
,
439 nvlist_t
*props
, uint64_t version
, prop_flags_t flags
, char *errbuf
)
447 struct stat64 statbuf
;
450 if (nvlist_alloc(&retprops
, NV_UNIQUE_NAME
, 0) != 0) {
451 (void) no_memory(hdl
);
456 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
457 const char *propname
= nvpair_name(elem
);
459 prop
= zpool_name_to_prop(propname
);
460 if (prop
== ZPROP_INVAL
&& zpool_prop_feature(propname
)) {
462 char *fname
= strchr(propname
, '@') + 1;
464 err
= zfeature_lookup_name(fname
, NULL
);
466 ASSERT3U(err
, ==, ENOENT
);
467 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
468 "invalid feature '%s'"), fname
);
469 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
473 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
474 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
475 "'%s' must be a string"), propname
);
476 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
480 (void) nvpair_value_string(elem
, &strval
);
481 if (strcmp(strval
, ZFS_FEATURE_ENABLED
) != 0 &&
482 strcmp(strval
, ZFS_FEATURE_DISABLED
) != 0) {
483 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
484 "property '%s' can only be set to "
485 "'enabled' or 'disabled'"), propname
);
486 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
490 if (nvlist_add_uint64(retprops
, propname
, 0) != 0) {
491 (void) no_memory(hdl
);
498 * Make sure this property is valid and applies to this type.
500 if (prop
== ZPROP_INVAL
) {
501 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
502 "invalid property '%s'"), propname
);
503 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
507 if (zpool_prop_readonly(prop
)) {
508 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
509 "is readonly"), propname
);
510 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
514 if (zprop_parse_value(hdl
, elem
, prop
, ZFS_TYPE_POOL
, retprops
,
515 &strval
, &intval
, errbuf
) != 0)
519 * Perform additional checking for specific properties.
522 case ZPOOL_PROP_VERSION
:
523 if (intval
< version
||
524 !SPA_VERSION_IS_SUPPORTED(intval
)) {
525 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
526 "property '%s' number %d is invalid."),
528 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
533 case ZPOOL_PROP_ASHIFT
:
535 (intval
< ASHIFT_MIN
|| intval
> ASHIFT_MAX
)) {
536 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
537 "invalid '%s=%d' property: only values "
538 "between %" PRId32
" and %" PRId32
" "
540 propname
, intval
, ASHIFT_MIN
, ASHIFT_MAX
);
541 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
546 case ZPOOL_PROP_BOOTFS
:
547 if (flags
.create
|| flags
.import
) {
548 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
549 "property '%s' cannot be set at creation "
550 "or import time"), propname
);
551 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
555 if (version
< SPA_VERSION_BOOTFS
) {
556 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
557 "pool must be upgraded to support "
558 "'%s' property"), propname
);
559 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
564 * bootfs property value has to be a dataset name and
565 * the dataset has to be in the same pool as it sets to.
567 if (strval
[0] != '\0' && !bootfs_name_valid(poolname
,
569 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
570 "is an invalid name"), strval
);
571 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
575 if ((zhp
= zpool_open_canfail(hdl
, poolname
)) == NULL
) {
576 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
577 "could not open pool '%s'"), poolname
);
578 (void) zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
);
584 case ZPOOL_PROP_ALTROOT
:
585 if (!flags
.create
&& !flags
.import
) {
586 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
587 "property '%s' can only be set during pool "
588 "creation or import"), propname
);
589 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
593 if (strval
[0] != '/') {
594 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
595 "bad alternate root '%s'"), strval
);
596 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
601 case ZPOOL_PROP_CACHEFILE
:
602 if (strval
[0] == '\0')
605 if (strcmp(strval
, "none") == 0)
608 if (strval
[0] != '/') {
609 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
610 "property '%s' must be empty, an "
611 "absolute path, or 'none'"), propname
);
612 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
616 slash
= strrchr(strval
, '/');
618 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
619 strcmp(slash
, "/..") == 0) {
620 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
621 "'%s' is not a valid file"), strval
);
622 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
628 if (strval
[0] != '\0' &&
629 (stat64(strval
, &statbuf
) != 0 ||
630 !S_ISDIR(statbuf
.st_mode
))) {
631 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
632 "'%s' is not a valid directory"),
634 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
641 case ZPOOL_PROP_COMMENT
:
642 for (check
= strval
; *check
!= '\0'; check
++) {
643 if (!isprint(*check
)) {
645 dgettext(TEXT_DOMAIN
,
646 "comment may only have printable "
648 (void) zfs_error(hdl
, EZFS_BADPROP
,
653 if (strlen(strval
) > ZPROP_MAX_COMMENT
) {
654 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
655 "comment must not exceed %d characters"),
657 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
661 case ZPOOL_PROP_READONLY
:
663 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
664 "property '%s' can only be set at "
665 "import time"), propname
);
666 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
670 case ZPOOL_PROP_TNAME
:
672 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
673 "property '%s' can only be set at "
674 "creation time"), propname
);
675 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
679 case ZPOOL_PROP_MULTIHOST
:
680 if (get_system_hostid() == 0) {
681 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
682 "requires a non-zero system hostid"));
683 (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
);
1792 * Dry-run failed, but we print out what success
1793 * looks like if we found a best txg
1795 if (policy
.zrp_request
& ZPOOL_TRY_REWIND
) {
1796 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1802 if (newname
== NULL
)
1803 (void) snprintf(desc
, sizeof (desc
),
1804 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1807 (void) snprintf(desc
, sizeof (desc
),
1808 dgettext(TEXT_DOMAIN
, "cannot import '%s' as '%s'"),
1813 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
1814 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1815 nvlist_exists(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
)) {
1816 (void) printf(dgettext(TEXT_DOMAIN
, "This "
1817 "pool uses the following feature(s) not "
1818 "supported by this system:\n"));
1819 zpool_print_unsup_feat(nv
);
1820 if (nvlist_exists(nvinfo
,
1821 ZPOOL_CONFIG_CAN_RDONLY
)) {
1822 (void) printf(dgettext(TEXT_DOMAIN
,
1823 "All unsupported features are only "
1824 "required for writing to the pool."
1825 "\nThe pool can be imported using "
1826 "'-o readonly=on'.\n"));
1830 * Unsupported version.
1832 (void) zfs_error(hdl
, EZFS_BADVERSION
, desc
);
1836 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
1837 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0) {
1838 char *hostname
= "<unknown>";
1839 uint64_t hostid
= 0;
1840 mmp_state_t mmp_state
;
1842 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
1843 ZPOOL_CONFIG_MMP_STATE
);
1845 if (nvlist_exists(nvinfo
,
1846 ZPOOL_CONFIG_MMP_HOSTNAME
))
1847 hostname
= fnvlist_lookup_string(nvinfo
,
1848 ZPOOL_CONFIG_MMP_HOSTNAME
);
1850 if (nvlist_exists(nvinfo
,
1851 ZPOOL_CONFIG_MMP_HOSTID
))
1852 hostid
= fnvlist_lookup_uint64(nvinfo
,
1853 ZPOOL_CONFIG_MMP_HOSTID
);
1855 if (mmp_state
== MMP_STATE_ACTIVE
) {
1856 (void) snprintf(aux
, sizeof (aux
),
1857 dgettext(TEXT_DOMAIN
, "pool is imp"
1858 "orted on host '%s' (hostid=%lx).\n"
1859 "Export the pool on the other "
1860 "system, then run 'zpool import'."),
1861 hostname
, (unsigned long) hostid
);
1862 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
1863 (void) snprintf(aux
, sizeof (aux
),
1864 dgettext(TEXT_DOMAIN
, "pool has "
1865 "the multihost property on and "
1866 "the\nsystem's hostid is not set. "
1867 "Set a unique system hostid with "
1868 "the zgenhostid(8) command.\n"));
1871 (void) zfs_error_aux(hdl
, aux
);
1873 (void) zfs_error(hdl
, EZFS_ACTIVE_POOL
, desc
);
1877 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, desc
);
1881 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1882 "one or more devices is read only"));
1883 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
1887 if (nv
&& nvlist_lookup_nvlist(nv
,
1888 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1889 nvlist_lookup_nvlist(nvinfo
,
1890 ZPOOL_CONFIG_MISSING_DEVICES
, &missing
) == 0) {
1891 (void) printf(dgettext(TEXT_DOMAIN
,
1892 "The devices below are missing, use "
1893 "'-m' to import the pool anyway:\n"));
1894 print_vdev_tree(hdl
, NULL
, missing
, 2);
1895 (void) printf("\n");
1897 (void) zpool_standard_error(hdl
, error
, desc
);
1901 (void) zpool_standard_error(hdl
, error
, desc
);
1905 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1906 "one or more devices are already in use\n"));
1907 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
1910 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1911 "new name of at least one dataset is longer than "
1912 "the maximum allowable length"));
1913 (void) zfs_error(hdl
, EZFS_NAMETOOLONG
, desc
);
1916 (void) zpool_standard_error(hdl
, error
, desc
);
1917 zpool_explain_recover(hdl
,
1918 newname
? origname
: thename
, -error
, nv
);
1925 zpool_handle_t
*zhp
;
1928 * This should never fail, but play it safe anyway.
1930 if (zpool_open_silent(hdl
, thename
, &zhp
) != 0)
1932 else if (zhp
!= NULL
)
1934 if (policy
.zrp_request
&
1935 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
1936 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1937 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0), nv
);
1950 zpool_scan(zpool_handle_t
*zhp
, pool_scan_func_t func
, pool_scrub_cmd_t cmd
)
1952 zfs_cmd_t zc
= {"\0"};
1955 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1957 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1958 zc
.zc_cookie
= func
;
1961 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_SCAN
, &zc
) == 0)
1966 /* ECANCELED on a scrub means we resumed a paused scrub */
1967 if (err
== ECANCELED
&& func
== POOL_SCAN_SCRUB
&&
1968 cmd
== POOL_SCRUB_NORMAL
)
1971 if (err
== ENOENT
&& func
!= POOL_SCAN_NONE
&& cmd
== POOL_SCRUB_NORMAL
)
1974 if (func
== POOL_SCAN_SCRUB
) {
1975 if (cmd
== POOL_SCRUB_PAUSE
) {
1976 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1977 "cannot pause scrubbing %s"), zc
.zc_name
);
1979 assert(cmd
== POOL_SCRUB_NORMAL
);
1980 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1981 "cannot scrub %s"), zc
.zc_name
);
1983 } else if (func
== POOL_SCAN_NONE
) {
1984 (void) snprintf(msg
, sizeof (msg
),
1985 dgettext(TEXT_DOMAIN
, "cannot cancel scrubbing %s"),
1988 assert(!"unexpected result");
1993 pool_scan_stat_t
*ps
= NULL
;
1996 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
1997 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
1998 (void) nvlist_lookup_uint64_array(nvroot
,
1999 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &psc
);
2000 if (ps
&& ps
->pss_func
== POOL_SCAN_SCRUB
) {
2001 if (cmd
== POOL_SCRUB_PAUSE
)
2002 return (zfs_error(hdl
, EZFS_SCRUB_PAUSED
, msg
));
2004 return (zfs_error(hdl
, EZFS_SCRUBBING
, msg
));
2006 return (zfs_error(hdl
, EZFS_RESILVERING
, msg
));
2008 } else if (err
== ENOENT
) {
2009 return (zfs_error(hdl
, EZFS_NO_SCRUB
, msg
));
2011 return (zpool_standard_error(hdl
, err
, msg
));
2016 * Find a vdev that matches the search criteria specified. We use the
2017 * the nvpair name to determine how we should look for the device.
2018 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2019 * spare; but FALSE if its an INUSE spare.
2022 vdev_to_nvlist_iter(nvlist_t
*nv
, nvlist_t
*search
, boolean_t
*avail_spare
,
2023 boolean_t
*l2cache
, boolean_t
*log
)
2030 nvpair_t
*pair
= nvlist_next_nvpair(search
, NULL
);
2032 /* Nothing to look for */
2033 if (search
== NULL
|| pair
== NULL
)
2036 /* Obtain the key we will use to search */
2037 srchkey
= nvpair_name(pair
);
2039 switch (nvpair_type(pair
)) {
2040 case DATA_TYPE_UINT64
:
2041 if (strcmp(srchkey
, ZPOOL_CONFIG_GUID
) == 0) {
2042 uint64_t srchval
, theguid
;
2044 verify(nvpair_value_uint64(pair
, &srchval
) == 0);
2045 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
2047 if (theguid
== srchval
)
2052 case DATA_TYPE_STRING
: {
2053 char *srchval
, *val
;
2055 verify(nvpair_value_string(pair
, &srchval
) == 0);
2056 if (nvlist_lookup_string(nv
, srchkey
, &val
) != 0)
2060 * Search for the requested value. Special cases:
2062 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2063 * "-part1", or "p1". The suffix is hidden from the user,
2064 * but included in the string, so this matches around it.
2065 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2066 * is used to check all possible expanded paths.
2067 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2069 * Otherwise, all other searches are simple string compares.
2071 if (strcmp(srchkey
, ZPOOL_CONFIG_PATH
) == 0) {
2072 uint64_t wholedisk
= 0;
2074 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
2076 if (zfs_strcmp_pathname(srchval
, val
, wholedisk
) == 0)
2079 } else if (strcmp(srchkey
, ZPOOL_CONFIG_TYPE
) == 0 && val
) {
2080 char *type
, *idx
, *end
, *p
;
2081 uint64_t id
, vdev_id
;
2084 * Determine our vdev type, keeping in mind
2085 * that the srchval is composed of a type and
2086 * vdev id pair (i.e. mirror-4).
2088 if ((type
= strdup(srchval
)) == NULL
)
2091 if ((p
= strrchr(type
, '-')) == NULL
) {
2099 * If the types don't match then keep looking.
2101 if (strncmp(val
, type
, strlen(val
)) != 0) {
2106 verify(strncmp(type
, VDEV_TYPE_RAIDZ
,
2107 strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2108 strncmp(type
, VDEV_TYPE_MIRROR
,
2109 strlen(VDEV_TYPE_MIRROR
)) == 0);
2110 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
2114 vdev_id
= strtoull(idx
, &end
, 10);
2121 * Now verify that we have the correct vdev id.
2130 if (strcmp(srchval
, val
) == 0)
2139 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2140 &child
, &children
) != 0)
2143 for (c
= 0; c
< children
; c
++) {
2144 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2145 avail_spare
, l2cache
, NULL
)) != NULL
) {
2147 * The 'is_log' value is only set for the toplevel
2148 * vdev, not the leaf vdevs. So we always lookup the
2149 * log device from the root of the vdev tree (where
2150 * 'log' is non-NULL).
2153 nvlist_lookup_uint64(child
[c
],
2154 ZPOOL_CONFIG_IS_LOG
, &is_log
) == 0 &&
2162 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2163 &child
, &children
) == 0) {
2164 for (c
= 0; c
< children
; c
++) {
2165 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2166 avail_spare
, l2cache
, NULL
)) != NULL
) {
2167 *avail_spare
= B_TRUE
;
2173 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2174 &child
, &children
) == 0) {
2175 for (c
= 0; c
< children
; c
++) {
2176 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2177 avail_spare
, l2cache
, NULL
)) != NULL
) {
2188 * Given a physical path (minus the "/devices" prefix), find the
2192 zpool_find_vdev_by_physpath(zpool_handle_t
*zhp
, const char *ppath
,
2193 boolean_t
*avail_spare
, boolean_t
*l2cache
, boolean_t
*log
)
2195 nvlist_t
*search
, *nvroot
, *ret
;
2197 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2198 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PHYS_PATH
, ppath
) == 0);
2200 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2203 *avail_spare
= B_FALSE
;
2207 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2208 nvlist_free(search
);
2214 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2217 zpool_vdev_is_interior(const char *name
)
2219 if (strncmp(name
, VDEV_TYPE_RAIDZ
, strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2220 strncmp(name
, VDEV_TYPE_MIRROR
, strlen(VDEV_TYPE_MIRROR
)) == 0)
2226 zpool_find_vdev(zpool_handle_t
*zhp
, const char *path
, boolean_t
*avail_spare
,
2227 boolean_t
*l2cache
, boolean_t
*log
)
2230 nvlist_t
*nvroot
, *search
, *ret
;
2233 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2235 guid
= strtoull(path
, &end
, 0);
2236 if (guid
!= 0 && *end
== '\0') {
2237 verify(nvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
) == 0);
2238 } else if (zpool_vdev_is_interior(path
)) {
2239 verify(nvlist_add_string(search
, ZPOOL_CONFIG_TYPE
, path
) == 0);
2241 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PATH
, path
) == 0);
2244 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2247 *avail_spare
= B_FALSE
;
2251 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2252 nvlist_free(search
);
2258 vdev_is_online(nvlist_t
*nv
)
2262 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
, &ival
) == 0 ||
2263 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
, &ival
) == 0 ||
2264 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
, &ival
) == 0)
2271 * Helper function for zpool_get_physpaths().
2274 vdev_get_one_physpath(nvlist_t
*config
, char *physpath
, size_t physpath_size
,
2275 size_t *bytes_written
)
2277 size_t bytes_left
, pos
, rsz
;
2281 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PHYS_PATH
,
2283 return (EZFS_NODEVICE
);
2285 pos
= *bytes_written
;
2286 bytes_left
= physpath_size
- pos
;
2287 format
= (pos
== 0) ? "%s" : " %s";
2289 rsz
= snprintf(physpath
+ pos
, bytes_left
, format
, tmppath
);
2290 *bytes_written
+= rsz
;
2292 if (rsz
>= bytes_left
) {
2293 /* if physpath was not copied properly, clear it */
2294 if (bytes_left
!= 0) {
2297 return (EZFS_NOSPC
);
2303 vdev_get_physpaths(nvlist_t
*nv
, char *physpath
, size_t phypath_size
,
2304 size_t *rsz
, boolean_t is_spare
)
2309 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
2310 return (EZFS_INVALCONFIG
);
2312 if (strcmp(type
, VDEV_TYPE_DISK
) == 0) {
2314 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2315 * For a spare vdev, we only want to boot from the active
2320 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
2323 return (EZFS_INVALCONFIG
);
2326 if (vdev_is_online(nv
)) {
2327 if ((ret
= vdev_get_one_physpath(nv
, physpath
,
2328 phypath_size
, rsz
)) != 0)
2331 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) == 0 ||
2332 strcmp(type
, VDEV_TYPE_RAIDZ
) == 0 ||
2333 strcmp(type
, VDEV_TYPE_REPLACING
) == 0 ||
2334 (is_spare
= (strcmp(type
, VDEV_TYPE_SPARE
) == 0))) {
2339 if (nvlist_lookup_nvlist_array(nv
,
2340 ZPOOL_CONFIG_CHILDREN
, &child
, &count
) != 0)
2341 return (EZFS_INVALCONFIG
);
2343 for (i
= 0; i
< count
; i
++) {
2344 ret
= vdev_get_physpaths(child
[i
], physpath
,
2345 phypath_size
, rsz
, is_spare
);
2346 if (ret
== EZFS_NOSPC
)
2351 return (EZFS_POOL_INVALARG
);
2355 * Get phys_path for a root pool config.
2356 * Return 0 on success; non-zero on failure.
2359 zpool_get_config_physpath(nvlist_t
*config
, char *physpath
, size_t phypath_size
)
2362 nvlist_t
*vdev_root
;
2369 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2371 return (EZFS_INVALCONFIG
);
2373 if (nvlist_lookup_string(vdev_root
, ZPOOL_CONFIG_TYPE
, &type
) != 0 ||
2374 nvlist_lookup_nvlist_array(vdev_root
, ZPOOL_CONFIG_CHILDREN
,
2375 &child
, &count
) != 0)
2376 return (EZFS_INVALCONFIG
);
2379 * root pool can only have a single top-level vdev.
2381 if (strcmp(type
, VDEV_TYPE_ROOT
) != 0 || count
!= 1)
2382 return (EZFS_POOL_INVALARG
);
2384 (void) vdev_get_physpaths(child
[0], physpath
, phypath_size
, &rsz
,
2387 /* No online devices */
2389 return (EZFS_NODEVICE
);
2395 * Get phys_path for a root pool
2396 * Return 0 on success; non-zero on failure.
2399 zpool_get_physpath(zpool_handle_t
*zhp
, char *physpath
, size_t phypath_size
)
2401 return (zpool_get_config_physpath(zhp
->zpool_config
, physpath
,
2406 * If the device has being dynamically expanded then we need to relabel
2407 * the disk to use the new unallocated space.
2410 zpool_relabel_disk(libzfs_handle_t
*hdl
, const char *path
, const char *msg
)
2414 if ((fd
= open(path
, O_RDWR
|O_DIRECT
)) < 0) {
2415 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2416 "relabel '%s': unable to open device: %d"), path
, errno
);
2417 return (zfs_error(hdl
, EZFS_OPENFAILED
, msg
));
2421 * It's possible that we might encounter an error if the device
2422 * does not have any unallocated space left. If so, we simply
2423 * ignore that error and continue on.
2425 * Also, we don't call efi_rescan() - that would just return EBUSY.
2426 * The module will do it for us in vdev_disk_open().
2428 error
= efi_use_whole_disk(fd
);
2430 /* Flush the buffers to disk and invalidate the page cache. */
2432 (void) ioctl(fd
, BLKFLSBUF
);
2435 if (error
&& error
!= VT_ENOSPC
) {
2436 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2437 "relabel '%s': unable to read disk capacity"), path
);
2438 return (zfs_error(hdl
, EZFS_NOCAP
, msg
));
2445 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
2447 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
2448 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
2452 zpool_vdev_path_to_guid_impl(zpool_handle_t
*zhp
, const char *path
,
2453 boolean_t
*is_spare
, boolean_t
*is_l2cache
, boolean_t
*is_log
)
2456 boolean_t spare
= B_FALSE
, l2cache
= B_FALSE
, log
= B_FALSE
;
2459 if ((tgt
= zpool_find_vdev(zhp
, path
, &spare
, &l2cache
,
2463 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &guid
) == 0);
2464 if (is_spare
!= NULL
)
2466 if (is_l2cache
!= NULL
)
2467 *is_l2cache
= l2cache
;
2474 /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
2476 zpool_vdev_path_to_guid(zpool_handle_t
*zhp
, const char *path
)
2478 return (zpool_vdev_path_to_guid_impl(zhp
, path
, NULL
, NULL
, NULL
));
2482 * Bring the specified vdev online. The 'flags' parameter is a set of the
2483 * ZFS_ONLINE_* flags.
2486 zpool_vdev_online(zpool_handle_t
*zhp
, const char *path
, int flags
,
2487 vdev_state_t
*newstate
)
2489 zfs_cmd_t zc
= {"\0"};
2492 boolean_t avail_spare
, l2cache
, islog
;
2493 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2496 if (flags
& ZFS_ONLINE_EXPAND
) {
2497 (void) snprintf(msg
, sizeof (msg
),
2498 dgettext(TEXT_DOMAIN
, "cannot expand %s"), path
);
2500 (void) snprintf(msg
, sizeof (msg
),
2501 dgettext(TEXT_DOMAIN
, "cannot online %s"), path
);
2504 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2505 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2507 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2509 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2512 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2514 if (flags
& ZFS_ONLINE_EXPAND
||
2515 zpool_get_prop_int(zhp
, ZPOOL_PROP_AUTOEXPAND
, NULL
)) {
2516 uint64_t wholedisk
= 0;
2518 (void) nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_WHOLE_DISK
,
2522 * XXX - L2ARC 1.0 devices can't support expansion.
2525 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2526 "cannot expand cache devices"));
2527 return (zfs_error(hdl
, EZFS_VDEVNOTSUP
, msg
));
2531 const char *fullpath
= path
;
2532 char buf
[MAXPATHLEN
];
2534 if (path
[0] != '/') {
2535 error
= zfs_resolve_shortname(path
, buf
,
2538 return (zfs_error(hdl
, EZFS_NODEVICE
,
2544 error
= zpool_relabel_disk(hdl
, fullpath
, msg
);
2550 zc
.zc_cookie
= VDEV_STATE_ONLINE
;
2553 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) != 0) {
2554 if (errno
== EINVAL
) {
2555 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "was split "
2556 "from this pool into a new one. Use '%s' "
2557 "instead"), "zpool detach");
2558 return (zfs_error(hdl
, EZFS_POSTSPLIT_ONLINE
, msg
));
2560 return (zpool_standard_error(hdl
, errno
, msg
));
2563 *newstate
= zc
.zc_cookie
;
2568 * Take the specified vdev offline
2571 zpool_vdev_offline(zpool_handle_t
*zhp
, const char *path
, boolean_t istmp
)
2573 zfs_cmd_t zc
= {"\0"};
2576 boolean_t avail_spare
, l2cache
;
2577 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2579 (void) snprintf(msg
, sizeof (msg
),
2580 dgettext(TEXT_DOMAIN
, "cannot offline %s"), path
);
2582 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2583 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2585 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2587 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2590 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2592 zc
.zc_cookie
= VDEV_STATE_OFFLINE
;
2593 zc
.zc_obj
= istmp
? ZFS_OFFLINE_TEMPORARY
: 0;
2595 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2602 * There are no other replicas of this device.
2604 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2608 * The log device has unplayed logs
2610 return (zfs_error(hdl
, EZFS_UNPLAYED_LOGS
, msg
));
2613 return (zpool_standard_error(hdl
, errno
, msg
));
2618 * Mark the given vdev faulted.
2621 zpool_vdev_fault(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2623 zfs_cmd_t zc
= {"\0"};
2625 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2627 (void) snprintf(msg
, sizeof (msg
),
2628 dgettext(TEXT_DOMAIN
, "cannot fault %llu"), (u_longlong_t
)guid
);
2630 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2632 zc
.zc_cookie
= VDEV_STATE_FAULTED
;
2635 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2642 * There are no other replicas of this device.
2644 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2647 return (zpool_standard_error(hdl
, errno
, msg
));
2653 * Mark the given vdev degraded.
2656 zpool_vdev_degrade(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2658 zfs_cmd_t zc
= {"\0"};
2660 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2662 (void) snprintf(msg
, sizeof (msg
),
2663 dgettext(TEXT_DOMAIN
, "cannot degrade %llu"), (u_longlong_t
)guid
);
2665 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2667 zc
.zc_cookie
= VDEV_STATE_DEGRADED
;
2670 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2673 return (zpool_standard_error(hdl
, errno
, msg
));
2677 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2681 is_replacing_spare(nvlist_t
*search
, nvlist_t
*tgt
, int which
)
2687 if (nvlist_lookup_nvlist_array(search
, ZPOOL_CONFIG_CHILDREN
, &child
,
2689 verify(nvlist_lookup_string(search
, ZPOOL_CONFIG_TYPE
,
2692 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0 &&
2693 children
== 2 && child
[which
] == tgt
)
2696 for (c
= 0; c
< children
; c
++)
2697 if (is_replacing_spare(child
[c
], tgt
, which
))
2705 * Attach new_disk (fully described by nvroot) to old_disk.
2706 * If 'replacing' is specified, the new disk will replace the old one.
2709 zpool_vdev_attach(zpool_handle_t
*zhp
,
2710 const char *old_disk
, const char *new_disk
, nvlist_t
*nvroot
, int replacing
)
2712 zfs_cmd_t zc
= {"\0"};
2716 boolean_t avail_spare
, l2cache
, islog
;
2721 nvlist_t
*config_root
;
2722 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2723 boolean_t rootpool
= zpool_is_bootable(zhp
);
2726 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2727 "cannot replace %s with %s"), old_disk
, new_disk
);
2729 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2730 "cannot attach %s to %s"), new_disk
, old_disk
);
2732 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2733 if ((tgt
= zpool_find_vdev(zhp
, old_disk
, &avail_spare
, &l2cache
,
2735 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2738 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2741 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2743 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2744 zc
.zc_cookie
= replacing
;
2746 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
2747 &child
, &children
) != 0 || children
!= 1) {
2748 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2749 "new device must be a single disk"));
2750 return (zfs_error(hdl
, EZFS_INVALCONFIG
, msg
));
2753 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
2754 ZPOOL_CONFIG_VDEV_TREE
, &config_root
) == 0);
2756 if ((newname
= zpool_vdev_name(NULL
, NULL
, child
[0], 0)) == NULL
)
2760 * If the target is a hot spare that has been swapped in, we can only
2761 * replace it with another hot spare.
2764 nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_IS_SPARE
, &val
) == 0 &&
2765 (zpool_find_vdev(zhp
, newname
, &avail_spare
, &l2cache
,
2766 NULL
) == NULL
|| !avail_spare
) &&
2767 is_replacing_spare(config_root
, tgt
, 1)) {
2768 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2769 "can only be replaced by another hot spare"));
2771 return (zfs_error(hdl
, EZFS_BADTARGET
, msg
));
2776 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
2779 ret
= zfs_ioctl(hdl
, ZFS_IOC_VDEV_ATTACH
, &zc
);
2781 zcmd_free_nvlists(&zc
);
2786 * XXX need a better way to prevent user from
2787 * booting up a half-baked vdev.
2789 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Make "
2790 "sure to wait until resilver is done "
2791 "before rebooting.\n"));
2799 * Can't attach to or replace this type of vdev.
2802 uint64_t version
= zpool_get_prop_int(zhp
,
2803 ZPOOL_PROP_VERSION
, NULL
);
2806 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2807 "cannot replace a log with a spare"));
2808 else if (version
>= SPA_VERSION_MULTI_REPLACE
)
2809 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2810 "already in replacing/spare config; wait "
2811 "for completion or use 'zpool detach'"));
2813 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2814 "cannot replace a replacing device"));
2816 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2817 "can only attach to mirrors and top-level "
2820 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2825 * The new device must be a single disk.
2827 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2828 "new device must be a single disk"));
2829 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2833 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "%s is busy"),
2835 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2840 * The new device is too small.
2842 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2843 "device is too small"));
2844 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2849 * The new device has a different optimal sector size.
2851 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2852 "new device has a different optimal sector size; use the "
2853 "option '-o ashift=N' to override the optimal size"));
2854 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2859 * The resulting top-level vdev spec won't fit in the label.
2861 (void) zfs_error(hdl
, EZFS_DEVOVERFLOW
, msg
);
2865 (void) zpool_standard_error(hdl
, errno
, msg
);
2872 * Detach the specified device.
2875 zpool_vdev_detach(zpool_handle_t
*zhp
, const char *path
)
2877 zfs_cmd_t zc
= {"\0"};
2880 boolean_t avail_spare
, l2cache
;
2881 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2883 (void) snprintf(msg
, sizeof (msg
),
2884 dgettext(TEXT_DOMAIN
, "cannot detach %s"), path
);
2886 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2887 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2889 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2892 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2895 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2897 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2899 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_DETACH
, &zc
) == 0)
2906 * Can't detach from this type of vdev.
2908 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "only "
2909 "applicable to mirror and replacing vdevs"));
2910 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2915 * There are no other replicas of this device.
2917 (void) zfs_error(hdl
, EZFS_NOREPLICAS
, msg
);
2921 (void) zpool_standard_error(hdl
, errno
, msg
);
2928 * Find a mirror vdev in the source nvlist.
2930 * The mchild array contains a list of disks in one of the top-level mirrors
2931 * of the source pool. The schild array contains a list of disks that the
2932 * user specified on the command line. We loop over the mchild array to
2933 * see if any entry in the schild array matches.
2935 * If a disk in the mchild array is found in the schild array, we return
2936 * the index of that entry. Otherwise we return -1.
2939 find_vdev_entry(zpool_handle_t
*zhp
, nvlist_t
**mchild
, uint_t mchildren
,
2940 nvlist_t
**schild
, uint_t schildren
)
2944 for (mc
= 0; mc
< mchildren
; mc
++) {
2946 char *mpath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
2949 for (sc
= 0; sc
< schildren
; sc
++) {
2950 char *spath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
2952 boolean_t result
= (strcmp(mpath
, spath
) == 0);
2968 * Split a mirror pool. If newroot points to null, then a new nvlist
2969 * is generated and it is the responsibility of the caller to free it.
2972 zpool_vdev_split(zpool_handle_t
*zhp
, char *newname
, nvlist_t
**newroot
,
2973 nvlist_t
*props
, splitflags_t flags
)
2975 zfs_cmd_t zc
= {"\0"};
2977 nvlist_t
*tree
, *config
, **child
, **newchild
, *newconfig
= NULL
;
2978 nvlist_t
**varray
= NULL
, *zc_props
= NULL
;
2979 uint_t c
, children
, newchildren
, lastlog
= 0, vcount
, found
= 0;
2980 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2982 boolean_t freelist
= B_FALSE
, memory_err
= B_TRUE
;
2985 (void) snprintf(msg
, sizeof (msg
),
2986 dgettext(TEXT_DOMAIN
, "Unable to split %s"), zhp
->zpool_name
);
2988 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
2989 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
2991 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
2992 (void) fprintf(stderr
, gettext("Internal error: unable to "
2993 "retrieve pool configuration\n"));
2997 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &tree
)
2999 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
, &vers
) == 0);
3002 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
3003 if ((zc_props
= zpool_valid_proplist(hdl
, zhp
->zpool_name
,
3004 props
, vers
, flags
, msg
)) == NULL
)
3008 if (nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
3010 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3011 "Source pool is missing vdev tree"));
3012 nvlist_free(zc_props
);
3016 varray
= zfs_alloc(hdl
, children
* sizeof (nvlist_t
*));
3019 if (*newroot
== NULL
||
3020 nvlist_lookup_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
3021 &newchild
, &newchildren
) != 0)
3024 for (c
= 0; c
< children
; c
++) {
3025 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
3027 nvlist_t
**mchild
, *vdev
;
3032 * Unlike cache & spares, slogs are stored in the
3033 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3035 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
3037 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
3039 if (is_log
|| is_hole
) {
3041 * Create a hole vdev and put it in the config.
3043 if (nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) != 0)
3045 if (nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
,
3046 VDEV_TYPE_HOLE
) != 0)
3048 if (nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_HOLE
,
3053 varray
[vcount
++] = vdev
;
3057 verify(nvlist_lookup_string(child
[c
], ZPOOL_CONFIG_TYPE
, &type
)
3059 if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
3060 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3061 "Source pool must be composed only of mirrors\n"));
3062 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3066 verify(nvlist_lookup_nvlist_array(child
[c
],
3067 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
3069 /* find or add an entry for this top-level vdev */
3070 if (newchildren
> 0 &&
3071 (entry
= find_vdev_entry(zhp
, mchild
, mchildren
,
3072 newchild
, newchildren
)) >= 0) {
3073 /* We found a disk that the user specified. */
3074 vdev
= mchild
[entry
];
3077 /* User didn't specify a disk for this vdev. */
3078 vdev
= mchild
[mchildren
- 1];
3081 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
3085 /* did we find every disk the user specified? */
3086 if (found
!= newchildren
) {
3087 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Device list must "
3088 "include at most one disk from each mirror"));
3089 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3093 /* Prepare the nvlist for populating. */
3094 if (*newroot
== NULL
) {
3095 if (nvlist_alloc(newroot
, NV_UNIQUE_NAME
, 0) != 0)
3098 if (nvlist_add_string(*newroot
, ZPOOL_CONFIG_TYPE
,
3099 VDEV_TYPE_ROOT
) != 0)
3102 verify(nvlist_remove_all(*newroot
, ZPOOL_CONFIG_CHILDREN
) == 0);
3105 /* Add all the children we found */
3106 if (nvlist_add_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
, varray
,
3107 lastlog
== 0 ? vcount
: lastlog
) != 0)
3111 * If we're just doing a dry run, exit now with success.
3114 memory_err
= B_FALSE
;
3119 /* now build up the config list & call the ioctl */
3120 if (nvlist_alloc(&newconfig
, NV_UNIQUE_NAME
, 0) != 0)
3123 if (nvlist_add_nvlist(newconfig
,
3124 ZPOOL_CONFIG_VDEV_TREE
, *newroot
) != 0 ||
3125 nvlist_add_string(newconfig
,
3126 ZPOOL_CONFIG_POOL_NAME
, newname
) != 0 ||
3127 nvlist_add_uint64(newconfig
, ZPOOL_CONFIG_VERSION
, vers
) != 0)
3131 * The new pool is automatically part of the namespace unless we
3132 * explicitly export it.
3135 zc
.zc_cookie
= ZPOOL_EXPORT_AFTER_SPLIT
;
3136 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3137 (void) strlcpy(zc
.zc_string
, newname
, sizeof (zc
.zc_string
));
3138 if (zcmd_write_conf_nvlist(hdl
, &zc
, newconfig
) != 0)
3140 if (zc_props
!= NULL
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
3143 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SPLIT
, &zc
) != 0) {
3144 retval
= zpool_standard_error(hdl
, errno
, msg
);
3149 memory_err
= B_FALSE
;
3152 if (varray
!= NULL
) {
3155 for (v
= 0; v
< vcount
; v
++)
3156 nvlist_free(varray
[v
]);
3159 zcmd_free_nvlists(&zc
);
3160 nvlist_free(zc_props
);
3161 nvlist_free(newconfig
);
3163 nvlist_free(*newroot
);
3171 return (no_memory(hdl
));
3177 * Remove the given device. Currently, this is supported only for hot spares,
3178 * cache, and log devices.
3181 zpool_vdev_remove(zpool_handle_t
*zhp
, const char *path
)
3183 zfs_cmd_t zc
= {"\0"};
3186 boolean_t avail_spare
, l2cache
, islog
;
3187 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3190 (void) snprintf(msg
, sizeof (msg
),
3191 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3193 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3194 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3196 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3198 * XXX - this should just go away.
3200 if (!avail_spare
&& !l2cache
&& !islog
) {
3201 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3202 "only inactive hot spares, cache, "
3203 "or log devices can be removed"));
3204 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3207 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
3208 if (islog
&& version
< SPA_VERSION_HOLES
) {
3209 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3210 "pool must be upgrade to support log removal"));
3211 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
3214 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
3216 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3219 return (zpool_standard_error(hdl
, errno
, msg
));
3223 * Clear the errors for the pool, or the particular device if specified.
3226 zpool_clear(zpool_handle_t
*zhp
, const char *path
, nvlist_t
*rewindnvl
)
3228 zfs_cmd_t zc
= {"\0"};
3231 zpool_rewind_policy_t policy
;
3232 boolean_t avail_spare
, l2cache
;
3233 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3234 nvlist_t
*nvi
= NULL
;
3238 (void) snprintf(msg
, sizeof (msg
),
3239 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3242 (void) snprintf(msg
, sizeof (msg
),
3243 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3246 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3248 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
,
3249 &l2cache
, NULL
)) == 0)
3250 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3253 * Don't allow error clearing for hot spares. Do allow
3254 * error clearing for l2cache devices.
3257 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3259 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
,
3263 zpool_get_rewind_policy(rewindnvl
, &policy
);
3264 zc
.zc_cookie
= policy
.zrp_request
;
3266 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zhp
->zpool_config_size
* 2) != 0)
3269 if (zcmd_write_src_nvlist(hdl
, &zc
, rewindnvl
) != 0)
3272 while ((error
= zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
)) != 0 &&
3274 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
3275 zcmd_free_nvlists(&zc
);
3280 if (!error
|| ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) &&
3281 errno
!= EPERM
&& errno
!= EACCES
)) {
3282 if (policy
.zrp_request
&
3283 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
3284 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nvi
);
3285 zpool_rewind_exclaim(hdl
, zc
.zc_name
,
3286 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0),
3290 zcmd_free_nvlists(&zc
);
3294 zcmd_free_nvlists(&zc
);
3295 return (zpool_standard_error(hdl
, errno
, msg
));
3299 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3302 zpool_vdev_clear(zpool_handle_t
*zhp
, uint64_t guid
)
3304 zfs_cmd_t zc
= {"\0"};
3306 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3308 (void) snprintf(msg
, sizeof (msg
),
3309 dgettext(TEXT_DOMAIN
, "cannot clear errors for %llx"),
3310 (u_longlong_t
)guid
);
3312 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3314 zc
.zc_cookie
= ZPOOL_NO_REWIND
;
3316 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_CLEAR
, &zc
) == 0)
3319 return (zpool_standard_error(hdl
, errno
, msg
));
3323 * Change the GUID for a pool.
3326 zpool_reguid(zpool_handle_t
*zhp
)
3329 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3330 zfs_cmd_t zc
= {"\0"};
3332 (void) snprintf(msg
, sizeof (msg
),
3333 dgettext(TEXT_DOMAIN
, "cannot reguid '%s'"), zhp
->zpool_name
);
3335 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3336 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REGUID
, &zc
) == 0)
3339 return (zpool_standard_error(hdl
, errno
, msg
));
3346 zpool_reopen(zpool_handle_t
*zhp
)
3348 zfs_cmd_t zc
= {"\0"};
3350 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3352 (void) snprintf(msg
, sizeof (msg
),
3353 dgettext(TEXT_DOMAIN
, "cannot reopen '%s'"),
3356 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3357 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REOPEN
, &zc
) == 0)
3359 return (zpool_standard_error(hdl
, errno
, msg
));
3362 /* call into libzfs_core to execute the sync IOCTL per pool */
3364 zpool_sync_one(zpool_handle_t
*zhp
, void *data
)
3367 libzfs_handle_t
*hdl
= zpool_get_handle(zhp
);
3368 const char *pool_name
= zpool_get_name(zhp
);
3369 boolean_t
*force
= data
;
3370 nvlist_t
*innvl
= fnvlist_alloc();
3372 fnvlist_add_boolean_value(innvl
, "force", *force
);
3373 if ((ret
= lzc_sync(pool_name
, innvl
, NULL
)) != 0) {
3375 return (zpool_standard_error_fmt(hdl
, ret
,
3376 dgettext(TEXT_DOMAIN
, "sync '%s' failed"), pool_name
));
3383 #if defined(__sun__) || defined(__sun)
3385 * Convert from a devid string to a path.
3388 devid_to_path(char *devid_str
)
3393 devid_nmlist_t
*list
= NULL
;
3396 if (devid_str_decode(devid_str
, &devid
, &minor
) != 0)
3399 ret
= devid_deviceid_to_nmlist("/dev", devid
, minor
, &list
);
3401 devid_str_free(minor
);
3408 * In a case the strdup() fails, we will just return NULL below.
3410 path
= strdup(list
[0].devname
);
3412 devid_free_nmlist(list
);
3418 * Convert from a path to a devid string.
3421 path_to_devid(const char *path
)
3427 if ((fd
= open(path
, O_RDONLY
)) < 0)
3432 if (devid_get(fd
, &devid
) == 0) {
3433 if (devid_get_minor_name(fd
, &minor
) == 0)
3434 ret
= devid_str_encode(devid
, minor
);
3436 devid_str_free(minor
);
3445 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3446 * ignore any failure here, since a common case is for an unprivileged user to
3447 * type 'zpool status', and we'll display the correct information anyway.
3450 set_path(zpool_handle_t
*zhp
, nvlist_t
*nv
, const char *path
)
3452 zfs_cmd_t zc
= {"\0"};
3454 (void) strncpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3455 (void) strncpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
3456 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
3459 (void) ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_VDEV_SETPATH
, &zc
);
3464 * Remove partition suffix from a vdev path. Partition suffixes may take three
3465 * forms: "-partX", "pX", or "X", where X is a string of digits. The second
3466 * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
3467 * third case only occurs when preceded by a string matching the regular
3468 * expression "^([hsv]|xv)d[a-z]+", i.e. a scsi, ide, virtio or xen disk.
3470 * caller must free the returned string
3473 zfs_strip_partition(char *path
)
3475 char *tmp
= strdup(path
);
3476 char *part
= NULL
, *d
= NULL
;
3480 if ((part
= strstr(tmp
, "-part")) && part
!= tmp
) {
3482 } else if ((part
= strrchr(tmp
, 'p')) &&
3483 part
> tmp
+ 1 && isdigit(*(part
-1))) {
3485 } else if ((tmp
[0] == 'h' || tmp
[0] == 's' || tmp
[0] == 'v') &&
3487 for (d
= &tmp
[2]; isalpha(*d
); part
= ++d
) { }
3488 } else if (strncmp("xvd", tmp
, 3) == 0) {
3489 for (d
= &tmp
[3]; isalpha(*d
); part
= ++d
) { }
3491 if (part
&& d
&& *d
!= '\0') {
3492 for (; isdigit(*d
); d
++) { }
3501 * Same as zfs_strip_partition, but allows "/dev/" to be in the pathname
3506 * Returned string must be freed.
3509 zfs_strip_partition_path(char *path
)
3511 char *newpath
= strdup(path
);
3518 /* Point to "sda1" part of "/dev/sda1" */
3519 sd_offset
= strrchr(newpath
, '/') + 1;
3521 /* Get our new name "sda" */
3522 new_sd
= zfs_strip_partition(sd_offset
);
3528 /* Paste the "sda" where "sda1" was */
3529 strlcpy(sd_offset
, new_sd
, strlen(sd_offset
) + 1);
3531 /* Free temporary "sda" */
3537 #define PATH_BUF_LEN 64
3540 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3541 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3542 * We also check if this is a whole disk, in which case we strip off the
3543 * trailing 's0' slice name.
3545 * This routine is also responsible for identifying when disks have been
3546 * reconfigured in a new location. The kernel will have opened the device by
3547 * devid, but the path will still refer to the old location. To catch this, we
3548 * first do a path -> devid translation (which is fast for the common case). If
3549 * the devid matches, we're done. If not, we do a reverse devid -> path
3550 * translation and issue the appropriate ioctl() to update the path of the vdev.
3551 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3555 zpool_vdev_name(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, nvlist_t
*nv
,
3558 char *path
, *type
, *env
;
3560 char buf
[PATH_BUF_LEN
];
3561 char tmpbuf
[PATH_BUF_LEN
];
3563 env
= getenv("ZPOOL_VDEV_NAME_PATH");
3564 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3565 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3566 name_flags
|= VDEV_NAME_PATH
;
3568 env
= getenv("ZPOOL_VDEV_NAME_GUID");
3569 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3570 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3571 name_flags
|= VDEV_NAME_GUID
;
3573 env
= getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
3574 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3575 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3576 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
3578 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
, &value
) == 0 ||
3579 name_flags
& VDEV_NAME_GUID
) {
3580 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
);
3581 (void) snprintf(buf
, sizeof (buf
), "%llu", (u_longlong_t
)value
);
3583 } else if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
3584 #if defined(__sun__) || defined(__sun)
3586 * Live VDEV path updates to a kernel VDEV during a
3587 * zpool_vdev_name lookup are not supported on Linux.
3594 * If the device is dead (faulted, offline, etc) then don't
3595 * bother opening it. Otherwise we may be forcing the user to
3596 * open a misbehaving device, which can have undesirable
3599 if ((nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
3600 (uint64_t **)&vs
, &vsc
) != 0 ||
3601 vs
->vs_state
>= VDEV_STATE_DEGRADED
) &&
3603 nvlist_lookup_string(nv
, ZPOOL_CONFIG_DEVID
, &devid
) == 0) {
3605 * Determine if the current path is correct.
3607 char *newdevid
= path_to_devid(path
);
3609 if (newdevid
== NULL
||
3610 strcmp(devid
, newdevid
) != 0) {
3613 if ((newpath
= devid_to_path(devid
)) != NULL
) {
3615 * Update the path appropriately.
3617 set_path(zhp
, nv
, newpath
);
3618 if (nvlist_add_string(nv
,
3619 ZPOOL_CONFIG_PATH
, newpath
) == 0)
3620 verify(nvlist_lookup_string(nv
,
3628 devid_str_free(newdevid
);
3632 if (name_flags
& VDEV_NAME_FOLLOW_LINKS
) {
3633 char *rp
= realpath(path
, NULL
);
3635 strlcpy(buf
, rp
, sizeof (buf
));
3642 * For a block device only use the name.
3644 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
3645 if ((strcmp(type
, VDEV_TYPE_DISK
) == 0) &&
3646 !(name_flags
& VDEV_NAME_PATH
)) {
3647 path
= strrchr(path
, '/');
3652 * Remove the partition from the path it this is a whole disk.
3654 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
, &value
)
3655 == 0 && value
&& !(name_flags
& VDEV_NAME_PATH
)) {
3656 return (zfs_strip_partition(path
));
3659 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &path
) == 0);
3662 * If it's a raidz device, we need to stick in the parity level.
3664 if (strcmp(path
, VDEV_TYPE_RAIDZ
) == 0) {
3665 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
,
3667 (void) snprintf(buf
, sizeof (buf
), "%s%llu", path
,
3668 (u_longlong_t
)value
);
3673 * We identify each top-level vdev by using a <type-id>
3674 * naming convention.
3676 if (name_flags
& VDEV_NAME_TYPE_ID
) {
3678 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
3680 (void) snprintf(tmpbuf
, sizeof (tmpbuf
), "%s-%llu",
3681 path
, (u_longlong_t
)id
);
3686 return (zfs_strdup(hdl
, path
));
3690 zbookmark_mem_compare(const void *a
, const void *b
)
3692 return (memcmp(a
, b
, sizeof (zbookmark_phys_t
)));
3696 * Retrieve the persistent error log, uniquify the members, and return to the
3700 zpool_get_errlog(zpool_handle_t
*zhp
, nvlist_t
**nverrlistp
)
3702 zfs_cmd_t zc
= {"\0"};
3703 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3705 zbookmark_phys_t
*zb
= NULL
;
3709 * Retrieve the raw error list from the kernel. If the number of errors
3710 * has increased, allocate more space and continue until we get the
3713 verify(nvlist_lookup_uint64(zhp
->zpool_config
, ZPOOL_CONFIG_ERRCOUNT
,
3717 zc
.zc_nvlist_dst
= (uintptr_t)zfs_alloc(zhp
->zpool_hdl
,
3718 count
* sizeof (zbookmark_phys_t
));
3719 zc
.zc_nvlist_dst_size
= count
;
3720 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3722 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_ERROR_LOG
,
3724 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3725 if (errno
== ENOMEM
) {
3728 count
= zc
.zc_nvlist_dst_size
;
3729 dst
= zfs_alloc(zhp
->zpool_hdl
, count
*
3730 sizeof (zbookmark_phys_t
));
3731 zc
.zc_nvlist_dst
= (uintptr_t)dst
;
3733 return (zpool_standard_error_fmt(hdl
, errno
,
3734 dgettext(TEXT_DOMAIN
, "errors: List of "
3735 "errors unavailable")));
3743 * Sort the resulting bookmarks. This is a little confusing due to the
3744 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3745 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3746 * _not_ copied as part of the process. So we point the start of our
3747 * array appropriate and decrement the total number of elements.
3749 zb
= ((zbookmark_phys_t
*)(uintptr_t)zc
.zc_nvlist_dst
) +
3750 zc
.zc_nvlist_dst_size
;
3751 count
-= zc
.zc_nvlist_dst_size
;
3753 qsort(zb
, count
, sizeof (zbookmark_phys_t
), zbookmark_mem_compare
);
3755 verify(nvlist_alloc(nverrlistp
, 0, KM_SLEEP
) == 0);
3758 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3760 for (i
= 0; i
< count
; i
++) {
3763 /* ignoring zb_blkid and zb_level for now */
3764 if (i
> 0 && zb
[i
-1].zb_objset
== zb
[i
].zb_objset
&&
3765 zb
[i
-1].zb_object
== zb
[i
].zb_object
)
3768 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) != 0)
3770 if (nvlist_add_uint64(nv
, ZPOOL_ERR_DATASET
,
3771 zb
[i
].zb_objset
) != 0) {
3775 if (nvlist_add_uint64(nv
, ZPOOL_ERR_OBJECT
,
3776 zb
[i
].zb_object
) != 0) {
3780 if (nvlist_add_nvlist(*nverrlistp
, "ejk", nv
) != 0) {
3787 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3791 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3792 return (no_memory(zhp
->zpool_hdl
));
3796 * Upgrade a ZFS pool to the latest on-disk version.
3799 zpool_upgrade(zpool_handle_t
*zhp
, uint64_t new_version
)
3801 zfs_cmd_t zc
= {"\0"};
3802 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3804 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3805 zc
.zc_cookie
= new_version
;
3807 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_UPGRADE
, &zc
) != 0)
3808 return (zpool_standard_error_fmt(hdl
, errno
,
3809 dgettext(TEXT_DOMAIN
, "cannot upgrade '%s'"),
3815 zfs_save_arguments(int argc
, char **argv
, char *string
, int len
)
3819 (void) strlcpy(string
, basename(argv
[0]), len
);
3820 for (i
= 1; i
< argc
; i
++) {
3821 (void) strlcat(string
, " ", len
);
3822 (void) strlcat(string
, argv
[i
], len
);
3827 zpool_log_history(libzfs_handle_t
*hdl
, const char *message
)
3829 zfs_cmd_t zc
= {"\0"};
3833 args
= fnvlist_alloc();
3834 fnvlist_add_string(args
, "message", message
);
3835 err
= zcmd_write_src_nvlist(hdl
, &zc
, args
);
3837 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_LOG_HISTORY
, &zc
);
3839 zcmd_free_nvlists(&zc
);
3844 * Perform ioctl to get some command history of a pool.
3846 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3847 * logical offset of the history buffer to start reading from.
3849 * Upon return, 'off' is the next logical offset to read from and
3850 * 'len' is the actual amount of bytes read into 'buf'.
3853 get_history(zpool_handle_t
*zhp
, char *buf
, uint64_t *off
, uint64_t *len
)
3855 zfs_cmd_t zc
= {"\0"};
3856 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3858 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3860 zc
.zc_history
= (uint64_t)(uintptr_t)buf
;
3861 zc
.zc_history_len
= *len
;
3862 zc
.zc_history_offset
= *off
;
3864 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_HISTORY
, &zc
) != 0) {
3867 return (zfs_error_fmt(hdl
, EZFS_PERM
,
3868 dgettext(TEXT_DOMAIN
,
3869 "cannot show history for pool '%s'"),
3872 return (zfs_error_fmt(hdl
, EZFS_NOHISTORY
,
3873 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
3874 "'%s'"), zhp
->zpool_name
));
3876 return (zfs_error_fmt(hdl
, EZFS_BADVERSION
,
3877 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
3878 "'%s', pool must be upgraded"), zhp
->zpool_name
));
3880 return (zpool_standard_error_fmt(hdl
, errno
,
3881 dgettext(TEXT_DOMAIN
,
3882 "cannot get history for '%s'"), zhp
->zpool_name
));
3886 *len
= zc
.zc_history_len
;
3887 *off
= zc
.zc_history_offset
;
3893 * Process the buffer of nvlists, unpacking and storing each nvlist record
3894 * into 'records'. 'leftover' is set to the number of bytes that weren't
3895 * processed as there wasn't a complete record.
3898 zpool_history_unpack(char *buf
, uint64_t bytes_read
, uint64_t *leftover
,
3899 nvlist_t
***records
, uint_t
*numrecords
)
3906 while (bytes_read
> sizeof (reclen
)) {
3908 /* get length of packed record (stored as little endian) */
3909 for (i
= 0, reclen
= 0; i
< sizeof (reclen
); i
++)
3910 reclen
+= (uint64_t)(((uchar_t
*)buf
)[i
]) << (8*i
);
3912 if (bytes_read
< sizeof (reclen
) + reclen
)
3916 if (nvlist_unpack(buf
+ sizeof (reclen
), reclen
, &nv
, 0) != 0)
3918 bytes_read
-= sizeof (reclen
) + reclen
;
3919 buf
+= sizeof (reclen
) + reclen
;
3921 /* add record to nvlist array */
3923 if (ISP2(*numrecords
+ 1)) {
3924 tmp
= realloc(*records
,
3925 *numrecords
* 2 * sizeof (nvlist_t
*));
3933 (*records
)[*numrecords
- 1] = nv
;
3936 *leftover
= bytes_read
;
3941 * Retrieve the command history of a pool.
3944 zpool_get_history(zpool_handle_t
*zhp
, nvlist_t
**nvhisp
)
3947 int buflen
= 128 * 1024;
3949 nvlist_t
**records
= NULL
;
3950 uint_t numrecords
= 0;
3953 buf
= malloc(buflen
);
3957 uint64_t bytes_read
= buflen
;
3960 if ((err
= get_history(zhp
, buf
, &off
, &bytes_read
)) != 0)
3963 /* if nothing else was read in, we're at EOF, just return */
3967 if ((err
= zpool_history_unpack(buf
, bytes_read
,
3968 &leftover
, &records
, &numrecords
)) != 0)
3971 if (leftover
== bytes_read
) {
3973 * no progress made, because buffer is not big enough
3974 * to hold this record; resize and retry.
3978 buf
= malloc(buflen
);
3989 verify(nvlist_alloc(nvhisp
, NV_UNIQUE_NAME
, 0) == 0);
3990 verify(nvlist_add_nvlist_array(*nvhisp
, ZPOOL_HIST_RECORD
,
3991 records
, numrecords
) == 0);
3993 for (i
= 0; i
< numrecords
; i
++)
3994 nvlist_free(records
[i
]);
4001 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
4002 * If there is a new event available 'nvp' will contain a newly allocated
4003 * nvlist and 'dropped' will be set to the number of missed events since
4004 * the last call to this function. When 'nvp' is set to NULL it indicates
4005 * no new events are available. In either case the function returns 0 and
4006 * it is up to the caller to free 'nvp'. In the case of a fatal error the
4007 * function will return a non-zero value. When the function is called in
4008 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
4009 * it will not return until a new event is available.
4012 zpool_events_next(libzfs_handle_t
*hdl
, nvlist_t
**nvp
,
4013 int *dropped
, unsigned flags
, int zevent_fd
)
4015 zfs_cmd_t zc
= {"\0"};
4020 zc
.zc_cleanup_fd
= zevent_fd
;
4022 if (flags
& ZEVENT_NONBLOCK
)
4023 zc
.zc_guid
= ZEVENT_NONBLOCK
;
4025 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, ZEVENT_SIZE
) != 0)
4029 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_NEXT
, &zc
) != 0) {
4032 error
= zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
4033 dgettext(TEXT_DOMAIN
, "zfs shutdown"));
4036 /* Blocking error case should not occur */
4037 if (!(flags
& ZEVENT_NONBLOCK
))
4038 error
= zpool_standard_error_fmt(hdl
, errno
,
4039 dgettext(TEXT_DOMAIN
, "cannot get event"));
4043 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
4044 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
4045 dgettext(TEXT_DOMAIN
, "cannot get event"));
4051 error
= zpool_standard_error_fmt(hdl
, errno
,
4052 dgettext(TEXT_DOMAIN
, "cannot get event"));
4057 error
= zcmd_read_dst_nvlist(hdl
, &zc
, nvp
);
4061 *dropped
= (int)zc
.zc_cookie
;
4063 zcmd_free_nvlists(&zc
);
4072 zpool_events_clear(libzfs_handle_t
*hdl
, int *count
)
4074 zfs_cmd_t zc
= {"\0"};
4077 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
4078 "cannot clear events"));
4080 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_CLEAR
, &zc
) != 0)
4081 return (zpool_standard_error_fmt(hdl
, errno
, msg
));
4084 *count
= (int)zc
.zc_cookie
; /* # of events cleared */
4090 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4091 * the passed zevent_fd file handle. On success zero is returned,
4092 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4095 zpool_events_seek(libzfs_handle_t
*hdl
, uint64_t eid
, int zevent_fd
)
4097 zfs_cmd_t zc
= {"\0"};
4101 zc
.zc_cleanup_fd
= zevent_fd
;
4103 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_SEEK
, &zc
) != 0) {
4106 error
= zfs_error_fmt(hdl
, EZFS_NOENT
,
4107 dgettext(TEXT_DOMAIN
, "cannot get event"));
4111 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
4112 dgettext(TEXT_DOMAIN
, "cannot get event"));
4116 error
= zpool_standard_error_fmt(hdl
, errno
,
4117 dgettext(TEXT_DOMAIN
, "cannot get event"));
4126 zpool_obj_to_path(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4127 char *pathname
, size_t len
)
4129 zfs_cmd_t zc
= {"\0"};
4130 boolean_t mounted
= B_FALSE
;
4131 char *mntpnt
= NULL
;
4132 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
4135 /* special case for the MOS */
4136 (void) snprintf(pathname
, len
, "<metadata>:<0x%llx>",
4141 /* get the dataset's name */
4142 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4144 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
,
4145 ZFS_IOC_DSOBJ_TO_DSNAME
, &zc
) != 0) {
4146 /* just write out a path of two object numbers */
4147 (void) snprintf(pathname
, len
, "<0x%llx>:<0x%llx>",
4148 (longlong_t
)dsobj
, (longlong_t
)obj
);
4151 (void) strlcpy(dsname
, zc
.zc_value
, sizeof (dsname
));
4153 /* find out if the dataset is mounted */
4154 mounted
= is_mounted(zhp
->zpool_hdl
, dsname
, &mntpnt
);
4156 /* get the corrupted object's path */
4157 (void) strlcpy(zc
.zc_name
, dsname
, sizeof (zc
.zc_name
));
4159 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_OBJ_TO_PATH
,
4162 (void) snprintf(pathname
, len
, "%s%s", mntpnt
,
4165 (void) snprintf(pathname
, len
, "%s:%s",
4166 dsname
, zc
.zc_value
);
4169 (void) snprintf(pathname
, len
, "%s:<0x%llx>", dsname
,
4176 * Read the EFI label from the config, if a label does not exist then
4177 * pass back the error to the caller. If the caller has passed a non-NULL
4178 * diskaddr argument then we set it to the starting address of the EFI
4182 read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
)
4186 char diskname
[MAXPATHLEN
];
4189 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PATH
, &path
) != 0)
4192 (void) snprintf(diskname
, sizeof (diskname
), "%s%s", DISK_ROOT
,
4193 strrchr(path
, '/'));
4194 if ((fd
= open(diskname
, O_RDONLY
|O_DIRECT
)) >= 0) {
4195 struct dk_gpt
*vtoc
;
4197 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) >= 0) {
4199 *sb
= vtoc
->efi_parts
[0].p_start
;
4208 * determine where a partition starts on a disk in the current
4212 find_start_block(nvlist_t
*config
)
4216 diskaddr_t sb
= MAXOFFSET_T
;
4219 if (nvlist_lookup_nvlist_array(config
,
4220 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) != 0) {
4221 if (nvlist_lookup_uint64(config
,
4222 ZPOOL_CONFIG_WHOLE_DISK
,
4223 &wholedisk
) != 0 || !wholedisk
) {
4224 return (MAXOFFSET_T
);
4226 if (read_efi_label(config
, &sb
) < 0)
4231 for (c
= 0; c
< children
; c
++) {
4232 sb
= find_start_block(child
[c
]);
4233 if (sb
!= MAXOFFSET_T
) {
4237 return (MAXOFFSET_T
);
4241 zpool_label_disk_check(char *path
)
4243 struct dk_gpt
*vtoc
;
4246 if ((fd
= open(path
, O_RDONLY
|O_DIRECT
)) < 0)
4249 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) != 0) {
4254 if (vtoc
->efi_flags
& EFI_GPT_PRIMARY_CORRUPT
) {
4266 * Generate a unique partition name for the ZFS member. Partitions must
4267 * have unique names to ensure udev will be able to create symlinks under
4268 * /dev/disk/by-partlabel/ for all pool members. The partition names are
4269 * of the form <pool>-<unique-id>.
4272 zpool_label_name(char *label_name
, int label_size
)
4277 fd
= open("/dev/urandom", O_RDONLY
);
4279 if (read(fd
, &id
, sizeof (id
)) != sizeof (id
))
4286 id
= (((uint64_t)rand()) << 32) | (uint64_t)rand();
4288 snprintf(label_name
, label_size
, "zfs-%016llx", (u_longlong_t
)id
);
4292 * Label an individual disk. The name provided is the short name,
4293 * stripped of any leading /dev path.
4296 zpool_label_disk(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, char *name
)
4298 char path
[MAXPATHLEN
];
4299 struct dk_gpt
*vtoc
;
4301 size_t resv
= EFI_MIN_RESV_SIZE
;
4302 uint64_t slice_size
;
4303 diskaddr_t start_block
;
4306 /* prepare an error message just in case */
4307 (void) snprintf(errbuf
, sizeof (errbuf
),
4308 dgettext(TEXT_DOMAIN
, "cannot label '%s'"), name
);
4313 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
4314 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
4316 if (zhp
->zpool_start_block
== 0)
4317 start_block
= find_start_block(nvroot
);
4319 start_block
= zhp
->zpool_start_block
;
4320 zhp
->zpool_start_block
= start_block
;
4323 start_block
= NEW_START_BLOCK
;
4326 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4328 if ((fd
= open(path
, O_RDWR
|O_DIRECT
|O_EXCL
)) < 0) {
4330 * This shouldn't happen. We've long since verified that this
4331 * is a valid device.
4333 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
4334 "label '%s': unable to open device: %d"), path
, errno
);
4335 return (zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
));
4338 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, &vtoc
) != 0) {
4340 * The only way this can fail is if we run out of memory, or we
4341 * were unable to read the disk's capacity
4343 if (errno
== ENOMEM
)
4344 (void) no_memory(hdl
);
4347 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
4348 "label '%s': unable to read disk capacity"), path
);
4350 return (zfs_error(hdl
, EZFS_NOCAP
, errbuf
));
4353 slice_size
= vtoc
->efi_last_u_lba
+ 1;
4354 slice_size
-= EFI_MIN_RESV_SIZE
;
4355 if (start_block
== MAXOFFSET_T
)
4356 start_block
= NEW_START_BLOCK
;
4357 slice_size
-= start_block
;
4358 slice_size
= P2ALIGN(slice_size
, PARTITION_END_ALIGNMENT
);
4360 vtoc
->efi_parts
[0].p_start
= start_block
;
4361 vtoc
->efi_parts
[0].p_size
= slice_size
;
4364 * Why we use V_USR: V_BACKUP confuses users, and is considered
4365 * disposable by some EFI utilities (since EFI doesn't have a backup
4366 * slice). V_UNASSIGNED is supposed to be used only for zero size
4367 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4368 * etc. were all pretty specific. V_USR is as close to reality as we
4369 * can get, in the absence of V_OTHER.
4371 vtoc
->efi_parts
[0].p_tag
= V_USR
;
4372 zpool_label_name(vtoc
->efi_parts
[0].p_name
, EFI_PART_NAME_LEN
);
4374 vtoc
->efi_parts
[8].p_start
= slice_size
+ start_block
;
4375 vtoc
->efi_parts
[8].p_size
= resv
;
4376 vtoc
->efi_parts
[8].p_tag
= V_RESERVED
;
4378 rval
= efi_write(fd
, vtoc
);
4380 /* Flush the buffers to disk and invalidate the page cache. */
4382 (void) ioctl(fd
, BLKFLSBUF
);
4385 rval
= efi_rescan(fd
);
4388 * Some block drivers (like pcata) may not support EFI GPT labels.
4389 * Print out a helpful error message directing the user to manually
4390 * label the disk and give a specific slice.
4396 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "try using "
4397 "parted(8) and then provide a specific slice: %d"), rval
);
4398 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4404 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4405 (void) zfs_append_partition(path
, MAXPATHLEN
);
4407 /* Wait to udev to signal use the device has settled. */
4408 rval
= zpool_label_disk_wait(path
, DISK_LABEL_WAIT
);
4410 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "failed to "
4411 "detect device partitions on '%s': %d"), path
, rval
);
4412 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4415 /* We can't be to paranoid. Read the label back and verify it. */
4416 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4417 rval
= zpool_label_disk_check(path
);
4419 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "freshly written "
4420 "EFI label on '%s' is damaged. Ensure\nthis device "
4421 "is not in in use, and is functioning properly: %d"),
4423 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4430 * Allocate and return the underlying device name for a device mapper device.
4431 * If a device mapper device maps to multiple devices, return the first device.
4433 * For example, dm_name = "/dev/dm-0" could return "/dev/sda". Symlinks to a
4434 * DM device (like /dev/disk/by-vdev/A0) are also allowed.
4436 * Returns device name, or NULL on error or no match. If dm_name is not a DM
4437 * device then return NULL.
4439 * NOTE: The returned name string must be *freed*.
4442 dm_get_underlying_path(char *dm_name
)
4452 if (dm_name
== NULL
)
4455 /* dm name may be a symlink (like /dev/disk/by-vdev/A0) */
4456 realp
= realpath(dm_name
, NULL
);
4461 * If they preface 'dev' with a path (like "/dev") then strip it off.
4462 * We just want the 'dm-N' part.
4464 tmp
= strrchr(realp
, '/');
4466 dev_str
= tmp
+ 1; /* +1 since we want the chr after '/' */
4470 size
= asprintf(&tmp
, "/sys/block/%s/slaves/", dev_str
);
4471 if (size
== -1 || !tmp
)
4478 /* Return first sd* entry in /sys/block/dm-N/slaves/ */
4479 while ((ep
= readdir(dp
))) {
4480 if (ep
->d_type
!= DT_DIR
) { /* skip "." and ".." dirs */
4481 size
= asprintf(&path
, "/dev/%s", ep
->d_name
);
4495 * Return 1 if device is a device mapper or multipath device.
4499 zfs_dev_is_dm(char *dev_name
)
4503 tmp
= dm_get_underlying_path(dev_name
);
4512 * By "whole disk" we mean an entire physical disk (something we can
4513 * label, toggle the write cache on, etc.) as opposed to the full
4514 * capacity of a pseudo-device such as lofi or did. We act as if we
4515 * are labeling the disk, which should be a pretty good test of whether
4516 * it's a viable device or not. Returns B_TRUE if it is and B_FALSE if
4520 zfs_dev_is_whole_disk(char *dev_name
)
4522 struct dk_gpt
*label
;
4525 if ((fd
= open(dev_name
, O_RDONLY
| O_DIRECT
)) < 0)
4528 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, &label
) != 0) {
4540 * Lookup the underlying device for a device name
4542 * Often you'll have a symlink to a device, a partition device,
4543 * or a multipath device, and want to look up the underlying device.
4544 * This function returns the underlying device name. If the device
4545 * name is already the underlying device, then just return the same
4546 * name. If the device is a DM device with multiple underlying devices
4547 * then return the first one.
4551 * 1. /dev/disk/by-id/ata-QEMU_HARDDISK_QM00001 -> ../../sda
4552 * dev_name: /dev/disk/by-id/ata-QEMU_HARDDISK_QM00001
4555 * 2. /dev/mapper/mpatha (made up of /dev/sda and /dev/sdb)
4556 * dev_name: /dev/mapper/mpatha
4557 * returns: /dev/sda (first device)
4559 * 3. /dev/sda (already the underlying device)
4560 * dev_name: /dev/sda
4563 * 4. /dev/dm-3 (mapped to /dev/sda)
4564 * dev_name: /dev/dm-3
4567 * 5. /dev/disk/by-id/scsi-0QEMU_drive-scsi0-0-0-0-part9 -> ../../sdb9
4568 * dev_name: /dev/disk/by-id/scsi-0QEMU_drive-scsi0-0-0-0-part9
4571 * 6. /dev/disk/by-uuid/5df030cf-3cd9-46e4-8e99-3ccb462a4e9a -> ../dev/sda2
4572 * dev_name: /dev/disk/by-uuid/5df030cf-3cd9-46e4-8e99-3ccb462a4e9a
4575 * Returns underlying device name, or NULL on error or no match.
4577 * NOTE: The returned name string must be *freed*.
4580 zfs_get_underlying_path(char *dev_name
)
4585 if (dev_name
== NULL
)
4588 tmp
= dm_get_underlying_path(dev_name
);
4590 /* dev_name not a DM device, so just un-symlinkize it */
4592 tmp
= realpath(dev_name
, NULL
);
4595 name
= zfs_strip_partition_path(tmp
);
4603 * Given a dev name like "sda", return the full enclosure sysfs path to
4604 * the disk. You can also pass in the name with "/dev" prepended
4605 * to it (like /dev/sda).
4607 * For example, disk "sda" in enclosure slot 1:
4609 * returns: "/sys/class/enclosure/1:0:3:0/Slot 1"
4611 * 'dev' must be a non-devicemapper device.
4613 * Returned string must be freed.
4616 zfs_get_enclosure_sysfs_path(char *dev_name
)
4620 char buf
[MAXPATHLEN
];
4628 if (dev_name
== NULL
)
4631 /* If they preface 'dev' with a path (like "/dev") then strip it off */
4632 tmp1
= strrchr(dev_name
, '/');
4634 dev_name
= tmp1
+ 1; /* +1 since we want the chr after '/' */
4636 tmpsize
= asprintf(&tmp1
, "/sys/block/%s/device", dev_name
);
4637 if (tmpsize
== -1 || tmp1
== NULL
) {
4644 tmp1
= NULL
; /* To make free() at the end a NOP */
4649 * Look though all sysfs entries in /sys/block/<dev>/device for
4650 * the enclosure symlink.
4652 while ((ep
= readdir(dp
))) {
4653 /* Ignore everything that's not our enclosure_device link */
4654 if (strstr(ep
->d_name
, "enclosure_device") == NULL
)
4657 if (asprintf(&tmp2
, "%s/%s", tmp1
, ep
->d_name
) == -1 ||
4661 size
= readlink(tmp2
, buf
, sizeof (buf
));
4663 /* Did readlink fail or crop the link name? */
4664 if (size
== -1 || size
>= sizeof (buf
)) {
4666 tmp2
= NULL
; /* To make free() at the end a NOP */
4671 * We got a valid link. readlink() doesn't terminate strings
4672 * so we have to do it.
4677 * Our link will look like:
4679 * "../../../../port-11:1:2/..STUFF../enclosure/1:0:3:0/SLOT 1"
4681 * We want to grab the "enclosure/1:0:3:0/SLOT 1" part
4683 tmp3
= strstr(buf
, "enclosure");
4687 if (asprintf(&path
, "/sys/class/%s", tmp3
) == -1) {
4688 /* If asprintf() fails, 'path' is undefined */