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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013 by Delphix. All rights reserved.
25 * Copyright (c) 2013, 2014, Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
30 * SPA: Storage Pool Allocator
32 * This file contains all the routines used when modifying on-disk SPA state.
33 * This includes opening, importing, destroying, exporting a pool, and syncing a
37 #include <sys/zfs_context.h>
38 #include <sys/fm/fs/zfs.h>
39 #include <sys/spa_impl.h>
41 #include <sys/zio_checksum.h>
43 #include <sys/dmu_tx.h>
47 #include <sys/vdev_impl.h>
48 #include <sys/vdev_disk.h>
49 #include <sys/metaslab.h>
50 #include <sys/metaslab_impl.h>
51 #include <sys/uberblock_impl.h>
54 #include <sys/dmu_traverse.h>
55 #include <sys/dmu_objset.h>
56 #include <sys/unique.h>
57 #include <sys/dsl_pool.h>
58 #include <sys/dsl_dataset.h>
59 #include <sys/dsl_dir.h>
60 #include <sys/dsl_prop.h>
61 #include <sys/dsl_synctask.h>
62 #include <sys/fs/zfs.h>
64 #include <sys/callb.h>
65 #include <sys/systeminfo.h>
66 #include <sys/spa_boot.h>
67 #include <sys/zfs_ioctl.h>
68 #include <sys/dsl_scan.h>
69 #include <sys/zfeature.h>
70 #include <sys/dsl_destroy.h>
74 #include <sys/bootprops.h>
75 #include <sys/callb.h>
76 #include <sys/cpupart.h>
78 #include <sys/sysdc.h>
83 #include "zfs_comutil.h"
85 typedef enum zti_modes
{
86 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
87 ZTI_MODE_BATCH
, /* cpu-intensive; value is ignored */
88 ZTI_MODE_NULL
, /* don't create a taskq */
92 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
93 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
94 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
95 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
97 #define ZTI_N(n) ZTI_P(n, 1)
98 #define ZTI_ONE ZTI_N(1)
100 typedef struct zio_taskq_info
{
101 zti_modes_t zti_mode
;
106 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
107 "iss", "iss_h", "int", "int_h"
111 * This table defines the taskq settings for each ZFS I/O type. When
112 * initializing a pool, we use this table to create an appropriately sized
113 * taskq. Some operations are low volume and therefore have a small, static
114 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
115 * macros. Other operations process a large amount of data; the ZTI_BATCH
116 * macro causes us to create a taskq oriented for throughput. Some operations
117 * are so high frequency and short-lived that the taskq itself can become a a
118 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
119 * additional degree of parallelism specified by the number of threads per-
120 * taskq and the number of taskqs; when dispatching an event in this case, the
121 * particular taskq is chosen at random.
123 * The different taskq priorities are to handle the different contexts (issue
124 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
125 * need to be handled with minimum delay.
127 const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
128 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
129 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
130 { ZTI_N(8), ZTI_NULL
, ZTI_BATCH
, ZTI_NULL
}, /* READ */
131 { ZTI_BATCH
, ZTI_N(5), ZTI_N(16), ZTI_N(5) }, /* WRITE */
132 { ZTI_P(4, 8), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
133 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
134 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
137 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
138 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
139 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
140 static inline int spa_load_impl(spa_t
*spa
, uint64_t, nvlist_t
*config
,
141 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
143 static void spa_vdev_resilver_done(spa_t
*spa
);
145 uint_t zio_taskq_batch_pct
= 75; /* 1 thread per cpu in pset */
146 id_t zio_taskq_psrset_bind
= PS_NONE
;
147 boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
148 uint_t zio_taskq_basedc
= 80; /* base duty cycle */
150 boolean_t spa_create_process
= B_TRUE
; /* no process ==> no sysdc */
153 * This (illegal) pool name is used when temporarily importing a spa_t in order
154 * to get the vdev stats associated with the imported devices.
156 #define TRYIMPORT_NAME "$import"
159 * ==========================================================================
160 * SPA properties routines
161 * ==========================================================================
165 * Add a (source=src, propname=propval) list to an nvlist.
168 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, char *strval
,
169 uint64_t intval
, zprop_source_t src
)
171 const char *propname
= zpool_prop_to_name(prop
);
174 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
175 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
178 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
180 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, intval
) == 0);
182 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
183 nvlist_free(propval
);
187 * Get property values from the spa configuration.
190 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
192 vdev_t
*rvd
= spa
->spa_root_vdev
;
193 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
194 uint64_t size
, alloc
, cap
, version
;
195 zprop_source_t src
= ZPROP_SRC_NONE
;
196 spa_config_dirent_t
*dp
;
197 metaslab_class_t
*mc
= spa_normal_class(spa
);
199 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
202 alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
203 size
= metaslab_class_get_space(spa_normal_class(spa
));
204 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
205 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
206 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
207 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
210 spa_prop_add_list(*nvp
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
211 metaslab_class_fragmentation(mc
), src
);
212 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
,
213 metaslab_class_expandable_space(mc
), src
);
214 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
215 (spa_mode(spa
) == FREAD
), src
);
217 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
218 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
220 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
221 ddt_get_pool_dedup_ratio(spa
), src
);
223 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
224 rvd
->vdev_state
, src
);
226 version
= spa_version(spa
);
227 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
))
228 src
= ZPROP_SRC_DEFAULT
;
230 src
= ZPROP_SRC_LOCAL
;
231 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
, version
, src
);
236 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
237 * when opening pools before this version freedir will be NULL.
239 if (pool
->dp_free_dir
!= NULL
) {
240 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
241 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
244 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
248 if (pool
->dp_leak_dir
!= NULL
) {
249 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
250 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
253 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
258 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
260 if (spa
->spa_comment
!= NULL
) {
261 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
265 if (spa
->spa_root
!= NULL
)
266 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
269 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
270 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
271 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
273 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
274 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
277 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
278 if (dp
->scd_path
== NULL
) {
279 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
280 "none", 0, ZPROP_SRC_LOCAL
);
281 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
282 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
283 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
289 * Get zpool property values.
292 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
294 objset_t
*mos
= spa
->spa_meta_objset
;
299 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
);
303 mutex_enter(&spa
->spa_props_lock
);
306 * Get properties from the spa config.
308 spa_prop_get_config(spa
, nvp
);
310 /* If no pool property object, no more prop to get. */
311 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0) {
312 mutex_exit(&spa
->spa_props_lock
);
317 * Get properties from the MOS pool property object.
319 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
320 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
321 zap_cursor_advance(&zc
)) {
324 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
327 if ((prop
= zpool_name_to_prop(za
.za_name
)) == ZPROP_INVAL
)
330 switch (za
.za_integer_length
) {
332 /* integer property */
333 if (za
.za_first_integer
!=
334 zpool_prop_default_numeric(prop
))
335 src
= ZPROP_SRC_LOCAL
;
337 if (prop
== ZPOOL_PROP_BOOTFS
) {
339 dsl_dataset_t
*ds
= NULL
;
341 dp
= spa_get_dsl(spa
);
342 dsl_pool_config_enter(dp
, FTAG
);
343 if ((err
= dsl_dataset_hold_obj(dp
,
344 za
.za_first_integer
, FTAG
, &ds
))) {
345 dsl_pool_config_exit(dp
, FTAG
);
350 MAXNAMELEN
+ strlen(MOS_DIR_NAME
) + 1,
352 dsl_dataset_name(ds
, strval
);
353 dsl_dataset_rele(ds
, FTAG
);
354 dsl_pool_config_exit(dp
, FTAG
);
357 intval
= za
.za_first_integer
;
360 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
364 MAXNAMELEN
+ strlen(MOS_DIR_NAME
) + 1);
369 /* string property */
370 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
371 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
372 za
.za_name
, 1, za
.za_num_integers
, strval
);
374 kmem_free(strval
, za
.za_num_integers
);
377 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
378 kmem_free(strval
, za
.za_num_integers
);
385 zap_cursor_fini(&zc
);
386 mutex_exit(&spa
->spa_props_lock
);
388 if (err
&& err
!= ENOENT
) {
398 * Validate the given pool properties nvlist and modify the list
399 * for the property values to be set.
402 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
405 int error
= 0, reset_bootfs
= 0;
407 boolean_t has_feature
= B_FALSE
;
410 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
412 char *strval
, *slash
, *check
, *fname
;
413 const char *propname
= nvpair_name(elem
);
414 zpool_prop_t prop
= zpool_name_to_prop(propname
);
418 if (!zpool_prop_feature(propname
)) {
419 error
= SET_ERROR(EINVAL
);
424 * Sanitize the input.
426 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
427 error
= SET_ERROR(EINVAL
);
431 if (nvpair_value_uint64(elem
, &intval
) != 0) {
432 error
= SET_ERROR(EINVAL
);
437 error
= SET_ERROR(EINVAL
);
441 fname
= strchr(propname
, '@') + 1;
442 if (zfeature_lookup_name(fname
, NULL
) != 0) {
443 error
= SET_ERROR(EINVAL
);
447 has_feature
= B_TRUE
;
450 case ZPOOL_PROP_VERSION
:
451 error
= nvpair_value_uint64(elem
, &intval
);
453 (intval
< spa_version(spa
) ||
454 intval
> SPA_VERSION_BEFORE_FEATURES
||
456 error
= SET_ERROR(EINVAL
);
459 case ZPOOL_PROP_DELEGATION
:
460 case ZPOOL_PROP_AUTOREPLACE
:
461 case ZPOOL_PROP_LISTSNAPS
:
462 case ZPOOL_PROP_AUTOEXPAND
:
463 error
= nvpair_value_uint64(elem
, &intval
);
464 if (!error
&& intval
> 1)
465 error
= SET_ERROR(EINVAL
);
468 case ZPOOL_PROP_BOOTFS
:
470 * If the pool version is less than SPA_VERSION_BOOTFS,
471 * or the pool is still being created (version == 0),
472 * the bootfs property cannot be set.
474 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
475 error
= SET_ERROR(ENOTSUP
);
480 * Make sure the vdev config is bootable
482 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
483 error
= SET_ERROR(ENOTSUP
);
489 error
= nvpair_value_string(elem
, &strval
);
495 if (strval
== NULL
|| strval
[0] == '\0') {
496 objnum
= zpool_prop_default_numeric(
501 error
= dmu_objset_hold(strval
, FTAG
, &os
);
506 * Must be ZPL, and its property settings
507 * must be supported by GRUB (compression
508 * is not gzip, and large blocks are not used).
511 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
512 error
= SET_ERROR(ENOTSUP
);
514 dsl_prop_get_int_ds(dmu_objset_ds(os
),
515 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
517 !BOOTFS_COMPRESS_VALID(propval
)) {
518 error
= SET_ERROR(ENOTSUP
);
520 dsl_prop_get_int_ds(dmu_objset_ds(os
),
521 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
),
523 propval
> SPA_OLD_MAXBLOCKSIZE
) {
524 error
= SET_ERROR(ENOTSUP
);
526 objnum
= dmu_objset_id(os
);
528 dmu_objset_rele(os
, FTAG
);
532 case ZPOOL_PROP_FAILUREMODE
:
533 error
= nvpair_value_uint64(elem
, &intval
);
534 if (!error
&& (intval
< ZIO_FAILURE_MODE_WAIT
||
535 intval
> ZIO_FAILURE_MODE_PANIC
))
536 error
= SET_ERROR(EINVAL
);
539 * This is a special case which only occurs when
540 * the pool has completely failed. This allows
541 * the user to change the in-core failmode property
542 * without syncing it out to disk (I/Os might
543 * currently be blocked). We do this by returning
544 * EIO to the caller (spa_prop_set) to trick it
545 * into thinking we encountered a property validation
548 if (!error
&& spa_suspended(spa
)) {
549 spa
->spa_failmode
= intval
;
550 error
= SET_ERROR(EIO
);
554 case ZPOOL_PROP_CACHEFILE
:
555 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
558 if (strval
[0] == '\0')
561 if (strcmp(strval
, "none") == 0)
564 if (strval
[0] != '/') {
565 error
= SET_ERROR(EINVAL
);
569 slash
= strrchr(strval
, '/');
570 ASSERT(slash
!= NULL
);
572 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
573 strcmp(slash
, "/..") == 0)
574 error
= SET_ERROR(EINVAL
);
577 case ZPOOL_PROP_COMMENT
:
578 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
580 for (check
= strval
; *check
!= '\0'; check
++) {
581 if (!isprint(*check
)) {
582 error
= SET_ERROR(EINVAL
);
587 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
588 error
= SET_ERROR(E2BIG
);
591 case ZPOOL_PROP_DEDUPDITTO
:
592 if (spa_version(spa
) < SPA_VERSION_DEDUP
)
593 error
= SET_ERROR(ENOTSUP
);
595 error
= nvpair_value_uint64(elem
, &intval
);
597 intval
!= 0 && intval
< ZIO_DEDUPDITTO_MIN
)
598 error
= SET_ERROR(EINVAL
);
609 if (!error
&& reset_bootfs
) {
610 error
= nvlist_remove(props
,
611 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
614 error
= nvlist_add_uint64(props
,
615 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
623 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
626 spa_config_dirent_t
*dp
;
628 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
632 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
635 if (cachefile
[0] == '\0')
636 dp
->scd_path
= spa_strdup(spa_config_path
);
637 else if (strcmp(cachefile
, "none") == 0)
640 dp
->scd_path
= spa_strdup(cachefile
);
642 list_insert_head(&spa
->spa_config_list
, dp
);
644 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
648 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
651 nvpair_t
*elem
= NULL
;
652 boolean_t need_sync
= B_FALSE
;
654 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
657 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
658 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
660 if (prop
== ZPOOL_PROP_CACHEFILE
||
661 prop
== ZPOOL_PROP_ALTROOT
||
662 prop
== ZPOOL_PROP_READONLY
)
665 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPROP_INVAL
) {
668 if (prop
== ZPOOL_PROP_VERSION
) {
669 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
671 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
672 ver
= SPA_VERSION_FEATURES
;
676 /* Save time if the version is already set. */
677 if (ver
== spa_version(spa
))
681 * In addition to the pool directory object, we might
682 * create the pool properties object, the features for
683 * read object, the features for write object, or the
684 * feature descriptions object.
686 error
= dsl_sync_task(spa
->spa_name
, NULL
,
687 spa_sync_version
, &ver
,
688 6, ZFS_SPACE_CHECK_RESERVED
);
699 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
700 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
707 * If the bootfs property value is dsobj, clear it.
710 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
712 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
713 VERIFY(zap_remove(spa
->spa_meta_objset
,
714 spa
->spa_pool_props_object
,
715 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
722 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
724 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
725 vdev_t
*rvd
= spa
->spa_root_vdev
;
727 ASSERTV(uint64_t *newguid
= arg
);
729 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
730 vdev_state
= rvd
->vdev_state
;
731 spa_config_exit(spa
, SCL_STATE
, FTAG
);
733 if (vdev_state
!= VDEV_STATE_HEALTHY
)
734 return (SET_ERROR(ENXIO
));
736 ASSERT3U(spa_guid(spa
), !=, *newguid
);
742 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
744 uint64_t *newguid
= arg
;
745 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
747 vdev_t
*rvd
= spa
->spa_root_vdev
;
749 oldguid
= spa_guid(spa
);
751 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
752 rvd
->vdev_guid
= *newguid
;
753 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
754 vdev_config_dirty(rvd
);
755 spa_config_exit(spa
, SCL_STATE
, FTAG
);
757 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
762 * Change the GUID for the pool. This is done so that we can later
763 * re-import a pool built from a clone of our own vdevs. We will modify
764 * the root vdev's guid, our own pool guid, and then mark all of our
765 * vdevs dirty. Note that we must make sure that all our vdevs are
766 * online when we do this, or else any vdevs that weren't present
767 * would be orphaned from our pool. We are also going to issue a
768 * sysevent to update any watchers.
771 spa_change_guid(spa_t
*spa
)
776 mutex_enter(&spa
->spa_vdev_top_lock
);
777 mutex_enter(&spa_namespace_lock
);
778 guid
= spa_generate_guid(NULL
);
780 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
781 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
784 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
785 spa_event_notify(spa
, NULL
, FM_EREPORT_ZFS_POOL_REGUID
);
788 mutex_exit(&spa_namespace_lock
);
789 mutex_exit(&spa
->spa_vdev_top_lock
);
795 * ==========================================================================
796 * SPA state manipulation (open/create/destroy/import/export)
797 * ==========================================================================
801 spa_error_entry_compare(const void *a
, const void *b
)
803 spa_error_entry_t
*sa
= (spa_error_entry_t
*)a
;
804 spa_error_entry_t
*sb
= (spa_error_entry_t
*)b
;
807 ret
= bcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
808 sizeof (zbookmark_phys_t
));
819 * Utility function which retrieves copies of the current logs and
820 * re-initializes them in the process.
823 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
825 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
827 bcopy(&spa
->spa_errlist_last
, last
, sizeof (avl_tree_t
));
828 bcopy(&spa
->spa_errlist_scrub
, scrub
, sizeof (avl_tree_t
));
830 avl_create(&spa
->spa_errlist_scrub
,
831 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
832 offsetof(spa_error_entry_t
, se_avl
));
833 avl_create(&spa
->spa_errlist_last
,
834 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
835 offsetof(spa_error_entry_t
, se_avl
));
839 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
841 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
842 enum zti_modes mode
= ztip
->zti_mode
;
843 uint_t value
= ztip
->zti_value
;
844 uint_t count
= ztip
->zti_count
;
845 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
848 boolean_t batch
= B_FALSE
;
850 if (mode
== ZTI_MODE_NULL
) {
852 tqs
->stqs_taskq
= NULL
;
856 ASSERT3U(count
, >, 0);
858 tqs
->stqs_count
= count
;
859 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
863 ASSERT3U(value
, >=, 1);
864 value
= MAX(value
, 1);
869 flags
|= TASKQ_THREADS_CPU_PCT
;
870 value
= zio_taskq_batch_pct
;
874 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
876 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
880 for (i
= 0; i
< count
; i
++) {
884 (void) snprintf(name
, sizeof (name
), "%s_%s_%u",
885 zio_type_name
[t
], zio_taskq_types
[q
], i
);
887 (void) snprintf(name
, sizeof (name
), "%s_%s",
888 zio_type_name
[t
], zio_taskq_types
[q
]);
891 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
893 flags
|= TASKQ_DC_BATCH
;
895 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
896 spa
->spa_proc
, zio_taskq_basedc
, flags
);
898 pri_t pri
= maxclsyspri
;
900 * The write issue taskq can be extremely CPU
901 * intensive. Run it at slightly lower priority
902 * than the other taskqs.
904 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
)
907 tq
= taskq_create_proc(name
, value
, pri
, 50,
908 INT_MAX
, spa
->spa_proc
, flags
);
911 tqs
->stqs_taskq
[i
] = tq
;
916 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
918 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
921 if (tqs
->stqs_taskq
== NULL
) {
922 ASSERT3U(tqs
->stqs_count
, ==, 0);
926 for (i
= 0; i
< tqs
->stqs_count
; i
++) {
927 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
928 taskq_destroy(tqs
->stqs_taskq
[i
]);
931 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
932 tqs
->stqs_taskq
= NULL
;
936 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
937 * Note that a type may have multiple discrete taskqs to avoid lock contention
938 * on the taskq itself. In that case we choose which taskq at random by using
939 * the low bits of gethrtime().
942 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
943 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
945 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
948 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
949 ASSERT3U(tqs
->stqs_count
, !=, 0);
951 if (tqs
->stqs_count
== 1) {
952 tq
= tqs
->stqs_taskq
[0];
954 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
957 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
961 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
964 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
965 task_func_t
*func
, void *arg
, uint_t flags
)
967 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
971 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
972 ASSERT3U(tqs
->stqs_count
, !=, 0);
974 if (tqs
->stqs_count
== 1) {
975 tq
= tqs
->stqs_taskq
[0];
977 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
980 id
= taskq_dispatch(tq
, func
, arg
, flags
);
982 taskq_wait_id(tq
, id
);
986 spa_create_zio_taskqs(spa_t
*spa
)
990 for (t
= 0; t
< ZIO_TYPES
; t
++) {
991 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
992 spa_taskqs_init(spa
, t
, q
);
997 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
999 spa_thread(void *arg
)
1001 callb_cpr_t cprinfo
;
1004 user_t
*pu
= PTOU(curproc
);
1006 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
1009 ASSERT(curproc
!= &p0
);
1010 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
1011 "zpool-%s", spa
->spa_name
);
1012 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
1014 /* bind this thread to the requested psrset */
1015 if (zio_taskq_psrset_bind
!= PS_NONE
) {
1017 mutex_enter(&cpu_lock
);
1018 mutex_enter(&pidlock
);
1019 mutex_enter(&curproc
->p_lock
);
1021 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1022 0, NULL
, NULL
) == 0) {
1023 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1026 "Couldn't bind process for zfs pool \"%s\" to "
1027 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1030 mutex_exit(&curproc
->p_lock
);
1031 mutex_exit(&pidlock
);
1032 mutex_exit(&cpu_lock
);
1036 if (zio_taskq_sysdc
) {
1037 sysdc_thread_enter(curthread
, 100, 0);
1040 spa
->spa_proc
= curproc
;
1041 spa
->spa_did
= curthread
->t_did
;
1043 spa_create_zio_taskqs(spa
);
1045 mutex_enter(&spa
->spa_proc_lock
);
1046 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1048 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1049 cv_broadcast(&spa
->spa_proc_cv
);
1051 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1052 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1053 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1054 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1056 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1057 spa
->spa_proc_state
= SPA_PROC_GONE
;
1058 spa
->spa_proc
= &p0
;
1059 cv_broadcast(&spa
->spa_proc_cv
);
1060 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1062 mutex_enter(&curproc
->p_lock
);
1068 * Activate an uninitialized pool.
1071 spa_activate(spa_t
*spa
, int mode
)
1073 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1075 spa
->spa_state
= POOL_STATE_ACTIVE
;
1076 spa
->spa_mode
= mode
;
1078 spa
->spa_normal_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1079 spa
->spa_log_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1081 /* Try to create a covering process */
1082 mutex_enter(&spa
->spa_proc_lock
);
1083 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1084 ASSERT(spa
->spa_proc
== &p0
);
1087 #ifdef HAVE_SPA_THREAD
1088 /* Only create a process if we're going to be around a while. */
1089 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1090 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1092 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1093 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1094 cv_wait(&spa
->spa_proc_cv
,
1095 &spa
->spa_proc_lock
);
1097 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1098 ASSERT(spa
->spa_proc
!= &p0
);
1099 ASSERT(spa
->spa_did
!= 0);
1103 "Couldn't create process for zfs pool \"%s\"\n",
1108 #endif /* HAVE_SPA_THREAD */
1109 mutex_exit(&spa
->spa_proc_lock
);
1111 /* If we didn't create a process, we need to create our taskqs. */
1112 if (spa
->spa_proc
== &p0
) {
1113 spa_create_zio_taskqs(spa
);
1116 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1117 offsetof(vdev_t
, vdev_config_dirty_node
));
1118 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1119 offsetof(objset_t
, os_evicting_node
));
1120 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1121 offsetof(vdev_t
, vdev_state_dirty_node
));
1123 txg_list_create(&spa
->spa_vdev_txg_list
,
1124 offsetof(struct vdev
, vdev_txg_node
));
1126 avl_create(&spa
->spa_errlist_scrub
,
1127 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1128 offsetof(spa_error_entry_t
, se_avl
));
1129 avl_create(&spa
->spa_errlist_last
,
1130 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1131 offsetof(spa_error_entry_t
, se_avl
));
1135 * Opposite of spa_activate().
1138 spa_deactivate(spa_t
*spa
)
1142 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1143 ASSERT(spa
->spa_dsl_pool
== NULL
);
1144 ASSERT(spa
->spa_root_vdev
== NULL
);
1145 ASSERT(spa
->spa_async_zio_root
== NULL
);
1146 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1148 spa_evicting_os_wait(spa
);
1150 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1152 list_destroy(&spa
->spa_config_dirty_list
);
1153 list_destroy(&spa
->spa_evicting_os_list
);
1154 list_destroy(&spa
->spa_state_dirty_list
);
1156 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
1158 for (t
= 0; t
< ZIO_TYPES
; t
++) {
1159 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1160 spa_taskqs_fini(spa
, t
, q
);
1164 metaslab_class_destroy(spa
->spa_normal_class
);
1165 spa
->spa_normal_class
= NULL
;
1167 metaslab_class_destroy(spa
->spa_log_class
);
1168 spa
->spa_log_class
= NULL
;
1171 * If this was part of an import or the open otherwise failed, we may
1172 * still have errors left in the queues. Empty them just in case.
1174 spa_errlog_drain(spa
);
1176 avl_destroy(&spa
->spa_errlist_scrub
);
1177 avl_destroy(&spa
->spa_errlist_last
);
1179 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1181 mutex_enter(&spa
->spa_proc_lock
);
1182 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1183 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1184 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1185 cv_broadcast(&spa
->spa_proc_cv
);
1186 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1187 ASSERT(spa
->spa_proc
!= &p0
);
1188 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1190 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1191 spa
->spa_proc_state
= SPA_PROC_NONE
;
1193 ASSERT(spa
->spa_proc
== &p0
);
1194 mutex_exit(&spa
->spa_proc_lock
);
1197 * We want to make sure spa_thread() has actually exited the ZFS
1198 * module, so that the module can't be unloaded out from underneath
1201 if (spa
->spa_did
!= 0) {
1202 thread_join(spa
->spa_did
);
1208 * Verify a pool configuration, and construct the vdev tree appropriately. This
1209 * will create all the necessary vdevs in the appropriate layout, with each vdev
1210 * in the CLOSED state. This will prep the pool before open/creation/import.
1211 * All vdev validation is done by the vdev_alloc() routine.
1214 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1215 uint_t id
, int atype
)
1222 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1225 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1228 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1231 if (error
== ENOENT
)
1237 return (SET_ERROR(EINVAL
));
1240 for (c
= 0; c
< children
; c
++) {
1242 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1250 ASSERT(*vdp
!= NULL
);
1256 * Opposite of spa_load().
1259 spa_unload(spa_t
*spa
)
1263 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1268 spa_async_suspend(spa
);
1273 if (spa
->spa_sync_on
) {
1274 txg_sync_stop(spa
->spa_dsl_pool
);
1275 spa
->spa_sync_on
= B_FALSE
;
1279 * Wait for any outstanding async I/O to complete.
1281 if (spa
->spa_async_zio_root
!= NULL
) {
1282 for (i
= 0; i
< max_ncpus
; i
++)
1283 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
1284 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
1285 spa
->spa_async_zio_root
= NULL
;
1288 bpobj_close(&spa
->spa_deferred_bpobj
);
1290 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1295 if (spa
->spa_root_vdev
)
1296 vdev_free(spa
->spa_root_vdev
);
1297 ASSERT(spa
->spa_root_vdev
== NULL
);
1300 * Close the dsl pool.
1302 if (spa
->spa_dsl_pool
) {
1303 dsl_pool_close(spa
->spa_dsl_pool
);
1304 spa
->spa_dsl_pool
= NULL
;
1305 spa
->spa_meta_objset
= NULL
;
1312 * Drop and purge level 2 cache
1314 spa_l2cache_drop(spa
);
1316 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1317 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1318 if (spa
->spa_spares
.sav_vdevs
) {
1319 kmem_free(spa
->spa_spares
.sav_vdevs
,
1320 spa
->spa_spares
.sav_count
* sizeof (void *));
1321 spa
->spa_spares
.sav_vdevs
= NULL
;
1323 if (spa
->spa_spares
.sav_config
) {
1324 nvlist_free(spa
->spa_spares
.sav_config
);
1325 spa
->spa_spares
.sav_config
= NULL
;
1327 spa
->spa_spares
.sav_count
= 0;
1329 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1330 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1331 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1333 if (spa
->spa_l2cache
.sav_vdevs
) {
1334 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1335 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1336 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1338 if (spa
->spa_l2cache
.sav_config
) {
1339 nvlist_free(spa
->spa_l2cache
.sav_config
);
1340 spa
->spa_l2cache
.sav_config
= NULL
;
1342 spa
->spa_l2cache
.sav_count
= 0;
1344 spa
->spa_async_suspended
= 0;
1346 if (spa
->spa_comment
!= NULL
) {
1347 spa_strfree(spa
->spa_comment
);
1348 spa
->spa_comment
= NULL
;
1351 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1355 * Load (or re-load) the current list of vdevs describing the active spares for
1356 * this pool. When this is called, we have some form of basic information in
1357 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1358 * then re-generate a more complete list including status information.
1361 spa_load_spares(spa_t
*spa
)
1368 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1371 * First, close and free any existing spare vdevs.
1373 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1374 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1376 /* Undo the call to spa_activate() below */
1377 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1378 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1379 spa_spare_remove(tvd
);
1384 if (spa
->spa_spares
.sav_vdevs
)
1385 kmem_free(spa
->spa_spares
.sav_vdevs
,
1386 spa
->spa_spares
.sav_count
* sizeof (void *));
1388 if (spa
->spa_spares
.sav_config
== NULL
)
1391 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1392 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
1394 spa
->spa_spares
.sav_count
= (int)nspares
;
1395 spa
->spa_spares
.sav_vdevs
= NULL
;
1401 * Construct the array of vdevs, opening them to get status in the
1402 * process. For each spare, there is potentially two different vdev_t
1403 * structures associated with it: one in the list of spares (used only
1404 * for basic validation purposes) and one in the active vdev
1405 * configuration (if it's spared in). During this phase we open and
1406 * validate each vdev on the spare list. If the vdev also exists in the
1407 * active configuration, then we also mark this vdev as an active spare.
1409 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
1411 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1412 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1413 VDEV_ALLOC_SPARE
) == 0);
1416 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1418 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1419 B_FALSE
)) != NULL
) {
1420 if (!tvd
->vdev_isspare
)
1424 * We only mark the spare active if we were successfully
1425 * able to load the vdev. Otherwise, importing a pool
1426 * with a bad active spare would result in strange
1427 * behavior, because multiple pool would think the spare
1428 * is actively in use.
1430 * There is a vulnerability here to an equally bizarre
1431 * circumstance, where a dead active spare is later
1432 * brought back to life (onlined or otherwise). Given
1433 * the rarity of this scenario, and the extra complexity
1434 * it adds, we ignore the possibility.
1436 if (!vdev_is_dead(tvd
))
1437 spa_spare_activate(tvd
);
1441 vd
->vdev_aux
= &spa
->spa_spares
;
1443 if (vdev_open(vd
) != 0)
1446 if (vdev_validate_aux(vd
) == 0)
1451 * Recompute the stashed list of spares, with status information
1454 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
,
1455 DATA_TYPE_NVLIST_ARRAY
) == 0);
1457 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1459 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1460 spares
[i
] = vdev_config_generate(spa
,
1461 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1462 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1463 ZPOOL_CONFIG_SPARES
, spares
, spa
->spa_spares
.sav_count
) == 0);
1464 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1465 nvlist_free(spares
[i
]);
1466 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1470 * Load (or re-load) the current list of vdevs describing the active l2cache for
1471 * this pool. When this is called, we have some form of basic information in
1472 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1473 * then re-generate a more complete list including status information.
1474 * Devices which are already active have their details maintained, and are
1478 spa_load_l2cache(spa_t
*spa
)
1482 int i
, j
, oldnvdevs
;
1484 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1485 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1487 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1489 if (sav
->sav_config
!= NULL
) {
1490 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
1491 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
1492 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
1498 oldvdevs
= sav
->sav_vdevs
;
1499 oldnvdevs
= sav
->sav_count
;
1500 sav
->sav_vdevs
= NULL
;
1504 * Process new nvlist of vdevs.
1506 for (i
= 0; i
< nl2cache
; i
++) {
1507 VERIFY(nvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
,
1511 for (j
= 0; j
< oldnvdevs
; j
++) {
1513 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1515 * Retain previous vdev for add/remove ops.
1523 if (newvdevs
[i
] == NULL
) {
1527 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1528 VDEV_ALLOC_L2CACHE
) == 0);
1533 * Commit this vdev as an l2cache device,
1534 * even if it fails to open.
1536 spa_l2cache_add(vd
);
1541 spa_l2cache_activate(vd
);
1543 if (vdev_open(vd
) != 0)
1546 (void) vdev_validate_aux(vd
);
1548 if (!vdev_is_dead(vd
))
1549 l2arc_add_vdev(spa
, vd
);
1554 * Purge vdevs that were dropped
1556 for (i
= 0; i
< oldnvdevs
; i
++) {
1561 ASSERT(vd
->vdev_isl2cache
);
1563 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
1564 pool
!= 0ULL && l2arc_vdev_present(vd
))
1565 l2arc_remove_vdev(vd
);
1566 vdev_clear_stats(vd
);
1572 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
1574 if (sav
->sav_config
== NULL
)
1577 sav
->sav_vdevs
= newvdevs
;
1578 sav
->sav_count
= (int)nl2cache
;
1581 * Recompute the stashed list of l2cache devices, with status
1582 * information this time.
1584 VERIFY(nvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
1585 DATA_TYPE_NVLIST_ARRAY
) == 0);
1587 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *), KM_SLEEP
);
1588 for (i
= 0; i
< sav
->sav_count
; i
++)
1589 l2cache
[i
] = vdev_config_generate(spa
,
1590 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
1591 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
1592 ZPOOL_CONFIG_L2CACHE
, l2cache
, sav
->sav_count
) == 0);
1594 for (i
= 0; i
< sav
->sav_count
; i
++)
1595 nvlist_free(l2cache
[i
]);
1597 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
1601 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
1604 char *packed
= NULL
;
1609 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
1613 nvsize
= *(uint64_t *)db
->db_data
;
1614 dmu_buf_rele(db
, FTAG
);
1616 packed
= vmem_alloc(nvsize
, KM_SLEEP
);
1617 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
1620 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
1621 vmem_free(packed
, nvsize
);
1627 * Checks to see if the given vdev could not be opened, in which case we post a
1628 * sysevent to notify the autoreplace code that the device has been removed.
1631 spa_check_removed(vdev_t
*vd
)
1635 for (c
= 0; c
< vd
->vdev_children
; c
++)
1636 spa_check_removed(vd
->vdev_child
[c
]);
1638 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
1640 zfs_ereport_post(FM_EREPORT_RESOURCE_AUTOREPLACE
,
1641 vd
->vdev_spa
, vd
, NULL
, 0, 0);
1642 spa_event_notify(vd
->vdev_spa
, vd
, FM_EREPORT_ZFS_DEVICE_CHECK
);
1647 * Validate the current config against the MOS config
1650 spa_config_valid(spa_t
*spa
, nvlist_t
*config
)
1652 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
1656 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0);
1658 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1659 VERIFY(spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
) == 0);
1661 ASSERT3U(rvd
->vdev_children
, ==, mrvd
->vdev_children
);
1664 * If we're doing a normal import, then build up any additional
1665 * diagnostic information about missing devices in this config.
1666 * We'll pass this up to the user for further processing.
1668 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
1669 nvlist_t
**child
, *nv
;
1672 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
**),
1674 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1676 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1677 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1678 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1680 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1681 mtvd
->vdev_ops
!= &vdev_missing_ops
&&
1683 child
[idx
++] = vdev_config_generate(spa
, mtvd
,
1688 VERIFY(nvlist_add_nvlist_array(nv
,
1689 ZPOOL_CONFIG_CHILDREN
, child
, idx
) == 0);
1690 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
1691 ZPOOL_CONFIG_MISSING_DEVICES
, nv
) == 0);
1693 for (i
= 0; i
< idx
; i
++)
1694 nvlist_free(child
[i
]);
1697 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
1701 * Compare the root vdev tree with the information we have
1702 * from the MOS config (mrvd). Check each top-level vdev
1703 * with the corresponding MOS config top-level (mtvd).
1705 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1706 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1707 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1710 * Resolve any "missing" vdevs in the current configuration.
1711 * If we find that the MOS config has more accurate information
1712 * about the top-level vdev then use that vdev instead.
1714 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1715 mtvd
->vdev_ops
!= &vdev_missing_ops
) {
1717 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
))
1721 * Device specific actions.
1723 if (mtvd
->vdev_islog
) {
1724 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
1727 * XXX - once we have 'readonly' pool
1728 * support we should be able to handle
1729 * missing data devices by transitioning
1730 * the pool to readonly.
1736 * Swap the missing vdev with the data we were
1737 * able to obtain from the MOS config.
1739 vdev_remove_child(rvd
, tvd
);
1740 vdev_remove_child(mrvd
, mtvd
);
1742 vdev_add_child(rvd
, mtvd
);
1743 vdev_add_child(mrvd
, tvd
);
1745 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1747 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1750 } else if (mtvd
->vdev_islog
) {
1752 * Load the slog device's state from the MOS config
1753 * since it's possible that the label does not
1754 * contain the most up-to-date information.
1756 vdev_load_log_state(tvd
, mtvd
);
1761 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1764 * Ensure we were able to validate the config.
1766 return (rvd
->vdev_guid_sum
== spa
->spa_uberblock
.ub_guid_sum
);
1770 * Check for missing log devices
1773 spa_check_logs(spa_t
*spa
)
1775 boolean_t rv
= B_FALSE
;
1776 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1778 switch (spa
->spa_log_state
) {
1781 case SPA_LOG_MISSING
:
1782 /* need to recheck in case slog has been restored */
1783 case SPA_LOG_UNKNOWN
:
1784 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1785 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
1787 spa_set_log_state(spa
, SPA_LOG_MISSING
);
1794 spa_passivate_log(spa_t
*spa
)
1796 vdev_t
*rvd
= spa
->spa_root_vdev
;
1797 boolean_t slog_found
= B_FALSE
;
1800 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1802 if (!spa_has_slogs(spa
))
1805 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1806 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1807 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1809 if (tvd
->vdev_islog
) {
1810 metaslab_group_passivate(mg
);
1811 slog_found
= B_TRUE
;
1815 return (slog_found
);
1819 spa_activate_log(spa_t
*spa
)
1821 vdev_t
*rvd
= spa
->spa_root_vdev
;
1824 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1826 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1827 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1828 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1830 if (tvd
->vdev_islog
)
1831 metaslab_group_activate(mg
);
1836 spa_offline_log(spa_t
*spa
)
1840 error
= dmu_objset_find(spa_name(spa
), zil_vdev_offline
,
1841 NULL
, DS_FIND_CHILDREN
);
1844 * We successfully offlined the log device, sync out the
1845 * current txg so that the "stubby" block can be removed
1848 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1854 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
1858 for (i
= 0; i
< sav
->sav_count
; i
++)
1859 spa_check_removed(sav
->sav_vdevs
[i
]);
1863 spa_claim_notify(zio_t
*zio
)
1865 spa_t
*spa
= zio
->io_spa
;
1870 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
1871 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
1872 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
1873 mutex_exit(&spa
->spa_props_lock
);
1876 typedef struct spa_load_error
{
1877 uint64_t sle_meta_count
;
1878 uint64_t sle_data_count
;
1882 spa_load_verify_done(zio_t
*zio
)
1884 blkptr_t
*bp
= zio
->io_bp
;
1885 spa_load_error_t
*sle
= zio
->io_private
;
1886 dmu_object_type_t type
= BP_GET_TYPE(bp
);
1887 int error
= zio
->io_error
;
1888 spa_t
*spa
= zio
->io_spa
;
1891 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
1892 type
!= DMU_OT_INTENT_LOG
)
1893 atomic_add_64(&sle
->sle_meta_count
, 1);
1895 atomic_add_64(&sle
->sle_data_count
, 1);
1897 zio_data_buf_free(zio
->io_data
, zio
->io_size
);
1899 mutex_enter(&spa
->spa_scrub_lock
);
1900 spa
->spa_scrub_inflight
--;
1901 cv_broadcast(&spa
->spa_scrub_io_cv
);
1902 mutex_exit(&spa
->spa_scrub_lock
);
1906 * Maximum number of concurrent scrub i/os to create while verifying
1907 * a pool while importing it.
1909 int spa_load_verify_maxinflight
= 10000;
1910 int spa_load_verify_metadata
= B_TRUE
;
1911 int spa_load_verify_data
= B_TRUE
;
1915 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
1916 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
1922 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
1925 * Note: normally this routine will not be called if
1926 * spa_load_verify_metadata is not set. However, it may be useful
1927 * to manually set the flag after the traversal has begun.
1929 if (!spa_load_verify_metadata
)
1931 if (BP_GET_BUFC_TYPE(bp
) == ARC_BUFC_DATA
&& !spa_load_verify_data
)
1935 size
= BP_GET_PSIZE(bp
);
1936 data
= zio_data_buf_alloc(size
);
1938 mutex_enter(&spa
->spa_scrub_lock
);
1939 while (spa
->spa_scrub_inflight
>= spa_load_verify_maxinflight
)
1940 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
1941 spa
->spa_scrub_inflight
++;
1942 mutex_exit(&spa
->spa_scrub_lock
);
1944 zio_nowait(zio_read(rio
, spa
, bp
, data
, size
,
1945 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
1946 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
1947 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
1952 spa_load_verify(spa_t
*spa
)
1955 spa_load_error_t sle
= { 0 };
1956 zpool_rewind_policy_t policy
;
1957 boolean_t verify_ok
= B_FALSE
;
1960 zpool_get_rewind_policy(spa
->spa_config
, &policy
);
1962 if (policy
.zrp_request
& ZPOOL_NEVER_REWIND
)
1965 rio
= zio_root(spa
, NULL
, &sle
,
1966 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
1968 if (spa_load_verify_metadata
) {
1969 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
1970 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
,
1971 spa_load_verify_cb
, rio
);
1974 (void) zio_wait(rio
);
1976 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
1977 spa
->spa_load_data_errors
= sle
.sle_data_count
;
1979 if (!error
&& sle
.sle_meta_count
<= policy
.zrp_maxmeta
&&
1980 sle
.sle_data_count
<= policy
.zrp_maxdata
) {
1984 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
1985 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
1987 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
1988 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
1989 ZPOOL_CONFIG_LOAD_TIME
, spa
->spa_load_txg_ts
) == 0);
1990 VERIFY(nvlist_add_int64(spa
->spa_load_info
,
1991 ZPOOL_CONFIG_REWIND_TIME
, loss
) == 0);
1992 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
1993 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
) == 0);
1995 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
1999 if (error
!= ENXIO
&& error
!= EIO
)
2000 error
= SET_ERROR(EIO
);
2004 return (verify_ok
? 0 : EIO
);
2008 * Find a value in the pool props object.
2011 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2013 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2014 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2018 * Find a value in the pool directory object.
2021 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
)
2023 return (zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2024 name
, sizeof (uint64_t), 1, val
));
2028 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2030 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2035 * Fix up config after a partly-completed split. This is done with the
2036 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2037 * pool have that entry in their config, but only the splitting one contains
2038 * a list of all the guids of the vdevs that are being split off.
2040 * This function determines what to do with that list: either rejoin
2041 * all the disks to the pool, or complete the splitting process. To attempt
2042 * the rejoin, each disk that is offlined is marked online again, and
2043 * we do a reopen() call. If the vdev label for every disk that was
2044 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2045 * then we call vdev_split() on each disk, and complete the split.
2047 * Otherwise we leave the config alone, with all the vdevs in place in
2048 * the original pool.
2051 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
2058 boolean_t attempt_reopen
;
2060 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
2063 /* check that the config is complete */
2064 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
2065 &glist
, &gcount
) != 0)
2068 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
2070 /* attempt to online all the vdevs & validate */
2071 attempt_reopen
= B_TRUE
;
2072 for (i
= 0; i
< gcount
; i
++) {
2073 if (glist
[i
] == 0) /* vdev is hole */
2076 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
2077 if (vd
[i
] == NULL
) {
2079 * Don't bother attempting to reopen the disks;
2080 * just do the split.
2082 attempt_reopen
= B_FALSE
;
2084 /* attempt to re-online it */
2085 vd
[i
]->vdev_offline
= B_FALSE
;
2089 if (attempt_reopen
) {
2090 vdev_reopen(spa
->spa_root_vdev
);
2092 /* check each device to see what state it's in */
2093 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
2094 if (vd
[i
] != NULL
&&
2095 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
2102 * If every disk has been moved to the new pool, or if we never
2103 * even attempted to look at them, then we split them off for
2106 if (!attempt_reopen
|| gcount
== extracted
) {
2107 for (i
= 0; i
< gcount
; i
++)
2110 vdev_reopen(spa
->spa_root_vdev
);
2113 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
2117 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
,
2118 boolean_t mosconfig
)
2120 nvlist_t
*config
= spa
->spa_config
;
2121 char *ereport
= FM_EREPORT_ZFS_POOL
;
2127 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
))
2128 return (SET_ERROR(EINVAL
));
2130 ASSERT(spa
->spa_comment
== NULL
);
2131 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2132 spa
->spa_comment
= spa_strdup(comment
);
2135 * Versioning wasn't explicitly added to the label until later, so if
2136 * it's not present treat it as the initial version.
2138 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
2139 &spa
->spa_ubsync
.ub_version
) != 0)
2140 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
2142 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
2143 &spa
->spa_config_txg
);
2145 if ((state
== SPA_LOAD_IMPORT
|| state
== SPA_LOAD_TRYIMPORT
) &&
2146 spa_guid_exists(pool_guid
, 0)) {
2147 error
= SET_ERROR(EEXIST
);
2149 spa
->spa_config_guid
= pool_guid
;
2151 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
,
2153 VERIFY(nvlist_dup(nvl
, &spa
->spa_config_splitting
,
2157 nvlist_free(spa
->spa_load_info
);
2158 spa
->spa_load_info
= fnvlist_alloc();
2160 gethrestime(&spa
->spa_loaded_ts
);
2161 error
= spa_load_impl(spa
, pool_guid
, config
, state
, type
,
2162 mosconfig
, &ereport
);
2166 * Don't count references from objsets that are already closed
2167 * and are making their way through the eviction process.
2169 spa_evicting_os_wait(spa
);
2170 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
2172 if (error
!= EEXIST
) {
2173 spa
->spa_loaded_ts
.tv_sec
= 0;
2174 spa
->spa_loaded_ts
.tv_nsec
= 0;
2176 if (error
!= EBADF
) {
2177 zfs_ereport_post(ereport
, spa
, NULL
, NULL
, 0, 0);
2180 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
2187 * Load an existing storage pool, using the pool's builtin spa_config as a
2188 * source of configuration information.
2190 __attribute__((always_inline
))
2192 spa_load_impl(spa_t
*spa
, uint64_t pool_guid
, nvlist_t
*config
,
2193 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
2197 nvlist_t
*nvroot
= NULL
;
2200 uberblock_t
*ub
= &spa
->spa_uberblock
;
2201 uint64_t children
, config_cache_txg
= spa
->spa_config_txg
;
2202 int orig_mode
= spa
->spa_mode
;
2205 boolean_t missing_feat_write
= B_FALSE
;
2208 * If this is an untrusted config, access the pool in read-only mode.
2209 * This prevents things like resilvering recently removed devices.
2212 spa
->spa_mode
= FREAD
;
2214 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2216 spa
->spa_load_state
= state
;
2218 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvroot
))
2219 return (SET_ERROR(EINVAL
));
2221 parse
= (type
== SPA_IMPORT_EXISTING
?
2222 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
2225 * Create "The Godfather" zio to hold all async IOs
2227 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
2229 for (i
= 0; i
< max_ncpus
; i
++) {
2230 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
2231 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
2232 ZIO_FLAG_GODFATHER
);
2236 * Parse the configuration into a vdev tree. We explicitly set the
2237 * value that will be returned by spa_version() since parsing the
2238 * configuration requires knowing the version number.
2240 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2241 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, parse
);
2242 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2247 ASSERT(spa
->spa_root_vdev
== rvd
);
2249 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2250 ASSERT(spa_guid(spa
) == pool_guid
);
2254 * Try to open all vdevs, loading each label in the process.
2256 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2257 error
= vdev_open(rvd
);
2258 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2263 * We need to validate the vdev labels against the configuration that
2264 * we have in hand, which is dependent on the setting of mosconfig. If
2265 * mosconfig is true then we're validating the vdev labels based on
2266 * that config. Otherwise, we're validating against the cached config
2267 * (zpool.cache) that was read when we loaded the zfs module, and then
2268 * later we will recursively call spa_load() and validate against
2271 * If we're assembling a new pool that's been split off from an
2272 * existing pool, the labels haven't yet been updated so we skip
2273 * validation for now.
2275 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2276 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2277 error
= vdev_validate(rvd
, mosconfig
);
2278 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2283 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2284 return (SET_ERROR(ENXIO
));
2288 * Find the best uberblock.
2290 vdev_uberblock_load(rvd
, ub
, &label
);
2293 * If we weren't able to find a single valid uberblock, return failure.
2295 if (ub
->ub_txg
== 0) {
2297 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
2301 * If the pool has an unsupported version we can't open it.
2303 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
2305 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
2308 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2312 * If we weren't able to find what's necessary for reading the
2313 * MOS in the label, return failure.
2315 if (label
== NULL
|| nvlist_lookup_nvlist(label
,
2316 ZPOOL_CONFIG_FEATURES_FOR_READ
, &features
) != 0) {
2318 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2323 * Update our in-core representation with the definitive values
2326 nvlist_free(spa
->spa_label_features
);
2327 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
, 0) == 0);
2333 * Look through entries in the label nvlist's features_for_read. If
2334 * there is a feature listed there which we don't understand then we
2335 * cannot open a pool.
2337 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2338 nvlist_t
*unsup_feat
;
2341 VERIFY(nvlist_alloc(&unsup_feat
, NV_UNIQUE_NAME
, KM_SLEEP
) ==
2344 for (nvp
= nvlist_next_nvpair(spa
->spa_label_features
, NULL
);
2346 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
2347 if (!zfeature_is_supported(nvpair_name(nvp
))) {
2348 VERIFY(nvlist_add_string(unsup_feat
,
2349 nvpair_name(nvp
), "") == 0);
2353 if (!nvlist_empty(unsup_feat
)) {
2354 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
2355 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
) == 0);
2356 nvlist_free(unsup_feat
);
2357 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2361 nvlist_free(unsup_feat
);
2365 * If the vdev guid sum doesn't match the uberblock, we have an
2366 * incomplete configuration. We first check to see if the pool
2367 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2368 * If it is, defer the vdev_guid_sum check till later so we
2369 * can handle missing vdevs.
2371 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VDEV_CHILDREN
,
2372 &children
) != 0 && mosconfig
&& type
!= SPA_IMPORT_ASSEMBLE
&&
2373 rvd
->vdev_guid_sum
!= ub
->ub_guid_sum
)
2374 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
2376 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
2377 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2378 spa_try_repair(spa
, config
);
2379 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2380 nvlist_free(spa
->spa_config_splitting
);
2381 spa
->spa_config_splitting
= NULL
;
2385 * Initialize internal SPA structures.
2387 spa
->spa_state
= POOL_STATE_ACTIVE
;
2388 spa
->spa_ubsync
= spa
->spa_uberblock
;
2389 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
2390 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
2391 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
2392 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
2393 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
2394 spa
->spa_prev_software_version
= ub
->ub_software_version
;
2396 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
2398 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2399 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
2401 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
) != 0)
2402 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2404 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
2405 boolean_t missing_feat_read
= B_FALSE
;
2406 nvlist_t
*unsup_feat
, *enabled_feat
;
2409 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
2410 &spa
->spa_feat_for_read_obj
) != 0) {
2411 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2414 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
2415 &spa
->spa_feat_for_write_obj
) != 0) {
2416 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2419 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
2420 &spa
->spa_feat_desc_obj
) != 0) {
2421 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2424 enabled_feat
= fnvlist_alloc();
2425 unsup_feat
= fnvlist_alloc();
2427 if (!spa_features_check(spa
, B_FALSE
,
2428 unsup_feat
, enabled_feat
))
2429 missing_feat_read
= B_TRUE
;
2431 if (spa_writeable(spa
) || state
== SPA_LOAD_TRYIMPORT
) {
2432 if (!spa_features_check(spa
, B_TRUE
,
2433 unsup_feat
, enabled_feat
)) {
2434 missing_feat_write
= B_TRUE
;
2438 fnvlist_add_nvlist(spa
->spa_load_info
,
2439 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
2441 if (!nvlist_empty(unsup_feat
)) {
2442 fnvlist_add_nvlist(spa
->spa_load_info
,
2443 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
2446 fnvlist_free(enabled_feat
);
2447 fnvlist_free(unsup_feat
);
2449 if (!missing_feat_read
) {
2450 fnvlist_add_boolean(spa
->spa_load_info
,
2451 ZPOOL_CONFIG_CAN_RDONLY
);
2455 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2456 * twofold: to determine whether the pool is available for
2457 * import in read-write mode and (if it is not) whether the
2458 * pool is available for import in read-only mode. If the pool
2459 * is available for import in read-write mode, it is displayed
2460 * as available in userland; if it is not available for import
2461 * in read-only mode, it is displayed as unavailable in
2462 * userland. If the pool is available for import in read-only
2463 * mode but not read-write mode, it is displayed as unavailable
2464 * in userland with a special note that the pool is actually
2465 * available for open in read-only mode.
2467 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2468 * missing a feature for write, we must first determine whether
2469 * the pool can be opened read-only before returning to
2470 * userland in order to know whether to display the
2471 * abovementioned note.
2473 if (missing_feat_read
|| (missing_feat_write
&&
2474 spa_writeable(spa
))) {
2475 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2480 * Load refcounts for ZFS features from disk into an in-memory
2481 * cache during SPA initialization.
2483 for (i
= 0; i
< SPA_FEATURES
; i
++) {
2486 error
= feature_get_refcount_from_disk(spa
,
2487 &spa_feature_table
[i
], &refcount
);
2489 spa
->spa_feat_refcount_cache
[i
] = refcount
;
2490 } else if (error
== ENOTSUP
) {
2491 spa
->spa_feat_refcount_cache
[i
] =
2492 SPA_FEATURE_DISABLED
;
2494 return (spa_vdev_err(rvd
,
2495 VDEV_AUX_CORRUPT_DATA
, EIO
));
2500 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
2501 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
2502 &spa
->spa_feat_enabled_txg_obj
) != 0)
2503 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2506 spa
->spa_is_initializing
= B_TRUE
;
2507 error
= dsl_pool_open(spa
->spa_dsl_pool
);
2508 spa
->spa_is_initializing
= B_FALSE
;
2510 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2514 nvlist_t
*policy
= NULL
, *nvconfig
;
2516 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2517 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2519 if (!spa_is_root(spa
) && nvlist_lookup_uint64(nvconfig
,
2520 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
2522 unsigned long myhostid
= 0;
2524 VERIFY(nvlist_lookup_string(nvconfig
,
2525 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
2528 myhostid
= zone_get_hostid(NULL
);
2531 * We're emulating the system's hostid in userland, so
2532 * we can't use zone_get_hostid().
2534 (void) ddi_strtoul(hw_serial
, NULL
, 10, &myhostid
);
2535 #endif /* _KERNEL */
2536 if (hostid
!= 0 && myhostid
!= 0 &&
2537 hostid
!= myhostid
) {
2538 nvlist_free(nvconfig
);
2539 cmn_err(CE_WARN
, "pool '%s' could not be "
2540 "loaded as it was last accessed by another "
2541 "system (host: %s hostid: 0x%lx). See: "
2542 "http://zfsonlinux.org/msg/ZFS-8000-EY",
2543 spa_name(spa
), hostname
,
2544 (unsigned long)hostid
);
2545 return (SET_ERROR(EBADF
));
2548 if (nvlist_lookup_nvlist(spa
->spa_config
,
2549 ZPOOL_REWIND_POLICY
, &policy
) == 0)
2550 VERIFY(nvlist_add_nvlist(nvconfig
,
2551 ZPOOL_REWIND_POLICY
, policy
) == 0);
2553 spa_config_set(spa
, nvconfig
);
2555 spa_deactivate(spa
);
2556 spa_activate(spa
, orig_mode
);
2558 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, B_TRUE
));
2561 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
) != 0)
2562 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2563 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
2565 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2568 * Load the bit that tells us to use the new accounting function
2569 * (raid-z deflation). If we have an older pool, this will not
2572 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
);
2573 if (error
!= 0 && error
!= ENOENT
)
2574 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2576 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
2577 &spa
->spa_creation_version
);
2578 if (error
!= 0 && error
!= ENOENT
)
2579 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2582 * Load the persistent error log. If we have an older pool, this will
2585 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
);
2586 if (error
!= 0 && error
!= ENOENT
)
2587 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2589 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
2590 &spa
->spa_errlog_scrub
);
2591 if (error
!= 0 && error
!= ENOENT
)
2592 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2595 * Load the history object. If we have an older pool, this
2596 * will not be present.
2598 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
);
2599 if (error
!= 0 && error
!= ENOENT
)
2600 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2603 * If we're assembling the pool from the split-off vdevs of
2604 * an existing pool, we don't want to attach the spares & cache
2609 * Load any hot spares for this pool.
2611 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
);
2612 if (error
!= 0 && error
!= ENOENT
)
2613 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2614 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2615 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
2616 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
2617 &spa
->spa_spares
.sav_config
) != 0)
2618 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2620 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2621 spa_load_spares(spa
);
2622 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2623 } else if (error
== 0) {
2624 spa
->spa_spares
.sav_sync
= B_TRUE
;
2628 * Load any level 2 ARC devices for this pool.
2630 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
2631 &spa
->spa_l2cache
.sav_object
);
2632 if (error
!= 0 && error
!= ENOENT
)
2633 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2634 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2635 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
2636 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
2637 &spa
->spa_l2cache
.sav_config
) != 0)
2638 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2640 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2641 spa_load_l2cache(spa
);
2642 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2643 } else if (error
== 0) {
2644 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
2647 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
2649 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
);
2650 if (error
&& error
!= ENOENT
)
2651 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2654 uint64_t autoreplace
= 0;
2656 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
2657 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
2658 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
2659 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
2660 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
2661 spa_prop_find(spa
, ZPOOL_PROP_DEDUPDITTO
,
2662 &spa
->spa_dedup_ditto
);
2664 spa
->spa_autoreplace
= (autoreplace
!= 0);
2668 * If the 'autoreplace' property is set, then post a resource notifying
2669 * the ZFS DE that it should not issue any faults for unopenable
2670 * devices. We also iterate over the vdevs, and post a sysevent for any
2671 * unopenable vdevs so that the normal autoreplace handler can take
2674 if (spa
->spa_autoreplace
&& state
!= SPA_LOAD_TRYIMPORT
) {
2675 spa_check_removed(spa
->spa_root_vdev
);
2677 * For the import case, this is done in spa_import(), because
2678 * at this point we're using the spare definitions from
2679 * the MOS config, not necessarily from the userland config.
2681 if (state
!= SPA_LOAD_IMPORT
) {
2682 spa_aux_check_removed(&spa
->spa_spares
);
2683 spa_aux_check_removed(&spa
->spa_l2cache
);
2688 * Load the vdev state for all toplevel vdevs.
2693 * Propagate the leaf DTLs we just loaded all the way up the tree.
2695 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2696 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
);
2697 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2700 * Load the DDTs (dedup tables).
2702 error
= ddt_load(spa
);
2704 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2706 spa_update_dspace(spa
);
2709 * Validate the config, using the MOS config to fill in any
2710 * information which might be missing. If we fail to validate
2711 * the config then declare the pool unfit for use. If we're
2712 * assembling a pool from a split, the log is not transferred
2715 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2718 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2719 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2721 if (!spa_config_valid(spa
, nvconfig
)) {
2722 nvlist_free(nvconfig
);
2723 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
2726 nvlist_free(nvconfig
);
2729 * Now that we've validated the config, check the state of the
2730 * root vdev. If it can't be opened, it indicates one or
2731 * more toplevel vdevs are faulted.
2733 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2734 return (SET_ERROR(ENXIO
));
2736 if (spa_writeable(spa
) && spa_check_logs(spa
)) {
2737 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
2738 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
, ENXIO
));
2742 if (missing_feat_write
) {
2743 ASSERT(state
== SPA_LOAD_TRYIMPORT
);
2746 * At this point, we know that we can open the pool in
2747 * read-only mode but not read-write mode. We now have enough
2748 * information and can return to userland.
2750 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
, ENOTSUP
));
2754 * We've successfully opened the pool, verify that we're ready
2755 * to start pushing transactions.
2757 if (state
!= SPA_LOAD_TRYIMPORT
) {
2758 if ((error
= spa_load_verify(spa
)))
2759 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2763 if (spa_writeable(spa
) && (state
== SPA_LOAD_RECOVER
||
2764 spa
->spa_load_max_txg
== UINT64_MAX
)) {
2766 int need_update
= B_FALSE
;
2767 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2770 ASSERT(state
!= SPA_LOAD_TRYIMPORT
);
2773 * Claim log blocks that haven't been committed yet.
2774 * This must all happen in a single txg.
2775 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2776 * invoked from zil_claim_log_block()'s i/o done callback.
2777 * Price of rollback is that we abandon the log.
2779 spa
->spa_claiming
= B_TRUE
;
2781 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
2782 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
2783 zil_claim
, tx
, DS_FIND_CHILDREN
);
2786 spa
->spa_claiming
= B_FALSE
;
2788 spa_set_log_state(spa
, SPA_LOG_GOOD
);
2789 spa
->spa_sync_on
= B_TRUE
;
2790 txg_sync_start(spa
->spa_dsl_pool
);
2793 * Wait for all claims to sync. We sync up to the highest
2794 * claimed log block birth time so that claimed log blocks
2795 * don't appear to be from the future. spa_claim_max_txg
2796 * will have been set for us by either zil_check_log_chain()
2797 * (invoked from spa_check_logs()) or zil_claim() above.
2799 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
2802 * If the config cache is stale, or we have uninitialized
2803 * metaslabs (see spa_vdev_add()), then update the config.
2805 * If this is a verbatim import, trust the current
2806 * in-core spa_config and update the disk labels.
2808 if (config_cache_txg
!= spa
->spa_config_txg
||
2809 state
== SPA_LOAD_IMPORT
||
2810 state
== SPA_LOAD_RECOVER
||
2811 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
2812 need_update
= B_TRUE
;
2814 for (c
= 0; c
< rvd
->vdev_children
; c
++)
2815 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
2816 need_update
= B_TRUE
;
2819 * Update the config cache asychronously in case we're the
2820 * root pool, in which case the config cache isn't writable yet.
2823 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
2826 * Check all DTLs to see if anything needs resilvering.
2828 if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
2829 vdev_resilver_needed(rvd
, NULL
, NULL
))
2830 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
2833 * Log the fact that we booted up (so that we can detect if
2834 * we rebooted in the middle of an operation).
2836 spa_history_log_version(spa
, "open");
2839 * Delete any inconsistent datasets.
2841 (void) dmu_objset_find(spa_name(spa
),
2842 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
2845 * Clean up any stale temporary dataset userrefs.
2847 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
2854 spa_load_retry(spa_t
*spa
, spa_load_state_t state
, int mosconfig
)
2856 int mode
= spa
->spa_mode
;
2859 spa_deactivate(spa
);
2861 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
2863 spa_activate(spa
, mode
);
2864 spa_async_suspend(spa
);
2866 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, mosconfig
));
2870 * If spa_load() fails this function will try loading prior txg's. If
2871 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2872 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2873 * function will not rewind the pool and will return the same error as
2877 spa_load_best(spa_t
*spa
, spa_load_state_t state
, int mosconfig
,
2878 uint64_t max_request
, int rewind_flags
)
2880 nvlist_t
*loadinfo
= NULL
;
2881 nvlist_t
*config
= NULL
;
2882 int load_error
, rewind_error
;
2883 uint64_t safe_rewind_txg
;
2886 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
2887 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
2888 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2890 spa
->spa_load_max_txg
= max_request
;
2891 if (max_request
!= UINT64_MAX
)
2892 spa
->spa_extreme_rewind
= B_TRUE
;
2895 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
,
2897 if (load_error
== 0)
2900 if (spa
->spa_root_vdev
!= NULL
)
2901 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
2903 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
2904 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2906 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
2907 nvlist_free(config
);
2908 return (load_error
);
2911 if (state
== SPA_LOAD_RECOVER
) {
2912 /* Price of rolling back is discarding txgs, including log */
2913 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2916 * If we aren't rolling back save the load info from our first
2917 * import attempt so that we can restore it after attempting
2920 loadinfo
= spa
->spa_load_info
;
2921 spa
->spa_load_info
= fnvlist_alloc();
2924 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
2925 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
2926 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
2927 TXG_INITIAL
: safe_rewind_txg
;
2930 * Continue as long as we're finding errors, we're still within
2931 * the acceptable rewind range, and we're still finding uberblocks
2933 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
2934 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
2935 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
2936 spa
->spa_extreme_rewind
= B_TRUE
;
2937 rewind_error
= spa_load_retry(spa
, state
, mosconfig
);
2940 spa
->spa_extreme_rewind
= B_FALSE
;
2941 spa
->spa_load_max_txg
= UINT64_MAX
;
2943 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
2944 spa_config_set(spa
, config
);
2946 if (state
== SPA_LOAD_RECOVER
) {
2947 ASSERT3P(loadinfo
, ==, NULL
);
2948 return (rewind_error
);
2950 /* Store the rewind info as part of the initial load info */
2951 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
2952 spa
->spa_load_info
);
2954 /* Restore the initial load info */
2955 fnvlist_free(spa
->spa_load_info
);
2956 spa
->spa_load_info
= loadinfo
;
2958 return (load_error
);
2965 * The import case is identical to an open except that the configuration is sent
2966 * down from userland, instead of grabbed from the configuration cache. For the
2967 * case of an open, the pool configuration will exist in the
2968 * POOL_STATE_UNINITIALIZED state.
2970 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2971 * the same time open the pool, without having to keep around the spa_t in some
2975 spa_open_common(const char *pool
, spa_t
**spapp
, void *tag
, nvlist_t
*nvpolicy
,
2979 spa_load_state_t state
= SPA_LOAD_OPEN
;
2981 int locked
= B_FALSE
;
2982 int firstopen
= B_FALSE
;
2987 * As disgusting as this is, we need to support recursive calls to this
2988 * function because dsl_dir_open() is called during spa_load(), and ends
2989 * up calling spa_open() again. The real fix is to figure out how to
2990 * avoid dsl_dir_open() calling this in the first place.
2992 if (mutex_owner(&spa_namespace_lock
) != curthread
) {
2993 mutex_enter(&spa_namespace_lock
);
2997 if ((spa
= spa_lookup(pool
)) == NULL
) {
2999 mutex_exit(&spa_namespace_lock
);
3000 return (SET_ERROR(ENOENT
));
3003 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
3004 zpool_rewind_policy_t policy
;
3008 zpool_get_rewind_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
3010 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
3011 state
= SPA_LOAD_RECOVER
;
3013 spa_activate(spa
, spa_mode_global
);
3015 if (state
!= SPA_LOAD_RECOVER
)
3016 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
3018 error
= spa_load_best(spa
, state
, B_FALSE
, policy
.zrp_txg
,
3019 policy
.zrp_request
);
3021 if (error
== EBADF
) {
3023 * If vdev_validate() returns failure (indicated by
3024 * EBADF), it indicates that one of the vdevs indicates
3025 * that the pool has been exported or destroyed. If
3026 * this is the case, the config cache is out of sync and
3027 * we should remove the pool from the namespace.
3030 spa_deactivate(spa
);
3031 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
3034 mutex_exit(&spa_namespace_lock
);
3035 return (SET_ERROR(ENOENT
));
3040 * We can't open the pool, but we still have useful
3041 * information: the state of each vdev after the
3042 * attempted vdev_open(). Return this to the user.
3044 if (config
!= NULL
&& spa
->spa_config
) {
3045 VERIFY(nvlist_dup(spa
->spa_config
, config
,
3047 VERIFY(nvlist_add_nvlist(*config
,
3048 ZPOOL_CONFIG_LOAD_INFO
,
3049 spa
->spa_load_info
) == 0);
3052 spa_deactivate(spa
);
3053 spa
->spa_last_open_failed
= error
;
3055 mutex_exit(&spa_namespace_lock
);
3061 spa_open_ref(spa
, tag
);
3064 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3067 * If we've recovered the pool, pass back any information we
3068 * gathered while doing the load.
3070 if (state
== SPA_LOAD_RECOVER
) {
3071 VERIFY(nvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
3072 spa
->spa_load_info
) == 0);
3076 spa
->spa_last_open_failed
= 0;
3077 spa
->spa_last_ubsync_txg
= 0;
3078 spa
->spa_load_txg
= 0;
3079 mutex_exit(&spa_namespace_lock
);
3084 zvol_create_minors(spa
->spa_name
);
3093 spa_open_rewind(const char *name
, spa_t
**spapp
, void *tag
, nvlist_t
*policy
,
3096 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
3100 spa_open(const char *name
, spa_t
**spapp
, void *tag
)
3102 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
3106 * Lookup the given spa_t, incrementing the inject count in the process,
3107 * preventing it from being exported or destroyed.
3110 spa_inject_addref(char *name
)
3114 mutex_enter(&spa_namespace_lock
);
3115 if ((spa
= spa_lookup(name
)) == NULL
) {
3116 mutex_exit(&spa_namespace_lock
);
3119 spa
->spa_inject_ref
++;
3120 mutex_exit(&spa_namespace_lock
);
3126 spa_inject_delref(spa_t
*spa
)
3128 mutex_enter(&spa_namespace_lock
);
3129 spa
->spa_inject_ref
--;
3130 mutex_exit(&spa_namespace_lock
);
3134 * Add spares device information to the nvlist.
3137 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
3147 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3149 if (spa
->spa_spares
.sav_count
== 0)
3152 VERIFY(nvlist_lookup_nvlist(config
,
3153 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3154 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
3155 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3157 VERIFY(nvlist_add_nvlist_array(nvroot
,
3158 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3159 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3160 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3163 * Go through and find any spares which have since been
3164 * repurposed as an active spare. If this is the case, update
3165 * their status appropriately.
3167 for (i
= 0; i
< nspares
; i
++) {
3168 VERIFY(nvlist_lookup_uint64(spares
[i
],
3169 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3170 if (spa_spare_exists(guid
, &pool
, NULL
) &&
3172 VERIFY(nvlist_lookup_uint64_array(
3173 spares
[i
], ZPOOL_CONFIG_VDEV_STATS
,
3174 (uint64_t **)&vs
, &vsc
) == 0);
3175 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
3176 vs
->vs_aux
= VDEV_AUX_SPARED
;
3183 * Add l2cache device information to the nvlist, including vdev stats.
3186 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
3189 uint_t i
, j
, nl2cache
;
3196 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3198 if (spa
->spa_l2cache
.sav_count
== 0)
3201 VERIFY(nvlist_lookup_nvlist(config
,
3202 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3203 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
3204 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3205 if (nl2cache
!= 0) {
3206 VERIFY(nvlist_add_nvlist_array(nvroot
,
3207 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3208 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3209 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3212 * Update level 2 cache device stats.
3215 for (i
= 0; i
< nl2cache
; i
++) {
3216 VERIFY(nvlist_lookup_uint64(l2cache
[i
],
3217 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3220 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
3222 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
3223 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
3229 VERIFY(nvlist_lookup_uint64_array(l2cache
[i
],
3230 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
3232 vdev_get_stats(vd
, vs
);
3238 spa_feature_stats_from_disk(spa_t
*spa
, nvlist_t
*features
)
3243 if (spa
->spa_feat_for_read_obj
!= 0) {
3244 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3245 spa
->spa_feat_for_read_obj
);
3246 zap_cursor_retrieve(&zc
, &za
) == 0;
3247 zap_cursor_advance(&zc
)) {
3248 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3249 za
.za_num_integers
== 1);
3250 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
3251 za
.za_first_integer
));
3253 zap_cursor_fini(&zc
);
3256 if (spa
->spa_feat_for_write_obj
!= 0) {
3257 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3258 spa
->spa_feat_for_write_obj
);
3259 zap_cursor_retrieve(&zc
, &za
) == 0;
3260 zap_cursor_advance(&zc
)) {
3261 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3262 za
.za_num_integers
== 1);
3263 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
3264 za
.za_first_integer
));
3266 zap_cursor_fini(&zc
);
3271 spa_feature_stats_from_cache(spa_t
*spa
, nvlist_t
*features
)
3275 for (i
= 0; i
< SPA_FEATURES
; i
++) {
3276 zfeature_info_t feature
= spa_feature_table
[i
];
3279 if (feature_get_refcount(spa
, &feature
, &refcount
) != 0)
3282 VERIFY0(nvlist_add_uint64(features
, feature
.fi_guid
, refcount
));
3287 * Store a list of pool features and their reference counts in the
3290 * The first time this is called on a spa, allocate a new nvlist, fetch
3291 * the pool features and reference counts from disk, then save the list
3292 * in the spa. In subsequent calls on the same spa use the saved nvlist
3293 * and refresh its values from the cached reference counts. This
3294 * ensures we don't block here on I/O on a suspended pool so 'zpool
3295 * clear' can resume the pool.
3298 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
3302 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3304 mutex_enter(&spa
->spa_feat_stats_lock
);
3305 features
= spa
->spa_feat_stats
;
3307 if (features
!= NULL
) {
3308 spa_feature_stats_from_cache(spa
, features
);
3310 VERIFY0(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
));
3311 spa
->spa_feat_stats
= features
;
3312 spa_feature_stats_from_disk(spa
, features
);
3315 VERIFY0(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
3318 mutex_exit(&spa
->spa_feat_stats_lock
);
3322 spa_get_stats(const char *name
, nvlist_t
**config
,
3323 char *altroot
, size_t buflen
)
3329 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
3333 * This still leaves a window of inconsistency where the spares
3334 * or l2cache devices could change and the config would be
3335 * self-inconsistent.
3337 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
3339 if (*config
!= NULL
) {
3340 uint64_t loadtimes
[2];
3342 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
3343 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
3344 VERIFY(nvlist_add_uint64_array(*config
,
3345 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2) == 0);
3347 VERIFY(nvlist_add_uint64(*config
,
3348 ZPOOL_CONFIG_ERRCOUNT
,
3349 spa_get_errlog_size(spa
)) == 0);
3351 if (spa_suspended(spa
))
3352 VERIFY(nvlist_add_uint64(*config
,
3353 ZPOOL_CONFIG_SUSPENDED
,
3354 spa
->spa_failmode
) == 0);
3356 spa_add_spares(spa
, *config
);
3357 spa_add_l2cache(spa
, *config
);
3358 spa_add_feature_stats(spa
, *config
);
3363 * We want to get the alternate root even for faulted pools, so we cheat
3364 * and call spa_lookup() directly.
3368 mutex_enter(&spa_namespace_lock
);
3369 spa
= spa_lookup(name
);
3371 spa_altroot(spa
, altroot
, buflen
);
3375 mutex_exit(&spa_namespace_lock
);
3377 spa_altroot(spa
, altroot
, buflen
);
3382 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
3383 spa_close(spa
, FTAG
);
3390 * Validate that the auxiliary device array is well formed. We must have an
3391 * array of nvlists, each which describes a valid leaf vdev. If this is an
3392 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3393 * specified, as long as they are well-formed.
3396 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
3397 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
3398 vdev_labeltype_t label
)
3405 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3408 * It's acceptable to have no devs specified.
3410 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
3414 return (SET_ERROR(EINVAL
));
3417 * Make sure the pool is formatted with a version that supports this
3420 if (spa_version(spa
) < version
)
3421 return (SET_ERROR(ENOTSUP
));
3424 * Set the pending device list so we correctly handle device in-use
3427 sav
->sav_pending
= dev
;
3428 sav
->sav_npending
= ndev
;
3430 for (i
= 0; i
< ndev
; i
++) {
3431 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
3435 if (!vd
->vdev_ops
->vdev_op_leaf
) {
3437 error
= SET_ERROR(EINVAL
);
3442 * The L2ARC currently only supports disk devices in
3443 * kernel context. For user-level testing, we allow it.
3446 if ((strcmp(config
, ZPOOL_CONFIG_L2CACHE
) == 0) &&
3447 strcmp(vd
->vdev_ops
->vdev_op_type
, VDEV_TYPE_DISK
) != 0) {
3448 error
= SET_ERROR(ENOTBLK
);
3455 if ((error
= vdev_open(vd
)) == 0 &&
3456 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
3457 VERIFY(nvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
3458 vd
->vdev_guid
) == 0);
3464 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
3471 sav
->sav_pending
= NULL
;
3472 sav
->sav_npending
= 0;
3477 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
3481 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3483 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3484 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
3485 VDEV_LABEL_SPARE
)) != 0) {
3489 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3490 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
3491 VDEV_LABEL_L2CACHE
));
3495 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
3500 if (sav
->sav_config
!= NULL
) {
3506 * Generate new dev list by concatentating with the
3509 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
3510 &olddevs
, &oldndevs
) == 0);
3512 newdevs
= kmem_alloc(sizeof (void *) *
3513 (ndevs
+ oldndevs
), KM_SLEEP
);
3514 for (i
= 0; i
< oldndevs
; i
++)
3515 VERIFY(nvlist_dup(olddevs
[i
], &newdevs
[i
],
3517 for (i
= 0; i
< ndevs
; i
++)
3518 VERIFY(nvlist_dup(devs
[i
], &newdevs
[i
+ oldndevs
],
3521 VERIFY(nvlist_remove(sav
->sav_config
, config
,
3522 DATA_TYPE_NVLIST_ARRAY
) == 0);
3524 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
3525 config
, newdevs
, ndevs
+ oldndevs
) == 0);
3526 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
3527 nvlist_free(newdevs
[i
]);
3528 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
3531 * Generate a new dev list.
3533 VERIFY(nvlist_alloc(&sav
->sav_config
, NV_UNIQUE_NAME
,
3535 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
, config
,
3541 * Stop and drop level 2 ARC devices
3544 spa_l2cache_drop(spa_t
*spa
)
3548 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
3550 for (i
= 0; i
< sav
->sav_count
; i
++) {
3553 vd
= sav
->sav_vdevs
[i
];
3556 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
3557 pool
!= 0ULL && l2arc_vdev_present(vd
))
3558 l2arc_remove_vdev(vd
);
3566 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
3570 char *altroot
= NULL
;
3575 uint64_t txg
= TXG_INITIAL
;
3576 nvlist_t
**spares
, **l2cache
;
3577 uint_t nspares
, nl2cache
;
3578 uint64_t version
, obj
;
3579 boolean_t has_features
;
3585 if (nvlist_lookup_string(props
, "tname", &poolname
) != 0)
3586 poolname
= (char *)pool
;
3589 * If this pool already exists, return failure.
3591 mutex_enter(&spa_namespace_lock
);
3592 if (spa_lookup(poolname
) != NULL
) {
3593 mutex_exit(&spa_namespace_lock
);
3594 return (SET_ERROR(EEXIST
));
3598 * Allocate a new spa_t structure.
3600 nvl
= fnvlist_alloc();
3601 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
3602 (void) nvlist_lookup_string(props
,
3603 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
3604 spa
= spa_add(poolname
, nvl
, altroot
);
3606 spa_activate(spa
, spa_mode_global
);
3608 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
3609 spa_deactivate(spa
);
3611 mutex_exit(&spa_namespace_lock
);
3616 * Temporary pool names should never be written to disk.
3618 if (poolname
!= pool
)
3619 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
3621 has_features
= B_FALSE
;
3622 for (elem
= nvlist_next_nvpair(props
, NULL
);
3623 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
3624 if (zpool_prop_feature(nvpair_name(elem
)))
3625 has_features
= B_TRUE
;
3628 if (has_features
|| nvlist_lookup_uint64(props
,
3629 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
3630 version
= SPA_VERSION
;
3632 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
3634 spa
->spa_first_txg
= txg
;
3635 spa
->spa_uberblock
.ub_txg
= txg
- 1;
3636 spa
->spa_uberblock
.ub_version
= version
;
3637 spa
->spa_ubsync
= spa
->spa_uberblock
;
3640 * Create "The Godfather" zio to hold all async IOs
3642 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
3644 for (i
= 0; i
< max_ncpus
; i
++) {
3645 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
3646 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
3647 ZIO_FLAG_GODFATHER
);
3651 * Create the root vdev.
3653 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3655 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
3657 ASSERT(error
!= 0 || rvd
!= NULL
);
3658 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
3660 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
3661 error
= SET_ERROR(EINVAL
);
3664 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
3665 (error
= spa_validate_aux(spa
, nvroot
, txg
,
3666 VDEV_ALLOC_ADD
)) == 0) {
3667 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
3668 vdev_metaslab_set_size(rvd
->vdev_child
[c
]);
3669 vdev_expand(rvd
->vdev_child
[c
], txg
);
3673 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3677 spa_deactivate(spa
);
3679 mutex_exit(&spa_namespace_lock
);
3684 * Get the list of spares, if specified.
3686 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
3687 &spares
, &nspares
) == 0) {
3688 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
, NV_UNIQUE_NAME
,
3690 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
3691 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3692 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3693 spa_load_spares(spa
);
3694 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3695 spa
->spa_spares
.sav_sync
= B_TRUE
;
3699 * Get the list of level 2 cache devices, if specified.
3701 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
3702 &l2cache
, &nl2cache
) == 0) {
3703 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
3704 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3705 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
3706 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3707 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3708 spa_load_l2cache(spa
);
3709 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3710 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3713 spa
->spa_is_initializing
= B_TRUE
;
3714 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, txg
);
3715 spa
->spa_meta_objset
= dp
->dp_meta_objset
;
3716 spa
->spa_is_initializing
= B_FALSE
;
3719 * Create DDTs (dedup tables).
3723 spa_update_dspace(spa
);
3725 tx
= dmu_tx_create_assigned(dp
, txg
);
3728 * Create the pool config object.
3730 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
3731 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
3732 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
3734 if (zap_add(spa
->spa_meta_objset
,
3735 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
3736 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
3737 cmn_err(CE_PANIC
, "failed to add pool config");
3740 if (spa_version(spa
) >= SPA_VERSION_FEATURES
)
3741 spa_feature_create_zap_objects(spa
, tx
);
3743 if (zap_add(spa
->spa_meta_objset
,
3744 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
3745 sizeof (uint64_t), 1, &version
, tx
) != 0) {
3746 cmn_err(CE_PANIC
, "failed to add pool version");
3749 /* Newly created pools with the right version are always deflated. */
3750 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
3751 spa
->spa_deflate
= TRUE
;
3752 if (zap_add(spa
->spa_meta_objset
,
3753 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
3754 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
3755 cmn_err(CE_PANIC
, "failed to add deflate");
3760 * Create the deferred-free bpobj. Turn off compression
3761 * because sync-to-convergence takes longer if the blocksize
3764 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
3765 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
3766 ZIO_COMPRESS_OFF
, tx
);
3767 if (zap_add(spa
->spa_meta_objset
,
3768 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
3769 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
3770 cmn_err(CE_PANIC
, "failed to add bpobj");
3772 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
3773 spa
->spa_meta_objset
, obj
));
3776 * Create the pool's history object.
3778 if (version
>= SPA_VERSION_ZPOOL_HISTORY
)
3779 spa_history_create_obj(spa
, tx
);
3782 * Set pool properties.
3784 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
3785 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
3786 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
3787 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
3789 if (props
!= NULL
) {
3790 spa_configfile_set(spa
, props
, B_FALSE
);
3791 spa_sync_props(props
, tx
);
3796 spa
->spa_sync_on
= B_TRUE
;
3797 txg_sync_start(spa
->spa_dsl_pool
);
3800 * We explicitly wait for the first transaction to complete so that our
3801 * bean counters are appropriately updated.
3803 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
3805 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
3807 spa_history_log_version(spa
, "create");
3810 * Don't count references from objsets that are already closed
3811 * and are making their way through the eviction process.
3813 spa_evicting_os_wait(spa
);
3814 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
3816 mutex_exit(&spa_namespace_lock
);
3823 * Get the root pool information from the root disk, then import the root pool
3824 * during the system boot up time.
3826 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t
**);
3829 spa_generate_rootconf(char *devpath
, char *devid
, uint64_t *guid
)
3832 nvlist_t
*nvtop
, *nvroot
;
3835 if (vdev_disk_read_rootlabel(devpath
, devid
, &config
) != 0)
3839 * Add this top-level vdev to the child array.
3841 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3843 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
3845 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_GUID
, guid
) == 0);
3848 * Put this pool's top-level vdevs into a root vdev.
3850 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3851 VERIFY(nvlist_add_string(nvroot
, ZPOOL_CONFIG_TYPE
,
3852 VDEV_TYPE_ROOT
) == 0);
3853 VERIFY(nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_ID
, 0ULL) == 0);
3854 VERIFY(nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_GUID
, pgid
) == 0);
3855 VERIFY(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
3859 * Replace the existing vdev_tree with the new root vdev in
3860 * this pool's configuration (remove the old, add the new).
3862 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, nvroot
) == 0);
3863 nvlist_free(nvroot
);
3868 * Walk the vdev tree and see if we can find a device with "better"
3869 * configuration. A configuration is "better" if the label on that
3870 * device has a more recent txg.
3873 spa_alt_rootvdev(vdev_t
*vd
, vdev_t
**avd
, uint64_t *txg
)
3877 for (c
= 0; c
< vd
->vdev_children
; c
++)
3878 spa_alt_rootvdev(vd
->vdev_child
[c
], avd
, txg
);
3880 if (vd
->vdev_ops
->vdev_op_leaf
) {
3884 if (vdev_disk_read_rootlabel(vd
->vdev_physpath
, vd
->vdev_devid
,
3888 VERIFY(nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_TXG
,
3892 * Do we have a better boot device?
3894 if (label_txg
> *txg
) {
3903 * Import a root pool.
3905 * For x86. devpath_list will consist of devid and/or physpath name of
3906 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3907 * The GRUB "findroot" command will return the vdev we should boot.
3909 * For Sparc, devpath_list consists the physpath name of the booting device
3910 * no matter the rootpool is a single device pool or a mirrored pool.
3912 * "/pci@1f,0/ide@d/disk@0,0:a"
3915 spa_import_rootpool(char *devpath
, char *devid
)
3918 vdev_t
*rvd
, *bvd
, *avd
= NULL
;
3919 nvlist_t
*config
, *nvtop
;
3925 * Read the label from the boot device and generate a configuration.
3927 config
= spa_generate_rootconf(devpath
, devid
, &guid
);
3928 #if defined(_OBP) && defined(_KERNEL)
3929 if (config
== NULL
) {
3930 if (strstr(devpath
, "/iscsi/ssd") != NULL
) {
3932 get_iscsi_bootpath_phy(devpath
);
3933 config
= spa_generate_rootconf(devpath
, devid
, &guid
);
3937 if (config
== NULL
) {
3938 cmn_err(CE_NOTE
, "Cannot read the pool label from '%s'",
3940 return (SET_ERROR(EIO
));
3943 VERIFY(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
3945 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
, &txg
) == 0);
3947 mutex_enter(&spa_namespace_lock
);
3948 if ((spa
= spa_lookup(pname
)) != NULL
) {
3950 * Remove the existing root pool from the namespace so that we
3951 * can replace it with the correct config we just read in.
3956 spa
= spa_add(pname
, config
, NULL
);
3957 spa
->spa_is_root
= B_TRUE
;
3958 spa
->spa_import_flags
= ZFS_IMPORT_VERBATIM
;
3961 * Build up a vdev tree based on the boot device's label config.
3963 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3965 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3966 error
= spa_config_parse(spa
, &rvd
, nvtop
, NULL
, 0,
3967 VDEV_ALLOC_ROOTPOOL
);
3968 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3970 mutex_exit(&spa_namespace_lock
);
3971 nvlist_free(config
);
3972 cmn_err(CE_NOTE
, "Can not parse the config for pool '%s'",
3978 * Get the boot vdev.
3980 if ((bvd
= vdev_lookup_by_guid(rvd
, guid
)) == NULL
) {
3981 cmn_err(CE_NOTE
, "Can not find the boot vdev for guid %llu",
3982 (u_longlong_t
)guid
);
3983 error
= SET_ERROR(ENOENT
);
3988 * Determine if there is a better boot device.
3991 spa_alt_rootvdev(rvd
, &avd
, &txg
);
3993 cmn_err(CE_NOTE
, "The boot device is 'degraded'. Please "
3994 "try booting from '%s'", avd
->vdev_path
);
3995 error
= SET_ERROR(EINVAL
);
4000 * If the boot device is part of a spare vdev then ensure that
4001 * we're booting off the active spare.
4003 if (bvd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
4004 !bvd
->vdev_isspare
) {
4005 cmn_err(CE_NOTE
, "The boot device is currently spared. Please "
4006 "try booting from '%s'",
4008 vdev_child
[bvd
->vdev_parent
->vdev_children
- 1]->vdev_path
);
4009 error
= SET_ERROR(EINVAL
);
4015 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4017 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4018 mutex_exit(&spa_namespace_lock
);
4020 nvlist_free(config
);
4027 * Import a non-root pool into the system.
4030 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
4033 char *altroot
= NULL
;
4034 spa_load_state_t state
= SPA_LOAD_IMPORT
;
4035 zpool_rewind_policy_t policy
;
4036 uint64_t mode
= spa_mode_global
;
4037 uint64_t readonly
= B_FALSE
;
4040 nvlist_t
**spares
, **l2cache
;
4041 uint_t nspares
, nl2cache
;
4044 * If a pool with this name exists, return failure.
4046 mutex_enter(&spa_namespace_lock
);
4047 if (spa_lookup(pool
) != NULL
) {
4048 mutex_exit(&spa_namespace_lock
);
4049 return (SET_ERROR(EEXIST
));
4053 * Create and initialize the spa structure.
4055 (void) nvlist_lookup_string(props
,
4056 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
4057 (void) nvlist_lookup_uint64(props
,
4058 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
4061 spa
= spa_add(pool
, config
, altroot
);
4062 spa
->spa_import_flags
= flags
;
4065 * Verbatim import - Take a pool and insert it into the namespace
4066 * as if it had been loaded at boot.
4068 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
4070 spa_configfile_set(spa
, props
, B_FALSE
);
4072 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
4074 mutex_exit(&spa_namespace_lock
);
4078 spa_activate(spa
, mode
);
4081 * Don't start async tasks until we know everything is healthy.
4083 spa_async_suspend(spa
);
4085 zpool_get_rewind_policy(config
, &policy
);
4086 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
4087 state
= SPA_LOAD_RECOVER
;
4090 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4091 * because the user-supplied config is actually the one to trust when
4094 if (state
!= SPA_LOAD_RECOVER
)
4095 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
4097 error
= spa_load_best(spa
, state
, B_TRUE
, policy
.zrp_txg
,
4098 policy
.zrp_request
);
4101 * Propagate anything learned while loading the pool and pass it
4102 * back to caller (i.e. rewind info, missing devices, etc).
4104 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4105 spa
->spa_load_info
) == 0);
4107 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4109 * Toss any existing sparelist, as it doesn't have any validity
4110 * anymore, and conflicts with spa_has_spare().
4112 if (spa
->spa_spares
.sav_config
) {
4113 nvlist_free(spa
->spa_spares
.sav_config
);
4114 spa
->spa_spares
.sav_config
= NULL
;
4115 spa_load_spares(spa
);
4117 if (spa
->spa_l2cache
.sav_config
) {
4118 nvlist_free(spa
->spa_l2cache
.sav_config
);
4119 spa
->spa_l2cache
.sav_config
= NULL
;
4120 spa_load_l2cache(spa
);
4123 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4126 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
4129 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
4130 VDEV_ALLOC_L2CACHE
);
4131 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4134 spa_configfile_set(spa
, props
, B_FALSE
);
4136 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
4137 (error
= spa_prop_set(spa
, props
)))) {
4139 spa_deactivate(spa
);
4141 mutex_exit(&spa_namespace_lock
);
4145 spa_async_resume(spa
);
4148 * Override any spares and level 2 cache devices as specified by
4149 * the user, as these may have correct device names/devids, etc.
4151 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4152 &spares
, &nspares
) == 0) {
4153 if (spa
->spa_spares
.sav_config
)
4154 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
,
4155 ZPOOL_CONFIG_SPARES
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4157 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
,
4158 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4159 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
4160 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
4161 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4162 spa_load_spares(spa
);
4163 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4164 spa
->spa_spares
.sav_sync
= B_TRUE
;
4166 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4167 &l2cache
, &nl2cache
) == 0) {
4168 if (spa
->spa_l2cache
.sav_config
)
4169 VERIFY(nvlist_remove(spa
->spa_l2cache
.sav_config
,
4170 ZPOOL_CONFIG_L2CACHE
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4172 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
4173 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4174 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
4175 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
4176 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4177 spa_load_l2cache(spa
);
4178 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4179 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4183 * Check for any removed devices.
4185 if (spa
->spa_autoreplace
) {
4186 spa_aux_check_removed(&spa
->spa_spares
);
4187 spa_aux_check_removed(&spa
->spa_l2cache
);
4190 if (spa_writeable(spa
)) {
4192 * Update the config cache to include the newly-imported pool.
4194 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4198 * It's possible that the pool was expanded while it was exported.
4199 * We kick off an async task to handle this for us.
4201 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
4203 mutex_exit(&spa_namespace_lock
);
4204 spa_history_log_version(spa
, "import");
4207 zvol_create_minors(pool
);
4214 spa_tryimport(nvlist_t
*tryconfig
)
4216 nvlist_t
*config
= NULL
;
4222 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
4225 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
4229 * Create and initialize the spa structure.
4231 mutex_enter(&spa_namespace_lock
);
4232 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
4233 spa_activate(spa
, FREAD
);
4236 * Pass off the heavy lifting to spa_load().
4237 * Pass TRUE for mosconfig because the user-supplied config
4238 * is actually the one to trust when doing an import.
4240 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
, B_TRUE
);
4243 * If 'tryconfig' was at least parsable, return the current config.
4245 if (spa
->spa_root_vdev
!= NULL
) {
4246 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4247 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
,
4249 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
4251 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
4252 spa
->spa_uberblock
.ub_timestamp
) == 0);
4253 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4254 spa
->spa_load_info
) == 0);
4255 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
4256 spa
->spa_errata
) == 0);
4259 * If the bootfs property exists on this pool then we
4260 * copy it out so that external consumers can tell which
4261 * pools are bootable.
4263 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
4264 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4267 * We have to play games with the name since the
4268 * pool was opened as TRYIMPORT_NAME.
4270 if (dsl_dsobj_to_dsname(spa_name(spa
),
4271 spa
->spa_bootfs
, tmpname
) == 0) {
4275 dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4277 cp
= strchr(tmpname
, '/');
4279 (void) strlcpy(dsname
, tmpname
,
4282 (void) snprintf(dsname
, MAXPATHLEN
,
4283 "%s/%s", poolname
, ++cp
);
4285 VERIFY(nvlist_add_string(config
,
4286 ZPOOL_CONFIG_BOOTFS
, dsname
) == 0);
4287 kmem_free(dsname
, MAXPATHLEN
);
4289 kmem_free(tmpname
, MAXPATHLEN
);
4293 * Add the list of hot spares and level 2 cache devices.
4295 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4296 spa_add_spares(spa
, config
);
4297 spa_add_l2cache(spa
, config
);
4298 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4302 spa_deactivate(spa
);
4304 mutex_exit(&spa_namespace_lock
);
4310 * Pool export/destroy
4312 * The act of destroying or exporting a pool is very simple. We make sure there
4313 * is no more pending I/O and any references to the pool are gone. Then, we
4314 * update the pool state and sync all the labels to disk, removing the
4315 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4316 * we don't sync the labels or remove the configuration cache.
4319 spa_export_common(char *pool
, int new_state
, nvlist_t
**oldconfig
,
4320 boolean_t force
, boolean_t hardforce
)
4327 if (!(spa_mode_global
& FWRITE
))
4328 return (SET_ERROR(EROFS
));
4330 mutex_enter(&spa_namespace_lock
);
4331 if ((spa
= spa_lookup(pool
)) == NULL
) {
4332 mutex_exit(&spa_namespace_lock
);
4333 return (SET_ERROR(ENOENT
));
4337 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4338 * reacquire the namespace lock, and see if we can export.
4340 spa_open_ref(spa
, FTAG
);
4341 mutex_exit(&spa_namespace_lock
);
4342 spa_async_suspend(spa
);
4343 mutex_enter(&spa_namespace_lock
);
4344 spa_close(spa
, FTAG
);
4346 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
4349 * The pool will be in core if it's openable, in which case we can
4350 * modify its state. Objsets may be open only because they're dirty,
4351 * so we have to force it to sync before checking spa_refcnt.
4353 if (spa
->spa_sync_on
) {
4354 txg_wait_synced(spa
->spa_dsl_pool
, 0);
4355 spa_evicting_os_wait(spa
);
4359 * A pool cannot be exported or destroyed if there are active
4360 * references. If we are resetting a pool, allow references by
4361 * fault injection handlers.
4363 if (!spa_refcount_zero(spa
) ||
4364 (spa
->spa_inject_ref
!= 0 &&
4365 new_state
!= POOL_STATE_UNINITIALIZED
)) {
4366 spa_async_resume(spa
);
4367 mutex_exit(&spa_namespace_lock
);
4368 return (SET_ERROR(EBUSY
));
4371 if (spa
->spa_sync_on
) {
4373 * A pool cannot be exported if it has an active shared spare.
4374 * This is to prevent other pools stealing the active spare
4375 * from an exported pool. At user's own will, such pool can
4376 * be forcedly exported.
4378 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
4379 spa_has_active_shared_spare(spa
)) {
4380 spa_async_resume(spa
);
4381 mutex_exit(&spa_namespace_lock
);
4382 return (SET_ERROR(EXDEV
));
4386 * We want this to be reflected on every label,
4387 * so mark them all dirty. spa_unload() will do the
4388 * final sync that pushes these changes out.
4390 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
4391 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4392 spa
->spa_state
= new_state
;
4393 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
4395 vdev_config_dirty(spa
->spa_root_vdev
);
4396 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4401 spa_event_notify(spa
, NULL
, FM_EREPORT_ZFS_POOL_DESTROY
);
4403 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
4405 spa_deactivate(spa
);
4408 if (oldconfig
&& spa
->spa_config
)
4409 VERIFY(nvlist_dup(spa
->spa_config
, oldconfig
, 0) == 0);
4411 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
4413 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
4416 mutex_exit(&spa_namespace_lock
);
4422 * Destroy a storage pool.
4425 spa_destroy(char *pool
)
4427 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
4432 * Export a storage pool.
4435 spa_export(char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
4436 boolean_t hardforce
)
4438 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
4443 * Similar to spa_export(), this unloads the spa_t without actually removing it
4444 * from the namespace in any way.
4447 spa_reset(char *pool
)
4449 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
4454 * ==========================================================================
4455 * Device manipulation
4456 * ==========================================================================
4460 * Add a device to a storage pool.
4463 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
4467 vdev_t
*rvd
= spa
->spa_root_vdev
;
4469 nvlist_t
**spares
, **l2cache
;
4470 uint_t nspares
, nl2cache
;
4473 ASSERT(spa_writeable(spa
));
4475 txg
= spa_vdev_enter(spa
);
4477 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
4478 VDEV_ALLOC_ADD
)) != 0)
4479 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4481 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
4483 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
4487 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
4491 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
4492 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
4494 if (vd
->vdev_children
!= 0 &&
4495 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0)
4496 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4499 * We must validate the spares and l2cache devices after checking the
4500 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4502 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
4503 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4506 * Transfer each new top-level vdev from vd to rvd.
4508 for (c
= 0; c
< vd
->vdev_children
; c
++) {
4511 * Set the vdev id to the first hole, if one exists.
4513 for (id
= 0; id
< rvd
->vdev_children
; id
++) {
4514 if (rvd
->vdev_child
[id
]->vdev_ishole
) {
4515 vdev_free(rvd
->vdev_child
[id
]);
4519 tvd
= vd
->vdev_child
[c
];
4520 vdev_remove_child(vd
, tvd
);
4522 vdev_add_child(rvd
, tvd
);
4523 vdev_config_dirty(tvd
);
4527 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
4528 ZPOOL_CONFIG_SPARES
);
4529 spa_load_spares(spa
);
4530 spa
->spa_spares
.sav_sync
= B_TRUE
;
4533 if (nl2cache
!= 0) {
4534 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
4535 ZPOOL_CONFIG_L2CACHE
);
4536 spa_load_l2cache(spa
);
4537 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4541 * We have to be careful when adding new vdevs to an existing pool.
4542 * If other threads start allocating from these vdevs before we
4543 * sync the config cache, and we lose power, then upon reboot we may
4544 * fail to open the pool because there are DVAs that the config cache
4545 * can't translate. Therefore, we first add the vdevs without
4546 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4547 * and then let spa_config_update() initialize the new metaslabs.
4549 * spa_load() checks for added-but-not-initialized vdevs, so that
4550 * if we lose power at any point in this sequence, the remaining
4551 * steps will be completed the next time we load the pool.
4553 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
4555 mutex_enter(&spa_namespace_lock
);
4556 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4557 mutex_exit(&spa_namespace_lock
);
4563 * Attach a device to a mirror. The arguments are the path to any device
4564 * in the mirror, and the nvroot for the new device. If the path specifies
4565 * a device that is not mirrored, we automatically insert the mirror vdev.
4567 * If 'replacing' is specified, the new device is intended to replace the
4568 * existing device; in this case the two devices are made into their own
4569 * mirror using the 'replacing' vdev, which is functionally identical to
4570 * the mirror vdev (it actually reuses all the same ops) but has a few
4571 * extra rules: you can't attach to it after it's been created, and upon
4572 * completion of resilvering, the first disk (the one being replaced)
4573 * is automatically detached.
4576 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
)
4578 uint64_t txg
, dtl_max_txg
;
4579 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
4581 char *oldvdpath
, *newvdpath
;
4584 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
4586 ASSERT(spa_writeable(spa
));
4588 txg
= spa_vdev_enter(spa
);
4590 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4593 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4595 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
4596 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4598 pvd
= oldvd
->vdev_parent
;
4600 if ((error
= spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
4601 VDEV_ALLOC_ATTACH
)) != 0)
4602 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4604 if (newrootvd
->vdev_children
!= 1)
4605 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4607 newvd
= newrootvd
->vdev_child
[0];
4609 if (!newvd
->vdev_ops
->vdev_op_leaf
)
4610 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4612 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
4613 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
4616 * Spares can't replace logs
4618 if (oldvd
->vdev_top
->vdev_islog
&& newvd
->vdev_isspare
)
4619 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4623 * For attach, the only allowable parent is a mirror or the root
4626 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4627 pvd
->vdev_ops
!= &vdev_root_ops
)
4628 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4630 pvops
= &vdev_mirror_ops
;
4633 * Active hot spares can only be replaced by inactive hot
4636 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4637 oldvd
->vdev_isspare
&&
4638 !spa_has_spare(spa
, newvd
->vdev_guid
))
4639 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4642 * If the source is a hot spare, and the parent isn't already a
4643 * spare, then we want to create a new hot spare. Otherwise, we
4644 * want to create a replacing vdev. The user is not allowed to
4645 * attach to a spared vdev child unless the 'isspare' state is
4646 * the same (spare replaces spare, non-spare replaces
4649 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
4650 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
4651 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4652 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4653 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
4654 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4657 if (newvd
->vdev_isspare
)
4658 pvops
= &vdev_spare_ops
;
4660 pvops
= &vdev_replacing_ops
;
4664 * Make sure the new device is big enough.
4666 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
4667 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
4670 * The new device cannot have a higher alignment requirement
4671 * than the top-level vdev.
4673 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
4674 return (spa_vdev_exit(spa
, newrootvd
, txg
, EDOM
));
4677 * If this is an in-place replacement, update oldvd's path and devid
4678 * to make it distinguishable from newvd, and unopenable from now on.
4680 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
4681 spa_strfree(oldvd
->vdev_path
);
4682 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
4684 (void) sprintf(oldvd
->vdev_path
, "%s/%s",
4685 newvd
->vdev_path
, "old");
4686 if (oldvd
->vdev_devid
!= NULL
) {
4687 spa_strfree(oldvd
->vdev_devid
);
4688 oldvd
->vdev_devid
= NULL
;
4692 /* mark the device being resilvered */
4693 newvd
->vdev_resilver_txg
= txg
;
4696 * If the parent is not a mirror, or if we're replacing, insert the new
4697 * mirror/replacing/spare vdev above oldvd.
4699 if (pvd
->vdev_ops
!= pvops
)
4700 pvd
= vdev_add_parent(oldvd
, pvops
);
4702 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
4703 ASSERT(pvd
->vdev_ops
== pvops
);
4704 ASSERT(oldvd
->vdev_parent
== pvd
);
4707 * Extract the new device from its root and add it to pvd.
4709 vdev_remove_child(newrootvd
, newvd
);
4710 newvd
->vdev_id
= pvd
->vdev_children
;
4711 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
4712 vdev_add_child(pvd
, newvd
);
4714 tvd
= newvd
->vdev_top
;
4715 ASSERT(pvd
->vdev_top
== tvd
);
4716 ASSERT(tvd
->vdev_parent
== rvd
);
4718 vdev_config_dirty(tvd
);
4721 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4722 * for any dmu_sync-ed blocks. It will propagate upward when
4723 * spa_vdev_exit() calls vdev_dtl_reassess().
4725 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
4727 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
4728 dtl_max_txg
- TXG_INITIAL
);
4730 if (newvd
->vdev_isspare
) {
4731 spa_spare_activate(newvd
);
4732 spa_event_notify(spa
, newvd
, FM_EREPORT_ZFS_DEVICE_SPARE
);
4735 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
4736 newvdpath
= spa_strdup(newvd
->vdev_path
);
4737 newvd_isspare
= newvd
->vdev_isspare
;
4740 * Mark newvd's DTL dirty in this txg.
4742 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
4745 * Schedule the resilver to restart in the future. We do this to
4746 * ensure that dmu_sync-ed blocks have been stitched into the
4747 * respective datasets.
4749 dsl_resilver_restart(spa
->spa_dsl_pool
, dtl_max_txg
);
4754 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
4756 spa_history_log_internal(spa
, "vdev attach", NULL
,
4757 "%s vdev=%s %s vdev=%s",
4758 replacing
&& newvd_isspare
? "spare in" :
4759 replacing
? "replace" : "attach", newvdpath
,
4760 replacing
? "for" : "to", oldvdpath
);
4762 spa_strfree(oldvdpath
);
4763 spa_strfree(newvdpath
);
4765 if (spa
->spa_bootfs
)
4766 spa_event_notify(spa
, newvd
, FM_EREPORT_ZFS_BOOTFS_VDEV_ATTACH
);
4772 * Detach a device from a mirror or replacing vdev.
4774 * If 'replace_done' is specified, only detach if the parent
4775 * is a replacing vdev.
4778 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
4782 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
4783 boolean_t unspare
= B_FALSE
;
4784 uint64_t unspare_guid
= 0;
4787 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
4788 ASSERT(spa_writeable(spa
));
4790 txg
= spa_vdev_enter(spa
);
4792 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4795 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4797 if (!vd
->vdev_ops
->vdev_op_leaf
)
4798 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4800 pvd
= vd
->vdev_parent
;
4803 * If the parent/child relationship is not as expected, don't do it.
4804 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4805 * vdev that's replacing B with C. The user's intent in replacing
4806 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4807 * the replace by detaching C, the expected behavior is to end up
4808 * M(A,B). But suppose that right after deciding to detach C,
4809 * the replacement of B completes. We would have M(A,C), and then
4810 * ask to detach C, which would leave us with just A -- not what
4811 * the user wanted. To prevent this, we make sure that the
4812 * parent/child relationship hasn't changed -- in this example,
4813 * that C's parent is still the replacing vdev R.
4815 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
4816 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4819 * Only 'replacing' or 'spare' vdevs can be replaced.
4821 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4822 pvd
->vdev_ops
!= &vdev_spare_ops
)
4823 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4825 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
4826 spa_version(spa
) >= SPA_VERSION_SPARES
);
4829 * Only mirror, replacing, and spare vdevs support detach.
4831 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4832 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4833 pvd
->vdev_ops
!= &vdev_spare_ops
)
4834 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4837 * If this device has the only valid copy of some data,
4838 * we cannot safely detach it.
4840 if (vdev_dtl_required(vd
))
4841 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4843 ASSERT(pvd
->vdev_children
>= 2);
4846 * If we are detaching the second disk from a replacing vdev, then
4847 * check to see if we changed the original vdev's path to have "/old"
4848 * at the end in spa_vdev_attach(). If so, undo that change now.
4850 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
4851 vd
->vdev_path
!= NULL
) {
4852 size_t len
= strlen(vd
->vdev_path
);
4854 for (c
= 0; c
< pvd
->vdev_children
; c
++) {
4855 cvd
= pvd
->vdev_child
[c
];
4857 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
4860 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
4861 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
4862 spa_strfree(cvd
->vdev_path
);
4863 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
4870 * If we are detaching the original disk from a spare, then it implies
4871 * that the spare should become a real disk, and be removed from the
4872 * active spare list for the pool.
4874 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4876 pvd
->vdev_child
[pvd
->vdev_children
- 1]->vdev_isspare
)
4880 * Erase the disk labels so the disk can be used for other things.
4881 * This must be done after all other error cases are handled,
4882 * but before we disembowel vd (so we can still do I/O to it).
4883 * But if we can't do it, don't treat the error as fatal --
4884 * it may be that the unwritability of the disk is the reason
4885 * it's being detached!
4887 error
= vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
4890 * Remove vd from its parent and compact the parent's children.
4892 vdev_remove_child(pvd
, vd
);
4893 vdev_compact_children(pvd
);
4896 * Remember one of the remaining children so we can get tvd below.
4898 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
4901 * If we need to remove the remaining child from the list of hot spares,
4902 * do it now, marking the vdev as no longer a spare in the process.
4903 * We must do this before vdev_remove_parent(), because that can
4904 * change the GUID if it creates a new toplevel GUID. For a similar
4905 * reason, we must remove the spare now, in the same txg as the detach;
4906 * otherwise someone could attach a new sibling, change the GUID, and
4907 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4910 ASSERT(cvd
->vdev_isspare
);
4911 spa_spare_remove(cvd
);
4912 unspare_guid
= cvd
->vdev_guid
;
4913 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
4914 cvd
->vdev_unspare
= B_TRUE
;
4918 * If the parent mirror/replacing vdev only has one child,
4919 * the parent is no longer needed. Remove it from the tree.
4921 if (pvd
->vdev_children
== 1) {
4922 if (pvd
->vdev_ops
== &vdev_spare_ops
)
4923 cvd
->vdev_unspare
= B_FALSE
;
4924 vdev_remove_parent(cvd
);
4929 * We don't set tvd until now because the parent we just removed
4930 * may have been the previous top-level vdev.
4932 tvd
= cvd
->vdev_top
;
4933 ASSERT(tvd
->vdev_parent
== rvd
);
4936 * Reevaluate the parent vdev state.
4938 vdev_propagate_state(cvd
);
4941 * If the 'autoexpand' property is set on the pool then automatically
4942 * try to expand the size of the pool. For example if the device we
4943 * just detached was smaller than the others, it may be possible to
4944 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4945 * first so that we can obtain the updated sizes of the leaf vdevs.
4947 if (spa
->spa_autoexpand
) {
4949 vdev_expand(tvd
, txg
);
4952 vdev_config_dirty(tvd
);
4955 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4956 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4957 * But first make sure we're not on any *other* txg's DTL list, to
4958 * prevent vd from being accessed after it's freed.
4960 vdpath
= spa_strdup(vd
->vdev_path
);
4961 for (t
= 0; t
< TXG_SIZE
; t
++)
4962 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
4963 vd
->vdev_detached
= B_TRUE
;
4964 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
4966 spa_event_notify(spa
, vd
, FM_EREPORT_ZFS_DEVICE_REMOVE
);
4968 /* hang on to the spa before we release the lock */
4969 spa_open_ref(spa
, FTAG
);
4971 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
4973 spa_history_log_internal(spa
, "detach", NULL
,
4975 spa_strfree(vdpath
);
4978 * If this was the removal of the original device in a hot spare vdev,
4979 * then we want to go through and remove the device from the hot spare
4980 * list of every other pool.
4983 spa_t
*altspa
= NULL
;
4985 mutex_enter(&spa_namespace_lock
);
4986 while ((altspa
= spa_next(altspa
)) != NULL
) {
4987 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
4991 spa_open_ref(altspa
, FTAG
);
4992 mutex_exit(&spa_namespace_lock
);
4993 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
4994 mutex_enter(&spa_namespace_lock
);
4995 spa_close(altspa
, FTAG
);
4997 mutex_exit(&spa_namespace_lock
);
4999 /* search the rest of the vdevs for spares to remove */
5000 spa_vdev_resilver_done(spa
);
5003 /* all done with the spa; OK to release */
5004 mutex_enter(&spa_namespace_lock
);
5005 spa_close(spa
, FTAG
);
5006 mutex_exit(&spa_namespace_lock
);
5012 * Split a set of devices from their mirrors, and create a new pool from them.
5015 spa_vdev_split_mirror(spa_t
*spa
, char *newname
, nvlist_t
*config
,
5016 nvlist_t
*props
, boolean_t exp
)
5019 uint64_t txg
, *glist
;
5021 uint_t c
, children
, lastlog
;
5022 nvlist_t
**child
, *nvl
, *tmp
;
5024 char *altroot
= NULL
;
5025 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
5026 boolean_t activate_slog
;
5028 ASSERT(spa_writeable(spa
));
5030 txg
= spa_vdev_enter(spa
);
5032 /* clear the log and flush everything up to now */
5033 activate_slog
= spa_passivate_log(spa
);
5034 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5035 error
= spa_offline_log(spa
);
5036 txg
= spa_vdev_config_enter(spa
);
5039 spa_activate_log(spa
);
5042 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5044 /* check new spa name before going any further */
5045 if (spa_lookup(newname
) != NULL
)
5046 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
5049 * scan through all the children to ensure they're all mirrors
5051 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
5052 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
5054 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5056 /* first, check to ensure we've got the right child count */
5057 rvd
= spa
->spa_root_vdev
;
5059 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
5060 vdev_t
*vd
= rvd
->vdev_child
[c
];
5062 /* don't count the holes & logs as children */
5063 if (vd
->vdev_islog
|| vd
->vdev_ishole
) {
5071 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
5072 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5074 /* next, ensure no spare or cache devices are part of the split */
5075 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
5076 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
5077 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5079 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
5080 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
5082 /* then, loop over each vdev and validate it */
5083 for (c
= 0; c
< children
; c
++) {
5084 uint64_t is_hole
= 0;
5086 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
5090 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
5091 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
5094 error
= SET_ERROR(EINVAL
);
5099 /* which disk is going to be split? */
5100 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
5102 error
= SET_ERROR(EINVAL
);
5106 /* look it up in the spa */
5107 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
5108 if (vml
[c
] == NULL
) {
5109 error
= SET_ERROR(ENODEV
);
5113 /* make sure there's nothing stopping the split */
5114 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
5115 vml
[c
]->vdev_islog
||
5116 vml
[c
]->vdev_ishole
||
5117 vml
[c
]->vdev_isspare
||
5118 vml
[c
]->vdev_isl2cache
||
5119 !vdev_writeable(vml
[c
]) ||
5120 vml
[c
]->vdev_children
!= 0 ||
5121 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
5122 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
5123 error
= SET_ERROR(EINVAL
);
5127 if (vdev_dtl_required(vml
[c
])) {
5128 error
= SET_ERROR(EBUSY
);
5132 /* we need certain info from the top level */
5133 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
5134 vml
[c
]->vdev_top
->vdev_ms_array
) == 0);
5135 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
5136 vml
[c
]->vdev_top
->vdev_ms_shift
) == 0);
5137 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
5138 vml
[c
]->vdev_top
->vdev_asize
) == 0);
5139 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
5140 vml
[c
]->vdev_top
->vdev_ashift
) == 0);
5144 kmem_free(vml
, children
* sizeof (vdev_t
*));
5145 kmem_free(glist
, children
* sizeof (uint64_t));
5146 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5149 /* stop writers from using the disks */
5150 for (c
= 0; c
< children
; c
++) {
5152 vml
[c
]->vdev_offline
= B_TRUE
;
5154 vdev_reopen(spa
->spa_root_vdev
);
5157 * Temporarily record the splitting vdevs in the spa config. This
5158 * will disappear once the config is regenerated.
5160 VERIFY(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5161 VERIFY(nvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
5162 glist
, children
) == 0);
5163 kmem_free(glist
, children
* sizeof (uint64_t));
5165 mutex_enter(&spa
->spa_props_lock
);
5166 VERIFY(nvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
,
5168 mutex_exit(&spa
->spa_props_lock
);
5169 spa
->spa_config_splitting
= nvl
;
5170 vdev_config_dirty(spa
->spa_root_vdev
);
5172 /* configure and create the new pool */
5173 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
) == 0);
5174 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
5175 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
) == 0);
5176 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
5177 spa_version(spa
)) == 0);
5178 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
5179 spa
->spa_config_txg
) == 0);
5180 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
5181 spa_generate_guid(NULL
)) == 0);
5182 (void) nvlist_lookup_string(props
,
5183 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5185 /* add the new pool to the namespace */
5186 newspa
= spa_add(newname
, config
, altroot
);
5187 newspa
->spa_config_txg
= spa
->spa_config_txg
;
5188 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
5190 /* release the spa config lock, retaining the namespace lock */
5191 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5193 if (zio_injection_enabled
)
5194 zio_handle_panic_injection(spa
, FTAG
, 1);
5196 spa_activate(newspa
, spa_mode_global
);
5197 spa_async_suspend(newspa
);
5199 /* create the new pool from the disks of the original pool */
5200 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
, B_TRUE
);
5204 /* if that worked, generate a real config for the new pool */
5205 if (newspa
->spa_root_vdev
!= NULL
) {
5206 VERIFY(nvlist_alloc(&newspa
->spa_config_splitting
,
5207 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5208 VERIFY(nvlist_add_uint64(newspa
->spa_config_splitting
,
5209 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
)) == 0);
5210 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
5215 if (props
!= NULL
) {
5216 spa_configfile_set(newspa
, props
, B_FALSE
);
5217 error
= spa_prop_set(newspa
, props
);
5222 /* flush everything */
5223 txg
= spa_vdev_config_enter(newspa
);
5224 vdev_config_dirty(newspa
->spa_root_vdev
);
5225 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
5227 if (zio_injection_enabled
)
5228 zio_handle_panic_injection(spa
, FTAG
, 2);
5230 spa_async_resume(newspa
);
5232 /* finally, update the original pool's config */
5233 txg
= spa_vdev_config_enter(spa
);
5234 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
5235 error
= dmu_tx_assign(tx
, TXG_WAIT
);
5238 for (c
= 0; c
< children
; c
++) {
5239 if (vml
[c
] != NULL
) {
5242 spa_history_log_internal(spa
, "detach", tx
,
5243 "vdev=%s", vml
[c
]->vdev_path
);
5247 vdev_config_dirty(spa
->spa_root_vdev
);
5248 spa
->spa_config_splitting
= NULL
;
5252 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
5254 if (zio_injection_enabled
)
5255 zio_handle_panic_injection(spa
, FTAG
, 3);
5257 /* split is complete; log a history record */
5258 spa_history_log_internal(newspa
, "split", NULL
,
5259 "from pool %s", spa_name(spa
));
5261 kmem_free(vml
, children
* sizeof (vdev_t
*));
5263 /* if we're not going to mount the filesystems in userland, export */
5265 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
5272 spa_deactivate(newspa
);
5275 txg
= spa_vdev_config_enter(spa
);
5277 /* re-online all offlined disks */
5278 for (c
= 0; c
< children
; c
++) {
5280 vml
[c
]->vdev_offline
= B_FALSE
;
5282 vdev_reopen(spa
->spa_root_vdev
);
5284 nvlist_free(spa
->spa_config_splitting
);
5285 spa
->spa_config_splitting
= NULL
;
5286 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
5288 kmem_free(vml
, children
* sizeof (vdev_t
*));
5293 spa_nvlist_lookup_by_guid(nvlist_t
**nvpp
, int count
, uint64_t target_guid
)
5297 for (i
= 0; i
< count
; i
++) {
5300 VERIFY(nvlist_lookup_uint64(nvpp
[i
], ZPOOL_CONFIG_GUID
,
5303 if (guid
== target_guid
)
5311 spa_vdev_remove_aux(nvlist_t
*config
, char *name
, nvlist_t
**dev
, int count
,
5312 nvlist_t
*dev_to_remove
)
5314 nvlist_t
**newdev
= NULL
;
5318 newdev
= kmem_alloc((count
- 1) * sizeof (void *), KM_SLEEP
);
5320 for (i
= 0, j
= 0; i
< count
; i
++) {
5321 if (dev
[i
] == dev_to_remove
)
5323 VERIFY(nvlist_dup(dev
[i
], &newdev
[j
++], KM_SLEEP
) == 0);
5326 VERIFY(nvlist_remove(config
, name
, DATA_TYPE_NVLIST_ARRAY
) == 0);
5327 VERIFY(nvlist_add_nvlist_array(config
, name
, newdev
, count
- 1) == 0);
5329 for (i
= 0; i
< count
- 1; i
++)
5330 nvlist_free(newdev
[i
]);
5333 kmem_free(newdev
, (count
- 1) * sizeof (void *));
5337 * Evacuate the device.
5340 spa_vdev_remove_evacuate(spa_t
*spa
, vdev_t
*vd
)
5345 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5346 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5347 ASSERT(vd
== vd
->vdev_top
);
5350 * Evacuate the device. We don't hold the config lock as writer
5351 * since we need to do I/O but we do keep the
5352 * spa_namespace_lock held. Once this completes the device
5353 * should no longer have any blocks allocated on it.
5355 if (vd
->vdev_islog
) {
5356 if (vd
->vdev_stat
.vs_alloc
!= 0)
5357 error
= spa_offline_log(spa
);
5359 error
= SET_ERROR(ENOTSUP
);
5366 * The evacuation succeeded. Remove any remaining MOS metadata
5367 * associated with this vdev, and wait for these changes to sync.
5369 ASSERT0(vd
->vdev_stat
.vs_alloc
);
5370 txg
= spa_vdev_config_enter(spa
);
5371 vd
->vdev_removing
= B_TRUE
;
5372 vdev_dirty_leaves(vd
, VDD_DTL
, txg
);
5373 vdev_config_dirty(vd
);
5374 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5380 * Complete the removal by cleaning up the namespace.
5383 spa_vdev_remove_from_namespace(spa_t
*spa
, vdev_t
*vd
)
5385 vdev_t
*rvd
= spa
->spa_root_vdev
;
5386 uint64_t id
= vd
->vdev_id
;
5387 boolean_t last_vdev
= (id
== (rvd
->vdev_children
- 1));
5389 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5390 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5391 ASSERT(vd
== vd
->vdev_top
);
5394 * Only remove any devices which are empty.
5396 if (vd
->vdev_stat
.vs_alloc
!= 0)
5399 (void) vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
5401 if (list_link_active(&vd
->vdev_state_dirty_node
))
5402 vdev_state_clean(vd
);
5403 if (list_link_active(&vd
->vdev_config_dirty_node
))
5404 vdev_config_clean(vd
);
5409 vdev_compact_children(rvd
);
5411 vd
= vdev_alloc_common(spa
, id
, 0, &vdev_hole_ops
);
5412 vdev_add_child(rvd
, vd
);
5414 vdev_config_dirty(rvd
);
5417 * Reassess the health of our root vdev.
5423 * Remove a device from the pool -
5425 * Removing a device from the vdev namespace requires several steps
5426 * and can take a significant amount of time. As a result we use
5427 * the spa_vdev_config_[enter/exit] functions which allow us to
5428 * grab and release the spa_config_lock while still holding the namespace
5429 * lock. During each step the configuration is synced out.
5431 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5435 spa_vdev_remove(spa_t
*spa
, uint64_t guid
, boolean_t unspare
)
5438 metaslab_group_t
*mg
;
5439 nvlist_t
**spares
, **l2cache
, *nv
;
5441 uint_t nspares
, nl2cache
;
5443 boolean_t locked
= MUTEX_HELD(&spa_namespace_lock
);
5445 ASSERT(spa_writeable(spa
));
5448 txg
= spa_vdev_enter(spa
);
5450 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
5452 if (spa
->spa_spares
.sav_vdevs
!= NULL
&&
5453 nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
5454 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0 &&
5455 (nv
= spa_nvlist_lookup_by_guid(spares
, nspares
, guid
)) != NULL
) {
5457 * Only remove the hot spare if it's not currently in use
5460 if (vd
== NULL
|| unspare
) {
5461 spa_vdev_remove_aux(spa
->spa_spares
.sav_config
,
5462 ZPOOL_CONFIG_SPARES
, spares
, nspares
, nv
);
5463 spa_load_spares(spa
);
5464 spa
->spa_spares
.sav_sync
= B_TRUE
;
5466 error
= SET_ERROR(EBUSY
);
5468 } else if (spa
->spa_l2cache
.sav_vdevs
!= NULL
&&
5469 nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
5470 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0 &&
5471 (nv
= spa_nvlist_lookup_by_guid(l2cache
, nl2cache
, guid
)) != NULL
) {
5473 * Cache devices can always be removed.
5475 spa_vdev_remove_aux(spa
->spa_l2cache
.sav_config
,
5476 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
, nv
);
5477 spa_load_l2cache(spa
);
5478 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5479 } else if (vd
!= NULL
&& vd
->vdev_islog
) {
5481 ASSERT(vd
== vd
->vdev_top
);
5486 * Stop allocating from this vdev.
5488 metaslab_group_passivate(mg
);
5491 * Wait for the youngest allocations and frees to sync,
5492 * and then wait for the deferral of those frees to finish.
5494 spa_vdev_config_exit(spa
, NULL
,
5495 txg
+ TXG_CONCURRENT_STATES
+ TXG_DEFER_SIZE
, 0, FTAG
);
5498 * Attempt to evacuate the vdev.
5500 error
= spa_vdev_remove_evacuate(spa
, vd
);
5502 txg
= spa_vdev_config_enter(spa
);
5505 * If we couldn't evacuate the vdev, unwind.
5508 metaslab_group_activate(mg
);
5509 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5513 * Clean up the vdev namespace.
5515 spa_vdev_remove_from_namespace(spa
, vd
);
5517 } else if (vd
!= NULL
) {
5519 * Normal vdevs cannot be removed (yet).
5521 error
= SET_ERROR(ENOTSUP
);
5524 * There is no vdev of any kind with the specified guid.
5526 error
= SET_ERROR(ENOENT
);
5530 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5536 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5537 * currently spared, so we can detach it.
5540 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
5542 vdev_t
*newvd
, *oldvd
;
5545 for (c
= 0; c
< vd
->vdev_children
; c
++) {
5546 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
5552 * Check for a completed replacement. We always consider the first
5553 * vdev in the list to be the oldest vdev, and the last one to be
5554 * the newest (see spa_vdev_attach() for how that works). In
5555 * the case where the newest vdev is faulted, we will not automatically
5556 * remove it after a resilver completes. This is OK as it will require
5557 * user intervention to determine which disk the admin wishes to keep.
5559 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
5560 ASSERT(vd
->vdev_children
> 1);
5562 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
5563 oldvd
= vd
->vdev_child
[0];
5565 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5566 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5567 !vdev_dtl_required(oldvd
))
5572 * Check for a completed resilver with the 'unspare' flag set.
5574 if (vd
->vdev_ops
== &vdev_spare_ops
) {
5575 vdev_t
*first
= vd
->vdev_child
[0];
5576 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
5578 if (last
->vdev_unspare
) {
5581 } else if (first
->vdev_unspare
) {
5588 if (oldvd
!= NULL
&&
5589 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5590 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5591 !vdev_dtl_required(oldvd
))
5595 * If there are more than two spares attached to a disk,
5596 * and those spares are not required, then we want to
5597 * attempt to free them up now so that they can be used
5598 * by other pools. Once we're back down to a single
5599 * disk+spare, we stop removing them.
5601 if (vd
->vdev_children
> 2) {
5602 newvd
= vd
->vdev_child
[1];
5604 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
5605 vdev_dtl_empty(last
, DTL_MISSING
) &&
5606 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
5607 !vdev_dtl_required(newvd
))
5616 spa_vdev_resilver_done(spa_t
*spa
)
5618 vdev_t
*vd
, *pvd
, *ppvd
;
5619 uint64_t guid
, sguid
, pguid
, ppguid
;
5621 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5623 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
5624 pvd
= vd
->vdev_parent
;
5625 ppvd
= pvd
->vdev_parent
;
5626 guid
= vd
->vdev_guid
;
5627 pguid
= pvd
->vdev_guid
;
5628 ppguid
= ppvd
->vdev_guid
;
5631 * If we have just finished replacing a hot spared device, then
5632 * we need to detach the parent's first child (the original hot
5635 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
5636 ppvd
->vdev_children
== 2) {
5637 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
5638 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
5640 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
5642 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5643 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
5645 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
5647 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5650 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5654 * Update the stored path or FRU for this vdev.
5657 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
5661 boolean_t sync
= B_FALSE
;
5663 ASSERT(spa_writeable(spa
));
5665 spa_vdev_state_enter(spa
, SCL_ALL
);
5667 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
5668 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
5670 if (!vd
->vdev_ops
->vdev_op_leaf
)
5671 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
5674 if (strcmp(value
, vd
->vdev_path
) != 0) {
5675 spa_strfree(vd
->vdev_path
);
5676 vd
->vdev_path
= spa_strdup(value
);
5680 if (vd
->vdev_fru
== NULL
) {
5681 vd
->vdev_fru
= spa_strdup(value
);
5683 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
5684 spa_strfree(vd
->vdev_fru
);
5685 vd
->vdev_fru
= spa_strdup(value
);
5690 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
5694 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
5696 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
5700 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
5702 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
5706 * ==========================================================================
5708 * ==========================================================================
5712 spa_scan_stop(spa_t
*spa
)
5714 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5715 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
5716 return (SET_ERROR(EBUSY
));
5717 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
5721 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
5723 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5725 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
5726 return (SET_ERROR(ENOTSUP
));
5729 * If a resilver was requested, but there is no DTL on a
5730 * writeable leaf device, we have nothing to do.
5732 if (func
== POOL_SCAN_RESILVER
&&
5733 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
5734 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
5738 return (dsl_scan(spa
->spa_dsl_pool
, func
));
5742 * ==========================================================================
5743 * SPA async task processing
5744 * ==========================================================================
5748 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
5752 if (vd
->vdev_remove_wanted
) {
5753 vd
->vdev_remove_wanted
= B_FALSE
;
5754 vd
->vdev_delayed_close
= B_FALSE
;
5755 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
5758 * We want to clear the stats, but we don't want to do a full
5759 * vdev_clear() as that will cause us to throw away
5760 * degraded/faulted state as well as attempt to reopen the
5761 * device, all of which is a waste.
5763 vd
->vdev_stat
.vs_read_errors
= 0;
5764 vd
->vdev_stat
.vs_write_errors
= 0;
5765 vd
->vdev_stat
.vs_checksum_errors
= 0;
5767 vdev_state_dirty(vd
->vdev_top
);
5770 for (c
= 0; c
< vd
->vdev_children
; c
++)
5771 spa_async_remove(spa
, vd
->vdev_child
[c
]);
5775 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
5779 if (vd
->vdev_probe_wanted
) {
5780 vd
->vdev_probe_wanted
= B_FALSE
;
5781 vdev_reopen(vd
); /* vdev_open() does the actual probe */
5784 for (c
= 0; c
< vd
->vdev_children
; c
++)
5785 spa_async_probe(spa
, vd
->vdev_child
[c
]);
5789 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
5793 if (!spa
->spa_autoexpand
)
5796 for (c
= 0; c
< vd
->vdev_children
; c
++) {
5797 vdev_t
*cvd
= vd
->vdev_child
[c
];
5798 spa_async_autoexpand(spa
, cvd
);
5801 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
5804 spa_event_notify(vd
->vdev_spa
, vd
, FM_EREPORT_ZFS_DEVICE_AUTOEXPAND
);
5808 spa_async_thread(spa_t
*spa
)
5812 ASSERT(spa
->spa_sync_on
);
5814 mutex_enter(&spa
->spa_async_lock
);
5815 tasks
= spa
->spa_async_tasks
;
5816 spa
->spa_async_tasks
= 0;
5817 mutex_exit(&spa
->spa_async_lock
);
5820 * See if the config needs to be updated.
5822 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
5823 uint64_t old_space
, new_space
;
5825 mutex_enter(&spa_namespace_lock
);
5826 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
5827 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5828 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
5829 mutex_exit(&spa_namespace_lock
);
5832 * If the pool grew as a result of the config update,
5833 * then log an internal history event.
5835 if (new_space
!= old_space
) {
5836 spa_history_log_internal(spa
, "vdev online", NULL
,
5837 "pool '%s' size: %llu(+%llu)",
5838 spa_name(spa
), new_space
, new_space
- old_space
);
5843 * See if any devices need to be marked REMOVED.
5845 if (tasks
& SPA_ASYNC_REMOVE
) {
5846 spa_vdev_state_enter(spa
, SCL_NONE
);
5847 spa_async_remove(spa
, spa
->spa_root_vdev
);
5848 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
5849 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
5850 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
5851 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
5852 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5855 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
5856 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5857 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
5858 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5862 * See if any devices need to be probed.
5864 if (tasks
& SPA_ASYNC_PROBE
) {
5865 spa_vdev_state_enter(spa
, SCL_NONE
);
5866 spa_async_probe(spa
, spa
->spa_root_vdev
);
5867 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5871 * If any devices are done replacing, detach them.
5873 if (tasks
& SPA_ASYNC_RESILVER_DONE
)
5874 spa_vdev_resilver_done(spa
);
5877 * Kick off a resilver.
5879 if (tasks
& SPA_ASYNC_RESILVER
)
5880 dsl_resilver_restart(spa
->spa_dsl_pool
, 0);
5883 * Let the world know that we're done.
5885 mutex_enter(&spa
->spa_async_lock
);
5886 spa
->spa_async_thread
= NULL
;
5887 cv_broadcast(&spa
->spa_async_cv
);
5888 mutex_exit(&spa
->spa_async_lock
);
5893 spa_async_suspend(spa_t
*spa
)
5895 mutex_enter(&spa
->spa_async_lock
);
5896 spa
->spa_async_suspended
++;
5897 while (spa
->spa_async_thread
!= NULL
)
5898 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
5899 mutex_exit(&spa
->spa_async_lock
);
5903 spa_async_resume(spa_t
*spa
)
5905 mutex_enter(&spa
->spa_async_lock
);
5906 ASSERT(spa
->spa_async_suspended
!= 0);
5907 spa
->spa_async_suspended
--;
5908 mutex_exit(&spa
->spa_async_lock
);
5912 spa_async_dispatch(spa_t
*spa
)
5914 mutex_enter(&spa
->spa_async_lock
);
5915 if (spa
->spa_async_tasks
&& !spa
->spa_async_suspended
&&
5916 spa
->spa_async_thread
== NULL
&&
5917 rootdir
!= NULL
&& !vn_is_readonly(rootdir
))
5918 spa
->spa_async_thread
= thread_create(NULL
, 0,
5919 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
5920 mutex_exit(&spa
->spa_async_lock
);
5924 spa_async_request(spa_t
*spa
, int task
)
5926 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
5927 mutex_enter(&spa
->spa_async_lock
);
5928 spa
->spa_async_tasks
|= task
;
5929 mutex_exit(&spa
->spa_async_lock
);
5933 * ==========================================================================
5934 * SPA syncing routines
5935 * ==========================================================================
5939 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
5942 bpobj_enqueue(bpo
, bp
, tx
);
5947 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
5951 zio_nowait(zio_free_sync(zio
, zio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
5957 * Note: this simple function is not inlined to make it easier to dtrace the
5958 * amount of time spent syncing frees.
5961 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
5963 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
5964 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
5965 VERIFY(zio_wait(zio
) == 0);
5969 * Note: this simple function is not inlined to make it easier to dtrace the
5970 * amount of time spent syncing deferred frees.
5973 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
5975 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
5976 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
5977 spa_free_sync_cb
, zio
, tx
), ==, 0);
5978 VERIFY0(zio_wait(zio
));
5982 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
5984 char *packed
= NULL
;
5989 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
5992 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
5993 * information. This avoids the dmu_buf_will_dirty() path and
5994 * saves us a pre-read to get data we don't actually care about.
5996 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
5997 packed
= vmem_alloc(bufsize
, KM_SLEEP
);
5999 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
6001 bzero(packed
+ nvsize
, bufsize
- nvsize
);
6003 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
6005 vmem_free(packed
, bufsize
);
6007 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
6008 dmu_buf_will_dirty(db
, tx
);
6009 *(uint64_t *)db
->db_data
= nvsize
;
6010 dmu_buf_rele(db
, FTAG
);
6014 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
6015 const char *config
, const char *entry
)
6025 * Update the MOS nvlist describing the list of available devices.
6026 * spa_validate_aux() will have already made sure this nvlist is
6027 * valid and the vdevs are labeled appropriately.
6029 if (sav
->sav_object
== 0) {
6030 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
6031 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
6032 sizeof (uint64_t), tx
);
6033 VERIFY(zap_update(spa
->spa_meta_objset
,
6034 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
6035 &sav
->sav_object
, tx
) == 0);
6038 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
6039 if (sav
->sav_count
== 0) {
6040 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, NULL
, 0) == 0);
6042 list
= kmem_alloc(sav
->sav_count
*sizeof (void *), KM_SLEEP
);
6043 for (i
= 0; i
< sav
->sav_count
; i
++)
6044 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
6045 B_FALSE
, VDEV_CONFIG_L2CACHE
);
6046 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, list
,
6047 sav
->sav_count
) == 0);
6048 for (i
= 0; i
< sav
->sav_count
; i
++)
6049 nvlist_free(list
[i
]);
6050 kmem_free(list
, sav
->sav_count
* sizeof (void *));
6053 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
6054 nvlist_free(nvroot
);
6056 sav
->sav_sync
= B_FALSE
;
6060 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
6064 if (list_is_empty(&spa
->spa_config_dirty_list
))
6067 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6069 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
6070 dmu_tx_get_txg(tx
), B_FALSE
);
6073 * If we're upgrading the spa version then make sure that
6074 * the config object gets updated with the correct version.
6076 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
6077 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
6078 spa
->spa_uberblock
.ub_version
);
6080 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6082 if (spa
->spa_config_syncing
)
6083 nvlist_free(spa
->spa_config_syncing
);
6084 spa
->spa_config_syncing
= config
;
6086 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
6090 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
6092 uint64_t *versionp
= arg
;
6093 uint64_t version
= *versionp
;
6094 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6097 * Setting the version is special cased when first creating the pool.
6099 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
6101 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
6102 ASSERT(version
>= spa_version(spa
));
6104 spa
->spa_uberblock
.ub_version
= version
;
6105 vdev_config_dirty(spa
->spa_root_vdev
);
6106 spa_history_log_internal(spa
, "set", tx
, "version=%lld", version
);
6110 * Set zpool properties.
6113 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
6115 nvlist_t
*nvp
= arg
;
6116 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6117 objset_t
*mos
= spa
->spa_meta_objset
;
6118 nvpair_t
*elem
= NULL
;
6120 mutex_enter(&spa
->spa_props_lock
);
6122 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
6124 char *strval
, *fname
;
6126 const char *propname
;
6127 zprop_type_t proptype
;
6130 prop
= zpool_name_to_prop(nvpair_name(elem
));
6131 switch ((int)prop
) {
6134 * We checked this earlier in spa_prop_validate().
6136 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
6138 fname
= strchr(nvpair_name(elem
), '@') + 1;
6139 VERIFY0(zfeature_lookup_name(fname
, &fid
));
6141 spa_feature_enable(spa
, fid
, tx
);
6142 spa_history_log_internal(spa
, "set", tx
,
6143 "%s=enabled", nvpair_name(elem
));
6146 case ZPOOL_PROP_VERSION
:
6147 intval
= fnvpair_value_uint64(elem
);
6149 * The version is synced seperatly before other
6150 * properties and should be correct by now.
6152 ASSERT3U(spa_version(spa
), >=, intval
);
6155 case ZPOOL_PROP_ALTROOT
:
6157 * 'altroot' is a non-persistent property. It should
6158 * have been set temporarily at creation or import time.
6160 ASSERT(spa
->spa_root
!= NULL
);
6163 case ZPOOL_PROP_READONLY
:
6164 case ZPOOL_PROP_CACHEFILE
:
6166 * 'readonly' and 'cachefile' are also non-persisitent
6170 case ZPOOL_PROP_COMMENT
:
6171 strval
= fnvpair_value_string(elem
);
6172 if (spa
->spa_comment
!= NULL
)
6173 spa_strfree(spa
->spa_comment
);
6174 spa
->spa_comment
= spa_strdup(strval
);
6176 * We need to dirty the configuration on all the vdevs
6177 * so that their labels get updated. It's unnecessary
6178 * to do this for pool creation since the vdev's
6179 * configuratoin has already been dirtied.
6181 if (tx
->tx_txg
!= TXG_INITIAL
)
6182 vdev_config_dirty(spa
->spa_root_vdev
);
6183 spa_history_log_internal(spa
, "set", tx
,
6184 "%s=%s", nvpair_name(elem
), strval
);
6188 * Set pool property values in the poolprops mos object.
6190 if (spa
->spa_pool_props_object
== 0) {
6191 spa
->spa_pool_props_object
=
6192 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
6193 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
6197 /* normalize the property name */
6198 propname
= zpool_prop_to_name(prop
);
6199 proptype
= zpool_prop_get_type(prop
);
6201 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
6202 ASSERT(proptype
== PROP_TYPE_STRING
);
6203 strval
= fnvpair_value_string(elem
);
6204 VERIFY0(zap_update(mos
,
6205 spa
->spa_pool_props_object
, propname
,
6206 1, strlen(strval
) + 1, strval
, tx
));
6207 spa_history_log_internal(spa
, "set", tx
,
6208 "%s=%s", nvpair_name(elem
), strval
);
6209 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
6210 intval
= fnvpair_value_uint64(elem
);
6212 if (proptype
== PROP_TYPE_INDEX
) {
6214 VERIFY0(zpool_prop_index_to_string(
6215 prop
, intval
, &unused
));
6217 VERIFY0(zap_update(mos
,
6218 spa
->spa_pool_props_object
, propname
,
6219 8, 1, &intval
, tx
));
6220 spa_history_log_internal(spa
, "set", tx
,
6221 "%s=%lld", nvpair_name(elem
), intval
);
6223 ASSERT(0); /* not allowed */
6227 case ZPOOL_PROP_DELEGATION
:
6228 spa
->spa_delegation
= intval
;
6230 case ZPOOL_PROP_BOOTFS
:
6231 spa
->spa_bootfs
= intval
;
6233 case ZPOOL_PROP_FAILUREMODE
:
6234 spa
->spa_failmode
= intval
;
6236 case ZPOOL_PROP_AUTOEXPAND
:
6237 spa
->spa_autoexpand
= intval
;
6238 if (tx
->tx_txg
!= TXG_INITIAL
)
6239 spa_async_request(spa
,
6240 SPA_ASYNC_AUTOEXPAND
);
6242 case ZPOOL_PROP_DEDUPDITTO
:
6243 spa
->spa_dedup_ditto
= intval
;
6252 mutex_exit(&spa
->spa_props_lock
);
6256 * Perform one-time upgrade on-disk changes. spa_version() does not
6257 * reflect the new version this txg, so there must be no changes this
6258 * txg to anything that the upgrade code depends on after it executes.
6259 * Therefore this must be called after dsl_pool_sync() does the sync
6263 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
6265 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6267 ASSERT(spa
->spa_sync_pass
== 1);
6269 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
6271 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
6272 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
6273 dsl_pool_create_origin(dp
, tx
);
6275 /* Keeping the origin open increases spa_minref */
6276 spa
->spa_minref
+= 3;
6279 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
6280 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
6281 dsl_pool_upgrade_clones(dp
, tx
);
6284 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
6285 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
6286 dsl_pool_upgrade_dir_clones(dp
, tx
);
6288 /* Keeping the freedir open increases spa_minref */
6289 spa
->spa_minref
+= 3;
6292 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
6293 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6294 spa_feature_create_zap_objects(spa
, tx
);
6298 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6299 * when possibility to use lz4 compression for metadata was added
6300 * Old pools that have this feature enabled must be upgraded to have
6301 * this feature active
6303 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6304 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
6305 SPA_FEATURE_LZ4_COMPRESS
);
6306 boolean_t lz4_ac
= spa_feature_is_active(spa
,
6307 SPA_FEATURE_LZ4_COMPRESS
);
6309 if (lz4_en
&& !lz4_ac
)
6310 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
6312 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
6316 * Sync the specified transaction group. New blocks may be dirtied as
6317 * part of the process, so we iterate until it converges.
6320 spa_sync(spa_t
*spa
, uint64_t txg
)
6322 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6323 objset_t
*mos
= spa
->spa_meta_objset
;
6324 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
6325 vdev_t
*rvd
= spa
->spa_root_vdev
;
6331 VERIFY(spa_writeable(spa
));
6334 * Lock out configuration changes.
6336 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6338 spa
->spa_syncing_txg
= txg
;
6339 spa
->spa_sync_pass
= 0;
6342 * If there are any pending vdev state changes, convert them
6343 * into config changes that go out with this transaction group.
6345 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6346 while (list_head(&spa
->spa_state_dirty_list
) != NULL
) {
6348 * We need the write lock here because, for aux vdevs,
6349 * calling vdev_config_dirty() modifies sav_config.
6350 * This is ugly and will become unnecessary when we
6351 * eliminate the aux vdev wart by integrating all vdevs
6352 * into the root vdev tree.
6354 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6355 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
6356 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
6357 vdev_state_clean(vd
);
6358 vdev_config_dirty(vd
);
6360 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6361 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
6363 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6365 tx
= dmu_tx_create_assigned(dp
, txg
);
6367 spa
->spa_sync_starttime
= gethrtime();
6368 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
6369 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_taskq
,
6370 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
6371 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
6374 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6375 * set spa_deflate if we have no raid-z vdevs.
6377 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
6378 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
6381 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
6382 vd
= rvd
->vdev_child
[i
];
6383 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
6386 if (i
== rvd
->vdev_children
) {
6387 spa
->spa_deflate
= TRUE
;
6388 VERIFY(0 == zap_add(spa
->spa_meta_objset
,
6389 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
6390 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
6395 * If anything has changed in this txg, or if someone is waiting
6396 * for this txg to sync (eg, spa_vdev_remove()), push the
6397 * deferred frees from the previous txg. If not, leave them
6398 * alone so that we don't generate work on an otherwise idle
6401 if (!txg_list_empty(&dp
->dp_dirty_datasets
, txg
) ||
6402 !txg_list_empty(&dp
->dp_dirty_dirs
, txg
) ||
6403 !txg_list_empty(&dp
->dp_sync_tasks
, txg
) ||
6404 ((dsl_scan_active(dp
->dp_scan
) ||
6405 txg_sync_waiting(dp
)) && !spa_shutting_down(spa
))) {
6406 spa_sync_deferred_frees(spa
, tx
);
6410 * Iterate to convergence.
6413 int pass
= ++spa
->spa_sync_pass
;
6415 spa_sync_config_object(spa
, tx
);
6416 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
6417 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
6418 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
6419 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
6420 spa_errlog_sync(spa
, txg
);
6421 dsl_pool_sync(dp
, txg
);
6423 if (pass
< zfs_sync_pass_deferred_free
) {
6424 spa_sync_frees(spa
, free_bpl
, tx
);
6426 bplist_iterate(free_bpl
, bpobj_enqueue_cb
,
6427 &spa
->spa_deferred_bpobj
, tx
);
6431 dsl_scan_sync(dp
, tx
);
6433 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
)))
6437 spa_sync_upgrades(spa
, tx
);
6439 } while (dmu_objset_is_dirty(mos
, txg
));
6442 * Rewrite the vdev configuration (which includes the uberblock)
6443 * to commit the transaction group.
6445 * If there are no dirty vdevs, we sync the uberblock to a few
6446 * random top-level vdevs that are known to be visible in the
6447 * config cache (see spa_vdev_add() for a complete description).
6448 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6452 * We hold SCL_STATE to prevent vdev open/close/etc.
6453 * while we're attempting to write the vdev labels.
6455 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6457 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
6458 vdev_t
*svd
[SPA_DVAS_PER_BP
];
6460 int children
= rvd
->vdev_children
;
6461 int c0
= spa_get_random(children
);
6463 for (c
= 0; c
< children
; c
++) {
6464 vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
6465 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
)
6467 svd
[svdcount
++] = vd
;
6468 if (svdcount
== SPA_DVAS_PER_BP
)
6471 error
= vdev_config_sync(svd
, svdcount
, txg
, B_FALSE
);
6473 error
= vdev_config_sync(svd
, svdcount
, txg
,
6476 error
= vdev_config_sync(rvd
->vdev_child
,
6477 rvd
->vdev_children
, txg
, B_FALSE
);
6479 error
= vdev_config_sync(rvd
->vdev_child
,
6480 rvd
->vdev_children
, txg
, B_TRUE
);
6484 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
6486 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6490 zio_suspend(spa
, NULL
);
6491 zio_resume_wait(spa
);
6495 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
6496 spa
->spa_deadman_tqid
= 0;
6499 * Clear the dirty config list.
6501 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
6502 vdev_config_clean(vd
);
6505 * Now that the new config has synced transactionally,
6506 * let it become visible to the config cache.
6508 if (spa
->spa_config_syncing
!= NULL
) {
6509 spa_config_set(spa
, spa
->spa_config_syncing
);
6510 spa
->spa_config_txg
= txg
;
6511 spa
->spa_config_syncing
= NULL
;
6514 spa
->spa_ubsync
= spa
->spa_uberblock
;
6516 dsl_pool_sync_done(dp
, txg
);
6519 * Update usable space statistics.
6521 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
))))
6522 vdev_sync_done(vd
, txg
);
6524 spa_update_dspace(spa
);
6527 * It had better be the case that we didn't dirty anything
6528 * since vdev_config_sync().
6530 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
6531 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
6532 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
6534 spa
->spa_sync_pass
= 0;
6536 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6538 spa_handle_ignored_writes(spa
);
6541 * If any async tasks have been requested, kick them off.
6543 spa_async_dispatch(spa
);
6547 * Sync all pools. We don't want to hold the namespace lock across these
6548 * operations, so we take a reference on the spa_t and drop the lock during the
6552 spa_sync_allpools(void)
6555 mutex_enter(&spa_namespace_lock
);
6556 while ((spa
= spa_next(spa
)) != NULL
) {
6557 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
6558 !spa_writeable(spa
) || spa_suspended(spa
))
6560 spa_open_ref(spa
, FTAG
);
6561 mutex_exit(&spa_namespace_lock
);
6562 txg_wait_synced(spa_get_dsl(spa
), 0);
6563 mutex_enter(&spa_namespace_lock
);
6564 spa_close(spa
, FTAG
);
6566 mutex_exit(&spa_namespace_lock
);
6570 * ==========================================================================
6571 * Miscellaneous routines
6572 * ==========================================================================
6576 * Remove all pools in the system.
6584 * Remove all cached state. All pools should be closed now,
6585 * so every spa in the AVL tree should be unreferenced.
6587 mutex_enter(&spa_namespace_lock
);
6588 while ((spa
= spa_next(NULL
)) != NULL
) {
6590 * Stop async tasks. The async thread may need to detach
6591 * a device that's been replaced, which requires grabbing
6592 * spa_namespace_lock, so we must drop it here.
6594 spa_open_ref(spa
, FTAG
);
6595 mutex_exit(&spa_namespace_lock
);
6596 spa_async_suspend(spa
);
6597 mutex_enter(&spa_namespace_lock
);
6598 spa_close(spa
, FTAG
);
6600 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
6602 spa_deactivate(spa
);
6606 mutex_exit(&spa_namespace_lock
);
6610 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
6615 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
6619 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
6620 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
6621 if (vd
->vdev_guid
== guid
)
6625 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
6626 vd
= spa
->spa_spares
.sav_vdevs
[i
];
6627 if (vd
->vdev_guid
== guid
)
6636 spa_upgrade(spa_t
*spa
, uint64_t version
)
6638 ASSERT(spa_writeable(spa
));
6640 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6643 * This should only be called for a non-faulted pool, and since a
6644 * future version would result in an unopenable pool, this shouldn't be
6647 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
6648 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
6650 spa
->spa_uberblock
.ub_version
= version
;
6651 vdev_config_dirty(spa
->spa_root_vdev
);
6653 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6655 txg_wait_synced(spa_get_dsl(spa
), 0);
6659 spa_has_spare(spa_t
*spa
, uint64_t guid
)
6663 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
6665 for (i
= 0; i
< sav
->sav_count
; i
++)
6666 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
6669 for (i
= 0; i
< sav
->sav_npending
; i
++) {
6670 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
6671 &spareguid
) == 0 && spareguid
== guid
)
6679 * Check if a pool has an active shared spare device.
6680 * Note: reference count of an active spare is 2, as a spare and as a replace
6683 spa_has_active_shared_spare(spa_t
*spa
)
6687 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
6689 for (i
= 0; i
< sav
->sav_count
; i
++) {
6690 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
6691 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
6700 * Post a FM_EREPORT_ZFS_* event from sys/fm/fs/zfs.h. The payload will be
6701 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6702 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6703 * or zdb as real changes.
6706 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, const char *name
)
6709 zfs_ereport_post(name
, spa
, vd
, NULL
, 0, 0);
6713 #if defined(_KERNEL) && defined(HAVE_SPL)
6714 /* state manipulation functions */
6715 EXPORT_SYMBOL(spa_open
);
6716 EXPORT_SYMBOL(spa_open_rewind
);
6717 EXPORT_SYMBOL(spa_get_stats
);
6718 EXPORT_SYMBOL(spa_create
);
6719 EXPORT_SYMBOL(spa_import_rootpool
);
6720 EXPORT_SYMBOL(spa_import
);
6721 EXPORT_SYMBOL(spa_tryimport
);
6722 EXPORT_SYMBOL(spa_destroy
);
6723 EXPORT_SYMBOL(spa_export
);
6724 EXPORT_SYMBOL(spa_reset
);
6725 EXPORT_SYMBOL(spa_async_request
);
6726 EXPORT_SYMBOL(spa_async_suspend
);
6727 EXPORT_SYMBOL(spa_async_resume
);
6728 EXPORT_SYMBOL(spa_inject_addref
);
6729 EXPORT_SYMBOL(spa_inject_delref
);
6730 EXPORT_SYMBOL(spa_scan_stat_init
);
6731 EXPORT_SYMBOL(spa_scan_get_stats
);
6733 /* device maniion */
6734 EXPORT_SYMBOL(spa_vdev_add
);
6735 EXPORT_SYMBOL(spa_vdev_attach
);
6736 EXPORT_SYMBOL(spa_vdev_detach
);
6737 EXPORT_SYMBOL(spa_vdev_remove
);
6738 EXPORT_SYMBOL(spa_vdev_setpath
);
6739 EXPORT_SYMBOL(spa_vdev_setfru
);
6740 EXPORT_SYMBOL(spa_vdev_split_mirror
);
6742 /* spare statech is global across all pools) */
6743 EXPORT_SYMBOL(spa_spare_add
);
6744 EXPORT_SYMBOL(spa_spare_remove
);
6745 EXPORT_SYMBOL(spa_spare_exists
);
6746 EXPORT_SYMBOL(spa_spare_activate
);
6748 /* L2ARC statech is global across all pools) */
6749 EXPORT_SYMBOL(spa_l2cache_add
);
6750 EXPORT_SYMBOL(spa_l2cache_remove
);
6751 EXPORT_SYMBOL(spa_l2cache_exists
);
6752 EXPORT_SYMBOL(spa_l2cache_activate
);
6753 EXPORT_SYMBOL(spa_l2cache_drop
);
6756 EXPORT_SYMBOL(spa_scan
);
6757 EXPORT_SYMBOL(spa_scan_stop
);
6760 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
6761 EXPORT_SYMBOL(spa_sync_allpools
);
6764 EXPORT_SYMBOL(spa_prop_set
);
6765 EXPORT_SYMBOL(spa_prop_get
);
6766 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
6768 /* asynchronous event notification */
6769 EXPORT_SYMBOL(spa_event_notify
);
6772 #if defined(_KERNEL) && defined(HAVE_SPL)
6773 module_param(spa_load_verify_maxinflight
, int, 0644);
6774 MODULE_PARM_DESC(spa_load_verify_maxinflight
,
6775 "Max concurrent traversal I/Os while verifying pool during import -X");
6777 module_param(spa_load_verify_metadata
, int, 0644);
6778 MODULE_PARM_DESC(spa_load_verify_metadata
,
6779 "Set to traverse metadata on pool import");
6781 module_param(spa_load_verify_data
, int, 0644);
6782 MODULE_PARM_DESC(spa_load_verify_data
,
6783 "Set to traverse data on pool import");