4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013 by Delphix. All rights reserved.
25 * Copyright (c) 2013, 2014, Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
30 * SPA: Storage Pool Allocator
32 * This file contains all the routines used when modifying on-disk SPA state.
33 * This includes opening, importing, destroying, exporting a pool, and syncing a
37 #include <sys/zfs_context.h>
38 #include <sys/fm/fs/zfs.h>
39 #include <sys/spa_impl.h>
41 #include <sys/zio_checksum.h>
43 #include <sys/dmu_tx.h>
47 #include <sys/vdev_impl.h>
48 #include <sys/vdev_disk.h>
49 #include <sys/metaslab.h>
50 #include <sys/metaslab_impl.h>
51 #include <sys/uberblock_impl.h>
54 #include <sys/dmu_traverse.h>
55 #include <sys/dmu_objset.h>
56 #include <sys/unique.h>
57 #include <sys/dsl_pool.h>
58 #include <sys/dsl_dataset.h>
59 #include <sys/dsl_dir.h>
60 #include <sys/dsl_prop.h>
61 #include <sys/dsl_synctask.h>
62 #include <sys/fs/zfs.h>
64 #include <sys/callb.h>
65 #include <sys/systeminfo.h>
66 #include <sys/spa_boot.h>
67 #include <sys/zfs_ioctl.h>
68 #include <sys/dsl_scan.h>
69 #include <sys/zfeature.h>
70 #include <sys/dsl_destroy.h>
74 #include <sys/bootprops.h>
75 #include <sys/callb.h>
76 #include <sys/cpupart.h>
78 #include <sys/sysdc.h>
83 #include "zfs_comutil.h"
85 typedef enum zti_modes
{
86 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
87 ZTI_MODE_BATCH
, /* cpu-intensive; value is ignored */
88 ZTI_MODE_NULL
, /* don't create a taskq */
92 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
93 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
94 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
95 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
97 #define ZTI_N(n) ZTI_P(n, 1)
98 #define ZTI_ONE ZTI_N(1)
100 typedef struct zio_taskq_info
{
101 zti_modes_t zti_mode
;
106 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
107 "iss", "iss_h", "int", "int_h"
111 * This table defines the taskq settings for each ZFS I/O type. When
112 * initializing a pool, we use this table to create an appropriately sized
113 * taskq. Some operations are low volume and therefore have a small, static
114 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
115 * macros. Other operations process a large amount of data; the ZTI_BATCH
116 * macro causes us to create a taskq oriented for throughput. Some operations
117 * are so high frequency and short-lived that the taskq itself can become a a
118 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
119 * additional degree of parallelism specified by the number of threads per-
120 * taskq and the number of taskqs; when dispatching an event in this case, the
121 * particular taskq is chosen at random.
123 * The different taskq priorities are to handle the different contexts (issue
124 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
125 * need to be handled with minimum delay.
127 const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
128 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
129 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
130 { ZTI_N(8), ZTI_NULL
, ZTI_P(12, 8), ZTI_NULL
}, /* READ */
131 { ZTI_BATCH
, ZTI_N(5), ZTI_P(12, 8), ZTI_N(5) }, /* WRITE */
132 { ZTI_P(12, 8), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
133 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
134 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
137 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
138 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
139 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
140 static inline int spa_load_impl(spa_t
*spa
, uint64_t, nvlist_t
*config
,
141 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
143 static void spa_vdev_resilver_done(spa_t
*spa
);
145 uint_t zio_taskq_batch_pct
= 75; /* 1 thread per cpu in pset */
146 id_t zio_taskq_psrset_bind
= PS_NONE
;
147 boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
148 uint_t zio_taskq_basedc
= 80; /* base duty cycle */
150 boolean_t spa_create_process
= B_TRUE
; /* no process ==> no sysdc */
153 * This (illegal) pool name is used when temporarily importing a spa_t in order
154 * to get the vdev stats associated with the imported devices.
156 #define TRYIMPORT_NAME "$import"
159 * ==========================================================================
160 * SPA properties routines
161 * ==========================================================================
165 * Add a (source=src, propname=propval) list to an nvlist.
168 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, char *strval
,
169 uint64_t intval
, zprop_source_t src
)
171 const char *propname
= zpool_prop_to_name(prop
);
174 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
175 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
178 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
180 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, intval
) == 0);
182 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
183 nvlist_free(propval
);
187 * Get property values from the spa configuration.
190 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
192 vdev_t
*rvd
= spa
->spa_root_vdev
;
193 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
194 uint64_t size
, alloc
, cap
, version
;
195 zprop_source_t src
= ZPROP_SRC_NONE
;
196 spa_config_dirent_t
*dp
;
197 metaslab_class_t
*mc
= spa_normal_class(spa
);
199 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
202 alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
203 size
= metaslab_class_get_space(spa_normal_class(spa
));
204 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
205 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
206 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
207 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
210 spa_prop_add_list(*nvp
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
211 metaslab_class_fragmentation(mc
), src
);
212 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
,
213 metaslab_class_expandable_space(mc
), src
);
214 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
215 (spa_mode(spa
) == FREAD
), src
);
217 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
218 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
220 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
221 ddt_get_pool_dedup_ratio(spa
), src
);
223 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
224 rvd
->vdev_state
, src
);
226 version
= spa_version(spa
);
227 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
))
228 src
= ZPROP_SRC_DEFAULT
;
230 src
= ZPROP_SRC_LOCAL
;
231 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
, version
, src
);
236 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
237 * when opening pools before this version freedir will be NULL.
239 if (pool
->dp_free_dir
!= NULL
) {
240 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
241 dsl_dir_phys(pool
->dp_free_dir
)->dd_used_bytes
,
244 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
248 if (pool
->dp_leak_dir
!= NULL
) {
249 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
250 dsl_dir_phys(pool
->dp_leak_dir
)->dd_used_bytes
,
253 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
258 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
260 if (spa
->spa_comment
!= NULL
) {
261 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
265 if (spa
->spa_root
!= NULL
)
266 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
269 if (spa_feature_is_enabled(spa
, SPA_FEATURE_LARGE_BLOCKS
)) {
270 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
271 MIN(zfs_max_recordsize
, SPA_MAXBLOCKSIZE
), ZPROP_SRC_NONE
);
273 spa_prop_add_list(*nvp
, ZPOOL_PROP_MAXBLOCKSIZE
, NULL
,
274 SPA_OLD_MAXBLOCKSIZE
, ZPROP_SRC_NONE
);
277 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
278 if (dp
->scd_path
== NULL
) {
279 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
280 "none", 0, ZPROP_SRC_LOCAL
);
281 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
282 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
283 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
289 * Get zpool property values.
292 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
294 objset_t
*mos
= spa
->spa_meta_objset
;
299 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_SLEEP
);
303 mutex_enter(&spa
->spa_props_lock
);
306 * Get properties from the spa config.
308 spa_prop_get_config(spa
, nvp
);
310 /* If no pool property object, no more prop to get. */
311 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0) {
312 mutex_exit(&spa
->spa_props_lock
);
317 * Get properties from the MOS pool property object.
319 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
320 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
321 zap_cursor_advance(&zc
)) {
324 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
327 if ((prop
= zpool_name_to_prop(za
.za_name
)) == ZPROP_INVAL
)
330 switch (za
.za_integer_length
) {
332 /* integer property */
333 if (za
.za_first_integer
!=
334 zpool_prop_default_numeric(prop
))
335 src
= ZPROP_SRC_LOCAL
;
337 if (prop
== ZPOOL_PROP_BOOTFS
) {
339 dsl_dataset_t
*ds
= NULL
;
341 dp
= spa_get_dsl(spa
);
342 dsl_pool_config_enter(dp
, FTAG
);
343 if ((err
= dsl_dataset_hold_obj(dp
,
344 za
.za_first_integer
, FTAG
, &ds
))) {
345 dsl_pool_config_exit(dp
, FTAG
);
350 MAXNAMELEN
+ strlen(MOS_DIR_NAME
) + 1,
352 dsl_dataset_name(ds
, strval
);
353 dsl_dataset_rele(ds
, FTAG
);
354 dsl_pool_config_exit(dp
, FTAG
);
357 intval
= za
.za_first_integer
;
360 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
364 MAXNAMELEN
+ strlen(MOS_DIR_NAME
) + 1);
369 /* string property */
370 strval
= kmem_alloc(za
.za_num_integers
, KM_SLEEP
);
371 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
372 za
.za_name
, 1, za
.za_num_integers
, strval
);
374 kmem_free(strval
, za
.za_num_integers
);
377 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
378 kmem_free(strval
, za
.za_num_integers
);
385 zap_cursor_fini(&zc
);
386 mutex_exit(&spa
->spa_props_lock
);
388 if (err
&& err
!= ENOENT
) {
398 * Validate the given pool properties nvlist and modify the list
399 * for the property values to be set.
402 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
405 int error
= 0, reset_bootfs
= 0;
407 boolean_t has_feature
= B_FALSE
;
410 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
412 char *strval
, *slash
, *check
, *fname
;
413 const char *propname
= nvpair_name(elem
);
414 zpool_prop_t prop
= zpool_name_to_prop(propname
);
418 if (!zpool_prop_feature(propname
)) {
419 error
= SET_ERROR(EINVAL
);
424 * Sanitize the input.
426 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
427 error
= SET_ERROR(EINVAL
);
431 if (nvpair_value_uint64(elem
, &intval
) != 0) {
432 error
= SET_ERROR(EINVAL
);
437 error
= SET_ERROR(EINVAL
);
441 fname
= strchr(propname
, '@') + 1;
442 if (zfeature_lookup_name(fname
, NULL
) != 0) {
443 error
= SET_ERROR(EINVAL
);
447 has_feature
= B_TRUE
;
450 case ZPOOL_PROP_VERSION
:
451 error
= nvpair_value_uint64(elem
, &intval
);
453 (intval
< spa_version(spa
) ||
454 intval
> SPA_VERSION_BEFORE_FEATURES
||
456 error
= SET_ERROR(EINVAL
);
459 case ZPOOL_PROP_DELEGATION
:
460 case ZPOOL_PROP_AUTOREPLACE
:
461 case ZPOOL_PROP_LISTSNAPS
:
462 case ZPOOL_PROP_AUTOEXPAND
:
463 error
= nvpair_value_uint64(elem
, &intval
);
464 if (!error
&& intval
> 1)
465 error
= SET_ERROR(EINVAL
);
468 case ZPOOL_PROP_BOOTFS
:
470 * If the pool version is less than SPA_VERSION_BOOTFS,
471 * or the pool is still being created (version == 0),
472 * the bootfs property cannot be set.
474 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
475 error
= SET_ERROR(ENOTSUP
);
480 * Make sure the vdev config is bootable
482 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
483 error
= SET_ERROR(ENOTSUP
);
489 error
= nvpair_value_string(elem
, &strval
);
495 if (strval
== NULL
|| strval
[0] == '\0') {
496 objnum
= zpool_prop_default_numeric(
501 error
= dmu_objset_hold(strval
, FTAG
, &os
);
506 * Must be ZPL, and its property settings
507 * must be supported by GRUB (compression
508 * is not gzip, and large blocks are not used).
511 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
512 error
= SET_ERROR(ENOTSUP
);
514 dsl_prop_get_int_ds(dmu_objset_ds(os
),
515 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
517 !BOOTFS_COMPRESS_VALID(propval
)) {
518 error
= SET_ERROR(ENOTSUP
);
520 dsl_prop_get_int_ds(dmu_objset_ds(os
),
521 zfs_prop_to_name(ZFS_PROP_RECORDSIZE
),
523 propval
> SPA_OLD_MAXBLOCKSIZE
) {
524 error
= SET_ERROR(ENOTSUP
);
526 objnum
= dmu_objset_id(os
);
528 dmu_objset_rele(os
, FTAG
);
532 case ZPOOL_PROP_FAILUREMODE
:
533 error
= nvpair_value_uint64(elem
, &intval
);
534 if (!error
&& (intval
< ZIO_FAILURE_MODE_WAIT
||
535 intval
> ZIO_FAILURE_MODE_PANIC
))
536 error
= SET_ERROR(EINVAL
);
539 * This is a special case which only occurs when
540 * the pool has completely failed. This allows
541 * the user to change the in-core failmode property
542 * without syncing it out to disk (I/Os might
543 * currently be blocked). We do this by returning
544 * EIO to the caller (spa_prop_set) to trick it
545 * into thinking we encountered a property validation
548 if (!error
&& spa_suspended(spa
)) {
549 spa
->spa_failmode
= intval
;
550 error
= SET_ERROR(EIO
);
554 case ZPOOL_PROP_CACHEFILE
:
555 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
558 if (strval
[0] == '\0')
561 if (strcmp(strval
, "none") == 0)
564 if (strval
[0] != '/') {
565 error
= SET_ERROR(EINVAL
);
569 slash
= strrchr(strval
, '/');
570 ASSERT(slash
!= NULL
);
572 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
573 strcmp(slash
, "/..") == 0)
574 error
= SET_ERROR(EINVAL
);
577 case ZPOOL_PROP_COMMENT
:
578 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
580 for (check
= strval
; *check
!= '\0'; check
++) {
581 if (!isprint(*check
)) {
582 error
= SET_ERROR(EINVAL
);
587 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
588 error
= SET_ERROR(E2BIG
);
591 case ZPOOL_PROP_DEDUPDITTO
:
592 if (spa_version(spa
) < SPA_VERSION_DEDUP
)
593 error
= SET_ERROR(ENOTSUP
);
595 error
= nvpair_value_uint64(elem
, &intval
);
597 intval
!= 0 && intval
< ZIO_DEDUPDITTO_MIN
)
598 error
= SET_ERROR(EINVAL
);
609 if (!error
&& reset_bootfs
) {
610 error
= nvlist_remove(props
,
611 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
614 error
= nvlist_add_uint64(props
,
615 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
623 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
626 spa_config_dirent_t
*dp
;
628 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
632 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
635 if (cachefile
[0] == '\0')
636 dp
->scd_path
= spa_strdup(spa_config_path
);
637 else if (strcmp(cachefile
, "none") == 0)
640 dp
->scd_path
= spa_strdup(cachefile
);
642 list_insert_head(&spa
->spa_config_list
, dp
);
644 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
648 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
651 nvpair_t
*elem
= NULL
;
652 boolean_t need_sync
= B_FALSE
;
654 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
657 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
658 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
660 if (prop
== ZPOOL_PROP_CACHEFILE
||
661 prop
== ZPOOL_PROP_ALTROOT
||
662 prop
== ZPOOL_PROP_READONLY
)
665 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPROP_INVAL
) {
668 if (prop
== ZPOOL_PROP_VERSION
) {
669 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
671 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
672 ver
= SPA_VERSION_FEATURES
;
676 /* Save time if the version is already set. */
677 if (ver
== spa_version(spa
))
681 * In addition to the pool directory object, we might
682 * create the pool properties object, the features for
683 * read object, the features for write object, or the
684 * feature descriptions object.
686 error
= dsl_sync_task(spa
->spa_name
, NULL
,
687 spa_sync_version
, &ver
,
688 6, ZFS_SPACE_CHECK_RESERVED
);
699 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
700 nvp
, 6, ZFS_SPACE_CHECK_RESERVED
));
707 * If the bootfs property value is dsobj, clear it.
710 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
712 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
713 VERIFY(zap_remove(spa
->spa_meta_objset
,
714 spa
->spa_pool_props_object
,
715 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
722 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
724 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
725 vdev_t
*rvd
= spa
->spa_root_vdev
;
727 ASSERTV(uint64_t *newguid
= arg
);
729 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
730 vdev_state
= rvd
->vdev_state
;
731 spa_config_exit(spa
, SCL_STATE
, FTAG
);
733 if (vdev_state
!= VDEV_STATE_HEALTHY
)
734 return (SET_ERROR(ENXIO
));
736 ASSERT3U(spa_guid(spa
), !=, *newguid
);
742 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
744 uint64_t *newguid
= arg
;
745 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
747 vdev_t
*rvd
= spa
->spa_root_vdev
;
749 oldguid
= spa_guid(spa
);
751 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
752 rvd
->vdev_guid
= *newguid
;
753 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
754 vdev_config_dirty(rvd
);
755 spa_config_exit(spa
, SCL_STATE
, FTAG
);
757 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
762 * Change the GUID for the pool. This is done so that we can later
763 * re-import a pool built from a clone of our own vdevs. We will modify
764 * the root vdev's guid, our own pool guid, and then mark all of our
765 * vdevs dirty. Note that we must make sure that all our vdevs are
766 * online when we do this, or else any vdevs that weren't present
767 * would be orphaned from our pool. We are also going to issue a
768 * sysevent to update any watchers.
771 spa_change_guid(spa_t
*spa
)
776 mutex_enter(&spa
->spa_vdev_top_lock
);
777 mutex_enter(&spa_namespace_lock
);
778 guid
= spa_generate_guid(NULL
);
780 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
781 spa_change_guid_sync
, &guid
, 5, ZFS_SPACE_CHECK_RESERVED
);
784 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
785 spa_event_notify(spa
, NULL
, FM_EREPORT_ZFS_POOL_REGUID
);
788 mutex_exit(&spa_namespace_lock
);
789 mutex_exit(&spa
->spa_vdev_top_lock
);
795 * ==========================================================================
796 * SPA state manipulation (open/create/destroy/import/export)
797 * ==========================================================================
801 spa_error_entry_compare(const void *a
, const void *b
)
803 spa_error_entry_t
*sa
= (spa_error_entry_t
*)a
;
804 spa_error_entry_t
*sb
= (spa_error_entry_t
*)b
;
807 ret
= bcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
808 sizeof (zbookmark_phys_t
));
819 * Utility function which retrieves copies of the current logs and
820 * re-initializes them in the process.
823 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
825 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
827 bcopy(&spa
->spa_errlist_last
, last
, sizeof (avl_tree_t
));
828 bcopy(&spa
->spa_errlist_scrub
, scrub
, sizeof (avl_tree_t
));
830 avl_create(&spa
->spa_errlist_scrub
,
831 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
832 offsetof(spa_error_entry_t
, se_avl
));
833 avl_create(&spa
->spa_errlist_last
,
834 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
835 offsetof(spa_error_entry_t
, se_avl
));
839 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
841 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
842 enum zti_modes mode
= ztip
->zti_mode
;
843 uint_t value
= ztip
->zti_value
;
844 uint_t count
= ztip
->zti_count
;
845 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
847 uint_t i
, flags
= TASKQ_DYNAMIC
;
848 boolean_t batch
= B_FALSE
;
850 if (mode
== ZTI_MODE_NULL
) {
852 tqs
->stqs_taskq
= NULL
;
856 ASSERT3U(count
, >, 0);
858 tqs
->stqs_count
= count
;
859 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
863 ASSERT3U(value
, >=, 1);
864 value
= MAX(value
, 1);
869 flags
|= TASKQ_THREADS_CPU_PCT
;
870 value
= zio_taskq_batch_pct
;
874 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
876 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
880 for (i
= 0; i
< count
; i
++) {
884 (void) snprintf(name
, sizeof (name
), "%s_%s_%u",
885 zio_type_name
[t
], zio_taskq_types
[q
], i
);
887 (void) snprintf(name
, sizeof (name
), "%s_%s",
888 zio_type_name
[t
], zio_taskq_types
[q
]);
891 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
893 flags
|= TASKQ_DC_BATCH
;
895 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
896 spa
->spa_proc
, zio_taskq_basedc
, flags
);
898 pri_t pri
= maxclsyspri
;
900 * The write issue taskq can be extremely CPU
901 * intensive. Run it at slightly lower priority
902 * than the other taskqs.
904 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
)
907 tq
= taskq_create_proc(name
, value
, pri
, 50,
908 INT_MAX
, spa
->spa_proc
, flags
);
911 tqs
->stqs_taskq
[i
] = tq
;
916 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
918 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
921 if (tqs
->stqs_taskq
== NULL
) {
922 ASSERT3U(tqs
->stqs_count
, ==, 0);
926 for (i
= 0; i
< tqs
->stqs_count
; i
++) {
927 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
928 taskq_destroy(tqs
->stqs_taskq
[i
]);
931 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
932 tqs
->stqs_taskq
= NULL
;
936 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
937 * Note that a type may have multiple discrete taskqs to avoid lock contention
938 * on the taskq itself. In that case we choose which taskq at random by using
939 * the low bits of gethrtime().
942 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
943 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
945 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
948 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
949 ASSERT3U(tqs
->stqs_count
, !=, 0);
951 if (tqs
->stqs_count
== 1) {
952 tq
= tqs
->stqs_taskq
[0];
954 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
957 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
961 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
964 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
965 task_func_t
*func
, void *arg
, uint_t flags
)
967 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
971 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
972 ASSERT3U(tqs
->stqs_count
, !=, 0);
974 if (tqs
->stqs_count
== 1) {
975 tq
= tqs
->stqs_taskq
[0];
977 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
980 id
= taskq_dispatch(tq
, func
, arg
, flags
);
982 taskq_wait_id(tq
, id
);
986 spa_create_zio_taskqs(spa_t
*spa
)
990 for (t
= 0; t
< ZIO_TYPES
; t
++) {
991 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
992 spa_taskqs_init(spa
, t
, q
);
997 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
999 spa_thread(void *arg
)
1001 callb_cpr_t cprinfo
;
1004 user_t
*pu
= PTOU(curproc
);
1006 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
1009 ASSERT(curproc
!= &p0
);
1010 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
1011 "zpool-%s", spa
->spa_name
);
1012 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
1014 /* bind this thread to the requested psrset */
1015 if (zio_taskq_psrset_bind
!= PS_NONE
) {
1017 mutex_enter(&cpu_lock
);
1018 mutex_enter(&pidlock
);
1019 mutex_enter(&curproc
->p_lock
);
1021 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1022 0, NULL
, NULL
) == 0) {
1023 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1026 "Couldn't bind process for zfs pool \"%s\" to "
1027 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1030 mutex_exit(&curproc
->p_lock
);
1031 mutex_exit(&pidlock
);
1032 mutex_exit(&cpu_lock
);
1036 if (zio_taskq_sysdc
) {
1037 sysdc_thread_enter(curthread
, 100, 0);
1040 spa
->spa_proc
= curproc
;
1041 spa
->spa_did
= curthread
->t_did
;
1043 spa_create_zio_taskqs(spa
);
1045 mutex_enter(&spa
->spa_proc_lock
);
1046 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1048 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1049 cv_broadcast(&spa
->spa_proc_cv
);
1051 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1052 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1053 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1054 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1056 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1057 spa
->spa_proc_state
= SPA_PROC_GONE
;
1058 spa
->spa_proc
= &p0
;
1059 cv_broadcast(&spa
->spa_proc_cv
);
1060 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1062 mutex_enter(&curproc
->p_lock
);
1068 * Activate an uninitialized pool.
1071 spa_activate(spa_t
*spa
, int mode
)
1073 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1075 spa
->spa_state
= POOL_STATE_ACTIVE
;
1076 spa
->spa_mode
= mode
;
1078 spa
->spa_normal_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1079 spa
->spa_log_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1081 /* Try to create a covering process */
1082 mutex_enter(&spa
->spa_proc_lock
);
1083 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1084 ASSERT(spa
->spa_proc
== &p0
);
1087 #ifdef HAVE_SPA_THREAD
1088 /* Only create a process if we're going to be around a while. */
1089 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1090 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1092 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1093 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1094 cv_wait(&spa
->spa_proc_cv
,
1095 &spa
->spa_proc_lock
);
1097 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1098 ASSERT(spa
->spa_proc
!= &p0
);
1099 ASSERT(spa
->spa_did
!= 0);
1103 "Couldn't create process for zfs pool \"%s\"\n",
1108 #endif /* HAVE_SPA_THREAD */
1109 mutex_exit(&spa
->spa_proc_lock
);
1111 /* If we didn't create a process, we need to create our taskqs. */
1112 if (spa
->spa_proc
== &p0
) {
1113 spa_create_zio_taskqs(spa
);
1116 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1117 offsetof(vdev_t
, vdev_config_dirty_node
));
1118 list_create(&spa
->spa_evicting_os_list
, sizeof (objset_t
),
1119 offsetof(objset_t
, os_evicting_node
));
1120 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1121 offsetof(vdev_t
, vdev_state_dirty_node
));
1123 txg_list_create(&spa
->spa_vdev_txg_list
,
1124 offsetof(struct vdev
, vdev_txg_node
));
1126 avl_create(&spa
->spa_errlist_scrub
,
1127 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1128 offsetof(spa_error_entry_t
, se_avl
));
1129 avl_create(&spa
->spa_errlist_last
,
1130 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1131 offsetof(spa_error_entry_t
, se_avl
));
1135 * Opposite of spa_activate().
1138 spa_deactivate(spa_t
*spa
)
1142 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1143 ASSERT(spa
->spa_dsl_pool
== NULL
);
1144 ASSERT(spa
->spa_root_vdev
== NULL
);
1145 ASSERT(spa
->spa_async_zio_root
== NULL
);
1146 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1148 spa_evicting_os_wait(spa
);
1150 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1152 list_destroy(&spa
->spa_config_dirty_list
);
1153 list_destroy(&spa
->spa_evicting_os_list
);
1154 list_destroy(&spa
->spa_state_dirty_list
);
1156 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
1158 for (t
= 0; t
< ZIO_TYPES
; t
++) {
1159 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1160 spa_taskqs_fini(spa
, t
, q
);
1164 metaslab_class_destroy(spa
->spa_normal_class
);
1165 spa
->spa_normal_class
= NULL
;
1167 metaslab_class_destroy(spa
->spa_log_class
);
1168 spa
->spa_log_class
= NULL
;
1171 * If this was part of an import or the open otherwise failed, we may
1172 * still have errors left in the queues. Empty them just in case.
1174 spa_errlog_drain(spa
);
1176 avl_destroy(&spa
->spa_errlist_scrub
);
1177 avl_destroy(&spa
->spa_errlist_last
);
1179 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1181 mutex_enter(&spa
->spa_proc_lock
);
1182 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1183 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1184 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1185 cv_broadcast(&spa
->spa_proc_cv
);
1186 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1187 ASSERT(spa
->spa_proc
!= &p0
);
1188 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1190 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1191 spa
->spa_proc_state
= SPA_PROC_NONE
;
1193 ASSERT(spa
->spa_proc
== &p0
);
1194 mutex_exit(&spa
->spa_proc_lock
);
1197 * We want to make sure spa_thread() has actually exited the ZFS
1198 * module, so that the module can't be unloaded out from underneath
1201 if (spa
->spa_did
!= 0) {
1202 thread_join(spa
->spa_did
);
1208 * Verify a pool configuration, and construct the vdev tree appropriately. This
1209 * will create all the necessary vdevs in the appropriate layout, with each vdev
1210 * in the CLOSED state. This will prep the pool before open/creation/import.
1211 * All vdev validation is done by the vdev_alloc() routine.
1214 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1215 uint_t id
, int atype
)
1222 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1225 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1228 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1231 if (error
== ENOENT
)
1237 return (SET_ERROR(EINVAL
));
1240 for (c
= 0; c
< children
; c
++) {
1242 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1250 ASSERT(*vdp
!= NULL
);
1256 * Opposite of spa_load().
1259 spa_unload(spa_t
*spa
)
1263 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1268 spa_async_suspend(spa
);
1273 if (spa
->spa_sync_on
) {
1274 txg_sync_stop(spa
->spa_dsl_pool
);
1275 spa
->spa_sync_on
= B_FALSE
;
1279 * Wait for any outstanding async I/O to complete.
1281 if (spa
->spa_async_zio_root
!= NULL
) {
1282 for (i
= 0; i
< max_ncpus
; i
++)
1283 (void) zio_wait(spa
->spa_async_zio_root
[i
]);
1284 kmem_free(spa
->spa_async_zio_root
, max_ncpus
* sizeof (void *));
1285 spa
->spa_async_zio_root
= NULL
;
1288 bpobj_close(&spa
->spa_deferred_bpobj
);
1290 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1295 if (spa
->spa_root_vdev
)
1296 vdev_free(spa
->spa_root_vdev
);
1297 ASSERT(spa
->spa_root_vdev
== NULL
);
1300 * Close the dsl pool.
1302 if (spa
->spa_dsl_pool
) {
1303 dsl_pool_close(spa
->spa_dsl_pool
);
1304 spa
->spa_dsl_pool
= NULL
;
1305 spa
->spa_meta_objset
= NULL
;
1312 * Drop and purge level 2 cache
1314 spa_l2cache_drop(spa
);
1316 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1317 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1318 if (spa
->spa_spares
.sav_vdevs
) {
1319 kmem_free(spa
->spa_spares
.sav_vdevs
,
1320 spa
->spa_spares
.sav_count
* sizeof (void *));
1321 spa
->spa_spares
.sav_vdevs
= NULL
;
1323 if (spa
->spa_spares
.sav_config
) {
1324 nvlist_free(spa
->spa_spares
.sav_config
);
1325 spa
->spa_spares
.sav_config
= NULL
;
1327 spa
->spa_spares
.sav_count
= 0;
1329 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1330 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1331 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1333 if (spa
->spa_l2cache
.sav_vdevs
) {
1334 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1335 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1336 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1338 if (spa
->spa_l2cache
.sav_config
) {
1339 nvlist_free(spa
->spa_l2cache
.sav_config
);
1340 spa
->spa_l2cache
.sav_config
= NULL
;
1342 spa
->spa_l2cache
.sav_count
= 0;
1344 spa
->spa_async_suspended
= 0;
1346 if (spa
->spa_comment
!= NULL
) {
1347 spa_strfree(spa
->spa_comment
);
1348 spa
->spa_comment
= NULL
;
1351 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1355 * Load (or re-load) the current list of vdevs describing the active spares for
1356 * this pool. When this is called, we have some form of basic information in
1357 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1358 * then re-generate a more complete list including status information.
1361 spa_load_spares(spa_t
*spa
)
1368 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1371 * First, close and free any existing spare vdevs.
1373 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1374 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1376 /* Undo the call to spa_activate() below */
1377 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1378 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1379 spa_spare_remove(tvd
);
1384 if (spa
->spa_spares
.sav_vdevs
)
1385 kmem_free(spa
->spa_spares
.sav_vdevs
,
1386 spa
->spa_spares
.sav_count
* sizeof (void *));
1388 if (spa
->spa_spares
.sav_config
== NULL
)
1391 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1392 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
1394 spa
->spa_spares
.sav_count
= (int)nspares
;
1395 spa
->spa_spares
.sav_vdevs
= NULL
;
1401 * Construct the array of vdevs, opening them to get status in the
1402 * process. For each spare, there is potentially two different vdev_t
1403 * structures associated with it: one in the list of spares (used only
1404 * for basic validation purposes) and one in the active vdev
1405 * configuration (if it's spared in). During this phase we open and
1406 * validate each vdev on the spare list. If the vdev also exists in the
1407 * active configuration, then we also mark this vdev as an active spare.
1409 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
1411 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1412 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1413 VDEV_ALLOC_SPARE
) == 0);
1416 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1418 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1419 B_FALSE
)) != NULL
) {
1420 if (!tvd
->vdev_isspare
)
1424 * We only mark the spare active if we were successfully
1425 * able to load the vdev. Otherwise, importing a pool
1426 * with a bad active spare would result in strange
1427 * behavior, because multiple pool would think the spare
1428 * is actively in use.
1430 * There is a vulnerability here to an equally bizarre
1431 * circumstance, where a dead active spare is later
1432 * brought back to life (onlined or otherwise). Given
1433 * the rarity of this scenario, and the extra complexity
1434 * it adds, we ignore the possibility.
1436 if (!vdev_is_dead(tvd
))
1437 spa_spare_activate(tvd
);
1441 vd
->vdev_aux
= &spa
->spa_spares
;
1443 if (vdev_open(vd
) != 0)
1446 if (vdev_validate_aux(vd
) == 0)
1451 * Recompute the stashed list of spares, with status information
1454 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
,
1455 DATA_TYPE_NVLIST_ARRAY
) == 0);
1457 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1459 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1460 spares
[i
] = vdev_config_generate(spa
,
1461 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1462 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1463 ZPOOL_CONFIG_SPARES
, spares
, spa
->spa_spares
.sav_count
) == 0);
1464 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1465 nvlist_free(spares
[i
]);
1466 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1470 * Load (or re-load) the current list of vdevs describing the active l2cache for
1471 * this pool. When this is called, we have some form of basic information in
1472 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1473 * then re-generate a more complete list including status information.
1474 * Devices which are already active have their details maintained, and are
1478 spa_load_l2cache(spa_t
*spa
)
1482 int i
, j
, oldnvdevs
;
1484 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1485 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1487 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1489 if (sav
->sav_config
!= NULL
) {
1490 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
1491 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
1492 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_SLEEP
);
1498 oldvdevs
= sav
->sav_vdevs
;
1499 oldnvdevs
= sav
->sav_count
;
1500 sav
->sav_vdevs
= NULL
;
1504 * Process new nvlist of vdevs.
1506 for (i
= 0; i
< nl2cache
; i
++) {
1507 VERIFY(nvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
,
1511 for (j
= 0; j
< oldnvdevs
; j
++) {
1513 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1515 * Retain previous vdev for add/remove ops.
1523 if (newvdevs
[i
] == NULL
) {
1527 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1528 VDEV_ALLOC_L2CACHE
) == 0);
1533 * Commit this vdev as an l2cache device,
1534 * even if it fails to open.
1536 spa_l2cache_add(vd
);
1541 spa_l2cache_activate(vd
);
1543 if (vdev_open(vd
) != 0)
1546 (void) vdev_validate_aux(vd
);
1548 if (!vdev_is_dead(vd
))
1549 l2arc_add_vdev(spa
, vd
);
1554 * Purge vdevs that were dropped
1556 for (i
= 0; i
< oldnvdevs
; i
++) {
1561 ASSERT(vd
->vdev_isl2cache
);
1563 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
1564 pool
!= 0ULL && l2arc_vdev_present(vd
))
1565 l2arc_remove_vdev(vd
);
1566 vdev_clear_stats(vd
);
1572 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
1574 if (sav
->sav_config
== NULL
)
1577 sav
->sav_vdevs
= newvdevs
;
1578 sav
->sav_count
= (int)nl2cache
;
1581 * Recompute the stashed list of l2cache devices, with status
1582 * information this time.
1584 VERIFY(nvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
1585 DATA_TYPE_NVLIST_ARRAY
) == 0);
1587 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *), KM_SLEEP
);
1588 for (i
= 0; i
< sav
->sav_count
; i
++)
1589 l2cache
[i
] = vdev_config_generate(spa
,
1590 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
1591 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
1592 ZPOOL_CONFIG_L2CACHE
, l2cache
, sav
->sav_count
) == 0);
1594 for (i
= 0; i
< sav
->sav_count
; i
++)
1595 nvlist_free(l2cache
[i
]);
1597 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
1601 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
1604 char *packed
= NULL
;
1609 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
1613 nvsize
= *(uint64_t *)db
->db_data
;
1614 dmu_buf_rele(db
, FTAG
);
1616 packed
= vmem_alloc(nvsize
, KM_SLEEP
);
1617 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
1620 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
1621 vmem_free(packed
, nvsize
);
1627 * Checks to see if the given vdev could not be opened, in which case we post a
1628 * sysevent to notify the autoreplace code that the device has been removed.
1631 spa_check_removed(vdev_t
*vd
)
1635 for (c
= 0; c
< vd
->vdev_children
; c
++)
1636 spa_check_removed(vd
->vdev_child
[c
]);
1638 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
1640 zfs_ereport_post(FM_EREPORT_RESOURCE_AUTOREPLACE
,
1641 vd
->vdev_spa
, vd
, NULL
, 0, 0);
1642 spa_event_notify(vd
->vdev_spa
, vd
, FM_EREPORT_ZFS_DEVICE_CHECK
);
1647 * Validate the current config against the MOS config
1650 spa_config_valid(spa_t
*spa
, nvlist_t
*config
)
1652 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
1656 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0);
1658 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1659 VERIFY(spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
) == 0);
1661 ASSERT3U(rvd
->vdev_children
, ==, mrvd
->vdev_children
);
1664 * If we're doing a normal import, then build up any additional
1665 * diagnostic information about missing devices in this config.
1666 * We'll pass this up to the user for further processing.
1668 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
1669 nvlist_t
**child
, *nv
;
1672 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
**),
1674 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
1676 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1677 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1678 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1680 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1681 mtvd
->vdev_ops
!= &vdev_missing_ops
&&
1683 child
[idx
++] = vdev_config_generate(spa
, mtvd
,
1688 VERIFY(nvlist_add_nvlist_array(nv
,
1689 ZPOOL_CONFIG_CHILDREN
, child
, idx
) == 0);
1690 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
1691 ZPOOL_CONFIG_MISSING_DEVICES
, nv
) == 0);
1693 for (i
= 0; i
< idx
; i
++)
1694 nvlist_free(child
[i
]);
1697 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
1701 * Compare the root vdev tree with the information we have
1702 * from the MOS config (mrvd). Check each top-level vdev
1703 * with the corresponding MOS config top-level (mtvd).
1705 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1706 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1707 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1710 * Resolve any "missing" vdevs in the current configuration.
1711 * If we find that the MOS config has more accurate information
1712 * about the top-level vdev then use that vdev instead.
1714 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1715 mtvd
->vdev_ops
!= &vdev_missing_ops
) {
1717 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
))
1721 * Device specific actions.
1723 if (mtvd
->vdev_islog
) {
1724 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
1727 * XXX - once we have 'readonly' pool
1728 * support we should be able to handle
1729 * missing data devices by transitioning
1730 * the pool to readonly.
1736 * Swap the missing vdev with the data we were
1737 * able to obtain from the MOS config.
1739 vdev_remove_child(rvd
, tvd
);
1740 vdev_remove_child(mrvd
, mtvd
);
1742 vdev_add_child(rvd
, mtvd
);
1743 vdev_add_child(mrvd
, tvd
);
1745 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1747 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1750 } else if (mtvd
->vdev_islog
) {
1752 * Load the slog device's state from the MOS config
1753 * since it's possible that the label does not
1754 * contain the most up-to-date information.
1756 vdev_load_log_state(tvd
, mtvd
);
1761 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1764 * Ensure we were able to validate the config.
1766 return (rvd
->vdev_guid_sum
== spa
->spa_uberblock
.ub_guid_sum
);
1770 * Check for missing log devices
1773 spa_check_logs(spa_t
*spa
)
1775 boolean_t rv
= B_FALSE
;
1776 dsl_pool_t
*dp
= spa_get_dsl(spa
);
1778 switch (spa
->spa_log_state
) {
1781 case SPA_LOG_MISSING
:
1782 /* need to recheck in case slog has been restored */
1783 case SPA_LOG_UNKNOWN
:
1784 rv
= (dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
1785 zil_check_log_chain
, NULL
, DS_FIND_CHILDREN
) != 0);
1787 spa_set_log_state(spa
, SPA_LOG_MISSING
);
1794 spa_passivate_log(spa_t
*spa
)
1796 vdev_t
*rvd
= spa
->spa_root_vdev
;
1797 boolean_t slog_found
= B_FALSE
;
1800 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1802 if (!spa_has_slogs(spa
))
1805 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1806 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1807 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1809 if (tvd
->vdev_islog
) {
1810 metaslab_group_passivate(mg
);
1811 slog_found
= B_TRUE
;
1815 return (slog_found
);
1819 spa_activate_log(spa_t
*spa
)
1821 vdev_t
*rvd
= spa
->spa_root_vdev
;
1824 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1826 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1827 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1828 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1830 if (tvd
->vdev_islog
)
1831 metaslab_group_activate(mg
);
1836 spa_offline_log(spa_t
*spa
)
1840 error
= dmu_objset_find(spa_name(spa
), zil_vdev_offline
,
1841 NULL
, DS_FIND_CHILDREN
);
1844 * We successfully offlined the log device, sync out the
1845 * current txg so that the "stubby" block can be removed
1848 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1854 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
1858 for (i
= 0; i
< sav
->sav_count
; i
++)
1859 spa_check_removed(sav
->sav_vdevs
[i
]);
1863 spa_claim_notify(zio_t
*zio
)
1865 spa_t
*spa
= zio
->io_spa
;
1870 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
1871 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
1872 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
1873 mutex_exit(&spa
->spa_props_lock
);
1876 typedef struct spa_load_error
{
1877 uint64_t sle_meta_count
;
1878 uint64_t sle_data_count
;
1882 spa_load_verify_done(zio_t
*zio
)
1884 blkptr_t
*bp
= zio
->io_bp
;
1885 spa_load_error_t
*sle
= zio
->io_private
;
1886 dmu_object_type_t type
= BP_GET_TYPE(bp
);
1887 int error
= zio
->io_error
;
1888 spa_t
*spa
= zio
->io_spa
;
1891 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
1892 type
!= DMU_OT_INTENT_LOG
)
1893 atomic_add_64(&sle
->sle_meta_count
, 1);
1895 atomic_add_64(&sle
->sle_data_count
, 1);
1897 zio_data_buf_free(zio
->io_data
, zio
->io_size
);
1899 mutex_enter(&spa
->spa_scrub_lock
);
1900 spa
->spa_scrub_inflight
--;
1901 cv_broadcast(&spa
->spa_scrub_io_cv
);
1902 mutex_exit(&spa
->spa_scrub_lock
);
1906 * Maximum number of concurrent scrub i/os to create while verifying
1907 * a pool while importing it.
1909 int spa_load_verify_maxinflight
= 10000;
1910 int spa_load_verify_metadata
= B_TRUE
;
1911 int spa_load_verify_data
= B_TRUE
;
1915 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
1916 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
1922 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
1925 * Note: normally this routine will not be called if
1926 * spa_load_verify_metadata is not set. However, it may be useful
1927 * to manually set the flag after the traversal has begun.
1929 if (!spa_load_verify_metadata
)
1931 if (BP_GET_BUFC_TYPE(bp
) == ARC_BUFC_DATA
&& !spa_load_verify_data
)
1935 size
= BP_GET_PSIZE(bp
);
1936 data
= zio_data_buf_alloc(size
);
1938 mutex_enter(&spa
->spa_scrub_lock
);
1939 while (spa
->spa_scrub_inflight
>= spa_load_verify_maxinflight
)
1940 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
1941 spa
->spa_scrub_inflight
++;
1942 mutex_exit(&spa
->spa_scrub_lock
);
1944 zio_nowait(zio_read(rio
, spa
, bp
, data
, size
,
1945 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
1946 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
1947 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
1952 spa_load_verify(spa_t
*spa
)
1955 spa_load_error_t sle
= { 0 };
1956 zpool_rewind_policy_t policy
;
1957 boolean_t verify_ok
= B_FALSE
;
1960 zpool_get_rewind_policy(spa
->spa_config
, &policy
);
1962 if (policy
.zrp_request
& ZPOOL_NEVER_REWIND
)
1965 rio
= zio_root(spa
, NULL
, &sle
,
1966 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
1968 if (spa_load_verify_metadata
) {
1969 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
1970 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
,
1971 spa_load_verify_cb
, rio
);
1974 (void) zio_wait(rio
);
1976 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
1977 spa
->spa_load_data_errors
= sle
.sle_data_count
;
1979 if (!error
&& sle
.sle_meta_count
<= policy
.zrp_maxmeta
&&
1980 sle
.sle_data_count
<= policy
.zrp_maxdata
) {
1984 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
1985 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
1987 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
1988 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
1989 ZPOOL_CONFIG_LOAD_TIME
, spa
->spa_load_txg_ts
) == 0);
1990 VERIFY(nvlist_add_int64(spa
->spa_load_info
,
1991 ZPOOL_CONFIG_REWIND_TIME
, loss
) == 0);
1992 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
1993 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
) == 0);
1995 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
1999 if (error
!= ENXIO
&& error
!= EIO
)
2000 error
= SET_ERROR(EIO
);
2004 return (verify_ok
? 0 : EIO
);
2008 * Find a value in the pool props object.
2011 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
2013 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
2014 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
2018 * Find a value in the pool directory object.
2021 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
)
2023 return (zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
2024 name
, sizeof (uint64_t), 1, val
));
2028 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2030 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2035 * Fix up config after a partly-completed split. This is done with the
2036 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2037 * pool have that entry in their config, but only the splitting one contains
2038 * a list of all the guids of the vdevs that are being split off.
2040 * This function determines what to do with that list: either rejoin
2041 * all the disks to the pool, or complete the splitting process. To attempt
2042 * the rejoin, each disk that is offlined is marked online again, and
2043 * we do a reopen() call. If the vdev label for every disk that was
2044 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2045 * then we call vdev_split() on each disk, and complete the split.
2047 * Otherwise we leave the config alone, with all the vdevs in place in
2048 * the original pool.
2051 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
2058 boolean_t attempt_reopen
;
2060 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
2063 /* check that the config is complete */
2064 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
2065 &glist
, &gcount
) != 0)
2068 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_SLEEP
);
2070 /* attempt to online all the vdevs & validate */
2071 attempt_reopen
= B_TRUE
;
2072 for (i
= 0; i
< gcount
; i
++) {
2073 if (glist
[i
] == 0) /* vdev is hole */
2076 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
2077 if (vd
[i
] == NULL
) {
2079 * Don't bother attempting to reopen the disks;
2080 * just do the split.
2082 attempt_reopen
= B_FALSE
;
2084 /* attempt to re-online it */
2085 vd
[i
]->vdev_offline
= B_FALSE
;
2089 if (attempt_reopen
) {
2090 vdev_reopen(spa
->spa_root_vdev
);
2092 /* check each device to see what state it's in */
2093 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
2094 if (vd
[i
] != NULL
&&
2095 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
2102 * If every disk has been moved to the new pool, or if we never
2103 * even attempted to look at them, then we split them off for
2106 if (!attempt_reopen
|| gcount
== extracted
) {
2107 for (i
= 0; i
< gcount
; i
++)
2110 vdev_reopen(spa
->spa_root_vdev
);
2113 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
2117 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
,
2118 boolean_t mosconfig
)
2120 nvlist_t
*config
= spa
->spa_config
;
2121 char *ereport
= FM_EREPORT_ZFS_POOL
;
2127 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
))
2128 return (SET_ERROR(EINVAL
));
2130 ASSERT(spa
->spa_comment
== NULL
);
2131 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2132 spa
->spa_comment
= spa_strdup(comment
);
2135 * Versioning wasn't explicitly added to the label until later, so if
2136 * it's not present treat it as the initial version.
2138 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
2139 &spa
->spa_ubsync
.ub_version
) != 0)
2140 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
2142 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
2143 &spa
->spa_config_txg
);
2145 if ((state
== SPA_LOAD_IMPORT
|| state
== SPA_LOAD_TRYIMPORT
) &&
2146 spa_guid_exists(pool_guid
, 0)) {
2147 error
= SET_ERROR(EEXIST
);
2149 spa
->spa_config_guid
= pool_guid
;
2151 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
,
2153 VERIFY(nvlist_dup(nvl
, &spa
->spa_config_splitting
,
2157 nvlist_free(spa
->spa_load_info
);
2158 spa
->spa_load_info
= fnvlist_alloc();
2160 gethrestime(&spa
->spa_loaded_ts
);
2161 error
= spa_load_impl(spa
, pool_guid
, config
, state
, type
,
2162 mosconfig
, &ereport
);
2166 * Don't count references from objsets that are already closed
2167 * and are making their way through the eviction process.
2169 spa_evicting_os_wait(spa
);
2170 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
2172 if (error
!= EEXIST
) {
2173 spa
->spa_loaded_ts
.tv_sec
= 0;
2174 spa
->spa_loaded_ts
.tv_nsec
= 0;
2176 if (error
!= EBADF
) {
2177 zfs_ereport_post(ereport
, spa
, NULL
, NULL
, 0, 0);
2180 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
2187 * Load an existing storage pool, using the pool's builtin spa_config as a
2188 * source of configuration information.
2190 __attribute__((always_inline
))
2192 spa_load_impl(spa_t
*spa
, uint64_t pool_guid
, nvlist_t
*config
,
2193 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
2197 nvlist_t
*nvroot
= NULL
;
2200 uberblock_t
*ub
= &spa
->spa_uberblock
;
2201 uint64_t children
, config_cache_txg
= spa
->spa_config_txg
;
2202 int orig_mode
= spa
->spa_mode
;
2205 boolean_t missing_feat_write
= B_FALSE
;
2208 * If this is an untrusted config, access the pool in read-only mode.
2209 * This prevents things like resilvering recently removed devices.
2212 spa
->spa_mode
= FREAD
;
2214 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2216 spa
->spa_load_state
= state
;
2218 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvroot
))
2219 return (SET_ERROR(EINVAL
));
2221 parse
= (type
== SPA_IMPORT_EXISTING
?
2222 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
2225 * Create "The Godfather" zio to hold all async IOs
2227 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
2229 for (i
= 0; i
< max_ncpus
; i
++) {
2230 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
2231 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
2232 ZIO_FLAG_GODFATHER
);
2236 * Parse the configuration into a vdev tree. We explicitly set the
2237 * value that will be returned by spa_version() since parsing the
2238 * configuration requires knowing the version number.
2240 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2241 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, parse
);
2242 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2247 ASSERT(spa
->spa_root_vdev
== rvd
);
2248 ASSERT3U(spa
->spa_min_ashift
, >=, SPA_MINBLOCKSHIFT
);
2249 ASSERT3U(spa
->spa_max_ashift
, <=, SPA_MAXBLOCKSHIFT
);
2251 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2252 ASSERT(spa_guid(spa
) == pool_guid
);
2256 * Try to open all vdevs, loading each label in the process.
2258 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2259 error
= vdev_open(rvd
);
2260 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2265 * We need to validate the vdev labels against the configuration that
2266 * we have in hand, which is dependent on the setting of mosconfig. If
2267 * mosconfig is true then we're validating the vdev labels based on
2268 * that config. Otherwise, we're validating against the cached config
2269 * (zpool.cache) that was read when we loaded the zfs module, and then
2270 * later we will recursively call spa_load() and validate against
2273 * If we're assembling a new pool that's been split off from an
2274 * existing pool, the labels haven't yet been updated so we skip
2275 * validation for now.
2277 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2278 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2279 error
= vdev_validate(rvd
, mosconfig
);
2280 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2285 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2286 return (SET_ERROR(ENXIO
));
2290 * Find the best uberblock.
2292 vdev_uberblock_load(rvd
, ub
, &label
);
2295 * If we weren't able to find a single valid uberblock, return failure.
2297 if (ub
->ub_txg
== 0) {
2299 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
2303 * If the pool has an unsupported version we can't open it.
2305 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
2307 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
2310 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2314 * If we weren't able to find what's necessary for reading the
2315 * MOS in the label, return failure.
2317 if (label
== NULL
|| nvlist_lookup_nvlist(label
,
2318 ZPOOL_CONFIG_FEATURES_FOR_READ
, &features
) != 0) {
2320 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2325 * Update our in-core representation with the definitive values
2328 nvlist_free(spa
->spa_label_features
);
2329 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
, 0) == 0);
2335 * Look through entries in the label nvlist's features_for_read. If
2336 * there is a feature listed there which we don't understand then we
2337 * cannot open a pool.
2339 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2340 nvlist_t
*unsup_feat
;
2343 VERIFY(nvlist_alloc(&unsup_feat
, NV_UNIQUE_NAME
, KM_SLEEP
) ==
2346 for (nvp
= nvlist_next_nvpair(spa
->spa_label_features
, NULL
);
2348 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
2349 if (!zfeature_is_supported(nvpair_name(nvp
))) {
2350 VERIFY(nvlist_add_string(unsup_feat
,
2351 nvpair_name(nvp
), "") == 0);
2355 if (!nvlist_empty(unsup_feat
)) {
2356 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
2357 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
) == 0);
2358 nvlist_free(unsup_feat
);
2359 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2363 nvlist_free(unsup_feat
);
2367 * If the vdev guid sum doesn't match the uberblock, we have an
2368 * incomplete configuration. We first check to see if the pool
2369 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2370 * If it is, defer the vdev_guid_sum check till later so we
2371 * can handle missing vdevs.
2373 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VDEV_CHILDREN
,
2374 &children
) != 0 && mosconfig
&& type
!= SPA_IMPORT_ASSEMBLE
&&
2375 rvd
->vdev_guid_sum
!= ub
->ub_guid_sum
)
2376 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
2378 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
2379 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2380 spa_try_repair(spa
, config
);
2381 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2382 nvlist_free(spa
->spa_config_splitting
);
2383 spa
->spa_config_splitting
= NULL
;
2387 * Initialize internal SPA structures.
2389 spa
->spa_state
= POOL_STATE_ACTIVE
;
2390 spa
->spa_ubsync
= spa
->spa_uberblock
;
2391 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
2392 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
2393 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
2394 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
2395 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
2396 spa
->spa_prev_software_version
= ub
->ub_software_version
;
2398 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
2400 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2401 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
2403 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
) != 0)
2404 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2406 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
2407 boolean_t missing_feat_read
= B_FALSE
;
2408 nvlist_t
*unsup_feat
, *enabled_feat
;
2411 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
2412 &spa
->spa_feat_for_read_obj
) != 0) {
2413 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2416 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
2417 &spa
->spa_feat_for_write_obj
) != 0) {
2418 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2421 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
2422 &spa
->spa_feat_desc_obj
) != 0) {
2423 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2426 enabled_feat
= fnvlist_alloc();
2427 unsup_feat
= fnvlist_alloc();
2429 if (!spa_features_check(spa
, B_FALSE
,
2430 unsup_feat
, enabled_feat
))
2431 missing_feat_read
= B_TRUE
;
2433 if (spa_writeable(spa
) || state
== SPA_LOAD_TRYIMPORT
) {
2434 if (!spa_features_check(spa
, B_TRUE
,
2435 unsup_feat
, enabled_feat
)) {
2436 missing_feat_write
= B_TRUE
;
2440 fnvlist_add_nvlist(spa
->spa_load_info
,
2441 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
2443 if (!nvlist_empty(unsup_feat
)) {
2444 fnvlist_add_nvlist(spa
->spa_load_info
,
2445 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
2448 fnvlist_free(enabled_feat
);
2449 fnvlist_free(unsup_feat
);
2451 if (!missing_feat_read
) {
2452 fnvlist_add_boolean(spa
->spa_load_info
,
2453 ZPOOL_CONFIG_CAN_RDONLY
);
2457 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2458 * twofold: to determine whether the pool is available for
2459 * import in read-write mode and (if it is not) whether the
2460 * pool is available for import in read-only mode. If the pool
2461 * is available for import in read-write mode, it is displayed
2462 * as available in userland; if it is not available for import
2463 * in read-only mode, it is displayed as unavailable in
2464 * userland. If the pool is available for import in read-only
2465 * mode but not read-write mode, it is displayed as unavailable
2466 * in userland with a special note that the pool is actually
2467 * available for open in read-only mode.
2469 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2470 * missing a feature for write, we must first determine whether
2471 * the pool can be opened read-only before returning to
2472 * userland in order to know whether to display the
2473 * abovementioned note.
2475 if (missing_feat_read
|| (missing_feat_write
&&
2476 spa_writeable(spa
))) {
2477 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2482 * Load refcounts for ZFS features from disk into an in-memory
2483 * cache during SPA initialization.
2485 for (i
= 0; i
< SPA_FEATURES
; i
++) {
2488 error
= feature_get_refcount_from_disk(spa
,
2489 &spa_feature_table
[i
], &refcount
);
2491 spa
->spa_feat_refcount_cache
[i
] = refcount
;
2492 } else if (error
== ENOTSUP
) {
2493 spa
->spa_feat_refcount_cache
[i
] =
2494 SPA_FEATURE_DISABLED
;
2496 return (spa_vdev_err(rvd
,
2497 VDEV_AUX_CORRUPT_DATA
, EIO
));
2502 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
2503 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
2504 &spa
->spa_feat_enabled_txg_obj
) != 0)
2505 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2508 spa
->spa_is_initializing
= B_TRUE
;
2509 error
= dsl_pool_open(spa
->spa_dsl_pool
);
2510 spa
->spa_is_initializing
= B_FALSE
;
2512 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2516 nvlist_t
*policy
= NULL
, *nvconfig
;
2518 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2519 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2521 if (!spa_is_root(spa
) && nvlist_lookup_uint64(nvconfig
,
2522 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
2524 unsigned long myhostid
= 0;
2526 VERIFY(nvlist_lookup_string(nvconfig
,
2527 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
2530 myhostid
= zone_get_hostid(NULL
);
2533 * We're emulating the system's hostid in userland, so
2534 * we can't use zone_get_hostid().
2536 (void) ddi_strtoul(hw_serial
, NULL
, 10, &myhostid
);
2537 #endif /* _KERNEL */
2538 if (hostid
!= 0 && myhostid
!= 0 &&
2539 hostid
!= myhostid
) {
2540 nvlist_free(nvconfig
);
2541 cmn_err(CE_WARN
, "pool '%s' could not be "
2542 "loaded as it was last accessed by another "
2543 "system (host: %s hostid: 0x%lx). See: "
2544 "http://zfsonlinux.org/msg/ZFS-8000-EY",
2545 spa_name(spa
), hostname
,
2546 (unsigned long)hostid
);
2547 return (SET_ERROR(EBADF
));
2550 if (nvlist_lookup_nvlist(spa
->spa_config
,
2551 ZPOOL_REWIND_POLICY
, &policy
) == 0)
2552 VERIFY(nvlist_add_nvlist(nvconfig
,
2553 ZPOOL_REWIND_POLICY
, policy
) == 0);
2555 spa_config_set(spa
, nvconfig
);
2557 spa_deactivate(spa
);
2558 spa_activate(spa
, orig_mode
);
2560 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, B_TRUE
));
2563 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
) != 0)
2564 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2565 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
2567 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2570 * Load the bit that tells us to use the new accounting function
2571 * (raid-z deflation). If we have an older pool, this will not
2574 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
);
2575 if (error
!= 0 && error
!= ENOENT
)
2576 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2578 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
2579 &spa
->spa_creation_version
);
2580 if (error
!= 0 && error
!= ENOENT
)
2581 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2584 * Load the persistent error log. If we have an older pool, this will
2587 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
);
2588 if (error
!= 0 && error
!= ENOENT
)
2589 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2591 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
2592 &spa
->spa_errlog_scrub
);
2593 if (error
!= 0 && error
!= ENOENT
)
2594 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2597 * Load the history object. If we have an older pool, this
2598 * will not be present.
2600 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
);
2601 if (error
!= 0 && error
!= ENOENT
)
2602 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2605 * If we're assembling the pool from the split-off vdevs of
2606 * an existing pool, we don't want to attach the spares & cache
2611 * Load any hot spares for this pool.
2613 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
);
2614 if (error
!= 0 && error
!= ENOENT
)
2615 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2616 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2617 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
2618 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
2619 &spa
->spa_spares
.sav_config
) != 0)
2620 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2622 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2623 spa_load_spares(spa
);
2624 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2625 } else if (error
== 0) {
2626 spa
->spa_spares
.sav_sync
= B_TRUE
;
2630 * Load any level 2 ARC devices for this pool.
2632 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
2633 &spa
->spa_l2cache
.sav_object
);
2634 if (error
!= 0 && error
!= ENOENT
)
2635 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2636 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2637 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
2638 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
2639 &spa
->spa_l2cache
.sav_config
) != 0)
2640 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2642 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2643 spa_load_l2cache(spa
);
2644 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2645 } else if (error
== 0) {
2646 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
2649 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
2651 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
);
2652 if (error
&& error
!= ENOENT
)
2653 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2656 uint64_t autoreplace
= 0;
2658 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
2659 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
2660 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
2661 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
2662 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
2663 spa_prop_find(spa
, ZPOOL_PROP_DEDUPDITTO
,
2664 &spa
->spa_dedup_ditto
);
2666 spa
->spa_autoreplace
= (autoreplace
!= 0);
2670 * If the 'autoreplace' property is set, then post a resource notifying
2671 * the ZFS DE that it should not issue any faults for unopenable
2672 * devices. We also iterate over the vdevs, and post a sysevent for any
2673 * unopenable vdevs so that the normal autoreplace handler can take
2676 if (spa
->spa_autoreplace
&& state
!= SPA_LOAD_TRYIMPORT
) {
2677 spa_check_removed(spa
->spa_root_vdev
);
2679 * For the import case, this is done in spa_import(), because
2680 * at this point we're using the spare definitions from
2681 * the MOS config, not necessarily from the userland config.
2683 if (state
!= SPA_LOAD_IMPORT
) {
2684 spa_aux_check_removed(&spa
->spa_spares
);
2685 spa_aux_check_removed(&spa
->spa_l2cache
);
2690 * Load the vdev state for all toplevel vdevs.
2695 * Propagate the leaf DTLs we just loaded all the way up the tree.
2697 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2698 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
);
2699 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2702 * Load the DDTs (dedup tables).
2704 error
= ddt_load(spa
);
2706 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2708 spa_update_dspace(spa
);
2711 * Validate the config, using the MOS config to fill in any
2712 * information which might be missing. If we fail to validate
2713 * the config then declare the pool unfit for use. If we're
2714 * assembling a pool from a split, the log is not transferred
2717 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2720 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2721 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2723 if (!spa_config_valid(spa
, nvconfig
)) {
2724 nvlist_free(nvconfig
);
2725 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
2728 nvlist_free(nvconfig
);
2731 * Now that we've validated the config, check the state of the
2732 * root vdev. If it can't be opened, it indicates one or
2733 * more toplevel vdevs are faulted.
2735 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2736 return (SET_ERROR(ENXIO
));
2738 if (spa_writeable(spa
) && spa_check_logs(spa
)) {
2739 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
2740 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
, ENXIO
));
2744 if (missing_feat_write
) {
2745 ASSERT(state
== SPA_LOAD_TRYIMPORT
);
2748 * At this point, we know that we can open the pool in
2749 * read-only mode but not read-write mode. We now have enough
2750 * information and can return to userland.
2752 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
, ENOTSUP
));
2756 * We've successfully opened the pool, verify that we're ready
2757 * to start pushing transactions.
2759 if (state
!= SPA_LOAD_TRYIMPORT
) {
2760 if ((error
= spa_load_verify(spa
)))
2761 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2765 if (spa_writeable(spa
) && (state
== SPA_LOAD_RECOVER
||
2766 spa
->spa_load_max_txg
== UINT64_MAX
)) {
2768 int need_update
= B_FALSE
;
2769 dsl_pool_t
*dp
= spa_get_dsl(spa
);
2772 ASSERT(state
!= SPA_LOAD_TRYIMPORT
);
2775 * Claim log blocks that haven't been committed yet.
2776 * This must all happen in a single txg.
2777 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2778 * invoked from zil_claim_log_block()'s i/o done callback.
2779 * Price of rollback is that we abandon the log.
2781 spa
->spa_claiming
= B_TRUE
;
2783 tx
= dmu_tx_create_assigned(dp
, spa_first_txg(spa
));
2784 (void) dmu_objset_find_dp(dp
, dp
->dp_root_dir_obj
,
2785 zil_claim
, tx
, DS_FIND_CHILDREN
);
2788 spa
->spa_claiming
= B_FALSE
;
2790 spa_set_log_state(spa
, SPA_LOG_GOOD
);
2791 spa
->spa_sync_on
= B_TRUE
;
2792 txg_sync_start(spa
->spa_dsl_pool
);
2795 * Wait for all claims to sync. We sync up to the highest
2796 * claimed log block birth time so that claimed log blocks
2797 * don't appear to be from the future. spa_claim_max_txg
2798 * will have been set for us by either zil_check_log_chain()
2799 * (invoked from spa_check_logs()) or zil_claim() above.
2801 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
2804 * If the config cache is stale, or we have uninitialized
2805 * metaslabs (see spa_vdev_add()), then update the config.
2807 * If this is a verbatim import, trust the current
2808 * in-core spa_config and update the disk labels.
2810 if (config_cache_txg
!= spa
->spa_config_txg
||
2811 state
== SPA_LOAD_IMPORT
||
2812 state
== SPA_LOAD_RECOVER
||
2813 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
2814 need_update
= B_TRUE
;
2816 for (c
= 0; c
< rvd
->vdev_children
; c
++)
2817 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
2818 need_update
= B_TRUE
;
2821 * Update the config cache asychronously in case we're the
2822 * root pool, in which case the config cache isn't writable yet.
2825 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
2828 * Check all DTLs to see if anything needs resilvering.
2830 if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
2831 vdev_resilver_needed(rvd
, NULL
, NULL
))
2832 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
2835 * Log the fact that we booted up (so that we can detect if
2836 * we rebooted in the middle of an operation).
2838 spa_history_log_version(spa
, "open");
2841 * Delete any inconsistent datasets.
2843 (void) dmu_objset_find(spa_name(spa
),
2844 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
2847 * Clean up any stale temporary dataset userrefs.
2849 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
2856 spa_load_retry(spa_t
*spa
, spa_load_state_t state
, int mosconfig
)
2858 int mode
= spa
->spa_mode
;
2861 spa_deactivate(spa
);
2863 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
2865 spa_activate(spa
, mode
);
2866 spa_async_suspend(spa
);
2868 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, mosconfig
));
2872 * If spa_load() fails this function will try loading prior txg's. If
2873 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2874 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2875 * function will not rewind the pool and will return the same error as
2879 spa_load_best(spa_t
*spa
, spa_load_state_t state
, int mosconfig
,
2880 uint64_t max_request
, int rewind_flags
)
2882 nvlist_t
*loadinfo
= NULL
;
2883 nvlist_t
*config
= NULL
;
2884 int load_error
, rewind_error
;
2885 uint64_t safe_rewind_txg
;
2888 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
2889 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
2890 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2892 spa
->spa_load_max_txg
= max_request
;
2893 if (max_request
!= UINT64_MAX
)
2894 spa
->spa_extreme_rewind
= B_TRUE
;
2897 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
,
2899 if (load_error
== 0)
2902 if (spa
->spa_root_vdev
!= NULL
)
2903 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
2905 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
2906 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2908 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
2909 nvlist_free(config
);
2910 return (load_error
);
2913 if (state
== SPA_LOAD_RECOVER
) {
2914 /* Price of rolling back is discarding txgs, including log */
2915 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2918 * If we aren't rolling back save the load info from our first
2919 * import attempt so that we can restore it after attempting
2922 loadinfo
= spa
->spa_load_info
;
2923 spa
->spa_load_info
= fnvlist_alloc();
2926 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
2927 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
2928 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
2929 TXG_INITIAL
: safe_rewind_txg
;
2932 * Continue as long as we're finding errors, we're still within
2933 * the acceptable rewind range, and we're still finding uberblocks
2935 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
2936 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
2937 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
2938 spa
->spa_extreme_rewind
= B_TRUE
;
2939 rewind_error
= spa_load_retry(spa
, state
, mosconfig
);
2942 spa
->spa_extreme_rewind
= B_FALSE
;
2943 spa
->spa_load_max_txg
= UINT64_MAX
;
2945 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
2946 spa_config_set(spa
, config
);
2948 if (state
== SPA_LOAD_RECOVER
) {
2949 ASSERT3P(loadinfo
, ==, NULL
);
2950 return (rewind_error
);
2952 /* Store the rewind info as part of the initial load info */
2953 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
2954 spa
->spa_load_info
);
2956 /* Restore the initial load info */
2957 fnvlist_free(spa
->spa_load_info
);
2958 spa
->spa_load_info
= loadinfo
;
2960 return (load_error
);
2967 * The import case is identical to an open except that the configuration is sent
2968 * down from userland, instead of grabbed from the configuration cache. For the
2969 * case of an open, the pool configuration will exist in the
2970 * POOL_STATE_UNINITIALIZED state.
2972 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2973 * the same time open the pool, without having to keep around the spa_t in some
2977 spa_open_common(const char *pool
, spa_t
**spapp
, void *tag
, nvlist_t
*nvpolicy
,
2981 spa_load_state_t state
= SPA_LOAD_OPEN
;
2983 int locked
= B_FALSE
;
2984 int firstopen
= B_FALSE
;
2989 * As disgusting as this is, we need to support recursive calls to this
2990 * function because dsl_dir_open() is called during spa_load(), and ends
2991 * up calling spa_open() again. The real fix is to figure out how to
2992 * avoid dsl_dir_open() calling this in the first place.
2994 if (mutex_owner(&spa_namespace_lock
) != curthread
) {
2995 mutex_enter(&spa_namespace_lock
);
2999 if ((spa
= spa_lookup(pool
)) == NULL
) {
3001 mutex_exit(&spa_namespace_lock
);
3002 return (SET_ERROR(ENOENT
));
3005 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
3006 zpool_rewind_policy_t policy
;
3010 zpool_get_rewind_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
3012 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
3013 state
= SPA_LOAD_RECOVER
;
3015 spa_activate(spa
, spa_mode_global
);
3017 if (state
!= SPA_LOAD_RECOVER
)
3018 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
3020 error
= spa_load_best(spa
, state
, B_FALSE
, policy
.zrp_txg
,
3021 policy
.zrp_request
);
3023 if (error
== EBADF
) {
3025 * If vdev_validate() returns failure (indicated by
3026 * EBADF), it indicates that one of the vdevs indicates
3027 * that the pool has been exported or destroyed. If
3028 * this is the case, the config cache is out of sync and
3029 * we should remove the pool from the namespace.
3032 spa_deactivate(spa
);
3033 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
3036 mutex_exit(&spa_namespace_lock
);
3037 return (SET_ERROR(ENOENT
));
3042 * We can't open the pool, but we still have useful
3043 * information: the state of each vdev after the
3044 * attempted vdev_open(). Return this to the user.
3046 if (config
!= NULL
&& spa
->spa_config
) {
3047 VERIFY(nvlist_dup(spa
->spa_config
, config
,
3049 VERIFY(nvlist_add_nvlist(*config
,
3050 ZPOOL_CONFIG_LOAD_INFO
,
3051 spa
->spa_load_info
) == 0);
3054 spa_deactivate(spa
);
3055 spa
->spa_last_open_failed
= error
;
3057 mutex_exit(&spa_namespace_lock
);
3063 spa_open_ref(spa
, tag
);
3066 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3069 * If we've recovered the pool, pass back any information we
3070 * gathered while doing the load.
3072 if (state
== SPA_LOAD_RECOVER
) {
3073 VERIFY(nvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
3074 spa
->spa_load_info
) == 0);
3078 spa
->spa_last_open_failed
= 0;
3079 spa
->spa_last_ubsync_txg
= 0;
3080 spa
->spa_load_txg
= 0;
3081 mutex_exit(&spa_namespace_lock
);
3086 zvol_create_minors(spa
->spa_name
);
3095 spa_open_rewind(const char *name
, spa_t
**spapp
, void *tag
, nvlist_t
*policy
,
3098 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
3102 spa_open(const char *name
, spa_t
**spapp
, void *tag
)
3104 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
3108 * Lookup the given spa_t, incrementing the inject count in the process,
3109 * preventing it from being exported or destroyed.
3112 spa_inject_addref(char *name
)
3116 mutex_enter(&spa_namespace_lock
);
3117 if ((spa
= spa_lookup(name
)) == NULL
) {
3118 mutex_exit(&spa_namespace_lock
);
3121 spa
->spa_inject_ref
++;
3122 mutex_exit(&spa_namespace_lock
);
3128 spa_inject_delref(spa_t
*spa
)
3130 mutex_enter(&spa_namespace_lock
);
3131 spa
->spa_inject_ref
--;
3132 mutex_exit(&spa_namespace_lock
);
3136 * Add spares device information to the nvlist.
3139 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
3149 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3151 if (spa
->spa_spares
.sav_count
== 0)
3154 VERIFY(nvlist_lookup_nvlist(config
,
3155 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3156 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
3157 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3159 VERIFY(nvlist_add_nvlist_array(nvroot
,
3160 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3161 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3162 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3165 * Go through and find any spares which have since been
3166 * repurposed as an active spare. If this is the case, update
3167 * their status appropriately.
3169 for (i
= 0; i
< nspares
; i
++) {
3170 VERIFY(nvlist_lookup_uint64(spares
[i
],
3171 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3172 if (spa_spare_exists(guid
, &pool
, NULL
) &&
3174 VERIFY(nvlist_lookup_uint64_array(
3175 spares
[i
], ZPOOL_CONFIG_VDEV_STATS
,
3176 (uint64_t **)&vs
, &vsc
) == 0);
3177 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
3178 vs
->vs_aux
= VDEV_AUX_SPARED
;
3185 * Add l2cache device information to the nvlist, including vdev stats.
3188 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
3191 uint_t i
, j
, nl2cache
;
3198 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3200 if (spa
->spa_l2cache
.sav_count
== 0)
3203 VERIFY(nvlist_lookup_nvlist(config
,
3204 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3205 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
3206 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3207 if (nl2cache
!= 0) {
3208 VERIFY(nvlist_add_nvlist_array(nvroot
,
3209 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3210 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3211 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3214 * Update level 2 cache device stats.
3217 for (i
= 0; i
< nl2cache
; i
++) {
3218 VERIFY(nvlist_lookup_uint64(l2cache
[i
],
3219 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3222 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
3224 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
3225 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
3231 VERIFY(nvlist_lookup_uint64_array(l2cache
[i
],
3232 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
3234 vdev_get_stats(vd
, vs
);
3240 spa_feature_stats_from_disk(spa_t
*spa
, nvlist_t
*features
)
3245 if (spa
->spa_feat_for_read_obj
!= 0) {
3246 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3247 spa
->spa_feat_for_read_obj
);
3248 zap_cursor_retrieve(&zc
, &za
) == 0;
3249 zap_cursor_advance(&zc
)) {
3250 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3251 za
.za_num_integers
== 1);
3252 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
3253 za
.za_first_integer
));
3255 zap_cursor_fini(&zc
);
3258 if (spa
->spa_feat_for_write_obj
!= 0) {
3259 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3260 spa
->spa_feat_for_write_obj
);
3261 zap_cursor_retrieve(&zc
, &za
) == 0;
3262 zap_cursor_advance(&zc
)) {
3263 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3264 za
.za_num_integers
== 1);
3265 VERIFY0(nvlist_add_uint64(features
, za
.za_name
,
3266 za
.za_first_integer
));
3268 zap_cursor_fini(&zc
);
3273 spa_feature_stats_from_cache(spa_t
*spa
, nvlist_t
*features
)
3277 for (i
= 0; i
< SPA_FEATURES
; i
++) {
3278 zfeature_info_t feature
= spa_feature_table
[i
];
3281 if (feature_get_refcount(spa
, &feature
, &refcount
) != 0)
3284 VERIFY0(nvlist_add_uint64(features
, feature
.fi_guid
, refcount
));
3289 * Store a list of pool features and their reference counts in the
3292 * The first time this is called on a spa, allocate a new nvlist, fetch
3293 * the pool features and reference counts from disk, then save the list
3294 * in the spa. In subsequent calls on the same spa use the saved nvlist
3295 * and refresh its values from the cached reference counts. This
3296 * ensures we don't block here on I/O on a suspended pool so 'zpool
3297 * clear' can resume the pool.
3300 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
3304 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3306 mutex_enter(&spa
->spa_feat_stats_lock
);
3307 features
= spa
->spa_feat_stats
;
3309 if (features
!= NULL
) {
3310 spa_feature_stats_from_cache(spa
, features
);
3312 VERIFY0(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
));
3313 spa
->spa_feat_stats
= features
;
3314 spa_feature_stats_from_disk(spa
, features
);
3317 VERIFY0(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
3320 mutex_exit(&spa
->spa_feat_stats_lock
);
3324 spa_get_stats(const char *name
, nvlist_t
**config
,
3325 char *altroot
, size_t buflen
)
3331 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
3335 * This still leaves a window of inconsistency where the spares
3336 * or l2cache devices could change and the config would be
3337 * self-inconsistent.
3339 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
3341 if (*config
!= NULL
) {
3342 uint64_t loadtimes
[2];
3344 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
3345 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
3346 VERIFY(nvlist_add_uint64_array(*config
,
3347 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2) == 0);
3349 VERIFY(nvlist_add_uint64(*config
,
3350 ZPOOL_CONFIG_ERRCOUNT
,
3351 spa_get_errlog_size(spa
)) == 0);
3353 if (spa_suspended(spa
))
3354 VERIFY(nvlist_add_uint64(*config
,
3355 ZPOOL_CONFIG_SUSPENDED
,
3356 spa
->spa_failmode
) == 0);
3358 spa_add_spares(spa
, *config
);
3359 spa_add_l2cache(spa
, *config
);
3360 spa_add_feature_stats(spa
, *config
);
3365 * We want to get the alternate root even for faulted pools, so we cheat
3366 * and call spa_lookup() directly.
3370 mutex_enter(&spa_namespace_lock
);
3371 spa
= spa_lookup(name
);
3373 spa_altroot(spa
, altroot
, buflen
);
3377 mutex_exit(&spa_namespace_lock
);
3379 spa_altroot(spa
, altroot
, buflen
);
3384 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
3385 spa_close(spa
, FTAG
);
3392 * Validate that the auxiliary device array is well formed. We must have an
3393 * array of nvlists, each which describes a valid leaf vdev. If this is an
3394 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3395 * specified, as long as they are well-formed.
3398 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
3399 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
3400 vdev_labeltype_t label
)
3407 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3410 * It's acceptable to have no devs specified.
3412 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
3416 return (SET_ERROR(EINVAL
));
3419 * Make sure the pool is formatted with a version that supports this
3422 if (spa_version(spa
) < version
)
3423 return (SET_ERROR(ENOTSUP
));
3426 * Set the pending device list so we correctly handle device in-use
3429 sav
->sav_pending
= dev
;
3430 sav
->sav_npending
= ndev
;
3432 for (i
= 0; i
< ndev
; i
++) {
3433 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
3437 if (!vd
->vdev_ops
->vdev_op_leaf
) {
3439 error
= SET_ERROR(EINVAL
);
3444 * The L2ARC currently only supports disk devices in
3445 * kernel context. For user-level testing, we allow it.
3448 if ((strcmp(config
, ZPOOL_CONFIG_L2CACHE
) == 0) &&
3449 strcmp(vd
->vdev_ops
->vdev_op_type
, VDEV_TYPE_DISK
) != 0) {
3450 error
= SET_ERROR(ENOTBLK
);
3457 if ((error
= vdev_open(vd
)) == 0 &&
3458 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
3459 VERIFY(nvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
3460 vd
->vdev_guid
) == 0);
3466 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
3473 sav
->sav_pending
= NULL
;
3474 sav
->sav_npending
= 0;
3479 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
3483 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3485 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3486 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
3487 VDEV_LABEL_SPARE
)) != 0) {
3491 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3492 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
3493 VDEV_LABEL_L2CACHE
));
3497 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
3502 if (sav
->sav_config
!= NULL
) {
3508 * Generate new dev list by concatentating with the
3511 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
3512 &olddevs
, &oldndevs
) == 0);
3514 newdevs
= kmem_alloc(sizeof (void *) *
3515 (ndevs
+ oldndevs
), KM_SLEEP
);
3516 for (i
= 0; i
< oldndevs
; i
++)
3517 VERIFY(nvlist_dup(olddevs
[i
], &newdevs
[i
],
3519 for (i
= 0; i
< ndevs
; i
++)
3520 VERIFY(nvlist_dup(devs
[i
], &newdevs
[i
+ oldndevs
],
3523 VERIFY(nvlist_remove(sav
->sav_config
, config
,
3524 DATA_TYPE_NVLIST_ARRAY
) == 0);
3526 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
3527 config
, newdevs
, ndevs
+ oldndevs
) == 0);
3528 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
3529 nvlist_free(newdevs
[i
]);
3530 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
3533 * Generate a new dev list.
3535 VERIFY(nvlist_alloc(&sav
->sav_config
, NV_UNIQUE_NAME
,
3537 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
, config
,
3543 * Stop and drop level 2 ARC devices
3546 spa_l2cache_drop(spa_t
*spa
)
3550 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
3552 for (i
= 0; i
< sav
->sav_count
; i
++) {
3555 vd
= sav
->sav_vdevs
[i
];
3558 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
3559 pool
!= 0ULL && l2arc_vdev_present(vd
))
3560 l2arc_remove_vdev(vd
);
3568 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
3572 char *altroot
= NULL
;
3577 uint64_t txg
= TXG_INITIAL
;
3578 nvlist_t
**spares
, **l2cache
;
3579 uint_t nspares
, nl2cache
;
3580 uint64_t version
, obj
;
3581 boolean_t has_features
;
3587 if (nvlist_lookup_string(props
, "tname", &poolname
) != 0)
3588 poolname
= (char *)pool
;
3591 * If this pool already exists, return failure.
3593 mutex_enter(&spa_namespace_lock
);
3594 if (spa_lookup(poolname
) != NULL
) {
3595 mutex_exit(&spa_namespace_lock
);
3596 return (SET_ERROR(EEXIST
));
3600 * Allocate a new spa_t structure.
3602 nvl
= fnvlist_alloc();
3603 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
3604 (void) nvlist_lookup_string(props
,
3605 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
3606 spa
= spa_add(poolname
, nvl
, altroot
);
3608 spa_activate(spa
, spa_mode_global
);
3610 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
3611 spa_deactivate(spa
);
3613 mutex_exit(&spa_namespace_lock
);
3618 * Temporary pool names should never be written to disk.
3620 if (poolname
!= pool
)
3621 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
3623 has_features
= B_FALSE
;
3624 for (elem
= nvlist_next_nvpair(props
, NULL
);
3625 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
3626 if (zpool_prop_feature(nvpair_name(elem
)))
3627 has_features
= B_TRUE
;
3630 if (has_features
|| nvlist_lookup_uint64(props
,
3631 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
3632 version
= SPA_VERSION
;
3634 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
3636 spa
->spa_first_txg
= txg
;
3637 spa
->spa_uberblock
.ub_txg
= txg
- 1;
3638 spa
->spa_uberblock
.ub_version
= version
;
3639 spa
->spa_ubsync
= spa
->spa_uberblock
;
3642 * Create "The Godfather" zio to hold all async IOs
3644 spa
->spa_async_zio_root
= kmem_alloc(max_ncpus
* sizeof (void *),
3646 for (i
= 0; i
< max_ncpus
; i
++) {
3647 spa
->spa_async_zio_root
[i
] = zio_root(spa
, NULL
, NULL
,
3648 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
|
3649 ZIO_FLAG_GODFATHER
);
3653 * Create the root vdev.
3655 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3657 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
3659 ASSERT(error
!= 0 || rvd
!= NULL
);
3660 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
3662 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
3663 error
= SET_ERROR(EINVAL
);
3666 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
3667 (error
= spa_validate_aux(spa
, nvroot
, txg
,
3668 VDEV_ALLOC_ADD
)) == 0) {
3669 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
3670 vdev_metaslab_set_size(rvd
->vdev_child
[c
]);
3671 vdev_expand(rvd
->vdev_child
[c
], txg
);
3675 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3679 spa_deactivate(spa
);
3681 mutex_exit(&spa_namespace_lock
);
3686 * Get the list of spares, if specified.
3688 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
3689 &spares
, &nspares
) == 0) {
3690 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
, NV_UNIQUE_NAME
,
3692 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
3693 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3694 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3695 spa_load_spares(spa
);
3696 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3697 spa
->spa_spares
.sav_sync
= B_TRUE
;
3701 * Get the list of level 2 cache devices, if specified.
3703 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
3704 &l2cache
, &nl2cache
) == 0) {
3705 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
3706 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3707 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
3708 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3709 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3710 spa_load_l2cache(spa
);
3711 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3712 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3715 spa
->spa_is_initializing
= B_TRUE
;
3716 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, txg
);
3717 spa
->spa_meta_objset
= dp
->dp_meta_objset
;
3718 spa
->spa_is_initializing
= B_FALSE
;
3721 * Create DDTs (dedup tables).
3725 spa_update_dspace(spa
);
3727 tx
= dmu_tx_create_assigned(dp
, txg
);
3730 * Create the pool config object.
3732 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
3733 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
3734 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
3736 if (zap_add(spa
->spa_meta_objset
,
3737 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
3738 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
3739 cmn_err(CE_PANIC
, "failed to add pool config");
3742 if (spa_version(spa
) >= SPA_VERSION_FEATURES
)
3743 spa_feature_create_zap_objects(spa
, tx
);
3745 if (zap_add(spa
->spa_meta_objset
,
3746 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
3747 sizeof (uint64_t), 1, &version
, tx
) != 0) {
3748 cmn_err(CE_PANIC
, "failed to add pool version");
3751 /* Newly created pools with the right version are always deflated. */
3752 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
3753 spa
->spa_deflate
= TRUE
;
3754 if (zap_add(spa
->spa_meta_objset
,
3755 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
3756 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
3757 cmn_err(CE_PANIC
, "failed to add deflate");
3762 * Create the deferred-free bpobj. Turn off compression
3763 * because sync-to-convergence takes longer if the blocksize
3766 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
3767 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
3768 ZIO_COMPRESS_OFF
, tx
);
3769 if (zap_add(spa
->spa_meta_objset
,
3770 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
3771 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
3772 cmn_err(CE_PANIC
, "failed to add bpobj");
3774 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
3775 spa
->spa_meta_objset
, obj
));
3778 * Create the pool's history object.
3780 if (version
>= SPA_VERSION_ZPOOL_HISTORY
)
3781 spa_history_create_obj(spa
, tx
);
3784 * Set pool properties.
3786 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
3787 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
3788 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
3789 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
3791 if (props
!= NULL
) {
3792 spa_configfile_set(spa
, props
, B_FALSE
);
3793 spa_sync_props(props
, tx
);
3798 spa
->spa_sync_on
= B_TRUE
;
3799 txg_sync_start(spa
->spa_dsl_pool
);
3802 * We explicitly wait for the first transaction to complete so that our
3803 * bean counters are appropriately updated.
3805 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
3807 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
3809 spa_history_log_version(spa
, "create");
3812 * Don't count references from objsets that are already closed
3813 * and are making their way through the eviction process.
3815 spa_evicting_os_wait(spa
);
3816 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
3818 mutex_exit(&spa_namespace_lock
);
3825 * Get the root pool information from the root disk, then import the root pool
3826 * during the system boot up time.
3828 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t
**);
3831 spa_generate_rootconf(char *devpath
, char *devid
, uint64_t *guid
)
3834 nvlist_t
*nvtop
, *nvroot
;
3837 if (vdev_disk_read_rootlabel(devpath
, devid
, &config
) != 0)
3841 * Add this top-level vdev to the child array.
3843 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3845 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
3847 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_GUID
, guid
) == 0);
3850 * Put this pool's top-level vdevs into a root vdev.
3852 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3853 VERIFY(nvlist_add_string(nvroot
, ZPOOL_CONFIG_TYPE
,
3854 VDEV_TYPE_ROOT
) == 0);
3855 VERIFY(nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_ID
, 0ULL) == 0);
3856 VERIFY(nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_GUID
, pgid
) == 0);
3857 VERIFY(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
3861 * Replace the existing vdev_tree with the new root vdev in
3862 * this pool's configuration (remove the old, add the new).
3864 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, nvroot
) == 0);
3865 nvlist_free(nvroot
);
3870 * Walk the vdev tree and see if we can find a device with "better"
3871 * configuration. A configuration is "better" if the label on that
3872 * device has a more recent txg.
3875 spa_alt_rootvdev(vdev_t
*vd
, vdev_t
**avd
, uint64_t *txg
)
3879 for (c
= 0; c
< vd
->vdev_children
; c
++)
3880 spa_alt_rootvdev(vd
->vdev_child
[c
], avd
, txg
);
3882 if (vd
->vdev_ops
->vdev_op_leaf
) {
3886 if (vdev_disk_read_rootlabel(vd
->vdev_physpath
, vd
->vdev_devid
,
3890 VERIFY(nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_TXG
,
3894 * Do we have a better boot device?
3896 if (label_txg
> *txg
) {
3905 * Import a root pool.
3907 * For x86. devpath_list will consist of devid and/or physpath name of
3908 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3909 * The GRUB "findroot" command will return the vdev we should boot.
3911 * For Sparc, devpath_list consists the physpath name of the booting device
3912 * no matter the rootpool is a single device pool or a mirrored pool.
3914 * "/pci@1f,0/ide@d/disk@0,0:a"
3917 spa_import_rootpool(char *devpath
, char *devid
)
3920 vdev_t
*rvd
, *bvd
, *avd
= NULL
;
3921 nvlist_t
*config
, *nvtop
;
3927 * Read the label from the boot device and generate a configuration.
3929 config
= spa_generate_rootconf(devpath
, devid
, &guid
);
3930 #if defined(_OBP) && defined(_KERNEL)
3931 if (config
== NULL
) {
3932 if (strstr(devpath
, "/iscsi/ssd") != NULL
) {
3934 get_iscsi_bootpath_phy(devpath
);
3935 config
= spa_generate_rootconf(devpath
, devid
, &guid
);
3939 if (config
== NULL
) {
3940 cmn_err(CE_NOTE
, "Cannot read the pool label from '%s'",
3942 return (SET_ERROR(EIO
));
3945 VERIFY(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
3947 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
, &txg
) == 0);
3949 mutex_enter(&spa_namespace_lock
);
3950 if ((spa
= spa_lookup(pname
)) != NULL
) {
3952 * Remove the existing root pool from the namespace so that we
3953 * can replace it with the correct config we just read in.
3958 spa
= spa_add(pname
, config
, NULL
);
3959 spa
->spa_is_root
= B_TRUE
;
3960 spa
->spa_import_flags
= ZFS_IMPORT_VERBATIM
;
3963 * Build up a vdev tree based on the boot device's label config.
3965 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3967 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3968 error
= spa_config_parse(spa
, &rvd
, nvtop
, NULL
, 0,
3969 VDEV_ALLOC_ROOTPOOL
);
3970 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3972 mutex_exit(&spa_namespace_lock
);
3973 nvlist_free(config
);
3974 cmn_err(CE_NOTE
, "Can not parse the config for pool '%s'",
3980 * Get the boot vdev.
3982 if ((bvd
= vdev_lookup_by_guid(rvd
, guid
)) == NULL
) {
3983 cmn_err(CE_NOTE
, "Can not find the boot vdev for guid %llu",
3984 (u_longlong_t
)guid
);
3985 error
= SET_ERROR(ENOENT
);
3990 * Determine if there is a better boot device.
3993 spa_alt_rootvdev(rvd
, &avd
, &txg
);
3995 cmn_err(CE_NOTE
, "The boot device is 'degraded'. Please "
3996 "try booting from '%s'", avd
->vdev_path
);
3997 error
= SET_ERROR(EINVAL
);
4002 * If the boot device is part of a spare vdev then ensure that
4003 * we're booting off the active spare.
4005 if (bvd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
4006 !bvd
->vdev_isspare
) {
4007 cmn_err(CE_NOTE
, "The boot device is currently spared. Please "
4008 "try booting from '%s'",
4010 vdev_child
[bvd
->vdev_parent
->vdev_children
- 1]->vdev_path
);
4011 error
= SET_ERROR(EINVAL
);
4017 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4019 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4020 mutex_exit(&spa_namespace_lock
);
4022 nvlist_free(config
);
4029 * Import a non-root pool into the system.
4032 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
4035 char *altroot
= NULL
;
4036 spa_load_state_t state
= SPA_LOAD_IMPORT
;
4037 zpool_rewind_policy_t policy
;
4038 uint64_t mode
= spa_mode_global
;
4039 uint64_t readonly
= B_FALSE
;
4042 nvlist_t
**spares
, **l2cache
;
4043 uint_t nspares
, nl2cache
;
4046 * If a pool with this name exists, return failure.
4048 mutex_enter(&spa_namespace_lock
);
4049 if (spa_lookup(pool
) != NULL
) {
4050 mutex_exit(&spa_namespace_lock
);
4051 return (SET_ERROR(EEXIST
));
4055 * Create and initialize the spa structure.
4057 (void) nvlist_lookup_string(props
,
4058 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
4059 (void) nvlist_lookup_uint64(props
,
4060 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
4063 spa
= spa_add(pool
, config
, altroot
);
4064 spa
->spa_import_flags
= flags
;
4067 * Verbatim import - Take a pool and insert it into the namespace
4068 * as if it had been loaded at boot.
4070 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
4072 spa_configfile_set(spa
, props
, B_FALSE
);
4074 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
4076 mutex_exit(&spa_namespace_lock
);
4080 spa_activate(spa
, mode
);
4083 * Don't start async tasks until we know everything is healthy.
4085 spa_async_suspend(spa
);
4087 zpool_get_rewind_policy(config
, &policy
);
4088 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
4089 state
= SPA_LOAD_RECOVER
;
4092 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
4093 * because the user-supplied config is actually the one to trust when
4096 if (state
!= SPA_LOAD_RECOVER
)
4097 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
4099 error
= spa_load_best(spa
, state
, B_TRUE
, policy
.zrp_txg
,
4100 policy
.zrp_request
);
4103 * Propagate anything learned while loading the pool and pass it
4104 * back to caller (i.e. rewind info, missing devices, etc).
4106 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4107 spa
->spa_load_info
) == 0);
4109 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4111 * Toss any existing sparelist, as it doesn't have any validity
4112 * anymore, and conflicts with spa_has_spare().
4114 if (spa
->spa_spares
.sav_config
) {
4115 nvlist_free(spa
->spa_spares
.sav_config
);
4116 spa
->spa_spares
.sav_config
= NULL
;
4117 spa_load_spares(spa
);
4119 if (spa
->spa_l2cache
.sav_config
) {
4120 nvlist_free(spa
->spa_l2cache
.sav_config
);
4121 spa
->spa_l2cache
.sav_config
= NULL
;
4122 spa_load_l2cache(spa
);
4125 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4128 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
4131 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
4132 VDEV_ALLOC_L2CACHE
);
4133 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4136 spa_configfile_set(spa
, props
, B_FALSE
);
4138 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
4139 (error
= spa_prop_set(spa
, props
)))) {
4141 spa_deactivate(spa
);
4143 mutex_exit(&spa_namespace_lock
);
4147 spa_async_resume(spa
);
4150 * Override any spares and level 2 cache devices as specified by
4151 * the user, as these may have correct device names/devids, etc.
4153 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4154 &spares
, &nspares
) == 0) {
4155 if (spa
->spa_spares
.sav_config
)
4156 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
,
4157 ZPOOL_CONFIG_SPARES
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4159 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
,
4160 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4161 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
4162 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
4163 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4164 spa_load_spares(spa
);
4165 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4166 spa
->spa_spares
.sav_sync
= B_TRUE
;
4168 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4169 &l2cache
, &nl2cache
) == 0) {
4170 if (spa
->spa_l2cache
.sav_config
)
4171 VERIFY(nvlist_remove(spa
->spa_l2cache
.sav_config
,
4172 ZPOOL_CONFIG_L2CACHE
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4174 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
4175 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
4176 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
4177 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
4178 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4179 spa_load_l2cache(spa
);
4180 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4181 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4185 * Check for any removed devices.
4187 if (spa
->spa_autoreplace
) {
4188 spa_aux_check_removed(&spa
->spa_spares
);
4189 spa_aux_check_removed(&spa
->spa_l2cache
);
4192 if (spa_writeable(spa
)) {
4194 * Update the config cache to include the newly-imported pool.
4196 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4200 * It's possible that the pool was expanded while it was exported.
4201 * We kick off an async task to handle this for us.
4203 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
4205 mutex_exit(&spa_namespace_lock
);
4206 spa_history_log_version(spa
, "import");
4209 zvol_create_minors(pool
);
4216 spa_tryimport(nvlist_t
*tryconfig
)
4218 nvlist_t
*config
= NULL
;
4224 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
4227 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
4231 * Create and initialize the spa structure.
4233 mutex_enter(&spa_namespace_lock
);
4234 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
4235 spa_activate(spa
, FREAD
);
4238 * Pass off the heavy lifting to spa_load().
4239 * Pass TRUE for mosconfig because the user-supplied config
4240 * is actually the one to trust when doing an import.
4242 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
, B_TRUE
);
4245 * If 'tryconfig' was at least parsable, return the current config.
4247 if (spa
->spa_root_vdev
!= NULL
) {
4248 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4249 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
,
4251 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
4253 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
4254 spa
->spa_uberblock
.ub_timestamp
) == 0);
4255 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4256 spa
->spa_load_info
) == 0);
4257 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
4258 spa
->spa_errata
) == 0);
4261 * If the bootfs property exists on this pool then we
4262 * copy it out so that external consumers can tell which
4263 * pools are bootable.
4265 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
4266 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4269 * We have to play games with the name since the
4270 * pool was opened as TRYIMPORT_NAME.
4272 if (dsl_dsobj_to_dsname(spa_name(spa
),
4273 spa
->spa_bootfs
, tmpname
) == 0) {
4277 dsname
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
4279 cp
= strchr(tmpname
, '/');
4281 (void) strlcpy(dsname
, tmpname
,
4284 (void) snprintf(dsname
, MAXPATHLEN
,
4285 "%s/%s", poolname
, ++cp
);
4287 VERIFY(nvlist_add_string(config
,
4288 ZPOOL_CONFIG_BOOTFS
, dsname
) == 0);
4289 kmem_free(dsname
, MAXPATHLEN
);
4291 kmem_free(tmpname
, MAXPATHLEN
);
4295 * Add the list of hot spares and level 2 cache devices.
4297 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4298 spa_add_spares(spa
, config
);
4299 spa_add_l2cache(spa
, config
);
4300 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4304 spa_deactivate(spa
);
4306 mutex_exit(&spa_namespace_lock
);
4312 * Pool export/destroy
4314 * The act of destroying or exporting a pool is very simple. We make sure there
4315 * is no more pending I/O and any references to the pool are gone. Then, we
4316 * update the pool state and sync all the labels to disk, removing the
4317 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4318 * we don't sync the labels or remove the configuration cache.
4321 spa_export_common(char *pool
, int new_state
, nvlist_t
**oldconfig
,
4322 boolean_t force
, boolean_t hardforce
)
4329 if (!(spa_mode_global
& FWRITE
))
4330 return (SET_ERROR(EROFS
));
4332 mutex_enter(&spa_namespace_lock
);
4333 if ((spa
= spa_lookup(pool
)) == NULL
) {
4334 mutex_exit(&spa_namespace_lock
);
4335 return (SET_ERROR(ENOENT
));
4339 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4340 * reacquire the namespace lock, and see if we can export.
4342 spa_open_ref(spa
, FTAG
);
4343 mutex_exit(&spa_namespace_lock
);
4344 spa_async_suspend(spa
);
4345 mutex_enter(&spa_namespace_lock
);
4346 spa_close(spa
, FTAG
);
4348 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
)
4351 * The pool will be in core if it's openable, in which case we can
4352 * modify its state. Objsets may be open only because they're dirty,
4353 * so we have to force it to sync before checking spa_refcnt.
4355 if (spa
->spa_sync_on
) {
4356 txg_wait_synced(spa
->spa_dsl_pool
, 0);
4357 spa_evicting_os_wait(spa
);
4361 * A pool cannot be exported or destroyed if there are active
4362 * references. If we are resetting a pool, allow references by
4363 * fault injection handlers.
4365 if (!spa_refcount_zero(spa
) ||
4366 (spa
->spa_inject_ref
!= 0 &&
4367 new_state
!= POOL_STATE_UNINITIALIZED
)) {
4368 spa_async_resume(spa
);
4369 mutex_exit(&spa_namespace_lock
);
4370 return (SET_ERROR(EBUSY
));
4373 if (spa
->spa_sync_on
) {
4375 * A pool cannot be exported if it has an active shared spare.
4376 * This is to prevent other pools stealing the active spare
4377 * from an exported pool. At user's own will, such pool can
4378 * be forcedly exported.
4380 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
4381 spa_has_active_shared_spare(spa
)) {
4382 spa_async_resume(spa
);
4383 mutex_exit(&spa_namespace_lock
);
4384 return (SET_ERROR(EXDEV
));
4388 * We want this to be reflected on every label,
4389 * so mark them all dirty. spa_unload() will do the
4390 * final sync that pushes these changes out.
4392 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
4393 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4394 spa
->spa_state
= new_state
;
4395 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
4397 vdev_config_dirty(spa
->spa_root_vdev
);
4398 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4403 spa_event_notify(spa
, NULL
, FM_EREPORT_ZFS_POOL_DESTROY
);
4405 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
4407 spa_deactivate(spa
);
4410 if (oldconfig
&& spa
->spa_config
)
4411 VERIFY(nvlist_dup(spa
->spa_config
, oldconfig
, 0) == 0);
4413 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
4415 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
4418 mutex_exit(&spa_namespace_lock
);
4424 * Destroy a storage pool.
4427 spa_destroy(char *pool
)
4429 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
4434 * Export a storage pool.
4437 spa_export(char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
4438 boolean_t hardforce
)
4440 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
4445 * Similar to spa_export(), this unloads the spa_t without actually removing it
4446 * from the namespace in any way.
4449 spa_reset(char *pool
)
4451 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
4456 * ==========================================================================
4457 * Device manipulation
4458 * ==========================================================================
4462 * Add a device to a storage pool.
4465 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
4469 vdev_t
*rvd
= spa
->spa_root_vdev
;
4471 nvlist_t
**spares
, **l2cache
;
4472 uint_t nspares
, nl2cache
;
4475 ASSERT(spa_writeable(spa
));
4477 txg
= spa_vdev_enter(spa
);
4479 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
4480 VDEV_ALLOC_ADD
)) != 0)
4481 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4483 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
4485 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
4489 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
4493 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
4494 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
4496 if (vd
->vdev_children
!= 0 &&
4497 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0)
4498 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4501 * We must validate the spares and l2cache devices after checking the
4502 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4504 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
4505 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4508 * Transfer each new top-level vdev from vd to rvd.
4510 for (c
= 0; c
< vd
->vdev_children
; c
++) {
4513 * Set the vdev id to the first hole, if one exists.
4515 for (id
= 0; id
< rvd
->vdev_children
; id
++) {
4516 if (rvd
->vdev_child
[id
]->vdev_ishole
) {
4517 vdev_free(rvd
->vdev_child
[id
]);
4521 tvd
= vd
->vdev_child
[c
];
4522 vdev_remove_child(vd
, tvd
);
4524 vdev_add_child(rvd
, tvd
);
4525 vdev_config_dirty(tvd
);
4529 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
4530 ZPOOL_CONFIG_SPARES
);
4531 spa_load_spares(spa
);
4532 spa
->spa_spares
.sav_sync
= B_TRUE
;
4535 if (nl2cache
!= 0) {
4536 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
4537 ZPOOL_CONFIG_L2CACHE
);
4538 spa_load_l2cache(spa
);
4539 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4543 * We have to be careful when adding new vdevs to an existing pool.
4544 * If other threads start allocating from these vdevs before we
4545 * sync the config cache, and we lose power, then upon reboot we may
4546 * fail to open the pool because there are DVAs that the config cache
4547 * can't translate. Therefore, we first add the vdevs without
4548 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4549 * and then let spa_config_update() initialize the new metaslabs.
4551 * spa_load() checks for added-but-not-initialized vdevs, so that
4552 * if we lose power at any point in this sequence, the remaining
4553 * steps will be completed the next time we load the pool.
4555 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
4557 mutex_enter(&spa_namespace_lock
);
4558 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4559 mutex_exit(&spa_namespace_lock
);
4565 * Attach a device to a mirror. The arguments are the path to any device
4566 * in the mirror, and the nvroot for the new device. If the path specifies
4567 * a device that is not mirrored, we automatically insert the mirror vdev.
4569 * If 'replacing' is specified, the new device is intended to replace the
4570 * existing device; in this case the two devices are made into their own
4571 * mirror using the 'replacing' vdev, which is functionally identical to
4572 * the mirror vdev (it actually reuses all the same ops) but has a few
4573 * extra rules: you can't attach to it after it's been created, and upon
4574 * completion of resilvering, the first disk (the one being replaced)
4575 * is automatically detached.
4578 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
)
4580 uint64_t txg
, dtl_max_txg
;
4581 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
4583 char *oldvdpath
, *newvdpath
;
4586 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
4588 ASSERT(spa_writeable(spa
));
4590 txg
= spa_vdev_enter(spa
);
4592 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4595 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4597 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
4598 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4600 pvd
= oldvd
->vdev_parent
;
4602 if ((error
= spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
4603 VDEV_ALLOC_ATTACH
)) != 0)
4604 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4606 if (newrootvd
->vdev_children
!= 1)
4607 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4609 newvd
= newrootvd
->vdev_child
[0];
4611 if (!newvd
->vdev_ops
->vdev_op_leaf
)
4612 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4614 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
4615 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
4618 * Spares can't replace logs
4620 if (oldvd
->vdev_top
->vdev_islog
&& newvd
->vdev_isspare
)
4621 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4625 * For attach, the only allowable parent is a mirror or the root
4628 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4629 pvd
->vdev_ops
!= &vdev_root_ops
)
4630 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4632 pvops
= &vdev_mirror_ops
;
4635 * Active hot spares can only be replaced by inactive hot
4638 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4639 oldvd
->vdev_isspare
&&
4640 !spa_has_spare(spa
, newvd
->vdev_guid
))
4641 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4644 * If the source is a hot spare, and the parent isn't already a
4645 * spare, then we want to create a new hot spare. Otherwise, we
4646 * want to create a replacing vdev. The user is not allowed to
4647 * attach to a spared vdev child unless the 'isspare' state is
4648 * the same (spare replaces spare, non-spare replaces
4651 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
4652 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
4653 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4654 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4655 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
4656 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4659 if (newvd
->vdev_isspare
)
4660 pvops
= &vdev_spare_ops
;
4662 pvops
= &vdev_replacing_ops
;
4666 * Make sure the new device is big enough.
4668 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
4669 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
4672 * The new device cannot have a higher alignment requirement
4673 * than the top-level vdev.
4675 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
4676 return (spa_vdev_exit(spa
, newrootvd
, txg
, EDOM
));
4679 * If this is an in-place replacement, update oldvd's path and devid
4680 * to make it distinguishable from newvd, and unopenable from now on.
4682 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
4683 spa_strfree(oldvd
->vdev_path
);
4684 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
4686 (void) sprintf(oldvd
->vdev_path
, "%s/%s",
4687 newvd
->vdev_path
, "old");
4688 if (oldvd
->vdev_devid
!= NULL
) {
4689 spa_strfree(oldvd
->vdev_devid
);
4690 oldvd
->vdev_devid
= NULL
;
4694 /* mark the device being resilvered */
4695 newvd
->vdev_resilver_txg
= txg
;
4698 * If the parent is not a mirror, or if we're replacing, insert the new
4699 * mirror/replacing/spare vdev above oldvd.
4701 if (pvd
->vdev_ops
!= pvops
)
4702 pvd
= vdev_add_parent(oldvd
, pvops
);
4704 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
4705 ASSERT(pvd
->vdev_ops
== pvops
);
4706 ASSERT(oldvd
->vdev_parent
== pvd
);
4709 * Extract the new device from its root and add it to pvd.
4711 vdev_remove_child(newrootvd
, newvd
);
4712 newvd
->vdev_id
= pvd
->vdev_children
;
4713 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
4714 vdev_add_child(pvd
, newvd
);
4716 tvd
= newvd
->vdev_top
;
4717 ASSERT(pvd
->vdev_top
== tvd
);
4718 ASSERT(tvd
->vdev_parent
== rvd
);
4720 vdev_config_dirty(tvd
);
4723 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4724 * for any dmu_sync-ed blocks. It will propagate upward when
4725 * spa_vdev_exit() calls vdev_dtl_reassess().
4727 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
4729 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
4730 dtl_max_txg
- TXG_INITIAL
);
4732 if (newvd
->vdev_isspare
) {
4733 spa_spare_activate(newvd
);
4734 spa_event_notify(spa
, newvd
, FM_EREPORT_ZFS_DEVICE_SPARE
);
4737 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
4738 newvdpath
= spa_strdup(newvd
->vdev_path
);
4739 newvd_isspare
= newvd
->vdev_isspare
;
4742 * Mark newvd's DTL dirty in this txg.
4744 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
4747 * Schedule the resilver to restart in the future. We do this to
4748 * ensure that dmu_sync-ed blocks have been stitched into the
4749 * respective datasets.
4751 dsl_resilver_restart(spa
->spa_dsl_pool
, dtl_max_txg
);
4756 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
4758 spa_history_log_internal(spa
, "vdev attach", NULL
,
4759 "%s vdev=%s %s vdev=%s",
4760 replacing
&& newvd_isspare
? "spare in" :
4761 replacing
? "replace" : "attach", newvdpath
,
4762 replacing
? "for" : "to", oldvdpath
);
4764 spa_strfree(oldvdpath
);
4765 spa_strfree(newvdpath
);
4767 if (spa
->spa_bootfs
)
4768 spa_event_notify(spa
, newvd
, FM_EREPORT_ZFS_BOOTFS_VDEV_ATTACH
);
4774 * Detach a device from a mirror or replacing vdev.
4776 * If 'replace_done' is specified, only detach if the parent
4777 * is a replacing vdev.
4780 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
4784 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
4785 boolean_t unspare
= B_FALSE
;
4786 uint64_t unspare_guid
= 0;
4789 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
4790 ASSERT(spa_writeable(spa
));
4792 txg
= spa_vdev_enter(spa
);
4794 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4797 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4799 if (!vd
->vdev_ops
->vdev_op_leaf
)
4800 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4802 pvd
= vd
->vdev_parent
;
4805 * If the parent/child relationship is not as expected, don't do it.
4806 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4807 * vdev that's replacing B with C. The user's intent in replacing
4808 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4809 * the replace by detaching C, the expected behavior is to end up
4810 * M(A,B). But suppose that right after deciding to detach C,
4811 * the replacement of B completes. We would have M(A,C), and then
4812 * ask to detach C, which would leave us with just A -- not what
4813 * the user wanted. To prevent this, we make sure that the
4814 * parent/child relationship hasn't changed -- in this example,
4815 * that C's parent is still the replacing vdev R.
4817 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
4818 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4821 * Only 'replacing' or 'spare' vdevs can be replaced.
4823 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4824 pvd
->vdev_ops
!= &vdev_spare_ops
)
4825 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4827 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
4828 spa_version(spa
) >= SPA_VERSION_SPARES
);
4831 * Only mirror, replacing, and spare vdevs support detach.
4833 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4834 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4835 pvd
->vdev_ops
!= &vdev_spare_ops
)
4836 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4839 * If this device has the only valid copy of some data,
4840 * we cannot safely detach it.
4842 if (vdev_dtl_required(vd
))
4843 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4845 ASSERT(pvd
->vdev_children
>= 2);
4848 * If we are detaching the second disk from a replacing vdev, then
4849 * check to see if we changed the original vdev's path to have "/old"
4850 * at the end in spa_vdev_attach(). If so, undo that change now.
4852 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
4853 vd
->vdev_path
!= NULL
) {
4854 size_t len
= strlen(vd
->vdev_path
);
4856 for (c
= 0; c
< pvd
->vdev_children
; c
++) {
4857 cvd
= pvd
->vdev_child
[c
];
4859 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
4862 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
4863 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
4864 spa_strfree(cvd
->vdev_path
);
4865 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
4872 * If we are detaching the original disk from a spare, then it implies
4873 * that the spare should become a real disk, and be removed from the
4874 * active spare list for the pool.
4876 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4878 pvd
->vdev_child
[pvd
->vdev_children
- 1]->vdev_isspare
)
4882 * Erase the disk labels so the disk can be used for other things.
4883 * This must be done after all other error cases are handled,
4884 * but before we disembowel vd (so we can still do I/O to it).
4885 * But if we can't do it, don't treat the error as fatal --
4886 * it may be that the unwritability of the disk is the reason
4887 * it's being detached!
4889 error
= vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
4892 * Remove vd from its parent and compact the parent's children.
4894 vdev_remove_child(pvd
, vd
);
4895 vdev_compact_children(pvd
);
4898 * Remember one of the remaining children so we can get tvd below.
4900 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
4903 * If we need to remove the remaining child from the list of hot spares,
4904 * do it now, marking the vdev as no longer a spare in the process.
4905 * We must do this before vdev_remove_parent(), because that can
4906 * change the GUID if it creates a new toplevel GUID. For a similar
4907 * reason, we must remove the spare now, in the same txg as the detach;
4908 * otherwise someone could attach a new sibling, change the GUID, and
4909 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4912 ASSERT(cvd
->vdev_isspare
);
4913 spa_spare_remove(cvd
);
4914 unspare_guid
= cvd
->vdev_guid
;
4915 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
4916 cvd
->vdev_unspare
= B_TRUE
;
4920 * If the parent mirror/replacing vdev only has one child,
4921 * the parent is no longer needed. Remove it from the tree.
4923 if (pvd
->vdev_children
== 1) {
4924 if (pvd
->vdev_ops
== &vdev_spare_ops
)
4925 cvd
->vdev_unspare
= B_FALSE
;
4926 vdev_remove_parent(cvd
);
4931 * We don't set tvd until now because the parent we just removed
4932 * may have been the previous top-level vdev.
4934 tvd
= cvd
->vdev_top
;
4935 ASSERT(tvd
->vdev_parent
== rvd
);
4938 * Reevaluate the parent vdev state.
4940 vdev_propagate_state(cvd
);
4943 * If the 'autoexpand' property is set on the pool then automatically
4944 * try to expand the size of the pool. For example if the device we
4945 * just detached was smaller than the others, it may be possible to
4946 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4947 * first so that we can obtain the updated sizes of the leaf vdevs.
4949 if (spa
->spa_autoexpand
) {
4951 vdev_expand(tvd
, txg
);
4954 vdev_config_dirty(tvd
);
4957 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4958 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4959 * But first make sure we're not on any *other* txg's DTL list, to
4960 * prevent vd from being accessed after it's freed.
4962 vdpath
= spa_strdup(vd
->vdev_path
);
4963 for (t
= 0; t
< TXG_SIZE
; t
++)
4964 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
4965 vd
->vdev_detached
= B_TRUE
;
4966 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
4968 spa_event_notify(spa
, vd
, FM_EREPORT_ZFS_DEVICE_REMOVE
);
4970 /* hang on to the spa before we release the lock */
4971 spa_open_ref(spa
, FTAG
);
4973 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
4975 spa_history_log_internal(spa
, "detach", NULL
,
4977 spa_strfree(vdpath
);
4980 * If this was the removal of the original device in a hot spare vdev,
4981 * then we want to go through and remove the device from the hot spare
4982 * list of every other pool.
4985 spa_t
*altspa
= NULL
;
4987 mutex_enter(&spa_namespace_lock
);
4988 while ((altspa
= spa_next(altspa
)) != NULL
) {
4989 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
4993 spa_open_ref(altspa
, FTAG
);
4994 mutex_exit(&spa_namespace_lock
);
4995 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
4996 mutex_enter(&spa_namespace_lock
);
4997 spa_close(altspa
, FTAG
);
4999 mutex_exit(&spa_namespace_lock
);
5001 /* search the rest of the vdevs for spares to remove */
5002 spa_vdev_resilver_done(spa
);
5005 /* all done with the spa; OK to release */
5006 mutex_enter(&spa_namespace_lock
);
5007 spa_close(spa
, FTAG
);
5008 mutex_exit(&spa_namespace_lock
);
5014 * Split a set of devices from their mirrors, and create a new pool from them.
5017 spa_vdev_split_mirror(spa_t
*spa
, char *newname
, nvlist_t
*config
,
5018 nvlist_t
*props
, boolean_t exp
)
5021 uint64_t txg
, *glist
;
5023 uint_t c
, children
, lastlog
;
5024 nvlist_t
**child
, *nvl
, *tmp
;
5026 char *altroot
= NULL
;
5027 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
5028 boolean_t activate_slog
;
5030 ASSERT(spa_writeable(spa
));
5032 txg
= spa_vdev_enter(spa
);
5034 /* clear the log and flush everything up to now */
5035 activate_slog
= spa_passivate_log(spa
);
5036 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5037 error
= spa_offline_log(spa
);
5038 txg
= spa_vdev_config_enter(spa
);
5041 spa_activate_log(spa
);
5044 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5046 /* check new spa name before going any further */
5047 if (spa_lookup(newname
) != NULL
)
5048 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
5051 * scan through all the children to ensure they're all mirrors
5053 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
5054 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
5056 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5058 /* first, check to ensure we've got the right child count */
5059 rvd
= spa
->spa_root_vdev
;
5061 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
5062 vdev_t
*vd
= rvd
->vdev_child
[c
];
5064 /* don't count the holes & logs as children */
5065 if (vd
->vdev_islog
|| vd
->vdev_ishole
) {
5073 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
5074 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5076 /* next, ensure no spare or cache devices are part of the split */
5077 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
5078 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
5079 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
5081 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_SLEEP
);
5082 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_SLEEP
);
5084 /* then, loop over each vdev and validate it */
5085 for (c
= 0; c
< children
; c
++) {
5086 uint64_t is_hole
= 0;
5088 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
5092 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
5093 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
5096 error
= SET_ERROR(EINVAL
);
5101 /* which disk is going to be split? */
5102 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
5104 error
= SET_ERROR(EINVAL
);
5108 /* look it up in the spa */
5109 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
5110 if (vml
[c
] == NULL
) {
5111 error
= SET_ERROR(ENODEV
);
5115 /* make sure there's nothing stopping the split */
5116 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
5117 vml
[c
]->vdev_islog
||
5118 vml
[c
]->vdev_ishole
||
5119 vml
[c
]->vdev_isspare
||
5120 vml
[c
]->vdev_isl2cache
||
5121 !vdev_writeable(vml
[c
]) ||
5122 vml
[c
]->vdev_children
!= 0 ||
5123 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
5124 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
5125 error
= SET_ERROR(EINVAL
);
5129 if (vdev_dtl_required(vml
[c
])) {
5130 error
= SET_ERROR(EBUSY
);
5134 /* we need certain info from the top level */
5135 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
5136 vml
[c
]->vdev_top
->vdev_ms_array
) == 0);
5137 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
5138 vml
[c
]->vdev_top
->vdev_ms_shift
) == 0);
5139 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
5140 vml
[c
]->vdev_top
->vdev_asize
) == 0);
5141 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
5142 vml
[c
]->vdev_top
->vdev_ashift
) == 0);
5146 kmem_free(vml
, children
* sizeof (vdev_t
*));
5147 kmem_free(glist
, children
* sizeof (uint64_t));
5148 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5151 /* stop writers from using the disks */
5152 for (c
= 0; c
< children
; c
++) {
5154 vml
[c
]->vdev_offline
= B_TRUE
;
5156 vdev_reopen(spa
->spa_root_vdev
);
5159 * Temporarily record the splitting vdevs in the spa config. This
5160 * will disappear once the config is regenerated.
5162 VERIFY(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5163 VERIFY(nvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
5164 glist
, children
) == 0);
5165 kmem_free(glist
, children
* sizeof (uint64_t));
5167 mutex_enter(&spa
->spa_props_lock
);
5168 VERIFY(nvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
,
5170 mutex_exit(&spa
->spa_props_lock
);
5171 spa
->spa_config_splitting
= nvl
;
5172 vdev_config_dirty(spa
->spa_root_vdev
);
5174 /* configure and create the new pool */
5175 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
) == 0);
5176 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
5177 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
) == 0);
5178 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
5179 spa_version(spa
)) == 0);
5180 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
5181 spa
->spa_config_txg
) == 0);
5182 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
5183 spa_generate_guid(NULL
)) == 0);
5184 (void) nvlist_lookup_string(props
,
5185 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5187 /* add the new pool to the namespace */
5188 newspa
= spa_add(newname
, config
, altroot
);
5189 newspa
->spa_config_txg
= spa
->spa_config_txg
;
5190 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
5192 /* release the spa config lock, retaining the namespace lock */
5193 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5195 if (zio_injection_enabled
)
5196 zio_handle_panic_injection(spa
, FTAG
, 1);
5198 spa_activate(newspa
, spa_mode_global
);
5199 spa_async_suspend(newspa
);
5201 /* create the new pool from the disks of the original pool */
5202 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
, B_TRUE
);
5206 /* if that worked, generate a real config for the new pool */
5207 if (newspa
->spa_root_vdev
!= NULL
) {
5208 VERIFY(nvlist_alloc(&newspa
->spa_config_splitting
,
5209 NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
5210 VERIFY(nvlist_add_uint64(newspa
->spa_config_splitting
,
5211 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
)) == 0);
5212 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
5217 if (props
!= NULL
) {
5218 spa_configfile_set(newspa
, props
, B_FALSE
);
5219 error
= spa_prop_set(newspa
, props
);
5224 /* flush everything */
5225 txg
= spa_vdev_config_enter(newspa
);
5226 vdev_config_dirty(newspa
->spa_root_vdev
);
5227 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
5229 if (zio_injection_enabled
)
5230 zio_handle_panic_injection(spa
, FTAG
, 2);
5232 spa_async_resume(newspa
);
5234 /* finally, update the original pool's config */
5235 txg
= spa_vdev_config_enter(spa
);
5236 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
5237 error
= dmu_tx_assign(tx
, TXG_WAIT
);
5240 for (c
= 0; c
< children
; c
++) {
5241 if (vml
[c
] != NULL
) {
5244 spa_history_log_internal(spa
, "detach", tx
,
5245 "vdev=%s", vml
[c
]->vdev_path
);
5249 vdev_config_dirty(spa
->spa_root_vdev
);
5250 spa
->spa_config_splitting
= NULL
;
5254 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
5256 if (zio_injection_enabled
)
5257 zio_handle_panic_injection(spa
, FTAG
, 3);
5259 /* split is complete; log a history record */
5260 spa_history_log_internal(newspa
, "split", NULL
,
5261 "from pool %s", spa_name(spa
));
5263 kmem_free(vml
, children
* sizeof (vdev_t
*));
5265 /* if we're not going to mount the filesystems in userland, export */
5267 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
5274 spa_deactivate(newspa
);
5277 txg
= spa_vdev_config_enter(spa
);
5279 /* re-online all offlined disks */
5280 for (c
= 0; c
< children
; c
++) {
5282 vml
[c
]->vdev_offline
= B_FALSE
;
5284 vdev_reopen(spa
->spa_root_vdev
);
5286 nvlist_free(spa
->spa_config_splitting
);
5287 spa
->spa_config_splitting
= NULL
;
5288 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
5290 kmem_free(vml
, children
* sizeof (vdev_t
*));
5295 spa_nvlist_lookup_by_guid(nvlist_t
**nvpp
, int count
, uint64_t target_guid
)
5299 for (i
= 0; i
< count
; i
++) {
5302 VERIFY(nvlist_lookup_uint64(nvpp
[i
], ZPOOL_CONFIG_GUID
,
5305 if (guid
== target_guid
)
5313 spa_vdev_remove_aux(nvlist_t
*config
, char *name
, nvlist_t
**dev
, int count
,
5314 nvlist_t
*dev_to_remove
)
5316 nvlist_t
**newdev
= NULL
;
5320 newdev
= kmem_alloc((count
- 1) * sizeof (void *), KM_SLEEP
);
5322 for (i
= 0, j
= 0; i
< count
; i
++) {
5323 if (dev
[i
] == dev_to_remove
)
5325 VERIFY(nvlist_dup(dev
[i
], &newdev
[j
++], KM_SLEEP
) == 0);
5328 VERIFY(nvlist_remove(config
, name
, DATA_TYPE_NVLIST_ARRAY
) == 0);
5329 VERIFY(nvlist_add_nvlist_array(config
, name
, newdev
, count
- 1) == 0);
5331 for (i
= 0; i
< count
- 1; i
++)
5332 nvlist_free(newdev
[i
]);
5335 kmem_free(newdev
, (count
- 1) * sizeof (void *));
5339 * Evacuate the device.
5342 spa_vdev_remove_evacuate(spa_t
*spa
, vdev_t
*vd
)
5347 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5348 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5349 ASSERT(vd
== vd
->vdev_top
);
5352 * Evacuate the device. We don't hold the config lock as writer
5353 * since we need to do I/O but we do keep the
5354 * spa_namespace_lock held. Once this completes the device
5355 * should no longer have any blocks allocated on it.
5357 if (vd
->vdev_islog
) {
5358 if (vd
->vdev_stat
.vs_alloc
!= 0)
5359 error
= spa_offline_log(spa
);
5361 error
= SET_ERROR(ENOTSUP
);
5368 * The evacuation succeeded. Remove any remaining MOS metadata
5369 * associated with this vdev, and wait for these changes to sync.
5371 ASSERT0(vd
->vdev_stat
.vs_alloc
);
5372 txg
= spa_vdev_config_enter(spa
);
5373 vd
->vdev_removing
= B_TRUE
;
5374 vdev_dirty_leaves(vd
, VDD_DTL
, txg
);
5375 vdev_config_dirty(vd
);
5376 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5382 * Complete the removal by cleaning up the namespace.
5385 spa_vdev_remove_from_namespace(spa_t
*spa
, vdev_t
*vd
)
5387 vdev_t
*rvd
= spa
->spa_root_vdev
;
5388 uint64_t id
= vd
->vdev_id
;
5389 boolean_t last_vdev
= (id
== (rvd
->vdev_children
- 1));
5391 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5392 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5393 ASSERT(vd
== vd
->vdev_top
);
5396 * Only remove any devices which are empty.
5398 if (vd
->vdev_stat
.vs_alloc
!= 0)
5401 (void) vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
5403 if (list_link_active(&vd
->vdev_state_dirty_node
))
5404 vdev_state_clean(vd
);
5405 if (list_link_active(&vd
->vdev_config_dirty_node
))
5406 vdev_config_clean(vd
);
5411 vdev_compact_children(rvd
);
5413 vd
= vdev_alloc_common(spa
, id
, 0, &vdev_hole_ops
);
5414 vdev_add_child(rvd
, vd
);
5416 vdev_config_dirty(rvd
);
5419 * Reassess the health of our root vdev.
5425 * Remove a device from the pool -
5427 * Removing a device from the vdev namespace requires several steps
5428 * and can take a significant amount of time. As a result we use
5429 * the spa_vdev_config_[enter/exit] functions which allow us to
5430 * grab and release the spa_config_lock while still holding the namespace
5431 * lock. During each step the configuration is synced out.
5433 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5437 spa_vdev_remove(spa_t
*spa
, uint64_t guid
, boolean_t unspare
)
5440 metaslab_group_t
*mg
;
5441 nvlist_t
**spares
, **l2cache
, *nv
;
5443 uint_t nspares
, nl2cache
;
5445 boolean_t locked
= MUTEX_HELD(&spa_namespace_lock
);
5447 ASSERT(spa_writeable(spa
));
5450 txg
= spa_vdev_enter(spa
);
5452 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
5454 if (spa
->spa_spares
.sav_vdevs
!= NULL
&&
5455 nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
5456 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0 &&
5457 (nv
= spa_nvlist_lookup_by_guid(spares
, nspares
, guid
)) != NULL
) {
5459 * Only remove the hot spare if it's not currently in use
5462 if (vd
== NULL
|| unspare
) {
5463 spa_vdev_remove_aux(spa
->spa_spares
.sav_config
,
5464 ZPOOL_CONFIG_SPARES
, spares
, nspares
, nv
);
5465 spa_load_spares(spa
);
5466 spa
->spa_spares
.sav_sync
= B_TRUE
;
5468 error
= SET_ERROR(EBUSY
);
5470 } else if (spa
->spa_l2cache
.sav_vdevs
!= NULL
&&
5471 nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
5472 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0 &&
5473 (nv
= spa_nvlist_lookup_by_guid(l2cache
, nl2cache
, guid
)) != NULL
) {
5475 * Cache devices can always be removed.
5477 spa_vdev_remove_aux(spa
->spa_l2cache
.sav_config
,
5478 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
, nv
);
5479 spa_load_l2cache(spa
);
5480 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5481 } else if (vd
!= NULL
&& vd
->vdev_islog
) {
5483 ASSERT(vd
== vd
->vdev_top
);
5488 * Stop allocating from this vdev.
5490 metaslab_group_passivate(mg
);
5493 * Wait for the youngest allocations and frees to sync,
5494 * and then wait for the deferral of those frees to finish.
5496 spa_vdev_config_exit(spa
, NULL
,
5497 txg
+ TXG_CONCURRENT_STATES
+ TXG_DEFER_SIZE
, 0, FTAG
);
5500 * Attempt to evacuate the vdev.
5502 error
= spa_vdev_remove_evacuate(spa
, vd
);
5504 txg
= spa_vdev_config_enter(spa
);
5507 * If we couldn't evacuate the vdev, unwind.
5510 metaslab_group_activate(mg
);
5511 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5515 * Clean up the vdev namespace.
5517 spa_vdev_remove_from_namespace(spa
, vd
);
5519 } else if (vd
!= NULL
) {
5521 * Normal vdevs cannot be removed (yet).
5523 error
= SET_ERROR(ENOTSUP
);
5526 * There is no vdev of any kind with the specified guid.
5528 error
= SET_ERROR(ENOENT
);
5532 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5538 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5539 * currently spared, so we can detach it.
5542 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
5544 vdev_t
*newvd
, *oldvd
;
5547 for (c
= 0; c
< vd
->vdev_children
; c
++) {
5548 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
5554 * Check for a completed replacement. We always consider the first
5555 * vdev in the list to be the oldest vdev, and the last one to be
5556 * the newest (see spa_vdev_attach() for how that works). In
5557 * the case where the newest vdev is faulted, we will not automatically
5558 * remove it after a resilver completes. This is OK as it will require
5559 * user intervention to determine which disk the admin wishes to keep.
5561 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
5562 ASSERT(vd
->vdev_children
> 1);
5564 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
5565 oldvd
= vd
->vdev_child
[0];
5567 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5568 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5569 !vdev_dtl_required(oldvd
))
5574 * Check for a completed resilver with the 'unspare' flag set.
5576 if (vd
->vdev_ops
== &vdev_spare_ops
) {
5577 vdev_t
*first
= vd
->vdev_child
[0];
5578 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
5580 if (last
->vdev_unspare
) {
5583 } else if (first
->vdev_unspare
) {
5590 if (oldvd
!= NULL
&&
5591 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5592 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5593 !vdev_dtl_required(oldvd
))
5597 * If there are more than two spares attached to a disk,
5598 * and those spares are not required, then we want to
5599 * attempt to free them up now so that they can be used
5600 * by other pools. Once we're back down to a single
5601 * disk+spare, we stop removing them.
5603 if (vd
->vdev_children
> 2) {
5604 newvd
= vd
->vdev_child
[1];
5606 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
5607 vdev_dtl_empty(last
, DTL_MISSING
) &&
5608 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
5609 !vdev_dtl_required(newvd
))
5618 spa_vdev_resilver_done(spa_t
*spa
)
5620 vdev_t
*vd
, *pvd
, *ppvd
;
5621 uint64_t guid
, sguid
, pguid
, ppguid
;
5623 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5625 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
5626 pvd
= vd
->vdev_parent
;
5627 ppvd
= pvd
->vdev_parent
;
5628 guid
= vd
->vdev_guid
;
5629 pguid
= pvd
->vdev_guid
;
5630 ppguid
= ppvd
->vdev_guid
;
5633 * If we have just finished replacing a hot spared device, then
5634 * we need to detach the parent's first child (the original hot
5637 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
5638 ppvd
->vdev_children
== 2) {
5639 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
5640 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
5642 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
5644 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5645 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
5647 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
5649 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5652 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5656 * Update the stored path or FRU for this vdev.
5659 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
5663 boolean_t sync
= B_FALSE
;
5665 ASSERT(spa_writeable(spa
));
5667 spa_vdev_state_enter(spa
, SCL_ALL
);
5669 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
5670 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
5672 if (!vd
->vdev_ops
->vdev_op_leaf
)
5673 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
5676 if (strcmp(value
, vd
->vdev_path
) != 0) {
5677 spa_strfree(vd
->vdev_path
);
5678 vd
->vdev_path
= spa_strdup(value
);
5682 if (vd
->vdev_fru
== NULL
) {
5683 vd
->vdev_fru
= spa_strdup(value
);
5685 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
5686 spa_strfree(vd
->vdev_fru
);
5687 vd
->vdev_fru
= spa_strdup(value
);
5692 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
5696 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
5698 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
5702 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
5704 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
5708 * ==========================================================================
5710 * ==========================================================================
5714 spa_scan_stop(spa_t
*spa
)
5716 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5717 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
5718 return (SET_ERROR(EBUSY
));
5719 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
5723 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
5725 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5727 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
5728 return (SET_ERROR(ENOTSUP
));
5731 * If a resilver was requested, but there is no DTL on a
5732 * writeable leaf device, we have nothing to do.
5734 if (func
== POOL_SCAN_RESILVER
&&
5735 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
5736 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
5740 return (dsl_scan(spa
->spa_dsl_pool
, func
));
5744 * ==========================================================================
5745 * SPA async task processing
5746 * ==========================================================================
5750 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
5754 if (vd
->vdev_remove_wanted
) {
5755 vd
->vdev_remove_wanted
= B_FALSE
;
5756 vd
->vdev_delayed_close
= B_FALSE
;
5757 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
5760 * We want to clear the stats, but we don't want to do a full
5761 * vdev_clear() as that will cause us to throw away
5762 * degraded/faulted state as well as attempt to reopen the
5763 * device, all of which is a waste.
5765 vd
->vdev_stat
.vs_read_errors
= 0;
5766 vd
->vdev_stat
.vs_write_errors
= 0;
5767 vd
->vdev_stat
.vs_checksum_errors
= 0;
5769 vdev_state_dirty(vd
->vdev_top
);
5772 for (c
= 0; c
< vd
->vdev_children
; c
++)
5773 spa_async_remove(spa
, vd
->vdev_child
[c
]);
5777 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
5781 if (vd
->vdev_probe_wanted
) {
5782 vd
->vdev_probe_wanted
= B_FALSE
;
5783 vdev_reopen(vd
); /* vdev_open() does the actual probe */
5786 for (c
= 0; c
< vd
->vdev_children
; c
++)
5787 spa_async_probe(spa
, vd
->vdev_child
[c
]);
5791 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
5795 if (!spa
->spa_autoexpand
)
5798 for (c
= 0; c
< vd
->vdev_children
; c
++) {
5799 vdev_t
*cvd
= vd
->vdev_child
[c
];
5800 spa_async_autoexpand(spa
, cvd
);
5803 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
5806 spa_event_notify(vd
->vdev_spa
, vd
, FM_EREPORT_ZFS_DEVICE_AUTOEXPAND
);
5810 spa_async_thread(spa_t
*spa
)
5814 ASSERT(spa
->spa_sync_on
);
5816 mutex_enter(&spa
->spa_async_lock
);
5817 tasks
= spa
->spa_async_tasks
;
5818 spa
->spa_async_tasks
= 0;
5819 mutex_exit(&spa
->spa_async_lock
);
5822 * See if the config needs to be updated.
5824 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
5825 uint64_t old_space
, new_space
;
5827 mutex_enter(&spa_namespace_lock
);
5828 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
5829 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5830 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
5831 mutex_exit(&spa_namespace_lock
);
5834 * If the pool grew as a result of the config update,
5835 * then log an internal history event.
5837 if (new_space
!= old_space
) {
5838 spa_history_log_internal(spa
, "vdev online", NULL
,
5839 "pool '%s' size: %llu(+%llu)",
5840 spa_name(spa
), new_space
, new_space
- old_space
);
5845 * See if any devices need to be marked REMOVED.
5847 if (tasks
& SPA_ASYNC_REMOVE
) {
5848 spa_vdev_state_enter(spa
, SCL_NONE
);
5849 spa_async_remove(spa
, spa
->spa_root_vdev
);
5850 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
5851 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
5852 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
5853 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
5854 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5857 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
5858 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5859 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
5860 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5864 * See if any devices need to be probed.
5866 if (tasks
& SPA_ASYNC_PROBE
) {
5867 spa_vdev_state_enter(spa
, SCL_NONE
);
5868 spa_async_probe(spa
, spa
->spa_root_vdev
);
5869 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5873 * If any devices are done replacing, detach them.
5875 if (tasks
& SPA_ASYNC_RESILVER_DONE
)
5876 spa_vdev_resilver_done(spa
);
5879 * Kick off a resilver.
5881 if (tasks
& SPA_ASYNC_RESILVER
)
5882 dsl_resilver_restart(spa
->spa_dsl_pool
, 0);
5885 * Let the world know that we're done.
5887 mutex_enter(&spa
->spa_async_lock
);
5888 spa
->spa_async_thread
= NULL
;
5889 cv_broadcast(&spa
->spa_async_cv
);
5890 mutex_exit(&spa
->spa_async_lock
);
5895 spa_async_suspend(spa_t
*spa
)
5897 mutex_enter(&spa
->spa_async_lock
);
5898 spa
->spa_async_suspended
++;
5899 while (spa
->spa_async_thread
!= NULL
)
5900 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
5901 mutex_exit(&spa
->spa_async_lock
);
5905 spa_async_resume(spa_t
*spa
)
5907 mutex_enter(&spa
->spa_async_lock
);
5908 ASSERT(spa
->spa_async_suspended
!= 0);
5909 spa
->spa_async_suspended
--;
5910 mutex_exit(&spa
->spa_async_lock
);
5914 spa_async_dispatch(spa_t
*spa
)
5916 mutex_enter(&spa
->spa_async_lock
);
5917 if (spa
->spa_async_tasks
&& !spa
->spa_async_suspended
&&
5918 spa
->spa_async_thread
== NULL
&&
5919 rootdir
!= NULL
&& !vn_is_readonly(rootdir
))
5920 spa
->spa_async_thread
= thread_create(NULL
, 0,
5921 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
5922 mutex_exit(&spa
->spa_async_lock
);
5926 spa_async_request(spa_t
*spa
, int task
)
5928 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
5929 mutex_enter(&spa
->spa_async_lock
);
5930 spa
->spa_async_tasks
|= task
;
5931 mutex_exit(&spa
->spa_async_lock
);
5935 * ==========================================================================
5936 * SPA syncing routines
5937 * ==========================================================================
5941 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
5944 bpobj_enqueue(bpo
, bp
, tx
);
5949 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
5953 zio_nowait(zio_free_sync(zio
, zio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
5959 * Note: this simple function is not inlined to make it easier to dtrace the
5960 * amount of time spent syncing frees.
5963 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
5965 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
5966 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
5967 VERIFY(zio_wait(zio
) == 0);
5971 * Note: this simple function is not inlined to make it easier to dtrace the
5972 * amount of time spent syncing deferred frees.
5975 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
5977 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
5978 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
5979 spa_free_sync_cb
, zio
, tx
), ==, 0);
5980 VERIFY0(zio_wait(zio
));
5984 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
5986 char *packed
= NULL
;
5991 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
5994 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
5995 * information. This avoids the dmu_buf_will_dirty() path and
5996 * saves us a pre-read to get data we don't actually care about.
5998 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
5999 packed
= vmem_alloc(bufsize
, KM_SLEEP
);
6001 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
6003 bzero(packed
+ nvsize
, bufsize
- nvsize
);
6005 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
6007 vmem_free(packed
, bufsize
);
6009 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
6010 dmu_buf_will_dirty(db
, tx
);
6011 *(uint64_t *)db
->db_data
= nvsize
;
6012 dmu_buf_rele(db
, FTAG
);
6016 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
6017 const char *config
, const char *entry
)
6027 * Update the MOS nvlist describing the list of available devices.
6028 * spa_validate_aux() will have already made sure this nvlist is
6029 * valid and the vdevs are labeled appropriately.
6031 if (sav
->sav_object
== 0) {
6032 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
6033 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
6034 sizeof (uint64_t), tx
);
6035 VERIFY(zap_update(spa
->spa_meta_objset
,
6036 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
6037 &sav
->sav_object
, tx
) == 0);
6040 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
6041 if (sav
->sav_count
== 0) {
6042 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, NULL
, 0) == 0);
6044 list
= kmem_alloc(sav
->sav_count
*sizeof (void *), KM_SLEEP
);
6045 for (i
= 0; i
< sav
->sav_count
; i
++)
6046 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
6047 B_FALSE
, VDEV_CONFIG_L2CACHE
);
6048 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, list
,
6049 sav
->sav_count
) == 0);
6050 for (i
= 0; i
< sav
->sav_count
; i
++)
6051 nvlist_free(list
[i
]);
6052 kmem_free(list
, sav
->sav_count
* sizeof (void *));
6055 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
6056 nvlist_free(nvroot
);
6058 sav
->sav_sync
= B_FALSE
;
6062 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
6066 if (list_is_empty(&spa
->spa_config_dirty_list
))
6069 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6071 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
6072 dmu_tx_get_txg(tx
), B_FALSE
);
6075 * If we're upgrading the spa version then make sure that
6076 * the config object gets updated with the correct version.
6078 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
6079 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
6080 spa
->spa_uberblock
.ub_version
);
6082 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6084 if (spa
->spa_config_syncing
)
6085 nvlist_free(spa
->spa_config_syncing
);
6086 spa
->spa_config_syncing
= config
;
6088 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
6092 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
6094 uint64_t *versionp
= arg
;
6095 uint64_t version
= *versionp
;
6096 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6099 * Setting the version is special cased when first creating the pool.
6101 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
6103 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
6104 ASSERT(version
>= spa_version(spa
));
6106 spa
->spa_uberblock
.ub_version
= version
;
6107 vdev_config_dirty(spa
->spa_root_vdev
);
6108 spa_history_log_internal(spa
, "set", tx
, "version=%lld", version
);
6112 * Set zpool properties.
6115 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
6117 nvlist_t
*nvp
= arg
;
6118 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6119 objset_t
*mos
= spa
->spa_meta_objset
;
6120 nvpair_t
*elem
= NULL
;
6122 mutex_enter(&spa
->spa_props_lock
);
6124 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
6126 char *strval
, *fname
;
6128 const char *propname
;
6129 zprop_type_t proptype
;
6132 prop
= zpool_name_to_prop(nvpair_name(elem
));
6133 switch ((int)prop
) {
6136 * We checked this earlier in spa_prop_validate().
6138 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
6140 fname
= strchr(nvpair_name(elem
), '@') + 1;
6141 VERIFY0(zfeature_lookup_name(fname
, &fid
));
6143 spa_feature_enable(spa
, fid
, tx
);
6144 spa_history_log_internal(spa
, "set", tx
,
6145 "%s=enabled", nvpair_name(elem
));
6148 case ZPOOL_PROP_VERSION
:
6149 intval
= fnvpair_value_uint64(elem
);
6151 * The version is synced seperatly before other
6152 * properties and should be correct by now.
6154 ASSERT3U(spa_version(spa
), >=, intval
);
6157 case ZPOOL_PROP_ALTROOT
:
6159 * 'altroot' is a non-persistent property. It should
6160 * have been set temporarily at creation or import time.
6162 ASSERT(spa
->spa_root
!= NULL
);
6165 case ZPOOL_PROP_READONLY
:
6166 case ZPOOL_PROP_CACHEFILE
:
6168 * 'readonly' and 'cachefile' are also non-persisitent
6172 case ZPOOL_PROP_COMMENT
:
6173 strval
= fnvpair_value_string(elem
);
6174 if (spa
->spa_comment
!= NULL
)
6175 spa_strfree(spa
->spa_comment
);
6176 spa
->spa_comment
= spa_strdup(strval
);
6178 * We need to dirty the configuration on all the vdevs
6179 * so that their labels get updated. It's unnecessary
6180 * to do this for pool creation since the vdev's
6181 * configuratoin has already been dirtied.
6183 if (tx
->tx_txg
!= TXG_INITIAL
)
6184 vdev_config_dirty(spa
->spa_root_vdev
);
6185 spa_history_log_internal(spa
, "set", tx
,
6186 "%s=%s", nvpair_name(elem
), strval
);
6190 * Set pool property values in the poolprops mos object.
6192 if (spa
->spa_pool_props_object
== 0) {
6193 spa
->spa_pool_props_object
=
6194 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
6195 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
6199 /* normalize the property name */
6200 propname
= zpool_prop_to_name(prop
);
6201 proptype
= zpool_prop_get_type(prop
);
6203 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
6204 ASSERT(proptype
== PROP_TYPE_STRING
);
6205 strval
= fnvpair_value_string(elem
);
6206 VERIFY0(zap_update(mos
,
6207 spa
->spa_pool_props_object
, propname
,
6208 1, strlen(strval
) + 1, strval
, tx
));
6209 spa_history_log_internal(spa
, "set", tx
,
6210 "%s=%s", nvpair_name(elem
), strval
);
6211 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
6212 intval
= fnvpair_value_uint64(elem
);
6214 if (proptype
== PROP_TYPE_INDEX
) {
6216 VERIFY0(zpool_prop_index_to_string(
6217 prop
, intval
, &unused
));
6219 VERIFY0(zap_update(mos
,
6220 spa
->spa_pool_props_object
, propname
,
6221 8, 1, &intval
, tx
));
6222 spa_history_log_internal(spa
, "set", tx
,
6223 "%s=%lld", nvpair_name(elem
), intval
);
6225 ASSERT(0); /* not allowed */
6229 case ZPOOL_PROP_DELEGATION
:
6230 spa
->spa_delegation
= intval
;
6232 case ZPOOL_PROP_BOOTFS
:
6233 spa
->spa_bootfs
= intval
;
6235 case ZPOOL_PROP_FAILUREMODE
:
6236 spa
->spa_failmode
= intval
;
6238 case ZPOOL_PROP_AUTOEXPAND
:
6239 spa
->spa_autoexpand
= intval
;
6240 if (tx
->tx_txg
!= TXG_INITIAL
)
6241 spa_async_request(spa
,
6242 SPA_ASYNC_AUTOEXPAND
);
6244 case ZPOOL_PROP_DEDUPDITTO
:
6245 spa
->spa_dedup_ditto
= intval
;
6254 mutex_exit(&spa
->spa_props_lock
);
6258 * Perform one-time upgrade on-disk changes. spa_version() does not
6259 * reflect the new version this txg, so there must be no changes this
6260 * txg to anything that the upgrade code depends on after it executes.
6261 * Therefore this must be called after dsl_pool_sync() does the sync
6265 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
6267 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6269 ASSERT(spa
->spa_sync_pass
== 1);
6271 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
6273 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
6274 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
6275 dsl_pool_create_origin(dp
, tx
);
6277 /* Keeping the origin open increases spa_minref */
6278 spa
->spa_minref
+= 3;
6281 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
6282 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
6283 dsl_pool_upgrade_clones(dp
, tx
);
6286 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
6287 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
6288 dsl_pool_upgrade_dir_clones(dp
, tx
);
6290 /* Keeping the freedir open increases spa_minref */
6291 spa
->spa_minref
+= 3;
6294 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
6295 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6296 spa_feature_create_zap_objects(spa
, tx
);
6300 * LZ4_COMPRESS feature's behaviour was changed to activate_on_enable
6301 * when possibility to use lz4 compression for metadata was added
6302 * Old pools that have this feature enabled must be upgraded to have
6303 * this feature active
6305 if (spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6306 boolean_t lz4_en
= spa_feature_is_enabled(spa
,
6307 SPA_FEATURE_LZ4_COMPRESS
);
6308 boolean_t lz4_ac
= spa_feature_is_active(spa
,
6309 SPA_FEATURE_LZ4_COMPRESS
);
6311 if (lz4_en
&& !lz4_ac
)
6312 spa_feature_incr(spa
, SPA_FEATURE_LZ4_COMPRESS
, tx
);
6314 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
6318 * Sync the specified transaction group. New blocks may be dirtied as
6319 * part of the process, so we iterate until it converges.
6322 spa_sync(spa_t
*spa
, uint64_t txg
)
6324 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6325 objset_t
*mos
= spa
->spa_meta_objset
;
6326 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
6327 vdev_t
*rvd
= spa
->spa_root_vdev
;
6333 VERIFY(spa_writeable(spa
));
6336 * Lock out configuration changes.
6338 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6340 spa
->spa_syncing_txg
= txg
;
6341 spa
->spa_sync_pass
= 0;
6344 * If there are any pending vdev state changes, convert them
6345 * into config changes that go out with this transaction group.
6347 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6348 while (list_head(&spa
->spa_state_dirty_list
) != NULL
) {
6350 * We need the write lock here because, for aux vdevs,
6351 * calling vdev_config_dirty() modifies sav_config.
6352 * This is ugly and will become unnecessary when we
6353 * eliminate the aux vdev wart by integrating all vdevs
6354 * into the root vdev tree.
6356 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6357 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
6358 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
6359 vdev_state_clean(vd
);
6360 vdev_config_dirty(vd
);
6362 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6363 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
6365 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6367 tx
= dmu_tx_create_assigned(dp
, txg
);
6369 spa
->spa_sync_starttime
= gethrtime();
6370 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
6371 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_taskq
,
6372 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
6373 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
6376 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6377 * set spa_deflate if we have no raid-z vdevs.
6379 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
6380 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
6383 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
6384 vd
= rvd
->vdev_child
[i
];
6385 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
6388 if (i
== rvd
->vdev_children
) {
6389 spa
->spa_deflate
= TRUE
;
6390 VERIFY(0 == zap_add(spa
->spa_meta_objset
,
6391 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
6392 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
6397 * Iterate to convergence.
6400 int pass
= ++spa
->spa_sync_pass
;
6402 spa_sync_config_object(spa
, tx
);
6403 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
6404 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
6405 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
6406 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
6407 spa_errlog_sync(spa
, txg
);
6408 dsl_pool_sync(dp
, txg
);
6410 if (pass
< zfs_sync_pass_deferred_free
) {
6411 spa_sync_frees(spa
, free_bpl
, tx
);
6414 * We can not defer frees in pass 1, because
6415 * we sync the deferred frees later in pass 1.
6417 ASSERT3U(pass
, >, 1);
6418 bplist_iterate(free_bpl
, bpobj_enqueue_cb
,
6419 &spa
->spa_deferred_bpobj
, tx
);
6423 dsl_scan_sync(dp
, tx
);
6425 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
)))
6429 spa_sync_upgrades(spa
, tx
);
6431 spa
->spa_uberblock
.ub_rootbp
.blk_birth
);
6433 * Note: We need to check if the MOS is dirty
6434 * because we could have marked the MOS dirty
6435 * without updating the uberblock (e.g. if we
6436 * have sync tasks but no dirty user data). We
6437 * need to check the uberblock's rootbp because
6438 * it is updated if we have synced out dirty
6439 * data (though in this case the MOS will most
6440 * likely also be dirty due to second order
6441 * effects, we don't want to rely on that here).
6443 if (spa
->spa_uberblock
.ub_rootbp
.blk_birth
< txg
&&
6444 !dmu_objset_is_dirty(mos
, txg
)) {
6446 * Nothing changed on the first pass,
6447 * therefore this TXG is a no-op. Avoid
6448 * syncing deferred frees, so that we
6449 * can keep this TXG as a no-op.
6451 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
,
6453 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
6454 ASSERT(txg_list_empty(&dp
->dp_sync_tasks
, txg
));
6457 spa_sync_deferred_frees(spa
, tx
);
6460 } while (dmu_objset_is_dirty(mos
, txg
));
6463 * Rewrite the vdev configuration (which includes the uberblock)
6464 * to commit the transaction group.
6466 * If there are no dirty vdevs, we sync the uberblock to a few
6467 * random top-level vdevs that are known to be visible in the
6468 * config cache (see spa_vdev_add() for a complete description).
6469 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6473 * We hold SCL_STATE to prevent vdev open/close/etc.
6474 * while we're attempting to write the vdev labels.
6476 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6478 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
6479 vdev_t
*svd
[SPA_DVAS_PER_BP
];
6481 int children
= rvd
->vdev_children
;
6482 int c0
= spa_get_random(children
);
6484 for (c
= 0; c
< children
; c
++) {
6485 vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
6486 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
)
6488 svd
[svdcount
++] = vd
;
6489 if (svdcount
== SPA_DVAS_PER_BP
)
6492 error
= vdev_config_sync(svd
, svdcount
, txg
, B_FALSE
);
6494 error
= vdev_config_sync(svd
, svdcount
, txg
,
6497 error
= vdev_config_sync(rvd
->vdev_child
,
6498 rvd
->vdev_children
, txg
, B_FALSE
);
6500 error
= vdev_config_sync(rvd
->vdev_child
,
6501 rvd
->vdev_children
, txg
, B_TRUE
);
6505 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
6507 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6511 zio_suspend(spa
, NULL
);
6512 zio_resume_wait(spa
);
6516 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
6517 spa
->spa_deadman_tqid
= 0;
6520 * Clear the dirty config list.
6522 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
6523 vdev_config_clean(vd
);
6526 * Now that the new config has synced transactionally,
6527 * let it become visible to the config cache.
6529 if (spa
->spa_config_syncing
!= NULL
) {
6530 spa_config_set(spa
, spa
->spa_config_syncing
);
6531 spa
->spa_config_txg
= txg
;
6532 spa
->spa_config_syncing
= NULL
;
6535 spa
->spa_ubsync
= spa
->spa_uberblock
;
6537 dsl_pool_sync_done(dp
, txg
);
6540 * Update usable space statistics.
6542 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
))))
6543 vdev_sync_done(vd
, txg
);
6545 spa_update_dspace(spa
);
6548 * It had better be the case that we didn't dirty anything
6549 * since vdev_config_sync().
6551 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
6552 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
6553 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
6555 spa
->spa_sync_pass
= 0;
6557 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6559 spa_handle_ignored_writes(spa
);
6562 * If any async tasks have been requested, kick them off.
6564 spa_async_dispatch(spa
);
6568 * Sync all pools. We don't want to hold the namespace lock across these
6569 * operations, so we take a reference on the spa_t and drop the lock during the
6573 spa_sync_allpools(void)
6576 mutex_enter(&spa_namespace_lock
);
6577 while ((spa
= spa_next(spa
)) != NULL
) {
6578 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
6579 !spa_writeable(spa
) || spa_suspended(spa
))
6581 spa_open_ref(spa
, FTAG
);
6582 mutex_exit(&spa_namespace_lock
);
6583 txg_wait_synced(spa_get_dsl(spa
), 0);
6584 mutex_enter(&spa_namespace_lock
);
6585 spa_close(spa
, FTAG
);
6587 mutex_exit(&spa_namespace_lock
);
6591 * ==========================================================================
6592 * Miscellaneous routines
6593 * ==========================================================================
6597 * Remove all pools in the system.
6605 * Remove all cached state. All pools should be closed now,
6606 * so every spa in the AVL tree should be unreferenced.
6608 mutex_enter(&spa_namespace_lock
);
6609 while ((spa
= spa_next(NULL
)) != NULL
) {
6611 * Stop async tasks. The async thread may need to detach
6612 * a device that's been replaced, which requires grabbing
6613 * spa_namespace_lock, so we must drop it here.
6615 spa_open_ref(spa
, FTAG
);
6616 mutex_exit(&spa_namespace_lock
);
6617 spa_async_suspend(spa
);
6618 mutex_enter(&spa_namespace_lock
);
6619 spa_close(spa
, FTAG
);
6621 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
6623 spa_deactivate(spa
);
6627 mutex_exit(&spa_namespace_lock
);
6631 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
6636 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
6640 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
6641 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
6642 if (vd
->vdev_guid
== guid
)
6646 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
6647 vd
= spa
->spa_spares
.sav_vdevs
[i
];
6648 if (vd
->vdev_guid
== guid
)
6657 spa_upgrade(spa_t
*spa
, uint64_t version
)
6659 ASSERT(spa_writeable(spa
));
6661 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6664 * This should only be called for a non-faulted pool, and since a
6665 * future version would result in an unopenable pool, this shouldn't be
6668 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
6669 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
6671 spa
->spa_uberblock
.ub_version
= version
;
6672 vdev_config_dirty(spa
->spa_root_vdev
);
6674 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6676 txg_wait_synced(spa_get_dsl(spa
), 0);
6680 spa_has_spare(spa_t
*spa
, uint64_t guid
)
6684 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
6686 for (i
= 0; i
< sav
->sav_count
; i
++)
6687 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
6690 for (i
= 0; i
< sav
->sav_npending
; i
++) {
6691 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
6692 &spareguid
) == 0 && spareguid
== guid
)
6700 * Check if a pool has an active shared spare device.
6701 * Note: reference count of an active spare is 2, as a spare and as a replace
6704 spa_has_active_shared_spare(spa_t
*spa
)
6708 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
6710 for (i
= 0; i
< sav
->sav_count
; i
++) {
6711 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
6712 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
6721 * Post a FM_EREPORT_ZFS_* event from sys/fm/fs/zfs.h. The payload will be
6722 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6723 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6724 * or zdb as real changes.
6727 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, const char *name
)
6730 zfs_ereport_post(name
, spa
, vd
, NULL
, 0, 0);
6734 #if defined(_KERNEL) && defined(HAVE_SPL)
6735 /* state manipulation functions */
6736 EXPORT_SYMBOL(spa_open
);
6737 EXPORT_SYMBOL(spa_open_rewind
);
6738 EXPORT_SYMBOL(spa_get_stats
);
6739 EXPORT_SYMBOL(spa_create
);
6740 EXPORT_SYMBOL(spa_import_rootpool
);
6741 EXPORT_SYMBOL(spa_import
);
6742 EXPORT_SYMBOL(spa_tryimport
);
6743 EXPORT_SYMBOL(spa_destroy
);
6744 EXPORT_SYMBOL(spa_export
);
6745 EXPORT_SYMBOL(spa_reset
);
6746 EXPORT_SYMBOL(spa_async_request
);
6747 EXPORT_SYMBOL(spa_async_suspend
);
6748 EXPORT_SYMBOL(spa_async_resume
);
6749 EXPORT_SYMBOL(spa_inject_addref
);
6750 EXPORT_SYMBOL(spa_inject_delref
);
6751 EXPORT_SYMBOL(spa_scan_stat_init
);
6752 EXPORT_SYMBOL(spa_scan_get_stats
);
6754 /* device maniion */
6755 EXPORT_SYMBOL(spa_vdev_add
);
6756 EXPORT_SYMBOL(spa_vdev_attach
);
6757 EXPORT_SYMBOL(spa_vdev_detach
);
6758 EXPORT_SYMBOL(spa_vdev_remove
);
6759 EXPORT_SYMBOL(spa_vdev_setpath
);
6760 EXPORT_SYMBOL(spa_vdev_setfru
);
6761 EXPORT_SYMBOL(spa_vdev_split_mirror
);
6763 /* spare statech is global across all pools) */
6764 EXPORT_SYMBOL(spa_spare_add
);
6765 EXPORT_SYMBOL(spa_spare_remove
);
6766 EXPORT_SYMBOL(spa_spare_exists
);
6767 EXPORT_SYMBOL(spa_spare_activate
);
6769 /* L2ARC statech is global across all pools) */
6770 EXPORT_SYMBOL(spa_l2cache_add
);
6771 EXPORT_SYMBOL(spa_l2cache_remove
);
6772 EXPORT_SYMBOL(spa_l2cache_exists
);
6773 EXPORT_SYMBOL(spa_l2cache_activate
);
6774 EXPORT_SYMBOL(spa_l2cache_drop
);
6777 EXPORT_SYMBOL(spa_scan
);
6778 EXPORT_SYMBOL(spa_scan_stop
);
6781 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
6782 EXPORT_SYMBOL(spa_sync_allpools
);
6785 EXPORT_SYMBOL(spa_prop_set
);
6786 EXPORT_SYMBOL(spa_prop_get
);
6787 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
6789 /* asynchronous event notification */
6790 EXPORT_SYMBOL(spa_event_notify
);
6793 #if defined(_KERNEL) && defined(HAVE_SPL)
6794 module_param(spa_load_verify_maxinflight
, int, 0644);
6795 MODULE_PARM_DESC(spa_load_verify_maxinflight
,
6796 "Max concurrent traversal I/Os while verifying pool during import -X");
6798 module_param(spa_load_verify_metadata
, int, 0644);
6799 MODULE_PARM_DESC(spa_load_verify_metadata
,
6800 "Set to traverse metadata on pool import");
6802 module_param(spa_load_verify_data
, int, 0644);
6803 MODULE_PARM_DESC(spa_load_verify_data
,
6804 "Set to traverse data on pool import");