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, 2017 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
27 * Copyright (c) 2017 Datto Inc.
28 * Copyright (c) 2017 Open-E, Inc. All Rights Reserved.
43 #include <sys/efi_partition.h>
44 #include <sys/systeminfo.h>
46 #include <sys/zfs_ioctl.h>
49 #include "zfs_namecheck.h"
51 #include "libzfs_impl.h"
52 #include "zfs_comutil.h"
53 #include "zfeature_common.h"
55 static int read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
);
56 static boolean_t
zpool_vdev_is_interior(const char *name
);
58 typedef struct prop_flags
{
59 int create
:1; /* Validate property on creation */
60 int import
:1; /* Validate property on import */
64 * ====================================================================
65 * zpool property functions
66 * ====================================================================
70 zpool_get_all_props(zpool_handle_t
*zhp
)
72 zfs_cmd_t zc
= {"\0"};
73 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
75 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
77 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, 0) != 0)
80 while (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_PROPS
, &zc
) != 0) {
81 if (errno
== ENOMEM
) {
82 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
83 zcmd_free_nvlists(&zc
);
87 zcmd_free_nvlists(&zc
);
92 if (zcmd_read_dst_nvlist(hdl
, &zc
, &zhp
->zpool_props
) != 0) {
93 zcmd_free_nvlists(&zc
);
97 zcmd_free_nvlists(&zc
);
103 zpool_props_refresh(zpool_handle_t
*zhp
)
107 old_props
= zhp
->zpool_props
;
109 if (zpool_get_all_props(zhp
) != 0)
112 nvlist_free(old_props
);
117 zpool_get_prop_string(zpool_handle_t
*zhp
, zpool_prop_t prop
,
123 zprop_source_t source
;
125 nvl
= zhp
->zpool_props
;
126 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
127 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &ival
) == 0);
129 verify(nvlist_lookup_string(nv
, ZPROP_VALUE
, &value
) == 0);
131 source
= ZPROP_SRC_DEFAULT
;
132 if ((value
= (char *)zpool_prop_default_string(prop
)) == NULL
)
143 zpool_get_prop_int(zpool_handle_t
*zhp
, zpool_prop_t prop
, zprop_source_t
*src
)
147 zprop_source_t source
;
149 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
)) {
151 * zpool_get_all_props() has most likely failed because
152 * the pool is faulted, but if all we need is the top level
153 * vdev's guid then get it from the zhp config nvlist.
155 if ((prop
== ZPOOL_PROP_GUID
) &&
156 (nvlist_lookup_nvlist(zhp
->zpool_config
,
157 ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0) &&
158 (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
)
162 return (zpool_prop_default_numeric(prop
));
165 nvl
= zhp
->zpool_props
;
166 if (nvlist_lookup_nvlist(nvl
, zpool_prop_to_name(prop
), &nv
) == 0) {
167 verify(nvlist_lookup_uint64(nv
, ZPROP_SOURCE
, &value
) == 0);
169 verify(nvlist_lookup_uint64(nv
, ZPROP_VALUE
, &value
) == 0);
171 source
= ZPROP_SRC_DEFAULT
;
172 value
= zpool_prop_default_numeric(prop
);
182 * Map VDEV STATE to printed strings.
185 zpool_state_to_name(vdev_state_t state
, vdev_aux_t aux
)
188 case VDEV_STATE_CLOSED
:
189 case VDEV_STATE_OFFLINE
:
190 return (gettext("OFFLINE"));
191 case VDEV_STATE_REMOVED
:
192 return (gettext("REMOVED"));
193 case VDEV_STATE_CANT_OPEN
:
194 if (aux
== VDEV_AUX_CORRUPT_DATA
|| aux
== VDEV_AUX_BAD_LOG
)
195 return (gettext("FAULTED"));
196 else if (aux
== VDEV_AUX_SPLIT_POOL
)
197 return (gettext("SPLIT"));
199 return (gettext("UNAVAIL"));
200 case VDEV_STATE_FAULTED
:
201 return (gettext("FAULTED"));
202 case VDEV_STATE_DEGRADED
:
203 return (gettext("DEGRADED"));
204 case VDEV_STATE_HEALTHY
:
205 return (gettext("ONLINE"));
211 return (gettext("UNKNOWN"));
215 * Map POOL STATE to printed strings.
218 zpool_pool_state_to_name(pool_state_t state
)
223 case POOL_STATE_ACTIVE
:
224 return (gettext("ACTIVE"));
225 case POOL_STATE_EXPORTED
:
226 return (gettext("EXPORTED"));
227 case POOL_STATE_DESTROYED
:
228 return (gettext("DESTROYED"));
229 case POOL_STATE_SPARE
:
230 return (gettext("SPARE"));
231 case POOL_STATE_L2CACHE
:
232 return (gettext("L2CACHE"));
233 case POOL_STATE_UNINITIALIZED
:
234 return (gettext("UNINITIALIZED"));
235 case POOL_STATE_UNAVAIL
:
236 return (gettext("UNAVAIL"));
237 case POOL_STATE_POTENTIALLY_ACTIVE
:
238 return (gettext("POTENTIALLY_ACTIVE"));
241 return (gettext("UNKNOWN"));
245 * Get a zpool property value for 'prop' and return the value in
246 * a pre-allocated buffer.
249 zpool_get_prop(zpool_handle_t
*zhp
, zpool_prop_t prop
, char *buf
,
250 size_t len
, zprop_source_t
*srctype
, boolean_t literal
)
254 zprop_source_t src
= ZPROP_SRC_NONE
;
259 if (zpool_get_state(zhp
) == POOL_STATE_UNAVAIL
) {
261 case ZPOOL_PROP_NAME
:
262 (void) strlcpy(buf
, zpool_get_name(zhp
), len
);
265 case ZPOOL_PROP_HEALTH
:
266 (void) strlcpy(buf
, "FAULTED", len
);
269 case ZPOOL_PROP_GUID
:
270 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
271 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
274 case ZPOOL_PROP_ALTROOT
:
275 case ZPOOL_PROP_CACHEFILE
:
276 case ZPOOL_PROP_COMMENT
:
277 if (zhp
->zpool_props
!= NULL
||
278 zpool_get_all_props(zhp
) == 0) {
280 zpool_get_prop_string(zhp
, prop
, &src
),
286 (void) strlcpy(buf
, "-", len
);
295 if (zhp
->zpool_props
== NULL
&& zpool_get_all_props(zhp
) &&
296 prop
!= ZPOOL_PROP_NAME
)
299 switch (zpool_prop_get_type(prop
)) {
300 case PROP_TYPE_STRING
:
301 (void) strlcpy(buf
, zpool_get_prop_string(zhp
, prop
, &src
),
305 case PROP_TYPE_NUMBER
:
306 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
309 case ZPOOL_PROP_SIZE
:
310 case ZPOOL_PROP_ALLOCATED
:
311 case ZPOOL_PROP_FREE
:
312 case ZPOOL_PROP_FREEING
:
313 case ZPOOL_PROP_LEAKED
:
314 case ZPOOL_PROP_ASHIFT
:
316 (void) snprintf(buf
, len
, "%llu",
317 (u_longlong_t
)intval
);
319 (void) zfs_nicenum(intval
, buf
, len
);
322 case ZPOOL_PROP_EXPANDSZ
:
324 (void) strlcpy(buf
, "-", len
);
325 } else if (literal
) {
326 (void) snprintf(buf
, len
, "%llu",
327 (u_longlong_t
)intval
);
329 (void) zfs_nicebytes(intval
, buf
, len
);
333 case ZPOOL_PROP_CAPACITY
:
335 (void) snprintf(buf
, len
, "%llu",
336 (u_longlong_t
)intval
);
338 (void) snprintf(buf
, len
, "%llu%%",
339 (u_longlong_t
)intval
);
343 case ZPOOL_PROP_FRAGMENTATION
:
344 if (intval
== UINT64_MAX
) {
345 (void) strlcpy(buf
, "-", len
);
346 } else if (literal
) {
347 (void) snprintf(buf
, len
, "%llu",
348 (u_longlong_t
)intval
);
350 (void) snprintf(buf
, len
, "%llu%%",
351 (u_longlong_t
)intval
);
355 case ZPOOL_PROP_DEDUPRATIO
:
357 (void) snprintf(buf
, len
, "%llu.%02llu",
358 (u_longlong_t
)(intval
/ 100),
359 (u_longlong_t
)(intval
% 100));
361 (void) snprintf(buf
, len
, "%llu.%02llux",
362 (u_longlong_t
)(intval
/ 100),
363 (u_longlong_t
)(intval
% 100));
366 case ZPOOL_PROP_HEALTH
:
367 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
368 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
369 verify(nvlist_lookup_uint64_array(nvroot
,
370 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
373 (void) strlcpy(buf
, zpool_state_to_name(intval
,
376 case ZPOOL_PROP_VERSION
:
377 if (intval
>= SPA_VERSION_FEATURES
) {
378 (void) snprintf(buf
, len
, "-");
383 (void) snprintf(buf
, len
, "%llu", (u_longlong_t
)intval
);
387 case PROP_TYPE_INDEX
:
388 intval
= zpool_get_prop_int(zhp
, prop
, &src
);
389 if (zpool_prop_index_to_string(prop
, intval
, &strval
)
392 (void) strlcpy(buf
, strval
, len
);
406 * Check if the bootfs name has the same pool name as it is set to.
407 * Assuming bootfs is a valid dataset name.
410 bootfs_name_valid(const char *pool
, char *bootfs
)
412 int len
= strlen(pool
);
413 if (bootfs
[0] == '\0')
416 if (!zfs_name_valid(bootfs
, ZFS_TYPE_FILESYSTEM
|ZFS_TYPE_SNAPSHOT
))
419 if (strncmp(pool
, bootfs
, len
) == 0 &&
420 (bootfs
[len
] == '/' || bootfs
[len
] == '\0'))
427 zpool_is_bootable(zpool_handle_t
*zhp
)
429 char bootfs
[ZFS_MAX_DATASET_NAME_LEN
];
431 return (zpool_get_prop(zhp
, ZPOOL_PROP_BOOTFS
, bootfs
,
432 sizeof (bootfs
), NULL
, B_FALSE
) == 0 && strncmp(bootfs
, "-",
433 sizeof (bootfs
)) != 0);
438 * Given an nvlist of zpool properties to be set, validate that they are
439 * correct, and parse any numeric properties (index, boolean, etc) if they are
440 * specified as strings.
443 zpool_valid_proplist(libzfs_handle_t
*hdl
, const char *poolname
,
444 nvlist_t
*props
, uint64_t version
, prop_flags_t flags
, char *errbuf
)
452 struct stat64 statbuf
;
455 if (nvlist_alloc(&retprops
, NV_UNIQUE_NAME
, 0) != 0) {
456 (void) no_memory(hdl
);
461 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
462 const char *propname
= nvpair_name(elem
);
464 prop
= zpool_name_to_prop(propname
);
465 if (prop
== ZPOOL_PROP_INVAL
&& zpool_prop_feature(propname
)) {
467 char *fname
= strchr(propname
, '@') + 1;
469 err
= zfeature_lookup_name(fname
, NULL
);
471 ASSERT3U(err
, ==, ENOENT
);
472 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
473 "invalid feature '%s'"), fname
);
474 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
478 if (nvpair_type(elem
) != DATA_TYPE_STRING
) {
479 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
480 "'%s' must be a string"), propname
);
481 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
485 (void) nvpair_value_string(elem
, &strval
);
486 if (strcmp(strval
, ZFS_FEATURE_ENABLED
) != 0 &&
487 strcmp(strval
, ZFS_FEATURE_DISABLED
) != 0) {
488 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
489 "property '%s' can only be set to "
490 "'enabled' or 'disabled'"), propname
);
491 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
496 strcmp(strval
, ZFS_FEATURE_DISABLED
) == 0) {
497 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
498 "property '%s' can only be set to "
499 "'disabled' at creation time"), propname
);
500 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
504 if (nvlist_add_uint64(retprops
, propname
, 0) != 0) {
505 (void) no_memory(hdl
);
512 * Make sure this property is valid and applies to this type.
514 if (prop
== ZPOOL_PROP_INVAL
) {
515 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
516 "invalid property '%s'"), propname
);
517 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
521 if (zpool_prop_readonly(prop
)) {
522 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
523 "is readonly"), propname
);
524 (void) zfs_error(hdl
, EZFS_PROPREADONLY
, errbuf
);
528 if (zprop_parse_value(hdl
, elem
, prop
, ZFS_TYPE_POOL
, retprops
,
529 &strval
, &intval
, errbuf
) != 0)
533 * Perform additional checking for specific properties.
536 case ZPOOL_PROP_VERSION
:
537 if (intval
< version
||
538 !SPA_VERSION_IS_SUPPORTED(intval
)) {
539 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
540 "property '%s' number %d is invalid."),
542 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
547 case ZPOOL_PROP_ASHIFT
:
549 (intval
< ASHIFT_MIN
|| intval
> ASHIFT_MAX
)) {
550 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
551 "invalid '%s=%d' property: only values "
552 "between %" PRId32
" and %" PRId32
" "
554 propname
, intval
, ASHIFT_MIN
, ASHIFT_MAX
);
555 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
560 case ZPOOL_PROP_BOOTFS
:
561 if (flags
.create
|| flags
.import
) {
562 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
563 "property '%s' cannot be set at creation "
564 "or import time"), propname
);
565 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
569 if (version
< SPA_VERSION_BOOTFS
) {
570 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
571 "pool must be upgraded to support "
572 "'%s' property"), propname
);
573 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
578 * bootfs property value has to be a dataset name and
579 * the dataset has to be in the same pool as it sets to.
581 if (!bootfs_name_valid(poolname
, strval
)) {
582 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "'%s' "
583 "is an invalid name"), strval
);
584 (void) zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
588 if ((zhp
= zpool_open_canfail(hdl
, poolname
)) == NULL
) {
589 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
590 "could not open pool '%s'"), poolname
);
591 (void) zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
);
597 case ZPOOL_PROP_ALTROOT
:
598 if (!flags
.create
&& !flags
.import
) {
599 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
600 "property '%s' can only be set during pool "
601 "creation or import"), propname
);
602 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
606 if (strval
[0] != '/') {
607 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
608 "bad alternate root '%s'"), strval
);
609 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
614 case ZPOOL_PROP_CACHEFILE
:
615 if (strval
[0] == '\0')
618 if (strcmp(strval
, "none") == 0)
621 if (strval
[0] != '/') {
622 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
623 "property '%s' must be empty, an "
624 "absolute path, or 'none'"), propname
);
625 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
629 slash
= strrchr(strval
, '/');
631 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
632 strcmp(slash
, "/..") == 0) {
633 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
634 "'%s' is not a valid file"), strval
);
635 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
641 if (strval
[0] != '\0' &&
642 (stat64(strval
, &statbuf
) != 0 ||
643 !S_ISDIR(statbuf
.st_mode
))) {
644 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
645 "'%s' is not a valid directory"),
647 (void) zfs_error(hdl
, EZFS_BADPATH
, errbuf
);
654 case ZPOOL_PROP_COMMENT
:
655 for (check
= strval
; *check
!= '\0'; check
++) {
656 if (!isprint(*check
)) {
658 dgettext(TEXT_DOMAIN
,
659 "comment may only have printable "
661 (void) zfs_error(hdl
, EZFS_BADPROP
,
666 if (strlen(strval
) > ZPROP_MAX_COMMENT
) {
667 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
668 "comment must not exceed %d characters"),
670 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
674 case ZPOOL_PROP_READONLY
:
676 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
677 "property '%s' can only be set at "
678 "import time"), propname
);
679 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
683 case ZPOOL_PROP_TNAME
:
685 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
686 "property '%s' can only be set at "
687 "creation time"), propname
);
688 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
692 case ZPOOL_PROP_MULTIHOST
:
693 if (get_system_hostid() == 0) {
694 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
695 "requires a non-zero system hostid"));
696 (void) zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
701 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
702 "property '%s'(%d) not defined"), propname
, prop
);
709 nvlist_free(retprops
);
714 * Set zpool property : propname=propval.
717 zpool_set_prop(zpool_handle_t
*zhp
, const char *propname
, const char *propval
)
719 zfs_cmd_t zc
= {"\0"};
722 nvlist_t
*nvl
= NULL
;
725 prop_flags_t flags
= { 0 };
727 (void) snprintf(errbuf
, sizeof (errbuf
),
728 dgettext(TEXT_DOMAIN
, "cannot set property for '%s'"),
731 if (nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, 0) != 0)
732 return (no_memory(zhp
->zpool_hdl
));
734 if (nvlist_add_string(nvl
, propname
, propval
) != 0) {
736 return (no_memory(zhp
->zpool_hdl
));
739 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
740 if ((realprops
= zpool_valid_proplist(zhp
->zpool_hdl
,
741 zhp
->zpool_name
, nvl
, version
, flags
, errbuf
)) == NULL
) {
750 * Execute the corresponding ioctl() to set this property.
752 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
754 if (zcmd_write_src_nvlist(zhp
->zpool_hdl
, &zc
, nvl
) != 0) {
759 ret
= zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_SET_PROPS
, &zc
);
761 zcmd_free_nvlists(&zc
);
765 (void) zpool_standard_error(zhp
->zpool_hdl
, errno
, errbuf
);
767 (void) zpool_props_refresh(zhp
);
773 zpool_expand_proplist(zpool_handle_t
*zhp
, zprop_list_t
**plp
)
775 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
777 char buf
[ZFS_MAXPROPLEN
];
778 nvlist_t
*features
= NULL
;
781 boolean_t firstexpand
= (NULL
== *plp
);
784 if (zprop_expand_list(hdl
, plp
, ZFS_TYPE_POOL
) != 0)
788 while (*last
!= NULL
)
789 last
= &(*last
)->pl_next
;
792 features
= zpool_get_features(zhp
);
794 if ((*plp
)->pl_all
&& firstexpand
) {
795 for (i
= 0; i
< SPA_FEATURES
; i
++) {
796 zprop_list_t
*entry
= zfs_alloc(hdl
,
797 sizeof (zprop_list_t
));
798 entry
->pl_prop
= ZPROP_INVAL
;
799 entry
->pl_user_prop
= zfs_asprintf(hdl
, "feature@%s",
800 spa_feature_table
[i
].fi_uname
);
801 entry
->pl_width
= strlen(entry
->pl_user_prop
);
802 entry
->pl_all
= B_TRUE
;
805 last
= &entry
->pl_next
;
809 /* add any unsupported features */
810 for (nvp
= nvlist_next_nvpair(features
, NULL
);
811 nvp
!= NULL
; nvp
= nvlist_next_nvpair(features
, nvp
)) {
816 if (zfeature_is_supported(nvpair_name(nvp
)))
819 propname
= zfs_asprintf(hdl
, "unsupported@%s",
823 * Before adding the property to the list make sure that no
824 * other pool already added the same property.
828 while (entry
!= NULL
) {
829 if (entry
->pl_user_prop
!= NULL
&&
830 strcmp(propname
, entry
->pl_user_prop
) == 0) {
834 entry
= entry
->pl_next
;
841 entry
= zfs_alloc(hdl
, sizeof (zprop_list_t
));
842 entry
->pl_prop
= ZPROP_INVAL
;
843 entry
->pl_user_prop
= propname
;
844 entry
->pl_width
= strlen(entry
->pl_user_prop
);
845 entry
->pl_all
= B_TRUE
;
848 last
= &entry
->pl_next
;
851 for (entry
= *plp
; entry
!= NULL
; entry
= entry
->pl_next
) {
856 if (entry
->pl_prop
!= ZPROP_INVAL
&&
857 zpool_get_prop(zhp
, entry
->pl_prop
, buf
, sizeof (buf
),
858 NULL
, B_FALSE
) == 0) {
859 if (strlen(buf
) > entry
->pl_width
)
860 entry
->pl_width
= strlen(buf
);
868 * Get the state for the given feature on the given ZFS pool.
871 zpool_prop_get_feature(zpool_handle_t
*zhp
, const char *propname
, char *buf
,
875 boolean_t found
= B_FALSE
;
876 nvlist_t
*features
= zpool_get_features(zhp
);
878 const char *feature
= strchr(propname
, '@') + 1;
880 supported
= zpool_prop_feature(propname
);
881 ASSERT(supported
|| zpool_prop_unsupported(propname
));
884 * Convert from feature name to feature guid. This conversion is
885 * unnecessary for unsupported@... properties because they already
892 ret
= zfeature_lookup_name(feature
, &fid
);
894 (void) strlcpy(buf
, "-", len
);
897 feature
= spa_feature_table
[fid
].fi_guid
;
900 if (nvlist_lookup_uint64(features
, feature
, &refcount
) == 0)
905 (void) strlcpy(buf
, ZFS_FEATURE_DISABLED
, len
);
908 (void) strlcpy(buf
, ZFS_FEATURE_ENABLED
, len
);
910 (void) strlcpy(buf
, ZFS_FEATURE_ACTIVE
, len
);
915 (void) strcpy(buf
, ZFS_UNSUPPORTED_INACTIVE
);
917 (void) strcpy(buf
, ZFS_UNSUPPORTED_READONLY
);
920 (void) strlcpy(buf
, "-", len
);
929 * Don't start the slice at the default block of 34; many storage
930 * devices will use a stripe width of 128k, other vendors prefer a 1m
931 * alignment. It is best to play it safe and ensure a 1m alignment
932 * given 512B blocks. When the block size is larger by a power of 2
933 * we will still be 1m aligned. Some devices are sensitive to the
934 * partition ending alignment as well.
936 #define NEW_START_BLOCK 2048
937 #define PARTITION_END_ALIGNMENT 2048
940 * Validate the given pool name, optionally putting an extended error message in
944 zpool_name_valid(libzfs_handle_t
*hdl
, boolean_t isopen
, const char *pool
)
950 ret
= pool_namecheck(pool
, &why
, &what
);
953 * The rules for reserved pool names were extended at a later point.
954 * But we need to support users with existing pools that may now be
955 * invalid. So we only check for this expanded set of names during a
956 * create (or import), and only in userland.
958 if (ret
== 0 && !isopen
&&
959 (strncmp(pool
, "mirror", 6) == 0 ||
960 strncmp(pool
, "raidz", 5) == 0 ||
961 strncmp(pool
, "spare", 5) == 0 ||
962 strcmp(pool
, "log") == 0)) {
965 dgettext(TEXT_DOMAIN
, "name is reserved"));
973 case NAME_ERR_TOOLONG
:
975 dgettext(TEXT_DOMAIN
, "name is too long"));
978 case NAME_ERR_INVALCHAR
:
980 dgettext(TEXT_DOMAIN
, "invalid character "
981 "'%c' in pool name"), what
);
984 case NAME_ERR_NOLETTER
:
985 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
986 "name must begin with a letter"));
989 case NAME_ERR_RESERVED
:
990 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
991 "name is reserved"));
994 case NAME_ERR_DISKLIKE
:
995 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
996 "pool name is reserved"));
999 case NAME_ERR_LEADING_SLASH
:
1000 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1001 "leading slash in name"));
1004 case NAME_ERR_EMPTY_COMPONENT
:
1005 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1006 "empty component in name"));
1009 case NAME_ERR_TRAILING_SLASH
:
1010 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1011 "trailing slash in name"));
1014 case NAME_ERR_MULTIPLE_DELIMITERS
:
1015 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1016 "multiple '@' and/or '#' delimiters in "
1020 case NAME_ERR_NO_AT
:
1021 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1022 "permission set is missing '@'"));
1026 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1027 "(%d) not defined"), why
);
1038 * Open a handle to the given pool, even if the pool is currently in the FAULTED
1042 zpool_open_canfail(libzfs_handle_t
*hdl
, const char *pool
)
1044 zpool_handle_t
*zhp
;
1048 * Make sure the pool name is valid.
1050 if (!zpool_name_valid(hdl
, B_TRUE
, pool
)) {
1051 (void) zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1052 dgettext(TEXT_DOMAIN
, "cannot open '%s'"),
1057 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
1060 zhp
->zpool_hdl
= hdl
;
1061 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1063 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1069 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "no such pool"));
1070 (void) zfs_error_fmt(hdl
, EZFS_NOENT
,
1071 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), pool
);
1080 * Like the above, but silent on error. Used when iterating over pools (because
1081 * the configuration cache may be out of date).
1084 zpool_open_silent(libzfs_handle_t
*hdl
, const char *pool
, zpool_handle_t
**ret
)
1086 zpool_handle_t
*zhp
;
1089 if ((zhp
= zfs_alloc(hdl
, sizeof (zpool_handle_t
))) == NULL
)
1092 zhp
->zpool_hdl
= hdl
;
1093 (void) strlcpy(zhp
->zpool_name
, pool
, sizeof (zhp
->zpool_name
));
1095 if (zpool_refresh_stats(zhp
, &missing
) != 0) {
1111 * Similar to zpool_open_canfail(), but refuses to open pools in the faulted
1115 zpool_open(libzfs_handle_t
*hdl
, const char *pool
)
1117 zpool_handle_t
*zhp
;
1119 if ((zhp
= zpool_open_canfail(hdl
, pool
)) == NULL
)
1122 if (zhp
->zpool_state
== POOL_STATE_UNAVAIL
) {
1123 (void) zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
1124 dgettext(TEXT_DOMAIN
, "cannot open '%s'"), zhp
->zpool_name
);
1133 * Close the handle. Simply frees the memory associated with the handle.
1136 zpool_close(zpool_handle_t
*zhp
)
1138 nvlist_free(zhp
->zpool_config
);
1139 nvlist_free(zhp
->zpool_old_config
);
1140 nvlist_free(zhp
->zpool_props
);
1145 * Return the name of the pool.
1148 zpool_get_name(zpool_handle_t
*zhp
)
1150 return (zhp
->zpool_name
);
1155 * Return the state of the pool (ACTIVE or UNAVAILABLE)
1158 zpool_get_state(zpool_handle_t
*zhp
)
1160 return (zhp
->zpool_state
);
1164 * Create the named pool, using the provided vdev list. It is assumed
1165 * that the consumer has already validated the contents of the nvlist, so we
1166 * don't have to worry about error semantics.
1169 zpool_create(libzfs_handle_t
*hdl
, const char *pool
, nvlist_t
*nvroot
,
1170 nvlist_t
*props
, nvlist_t
*fsprops
)
1172 zfs_cmd_t zc
= {"\0"};
1173 nvlist_t
*zc_fsprops
= NULL
;
1174 nvlist_t
*zc_props
= NULL
;
1175 nvlist_t
*hidden_args
= NULL
;
1176 uint8_t *wkeydata
= NULL
;
1181 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1182 "cannot create '%s'"), pool
);
1184 if (!zpool_name_valid(hdl
, B_FALSE
, pool
))
1185 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
1187 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1191 prop_flags_t flags
= { .create
= B_TRUE
, .import
= B_FALSE
};
1193 if ((zc_props
= zpool_valid_proplist(hdl
, pool
, props
,
1194 SPA_VERSION_1
, flags
, msg
)) == NULL
) {
1203 zoned
= ((nvlist_lookup_string(fsprops
,
1204 zfs_prop_to_name(ZFS_PROP_ZONED
), &zonestr
) == 0) &&
1205 strcmp(zonestr
, "on") == 0);
1207 if ((zc_fsprops
= zfs_valid_proplist(hdl
, ZFS_TYPE_FILESYSTEM
,
1208 fsprops
, zoned
, NULL
, NULL
, B_TRUE
, msg
)) == NULL
) {
1212 (nvlist_alloc(&zc_props
, NV_UNIQUE_NAME
, 0) != 0)) {
1215 if (zfs_crypto_create(hdl
, NULL
, zc_fsprops
, props
,
1216 &wkeydata
, &wkeylen
) != 0) {
1217 zfs_error(hdl
, EZFS_CRYPTOFAILED
, msg
);
1220 if (nvlist_add_nvlist(zc_props
,
1221 ZPOOL_ROOTFS_PROPS
, zc_fsprops
) != 0) {
1224 if (wkeydata
!= NULL
) {
1225 if (nvlist_alloc(&hidden_args
, NV_UNIQUE_NAME
, 0) != 0)
1228 if (nvlist_add_uint8_array(hidden_args
, "wkeydata",
1229 wkeydata
, wkeylen
) != 0)
1232 if (nvlist_add_nvlist(zc_props
, ZPOOL_HIDDEN_ARGS
,
1238 if (zc_props
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
1241 (void) strlcpy(zc
.zc_name
, pool
, sizeof (zc
.zc_name
));
1243 if ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_CREATE
, &zc
)) != 0) {
1245 zcmd_free_nvlists(&zc
);
1246 nvlist_free(zc_props
);
1247 nvlist_free(zc_fsprops
);
1248 nvlist_free(hidden_args
);
1249 if (wkeydata
!= NULL
)
1255 * This can happen if the user has specified the same
1256 * device multiple times. We can't reliably detect this
1257 * until we try to add it and see we already have a
1258 * label. This can also happen under if the device is
1259 * part of an active md or lvm device.
1261 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1262 "one or more vdevs refer to the same device, or "
1263 "one of\nthe devices is part of an active md or "
1265 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1269 * This happens if the record size is smaller or larger
1270 * than the allowed size range, or not a power of 2.
1272 * NOTE: although zfs_valid_proplist is called earlier,
1273 * this case may have slipped through since the
1274 * pool does not exist yet and it is therefore
1275 * impossible to read properties e.g. max blocksize
1278 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1279 "record size invalid"));
1280 return (zfs_error(hdl
, EZFS_BADPROP
, msg
));
1284 * This occurs when one of the devices is below
1285 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1286 * device was the problem device since there's no
1287 * reliable way to determine device size from userland.
1292 zfs_nicebytes(SPA_MINDEVSIZE
, buf
,
1295 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1296 "one or more devices is less than the "
1297 "minimum size (%s)"), buf
);
1299 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1302 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1303 "one or more devices is out of space"));
1304 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1307 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1308 "cache device must be a disk or disk slice"));
1309 return (zfs_error(hdl
, EZFS_BADDEV
, msg
));
1312 return (zpool_standard_error(hdl
, errno
, msg
));
1317 zcmd_free_nvlists(&zc
);
1318 nvlist_free(zc_props
);
1319 nvlist_free(zc_fsprops
);
1320 nvlist_free(hidden_args
);
1321 if (wkeydata
!= NULL
)
1327 * Destroy the given pool. It is up to the caller to ensure that there are no
1328 * datasets left in the pool.
1331 zpool_destroy(zpool_handle_t
*zhp
, const char *log_str
)
1333 zfs_cmd_t zc
= {"\0"};
1334 zfs_handle_t
*zfp
= NULL
;
1335 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1338 if (zhp
->zpool_state
== POOL_STATE_ACTIVE
&&
1339 (zfp
= zfs_open(hdl
, zhp
->zpool_name
, ZFS_TYPE_FILESYSTEM
)) == NULL
)
1342 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1343 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1345 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_DESTROY
, &zc
) != 0) {
1346 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1347 "cannot destroy '%s'"), zhp
->zpool_name
);
1349 if (errno
== EROFS
) {
1350 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1351 "one or more devices is read only"));
1352 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1354 (void) zpool_standard_error(hdl
, errno
, msg
);
1363 remove_mountpoint(zfp
);
1371 * Add the given vdevs to the pool. The caller must have already performed the
1372 * necessary verification to ensure that the vdev specification is well-formed.
1375 zpool_add(zpool_handle_t
*zhp
, nvlist_t
*nvroot
)
1377 zfs_cmd_t zc
= {"\0"};
1379 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
1381 nvlist_t
**spares
, **l2cache
;
1382 uint_t nspares
, nl2cache
;
1384 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1385 "cannot add to '%s'"), zhp
->zpool_name
);
1387 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1388 SPA_VERSION_SPARES
&&
1389 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
1390 &spares
, &nspares
) == 0) {
1391 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1392 "upgraded to add hot spares"));
1393 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1396 if (zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
) <
1397 SPA_VERSION_L2CACHE
&&
1398 nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
1399 &l2cache
, &nl2cache
) == 0) {
1400 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "pool must be "
1401 "upgraded to add cache devices"));
1402 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
1405 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
1407 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1409 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_ADD
, &zc
) != 0) {
1413 * This can happen if the user has specified the same
1414 * device multiple times. We can't reliably detect this
1415 * until we try to add it and see we already have a
1418 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1419 "one or more vdevs refer to the same device"));
1420 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1424 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1425 "invalid config; a pool with removing/removed "
1426 "vdevs does not support adding raidz vdevs"));
1427 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1432 * This occurrs when one of the devices is below
1433 * SPA_MINDEVSIZE. Unfortunately, we can't detect which
1434 * device was the problem device since there's no
1435 * reliable way to determine device size from userland.
1440 zfs_nicebytes(SPA_MINDEVSIZE
, buf
,
1443 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1444 "device is less than the minimum "
1447 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1451 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1452 "pool must be upgraded to add these vdevs"));
1453 (void) zfs_error(hdl
, EZFS_BADVERSION
, msg
);
1457 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1458 "cache device must be a disk or disk slice"));
1459 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
1463 (void) zpool_standard_error(hdl
, errno
, msg
);
1471 zcmd_free_nvlists(&zc
);
1477 * Exports the pool from the system. The caller must ensure that there are no
1478 * mounted datasets in the pool.
1481 zpool_export_common(zpool_handle_t
*zhp
, boolean_t force
, boolean_t hardforce
,
1482 const char *log_str
)
1484 zfs_cmd_t zc
= {"\0"};
1487 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
1488 "cannot export '%s'"), zhp
->zpool_name
);
1490 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
1491 zc
.zc_cookie
= force
;
1492 zc
.zc_guid
= hardforce
;
1493 zc
.zc_history
= (uint64_t)(uintptr_t)log_str
;
1495 if (zfs_ioctl(zhp
->zpool_hdl
, ZFS_IOC_POOL_EXPORT
, &zc
) != 0) {
1498 zfs_error_aux(zhp
->zpool_hdl
, dgettext(TEXT_DOMAIN
,
1499 "use '-f' to override the following errors:\n"
1500 "'%s' has an active shared spare which could be"
1501 " used by other pools once '%s' is exported."),
1502 zhp
->zpool_name
, zhp
->zpool_name
);
1503 return (zfs_error(zhp
->zpool_hdl
, EZFS_ACTIVE_SPARE
,
1506 return (zpool_standard_error_fmt(zhp
->zpool_hdl
, errno
,
1515 zpool_export(zpool_handle_t
*zhp
, boolean_t force
, const char *log_str
)
1517 return (zpool_export_common(zhp
, force
, B_FALSE
, log_str
));
1521 zpool_export_force(zpool_handle_t
*zhp
, const char *log_str
)
1523 return (zpool_export_common(zhp
, B_TRUE
, B_TRUE
, log_str
));
1527 zpool_rewind_exclaim(libzfs_handle_t
*hdl
, const char *name
, boolean_t dryrun
,
1530 nvlist_t
*nv
= NULL
;
1536 if (!hdl
->libzfs_printerr
|| config
== NULL
)
1539 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1540 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0) {
1544 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1546 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1548 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1549 strftime(timestr
, 128, "%c", &t
) != 0) {
1551 (void) printf(dgettext(TEXT_DOMAIN
,
1552 "Would be able to return %s "
1553 "to its state as of %s.\n"),
1556 (void) printf(dgettext(TEXT_DOMAIN
,
1557 "Pool %s returned to its state as of %s.\n"),
1561 (void) printf(dgettext(TEXT_DOMAIN
,
1562 "%s approximately %lld "),
1563 dryrun
? "Would discard" : "Discarded",
1564 ((longlong_t
)loss
+ 30) / 60);
1565 (void) printf(dgettext(TEXT_DOMAIN
,
1566 "minutes of transactions.\n"));
1567 } else if (loss
> 0) {
1568 (void) printf(dgettext(TEXT_DOMAIN
,
1569 "%s approximately %lld "),
1570 dryrun
? "Would discard" : "Discarded",
1572 (void) printf(dgettext(TEXT_DOMAIN
,
1573 "seconds of transactions.\n"));
1579 zpool_explain_recover(libzfs_handle_t
*hdl
, const char *name
, int reason
,
1582 nvlist_t
*nv
= NULL
;
1584 uint64_t edata
= UINT64_MAX
;
1589 if (!hdl
->libzfs_printerr
)
1593 (void) printf(dgettext(TEXT_DOMAIN
, "action: "));
1595 (void) printf(dgettext(TEXT_DOMAIN
, "\t"));
1597 /* All attempted rewinds failed if ZPOOL_CONFIG_LOAD_TIME missing */
1598 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nv
) != 0 ||
1599 nvlist_lookup_nvlist(nv
, ZPOOL_CONFIG_REWIND_INFO
, &nv
) != 0 ||
1600 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_TIME
, &rewindto
) != 0)
1603 (void) nvlist_lookup_int64(nv
, ZPOOL_CONFIG_REWIND_TIME
, &loss
);
1604 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_LOAD_DATA_ERRORS
,
1607 (void) printf(dgettext(TEXT_DOMAIN
,
1608 "Recovery is possible, but will result in some data loss.\n"));
1610 if (localtime_r((time_t *)&rewindto
, &t
) != NULL
&&
1611 strftime(timestr
, 128, "%c", &t
) != 0) {
1612 (void) printf(dgettext(TEXT_DOMAIN
,
1613 "\tReturning the pool to its state as of %s\n"
1614 "\tshould correct the problem. "),
1617 (void) printf(dgettext(TEXT_DOMAIN
,
1618 "\tReverting the pool to an earlier state "
1619 "should correct the problem.\n\t"));
1623 (void) printf(dgettext(TEXT_DOMAIN
,
1624 "Approximately %lld minutes of data\n"
1625 "\tmust be discarded, irreversibly. "),
1626 ((longlong_t
)loss
+ 30) / 60);
1627 } else if (loss
> 0) {
1628 (void) printf(dgettext(TEXT_DOMAIN
,
1629 "Approximately %lld seconds of data\n"
1630 "\tmust be discarded, irreversibly. "),
1633 if (edata
!= 0 && edata
!= UINT64_MAX
) {
1635 (void) printf(dgettext(TEXT_DOMAIN
,
1636 "After rewind, at least\n"
1637 "\tone persistent user-data error will remain. "));
1639 (void) printf(dgettext(TEXT_DOMAIN
,
1640 "After rewind, several\n"
1641 "\tpersistent user-data errors will remain. "));
1644 (void) printf(dgettext(TEXT_DOMAIN
,
1645 "Recovery can be attempted\n\tby executing 'zpool %s -F %s'. "),
1646 reason
>= 0 ? "clear" : "import", name
);
1648 (void) printf(dgettext(TEXT_DOMAIN
,
1649 "A scrub of the pool\n"
1650 "\tis strongly recommended after recovery.\n"));
1654 (void) printf(dgettext(TEXT_DOMAIN
,
1655 "Destroy and re-create the pool from\n\ta backup source.\n"));
1659 * zpool_import() is a contracted interface. Should be kept the same
1662 * Applications should use zpool_import_props() to import a pool with
1663 * new properties value to be set.
1666 zpool_import(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1669 nvlist_t
*props
= NULL
;
1672 if (altroot
!= NULL
) {
1673 if (nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0) != 0) {
1674 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1675 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1679 if (nvlist_add_string(props
,
1680 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), altroot
) != 0 ||
1681 nvlist_add_string(props
,
1682 zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
), "none") != 0) {
1684 return (zfs_error_fmt(hdl
, EZFS_NOMEM
,
1685 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1690 ret
= zpool_import_props(hdl
, config
, newname
, props
,
1697 print_vdev_tree(libzfs_handle_t
*hdl
, const char *name
, nvlist_t
*nv
,
1703 uint64_t is_log
= 0;
1705 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
,
1709 (void) printf("\t%*s%s%s\n", indent
, "", name
,
1710 is_log
? " [log]" : "");
1712 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1713 &child
, &children
) != 0)
1716 for (c
= 0; c
< children
; c
++) {
1717 vname
= zpool_vdev_name(hdl
, NULL
, child
[c
], VDEV_NAME_TYPE_ID
);
1718 print_vdev_tree(hdl
, vname
, child
[c
], indent
+ 2);
1724 zpool_print_unsup_feat(nvlist_t
*config
)
1726 nvlist_t
*nvinfo
, *unsup_feat
;
1729 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) ==
1731 verify(nvlist_lookup_nvlist(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
,
1734 for (nvp
= nvlist_next_nvpair(unsup_feat
, NULL
); nvp
!= NULL
;
1735 nvp
= nvlist_next_nvpair(unsup_feat
, nvp
)) {
1738 verify(nvpair_type(nvp
) == DATA_TYPE_STRING
);
1739 verify(nvpair_value_string(nvp
, &desc
) == 0);
1741 if (strlen(desc
) > 0)
1742 (void) printf("\t%s (%s)\n", nvpair_name(nvp
), desc
);
1744 (void) printf("\t%s\n", nvpair_name(nvp
));
1749 * Import the given pool using the known configuration and a list of
1750 * properties to be set. The configuration should have come from
1751 * zpool_find_import(). The 'newname' parameters control whether the pool
1752 * is imported with a different name.
1755 zpool_import_props(libzfs_handle_t
*hdl
, nvlist_t
*config
, const char *newname
,
1756 nvlist_t
*props
, int flags
)
1758 zfs_cmd_t zc
= {"\0"};
1759 zpool_rewind_policy_t policy
;
1760 nvlist_t
*nv
= NULL
;
1761 nvlist_t
*nvinfo
= NULL
;
1762 nvlist_t
*missing
= NULL
;
1769 verify(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
1772 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1773 "cannot import pool '%s'"), origname
);
1775 if (newname
!= NULL
) {
1776 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
1777 return (zfs_error_fmt(hdl
, EZFS_INVALIDNAME
,
1778 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1780 thename
= (char *)newname
;
1785 if (props
!= NULL
) {
1787 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
1789 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
1792 if ((props
= zpool_valid_proplist(hdl
, origname
,
1793 props
, version
, flags
, errbuf
)) == NULL
)
1795 if (zcmd_write_src_nvlist(hdl
, &zc
, props
) != 0) {
1802 (void) strlcpy(zc
.zc_name
, thename
, sizeof (zc
.zc_name
));
1804 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
1807 if (zcmd_write_conf_nvlist(hdl
, &zc
, config
) != 0) {
1808 zcmd_free_nvlists(&zc
);
1811 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zc
.zc_nvlist_conf_size
* 2) != 0) {
1812 zcmd_free_nvlists(&zc
);
1816 zc
.zc_cookie
= flags
;
1817 while ((ret
= zfs_ioctl(hdl
, ZFS_IOC_POOL_IMPORT
, &zc
)) != 0 &&
1819 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
1820 zcmd_free_nvlists(&zc
);
1827 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nv
);
1829 zcmd_free_nvlists(&zc
);
1831 zpool_get_rewind_policy(config
, &policy
);
1838 * Dry-run failed, but we print out what success
1839 * looks like if we found a best txg
1841 if (policy
.zrp_request
& ZPOOL_TRY_REWIND
) {
1842 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1848 if (newname
== NULL
)
1849 (void) snprintf(desc
, sizeof (desc
),
1850 dgettext(TEXT_DOMAIN
, "cannot import '%s'"),
1853 (void) snprintf(desc
, sizeof (desc
),
1854 dgettext(TEXT_DOMAIN
, "cannot import '%s' as '%s'"),
1859 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
1860 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1861 nvlist_exists(nvinfo
, ZPOOL_CONFIG_UNSUP_FEAT
)) {
1862 (void) printf(dgettext(TEXT_DOMAIN
, "This "
1863 "pool uses the following feature(s) not "
1864 "supported by this system:\n"));
1865 zpool_print_unsup_feat(nv
);
1866 if (nvlist_exists(nvinfo
,
1867 ZPOOL_CONFIG_CAN_RDONLY
)) {
1868 (void) printf(dgettext(TEXT_DOMAIN
,
1869 "All unsupported features are only "
1870 "required for writing to the pool."
1871 "\nThe pool can be imported using "
1872 "'-o readonly=on'.\n"));
1876 * Unsupported version.
1878 (void) zfs_error(hdl
, EZFS_BADVERSION
, desc
);
1882 if (nv
!= NULL
&& nvlist_lookup_nvlist(nv
,
1883 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0) {
1884 char *hostname
= "<unknown>";
1885 uint64_t hostid
= 0;
1886 mmp_state_t mmp_state
;
1888 mmp_state
= fnvlist_lookup_uint64(nvinfo
,
1889 ZPOOL_CONFIG_MMP_STATE
);
1891 if (nvlist_exists(nvinfo
,
1892 ZPOOL_CONFIG_MMP_HOSTNAME
))
1893 hostname
= fnvlist_lookup_string(nvinfo
,
1894 ZPOOL_CONFIG_MMP_HOSTNAME
);
1896 if (nvlist_exists(nvinfo
,
1897 ZPOOL_CONFIG_MMP_HOSTID
))
1898 hostid
= fnvlist_lookup_uint64(nvinfo
,
1899 ZPOOL_CONFIG_MMP_HOSTID
);
1901 if (mmp_state
== MMP_STATE_ACTIVE
) {
1902 (void) snprintf(aux
, sizeof (aux
),
1903 dgettext(TEXT_DOMAIN
, "pool is imp"
1904 "orted on host '%s' (hostid=%lx).\n"
1905 "Export the pool on the other "
1906 "system, then run 'zpool import'."),
1907 hostname
, (unsigned long) hostid
);
1908 } else if (mmp_state
== MMP_STATE_NO_HOSTID
) {
1909 (void) snprintf(aux
, sizeof (aux
),
1910 dgettext(TEXT_DOMAIN
, "pool has "
1911 "the multihost property on and "
1912 "the\nsystem's hostid is not set. "
1913 "Set a unique system hostid with "
1914 "the zgenhostid(8) command.\n"));
1917 (void) zfs_error_aux(hdl
, aux
);
1919 (void) zfs_error(hdl
, EZFS_ACTIVE_POOL
, desc
);
1923 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, desc
);
1927 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1928 "one or more devices is read only"));
1929 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
1933 if (nv
&& nvlist_lookup_nvlist(nv
,
1934 ZPOOL_CONFIG_LOAD_INFO
, &nvinfo
) == 0 &&
1935 nvlist_lookup_nvlist(nvinfo
,
1936 ZPOOL_CONFIG_MISSING_DEVICES
, &missing
) == 0) {
1937 (void) printf(dgettext(TEXT_DOMAIN
,
1938 "The devices below are missing, use "
1939 "'-m' to import the pool anyway:\n"));
1940 print_vdev_tree(hdl
, NULL
, missing
, 2);
1941 (void) printf("\n");
1943 (void) zpool_standard_error(hdl
, error
, desc
);
1947 (void) zpool_standard_error(hdl
, error
, desc
);
1951 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1952 "one or more devices are already in use\n"));
1953 (void) zfs_error(hdl
, EZFS_BADDEV
, desc
);
1956 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1957 "new name of at least one dataset is longer than "
1958 "the maximum allowable length"));
1959 (void) zfs_error(hdl
, EZFS_NAMETOOLONG
, desc
);
1962 (void) zpool_standard_error(hdl
, error
, desc
);
1963 zpool_explain_recover(hdl
,
1964 newname
? origname
: thename
, -error
, nv
);
1971 zpool_handle_t
*zhp
;
1974 * This should never fail, but play it safe anyway.
1976 if (zpool_open_silent(hdl
, thename
, &zhp
) != 0)
1978 else if (zhp
!= NULL
)
1980 if (policy
.zrp_request
&
1981 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
1982 zpool_rewind_exclaim(hdl
, newname
? origname
: thename
,
1983 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0), nv
);
1996 zpool_scan(zpool_handle_t
*zhp
, pool_scan_func_t func
, pool_scrub_cmd_t cmd
)
1998 zfs_cmd_t zc
= {"\0"};
2001 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2003 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2004 zc
.zc_cookie
= func
;
2007 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_SCAN
, &zc
) == 0)
2012 /* ECANCELED on a scrub means we resumed a paused scrub */
2013 if (err
== ECANCELED
&& func
== POOL_SCAN_SCRUB
&&
2014 cmd
== POOL_SCRUB_NORMAL
)
2017 if (err
== ENOENT
&& func
!= POOL_SCAN_NONE
&& cmd
== POOL_SCRUB_NORMAL
)
2020 if (func
== POOL_SCAN_SCRUB
) {
2021 if (cmd
== POOL_SCRUB_PAUSE
) {
2022 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2023 "cannot pause scrubbing %s"), zc
.zc_name
);
2025 assert(cmd
== POOL_SCRUB_NORMAL
);
2026 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2027 "cannot scrub %s"), zc
.zc_name
);
2029 } else if (func
== POOL_SCAN_NONE
) {
2030 (void) snprintf(msg
, sizeof (msg
),
2031 dgettext(TEXT_DOMAIN
, "cannot cancel scrubbing %s"),
2034 assert(!"unexpected result");
2039 pool_scan_stat_t
*ps
= NULL
;
2042 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
2043 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
2044 (void) nvlist_lookup_uint64_array(nvroot
,
2045 ZPOOL_CONFIG_SCAN_STATS
, (uint64_t **)&ps
, &psc
);
2046 if (ps
&& ps
->pss_func
== POOL_SCAN_SCRUB
) {
2047 if (cmd
== POOL_SCRUB_PAUSE
)
2048 return (zfs_error(hdl
, EZFS_SCRUB_PAUSED
, msg
));
2050 return (zfs_error(hdl
, EZFS_SCRUBBING
, msg
));
2052 return (zfs_error(hdl
, EZFS_RESILVERING
, msg
));
2054 } else if (err
== ENOENT
) {
2055 return (zfs_error(hdl
, EZFS_NO_SCRUB
, msg
));
2057 return (zpool_standard_error(hdl
, err
, msg
));
2062 * Find a vdev that matches the search criteria specified. We use the
2063 * the nvpair name to determine how we should look for the device.
2064 * 'avail_spare' is set to TRUE if the provided guid refers to an AVAIL
2065 * spare; but FALSE if its an INUSE spare.
2068 vdev_to_nvlist_iter(nvlist_t
*nv
, nvlist_t
*search
, boolean_t
*avail_spare
,
2069 boolean_t
*l2cache
, boolean_t
*log
)
2076 nvpair_t
*pair
= nvlist_next_nvpair(search
, NULL
);
2078 /* Nothing to look for */
2079 if (search
== NULL
|| pair
== NULL
)
2082 /* Obtain the key we will use to search */
2083 srchkey
= nvpair_name(pair
);
2085 switch (nvpair_type(pair
)) {
2086 case DATA_TYPE_UINT64
:
2087 if (strcmp(srchkey
, ZPOOL_CONFIG_GUID
) == 0) {
2088 uint64_t srchval
, theguid
;
2090 verify(nvpair_value_uint64(pair
, &srchval
) == 0);
2091 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
2093 if (theguid
== srchval
)
2098 case DATA_TYPE_STRING
: {
2099 char *srchval
, *val
;
2101 verify(nvpair_value_string(pair
, &srchval
) == 0);
2102 if (nvlist_lookup_string(nv
, srchkey
, &val
) != 0)
2106 * Search for the requested value. Special cases:
2108 * - ZPOOL_CONFIG_PATH for whole disk entries. These end in
2109 * "-part1", or "p1". The suffix is hidden from the user,
2110 * but included in the string, so this matches around it.
2111 * - ZPOOL_CONFIG_PATH for short names zfs_strcmp_shortname()
2112 * is used to check all possible expanded paths.
2113 * - looking for a top-level vdev name (i.e. ZPOOL_CONFIG_TYPE).
2115 * Otherwise, all other searches are simple string compares.
2117 if (strcmp(srchkey
, ZPOOL_CONFIG_PATH
) == 0) {
2118 uint64_t wholedisk
= 0;
2120 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
,
2122 if (zfs_strcmp_pathname(srchval
, val
, wholedisk
) == 0)
2125 } else if (strcmp(srchkey
, ZPOOL_CONFIG_TYPE
) == 0 && val
) {
2126 char *type
, *idx
, *end
, *p
;
2127 uint64_t id
, vdev_id
;
2130 * Determine our vdev type, keeping in mind
2131 * that the srchval is composed of a type and
2132 * vdev id pair (i.e. mirror-4).
2134 if ((type
= strdup(srchval
)) == NULL
)
2137 if ((p
= strrchr(type
, '-')) == NULL
) {
2145 * If the types don't match then keep looking.
2147 if (strncmp(val
, type
, strlen(val
)) != 0) {
2152 verify(zpool_vdev_is_interior(type
));
2153 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
2157 vdev_id
= strtoull(idx
, &end
, 10);
2164 * Now verify that we have the correct vdev id.
2173 if (strcmp(srchval
, val
) == 0)
2182 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
2183 &child
, &children
) != 0)
2186 for (c
= 0; c
< children
; c
++) {
2187 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2188 avail_spare
, l2cache
, NULL
)) != NULL
) {
2190 * The 'is_log' value is only set for the toplevel
2191 * vdev, not the leaf vdevs. So we always lookup the
2192 * log device from the root of the vdev tree (where
2193 * 'log' is non-NULL).
2196 nvlist_lookup_uint64(child
[c
],
2197 ZPOOL_CONFIG_IS_LOG
, &is_log
) == 0 &&
2205 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_SPARES
,
2206 &child
, &children
) == 0) {
2207 for (c
= 0; c
< children
; c
++) {
2208 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2209 avail_spare
, l2cache
, NULL
)) != NULL
) {
2210 *avail_spare
= B_TRUE
;
2216 if (nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_L2CACHE
,
2217 &child
, &children
) == 0) {
2218 for (c
= 0; c
< children
; c
++) {
2219 if ((ret
= vdev_to_nvlist_iter(child
[c
], search
,
2220 avail_spare
, l2cache
, NULL
)) != NULL
) {
2231 * Given a physical path (minus the "/devices" prefix), find the
2235 zpool_find_vdev_by_physpath(zpool_handle_t
*zhp
, const char *ppath
,
2236 boolean_t
*avail_spare
, boolean_t
*l2cache
, boolean_t
*log
)
2238 nvlist_t
*search
, *nvroot
, *ret
;
2240 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2241 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PHYS_PATH
, ppath
) == 0);
2243 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2246 *avail_spare
= B_FALSE
;
2250 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2251 nvlist_free(search
);
2257 * Determine if we have an "interior" top-level vdev (i.e mirror/raidz).
2260 zpool_vdev_is_interior(const char *name
)
2262 if (strncmp(name
, VDEV_TYPE_RAIDZ
, strlen(VDEV_TYPE_RAIDZ
)) == 0 ||
2263 strncmp(name
, VDEV_TYPE_SPARE
, strlen(VDEV_TYPE_SPARE
)) == 0 ||
2265 VDEV_TYPE_REPLACING
, strlen(VDEV_TYPE_REPLACING
)) == 0 ||
2266 strncmp(name
, VDEV_TYPE_MIRROR
, strlen(VDEV_TYPE_MIRROR
)) == 0)
2272 zpool_find_vdev(zpool_handle_t
*zhp
, const char *path
, boolean_t
*avail_spare
,
2273 boolean_t
*l2cache
, boolean_t
*log
)
2276 nvlist_t
*nvroot
, *search
, *ret
;
2279 verify(nvlist_alloc(&search
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
2281 guid
= strtoull(path
, &end
, 0);
2282 if (guid
!= 0 && *end
== '\0') {
2283 verify(nvlist_add_uint64(search
, ZPOOL_CONFIG_GUID
, guid
) == 0);
2284 } else if (zpool_vdev_is_interior(path
)) {
2285 verify(nvlist_add_string(search
, ZPOOL_CONFIG_TYPE
, path
) == 0);
2287 verify(nvlist_add_string(search
, ZPOOL_CONFIG_PATH
, path
) == 0);
2290 verify(nvlist_lookup_nvlist(zhp
->zpool_config
, ZPOOL_CONFIG_VDEV_TREE
,
2293 *avail_spare
= B_FALSE
;
2297 ret
= vdev_to_nvlist_iter(nvroot
, search
, avail_spare
, l2cache
, log
);
2298 nvlist_free(search
);
2304 vdev_is_online(nvlist_t
*nv
)
2308 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_OFFLINE
, &ival
) == 0 ||
2309 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_FAULTED
, &ival
) == 0 ||
2310 nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_REMOVED
, &ival
) == 0)
2317 * Helper function for zpool_get_physpaths().
2320 vdev_get_one_physpath(nvlist_t
*config
, char *physpath
, size_t physpath_size
,
2321 size_t *bytes_written
)
2323 size_t bytes_left
, pos
, rsz
;
2327 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PHYS_PATH
,
2329 return (EZFS_NODEVICE
);
2331 pos
= *bytes_written
;
2332 bytes_left
= physpath_size
- pos
;
2333 format
= (pos
== 0) ? "%s" : " %s";
2335 rsz
= snprintf(physpath
+ pos
, bytes_left
, format
, tmppath
);
2336 *bytes_written
+= rsz
;
2338 if (rsz
>= bytes_left
) {
2339 /* if physpath was not copied properly, clear it */
2340 if (bytes_left
!= 0) {
2343 return (EZFS_NOSPC
);
2349 vdev_get_physpaths(nvlist_t
*nv
, char *physpath
, size_t phypath_size
,
2350 size_t *rsz
, boolean_t is_spare
)
2355 if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) != 0)
2356 return (EZFS_INVALCONFIG
);
2358 if (strcmp(type
, VDEV_TYPE_DISK
) == 0) {
2360 * An active spare device has ZPOOL_CONFIG_IS_SPARE set.
2361 * For a spare vdev, we only want to boot from the active
2366 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_SPARE
,
2369 return (EZFS_INVALCONFIG
);
2372 if (vdev_is_online(nv
)) {
2373 if ((ret
= vdev_get_one_physpath(nv
, physpath
,
2374 phypath_size
, rsz
)) != 0)
2377 } else if (strcmp(type
, VDEV_TYPE_MIRROR
) == 0 ||
2378 strcmp(type
, VDEV_TYPE_RAIDZ
) == 0 ||
2379 strcmp(type
, VDEV_TYPE_REPLACING
) == 0 ||
2380 (is_spare
= (strcmp(type
, VDEV_TYPE_SPARE
) == 0))) {
2385 if (nvlist_lookup_nvlist_array(nv
,
2386 ZPOOL_CONFIG_CHILDREN
, &child
, &count
) != 0)
2387 return (EZFS_INVALCONFIG
);
2389 for (i
= 0; i
< count
; i
++) {
2390 ret
= vdev_get_physpaths(child
[i
], physpath
,
2391 phypath_size
, rsz
, is_spare
);
2392 if (ret
== EZFS_NOSPC
)
2397 return (EZFS_POOL_INVALARG
);
2401 * Get phys_path for a root pool config.
2402 * Return 0 on success; non-zero on failure.
2405 zpool_get_config_physpath(nvlist_t
*config
, char *physpath
, size_t phypath_size
)
2408 nvlist_t
*vdev_root
;
2415 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
2417 return (EZFS_INVALCONFIG
);
2419 if (nvlist_lookup_string(vdev_root
, ZPOOL_CONFIG_TYPE
, &type
) != 0 ||
2420 nvlist_lookup_nvlist_array(vdev_root
, ZPOOL_CONFIG_CHILDREN
,
2421 &child
, &count
) != 0)
2422 return (EZFS_INVALCONFIG
);
2425 * root pool can only have a single top-level vdev.
2427 if (strcmp(type
, VDEV_TYPE_ROOT
) != 0 || count
!= 1)
2428 return (EZFS_POOL_INVALARG
);
2430 (void) vdev_get_physpaths(child
[0], physpath
, phypath_size
, &rsz
,
2433 /* No online devices */
2435 return (EZFS_NODEVICE
);
2441 * Get phys_path for a root pool
2442 * Return 0 on success; non-zero on failure.
2445 zpool_get_physpath(zpool_handle_t
*zhp
, char *physpath
, size_t phypath_size
)
2447 return (zpool_get_config_physpath(zhp
->zpool_config
, physpath
,
2452 * If the device has being dynamically expanded then we need to relabel
2453 * the disk to use the new unallocated space.
2456 zpool_relabel_disk(libzfs_handle_t
*hdl
, const char *path
, const char *msg
)
2460 if ((fd
= open(path
, O_RDWR
|O_DIRECT
)) < 0) {
2461 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2462 "relabel '%s': unable to open device: %d"), path
, errno
);
2463 return (zfs_error(hdl
, EZFS_OPENFAILED
, msg
));
2467 * It's possible that we might encounter an error if the device
2468 * does not have any unallocated space left. If so, we simply
2469 * ignore that error and continue on.
2471 * Also, we don't call efi_rescan() - that would just return EBUSY.
2472 * The module will do it for us in vdev_disk_open().
2474 error
= efi_use_whole_disk(fd
);
2476 /* Flush the buffers to disk and invalidate the page cache. */
2478 (void) ioctl(fd
, BLKFLSBUF
);
2481 if (error
&& error
!= VT_ENOSPC
) {
2482 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
2483 "relabel '%s': unable to read disk capacity"), path
);
2484 return (zfs_error(hdl
, EZFS_NOCAP
, msg
));
2491 * Convert a vdev path to a GUID. Returns GUID or 0 on error.
2493 * If is_spare, is_l2cache, or is_log is non-NULL, then store within it
2494 * if the VDEV is a spare, l2cache, or log device. If they're NULL then
2498 zpool_vdev_path_to_guid_impl(zpool_handle_t
*zhp
, const char *path
,
2499 boolean_t
*is_spare
, boolean_t
*is_l2cache
, boolean_t
*is_log
)
2502 boolean_t spare
= B_FALSE
, l2cache
= B_FALSE
, log
= B_FALSE
;
2505 if ((tgt
= zpool_find_vdev(zhp
, path
, &spare
, &l2cache
,
2509 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &guid
) == 0);
2510 if (is_spare
!= NULL
)
2512 if (is_l2cache
!= NULL
)
2513 *is_l2cache
= l2cache
;
2520 /* Convert a vdev path to a GUID. Returns GUID or 0 on error. */
2522 zpool_vdev_path_to_guid(zpool_handle_t
*zhp
, const char *path
)
2524 return (zpool_vdev_path_to_guid_impl(zhp
, path
, NULL
, NULL
, NULL
));
2528 * Bring the specified vdev online. The 'flags' parameter is a set of the
2529 * ZFS_ONLINE_* flags.
2532 zpool_vdev_online(zpool_handle_t
*zhp
, const char *path
, int flags
,
2533 vdev_state_t
*newstate
)
2535 zfs_cmd_t zc
= {"\0"};
2539 boolean_t avail_spare
, l2cache
, islog
;
2540 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2543 if (flags
& ZFS_ONLINE_EXPAND
) {
2544 (void) snprintf(msg
, sizeof (msg
),
2545 dgettext(TEXT_DOMAIN
, "cannot expand %s"), path
);
2547 (void) snprintf(msg
, sizeof (msg
),
2548 dgettext(TEXT_DOMAIN
, "cannot online %s"), path
);
2551 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2552 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2554 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2556 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2559 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2561 if ((flags
& ZFS_ONLINE_EXPAND
||
2562 zpool_get_prop_int(zhp
, ZPOOL_PROP_AUTOEXPAND
, NULL
)) &&
2563 nvlist_lookup_string(tgt
, ZPOOL_CONFIG_PATH
, &pathname
) == 0) {
2564 uint64_t wholedisk
= 0;
2566 (void) nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_WHOLE_DISK
,
2570 * XXX - L2ARC 1.0 devices can't support expansion.
2573 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2574 "cannot expand cache devices"));
2575 return (zfs_error(hdl
, EZFS_VDEVNOTSUP
, msg
));
2579 const char *fullpath
= path
;
2580 char buf
[MAXPATHLEN
];
2582 if (path
[0] != '/') {
2583 error
= zfs_resolve_shortname(path
, buf
,
2586 return (zfs_error(hdl
, EZFS_NODEVICE
,
2592 error
= zpool_relabel_disk(hdl
, fullpath
, msg
);
2598 zc
.zc_cookie
= VDEV_STATE_ONLINE
;
2601 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) != 0) {
2602 if (errno
== EINVAL
) {
2603 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "was split "
2604 "from this pool into a new one. Use '%s' "
2605 "instead"), "zpool detach");
2606 return (zfs_error(hdl
, EZFS_POSTSPLIT_ONLINE
, msg
));
2608 return (zpool_standard_error(hdl
, errno
, msg
));
2611 *newstate
= zc
.zc_cookie
;
2616 * Take the specified vdev offline
2619 zpool_vdev_offline(zpool_handle_t
*zhp
, const char *path
, boolean_t istmp
)
2621 zfs_cmd_t zc
= {"\0"};
2624 boolean_t avail_spare
, l2cache
;
2625 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2627 (void) snprintf(msg
, sizeof (msg
),
2628 dgettext(TEXT_DOMAIN
, "cannot offline %s"), path
);
2630 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2631 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2633 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2635 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2638 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2640 zc
.zc_cookie
= VDEV_STATE_OFFLINE
;
2641 zc
.zc_obj
= istmp
? ZFS_OFFLINE_TEMPORARY
: 0;
2643 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2650 * There are no other replicas of this device.
2652 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2656 * The log device has unplayed logs
2658 return (zfs_error(hdl
, EZFS_UNPLAYED_LOGS
, msg
));
2661 return (zpool_standard_error(hdl
, errno
, msg
));
2666 * Mark the given vdev faulted.
2669 zpool_vdev_fault(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2671 zfs_cmd_t zc
= {"\0"};
2673 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2675 (void) snprintf(msg
, sizeof (msg
),
2676 dgettext(TEXT_DOMAIN
, "cannot fault %llu"), (u_longlong_t
)guid
);
2678 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2680 zc
.zc_cookie
= VDEV_STATE_FAULTED
;
2683 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2690 * There are no other replicas of this device.
2692 return (zfs_error(hdl
, EZFS_NOREPLICAS
, msg
));
2695 return (zpool_standard_error(hdl
, errno
, msg
));
2701 * Mark the given vdev degraded.
2704 zpool_vdev_degrade(zpool_handle_t
*zhp
, uint64_t guid
, vdev_aux_t aux
)
2706 zfs_cmd_t zc
= {"\0"};
2708 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2710 (void) snprintf(msg
, sizeof (msg
),
2711 dgettext(TEXT_DOMAIN
, "cannot degrade %llu"), (u_longlong_t
)guid
);
2713 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2715 zc
.zc_cookie
= VDEV_STATE_DEGRADED
;
2718 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_VDEV_SET_STATE
, &zc
) == 0)
2721 return (zpool_standard_error(hdl
, errno
, msg
));
2725 * Returns TRUE if the given nvlist is a vdev that was originally swapped in as
2729 is_replacing_spare(nvlist_t
*search
, nvlist_t
*tgt
, int which
)
2735 if (nvlist_lookup_nvlist_array(search
, ZPOOL_CONFIG_CHILDREN
, &child
,
2737 verify(nvlist_lookup_string(search
, ZPOOL_CONFIG_TYPE
,
2740 if (strcmp(type
, VDEV_TYPE_SPARE
) == 0 &&
2741 children
== 2 && child
[which
] == tgt
)
2744 for (c
= 0; c
< children
; c
++)
2745 if (is_replacing_spare(child
[c
], tgt
, which
))
2753 * Attach new_disk (fully described by nvroot) to old_disk.
2754 * If 'replacing' is specified, the new disk will replace the old one.
2757 zpool_vdev_attach(zpool_handle_t
*zhp
,
2758 const char *old_disk
, const char *new_disk
, nvlist_t
*nvroot
, int replacing
)
2760 zfs_cmd_t zc
= {"\0"};
2764 boolean_t avail_spare
, l2cache
, islog
;
2769 nvlist_t
*config_root
;
2770 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2771 boolean_t rootpool
= zpool_is_bootable(zhp
);
2774 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2775 "cannot replace %s with %s"), old_disk
, new_disk
);
2777 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
2778 "cannot attach %s to %s"), new_disk
, old_disk
);
2780 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2781 if ((tgt
= zpool_find_vdev(zhp
, old_disk
, &avail_spare
, &l2cache
,
2783 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2786 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2789 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2791 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2792 zc
.zc_cookie
= replacing
;
2794 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
2795 &child
, &children
) != 0 || children
!= 1) {
2796 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2797 "new device must be a single disk"));
2798 return (zfs_error(hdl
, EZFS_INVALCONFIG
, msg
));
2801 verify(nvlist_lookup_nvlist(zpool_get_config(zhp
, NULL
),
2802 ZPOOL_CONFIG_VDEV_TREE
, &config_root
) == 0);
2804 if ((newname
= zpool_vdev_name(NULL
, NULL
, child
[0], 0)) == NULL
)
2808 * If the target is a hot spare that has been swapped in, we can only
2809 * replace it with another hot spare.
2812 nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_IS_SPARE
, &val
) == 0 &&
2813 (zpool_find_vdev(zhp
, newname
, &avail_spare
, &l2cache
,
2814 NULL
) == NULL
|| !avail_spare
) &&
2815 is_replacing_spare(config_root
, tgt
, 1)) {
2816 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2817 "can only be replaced by another hot spare"));
2819 return (zfs_error(hdl
, EZFS_BADTARGET
, msg
));
2824 if (zcmd_write_conf_nvlist(hdl
, &zc
, nvroot
) != 0)
2827 ret
= zfs_ioctl(hdl
, ZFS_IOC_VDEV_ATTACH
, &zc
);
2829 zcmd_free_nvlists(&zc
);
2834 * XXX need a better way to prevent user from
2835 * booting up a half-baked vdev.
2837 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Make "
2838 "sure to wait until resilver is done "
2839 "before rebooting.\n"));
2847 * Can't attach to or replace this type of vdev.
2850 uint64_t version
= zpool_get_prop_int(zhp
,
2851 ZPOOL_PROP_VERSION
, NULL
);
2854 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2855 "cannot replace a log with a spare"));
2856 else if (version
>= SPA_VERSION_MULTI_REPLACE
)
2857 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2858 "already in replacing/spare config; wait "
2859 "for completion or use 'zpool detach'"));
2861 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2862 "cannot replace a replacing device"));
2864 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2865 "can only attach to mirrors and top-level "
2868 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2873 * The new device must be a single disk.
2875 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2876 "new device must be a single disk"));
2877 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
2881 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "%s is busy, "
2882 "or device removal is in progress"),
2884 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2889 * The new device is too small.
2891 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2892 "device is too small"));
2893 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2898 * The new device has a different optimal sector size.
2900 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2901 "new device has a different optimal sector size; use the "
2902 "option '-o ashift=N' to override the optimal size"));
2903 (void) zfs_error(hdl
, EZFS_BADDEV
, msg
);
2908 * The resulting top-level vdev spec won't fit in the label.
2910 (void) zfs_error(hdl
, EZFS_DEVOVERFLOW
, msg
);
2914 (void) zpool_standard_error(hdl
, errno
, msg
);
2921 * Detach the specified device.
2924 zpool_vdev_detach(zpool_handle_t
*zhp
, const char *path
)
2926 zfs_cmd_t zc
= {"\0"};
2929 boolean_t avail_spare
, l2cache
;
2930 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
2932 (void) snprintf(msg
, sizeof (msg
),
2933 dgettext(TEXT_DOMAIN
, "cannot detach %s"), path
);
2935 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
2936 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
2938 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
2941 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
2944 return (zfs_error(hdl
, EZFS_ISL2CACHE
, msg
));
2946 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
, &zc
.zc_guid
) == 0);
2948 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_DETACH
, &zc
) == 0)
2955 * Can't detach from this type of vdev.
2957 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "only "
2958 "applicable to mirror and replacing vdevs"));
2959 (void) zfs_error(hdl
, EZFS_BADTARGET
, msg
);
2964 * There are no other replicas of this device.
2966 (void) zfs_error(hdl
, EZFS_NOREPLICAS
, msg
);
2970 (void) zpool_standard_error(hdl
, errno
, msg
);
2977 * Find a mirror vdev in the source nvlist.
2979 * The mchild array contains a list of disks in one of the top-level mirrors
2980 * of the source pool. The schild array contains a list of disks that the
2981 * user specified on the command line. We loop over the mchild array to
2982 * see if any entry in the schild array matches.
2984 * If a disk in the mchild array is found in the schild array, we return
2985 * the index of that entry. Otherwise we return -1.
2988 find_vdev_entry(zpool_handle_t
*zhp
, nvlist_t
**mchild
, uint_t mchildren
,
2989 nvlist_t
**schild
, uint_t schildren
)
2993 for (mc
= 0; mc
< mchildren
; mc
++) {
2995 char *mpath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
2998 for (sc
= 0; sc
< schildren
; sc
++) {
2999 char *spath
= zpool_vdev_name(zhp
->zpool_hdl
, zhp
,
3001 boolean_t result
= (strcmp(mpath
, spath
) == 0);
3017 * Split a mirror pool. If newroot points to null, then a new nvlist
3018 * is generated and it is the responsibility of the caller to free it.
3021 zpool_vdev_split(zpool_handle_t
*zhp
, char *newname
, nvlist_t
**newroot
,
3022 nvlist_t
*props
, splitflags_t flags
)
3024 zfs_cmd_t zc
= {"\0"};
3026 nvlist_t
*tree
, *config
, **child
, **newchild
, *newconfig
= NULL
;
3027 nvlist_t
**varray
= NULL
, *zc_props
= NULL
;
3028 uint_t c
, children
, newchildren
, lastlog
= 0, vcount
, found
= 0;
3029 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3030 uint64_t vers
, readonly
= B_FALSE
;
3031 boolean_t freelist
= B_FALSE
, memory_err
= B_TRUE
;
3034 (void) snprintf(msg
, sizeof (msg
),
3035 dgettext(TEXT_DOMAIN
, "Unable to split %s"), zhp
->zpool_name
);
3037 if (!zpool_name_valid(hdl
, B_FALSE
, newname
))
3038 return (zfs_error(hdl
, EZFS_INVALIDNAME
, msg
));
3040 if ((config
= zpool_get_config(zhp
, NULL
)) == NULL
) {
3041 (void) fprintf(stderr
, gettext("Internal error: unable to "
3042 "retrieve pool configuration\n"));
3046 verify(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &tree
)
3048 verify(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
, &vers
) == 0);
3051 prop_flags_t flags
= { .create
= B_FALSE
, .import
= B_TRUE
};
3052 if ((zc_props
= zpool_valid_proplist(hdl
, zhp
->zpool_name
,
3053 props
, vers
, flags
, msg
)) == NULL
)
3055 (void) nvlist_lookup_uint64(zc_props
,
3056 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
3058 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3059 "property %s can only be set at import time"),
3060 zpool_prop_to_name(ZPOOL_PROP_READONLY
));
3065 if (nvlist_lookup_nvlist_array(tree
, ZPOOL_CONFIG_CHILDREN
, &child
,
3067 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3068 "Source pool is missing vdev tree"));
3069 nvlist_free(zc_props
);
3073 varray
= zfs_alloc(hdl
, children
* sizeof (nvlist_t
*));
3076 if (*newroot
== NULL
||
3077 nvlist_lookup_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
,
3078 &newchild
, &newchildren
) != 0)
3081 for (c
= 0; c
< children
; c
++) {
3082 uint64_t is_log
= B_FALSE
, is_hole
= B_FALSE
;
3084 nvlist_t
**mchild
, *vdev
;
3089 * Unlike cache & spares, slogs are stored in the
3090 * ZPOOL_CONFIG_CHILDREN array. We filter them out here.
3092 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_LOG
,
3094 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
3096 if (is_log
|| is_hole
) {
3098 * Create a hole vdev and put it in the config.
3100 if (nvlist_alloc(&vdev
, NV_UNIQUE_NAME
, 0) != 0)
3102 if (nvlist_add_string(vdev
, ZPOOL_CONFIG_TYPE
,
3103 VDEV_TYPE_HOLE
) != 0)
3105 if (nvlist_add_uint64(vdev
, ZPOOL_CONFIG_IS_HOLE
,
3110 varray
[vcount
++] = vdev
;
3114 verify(nvlist_lookup_string(child
[c
], ZPOOL_CONFIG_TYPE
, &type
)
3116 if (strcmp(type
, VDEV_TYPE_MIRROR
) != 0) {
3117 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3118 "Source pool must be composed only of mirrors\n"));
3119 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3123 verify(nvlist_lookup_nvlist_array(child
[c
],
3124 ZPOOL_CONFIG_CHILDREN
, &mchild
, &mchildren
) == 0);
3126 /* find or add an entry for this top-level vdev */
3127 if (newchildren
> 0 &&
3128 (entry
= find_vdev_entry(zhp
, mchild
, mchildren
,
3129 newchild
, newchildren
)) >= 0) {
3130 /* We found a disk that the user specified. */
3131 vdev
= mchild
[entry
];
3134 /* User didn't specify a disk for this vdev. */
3135 vdev
= mchild
[mchildren
- 1];
3138 if (nvlist_dup(vdev
, &varray
[vcount
++], 0) != 0)
3142 /* did we find every disk the user specified? */
3143 if (found
!= newchildren
) {
3144 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "Device list must "
3145 "include at most one disk from each mirror"));
3146 retval
= zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3150 /* Prepare the nvlist for populating. */
3151 if (*newroot
== NULL
) {
3152 if (nvlist_alloc(newroot
, NV_UNIQUE_NAME
, 0) != 0)
3155 if (nvlist_add_string(*newroot
, ZPOOL_CONFIG_TYPE
,
3156 VDEV_TYPE_ROOT
) != 0)
3159 verify(nvlist_remove_all(*newroot
, ZPOOL_CONFIG_CHILDREN
) == 0);
3162 /* Add all the children we found */
3163 if (nvlist_add_nvlist_array(*newroot
, ZPOOL_CONFIG_CHILDREN
, varray
,
3164 lastlog
== 0 ? vcount
: lastlog
) != 0)
3168 * If we're just doing a dry run, exit now with success.
3171 memory_err
= B_FALSE
;
3176 /* now build up the config list & call the ioctl */
3177 if (nvlist_alloc(&newconfig
, NV_UNIQUE_NAME
, 0) != 0)
3180 if (nvlist_add_nvlist(newconfig
,
3181 ZPOOL_CONFIG_VDEV_TREE
, *newroot
) != 0 ||
3182 nvlist_add_string(newconfig
,
3183 ZPOOL_CONFIG_POOL_NAME
, newname
) != 0 ||
3184 nvlist_add_uint64(newconfig
, ZPOOL_CONFIG_VERSION
, vers
) != 0)
3188 * The new pool is automatically part of the namespace unless we
3189 * explicitly export it.
3192 zc
.zc_cookie
= ZPOOL_EXPORT_AFTER_SPLIT
;
3193 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3194 (void) strlcpy(zc
.zc_string
, newname
, sizeof (zc
.zc_string
));
3195 if (zcmd_write_conf_nvlist(hdl
, &zc
, newconfig
) != 0)
3197 if (zc_props
!= NULL
&& zcmd_write_src_nvlist(hdl
, &zc
, zc_props
) != 0)
3200 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_SPLIT
, &zc
) != 0) {
3201 retval
= zpool_standard_error(hdl
, errno
, msg
);
3206 memory_err
= B_FALSE
;
3209 if (varray
!= NULL
) {
3212 for (v
= 0; v
< vcount
; v
++)
3213 nvlist_free(varray
[v
]);
3216 zcmd_free_nvlists(&zc
);
3217 nvlist_free(zc_props
);
3218 nvlist_free(newconfig
);
3220 nvlist_free(*newroot
);
3228 return (no_memory(hdl
));
3234 * Remove the given device.
3237 zpool_vdev_remove(zpool_handle_t
*zhp
, const char *path
)
3239 zfs_cmd_t zc
= {"\0"};
3242 boolean_t avail_spare
, l2cache
, islog
;
3243 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3246 (void) snprintf(msg
, sizeof (msg
),
3247 dgettext(TEXT_DOMAIN
, "cannot remove %s"), path
);
3249 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3250 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3252 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3254 version
= zpool_get_prop_int(zhp
, ZPOOL_PROP_VERSION
, NULL
);
3255 if (islog
&& version
< SPA_VERSION_HOLES
) {
3256 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3257 "pool must be upgraded to support log removal"));
3258 return (zfs_error(hdl
, EZFS_BADVERSION
, msg
));
3261 if (!islog
&& !avail_spare
&& !l2cache
&& zpool_is_bootable(zhp
)) {
3262 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3263 "root pool can not have removed devices, "
3264 "because GRUB does not understand them"));
3265 return (zfs_error(hdl
, EINVAL
, msg
));
3268 zc
.zc_guid
= fnvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
);
3270 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3276 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3277 "invalid config; all top-level vdevs must "
3278 "have the same sector size and not be raidz."));
3279 (void) zfs_error(hdl
, EZFS_INVALCONFIG
, msg
);
3283 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3284 "Pool busy; removal may already be in progress"));
3285 (void) zfs_error(hdl
, EZFS_BUSY
, msg
);
3289 (void) zpool_standard_error(hdl
, errno
, msg
);
3295 zpool_vdev_remove_cancel(zpool_handle_t
*zhp
)
3299 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3301 (void) snprintf(msg
, sizeof (msg
),
3302 dgettext(TEXT_DOMAIN
, "cannot cancel removal"));
3304 bzero(&zc
, sizeof (zc
));
3305 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3308 if (zfs_ioctl(hdl
, ZFS_IOC_VDEV_REMOVE
, &zc
) == 0)
3311 return (zpool_standard_error(hdl
, errno
, msg
));
3315 zpool_vdev_indirect_size(zpool_handle_t
*zhp
, const char *path
,
3320 boolean_t avail_spare
, l2cache
, islog
;
3321 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3323 (void) snprintf(msg
, sizeof (msg
),
3324 dgettext(TEXT_DOMAIN
, "cannot determine indirect size of %s"),
3327 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
, &l2cache
,
3329 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3331 if (avail_spare
|| l2cache
|| islog
) {
3336 if (nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_INDIRECT_SIZE
, sizep
) != 0) {
3337 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3338 "indirect size not available"));
3339 return (zfs_error(hdl
, EINVAL
, msg
));
3345 * Clear the errors for the pool, or the particular device if specified.
3348 zpool_clear(zpool_handle_t
*zhp
, const char *path
, nvlist_t
*rewindnvl
)
3350 zfs_cmd_t zc
= {"\0"};
3353 zpool_rewind_policy_t policy
;
3354 boolean_t avail_spare
, l2cache
;
3355 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3356 nvlist_t
*nvi
= NULL
;
3360 (void) snprintf(msg
, sizeof (msg
),
3361 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3364 (void) snprintf(msg
, sizeof (msg
),
3365 dgettext(TEXT_DOMAIN
, "cannot clear errors for %s"),
3368 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3370 if ((tgt
= zpool_find_vdev(zhp
, path
, &avail_spare
,
3371 &l2cache
, NULL
)) == NULL
)
3372 return (zfs_error(hdl
, EZFS_NODEVICE
, msg
));
3375 * Don't allow error clearing for hot spares. Do allow
3376 * error clearing for l2cache devices.
3379 return (zfs_error(hdl
, EZFS_ISSPARE
, msg
));
3381 verify(nvlist_lookup_uint64(tgt
, ZPOOL_CONFIG_GUID
,
3385 zpool_get_rewind_policy(rewindnvl
, &policy
);
3386 zc
.zc_cookie
= policy
.zrp_request
;
3388 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, zhp
->zpool_config_size
* 2) != 0)
3391 if (zcmd_write_src_nvlist(hdl
, &zc
, rewindnvl
) != 0)
3394 while ((error
= zfs_ioctl(hdl
, ZFS_IOC_CLEAR
, &zc
)) != 0 &&
3396 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
3397 zcmd_free_nvlists(&zc
);
3402 if (!error
|| ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) &&
3403 errno
!= EPERM
&& errno
!= EACCES
)) {
3404 if (policy
.zrp_request
&
3405 (ZPOOL_DO_REWIND
| ZPOOL_TRY_REWIND
)) {
3406 (void) zcmd_read_dst_nvlist(hdl
, &zc
, &nvi
);
3407 zpool_rewind_exclaim(hdl
, zc
.zc_name
,
3408 ((policy
.zrp_request
& ZPOOL_TRY_REWIND
) != 0),
3412 zcmd_free_nvlists(&zc
);
3416 zcmd_free_nvlists(&zc
);
3417 return (zpool_standard_error(hdl
, errno
, msg
));
3421 * Similar to zpool_clear(), but takes a GUID (used by fmd).
3424 zpool_vdev_clear(zpool_handle_t
*zhp
, uint64_t guid
)
3426 zfs_cmd_t zc
= {"\0"};
3428 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3430 (void) snprintf(msg
, sizeof (msg
),
3431 dgettext(TEXT_DOMAIN
, "cannot clear errors for %llx"),
3432 (u_longlong_t
)guid
);
3434 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3436 zc
.zc_cookie
= ZPOOL_NO_REWIND
;
3438 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_CLEAR
, &zc
) == 0)
3441 return (zpool_standard_error(hdl
, errno
, msg
));
3445 * Change the GUID for a pool.
3448 zpool_reguid(zpool_handle_t
*zhp
)
3451 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3452 zfs_cmd_t zc
= {"\0"};
3454 (void) snprintf(msg
, sizeof (msg
),
3455 dgettext(TEXT_DOMAIN
, "cannot reguid '%s'"), zhp
->zpool_name
);
3457 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3458 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_REGUID
, &zc
) == 0)
3461 return (zpool_standard_error(hdl
, errno
, msg
));
3468 zpool_reopen_one(zpool_handle_t
*zhp
, void *data
)
3470 libzfs_handle_t
*hdl
= zpool_get_handle(zhp
);
3471 const char *pool_name
= zpool_get_name(zhp
);
3472 boolean_t
*scrub_restart
= data
;
3475 error
= lzc_reopen(pool_name
, *scrub_restart
);
3477 return (zpool_standard_error_fmt(hdl
, error
,
3478 dgettext(TEXT_DOMAIN
, "cannot reopen '%s'"), pool_name
));
3484 /* call into libzfs_core to execute the sync IOCTL per pool */
3486 zpool_sync_one(zpool_handle_t
*zhp
, void *data
)
3489 libzfs_handle_t
*hdl
= zpool_get_handle(zhp
);
3490 const char *pool_name
= zpool_get_name(zhp
);
3491 boolean_t
*force
= data
;
3492 nvlist_t
*innvl
= fnvlist_alloc();
3494 fnvlist_add_boolean_value(innvl
, "force", *force
);
3495 if ((ret
= lzc_sync(pool_name
, innvl
, NULL
)) != 0) {
3497 return (zpool_standard_error_fmt(hdl
, ret
,
3498 dgettext(TEXT_DOMAIN
, "sync '%s' failed"), pool_name
));
3505 #if defined(__sun__) || defined(__sun)
3507 * Convert from a devid string to a path.
3510 devid_to_path(char *devid_str
)
3515 devid_nmlist_t
*list
= NULL
;
3518 if (devid_str_decode(devid_str
, &devid
, &minor
) != 0)
3521 ret
= devid_deviceid_to_nmlist("/dev", devid
, minor
, &list
);
3523 devid_str_free(minor
);
3530 * In a case the strdup() fails, we will just return NULL below.
3532 path
= strdup(list
[0].devname
);
3534 devid_free_nmlist(list
);
3540 * Convert from a path to a devid string.
3543 path_to_devid(const char *path
)
3549 if ((fd
= open(path
, O_RDONLY
)) < 0)
3554 if (devid_get(fd
, &devid
) == 0) {
3555 if (devid_get_minor_name(fd
, &minor
) == 0)
3556 ret
= devid_str_encode(devid
, minor
);
3558 devid_str_free(minor
);
3567 * Issue the necessary ioctl() to update the stored path value for the vdev. We
3568 * ignore any failure here, since a common case is for an unprivileged user to
3569 * type 'zpool status', and we'll display the correct information anyway.
3572 set_path(zpool_handle_t
*zhp
, nvlist_t
*nv
, const char *path
)
3574 zfs_cmd_t zc
= {"\0"};
3576 (void) strncpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3577 (void) strncpy(zc
.zc_value
, path
, sizeof (zc
.zc_value
));
3578 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
,
3581 (void) ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_VDEV_SETPATH
, &zc
);
3586 * Remove partition suffix from a vdev path. Partition suffixes may take three
3587 * forms: "-partX", "pX", or "X", where X is a string of digits. The second
3588 * case only occurs when the suffix is preceded by a digit, i.e. "md0p0" The
3589 * third case only occurs when preceded by a string matching the regular
3590 * expression "^([hsv]|xv)d[a-z]+", i.e. a scsi, ide, virtio or xen disk.
3592 * caller must free the returned string
3595 zfs_strip_partition(char *path
)
3597 char *tmp
= strdup(path
);
3598 char *part
= NULL
, *d
= NULL
;
3602 if ((part
= strstr(tmp
, "-part")) && part
!= tmp
) {
3604 } else if ((part
= strrchr(tmp
, 'p')) &&
3605 part
> tmp
+ 1 && isdigit(*(part
-1))) {
3607 } else if ((tmp
[0] == 'h' || tmp
[0] == 's' || tmp
[0] == 'v') &&
3609 for (d
= &tmp
[2]; isalpha(*d
); part
= ++d
) { }
3610 } else if (strncmp("xvd", tmp
, 3) == 0) {
3611 for (d
= &tmp
[3]; isalpha(*d
); part
= ++d
) { }
3613 if (part
&& d
&& *d
!= '\0') {
3614 for (; isdigit(*d
); d
++) { }
3623 * Same as zfs_strip_partition, but allows "/dev/" to be in the pathname
3628 * Returned string must be freed.
3631 zfs_strip_partition_path(char *path
)
3633 char *newpath
= strdup(path
);
3640 /* Point to "sda1" part of "/dev/sda1" */
3641 sd_offset
= strrchr(newpath
, '/') + 1;
3643 /* Get our new name "sda" */
3644 new_sd
= zfs_strip_partition(sd_offset
);
3650 /* Paste the "sda" where "sda1" was */
3651 strlcpy(sd_offset
, new_sd
, strlen(sd_offset
) + 1);
3653 /* Free temporary "sda" */
3659 #define PATH_BUF_LEN 64
3662 * Given a vdev, return the name to display in iostat. If the vdev has a path,
3663 * we use that, stripping off any leading "/dev/dsk/"; if not, we use the type.
3664 * We also check if this is a whole disk, in which case we strip off the
3665 * trailing 's0' slice name.
3667 * This routine is also responsible for identifying when disks have been
3668 * reconfigured in a new location. The kernel will have opened the device by
3669 * devid, but the path will still refer to the old location. To catch this, we
3670 * first do a path -> devid translation (which is fast for the common case). If
3671 * the devid matches, we're done. If not, we do a reverse devid -> path
3672 * translation and issue the appropriate ioctl() to update the path of the vdev.
3673 * If 'zhp' is NULL, then this is an exported pool, and we don't need to do any
3677 zpool_vdev_name(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, nvlist_t
*nv
,
3680 char *path
, *type
, *env
;
3682 char buf
[PATH_BUF_LEN
];
3683 char tmpbuf
[PATH_BUF_LEN
];
3686 * vdev_name will be "root"/"root-0" for the root vdev, but it is the
3687 * zpool name that will be displayed to the user.
3689 verify(nvlist_lookup_string(nv
, ZPOOL_CONFIG_TYPE
, &type
) == 0);
3690 if (zhp
!= NULL
&& strcmp(type
, "root") == 0)
3691 return (zfs_strdup(hdl
, zpool_get_name(zhp
)));
3693 env
= getenv("ZPOOL_VDEV_NAME_PATH");
3694 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3695 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3696 name_flags
|= VDEV_NAME_PATH
;
3698 env
= getenv("ZPOOL_VDEV_NAME_GUID");
3699 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3700 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3701 name_flags
|= VDEV_NAME_GUID
;
3703 env
= getenv("ZPOOL_VDEV_NAME_FOLLOW_LINKS");
3704 if (env
&& (strtoul(env
, NULL
, 0) > 0 ||
3705 !strncasecmp(env
, "YES", 3) || !strncasecmp(env
, "ON", 2)))
3706 name_flags
|= VDEV_NAME_FOLLOW_LINKS
;
3708 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NOT_PRESENT
, &value
) == 0 ||
3709 name_flags
& VDEV_NAME_GUID
) {
3710 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_GUID
, &value
);
3711 (void) snprintf(buf
, sizeof (buf
), "%llu", (u_longlong_t
)value
);
3713 } else if (nvlist_lookup_string(nv
, ZPOOL_CONFIG_PATH
, &path
) == 0) {
3714 #if defined(__sun__) || defined(__sun)
3716 * Live VDEV path updates to a kernel VDEV during a
3717 * zpool_vdev_name lookup are not supported on Linux.
3724 * If the device is dead (faulted, offline, etc) then don't
3725 * bother opening it. Otherwise we may be forcing the user to
3726 * open a misbehaving device, which can have undesirable
3729 if ((nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
3730 (uint64_t **)&vs
, &vsc
) != 0 ||
3731 vs
->vs_state
>= VDEV_STATE_DEGRADED
) &&
3733 nvlist_lookup_string(nv
, ZPOOL_CONFIG_DEVID
, &devid
) == 0) {
3735 * Determine if the current path is correct.
3737 char *newdevid
= path_to_devid(path
);
3739 if (newdevid
== NULL
||
3740 strcmp(devid
, newdevid
) != 0) {
3743 if ((newpath
= devid_to_path(devid
)) != NULL
) {
3745 * Update the path appropriately.
3747 set_path(zhp
, nv
, newpath
);
3748 if (nvlist_add_string(nv
,
3749 ZPOOL_CONFIG_PATH
, newpath
) == 0)
3750 verify(nvlist_lookup_string(nv
,
3758 devid_str_free(newdevid
);
3762 if (name_flags
& VDEV_NAME_FOLLOW_LINKS
) {
3763 char *rp
= realpath(path
, NULL
);
3765 strlcpy(buf
, rp
, sizeof (buf
));
3772 * For a block device only use the name.
3774 if ((strcmp(type
, VDEV_TYPE_DISK
) == 0) &&
3775 !(name_flags
& VDEV_NAME_PATH
)) {
3776 path
= strrchr(path
, '/');
3781 * Remove the partition from the path it this is a whole disk.
3783 if (nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_WHOLE_DISK
, &value
)
3784 == 0 && value
&& !(name_flags
& VDEV_NAME_PATH
)) {
3785 return (zfs_strip_partition(path
));
3791 * If it's a raidz device, we need to stick in the parity level.
3793 if (strcmp(path
, VDEV_TYPE_RAIDZ
) == 0) {
3794 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_NPARITY
,
3796 (void) snprintf(buf
, sizeof (buf
), "%s%llu", path
,
3797 (u_longlong_t
)value
);
3802 * We identify each top-level vdev by using a <type-id>
3803 * naming convention.
3805 if (name_flags
& VDEV_NAME_TYPE_ID
) {
3807 verify(nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_ID
,
3809 (void) snprintf(tmpbuf
, sizeof (tmpbuf
), "%s-%llu",
3810 path
, (u_longlong_t
)id
);
3815 return (zfs_strdup(hdl
, path
));
3819 zbookmark_mem_compare(const void *a
, const void *b
)
3821 return (memcmp(a
, b
, sizeof (zbookmark_phys_t
)));
3825 * Retrieve the persistent error log, uniquify the members, and return to the
3829 zpool_get_errlog(zpool_handle_t
*zhp
, nvlist_t
**nverrlistp
)
3831 zfs_cmd_t zc
= {"\0"};
3832 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3834 zbookmark_phys_t
*zb
= NULL
;
3838 * Retrieve the raw error list from the kernel. If the number of errors
3839 * has increased, allocate more space and continue until we get the
3842 verify(nvlist_lookup_uint64(zhp
->zpool_config
, ZPOOL_CONFIG_ERRCOUNT
,
3846 zc
.zc_nvlist_dst
= (uintptr_t)zfs_alloc(zhp
->zpool_hdl
,
3847 count
* sizeof (zbookmark_phys_t
));
3848 zc
.zc_nvlist_dst_size
= count
;
3849 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3851 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_ERROR_LOG
,
3853 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3854 if (errno
== ENOMEM
) {
3857 count
= zc
.zc_nvlist_dst_size
;
3858 dst
= zfs_alloc(zhp
->zpool_hdl
, count
*
3859 sizeof (zbookmark_phys_t
));
3860 zc
.zc_nvlist_dst
= (uintptr_t)dst
;
3862 return (zpool_standard_error_fmt(hdl
, errno
,
3863 dgettext(TEXT_DOMAIN
, "errors: List of "
3864 "errors unavailable")));
3872 * Sort the resulting bookmarks. This is a little confusing due to the
3873 * implementation of ZFS_IOC_ERROR_LOG. The bookmarks are copied last
3874 * to first, and 'zc_nvlist_dst_size' indicates the number of boomarks
3875 * _not_ copied as part of the process. So we point the start of our
3876 * array appropriate and decrement the total number of elements.
3878 zb
= ((zbookmark_phys_t
*)(uintptr_t)zc
.zc_nvlist_dst
) +
3879 zc
.zc_nvlist_dst_size
;
3880 count
-= zc
.zc_nvlist_dst_size
;
3882 qsort(zb
, count
, sizeof (zbookmark_phys_t
), zbookmark_mem_compare
);
3884 verify(nvlist_alloc(nverrlistp
, 0, KM_SLEEP
) == 0);
3887 * Fill in the nverrlistp with nvlist's of dataset and object numbers.
3889 for (i
= 0; i
< count
; i
++) {
3892 /* ignoring zb_blkid and zb_level for now */
3893 if (i
> 0 && zb
[i
-1].zb_objset
== zb
[i
].zb_objset
&&
3894 zb
[i
-1].zb_object
== zb
[i
].zb_object
)
3897 if (nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) != 0)
3899 if (nvlist_add_uint64(nv
, ZPOOL_ERR_DATASET
,
3900 zb
[i
].zb_objset
) != 0) {
3904 if (nvlist_add_uint64(nv
, ZPOOL_ERR_OBJECT
,
3905 zb
[i
].zb_object
) != 0) {
3909 if (nvlist_add_nvlist(*nverrlistp
, "ejk", nv
) != 0) {
3916 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3920 free((void *)(uintptr_t)zc
.zc_nvlist_dst
);
3921 return (no_memory(zhp
->zpool_hdl
));
3925 * Upgrade a ZFS pool to the latest on-disk version.
3928 zpool_upgrade(zpool_handle_t
*zhp
, uint64_t new_version
)
3930 zfs_cmd_t zc
= {"\0"};
3931 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3933 (void) strcpy(zc
.zc_name
, zhp
->zpool_name
);
3934 zc
.zc_cookie
= new_version
;
3936 if (zfs_ioctl(hdl
, ZFS_IOC_POOL_UPGRADE
, &zc
) != 0)
3937 return (zpool_standard_error_fmt(hdl
, errno
,
3938 dgettext(TEXT_DOMAIN
, "cannot upgrade '%s'"),
3944 zfs_save_arguments(int argc
, char **argv
, char *string
, int len
)
3948 (void) strlcpy(string
, basename(argv
[0]), len
);
3949 for (i
= 1; i
< argc
; i
++) {
3950 (void) strlcat(string
, " ", len
);
3951 (void) strlcat(string
, argv
[i
], len
);
3956 zpool_log_history(libzfs_handle_t
*hdl
, const char *message
)
3958 zfs_cmd_t zc
= {"\0"};
3962 args
= fnvlist_alloc();
3963 fnvlist_add_string(args
, "message", message
);
3964 err
= zcmd_write_src_nvlist(hdl
, &zc
, args
);
3966 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_LOG_HISTORY
, &zc
);
3968 zcmd_free_nvlists(&zc
);
3973 * Perform ioctl to get some command history of a pool.
3975 * 'buf' is the buffer to fill up to 'len' bytes. 'off' is the
3976 * logical offset of the history buffer to start reading from.
3978 * Upon return, 'off' is the next logical offset to read from and
3979 * 'len' is the actual amount of bytes read into 'buf'.
3982 get_history(zpool_handle_t
*zhp
, char *buf
, uint64_t *off
, uint64_t *len
)
3984 zfs_cmd_t zc
= {"\0"};
3985 libzfs_handle_t
*hdl
= zhp
->zpool_hdl
;
3987 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
3989 zc
.zc_history
= (uint64_t)(uintptr_t)buf
;
3990 zc
.zc_history_len
= *len
;
3991 zc
.zc_history_offset
= *off
;
3993 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_POOL_GET_HISTORY
, &zc
) != 0) {
3996 return (zfs_error_fmt(hdl
, EZFS_PERM
,
3997 dgettext(TEXT_DOMAIN
,
3998 "cannot show history for pool '%s'"),
4001 return (zfs_error_fmt(hdl
, EZFS_NOHISTORY
,
4002 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
4003 "'%s'"), zhp
->zpool_name
));
4005 return (zfs_error_fmt(hdl
, EZFS_BADVERSION
,
4006 dgettext(TEXT_DOMAIN
, "cannot get history for pool "
4007 "'%s', pool must be upgraded"), zhp
->zpool_name
));
4009 return (zpool_standard_error_fmt(hdl
, errno
,
4010 dgettext(TEXT_DOMAIN
,
4011 "cannot get history for '%s'"), zhp
->zpool_name
));
4015 *len
= zc
.zc_history_len
;
4016 *off
= zc
.zc_history_offset
;
4022 * Process the buffer of nvlists, unpacking and storing each nvlist record
4023 * into 'records'. 'leftover' is set to the number of bytes that weren't
4024 * processed as there wasn't a complete record.
4027 zpool_history_unpack(char *buf
, uint64_t bytes_read
, uint64_t *leftover
,
4028 nvlist_t
***records
, uint_t
*numrecords
)
4035 while (bytes_read
> sizeof (reclen
)) {
4037 /* get length of packed record (stored as little endian) */
4038 for (i
= 0, reclen
= 0; i
< sizeof (reclen
); i
++)
4039 reclen
+= (uint64_t)(((uchar_t
*)buf
)[i
]) << (8*i
);
4041 if (bytes_read
< sizeof (reclen
) + reclen
)
4045 if (nvlist_unpack(buf
+ sizeof (reclen
), reclen
, &nv
, 0) != 0)
4047 bytes_read
-= sizeof (reclen
) + reclen
;
4048 buf
+= sizeof (reclen
) + reclen
;
4050 /* add record to nvlist array */
4052 if (ISP2(*numrecords
+ 1)) {
4053 tmp
= realloc(*records
,
4054 *numrecords
* 2 * sizeof (nvlist_t
*));
4062 (*records
)[*numrecords
- 1] = nv
;
4065 *leftover
= bytes_read
;
4070 * Retrieve the command history of a pool.
4073 zpool_get_history(zpool_handle_t
*zhp
, nvlist_t
**nvhisp
)
4076 int buflen
= 128 * 1024;
4078 nvlist_t
**records
= NULL
;
4079 uint_t numrecords
= 0;
4082 buf
= malloc(buflen
);
4086 uint64_t bytes_read
= buflen
;
4089 if ((err
= get_history(zhp
, buf
, &off
, &bytes_read
)) != 0)
4092 /* if nothing else was read in, we're at EOF, just return */
4096 if ((err
= zpool_history_unpack(buf
, bytes_read
,
4097 &leftover
, &records
, &numrecords
)) != 0)
4100 if (leftover
== bytes_read
) {
4102 * no progress made, because buffer is not big enough
4103 * to hold this record; resize and retry.
4107 buf
= malloc(buflen
);
4118 verify(nvlist_alloc(nvhisp
, NV_UNIQUE_NAME
, 0) == 0);
4119 verify(nvlist_add_nvlist_array(*nvhisp
, ZPOOL_HIST_RECORD
,
4120 records
, numrecords
) == 0);
4122 for (i
= 0; i
< numrecords
; i
++)
4123 nvlist_free(records
[i
]);
4130 * Retrieve the next event given the passed 'zevent_fd' file descriptor.
4131 * If there is a new event available 'nvp' will contain a newly allocated
4132 * nvlist and 'dropped' will be set to the number of missed events since
4133 * the last call to this function. When 'nvp' is set to NULL it indicates
4134 * no new events are available. In either case the function returns 0 and
4135 * it is up to the caller to free 'nvp'. In the case of a fatal error the
4136 * function will return a non-zero value. When the function is called in
4137 * blocking mode (the default, unless the ZEVENT_NONBLOCK flag is passed),
4138 * it will not return until a new event is available.
4141 zpool_events_next(libzfs_handle_t
*hdl
, nvlist_t
**nvp
,
4142 int *dropped
, unsigned flags
, int zevent_fd
)
4144 zfs_cmd_t zc
= {"\0"};
4149 zc
.zc_cleanup_fd
= zevent_fd
;
4151 if (flags
& ZEVENT_NONBLOCK
)
4152 zc
.zc_guid
= ZEVENT_NONBLOCK
;
4154 if (zcmd_alloc_dst_nvlist(hdl
, &zc
, ZEVENT_SIZE
) != 0)
4158 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_NEXT
, &zc
) != 0) {
4161 error
= zfs_error_fmt(hdl
, EZFS_POOLUNAVAIL
,
4162 dgettext(TEXT_DOMAIN
, "zfs shutdown"));
4165 /* Blocking error case should not occur */
4166 if (!(flags
& ZEVENT_NONBLOCK
))
4167 error
= zpool_standard_error_fmt(hdl
, errno
,
4168 dgettext(TEXT_DOMAIN
, "cannot get event"));
4172 if (zcmd_expand_dst_nvlist(hdl
, &zc
) != 0) {
4173 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
4174 dgettext(TEXT_DOMAIN
, "cannot get event"));
4180 error
= zpool_standard_error_fmt(hdl
, errno
,
4181 dgettext(TEXT_DOMAIN
, "cannot get event"));
4186 error
= zcmd_read_dst_nvlist(hdl
, &zc
, nvp
);
4190 *dropped
= (int)zc
.zc_cookie
;
4192 zcmd_free_nvlists(&zc
);
4201 zpool_events_clear(libzfs_handle_t
*hdl
, int *count
)
4203 zfs_cmd_t zc
= {"\0"};
4206 (void) snprintf(msg
, sizeof (msg
), dgettext(TEXT_DOMAIN
,
4207 "cannot clear events"));
4209 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_CLEAR
, &zc
) != 0)
4210 return (zpool_standard_error_fmt(hdl
, errno
, msg
));
4213 *count
= (int)zc
.zc_cookie
; /* # of events cleared */
4219 * Seek to a specific EID, ZEVENT_SEEK_START, or ZEVENT_SEEK_END for
4220 * the passed zevent_fd file handle. On success zero is returned,
4221 * otherwise -1 is returned and hdl->libzfs_error is set to the errno.
4224 zpool_events_seek(libzfs_handle_t
*hdl
, uint64_t eid
, int zevent_fd
)
4226 zfs_cmd_t zc
= {"\0"};
4230 zc
.zc_cleanup_fd
= zevent_fd
;
4232 if (zfs_ioctl(hdl
, ZFS_IOC_EVENTS_SEEK
, &zc
) != 0) {
4235 error
= zfs_error_fmt(hdl
, EZFS_NOENT
,
4236 dgettext(TEXT_DOMAIN
, "cannot get event"));
4240 error
= zfs_error_fmt(hdl
, EZFS_NOMEM
,
4241 dgettext(TEXT_DOMAIN
, "cannot get event"));
4245 error
= zpool_standard_error_fmt(hdl
, errno
,
4246 dgettext(TEXT_DOMAIN
, "cannot get event"));
4255 zpool_obj_to_path(zpool_handle_t
*zhp
, uint64_t dsobj
, uint64_t obj
,
4256 char *pathname
, size_t len
)
4258 zfs_cmd_t zc
= {"\0"};
4259 boolean_t mounted
= B_FALSE
;
4260 char *mntpnt
= NULL
;
4261 char dsname
[ZFS_MAX_DATASET_NAME_LEN
];
4264 /* special case for the MOS */
4265 (void) snprintf(pathname
, len
, "<metadata>:<0x%llx>",
4270 /* get the dataset's name */
4271 (void) strlcpy(zc
.zc_name
, zhp
->zpool_name
, sizeof (zc
.zc_name
));
4273 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
,
4274 ZFS_IOC_DSOBJ_TO_DSNAME
, &zc
) != 0) {
4275 /* just write out a path of two object numbers */
4276 (void) snprintf(pathname
, len
, "<0x%llx>:<0x%llx>",
4277 (longlong_t
)dsobj
, (longlong_t
)obj
);
4280 (void) strlcpy(dsname
, zc
.zc_value
, sizeof (dsname
));
4282 /* find out if the dataset is mounted */
4283 mounted
= is_mounted(zhp
->zpool_hdl
, dsname
, &mntpnt
);
4285 /* get the corrupted object's path */
4286 (void) strlcpy(zc
.zc_name
, dsname
, sizeof (zc
.zc_name
));
4288 if (ioctl(zhp
->zpool_hdl
->libzfs_fd
, ZFS_IOC_OBJ_TO_PATH
,
4291 (void) snprintf(pathname
, len
, "%s%s", mntpnt
,
4294 (void) snprintf(pathname
, len
, "%s:%s",
4295 dsname
, zc
.zc_value
);
4298 (void) snprintf(pathname
, len
, "%s:<0x%llx>", dsname
,
4305 * Read the EFI label from the config, if a label does not exist then
4306 * pass back the error to the caller. If the caller has passed a non-NULL
4307 * diskaddr argument then we set it to the starting address of the EFI
4311 read_efi_label(nvlist_t
*config
, diskaddr_t
*sb
)
4315 char diskname
[MAXPATHLEN
];
4318 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_PATH
, &path
) != 0)
4321 (void) snprintf(diskname
, sizeof (diskname
), "%s%s", DISK_ROOT
,
4322 strrchr(path
, '/'));
4323 if ((fd
= open(diskname
, O_RDONLY
|O_DIRECT
)) >= 0) {
4324 struct dk_gpt
*vtoc
;
4326 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) >= 0) {
4328 *sb
= vtoc
->efi_parts
[0].p_start
;
4337 * determine where a partition starts on a disk in the current
4341 find_start_block(nvlist_t
*config
)
4345 diskaddr_t sb
= MAXOFFSET_T
;
4348 if (nvlist_lookup_nvlist_array(config
,
4349 ZPOOL_CONFIG_CHILDREN
, &child
, &children
) != 0) {
4350 if (nvlist_lookup_uint64(config
,
4351 ZPOOL_CONFIG_WHOLE_DISK
,
4352 &wholedisk
) != 0 || !wholedisk
) {
4353 return (MAXOFFSET_T
);
4355 if (read_efi_label(config
, &sb
) < 0)
4360 for (c
= 0; c
< children
; c
++) {
4361 sb
= find_start_block(child
[c
]);
4362 if (sb
!= MAXOFFSET_T
) {
4366 return (MAXOFFSET_T
);
4370 zpool_label_disk_check(char *path
)
4372 struct dk_gpt
*vtoc
;
4375 if ((fd
= open(path
, O_RDONLY
|O_DIRECT
)) < 0)
4378 if ((err
= efi_alloc_and_read(fd
, &vtoc
)) != 0) {
4383 if (vtoc
->efi_flags
& EFI_GPT_PRIMARY_CORRUPT
) {
4395 * Generate a unique partition name for the ZFS member. Partitions must
4396 * have unique names to ensure udev will be able to create symlinks under
4397 * /dev/disk/by-partlabel/ for all pool members. The partition names are
4398 * of the form <pool>-<unique-id>.
4401 zpool_label_name(char *label_name
, int label_size
)
4406 fd
= open("/dev/urandom", O_RDONLY
);
4408 if (read(fd
, &id
, sizeof (id
)) != sizeof (id
))
4415 id
= (((uint64_t)rand()) << 32) | (uint64_t)rand();
4417 snprintf(label_name
, label_size
, "zfs-%016llx", (u_longlong_t
)id
);
4421 * Label an individual disk. The name provided is the short name,
4422 * stripped of any leading /dev path.
4425 zpool_label_disk(libzfs_handle_t
*hdl
, zpool_handle_t
*zhp
, char *name
)
4427 char path
[MAXPATHLEN
];
4428 struct dk_gpt
*vtoc
;
4430 size_t resv
= EFI_MIN_RESV_SIZE
;
4431 uint64_t slice_size
;
4432 diskaddr_t start_block
;
4435 /* prepare an error message just in case */
4436 (void) snprintf(errbuf
, sizeof (errbuf
),
4437 dgettext(TEXT_DOMAIN
, "cannot label '%s'"), name
);
4442 verify(nvlist_lookup_nvlist(zhp
->zpool_config
,
4443 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
4445 if (zhp
->zpool_start_block
== 0)
4446 start_block
= find_start_block(nvroot
);
4448 start_block
= zhp
->zpool_start_block
;
4449 zhp
->zpool_start_block
= start_block
;
4452 start_block
= NEW_START_BLOCK
;
4455 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4457 if ((fd
= open(path
, O_RDWR
|O_DIRECT
|O_EXCL
)) < 0) {
4459 * This shouldn't happen. We've long since verified that this
4460 * is a valid device.
4462 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
4463 "label '%s': unable to open device: %d"), path
, errno
);
4464 return (zfs_error(hdl
, EZFS_OPENFAILED
, errbuf
));
4467 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, &vtoc
) != 0) {
4469 * The only way this can fail is if we run out of memory, or we
4470 * were unable to read the disk's capacity
4472 if (errno
== ENOMEM
)
4473 (void) no_memory(hdl
);
4476 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "cannot "
4477 "label '%s': unable to read disk capacity"), path
);
4479 return (zfs_error(hdl
, EZFS_NOCAP
, errbuf
));
4482 slice_size
= vtoc
->efi_last_u_lba
+ 1;
4483 slice_size
-= EFI_MIN_RESV_SIZE
;
4484 if (start_block
== MAXOFFSET_T
)
4485 start_block
= NEW_START_BLOCK
;
4486 slice_size
-= start_block
;
4487 slice_size
= P2ALIGN(slice_size
, PARTITION_END_ALIGNMENT
);
4489 vtoc
->efi_parts
[0].p_start
= start_block
;
4490 vtoc
->efi_parts
[0].p_size
= slice_size
;
4493 * Why we use V_USR: V_BACKUP confuses users, and is considered
4494 * disposable by some EFI utilities (since EFI doesn't have a backup
4495 * slice). V_UNASSIGNED is supposed to be used only for zero size
4496 * partitions, and efi_write() will fail if we use it. V_ROOT, V_BOOT,
4497 * etc. were all pretty specific. V_USR is as close to reality as we
4498 * can get, in the absence of V_OTHER.
4500 vtoc
->efi_parts
[0].p_tag
= V_USR
;
4501 zpool_label_name(vtoc
->efi_parts
[0].p_name
, EFI_PART_NAME_LEN
);
4503 vtoc
->efi_parts
[8].p_start
= slice_size
+ start_block
;
4504 vtoc
->efi_parts
[8].p_size
= resv
;
4505 vtoc
->efi_parts
[8].p_tag
= V_RESERVED
;
4507 rval
= efi_write(fd
, vtoc
);
4509 /* Flush the buffers to disk and invalidate the page cache. */
4511 (void) ioctl(fd
, BLKFLSBUF
);
4514 rval
= efi_rescan(fd
);
4517 * Some block drivers (like pcata) may not support EFI GPT labels.
4518 * Print out a helpful error message directing the user to manually
4519 * label the disk and give a specific slice.
4525 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "try using "
4526 "parted(8) and then provide a specific slice: %d"), rval
);
4527 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4533 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4534 (void) zfs_append_partition(path
, MAXPATHLEN
);
4536 /* Wait to udev to signal use the device has settled. */
4537 rval
= zpool_label_disk_wait(path
, DISK_LABEL_WAIT
);
4539 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "failed to "
4540 "detect device partitions on '%s': %d"), path
, rval
);
4541 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4544 /* We can't be to paranoid. Read the label back and verify it. */
4545 (void) snprintf(path
, sizeof (path
), "%s/%s", DISK_ROOT
, name
);
4546 rval
= zpool_label_disk_check(path
);
4548 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "freshly written "
4549 "EFI label on '%s' is damaged. Ensure\nthis device "
4550 "is not in in use, and is functioning properly: %d"),
4552 return (zfs_error(hdl
, EZFS_LABELFAILED
, errbuf
));
4559 * Allocate and return the underlying device name for a device mapper device.
4560 * If a device mapper device maps to multiple devices, return the first device.
4562 * For example, dm_name = "/dev/dm-0" could return "/dev/sda". Symlinks to a
4563 * DM device (like /dev/disk/by-vdev/A0) are also allowed.
4565 * Returns device name, or NULL on error or no match. If dm_name is not a DM
4566 * device then return NULL.
4568 * NOTE: The returned name string must be *freed*.
4571 dm_get_underlying_path(char *dm_name
)
4581 if (dm_name
== NULL
)
4584 /* dm name may be a symlink (like /dev/disk/by-vdev/A0) */
4585 realp
= realpath(dm_name
, NULL
);
4590 * If they preface 'dev' with a path (like "/dev") then strip it off.
4591 * We just want the 'dm-N' part.
4593 tmp
= strrchr(realp
, '/');
4595 dev_str
= tmp
+ 1; /* +1 since we want the chr after '/' */
4599 size
= asprintf(&tmp
, "/sys/block/%s/slaves/", dev_str
);
4600 if (size
== -1 || !tmp
)
4607 /* Return first sd* entry in /sys/block/dm-N/slaves/ */
4608 while ((ep
= readdir(dp
))) {
4609 if (ep
->d_type
!= DT_DIR
) { /* skip "." and ".." dirs */
4610 size
= asprintf(&path
, "/dev/%s", ep
->d_name
);
4624 * Return 1 if device is a device mapper or multipath device.
4628 zfs_dev_is_dm(char *dev_name
)
4632 tmp
= dm_get_underlying_path(dev_name
);
4641 * By "whole disk" we mean an entire physical disk (something we can
4642 * label, toggle the write cache on, etc.) as opposed to the full
4643 * capacity of a pseudo-device such as lofi or did. We act as if we
4644 * are labeling the disk, which should be a pretty good test of whether
4645 * it's a viable device or not. Returns B_TRUE if it is and B_FALSE if
4649 zfs_dev_is_whole_disk(char *dev_name
)
4651 struct dk_gpt
*label
;
4654 if ((fd
= open(dev_name
, O_RDONLY
| O_DIRECT
)) < 0)
4657 if (efi_alloc_and_init(fd
, EFI_NUMPAR
, &label
) != 0) {
4669 * Lookup the underlying device for a device name
4671 * Often you'll have a symlink to a device, a partition device,
4672 * or a multipath device, and want to look up the underlying device.
4673 * This function returns the underlying device name. If the device
4674 * name is already the underlying device, then just return the same
4675 * name. If the device is a DM device with multiple underlying devices
4676 * then return the first one.
4680 * 1. /dev/disk/by-id/ata-QEMU_HARDDISK_QM00001 -> ../../sda
4681 * dev_name: /dev/disk/by-id/ata-QEMU_HARDDISK_QM00001
4684 * 2. /dev/mapper/mpatha (made up of /dev/sda and /dev/sdb)
4685 * dev_name: /dev/mapper/mpatha
4686 * returns: /dev/sda (first device)
4688 * 3. /dev/sda (already the underlying device)
4689 * dev_name: /dev/sda
4692 * 4. /dev/dm-3 (mapped to /dev/sda)
4693 * dev_name: /dev/dm-3
4696 * 5. /dev/disk/by-id/scsi-0QEMU_drive-scsi0-0-0-0-part9 -> ../../sdb9
4697 * dev_name: /dev/disk/by-id/scsi-0QEMU_drive-scsi0-0-0-0-part9
4700 * 6. /dev/disk/by-uuid/5df030cf-3cd9-46e4-8e99-3ccb462a4e9a -> ../dev/sda2
4701 * dev_name: /dev/disk/by-uuid/5df030cf-3cd9-46e4-8e99-3ccb462a4e9a
4704 * Returns underlying device name, or NULL on error or no match.
4706 * NOTE: The returned name string must be *freed*.
4709 zfs_get_underlying_path(char *dev_name
)
4714 if (dev_name
== NULL
)
4717 tmp
= dm_get_underlying_path(dev_name
);
4719 /* dev_name not a DM device, so just un-symlinkize it */
4721 tmp
= realpath(dev_name
, NULL
);
4724 name
= zfs_strip_partition_path(tmp
);
4732 * Given a dev name like "sda", return the full enclosure sysfs path to
4733 * the disk. You can also pass in the name with "/dev" prepended
4734 * to it (like /dev/sda).
4736 * For example, disk "sda" in enclosure slot 1:
4738 * returns: "/sys/class/enclosure/1:0:3:0/Slot 1"
4740 * 'dev' must be a non-devicemapper device.
4742 * Returned string must be freed.
4745 zfs_get_enclosure_sysfs_path(char *dev_name
)
4749 char buf
[MAXPATHLEN
];
4757 if (dev_name
== NULL
)
4760 /* If they preface 'dev' with a path (like "/dev") then strip it off */
4761 tmp1
= strrchr(dev_name
, '/');
4763 dev_name
= tmp1
+ 1; /* +1 since we want the chr after '/' */
4765 tmpsize
= asprintf(&tmp1
, "/sys/block/%s/device", dev_name
);
4766 if (tmpsize
== -1 || tmp1
== NULL
) {
4773 tmp1
= NULL
; /* To make free() at the end a NOP */
4778 * Look though all sysfs entries in /sys/block/<dev>/device for
4779 * the enclosure symlink.
4781 while ((ep
= readdir(dp
))) {
4782 /* Ignore everything that's not our enclosure_device link */
4783 if (strstr(ep
->d_name
, "enclosure_device") == NULL
)
4786 if (asprintf(&tmp2
, "%s/%s", tmp1
, ep
->d_name
) == -1 ||
4790 size
= readlink(tmp2
, buf
, sizeof (buf
));
4792 /* Did readlink fail or crop the link name? */
4793 if (size
== -1 || size
>= sizeof (buf
)) {
4795 tmp2
= NULL
; /* To make free() at the end a NOP */
4800 * We got a valid link. readlink() doesn't terminate strings
4801 * so we have to do it.
4806 * Our link will look like:
4808 * "../../../../port-11:1:2/..STUFF../enclosure/1:0:3:0/SLOT 1"
4810 * We want to grab the "enclosure/1:0:3:0/SLOT 1" part
4812 tmp3
= strstr(buf
, "enclosure");
4816 if (asprintf(&path
, "/sys/class/%s", tmp3
) == -1) {
4817 /* If asprintf() fails, 'path' is undefined */