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) 2015, Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2013, 2014, Nexenta Systems, Inc. All rights reserved.
27 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
28 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
32 * SPA: Storage Pool Allocator
34 * This file contains all the routines used when modifying on-disk SPA state.
35 * This includes opening, importing, destroying, exporting a pool, and syncing a
39 #include <sys/zfs_context.h>
40 #include <sys/fm/fs/zfs.h>
41 #include <sys/spa_impl.h>
43 #include <sys/zio_checksum.h>
45 #include <sys/dmu_tx.h>
49 #include <sys/vdev_impl.h>
50 #include <sys/vdev_disk.h>
51 #include <sys/metaslab.h>
52 #include <sys/metaslab_impl.h>
53 #include <sys/uberblock_impl.h>
56 #include <sys/dmu_traverse.h>
57 #include <sys/dmu_objset.h>
58 #include <sys/unique.h>
59 #include <sys/dsl_pool.h>
60 #include <sys/dsl_dataset.h>
61 #include <sys/dsl_dir.h>
62 #include <sys/dsl_prop.h>
63 #include <sys/dsl_synctask.h>
64 #include <sys/fs/zfs.h>
66 #include <sys/callb.h>
67 #include <sys/systeminfo.h>
68 #include <sys/spa_boot.h>
69 #include <sys/zfs_ioctl.h>
70 #include <sys/dsl_scan.h>
71 #include <sys/zfeature.h>
72 #include <sys/dsl_destroy.h>
76 #include <sys/bootprops.h>
77 #include <sys/callb.h>
78 #include <sys/cpupart.h>
80 #include <sys/sysdc.h>
85 #include "zfs_comutil.h"
88 * The interval, in seconds, at which failed configuration cache file writes
91 static int zfs_ccw_retry_interval
= 300;
93 typedef enum zti_modes
{
94 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
95 ZTI_MODE_BATCH
, /* cpu-intensive; value is ignored */
96 ZTI_MODE_NULL
, /* don't create a taskq */
100 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
101 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
102 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
103 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
105 #define ZTI_N(n) ZTI_P(n, 1)
106 #define ZTI_ONE ZTI_N(1)
108 typedef struct zio_taskq_info
{
109 zti_modes_t zti_mode
;
114 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
115 "iss", "iss_h", "int", "int_h"
119 * This table defines the taskq settings for each ZFS I/O type. When
120 * initializing a pool, we use this table to create an appropriately sized
121 * taskq. Some operations are low volume and therefore have a small, static
122 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
123 * macros. Other operations process a large amount of data; the ZTI_BATCH
124 * macro causes us to create a taskq oriented for throughput. Some operations
125 * are so high frequency and short-lived that the taskq itself can become a a
126 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
127 * additional degree of parallelism specified by the number of threads per-
128 * taskq and the number of taskqs; when dispatching an event in this case, the
129 * particular taskq is chosen at random.
131 * The different taskq priorities are to handle the different contexts (issue
132 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
133 * need to be handled with minimum delay.
135 const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
136 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
137 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
138 { ZTI_N(8), ZTI_NULL
, ZTI_P(12, 8), ZTI_NULL
}, /* READ */
139 { ZTI_BATCH
, ZTI_N(5), ZTI_P(12, 8), ZTI_N(5) }, /* WRITE */
140 { ZTI_P(12, 8), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
141 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
142 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
145 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
146 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
147 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
148 static inline int spa_load_impl(spa_t
*spa
, uint64_t, nvlist_t
*config
,
149 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
151 static void spa_vdev_resilver_done(spa_t
*spa
);
153 uint_t zio_taskq_batch_pct
= 75; /* 1 thread per cpu in pset */
154 id_t zio_taskq_psrset_bind
= PS_NONE
;
155 boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
156 uint_t zio_taskq_basedc
= 80; /* base duty cycle */
158 boolean_t spa_create_process
= B_TRUE
; /* no process ==> no sysdc */
161 * This (illegal) pool name is used when temporarily importing a spa_t in order
162 * to get the vdev stats associated with the imported devices.
164 #define TRYIMPORT_NAME "$import"
167 * ==========================================================================
168 * SPA properties routines
169 * ==========================================================================
173 * Add a (source=src, propname=propval) list to an nvlist.
176 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, char *strval
,
177 uint64_t intval
, zprop_source_t src
)
179 const char *propname
= zpool_prop_to_name(prop
);
182 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
183 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
186 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
188 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, intval
) == 0);
190 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
191 nvlist_free(propval
);
195 * Get property values from the spa configuration.
198 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
200 vdev_t
*rvd
= spa
->spa_root_vdev
;
201 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
202 uint64_t size
, alloc
, cap
, version
;
203 const zprop_source_t src
= ZPROP_SRC_NONE
;
204 spa_config_dirent_t
*dp
;
205 metaslab_class_t
*mc
= spa_normal_class(spa
);
207 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
210 alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
211 size
= metaslab_class_get_space(spa_normal_class(spa
));
212 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
213 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
214 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
215 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
218 spa_prop_add_list(*nvp
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
219 metaslab_class_fragmentation(mc
), src
);
220 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
,
221 metaslab_class_expandable_space(mc
), src
);
222 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
223 (spa_mode(spa
) == FREAD
), src
);
225 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
226 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
228 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
229 ddt_get_pool_dedup_ratio(spa
), src
);
231 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
232 rvd
->vdev_state
, src
);
234 version
= spa_version(spa
);
235 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
)) {
236 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
237 version
, ZPROP_SRC_DEFAULT
);
239 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
,
240 version
, ZPROP_SRC_LOCAL
);
246 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
247 * when opening pools before this version freedir will be NULL.
249 if (pool
->dp_free_dir
!= NULL
) {
250 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
251 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
254 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
258 if (pool
->dp_leak_dir
!= NULL
) {
259 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
260 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
263 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
268 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
270 if (spa
->spa_comment
!= NULL
) {
271 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
275 if (spa
->spa_root
!= NULL
)
276 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
279 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
280 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
281 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
283 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
284 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
287 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_DNODE
)) {
288 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
289 DNODE_MAX_SIZE
, ZPROP_SRC_NONE
);
291 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXDNODESIZE
, NULL
,
292 DNODE_MIN_SIZE
, ZPROP_SRC_NONE
);
295 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
296 if (dp
->scd_path
== NULL
) {
297 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
298 "none", 0, ZPROP_SRC_LOCAL
);
299 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
300 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
301 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
307 * Get zpool property values.
310 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
312 objset_t
*mos
= spa
->spa_meta_objset
;
317 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
);
321 mutex_enter(&spa
->spa_props_lock
);
324 * Get properties from the spa config.
326 spa_prop_get_config(spa
, nvp
);
328 /* If no pool property object, no more prop to get. */
329 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0) {
330 mutex_exit(&spa
->spa_props_lock
);
335 * Get properties from the MOS pool property object.
337 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
338 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
339 zap_cursor_advance(&zc
)) {
342 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
345 if ((prop
= zpool_name_to_prop(za
.za_name
)) == ZPROP_INVAL
)
348 switch (za
.za_integer_length
) {
350 /* integer property */
351 if (za
.za_first_integer
!=
352 zpool_prop_default_numeric(prop
))
353 src
= ZPROP_SRC_LOCAL
;
355 if (prop
== ZPOOL_PROP_BOOTFS
) {
357 dsl_dataset_t
*ds
= NULL
;
359 dp
= spa_get_dsl(spa
);
360 dsl_pool_config_enter(dp
, FTAG
);
361 if ((err
= dsl_dataset_hold_obj(dp
,
362 za
.za_first_integer
, FTAG
, &ds
))) {
363 dsl_pool_config_exit(dp
, FTAG
);
367 strval
= kmem_alloc(ZFS_MAX_DATASET_NAME_LEN
,
369 dsl_dataset_name(ds
, strval
);
370 dsl_dataset_rele(ds
, FTAG
);
371 dsl_pool_config_exit(dp
, FTAG
);
374 intval
= za
.za_first_integer
;
377 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
380 kmem_free(strval
, ZFS_MAX_DATASET_NAME_LEN
);
385 /* string property */
386 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
387 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
388 za
.za_name
, 1, za
.za_num_integers
, strval
);
390 kmem_free(strval
, za
.za_num_integers
);
393 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
394 kmem_free(strval
, za
.za_num_integers
);
401 zap_cursor_fini(&zc
);
402 mutex_exit(&spa
->spa_props_lock
);
404 if (err
&& err
!= ENOENT
) {
414 * Validate the given pool properties nvlist and modify the list
415 * for the property values to be set.
418 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
421 int error
= 0, reset_bootfs
= 0;
423 boolean_t has_feature
= B_FALSE
;
426 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
428 char *strval
, *slash
, *check
, *fname
;
429 const char *propname
= nvpair_name(elem
);
430 zpool_prop_t prop
= zpool_name_to_prop(propname
);
434 if (!zpool_prop_feature(propname
)) {
435 error
= SET_ERROR(EINVAL
);
440 * Sanitize the input.
442 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
443 error
= SET_ERROR(EINVAL
);
447 if (nvpair_value_uint64(elem
, &intval
) != 0) {
448 error
= SET_ERROR(EINVAL
);
453 error
= SET_ERROR(EINVAL
);
457 fname
= strchr(propname
, '@') + 1;
458 if (zfeature_lookup_name(fname
, NULL
) != 0) {
459 error
= SET_ERROR(EINVAL
);
463 has_feature
= B_TRUE
;
466 case ZPOOL_PROP_VERSION
:
467 error
= nvpair_value_uint64(elem
, &intval
);
469 (intval
< spa_version(spa
) ||
470 intval
> SPA_VERSION_BEFORE_FEATURES
||
472 error
= SET_ERROR(EINVAL
);
475 case ZPOOL_PROP_DELEGATION
:
476 case ZPOOL_PROP_AUTOREPLACE
:
477 case ZPOOL_PROP_LISTSNAPS
:
478 case ZPOOL_PROP_AUTOEXPAND
:
479 error
= nvpair_value_uint64(elem
, &intval
);
480 if (!error
&& intval
> 1)
481 error
= SET_ERROR(EINVAL
);
484 case ZPOOL_PROP_BOOTFS
:
486 * If the pool version is less than SPA_VERSION_BOOTFS,
487 * or the pool is still being created (version == 0),
488 * the bootfs property cannot be set.
490 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
491 error
= SET_ERROR(ENOTSUP
);
496 * Make sure the vdev config is bootable
498 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
499 error
= SET_ERROR(ENOTSUP
);
505 error
= nvpair_value_string(elem
, &strval
);
511 if (strval
== NULL
|| strval
[0] == '\0') {
512 objnum
= zpool_prop_default_numeric(
517 error
= dmu_objset_hold(strval
, FTAG
, &os
);
522 * Must be ZPL, and its property settings
523 * must be supported by GRUB (compression
524 * is not gzip, and large blocks or large
525 * dnodes are not used).
528 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
529 error
= SET_ERROR(ENOTSUP
);
531 dsl_prop_get_int_ds(dmu_objset_ds(os
),
532 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
534 !BOOTFS_COMPRESS_VALID(propval
)) {
535 error
= SET_ERROR(ENOTSUP
);
537 dsl_prop_get_int_ds(dmu_objset_ds(os
),
538 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
),
540 propval
> SPA_OLD_MAXBLOCKSIZE
) {
541 error
= SET_ERROR(ENOTSUP
);
543 dsl_prop_get_int_ds(dmu_objset_ds(os
),
544 zfs_prop_to_name(ZFS_PROP_DNODESIZE
),
546 propval
!= ZFS_DNSIZE_LEGACY
) {
547 error
= SET_ERROR(ENOTSUP
);
549 objnum
= dmu_objset_id(os
);
551 dmu_objset_rele(os
, FTAG
);
555 case ZPOOL_PROP_FAILUREMODE
:
556 error
= nvpair_value_uint64(elem
, &intval
);
557 if (!error
&& (intval
< ZIO_FAILURE_MODE_WAIT
||
558 intval
> ZIO_FAILURE_MODE_PANIC
))
559 error
= SET_ERROR(EINVAL
);
562 * This is a special case which only occurs when
563 * the pool has completely failed. This allows
564 * the user to change the in-core failmode property
565 * without syncing it out to disk (I/Os might
566 * currently be blocked). We do this by returning
567 * EIO to the caller (spa_prop_set) to trick it
568 * into thinking we encountered a property validation
571 if (!error
&& spa_suspended(spa
)) {
572 spa
->spa_failmode
= intval
;
573 error
= SET_ERROR(EIO
);
577 case ZPOOL_PROP_CACHEFILE
:
578 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
581 if (strval
[0] == '\0')
584 if (strcmp(strval
, "none") == 0)
587 if (strval
[0] != '/') {
588 error
= SET_ERROR(EINVAL
);
592 slash
= strrchr(strval
, '/');
593 ASSERT(slash
!= NULL
);
595 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
596 strcmp(slash
, "/..") == 0)
597 error
= SET_ERROR(EINVAL
);
600 case ZPOOL_PROP_COMMENT
:
601 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
603 for (check
= strval
; *check
!= '\0'; check
++) {
604 if (!isprint(*check
)) {
605 error
= SET_ERROR(EINVAL
);
609 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
610 error
= SET_ERROR(E2BIG
);
613 case ZPOOL_PROP_DEDUPDITTO
:
614 if (spa_version(spa
) < SPA_VERSION_DEDUP
)
615 error
= SET_ERROR(ENOTSUP
);
617 error
= nvpair_value_uint64(elem
, &intval
);
619 intval
!= 0 && intval
< ZIO_DEDUPDITTO_MIN
)
620 error
= SET_ERROR(EINVAL
);
631 if (!error
&& reset_bootfs
) {
632 error
= nvlist_remove(props
,
633 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
636 error
= nvlist_add_uint64(props
,
637 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
645 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
648 spa_config_dirent_t
*dp
;
650 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
654 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
657 if (cachefile
[0] == '\0')
658 dp
->scd_path
= spa_strdup(spa_config_path
);
659 else if (strcmp(cachefile
, "none") == 0)
662 dp
->scd_path
= spa_strdup(cachefile
);
664 list_insert_head(&spa
->spa_config_list
, dp
);
666 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
670 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
673 nvpair_t
*elem
= NULL
;
674 boolean_t need_sync
= B_FALSE
;
676 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
679 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
680 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
682 if (prop
== ZPOOL_PROP_CACHEFILE
||
683 prop
== ZPOOL_PROP_ALTROOT
||
684 prop
== ZPOOL_PROP_READONLY
)
687 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPROP_INVAL
) {
690 if (prop
== ZPOOL_PROP_VERSION
) {
691 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
693 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
694 ver
= SPA_VERSION_FEATURES
;
698 /* Save time if the version is already set. */
699 if (ver
== spa_version(spa
))
703 * In addition to the pool directory object, we might
704 * create the pool properties object, the features for
705 * read object, the features for write object, or the
706 * feature descriptions object.
708 error
= dsl_sync_task(spa
->spa_name
, NULL
,
709 spa_sync_version
, &ver
,
710 6, ZFS_SPACE_CHECK_RESERVED
);
721 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
722 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
729 * If the bootfs property value is dsobj, clear it.
732 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
734 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
735 VERIFY(zap_remove(spa
->spa_meta_objset
,
736 spa
->spa_pool_props_object
,
737 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
744 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
746 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
747 vdev_t
*rvd
= spa
->spa_root_vdev
;
749 ASSERTV(uint64_t *newguid
= arg
);
751 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
752 vdev_state
= rvd
->vdev_state
;
753 spa_config_exit(spa
, SCL_STATE
, FTAG
);
755 if (vdev_state
!= VDEV_STATE_HEALTHY
)
756 return (SET_ERROR(ENXIO
));
758 ASSERT3U(spa_guid(spa
), !=, *newguid
);
764 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
766 uint64_t *newguid
= arg
;
767 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
769 vdev_t
*rvd
= spa
->spa_root_vdev
;
771 oldguid
= spa_guid(spa
);
773 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
774 rvd
->vdev_guid
= *newguid
;
775 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
776 vdev_config_dirty(rvd
);
777 spa_config_exit(spa
, SCL_STATE
, FTAG
);
779 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
784 * Change the GUID for the pool. This is done so that we can later
785 * re-import a pool built from a clone of our own vdevs. We will modify
786 * the root vdev's guid, our own pool guid, and then mark all of our
787 * vdevs dirty. Note that we must make sure that all our vdevs are
788 * online when we do this, or else any vdevs that weren't present
789 * would be orphaned from our pool. We are also going to issue a
790 * sysevent to update any watchers.
793 spa_change_guid(spa_t
*spa
)
798 mutex_enter(&spa
->spa_vdev_top_lock
);
799 mutex_enter(&spa_namespace_lock
);
800 guid
= spa_generate_guid(NULL
);
802 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
803 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
806 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
807 spa_event_notify(spa
, NULL
, ESC_ZFS_POOL_REGUID
);
810 mutex_exit(&spa_namespace_lock
);
811 mutex_exit(&spa
->spa_vdev_top_lock
);
817 * ==========================================================================
818 * SPA state manipulation (open/create/destroy/import/export)
819 * ==========================================================================
823 spa_error_entry_compare(const void *a
, const void *b
)
825 spa_error_entry_t
*sa
= (spa_error_entry_t
*)a
;
826 spa_error_entry_t
*sb
= (spa_error_entry_t
*)b
;
829 ret
= bcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
830 sizeof (zbookmark_phys_t
));
841 * Utility function which retrieves copies of the current logs and
842 * re-initializes them in the process.
845 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
847 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
849 bcopy(&spa
->spa_errlist_last
, last
, sizeof (avl_tree_t
));
850 bcopy(&spa
->spa_errlist_scrub
, scrub
, sizeof (avl_tree_t
));
852 avl_create(&spa
->spa_errlist_scrub
,
853 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
854 offsetof(spa_error_entry_t
, se_avl
));
855 avl_create(&spa
->spa_errlist_last
,
856 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
857 offsetof(spa_error_entry_t
, se_avl
));
861 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
863 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
864 enum zti_modes mode
= ztip
->zti_mode
;
865 uint_t value
= ztip
->zti_value
;
866 uint_t count
= ztip
->zti_count
;
867 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
869 uint_t i
, flags
= TASKQ_DYNAMIC
;
870 boolean_t batch
= B_FALSE
;
872 if (mode
== ZTI_MODE_NULL
) {
874 tqs
->stqs_taskq
= NULL
;
878 ASSERT3U(count
, >, 0);
880 tqs
->stqs_count
= count
;
881 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
885 ASSERT3U(value
, >=, 1);
886 value
= MAX(value
, 1);
891 flags
|= TASKQ_THREADS_CPU_PCT
;
892 value
= MIN(zio_taskq_batch_pct
, 100);
896 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
898 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
902 for (i
= 0; i
< count
; i
++) {
906 (void) snprintf(name
, sizeof (name
), "%s_%s_%u",
907 zio_type_name
[t
], zio_taskq_types
[q
], i
);
909 (void) snprintf(name
, sizeof (name
), "%s_%s",
910 zio_type_name
[t
], zio_taskq_types
[q
]);
913 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
915 flags
|= TASKQ_DC_BATCH
;
917 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
918 spa
->spa_proc
, zio_taskq_basedc
, flags
);
920 pri_t pri
= maxclsyspri
;
922 * The write issue taskq can be extremely CPU
923 * intensive. Run it at slightly less important
924 * priority than the other taskqs. Under Linux this
925 * means incrementing the priority value on platforms
926 * like illumos it should be decremented.
928 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
)
931 tq
= taskq_create_proc(name
, value
, pri
, 50,
932 INT_MAX
, spa
->spa_proc
, flags
);
935 tqs
->stqs_taskq
[i
] = tq
;
940 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
942 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
945 if (tqs
->stqs_taskq
== NULL
) {
946 ASSERT3U(tqs
->stqs_count
, ==, 0);
950 for (i
= 0; i
< tqs
->stqs_count
; i
++) {
951 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
952 taskq_destroy(tqs
->stqs_taskq
[i
]);
955 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
956 tqs
->stqs_taskq
= NULL
;
960 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
961 * Note that a type may have multiple discrete taskqs to avoid lock contention
962 * on the taskq itself. In that case we choose which taskq at random by using
963 * the low bits of gethrtime().
966 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
967 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
969 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
972 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
973 ASSERT3U(tqs
->stqs_count
, !=, 0);
975 if (tqs
->stqs_count
== 1) {
976 tq
= tqs
->stqs_taskq
[0];
978 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
981 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
985 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
988 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
989 task_func_t
*func
, void *arg
, uint_t flags
)
991 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
995 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
996 ASSERT3U(tqs
->stqs_count
, !=, 0);
998 if (tqs
->stqs_count
== 1) {
999 tq
= tqs
->stqs_taskq
[0];
1001 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
1004 id
= taskq_dispatch(tq
, func
, arg
, flags
);
1006 taskq_wait_id(tq
, id
);
1010 spa_create_zio_taskqs(spa_t
*spa
)
1014 for (t
= 0; t
< ZIO_TYPES
; t
++) {
1015 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1016 spa_taskqs_init(spa
, t
, q
);
1021 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
1023 spa_thread(void *arg
)
1025 callb_cpr_t cprinfo
;
1028 user_t
*pu
= PTOU(curproc
);
1030 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
1033 ASSERT(curproc
!= &p0
);
1034 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
1035 "zpool-%s", spa
->spa_name
);
1036 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
1038 /* bind this thread to the requested psrset */
1039 if (zio_taskq_psrset_bind
!= PS_NONE
) {
1041 mutex_enter(&cpu_lock
);
1042 mutex_enter(&pidlock
);
1043 mutex_enter(&curproc
->p_lock
);
1045 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1046 0, NULL
, NULL
) == 0) {
1047 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1050 "Couldn't bind process for zfs pool \"%s\" to "
1051 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1054 mutex_exit(&curproc
->p_lock
);
1055 mutex_exit(&pidlock
);
1056 mutex_exit(&cpu_lock
);
1060 if (zio_taskq_sysdc
) {
1061 sysdc_thread_enter(curthread
, 100, 0);
1064 spa
->spa_proc
= curproc
;
1065 spa
->spa_did
= curthread
->t_did
;
1067 spa_create_zio_taskqs(spa
);
1069 mutex_enter(&spa
->spa_proc_lock
);
1070 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1072 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1073 cv_broadcast(&spa
->spa_proc_cv
);
1075 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1076 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1077 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1078 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1080 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1081 spa
->spa_proc_state
= SPA_PROC_GONE
;
1082 spa
->spa_proc
= &p0
;
1083 cv_broadcast(&spa
->spa_proc_cv
);
1084 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1086 mutex_enter(&curproc
->p_lock
);
1092 * Activate an uninitialized pool.
1095 spa_activate(spa_t
*spa
, int mode
)
1097 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1099 spa
->spa_state
= POOL_STATE_ACTIVE
;
1100 spa
->spa_mode
= mode
;
1102 spa
->spa_normal_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1103 spa
->spa_log_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1105 /* Try to create a covering process */
1106 mutex_enter(&spa
->spa_proc_lock
);
1107 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1108 ASSERT(spa
->spa_proc
== &p0
);
1111 #ifdef HAVE_SPA_THREAD
1112 /* Only create a process if we're going to be around a while. */
1113 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1114 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1116 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1117 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1118 cv_wait(&spa
->spa_proc_cv
,
1119 &spa
->spa_proc_lock
);
1121 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1122 ASSERT(spa
->spa_proc
!= &p0
);
1123 ASSERT(spa
->spa_did
!= 0);
1127 "Couldn't create process for zfs pool \"%s\"\n",
1132 #endif /* HAVE_SPA_THREAD */
1133 mutex_exit(&spa
->spa_proc_lock
);
1135 /* If we didn't create a process, we need to create our taskqs. */
1136 if (spa
->spa_proc
== &p0
) {
1137 spa_create_zio_taskqs(spa
);
1140 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1141 offsetof(vdev_t
, vdev_config_dirty_node
));
1142 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1143 offsetof(objset_t
, os_evicting_node
));
1144 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1145 offsetof(vdev_t
, vdev_state_dirty_node
));
1147 txg_list_create(&spa
->spa_vdev_txg_list
,
1148 offsetof(struct vdev
, vdev_txg_node
));
1150 avl_create(&spa
->spa_errlist_scrub
,
1151 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1152 offsetof(spa_error_entry_t
, se_avl
));
1153 avl_create(&spa
->spa_errlist_last
,
1154 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1155 offsetof(spa_error_entry_t
, se_avl
));
1158 * This taskq is used to perform zvol-minor-related tasks
1159 * asynchronously. This has several advantages, including easy
1160 * resolution of various deadlocks (zfsonlinux bug #3681).
1162 * The taskq must be single threaded to ensure tasks are always
1163 * processed in the order in which they were dispatched.
1165 * A taskq per pool allows one to keep the pools independent.
1166 * This way if one pool is suspended, it will not impact another.
1168 * The preferred location to dispatch a zvol minor task is a sync
1169 * task. In this context, there is easy access to the spa_t and minimal
1170 * error handling is required because the sync task must succeed.
1172 spa
->spa_zvol_taskq
= taskq_create("z_zvol", 1, defclsyspri
,
1177 * Opposite of spa_activate().
1180 spa_deactivate(spa_t
*spa
)
1184 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1185 ASSERT(spa
->spa_dsl_pool
== NULL
);
1186 ASSERT(spa
->spa_root_vdev
== NULL
);
1187 ASSERT(spa
->spa_async_zio_root
== NULL
);
1188 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1190 spa_evicting_os_wait(spa
);
1192 if (spa
->spa_zvol_taskq
) {
1193 taskq_destroy(spa
->spa_zvol_taskq
);
1194 spa
->spa_zvol_taskq
= NULL
;
1197 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1199 list_destroy(&spa
->spa_config_dirty_list
);
1200 list_destroy(&spa
->spa_evicting_os_list
);
1201 list_destroy(&spa
->spa_state_dirty_list
);
1203 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
1205 for (t
= 0; t
< ZIO_TYPES
; t
++) {
1206 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1207 spa_taskqs_fini(spa
, t
, q
);
1211 metaslab_class_destroy(spa
->spa_normal_class
);
1212 spa
->spa_normal_class
= NULL
;
1214 metaslab_class_destroy(spa
->spa_log_class
);
1215 spa
->spa_log_class
= NULL
;
1218 * If this was part of an import or the open otherwise failed, we may
1219 * still have errors left in the queues. Empty them just in case.
1221 spa_errlog_drain(spa
);
1223 avl_destroy(&spa
->spa_errlist_scrub
);
1224 avl_destroy(&spa
->spa_errlist_last
);
1226 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1228 mutex_enter(&spa
->spa_proc_lock
);
1229 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1230 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1231 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1232 cv_broadcast(&spa
->spa_proc_cv
);
1233 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1234 ASSERT(spa
->spa_proc
!= &p0
);
1235 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1237 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1238 spa
->spa_proc_state
= SPA_PROC_NONE
;
1240 ASSERT(spa
->spa_proc
== &p0
);
1241 mutex_exit(&spa
->spa_proc_lock
);
1244 * We want to make sure spa_thread() has actually exited the ZFS
1245 * module, so that the module can't be unloaded out from underneath
1248 if (spa
->spa_did
!= 0) {
1249 thread_join(spa
->spa_did
);
1255 * Verify a pool configuration, and construct the vdev tree appropriately. This
1256 * will create all the necessary vdevs in the appropriate layout, with each vdev
1257 * in the CLOSED state. This will prep the pool before open/creation/import.
1258 * All vdev validation is done by the vdev_alloc() routine.
1261 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1262 uint_t id
, int atype
)
1269 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1272 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1275 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1278 if (error
== ENOENT
)
1284 return (SET_ERROR(EINVAL
));
1287 for (c
= 0; c
< children
; c
++) {
1289 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1297 ASSERT(*vdp
!= NULL
);
1303 * Opposite of spa_load().
1306 spa_unload(spa_t
*spa
)
1310 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1315 spa_async_suspend(spa
);
1320 if (spa
->spa_sync_on
) {
1321 txg_sync_stop(spa
->spa_dsl_pool
);
1322 spa
->spa_sync_on
= B_FALSE
;
1326 * Wait for any outstanding async I/O to complete.
1328 if (spa
->spa_async_zio_root
!= NULL
) {
1329 for (i
= 0; i
< max_ncpus
; i
++)
1330 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
1331 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
1332 spa
->spa_async_zio_root
= NULL
;
1335 bpobj_close(&spa
->spa_deferred_bpobj
);
1337 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1342 if (spa
->spa_root_vdev
)
1343 vdev_free(spa
->spa_root_vdev
);
1344 ASSERT(spa
->spa_root_vdev
== NULL
);
1347 * Close the dsl pool.
1349 if (spa
->spa_dsl_pool
) {
1350 dsl_pool_close(spa
->spa_dsl_pool
);
1351 spa
->spa_dsl_pool
= NULL
;
1352 spa
->spa_meta_objset
= NULL
;
1359 * Drop and purge level 2 cache
1361 spa_l2cache_drop(spa
);
1363 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1364 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1365 if (spa
->spa_spares
.sav_vdevs
) {
1366 kmem_free(spa
->spa_spares
.sav_vdevs
,
1367 spa
->spa_spares
.sav_count
* sizeof (void *));
1368 spa
->spa_spares
.sav_vdevs
= NULL
;
1370 if (spa
->spa_spares
.sav_config
) {
1371 nvlist_free(spa
->spa_spares
.sav_config
);
1372 spa
->spa_spares
.sav_config
= NULL
;
1374 spa
->spa_spares
.sav_count
= 0;
1376 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1377 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1378 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1380 if (spa
->spa_l2cache
.sav_vdevs
) {
1381 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1382 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1383 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1385 if (spa
->spa_l2cache
.sav_config
) {
1386 nvlist_free(spa
->spa_l2cache
.sav_config
);
1387 spa
->spa_l2cache
.sav_config
= NULL
;
1389 spa
->spa_l2cache
.sav_count
= 0;
1391 spa
->spa_async_suspended
= 0;
1393 if (spa
->spa_comment
!= NULL
) {
1394 spa_strfree(spa
->spa_comment
);
1395 spa
->spa_comment
= NULL
;
1398 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1402 * Load (or re-load) the current list of vdevs describing the active spares for
1403 * this pool. When this is called, we have some form of basic information in
1404 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1405 * then re-generate a more complete list including status information.
1408 spa_load_spares(spa_t
*spa
)
1415 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1418 * First, close and free any existing spare vdevs.
1420 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1421 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1423 /* Undo the call to spa_activate() below */
1424 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1425 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1426 spa_spare_remove(tvd
);
1431 if (spa
->spa_spares
.sav_vdevs
)
1432 kmem_free(spa
->spa_spares
.sav_vdevs
,
1433 spa
->spa_spares
.sav_count
* sizeof (void *));
1435 if (spa
->spa_spares
.sav_config
== NULL
)
1438 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1439 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
1441 spa
->spa_spares
.sav_count
= (int)nspares
;
1442 spa
->spa_spares
.sav_vdevs
= NULL
;
1448 * Construct the array of vdevs, opening them to get status in the
1449 * process. For each spare, there is potentially two different vdev_t
1450 * structures associated with it: one in the list of spares (used only
1451 * for basic validation purposes) and one in the active vdev
1452 * configuration (if it's spared in). During this phase we open and
1453 * validate each vdev on the spare list. If the vdev also exists in the
1454 * active configuration, then we also mark this vdev as an active spare.
1456 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
1458 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1459 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1460 VDEV_ALLOC_SPARE
) == 0);
1463 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1465 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1466 B_FALSE
)) != NULL
) {
1467 if (!tvd
->vdev_isspare
)
1471 * We only mark the spare active if we were successfully
1472 * able to load the vdev. Otherwise, importing a pool
1473 * with a bad active spare would result in strange
1474 * behavior, because multiple pool would think the spare
1475 * is actively in use.
1477 * There is a vulnerability here to an equally bizarre
1478 * circumstance, where a dead active spare is later
1479 * brought back to life (onlined or otherwise). Given
1480 * the rarity of this scenario, and the extra complexity
1481 * it adds, we ignore the possibility.
1483 if (!vdev_is_dead(tvd
))
1484 spa_spare_activate(tvd
);
1488 vd
->vdev_aux
= &spa
->spa_spares
;
1490 if (vdev_open(vd
) != 0)
1493 if (vdev_validate_aux(vd
) == 0)
1498 * Recompute the stashed list of spares, with status information
1501 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
,
1502 DATA_TYPE_NVLIST_ARRAY
) == 0);
1504 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1506 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1507 spares
[i
] = vdev_config_generate(spa
,
1508 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1509 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1510 ZPOOL_CONFIG_SPARES
, spares
, spa
->spa_spares
.sav_count
) == 0);
1511 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1512 nvlist_free(spares
[i
]);
1513 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1517 * Load (or re-load) the current list of vdevs describing the active l2cache for
1518 * this pool. When this is called, we have some form of basic information in
1519 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1520 * then re-generate a more complete list including status information.
1521 * Devices which are already active have their details maintained, and are
1525 spa_load_l2cache(spa_t
*spa
)
1529 int i
, j
, oldnvdevs
;
1531 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1532 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1534 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1536 if (sav
->sav_config
!= NULL
) {
1537 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
1538 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
1539 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
1545 oldvdevs
= sav
->sav_vdevs
;
1546 oldnvdevs
= sav
->sav_count
;
1547 sav
->sav_vdevs
= NULL
;
1551 * Process new nvlist of vdevs.
1553 for (i
= 0; i
< nl2cache
; i
++) {
1554 VERIFY(nvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
,
1558 for (j
= 0; j
< oldnvdevs
; j
++) {
1560 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1562 * Retain previous vdev for add/remove ops.
1570 if (newvdevs
[i
] == NULL
) {
1574 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1575 VDEV_ALLOC_L2CACHE
) == 0);
1580 * Commit this vdev as an l2cache device,
1581 * even if it fails to open.
1583 spa_l2cache_add(vd
);
1588 spa_l2cache_activate(vd
);
1590 if (vdev_open(vd
) != 0)
1593 (void) vdev_validate_aux(vd
);
1595 if (!vdev_is_dead(vd
))
1596 l2arc_add_vdev(spa
, vd
);
1601 * Purge vdevs that were dropped
1603 for (i
= 0; i
< oldnvdevs
; i
++) {
1608 ASSERT(vd
->vdev_isl2cache
);
1610 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
1611 pool
!= 0ULL && l2arc_vdev_present(vd
))
1612 l2arc_remove_vdev(vd
);
1613 vdev_clear_stats(vd
);
1619 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
1621 if (sav
->sav_config
== NULL
)
1624 sav
->sav_vdevs
= newvdevs
;
1625 sav
->sav_count
= (int)nl2cache
;
1628 * Recompute the stashed list of l2cache devices, with status
1629 * information this time.
1631 VERIFY(nvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
1632 DATA_TYPE_NVLIST_ARRAY
) == 0);
1634 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *), KM_SLEEP
);
1635 for (i
= 0; i
< sav
->sav_count
; i
++)
1636 l2cache
[i
] = vdev_config_generate(spa
,
1637 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
1638 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
1639 ZPOOL_CONFIG_L2CACHE
, l2cache
, sav
->sav_count
) == 0);
1641 for (i
= 0; i
< sav
->sav_count
; i
++)
1642 nvlist_free(l2cache
[i
]);
1644 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
1648 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
1651 char *packed
= NULL
;
1656 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
1660 nvsize
= *(uint64_t *)db
->db_data
;
1661 dmu_buf_rele(db
, FTAG
);
1663 packed
= vmem_alloc(nvsize
, KM_SLEEP
);
1664 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
1667 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
1668 vmem_free(packed
, nvsize
);
1674 * Checks to see if the given vdev could not be opened, in which case we post a
1675 * sysevent to notify the autoreplace code that the device has been removed.
1678 spa_check_removed(vdev_t
*vd
)
1682 for (c
= 0; c
< vd
->vdev_children
; c
++)
1683 spa_check_removed(vd
->vdev_child
[c
]);
1685 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
1687 zfs_post_autoreplace(vd
->vdev_spa
, vd
);
1688 spa_event_notify(vd
->vdev_spa
, vd
, ESC_ZFS_VDEV_CHECK
);
1693 spa_config_valid_zaps(vdev_t
*vd
, vdev_t
*mvd
)
1697 ASSERT3U(vd
->vdev_children
, ==, mvd
->vdev_children
);
1699 vd
->vdev_top_zap
= mvd
->vdev_top_zap
;
1700 vd
->vdev_leaf_zap
= mvd
->vdev_leaf_zap
;
1702 for (i
= 0; i
< vd
->vdev_children
; i
++) {
1703 spa_config_valid_zaps(vd
->vdev_child
[i
], mvd
->vdev_child
[i
]);
1708 * Validate the current config against the MOS config
1711 spa_config_valid(spa_t
*spa
, nvlist_t
*config
)
1713 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
1717 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0);
1719 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1720 VERIFY(spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
) == 0);
1722 ASSERT3U(rvd
->vdev_children
, ==, mrvd
->vdev_children
);
1725 * If we're doing a normal import, then build up any additional
1726 * diagnostic information about missing devices in this config.
1727 * We'll pass this up to the user for further processing.
1729 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
1730 nvlist_t
**child
, *nv
;
1733 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
**),
1735 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1737 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1738 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1739 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1741 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1742 mtvd
->vdev_ops
!= &vdev_missing_ops
&&
1744 child
[idx
++] = vdev_config_generate(spa
, mtvd
,
1749 VERIFY(nvlist_add_nvlist_array(nv
,
1750 ZPOOL_CONFIG_CHILDREN
, child
, idx
) == 0);
1751 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
1752 ZPOOL_CONFIG_MISSING_DEVICES
, nv
) == 0);
1754 for (i
= 0; i
< idx
; i
++)
1755 nvlist_free(child
[i
]);
1758 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
1762 * Compare the root vdev tree with the information we have
1763 * from the MOS config (mrvd). Check each top-level vdev
1764 * with the corresponding MOS config top-level (mtvd).
1766 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1767 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1768 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1771 * Resolve any "missing" vdevs in the current configuration.
1772 * If we find that the MOS config has more accurate information
1773 * about the top-level vdev then use that vdev instead.
1775 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1776 mtvd
->vdev_ops
!= &vdev_missing_ops
) {
1778 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
))
1782 * Device specific actions.
1784 if (mtvd
->vdev_islog
) {
1785 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
1788 * XXX - once we have 'readonly' pool
1789 * support we should be able to handle
1790 * missing data devices by transitioning
1791 * the pool to readonly.
1797 * Swap the missing vdev with the data we were
1798 * able to obtain from the MOS config.
1800 vdev_remove_child(rvd
, tvd
);
1801 vdev_remove_child(mrvd
, mtvd
);
1803 vdev_add_child(rvd
, mtvd
);
1804 vdev_add_child(mrvd
, tvd
);
1806 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1808 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1812 if (mtvd
->vdev_islog
) {
1814 * Load the slog device's state from the MOS
1815 * config since it's possible that the label
1816 * does not contain the most up-to-date
1819 vdev_load_log_state(tvd
, mtvd
);
1824 * Per-vdev ZAP info is stored exclusively in the MOS.
1826 spa_config_valid_zaps(tvd
, mtvd
);
1831 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1834 * Ensure we were able to validate the config.
1836 return (rvd
->vdev_guid_sum
== spa
->spa_uberblock
.ub_guid_sum
);
1840 * Check for missing log devices
1843 spa_check_logs(spa_t
*spa
)
1845 boolean_t rv
= B_FALSE
;
1846 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1848 switch (spa
->spa_log_state
) {
1851 case SPA_LOG_MISSING
:
1852 /* need to recheck in case slog has been restored */
1853 case SPA_LOG_UNKNOWN
:
1854 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1855 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
1857 spa_set_log_state(spa
, SPA_LOG_MISSING
);
1864 spa_passivate_log(spa_t
*spa
)
1866 vdev_t
*rvd
= spa
->spa_root_vdev
;
1867 boolean_t slog_found
= B_FALSE
;
1870 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1872 if (!spa_has_slogs(spa
))
1875 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1876 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1877 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1879 if (tvd
->vdev_islog
) {
1880 metaslab_group_passivate(mg
);
1881 slog_found
= B_TRUE
;
1885 return (slog_found
);
1889 spa_activate_log(spa_t
*spa
)
1891 vdev_t
*rvd
= spa
->spa_root_vdev
;
1894 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1896 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1897 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1898 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1900 if (tvd
->vdev_islog
)
1901 metaslab_group_activate(mg
);
1906 spa_offline_log(spa_t
*spa
)
1910 error
= dmu_objset_find(spa_name(spa
), zil_vdev_offline
,
1911 NULL
, DS_FIND_CHILDREN
);
1914 * We successfully offlined the log device, sync out the
1915 * current txg so that the "stubby" block can be removed
1918 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1924 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
1928 for (i
= 0; i
< sav
->sav_count
; i
++)
1929 spa_check_removed(sav
->sav_vdevs
[i
]);
1933 spa_claim_notify(zio_t
*zio
)
1935 spa_t
*spa
= zio
->io_spa
;
1940 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
1941 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
1942 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
1943 mutex_exit(&spa
->spa_props_lock
);
1946 typedef struct spa_load_error
{
1947 uint64_t sle_meta_count
;
1948 uint64_t sle_data_count
;
1952 spa_load_verify_done(zio_t
*zio
)
1954 blkptr_t
*bp
= zio
->io_bp
;
1955 spa_load_error_t
*sle
= zio
->io_private
;
1956 dmu_object_type_t type
= BP_GET_TYPE(bp
);
1957 int error
= zio
->io_error
;
1958 spa_t
*spa
= zio
->io_spa
;
1961 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
1962 type
!= DMU_OT_INTENT_LOG
)
1963 atomic_inc_64(&sle
->sle_meta_count
);
1965 atomic_inc_64(&sle
->sle_data_count
);
1967 zio_data_buf_free(zio
->io_data
, zio
->io_size
);
1969 mutex_enter(&spa
->spa_scrub_lock
);
1970 spa
->spa_scrub_inflight
--;
1971 cv_broadcast(&spa
->spa_scrub_io_cv
);
1972 mutex_exit(&spa
->spa_scrub_lock
);
1976 * Maximum number of concurrent scrub i/os to create while verifying
1977 * a pool while importing it.
1979 int spa_load_verify_maxinflight
= 10000;
1980 int spa_load_verify_metadata
= B_TRUE
;
1981 int spa_load_verify_data
= B_TRUE
;
1985 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
1986 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
1992 if (bp
== NULL
|| BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
1995 * Note: normally this routine will not be called if
1996 * spa_load_verify_metadata is not set. However, it may be useful
1997 * to manually set the flag after the traversal has begun.
1999 if (!spa_load_verify_metadata
)
2001 if (BP_GET_BUFC_TYPE(bp
) == ARC_BUFC_DATA
&& !spa_load_verify_data
)
2005 size
= BP_GET_PSIZE(bp
);
2006 data
= zio_data_buf_alloc(size
);
2008 mutex_enter(&spa
->spa_scrub_lock
);
2009 while (spa
->spa_scrub_inflight
>= spa_load_verify_maxinflight
)
2010 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
2011 spa
->spa_scrub_inflight
++;
2012 mutex_exit(&spa
->spa_scrub_lock
);
2014 zio_nowait(zio_read(rio
, spa
, bp
, data
, size
,
2015 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
2016 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
2017 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
2023 verify_dataset_name_len(dsl_pool_t
*dp
, dsl_dataset_t
*ds
, void *arg
)
2025 if (dsl_dataset_namelen(ds
) >= ZFS_MAX_DATASET_NAME_LEN
)
2026 return (SET_ERROR(ENAMETOOLONG
));
2032 spa_load_verify(spa_t
*spa
)
2035 spa_load_error_t sle
= { 0 };
2036 zpool_rewind_policy_t policy
;
2037 boolean_t verify_ok
= B_FALSE
;
2040 zpool_get_rewind_policy(spa
->spa_config
, &policy
);
2042 if (policy
.zrp_request
& ZPOOL_NEVER_REWIND
)
2045 dsl_pool_config_enter(spa
->spa_dsl_pool
, FTAG
);
2046 error
= dmu_objset_find_dp(spa
->spa_dsl_pool
,
2047 spa
->spa_dsl_pool
->dp_root_dir_obj
, verify_dataset_name_len
, NULL
,
2049 dsl_pool_config_exit(spa
->spa_dsl_pool
, FTAG
);
2053 rio
= zio_root(spa
, NULL
, &sle
,
2054 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
2056 if (spa_load_verify_metadata
) {
2057 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
2058 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
,
2059 spa_load_verify_cb
, rio
);
2062 (void) zio_wait(rio
);
2064 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
2065 spa
->spa_load_data_errors
= sle
.sle_data_count
;
2067 if (!error
&& sle
.sle_meta_count
<= policy
.zrp_maxmeta
&&
2068 sle
.sle_data_count
<= policy
.zrp_maxdata
) {
2072 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
2073 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2075 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
2076 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2077 ZPOOL_CONFIG_LOAD_TIME
, spa
->spa_load_txg_ts
) == 0);
2078 VERIFY(nvlist_add_int64(spa
->spa_load_info
,
2079 ZPOOL_CONFIG_REWIND_TIME
, loss
) == 0);
2080 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
2081 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
) == 0);
2083 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
2087 if (error
!= ENXIO
&& error
!= EIO
)
2088 error
= SET_ERROR(EIO
);
2092 return (verify_ok
? 0 : EIO
);
2096 * Find a value in the pool props object.
2099 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2101 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2102 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2106 * Find a value in the pool directory object.
2109 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
)
2111 return (zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2112 name
, sizeof (uint64_t), 1, val
));
2116 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2118 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2123 * Fix up config after a partly-completed split. This is done with the
2124 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2125 * pool have that entry in their config, but only the splitting one contains
2126 * a list of all the guids of the vdevs that are being split off.
2128 * This function determines what to do with that list: either rejoin
2129 * all the disks to the pool, or complete the splitting process. To attempt
2130 * the rejoin, each disk that is offlined is marked online again, and
2131 * we do a reopen() call. If the vdev label for every disk that was
2132 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2133 * then we call vdev_split() on each disk, and complete the split.
2135 * Otherwise we leave the config alone, with all the vdevs in place in
2136 * the original pool.
2139 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
2146 boolean_t attempt_reopen
;
2148 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
2151 /* check that the config is complete */
2152 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
2153 &glist
, &gcount
) != 0)
2156 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
2158 /* attempt to online all the vdevs & validate */
2159 attempt_reopen
= B_TRUE
;
2160 for (i
= 0; i
< gcount
; i
++) {
2161 if (glist
[i
] == 0) /* vdev is hole */
2164 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
2165 if (vd
[i
] == NULL
) {
2167 * Don't bother attempting to reopen the disks;
2168 * just do the split.
2170 attempt_reopen
= B_FALSE
;
2172 /* attempt to re-online it */
2173 vd
[i
]->vdev_offline
= B_FALSE
;
2177 if (attempt_reopen
) {
2178 vdev_reopen(spa
->spa_root_vdev
);
2180 /* check each device to see what state it's in */
2181 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
2182 if (vd
[i
] != NULL
&&
2183 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
2190 * If every disk has been moved to the new pool, or if we never
2191 * even attempted to look at them, then we split them off for
2194 if (!attempt_reopen
|| gcount
== extracted
) {
2195 for (i
= 0; i
< gcount
; i
++)
2198 vdev_reopen(spa
->spa_root_vdev
);
2201 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
2205 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
,
2206 boolean_t mosconfig
)
2208 nvlist_t
*config
= spa
->spa_config
;
2209 char *ereport
= FM_EREPORT_ZFS_POOL
;
2215 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
))
2216 return (SET_ERROR(EINVAL
));
2218 ASSERT(spa
->spa_comment
== NULL
);
2219 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2220 spa
->spa_comment
= spa_strdup(comment
);
2223 * Versioning wasn't explicitly added to the label until later, so if
2224 * it's not present treat it as the initial version.
2226 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
2227 &spa
->spa_ubsync
.ub_version
) != 0)
2228 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
2230 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
2231 &spa
->spa_config_txg
);
2233 if ((state
== SPA_LOAD_IMPORT
|| state
== SPA_LOAD_TRYIMPORT
) &&
2234 spa_guid_exists(pool_guid
, 0)) {
2235 error
= SET_ERROR(EEXIST
);
2237 spa
->spa_config_guid
= pool_guid
;
2239 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
,
2241 VERIFY(nvlist_dup(nvl
, &spa
->spa_config_splitting
,
2245 nvlist_free(spa
->spa_load_info
);
2246 spa
->spa_load_info
= fnvlist_alloc();
2248 gethrestime(&spa
->spa_loaded_ts
);
2249 error
= spa_load_impl(spa
, pool_guid
, config
, state
, type
,
2250 mosconfig
, &ereport
);
2254 * Don't count references from objsets that are already closed
2255 * and are making their way through the eviction process.
2257 spa_evicting_os_wait(spa
);
2258 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
2260 if (error
!= EEXIST
) {
2261 spa
->spa_loaded_ts
.tv_sec
= 0;
2262 spa
->spa_loaded_ts
.tv_nsec
= 0;
2264 if (error
!= EBADF
) {
2265 zfs_ereport_post(ereport
, spa
, NULL
, NULL
, 0, 0);
2268 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
2276 * Count the number of per-vdev ZAPs associated with all of the vdevs in the
2277 * vdev tree rooted in the given vd, and ensure that each ZAP is present in the
2278 * spa's per-vdev ZAP list.
2281 vdev_count_verify_zaps(vdev_t
*vd
)
2283 spa_t
*spa
= vd
->vdev_spa
;
2287 if (vd
->vdev_top_zap
!= 0) {
2289 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2290 spa
->spa_all_vdev_zaps
, vd
->vdev_top_zap
));
2292 if (vd
->vdev_leaf_zap
!= 0) {
2294 ASSERT0(zap_lookup_int(spa
->spa_meta_objset
,
2295 spa
->spa_all_vdev_zaps
, vd
->vdev_leaf_zap
));
2298 for (i
= 0; i
< vd
->vdev_children
; i
++) {
2299 total
+= vdev_count_verify_zaps(vd
->vdev_child
[i
]);
2307 * Load an existing storage pool, using the pool's builtin spa_config as a
2308 * source of configuration information.
2310 __attribute__((always_inline
))
2312 spa_load_impl(spa_t
*spa
, uint64_t pool_guid
, nvlist_t
*config
,
2313 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
2317 nvlist_t
*nvroot
= NULL
;
2320 uberblock_t
*ub
= &spa
->spa_uberblock
;
2321 uint64_t children
, config_cache_txg
= spa
->spa_config_txg
;
2322 int orig_mode
= spa
->spa_mode
;
2325 boolean_t missing_feat_write
= B_FALSE
;
2326 nvlist_t
*mos_config
;
2329 * If this is an untrusted config, access the pool in read-only mode.
2330 * This prevents things like resilvering recently removed devices.
2333 spa
->spa_mode
= FREAD
;
2335 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2337 spa
->spa_load_state
= state
;
2339 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvroot
))
2340 return (SET_ERROR(EINVAL
));
2342 parse
= (type
== SPA_IMPORT_EXISTING
?
2343 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
2346 * Create "The Godfather" zio to hold all async IOs
2348 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
2350 for (i
= 0; i
< max_ncpus
; i
++) {
2351 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
2352 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
2353 ZIO_FLAG_GODFATHER
);
2357 * Parse the configuration into a vdev tree. We explicitly set the
2358 * value that will be returned by spa_version() since parsing the
2359 * configuration requires knowing the version number.
2361 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2362 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, parse
);
2363 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2368 ASSERT(spa
->spa_root_vdev
== rvd
);
2369 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
2370 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
2372 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2373 ASSERT(spa_guid(spa
) == pool_guid
);
2377 * Try to open all vdevs, loading each label in the process.
2379 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2380 error
= vdev_open(rvd
);
2381 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2386 * We need to validate the vdev labels against the configuration that
2387 * we have in hand, which is dependent on the setting of mosconfig. If
2388 * mosconfig is true then we're validating the vdev labels based on
2389 * that config. Otherwise, we're validating against the cached config
2390 * (zpool.cache) that was read when we loaded the zfs module, and then
2391 * later we will recursively call spa_load() and validate against
2394 * If we're assembling a new pool that's been split off from an
2395 * existing pool, the labels haven't yet been updated so we skip
2396 * validation for now.
2398 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2399 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2400 error
= vdev_validate(rvd
, mosconfig
);
2401 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2406 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2407 return (SET_ERROR(ENXIO
));
2411 * Find the best uberblock.
2413 vdev_uberblock_load(rvd
, ub
, &label
);
2416 * If we weren't able to find a single valid uberblock, return failure.
2418 if (ub
->ub_txg
== 0) {
2420 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
2424 * If the pool has an unsupported version we can't open it.
2426 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
2428 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
2431 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2435 * If we weren't able to find what's necessary for reading the
2436 * MOS in the label, return failure.
2438 if (label
== NULL
|| nvlist_lookup_nvlist(label
,
2439 ZPOOL_CONFIG_FEATURES_FOR_READ
, &features
) != 0) {
2441 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2446 * Update our in-core representation with the definitive values
2449 nvlist_free(spa
->spa_label_features
);
2450 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
, 0) == 0);
2456 * Look through entries in the label nvlist's features_for_read. If
2457 * there is a feature listed there which we don't understand then we
2458 * cannot open a pool.
2460 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2461 nvlist_t
*unsup_feat
;
2464 VERIFY(nvlist_alloc(&unsup_feat
, NV_UNIQUE_NAME
, KM_SLEEP
) ==
2467 for (nvp
= nvlist_next_nvpair(spa
->spa_label_features
, NULL
);
2469 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
2470 if (!zfeature_is_supported(nvpair_name(nvp
))) {
2471 VERIFY(nvlist_add_string(unsup_feat
,
2472 nvpair_name(nvp
), "") == 0);
2476 if (!nvlist_empty(unsup_feat
)) {
2477 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
2478 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
) == 0);
2479 nvlist_free(unsup_feat
);
2480 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2484 nvlist_free(unsup_feat
);
2488 * If the vdev guid sum doesn't match the uberblock, we have an
2489 * incomplete configuration. We first check to see if the pool
2490 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2491 * If it is, defer the vdev_guid_sum check till later so we
2492 * can handle missing vdevs.
2494 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VDEV_CHILDREN
,
2495 &children
) != 0 && mosconfig
&& type
!= SPA_IMPORT_ASSEMBLE
&&
2496 rvd
->vdev_guid_sum
!= ub
->ub_guid_sum
)
2497 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
2499 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
2500 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2501 spa_try_repair(spa
, config
);
2502 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2503 nvlist_free(spa
->spa_config_splitting
);
2504 spa
->spa_config_splitting
= NULL
;
2508 * Initialize internal SPA structures.
2510 spa
->spa_state
= POOL_STATE_ACTIVE
;
2511 spa
->spa_ubsync
= spa
->spa_uberblock
;
2512 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
2513 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
2514 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
2515 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
2516 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
2517 spa
->spa_prev_software_version
= ub
->ub_software_version
;
2519 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
2521 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2522 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
2524 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
) != 0)
2525 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2527 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
2528 boolean_t missing_feat_read
= B_FALSE
;
2529 nvlist_t
*unsup_feat
, *enabled_feat
;
2532 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
2533 &spa
->spa_feat_for_read_obj
) != 0) {
2534 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2537 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
2538 &spa
->spa_feat_for_write_obj
) != 0) {
2539 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2542 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
2543 &spa
->spa_feat_desc_obj
) != 0) {
2544 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2547 enabled_feat
= fnvlist_alloc();
2548 unsup_feat
= fnvlist_alloc();
2550 if (!spa_features_check(spa
, B_FALSE
,
2551 unsup_feat
, enabled_feat
))
2552 missing_feat_read
= B_TRUE
;
2554 if (spa_writeable(spa
) || state
== SPA_LOAD_TRYIMPORT
) {
2555 if (!spa_features_check(spa
, B_TRUE
,
2556 unsup_feat
, enabled_feat
)) {
2557 missing_feat_write
= B_TRUE
;
2561 fnvlist_add_nvlist(spa
->spa_load_info
,
2562 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
2564 if (!nvlist_empty(unsup_feat
)) {
2565 fnvlist_add_nvlist(spa
->spa_load_info
,
2566 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
2569 fnvlist_free(enabled_feat
);
2570 fnvlist_free(unsup_feat
);
2572 if (!missing_feat_read
) {
2573 fnvlist_add_boolean(spa
->spa_load_info
,
2574 ZPOOL_CONFIG_CAN_RDONLY
);
2578 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2579 * twofold: to determine whether the pool is available for
2580 * import in read-write mode and (if it is not) whether the
2581 * pool is available for import in read-only mode. If the pool
2582 * is available for import in read-write mode, it is displayed
2583 * as available in userland; if it is not available for import
2584 * in read-only mode, it is displayed as unavailable in
2585 * userland. If the pool is available for import in read-only
2586 * mode but not read-write mode, it is displayed as unavailable
2587 * in userland with a special note that the pool is actually
2588 * available for open in read-only mode.
2590 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2591 * missing a feature for write, we must first determine whether
2592 * the pool can be opened read-only before returning to
2593 * userland in order to know whether to display the
2594 * abovementioned note.
2596 if (missing_feat_read
|| (missing_feat_write
&&
2597 spa_writeable(spa
))) {
2598 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2603 * Load refcounts for ZFS features from disk into an in-memory
2604 * cache during SPA initialization.
2606 for (i
= 0; i
< SPA_FEATURES
; i
++) {
2609 error
= feature_get_refcount_from_disk(spa
,
2610 &spa_feature_table
[i
], &refcount
);
2612 spa
->spa_feat_refcount_cache
[i
] = refcount
;
2613 } else if (error
== ENOTSUP
) {
2614 spa
->spa_feat_refcount_cache
[i
] =
2615 SPA_FEATURE_DISABLED
;
2617 return (spa_vdev_err(rvd
,
2618 VDEV_AUX_CORRUPT_DATA
, EIO
));
2623 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
2624 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
2625 &spa
->spa_feat_enabled_txg_obj
) != 0)
2626 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2629 spa
->spa_is_initializing
= B_TRUE
;
2630 error
= dsl_pool_open(spa
->spa_dsl_pool
);
2631 spa
->spa_is_initializing
= B_FALSE
;
2633 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2637 nvlist_t
*policy
= NULL
, *nvconfig
;
2639 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2640 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2642 if (!spa_is_root(spa
) && nvlist_lookup_uint64(nvconfig
,
2643 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
2645 unsigned long myhostid
= 0;
2647 VERIFY(nvlist_lookup_string(nvconfig
,
2648 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
2651 myhostid
= zone_get_hostid(NULL
);
2654 * We're emulating the system's hostid in userland, so
2655 * we can't use zone_get_hostid().
2657 (void) ddi_strtoul(hw_serial
, NULL
, 10, &myhostid
);
2658 #endif /* _KERNEL */
2659 if (hostid
!= 0 && myhostid
!= 0 &&
2660 hostid
!= myhostid
) {
2661 nvlist_free(nvconfig
);
2662 cmn_err(CE_WARN
, "pool '%s' could not be "
2663 "loaded as it was last accessed by another "
2664 "system (host: %s hostid: 0x%lx). See: "
2665 "http://zfsonlinux.org/msg/ZFS-8000-EY",
2666 spa_name(spa
), hostname
,
2667 (unsigned long)hostid
);
2668 return (SET_ERROR(EBADF
));
2671 if (nvlist_lookup_nvlist(spa
->spa_config
,
2672 ZPOOL_REWIND_POLICY
, &policy
) == 0)
2673 VERIFY(nvlist_add_nvlist(nvconfig
,
2674 ZPOOL_REWIND_POLICY
, policy
) == 0);
2676 spa_config_set(spa
, nvconfig
);
2678 spa_deactivate(spa
);
2679 spa_activate(spa
, orig_mode
);
2681 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, B_TRUE
));
2684 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
) != 0)
2685 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2686 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
2688 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2691 * Load the bit that tells us to use the new accounting function
2692 * (raid-z deflation). If we have an older pool, this will not
2695 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
);
2696 if (error
!= 0 && error
!= ENOENT
)
2697 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2699 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
2700 &spa
->spa_creation_version
);
2701 if (error
!= 0 && error
!= ENOENT
)
2702 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2705 * Load the persistent error log. If we have an older pool, this will
2708 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
);
2709 if (error
!= 0 && error
!= ENOENT
)
2710 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2712 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
2713 &spa
->spa_errlog_scrub
);
2714 if (error
!= 0 && error
!= ENOENT
)
2715 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2718 * Load the history object. If we have an older pool, this
2719 * will not be present.
2721 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
);
2722 if (error
!= 0 && error
!= ENOENT
)
2723 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2726 * Load the per-vdev ZAP map. If we have an older pool, this will not
2727 * be present; in this case, defer its creation to a later time to
2728 * avoid dirtying the MOS this early / out of sync context. See
2729 * spa_sync_config_object.
2732 /* The sentinel is only available in the MOS config. */
2733 if (load_nvlist(spa
, spa
->spa_config_object
, &mos_config
) != 0)
2734 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2736 error
= spa_dir_prop(spa
, DMU_POOL_VDEV_ZAP_MAP
,
2737 &spa
->spa_all_vdev_zaps
);
2739 if (error
!= ENOENT
&& error
!= 0) {
2740 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2741 } else if (error
== 0 && !nvlist_exists(mos_config
,
2742 ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
)) {
2744 * An older version of ZFS overwrote the sentinel value, so
2745 * we have orphaned per-vdev ZAPs in the MOS. Defer their
2746 * destruction to later; see spa_sync_config_object.
2748 spa
->spa_avz_action
= AVZ_ACTION_DESTROY
;
2750 * We're assuming that no vdevs have had their ZAPs created
2751 * before this. Better be sure of it.
2753 ASSERT0(vdev_count_verify_zaps(spa
->spa_root_vdev
));
2755 nvlist_free(mos_config
);
2758 * If we're assembling the pool from the split-off vdevs of
2759 * an existing pool, we don't want to attach the spares & cache
2764 * Load any hot spares for this pool.
2766 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
);
2767 if (error
!= 0 && error
!= ENOENT
)
2768 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2769 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2770 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
2771 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
2772 &spa
->spa_spares
.sav_config
) != 0)
2773 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2775 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2776 spa_load_spares(spa
);
2777 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2778 } else if (error
== 0) {
2779 spa
->spa_spares
.sav_sync
= B_TRUE
;
2783 * Load any level 2 ARC devices for this pool.
2785 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
2786 &spa
->spa_l2cache
.sav_object
);
2787 if (error
!= 0 && error
!= ENOENT
)
2788 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2789 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2790 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
2791 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
2792 &spa
->spa_l2cache
.sav_config
) != 0)
2793 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2795 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2796 spa_load_l2cache(spa
);
2797 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2798 } else if (error
== 0) {
2799 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
2802 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
2804 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
);
2805 if (error
&& error
!= ENOENT
)
2806 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2809 uint64_t autoreplace
= 0;
2811 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
2812 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
2813 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
2814 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
2815 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
2816 spa_prop_find(spa
, ZPOOL_PROP_DEDUPDITTO
,
2817 &spa
->spa_dedup_ditto
);
2819 spa
->spa_autoreplace
= (autoreplace
!= 0);
2823 * If the 'autoreplace' property is set, then post a resource notifying
2824 * the ZFS DE that it should not issue any faults for unopenable
2825 * devices. We also iterate over the vdevs, and post a sysevent for any
2826 * unopenable vdevs so that the normal autoreplace handler can take
2829 if (spa
->spa_autoreplace
&& state
!= SPA_LOAD_TRYIMPORT
) {
2830 spa_check_removed(spa
->spa_root_vdev
);
2832 * For the import case, this is done in spa_import(), because
2833 * at this point we're using the spare definitions from
2834 * the MOS config, not necessarily from the userland config.
2836 if (state
!= SPA_LOAD_IMPORT
) {
2837 spa_aux_check_removed(&spa
->spa_spares
);
2838 spa_aux_check_removed(&spa
->spa_l2cache
);
2843 * Load the vdev state for all toplevel vdevs.
2848 * Propagate the leaf DTLs we just loaded all the way up the tree.
2850 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2851 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
);
2852 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2855 * Load the DDTs (dedup tables).
2857 error
= ddt_load(spa
);
2859 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2861 spa_update_dspace(spa
);
2864 * Validate the config, using the MOS config to fill in any
2865 * information which might be missing. If we fail to validate
2866 * the config then declare the pool unfit for use. If we're
2867 * assembling a pool from a split, the log is not transferred
2870 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2873 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2874 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2876 if (!spa_config_valid(spa
, nvconfig
)) {
2877 nvlist_free(nvconfig
);
2878 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
2881 nvlist_free(nvconfig
);
2884 * Now that we've validated the config, check the state of the
2885 * root vdev. If it can't be opened, it indicates one or
2886 * more toplevel vdevs are faulted.
2888 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2889 return (SET_ERROR(ENXIO
));
2891 if (spa_writeable(spa
) && spa_check_logs(spa
)) {
2892 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
2893 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
, ENXIO
));
2897 if (missing_feat_write
) {
2898 ASSERT(state
== SPA_LOAD_TRYIMPORT
);
2901 * At this point, we know that we can open the pool in
2902 * read-only mode but not read-write mode. We now have enough
2903 * information and can return to userland.
2905 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
, ENOTSUP
));
2909 * We've successfully opened the pool, verify that we're ready
2910 * to start pushing transactions.
2912 if (state
!= SPA_LOAD_TRYIMPORT
) {
2913 if ((error
= spa_load_verify(spa
)))
2914 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2918 if (spa_writeable(spa
) && (state
== SPA_LOAD_RECOVER
||
2919 spa
->spa_load_max_txg
== UINT64_MAX
)) {
2921 int need_update
= B_FALSE
;
2922 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2925 ASSERT(state
!= SPA_LOAD_TRYIMPORT
);
2928 * Claim log blocks that haven't been committed yet.
2929 * This must all happen in a single txg.
2930 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2931 * invoked from zil_claim_log_block()'s i/o done callback.
2932 * Price of rollback is that we abandon the log.
2934 spa
->spa_claiming
= B_TRUE
;
2936 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
2937 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
2938 zil_claim
, tx
, DS_FIND_CHILDREN
);
2941 spa
->spa_claiming
= B_FALSE
;
2943 spa_set_log_state(spa
, SPA_LOG_GOOD
);
2944 spa
->spa_sync_on
= B_TRUE
;
2945 txg_sync_start(spa
->spa_dsl_pool
);
2948 * Wait for all claims to sync. We sync up to the highest
2949 * claimed log block birth time so that claimed log blocks
2950 * don't appear to be from the future. spa_claim_max_txg
2951 * will have been set for us by either zil_check_log_chain()
2952 * (invoked from spa_check_logs()) or zil_claim() above.
2954 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
2957 * If the config cache is stale, or we have uninitialized
2958 * metaslabs (see spa_vdev_add()), then update the config.
2960 * If this is a verbatim import, trust the current
2961 * in-core spa_config and update the disk labels.
2963 if (config_cache_txg
!= spa
->spa_config_txg
||
2964 state
== SPA_LOAD_IMPORT
||
2965 state
== SPA_LOAD_RECOVER
||
2966 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
2967 need_update
= B_TRUE
;
2969 for (c
= 0; c
< rvd
->vdev_children
; c
++)
2970 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
2971 need_update
= B_TRUE
;
2974 * Update the config cache asychronously in case we're the
2975 * root pool, in which case the config cache isn't writable yet.
2978 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
2981 * Check all DTLs to see if anything needs resilvering.
2983 if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
2984 vdev_resilver_needed(rvd
, NULL
, NULL
))
2985 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
2988 * Log the fact that we booted up (so that we can detect if
2989 * we rebooted in the middle of an operation).
2991 spa_history_log_version(spa
, "open");
2994 * Delete any inconsistent datasets.
2996 (void) dmu_objset_find(spa_name(spa
),
2997 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
3000 * Clean up any stale temporary dataset userrefs.
3002 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
3009 spa_load_retry(spa_t
*spa
, spa_load_state_t state
, int mosconfig
)
3011 int mode
= spa
->spa_mode
;
3014 spa_deactivate(spa
);
3016 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
3018 spa_activate(spa
, mode
);
3019 spa_async_suspend(spa
);
3021 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, mosconfig
));
3025 * If spa_load() fails this function will try loading prior txg's. If
3026 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
3027 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
3028 * function will not rewind the pool and will return the same error as
3032 spa_load_best(spa_t
*spa
, spa_load_state_t state
, int mosconfig
,
3033 uint64_t max_request
, int rewind_flags
)
3035 nvlist_t
*loadinfo
= NULL
;
3036 nvlist_t
*config
= NULL
;
3037 int load_error
, rewind_error
;
3038 uint64_t safe_rewind_txg
;
3041 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
3042 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
3043 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
3045 spa
->spa_load_max_txg
= max_request
;
3046 if (max_request
!= UINT64_MAX
)
3047 spa
->spa_extreme_rewind
= B_TRUE
;
3050 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
,
3052 if (load_error
== 0)
3055 if (spa
->spa_root_vdev
!= NULL
)
3056 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3058 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
3059 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
3061 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
3062 nvlist_free(config
);
3063 return (load_error
);
3066 if (state
== SPA_LOAD_RECOVER
) {
3067 /* Price of rolling back is discarding txgs, including log */
3068 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
3071 * If we aren't rolling back save the load info from our first
3072 * import attempt so that we can restore it after attempting
3075 loadinfo
= spa
->spa_load_info
;
3076 spa
->spa_load_info
= fnvlist_alloc();
3079 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
3080 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
3081 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
3082 TXG_INITIAL
: safe_rewind_txg
;
3085 * Continue as long as we're finding errors, we're still within
3086 * the acceptable rewind range, and we're still finding uberblocks
3088 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
3089 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
3090 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
3091 spa
->spa_extreme_rewind
= B_TRUE
;
3092 rewind_error
= spa_load_retry(spa
, state
, mosconfig
);
3095 spa
->spa_extreme_rewind
= B_FALSE
;
3096 spa
->spa_load_max_txg
= UINT64_MAX
;
3098 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
3099 spa_config_set(spa
, config
);
3101 nvlist_free(config
);
3103 if (state
== SPA_LOAD_RECOVER
) {
3104 ASSERT3P(loadinfo
, ==, NULL
);
3105 return (rewind_error
);
3107 /* Store the rewind info as part of the initial load info */
3108 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
3109 spa
->spa_load_info
);
3111 /* Restore the initial load info */
3112 fnvlist_free(spa
->spa_load_info
);
3113 spa
->spa_load_info
= loadinfo
;
3115 return (load_error
);
3122 * The import case is identical to an open except that the configuration is sent
3123 * down from userland, instead of grabbed from the configuration cache. For the
3124 * case of an open, the pool configuration will exist in the
3125 * POOL_STATE_UNINITIALIZED state.
3127 * The stats information (gen/count/ustats) is used to gather vdev statistics at
3128 * the same time open the pool, without having to keep around the spa_t in some
3132 spa_open_common(const char *pool
, spa_t
**spapp
, void *tag
, nvlist_t
*nvpolicy
,
3136 spa_load_state_t state
= SPA_LOAD_OPEN
;
3138 int locked
= B_FALSE
;
3139 int firstopen
= B_FALSE
;
3144 * As disgusting as this is, we need to support recursive calls to this
3145 * function because dsl_dir_open() is called during spa_load(), and ends
3146 * up calling spa_open() again. The real fix is to figure out how to
3147 * avoid dsl_dir_open() calling this in the first place.
3149 if (mutex_owner(&spa_namespace_lock
) != curthread
) {
3150 mutex_enter(&spa_namespace_lock
);
3154 if ((spa
= spa_lookup(pool
)) == NULL
) {
3156 mutex_exit(&spa_namespace_lock
);
3157 return (SET_ERROR(ENOENT
));
3160 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
3161 zpool_rewind_policy_t policy
;
3165 zpool_get_rewind_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
3167 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
3168 state
= SPA_LOAD_RECOVER
;
3170 spa_activate(spa
, spa_mode_global
);
3172 if (state
!= SPA_LOAD_RECOVER
)
3173 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
3175 error
= spa_load_best(spa
, state
, B_FALSE
, policy
.zrp_txg
,
3176 policy
.zrp_request
);
3178 if (error
== EBADF
) {
3180 * If vdev_validate() returns failure (indicated by
3181 * EBADF), it indicates that one of the vdevs indicates
3182 * that the pool has been exported or destroyed. If
3183 * this is the case, the config cache is out of sync and
3184 * we should remove the pool from the namespace.
3187 spa_deactivate(spa
);
3188 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
3191 mutex_exit(&spa_namespace_lock
);
3192 return (SET_ERROR(ENOENT
));
3197 * We can't open the pool, but we still have useful
3198 * information: the state of each vdev after the
3199 * attempted vdev_open(). Return this to the user.
3201 if (config
!= NULL
&& spa
->spa_config
) {
3202 VERIFY(nvlist_dup(spa
->spa_config
, config
,
3204 VERIFY(nvlist_add_nvlist(*config
,
3205 ZPOOL_CONFIG_LOAD_INFO
,
3206 spa
->spa_load_info
) == 0);
3209 spa_deactivate(spa
);
3210 spa
->spa_last_open_failed
= error
;
3212 mutex_exit(&spa_namespace_lock
);
3218 spa_open_ref(spa
, tag
);
3221 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3224 * If we've recovered the pool, pass back any information we
3225 * gathered while doing the load.
3227 if (state
== SPA_LOAD_RECOVER
) {
3228 VERIFY(nvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
3229 spa
->spa_load_info
) == 0);
3233 spa
->spa_last_open_failed
= 0;
3234 spa
->spa_last_ubsync_txg
= 0;
3235 spa
->spa_load_txg
= 0;
3236 mutex_exit(&spa_namespace_lock
);
3240 zvol_create_minors(spa
, spa_name(spa
), B_TRUE
);
3248 spa_open_rewind(const char *name
, spa_t
**spapp
, void *tag
, nvlist_t
*policy
,
3251 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
3255 spa_open(const char *name
, spa_t
**spapp
, void *tag
)
3257 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
3261 * Lookup the given spa_t, incrementing the inject count in the process,
3262 * preventing it from being exported or destroyed.
3265 spa_inject_addref(char *name
)
3269 mutex_enter(&spa_namespace_lock
);
3270 if ((spa
= spa_lookup(name
)) == NULL
) {
3271 mutex_exit(&spa_namespace_lock
);
3274 spa
->spa_inject_ref
++;
3275 mutex_exit(&spa_namespace_lock
);
3281 spa_inject_delref(spa_t
*spa
)
3283 mutex_enter(&spa_namespace_lock
);
3284 spa
->spa_inject_ref
--;
3285 mutex_exit(&spa_namespace_lock
);
3289 * Add spares device information to the nvlist.
3292 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
3302 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3304 if (spa
->spa_spares
.sav_count
== 0)
3307 VERIFY(nvlist_lookup_nvlist(config
,
3308 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3309 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
3310 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3312 VERIFY(nvlist_add_nvlist_array(nvroot
,
3313 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3314 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3315 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3318 * Go through and find any spares which have since been
3319 * repurposed as an active spare. If this is the case, update
3320 * their status appropriately.
3322 for (i
= 0; i
< nspares
; i
++) {
3323 VERIFY(nvlist_lookup_uint64(spares
[i
],
3324 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3325 if (spa_spare_exists(guid
, &pool
, NULL
) &&
3327 VERIFY(nvlist_lookup_uint64_array(
3328 spares
[i
], ZPOOL_CONFIG_VDEV_STATS
,
3329 (uint64_t **)&vs
, &vsc
) == 0);
3330 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
3331 vs
->vs_aux
= VDEV_AUX_SPARED
;
3338 * Add l2cache device information to the nvlist, including vdev stats.
3341 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
3344 uint_t i
, j
, nl2cache
;
3351 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3353 if (spa
->spa_l2cache
.sav_count
== 0)
3356 VERIFY(nvlist_lookup_nvlist(config
,
3357 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3358 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
3359 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3360 if (nl2cache
!= 0) {
3361 VERIFY(nvlist_add_nvlist_array(nvroot
,
3362 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3363 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3364 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3367 * Update level 2 cache device stats.
3370 for (i
= 0; i
< nl2cache
; i
++) {
3371 VERIFY(nvlist_lookup_uint64(l2cache
[i
],
3372 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3375 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
3377 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
3378 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
3384 VERIFY(nvlist_lookup_uint64_array(l2cache
[i
],
3385 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
3387 vdev_get_stats(vd
, vs
);
3388 vdev_config_generate_stats(vd
, l2cache
[i
]);
3395 spa_feature_stats_from_disk(spa_t
*spa
, nvlist_t
*features
)
3400 if (spa
->spa_feat_for_read_obj
!= 0) {
3401 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3402 spa
->spa_feat_for_read_obj
);
3403 zap_cursor_retrieve(&zc
, &za
) == 0;
3404 zap_cursor_advance(&zc
)) {
3405 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3406 za
.za_num_integers
== 1);
3407 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
3408 za
.za_first_integer
));
3410 zap_cursor_fini(&zc
);
3413 if (spa
->spa_feat_for_write_obj
!= 0) {
3414 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3415 spa
->spa_feat_for_write_obj
);
3416 zap_cursor_retrieve(&zc
, &za
) == 0;
3417 zap_cursor_advance(&zc
)) {
3418 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3419 za
.za_num_integers
== 1);
3420 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
3421 za
.za_first_integer
));
3423 zap_cursor_fini(&zc
);
3428 spa_feature_stats_from_cache(spa_t
*spa
, nvlist_t
*features
)
3432 for (i
= 0; i
< SPA_FEATURES
; i
++) {
3433 zfeature_info_t feature
= spa_feature_table
[i
];
3436 if (feature_get_refcount(spa
, &feature
, &refcount
) != 0)
3439 VERIFY0(nvlist_add_uint64(features
, feature
.fi_guid
, refcount
));
3444 * Store a list of pool features and their reference counts in the
3447 * The first time this is called on a spa, allocate a new nvlist, fetch
3448 * the pool features and reference counts from disk, then save the list
3449 * in the spa. In subsequent calls on the same spa use the saved nvlist
3450 * and refresh its values from the cached reference counts. This
3451 * ensures we don't block here on I/O on a suspended pool so 'zpool
3452 * clear' can resume the pool.
3455 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
3459 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3461 mutex_enter(&spa
->spa_feat_stats_lock
);
3462 features
= spa
->spa_feat_stats
;
3464 if (features
!= NULL
) {
3465 spa_feature_stats_from_cache(spa
, features
);
3467 VERIFY0(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
));
3468 spa
->spa_feat_stats
= features
;
3469 spa_feature_stats_from_disk(spa
, features
);
3472 VERIFY0(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
3475 mutex_exit(&spa
->spa_feat_stats_lock
);
3479 spa_get_stats(const char *name
, nvlist_t
**config
,
3480 char *altroot
, size_t buflen
)
3486 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
3490 * This still leaves a window of inconsistency where the spares
3491 * or l2cache devices could change and the config would be
3492 * self-inconsistent.
3494 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
3496 if (*config
!= NULL
) {
3497 uint64_t loadtimes
[2];
3499 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
3500 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
3501 VERIFY(nvlist_add_uint64_array(*config
,
3502 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2) == 0);
3504 VERIFY(nvlist_add_uint64(*config
,
3505 ZPOOL_CONFIG_ERRCOUNT
,
3506 spa_get_errlog_size(spa
)) == 0);
3508 if (spa_suspended(spa
))
3509 VERIFY(nvlist_add_uint64(*config
,
3510 ZPOOL_CONFIG_SUSPENDED
,
3511 spa
->spa_failmode
) == 0);
3513 spa_add_spares(spa
, *config
);
3514 spa_add_l2cache(spa
, *config
);
3515 spa_add_feature_stats(spa
, *config
);
3520 * We want to get the alternate root even for faulted pools, so we cheat
3521 * and call spa_lookup() directly.
3525 mutex_enter(&spa_namespace_lock
);
3526 spa
= spa_lookup(name
);
3528 spa_altroot(spa
, altroot
, buflen
);
3532 mutex_exit(&spa_namespace_lock
);
3534 spa_altroot(spa
, altroot
, buflen
);
3539 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
3540 spa_close(spa
, FTAG
);
3547 * Validate that the auxiliary device array is well formed. We must have an
3548 * array of nvlists, each which describes a valid leaf vdev. If this is an
3549 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3550 * specified, as long as they are well-formed.
3553 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
3554 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
3555 vdev_labeltype_t label
)
3562 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3565 * It's acceptable to have no devs specified.
3567 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
3571 return (SET_ERROR(EINVAL
));
3574 * Make sure the pool is formatted with a version that supports this
3577 if (spa_version(spa
) < version
)
3578 return (SET_ERROR(ENOTSUP
));
3581 * Set the pending device list so we correctly handle device in-use
3584 sav
->sav_pending
= dev
;
3585 sav
->sav_npending
= ndev
;
3587 for (i
= 0; i
< ndev
; i
++) {
3588 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
3592 if (!vd
->vdev_ops
->vdev_op_leaf
) {
3594 error
= SET_ERROR(EINVAL
);
3599 * The L2ARC currently only supports disk devices in
3600 * kernel context. For user-level testing, we allow it.
3603 if ((strcmp(config
, ZPOOL_CONFIG_L2CACHE
) == 0) &&
3604 strcmp(vd
->vdev_ops
->vdev_op_type
, VDEV_TYPE_DISK
) != 0) {
3605 error
= SET_ERROR(ENOTBLK
);
3612 if ((error
= vdev_open(vd
)) == 0 &&
3613 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
3614 VERIFY(nvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
3615 vd
->vdev_guid
) == 0);
3621 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
3628 sav
->sav_pending
= NULL
;
3629 sav
->sav_npending
= 0;
3634 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
3638 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3640 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3641 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
3642 VDEV_LABEL_SPARE
)) != 0) {
3646 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3647 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
3648 VDEV_LABEL_L2CACHE
));
3652 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
3657 if (sav
->sav_config
!= NULL
) {
3663 * Generate new dev list by concatentating with the
3666 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
3667 &olddevs
, &oldndevs
) == 0);
3669 newdevs
= kmem_alloc(sizeof (void *) *
3670 (ndevs
+ oldndevs
), KM_SLEEP
);
3671 for (i
= 0; i
< oldndevs
; i
++)
3672 VERIFY(nvlist_dup(olddevs
[i
], &newdevs
[i
],
3674 for (i
= 0; i
< ndevs
; i
++)
3675 VERIFY(nvlist_dup(devs
[i
], &newdevs
[i
+ oldndevs
],
3678 VERIFY(nvlist_remove(sav
->sav_config
, config
,
3679 DATA_TYPE_NVLIST_ARRAY
) == 0);
3681 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
3682 config
, newdevs
, ndevs
+ oldndevs
) == 0);
3683 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
3684 nvlist_free(newdevs
[i
]);
3685 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
3688 * Generate a new dev list.
3690 VERIFY(nvlist_alloc(&sav
->sav_config
, NV_UNIQUE_NAME
,
3692 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
, config
,
3698 * Stop and drop level 2 ARC devices
3701 spa_l2cache_drop(spa_t
*spa
)
3705 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
3707 for (i
= 0; i
< sav
->sav_count
; i
++) {
3710 vd
= sav
->sav_vdevs
[i
];
3713 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
3714 pool
!= 0ULL && l2arc_vdev_present(vd
))
3715 l2arc_remove_vdev(vd
);
3723 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
3727 char *altroot
= NULL
;
3732 uint64_t txg
= TXG_INITIAL
;
3733 nvlist_t
**spares
, **l2cache
;
3734 uint_t nspares
, nl2cache
;
3735 uint64_t version
, obj
;
3736 boolean_t has_features
;
3742 if (nvlist_lookup_string(props
, "tname", &poolname
) != 0)
3743 poolname
= (char *)pool
;
3746 * If this pool already exists, return failure.
3748 mutex_enter(&spa_namespace_lock
);
3749 if (spa_lookup(poolname
) != NULL
) {
3750 mutex_exit(&spa_namespace_lock
);
3751 return (SET_ERROR(EEXIST
));
3755 * Allocate a new spa_t structure.
3757 nvl
= fnvlist_alloc();
3758 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
3759 (void) nvlist_lookup_string(props
,
3760 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
3761 spa
= spa_add(poolname
, nvl
, altroot
);
3763 spa_activate(spa
, spa_mode_global
);
3765 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
3766 spa_deactivate(spa
);
3768 mutex_exit(&spa_namespace_lock
);
3773 * Temporary pool names should never be written to disk.
3775 if (poolname
!= pool
)
3776 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
3778 has_features
= B_FALSE
;
3779 for (elem
= nvlist_next_nvpair(props
, NULL
);
3780 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
3781 if (zpool_prop_feature(nvpair_name(elem
)))
3782 has_features
= B_TRUE
;
3785 if (has_features
|| nvlist_lookup_uint64(props
,
3786 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
3787 version
= SPA_VERSION
;
3789 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
3791 spa
->spa_first_txg
= txg
;
3792 spa
->spa_uberblock
.ub_txg
= txg
- 1;
3793 spa
->spa_uberblock
.ub_version
= version
;
3794 spa
->spa_ubsync
= spa
->spa_uberblock
;
3797 * Create "The Godfather" zio to hold all async IOs
3799 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
3801 for (i
= 0; i
< max_ncpus
; i
++) {
3802 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
3803 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
3804 ZIO_FLAG_GODFATHER
);
3808 * Create the root vdev.
3810 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3812 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
3814 ASSERT(error
!= 0 || rvd
!= NULL
);
3815 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
3817 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
3818 error
= SET_ERROR(EINVAL
);
3821 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
3822 (error
= spa_validate_aux(spa
, nvroot
, txg
,
3823 VDEV_ALLOC_ADD
)) == 0) {
3824 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
3825 vdev_metaslab_set_size(rvd
->vdev_child
[c
]);
3826 vdev_expand(rvd
->vdev_child
[c
], txg
);
3830 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3834 spa_deactivate(spa
);
3836 mutex_exit(&spa_namespace_lock
);
3841 * Get the list of spares, if specified.
3843 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
3844 &spares
, &nspares
) == 0) {
3845 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
, NV_UNIQUE_NAME
,
3847 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
3848 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3849 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3850 spa_load_spares(spa
);
3851 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3852 spa
->spa_spares
.sav_sync
= B_TRUE
;
3856 * Get the list of level 2 cache devices, if specified.
3858 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
3859 &l2cache
, &nl2cache
) == 0) {
3860 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
3861 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3862 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
3863 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3864 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3865 spa_load_l2cache(spa
);
3866 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3867 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3870 spa
->spa_is_initializing
= B_TRUE
;
3871 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, txg
);
3872 spa
->spa_meta_objset
= dp
->dp_meta_objset
;
3873 spa
->spa_is_initializing
= B_FALSE
;
3876 * Create DDTs (dedup tables).
3880 spa_update_dspace(spa
);
3882 tx
= dmu_tx_create_assigned(dp
, txg
);
3885 * Create the pool config object.
3887 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
3888 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
3889 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
3891 if (zap_add(spa
->spa_meta_objset
,
3892 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
3893 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
3894 cmn_err(CE_PANIC
, "failed to add pool config");
3897 if (spa_version(spa
) >= SPA_VERSION_FEATURES
)
3898 spa_feature_create_zap_objects(spa
, tx
);
3900 if (zap_add(spa
->spa_meta_objset
,
3901 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
3902 sizeof (uint64_t), 1, &version
, tx
) != 0) {
3903 cmn_err(CE_PANIC
, "failed to add pool version");
3906 /* Newly created pools with the right version are always deflated. */
3907 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
3908 spa
->spa_deflate
= TRUE
;
3909 if (zap_add(spa
->spa_meta_objset
,
3910 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
3911 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
3912 cmn_err(CE_PANIC
, "failed to add deflate");
3917 * Create the deferred-free bpobj. Turn off compression
3918 * because sync-to-convergence takes longer if the blocksize
3921 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
3922 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
3923 ZIO_COMPRESS_OFF
, tx
);
3924 if (zap_add(spa
->spa_meta_objset
,
3925 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
3926 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
3927 cmn_err(CE_PANIC
, "failed to add bpobj");
3929 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
3930 spa
->spa_meta_objset
, obj
));
3933 * Create the pool's history object.
3935 if (version
>= SPA_VERSION_ZPOOL_HISTORY
)
3936 spa_history_create_obj(spa
, tx
);
3939 * Set pool properties.
3941 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
3942 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
3943 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
3944 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
3946 if (props
!= NULL
) {
3947 spa_configfile_set(spa
, props
, B_FALSE
);
3948 spa_sync_props(props
, tx
);
3953 spa
->spa_sync_on
= B_TRUE
;
3954 txg_sync_start(spa
->spa_dsl_pool
);
3957 * We explicitly wait for the first transaction to complete so that our
3958 * bean counters are appropriately updated.
3960 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
3962 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
3963 spa_event_notify(spa
, NULL
, ESC_ZFS_POOL_CREATE
);
3965 spa_history_log_version(spa
, "create");
3968 * Don't count references from objsets that are already closed
3969 * and are making their way through the eviction process.
3971 spa_evicting_os_wait(spa
);
3972 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
3974 mutex_exit(&spa_namespace_lock
);
3980 * Import a non-root pool into the system.
3983 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
3986 char *altroot
= NULL
;
3987 spa_load_state_t state
= SPA_LOAD_IMPORT
;
3988 zpool_rewind_policy_t policy
;
3989 uint64_t mode
= spa_mode_global
;
3990 uint64_t readonly
= B_FALSE
;
3993 nvlist_t
**spares
, **l2cache
;
3994 uint_t nspares
, nl2cache
;
3997 * If a pool with this name exists, return failure.
3999 mutex_enter(&spa_namespace_lock
);
4000 if (spa_lookup(pool
) != NULL
) {
4001 mutex_exit(&spa_namespace_lock
);
4002 return (SET_ERROR(EEXIST
));
4006 * Create and initialize the spa structure.
4008 (void) nvlist_lookup_string(props
,
4009 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
4010 (void) nvlist_lookup_uint64(props
,
4011 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
4014 spa
= spa_add(pool
, config
, altroot
);
4015 spa
->spa_import_flags
= flags
;
4018 * Verbatim import - Take a pool and insert it into the namespace
4019 * as if it had been loaded at boot.
4021 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
4023 spa_configfile_set(spa
, props
, B_FALSE
);
4025 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
4026 spa_event_notify(spa
, NULL
, ESC_ZFS_POOL_IMPORT
);
4028 mutex_exit(&spa_namespace_lock
);
4032 spa_activate(spa
, mode
);
4035 * Don't start async tasks until we know everything is healthy.
4037 spa_async_suspend(spa
);
4039 zpool_get_rewind_policy(config
, &policy
);
4040 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
4041 state
= SPA_LOAD_RECOVER
;
4044 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4045 * because the user-supplied config is actually the one to trust when
4048 if (state
!= SPA_LOAD_RECOVER
)
4049 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
4051 error
= spa_load_best(spa
, state
, B_TRUE
, policy
.zrp_txg
,
4052 policy
.zrp_request
);
4055 * Propagate anything learned while loading the pool and pass it
4056 * back to caller (i.e. rewind info, missing devices, etc).
4058 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4059 spa
->spa_load_info
) == 0);
4061 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4063 * Toss any existing sparelist, as it doesn't have any validity
4064 * anymore, and conflicts with spa_has_spare().
4066 if (spa
->spa_spares
.sav_config
) {
4067 nvlist_free(spa
->spa_spares
.sav_config
);
4068 spa
->spa_spares
.sav_config
= NULL
;
4069 spa_load_spares(spa
);
4071 if (spa
->spa_l2cache
.sav_config
) {
4072 nvlist_free(spa
->spa_l2cache
.sav_config
);
4073 spa
->spa_l2cache
.sav_config
= NULL
;
4074 spa_load_l2cache(spa
);
4077 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4080 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
4083 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
4084 VDEV_ALLOC_L2CACHE
);
4085 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4088 spa_configfile_set(spa
, props
, B_FALSE
);
4090 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
4091 (error
= spa_prop_set(spa
, props
)))) {
4093 spa_deactivate(spa
);
4095 mutex_exit(&spa_namespace_lock
);
4099 spa_async_resume(spa
);
4102 * Override any spares and level 2 cache devices as specified by
4103 * the user, as these may have correct device names/devids, etc.
4105 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4106 &spares
, &nspares
) == 0) {
4107 if (spa
->spa_spares
.sav_config
)
4108 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
,
4109 ZPOOL_CONFIG_SPARES
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4111 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
,
4112 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4113 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
4114 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
4115 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4116 spa_load_spares(spa
);
4117 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4118 spa
->spa_spares
.sav_sync
= B_TRUE
;
4120 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4121 &l2cache
, &nl2cache
) == 0) {
4122 if (spa
->spa_l2cache
.sav_config
)
4123 VERIFY(nvlist_remove(spa
->spa_l2cache
.sav_config
,
4124 ZPOOL_CONFIG_L2CACHE
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4126 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
4127 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4128 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
4129 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
4130 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4131 spa_load_l2cache(spa
);
4132 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4133 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4137 * Check for any removed devices.
4139 if (spa
->spa_autoreplace
) {
4140 spa_aux_check_removed(&spa
->spa_spares
);
4141 spa_aux_check_removed(&spa
->spa_l2cache
);
4144 if (spa_writeable(spa
)) {
4146 * Update the config cache to include the newly-imported pool.
4148 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4152 * It's possible that the pool was expanded while it was exported.
4153 * We kick off an async task to handle this for us.
4155 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
4157 spa_history_log_version(spa
, "import");
4159 spa_event_notify(spa
, NULL
, ESC_ZFS_POOL_IMPORT
);
4161 zvol_create_minors(spa
, pool
, B_TRUE
);
4163 mutex_exit(&spa_namespace_lock
);
4169 spa_tryimport(nvlist_t
*tryconfig
)
4171 nvlist_t
*config
= NULL
;
4177 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
4180 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
4184 * Create and initialize the spa structure.
4186 mutex_enter(&spa_namespace_lock
);
4187 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
4188 spa_activate(spa
, FREAD
);
4191 * Pass off the heavy lifting to spa_load().
4192 * Pass TRUE for mosconfig because the user-supplied config
4193 * is actually the one to trust when doing an import.
4195 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
, B_TRUE
);
4198 * If 'tryconfig' was at least parsable, return the current config.
4200 if (spa
->spa_root_vdev
!= NULL
) {
4201 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4202 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
,
4204 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
4206 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
4207 spa
->spa_uberblock
.ub_timestamp
) == 0);
4208 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4209 spa
->spa_load_info
) == 0);
4210 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
4211 spa
->spa_errata
) == 0);
4214 * If the bootfs property exists on this pool then we
4215 * copy it out so that external consumers can tell which
4216 * pools are bootable.
4218 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
4219 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4222 * We have to play games with the name since the
4223 * pool was opened as TRYIMPORT_NAME.
4225 if (dsl_dsobj_to_dsname(spa_name(spa
),
4226 spa
->spa_bootfs
, tmpname
) == 0) {
4230 dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4232 cp
= strchr(tmpname
, '/');
4234 (void) strlcpy(dsname
, tmpname
,
4237 (void) snprintf(dsname
, MAXPATHLEN
,
4238 "%s/%s", poolname
, ++cp
);
4240 VERIFY(nvlist_add_string(config
,
4241 ZPOOL_CONFIG_BOOTFS
, dsname
) == 0);
4242 kmem_free(dsname
, MAXPATHLEN
);
4244 kmem_free(tmpname
, MAXPATHLEN
);
4248 * Add the list of hot spares and level 2 cache devices.
4250 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4251 spa_add_spares(spa
, config
);
4252 spa_add_l2cache(spa
, config
);
4253 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4257 spa_deactivate(spa
);
4259 mutex_exit(&spa_namespace_lock
);
4265 * Pool export/destroy
4267 * The act of destroying or exporting a pool is very simple. We make sure there
4268 * is no more pending I/O and any references to the pool are gone. Then, we
4269 * update the pool state and sync all the labels to disk, removing the
4270 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4271 * we don't sync the labels or remove the configuration cache.
4274 spa_export_common(char *pool
, int new_state
, nvlist_t
**oldconfig
,
4275 boolean_t force
, boolean_t hardforce
)
4282 if (!(spa_mode_global
& FWRITE
))
4283 return (SET_ERROR(EROFS
));
4285 mutex_enter(&spa_namespace_lock
);
4286 if ((spa
= spa_lookup(pool
)) == NULL
) {
4287 mutex_exit(&spa_namespace_lock
);
4288 return (SET_ERROR(ENOENT
));
4292 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4293 * reacquire the namespace lock, and see if we can export.
4295 spa_open_ref(spa
, FTAG
);
4296 mutex_exit(&spa_namespace_lock
);
4297 spa_async_suspend(spa
);
4298 if (spa
->spa_zvol_taskq
) {
4299 zvol_remove_minors(spa
, spa_name(spa
), B_TRUE
);
4300 taskq_wait(spa
->spa_zvol_taskq
);
4302 mutex_enter(&spa_namespace_lock
);
4303 spa_close(spa
, FTAG
);
4305 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
4308 * The pool will be in core if it's openable, in which case we can
4309 * modify its state. Objsets may be open only because they're dirty,
4310 * so we have to force it to sync before checking spa_refcnt.
4312 if (spa
->spa_sync_on
) {
4313 txg_wait_synced(spa
->spa_dsl_pool
, 0);
4314 spa_evicting_os_wait(spa
);
4318 * A pool cannot be exported or destroyed if there are active
4319 * references. If we are resetting a pool, allow references by
4320 * fault injection handlers.
4322 if (!spa_refcount_zero(spa
) ||
4323 (spa
->spa_inject_ref
!= 0 &&
4324 new_state
!= POOL_STATE_UNINITIALIZED
)) {
4325 spa_async_resume(spa
);
4326 mutex_exit(&spa_namespace_lock
);
4327 return (SET_ERROR(EBUSY
));
4330 if (spa
->spa_sync_on
) {
4332 * A pool cannot be exported if it has an active shared spare.
4333 * This is to prevent other pools stealing the active spare
4334 * from an exported pool. At user's own will, such pool can
4335 * be forcedly exported.
4337 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
4338 spa_has_active_shared_spare(spa
)) {
4339 spa_async_resume(spa
);
4340 mutex_exit(&spa_namespace_lock
);
4341 return (SET_ERROR(EXDEV
));
4345 * We want this to be reflected on every label,
4346 * so mark them all dirty. spa_unload() will do the
4347 * final sync that pushes these changes out.
4349 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
4350 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4351 spa
->spa_state
= new_state
;
4352 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
4354 vdev_config_dirty(spa
->spa_root_vdev
);
4355 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4360 spa_event_notify(spa
, NULL
, ESC_ZFS_POOL_DESTROY
);
4362 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
4364 spa_deactivate(spa
);
4367 if (oldconfig
&& spa
->spa_config
)
4368 VERIFY(nvlist_dup(spa
->spa_config
, oldconfig
, 0) == 0);
4370 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
4372 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
4375 mutex_exit(&spa_namespace_lock
);
4381 * Destroy a storage pool.
4384 spa_destroy(char *pool
)
4386 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
4391 * Export a storage pool.
4394 spa_export(char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
4395 boolean_t hardforce
)
4397 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
4402 * Similar to spa_export(), this unloads the spa_t without actually removing it
4403 * from the namespace in any way.
4406 spa_reset(char *pool
)
4408 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
4413 * ==========================================================================
4414 * Device manipulation
4415 * ==========================================================================
4419 * Add a device to a storage pool.
4422 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
4426 vdev_t
*rvd
= spa
->spa_root_vdev
;
4428 nvlist_t
**spares
, **l2cache
;
4429 uint_t nspares
, nl2cache
;
4432 ASSERT(spa_writeable(spa
));
4434 txg
= spa_vdev_enter(spa
);
4436 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
4437 VDEV_ALLOC_ADD
)) != 0)
4438 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4440 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
4442 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
4446 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
4450 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
4451 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
4453 if (vd
->vdev_children
!= 0 &&
4454 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0)
4455 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4458 * We must validate the spares and l2cache devices after checking the
4459 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4461 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
4462 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4465 * Transfer each new top-level vdev from vd to rvd.
4467 for (c
= 0; c
< vd
->vdev_children
; c
++) {
4470 * Set the vdev id to the first hole, if one exists.
4472 for (id
= 0; id
< rvd
->vdev_children
; id
++) {
4473 if (rvd
->vdev_child
[id
]->vdev_ishole
) {
4474 vdev_free(rvd
->vdev_child
[id
]);
4478 tvd
= vd
->vdev_child
[c
];
4479 vdev_remove_child(vd
, tvd
);
4481 vdev_add_child(rvd
, tvd
);
4482 vdev_config_dirty(tvd
);
4486 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
4487 ZPOOL_CONFIG_SPARES
);
4488 spa_load_spares(spa
);
4489 spa
->spa_spares
.sav_sync
= B_TRUE
;
4492 if (nl2cache
!= 0) {
4493 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
4494 ZPOOL_CONFIG_L2CACHE
);
4495 spa_load_l2cache(spa
);
4496 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4500 * We have to be careful when adding new vdevs to an existing pool.
4501 * If other threads start allocating from these vdevs before we
4502 * sync the config cache, and we lose power, then upon reboot we may
4503 * fail to open the pool because there are DVAs that the config cache
4504 * can't translate. Therefore, we first add the vdevs without
4505 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4506 * and then let spa_config_update() initialize the new metaslabs.
4508 * spa_load() checks for added-but-not-initialized vdevs, so that
4509 * if we lose power at any point in this sequence, the remaining
4510 * steps will be completed the next time we load the pool.
4512 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
4514 mutex_enter(&spa_namespace_lock
);
4515 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4516 spa_event_notify(spa
, NULL
, ESC_ZFS_VDEV_ADD
);
4517 mutex_exit(&spa_namespace_lock
);
4523 * Attach a device to a mirror. The arguments are the path to any device
4524 * in the mirror, and the nvroot for the new device. If the path specifies
4525 * a device that is not mirrored, we automatically insert the mirror vdev.
4527 * If 'replacing' is specified, the new device is intended to replace the
4528 * existing device; in this case the two devices are made into their own
4529 * mirror using the 'replacing' vdev, which is functionally identical to
4530 * the mirror vdev (it actually reuses all the same ops) but has a few
4531 * extra rules: you can't attach to it after it's been created, and upon
4532 * completion of resilvering, the first disk (the one being replaced)
4533 * is automatically detached.
4536 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
)
4538 uint64_t txg
, dtl_max_txg
;
4539 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
4541 char *oldvdpath
, *newvdpath
;
4544 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
4546 ASSERT(spa_writeable(spa
));
4548 txg
= spa_vdev_enter(spa
);
4550 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4553 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4555 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
4556 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4558 pvd
= oldvd
->vdev_parent
;
4560 if ((error
= spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
4561 VDEV_ALLOC_ATTACH
)) != 0)
4562 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4564 if (newrootvd
->vdev_children
!= 1)
4565 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4567 newvd
= newrootvd
->vdev_child
[0];
4569 if (!newvd
->vdev_ops
->vdev_op_leaf
)
4570 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4572 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
4573 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
4576 * Spares can't replace logs
4578 if (oldvd
->vdev_top
->vdev_islog
&& newvd
->vdev_isspare
)
4579 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4583 * For attach, the only allowable parent is a mirror or the root
4586 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4587 pvd
->vdev_ops
!= &vdev_root_ops
)
4588 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4590 pvops
= &vdev_mirror_ops
;
4593 * Active hot spares can only be replaced by inactive hot
4596 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4597 oldvd
->vdev_isspare
&&
4598 !spa_has_spare(spa
, newvd
->vdev_guid
))
4599 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4602 * If the source is a hot spare, and the parent isn't already a
4603 * spare, then we want to create a new hot spare. Otherwise, we
4604 * want to create a replacing vdev. The user is not allowed to
4605 * attach to a spared vdev child unless the 'isspare' state is
4606 * the same (spare replaces spare, non-spare replaces
4609 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
4610 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
4611 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4612 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4613 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
4614 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4617 if (newvd
->vdev_isspare
)
4618 pvops
= &vdev_spare_ops
;
4620 pvops
= &vdev_replacing_ops
;
4624 * Make sure the new device is big enough.
4626 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
4627 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
4630 * The new device cannot have a higher alignment requirement
4631 * than the top-level vdev.
4633 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
4634 return (spa_vdev_exit(spa
, newrootvd
, txg
, EDOM
));
4637 * If this is an in-place replacement, update oldvd's path and devid
4638 * to make it distinguishable from newvd, and unopenable from now on.
4640 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
4641 spa_strfree(oldvd
->vdev_path
);
4642 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
4644 (void) sprintf(oldvd
->vdev_path
, "%s/%s",
4645 newvd
->vdev_path
, "old");
4646 if (oldvd
->vdev_devid
!= NULL
) {
4647 spa_strfree(oldvd
->vdev_devid
);
4648 oldvd
->vdev_devid
= NULL
;
4652 /* mark the device being resilvered */
4653 newvd
->vdev_resilver_txg
= txg
;
4656 * If the parent is not a mirror, or if we're replacing, insert the new
4657 * mirror/replacing/spare vdev above oldvd.
4659 if (pvd
->vdev_ops
!= pvops
)
4660 pvd
= vdev_add_parent(oldvd
, pvops
);
4662 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
4663 ASSERT(pvd
->vdev_ops
== pvops
);
4664 ASSERT(oldvd
->vdev_parent
== pvd
);
4667 * Extract the new device from its root and add it to pvd.
4669 vdev_remove_child(newrootvd
, newvd
);
4670 newvd
->vdev_id
= pvd
->vdev_children
;
4671 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
4672 vdev_add_child(pvd
, newvd
);
4674 tvd
= newvd
->vdev_top
;
4675 ASSERT(pvd
->vdev_top
== tvd
);
4676 ASSERT(tvd
->vdev_parent
== rvd
);
4678 vdev_config_dirty(tvd
);
4681 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4682 * for any dmu_sync-ed blocks. It will propagate upward when
4683 * spa_vdev_exit() calls vdev_dtl_reassess().
4685 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
4687 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
4688 dtl_max_txg
- TXG_INITIAL
);
4690 if (newvd
->vdev_isspare
) {
4691 spa_spare_activate(newvd
);
4692 spa_event_notify(spa
, newvd
, ESC_ZFS_VDEV_SPARE
);
4695 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
4696 newvdpath
= spa_strdup(newvd
->vdev_path
);
4697 newvd_isspare
= newvd
->vdev_isspare
;
4700 * Mark newvd's DTL dirty in this txg.
4702 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
4705 * Schedule the resilver to restart in the future. We do this to
4706 * ensure that dmu_sync-ed blocks have been stitched into the
4707 * respective datasets.
4709 dsl_resilver_restart(spa
->spa_dsl_pool
, dtl_max_txg
);
4711 if (spa
->spa_bootfs
)
4712 spa_event_notify(spa
, newvd
, ESC_ZFS_BOOTFS_VDEV_ATTACH
);
4714 spa_event_notify(spa
, newvd
, ESC_ZFS_VDEV_ATTACH
);
4719 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
4721 spa_history_log_internal(spa
, "vdev attach", NULL
,
4722 "%s vdev=%s %s vdev=%s",
4723 replacing
&& newvd_isspare
? "spare in" :
4724 replacing
? "replace" : "attach", newvdpath
,
4725 replacing
? "for" : "to", oldvdpath
);
4727 spa_strfree(oldvdpath
);
4728 spa_strfree(newvdpath
);
4734 * Detach a device from a mirror or replacing vdev.
4736 * If 'replace_done' is specified, only detach if the parent
4737 * is a replacing vdev.
4740 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
4744 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
4745 boolean_t unspare
= B_FALSE
;
4746 uint64_t unspare_guid
= 0;
4749 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
4750 ASSERT(spa_writeable(spa
));
4752 txg
= spa_vdev_enter(spa
);
4754 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4757 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4759 if (!vd
->vdev_ops
->vdev_op_leaf
)
4760 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4762 pvd
= vd
->vdev_parent
;
4765 * If the parent/child relationship is not as expected, don't do it.
4766 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4767 * vdev that's replacing B with C. The user's intent in replacing
4768 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4769 * the replace by detaching C, the expected behavior is to end up
4770 * M(A,B). But suppose that right after deciding to detach C,
4771 * the replacement of B completes. We would have M(A,C), and then
4772 * ask to detach C, which would leave us with just A -- not what
4773 * the user wanted. To prevent this, we make sure that the
4774 * parent/child relationship hasn't changed -- in this example,
4775 * that C's parent is still the replacing vdev R.
4777 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
4778 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4781 * Only 'replacing' or 'spare' vdevs can be replaced.
4783 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4784 pvd
->vdev_ops
!= &vdev_spare_ops
)
4785 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4787 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
4788 spa_version(spa
) >= SPA_VERSION_SPARES
);
4791 * Only mirror, replacing, and spare vdevs support detach.
4793 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4794 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4795 pvd
->vdev_ops
!= &vdev_spare_ops
)
4796 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4799 * If this device has the only valid copy of some data,
4800 * we cannot safely detach it.
4802 if (vdev_dtl_required(vd
))
4803 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4805 ASSERT(pvd
->vdev_children
>= 2);
4808 * If we are detaching the second disk from a replacing vdev, then
4809 * check to see if we changed the original vdev's path to have "/old"
4810 * at the end in spa_vdev_attach(). If so, undo that change now.
4812 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
4813 vd
->vdev_path
!= NULL
) {
4814 size_t len
= strlen(vd
->vdev_path
);
4816 for (c
= 0; c
< pvd
->vdev_children
; c
++) {
4817 cvd
= pvd
->vdev_child
[c
];
4819 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
4822 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
4823 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
4824 spa_strfree(cvd
->vdev_path
);
4825 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
4832 * If we are detaching the original disk from a spare, then it implies
4833 * that the spare should become a real disk, and be removed from the
4834 * active spare list for the pool.
4836 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4838 pvd
->vdev_child
[pvd
->vdev_children
- 1]->vdev_isspare
)
4842 * Erase the disk labels so the disk can be used for other things.
4843 * This must be done after all other error cases are handled,
4844 * but before we disembowel vd (so we can still do I/O to it).
4845 * But if we can't do it, don't treat the error as fatal --
4846 * it may be that the unwritability of the disk is the reason
4847 * it's being detached!
4849 error
= vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
4852 * Remove vd from its parent and compact the parent's children.
4854 vdev_remove_child(pvd
, vd
);
4855 vdev_compact_children(pvd
);
4858 * Remember one of the remaining children so we can get tvd below.
4860 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
4863 * If we need to remove the remaining child from the list of hot spares,
4864 * do it now, marking the vdev as no longer a spare in the process.
4865 * We must do this before vdev_remove_parent(), because that can
4866 * change the GUID if it creates a new toplevel GUID. For a similar
4867 * reason, we must remove the spare now, in the same txg as the detach;
4868 * otherwise someone could attach a new sibling, change the GUID, and
4869 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4872 ASSERT(cvd
->vdev_isspare
);
4873 spa_spare_remove(cvd
);
4874 unspare_guid
= cvd
->vdev_guid
;
4875 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
4876 cvd
->vdev_unspare
= B_TRUE
;
4880 * If the parent mirror/replacing vdev only has one child,
4881 * the parent is no longer needed. Remove it from the tree.
4883 if (pvd
->vdev_children
== 1) {
4884 if (pvd
->vdev_ops
== &vdev_spare_ops
)
4885 cvd
->vdev_unspare
= B_FALSE
;
4886 vdev_remove_parent(cvd
);
4891 * We don't set tvd until now because the parent we just removed
4892 * may have been the previous top-level vdev.
4894 tvd
= cvd
->vdev_top
;
4895 ASSERT(tvd
->vdev_parent
== rvd
);
4898 * Reevaluate the parent vdev state.
4900 vdev_propagate_state(cvd
);
4903 * If the 'autoexpand' property is set on the pool then automatically
4904 * try to expand the size of the pool. For example if the device we
4905 * just detached was smaller than the others, it may be possible to
4906 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4907 * first so that we can obtain the updated sizes of the leaf vdevs.
4909 if (spa
->spa_autoexpand
) {
4911 vdev_expand(tvd
, txg
);
4914 vdev_config_dirty(tvd
);
4917 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4918 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4919 * But first make sure we're not on any *other* txg's DTL list, to
4920 * prevent vd from being accessed after it's freed.
4922 vdpath
= spa_strdup(vd
->vdev_path
);
4923 for (t
= 0; t
< TXG_SIZE
; t
++)
4924 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
4925 vd
->vdev_detached
= B_TRUE
;
4926 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
4928 spa_event_notify(spa
, vd
, ESC_ZFS_VDEV_REMOVE
);
4930 /* hang on to the spa before we release the lock */
4931 spa_open_ref(spa
, FTAG
);
4933 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
4935 spa_history_log_internal(spa
, "detach", NULL
,
4937 spa_strfree(vdpath
);
4940 * If this was the removal of the original device in a hot spare vdev,
4941 * then we want to go through and remove the device from the hot spare
4942 * list of every other pool.
4945 spa_t
*altspa
= NULL
;
4947 mutex_enter(&spa_namespace_lock
);
4948 while ((altspa
= spa_next(altspa
)) != NULL
) {
4949 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
4953 spa_open_ref(altspa
, FTAG
);
4954 mutex_exit(&spa_namespace_lock
);
4955 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
4956 mutex_enter(&spa_namespace_lock
);
4957 spa_close(altspa
, FTAG
);
4959 mutex_exit(&spa_namespace_lock
);
4961 /* search the rest of the vdevs for spares to remove */
4962 spa_vdev_resilver_done(spa
);
4965 /* all done with the spa; OK to release */
4966 mutex_enter(&spa_namespace_lock
);
4967 spa_close(spa
, FTAG
);
4968 mutex_exit(&spa_namespace_lock
);
4974 * Split a set of devices from their mirrors, and create a new pool from them.
4977 spa_vdev_split_mirror(spa_t
*spa
, char *newname
, nvlist_t
*config
,
4978 nvlist_t
*props
, boolean_t exp
)
4981 uint64_t txg
, *glist
;
4983 uint_t c
, children
, lastlog
;
4984 nvlist_t
**child
, *nvl
, *tmp
;
4986 char *altroot
= NULL
;
4987 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
4988 boolean_t activate_slog
;
4990 ASSERT(spa_writeable(spa
));
4992 txg
= spa_vdev_enter(spa
);
4994 /* clear the log and flush everything up to now */
4995 activate_slog
= spa_passivate_log(spa
);
4996 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
4997 error
= spa_offline_log(spa
);
4998 txg
= spa_vdev_config_enter(spa
);
5001 spa_activate_log(spa
);
5004 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5006 /* check new spa name before going any further */
5007 if (spa_lookup(newname
) != NULL
)
5008 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
5011 * scan through all the children to ensure they're all mirrors
5013 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
5014 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
5016 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5018 /* first, check to ensure we've got the right child count */
5019 rvd
= spa
->spa_root_vdev
;
5021 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
5022 vdev_t
*vd
= rvd
->vdev_child
[c
];
5024 /* don't count the holes & logs as children */
5025 if (vd
->vdev_islog
|| vd
->vdev_ishole
) {
5033 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
5034 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5036 /* next, ensure no spare or cache devices are part of the split */
5037 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
5038 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
5039 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5041 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
5042 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
5044 /* then, loop over each vdev and validate it */
5045 for (c
= 0; c
< children
; c
++) {
5046 uint64_t is_hole
= 0;
5048 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
5052 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
5053 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
5056 error
= SET_ERROR(EINVAL
);
5061 /* which disk is going to be split? */
5062 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
5064 error
= SET_ERROR(EINVAL
);
5068 /* look it up in the spa */
5069 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
5070 if (vml
[c
] == NULL
) {
5071 error
= SET_ERROR(ENODEV
);
5075 /* make sure there's nothing stopping the split */
5076 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
5077 vml
[c
]->vdev_islog
||
5078 vml
[c
]->vdev_ishole
||
5079 vml
[c
]->vdev_isspare
||
5080 vml
[c
]->vdev_isl2cache
||
5081 !vdev_writeable(vml
[c
]) ||
5082 vml
[c
]->vdev_children
!= 0 ||
5083 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
5084 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
5085 error
= SET_ERROR(EINVAL
);
5089 if (vdev_dtl_required(vml
[c
])) {
5090 error
= SET_ERROR(EBUSY
);
5094 /* we need certain info from the top level */
5095 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
5096 vml
[c
]->vdev_top
->vdev_ms_array
) == 0);
5097 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
5098 vml
[c
]->vdev_top
->vdev_ms_shift
) == 0);
5099 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
5100 vml
[c
]->vdev_top
->vdev_asize
) == 0);
5101 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
5102 vml
[c
]->vdev_top
->vdev_ashift
) == 0);
5104 /* transfer per-vdev ZAPs */
5105 ASSERT3U(vml
[c
]->vdev_leaf_zap
, !=, 0);
5106 VERIFY0(nvlist_add_uint64(child
[c
],
5107 ZPOOL_CONFIG_VDEV_LEAF_ZAP
, vml
[c
]->vdev_leaf_zap
));
5109 ASSERT3U(vml
[c
]->vdev_top
->vdev_top_zap
, !=, 0);
5110 VERIFY0(nvlist_add_uint64(child
[c
],
5111 ZPOOL_CONFIG_VDEV_TOP_ZAP
,
5112 vml
[c
]->vdev_parent
->vdev_top_zap
));
5116 kmem_free(vml
, children
* sizeof (vdev_t
*));
5117 kmem_free(glist
, children
* sizeof (uint64_t));
5118 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5121 /* stop writers from using the disks */
5122 for (c
= 0; c
< children
; c
++) {
5124 vml
[c
]->vdev_offline
= B_TRUE
;
5126 vdev_reopen(spa
->spa_root_vdev
);
5129 * Temporarily record the splitting vdevs in the spa config. This
5130 * will disappear once the config is regenerated.
5132 VERIFY(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5133 VERIFY(nvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
5134 glist
, children
) == 0);
5135 kmem_free(glist
, children
* sizeof (uint64_t));
5137 mutex_enter(&spa
->spa_props_lock
);
5138 VERIFY(nvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
,
5140 mutex_exit(&spa
->spa_props_lock
);
5141 spa
->spa_config_splitting
= nvl
;
5142 vdev_config_dirty(spa
->spa_root_vdev
);
5144 /* configure and create the new pool */
5145 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
) == 0);
5146 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
5147 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
) == 0);
5148 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
5149 spa_version(spa
)) == 0);
5150 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
5151 spa
->spa_config_txg
) == 0);
5152 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
5153 spa_generate_guid(NULL
)) == 0);
5154 VERIFY0(nvlist_add_boolean(config
, ZPOOL_CONFIG_HAS_PER_VDEV_ZAPS
));
5155 (void) nvlist_lookup_string(props
,
5156 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5158 /* add the new pool to the namespace */
5159 newspa
= spa_add(newname
, config
, altroot
);
5160 newspa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
5161 newspa
->spa_config_txg
= spa
->spa_config_txg
;
5162 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
5164 /* release the spa config lock, retaining the namespace lock */
5165 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5167 if (zio_injection_enabled
)
5168 zio_handle_panic_injection(spa
, FTAG
, 1);
5170 spa_activate(newspa
, spa_mode_global
);
5171 spa_async_suspend(newspa
);
5173 /* create the new pool from the disks of the original pool */
5174 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
, B_TRUE
);
5178 /* if that worked, generate a real config for the new pool */
5179 if (newspa
->spa_root_vdev
!= NULL
) {
5180 VERIFY(nvlist_alloc(&newspa
->spa_config_splitting
,
5181 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5182 VERIFY(nvlist_add_uint64(newspa
->spa_config_splitting
,
5183 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
)) == 0);
5184 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
5189 if (props
!= NULL
) {
5190 spa_configfile_set(newspa
, props
, B_FALSE
);
5191 error
= spa_prop_set(newspa
, props
);
5196 /* flush everything */
5197 txg
= spa_vdev_config_enter(newspa
);
5198 vdev_config_dirty(newspa
->spa_root_vdev
);
5199 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
5201 if (zio_injection_enabled
)
5202 zio_handle_panic_injection(spa
, FTAG
, 2);
5204 spa_async_resume(newspa
);
5206 /* finally, update the original pool's config */
5207 txg
= spa_vdev_config_enter(spa
);
5208 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
5209 error
= dmu_tx_assign(tx
, TXG_WAIT
);
5212 for (c
= 0; c
< children
; c
++) {
5213 if (vml
[c
] != NULL
) {
5216 spa_history_log_internal(spa
, "detach", tx
,
5217 "vdev=%s", vml
[c
]->vdev_path
);
5222 spa
->spa_avz_action
= AVZ_ACTION_REBUILD
;
5223 vdev_config_dirty(spa
->spa_root_vdev
);
5224 spa
->spa_config_splitting
= NULL
;
5228 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
5230 if (zio_injection_enabled
)
5231 zio_handle_panic_injection(spa
, FTAG
, 3);
5233 /* split is complete; log a history record */
5234 spa_history_log_internal(newspa
, "split", NULL
,
5235 "from pool %s", spa_name(spa
));
5237 kmem_free(vml
, children
* sizeof (vdev_t
*));
5239 /* if we're not going to mount the filesystems in userland, export */
5241 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
5248 spa_deactivate(newspa
);
5251 txg
= spa_vdev_config_enter(spa
);
5253 /* re-online all offlined disks */
5254 for (c
= 0; c
< children
; c
++) {
5256 vml
[c
]->vdev_offline
= B_FALSE
;
5258 vdev_reopen(spa
->spa_root_vdev
);
5260 nvlist_free(spa
->spa_config_splitting
);
5261 spa
->spa_config_splitting
= NULL
;
5262 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
5264 kmem_free(vml
, children
* sizeof (vdev_t
*));
5269 spa_nvlist_lookup_by_guid(nvlist_t
**nvpp
, int count
, uint64_t target_guid
)
5273 for (i
= 0; i
< count
; i
++) {
5276 VERIFY(nvlist_lookup_uint64(nvpp
[i
], ZPOOL_CONFIG_GUID
,
5279 if (guid
== target_guid
)
5287 spa_vdev_remove_aux(nvlist_t
*config
, char *name
, nvlist_t
**dev
, int count
,
5288 nvlist_t
*dev_to_remove
)
5290 nvlist_t
**newdev
= NULL
;
5294 newdev
= kmem_alloc((count
- 1) * sizeof (void *), KM_SLEEP
);
5296 for (i
= 0, j
= 0; i
< count
; i
++) {
5297 if (dev
[i
] == dev_to_remove
)
5299 VERIFY(nvlist_dup(dev
[i
], &newdev
[j
++], KM_SLEEP
) == 0);
5302 VERIFY(nvlist_remove(config
, name
, DATA_TYPE_NVLIST_ARRAY
) == 0);
5303 VERIFY(nvlist_add_nvlist_array(config
, name
, newdev
, count
- 1) == 0);
5305 for (i
= 0; i
< count
- 1; i
++)
5306 nvlist_free(newdev
[i
]);
5309 kmem_free(newdev
, (count
- 1) * sizeof (void *));
5313 * Evacuate the device.
5316 spa_vdev_remove_evacuate(spa_t
*spa
, vdev_t
*vd
)
5321 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5322 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5323 ASSERT(vd
== vd
->vdev_top
);
5326 * Evacuate the device. We don't hold the config lock as writer
5327 * since we need to do I/O but we do keep the
5328 * spa_namespace_lock held. Once this completes the device
5329 * should no longer have any blocks allocated on it.
5331 if (vd
->vdev_islog
) {
5332 if (vd
->vdev_stat
.vs_alloc
!= 0)
5333 error
= spa_offline_log(spa
);
5335 error
= SET_ERROR(ENOTSUP
);
5342 * The evacuation succeeded. Remove any remaining MOS metadata
5343 * associated with this vdev, and wait for these changes to sync.
5345 ASSERT0(vd
->vdev_stat
.vs_alloc
);
5346 txg
= spa_vdev_config_enter(spa
);
5347 vd
->vdev_removing
= B_TRUE
;
5348 vdev_dirty_leaves(vd
, VDD_DTL
, txg
);
5349 vdev_config_dirty(vd
);
5350 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5356 * Complete the removal by cleaning up the namespace.
5359 spa_vdev_remove_from_namespace(spa_t
*spa
, vdev_t
*vd
)
5361 vdev_t
*rvd
= spa
->spa_root_vdev
;
5362 uint64_t id
= vd
->vdev_id
;
5363 boolean_t last_vdev
= (id
== (rvd
->vdev_children
- 1));
5365 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5366 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5367 ASSERT(vd
== vd
->vdev_top
);
5370 * Only remove any devices which are empty.
5372 if (vd
->vdev_stat
.vs_alloc
!= 0)
5375 (void) vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
5377 if (list_link_active(&vd
->vdev_state_dirty_node
))
5378 vdev_state_clean(vd
);
5379 if (list_link_active(&vd
->vdev_config_dirty_node
))
5380 vdev_config_clean(vd
);
5385 vdev_compact_children(rvd
);
5387 vd
= vdev_alloc_common(spa
, id
, 0, &vdev_hole_ops
);
5388 vdev_add_child(rvd
, vd
);
5390 vdev_config_dirty(rvd
);
5393 * Reassess the health of our root vdev.
5399 * Remove a device from the pool -
5401 * Removing a device from the vdev namespace requires several steps
5402 * and can take a significant amount of time. As a result we use
5403 * the spa_vdev_config_[enter/exit] functions which allow us to
5404 * grab and release the spa_config_lock while still holding the namespace
5405 * lock. During each step the configuration is synced out.
5407 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5411 spa_vdev_remove(spa_t
*spa
, uint64_t guid
, boolean_t unspare
)
5414 metaslab_group_t
*mg
;
5415 nvlist_t
**spares
, **l2cache
, *nv
;
5417 uint_t nspares
, nl2cache
;
5419 boolean_t locked
= MUTEX_HELD(&spa_namespace_lock
);
5421 ASSERT(spa_writeable(spa
));
5424 txg
= spa_vdev_enter(spa
);
5426 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
5428 if (spa
->spa_spares
.sav_vdevs
!= NULL
&&
5429 nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
5430 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0 &&
5431 (nv
= spa_nvlist_lookup_by_guid(spares
, nspares
, guid
)) != NULL
) {
5433 * Only remove the hot spare if it's not currently in use
5436 if (vd
== NULL
|| unspare
) {
5437 spa_vdev_remove_aux(spa
->spa_spares
.sav_config
,
5438 ZPOOL_CONFIG_SPARES
, spares
, nspares
, nv
);
5439 spa_load_spares(spa
);
5440 spa
->spa_spares
.sav_sync
= B_TRUE
;
5442 error
= SET_ERROR(EBUSY
);
5444 spa_event_notify(spa
, vd
, ESC_ZFS_VDEV_REMOVE_AUX
);
5445 } else if (spa
->spa_l2cache
.sav_vdevs
!= NULL
&&
5446 nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
5447 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0 &&
5448 (nv
= spa_nvlist_lookup_by_guid(l2cache
, nl2cache
, guid
)) != NULL
) {
5450 * Cache devices can always be removed.
5452 spa_vdev_remove_aux(spa
->spa_l2cache
.sav_config
,
5453 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
, nv
);
5454 spa_load_l2cache(spa
);
5455 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5456 spa_event_notify(spa
, vd
, ESC_ZFS_VDEV_REMOVE_AUX
);
5457 } else if (vd
!= NULL
&& vd
->vdev_islog
) {
5459 ASSERT(vd
== vd
->vdev_top
);
5464 * Stop allocating from this vdev.
5466 metaslab_group_passivate(mg
);
5469 * Wait for the youngest allocations and frees to sync,
5470 * and then wait for the deferral of those frees to finish.
5472 spa_vdev_config_exit(spa
, NULL
,
5473 txg
+ TXG_CONCURRENT_STATES
+ TXG_DEFER_SIZE
, 0, FTAG
);
5476 * Attempt to evacuate the vdev.
5478 error
= spa_vdev_remove_evacuate(spa
, vd
);
5480 txg
= spa_vdev_config_enter(spa
);
5483 * If we couldn't evacuate the vdev, unwind.
5486 metaslab_group_activate(mg
);
5487 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5491 * Clean up the vdev namespace.
5493 spa_vdev_remove_from_namespace(spa
, vd
);
5495 spa_event_notify(spa
, vd
, ESC_ZFS_VDEV_REMOVE_DEV
);
5496 } else if (vd
!= NULL
) {
5498 * Normal vdevs cannot be removed (yet).
5500 error
= SET_ERROR(ENOTSUP
);
5503 * There is no vdev of any kind with the specified guid.
5505 error
= SET_ERROR(ENOENT
);
5509 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5515 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5516 * currently spared, so we can detach it.
5519 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
5521 vdev_t
*newvd
, *oldvd
;
5524 for (c
= 0; c
< vd
->vdev_children
; c
++) {
5525 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
5531 * Check for a completed replacement. We always consider the first
5532 * vdev in the list to be the oldest vdev, and the last one to be
5533 * the newest (see spa_vdev_attach() for how that works). In
5534 * the case where the newest vdev is faulted, we will not automatically
5535 * remove it after a resilver completes. This is OK as it will require
5536 * user intervention to determine which disk the admin wishes to keep.
5538 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
5539 ASSERT(vd
->vdev_children
> 1);
5541 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
5542 oldvd
= vd
->vdev_child
[0];
5544 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5545 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5546 !vdev_dtl_required(oldvd
))
5551 * Check for a completed resilver with the 'unspare' flag set.
5553 if (vd
->vdev_ops
== &vdev_spare_ops
) {
5554 vdev_t
*first
= vd
->vdev_child
[0];
5555 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
5557 if (last
->vdev_unspare
) {
5560 } else if (first
->vdev_unspare
) {
5567 if (oldvd
!= NULL
&&
5568 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5569 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5570 !vdev_dtl_required(oldvd
))
5574 * If there are more than two spares attached to a disk,
5575 * and those spares are not required, then we want to
5576 * attempt to free them up now so that they can be used
5577 * by other pools. Once we're back down to a single
5578 * disk+spare, we stop removing them.
5580 if (vd
->vdev_children
> 2) {
5581 newvd
= vd
->vdev_child
[1];
5583 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
5584 vdev_dtl_empty(last
, DTL_MISSING
) &&
5585 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
5586 !vdev_dtl_required(newvd
))
5595 spa_vdev_resilver_done(spa_t
*spa
)
5597 vdev_t
*vd
, *pvd
, *ppvd
;
5598 uint64_t guid
, sguid
, pguid
, ppguid
;
5600 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5602 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
5603 pvd
= vd
->vdev_parent
;
5604 ppvd
= pvd
->vdev_parent
;
5605 guid
= vd
->vdev_guid
;
5606 pguid
= pvd
->vdev_guid
;
5607 ppguid
= ppvd
->vdev_guid
;
5610 * If we have just finished replacing a hot spared device, then
5611 * we need to detach the parent's first child (the original hot
5614 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
5615 ppvd
->vdev_children
== 2) {
5616 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
5617 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
5619 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
5621 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5622 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
5624 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
5626 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5629 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5633 * Update the stored path or FRU for this vdev.
5636 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
5640 boolean_t sync
= B_FALSE
;
5642 ASSERT(spa_writeable(spa
));
5644 spa_vdev_state_enter(spa
, SCL_ALL
);
5646 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
5647 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
5649 if (!vd
->vdev_ops
->vdev_op_leaf
)
5650 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
5653 if (strcmp(value
, vd
->vdev_path
) != 0) {
5654 spa_strfree(vd
->vdev_path
);
5655 vd
->vdev_path
= spa_strdup(value
);
5659 if (vd
->vdev_fru
== NULL
) {
5660 vd
->vdev_fru
= spa_strdup(value
);
5662 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
5663 spa_strfree(vd
->vdev_fru
);
5664 vd
->vdev_fru
= spa_strdup(value
);
5669 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
5673 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
5675 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
5679 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
5681 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
5685 * ==========================================================================
5687 * ==========================================================================
5691 spa_scan_stop(spa_t
*spa
)
5693 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5694 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
5695 return (SET_ERROR(EBUSY
));
5696 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
5700 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
5702 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5704 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
5705 return (SET_ERROR(ENOTSUP
));
5708 * If a resilver was requested, but there is no DTL on a
5709 * writeable leaf device, we have nothing to do.
5711 if (func
== POOL_SCAN_RESILVER
&&
5712 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
5713 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
5717 return (dsl_scan(spa
->spa_dsl_pool
, func
));
5721 * ==========================================================================
5722 * SPA async task processing
5723 * ==========================================================================
5727 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
5731 if (vd
->vdev_remove_wanted
) {
5732 vd
->vdev_remove_wanted
= B_FALSE
;
5733 vd
->vdev_delayed_close
= B_FALSE
;
5734 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
5737 * We want to clear the stats, but we don't want to do a full
5738 * vdev_clear() as that will cause us to throw away
5739 * degraded/faulted state as well as attempt to reopen the
5740 * device, all of which is a waste.
5742 vd
->vdev_stat
.vs_read_errors
= 0;
5743 vd
->vdev_stat
.vs_write_errors
= 0;
5744 vd
->vdev_stat
.vs_checksum_errors
= 0;
5746 vdev_state_dirty(vd
->vdev_top
);
5749 for (c
= 0; c
< vd
->vdev_children
; c
++)
5750 spa_async_remove(spa
, vd
->vdev_child
[c
]);
5754 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
5758 if (vd
->vdev_probe_wanted
) {
5759 vd
->vdev_probe_wanted
= B_FALSE
;
5760 vdev_reopen(vd
); /* vdev_open() does the actual probe */
5763 for (c
= 0; c
< vd
->vdev_children
; c
++)
5764 spa_async_probe(spa
, vd
->vdev_child
[c
]);
5768 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
5772 if (!spa
->spa_autoexpand
)
5775 for (c
= 0; c
< vd
->vdev_children
; c
++) {
5776 vdev_t
*cvd
= vd
->vdev_child
[c
];
5777 spa_async_autoexpand(spa
, cvd
);
5780 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
5783 spa_event_notify(vd
->vdev_spa
, vd
, ESC_ZFS_VDEV_AUTOEXPAND
);
5787 spa_async_thread(spa_t
*spa
)
5791 ASSERT(spa
->spa_sync_on
);
5793 mutex_enter(&spa
->spa_async_lock
);
5794 tasks
= spa
->spa_async_tasks
;
5795 spa
->spa_async_tasks
= 0;
5796 mutex_exit(&spa
->spa_async_lock
);
5799 * See if the config needs to be updated.
5801 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
5802 uint64_t old_space
, new_space
;
5804 mutex_enter(&spa_namespace_lock
);
5805 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
5806 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5807 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
5808 mutex_exit(&spa_namespace_lock
);
5811 * If the pool grew as a result of the config update,
5812 * then log an internal history event.
5814 if (new_space
!= old_space
) {
5815 spa_history_log_internal(spa
, "vdev online", NULL
,
5816 "pool '%s' size: %llu(+%llu)",
5817 spa_name(spa
), new_space
, new_space
- old_space
);
5822 * See if any devices need to be marked REMOVED.
5824 if (tasks
& SPA_ASYNC_REMOVE
) {
5825 spa_vdev_state_enter(spa
, SCL_NONE
);
5826 spa_async_remove(spa
, spa
->spa_root_vdev
);
5827 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
5828 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
5829 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
5830 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
5831 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5834 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
5835 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5836 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
5837 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5841 * See if any devices need to be probed.
5843 if (tasks
& SPA_ASYNC_PROBE
) {
5844 spa_vdev_state_enter(spa
, SCL_NONE
);
5845 spa_async_probe(spa
, spa
->spa_root_vdev
);
5846 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5850 * If any devices are done replacing, detach them.
5852 if (tasks
& SPA_ASYNC_RESILVER_DONE
)
5853 spa_vdev_resilver_done(spa
);
5856 * Kick off a resilver.
5858 if (tasks
& SPA_ASYNC_RESILVER
)
5859 dsl_resilver_restart(spa
->spa_dsl_pool
, 0);
5862 * Let the world know that we're done.
5864 mutex_enter(&spa
->spa_async_lock
);
5865 spa
->spa_async_thread
= NULL
;
5866 cv_broadcast(&spa
->spa_async_cv
);
5867 mutex_exit(&spa
->spa_async_lock
);
5872 spa_async_suspend(spa_t
*spa
)
5874 mutex_enter(&spa
->spa_async_lock
);
5875 spa
->spa_async_suspended
++;
5876 while (spa
->spa_async_thread
!= NULL
)
5877 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
5878 mutex_exit(&spa
->spa_async_lock
);
5882 spa_async_resume(spa_t
*spa
)
5884 mutex_enter(&spa
->spa_async_lock
);
5885 ASSERT(spa
->spa_async_suspended
!= 0);
5886 spa
->spa_async_suspended
--;
5887 mutex_exit(&spa
->spa_async_lock
);
5891 spa_async_tasks_pending(spa_t
*spa
)
5893 uint_t non_config_tasks
;
5895 boolean_t config_task_suspended
;
5897 non_config_tasks
= spa
->spa_async_tasks
& ~SPA_ASYNC_CONFIG_UPDATE
;
5898 config_task
= spa
->spa_async_tasks
& SPA_ASYNC_CONFIG_UPDATE
;
5899 if (spa
->spa_ccw_fail_time
== 0) {
5900 config_task_suspended
= B_FALSE
;
5902 config_task_suspended
=
5903 (gethrtime() - spa
->spa_ccw_fail_time
) <
5904 (zfs_ccw_retry_interval
* NANOSEC
);
5907 return (non_config_tasks
|| (config_task
&& !config_task_suspended
));
5911 spa_async_dispatch(spa_t
*spa
)
5913 mutex_enter(&spa
->spa_async_lock
);
5914 if (spa_async_tasks_pending(spa
) &&
5915 !spa
->spa_async_suspended
&&
5916 spa
->spa_async_thread
== NULL
&&
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 * Rebuild spa's all-vdev ZAP from the vdev ZAPs indicated in each vdev_t.
6061 * The all-vdev ZAP must be empty.
6064 spa_avz_build(vdev_t
*vd
, uint64_t avz
, dmu_tx_t
*tx
)
6066 spa_t
*spa
= vd
->vdev_spa
;
6069 if (vd
->vdev_top_zap
!= 0) {
6070 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
6071 vd
->vdev_top_zap
, tx
));
6073 if (vd
->vdev_leaf_zap
!= 0) {
6074 VERIFY0(zap_add_int(spa
->spa_meta_objset
, avz
,
6075 vd
->vdev_leaf_zap
, tx
));
6077 for (i
= 0; i
< vd
->vdev_children
; i
++) {
6078 spa_avz_build(vd
->vdev_child
[i
], avz
, tx
);
6083 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
6088 * If the pool is being imported from a pre-per-vdev-ZAP version of ZFS,
6089 * its config may not be dirty but we still need to build per-vdev ZAPs.
6090 * Similarly, if the pool is being assembled (e.g. after a split), we
6091 * need to rebuild the AVZ although the config may not be dirty.
6093 if (list_is_empty(&spa
->spa_config_dirty_list
) &&
6094 spa
->spa_avz_action
== AVZ_ACTION_NONE
)
6097 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6099 ASSERT(spa
->spa_avz_action
== AVZ_ACTION_NONE
||
6100 spa
->spa_all_vdev_zaps
!= 0);
6102 if (spa
->spa_avz_action
== AVZ_ACTION_REBUILD
) {
6106 /* Make and build the new AVZ */
6107 uint64_t new_avz
= zap_create(spa
->spa_meta_objset
,
6108 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
6109 spa_avz_build(spa
->spa_root_vdev
, new_avz
, tx
);
6111 /* Diff old AVZ with new one */
6112 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
6113 spa
->spa_all_vdev_zaps
);
6114 zap_cursor_retrieve(&zc
, &za
) == 0;
6115 zap_cursor_advance(&zc
)) {
6116 uint64_t vdzap
= za
.za_first_integer
;
6117 if (zap_lookup_int(spa
->spa_meta_objset
, new_avz
,
6120 * ZAP is listed in old AVZ but not in new one;
6123 VERIFY0(zap_destroy(spa
->spa_meta_objset
, vdzap
,
6128 zap_cursor_fini(&zc
);
6130 /* Destroy the old AVZ */
6131 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
6132 spa
->spa_all_vdev_zaps
, tx
));
6134 /* Replace the old AVZ in the dir obj with the new one */
6135 VERIFY0(zap_update(spa
->spa_meta_objset
,
6136 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
,
6137 sizeof (new_avz
), 1, &new_avz
, tx
));
6139 spa
->spa_all_vdev_zaps
= new_avz
;
6140 } else if (spa
->spa_avz_action
== AVZ_ACTION_DESTROY
) {
6144 /* Walk through the AVZ and destroy all listed ZAPs */
6145 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
6146 spa
->spa_all_vdev_zaps
);
6147 zap_cursor_retrieve(&zc
, &za
) == 0;
6148 zap_cursor_advance(&zc
)) {
6149 uint64_t zap
= za
.za_first_integer
;
6150 VERIFY0(zap_destroy(spa
->spa_meta_objset
, zap
, tx
));
6153 zap_cursor_fini(&zc
);
6155 /* Destroy and unlink the AVZ itself */
6156 VERIFY0(zap_destroy(spa
->spa_meta_objset
,
6157 spa
->spa_all_vdev_zaps
, tx
));
6158 VERIFY0(zap_remove(spa
->spa_meta_objset
,
6159 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_VDEV_ZAP_MAP
, tx
));
6160 spa
->spa_all_vdev_zaps
= 0;
6163 if (spa
->spa_all_vdev_zaps
== 0) {
6164 spa
->spa_all_vdev_zaps
= zap_create_link(spa
->spa_meta_objset
,
6165 DMU_OTN_ZAP_METADATA
, DMU_POOL_DIRECTORY_OBJECT
,
6166 DMU_POOL_VDEV_ZAP_MAP
, tx
);
6168 spa
->spa_avz_action
= AVZ_ACTION_NONE
;
6170 /* Create ZAPs for vdevs that don't have them. */
6171 vdev_construct_zaps(spa
->spa_root_vdev
, tx
);
6173 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
6174 dmu_tx_get_txg(tx
), B_FALSE
);
6177 * If we're upgrading the spa version then make sure that
6178 * the config object gets updated with the correct version.
6180 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
6181 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
6182 spa
->spa_uberblock
.ub_version
);
6184 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6186 nvlist_free(spa
->spa_config_syncing
);
6187 spa
->spa_config_syncing
= config
;
6189 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
6193 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
6195 uint64_t *versionp
= arg
;
6196 uint64_t version
= *versionp
;
6197 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6200 * Setting the version is special cased when first creating the pool.
6202 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
6204 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
6205 ASSERT(version
>= spa_version(spa
));
6207 spa
->spa_uberblock
.ub_version
= version
;
6208 vdev_config_dirty(spa
->spa_root_vdev
);
6209 spa_history_log_internal(spa
, "set", tx
, "version=%lld", version
);
6213 * Set zpool properties.
6216 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
6218 nvlist_t
*nvp
= arg
;
6219 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6220 objset_t
*mos
= spa
->spa_meta_objset
;
6221 nvpair_t
*elem
= NULL
;
6223 mutex_enter(&spa
->spa_props_lock
);
6225 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
6227 char *strval
, *fname
;
6229 const char *propname
;
6230 zprop_type_t proptype
;
6233 prop
= zpool_name_to_prop(nvpair_name(elem
));
6234 switch ((int)prop
) {
6237 * We checked this earlier in spa_prop_validate().
6239 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
6241 fname
= strchr(nvpair_name(elem
), '@') + 1;
6242 VERIFY0(zfeature_lookup_name(fname
, &fid
));
6244 spa_feature_enable(spa
, fid
, tx
);
6245 spa_history_log_internal(spa
, "set", tx
,
6246 "%s=enabled", nvpair_name(elem
));
6249 case ZPOOL_PROP_VERSION
:
6250 intval
= fnvpair_value_uint64(elem
);
6252 * The version is synced seperatly before other
6253 * properties and should be correct by now.
6255 ASSERT3U(spa_version(spa
), >=, intval
);
6258 case ZPOOL_PROP_ALTROOT
:
6260 * 'altroot' is a non-persistent property. It should
6261 * have been set temporarily at creation or import time.
6263 ASSERT(spa
->spa_root
!= NULL
);
6266 case ZPOOL_PROP_READONLY
:
6267 case ZPOOL_PROP_CACHEFILE
:
6269 * 'readonly' and 'cachefile' are also non-persisitent
6273 case ZPOOL_PROP_COMMENT
:
6274 strval
= fnvpair_value_string(elem
);
6275 if (spa
->spa_comment
!= NULL
)
6276 spa_strfree(spa
->spa_comment
);
6277 spa
->spa_comment
= spa_strdup(strval
);
6279 * We need to dirty the configuration on all the vdevs
6280 * so that their labels get updated. It's unnecessary
6281 * to do this for pool creation since the vdev's
6282 * configuratoin has already been dirtied.
6284 if (tx
->tx_txg
!= TXG_INITIAL
)
6285 vdev_config_dirty(spa
->spa_root_vdev
);
6286 spa_history_log_internal(spa
, "set", tx
,
6287 "%s=%s", nvpair_name(elem
), strval
);
6291 * Set pool property values in the poolprops mos object.
6293 if (spa
->spa_pool_props_object
== 0) {
6294 spa
->spa_pool_props_object
=
6295 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
6296 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
6300 /* normalize the property name */
6301 propname
= zpool_prop_to_name(prop
);
6302 proptype
= zpool_prop_get_type(prop
);
6304 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
6305 ASSERT(proptype
== PROP_TYPE_STRING
);
6306 strval
= fnvpair_value_string(elem
);
6307 VERIFY0(zap_update(mos
,
6308 spa
->spa_pool_props_object
, propname
,
6309 1, strlen(strval
) + 1, strval
, tx
));
6310 spa_history_log_internal(spa
, "set", tx
,
6311 "%s=%s", nvpair_name(elem
), strval
);
6312 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
6313 intval
= fnvpair_value_uint64(elem
);
6315 if (proptype
== PROP_TYPE_INDEX
) {
6317 VERIFY0(zpool_prop_index_to_string(
6318 prop
, intval
, &unused
));
6320 VERIFY0(zap_update(mos
,
6321 spa
->spa_pool_props_object
, propname
,
6322 8, 1, &intval
, tx
));
6323 spa_history_log_internal(spa
, "set", tx
,
6324 "%s=%lld", nvpair_name(elem
), intval
);
6326 ASSERT(0); /* not allowed */
6330 case ZPOOL_PROP_DELEGATION
:
6331 spa
->spa_delegation
= intval
;
6333 case ZPOOL_PROP_BOOTFS
:
6334 spa
->spa_bootfs
= intval
;
6336 case ZPOOL_PROP_FAILUREMODE
:
6337 spa
->spa_failmode
= intval
;
6339 case ZPOOL_PROP_AUTOEXPAND
:
6340 spa
->spa_autoexpand
= intval
;
6341 if (tx
->tx_txg
!= TXG_INITIAL
)
6342 spa_async_request(spa
,
6343 SPA_ASYNC_AUTOEXPAND
);
6345 case ZPOOL_PROP_DEDUPDITTO
:
6346 spa
->spa_dedup_ditto
= intval
;
6355 mutex_exit(&spa
->spa_props_lock
);
6359 * Perform one-time upgrade on-disk changes. spa_version() does not
6360 * reflect the new version this txg, so there must be no changes this
6361 * txg to anything that the upgrade code depends on after it executes.
6362 * Therefore this must be called after dsl_pool_sync() does the sync
6366 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
6368 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6370 ASSERT(spa
->spa_sync_pass
== 1);
6372 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
6374 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
6375 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
6376 dsl_pool_create_origin(dp
, tx
);
6378 /* Keeping the origin open increases spa_minref */
6379 spa
->spa_minref
+= 3;
6382 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
6383 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
6384 dsl_pool_upgrade_clones(dp
, tx
);
6387 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
6388 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
6389 dsl_pool_upgrade_dir_clones(dp
, tx
);
6391 /* Keeping the freedir open increases spa_minref */
6392 spa
->spa_minref
+= 3;
6395 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
6396 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6397 spa_feature_create_zap_objects(spa
, tx
);
6401 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6402 * when possibility to use lz4 compression for metadata was added
6403 * Old pools that have this feature enabled must be upgraded to have
6404 * this feature active
6406 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6407 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
6408 SPA_FEATURE_LZ4_COMPRESS
);
6409 boolean_t lz4_ac
= spa_feature_is_active(spa
,
6410 SPA_FEATURE_LZ4_COMPRESS
);
6412 if (lz4_en
&& !lz4_ac
)
6413 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
6415 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
6419 * Sync the specified transaction group. New blocks may be dirtied as
6420 * part of the process, so we iterate until it converges.
6423 spa_sync(spa_t
*spa
, uint64_t txg
)
6425 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6426 objset_t
*mos
= spa
->spa_meta_objset
;
6427 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
6428 vdev_t
*rvd
= spa
->spa_root_vdev
;
6434 VERIFY(spa_writeable(spa
));
6437 * Lock out configuration changes.
6439 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6441 spa
->spa_syncing_txg
= txg
;
6442 spa
->spa_sync_pass
= 0;
6445 * If there are any pending vdev state changes, convert them
6446 * into config changes that go out with this transaction group.
6448 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6449 while (list_head(&spa
->spa_state_dirty_list
) != NULL
) {
6451 * We need the write lock here because, for aux vdevs,
6452 * calling vdev_config_dirty() modifies sav_config.
6453 * This is ugly and will become unnecessary when we
6454 * eliminate the aux vdev wart by integrating all vdevs
6455 * into the root vdev tree.
6457 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6458 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
6459 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
6460 vdev_state_clean(vd
);
6461 vdev_config_dirty(vd
);
6463 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6464 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
6466 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6468 tx
= dmu_tx_create_assigned(dp
, txg
);
6470 spa
->spa_sync_starttime
= gethrtime();
6471 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
6472 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_taskq
,
6473 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
6474 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
6477 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6478 * set spa_deflate if we have no raid-z vdevs.
6480 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
6481 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
6484 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
6485 vd
= rvd
->vdev_child
[i
];
6486 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
6489 if (i
== rvd
->vdev_children
) {
6490 spa
->spa_deflate
= TRUE
;
6491 VERIFY(0 == zap_add(spa
->spa_meta_objset
,
6492 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
6493 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
6498 * Iterate to convergence.
6501 int pass
= ++spa
->spa_sync_pass
;
6503 spa_sync_config_object(spa
, tx
);
6504 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
6505 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
6506 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
6507 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
6508 spa_errlog_sync(spa
, txg
);
6509 dsl_pool_sync(dp
, txg
);
6511 if (pass
< zfs_sync_pass_deferred_free
) {
6512 spa_sync_frees(spa
, free_bpl
, tx
);
6515 * We can not defer frees in pass 1, because
6516 * we sync the deferred frees later in pass 1.
6518 ASSERT3U(pass
, >, 1);
6519 bplist_iterate(free_bpl
, bpobj_enqueue_cb
,
6520 &spa
->spa_deferred_bpobj
, tx
);
6524 dsl_scan_sync(dp
, tx
);
6526 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
)))
6530 spa_sync_upgrades(spa
, tx
);
6532 spa
->spa_uberblock
.ub_rootbp
.blk_birth
);
6534 * Note: We need to check if the MOS is dirty
6535 * because we could have marked the MOS dirty
6536 * without updating the uberblock (e.g. if we
6537 * have sync tasks but no dirty user data). We
6538 * need to check the uberblock's rootbp because
6539 * it is updated if we have synced out dirty
6540 * data (though in this case the MOS will most
6541 * likely also be dirty due to second order
6542 * effects, we don't want to rely on that here).
6544 if (spa
->spa_uberblock
.ub_rootbp
.blk_birth
< txg
&&
6545 !dmu_objset_is_dirty(mos
, txg
)) {
6547 * Nothing changed on the first pass,
6548 * therefore this TXG is a no-op. Avoid
6549 * syncing deferred frees, so that we
6550 * can keep this TXG as a no-op.
6552 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
,
6554 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
6555 ASSERT(txg_list_empty(&dp
->dp_sync_tasks
, txg
));
6558 spa_sync_deferred_frees(spa
, tx
);
6561 } while (dmu_objset_is_dirty(mos
, txg
));
6564 if (!list_is_empty(&spa
->spa_config_dirty_list
)) {
6566 * Make sure that the number of ZAPs for all the vdevs matches
6567 * the number of ZAPs in the per-vdev ZAP list. This only gets
6568 * called if the config is dirty; otherwise there may be
6569 * outstanding AVZ operations that weren't completed in
6570 * spa_sync_config_object.
6572 uint64_t all_vdev_zap_entry_count
;
6573 ASSERT0(zap_count(spa
->spa_meta_objset
,
6574 spa
->spa_all_vdev_zaps
, &all_vdev_zap_entry_count
));
6575 ASSERT3U(vdev_count_verify_zaps(spa
->spa_root_vdev
), ==,
6576 all_vdev_zap_entry_count
);
6581 * Rewrite the vdev configuration (which includes the uberblock)
6582 * to commit the transaction group.
6584 * If there are no dirty vdevs, we sync the uberblock to a few
6585 * random top-level vdevs that are known to be visible in the
6586 * config cache (see spa_vdev_add() for a complete description).
6587 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6591 * We hold SCL_STATE to prevent vdev open/close/etc.
6592 * while we're attempting to write the vdev labels.
6594 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6596 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
6597 vdev_t
*svd
[SPA_DVAS_PER_BP
];
6599 int children
= rvd
->vdev_children
;
6600 int c0
= spa_get_random(children
);
6602 for (c
= 0; c
< children
; c
++) {
6603 vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
6604 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
)
6606 svd
[svdcount
++] = vd
;
6607 if (svdcount
== SPA_DVAS_PER_BP
)
6610 error
= vdev_config_sync(svd
, svdcount
, txg
);
6612 error
= vdev_config_sync(rvd
->vdev_child
,
6613 rvd
->vdev_children
, txg
);
6617 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
6619 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6623 zio_suspend(spa
, NULL
);
6624 zio_resume_wait(spa
);
6628 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
6629 spa
->spa_deadman_tqid
= 0;
6632 * Clear the dirty config list.
6634 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
6635 vdev_config_clean(vd
);
6638 * Now that the new config has synced transactionally,
6639 * let it become visible to the config cache.
6641 if (spa
->spa_config_syncing
!= NULL
) {
6642 spa_config_set(spa
, spa
->spa_config_syncing
);
6643 spa
->spa_config_txg
= txg
;
6644 spa
->spa_config_syncing
= NULL
;
6647 spa
->spa_ubsync
= spa
->spa_uberblock
;
6649 dsl_pool_sync_done(dp
, txg
);
6652 * Update usable space statistics.
6654 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
))))
6655 vdev_sync_done(vd
, txg
);
6657 spa_update_dspace(spa
);
6660 * It had better be the case that we didn't dirty anything
6661 * since vdev_config_sync().
6663 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
6664 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
6665 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
6667 spa
->spa_sync_pass
= 0;
6669 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6671 spa_handle_ignored_writes(spa
);
6674 * If any async tasks have been requested, kick them off.
6676 spa_async_dispatch(spa
);
6680 * Sync all pools. We don't want to hold the namespace lock across these
6681 * operations, so we take a reference on the spa_t and drop the lock during the
6685 spa_sync_allpools(void)
6688 mutex_enter(&spa_namespace_lock
);
6689 while ((spa
= spa_next(spa
)) != NULL
) {
6690 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
6691 !spa_writeable(spa
) || spa_suspended(spa
))
6693 spa_open_ref(spa
, FTAG
);
6694 mutex_exit(&spa_namespace_lock
);
6695 txg_wait_synced(spa_get_dsl(spa
), 0);
6696 mutex_enter(&spa_namespace_lock
);
6697 spa_close(spa
, FTAG
);
6699 mutex_exit(&spa_namespace_lock
);
6703 * ==========================================================================
6704 * Miscellaneous routines
6705 * ==========================================================================
6709 * Remove all pools in the system.
6717 * Remove all cached state. All pools should be closed now,
6718 * so every spa in the AVL tree should be unreferenced.
6720 mutex_enter(&spa_namespace_lock
);
6721 while ((spa
= spa_next(NULL
)) != NULL
) {
6723 * Stop async tasks. The async thread may need to detach
6724 * a device that's been replaced, which requires grabbing
6725 * spa_namespace_lock, so we must drop it here.
6727 spa_open_ref(spa
, FTAG
);
6728 mutex_exit(&spa_namespace_lock
);
6729 spa_async_suspend(spa
);
6730 mutex_enter(&spa_namespace_lock
);
6731 spa_close(spa
, FTAG
);
6733 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
6735 spa_deactivate(spa
);
6739 mutex_exit(&spa_namespace_lock
);
6743 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
6748 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
6752 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
6753 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
6754 if (vd
->vdev_guid
== guid
)
6758 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
6759 vd
= spa
->spa_spares
.sav_vdevs
[i
];
6760 if (vd
->vdev_guid
== guid
)
6769 spa_upgrade(spa_t
*spa
, uint64_t version
)
6771 ASSERT(spa_writeable(spa
));
6773 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6776 * This should only be called for a non-faulted pool, and since a
6777 * future version would result in an unopenable pool, this shouldn't be
6780 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
6781 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
6783 spa
->spa_uberblock
.ub_version
= version
;
6784 vdev_config_dirty(spa
->spa_root_vdev
);
6786 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6788 txg_wait_synced(spa_get_dsl(spa
), 0);
6792 spa_has_spare(spa_t
*spa
, uint64_t guid
)
6796 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
6798 for (i
= 0; i
< sav
->sav_count
; i
++)
6799 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
6802 for (i
= 0; i
< sav
->sav_npending
; i
++) {
6803 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
6804 &spareguid
) == 0 && spareguid
== guid
)
6812 * Check if a pool has an active shared spare device.
6813 * Note: reference count of an active spare is 2, as a spare and as a replace
6816 spa_has_active_shared_spare(spa_t
*spa
)
6820 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
6822 for (i
= 0; i
< sav
->sav_count
; i
++) {
6823 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
6824 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
6833 * Post a zevent corresponding to the given sysevent. The 'name' must be one
6834 * of the event definitions in sys/sysevent/eventdefs.h. The payload will be
6835 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6836 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6837 * or zdb as real changes.
6840 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, const char *name
)
6842 zfs_post_sysevent(spa
, vd
, name
);
6845 #if defined(_KERNEL) && defined(HAVE_SPL)
6846 /* state manipulation functions */
6847 EXPORT_SYMBOL(spa_open
);
6848 EXPORT_SYMBOL(spa_open_rewind
);
6849 EXPORT_SYMBOL(spa_get_stats
);
6850 EXPORT_SYMBOL(spa_create
);
6851 EXPORT_SYMBOL(spa_import
);
6852 EXPORT_SYMBOL(spa_tryimport
);
6853 EXPORT_SYMBOL(spa_destroy
);
6854 EXPORT_SYMBOL(spa_export
);
6855 EXPORT_SYMBOL(spa_reset
);
6856 EXPORT_SYMBOL(spa_async_request
);
6857 EXPORT_SYMBOL(spa_async_suspend
);
6858 EXPORT_SYMBOL(spa_async_resume
);
6859 EXPORT_SYMBOL(spa_inject_addref
);
6860 EXPORT_SYMBOL(spa_inject_delref
);
6861 EXPORT_SYMBOL(spa_scan_stat_init
);
6862 EXPORT_SYMBOL(spa_scan_get_stats
);
6864 /* device maniion */
6865 EXPORT_SYMBOL(spa_vdev_add
);
6866 EXPORT_SYMBOL(spa_vdev_attach
);
6867 EXPORT_SYMBOL(spa_vdev_detach
);
6868 EXPORT_SYMBOL(spa_vdev_remove
);
6869 EXPORT_SYMBOL(spa_vdev_setpath
);
6870 EXPORT_SYMBOL(spa_vdev_setfru
);
6871 EXPORT_SYMBOL(spa_vdev_split_mirror
);
6873 /* spare statech is global across all pools) */
6874 EXPORT_SYMBOL(spa_spare_add
);
6875 EXPORT_SYMBOL(spa_spare_remove
);
6876 EXPORT_SYMBOL(spa_spare_exists
);
6877 EXPORT_SYMBOL(spa_spare_activate
);
6879 /* L2ARC statech is global across all pools) */
6880 EXPORT_SYMBOL(spa_l2cache_add
);
6881 EXPORT_SYMBOL(spa_l2cache_remove
);
6882 EXPORT_SYMBOL(spa_l2cache_exists
);
6883 EXPORT_SYMBOL(spa_l2cache_activate
);
6884 EXPORT_SYMBOL(spa_l2cache_drop
);
6887 EXPORT_SYMBOL(spa_scan
);
6888 EXPORT_SYMBOL(spa_scan_stop
);
6891 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
6892 EXPORT_SYMBOL(spa_sync_allpools
);
6895 EXPORT_SYMBOL(spa_prop_set
);
6896 EXPORT_SYMBOL(spa_prop_get
);
6897 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
6899 /* asynchronous event notification */
6900 EXPORT_SYMBOL(spa_event_notify
);
6903 #if defined(_KERNEL) && defined(HAVE_SPL)
6904 module_param(spa_load_verify_maxinflight
, int, 0644);
6905 MODULE_PARM_DESC(spa_load_verify_maxinflight
,
6906 "Max concurrent traversal I/Os while verifying pool during import -X");
6908 module_param(spa_load_verify_metadata
, int, 0644);
6909 MODULE_PARM_DESC(spa_load_verify_metadata
,
6910 "Set to traverse metadata on pool import");
6912 module_param(spa_load_verify_data
, int, 0644);
6913 MODULE_PARM_DESC(spa_load_verify_data
,
6914 "Set to traverse data on pool import");
6916 module_param(zio_taskq_batch_pct
, uint
, 0444);
6917 MODULE_PARM_DESC(zio_taskq_batch_pct
,
6918 "Percentage of CPUs to run an IO worker thread");