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 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2012 by Delphix. All rights reserved.
29 * This file contains all the routines used when modifying on-disk SPA state.
30 * This includes opening, importing, destroying, exporting a pool, and syncing a
34 #include <sys/zfs_context.h>
35 #include <sys/fm/fs/zfs.h>
36 #include <sys/spa_impl.h>
38 #include <sys/zio_checksum.h>
40 #include <sys/dmu_tx.h>
44 #include <sys/vdev_impl.h>
45 #include <sys/vdev_disk.h>
46 #include <sys/metaslab.h>
47 #include <sys/metaslab_impl.h>
48 #include <sys/uberblock_impl.h>
51 #include <sys/dmu_traverse.h>
52 #include <sys/dmu_objset.h>
53 #include <sys/unique.h>
54 #include <sys/dsl_pool.h>
55 #include <sys/dsl_dataset.h>
56 #include <sys/dsl_dir.h>
57 #include <sys/dsl_prop.h>
58 #include <sys/dsl_synctask.h>
59 #include <sys/fs/zfs.h>
61 #include <sys/callb.h>
62 #include <sys/systeminfo.h>
63 #include <sys/spa_boot.h>
64 #include <sys/zfs_ioctl.h>
65 #include <sys/dsl_scan.h>
66 #include <sys/zfeature.h>
69 #include <sys/bootprops.h>
70 #include <sys/callb.h>
71 #include <sys/cpupart.h>
73 #include <sys/sysdc.h>
78 #include "zfs_comutil.h"
80 typedef enum zti_modes
{
81 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
82 ZTI_MODE_ONLINE_PERCENT
, /* value is % of online CPUs */
83 ZTI_MODE_BATCH
, /* cpu-intensive; value is ignored */
84 ZTI_MODE_NULL
, /* don't create a taskq */
88 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
89 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
90 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
91 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
93 #define ZTI_N(n) ZTI_P(n, 1)
94 #define ZTI_ONE ZTI_N(1)
96 typedef struct zio_taskq_info
{
102 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
103 "iss", "iss_h", "int", "int_h"
107 * This table defines the taskq settings for each ZFS I/O type. When
108 * initializing a pool, we use this table to create an appropriately sized
109 * taskq. Some operations are low volume and therefore have a small, static
110 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
111 * macros. Other operations process a large amount of data; the ZTI_BATCH
112 * macro causes us to create a taskq oriented for throughput. Some operations
113 * are so high frequency and short-lived that the taskq itself can become a a
114 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
115 * additional degree of parallelism specified by the number of threads per-
116 * taskq and the number of taskqs; when dispatching an event in this case, the
117 * particular taskq is chosen at random.
119 * The different taskq priorities are to handle the different contexts (issue
120 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
121 * need to be handled with minimum delay.
123 const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
124 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
125 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
126 { ZTI_N(8), ZTI_NULL
, ZTI_BATCH
, ZTI_NULL
}, /* READ */
127 { ZTI_BATCH
, ZTI_N(5), ZTI_N(16), ZTI_N(5) }, /* WRITE */
128 { ZTI_P(4, 8), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
129 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
130 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
133 static dsl_syncfunc_t spa_sync_version
;
134 static dsl_syncfunc_t spa_sync_props
;
135 static dsl_checkfunc_t spa_change_guid_check
;
136 static dsl_syncfunc_t spa_change_guid_sync
;
137 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
138 static inline int spa_load_impl(spa_t
*spa
, uint64_t, nvlist_t
*config
,
139 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
141 static void spa_vdev_resilver_done(spa_t
*spa
);
143 uint_t zio_taskq_batch_pct
= 100; /* 1 thread per cpu in pset */
144 id_t zio_taskq_psrset_bind
= PS_NONE
;
145 boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
146 uint_t zio_taskq_basedc
= 80; /* base duty cycle */
148 boolean_t spa_create_process
= B_TRUE
; /* no process ==> no sysdc */
151 * This (illegal) pool name is used when temporarily importing a spa_t in order
152 * to get the vdev stats associated with the imported devices.
154 #define TRYIMPORT_NAME "$import"
157 * ==========================================================================
158 * SPA properties routines
159 * ==========================================================================
163 * Add a (source=src, propname=propval) list to an nvlist.
166 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, char *strval
,
167 uint64_t intval
, zprop_source_t src
)
169 const char *propname
= zpool_prop_to_name(prop
);
172 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
173 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
176 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
178 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, intval
) == 0);
180 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
181 nvlist_free(propval
);
185 * Get property values from the spa configuration.
188 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
190 vdev_t
*rvd
= spa
->spa_root_vdev
;
191 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
195 uint64_t cap
, version
;
196 zprop_source_t src
= ZPROP_SRC_NONE
;
197 spa_config_dirent_t
*dp
;
200 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
203 alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
204 size
= metaslab_class_get_space(spa_normal_class(spa
));
205 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
206 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
207 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
208 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
212 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
213 vdev_t
*tvd
= rvd
->vdev_child
[c
];
214 space
+= tvd
->vdev_max_asize
- tvd
->vdev_asize
;
216 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
, space
,
219 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
220 (spa_mode(spa
) == FREAD
), src
);
222 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
223 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
225 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
226 ddt_get_pool_dedup_ratio(spa
), src
);
228 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
229 rvd
->vdev_state
, src
);
231 version
= spa_version(spa
);
232 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
))
233 src
= ZPROP_SRC_DEFAULT
;
235 src
= ZPROP_SRC_LOCAL
;
236 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
, version
, src
);
240 dsl_dir_t
*freedir
= pool
->dp_free_dir
;
243 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
244 * when opening pools before this version freedir will be NULL.
246 if (freedir
!= NULL
) {
247 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
248 freedir
->dd_phys
->dd_used_bytes
, src
);
250 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
255 spa_prop_add_list(*nvp
, ZPOOL_PROP_GUID
, NULL
, spa_guid(spa
), src
);
257 if (spa
->spa_comment
!= NULL
) {
258 spa_prop_add_list(*nvp
, ZPOOL_PROP_COMMENT
, spa
->spa_comment
,
262 if (spa
->spa_root
!= NULL
)
263 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALTROOT
, spa
->spa_root
,
266 if ((dp
= list_head(&spa
->spa_config_list
)) != NULL
) {
267 if (dp
->scd_path
== NULL
) {
268 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
269 "none", 0, ZPROP_SRC_LOCAL
);
270 } else if (strcmp(dp
->scd_path
, spa_config_path
) != 0) {
271 spa_prop_add_list(*nvp
, ZPOOL_PROP_CACHEFILE
,
272 dp
->scd_path
, 0, ZPROP_SRC_LOCAL
);
278 * Get zpool property values.
281 spa_prop_get(spa_t
*spa
, nvlist_t
**nvp
)
283 objset_t
*mos
= spa
->spa_meta_objset
;
288 err
= nvlist_alloc(nvp
, NV_UNIQUE_NAME
, KM_PUSHPAGE
);
292 mutex_enter(&spa
->spa_props_lock
);
295 * Get properties from the spa config.
297 spa_prop_get_config(spa
, nvp
);
299 /* If no pool property object, no more prop to get. */
300 if (mos
== NULL
|| spa
->spa_pool_props_object
== 0) {
301 mutex_exit(&spa
->spa_props_lock
);
306 * Get properties from the MOS pool property object.
308 for (zap_cursor_init(&zc
, mos
, spa
->spa_pool_props_object
);
309 (err
= zap_cursor_retrieve(&zc
, &za
)) == 0;
310 zap_cursor_advance(&zc
)) {
313 zprop_source_t src
= ZPROP_SRC_DEFAULT
;
316 if ((prop
= zpool_name_to_prop(za
.za_name
)) == ZPROP_INVAL
)
319 switch (za
.za_integer_length
) {
321 /* integer property */
322 if (za
.za_first_integer
!=
323 zpool_prop_default_numeric(prop
))
324 src
= ZPROP_SRC_LOCAL
;
326 if (prop
== ZPOOL_PROP_BOOTFS
) {
328 dsl_dataset_t
*ds
= NULL
;
330 dp
= spa_get_dsl(spa
);
331 rw_enter(&dp
->dp_config_rwlock
, RW_READER
);
332 if ((err
= dsl_dataset_hold_obj(dp
,
333 za
.za_first_integer
, FTAG
, &ds
))) {
334 rw_exit(&dp
->dp_config_rwlock
);
339 MAXNAMELEN
+ strlen(MOS_DIR_NAME
) + 1,
341 dsl_dataset_name(ds
, strval
);
342 dsl_dataset_rele(ds
, FTAG
);
343 rw_exit(&dp
->dp_config_rwlock
);
346 intval
= za
.za_first_integer
;
349 spa_prop_add_list(*nvp
, prop
, strval
, intval
, src
);
353 MAXNAMELEN
+ strlen(MOS_DIR_NAME
) + 1);
358 /* string property */
359 strval
= kmem_alloc(za
.za_num_integers
, KM_PUSHPAGE
);
360 err
= zap_lookup(mos
, spa
->spa_pool_props_object
,
361 za
.za_name
, 1, za
.za_num_integers
, strval
);
363 kmem_free(strval
, za
.za_num_integers
);
366 spa_prop_add_list(*nvp
, prop
, strval
, 0, src
);
367 kmem_free(strval
, za
.za_num_integers
);
374 zap_cursor_fini(&zc
);
375 mutex_exit(&spa
->spa_props_lock
);
377 if (err
&& err
!= ENOENT
) {
387 * Validate the given pool properties nvlist and modify the list
388 * for the property values to be set.
391 spa_prop_validate(spa_t
*spa
, nvlist_t
*props
)
394 int error
= 0, reset_bootfs
= 0;
396 boolean_t has_feature
= B_FALSE
;
399 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
401 char *strval
, *slash
, *check
, *fname
;
402 const char *propname
= nvpair_name(elem
);
403 zpool_prop_t prop
= zpool_name_to_prop(propname
);
407 if (!zpool_prop_feature(propname
)) {
413 * Sanitize the input.
415 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
420 if (nvpair_value_uint64(elem
, &intval
) != 0) {
430 fname
= strchr(propname
, '@') + 1;
431 if (zfeature_lookup_name(fname
, NULL
) != 0) {
436 has_feature
= B_TRUE
;
439 case ZPOOL_PROP_VERSION
:
440 error
= nvpair_value_uint64(elem
, &intval
);
442 (intval
< spa_version(spa
) ||
443 intval
> SPA_VERSION_BEFORE_FEATURES
||
448 case ZPOOL_PROP_DELEGATION
:
449 case ZPOOL_PROP_AUTOREPLACE
:
450 case ZPOOL_PROP_LISTSNAPS
:
451 case ZPOOL_PROP_AUTOEXPAND
:
452 error
= nvpair_value_uint64(elem
, &intval
);
453 if (!error
&& intval
> 1)
457 case ZPOOL_PROP_BOOTFS
:
459 * If the pool version is less than SPA_VERSION_BOOTFS,
460 * or the pool is still being created (version == 0),
461 * the bootfs property cannot be set.
463 if (spa_version(spa
) < SPA_VERSION_BOOTFS
) {
469 * Make sure the vdev config is bootable
471 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
478 error
= nvpair_value_string(elem
, &strval
);
484 if (strval
== NULL
|| strval
[0] == '\0') {
485 objnum
= zpool_prop_default_numeric(
490 if ((error
= dmu_objset_hold(strval
,FTAG
,&os
)))
493 /* Must be ZPL and not gzip compressed. */
495 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
497 } else if ((error
= dsl_prop_get_integer(strval
,
498 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
499 &compress
, NULL
)) == 0 &&
500 !BOOTFS_COMPRESS_VALID(compress
)) {
503 objnum
= dmu_objset_id(os
);
505 dmu_objset_rele(os
, FTAG
);
509 case ZPOOL_PROP_FAILUREMODE
:
510 error
= nvpair_value_uint64(elem
, &intval
);
511 if (!error
&& (intval
< ZIO_FAILURE_MODE_WAIT
||
512 intval
> ZIO_FAILURE_MODE_PANIC
))
516 * This is a special case which only occurs when
517 * the pool has completely failed. This allows
518 * the user to change the in-core failmode property
519 * without syncing it out to disk (I/Os might
520 * currently be blocked). We do this by returning
521 * EIO to the caller (spa_prop_set) to trick it
522 * into thinking we encountered a property validation
525 if (!error
&& spa_suspended(spa
)) {
526 spa
->spa_failmode
= intval
;
531 case ZPOOL_PROP_CACHEFILE
:
532 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
535 if (strval
[0] == '\0')
538 if (strcmp(strval
, "none") == 0)
541 if (strval
[0] != '/') {
546 slash
= strrchr(strval
, '/');
547 ASSERT(slash
!= NULL
);
549 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
550 strcmp(slash
, "/..") == 0)
554 case ZPOOL_PROP_COMMENT
:
555 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
557 for (check
= strval
; *check
!= '\0'; check
++) {
558 if (!isprint(*check
)) {
564 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
568 case ZPOOL_PROP_DEDUPDITTO
:
569 if (spa_version(spa
) < SPA_VERSION_DEDUP
)
572 error
= nvpair_value_uint64(elem
, &intval
);
574 intval
!= 0 && intval
< ZIO_DEDUPDITTO_MIN
)
586 if (!error
&& reset_bootfs
) {
587 error
= nvlist_remove(props
,
588 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
591 error
= nvlist_add_uint64(props
,
592 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
600 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
603 spa_config_dirent_t
*dp
;
605 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
609 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
612 if (cachefile
[0] == '\0')
613 dp
->scd_path
= spa_strdup(spa_config_path
);
614 else if (strcmp(cachefile
, "none") == 0)
617 dp
->scd_path
= spa_strdup(cachefile
);
619 list_insert_head(&spa
->spa_config_list
, dp
);
621 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
625 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
628 nvpair_t
*elem
= NULL
;
629 boolean_t need_sync
= B_FALSE
;
631 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
634 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
635 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
637 if (prop
== ZPOOL_PROP_CACHEFILE
||
638 prop
== ZPOOL_PROP_ALTROOT
||
639 prop
== ZPOOL_PROP_READONLY
)
642 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPROP_INVAL
) {
645 if (prop
== ZPOOL_PROP_VERSION
) {
646 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
648 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
649 ver
= SPA_VERSION_FEATURES
;
653 /* Save time if the version is already set. */
654 if (ver
== spa_version(spa
))
658 * In addition to the pool directory object, we might
659 * create the pool properties object, the features for
660 * read object, the features for write object, or the
661 * feature descriptions object.
663 error
= dsl_sync_task_do(spa_get_dsl(spa
), NULL
,
664 spa_sync_version
, spa
, &ver
, 6);
675 return (dsl_sync_task_do(spa_get_dsl(spa
), NULL
, spa_sync_props
,
683 * If the bootfs property value is dsobj, clear it.
686 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
688 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
689 VERIFY(zap_remove(spa
->spa_meta_objset
,
690 spa
->spa_pool_props_object
,
691 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
698 spa_change_guid_check(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
701 vdev_t
*rvd
= spa
->spa_root_vdev
;
703 ASSERTV(uint64_t *newguid
= arg2
);
705 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
706 vdev_state
= rvd
->vdev_state
;
707 spa_config_exit(spa
, SCL_STATE
, FTAG
);
709 if (vdev_state
!= VDEV_STATE_HEALTHY
)
712 ASSERT3U(spa_guid(spa
), !=, *newguid
);
718 spa_change_guid_sync(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
721 uint64_t *newguid
= arg2
;
723 vdev_t
*rvd
= spa
->spa_root_vdev
;
725 oldguid
= spa_guid(spa
);
727 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
728 rvd
->vdev_guid
= *newguid
;
729 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
730 vdev_config_dirty(rvd
);
731 spa_config_exit(spa
, SCL_STATE
, FTAG
);
733 spa_history_log_internal(LOG_POOL_GUID_CHANGE
, spa
, tx
,
734 "old=%lld new=%lld", oldguid
, *newguid
);
738 * Change the GUID for the pool. This is done so that we can later
739 * re-import a pool built from a clone of our own vdevs. We will modify
740 * the root vdev's guid, our own pool guid, and then mark all of our
741 * vdevs dirty. Note that we must make sure that all our vdevs are
742 * online when we do this, or else any vdevs that weren't present
743 * would be orphaned from our pool. We are also going to issue a
744 * sysevent to update any watchers.
747 spa_change_guid(spa_t
*spa
)
752 mutex_enter(&spa_namespace_lock
);
753 guid
= spa_generate_guid(NULL
);
755 error
= dsl_sync_task_do(spa_get_dsl(spa
), spa_change_guid_check
,
756 spa_change_guid_sync
, spa
, &guid
, 5);
759 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
760 spa_event_notify(spa
, NULL
, FM_EREPORT_ZFS_POOL_REGUID
);
763 mutex_exit(&spa_namespace_lock
);
769 * ==========================================================================
770 * SPA state manipulation (open/create/destroy/import/export)
771 * ==========================================================================
775 spa_error_entry_compare(const void *a
, const void *b
)
777 spa_error_entry_t
*sa
= (spa_error_entry_t
*)a
;
778 spa_error_entry_t
*sb
= (spa_error_entry_t
*)b
;
781 ret
= bcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
782 sizeof (zbookmark_t
));
793 * Utility function which retrieves copies of the current logs and
794 * re-initializes them in the process.
797 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
799 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
801 bcopy(&spa
->spa_errlist_last
, last
, sizeof (avl_tree_t
));
802 bcopy(&spa
->spa_errlist_scrub
, scrub
, sizeof (avl_tree_t
));
804 avl_create(&spa
->spa_errlist_scrub
,
805 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
806 offsetof(spa_error_entry_t
, se_avl
));
807 avl_create(&spa
->spa_errlist_last
,
808 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
809 offsetof(spa_error_entry_t
, se_avl
));
813 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
815 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
816 enum zti_modes mode
= ztip
->zti_mode
;
817 uint_t value
= ztip
->zti_value
;
818 uint_t count
= ztip
->zti_count
;
819 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
822 boolean_t batch
= B_FALSE
;
824 if (mode
== ZTI_MODE_NULL
) {
826 tqs
->stqs_taskq
= NULL
;
830 ASSERT3U(count
, >, 0);
832 tqs
->stqs_count
= count
;
833 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
835 for (i
= 0; i
< count
; i
++) {
840 ASSERT3U(value
, >=, 1);
841 value
= MAX(value
, 1);
846 flags
|= TASKQ_THREADS_CPU_PCT
;
847 value
= zio_taskq_batch_pct
;
850 case ZTI_MODE_ONLINE_PERCENT
:
851 flags
|= TASKQ_THREADS_CPU_PCT
;
855 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
857 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
862 (void) snprintf(name
, sizeof (name
), "%s_%s_%u",
863 zio_type_name
[t
], zio_taskq_types
[q
], i
);
865 (void) snprintf(name
, sizeof (name
), "%s_%s",
866 zio_type_name
[t
], zio_taskq_types
[q
]);
869 if (zio_taskq_sysdc
&& spa
->spa_proc
!= &p0
) {
871 flags
|= TASKQ_DC_BATCH
;
873 tq
= taskq_create_sysdc(name
, value
, 50, INT_MAX
,
874 spa
->spa_proc
, zio_taskq_basedc
, flags
);
876 tq
= taskq_create_proc(name
, value
, maxclsyspri
, 50,
877 INT_MAX
, spa
->spa_proc
, flags
);
880 tqs
->stqs_taskq
[i
] = tq
;
885 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
887 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
890 if (tqs
->stqs_taskq
== NULL
) {
891 ASSERT3U(tqs
->stqs_count
, ==, 0);
895 for (i
= 0; i
< tqs
->stqs_count
; i
++) {
896 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
897 taskq_destroy(tqs
->stqs_taskq
[i
]);
900 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
901 tqs
->stqs_taskq
= NULL
;
905 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
906 * Note that a type may have multiple discrete taskqs to avoid lock contention
907 * on the taskq itself. In that case we choose which taskq at random by using
908 * the low bits of gethrtime().
911 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
912 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
914 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
917 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
918 ASSERT3U(tqs
->stqs_count
, !=, 0);
920 if (tqs
->stqs_count
== 1) {
921 tq
= tqs
->stqs_taskq
[0];
923 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
926 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
930 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
933 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
934 task_func_t
*func
, void *arg
, uint_t flags
)
936 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
940 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
941 ASSERT3U(tqs
->stqs_count
, !=, 0);
943 if (tqs
->stqs_count
== 1) {
944 tq
= tqs
->stqs_taskq
[0];
946 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
949 id
= taskq_dispatch(tq
, func
, arg
, flags
);
951 taskq_wait_id(tq
, id
);
955 spa_create_zio_taskqs(spa_t
*spa
)
959 for (t
= 0; t
< ZIO_TYPES
; t
++) {
960 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
961 spa_taskqs_init(spa
, t
, q
);
966 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
968 spa_thread(void *arg
)
973 user_t
*pu
= PTOU(curproc
);
975 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
978 ASSERT(curproc
!= &p0
);
979 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
980 "zpool-%s", spa
->spa_name
);
981 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
983 /* bind this thread to the requested psrset */
984 if (zio_taskq_psrset_bind
!= PS_NONE
) {
986 mutex_enter(&cpu_lock
);
987 mutex_enter(&pidlock
);
988 mutex_enter(&curproc
->p_lock
);
990 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
991 0, NULL
, NULL
) == 0) {
992 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
995 "Couldn't bind process for zfs pool \"%s\" to "
996 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
999 mutex_exit(&curproc
->p_lock
);
1000 mutex_exit(&pidlock
);
1001 mutex_exit(&cpu_lock
);
1005 if (zio_taskq_sysdc
) {
1006 sysdc_thread_enter(curthread
, 100, 0);
1009 spa
->spa_proc
= curproc
;
1010 spa
->spa_did
= curthread
->t_did
;
1012 spa_create_zio_taskqs(spa
);
1014 mutex_enter(&spa
->spa_proc_lock
);
1015 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1017 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1018 cv_broadcast(&spa
->spa_proc_cv
);
1020 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1021 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1022 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1023 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1025 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1026 spa
->spa_proc_state
= SPA_PROC_GONE
;
1027 spa
->spa_proc
= &p0
;
1028 cv_broadcast(&spa
->spa_proc_cv
);
1029 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1031 mutex_enter(&curproc
->p_lock
);
1037 * Activate an uninitialized pool.
1040 spa_activate(spa_t
*spa
, int mode
)
1042 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1044 spa
->spa_state
= POOL_STATE_ACTIVE
;
1045 spa
->spa_mode
= mode
;
1047 spa
->spa_normal_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1048 spa
->spa_log_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1050 /* Try to create a covering process */
1051 mutex_enter(&spa
->spa_proc_lock
);
1052 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1053 ASSERT(spa
->spa_proc
== &p0
);
1056 #ifdef HAVE_SPA_THREAD
1057 /* Only create a process if we're going to be around a while. */
1058 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1059 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1061 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1062 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1063 cv_wait(&spa
->spa_proc_cv
,
1064 &spa
->spa_proc_lock
);
1066 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1067 ASSERT(spa
->spa_proc
!= &p0
);
1068 ASSERT(spa
->spa_did
!= 0);
1072 "Couldn't create process for zfs pool \"%s\"\n",
1077 #endif /* HAVE_SPA_THREAD */
1078 mutex_exit(&spa
->spa_proc_lock
);
1080 /* If we didn't create a process, we need to create our taskqs. */
1081 if (spa
->spa_proc
== &p0
) {
1082 spa_create_zio_taskqs(spa
);
1085 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1086 offsetof(vdev_t
, vdev_config_dirty_node
));
1087 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1088 offsetof(vdev_t
, vdev_state_dirty_node
));
1090 txg_list_create(&spa
->spa_vdev_txg_list
,
1091 offsetof(struct vdev
, vdev_txg_node
));
1093 avl_create(&spa
->spa_errlist_scrub
,
1094 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1095 offsetof(spa_error_entry_t
, se_avl
));
1096 avl_create(&spa
->spa_errlist_last
,
1097 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1098 offsetof(spa_error_entry_t
, se_avl
));
1102 * Opposite of spa_activate().
1105 spa_deactivate(spa_t
*spa
)
1109 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1110 ASSERT(spa
->spa_dsl_pool
== NULL
);
1111 ASSERT(spa
->spa_root_vdev
== NULL
);
1112 ASSERT(spa
->spa_async_zio_root
== NULL
);
1113 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1115 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1117 list_destroy(&spa
->spa_config_dirty_list
);
1118 list_destroy(&spa
->spa_state_dirty_list
);
1120 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
1122 for (t
= 0; t
< ZIO_TYPES
; t
++) {
1123 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1124 spa_taskqs_fini(spa
, t
, q
);
1128 metaslab_class_destroy(spa
->spa_normal_class
);
1129 spa
->spa_normal_class
= NULL
;
1131 metaslab_class_destroy(spa
->spa_log_class
);
1132 spa
->spa_log_class
= NULL
;
1135 * If this was part of an import or the open otherwise failed, we may
1136 * still have errors left in the queues. Empty them just in case.
1138 spa_errlog_drain(spa
);
1140 avl_destroy(&spa
->spa_errlist_scrub
);
1141 avl_destroy(&spa
->spa_errlist_last
);
1143 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1145 mutex_enter(&spa
->spa_proc_lock
);
1146 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1147 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1148 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1149 cv_broadcast(&spa
->spa_proc_cv
);
1150 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1151 ASSERT(spa
->spa_proc
!= &p0
);
1152 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1154 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1155 spa
->spa_proc_state
= SPA_PROC_NONE
;
1157 ASSERT(spa
->spa_proc
== &p0
);
1158 mutex_exit(&spa
->spa_proc_lock
);
1161 * We want to make sure spa_thread() has actually exited the ZFS
1162 * module, so that the module can't be unloaded out from underneath
1165 if (spa
->spa_did
!= 0) {
1166 thread_join(spa
->spa_did
);
1172 * Verify a pool configuration, and construct the vdev tree appropriately. This
1173 * will create all the necessary vdevs in the appropriate layout, with each vdev
1174 * in the CLOSED state. This will prep the pool before open/creation/import.
1175 * All vdev validation is done by the vdev_alloc() routine.
1178 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1179 uint_t id
, int atype
)
1186 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1189 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1192 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1195 if (error
== ENOENT
)
1204 for (c
= 0; c
< children
; c
++) {
1206 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1214 ASSERT(*vdp
!= NULL
);
1220 * Opposite of spa_load().
1223 spa_unload(spa_t
*spa
)
1227 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1232 spa_async_suspend(spa
);
1237 if (spa
->spa_sync_on
) {
1238 txg_sync_stop(spa
->spa_dsl_pool
);
1239 spa
->spa_sync_on
= B_FALSE
;
1243 * Wait for any outstanding async I/O to complete.
1245 if (spa
->spa_async_zio_root
!= NULL
) {
1246 (void) zio_wait(spa
->spa_async_zio_root
);
1247 spa
->spa_async_zio_root
= NULL
;
1250 bpobj_close(&spa
->spa_deferred_bpobj
);
1253 * Close the dsl pool.
1255 if (spa
->spa_dsl_pool
) {
1256 dsl_pool_close(spa
->spa_dsl_pool
);
1257 spa
->spa_dsl_pool
= NULL
;
1258 spa
->spa_meta_objset
= NULL
;
1263 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1266 * Drop and purge level 2 cache
1268 spa_l2cache_drop(spa
);
1273 if (spa
->spa_root_vdev
)
1274 vdev_free(spa
->spa_root_vdev
);
1275 ASSERT(spa
->spa_root_vdev
== NULL
);
1277 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1278 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1279 if (spa
->spa_spares
.sav_vdevs
) {
1280 kmem_free(spa
->spa_spares
.sav_vdevs
,
1281 spa
->spa_spares
.sav_count
* sizeof (void *));
1282 spa
->spa_spares
.sav_vdevs
= NULL
;
1284 if (spa
->spa_spares
.sav_config
) {
1285 nvlist_free(spa
->spa_spares
.sav_config
);
1286 spa
->spa_spares
.sav_config
= NULL
;
1288 spa
->spa_spares
.sav_count
= 0;
1290 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1291 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1292 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1294 if (spa
->spa_l2cache
.sav_vdevs
) {
1295 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1296 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1297 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1299 if (spa
->spa_l2cache
.sav_config
) {
1300 nvlist_free(spa
->spa_l2cache
.sav_config
);
1301 spa
->spa_l2cache
.sav_config
= NULL
;
1303 spa
->spa_l2cache
.sav_count
= 0;
1305 spa
->spa_async_suspended
= 0;
1307 if (spa
->spa_comment
!= NULL
) {
1308 spa_strfree(spa
->spa_comment
);
1309 spa
->spa_comment
= NULL
;
1312 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1316 * Load (or re-load) the current list of vdevs describing the active spares for
1317 * this pool. When this is called, we have some form of basic information in
1318 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1319 * then re-generate a more complete list including status information.
1322 spa_load_spares(spa_t
*spa
)
1329 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1332 * First, close and free any existing spare vdevs.
1334 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1335 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1337 /* Undo the call to spa_activate() below */
1338 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1339 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1340 spa_spare_remove(tvd
);
1345 if (spa
->spa_spares
.sav_vdevs
)
1346 kmem_free(spa
->spa_spares
.sav_vdevs
,
1347 spa
->spa_spares
.sav_count
* sizeof (void *));
1349 if (spa
->spa_spares
.sav_config
== NULL
)
1352 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1353 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
1355 spa
->spa_spares
.sav_count
= (int)nspares
;
1356 spa
->spa_spares
.sav_vdevs
= NULL
;
1362 * Construct the array of vdevs, opening them to get status in the
1363 * process. For each spare, there is potentially two different vdev_t
1364 * structures associated with it: one in the list of spares (used only
1365 * for basic validation purposes) and one in the active vdev
1366 * configuration (if it's spared in). During this phase we open and
1367 * validate each vdev on the spare list. If the vdev also exists in the
1368 * active configuration, then we also mark this vdev as an active spare.
1370 spa
->spa_spares
.sav_vdevs
= kmem_alloc(nspares
* sizeof (void *),
1372 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1373 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1374 VDEV_ALLOC_SPARE
) == 0);
1377 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1379 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1380 B_FALSE
)) != NULL
) {
1381 if (!tvd
->vdev_isspare
)
1385 * We only mark the spare active if we were successfully
1386 * able to load the vdev. Otherwise, importing a pool
1387 * with a bad active spare would result in strange
1388 * behavior, because multiple pool would think the spare
1389 * is actively in use.
1391 * There is a vulnerability here to an equally bizarre
1392 * circumstance, where a dead active spare is later
1393 * brought back to life (onlined or otherwise). Given
1394 * the rarity of this scenario, and the extra complexity
1395 * it adds, we ignore the possibility.
1397 if (!vdev_is_dead(tvd
))
1398 spa_spare_activate(tvd
);
1402 vd
->vdev_aux
= &spa
->spa_spares
;
1404 if (vdev_open(vd
) != 0)
1407 if (vdev_validate_aux(vd
) == 0)
1412 * Recompute the stashed list of spares, with status information
1415 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
,
1416 DATA_TYPE_NVLIST_ARRAY
) == 0);
1418 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1420 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1421 spares
[i
] = vdev_config_generate(spa
,
1422 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1423 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1424 ZPOOL_CONFIG_SPARES
, spares
, spa
->spa_spares
.sav_count
) == 0);
1425 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1426 nvlist_free(spares
[i
]);
1427 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1431 * Load (or re-load) the current list of vdevs describing the active l2cache for
1432 * this pool. When this is called, we have some form of basic information in
1433 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1434 * then re-generate a more complete list including status information.
1435 * Devices which are already active have their details maintained, and are
1439 spa_load_l2cache(spa_t
*spa
)
1443 int i
, j
, oldnvdevs
;
1445 vdev_t
*vd
, **oldvdevs
, **newvdevs
= NULL
;
1446 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1448 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1450 if (sav
->sav_config
!= NULL
) {
1451 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
1452 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
1453 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_PUSHPAGE
);
1458 oldvdevs
= sav
->sav_vdevs
;
1459 oldnvdevs
= sav
->sav_count
;
1460 sav
->sav_vdevs
= NULL
;
1464 * Process new nvlist of vdevs.
1466 for (i
= 0; i
< nl2cache
; i
++) {
1467 VERIFY(nvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
,
1471 for (j
= 0; j
< oldnvdevs
; j
++) {
1473 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1475 * Retain previous vdev for add/remove ops.
1483 if (newvdevs
[i
] == NULL
) {
1487 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1488 VDEV_ALLOC_L2CACHE
) == 0);
1493 * Commit this vdev as an l2cache device,
1494 * even if it fails to open.
1496 spa_l2cache_add(vd
);
1501 spa_l2cache_activate(vd
);
1503 if (vdev_open(vd
) != 0)
1506 (void) vdev_validate_aux(vd
);
1508 if (!vdev_is_dead(vd
))
1509 l2arc_add_vdev(spa
, vd
);
1514 * Purge vdevs that were dropped
1516 for (i
= 0; i
< oldnvdevs
; i
++) {
1521 ASSERT(vd
->vdev_isl2cache
);
1523 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
1524 pool
!= 0ULL && l2arc_vdev_present(vd
))
1525 l2arc_remove_vdev(vd
);
1526 vdev_clear_stats(vd
);
1532 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
1534 if (sav
->sav_config
== NULL
)
1537 sav
->sav_vdevs
= newvdevs
;
1538 sav
->sav_count
= (int)nl2cache
;
1541 * Recompute the stashed list of l2cache devices, with status
1542 * information this time.
1544 VERIFY(nvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
1545 DATA_TYPE_NVLIST_ARRAY
) == 0);
1547 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *), KM_PUSHPAGE
);
1548 for (i
= 0; i
< sav
->sav_count
; i
++)
1549 l2cache
[i
] = vdev_config_generate(spa
,
1550 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
1551 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
1552 ZPOOL_CONFIG_L2CACHE
, l2cache
, sav
->sav_count
) == 0);
1554 for (i
= 0; i
< sav
->sav_count
; i
++)
1555 nvlist_free(l2cache
[i
]);
1557 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
1561 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
1564 char *packed
= NULL
;
1569 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
1573 nvsize
= *(uint64_t *)db
->db_data
;
1574 dmu_buf_rele(db
, FTAG
);
1576 packed
= kmem_alloc(nvsize
, KM_PUSHPAGE
| KM_NODEBUG
);
1577 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
1580 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
1581 kmem_free(packed
, nvsize
);
1587 * Checks to see if the given vdev could not be opened, in which case we post a
1588 * sysevent to notify the autoreplace code that the device has been removed.
1591 spa_check_removed(vdev_t
*vd
)
1595 for (c
= 0; c
< vd
->vdev_children
; c
++)
1596 spa_check_removed(vd
->vdev_child
[c
]);
1598 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
)) {
1599 zfs_ereport_post(FM_EREPORT_RESOURCE_AUTOREPLACE
,
1600 vd
->vdev_spa
, vd
, NULL
, 0, 0);
1601 spa_event_notify(vd
->vdev_spa
, vd
, FM_EREPORT_ZFS_DEVICE_CHECK
);
1606 * Validate the current config against the MOS config
1609 spa_config_valid(spa_t
*spa
, nvlist_t
*config
)
1611 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
1615 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0);
1617 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1618 VERIFY(spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
) == 0);
1620 ASSERT3U(rvd
->vdev_children
, ==, mrvd
->vdev_children
);
1623 * If we're doing a normal import, then build up any additional
1624 * diagnostic information about missing devices in this config.
1625 * We'll pass this up to the user for further processing.
1627 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
1628 nvlist_t
**child
, *nv
;
1631 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
**),
1633 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
1635 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1636 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1637 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1639 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1640 mtvd
->vdev_ops
!= &vdev_missing_ops
&&
1642 child
[idx
++] = vdev_config_generate(spa
, mtvd
,
1647 VERIFY(nvlist_add_nvlist_array(nv
,
1648 ZPOOL_CONFIG_CHILDREN
, child
, idx
) == 0);
1649 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
1650 ZPOOL_CONFIG_MISSING_DEVICES
, nv
) == 0);
1652 for (i
= 0; i
< idx
; i
++)
1653 nvlist_free(child
[i
]);
1656 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
1660 * Compare the root vdev tree with the information we have
1661 * from the MOS config (mrvd). Check each top-level vdev
1662 * with the corresponding MOS config top-level (mtvd).
1664 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1665 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1666 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1669 * Resolve any "missing" vdevs in the current configuration.
1670 * If we find that the MOS config has more accurate information
1671 * about the top-level vdev then use that vdev instead.
1673 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1674 mtvd
->vdev_ops
!= &vdev_missing_ops
) {
1676 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
))
1680 * Device specific actions.
1682 if (mtvd
->vdev_islog
) {
1683 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
1686 * XXX - once we have 'readonly' pool
1687 * support we should be able to handle
1688 * missing data devices by transitioning
1689 * the pool to readonly.
1695 * Swap the missing vdev with the data we were
1696 * able to obtain from the MOS config.
1698 vdev_remove_child(rvd
, tvd
);
1699 vdev_remove_child(mrvd
, mtvd
);
1701 vdev_add_child(rvd
, mtvd
);
1702 vdev_add_child(mrvd
, tvd
);
1704 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1706 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1709 } else if (mtvd
->vdev_islog
) {
1711 * Load the slog device's state from the MOS config
1712 * since it's possible that the label does not
1713 * contain the most up-to-date information.
1715 vdev_load_log_state(tvd
, mtvd
);
1720 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1723 * Ensure we were able to validate the config.
1725 return (rvd
->vdev_guid_sum
== spa
->spa_uberblock
.ub_guid_sum
);
1729 * Check for missing log devices
1732 spa_check_logs(spa_t
*spa
)
1734 switch (spa
->spa_log_state
) {
1737 case SPA_LOG_MISSING
:
1738 /* need to recheck in case slog has been restored */
1739 case SPA_LOG_UNKNOWN
:
1740 if (dmu_objset_find(spa
->spa_name
, zil_check_log_chain
, NULL
,
1741 DS_FIND_CHILDREN
)) {
1742 spa_set_log_state(spa
, SPA_LOG_MISSING
);
1751 spa_passivate_log(spa_t
*spa
)
1753 vdev_t
*rvd
= spa
->spa_root_vdev
;
1754 boolean_t slog_found
= B_FALSE
;
1757 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1759 if (!spa_has_slogs(spa
))
1762 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1763 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1764 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1766 if (tvd
->vdev_islog
) {
1767 metaslab_group_passivate(mg
);
1768 slog_found
= B_TRUE
;
1772 return (slog_found
);
1776 spa_activate_log(spa_t
*spa
)
1778 vdev_t
*rvd
= spa
->spa_root_vdev
;
1781 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1783 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1784 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1785 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1787 if (tvd
->vdev_islog
)
1788 metaslab_group_activate(mg
);
1793 spa_offline_log(spa_t
*spa
)
1797 if ((error
= dmu_objset_find(spa_name(spa
), zil_vdev_offline
,
1798 NULL
, DS_FIND_CHILDREN
)) == 0) {
1801 * We successfully offlined the log device, sync out the
1802 * current txg so that the "stubby" block can be removed
1805 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1811 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
1815 for (i
= 0; i
< sav
->sav_count
; i
++)
1816 spa_check_removed(sav
->sav_vdevs
[i
]);
1820 spa_claim_notify(zio_t
*zio
)
1822 spa_t
*spa
= zio
->io_spa
;
1827 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
1828 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
1829 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
1830 mutex_exit(&spa
->spa_props_lock
);
1833 typedef struct spa_load_error
{
1834 uint64_t sle_meta_count
;
1835 uint64_t sle_data_count
;
1839 spa_load_verify_done(zio_t
*zio
)
1841 blkptr_t
*bp
= zio
->io_bp
;
1842 spa_load_error_t
*sle
= zio
->io_private
;
1843 dmu_object_type_t type
= BP_GET_TYPE(bp
);
1844 int error
= zio
->io_error
;
1847 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
1848 type
!= DMU_OT_INTENT_LOG
)
1849 atomic_add_64(&sle
->sle_meta_count
, 1);
1851 atomic_add_64(&sle
->sle_data_count
, 1);
1853 zio_data_buf_free(zio
->io_data
, zio
->io_size
);
1858 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
1859 const zbookmark_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
1863 size_t size
= BP_GET_PSIZE(bp
);
1864 void *data
= zio_data_buf_alloc(size
);
1866 zio_nowait(zio_read(rio
, spa
, bp
, data
, size
,
1867 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
1868 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
1869 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
1875 spa_load_verify(spa_t
*spa
)
1878 spa_load_error_t sle
= { 0 };
1879 zpool_rewind_policy_t policy
;
1880 boolean_t verify_ok
= B_FALSE
;
1883 zpool_get_rewind_policy(spa
->spa_config
, &policy
);
1885 if (policy
.zrp_request
& ZPOOL_NEVER_REWIND
)
1888 rio
= zio_root(spa
, NULL
, &sle
,
1889 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
1891 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
1892 TRAVERSE_PRE
| TRAVERSE_PREFETCH
, spa_load_verify_cb
, rio
);
1894 (void) zio_wait(rio
);
1896 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
1897 spa
->spa_load_data_errors
= sle
.sle_data_count
;
1899 if (!error
&& sle
.sle_meta_count
<= policy
.zrp_maxmeta
&&
1900 sle
.sle_data_count
<= policy
.zrp_maxdata
) {
1904 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
1905 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
1907 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
1908 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
1909 ZPOOL_CONFIG_LOAD_TIME
, spa
->spa_load_txg_ts
) == 0);
1910 VERIFY(nvlist_add_int64(spa
->spa_load_info
,
1911 ZPOOL_CONFIG_REWIND_TIME
, loss
) == 0);
1912 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
1913 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
) == 0);
1915 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
1919 if (error
!= ENXIO
&& error
!= EIO
)
1924 return (verify_ok
? 0 : EIO
);
1928 * Find a value in the pool props object.
1931 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
1933 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
1934 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
1938 * Find a value in the pool directory object.
1941 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
)
1943 return (zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
1944 name
, sizeof (uint64_t), 1, val
));
1948 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
1950 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
1955 * Fix up config after a partly-completed split. This is done with the
1956 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
1957 * pool have that entry in their config, but only the splitting one contains
1958 * a list of all the guids of the vdevs that are being split off.
1960 * This function determines what to do with that list: either rejoin
1961 * all the disks to the pool, or complete the splitting process. To attempt
1962 * the rejoin, each disk that is offlined is marked online again, and
1963 * we do a reopen() call. If the vdev label for every disk that was
1964 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
1965 * then we call vdev_split() on each disk, and complete the split.
1967 * Otherwise we leave the config alone, with all the vdevs in place in
1968 * the original pool.
1971 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
1978 boolean_t attempt_reopen
;
1980 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
1983 /* check that the config is complete */
1984 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
1985 &glist
, &gcount
) != 0)
1988 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_PUSHPAGE
);
1990 /* attempt to online all the vdevs & validate */
1991 attempt_reopen
= B_TRUE
;
1992 for (i
= 0; i
< gcount
; i
++) {
1993 if (glist
[i
] == 0) /* vdev is hole */
1996 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
1997 if (vd
[i
] == NULL
) {
1999 * Don't bother attempting to reopen the disks;
2000 * just do the split.
2002 attempt_reopen
= B_FALSE
;
2004 /* attempt to re-online it */
2005 vd
[i
]->vdev_offline
= B_FALSE
;
2009 if (attempt_reopen
) {
2010 vdev_reopen(spa
->spa_root_vdev
);
2012 /* check each device to see what state it's in */
2013 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
2014 if (vd
[i
] != NULL
&&
2015 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
2022 * If every disk has been moved to the new pool, or if we never
2023 * even attempted to look at them, then we split them off for
2026 if (!attempt_reopen
|| gcount
== extracted
) {
2027 for (i
= 0; i
< gcount
; i
++)
2030 vdev_reopen(spa
->spa_root_vdev
);
2033 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
2037 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
,
2038 boolean_t mosconfig
)
2040 nvlist_t
*config
= spa
->spa_config
;
2041 char *ereport
= FM_EREPORT_ZFS_POOL
;
2047 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
))
2050 ASSERT(spa
->spa_comment
== NULL
);
2051 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2052 spa
->spa_comment
= spa_strdup(comment
);
2055 * Versioning wasn't explicitly added to the label until later, so if
2056 * it's not present treat it as the initial version.
2058 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
2059 &spa
->spa_ubsync
.ub_version
) != 0)
2060 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
2062 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
2063 &spa
->spa_config_txg
);
2065 if ((state
== SPA_LOAD_IMPORT
|| state
== SPA_LOAD_TRYIMPORT
) &&
2066 spa_guid_exists(pool_guid
, 0)) {
2069 spa
->spa_config_guid
= pool_guid
;
2071 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
,
2073 VERIFY(nvlist_dup(nvl
, &spa
->spa_config_splitting
,
2077 nvlist_free(spa
->spa_load_info
);
2078 spa
->spa_load_info
= fnvlist_alloc();
2080 gethrestime(&spa
->spa_loaded_ts
);
2081 error
= spa_load_impl(spa
, pool_guid
, config
, state
, type
,
2082 mosconfig
, &ereport
);
2085 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
2087 if (error
!= EEXIST
) {
2088 spa
->spa_loaded_ts
.tv_sec
= 0;
2089 spa
->spa_loaded_ts
.tv_nsec
= 0;
2091 if (error
!= EBADF
) {
2092 zfs_ereport_post(ereport
, spa
, NULL
, NULL
, 0, 0);
2095 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
2102 * Load an existing storage pool, using the pool's builtin spa_config as a
2103 * source of configuration information.
2105 __attribute__((always_inline
))
2107 spa_load_impl(spa_t
*spa
, uint64_t pool_guid
, nvlist_t
*config
,
2108 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
2112 nvlist_t
*nvroot
= NULL
;
2115 uberblock_t
*ub
= &spa
->spa_uberblock
;
2116 uint64_t children
, config_cache_txg
= spa
->spa_config_txg
;
2117 int orig_mode
= spa
->spa_mode
;
2120 boolean_t missing_feat_write
= B_FALSE
;
2123 * If this is an untrusted config, access the pool in read-only mode.
2124 * This prevents things like resilvering recently removed devices.
2127 spa
->spa_mode
= FREAD
;
2129 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2131 spa
->spa_load_state
= state
;
2133 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvroot
))
2136 parse
= (type
== SPA_IMPORT_EXISTING
?
2137 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
2140 * Create "The Godfather" zio to hold all async IOs
2142 spa
->spa_async_zio_root
= zio_root(spa
, NULL
, NULL
,
2143 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_GODFATHER
);
2146 * Parse the configuration into a vdev tree. We explicitly set the
2147 * value that will be returned by spa_version() since parsing the
2148 * configuration requires knowing the version number.
2150 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2151 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, parse
);
2152 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2157 ASSERT(spa
->spa_root_vdev
== rvd
);
2159 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2160 ASSERT(spa_guid(spa
) == pool_guid
);
2164 * Try to open all vdevs, loading each label in the process.
2166 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2167 error
= vdev_open(rvd
);
2168 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2173 * We need to validate the vdev labels against the configuration that
2174 * we have in hand, which is dependent on the setting of mosconfig. If
2175 * mosconfig is true then we're validating the vdev labels based on
2176 * that config. Otherwise, we're validating against the cached config
2177 * (zpool.cache) that was read when we loaded the zfs module, and then
2178 * later we will recursively call spa_load() and validate against
2181 * If we're assembling a new pool that's been split off from an
2182 * existing pool, the labels haven't yet been updated so we skip
2183 * validation for now.
2185 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2186 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2187 error
= vdev_validate(rvd
, mosconfig
);
2188 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2193 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2198 * Find the best uberblock.
2200 vdev_uberblock_load(rvd
, ub
, &label
);
2203 * If we weren't able to find a single valid uberblock, return failure.
2205 if (ub
->ub_txg
== 0) {
2207 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
2211 * If the pool has an unsupported version we can't open it.
2213 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
2215 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
2218 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2222 * If we weren't able to find what's necessary for reading the
2223 * MOS in the label, return failure.
2225 if (label
== NULL
|| nvlist_lookup_nvlist(label
,
2226 ZPOOL_CONFIG_FEATURES_FOR_READ
, &features
) != 0) {
2228 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2233 * Update our in-core representation with the definitive values
2236 nvlist_free(spa
->spa_label_features
);
2237 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
, 0) == 0);
2243 * Look through entries in the label nvlist's features_for_read. If
2244 * there is a feature listed there which we don't understand then we
2245 * cannot open a pool.
2247 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2248 nvlist_t
*unsup_feat
;
2251 VERIFY(nvlist_alloc(&unsup_feat
, NV_UNIQUE_NAME
, KM_SLEEP
) ==
2254 for (nvp
= nvlist_next_nvpair(spa
->spa_label_features
, NULL
);
2256 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
2257 if (!zfeature_is_supported(nvpair_name(nvp
))) {
2258 VERIFY(nvlist_add_string(unsup_feat
,
2259 nvpair_name(nvp
), "") == 0);
2263 if (!nvlist_empty(unsup_feat
)) {
2264 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
2265 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
) == 0);
2266 nvlist_free(unsup_feat
);
2267 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2271 nvlist_free(unsup_feat
);
2275 * If the vdev guid sum doesn't match the uberblock, we have an
2276 * incomplete configuration. We first check to see if the pool
2277 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2278 * If it is, defer the vdev_guid_sum check till later so we
2279 * can handle missing vdevs.
2281 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VDEV_CHILDREN
,
2282 &children
) != 0 && mosconfig
&& type
!= SPA_IMPORT_ASSEMBLE
&&
2283 rvd
->vdev_guid_sum
!= ub
->ub_guid_sum
)
2284 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
2286 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
2287 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2288 spa_try_repair(spa
, config
);
2289 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2290 nvlist_free(spa
->spa_config_splitting
);
2291 spa
->spa_config_splitting
= NULL
;
2295 * Initialize internal SPA structures.
2297 spa
->spa_state
= POOL_STATE_ACTIVE
;
2298 spa
->spa_ubsync
= spa
->spa_uberblock
;
2299 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
2300 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
2301 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
2302 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
2303 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
2304 spa
->spa_prev_software_version
= ub
->ub_software_version
;
2306 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
2308 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2309 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
2311 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
) != 0)
2312 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2314 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
2315 boolean_t missing_feat_read
= B_FALSE
;
2316 nvlist_t
*unsup_feat
, *enabled_feat
;
2318 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
2319 &spa
->spa_feat_for_read_obj
) != 0) {
2320 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2323 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
2324 &spa
->spa_feat_for_write_obj
) != 0) {
2325 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2328 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
2329 &spa
->spa_feat_desc_obj
) != 0) {
2330 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2333 enabled_feat
= fnvlist_alloc();
2334 unsup_feat
= fnvlist_alloc();
2336 if (!feature_is_supported(spa
->spa_meta_objset
,
2337 spa
->spa_feat_for_read_obj
, spa
->spa_feat_desc_obj
,
2338 unsup_feat
, enabled_feat
))
2339 missing_feat_read
= B_TRUE
;
2341 if (spa_writeable(spa
) || state
== SPA_LOAD_TRYIMPORT
) {
2342 if (!feature_is_supported(spa
->spa_meta_objset
,
2343 spa
->spa_feat_for_write_obj
, spa
->spa_feat_desc_obj
,
2344 unsup_feat
, enabled_feat
)) {
2345 missing_feat_write
= B_TRUE
;
2349 fnvlist_add_nvlist(spa
->spa_load_info
,
2350 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
2352 if (!nvlist_empty(unsup_feat
)) {
2353 fnvlist_add_nvlist(spa
->spa_load_info
,
2354 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
2357 fnvlist_free(enabled_feat
);
2358 fnvlist_free(unsup_feat
);
2360 if (!missing_feat_read
) {
2361 fnvlist_add_boolean(spa
->spa_load_info
,
2362 ZPOOL_CONFIG_CAN_RDONLY
);
2366 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2367 * twofold: to determine whether the pool is available for
2368 * import in read-write mode and (if it is not) whether the
2369 * pool is available for import in read-only mode. If the pool
2370 * is available for import in read-write mode, it is displayed
2371 * as available in userland; if it is not available for import
2372 * in read-only mode, it is displayed as unavailable in
2373 * userland. If the pool is available for import in read-only
2374 * mode but not read-write mode, it is displayed as unavailable
2375 * in userland with a special note that the pool is actually
2376 * available for open in read-only mode.
2378 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2379 * missing a feature for write, we must first determine whether
2380 * the pool can be opened read-only before returning to
2381 * userland in order to know whether to display the
2382 * abovementioned note.
2384 if (missing_feat_read
|| (missing_feat_write
&&
2385 spa_writeable(spa
))) {
2386 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2391 spa
->spa_is_initializing
= B_TRUE
;
2392 error
= dsl_pool_open(spa
->spa_dsl_pool
);
2393 spa
->spa_is_initializing
= B_FALSE
;
2395 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2399 nvlist_t
*policy
= NULL
, *nvconfig
;
2401 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2402 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2404 if (!spa_is_root(spa
) && nvlist_lookup_uint64(nvconfig
,
2405 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
2407 unsigned long myhostid
= 0;
2409 VERIFY(nvlist_lookup_string(nvconfig
,
2410 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
2413 myhostid
= zone_get_hostid(NULL
);
2416 * We're emulating the system's hostid in userland, so
2417 * we can't use zone_get_hostid().
2419 (void) ddi_strtoul(hw_serial
, NULL
, 10, &myhostid
);
2420 #endif /* _KERNEL */
2421 if (hostid
!= 0 && myhostid
!= 0 &&
2422 hostid
!= myhostid
) {
2423 nvlist_free(nvconfig
);
2424 cmn_err(CE_WARN
, "pool '%s' could not be "
2425 "loaded as it was last accessed by "
2426 "another system (host: %s hostid: 0x%lx). "
2427 "See: http://zfsonlinux.org/msg/ZFS-8000-EY",
2428 spa_name(spa
), hostname
,
2429 (unsigned long)hostid
);
2433 if (nvlist_lookup_nvlist(spa
->spa_config
,
2434 ZPOOL_REWIND_POLICY
, &policy
) == 0)
2435 VERIFY(nvlist_add_nvlist(nvconfig
,
2436 ZPOOL_REWIND_POLICY
, policy
) == 0);
2438 spa_config_set(spa
, nvconfig
);
2440 spa_deactivate(spa
);
2441 spa_activate(spa
, orig_mode
);
2443 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, B_TRUE
));
2446 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
) != 0)
2447 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2448 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
2450 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2453 * Load the bit that tells us to use the new accounting function
2454 * (raid-z deflation). If we have an older pool, this will not
2457 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
);
2458 if (error
!= 0 && error
!= ENOENT
)
2459 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2461 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
2462 &spa
->spa_creation_version
);
2463 if (error
!= 0 && error
!= ENOENT
)
2464 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2467 * Load the persistent error log. If we have an older pool, this will
2470 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
);
2471 if (error
!= 0 && error
!= ENOENT
)
2472 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2474 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
2475 &spa
->spa_errlog_scrub
);
2476 if (error
!= 0 && error
!= ENOENT
)
2477 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2480 * Load the history object. If we have an older pool, this
2481 * will not be present.
2483 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
);
2484 if (error
!= 0 && error
!= ENOENT
)
2485 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2488 * If we're assembling the pool from the split-off vdevs of
2489 * an existing pool, we don't want to attach the spares & cache
2494 * Load any hot spares for this pool.
2496 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
);
2497 if (error
!= 0 && error
!= ENOENT
)
2498 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2499 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2500 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
2501 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
2502 &spa
->spa_spares
.sav_config
) != 0)
2503 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2505 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2506 spa_load_spares(spa
);
2507 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2508 } else if (error
== 0) {
2509 spa
->spa_spares
.sav_sync
= B_TRUE
;
2513 * Load any level 2 ARC devices for this pool.
2515 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
2516 &spa
->spa_l2cache
.sav_object
);
2517 if (error
!= 0 && error
!= ENOENT
)
2518 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2519 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2520 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
2521 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
2522 &spa
->spa_l2cache
.sav_config
) != 0)
2523 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2525 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2526 spa_load_l2cache(spa
);
2527 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2528 } else if (error
== 0) {
2529 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
2532 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
2534 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
);
2535 if (error
&& error
!= ENOENT
)
2536 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2539 uint64_t autoreplace
;
2541 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
2542 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
2543 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
2544 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
2545 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
2546 spa_prop_find(spa
, ZPOOL_PROP_DEDUPDITTO
,
2547 &spa
->spa_dedup_ditto
);
2549 spa
->spa_autoreplace
= (autoreplace
!= 0);
2553 * If the 'autoreplace' property is set, then post a resource notifying
2554 * the ZFS DE that it should not issue any faults for unopenable
2555 * devices. We also iterate over the vdevs, and post a sysevent for any
2556 * unopenable vdevs so that the normal autoreplace handler can take
2559 if (spa
->spa_autoreplace
&& state
!= SPA_LOAD_TRYIMPORT
) {
2560 spa_check_removed(spa
->spa_root_vdev
);
2562 * For the import case, this is done in spa_import(), because
2563 * at this point we're using the spare definitions from
2564 * the MOS config, not necessarily from the userland config.
2566 if (state
!= SPA_LOAD_IMPORT
) {
2567 spa_aux_check_removed(&spa
->spa_spares
);
2568 spa_aux_check_removed(&spa
->spa_l2cache
);
2573 * Load the vdev state for all toplevel vdevs.
2578 * Propagate the leaf DTLs we just loaded all the way up the tree.
2580 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2581 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
);
2582 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2585 * Load the DDTs (dedup tables).
2587 error
= ddt_load(spa
);
2589 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2591 spa_update_dspace(spa
);
2594 * Validate the config, using the MOS config to fill in any
2595 * information which might be missing. If we fail to validate
2596 * the config then declare the pool unfit for use. If we're
2597 * assembling a pool from a split, the log is not transferred
2600 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2603 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2604 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2606 if (!spa_config_valid(spa
, nvconfig
)) {
2607 nvlist_free(nvconfig
);
2608 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
2611 nvlist_free(nvconfig
);
2614 * Now that we've validated the config, check the state of the
2615 * root vdev. If it can't be opened, it indicates one or
2616 * more toplevel vdevs are faulted.
2618 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2621 if (spa_check_logs(spa
)) {
2622 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
2623 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
, ENXIO
));
2627 if (missing_feat_write
) {
2628 ASSERT(state
== SPA_LOAD_TRYIMPORT
);
2631 * At this point, we know that we can open the pool in
2632 * read-only mode but not read-write mode. We now have enough
2633 * information and can return to userland.
2635 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
, ENOTSUP
));
2639 * We've successfully opened the pool, verify that we're ready
2640 * to start pushing transactions.
2642 if (state
!= SPA_LOAD_TRYIMPORT
) {
2643 if ((error
= spa_load_verify(spa
)))
2644 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2648 if (spa_writeable(spa
) && (state
== SPA_LOAD_RECOVER
||
2649 spa
->spa_load_max_txg
== UINT64_MAX
)) {
2651 int need_update
= B_FALSE
;
2654 ASSERT(state
!= SPA_LOAD_TRYIMPORT
);
2657 * Claim log blocks that haven't been committed yet.
2658 * This must all happen in a single txg.
2659 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2660 * invoked from zil_claim_log_block()'s i/o done callback.
2661 * Price of rollback is that we abandon the log.
2663 spa
->spa_claiming
= B_TRUE
;
2665 tx
= dmu_tx_create_assigned(spa_get_dsl(spa
),
2666 spa_first_txg(spa
));
2667 (void) dmu_objset_find(spa_name(spa
),
2668 zil_claim
, tx
, DS_FIND_CHILDREN
);
2671 spa
->spa_claiming
= B_FALSE
;
2673 spa_set_log_state(spa
, SPA_LOG_GOOD
);
2674 spa
->spa_sync_on
= B_TRUE
;
2675 txg_sync_start(spa
->spa_dsl_pool
);
2678 * Wait for all claims to sync. We sync up to the highest
2679 * claimed log block birth time so that claimed log blocks
2680 * don't appear to be from the future. spa_claim_max_txg
2681 * will have been set for us by either zil_check_log_chain()
2682 * (invoked from spa_check_logs()) or zil_claim() above.
2684 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
2687 * If the config cache is stale, or we have uninitialized
2688 * metaslabs (see spa_vdev_add()), then update the config.
2690 * If this is a verbatim import, trust the current
2691 * in-core spa_config and update the disk labels.
2693 if (config_cache_txg
!= spa
->spa_config_txg
||
2694 state
== SPA_LOAD_IMPORT
||
2695 state
== SPA_LOAD_RECOVER
||
2696 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
2697 need_update
= B_TRUE
;
2699 for (c
= 0; c
< rvd
->vdev_children
; c
++)
2700 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
2701 need_update
= B_TRUE
;
2704 * Update the config cache asychronously in case we're the
2705 * root pool, in which case the config cache isn't writable yet.
2708 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
2711 * Check all DTLs to see if anything needs resilvering.
2713 if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
2714 vdev_resilver_needed(rvd
, NULL
, NULL
))
2715 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
2718 * Delete any inconsistent datasets.
2720 (void) dmu_objset_find(spa_name(spa
),
2721 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
2724 * Clean up any stale temporary dataset userrefs.
2726 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
2733 spa_load_retry(spa_t
*spa
, spa_load_state_t state
, int mosconfig
)
2735 int mode
= spa
->spa_mode
;
2738 spa_deactivate(spa
);
2740 spa
->spa_load_max_txg
--;
2742 spa_activate(spa
, mode
);
2743 spa_async_suspend(spa
);
2745 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, mosconfig
));
2749 * If spa_load() fails this function will try loading prior txg's. If
2750 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2751 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2752 * function will not rewind the pool and will return the same error as
2756 spa_load_best(spa_t
*spa
, spa_load_state_t state
, int mosconfig
,
2757 uint64_t max_request
, int rewind_flags
)
2759 nvlist_t
*loadinfo
= NULL
;
2760 nvlist_t
*config
= NULL
;
2761 int load_error
, rewind_error
;
2762 uint64_t safe_rewind_txg
;
2765 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
2766 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
2767 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2769 spa
->spa_load_max_txg
= max_request
;
2772 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
,
2774 if (load_error
== 0)
2777 if (spa
->spa_root_vdev
!= NULL
)
2778 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
2780 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
2781 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2783 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
2784 nvlist_free(config
);
2785 return (load_error
);
2788 if (state
== SPA_LOAD_RECOVER
) {
2789 /* Price of rolling back is discarding txgs, including log */
2790 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2793 * If we aren't rolling back save the load info from our first
2794 * import attempt so that we can restore it after attempting
2797 loadinfo
= spa
->spa_load_info
;
2798 spa
->spa_load_info
= fnvlist_alloc();
2801 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
2802 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
2803 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
2804 TXG_INITIAL
: safe_rewind_txg
;
2807 * Continue as long as we're finding errors, we're still within
2808 * the acceptable rewind range, and we're still finding uberblocks
2810 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
2811 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
2812 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
2813 spa
->spa_extreme_rewind
= B_TRUE
;
2814 rewind_error
= spa_load_retry(spa
, state
, mosconfig
);
2817 spa
->spa_extreme_rewind
= B_FALSE
;
2818 spa
->spa_load_max_txg
= UINT64_MAX
;
2820 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
2821 spa_config_set(spa
, config
);
2823 if (state
== SPA_LOAD_RECOVER
) {
2824 ASSERT3P(loadinfo
, ==, NULL
);
2825 return (rewind_error
);
2827 /* Store the rewind info as part of the initial load info */
2828 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
2829 spa
->spa_load_info
);
2831 /* Restore the initial load info */
2832 fnvlist_free(spa
->spa_load_info
);
2833 spa
->spa_load_info
= loadinfo
;
2835 return (load_error
);
2842 * The import case is identical to an open except that the configuration is sent
2843 * down from userland, instead of grabbed from the configuration cache. For the
2844 * case of an open, the pool configuration will exist in the
2845 * POOL_STATE_UNINITIALIZED state.
2847 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2848 * the same time open the pool, without having to keep around the spa_t in some
2852 spa_open_common(const char *pool
, spa_t
**spapp
, void *tag
, nvlist_t
*nvpolicy
,
2856 spa_load_state_t state
= SPA_LOAD_OPEN
;
2858 int locked
= B_FALSE
;
2863 * As disgusting as this is, we need to support recursive calls to this
2864 * function because dsl_dir_open() is called during spa_load(), and ends
2865 * up calling spa_open() again. The real fix is to figure out how to
2866 * avoid dsl_dir_open() calling this in the first place.
2868 if (mutex_owner(&spa_namespace_lock
) != curthread
) {
2869 mutex_enter(&spa_namespace_lock
);
2873 if ((spa
= spa_lookup(pool
)) == NULL
) {
2875 mutex_exit(&spa_namespace_lock
);
2879 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
2880 zpool_rewind_policy_t policy
;
2882 zpool_get_rewind_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
2884 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
2885 state
= SPA_LOAD_RECOVER
;
2887 spa_activate(spa
, spa_mode_global
);
2889 if (state
!= SPA_LOAD_RECOVER
)
2890 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
2892 error
= spa_load_best(spa
, state
, B_FALSE
, policy
.zrp_txg
,
2893 policy
.zrp_request
);
2895 if (error
== EBADF
) {
2897 * If vdev_validate() returns failure (indicated by
2898 * EBADF), it indicates that one of the vdevs indicates
2899 * that the pool has been exported or destroyed. If
2900 * this is the case, the config cache is out of sync and
2901 * we should remove the pool from the namespace.
2904 spa_deactivate(spa
);
2905 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
2908 mutex_exit(&spa_namespace_lock
);
2914 * We can't open the pool, but we still have useful
2915 * information: the state of each vdev after the
2916 * attempted vdev_open(). Return this to the user.
2918 if (config
!= NULL
&& spa
->spa_config
) {
2919 VERIFY(nvlist_dup(spa
->spa_config
, config
,
2921 VERIFY(nvlist_add_nvlist(*config
,
2922 ZPOOL_CONFIG_LOAD_INFO
,
2923 spa
->spa_load_info
) == 0);
2926 spa_deactivate(spa
);
2927 spa
->spa_last_open_failed
= error
;
2929 mutex_exit(&spa_namespace_lock
);
2935 spa_open_ref(spa
, tag
);
2938 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
2941 * If we've recovered the pool, pass back any information we
2942 * gathered while doing the load.
2944 if (state
== SPA_LOAD_RECOVER
) {
2945 VERIFY(nvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
2946 spa
->spa_load_info
) == 0);
2950 spa
->spa_last_open_failed
= 0;
2951 spa
->spa_last_ubsync_txg
= 0;
2952 spa
->spa_load_txg
= 0;
2953 mutex_exit(&spa_namespace_lock
);
2962 spa_open_rewind(const char *name
, spa_t
**spapp
, void *tag
, nvlist_t
*policy
,
2965 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
2969 spa_open(const char *name
, spa_t
**spapp
, void *tag
)
2971 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
2975 * Lookup the given spa_t, incrementing the inject count in the process,
2976 * preventing it from being exported or destroyed.
2979 spa_inject_addref(char *name
)
2983 mutex_enter(&spa_namespace_lock
);
2984 if ((spa
= spa_lookup(name
)) == NULL
) {
2985 mutex_exit(&spa_namespace_lock
);
2988 spa
->spa_inject_ref
++;
2989 mutex_exit(&spa_namespace_lock
);
2995 spa_inject_delref(spa_t
*spa
)
2997 mutex_enter(&spa_namespace_lock
);
2998 spa
->spa_inject_ref
--;
2999 mutex_exit(&spa_namespace_lock
);
3003 * Add spares device information to the nvlist.
3006 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
3016 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3018 if (spa
->spa_spares
.sav_count
== 0)
3021 VERIFY(nvlist_lookup_nvlist(config
,
3022 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3023 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
3024 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3026 VERIFY(nvlist_add_nvlist_array(nvroot
,
3027 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3028 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3029 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3032 * Go through and find any spares which have since been
3033 * repurposed as an active spare. If this is the case, update
3034 * their status appropriately.
3036 for (i
= 0; i
< nspares
; i
++) {
3037 VERIFY(nvlist_lookup_uint64(spares
[i
],
3038 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3039 if (spa_spare_exists(guid
, &pool
, NULL
) &&
3041 VERIFY(nvlist_lookup_uint64_array(
3042 spares
[i
], ZPOOL_CONFIG_VDEV_STATS
,
3043 (uint64_t **)&vs
, &vsc
) == 0);
3044 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
3045 vs
->vs_aux
= VDEV_AUX_SPARED
;
3052 * Add l2cache device information to the nvlist, including vdev stats.
3055 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
3058 uint_t i
, j
, nl2cache
;
3065 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3067 if (spa
->spa_l2cache
.sav_count
== 0)
3070 VERIFY(nvlist_lookup_nvlist(config
,
3071 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3072 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
3073 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3074 if (nl2cache
!= 0) {
3075 VERIFY(nvlist_add_nvlist_array(nvroot
,
3076 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3077 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3078 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3081 * Update level 2 cache device stats.
3084 for (i
= 0; i
< nl2cache
; i
++) {
3085 VERIFY(nvlist_lookup_uint64(l2cache
[i
],
3086 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3089 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
3091 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
3092 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
3098 VERIFY(nvlist_lookup_uint64_array(l2cache
[i
],
3099 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
3101 vdev_get_stats(vd
, vs
);
3107 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
3113 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3114 VERIFY(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3116 if (spa
->spa_feat_for_read_obj
!= 0) {
3117 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3118 spa
->spa_feat_for_read_obj
);
3119 zap_cursor_retrieve(&zc
, &za
) == 0;
3120 zap_cursor_advance(&zc
)) {
3121 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3122 za
.za_num_integers
== 1);
3123 VERIFY3U(0, ==, nvlist_add_uint64(features
, za
.za_name
,
3124 za
.za_first_integer
));
3126 zap_cursor_fini(&zc
);
3129 if (spa
->spa_feat_for_write_obj
!= 0) {
3130 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3131 spa
->spa_feat_for_write_obj
);
3132 zap_cursor_retrieve(&zc
, &za
) == 0;
3133 zap_cursor_advance(&zc
)) {
3134 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3135 za
.za_num_integers
== 1);
3136 VERIFY3U(0, ==, nvlist_add_uint64(features
, za
.za_name
,
3137 za
.za_first_integer
));
3139 zap_cursor_fini(&zc
);
3142 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
3144 nvlist_free(features
);
3148 spa_get_stats(const char *name
, nvlist_t
**config
,
3149 char *altroot
, size_t buflen
)
3155 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
3159 * This still leaves a window of inconsistency where the spares
3160 * or l2cache devices could change and the config would be
3161 * self-inconsistent.
3163 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
3165 if (*config
!= NULL
) {
3166 uint64_t loadtimes
[2];
3168 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
3169 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
3170 VERIFY(nvlist_add_uint64_array(*config
,
3171 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2) == 0);
3173 VERIFY(nvlist_add_uint64(*config
,
3174 ZPOOL_CONFIG_ERRCOUNT
,
3175 spa_get_errlog_size(spa
)) == 0);
3177 if (spa_suspended(spa
))
3178 VERIFY(nvlist_add_uint64(*config
,
3179 ZPOOL_CONFIG_SUSPENDED
,
3180 spa
->spa_failmode
) == 0);
3182 spa_add_spares(spa
, *config
);
3183 spa_add_l2cache(spa
, *config
);
3184 spa_add_feature_stats(spa
, *config
);
3189 * We want to get the alternate root even for faulted pools, so we cheat
3190 * and call spa_lookup() directly.
3194 mutex_enter(&spa_namespace_lock
);
3195 spa
= spa_lookup(name
);
3197 spa_altroot(spa
, altroot
, buflen
);
3201 mutex_exit(&spa_namespace_lock
);
3203 spa_altroot(spa
, altroot
, buflen
);
3208 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
3209 spa_close(spa
, FTAG
);
3216 * Validate that the auxiliary device array is well formed. We must have an
3217 * array of nvlists, each which describes a valid leaf vdev. If this is an
3218 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3219 * specified, as long as they are well-formed.
3222 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
3223 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
3224 vdev_labeltype_t label
)
3231 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3234 * It's acceptable to have no devs specified.
3236 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
3243 * Make sure the pool is formatted with a version that supports this
3246 if (spa_version(spa
) < version
)
3250 * Set the pending device list so we correctly handle device in-use
3253 sav
->sav_pending
= dev
;
3254 sav
->sav_npending
= ndev
;
3256 for (i
= 0; i
< ndev
; i
++) {
3257 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
3261 if (!vd
->vdev_ops
->vdev_op_leaf
) {
3268 * The L2ARC currently only supports disk devices in
3269 * kernel context. For user-level testing, we allow it.
3272 if ((strcmp(config
, ZPOOL_CONFIG_L2CACHE
) == 0) &&
3273 strcmp(vd
->vdev_ops
->vdev_op_type
, VDEV_TYPE_DISK
) != 0) {
3281 if ((error
= vdev_open(vd
)) == 0 &&
3282 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
3283 VERIFY(nvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
3284 vd
->vdev_guid
) == 0);
3290 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
3297 sav
->sav_pending
= NULL
;
3298 sav
->sav_npending
= 0;
3303 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
3307 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3309 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3310 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
3311 VDEV_LABEL_SPARE
)) != 0) {
3315 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3316 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
3317 VDEV_LABEL_L2CACHE
));
3321 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
3326 if (sav
->sav_config
!= NULL
) {
3332 * Generate new dev list by concatentating with the
3335 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
3336 &olddevs
, &oldndevs
) == 0);
3338 newdevs
= kmem_alloc(sizeof (void *) *
3339 (ndevs
+ oldndevs
), KM_PUSHPAGE
);
3340 for (i
= 0; i
< oldndevs
; i
++)
3341 VERIFY(nvlist_dup(olddevs
[i
], &newdevs
[i
],
3343 for (i
= 0; i
< ndevs
; i
++)
3344 VERIFY(nvlist_dup(devs
[i
], &newdevs
[i
+ oldndevs
],
3347 VERIFY(nvlist_remove(sav
->sav_config
, config
,
3348 DATA_TYPE_NVLIST_ARRAY
) == 0);
3350 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
3351 config
, newdevs
, ndevs
+ oldndevs
) == 0);
3352 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
3353 nvlist_free(newdevs
[i
]);
3354 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
3357 * Generate a new dev list.
3359 VERIFY(nvlist_alloc(&sav
->sav_config
, NV_UNIQUE_NAME
,
3361 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
, config
,
3367 * Stop and drop level 2 ARC devices
3370 spa_l2cache_drop(spa_t
*spa
)
3374 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
3376 for (i
= 0; i
< sav
->sav_count
; i
++) {
3379 vd
= sav
->sav_vdevs
[i
];
3382 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
3383 pool
!= 0ULL && l2arc_vdev_present(vd
))
3384 l2arc_remove_vdev(vd
);
3392 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
3393 const char *history_str
, nvlist_t
*zplprops
)
3396 char *altroot
= NULL
;
3401 uint64_t txg
= TXG_INITIAL
;
3402 nvlist_t
**spares
, **l2cache
;
3403 uint_t nspares
, nl2cache
;
3404 uint64_t version
, obj
;
3405 boolean_t has_features
;
3410 * If this pool already exists, return failure.
3412 mutex_enter(&spa_namespace_lock
);
3413 if (spa_lookup(pool
) != NULL
) {
3414 mutex_exit(&spa_namespace_lock
);
3419 * Allocate a new spa_t structure.
3421 (void) nvlist_lookup_string(props
,
3422 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
3423 spa
= spa_add(pool
, NULL
, altroot
);
3424 spa_activate(spa
, spa_mode_global
);
3426 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
3427 spa_deactivate(spa
);
3429 mutex_exit(&spa_namespace_lock
);
3433 has_features
= B_FALSE
;
3434 for (elem
= nvlist_next_nvpair(props
, NULL
);
3435 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
3436 if (zpool_prop_feature(nvpair_name(elem
)))
3437 has_features
= B_TRUE
;
3440 if (has_features
|| nvlist_lookup_uint64(props
,
3441 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
3442 version
= SPA_VERSION
;
3444 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
3446 spa
->spa_first_txg
= txg
;
3447 spa
->spa_uberblock
.ub_txg
= txg
- 1;
3448 spa
->spa_uberblock
.ub_version
= version
;
3449 spa
->spa_ubsync
= spa
->spa_uberblock
;
3452 * Create "The Godfather" zio to hold all async IOs
3454 spa
->spa_async_zio_root
= zio_root(spa
, NULL
, NULL
,
3455 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_GODFATHER
);
3458 * Create the root vdev.
3460 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3462 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
3464 ASSERT(error
!= 0 || rvd
!= NULL
);
3465 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
3467 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
3471 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
3472 (error
= spa_validate_aux(spa
, nvroot
, txg
,
3473 VDEV_ALLOC_ADD
)) == 0) {
3474 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
3475 vdev_metaslab_set_size(rvd
->vdev_child
[c
]);
3476 vdev_expand(rvd
->vdev_child
[c
], txg
);
3480 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3484 spa_deactivate(spa
);
3486 mutex_exit(&spa_namespace_lock
);
3491 * Get the list of spares, if specified.
3493 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
3494 &spares
, &nspares
) == 0) {
3495 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
, NV_UNIQUE_NAME
,
3497 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
3498 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3499 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3500 spa_load_spares(spa
);
3501 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3502 spa
->spa_spares
.sav_sync
= B_TRUE
;
3506 * Get the list of level 2 cache devices, if specified.
3508 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
3509 &l2cache
, &nl2cache
) == 0) {
3510 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
3511 NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
3512 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
3513 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3514 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3515 spa_load_l2cache(spa
);
3516 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3517 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3520 spa
->spa_is_initializing
= B_TRUE
;
3521 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, txg
);
3522 spa
->spa_meta_objset
= dp
->dp_meta_objset
;
3523 spa
->spa_is_initializing
= B_FALSE
;
3526 * Create DDTs (dedup tables).
3530 spa_update_dspace(spa
);
3532 tx
= dmu_tx_create_assigned(dp
, txg
);
3535 * Create the pool config object.
3537 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
3538 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
3539 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
3541 if (zap_add(spa
->spa_meta_objset
,
3542 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
3543 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
3544 cmn_err(CE_PANIC
, "failed to add pool config");
3547 if (spa_version(spa
) >= SPA_VERSION_FEATURES
)
3548 spa_feature_create_zap_objects(spa
, tx
);
3550 if (zap_add(spa
->spa_meta_objset
,
3551 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
3552 sizeof (uint64_t), 1, &version
, tx
) != 0) {
3553 cmn_err(CE_PANIC
, "failed to add pool version");
3556 /* Newly created pools with the right version are always deflated. */
3557 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
3558 spa
->spa_deflate
= TRUE
;
3559 if (zap_add(spa
->spa_meta_objset
,
3560 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
3561 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
3562 cmn_err(CE_PANIC
, "failed to add deflate");
3567 * Create the deferred-free bpobj. Turn off compression
3568 * because sync-to-convergence takes longer if the blocksize
3571 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
3572 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
3573 ZIO_COMPRESS_OFF
, tx
);
3574 if (zap_add(spa
->spa_meta_objset
,
3575 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
3576 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
3577 cmn_err(CE_PANIC
, "failed to add bpobj");
3579 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
3580 spa
->spa_meta_objset
, obj
));
3583 * Create the pool's history object.
3585 if (version
>= SPA_VERSION_ZPOOL_HISTORY
)
3586 spa_history_create_obj(spa
, tx
);
3589 * Set pool properties.
3591 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
3592 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
3593 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
3594 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
3596 if (props
!= NULL
) {
3597 spa_configfile_set(spa
, props
, B_FALSE
);
3598 spa_sync_props(spa
, props
, tx
);
3603 spa
->spa_sync_on
= B_TRUE
;
3604 txg_sync_start(spa
->spa_dsl_pool
);
3607 * We explicitly wait for the first transaction to complete so that our
3608 * bean counters are appropriately updated.
3610 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
3612 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
3614 if (version
>= SPA_VERSION_ZPOOL_HISTORY
&& history_str
!= NULL
)
3615 (void) spa_history_log(spa
, history_str
, LOG_CMD_POOL_CREATE
);
3616 spa_history_log_version(spa
, LOG_POOL_CREATE
);
3618 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
3620 mutex_exit(&spa_namespace_lock
);
3627 * Get the root pool information from the root disk, then import the root pool
3628 * during the system boot up time.
3630 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t
**);
3633 spa_generate_rootconf(char *devpath
, char *devid
, uint64_t *guid
)
3636 nvlist_t
*nvtop
, *nvroot
;
3639 if (vdev_disk_read_rootlabel(devpath
, devid
, &config
) != 0)
3643 * Add this top-level vdev to the child array.
3645 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3647 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
3649 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_GUID
, guid
) == 0);
3652 * Put this pool's top-level vdevs into a root vdev.
3654 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
3655 VERIFY(nvlist_add_string(nvroot
, ZPOOL_CONFIG_TYPE
,
3656 VDEV_TYPE_ROOT
) == 0);
3657 VERIFY(nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_ID
, 0ULL) == 0);
3658 VERIFY(nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_GUID
, pgid
) == 0);
3659 VERIFY(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
3663 * Replace the existing vdev_tree with the new root vdev in
3664 * this pool's configuration (remove the old, add the new).
3666 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, nvroot
) == 0);
3667 nvlist_free(nvroot
);
3672 * Walk the vdev tree and see if we can find a device with "better"
3673 * configuration. A configuration is "better" if the label on that
3674 * device has a more recent txg.
3677 spa_alt_rootvdev(vdev_t
*vd
, vdev_t
**avd
, uint64_t *txg
)
3681 for (c
= 0; c
< vd
->vdev_children
; c
++)
3682 spa_alt_rootvdev(vd
->vdev_child
[c
], avd
, txg
);
3684 if (vd
->vdev_ops
->vdev_op_leaf
) {
3688 if (vdev_disk_read_rootlabel(vd
->vdev_physpath
, vd
->vdev_devid
,
3692 VERIFY(nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_TXG
,
3696 * Do we have a better boot device?
3698 if (label_txg
> *txg
) {
3707 * Import a root pool.
3709 * For x86. devpath_list will consist of devid and/or physpath name of
3710 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3711 * The GRUB "findroot" command will return the vdev we should boot.
3713 * For Sparc, devpath_list consists the physpath name of the booting device
3714 * no matter the rootpool is a single device pool or a mirrored pool.
3716 * "/pci@1f,0/ide@d/disk@0,0:a"
3719 spa_import_rootpool(char *devpath
, char *devid
)
3722 vdev_t
*rvd
, *bvd
, *avd
= NULL
;
3723 nvlist_t
*config
, *nvtop
;
3729 * Read the label from the boot device and generate a configuration.
3731 config
= spa_generate_rootconf(devpath
, devid
, &guid
);
3732 #if defined(_OBP) && defined(_KERNEL)
3733 if (config
== NULL
) {
3734 if (strstr(devpath
, "/iscsi/ssd") != NULL
) {
3736 get_iscsi_bootpath_phy(devpath
);
3737 config
= spa_generate_rootconf(devpath
, devid
, &guid
);
3741 if (config
== NULL
) {
3742 cmn_err(CE_NOTE
, "Cannot read the pool label from '%s'",
3747 VERIFY(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
3749 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
, &txg
) == 0);
3751 mutex_enter(&spa_namespace_lock
);
3752 if ((spa
= spa_lookup(pname
)) != NULL
) {
3754 * Remove the existing root pool from the namespace so that we
3755 * can replace it with the correct config we just read in.
3760 spa
= spa_add(pname
, config
, NULL
);
3761 spa
->spa_is_root
= B_TRUE
;
3762 spa
->spa_import_flags
= ZFS_IMPORT_VERBATIM
;
3765 * Build up a vdev tree based on the boot device's label config.
3767 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3769 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3770 error
= spa_config_parse(spa
, &rvd
, nvtop
, NULL
, 0,
3771 VDEV_ALLOC_ROOTPOOL
);
3772 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3774 mutex_exit(&spa_namespace_lock
);
3775 nvlist_free(config
);
3776 cmn_err(CE_NOTE
, "Can not parse the config for pool '%s'",
3782 * Get the boot vdev.
3784 if ((bvd
= vdev_lookup_by_guid(rvd
, guid
)) == NULL
) {
3785 cmn_err(CE_NOTE
, "Can not find the boot vdev for guid %llu",
3786 (u_longlong_t
)guid
);
3792 * Determine if there is a better boot device.
3795 spa_alt_rootvdev(rvd
, &avd
, &txg
);
3797 cmn_err(CE_NOTE
, "The boot device is 'degraded'. Please "
3798 "try booting from '%s'", avd
->vdev_path
);
3804 * If the boot device is part of a spare vdev then ensure that
3805 * we're booting off the active spare.
3807 if (bvd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
3808 !bvd
->vdev_isspare
) {
3809 cmn_err(CE_NOTE
, "The boot device is currently spared. Please "
3810 "try booting from '%s'",
3812 vdev_child
[bvd
->vdev_parent
->vdev_children
- 1]->vdev_path
);
3818 spa_history_log_version(spa
, LOG_POOL_IMPORT
);
3820 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3822 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3823 mutex_exit(&spa_namespace_lock
);
3825 nvlist_free(config
);
3832 * Import a non-root pool into the system.
3835 spa_import(const char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
3838 char *altroot
= NULL
;
3839 spa_load_state_t state
= SPA_LOAD_IMPORT
;
3840 zpool_rewind_policy_t policy
;
3841 uint64_t mode
= spa_mode_global
;
3842 uint64_t readonly
= B_FALSE
;
3845 nvlist_t
**spares
, **l2cache
;
3846 uint_t nspares
, nl2cache
;
3849 * If a pool with this name exists, return failure.
3851 mutex_enter(&spa_namespace_lock
);
3852 if (spa_lookup(pool
) != NULL
) {
3853 mutex_exit(&spa_namespace_lock
);
3858 * Create and initialize the spa structure.
3860 (void) nvlist_lookup_string(props
,
3861 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
3862 (void) nvlist_lookup_uint64(props
,
3863 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
3866 spa
= spa_add(pool
, config
, altroot
);
3867 spa
->spa_import_flags
= flags
;
3870 * Verbatim import - Take a pool and insert it into the namespace
3871 * as if it had been loaded at boot.
3873 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
3875 spa_configfile_set(spa
, props
, B_FALSE
);
3877 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
3879 mutex_exit(&spa_namespace_lock
);
3880 spa_history_log_version(spa
, LOG_POOL_IMPORT
);
3885 spa_activate(spa
, mode
);
3888 * Don't start async tasks until we know everything is healthy.
3890 spa_async_suspend(spa
);
3892 zpool_get_rewind_policy(config
, &policy
);
3893 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
3894 state
= SPA_LOAD_RECOVER
;
3897 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
3898 * because the user-supplied config is actually the one to trust when
3901 if (state
!= SPA_LOAD_RECOVER
)
3902 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
3904 error
= spa_load_best(spa
, state
, B_TRUE
, policy
.zrp_txg
,
3905 policy
.zrp_request
);
3908 * Propagate anything learned while loading the pool and pass it
3909 * back to caller (i.e. rewind info, missing devices, etc).
3911 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
3912 spa
->spa_load_info
) == 0);
3914 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3916 * Toss any existing sparelist, as it doesn't have any validity
3917 * anymore, and conflicts with spa_has_spare().
3919 if (spa
->spa_spares
.sav_config
) {
3920 nvlist_free(spa
->spa_spares
.sav_config
);
3921 spa
->spa_spares
.sav_config
= NULL
;
3922 spa_load_spares(spa
);
3924 if (spa
->spa_l2cache
.sav_config
) {
3925 nvlist_free(spa
->spa_l2cache
.sav_config
);
3926 spa
->spa_l2cache
.sav_config
= NULL
;
3927 spa_load_l2cache(spa
);
3930 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3933 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
3936 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
3937 VDEV_ALLOC_L2CACHE
);
3938 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3941 spa_configfile_set(spa
, props
, B_FALSE
);
3943 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
3944 (error
= spa_prop_set(spa
, props
)))) {
3946 spa_deactivate(spa
);
3948 mutex_exit(&spa_namespace_lock
);
3952 spa_async_resume(spa
);
3955 * Override any spares and level 2 cache devices as specified by
3956 * the user, as these may have correct device names/devids, etc.
3958 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
3959 &spares
, &nspares
) == 0) {
3960 if (spa
->spa_spares
.sav_config
)
3961 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
,
3962 ZPOOL_CONFIG_SPARES
, DATA_TYPE_NVLIST_ARRAY
) == 0);
3964 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
,
3965 NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
3966 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
3967 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3968 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3969 spa_load_spares(spa
);
3970 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3971 spa
->spa_spares
.sav_sync
= B_TRUE
;
3973 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
3974 &l2cache
, &nl2cache
) == 0) {
3975 if (spa
->spa_l2cache
.sav_config
)
3976 VERIFY(nvlist_remove(spa
->spa_l2cache
.sav_config
,
3977 ZPOOL_CONFIG_L2CACHE
, DATA_TYPE_NVLIST_ARRAY
) == 0);
3979 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
3980 NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
3981 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
3982 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3983 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3984 spa_load_l2cache(spa
);
3985 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3986 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3990 * Check for any removed devices.
3992 if (spa
->spa_autoreplace
) {
3993 spa_aux_check_removed(&spa
->spa_spares
);
3994 spa_aux_check_removed(&spa
->spa_l2cache
);
3997 if (spa_writeable(spa
)) {
3999 * Update the config cache to include the newly-imported pool.
4001 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4005 * It's possible that the pool was expanded while it was exported.
4006 * We kick off an async task to handle this for us.
4008 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
4010 mutex_exit(&spa_namespace_lock
);
4011 spa_history_log_version(spa
, LOG_POOL_IMPORT
);
4017 spa_tryimport(nvlist_t
*tryconfig
)
4019 nvlist_t
*config
= NULL
;
4025 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
4028 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
4032 * Create and initialize the spa structure.
4034 mutex_enter(&spa_namespace_lock
);
4035 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
4036 spa_activate(spa
, FREAD
);
4039 * Pass off the heavy lifting to spa_load().
4040 * Pass TRUE for mosconfig because the user-supplied config
4041 * is actually the one to trust when doing an import.
4043 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
, B_TRUE
);
4046 * If 'tryconfig' was at least parsable, return the current config.
4048 if (spa
->spa_root_vdev
!= NULL
) {
4049 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4050 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
,
4052 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
4054 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
4055 spa
->spa_uberblock
.ub_timestamp
) == 0);
4056 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4057 spa
->spa_load_info
) == 0);
4060 * If the bootfs property exists on this pool then we
4061 * copy it out so that external consumers can tell which
4062 * pools are bootable.
4064 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
4065 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_PUSHPAGE
);
4068 * We have to play games with the name since the
4069 * pool was opened as TRYIMPORT_NAME.
4071 if (dsl_dsobj_to_dsname(spa_name(spa
),
4072 spa
->spa_bootfs
, tmpname
) == 0) {
4074 char *dsname
= kmem_alloc(MAXPATHLEN
, KM_PUSHPAGE
);
4076 cp
= strchr(tmpname
, '/');
4078 (void) strlcpy(dsname
, tmpname
,
4081 (void) snprintf(dsname
, MAXPATHLEN
,
4082 "%s/%s", poolname
, ++cp
);
4084 VERIFY(nvlist_add_string(config
,
4085 ZPOOL_CONFIG_BOOTFS
, dsname
) == 0);
4086 kmem_free(dsname
, MAXPATHLEN
);
4088 kmem_free(tmpname
, MAXPATHLEN
);
4092 * Add the list of hot spares and level 2 cache devices.
4094 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4095 spa_add_spares(spa
, config
);
4096 spa_add_l2cache(spa
, config
);
4097 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4101 spa_deactivate(spa
);
4103 mutex_exit(&spa_namespace_lock
);
4109 * Pool export/destroy
4111 * The act of destroying or exporting a pool is very simple. We make sure there
4112 * is no more pending I/O and any references to the pool are gone. Then, we
4113 * update the pool state and sync all the labels to disk, removing the
4114 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4115 * we don't sync the labels or remove the configuration cache.
4118 spa_export_common(char *pool
, int new_state
, nvlist_t
**oldconfig
,
4119 boolean_t force
, boolean_t hardforce
)
4126 if (!(spa_mode_global
& FWRITE
))
4129 mutex_enter(&spa_namespace_lock
);
4130 if ((spa
= spa_lookup(pool
)) == NULL
) {
4131 mutex_exit(&spa_namespace_lock
);
4136 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4137 * reacquire the namespace lock, and see if we can export.
4139 spa_open_ref(spa
, FTAG
);
4140 mutex_exit(&spa_namespace_lock
);
4141 spa_async_suspend(spa
);
4142 mutex_enter(&spa_namespace_lock
);
4143 spa_close(spa
, FTAG
);
4146 * The pool will be in core if it's openable,
4147 * in which case we can modify its state.
4149 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
&& spa
->spa_sync_on
) {
4151 * Objsets may be open only because they're dirty, so we
4152 * have to force it to sync before checking spa_refcnt.
4154 txg_wait_synced(spa
->spa_dsl_pool
, 0);
4157 * A pool cannot be exported or destroyed if there are active
4158 * references. If we are resetting a pool, allow references by
4159 * fault injection handlers.
4161 if (!spa_refcount_zero(spa
) ||
4162 (spa
->spa_inject_ref
!= 0 &&
4163 new_state
!= POOL_STATE_UNINITIALIZED
)) {
4164 spa_async_resume(spa
);
4165 mutex_exit(&spa_namespace_lock
);
4170 * A pool cannot be exported if it has an active shared spare.
4171 * This is to prevent other pools stealing the active spare
4172 * from an exported pool. At user's own will, such pool can
4173 * be forcedly exported.
4175 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
4176 spa_has_active_shared_spare(spa
)) {
4177 spa_async_resume(spa
);
4178 mutex_exit(&spa_namespace_lock
);
4183 * We want this to be reflected on every label,
4184 * so mark them all dirty. spa_unload() will do the
4185 * final sync that pushes these changes out.
4187 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
4188 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4189 spa
->spa_state
= new_state
;
4190 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
4192 vdev_config_dirty(spa
->spa_root_vdev
);
4193 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4197 spa_event_notify(spa
, NULL
, FM_EREPORT_ZFS_POOL_DESTROY
);
4199 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
4201 spa_deactivate(spa
);
4204 if (oldconfig
&& spa
->spa_config
)
4205 VERIFY(nvlist_dup(spa
->spa_config
, oldconfig
, 0) == 0);
4207 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
4209 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
4212 mutex_exit(&spa_namespace_lock
);
4218 * Destroy a storage pool.
4221 spa_destroy(char *pool
)
4223 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
4228 * Export a storage pool.
4231 spa_export(char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
4232 boolean_t hardforce
)
4234 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
4239 * Similar to spa_export(), this unloads the spa_t without actually removing it
4240 * from the namespace in any way.
4243 spa_reset(char *pool
)
4245 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
4250 * ==========================================================================
4251 * Device manipulation
4252 * ==========================================================================
4256 * Add a device to a storage pool.
4259 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
4263 vdev_t
*rvd
= spa
->spa_root_vdev
;
4265 nvlist_t
**spares
, **l2cache
;
4266 uint_t nspares
, nl2cache
;
4269 ASSERT(spa_writeable(spa
));
4271 txg
= spa_vdev_enter(spa
);
4273 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
4274 VDEV_ALLOC_ADD
)) != 0)
4275 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4277 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
4279 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
4283 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
4287 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
4288 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
4290 if (vd
->vdev_children
!= 0 &&
4291 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0)
4292 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4295 * We must validate the spares and l2cache devices after checking the
4296 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4298 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
4299 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4302 * Transfer each new top-level vdev from vd to rvd.
4304 for (c
= 0; c
< vd
->vdev_children
; c
++) {
4307 * Set the vdev id to the first hole, if one exists.
4309 for (id
= 0; id
< rvd
->vdev_children
; id
++) {
4310 if (rvd
->vdev_child
[id
]->vdev_ishole
) {
4311 vdev_free(rvd
->vdev_child
[id
]);
4315 tvd
= vd
->vdev_child
[c
];
4316 vdev_remove_child(vd
, tvd
);
4318 vdev_add_child(rvd
, tvd
);
4319 vdev_config_dirty(tvd
);
4323 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
4324 ZPOOL_CONFIG_SPARES
);
4325 spa_load_spares(spa
);
4326 spa
->spa_spares
.sav_sync
= B_TRUE
;
4329 if (nl2cache
!= 0) {
4330 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
4331 ZPOOL_CONFIG_L2CACHE
);
4332 spa_load_l2cache(spa
);
4333 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4337 * We have to be careful when adding new vdevs to an existing pool.
4338 * If other threads start allocating from these vdevs before we
4339 * sync the config cache, and we lose power, then upon reboot we may
4340 * fail to open the pool because there are DVAs that the config cache
4341 * can't translate. Therefore, we first add the vdevs without
4342 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4343 * and then let spa_config_update() initialize the new metaslabs.
4345 * spa_load() checks for added-but-not-initialized vdevs, so that
4346 * if we lose power at any point in this sequence, the remaining
4347 * steps will be completed the next time we load the pool.
4349 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
4351 mutex_enter(&spa_namespace_lock
);
4352 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4353 mutex_exit(&spa_namespace_lock
);
4359 * Attach a device to a mirror. The arguments are the path to any device
4360 * in the mirror, and the nvroot for the new device. If the path specifies
4361 * a device that is not mirrored, we automatically insert the mirror vdev.
4363 * If 'replacing' is specified, the new device is intended to replace the
4364 * existing device; in this case the two devices are made into their own
4365 * mirror using the 'replacing' vdev, which is functionally identical to
4366 * the mirror vdev (it actually reuses all the same ops) but has a few
4367 * extra rules: you can't attach to it after it's been created, and upon
4368 * completion of resilvering, the first disk (the one being replaced)
4369 * is automatically detached.
4372 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
)
4374 uint64_t txg
, dtl_max_txg
;
4375 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
;)
4376 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
4378 char *oldvdpath
, *newvdpath
;
4382 ASSERT(spa_writeable(spa
));
4384 txg
= spa_vdev_enter(spa
);
4386 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4389 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4391 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
4392 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4394 pvd
= oldvd
->vdev_parent
;
4396 if ((error
= spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
4397 VDEV_ALLOC_ATTACH
)) != 0)
4398 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4400 if (newrootvd
->vdev_children
!= 1)
4401 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4403 newvd
= newrootvd
->vdev_child
[0];
4405 if (!newvd
->vdev_ops
->vdev_op_leaf
)
4406 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4408 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
4409 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
4412 * Spares can't replace logs
4414 if (oldvd
->vdev_top
->vdev_islog
&& newvd
->vdev_isspare
)
4415 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4419 * For attach, the only allowable parent is a mirror or the root
4422 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4423 pvd
->vdev_ops
!= &vdev_root_ops
)
4424 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4426 pvops
= &vdev_mirror_ops
;
4429 * Active hot spares can only be replaced by inactive hot
4432 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4433 oldvd
->vdev_isspare
&&
4434 !spa_has_spare(spa
, newvd
->vdev_guid
))
4435 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4438 * If the source is a hot spare, and the parent isn't already a
4439 * spare, then we want to create a new hot spare. Otherwise, we
4440 * want to create a replacing vdev. The user is not allowed to
4441 * attach to a spared vdev child unless the 'isspare' state is
4442 * the same (spare replaces spare, non-spare replaces
4445 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
4446 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
4447 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4448 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4449 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
4450 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4453 if (newvd
->vdev_isspare
)
4454 pvops
= &vdev_spare_ops
;
4456 pvops
= &vdev_replacing_ops
;
4460 * Make sure the new device is big enough.
4462 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
4463 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
4466 * The new device cannot have a higher alignment requirement
4467 * than the top-level vdev.
4469 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
4470 return (spa_vdev_exit(spa
, newrootvd
, txg
, EDOM
));
4473 * If this is an in-place replacement, update oldvd's path and devid
4474 * to make it distinguishable from newvd, and unopenable from now on.
4476 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
4477 spa_strfree(oldvd
->vdev_path
);
4478 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
4480 (void) sprintf(oldvd
->vdev_path
, "%s/%s",
4481 newvd
->vdev_path
, "old");
4482 if (oldvd
->vdev_devid
!= NULL
) {
4483 spa_strfree(oldvd
->vdev_devid
);
4484 oldvd
->vdev_devid
= NULL
;
4488 /* mark the device being resilvered */
4489 newvd
->vdev_resilvering
= B_TRUE
;
4492 * If the parent is not a mirror, or if we're replacing, insert the new
4493 * mirror/replacing/spare vdev above oldvd.
4495 if (pvd
->vdev_ops
!= pvops
)
4496 pvd
= vdev_add_parent(oldvd
, pvops
);
4498 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
4499 ASSERT(pvd
->vdev_ops
== pvops
);
4500 ASSERT(oldvd
->vdev_parent
== pvd
);
4503 * Extract the new device from its root and add it to pvd.
4505 vdev_remove_child(newrootvd
, newvd
);
4506 newvd
->vdev_id
= pvd
->vdev_children
;
4507 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
4508 vdev_add_child(pvd
, newvd
);
4510 tvd
= newvd
->vdev_top
;
4511 ASSERT(pvd
->vdev_top
== tvd
);
4512 ASSERT(tvd
->vdev_parent
== rvd
);
4514 vdev_config_dirty(tvd
);
4517 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4518 * for any dmu_sync-ed blocks. It will propagate upward when
4519 * spa_vdev_exit() calls vdev_dtl_reassess().
4521 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
4523 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
4524 dtl_max_txg
- TXG_INITIAL
);
4526 if (newvd
->vdev_isspare
) {
4527 spa_spare_activate(newvd
);
4528 spa_event_notify(spa
, newvd
, FM_EREPORT_ZFS_DEVICE_SPARE
);
4531 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
4532 newvdpath
= spa_strdup(newvd
->vdev_path
);
4533 newvd_isspare
= newvd
->vdev_isspare
;
4536 * Mark newvd's DTL dirty in this txg.
4538 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
4541 * Restart the resilver
4543 dsl_resilver_restart(spa
->spa_dsl_pool
, dtl_max_txg
);
4548 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
4550 spa_history_log_internal(LOG_POOL_VDEV_ATTACH
, spa
, NULL
,
4551 "%s vdev=%s %s vdev=%s",
4552 replacing
&& newvd_isspare
? "spare in" :
4553 replacing
? "replace" : "attach", newvdpath
,
4554 replacing
? "for" : "to", oldvdpath
);
4556 spa_strfree(oldvdpath
);
4557 spa_strfree(newvdpath
);
4559 if (spa
->spa_bootfs
)
4560 spa_event_notify(spa
, newvd
, FM_EREPORT_ZFS_BOOTFS_VDEV_ATTACH
);
4566 * Detach a device from a mirror or replacing vdev.
4567 * If 'replace_done' is specified, only detach if the parent
4568 * is a replacing vdev.
4571 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
4575 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
;)
4576 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
4577 boolean_t unspare
= B_FALSE
;
4578 uint64_t unspare_guid
= 0;
4582 ASSERT(spa_writeable(spa
));
4584 txg
= spa_vdev_enter(spa
);
4586 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4589 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4591 if (!vd
->vdev_ops
->vdev_op_leaf
)
4592 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4594 pvd
= vd
->vdev_parent
;
4597 * If the parent/child relationship is not as expected, don't do it.
4598 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4599 * vdev that's replacing B with C. The user's intent in replacing
4600 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4601 * the replace by detaching C, the expected behavior is to end up
4602 * M(A,B). But suppose that right after deciding to detach C,
4603 * the replacement of B completes. We would have M(A,C), and then
4604 * ask to detach C, which would leave us with just A -- not what
4605 * the user wanted. To prevent this, we make sure that the
4606 * parent/child relationship hasn't changed -- in this example,
4607 * that C's parent is still the replacing vdev R.
4609 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
4610 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4613 * Only 'replacing' or 'spare' vdevs can be replaced.
4615 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4616 pvd
->vdev_ops
!= &vdev_spare_ops
)
4617 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4619 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
4620 spa_version(spa
) >= SPA_VERSION_SPARES
);
4623 * Only mirror, replacing, and spare vdevs support detach.
4625 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4626 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4627 pvd
->vdev_ops
!= &vdev_spare_ops
)
4628 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4631 * If this device has the only valid copy of some data,
4632 * we cannot safely detach it.
4634 if (vdev_dtl_required(vd
))
4635 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4637 ASSERT(pvd
->vdev_children
>= 2);
4640 * If we are detaching the second disk from a replacing vdev, then
4641 * check to see if we changed the original vdev's path to have "/old"
4642 * at the end in spa_vdev_attach(). If so, undo that change now.
4644 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
4645 vd
->vdev_path
!= NULL
) {
4646 size_t len
= strlen(vd
->vdev_path
);
4648 for (c
= 0; c
< pvd
->vdev_children
; c
++) {
4649 cvd
= pvd
->vdev_child
[c
];
4651 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
4654 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
4655 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
4656 spa_strfree(cvd
->vdev_path
);
4657 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
4664 * If we are detaching the original disk from a spare, then it implies
4665 * that the spare should become a real disk, and be removed from the
4666 * active spare list for the pool.
4668 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4670 pvd
->vdev_child
[pvd
->vdev_children
- 1]->vdev_isspare
)
4674 * Erase the disk labels so the disk can be used for other things.
4675 * This must be done after all other error cases are handled,
4676 * but before we disembowel vd (so we can still do I/O to it).
4677 * But if we can't do it, don't treat the error as fatal --
4678 * it may be that the unwritability of the disk is the reason
4679 * it's being detached!
4681 error
= vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
4684 * Remove vd from its parent and compact the parent's children.
4686 vdev_remove_child(pvd
, vd
);
4687 vdev_compact_children(pvd
);
4690 * Remember one of the remaining children so we can get tvd below.
4692 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
4695 * If we need to remove the remaining child from the list of hot spares,
4696 * do it now, marking the vdev as no longer a spare in the process.
4697 * We must do this before vdev_remove_parent(), because that can
4698 * change the GUID if it creates a new toplevel GUID. For a similar
4699 * reason, we must remove the spare now, in the same txg as the detach;
4700 * otherwise someone could attach a new sibling, change the GUID, and
4701 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4704 ASSERT(cvd
->vdev_isspare
);
4705 spa_spare_remove(cvd
);
4706 unspare_guid
= cvd
->vdev_guid
;
4707 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
4708 cvd
->vdev_unspare
= B_TRUE
;
4712 * If the parent mirror/replacing vdev only has one child,
4713 * the parent is no longer needed. Remove it from the tree.
4715 if (pvd
->vdev_children
== 1) {
4716 if (pvd
->vdev_ops
== &vdev_spare_ops
)
4717 cvd
->vdev_unspare
= B_FALSE
;
4718 vdev_remove_parent(cvd
);
4719 cvd
->vdev_resilvering
= B_FALSE
;
4724 * We don't set tvd until now because the parent we just removed
4725 * may have been the previous top-level vdev.
4727 tvd
= cvd
->vdev_top
;
4728 ASSERT(tvd
->vdev_parent
== rvd
);
4731 * Reevaluate the parent vdev state.
4733 vdev_propagate_state(cvd
);
4736 * If the 'autoexpand' property is set on the pool then automatically
4737 * try to expand the size of the pool. For example if the device we
4738 * just detached was smaller than the others, it may be possible to
4739 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4740 * first so that we can obtain the updated sizes of the leaf vdevs.
4742 if (spa
->spa_autoexpand
) {
4744 vdev_expand(tvd
, txg
);
4747 vdev_config_dirty(tvd
);
4750 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4751 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4752 * But first make sure we're not on any *other* txg's DTL list, to
4753 * prevent vd from being accessed after it's freed.
4755 vdpath
= spa_strdup(vd
->vdev_path
);
4756 for (t
= 0; t
< TXG_SIZE
; t
++)
4757 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
4758 vd
->vdev_detached
= B_TRUE
;
4759 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
4761 spa_event_notify(spa
, vd
, FM_EREPORT_ZFS_DEVICE_REMOVE
);
4763 /* hang on to the spa before we release the lock */
4764 spa_open_ref(spa
, FTAG
);
4766 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
4768 spa_history_log_internal(LOG_POOL_VDEV_DETACH
, spa
, NULL
,
4770 spa_strfree(vdpath
);
4773 * If this was the removal of the original device in a hot spare vdev,
4774 * then we want to go through and remove the device from the hot spare
4775 * list of every other pool.
4778 spa_t
*altspa
= NULL
;
4780 mutex_enter(&spa_namespace_lock
);
4781 while ((altspa
= spa_next(altspa
)) != NULL
) {
4782 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
4786 spa_open_ref(altspa
, FTAG
);
4787 mutex_exit(&spa_namespace_lock
);
4788 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
4789 mutex_enter(&spa_namespace_lock
);
4790 spa_close(altspa
, FTAG
);
4792 mutex_exit(&spa_namespace_lock
);
4794 /* search the rest of the vdevs for spares to remove */
4795 spa_vdev_resilver_done(spa
);
4798 /* all done with the spa; OK to release */
4799 mutex_enter(&spa_namespace_lock
);
4800 spa_close(spa
, FTAG
);
4801 mutex_exit(&spa_namespace_lock
);
4807 * Split a set of devices from their mirrors, and create a new pool from them.
4810 spa_vdev_split_mirror(spa_t
*spa
, char *newname
, nvlist_t
*config
,
4811 nvlist_t
*props
, boolean_t exp
)
4814 uint64_t txg
, *glist
;
4816 uint_t c
, children
, lastlog
;
4817 nvlist_t
**child
, *nvl
, *tmp
;
4819 char *altroot
= NULL
;
4820 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
4821 boolean_t activate_slog
;
4823 ASSERT(spa_writeable(spa
));
4825 txg
= spa_vdev_enter(spa
);
4827 /* clear the log and flush everything up to now */
4828 activate_slog
= spa_passivate_log(spa
);
4829 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
4830 error
= spa_offline_log(spa
);
4831 txg
= spa_vdev_config_enter(spa
);
4834 spa_activate_log(spa
);
4837 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4839 /* check new spa name before going any further */
4840 if (spa_lookup(newname
) != NULL
)
4841 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
4844 * scan through all the children to ensure they're all mirrors
4846 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
4847 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
4849 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4851 /* first, check to ensure we've got the right child count */
4852 rvd
= spa
->spa_root_vdev
;
4854 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
4855 vdev_t
*vd
= rvd
->vdev_child
[c
];
4857 /* don't count the holes & logs as children */
4858 if (vd
->vdev_islog
|| vd
->vdev_ishole
) {
4866 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
4867 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4869 /* next, ensure no spare or cache devices are part of the split */
4870 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
4871 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
4872 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4874 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_PUSHPAGE
);
4875 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_PUSHPAGE
);
4877 /* then, loop over each vdev and validate it */
4878 for (c
= 0; c
< children
; c
++) {
4879 uint64_t is_hole
= 0;
4881 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
4885 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
4886 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
4894 /* which disk is going to be split? */
4895 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
4901 /* look it up in the spa */
4902 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
4903 if (vml
[c
] == NULL
) {
4908 /* make sure there's nothing stopping the split */
4909 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
4910 vml
[c
]->vdev_islog
||
4911 vml
[c
]->vdev_ishole
||
4912 vml
[c
]->vdev_isspare
||
4913 vml
[c
]->vdev_isl2cache
||
4914 !vdev_writeable(vml
[c
]) ||
4915 vml
[c
]->vdev_children
!= 0 ||
4916 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
4917 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
4922 if (vdev_dtl_required(vml
[c
])) {
4927 /* we need certain info from the top level */
4928 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
4929 vml
[c
]->vdev_top
->vdev_ms_array
) == 0);
4930 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
4931 vml
[c
]->vdev_top
->vdev_ms_shift
) == 0);
4932 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
4933 vml
[c
]->vdev_top
->vdev_asize
) == 0);
4934 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
4935 vml
[c
]->vdev_top
->vdev_ashift
) == 0);
4939 kmem_free(vml
, children
* sizeof (vdev_t
*));
4940 kmem_free(glist
, children
* sizeof (uint64_t));
4941 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4944 /* stop writers from using the disks */
4945 for (c
= 0; c
< children
; c
++) {
4947 vml
[c
]->vdev_offline
= B_TRUE
;
4949 vdev_reopen(spa
->spa_root_vdev
);
4952 * Temporarily record the splitting vdevs in the spa config. This
4953 * will disappear once the config is regenerated.
4955 VERIFY(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
4956 VERIFY(nvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
4957 glist
, children
) == 0);
4958 kmem_free(glist
, children
* sizeof (uint64_t));
4960 mutex_enter(&spa
->spa_props_lock
);
4961 VERIFY(nvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
,
4963 mutex_exit(&spa
->spa_props_lock
);
4964 spa
->spa_config_splitting
= nvl
;
4965 vdev_config_dirty(spa
->spa_root_vdev
);
4967 /* configure and create the new pool */
4968 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
) == 0);
4969 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
4970 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
) == 0);
4971 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
4972 spa_version(spa
)) == 0);
4973 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
4974 spa
->spa_config_txg
) == 0);
4975 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
4976 spa_generate_guid(NULL
)) == 0);
4977 (void) nvlist_lookup_string(props
,
4978 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
4980 /* add the new pool to the namespace */
4981 newspa
= spa_add(newname
, config
, altroot
);
4982 newspa
->spa_config_txg
= spa
->spa_config_txg
;
4983 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
4985 /* release the spa config lock, retaining the namespace lock */
4986 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
4988 if (zio_injection_enabled
)
4989 zio_handle_panic_injection(spa
, FTAG
, 1);
4991 spa_activate(newspa
, spa_mode_global
);
4992 spa_async_suspend(newspa
);
4994 /* create the new pool from the disks of the original pool */
4995 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
, B_TRUE
);
4999 /* if that worked, generate a real config for the new pool */
5000 if (newspa
->spa_root_vdev
!= NULL
) {
5001 VERIFY(nvlist_alloc(&newspa
->spa_config_splitting
,
5002 NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
5003 VERIFY(nvlist_add_uint64(newspa
->spa_config_splitting
,
5004 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
)) == 0);
5005 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
5010 if (props
!= NULL
) {
5011 spa_configfile_set(newspa
, props
, B_FALSE
);
5012 error
= spa_prop_set(newspa
, props
);
5017 /* flush everything */
5018 txg
= spa_vdev_config_enter(newspa
);
5019 vdev_config_dirty(newspa
->spa_root_vdev
);
5020 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
5022 if (zio_injection_enabled
)
5023 zio_handle_panic_injection(spa
, FTAG
, 2);
5025 spa_async_resume(newspa
);
5027 /* finally, update the original pool's config */
5028 txg
= spa_vdev_config_enter(spa
);
5029 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
5030 error
= dmu_tx_assign(tx
, TXG_WAIT
);
5033 for (c
= 0; c
< children
; c
++) {
5034 if (vml
[c
] != NULL
) {
5037 spa_history_log_internal(LOG_POOL_VDEV_DETACH
,
5043 vdev_config_dirty(spa
->spa_root_vdev
);
5044 spa
->spa_config_splitting
= NULL
;
5048 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
5050 if (zio_injection_enabled
)
5051 zio_handle_panic_injection(spa
, FTAG
, 3);
5053 /* split is complete; log a history record */
5054 spa_history_log_internal(LOG_POOL_SPLIT
, newspa
, NULL
,
5055 "split new pool %s from pool %s", newname
, spa_name(spa
));
5057 kmem_free(vml
, children
* sizeof (vdev_t
*));
5059 /* if we're not going to mount the filesystems in userland, export */
5061 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
5068 spa_deactivate(newspa
);
5071 txg
= spa_vdev_config_enter(spa
);
5073 /* re-online all offlined disks */
5074 for (c
= 0; c
< children
; c
++) {
5076 vml
[c
]->vdev_offline
= B_FALSE
;
5078 vdev_reopen(spa
->spa_root_vdev
);
5080 nvlist_free(spa
->spa_config_splitting
);
5081 spa
->spa_config_splitting
= NULL
;
5082 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
5084 kmem_free(vml
, children
* sizeof (vdev_t
*));
5089 spa_nvlist_lookup_by_guid(nvlist_t
**nvpp
, int count
, uint64_t target_guid
)
5093 for (i
= 0; i
< count
; i
++) {
5096 VERIFY(nvlist_lookup_uint64(nvpp
[i
], ZPOOL_CONFIG_GUID
,
5099 if (guid
== target_guid
)
5107 spa_vdev_remove_aux(nvlist_t
*config
, char *name
, nvlist_t
**dev
, int count
,
5108 nvlist_t
*dev_to_remove
)
5110 nvlist_t
**newdev
= NULL
;
5114 newdev
= kmem_alloc((count
- 1) * sizeof (void *), KM_PUSHPAGE
);
5116 for (i
= 0, j
= 0; i
< count
; i
++) {
5117 if (dev
[i
] == dev_to_remove
)
5119 VERIFY(nvlist_dup(dev
[i
], &newdev
[j
++], KM_PUSHPAGE
) == 0);
5122 VERIFY(nvlist_remove(config
, name
, DATA_TYPE_NVLIST_ARRAY
) == 0);
5123 VERIFY(nvlist_add_nvlist_array(config
, name
, newdev
, count
- 1) == 0);
5125 for (i
= 0; i
< count
- 1; i
++)
5126 nvlist_free(newdev
[i
]);
5129 kmem_free(newdev
, (count
- 1) * sizeof (void *));
5133 * Evacuate the device.
5136 spa_vdev_remove_evacuate(spa_t
*spa
, vdev_t
*vd
)
5141 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5142 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5143 ASSERT(vd
== vd
->vdev_top
);
5146 * Evacuate the device. We don't hold the config lock as writer
5147 * since we need to do I/O but we do keep the
5148 * spa_namespace_lock held. Once this completes the device
5149 * should no longer have any blocks allocated on it.
5151 if (vd
->vdev_islog
) {
5152 if (vd
->vdev_stat
.vs_alloc
!= 0)
5153 error
= spa_offline_log(spa
);
5162 * The evacuation succeeded. Remove any remaining MOS metadata
5163 * associated with this vdev, and wait for these changes to sync.
5165 ASSERT0(vd
->vdev_stat
.vs_alloc
);
5166 txg
= spa_vdev_config_enter(spa
);
5167 vd
->vdev_removing
= B_TRUE
;
5168 vdev_dirty(vd
, 0, NULL
, txg
);
5169 vdev_config_dirty(vd
);
5170 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5176 * Complete the removal by cleaning up the namespace.
5179 spa_vdev_remove_from_namespace(spa_t
*spa
, vdev_t
*vd
)
5181 vdev_t
*rvd
= spa
->spa_root_vdev
;
5182 uint64_t id
= vd
->vdev_id
;
5183 boolean_t last_vdev
= (id
== (rvd
->vdev_children
- 1));
5185 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5186 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5187 ASSERT(vd
== vd
->vdev_top
);
5190 * Only remove any devices which are empty.
5192 if (vd
->vdev_stat
.vs_alloc
!= 0)
5195 (void) vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
5197 if (list_link_active(&vd
->vdev_state_dirty_node
))
5198 vdev_state_clean(vd
);
5199 if (list_link_active(&vd
->vdev_config_dirty_node
))
5200 vdev_config_clean(vd
);
5205 vdev_compact_children(rvd
);
5207 vd
= vdev_alloc_common(spa
, id
, 0, &vdev_hole_ops
);
5208 vdev_add_child(rvd
, vd
);
5210 vdev_config_dirty(rvd
);
5213 * Reassess the health of our root vdev.
5219 * Remove a device from the pool -
5221 * Removing a device from the vdev namespace requires several steps
5222 * and can take a significant amount of time. As a result we use
5223 * the spa_vdev_config_[enter/exit] functions which allow us to
5224 * grab and release the spa_config_lock while still holding the namespace
5225 * lock. During each step the configuration is synced out.
5229 * Remove a device from the pool. Currently, this supports removing only hot
5230 * spares, slogs, and level 2 ARC devices.
5233 spa_vdev_remove(spa_t
*spa
, uint64_t guid
, boolean_t unspare
)
5236 metaslab_group_t
*mg
;
5237 nvlist_t
**spares
, **l2cache
, *nv
;
5239 uint_t nspares
, nl2cache
;
5241 boolean_t locked
= MUTEX_HELD(&spa_namespace_lock
);
5243 ASSERT(spa_writeable(spa
));
5246 txg
= spa_vdev_enter(spa
);
5248 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
5250 if (spa
->spa_spares
.sav_vdevs
!= NULL
&&
5251 nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
5252 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0 &&
5253 (nv
= spa_nvlist_lookup_by_guid(spares
, nspares
, guid
)) != NULL
) {
5255 * Only remove the hot spare if it's not currently in use
5258 if (vd
== NULL
|| unspare
) {
5259 spa_vdev_remove_aux(spa
->spa_spares
.sav_config
,
5260 ZPOOL_CONFIG_SPARES
, spares
, nspares
, nv
);
5261 spa_load_spares(spa
);
5262 spa
->spa_spares
.sav_sync
= B_TRUE
;
5266 } else if (spa
->spa_l2cache
.sav_vdevs
!= NULL
&&
5267 nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
5268 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0 &&
5269 (nv
= spa_nvlist_lookup_by_guid(l2cache
, nl2cache
, guid
)) != NULL
) {
5271 * Cache devices can always be removed.
5273 spa_vdev_remove_aux(spa
->spa_l2cache
.sav_config
,
5274 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
, nv
);
5275 spa_load_l2cache(spa
);
5276 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5277 } else if (vd
!= NULL
&& vd
->vdev_islog
) {
5279 ASSERT(vd
== vd
->vdev_top
);
5282 * XXX - Once we have bp-rewrite this should
5283 * become the common case.
5289 * Stop allocating from this vdev.
5291 metaslab_group_passivate(mg
);
5294 * Wait for the youngest allocations and frees to sync,
5295 * and then wait for the deferral of those frees to finish.
5297 spa_vdev_config_exit(spa
, NULL
,
5298 txg
+ TXG_CONCURRENT_STATES
+ TXG_DEFER_SIZE
, 0, FTAG
);
5301 * Attempt to evacuate the vdev.
5303 error
= spa_vdev_remove_evacuate(spa
, vd
);
5305 txg
= spa_vdev_config_enter(spa
);
5308 * If we couldn't evacuate the vdev, unwind.
5311 metaslab_group_activate(mg
);
5312 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5316 * Clean up the vdev namespace.
5318 spa_vdev_remove_from_namespace(spa
, vd
);
5320 } else if (vd
!= NULL
) {
5322 * Normal vdevs cannot be removed (yet).
5327 * There is no vdev of any kind with the specified guid.
5333 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5339 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5340 * current spared, so we can detach it.
5343 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
5345 vdev_t
*newvd
, *oldvd
;
5348 for (c
= 0; c
< vd
->vdev_children
; c
++) {
5349 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
5355 * Check for a completed replacement. We always consider the first
5356 * vdev in the list to be the oldest vdev, and the last one to be
5357 * the newest (see spa_vdev_attach() for how that works). In
5358 * the case where the newest vdev is faulted, we will not automatically
5359 * remove it after a resilver completes. This is OK as it will require
5360 * user intervention to determine which disk the admin wishes to keep.
5362 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
5363 ASSERT(vd
->vdev_children
> 1);
5365 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
5366 oldvd
= vd
->vdev_child
[0];
5368 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5369 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5370 !vdev_dtl_required(oldvd
))
5375 * Check for a completed resilver with the 'unspare' flag set.
5377 if (vd
->vdev_ops
== &vdev_spare_ops
) {
5378 vdev_t
*first
= vd
->vdev_child
[0];
5379 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
5381 if (last
->vdev_unspare
) {
5384 } else if (first
->vdev_unspare
) {
5391 if (oldvd
!= NULL
&&
5392 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5393 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5394 !vdev_dtl_required(oldvd
))
5398 * If there are more than two spares attached to a disk,
5399 * and those spares are not required, then we want to
5400 * attempt to free them up now so that they can be used
5401 * by other pools. Once we're back down to a single
5402 * disk+spare, we stop removing them.
5404 if (vd
->vdev_children
> 2) {
5405 newvd
= vd
->vdev_child
[1];
5407 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
5408 vdev_dtl_empty(last
, DTL_MISSING
) &&
5409 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
5410 !vdev_dtl_required(newvd
))
5419 spa_vdev_resilver_done(spa_t
*spa
)
5421 vdev_t
*vd
, *pvd
, *ppvd
;
5422 uint64_t guid
, sguid
, pguid
, ppguid
;
5424 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5426 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
5427 pvd
= vd
->vdev_parent
;
5428 ppvd
= pvd
->vdev_parent
;
5429 guid
= vd
->vdev_guid
;
5430 pguid
= pvd
->vdev_guid
;
5431 ppguid
= ppvd
->vdev_guid
;
5434 * If we have just finished replacing a hot spared device, then
5435 * we need to detach the parent's first child (the original hot
5438 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
5439 ppvd
->vdev_children
== 2) {
5440 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
5441 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
5443 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5444 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
5446 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
5448 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5451 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5455 * Update the stored path or FRU for this vdev.
5458 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
5462 boolean_t sync
= B_FALSE
;
5464 ASSERT(spa_writeable(spa
));
5466 spa_vdev_state_enter(spa
, SCL_ALL
);
5468 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
5469 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
5471 if (!vd
->vdev_ops
->vdev_op_leaf
)
5472 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
5475 if (strcmp(value
, vd
->vdev_path
) != 0) {
5476 spa_strfree(vd
->vdev_path
);
5477 vd
->vdev_path
= spa_strdup(value
);
5481 if (vd
->vdev_fru
== NULL
) {
5482 vd
->vdev_fru
= spa_strdup(value
);
5484 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
5485 spa_strfree(vd
->vdev_fru
);
5486 vd
->vdev_fru
= spa_strdup(value
);
5491 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
5495 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
5497 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
5501 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
5503 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
5507 * ==========================================================================
5509 * ==========================================================================
5513 spa_scan_stop(spa_t
*spa
)
5515 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5516 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
5518 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
5522 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
5524 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5526 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
5530 * If a resilver was requested, but there is no DTL on a
5531 * writeable leaf device, we have nothing to do.
5533 if (func
== POOL_SCAN_RESILVER
&&
5534 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
5535 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
5539 return (dsl_scan(spa
->spa_dsl_pool
, func
));
5543 * ==========================================================================
5544 * SPA async task processing
5545 * ==========================================================================
5549 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
5553 if (vd
->vdev_remove_wanted
) {
5554 vd
->vdev_remove_wanted
= B_FALSE
;
5555 vd
->vdev_delayed_close
= B_FALSE
;
5556 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
5559 * We want to clear the stats, but we don't want to do a full
5560 * vdev_clear() as that will cause us to throw away
5561 * degraded/faulted state as well as attempt to reopen the
5562 * device, all of which is a waste.
5564 vd
->vdev_stat
.vs_read_errors
= 0;
5565 vd
->vdev_stat
.vs_write_errors
= 0;
5566 vd
->vdev_stat
.vs_checksum_errors
= 0;
5568 vdev_state_dirty(vd
->vdev_top
);
5571 for (c
= 0; c
< vd
->vdev_children
; c
++)
5572 spa_async_remove(spa
, vd
->vdev_child
[c
]);
5576 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
5580 if (vd
->vdev_probe_wanted
) {
5581 vd
->vdev_probe_wanted
= B_FALSE
;
5582 vdev_reopen(vd
); /* vdev_open() does the actual probe */
5585 for (c
= 0; c
< vd
->vdev_children
; c
++)
5586 spa_async_probe(spa
, vd
->vdev_child
[c
]);
5590 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
5594 if (!spa
->spa_autoexpand
)
5597 for (c
= 0; c
< vd
->vdev_children
; c
++) {
5598 vdev_t
*cvd
= vd
->vdev_child
[c
];
5599 spa_async_autoexpand(spa
, cvd
);
5602 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
5605 spa_event_notify(vd
->vdev_spa
, vd
, FM_EREPORT_ZFS_DEVICE_AUTOEXPAND
);
5609 spa_async_thread(spa_t
*spa
)
5613 ASSERT(spa
->spa_sync_on
);
5615 mutex_enter(&spa
->spa_async_lock
);
5616 tasks
= spa
->spa_async_tasks
;
5617 spa
->spa_async_tasks
= 0;
5618 mutex_exit(&spa
->spa_async_lock
);
5621 * See if the config needs to be updated.
5623 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
5624 uint64_t old_space
, new_space
;
5626 mutex_enter(&spa_namespace_lock
);
5627 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
5628 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5629 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
5630 mutex_exit(&spa_namespace_lock
);
5633 * If the pool grew as a result of the config update,
5634 * then log an internal history event.
5636 if (new_space
!= old_space
) {
5637 spa_history_log_internal(LOG_POOL_VDEV_ONLINE
,
5639 "pool '%s' size: %llu(+%llu)",
5640 spa_name(spa
), new_space
, new_space
- old_space
);
5645 * See if any devices need to be marked REMOVED.
5647 if (tasks
& SPA_ASYNC_REMOVE
) {
5648 spa_vdev_state_enter(spa
, SCL_NONE
);
5649 spa_async_remove(spa
, spa
->spa_root_vdev
);
5650 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
5651 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
5652 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
5653 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
5654 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5657 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
5658 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5659 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
5660 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5664 * See if any devices need to be probed.
5666 if (tasks
& SPA_ASYNC_PROBE
) {
5667 spa_vdev_state_enter(spa
, SCL_NONE
);
5668 spa_async_probe(spa
, spa
->spa_root_vdev
);
5669 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5673 * If any devices are done replacing, detach them.
5675 if (tasks
& SPA_ASYNC_RESILVER_DONE
)
5676 spa_vdev_resilver_done(spa
);
5679 * Kick off a resilver.
5681 if (tasks
& SPA_ASYNC_RESILVER
)
5682 dsl_resilver_restart(spa
->spa_dsl_pool
, 0);
5685 * Let the world know that we're done.
5687 mutex_enter(&spa
->spa_async_lock
);
5688 spa
->spa_async_thread
= NULL
;
5689 cv_broadcast(&spa
->spa_async_cv
);
5690 mutex_exit(&spa
->spa_async_lock
);
5695 spa_async_suspend(spa_t
*spa
)
5697 mutex_enter(&spa
->spa_async_lock
);
5698 spa
->spa_async_suspended
++;
5699 while (spa
->spa_async_thread
!= NULL
)
5700 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
5701 mutex_exit(&spa
->spa_async_lock
);
5705 spa_async_resume(spa_t
*spa
)
5707 mutex_enter(&spa
->spa_async_lock
);
5708 ASSERT(spa
->spa_async_suspended
!= 0);
5709 spa
->spa_async_suspended
--;
5710 mutex_exit(&spa
->spa_async_lock
);
5714 spa_async_dispatch(spa_t
*spa
)
5716 mutex_enter(&spa
->spa_async_lock
);
5717 if (spa
->spa_async_tasks
&& !spa
->spa_async_suspended
&&
5718 spa
->spa_async_thread
== NULL
&&
5719 rootdir
!= NULL
&& !vn_is_readonly(rootdir
))
5720 spa
->spa_async_thread
= thread_create(NULL
, 0,
5721 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
5722 mutex_exit(&spa
->spa_async_lock
);
5726 spa_async_request(spa_t
*spa
, int task
)
5728 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
5729 mutex_enter(&spa
->spa_async_lock
);
5730 spa
->spa_async_tasks
|= task
;
5731 mutex_exit(&spa
->spa_async_lock
);
5735 * ==========================================================================
5736 * SPA syncing routines
5737 * ==========================================================================
5741 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
5744 bpobj_enqueue(bpo
, bp
, tx
);
5749 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
5753 zio_nowait(zio_free_sync(zio
, zio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
5759 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
5761 char *packed
= NULL
;
5766 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
5769 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
5770 * information. This avoids the dbuf_will_dirty() path and
5771 * saves us a pre-read to get data we don't actually care about.
5773 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
5774 packed
= vmem_alloc(bufsize
, KM_PUSHPAGE
);
5776 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
5778 bzero(packed
+ nvsize
, bufsize
- nvsize
);
5780 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
5782 vmem_free(packed
, bufsize
);
5784 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
5785 dmu_buf_will_dirty(db
, tx
);
5786 *(uint64_t *)db
->db_data
= nvsize
;
5787 dmu_buf_rele(db
, FTAG
);
5791 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
5792 const char *config
, const char *entry
)
5802 * Update the MOS nvlist describing the list of available devices.
5803 * spa_validate_aux() will have already made sure this nvlist is
5804 * valid and the vdevs are labeled appropriately.
5806 if (sav
->sav_object
== 0) {
5807 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
5808 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
5809 sizeof (uint64_t), tx
);
5810 VERIFY(zap_update(spa
->spa_meta_objset
,
5811 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
5812 &sav
->sav_object
, tx
) == 0);
5815 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
5816 if (sav
->sav_count
== 0) {
5817 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, NULL
, 0) == 0);
5819 list
= kmem_alloc(sav
->sav_count
* sizeof (void *), KM_PUSHPAGE
);
5820 for (i
= 0; i
< sav
->sav_count
; i
++)
5821 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
5822 B_FALSE
, VDEV_CONFIG_L2CACHE
);
5823 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, list
,
5824 sav
->sav_count
) == 0);
5825 for (i
= 0; i
< sav
->sav_count
; i
++)
5826 nvlist_free(list
[i
]);
5827 kmem_free(list
, sav
->sav_count
* sizeof (void *));
5830 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
5831 nvlist_free(nvroot
);
5833 sav
->sav_sync
= B_FALSE
;
5837 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
5841 if (list_is_empty(&spa
->spa_config_dirty_list
))
5844 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
5846 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
5847 dmu_tx_get_txg(tx
), B_FALSE
);
5850 * If we're upgrading the spa version then make sure that
5851 * the config object gets updated with the correct version.
5853 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
5854 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
5855 spa
->spa_uberblock
.ub_version
);
5857 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5859 if (spa
->spa_config_syncing
)
5860 nvlist_free(spa
->spa_config_syncing
);
5861 spa
->spa_config_syncing
= config
;
5863 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
5867 spa_sync_version(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
5870 uint64_t version
= *(uint64_t *)arg2
;
5873 * Setting the version is special cased when first creating the pool.
5875 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
5877 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
5878 ASSERT(version
>= spa_version(spa
));
5880 spa
->spa_uberblock
.ub_version
= version
;
5881 vdev_config_dirty(spa
->spa_root_vdev
);
5885 * Set zpool properties.
5888 spa_sync_props(void *arg1
, void *arg2
, dmu_tx_t
*tx
)
5891 objset_t
*mos
= spa
->spa_meta_objset
;
5892 nvlist_t
*nvp
= arg2
;
5893 nvpair_t
*elem
= NULL
;
5895 mutex_enter(&spa
->spa_props_lock
);
5897 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
5899 char *strval
, *fname
;
5901 const char *propname
;
5902 zprop_type_t proptype
;
5903 zfeature_info_t
*feature
;
5905 prop
= zpool_name_to_prop(nvpair_name(elem
));
5906 switch ((int)prop
) {
5909 * We checked this earlier in spa_prop_validate().
5911 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
5913 fname
= strchr(nvpair_name(elem
), '@') + 1;
5914 VERIFY3U(0, ==, zfeature_lookup_name(fname
, &feature
));
5916 spa_feature_enable(spa
, feature
, tx
);
5919 case ZPOOL_PROP_VERSION
:
5920 VERIFY(nvpair_value_uint64(elem
, &intval
) == 0);
5922 * The version is synced seperatly before other
5923 * properties and should be correct by now.
5925 ASSERT3U(spa_version(spa
), >=, intval
);
5928 case ZPOOL_PROP_ALTROOT
:
5930 * 'altroot' is a non-persistent property. It should
5931 * have been set temporarily at creation or import time.
5933 ASSERT(spa
->spa_root
!= NULL
);
5936 case ZPOOL_PROP_READONLY
:
5937 case ZPOOL_PROP_CACHEFILE
:
5939 * 'readonly' and 'cachefile' are also non-persisitent
5943 case ZPOOL_PROP_COMMENT
:
5944 VERIFY(nvpair_value_string(elem
, &strval
) == 0);
5945 if (spa
->spa_comment
!= NULL
)
5946 spa_strfree(spa
->spa_comment
);
5947 spa
->spa_comment
= spa_strdup(strval
);
5949 * We need to dirty the configuration on all the vdevs
5950 * so that their labels get updated. It's unnecessary
5951 * to do this for pool creation since the vdev's
5952 * configuratoin has already been dirtied.
5954 if (tx
->tx_txg
!= TXG_INITIAL
)
5955 vdev_config_dirty(spa
->spa_root_vdev
);
5959 * Set pool property values in the poolprops mos object.
5961 if (spa
->spa_pool_props_object
== 0) {
5962 spa
->spa_pool_props_object
=
5963 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
5964 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
5968 /* normalize the property name */
5969 propname
= zpool_prop_to_name(prop
);
5970 proptype
= zpool_prop_get_type(prop
);
5972 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
5973 ASSERT(proptype
== PROP_TYPE_STRING
);
5974 VERIFY(nvpair_value_string(elem
, &strval
) == 0);
5975 VERIFY(zap_update(mos
,
5976 spa
->spa_pool_props_object
, propname
,
5977 1, strlen(strval
) + 1, strval
, tx
) == 0);
5979 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
5980 VERIFY(nvpair_value_uint64(elem
, &intval
) == 0);
5982 if (proptype
== PROP_TYPE_INDEX
) {
5984 VERIFY(zpool_prop_index_to_string(
5985 prop
, intval
, &unused
) == 0);
5987 VERIFY(zap_update(mos
,
5988 spa
->spa_pool_props_object
, propname
,
5989 8, 1, &intval
, tx
) == 0);
5991 ASSERT(0); /* not allowed */
5995 case ZPOOL_PROP_DELEGATION
:
5996 spa
->spa_delegation
= intval
;
5998 case ZPOOL_PROP_BOOTFS
:
5999 spa
->spa_bootfs
= intval
;
6001 case ZPOOL_PROP_FAILUREMODE
:
6002 spa
->spa_failmode
= intval
;
6004 case ZPOOL_PROP_AUTOEXPAND
:
6005 spa
->spa_autoexpand
= intval
;
6006 if (tx
->tx_txg
!= TXG_INITIAL
)
6007 spa_async_request(spa
,
6008 SPA_ASYNC_AUTOEXPAND
);
6010 case ZPOOL_PROP_DEDUPDITTO
:
6011 spa
->spa_dedup_ditto
= intval
;
6018 /* log internal history if this is not a zpool create */
6019 if (spa_version(spa
) >= SPA_VERSION_ZPOOL_HISTORY
&&
6020 tx
->tx_txg
!= TXG_INITIAL
) {
6021 spa_history_log_internal(LOG_POOL_PROPSET
,
6022 spa
, tx
, "%s %lld %s",
6023 nvpair_name(elem
), intval
, spa_name(spa
));
6027 mutex_exit(&spa
->spa_props_lock
);
6031 * Perform one-time upgrade on-disk changes. spa_version() does not
6032 * reflect the new version this txg, so there must be no changes this
6033 * txg to anything that the upgrade code depends on after it executes.
6034 * Therefore this must be called after dsl_pool_sync() does the sync
6038 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
6040 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6042 ASSERT(spa
->spa_sync_pass
== 1);
6044 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
6045 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
6046 dsl_pool_create_origin(dp
, tx
);
6048 /* Keeping the origin open increases spa_minref */
6049 spa
->spa_minref
+= 3;
6052 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
6053 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
6054 dsl_pool_upgrade_clones(dp
, tx
);
6057 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
6058 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
6059 dsl_pool_upgrade_dir_clones(dp
, tx
);
6061 /* Keeping the freedir open increases spa_minref */
6062 spa
->spa_minref
+= 3;
6065 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
6066 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6067 spa_feature_create_zap_objects(spa
, tx
);
6072 * Sync the specified transaction group. New blocks may be dirtied as
6073 * part of the process, so we iterate until it converges.
6076 spa_sync(spa_t
*spa
, uint64_t txg
)
6078 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6079 objset_t
*mos
= spa
->spa_meta_objset
;
6080 bpobj_t
*defer_bpo
= &spa
->spa_deferred_bpobj
;
6081 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
6082 vdev_t
*rvd
= spa
->spa_root_vdev
;
6088 VERIFY(spa_writeable(spa
));
6091 * Lock out configuration changes.
6093 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6095 spa
->spa_syncing_txg
= txg
;
6096 spa
->spa_sync_pass
= 0;
6099 * If there are any pending vdev state changes, convert them
6100 * into config changes that go out with this transaction group.
6102 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6103 while (list_head(&spa
->spa_state_dirty_list
) != NULL
) {
6105 * We need the write lock here because, for aux vdevs,
6106 * calling vdev_config_dirty() modifies sav_config.
6107 * This is ugly and will become unnecessary when we
6108 * eliminate the aux vdev wart by integrating all vdevs
6109 * into the root vdev tree.
6111 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6112 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
6113 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
6114 vdev_state_clean(vd
);
6115 vdev_config_dirty(vd
);
6117 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6118 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
6120 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6122 tx
= dmu_tx_create_assigned(dp
, txg
);
6124 spa
->spa_sync_starttime
= gethrtime();
6125 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
6126 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_taskq
,
6127 spa_deadman
, spa
, TQ_SLEEP
, ddi_get_lbolt() +
6128 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
6131 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6132 * set spa_deflate if we have no raid-z vdevs.
6134 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
6135 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
6138 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
6139 vd
= rvd
->vdev_child
[i
];
6140 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
6143 if (i
== rvd
->vdev_children
) {
6144 spa
->spa_deflate
= TRUE
;
6145 VERIFY(0 == zap_add(spa
->spa_meta_objset
,
6146 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
6147 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
6152 * If anything has changed in this txg, or if someone is waiting
6153 * for this txg to sync (eg, spa_vdev_remove()), push the
6154 * deferred frees from the previous txg. If not, leave them
6155 * alone so that we don't generate work on an otherwise idle
6158 if (!txg_list_empty(&dp
->dp_dirty_datasets
, txg
) ||
6159 !txg_list_empty(&dp
->dp_dirty_dirs
, txg
) ||
6160 !txg_list_empty(&dp
->dp_sync_tasks
, txg
) ||
6161 ((dsl_scan_active(dp
->dp_scan
) ||
6162 txg_sync_waiting(dp
)) && !spa_shutting_down(spa
))) {
6163 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
6164 VERIFY3U(bpobj_iterate(defer_bpo
,
6165 spa_free_sync_cb
, zio
, tx
), ==, 0);
6166 VERIFY0(zio_wait(zio
));
6170 * Iterate to convergence.
6173 int pass
= ++spa
->spa_sync_pass
;
6175 spa_sync_config_object(spa
, tx
);
6176 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
6177 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
6178 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
6179 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
6180 spa_errlog_sync(spa
, txg
);
6181 dsl_pool_sync(dp
, txg
);
6183 if (pass
< zfs_sync_pass_deferred_free
) {
6184 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
6185 bplist_iterate(free_bpl
, spa_free_sync_cb
,
6187 VERIFY(zio_wait(zio
) == 0);
6189 bplist_iterate(free_bpl
, bpobj_enqueue_cb
,
6194 dsl_scan_sync(dp
, tx
);
6196 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
)))
6200 spa_sync_upgrades(spa
, tx
);
6202 } while (dmu_objset_is_dirty(mos
, txg
));
6205 * Rewrite the vdev configuration (which includes the uberblock)
6206 * to commit the transaction group.
6208 * If there are no dirty vdevs, we sync the uberblock to a few
6209 * random top-level vdevs that are known to be visible in the
6210 * config cache (see spa_vdev_add() for a complete description).
6211 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6215 * We hold SCL_STATE to prevent vdev open/close/etc.
6216 * while we're attempting to write the vdev labels.
6218 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6220 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
6221 vdev_t
*svd
[SPA_DVAS_PER_BP
];
6223 int children
= rvd
->vdev_children
;
6224 int c0
= spa_get_random(children
);
6226 for (c
= 0; c
< children
; c
++) {
6227 vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
6228 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
)
6230 svd
[svdcount
++] = vd
;
6231 if (svdcount
== SPA_DVAS_PER_BP
)
6234 error
= vdev_config_sync(svd
, svdcount
, txg
, B_FALSE
);
6236 error
= vdev_config_sync(svd
, svdcount
, txg
,
6239 error
= vdev_config_sync(rvd
->vdev_child
,
6240 rvd
->vdev_children
, txg
, B_FALSE
);
6242 error
= vdev_config_sync(rvd
->vdev_child
,
6243 rvd
->vdev_children
, txg
, B_TRUE
);
6247 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
6249 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6253 zio_suspend(spa
, NULL
);
6254 zio_resume_wait(spa
);
6258 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
6259 spa
->spa_deadman_tqid
= 0;
6262 * Clear the dirty config list.
6264 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
6265 vdev_config_clean(vd
);
6268 * Now that the new config has synced transactionally,
6269 * let it become visible to the config cache.
6271 if (spa
->spa_config_syncing
!= NULL
) {
6272 spa_config_set(spa
, spa
->spa_config_syncing
);
6273 spa
->spa_config_txg
= txg
;
6274 spa
->spa_config_syncing
= NULL
;
6277 spa
->spa_ubsync
= spa
->spa_uberblock
;
6279 dsl_pool_sync_done(dp
, txg
);
6282 * Update usable space statistics.
6284 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
))))
6285 vdev_sync_done(vd
, txg
);
6287 spa_update_dspace(spa
);
6290 * It had better be the case that we didn't dirty anything
6291 * since vdev_config_sync().
6293 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
6294 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
6295 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
6297 spa
->spa_sync_pass
= 0;
6299 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6301 spa_handle_ignored_writes(spa
);
6304 * If any async tasks have been requested, kick them off.
6306 spa_async_dispatch(spa
);
6310 * Sync all pools. We don't want to hold the namespace lock across these
6311 * operations, so we take a reference on the spa_t and drop the lock during the
6315 spa_sync_allpools(void)
6318 mutex_enter(&spa_namespace_lock
);
6319 while ((spa
= spa_next(spa
)) != NULL
) {
6320 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
6321 !spa_writeable(spa
) || spa_suspended(spa
))
6323 spa_open_ref(spa
, FTAG
);
6324 mutex_exit(&spa_namespace_lock
);
6325 txg_wait_synced(spa_get_dsl(spa
), 0);
6326 mutex_enter(&spa_namespace_lock
);
6327 spa_close(spa
, FTAG
);
6329 mutex_exit(&spa_namespace_lock
);
6333 * ==========================================================================
6334 * Miscellaneous routines
6335 * ==========================================================================
6339 * Remove all pools in the system.
6347 * Remove all cached state. All pools should be closed now,
6348 * so every spa in the AVL tree should be unreferenced.
6350 mutex_enter(&spa_namespace_lock
);
6351 while ((spa
= spa_next(NULL
)) != NULL
) {
6353 * Stop async tasks. The async thread may need to detach
6354 * a device that's been replaced, which requires grabbing
6355 * spa_namespace_lock, so we must drop it here.
6357 spa_open_ref(spa
, FTAG
);
6358 mutex_exit(&spa_namespace_lock
);
6359 spa_async_suspend(spa
);
6360 mutex_enter(&spa_namespace_lock
);
6361 spa_close(spa
, FTAG
);
6363 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
6365 spa_deactivate(spa
);
6369 mutex_exit(&spa_namespace_lock
);
6373 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
6378 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
6382 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
6383 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
6384 if (vd
->vdev_guid
== guid
)
6388 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
6389 vd
= spa
->spa_spares
.sav_vdevs
[i
];
6390 if (vd
->vdev_guid
== guid
)
6399 spa_upgrade(spa_t
*spa
, uint64_t version
)
6401 ASSERT(spa_writeable(spa
));
6403 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6406 * This should only be called for a non-faulted pool, and since a
6407 * future version would result in an unopenable pool, this shouldn't be
6410 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
6411 ASSERT(version
>= spa
->spa_uberblock
.ub_version
);
6413 spa
->spa_uberblock
.ub_version
= version
;
6414 vdev_config_dirty(spa
->spa_root_vdev
);
6416 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6418 txg_wait_synced(spa_get_dsl(spa
), 0);
6422 spa_has_spare(spa_t
*spa
, uint64_t guid
)
6426 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
6428 for (i
= 0; i
< sav
->sav_count
; i
++)
6429 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
6432 for (i
= 0; i
< sav
->sav_npending
; i
++) {
6433 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
6434 &spareguid
) == 0 && spareguid
== guid
)
6442 * Check if a pool has an active shared spare device.
6443 * Note: reference count of an active spare is 2, as a spare and as a replace
6446 spa_has_active_shared_spare(spa_t
*spa
)
6450 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
6452 for (i
= 0; i
< sav
->sav_count
; i
++) {
6453 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
6454 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
6463 * Post a FM_EREPORT_ZFS_* event from sys/fm/fs/zfs.h. The payload will be
6464 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6465 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6466 * or zdb as real changes.
6469 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, const char *name
)
6472 zfs_ereport_post(name
, spa
, vd
, NULL
, 0, 0);
6476 #if defined(_KERNEL) && defined(HAVE_SPL)
6477 /* state manipulation functions */
6478 EXPORT_SYMBOL(spa_open
);
6479 EXPORT_SYMBOL(spa_open_rewind
);
6480 EXPORT_SYMBOL(spa_get_stats
);
6481 EXPORT_SYMBOL(spa_create
);
6482 EXPORT_SYMBOL(spa_import_rootpool
);
6483 EXPORT_SYMBOL(spa_import
);
6484 EXPORT_SYMBOL(spa_tryimport
);
6485 EXPORT_SYMBOL(spa_destroy
);
6486 EXPORT_SYMBOL(spa_export
);
6487 EXPORT_SYMBOL(spa_reset
);
6488 EXPORT_SYMBOL(spa_async_request
);
6489 EXPORT_SYMBOL(spa_async_suspend
);
6490 EXPORT_SYMBOL(spa_async_resume
);
6491 EXPORT_SYMBOL(spa_inject_addref
);
6492 EXPORT_SYMBOL(spa_inject_delref
);
6493 EXPORT_SYMBOL(spa_scan_stat_init
);
6494 EXPORT_SYMBOL(spa_scan_get_stats
);
6496 /* device maniion */
6497 EXPORT_SYMBOL(spa_vdev_add
);
6498 EXPORT_SYMBOL(spa_vdev_attach
);
6499 EXPORT_SYMBOL(spa_vdev_detach
);
6500 EXPORT_SYMBOL(spa_vdev_remove
);
6501 EXPORT_SYMBOL(spa_vdev_setpath
);
6502 EXPORT_SYMBOL(spa_vdev_setfru
);
6503 EXPORT_SYMBOL(spa_vdev_split_mirror
);
6505 /* spare statech is global across all pools) */
6506 EXPORT_SYMBOL(spa_spare_add
);
6507 EXPORT_SYMBOL(spa_spare_remove
);
6508 EXPORT_SYMBOL(spa_spare_exists
);
6509 EXPORT_SYMBOL(spa_spare_activate
);
6511 /* L2ARC statech is global across all pools) */
6512 EXPORT_SYMBOL(spa_l2cache_add
);
6513 EXPORT_SYMBOL(spa_l2cache_remove
);
6514 EXPORT_SYMBOL(spa_l2cache_exists
);
6515 EXPORT_SYMBOL(spa_l2cache_activate
);
6516 EXPORT_SYMBOL(spa_l2cache_drop
);
6519 EXPORT_SYMBOL(spa_scan
);
6520 EXPORT_SYMBOL(spa_scan_stop
);
6523 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
6524 EXPORT_SYMBOL(spa_sync_allpools
);
6527 EXPORT_SYMBOL(spa_prop_set
);
6528 EXPORT_SYMBOL(spa_prop_get
);
6529 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
6531 /* asynchronous event notification */
6532 EXPORT_SYMBOL(spa_event_notify
);