4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2013 by Delphix. All rights reserved.
25 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
29 * SPA: Storage Pool Allocator
31 * This file contains all the routines used when modifying on-disk SPA state.
32 * This includes opening, importing, destroying, exporting a pool, and syncing a
36 #include <sys/zfs_context.h>
37 #include <sys/fm/fs/zfs.h>
38 #include <sys/spa_impl.h>
40 #include <sys/zio_checksum.h>
42 #include <sys/dmu_tx.h>
46 #include <sys/vdev_impl.h>
47 #include <sys/vdev_disk.h>
48 #include <sys/metaslab.h>
49 #include <sys/metaslab_impl.h>
50 #include <sys/uberblock_impl.h>
53 #include <sys/dmu_traverse.h>
54 #include <sys/dmu_objset.h>
55 #include <sys/unique.h>
56 #include <sys/dsl_pool.h>
57 #include <sys/dsl_dataset.h>
58 #include <sys/dsl_dir.h>
59 #include <sys/dsl_prop.h>
60 #include <sys/dsl_synctask.h>
61 #include <sys/fs/zfs.h>
63 #include <sys/callb.h>
64 #include <sys/systeminfo.h>
65 #include <sys/spa_boot.h>
66 #include <sys/zfs_ioctl.h>
67 #include <sys/dsl_scan.h>
68 #include <sys/zfeature.h>
69 #include <sys/dsl_destroy.h>
73 #include <sys/bootprops.h>
74 #include <sys/callb.h>
75 #include <sys/cpupart.h>
77 #include <sys/sysdc.h>
82 #include "zfs_comutil.h"
84 typedef enum zti_modes
{
85 ZTI_MODE_FIXED
, /* value is # of threads (min 1) */
86 ZTI_MODE_BATCH
, /* cpu-intensive; value is ignored */
87 ZTI_MODE_NULL
, /* don't create a taskq */
91 #define ZTI_P(n, q) { ZTI_MODE_FIXED, (n), (q) }
92 #define ZTI_PCT(n) { ZTI_MODE_ONLINE_PERCENT, (n), 1 }
93 #define ZTI_BATCH { ZTI_MODE_BATCH, 0, 1 }
94 #define ZTI_NULL { ZTI_MODE_NULL, 0, 0 }
96 #define ZTI_N(n) ZTI_P(n, 1)
97 #define ZTI_ONE ZTI_N(1)
99 typedef struct zio_taskq_info
{
100 zti_modes_t zti_mode
;
105 static const char *const zio_taskq_types
[ZIO_TASKQ_TYPES
] = {
106 "iss", "iss_h", "int", "int_h"
110 * This table defines the taskq settings for each ZFS I/O type. When
111 * initializing a pool, we use this table to create an appropriately sized
112 * taskq. Some operations are low volume and therefore have a small, static
113 * number of threads assigned to their taskqs using the ZTI_N(#) or ZTI_ONE
114 * macros. Other operations process a large amount of data; the ZTI_BATCH
115 * macro causes us to create a taskq oriented for throughput. Some operations
116 * are so high frequency and short-lived that the taskq itself can become a a
117 * point of lock contention. The ZTI_P(#, #) macro indicates that we need an
118 * additional degree of parallelism specified by the number of threads per-
119 * taskq and the number of taskqs; when dispatching an event in this case, the
120 * particular taskq is chosen at random.
122 * The different taskq priorities are to handle the different contexts (issue
123 * and interrupt) and then to reserve threads for ZIO_PRIORITY_NOW I/Os that
124 * need to be handled with minimum delay.
126 const zio_taskq_info_t zio_taskqs
[ZIO_TYPES
][ZIO_TASKQ_TYPES
] = {
127 /* ISSUE ISSUE_HIGH INTR INTR_HIGH */
128 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* NULL */
129 { ZTI_N(8), ZTI_NULL
, ZTI_BATCH
, ZTI_NULL
}, /* READ */
130 { ZTI_BATCH
, ZTI_N(5), ZTI_N(16), ZTI_N(5) }, /* WRITE */
131 { ZTI_P(4, 8), ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* FREE */
132 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* CLAIM */
133 { ZTI_ONE
, ZTI_NULL
, ZTI_ONE
, ZTI_NULL
}, /* IOCTL */
136 static void spa_sync_version(void *arg
, dmu_tx_t
*tx
);
137 static void spa_sync_props(void *arg
, dmu_tx_t
*tx
);
138 static boolean_t
spa_has_active_shared_spare(spa_t
*spa
);
139 static inline int spa_load_impl(spa_t
*spa
, uint64_t, nvlist_t
*config
,
140 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
142 static void spa_vdev_resilver_done(spa_t
*spa
);
144 uint_t zio_taskq_batch_pct
= 75; /* 1 thread per cpu in pset */
145 id_t zio_taskq_psrset_bind
= PS_NONE
;
146 boolean_t zio_taskq_sysdc
= B_TRUE
; /* use SDC scheduling class */
147 uint_t zio_taskq_basedc
= 80; /* base duty cycle */
149 boolean_t spa_create_process
= B_TRUE
; /* no process ==> no sysdc */
152 * This (illegal) pool name is used when temporarily importing a spa_t in order
153 * to get the vdev stats associated with the imported devices.
155 #define TRYIMPORT_NAME "$import"
158 * ==========================================================================
159 * SPA properties routines
160 * ==========================================================================
164 * Add a (source=src, propname=propval) list to an nvlist.
167 spa_prop_add_list(nvlist_t
*nvl
, zpool_prop_t prop
, char *strval
,
168 uint64_t intval
, zprop_source_t src
)
170 const char *propname
= zpool_prop_to_name(prop
);
173 VERIFY(nvlist_alloc(&propval
, NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
174 VERIFY(nvlist_add_uint64(propval
, ZPROP_SOURCE
, src
) == 0);
177 VERIFY(nvlist_add_string(propval
, ZPROP_VALUE
, strval
) == 0);
179 VERIFY(nvlist_add_uint64(propval
, ZPROP_VALUE
, intval
) == 0);
181 VERIFY(nvlist_add_nvlist(nvl
, propname
, propval
) == 0);
182 nvlist_free(propval
);
186 * Get property values from the spa configuration.
189 spa_prop_get_config(spa_t
*spa
, nvlist_t
**nvp
)
191 vdev_t
*rvd
= spa
->spa_root_vdev
;
192 dsl_pool_t
*pool
= spa
->spa_dsl_pool
;
193 uint64_t size
, alloc
, cap
, version
;
194 zprop_source_t src
= ZPROP_SRC_NONE
;
195 spa_config_dirent_t
*dp
;
196 metaslab_class_t
*mc
= spa_normal_class(spa
);
198 ASSERT(MUTEX_HELD(&spa
->spa_props_lock
));
201 alloc
= metaslab_class_get_alloc(spa_normal_class(spa
));
202 size
= metaslab_class_get_space(spa_normal_class(spa
));
203 spa_prop_add_list(*nvp
, ZPOOL_PROP_NAME
, spa_name(spa
), 0, src
);
204 spa_prop_add_list(*nvp
, ZPOOL_PROP_SIZE
, NULL
, size
, src
);
205 spa_prop_add_list(*nvp
, ZPOOL_PROP_ALLOCATED
, NULL
, alloc
, src
);
206 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREE
, NULL
,
209 spa_prop_add_list(*nvp
, ZPOOL_PROP_FRAGMENTATION
, NULL
,
210 metaslab_class_fragmentation(mc
), src
);
211 spa_prop_add_list(*nvp
, ZPOOL_PROP_EXPANDSZ
, NULL
,
212 metaslab_class_expandable_space(mc
), src
);
213 spa_prop_add_list(*nvp
, ZPOOL_PROP_READONLY
, NULL
,
214 (spa_mode(spa
) == FREAD
), src
);
216 cap
= (size
== 0) ? 0 : (alloc
* 100 / size
);
217 spa_prop_add_list(*nvp
, ZPOOL_PROP_CAPACITY
, NULL
, cap
, src
);
219 spa_prop_add_list(*nvp
, ZPOOL_PROP_DEDUPRATIO
, NULL
,
220 ddt_get_pool_dedup_ratio(spa
), src
);
222 spa_prop_add_list(*nvp
, ZPOOL_PROP_HEALTH
, NULL
,
223 rvd
->vdev_state
, src
);
225 version
= spa_version(spa
);
226 if (version
== zpool_prop_default_numeric(ZPOOL_PROP_VERSION
))
227 src
= ZPROP_SRC_DEFAULT
;
229 src
= ZPROP_SRC_LOCAL
;
230 spa_prop_add_list(*nvp
, ZPOOL_PROP_VERSION
, NULL
, version
, src
);
235 * The $FREE directory was introduced in SPA_VERSION_DEADLISTS,
236 * when opening pools before this version freedir will be NULL.
238 if (pool
->dp_free_dir
!= NULL
) {
239 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
, NULL
,
240 pool
->dp_free_dir
->dd_phys
->dd_used_bytes
, src
);
242 spa_prop_add_list(*nvp
, ZPOOL_PROP_FREEING
,
246 if (pool
->dp_leak_dir
!= NULL
) {
247 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
, NULL
,
248 pool
->dp_leak_dir
->dd_phys
->dd_used_bytes
, src
);
250 spa_prop_add_list(*nvp
, ZPOOL_PROP_LEAKED
,
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 dsl_pool_config_enter(dp
, FTAG
);
332 if ((err
= dsl_dataset_hold_obj(dp
,
333 za
.za_first_integer
, FTAG
, &ds
))) {
334 dsl_pool_config_exit(dp
, FTAG
);
339 MAXNAMELEN
+ strlen(MOS_DIR_NAME
) + 1,
341 dsl_dataset_name(ds
, strval
);
342 dsl_dataset_rele(ds
, FTAG
);
343 dsl_pool_config_exit(dp
, FTAG
);
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
)) {
408 error
= SET_ERROR(EINVAL
);
413 * Sanitize the input.
415 if (nvpair_type(elem
) != DATA_TYPE_UINT64
) {
416 error
= SET_ERROR(EINVAL
);
420 if (nvpair_value_uint64(elem
, &intval
) != 0) {
421 error
= SET_ERROR(EINVAL
);
426 error
= SET_ERROR(EINVAL
);
430 fname
= strchr(propname
, '@') + 1;
431 if (zfeature_lookup_name(fname
, NULL
) != 0) {
432 error
= SET_ERROR(EINVAL
);
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
||
445 error
= SET_ERROR(EINVAL
);
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)
454 error
= SET_ERROR(EINVAL
);
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
) {
464 error
= SET_ERROR(ENOTSUP
);
469 * Make sure the vdev config is bootable
471 if (!vdev_is_bootable(spa
->spa_root_vdev
)) {
472 error
= SET_ERROR(ENOTSUP
);
478 error
= nvpair_value_string(elem
, &strval
);
484 if (strval
== NULL
|| strval
[0] == '\0') {
485 objnum
= zpool_prop_default_numeric(
490 error
= dmu_objset_hold(strval
, FTAG
, &os
);
494 /* Must be ZPL and not gzip compressed. */
496 if (dmu_objset_type(os
) != DMU_OST_ZFS
) {
497 error
= SET_ERROR(ENOTSUP
);
499 dsl_prop_get_int_ds(dmu_objset_ds(os
),
500 zfs_prop_to_name(ZFS_PROP_COMPRESSION
),
502 !BOOTFS_COMPRESS_VALID(compress
)) {
503 error
= SET_ERROR(ENOTSUP
);
505 objnum
= dmu_objset_id(os
);
507 dmu_objset_rele(os
, FTAG
);
511 case ZPOOL_PROP_FAILUREMODE
:
512 error
= nvpair_value_uint64(elem
, &intval
);
513 if (!error
&& (intval
< ZIO_FAILURE_MODE_WAIT
||
514 intval
> ZIO_FAILURE_MODE_PANIC
))
515 error
= SET_ERROR(EINVAL
);
518 * This is a special case which only occurs when
519 * the pool has completely failed. This allows
520 * the user to change the in-core failmode property
521 * without syncing it out to disk (I/Os might
522 * currently be blocked). We do this by returning
523 * EIO to the caller (spa_prop_set) to trick it
524 * into thinking we encountered a property validation
527 if (!error
&& spa_suspended(spa
)) {
528 spa
->spa_failmode
= intval
;
529 error
= SET_ERROR(EIO
);
533 case ZPOOL_PROP_CACHEFILE
:
534 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
537 if (strval
[0] == '\0')
540 if (strcmp(strval
, "none") == 0)
543 if (strval
[0] != '/') {
544 error
= SET_ERROR(EINVAL
);
548 slash
= strrchr(strval
, '/');
549 ASSERT(slash
!= NULL
);
551 if (slash
[1] == '\0' || strcmp(slash
, "/.") == 0 ||
552 strcmp(slash
, "/..") == 0)
553 error
= SET_ERROR(EINVAL
);
556 case ZPOOL_PROP_COMMENT
:
557 if ((error
= nvpair_value_string(elem
, &strval
)) != 0)
559 for (check
= strval
; *check
!= '\0'; check
++) {
560 if (!isprint(*check
)) {
561 error
= SET_ERROR(EINVAL
);
566 if (strlen(strval
) > ZPROP_MAX_COMMENT
)
567 error
= SET_ERROR(E2BIG
);
570 case ZPOOL_PROP_DEDUPDITTO
:
571 if (spa_version(spa
) < SPA_VERSION_DEDUP
)
572 error
= SET_ERROR(ENOTSUP
);
574 error
= nvpair_value_uint64(elem
, &intval
);
576 intval
!= 0 && intval
< ZIO_DEDUPDITTO_MIN
)
577 error
= SET_ERROR(EINVAL
);
588 if (!error
&& reset_bootfs
) {
589 error
= nvlist_remove(props
,
590 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), DATA_TYPE_STRING
);
593 error
= nvlist_add_uint64(props
,
594 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), objnum
);
602 spa_configfile_set(spa_t
*spa
, nvlist_t
*nvp
, boolean_t need_sync
)
605 spa_config_dirent_t
*dp
;
607 if (nvlist_lookup_string(nvp
, zpool_prop_to_name(ZPOOL_PROP_CACHEFILE
),
611 dp
= kmem_alloc(sizeof (spa_config_dirent_t
),
614 if (cachefile
[0] == '\0')
615 dp
->scd_path
= spa_strdup(spa_config_path
);
616 else if (strcmp(cachefile
, "none") == 0)
619 dp
->scd_path
= spa_strdup(cachefile
);
621 list_insert_head(&spa
->spa_config_list
, dp
);
623 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
627 spa_prop_set(spa_t
*spa
, nvlist_t
*nvp
)
630 nvpair_t
*elem
= NULL
;
631 boolean_t need_sync
= B_FALSE
;
633 if ((error
= spa_prop_validate(spa
, nvp
)) != 0)
636 while ((elem
= nvlist_next_nvpair(nvp
, elem
)) != NULL
) {
637 zpool_prop_t prop
= zpool_name_to_prop(nvpair_name(elem
));
639 if (prop
== ZPOOL_PROP_CACHEFILE
||
640 prop
== ZPOOL_PROP_ALTROOT
||
641 prop
== ZPOOL_PROP_READONLY
)
644 if (prop
== ZPOOL_PROP_VERSION
|| prop
== ZPROP_INVAL
) {
647 if (prop
== ZPOOL_PROP_VERSION
) {
648 VERIFY(nvpair_value_uint64(elem
, &ver
) == 0);
650 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
651 ver
= SPA_VERSION_FEATURES
;
655 /* Save time if the version is already set. */
656 if (ver
== spa_version(spa
))
660 * In addition to the pool directory object, we might
661 * create the pool properties object, the features for
662 * read object, the features for write object, or the
663 * feature descriptions object.
665 error
= dsl_sync_task(spa
->spa_name
, NULL
,
666 spa_sync_version
, &ver
, 6);
677 return (dsl_sync_task(spa
->spa_name
, NULL
, spa_sync_props
,
685 * If the bootfs property value is dsobj, clear it.
688 spa_prop_clear_bootfs(spa_t
*spa
, uint64_t dsobj
, dmu_tx_t
*tx
)
690 if (spa
->spa_bootfs
== dsobj
&& spa
->spa_pool_props_object
!= 0) {
691 VERIFY(zap_remove(spa
->spa_meta_objset
,
692 spa
->spa_pool_props_object
,
693 zpool_prop_to_name(ZPOOL_PROP_BOOTFS
), tx
) == 0);
700 spa_change_guid_check(void *arg
, dmu_tx_t
*tx
)
702 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
703 vdev_t
*rvd
= spa
->spa_root_vdev
;
705 ASSERTV(uint64_t *newguid
= arg
);
707 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
708 vdev_state
= rvd
->vdev_state
;
709 spa_config_exit(spa
, SCL_STATE
, FTAG
);
711 if (vdev_state
!= VDEV_STATE_HEALTHY
)
712 return (SET_ERROR(ENXIO
));
714 ASSERT3U(spa_guid(spa
), !=, *newguid
);
720 spa_change_guid_sync(void *arg
, dmu_tx_t
*tx
)
722 uint64_t *newguid
= arg
;
723 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
725 vdev_t
*rvd
= spa
->spa_root_vdev
;
727 oldguid
= spa_guid(spa
);
729 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
730 rvd
->vdev_guid
= *newguid
;
731 rvd
->vdev_guid_sum
+= (*newguid
- oldguid
);
732 vdev_config_dirty(rvd
);
733 spa_config_exit(spa
, SCL_STATE
, FTAG
);
735 spa_history_log_internal(spa
, "guid change", tx
, "old=%llu new=%llu",
740 * Change the GUID for the pool. This is done so that we can later
741 * re-import a pool built from a clone of our own vdevs. We will modify
742 * the root vdev's guid, our own pool guid, and then mark all of our
743 * vdevs dirty. Note that we must make sure that all our vdevs are
744 * online when we do this, or else any vdevs that weren't present
745 * would be orphaned from our pool. We are also going to issue a
746 * sysevent to update any watchers.
749 spa_change_guid(spa_t
*spa
)
754 mutex_enter(&spa
->spa_vdev_top_lock
);
755 mutex_enter(&spa_namespace_lock
);
756 guid
= spa_generate_guid(NULL
);
758 error
= dsl_sync_task(spa
->spa_name
, spa_change_guid_check
,
759 spa_change_guid_sync
, &guid
, 5);
762 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
763 spa_event_notify(spa
, NULL
, FM_EREPORT_ZFS_POOL_REGUID
);
766 mutex_exit(&spa_namespace_lock
);
767 mutex_exit(&spa
->spa_vdev_top_lock
);
773 * ==========================================================================
774 * SPA state manipulation (open/create/destroy/import/export)
775 * ==========================================================================
779 spa_error_entry_compare(const void *a
, const void *b
)
781 spa_error_entry_t
*sa
= (spa_error_entry_t
*)a
;
782 spa_error_entry_t
*sb
= (spa_error_entry_t
*)b
;
785 ret
= bcmp(&sa
->se_bookmark
, &sb
->se_bookmark
,
786 sizeof (zbookmark_phys_t
));
797 * Utility function which retrieves copies of the current logs and
798 * re-initializes them in the process.
801 spa_get_errlists(spa_t
*spa
, avl_tree_t
*last
, avl_tree_t
*scrub
)
803 ASSERT(MUTEX_HELD(&spa
->spa_errlist_lock
));
805 bcopy(&spa
->spa_errlist_last
, last
, sizeof (avl_tree_t
));
806 bcopy(&spa
->spa_errlist_scrub
, scrub
, sizeof (avl_tree_t
));
808 avl_create(&spa
->spa_errlist_scrub
,
809 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
810 offsetof(spa_error_entry_t
, se_avl
));
811 avl_create(&spa
->spa_errlist_last
,
812 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
813 offsetof(spa_error_entry_t
, se_avl
));
817 spa_taskqs_init(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
819 const zio_taskq_info_t
*ztip
= &zio_taskqs
[t
][q
];
820 enum zti_modes mode
= ztip
->zti_mode
;
821 uint_t value
= ztip
->zti_value
;
822 uint_t count
= ztip
->zti_count
;
823 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
826 boolean_t batch
= B_FALSE
;
828 if (mode
== ZTI_MODE_NULL
) {
830 tqs
->stqs_taskq
= NULL
;
834 ASSERT3U(count
, >, 0);
836 tqs
->stqs_count
= count
;
837 tqs
->stqs_taskq
= kmem_alloc(count
* sizeof (taskq_t
*), KM_SLEEP
);
841 ASSERT3U(value
, >=, 1);
842 value
= MAX(value
, 1);
847 flags
|= TASKQ_THREADS_CPU_PCT
;
848 value
= zio_taskq_batch_pct
;
852 panic("unrecognized mode for %s_%s taskq (%u:%u) in "
854 zio_type_name
[t
], zio_taskq_types
[q
], mode
, value
);
858 for (i
= 0; i
< count
; i
++) {
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 pri_t pri
= maxclsyspri
;
878 * The write issue taskq can be extremely CPU
879 * intensive. Run it at slightly lower priority
880 * than the other taskqs.
882 if (t
== ZIO_TYPE_WRITE
&& q
== ZIO_TASKQ_ISSUE
)
885 tq
= taskq_create_proc(name
, value
, pri
, 50,
886 INT_MAX
, spa
->spa_proc
, flags
);
889 tqs
->stqs_taskq
[i
] = tq
;
894 spa_taskqs_fini(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
)
896 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
899 if (tqs
->stqs_taskq
== NULL
) {
900 ASSERT3U(tqs
->stqs_count
, ==, 0);
904 for (i
= 0; i
< tqs
->stqs_count
; i
++) {
905 ASSERT3P(tqs
->stqs_taskq
[i
], !=, NULL
);
906 taskq_destroy(tqs
->stqs_taskq
[i
]);
909 kmem_free(tqs
->stqs_taskq
, tqs
->stqs_count
* sizeof (taskq_t
*));
910 tqs
->stqs_taskq
= NULL
;
914 * Dispatch a task to the appropriate taskq for the ZFS I/O type and priority.
915 * Note that a type may have multiple discrete taskqs to avoid lock contention
916 * on the taskq itself. In that case we choose which taskq at random by using
917 * the low bits of gethrtime().
920 spa_taskq_dispatch_ent(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
921 task_func_t
*func
, void *arg
, uint_t flags
, taskq_ent_t
*ent
)
923 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
926 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
927 ASSERT3U(tqs
->stqs_count
, !=, 0);
929 if (tqs
->stqs_count
== 1) {
930 tq
= tqs
->stqs_taskq
[0];
932 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
935 taskq_dispatch_ent(tq
, func
, arg
, flags
, ent
);
939 * Same as spa_taskq_dispatch_ent() but block on the task until completion.
942 spa_taskq_dispatch_sync(spa_t
*spa
, zio_type_t t
, zio_taskq_type_t q
,
943 task_func_t
*func
, void *arg
, uint_t flags
)
945 spa_taskqs_t
*tqs
= &spa
->spa_zio_taskq
[t
][q
];
949 ASSERT3P(tqs
->stqs_taskq
, !=, NULL
);
950 ASSERT3U(tqs
->stqs_count
, !=, 0);
952 if (tqs
->stqs_count
== 1) {
953 tq
= tqs
->stqs_taskq
[0];
955 tq
= tqs
->stqs_taskq
[((uint64_t)gethrtime()) % tqs
->stqs_count
];
958 id
= taskq_dispatch(tq
, func
, arg
, flags
);
960 taskq_wait_id(tq
, id
);
964 spa_create_zio_taskqs(spa_t
*spa
)
968 for (t
= 0; t
< ZIO_TYPES
; t
++) {
969 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
970 spa_taskqs_init(spa
, t
, q
);
975 #if defined(_KERNEL) && defined(HAVE_SPA_THREAD)
977 spa_thread(void *arg
)
982 user_t
*pu
= PTOU(curproc
);
984 CALLB_CPR_INIT(&cprinfo
, &spa
->spa_proc_lock
, callb_generic_cpr
,
987 ASSERT(curproc
!= &p0
);
988 (void) snprintf(pu
->u_psargs
, sizeof (pu
->u_psargs
),
989 "zpool-%s", spa
->spa_name
);
990 (void) strlcpy(pu
->u_comm
, pu
->u_psargs
, sizeof (pu
->u_comm
));
992 /* bind this thread to the requested psrset */
993 if (zio_taskq_psrset_bind
!= PS_NONE
) {
995 mutex_enter(&cpu_lock
);
996 mutex_enter(&pidlock
);
997 mutex_enter(&curproc
->p_lock
);
999 if (cpupart_bind_thread(curthread
, zio_taskq_psrset_bind
,
1000 0, NULL
, NULL
) == 0) {
1001 curthread
->t_bind_pset
= zio_taskq_psrset_bind
;
1004 "Couldn't bind process for zfs pool \"%s\" to "
1005 "pset %d\n", spa
->spa_name
, zio_taskq_psrset_bind
);
1008 mutex_exit(&curproc
->p_lock
);
1009 mutex_exit(&pidlock
);
1010 mutex_exit(&cpu_lock
);
1014 if (zio_taskq_sysdc
) {
1015 sysdc_thread_enter(curthread
, 100, 0);
1018 spa
->spa_proc
= curproc
;
1019 spa
->spa_did
= curthread
->t_did
;
1021 spa_create_zio_taskqs(spa
);
1023 mutex_enter(&spa
->spa_proc_lock
);
1024 ASSERT(spa
->spa_proc_state
== SPA_PROC_CREATED
);
1026 spa
->spa_proc_state
= SPA_PROC_ACTIVE
;
1027 cv_broadcast(&spa
->spa_proc_cv
);
1029 CALLB_CPR_SAFE_BEGIN(&cprinfo
);
1030 while (spa
->spa_proc_state
== SPA_PROC_ACTIVE
)
1031 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1032 CALLB_CPR_SAFE_END(&cprinfo
, &spa
->spa_proc_lock
);
1034 ASSERT(spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
);
1035 spa
->spa_proc_state
= SPA_PROC_GONE
;
1036 spa
->spa_proc
= &p0
;
1037 cv_broadcast(&spa
->spa_proc_cv
);
1038 CALLB_CPR_EXIT(&cprinfo
); /* drops spa_proc_lock */
1040 mutex_enter(&curproc
->p_lock
);
1046 * Activate an uninitialized pool.
1049 spa_activate(spa_t
*spa
, int mode
)
1051 ASSERT(spa
->spa_state
== POOL_STATE_UNINITIALIZED
);
1053 spa
->spa_state
= POOL_STATE_ACTIVE
;
1054 spa
->spa_mode
= mode
;
1056 spa
->spa_normal_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1057 spa
->spa_log_class
= metaslab_class_create(spa
, zfs_metaslab_ops
);
1059 /* Try to create a covering process */
1060 mutex_enter(&spa
->spa_proc_lock
);
1061 ASSERT(spa
->spa_proc_state
== SPA_PROC_NONE
);
1062 ASSERT(spa
->spa_proc
== &p0
);
1065 #ifdef HAVE_SPA_THREAD
1066 /* Only create a process if we're going to be around a while. */
1067 if (spa_create_process
&& strcmp(spa
->spa_name
, TRYIMPORT_NAME
) != 0) {
1068 if (newproc(spa_thread
, (caddr_t
)spa
, syscid
, maxclsyspri
,
1070 spa
->spa_proc_state
= SPA_PROC_CREATED
;
1071 while (spa
->spa_proc_state
== SPA_PROC_CREATED
) {
1072 cv_wait(&spa
->spa_proc_cv
,
1073 &spa
->spa_proc_lock
);
1075 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1076 ASSERT(spa
->spa_proc
!= &p0
);
1077 ASSERT(spa
->spa_did
!= 0);
1081 "Couldn't create process for zfs pool \"%s\"\n",
1086 #endif /* HAVE_SPA_THREAD */
1087 mutex_exit(&spa
->spa_proc_lock
);
1089 /* If we didn't create a process, we need to create our taskqs. */
1090 if (spa
->spa_proc
== &p0
) {
1091 spa_create_zio_taskqs(spa
);
1094 list_create(&spa
->spa_config_dirty_list
, sizeof (vdev_t
),
1095 offsetof(vdev_t
, vdev_config_dirty_node
));
1096 list_create(&spa
->spa_state_dirty_list
, sizeof (vdev_t
),
1097 offsetof(vdev_t
, vdev_state_dirty_node
));
1099 txg_list_create(&spa
->spa_vdev_txg_list
,
1100 offsetof(struct vdev
, vdev_txg_node
));
1102 avl_create(&spa
->spa_errlist_scrub
,
1103 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1104 offsetof(spa_error_entry_t
, se_avl
));
1105 avl_create(&spa
->spa_errlist_last
,
1106 spa_error_entry_compare
, sizeof (spa_error_entry_t
),
1107 offsetof(spa_error_entry_t
, se_avl
));
1111 * Opposite of spa_activate().
1114 spa_deactivate(spa_t
*spa
)
1118 ASSERT(spa
->spa_sync_on
== B_FALSE
);
1119 ASSERT(spa
->spa_dsl_pool
== NULL
);
1120 ASSERT(spa
->spa_root_vdev
== NULL
);
1121 ASSERT(spa
->spa_async_zio_root
== NULL
);
1122 ASSERT(spa
->spa_state
!= POOL_STATE_UNINITIALIZED
);
1124 txg_list_destroy(&spa
->spa_vdev_txg_list
);
1126 list_destroy(&spa
->spa_config_dirty_list
);
1127 list_destroy(&spa
->spa_state_dirty_list
);
1129 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
1131 for (t
= 0; t
< ZIO_TYPES
; t
++) {
1132 for (q
= 0; q
< ZIO_TASKQ_TYPES
; q
++) {
1133 spa_taskqs_fini(spa
, t
, q
);
1137 metaslab_class_destroy(spa
->spa_normal_class
);
1138 spa
->spa_normal_class
= NULL
;
1140 metaslab_class_destroy(spa
->spa_log_class
);
1141 spa
->spa_log_class
= NULL
;
1144 * If this was part of an import or the open otherwise failed, we may
1145 * still have errors left in the queues. Empty them just in case.
1147 spa_errlog_drain(spa
);
1149 avl_destroy(&spa
->spa_errlist_scrub
);
1150 avl_destroy(&spa
->spa_errlist_last
);
1152 spa
->spa_state
= POOL_STATE_UNINITIALIZED
;
1154 mutex_enter(&spa
->spa_proc_lock
);
1155 if (spa
->spa_proc_state
!= SPA_PROC_NONE
) {
1156 ASSERT(spa
->spa_proc_state
== SPA_PROC_ACTIVE
);
1157 spa
->spa_proc_state
= SPA_PROC_DEACTIVATE
;
1158 cv_broadcast(&spa
->spa_proc_cv
);
1159 while (spa
->spa_proc_state
== SPA_PROC_DEACTIVATE
) {
1160 ASSERT(spa
->spa_proc
!= &p0
);
1161 cv_wait(&spa
->spa_proc_cv
, &spa
->spa_proc_lock
);
1163 ASSERT(spa
->spa_proc_state
== SPA_PROC_GONE
);
1164 spa
->spa_proc_state
= SPA_PROC_NONE
;
1166 ASSERT(spa
->spa_proc
== &p0
);
1167 mutex_exit(&spa
->spa_proc_lock
);
1170 * We want to make sure spa_thread() has actually exited the ZFS
1171 * module, so that the module can't be unloaded out from underneath
1174 if (spa
->spa_did
!= 0) {
1175 thread_join(spa
->spa_did
);
1181 * Verify a pool configuration, and construct the vdev tree appropriately. This
1182 * will create all the necessary vdevs in the appropriate layout, with each vdev
1183 * in the CLOSED state. This will prep the pool before open/creation/import.
1184 * All vdev validation is done by the vdev_alloc() routine.
1187 spa_config_parse(spa_t
*spa
, vdev_t
**vdp
, nvlist_t
*nv
, vdev_t
*parent
,
1188 uint_t id
, int atype
)
1195 if ((error
= vdev_alloc(spa
, vdp
, nv
, parent
, id
, atype
)) != 0)
1198 if ((*vdp
)->vdev_ops
->vdev_op_leaf
)
1201 error
= nvlist_lookup_nvlist_array(nv
, ZPOOL_CONFIG_CHILDREN
,
1204 if (error
== ENOENT
)
1210 return (SET_ERROR(EINVAL
));
1213 for (c
= 0; c
< children
; c
++) {
1215 if ((error
= spa_config_parse(spa
, &vd
, child
[c
], *vdp
, c
,
1223 ASSERT(*vdp
!= NULL
);
1229 * Opposite of spa_load().
1232 spa_unload(spa_t
*spa
)
1236 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
1241 spa_async_suspend(spa
);
1246 if (spa
->spa_sync_on
) {
1247 txg_sync_stop(spa
->spa_dsl_pool
);
1248 spa
->spa_sync_on
= B_FALSE
;
1252 * Wait for any outstanding async I/O to complete.
1254 if (spa
->spa_async_zio_root
!= NULL
) {
1255 (void) zio_wait(spa
->spa_async_zio_root
);
1256 spa
->spa_async_zio_root
= NULL
;
1259 bpobj_close(&spa
->spa_deferred_bpobj
);
1261 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1266 if (spa
->spa_root_vdev
)
1267 vdev_free(spa
->spa_root_vdev
);
1268 ASSERT(spa
->spa_root_vdev
== NULL
);
1271 * Close the dsl pool.
1273 if (spa
->spa_dsl_pool
) {
1274 dsl_pool_close(spa
->spa_dsl_pool
);
1275 spa
->spa_dsl_pool
= NULL
;
1276 spa
->spa_meta_objset
= NULL
;
1283 * Drop and purge level 2 cache
1285 spa_l2cache_drop(spa
);
1287 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1288 vdev_free(spa
->spa_spares
.sav_vdevs
[i
]);
1289 if (spa
->spa_spares
.sav_vdevs
) {
1290 kmem_free(spa
->spa_spares
.sav_vdevs
,
1291 spa
->spa_spares
.sav_count
* sizeof (void *));
1292 spa
->spa_spares
.sav_vdevs
= NULL
;
1294 if (spa
->spa_spares
.sav_config
) {
1295 nvlist_free(spa
->spa_spares
.sav_config
);
1296 spa
->spa_spares
.sav_config
= NULL
;
1298 spa
->spa_spares
.sav_count
= 0;
1300 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
1301 vdev_clear_stats(spa
->spa_l2cache
.sav_vdevs
[i
]);
1302 vdev_free(spa
->spa_l2cache
.sav_vdevs
[i
]);
1304 if (spa
->spa_l2cache
.sav_vdevs
) {
1305 kmem_free(spa
->spa_l2cache
.sav_vdevs
,
1306 spa
->spa_l2cache
.sav_count
* sizeof (void *));
1307 spa
->spa_l2cache
.sav_vdevs
= NULL
;
1309 if (spa
->spa_l2cache
.sav_config
) {
1310 nvlist_free(spa
->spa_l2cache
.sav_config
);
1311 spa
->spa_l2cache
.sav_config
= NULL
;
1313 spa
->spa_l2cache
.sav_count
= 0;
1315 spa
->spa_async_suspended
= 0;
1317 if (spa
->spa_comment
!= NULL
) {
1318 spa_strfree(spa
->spa_comment
);
1319 spa
->spa_comment
= NULL
;
1322 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1326 * Load (or re-load) the current list of vdevs describing the active spares for
1327 * this pool. When this is called, we have some form of basic information in
1328 * 'spa_spares.sav_config'. We parse this into vdevs, try to open them, and
1329 * then re-generate a more complete list including status information.
1332 spa_load_spares(spa_t
*spa
)
1339 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1342 * First, close and free any existing spare vdevs.
1344 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1345 vd
= spa
->spa_spares
.sav_vdevs
[i
];
1347 /* Undo the call to spa_activate() below */
1348 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1349 B_FALSE
)) != NULL
&& tvd
->vdev_isspare
)
1350 spa_spare_remove(tvd
);
1355 if (spa
->spa_spares
.sav_vdevs
)
1356 kmem_free(spa
->spa_spares
.sav_vdevs
,
1357 spa
->spa_spares
.sav_count
* sizeof (void *));
1359 if (spa
->spa_spares
.sav_config
== NULL
)
1362 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
1363 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
1365 spa
->spa_spares
.sav_count
= (int)nspares
;
1366 spa
->spa_spares
.sav_vdevs
= NULL
;
1372 * Construct the array of vdevs, opening them to get status in the
1373 * process. For each spare, there is potentially two different vdev_t
1374 * structures associated with it: one in the list of spares (used only
1375 * for basic validation purposes) and one in the active vdev
1376 * configuration (if it's spared in). During this phase we open and
1377 * validate each vdev on the spare list. If the vdev also exists in the
1378 * active configuration, then we also mark this vdev as an active spare.
1380 spa
->spa_spares
.sav_vdevs
= kmem_zalloc(nspares
* sizeof (void *),
1382 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
1383 VERIFY(spa_config_parse(spa
, &vd
, spares
[i
], NULL
, 0,
1384 VDEV_ALLOC_SPARE
) == 0);
1387 spa
->spa_spares
.sav_vdevs
[i
] = vd
;
1389 if ((tvd
= spa_lookup_by_guid(spa
, vd
->vdev_guid
,
1390 B_FALSE
)) != NULL
) {
1391 if (!tvd
->vdev_isspare
)
1395 * We only mark the spare active if we were successfully
1396 * able to load the vdev. Otherwise, importing a pool
1397 * with a bad active spare would result in strange
1398 * behavior, because multiple pool would think the spare
1399 * is actively in use.
1401 * There is a vulnerability here to an equally bizarre
1402 * circumstance, where a dead active spare is later
1403 * brought back to life (onlined or otherwise). Given
1404 * the rarity of this scenario, and the extra complexity
1405 * it adds, we ignore the possibility.
1407 if (!vdev_is_dead(tvd
))
1408 spa_spare_activate(tvd
);
1412 vd
->vdev_aux
= &spa
->spa_spares
;
1414 if (vdev_open(vd
) != 0)
1417 if (vdev_validate_aux(vd
) == 0)
1422 * Recompute the stashed list of spares, with status information
1425 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
, ZPOOL_CONFIG_SPARES
,
1426 DATA_TYPE_NVLIST_ARRAY
) == 0);
1428 spares
= kmem_alloc(spa
->spa_spares
.sav_count
* sizeof (void *),
1430 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1431 spares
[i
] = vdev_config_generate(spa
,
1432 spa
->spa_spares
.sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_SPARE
);
1433 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
1434 ZPOOL_CONFIG_SPARES
, spares
, spa
->spa_spares
.sav_count
) == 0);
1435 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
1436 nvlist_free(spares
[i
]);
1437 kmem_free(spares
, spa
->spa_spares
.sav_count
* sizeof (void *));
1441 * Load (or re-load) the current list of vdevs describing the active l2cache for
1442 * this pool. When this is called, we have some form of basic information in
1443 * 'spa_l2cache.sav_config'. We parse this into vdevs, try to open them, and
1444 * then re-generate a more complete list including status information.
1445 * Devices which are already active have their details maintained, and are
1449 spa_load_l2cache(spa_t
*spa
)
1453 int i
, j
, oldnvdevs
;
1455 vdev_t
*vd
, **oldvdevs
, **newvdevs
;
1456 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
1458 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
1460 if (sav
->sav_config
!= NULL
) {
1461 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
,
1462 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
1463 newvdevs
= kmem_alloc(nl2cache
* sizeof (void *), KM_PUSHPAGE
);
1469 oldvdevs
= sav
->sav_vdevs
;
1470 oldnvdevs
= sav
->sav_count
;
1471 sav
->sav_vdevs
= NULL
;
1475 * Process new nvlist of vdevs.
1477 for (i
= 0; i
< nl2cache
; i
++) {
1478 VERIFY(nvlist_lookup_uint64(l2cache
[i
], ZPOOL_CONFIG_GUID
,
1482 for (j
= 0; j
< oldnvdevs
; j
++) {
1484 if (vd
!= NULL
&& guid
== vd
->vdev_guid
) {
1486 * Retain previous vdev for add/remove ops.
1494 if (newvdevs
[i
] == NULL
) {
1498 VERIFY(spa_config_parse(spa
, &vd
, l2cache
[i
], NULL
, 0,
1499 VDEV_ALLOC_L2CACHE
) == 0);
1504 * Commit this vdev as an l2cache device,
1505 * even if it fails to open.
1507 spa_l2cache_add(vd
);
1512 spa_l2cache_activate(vd
);
1514 if (vdev_open(vd
) != 0)
1517 (void) vdev_validate_aux(vd
);
1519 if (!vdev_is_dead(vd
))
1520 l2arc_add_vdev(spa
, vd
);
1525 * Purge vdevs that were dropped
1527 for (i
= 0; i
< oldnvdevs
; i
++) {
1532 ASSERT(vd
->vdev_isl2cache
);
1534 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
1535 pool
!= 0ULL && l2arc_vdev_present(vd
))
1536 l2arc_remove_vdev(vd
);
1537 vdev_clear_stats(vd
);
1543 kmem_free(oldvdevs
, oldnvdevs
* sizeof (void *));
1545 if (sav
->sav_config
== NULL
)
1548 sav
->sav_vdevs
= newvdevs
;
1549 sav
->sav_count
= (int)nl2cache
;
1552 * Recompute the stashed list of l2cache devices, with status
1553 * information this time.
1555 VERIFY(nvlist_remove(sav
->sav_config
, ZPOOL_CONFIG_L2CACHE
,
1556 DATA_TYPE_NVLIST_ARRAY
) == 0);
1558 l2cache
= kmem_alloc(sav
->sav_count
* sizeof (void *), KM_PUSHPAGE
);
1559 for (i
= 0; i
< sav
->sav_count
; i
++)
1560 l2cache
[i
] = vdev_config_generate(spa
,
1561 sav
->sav_vdevs
[i
], B_TRUE
, VDEV_CONFIG_L2CACHE
);
1562 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
1563 ZPOOL_CONFIG_L2CACHE
, l2cache
, sav
->sav_count
) == 0);
1565 for (i
= 0; i
< sav
->sav_count
; i
++)
1566 nvlist_free(l2cache
[i
]);
1568 kmem_free(l2cache
, sav
->sav_count
* sizeof (void *));
1572 load_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
**value
)
1575 char *packed
= NULL
;
1580 error
= dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
);
1584 nvsize
= *(uint64_t *)db
->db_data
;
1585 dmu_buf_rele(db
, FTAG
);
1587 packed
= kmem_alloc(nvsize
, KM_PUSHPAGE
| KM_NODEBUG
);
1588 error
= dmu_read(spa
->spa_meta_objset
, obj
, 0, nvsize
, packed
,
1591 error
= nvlist_unpack(packed
, nvsize
, value
, 0);
1592 kmem_free(packed
, nvsize
);
1598 * Checks to see if the given vdev could not be opened, in which case we post a
1599 * sysevent to notify the autoreplace code that the device has been removed.
1602 spa_check_removed(vdev_t
*vd
)
1606 for (c
= 0; c
< vd
->vdev_children
; c
++)
1607 spa_check_removed(vd
->vdev_child
[c
]);
1609 if (vd
->vdev_ops
->vdev_op_leaf
&& vdev_is_dead(vd
) &&
1611 zfs_ereport_post(FM_EREPORT_RESOURCE_AUTOREPLACE
,
1612 vd
->vdev_spa
, vd
, NULL
, 0, 0);
1613 spa_event_notify(vd
->vdev_spa
, vd
, FM_EREPORT_ZFS_DEVICE_CHECK
);
1618 * Validate the current config against the MOS config
1621 spa_config_valid(spa_t
*spa
, nvlist_t
*config
)
1623 vdev_t
*mrvd
, *rvd
= spa
->spa_root_vdev
;
1627 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nv
) == 0);
1629 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1630 VERIFY(spa_config_parse(spa
, &mrvd
, nv
, NULL
, 0, VDEV_ALLOC_LOAD
) == 0);
1632 ASSERT3U(rvd
->vdev_children
, ==, mrvd
->vdev_children
);
1635 * If we're doing a normal import, then build up any additional
1636 * diagnostic information about missing devices in this config.
1637 * We'll pass this up to the user for further processing.
1639 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
)) {
1640 nvlist_t
**child
, *nv
;
1643 child
= kmem_alloc(rvd
->vdev_children
* sizeof (nvlist_t
**),
1645 VERIFY(nvlist_alloc(&nv
, NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
1647 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1648 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1649 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1651 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1652 mtvd
->vdev_ops
!= &vdev_missing_ops
&&
1654 child
[idx
++] = vdev_config_generate(spa
, mtvd
,
1659 VERIFY(nvlist_add_nvlist_array(nv
,
1660 ZPOOL_CONFIG_CHILDREN
, child
, idx
) == 0);
1661 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
1662 ZPOOL_CONFIG_MISSING_DEVICES
, nv
) == 0);
1664 for (i
= 0; i
< idx
; i
++)
1665 nvlist_free(child
[i
]);
1668 kmem_free(child
, rvd
->vdev_children
* sizeof (char **));
1672 * Compare the root vdev tree with the information we have
1673 * from the MOS config (mrvd). Check each top-level vdev
1674 * with the corresponding MOS config top-level (mtvd).
1676 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1677 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1678 vdev_t
*mtvd
= mrvd
->vdev_child
[c
];
1681 * Resolve any "missing" vdevs in the current configuration.
1682 * If we find that the MOS config has more accurate information
1683 * about the top-level vdev then use that vdev instead.
1685 if (tvd
->vdev_ops
== &vdev_missing_ops
&&
1686 mtvd
->vdev_ops
!= &vdev_missing_ops
) {
1688 if (!(spa
->spa_import_flags
& ZFS_IMPORT_MISSING_LOG
))
1692 * Device specific actions.
1694 if (mtvd
->vdev_islog
) {
1695 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
1698 * XXX - once we have 'readonly' pool
1699 * support we should be able to handle
1700 * missing data devices by transitioning
1701 * the pool to readonly.
1707 * Swap the missing vdev with the data we were
1708 * able to obtain from the MOS config.
1710 vdev_remove_child(rvd
, tvd
);
1711 vdev_remove_child(mrvd
, mtvd
);
1713 vdev_add_child(rvd
, mtvd
);
1714 vdev_add_child(mrvd
, tvd
);
1716 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1718 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
1721 } else if (mtvd
->vdev_islog
) {
1723 * Load the slog device's state from the MOS config
1724 * since it's possible that the label does not
1725 * contain the most up-to-date information.
1727 vdev_load_log_state(tvd
, mtvd
);
1732 spa_config_exit(spa
, SCL_ALL
, FTAG
);
1735 * Ensure we were able to validate the config.
1737 return (rvd
->vdev_guid_sum
== spa
->spa_uberblock
.ub_guid_sum
);
1741 * Check for missing log devices
1744 spa_check_logs(spa_t
*spa
)
1746 boolean_t rv
= B_FALSE
;
1748 switch (spa
->spa_log_state
) {
1751 case SPA_LOG_MISSING
:
1752 /* need to recheck in case slog has been restored */
1753 case SPA_LOG_UNKNOWN
:
1754 rv
= (dmu_objset_find(spa
->spa_name
, zil_check_log_chain
,
1755 NULL
, DS_FIND_CHILDREN
) != 0);
1757 spa_set_log_state(spa
, SPA_LOG_MISSING
);
1764 spa_passivate_log(spa_t
*spa
)
1766 vdev_t
*rvd
= spa
->spa_root_vdev
;
1767 boolean_t slog_found
= B_FALSE
;
1770 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1772 if (!spa_has_slogs(spa
))
1775 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1776 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1777 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1779 if (tvd
->vdev_islog
) {
1780 metaslab_group_passivate(mg
);
1781 slog_found
= B_TRUE
;
1785 return (slog_found
);
1789 spa_activate_log(spa_t
*spa
)
1791 vdev_t
*rvd
= spa
->spa_root_vdev
;
1794 ASSERT(spa_config_held(spa
, SCL_ALLOC
, RW_WRITER
));
1796 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
1797 vdev_t
*tvd
= rvd
->vdev_child
[c
];
1798 metaslab_group_t
*mg
= tvd
->vdev_mg
;
1800 if (tvd
->vdev_islog
)
1801 metaslab_group_activate(mg
);
1806 spa_offline_log(spa_t
*spa
)
1810 error
= dmu_objset_find(spa_name(spa
), zil_vdev_offline
,
1811 NULL
, DS_FIND_CHILDREN
);
1814 * We successfully offlined the log device, sync out the
1815 * current txg so that the "stubby" block can be removed
1818 txg_wait_synced(spa
->spa_dsl_pool
, 0);
1824 spa_aux_check_removed(spa_aux_vdev_t
*sav
)
1828 for (i
= 0; i
< sav
->sav_count
; i
++)
1829 spa_check_removed(sav
->sav_vdevs
[i
]);
1833 spa_claim_notify(zio_t
*zio
)
1835 spa_t
*spa
= zio
->io_spa
;
1840 mutex_enter(&spa
->spa_props_lock
); /* any mutex will do */
1841 if (spa
->spa_claim_max_txg
< zio
->io_bp
->blk_birth
)
1842 spa
->spa_claim_max_txg
= zio
->io_bp
->blk_birth
;
1843 mutex_exit(&spa
->spa_props_lock
);
1846 typedef struct spa_load_error
{
1847 uint64_t sle_meta_count
;
1848 uint64_t sle_data_count
;
1852 spa_load_verify_done(zio_t
*zio
)
1854 blkptr_t
*bp
= zio
->io_bp
;
1855 spa_load_error_t
*sle
= zio
->io_private
;
1856 dmu_object_type_t type
= BP_GET_TYPE(bp
);
1857 int error
= zio
->io_error
;
1858 spa_t
*spa
= zio
->io_spa
;
1861 if ((BP_GET_LEVEL(bp
) != 0 || DMU_OT_IS_METADATA(type
)) &&
1862 type
!= DMU_OT_INTENT_LOG
)
1863 atomic_add_64(&sle
->sle_meta_count
, 1);
1865 atomic_add_64(&sle
->sle_data_count
, 1);
1867 zio_data_buf_free(zio
->io_data
, zio
->io_size
);
1869 mutex_enter(&spa
->spa_scrub_lock
);
1870 spa
->spa_scrub_inflight
--;
1871 cv_broadcast(&spa
->spa_scrub_io_cv
);
1872 mutex_exit(&spa
->spa_scrub_lock
);
1876 * Maximum number of concurrent scrub i/os to create while verifying
1877 * a pool while importing it.
1879 int spa_load_verify_maxinflight
= 10000;
1880 int spa_load_verify_metadata
= B_TRUE
;
1881 int spa_load_verify_data
= B_TRUE
;
1885 spa_load_verify_cb(spa_t
*spa
, zilog_t
*zilog
, const blkptr_t
*bp
,
1886 const zbookmark_phys_t
*zb
, const dnode_phys_t
*dnp
, void *arg
)
1892 if (BP_IS_HOLE(bp
) || BP_IS_EMBEDDED(bp
))
1895 * Note: normally this routine will not be called if
1896 * spa_load_verify_metadata is not set. However, it may be useful
1897 * to manually set the flag after the traversal has begun.
1899 if (!spa_load_verify_metadata
)
1901 if (BP_GET_BUFC_TYPE(bp
) == ARC_BUFC_DATA
&& !spa_load_verify_data
)
1905 size
= BP_GET_PSIZE(bp
);
1906 data
= zio_data_buf_alloc(size
);
1908 mutex_enter(&spa
->spa_scrub_lock
);
1909 while (spa
->spa_scrub_inflight
>= spa_load_verify_maxinflight
)
1910 cv_wait(&spa
->spa_scrub_io_cv
, &spa
->spa_scrub_lock
);
1911 spa
->spa_scrub_inflight
++;
1912 mutex_exit(&spa
->spa_scrub_lock
);
1914 zio_nowait(zio_read(rio
, spa
, bp
, data
, size
,
1915 spa_load_verify_done
, rio
->io_private
, ZIO_PRIORITY_SCRUB
,
1916 ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_CANFAIL
|
1917 ZIO_FLAG_SCRUB
| ZIO_FLAG_RAW
, zb
));
1922 spa_load_verify(spa_t
*spa
)
1925 spa_load_error_t sle
= { 0 };
1926 zpool_rewind_policy_t policy
;
1927 boolean_t verify_ok
= B_FALSE
;
1930 zpool_get_rewind_policy(spa
->spa_config
, &policy
);
1932 if (policy
.zrp_request
& ZPOOL_NEVER_REWIND
)
1935 rio
= zio_root(spa
, NULL
, &sle
,
1936 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
);
1938 if (spa_load_verify_metadata
) {
1939 error
= traverse_pool(spa
, spa
->spa_verify_min_txg
,
1940 TRAVERSE_PRE
| TRAVERSE_PREFETCH_METADATA
,
1941 spa_load_verify_cb
, rio
);
1944 (void) zio_wait(rio
);
1946 spa
->spa_load_meta_errors
= sle
.sle_meta_count
;
1947 spa
->spa_load_data_errors
= sle
.sle_data_count
;
1949 if (!error
&& sle
.sle_meta_count
<= policy
.zrp_maxmeta
&&
1950 sle
.sle_data_count
<= policy
.zrp_maxdata
) {
1954 spa
->spa_load_txg
= spa
->spa_uberblock
.ub_txg
;
1955 spa
->spa_load_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
1957 loss
= spa
->spa_last_ubsync_txg_ts
- spa
->spa_load_txg_ts
;
1958 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
1959 ZPOOL_CONFIG_LOAD_TIME
, spa
->spa_load_txg_ts
) == 0);
1960 VERIFY(nvlist_add_int64(spa
->spa_load_info
,
1961 ZPOOL_CONFIG_REWIND_TIME
, loss
) == 0);
1962 VERIFY(nvlist_add_uint64(spa
->spa_load_info
,
1963 ZPOOL_CONFIG_LOAD_DATA_ERRORS
, sle
.sle_data_count
) == 0);
1965 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
;
1969 if (error
!= ENXIO
&& error
!= EIO
)
1970 error
= SET_ERROR(EIO
);
1974 return (verify_ok
? 0 : EIO
);
1978 * Find a value in the pool props object.
1981 spa_prop_find(spa_t
*spa
, zpool_prop_t prop
, uint64_t *val
)
1983 (void) zap_lookup(spa
->spa_meta_objset
, spa
->spa_pool_props_object
,
1984 zpool_prop_to_name(prop
), sizeof (uint64_t), 1, val
);
1988 * Find a value in the pool directory object.
1991 spa_dir_prop(spa_t
*spa
, const char *name
, uint64_t *val
)
1993 return (zap_lookup(spa
->spa_meta_objset
, DMU_POOL_DIRECTORY_OBJECT
,
1994 name
, sizeof (uint64_t), 1, val
));
1998 spa_vdev_err(vdev_t
*vdev
, vdev_aux_t aux
, int err
)
2000 vdev_set_state(vdev
, B_TRUE
, VDEV_STATE_CANT_OPEN
, aux
);
2005 * Fix up config after a partly-completed split. This is done with the
2006 * ZPOOL_CONFIG_SPLIT nvlist. Both the splitting pool and the split-off
2007 * pool have that entry in their config, but only the splitting one contains
2008 * a list of all the guids of the vdevs that are being split off.
2010 * This function determines what to do with that list: either rejoin
2011 * all the disks to the pool, or complete the splitting process. To attempt
2012 * the rejoin, each disk that is offlined is marked online again, and
2013 * we do a reopen() call. If the vdev label for every disk that was
2014 * marked online indicates it was successfully split off (VDEV_AUX_SPLIT_POOL)
2015 * then we call vdev_split() on each disk, and complete the split.
2017 * Otherwise we leave the config alone, with all the vdevs in place in
2018 * the original pool.
2021 spa_try_repair(spa_t
*spa
, nvlist_t
*config
)
2028 boolean_t attempt_reopen
;
2030 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
, &nvl
) != 0)
2033 /* check that the config is complete */
2034 if (nvlist_lookup_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
2035 &glist
, &gcount
) != 0)
2038 vd
= kmem_zalloc(gcount
* sizeof (vdev_t
*), KM_PUSHPAGE
);
2040 /* attempt to online all the vdevs & validate */
2041 attempt_reopen
= B_TRUE
;
2042 for (i
= 0; i
< gcount
; i
++) {
2043 if (glist
[i
] == 0) /* vdev is hole */
2046 vd
[i
] = spa_lookup_by_guid(spa
, glist
[i
], B_FALSE
);
2047 if (vd
[i
] == NULL
) {
2049 * Don't bother attempting to reopen the disks;
2050 * just do the split.
2052 attempt_reopen
= B_FALSE
;
2054 /* attempt to re-online it */
2055 vd
[i
]->vdev_offline
= B_FALSE
;
2059 if (attempt_reopen
) {
2060 vdev_reopen(spa
->spa_root_vdev
);
2062 /* check each device to see what state it's in */
2063 for (extracted
= 0, i
= 0; i
< gcount
; i
++) {
2064 if (vd
[i
] != NULL
&&
2065 vd
[i
]->vdev_stat
.vs_aux
!= VDEV_AUX_SPLIT_POOL
)
2072 * If every disk has been moved to the new pool, or if we never
2073 * even attempted to look at them, then we split them off for
2076 if (!attempt_reopen
|| gcount
== extracted
) {
2077 for (i
= 0; i
< gcount
; i
++)
2080 vdev_reopen(spa
->spa_root_vdev
);
2083 kmem_free(vd
, gcount
* sizeof (vdev_t
*));
2087 spa_load(spa_t
*spa
, spa_load_state_t state
, spa_import_type_t type
,
2088 boolean_t mosconfig
)
2090 nvlist_t
*config
= spa
->spa_config
;
2091 char *ereport
= FM_EREPORT_ZFS_POOL
;
2097 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
, &pool_guid
))
2098 return (SET_ERROR(EINVAL
));
2100 ASSERT(spa
->spa_comment
== NULL
);
2101 if (nvlist_lookup_string(config
, ZPOOL_CONFIG_COMMENT
, &comment
) == 0)
2102 spa
->spa_comment
= spa_strdup(comment
);
2105 * Versioning wasn't explicitly added to the label until later, so if
2106 * it's not present treat it as the initial version.
2108 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VERSION
,
2109 &spa
->spa_ubsync
.ub_version
) != 0)
2110 spa
->spa_ubsync
.ub_version
= SPA_VERSION_INITIAL
;
2112 (void) nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
2113 &spa
->spa_config_txg
);
2115 if ((state
== SPA_LOAD_IMPORT
|| state
== SPA_LOAD_TRYIMPORT
) &&
2116 spa_guid_exists(pool_guid
, 0)) {
2117 error
= SET_ERROR(EEXIST
);
2119 spa
->spa_config_guid
= pool_guid
;
2121 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_SPLIT
,
2123 VERIFY(nvlist_dup(nvl
, &spa
->spa_config_splitting
,
2127 nvlist_free(spa
->spa_load_info
);
2128 spa
->spa_load_info
= fnvlist_alloc();
2130 gethrestime(&spa
->spa_loaded_ts
);
2131 error
= spa_load_impl(spa
, pool_guid
, config
, state
, type
,
2132 mosconfig
, &ereport
);
2135 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
2137 if (error
!= EEXIST
) {
2138 spa
->spa_loaded_ts
.tv_sec
= 0;
2139 spa
->spa_loaded_ts
.tv_nsec
= 0;
2141 if (error
!= EBADF
) {
2142 zfs_ereport_post(ereport
, spa
, NULL
, NULL
, 0, 0);
2145 spa
->spa_load_state
= error
? SPA_LOAD_ERROR
: SPA_LOAD_NONE
;
2152 * Load an existing storage pool, using the pool's builtin spa_config as a
2153 * source of configuration information.
2155 __attribute__((always_inline
))
2157 spa_load_impl(spa_t
*spa
, uint64_t pool_guid
, nvlist_t
*config
,
2158 spa_load_state_t state
, spa_import_type_t type
, boolean_t mosconfig
,
2162 nvlist_t
*nvroot
= NULL
;
2165 uberblock_t
*ub
= &spa
->spa_uberblock
;
2166 uint64_t children
, config_cache_txg
= spa
->spa_config_txg
;
2167 int orig_mode
= spa
->spa_mode
;
2170 boolean_t missing_feat_write
= B_FALSE
;
2173 * If this is an untrusted config, access the pool in read-only mode.
2174 * This prevents things like resilvering recently removed devices.
2177 spa
->spa_mode
= FREAD
;
2179 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
2181 spa
->spa_load_state
= state
;
2183 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvroot
))
2184 return (SET_ERROR(EINVAL
));
2186 parse
= (type
== SPA_IMPORT_EXISTING
?
2187 VDEV_ALLOC_LOAD
: VDEV_ALLOC_SPLIT
);
2190 * Create "The Godfather" zio to hold all async IOs
2192 spa
->spa_async_zio_root
= zio_root(spa
, NULL
, NULL
,
2193 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_GODFATHER
);
2196 * Parse the configuration into a vdev tree. We explicitly set the
2197 * value that will be returned by spa_version() since parsing the
2198 * configuration requires knowing the version number.
2200 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2201 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, parse
);
2202 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2207 ASSERT(spa
->spa_root_vdev
== rvd
);
2209 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2210 ASSERT(spa_guid(spa
) == pool_guid
);
2214 * Try to open all vdevs, loading each label in the process.
2216 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2217 error
= vdev_open(rvd
);
2218 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2223 * We need to validate the vdev labels against the configuration that
2224 * we have in hand, which is dependent on the setting of mosconfig. If
2225 * mosconfig is true then we're validating the vdev labels based on
2226 * that config. Otherwise, we're validating against the cached config
2227 * (zpool.cache) that was read when we loaded the zfs module, and then
2228 * later we will recursively call spa_load() and validate against
2231 * If we're assembling a new pool that's been split off from an
2232 * existing pool, the labels haven't yet been updated so we skip
2233 * validation for now.
2235 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2236 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2237 error
= vdev_validate(rvd
, mosconfig
);
2238 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2243 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2244 return (SET_ERROR(ENXIO
));
2248 * Find the best uberblock.
2250 vdev_uberblock_load(rvd
, ub
, &label
);
2253 * If we weren't able to find a single valid uberblock, return failure.
2255 if (ub
->ub_txg
== 0) {
2257 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, ENXIO
));
2261 * If the pool has an unsupported version we can't open it.
2263 if (!SPA_VERSION_IS_SUPPORTED(ub
->ub_version
)) {
2265 return (spa_vdev_err(rvd
, VDEV_AUX_VERSION_NEWER
, ENOTSUP
));
2268 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2272 * If we weren't able to find what's necessary for reading the
2273 * MOS in the label, return failure.
2275 if (label
== NULL
|| nvlist_lookup_nvlist(label
,
2276 ZPOOL_CONFIG_FEATURES_FOR_READ
, &features
) != 0) {
2278 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2283 * Update our in-core representation with the definitive values
2286 nvlist_free(spa
->spa_label_features
);
2287 VERIFY(nvlist_dup(features
, &spa
->spa_label_features
, 0) == 0);
2293 * Look through entries in the label nvlist's features_for_read. If
2294 * there is a feature listed there which we don't understand then we
2295 * cannot open a pool.
2297 if (ub
->ub_version
>= SPA_VERSION_FEATURES
) {
2298 nvlist_t
*unsup_feat
;
2301 VERIFY(nvlist_alloc(&unsup_feat
, NV_UNIQUE_NAME
, KM_SLEEP
) ==
2304 for (nvp
= nvlist_next_nvpair(spa
->spa_label_features
, NULL
);
2306 nvp
= nvlist_next_nvpair(spa
->spa_label_features
, nvp
)) {
2307 if (!zfeature_is_supported(nvpair_name(nvp
))) {
2308 VERIFY(nvlist_add_string(unsup_feat
,
2309 nvpair_name(nvp
), "") == 0);
2313 if (!nvlist_empty(unsup_feat
)) {
2314 VERIFY(nvlist_add_nvlist(spa
->spa_load_info
,
2315 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
) == 0);
2316 nvlist_free(unsup_feat
);
2317 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2321 nvlist_free(unsup_feat
);
2325 * If the vdev guid sum doesn't match the uberblock, we have an
2326 * incomplete configuration. We first check to see if the pool
2327 * is aware of the complete config (i.e ZPOOL_CONFIG_VDEV_CHILDREN).
2328 * If it is, defer the vdev_guid_sum check till later so we
2329 * can handle missing vdevs.
2331 if (nvlist_lookup_uint64(config
, ZPOOL_CONFIG_VDEV_CHILDREN
,
2332 &children
) != 0 && mosconfig
&& type
!= SPA_IMPORT_ASSEMBLE
&&
2333 rvd
->vdev_guid_sum
!= ub
->ub_guid_sum
)
2334 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
, ENXIO
));
2336 if (type
!= SPA_IMPORT_ASSEMBLE
&& spa
->spa_config_splitting
) {
2337 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2338 spa_try_repair(spa
, config
);
2339 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2340 nvlist_free(spa
->spa_config_splitting
);
2341 spa
->spa_config_splitting
= NULL
;
2345 * Initialize internal SPA structures.
2347 spa
->spa_state
= POOL_STATE_ACTIVE
;
2348 spa
->spa_ubsync
= spa
->spa_uberblock
;
2349 spa
->spa_verify_min_txg
= spa
->spa_extreme_rewind
?
2350 TXG_INITIAL
- 1 : spa_last_synced_txg(spa
) - TXG_DEFER_SIZE
- 1;
2351 spa
->spa_first_txg
= spa
->spa_last_ubsync_txg
?
2352 spa
->spa_last_ubsync_txg
: spa_last_synced_txg(spa
) + 1;
2353 spa
->spa_claim_max_txg
= spa
->spa_first_txg
;
2354 spa
->spa_prev_software_version
= ub
->ub_software_version
;
2356 error
= dsl_pool_init(spa
, spa
->spa_first_txg
, &spa
->spa_dsl_pool
);
2358 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2359 spa
->spa_meta_objset
= spa
->spa_dsl_pool
->dp_meta_objset
;
2361 if (spa_dir_prop(spa
, DMU_POOL_CONFIG
, &spa
->spa_config_object
) != 0)
2362 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2364 if (spa_version(spa
) >= SPA_VERSION_FEATURES
) {
2365 boolean_t missing_feat_read
= B_FALSE
;
2366 nvlist_t
*unsup_feat
, *enabled_feat
;
2369 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_READ
,
2370 &spa
->spa_feat_for_read_obj
) != 0) {
2371 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2374 if (spa_dir_prop(spa
, DMU_POOL_FEATURES_FOR_WRITE
,
2375 &spa
->spa_feat_for_write_obj
) != 0) {
2376 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2379 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_DESCRIPTIONS
,
2380 &spa
->spa_feat_desc_obj
) != 0) {
2381 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2384 enabled_feat
= fnvlist_alloc();
2385 unsup_feat
= fnvlist_alloc();
2387 if (!spa_features_check(spa
, B_FALSE
,
2388 unsup_feat
, enabled_feat
))
2389 missing_feat_read
= B_TRUE
;
2391 if (spa_writeable(spa
) || state
== SPA_LOAD_TRYIMPORT
) {
2392 if (!spa_features_check(spa
, B_TRUE
,
2393 unsup_feat
, enabled_feat
)) {
2394 missing_feat_write
= B_TRUE
;
2398 fnvlist_add_nvlist(spa
->spa_load_info
,
2399 ZPOOL_CONFIG_ENABLED_FEAT
, enabled_feat
);
2401 if (!nvlist_empty(unsup_feat
)) {
2402 fnvlist_add_nvlist(spa
->spa_load_info
,
2403 ZPOOL_CONFIG_UNSUP_FEAT
, unsup_feat
);
2406 fnvlist_free(enabled_feat
);
2407 fnvlist_free(unsup_feat
);
2409 if (!missing_feat_read
) {
2410 fnvlist_add_boolean(spa
->spa_load_info
,
2411 ZPOOL_CONFIG_CAN_RDONLY
);
2415 * If the state is SPA_LOAD_TRYIMPORT, our objective is
2416 * twofold: to determine whether the pool is available for
2417 * import in read-write mode and (if it is not) whether the
2418 * pool is available for import in read-only mode. If the pool
2419 * is available for import in read-write mode, it is displayed
2420 * as available in userland; if it is not available for import
2421 * in read-only mode, it is displayed as unavailable in
2422 * userland. If the pool is available for import in read-only
2423 * mode but not read-write mode, it is displayed as unavailable
2424 * in userland with a special note that the pool is actually
2425 * available for open in read-only mode.
2427 * As a result, if the state is SPA_LOAD_TRYIMPORT and we are
2428 * missing a feature for write, we must first determine whether
2429 * the pool can be opened read-only before returning to
2430 * userland in order to know whether to display the
2431 * abovementioned note.
2433 if (missing_feat_read
|| (missing_feat_write
&&
2434 spa_writeable(spa
))) {
2435 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
,
2440 * Load refcounts for ZFS features from disk into an in-memory
2441 * cache during SPA initialization.
2443 for (i
= 0; i
< SPA_FEATURES
; i
++) {
2446 error
= feature_get_refcount_from_disk(spa
,
2447 &spa_feature_table
[i
], &refcount
);
2449 spa
->spa_feat_refcount_cache
[i
] = refcount
;
2450 } else if (error
== ENOTSUP
) {
2451 spa
->spa_feat_refcount_cache
[i
] =
2452 SPA_FEATURE_DISABLED
;
2454 return (spa_vdev_err(rvd
,
2455 VDEV_AUX_CORRUPT_DATA
, EIO
));
2460 if (spa_feature_is_active(spa
, SPA_FEATURE_ENABLED_TXG
)) {
2461 if (spa_dir_prop(spa
, DMU_POOL_FEATURE_ENABLED_TXG
,
2462 &spa
->spa_feat_enabled_txg_obj
) != 0)
2463 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2466 spa
->spa_is_initializing
= B_TRUE
;
2467 error
= dsl_pool_open(spa
->spa_dsl_pool
);
2468 spa
->spa_is_initializing
= B_FALSE
;
2470 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2474 nvlist_t
*policy
= NULL
, *nvconfig
;
2476 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2477 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2479 if (!spa_is_root(spa
) && nvlist_lookup_uint64(nvconfig
,
2480 ZPOOL_CONFIG_HOSTID
, &hostid
) == 0) {
2482 unsigned long myhostid
= 0;
2484 VERIFY(nvlist_lookup_string(nvconfig
,
2485 ZPOOL_CONFIG_HOSTNAME
, &hostname
) == 0);
2488 myhostid
= zone_get_hostid(NULL
);
2491 * We're emulating the system's hostid in userland, so
2492 * we can't use zone_get_hostid().
2494 (void) ddi_strtoul(hw_serial
, NULL
, 10, &myhostid
);
2495 #endif /* _KERNEL */
2496 if (hostid
!= 0 && myhostid
!= 0 &&
2497 hostid
!= myhostid
) {
2498 nvlist_free(nvconfig
);
2499 cmn_err(CE_WARN
, "pool '%s' could not be "
2500 "loaded as it was last accessed by another "
2501 "system (host: %s hostid: 0x%lx). See: "
2502 "http://zfsonlinux.org/msg/ZFS-8000-EY",
2503 spa_name(spa
), hostname
,
2504 (unsigned long)hostid
);
2505 return (SET_ERROR(EBADF
));
2508 if (nvlist_lookup_nvlist(spa
->spa_config
,
2509 ZPOOL_REWIND_POLICY
, &policy
) == 0)
2510 VERIFY(nvlist_add_nvlist(nvconfig
,
2511 ZPOOL_REWIND_POLICY
, policy
) == 0);
2513 spa_config_set(spa
, nvconfig
);
2515 spa_deactivate(spa
);
2516 spa_activate(spa
, orig_mode
);
2518 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, B_TRUE
));
2521 if (spa_dir_prop(spa
, DMU_POOL_SYNC_BPOBJ
, &obj
) != 0)
2522 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2523 error
= bpobj_open(&spa
->spa_deferred_bpobj
, spa
->spa_meta_objset
, obj
);
2525 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2528 * Load the bit that tells us to use the new accounting function
2529 * (raid-z deflation). If we have an older pool, this will not
2532 error
= spa_dir_prop(spa
, DMU_POOL_DEFLATE
, &spa
->spa_deflate
);
2533 if (error
!= 0 && error
!= ENOENT
)
2534 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2536 error
= spa_dir_prop(spa
, DMU_POOL_CREATION_VERSION
,
2537 &spa
->spa_creation_version
);
2538 if (error
!= 0 && error
!= ENOENT
)
2539 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2542 * Load the persistent error log. If we have an older pool, this will
2545 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_LAST
, &spa
->spa_errlog_last
);
2546 if (error
!= 0 && error
!= ENOENT
)
2547 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2549 error
= spa_dir_prop(spa
, DMU_POOL_ERRLOG_SCRUB
,
2550 &spa
->spa_errlog_scrub
);
2551 if (error
!= 0 && error
!= ENOENT
)
2552 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2555 * Load the history object. If we have an older pool, this
2556 * will not be present.
2558 error
= spa_dir_prop(spa
, DMU_POOL_HISTORY
, &spa
->spa_history
);
2559 if (error
!= 0 && error
!= ENOENT
)
2560 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2563 * If we're assembling the pool from the split-off vdevs of
2564 * an existing pool, we don't want to attach the spares & cache
2569 * Load any hot spares for this pool.
2571 error
= spa_dir_prop(spa
, DMU_POOL_SPARES
, &spa
->spa_spares
.sav_object
);
2572 if (error
!= 0 && error
!= ENOENT
)
2573 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2574 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2575 ASSERT(spa_version(spa
) >= SPA_VERSION_SPARES
);
2576 if (load_nvlist(spa
, spa
->spa_spares
.sav_object
,
2577 &spa
->spa_spares
.sav_config
) != 0)
2578 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2580 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2581 spa_load_spares(spa
);
2582 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2583 } else if (error
== 0) {
2584 spa
->spa_spares
.sav_sync
= B_TRUE
;
2588 * Load any level 2 ARC devices for this pool.
2590 error
= spa_dir_prop(spa
, DMU_POOL_L2CACHE
,
2591 &spa
->spa_l2cache
.sav_object
);
2592 if (error
!= 0 && error
!= ENOENT
)
2593 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2594 if (error
== 0 && type
!= SPA_IMPORT_ASSEMBLE
) {
2595 ASSERT(spa_version(spa
) >= SPA_VERSION_L2CACHE
);
2596 if (load_nvlist(spa
, spa
->spa_l2cache
.sav_object
,
2597 &spa
->spa_l2cache
.sav_config
) != 0)
2598 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2600 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2601 spa_load_l2cache(spa
);
2602 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2603 } else if (error
== 0) {
2604 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
2607 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
2609 error
= spa_dir_prop(spa
, DMU_POOL_PROPS
, &spa
->spa_pool_props_object
);
2610 if (error
&& error
!= ENOENT
)
2611 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2614 uint64_t autoreplace
= 0;
2616 spa_prop_find(spa
, ZPOOL_PROP_BOOTFS
, &spa
->spa_bootfs
);
2617 spa_prop_find(spa
, ZPOOL_PROP_AUTOREPLACE
, &autoreplace
);
2618 spa_prop_find(spa
, ZPOOL_PROP_DELEGATION
, &spa
->spa_delegation
);
2619 spa_prop_find(spa
, ZPOOL_PROP_FAILUREMODE
, &spa
->spa_failmode
);
2620 spa_prop_find(spa
, ZPOOL_PROP_AUTOEXPAND
, &spa
->spa_autoexpand
);
2621 spa_prop_find(spa
, ZPOOL_PROP_DEDUPDITTO
,
2622 &spa
->spa_dedup_ditto
);
2624 spa
->spa_autoreplace
= (autoreplace
!= 0);
2628 * If the 'autoreplace' property is set, then post a resource notifying
2629 * the ZFS DE that it should not issue any faults for unopenable
2630 * devices. We also iterate over the vdevs, and post a sysevent for any
2631 * unopenable vdevs so that the normal autoreplace handler can take
2634 if (spa
->spa_autoreplace
&& state
!= SPA_LOAD_TRYIMPORT
) {
2635 spa_check_removed(spa
->spa_root_vdev
);
2637 * For the import case, this is done in spa_import(), because
2638 * at this point we're using the spare definitions from
2639 * the MOS config, not necessarily from the userland config.
2641 if (state
!= SPA_LOAD_IMPORT
) {
2642 spa_aux_check_removed(&spa
->spa_spares
);
2643 spa_aux_check_removed(&spa
->spa_l2cache
);
2648 * Load the vdev state for all toplevel vdevs.
2653 * Propagate the leaf DTLs we just loaded all the way up the tree.
2655 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
2656 vdev_dtl_reassess(rvd
, 0, 0, B_FALSE
);
2657 spa_config_exit(spa
, SCL_ALL
, FTAG
);
2660 * Load the DDTs (dedup tables).
2662 error
= ddt_load(spa
);
2664 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2666 spa_update_dspace(spa
);
2669 * Validate the config, using the MOS config to fill in any
2670 * information which might be missing. If we fail to validate
2671 * the config then declare the pool unfit for use. If we're
2672 * assembling a pool from a split, the log is not transferred
2675 if (type
!= SPA_IMPORT_ASSEMBLE
) {
2678 if (load_nvlist(spa
, spa
->spa_config_object
, &nvconfig
) != 0)
2679 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
, EIO
));
2681 if (!spa_config_valid(spa
, nvconfig
)) {
2682 nvlist_free(nvconfig
);
2683 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_GUID_SUM
,
2686 nvlist_free(nvconfig
);
2689 * Now that we've validated the config, check the state of the
2690 * root vdev. If it can't be opened, it indicates one or
2691 * more toplevel vdevs are faulted.
2693 if (rvd
->vdev_state
<= VDEV_STATE_CANT_OPEN
)
2694 return (SET_ERROR(ENXIO
));
2696 if (spa_check_logs(spa
)) {
2697 *ereport
= FM_EREPORT_ZFS_LOG_REPLAY
;
2698 return (spa_vdev_err(rvd
, VDEV_AUX_BAD_LOG
, ENXIO
));
2702 if (missing_feat_write
) {
2703 ASSERT(state
== SPA_LOAD_TRYIMPORT
);
2706 * At this point, we know that we can open the pool in
2707 * read-only mode but not read-write mode. We now have enough
2708 * information and can return to userland.
2710 return (spa_vdev_err(rvd
, VDEV_AUX_UNSUP_FEAT
, ENOTSUP
));
2714 * We've successfully opened the pool, verify that we're ready
2715 * to start pushing transactions.
2717 if (state
!= SPA_LOAD_TRYIMPORT
) {
2718 if ((error
= spa_load_verify(spa
)))
2719 return (spa_vdev_err(rvd
, VDEV_AUX_CORRUPT_DATA
,
2723 if (spa_writeable(spa
) && (state
== SPA_LOAD_RECOVER
||
2724 spa
->spa_load_max_txg
== UINT64_MAX
)) {
2726 int need_update
= B_FALSE
;
2729 ASSERT(state
!= SPA_LOAD_TRYIMPORT
);
2732 * Claim log blocks that haven't been committed yet.
2733 * This must all happen in a single txg.
2734 * Note: spa_claim_max_txg is updated by spa_claim_notify(),
2735 * invoked from zil_claim_log_block()'s i/o done callback.
2736 * Price of rollback is that we abandon the log.
2738 spa
->spa_claiming
= B_TRUE
;
2740 tx
= dmu_tx_create_assigned(spa_get_dsl(spa
),
2741 spa_first_txg(spa
));
2742 (void) dmu_objset_find(spa_name(spa
),
2743 zil_claim
, tx
, DS_FIND_CHILDREN
);
2746 spa
->spa_claiming
= B_FALSE
;
2748 spa_set_log_state(spa
, SPA_LOG_GOOD
);
2749 spa
->spa_sync_on
= B_TRUE
;
2750 txg_sync_start(spa
->spa_dsl_pool
);
2753 * Wait for all claims to sync. We sync up to the highest
2754 * claimed log block birth time so that claimed log blocks
2755 * don't appear to be from the future. spa_claim_max_txg
2756 * will have been set for us by either zil_check_log_chain()
2757 * (invoked from spa_check_logs()) or zil_claim() above.
2759 txg_wait_synced(spa
->spa_dsl_pool
, spa
->spa_claim_max_txg
);
2762 * If the config cache is stale, or we have uninitialized
2763 * metaslabs (see spa_vdev_add()), then update the config.
2765 * If this is a verbatim import, trust the current
2766 * in-core spa_config and update the disk labels.
2768 if (config_cache_txg
!= spa
->spa_config_txg
||
2769 state
== SPA_LOAD_IMPORT
||
2770 state
== SPA_LOAD_RECOVER
||
2771 (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
))
2772 need_update
= B_TRUE
;
2774 for (c
= 0; c
< rvd
->vdev_children
; c
++)
2775 if (rvd
->vdev_child
[c
]->vdev_ms_array
== 0)
2776 need_update
= B_TRUE
;
2779 * Update the config cache asychronously in case we're the
2780 * root pool, in which case the config cache isn't writable yet.
2783 spa_async_request(spa
, SPA_ASYNC_CONFIG_UPDATE
);
2786 * Check all DTLs to see if anything needs resilvering.
2788 if (!dsl_scan_resilvering(spa
->spa_dsl_pool
) &&
2789 vdev_resilver_needed(rvd
, NULL
, NULL
))
2790 spa_async_request(spa
, SPA_ASYNC_RESILVER
);
2793 * Log the fact that we booted up (so that we can detect if
2794 * we rebooted in the middle of an operation).
2796 spa_history_log_version(spa
, "open");
2799 * Delete any inconsistent datasets.
2801 (void) dmu_objset_find(spa_name(spa
),
2802 dsl_destroy_inconsistent
, NULL
, DS_FIND_CHILDREN
);
2805 * Clean up any stale temporary dataset userrefs.
2807 dsl_pool_clean_tmp_userrefs(spa
->spa_dsl_pool
);
2814 spa_load_retry(spa_t
*spa
, spa_load_state_t state
, int mosconfig
)
2816 int mode
= spa
->spa_mode
;
2819 spa_deactivate(spa
);
2821 spa
->spa_load_max_txg
= spa
->spa_uberblock
.ub_txg
- 1;
2823 spa_activate(spa
, mode
);
2824 spa_async_suspend(spa
);
2826 return (spa_load(spa
, state
, SPA_IMPORT_EXISTING
, mosconfig
));
2830 * If spa_load() fails this function will try loading prior txg's. If
2831 * 'state' is SPA_LOAD_RECOVER and one of these loads succeeds the pool
2832 * will be rewound to that txg. If 'state' is not SPA_LOAD_RECOVER this
2833 * function will not rewind the pool and will return the same error as
2837 spa_load_best(spa_t
*spa
, spa_load_state_t state
, int mosconfig
,
2838 uint64_t max_request
, int rewind_flags
)
2840 nvlist_t
*loadinfo
= NULL
;
2841 nvlist_t
*config
= NULL
;
2842 int load_error
, rewind_error
;
2843 uint64_t safe_rewind_txg
;
2846 if (spa
->spa_load_txg
&& state
== SPA_LOAD_RECOVER
) {
2847 spa
->spa_load_max_txg
= spa
->spa_load_txg
;
2848 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2850 spa
->spa_load_max_txg
= max_request
;
2851 if (max_request
!= UINT64_MAX
)
2852 spa
->spa_extreme_rewind
= B_TRUE
;
2855 load_error
= rewind_error
= spa_load(spa
, state
, SPA_IMPORT_EXISTING
,
2857 if (load_error
== 0)
2860 if (spa
->spa_root_vdev
!= NULL
)
2861 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
2863 spa
->spa_last_ubsync_txg
= spa
->spa_uberblock
.ub_txg
;
2864 spa
->spa_last_ubsync_txg_ts
= spa
->spa_uberblock
.ub_timestamp
;
2866 if (rewind_flags
& ZPOOL_NEVER_REWIND
) {
2867 nvlist_free(config
);
2868 return (load_error
);
2871 if (state
== SPA_LOAD_RECOVER
) {
2872 /* Price of rolling back is discarding txgs, including log */
2873 spa_set_log_state(spa
, SPA_LOG_CLEAR
);
2876 * If we aren't rolling back save the load info from our first
2877 * import attempt so that we can restore it after attempting
2880 loadinfo
= spa
->spa_load_info
;
2881 spa
->spa_load_info
= fnvlist_alloc();
2884 spa
->spa_load_max_txg
= spa
->spa_last_ubsync_txg
;
2885 safe_rewind_txg
= spa
->spa_last_ubsync_txg
- TXG_DEFER_SIZE
;
2886 min_txg
= (rewind_flags
& ZPOOL_EXTREME_REWIND
) ?
2887 TXG_INITIAL
: safe_rewind_txg
;
2890 * Continue as long as we're finding errors, we're still within
2891 * the acceptable rewind range, and we're still finding uberblocks
2893 while (rewind_error
&& spa
->spa_uberblock
.ub_txg
>= min_txg
&&
2894 spa
->spa_uberblock
.ub_txg
<= spa
->spa_load_max_txg
) {
2895 if (spa
->spa_load_max_txg
< safe_rewind_txg
)
2896 spa
->spa_extreme_rewind
= B_TRUE
;
2897 rewind_error
= spa_load_retry(spa
, state
, mosconfig
);
2900 spa
->spa_extreme_rewind
= B_FALSE
;
2901 spa
->spa_load_max_txg
= UINT64_MAX
;
2903 if (config
&& (rewind_error
|| state
!= SPA_LOAD_RECOVER
))
2904 spa_config_set(spa
, config
);
2906 if (state
== SPA_LOAD_RECOVER
) {
2907 ASSERT3P(loadinfo
, ==, NULL
);
2908 return (rewind_error
);
2910 /* Store the rewind info as part of the initial load info */
2911 fnvlist_add_nvlist(loadinfo
, ZPOOL_CONFIG_REWIND_INFO
,
2912 spa
->spa_load_info
);
2914 /* Restore the initial load info */
2915 fnvlist_free(spa
->spa_load_info
);
2916 spa
->spa_load_info
= loadinfo
;
2918 return (load_error
);
2925 * The import case is identical to an open except that the configuration is sent
2926 * down from userland, instead of grabbed from the configuration cache. For the
2927 * case of an open, the pool configuration will exist in the
2928 * POOL_STATE_UNINITIALIZED state.
2930 * The stats information (gen/count/ustats) is used to gather vdev statistics at
2931 * the same time open the pool, without having to keep around the spa_t in some
2935 spa_open_common(const char *pool
, spa_t
**spapp
, void *tag
, nvlist_t
*nvpolicy
,
2939 spa_load_state_t state
= SPA_LOAD_OPEN
;
2941 int locked
= B_FALSE
;
2942 int firstopen
= B_FALSE
;
2947 * As disgusting as this is, we need to support recursive calls to this
2948 * function because dsl_dir_open() is called during spa_load(), and ends
2949 * up calling spa_open() again. The real fix is to figure out how to
2950 * avoid dsl_dir_open() calling this in the first place.
2952 if (mutex_owner(&spa_namespace_lock
) != curthread
) {
2953 mutex_enter(&spa_namespace_lock
);
2957 if ((spa
= spa_lookup(pool
)) == NULL
) {
2959 mutex_exit(&spa_namespace_lock
);
2960 return (SET_ERROR(ENOENT
));
2963 if (spa
->spa_state
== POOL_STATE_UNINITIALIZED
) {
2964 zpool_rewind_policy_t policy
;
2968 zpool_get_rewind_policy(nvpolicy
? nvpolicy
: spa
->spa_config
,
2970 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
2971 state
= SPA_LOAD_RECOVER
;
2973 spa_activate(spa
, spa_mode_global
);
2975 if (state
!= SPA_LOAD_RECOVER
)
2976 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
2978 error
= spa_load_best(spa
, state
, B_FALSE
, policy
.zrp_txg
,
2979 policy
.zrp_request
);
2981 if (error
== EBADF
) {
2983 * If vdev_validate() returns failure (indicated by
2984 * EBADF), it indicates that one of the vdevs indicates
2985 * that the pool has been exported or destroyed. If
2986 * this is the case, the config cache is out of sync and
2987 * we should remove the pool from the namespace.
2990 spa_deactivate(spa
);
2991 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
2994 mutex_exit(&spa_namespace_lock
);
2995 return (SET_ERROR(ENOENT
));
3000 * We can't open the pool, but we still have useful
3001 * information: the state of each vdev after the
3002 * attempted vdev_open(). Return this to the user.
3004 if (config
!= NULL
&& spa
->spa_config
) {
3005 VERIFY(nvlist_dup(spa
->spa_config
, config
,
3007 VERIFY(nvlist_add_nvlist(*config
,
3008 ZPOOL_CONFIG_LOAD_INFO
,
3009 spa
->spa_load_info
) == 0);
3012 spa_deactivate(spa
);
3013 spa
->spa_last_open_failed
= error
;
3015 mutex_exit(&spa_namespace_lock
);
3021 spa_open_ref(spa
, tag
);
3024 *config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
3027 * If we've recovered the pool, pass back any information we
3028 * gathered while doing the load.
3030 if (state
== SPA_LOAD_RECOVER
) {
3031 VERIFY(nvlist_add_nvlist(*config
, ZPOOL_CONFIG_LOAD_INFO
,
3032 spa
->spa_load_info
) == 0);
3036 spa
->spa_last_open_failed
= 0;
3037 spa
->spa_last_ubsync_txg
= 0;
3038 spa
->spa_load_txg
= 0;
3039 mutex_exit(&spa_namespace_lock
);
3044 zvol_create_minors(spa
->spa_name
);
3053 spa_open_rewind(const char *name
, spa_t
**spapp
, void *tag
, nvlist_t
*policy
,
3056 return (spa_open_common(name
, spapp
, tag
, policy
, config
));
3060 spa_open(const char *name
, spa_t
**spapp
, void *tag
)
3062 return (spa_open_common(name
, spapp
, tag
, NULL
, NULL
));
3066 * Lookup the given spa_t, incrementing the inject count in the process,
3067 * preventing it from being exported or destroyed.
3070 spa_inject_addref(char *name
)
3074 mutex_enter(&spa_namespace_lock
);
3075 if ((spa
= spa_lookup(name
)) == NULL
) {
3076 mutex_exit(&spa_namespace_lock
);
3079 spa
->spa_inject_ref
++;
3080 mutex_exit(&spa_namespace_lock
);
3086 spa_inject_delref(spa_t
*spa
)
3088 mutex_enter(&spa_namespace_lock
);
3089 spa
->spa_inject_ref
--;
3090 mutex_exit(&spa_namespace_lock
);
3094 * Add spares device information to the nvlist.
3097 spa_add_spares(spa_t
*spa
, nvlist_t
*config
)
3107 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3109 if (spa
->spa_spares
.sav_count
== 0)
3112 VERIFY(nvlist_lookup_nvlist(config
,
3113 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3114 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
3115 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3117 VERIFY(nvlist_add_nvlist_array(nvroot
,
3118 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3119 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3120 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0);
3123 * Go through and find any spares which have since been
3124 * repurposed as an active spare. If this is the case, update
3125 * their status appropriately.
3127 for (i
= 0; i
< nspares
; i
++) {
3128 VERIFY(nvlist_lookup_uint64(spares
[i
],
3129 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3130 if (spa_spare_exists(guid
, &pool
, NULL
) &&
3132 VERIFY(nvlist_lookup_uint64_array(
3133 spares
[i
], ZPOOL_CONFIG_VDEV_STATS
,
3134 (uint64_t **)&vs
, &vsc
) == 0);
3135 vs
->vs_state
= VDEV_STATE_CANT_OPEN
;
3136 vs
->vs_aux
= VDEV_AUX_SPARED
;
3143 * Add l2cache device information to the nvlist, including vdev stats.
3146 spa_add_l2cache(spa_t
*spa
, nvlist_t
*config
)
3149 uint_t i
, j
, nl2cache
;
3156 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3158 if (spa
->spa_l2cache
.sav_count
== 0)
3161 VERIFY(nvlist_lookup_nvlist(config
,
3162 ZPOOL_CONFIG_VDEV_TREE
, &nvroot
) == 0);
3163 VERIFY(nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
3164 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3165 if (nl2cache
!= 0) {
3166 VERIFY(nvlist_add_nvlist_array(nvroot
,
3167 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3168 VERIFY(nvlist_lookup_nvlist_array(nvroot
,
3169 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0);
3172 * Update level 2 cache device stats.
3175 for (i
= 0; i
< nl2cache
; i
++) {
3176 VERIFY(nvlist_lookup_uint64(l2cache
[i
],
3177 ZPOOL_CONFIG_GUID
, &guid
) == 0);
3180 for (j
= 0; j
< spa
->spa_l2cache
.sav_count
; j
++) {
3182 spa
->spa_l2cache
.sav_vdevs
[j
]->vdev_guid
) {
3183 vd
= spa
->spa_l2cache
.sav_vdevs
[j
];
3189 VERIFY(nvlist_lookup_uint64_array(l2cache
[i
],
3190 ZPOOL_CONFIG_VDEV_STATS
, (uint64_t **)&vs
, &vsc
)
3192 vdev_get_stats(vd
, vs
);
3198 spa_add_feature_stats(spa_t
*spa
, nvlist_t
*config
)
3204 ASSERT(spa_config_held(spa
, SCL_CONFIG
, RW_READER
));
3205 VERIFY(nvlist_alloc(&features
, NV_UNIQUE_NAME
, KM_SLEEP
) == 0);
3207 if (spa
->spa_feat_for_read_obj
!= 0) {
3208 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3209 spa
->spa_feat_for_read_obj
);
3210 zap_cursor_retrieve(&zc
, &za
) == 0;
3211 zap_cursor_advance(&zc
)) {
3212 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3213 za
.za_num_integers
== 1);
3214 VERIFY3U(0, ==, nvlist_add_uint64(features
, za
.za_name
,
3215 za
.za_first_integer
));
3217 zap_cursor_fini(&zc
);
3220 if (spa
->spa_feat_for_write_obj
!= 0) {
3221 for (zap_cursor_init(&zc
, spa
->spa_meta_objset
,
3222 spa
->spa_feat_for_write_obj
);
3223 zap_cursor_retrieve(&zc
, &za
) == 0;
3224 zap_cursor_advance(&zc
)) {
3225 ASSERT(za
.za_integer_length
== sizeof (uint64_t) &&
3226 za
.za_num_integers
== 1);
3227 VERIFY3U(0, ==, nvlist_add_uint64(features
, za
.za_name
,
3228 za
.za_first_integer
));
3230 zap_cursor_fini(&zc
);
3233 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_FEATURE_STATS
,
3235 nvlist_free(features
);
3239 spa_get_stats(const char *name
, nvlist_t
**config
,
3240 char *altroot
, size_t buflen
)
3246 error
= spa_open_common(name
, &spa
, FTAG
, NULL
, config
);
3250 * This still leaves a window of inconsistency where the spares
3251 * or l2cache devices could change and the config would be
3252 * self-inconsistent.
3254 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
3256 if (*config
!= NULL
) {
3257 uint64_t loadtimes
[2];
3259 loadtimes
[0] = spa
->spa_loaded_ts
.tv_sec
;
3260 loadtimes
[1] = spa
->spa_loaded_ts
.tv_nsec
;
3261 VERIFY(nvlist_add_uint64_array(*config
,
3262 ZPOOL_CONFIG_LOADED_TIME
, loadtimes
, 2) == 0);
3264 VERIFY(nvlist_add_uint64(*config
,
3265 ZPOOL_CONFIG_ERRCOUNT
,
3266 spa_get_errlog_size(spa
)) == 0);
3268 if (spa_suspended(spa
))
3269 VERIFY(nvlist_add_uint64(*config
,
3270 ZPOOL_CONFIG_SUSPENDED
,
3271 spa
->spa_failmode
) == 0);
3273 spa_add_spares(spa
, *config
);
3274 spa_add_l2cache(spa
, *config
);
3275 spa_add_feature_stats(spa
, *config
);
3280 * We want to get the alternate root even for faulted pools, so we cheat
3281 * and call spa_lookup() directly.
3285 mutex_enter(&spa_namespace_lock
);
3286 spa
= spa_lookup(name
);
3288 spa_altroot(spa
, altroot
, buflen
);
3292 mutex_exit(&spa_namespace_lock
);
3294 spa_altroot(spa
, altroot
, buflen
);
3299 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
3300 spa_close(spa
, FTAG
);
3307 * Validate that the auxiliary device array is well formed. We must have an
3308 * array of nvlists, each which describes a valid leaf vdev. If this is an
3309 * import (mode is VDEV_ALLOC_SPARE), then we allow corrupted spares to be
3310 * specified, as long as they are well-formed.
3313 spa_validate_aux_devs(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
,
3314 spa_aux_vdev_t
*sav
, const char *config
, uint64_t version
,
3315 vdev_labeltype_t label
)
3322 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3325 * It's acceptable to have no devs specified.
3327 if (nvlist_lookup_nvlist_array(nvroot
, config
, &dev
, &ndev
) != 0)
3331 return (SET_ERROR(EINVAL
));
3334 * Make sure the pool is formatted with a version that supports this
3337 if (spa_version(spa
) < version
)
3338 return (SET_ERROR(ENOTSUP
));
3341 * Set the pending device list so we correctly handle device in-use
3344 sav
->sav_pending
= dev
;
3345 sav
->sav_npending
= ndev
;
3347 for (i
= 0; i
< ndev
; i
++) {
3348 if ((error
= spa_config_parse(spa
, &vd
, dev
[i
], NULL
, 0,
3352 if (!vd
->vdev_ops
->vdev_op_leaf
) {
3354 error
= SET_ERROR(EINVAL
);
3359 * The L2ARC currently only supports disk devices in
3360 * kernel context. For user-level testing, we allow it.
3363 if ((strcmp(config
, ZPOOL_CONFIG_L2CACHE
) == 0) &&
3364 strcmp(vd
->vdev_ops
->vdev_op_type
, VDEV_TYPE_DISK
) != 0) {
3365 error
= SET_ERROR(ENOTBLK
);
3372 if ((error
= vdev_open(vd
)) == 0 &&
3373 (error
= vdev_label_init(vd
, crtxg
, label
)) == 0) {
3374 VERIFY(nvlist_add_uint64(dev
[i
], ZPOOL_CONFIG_GUID
,
3375 vd
->vdev_guid
) == 0);
3381 (mode
!= VDEV_ALLOC_SPARE
&& mode
!= VDEV_ALLOC_L2CACHE
))
3388 sav
->sav_pending
= NULL
;
3389 sav
->sav_npending
= 0;
3394 spa_validate_aux(spa_t
*spa
, nvlist_t
*nvroot
, uint64_t crtxg
, int mode
)
3398 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
3400 if ((error
= spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3401 &spa
->spa_spares
, ZPOOL_CONFIG_SPARES
, SPA_VERSION_SPARES
,
3402 VDEV_LABEL_SPARE
)) != 0) {
3406 return (spa_validate_aux_devs(spa
, nvroot
, crtxg
, mode
,
3407 &spa
->spa_l2cache
, ZPOOL_CONFIG_L2CACHE
, SPA_VERSION_L2CACHE
,
3408 VDEV_LABEL_L2CACHE
));
3412 spa_set_aux_vdevs(spa_aux_vdev_t
*sav
, nvlist_t
**devs
, int ndevs
,
3417 if (sav
->sav_config
!= NULL
) {
3423 * Generate new dev list by concatentating with the
3426 VERIFY(nvlist_lookup_nvlist_array(sav
->sav_config
, config
,
3427 &olddevs
, &oldndevs
) == 0);
3429 newdevs
= kmem_alloc(sizeof (void *) *
3430 (ndevs
+ oldndevs
), KM_PUSHPAGE
);
3431 for (i
= 0; i
< oldndevs
; i
++)
3432 VERIFY(nvlist_dup(olddevs
[i
], &newdevs
[i
],
3434 for (i
= 0; i
< ndevs
; i
++)
3435 VERIFY(nvlist_dup(devs
[i
], &newdevs
[i
+ oldndevs
],
3438 VERIFY(nvlist_remove(sav
->sav_config
, config
,
3439 DATA_TYPE_NVLIST_ARRAY
) == 0);
3441 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
,
3442 config
, newdevs
, ndevs
+ oldndevs
) == 0);
3443 for (i
= 0; i
< oldndevs
+ ndevs
; i
++)
3444 nvlist_free(newdevs
[i
]);
3445 kmem_free(newdevs
, (oldndevs
+ ndevs
) * sizeof (void *));
3448 * Generate a new dev list.
3450 VERIFY(nvlist_alloc(&sav
->sav_config
, NV_UNIQUE_NAME
,
3452 VERIFY(nvlist_add_nvlist_array(sav
->sav_config
, config
,
3458 * Stop and drop level 2 ARC devices
3461 spa_l2cache_drop(spa_t
*spa
)
3465 spa_aux_vdev_t
*sav
= &spa
->spa_l2cache
;
3467 for (i
= 0; i
< sav
->sav_count
; i
++) {
3470 vd
= sav
->sav_vdevs
[i
];
3473 if (spa_l2cache_exists(vd
->vdev_guid
, &pool
) &&
3474 pool
!= 0ULL && l2arc_vdev_present(vd
))
3475 l2arc_remove_vdev(vd
);
3483 spa_create(const char *pool
, nvlist_t
*nvroot
, nvlist_t
*props
,
3487 char *altroot
= NULL
;
3492 uint64_t txg
= TXG_INITIAL
;
3493 nvlist_t
**spares
, **l2cache
;
3494 uint_t nspares
, nl2cache
;
3495 uint64_t version
, obj
;
3496 boolean_t has_features
;
3502 if (nvlist_lookup_string(props
, "tname", &poolname
) != 0)
3503 poolname
= (char *)pool
;
3506 * If this pool already exists, return failure.
3508 mutex_enter(&spa_namespace_lock
);
3509 if (spa_lookup(poolname
) != NULL
) {
3510 mutex_exit(&spa_namespace_lock
);
3511 return (SET_ERROR(EEXIST
));
3515 * Allocate a new spa_t structure.
3517 nvl
= fnvlist_alloc();
3518 fnvlist_add_string(nvl
, ZPOOL_CONFIG_POOL_NAME
, pool
);
3519 (void) nvlist_lookup_string(props
,
3520 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
3521 spa
= spa_add(poolname
, nvl
, altroot
);
3523 spa_activate(spa
, spa_mode_global
);
3525 if (props
&& (error
= spa_prop_validate(spa
, props
))) {
3526 spa_deactivate(spa
);
3528 mutex_exit(&spa_namespace_lock
);
3533 * Temporary pool names should never be written to disk.
3535 if (poolname
!= pool
)
3536 spa
->spa_import_flags
|= ZFS_IMPORT_TEMP_NAME
;
3538 has_features
= B_FALSE
;
3539 for (elem
= nvlist_next_nvpair(props
, NULL
);
3540 elem
!= NULL
; elem
= nvlist_next_nvpair(props
, elem
)) {
3541 if (zpool_prop_feature(nvpair_name(elem
)))
3542 has_features
= B_TRUE
;
3545 if (has_features
|| nvlist_lookup_uint64(props
,
3546 zpool_prop_to_name(ZPOOL_PROP_VERSION
), &version
) != 0) {
3547 version
= SPA_VERSION
;
3549 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
3551 spa
->spa_first_txg
= txg
;
3552 spa
->spa_uberblock
.ub_txg
= txg
- 1;
3553 spa
->spa_uberblock
.ub_version
= version
;
3554 spa
->spa_ubsync
= spa
->spa_uberblock
;
3557 * Create "The Godfather" zio to hold all async IOs
3559 spa
->spa_async_zio_root
= zio_root(spa
, NULL
, NULL
,
3560 ZIO_FLAG_CANFAIL
| ZIO_FLAG_SPECULATIVE
| ZIO_FLAG_GODFATHER
);
3563 * Create the root vdev.
3565 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3567 error
= spa_config_parse(spa
, &rvd
, nvroot
, NULL
, 0, VDEV_ALLOC_ADD
);
3569 ASSERT(error
!= 0 || rvd
!= NULL
);
3570 ASSERT(error
!= 0 || spa
->spa_root_vdev
== rvd
);
3572 if (error
== 0 && !zfs_allocatable_devs(nvroot
))
3573 error
= SET_ERROR(EINVAL
);
3576 (error
= vdev_create(rvd
, txg
, B_FALSE
)) == 0 &&
3577 (error
= spa_validate_aux(spa
, nvroot
, txg
,
3578 VDEV_ALLOC_ADD
)) == 0) {
3579 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
3580 vdev_metaslab_set_size(rvd
->vdev_child
[c
]);
3581 vdev_expand(rvd
->vdev_child
[c
], txg
);
3585 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3589 spa_deactivate(spa
);
3591 mutex_exit(&spa_namespace_lock
);
3596 * Get the list of spares, if specified.
3598 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
3599 &spares
, &nspares
) == 0) {
3600 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
, NV_UNIQUE_NAME
,
3602 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
3603 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
3604 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3605 spa_load_spares(spa
);
3606 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3607 spa
->spa_spares
.sav_sync
= B_TRUE
;
3611 * Get the list of level 2 cache devices, if specified.
3613 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
3614 &l2cache
, &nl2cache
) == 0) {
3615 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
3616 NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
3617 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
3618 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
3619 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3620 spa_load_l2cache(spa
);
3621 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3622 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
3625 spa
->spa_is_initializing
= B_TRUE
;
3626 spa
->spa_dsl_pool
= dp
= dsl_pool_create(spa
, zplprops
, txg
);
3627 spa
->spa_meta_objset
= dp
->dp_meta_objset
;
3628 spa
->spa_is_initializing
= B_FALSE
;
3631 * Create DDTs (dedup tables).
3635 spa_update_dspace(spa
);
3637 tx
= dmu_tx_create_assigned(dp
, txg
);
3640 * Create the pool config object.
3642 spa
->spa_config_object
= dmu_object_alloc(spa
->spa_meta_objset
,
3643 DMU_OT_PACKED_NVLIST
, SPA_CONFIG_BLOCKSIZE
,
3644 DMU_OT_PACKED_NVLIST_SIZE
, sizeof (uint64_t), tx
);
3646 if (zap_add(spa
->spa_meta_objset
,
3647 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CONFIG
,
3648 sizeof (uint64_t), 1, &spa
->spa_config_object
, tx
) != 0) {
3649 cmn_err(CE_PANIC
, "failed to add pool config");
3652 if (spa_version(spa
) >= SPA_VERSION_FEATURES
)
3653 spa_feature_create_zap_objects(spa
, tx
);
3655 if (zap_add(spa
->spa_meta_objset
,
3656 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_CREATION_VERSION
,
3657 sizeof (uint64_t), 1, &version
, tx
) != 0) {
3658 cmn_err(CE_PANIC
, "failed to add pool version");
3661 /* Newly created pools with the right version are always deflated. */
3662 if (version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
3663 spa
->spa_deflate
= TRUE
;
3664 if (zap_add(spa
->spa_meta_objset
,
3665 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
3666 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
) != 0) {
3667 cmn_err(CE_PANIC
, "failed to add deflate");
3672 * Create the deferred-free bpobj. Turn off compression
3673 * because sync-to-convergence takes longer if the blocksize
3676 obj
= bpobj_alloc(spa
->spa_meta_objset
, 1 << 14, tx
);
3677 dmu_object_set_compress(spa
->spa_meta_objset
, obj
,
3678 ZIO_COMPRESS_OFF
, tx
);
3679 if (zap_add(spa
->spa_meta_objset
,
3680 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_SYNC_BPOBJ
,
3681 sizeof (uint64_t), 1, &obj
, tx
) != 0) {
3682 cmn_err(CE_PANIC
, "failed to add bpobj");
3684 VERIFY3U(0, ==, bpobj_open(&spa
->spa_deferred_bpobj
,
3685 spa
->spa_meta_objset
, obj
));
3688 * Create the pool's history object.
3690 if (version
>= SPA_VERSION_ZPOOL_HISTORY
)
3691 spa_history_create_obj(spa
, tx
);
3694 * Set pool properties.
3696 spa
->spa_bootfs
= zpool_prop_default_numeric(ZPOOL_PROP_BOOTFS
);
3697 spa
->spa_delegation
= zpool_prop_default_numeric(ZPOOL_PROP_DELEGATION
);
3698 spa
->spa_failmode
= zpool_prop_default_numeric(ZPOOL_PROP_FAILUREMODE
);
3699 spa
->spa_autoexpand
= zpool_prop_default_numeric(ZPOOL_PROP_AUTOEXPAND
);
3701 if (props
!= NULL
) {
3702 spa_configfile_set(spa
, props
, B_FALSE
);
3703 spa_sync_props(props
, tx
);
3708 spa
->spa_sync_on
= B_TRUE
;
3709 txg_sync_start(spa
->spa_dsl_pool
);
3712 * We explicitly wait for the first transaction to complete so that our
3713 * bean counters are appropriately updated.
3715 txg_wait_synced(spa
->spa_dsl_pool
, txg
);
3717 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
3719 spa_history_log_version(spa
, "create");
3721 spa
->spa_minref
= refcount_count(&spa
->spa_refcount
);
3723 mutex_exit(&spa_namespace_lock
);
3730 * Get the root pool information from the root disk, then import the root pool
3731 * during the system boot up time.
3733 extern int vdev_disk_read_rootlabel(char *, char *, nvlist_t
**);
3736 spa_generate_rootconf(char *devpath
, char *devid
, uint64_t *guid
)
3739 nvlist_t
*nvtop
, *nvroot
;
3742 if (vdev_disk_read_rootlabel(devpath
, devid
, &config
) != 0)
3746 * Add this top-level vdev to the child array.
3748 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3750 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
3752 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_GUID
, guid
) == 0);
3755 * Put this pool's top-level vdevs into a root vdev.
3757 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
3758 VERIFY(nvlist_add_string(nvroot
, ZPOOL_CONFIG_TYPE
,
3759 VDEV_TYPE_ROOT
) == 0);
3760 VERIFY(nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_ID
, 0ULL) == 0);
3761 VERIFY(nvlist_add_uint64(nvroot
, ZPOOL_CONFIG_GUID
, pgid
) == 0);
3762 VERIFY(nvlist_add_nvlist_array(nvroot
, ZPOOL_CONFIG_CHILDREN
,
3766 * Replace the existing vdev_tree with the new root vdev in
3767 * this pool's configuration (remove the old, add the new).
3769 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, nvroot
) == 0);
3770 nvlist_free(nvroot
);
3775 * Walk the vdev tree and see if we can find a device with "better"
3776 * configuration. A configuration is "better" if the label on that
3777 * device has a more recent txg.
3780 spa_alt_rootvdev(vdev_t
*vd
, vdev_t
**avd
, uint64_t *txg
)
3784 for (c
= 0; c
< vd
->vdev_children
; c
++)
3785 spa_alt_rootvdev(vd
->vdev_child
[c
], avd
, txg
);
3787 if (vd
->vdev_ops
->vdev_op_leaf
) {
3791 if (vdev_disk_read_rootlabel(vd
->vdev_physpath
, vd
->vdev_devid
,
3795 VERIFY(nvlist_lookup_uint64(label
, ZPOOL_CONFIG_POOL_TXG
,
3799 * Do we have a better boot device?
3801 if (label_txg
> *txg
) {
3810 * Import a root pool.
3812 * For x86. devpath_list will consist of devid and/or physpath name of
3813 * the vdev (e.g. "id1,sd@SSEAGATE..." or "/pci@1f,0/ide@d/disk@0,0:a").
3814 * The GRUB "findroot" command will return the vdev we should boot.
3816 * For Sparc, devpath_list consists the physpath name of the booting device
3817 * no matter the rootpool is a single device pool or a mirrored pool.
3819 * "/pci@1f,0/ide@d/disk@0,0:a"
3822 spa_import_rootpool(char *devpath
, char *devid
)
3825 vdev_t
*rvd
, *bvd
, *avd
= NULL
;
3826 nvlist_t
*config
, *nvtop
;
3832 * Read the label from the boot device and generate a configuration.
3834 config
= spa_generate_rootconf(devpath
, devid
, &guid
);
3835 #if defined(_OBP) && defined(_KERNEL)
3836 if (config
== NULL
) {
3837 if (strstr(devpath
, "/iscsi/ssd") != NULL
) {
3839 get_iscsi_bootpath_phy(devpath
);
3840 config
= spa_generate_rootconf(devpath
, devid
, &guid
);
3844 if (config
== NULL
) {
3845 cmn_err(CE_NOTE
, "Cannot read the pool label from '%s'",
3847 return (SET_ERROR(EIO
));
3850 VERIFY(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
3852 VERIFY(nvlist_lookup_uint64(config
, ZPOOL_CONFIG_POOL_TXG
, &txg
) == 0);
3854 mutex_enter(&spa_namespace_lock
);
3855 if ((spa
= spa_lookup(pname
)) != NULL
) {
3857 * Remove the existing root pool from the namespace so that we
3858 * can replace it with the correct config we just read in.
3863 spa
= spa_add(pname
, config
, NULL
);
3864 spa
->spa_is_root
= B_TRUE
;
3865 spa
->spa_import_flags
= ZFS_IMPORT_VERBATIM
;
3868 * Build up a vdev tree based on the boot device's label config.
3870 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
3872 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3873 error
= spa_config_parse(spa
, &rvd
, nvtop
, NULL
, 0,
3874 VDEV_ALLOC_ROOTPOOL
);
3875 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3877 mutex_exit(&spa_namespace_lock
);
3878 nvlist_free(config
);
3879 cmn_err(CE_NOTE
, "Can not parse the config for pool '%s'",
3885 * Get the boot vdev.
3887 if ((bvd
= vdev_lookup_by_guid(rvd
, guid
)) == NULL
) {
3888 cmn_err(CE_NOTE
, "Can not find the boot vdev for guid %llu",
3889 (u_longlong_t
)guid
);
3890 error
= SET_ERROR(ENOENT
);
3895 * Determine if there is a better boot device.
3898 spa_alt_rootvdev(rvd
, &avd
, &txg
);
3900 cmn_err(CE_NOTE
, "The boot device is 'degraded'. Please "
3901 "try booting from '%s'", avd
->vdev_path
);
3902 error
= SET_ERROR(EINVAL
);
3907 * If the boot device is part of a spare vdev then ensure that
3908 * we're booting off the active spare.
3910 if (bvd
->vdev_parent
->vdev_ops
== &vdev_spare_ops
&&
3911 !bvd
->vdev_isspare
) {
3912 cmn_err(CE_NOTE
, "The boot device is currently spared. Please "
3913 "try booting from '%s'",
3915 vdev_child
[bvd
->vdev_parent
->vdev_children
- 1]->vdev_path
);
3916 error
= SET_ERROR(EINVAL
);
3922 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
3924 spa_config_exit(spa
, SCL_ALL
, FTAG
);
3925 mutex_exit(&spa_namespace_lock
);
3927 nvlist_free(config
);
3934 * Import a non-root pool into the system.
3937 spa_import(char *pool
, nvlist_t
*config
, nvlist_t
*props
, uint64_t flags
)
3940 char *altroot
= NULL
;
3941 spa_load_state_t state
= SPA_LOAD_IMPORT
;
3942 zpool_rewind_policy_t policy
;
3943 uint64_t mode
= spa_mode_global
;
3944 uint64_t readonly
= B_FALSE
;
3947 nvlist_t
**spares
, **l2cache
;
3948 uint_t nspares
, nl2cache
;
3951 * If a pool with this name exists, return failure.
3953 mutex_enter(&spa_namespace_lock
);
3954 if (spa_lookup(pool
) != NULL
) {
3955 mutex_exit(&spa_namespace_lock
);
3956 return (SET_ERROR(EEXIST
));
3960 * Create and initialize the spa structure.
3962 (void) nvlist_lookup_string(props
,
3963 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
3964 (void) nvlist_lookup_uint64(props
,
3965 zpool_prop_to_name(ZPOOL_PROP_READONLY
), &readonly
);
3968 spa
= spa_add(pool
, config
, altroot
);
3969 spa
->spa_import_flags
= flags
;
3972 * Verbatim import - Take a pool and insert it into the namespace
3973 * as if it had been loaded at boot.
3975 if (spa
->spa_import_flags
& ZFS_IMPORT_VERBATIM
) {
3977 spa_configfile_set(spa
, props
, B_FALSE
);
3979 spa_config_sync(spa
, B_FALSE
, B_TRUE
);
3981 mutex_exit(&spa_namespace_lock
);
3985 spa_activate(spa
, mode
);
3988 * Don't start async tasks until we know everything is healthy.
3990 spa_async_suspend(spa
);
3992 zpool_get_rewind_policy(config
, &policy
);
3993 if (policy
.zrp_request
& ZPOOL_DO_REWIND
)
3994 state
= SPA_LOAD_RECOVER
;
3997 * Pass off the heavy lifting to spa_load(). Pass TRUE for mosconfig
3998 * because the user-supplied config is actually the one to trust when
4001 if (state
!= SPA_LOAD_RECOVER
)
4002 spa
->spa_last_ubsync_txg
= spa
->spa_load_txg
= 0;
4004 error
= spa_load_best(spa
, state
, B_TRUE
, policy
.zrp_txg
,
4005 policy
.zrp_request
);
4008 * Propagate anything learned while loading the pool and pass it
4009 * back to caller (i.e. rewind info, missing devices, etc).
4011 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4012 spa
->spa_load_info
) == 0);
4014 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4016 * Toss any existing sparelist, as it doesn't have any validity
4017 * anymore, and conflicts with spa_has_spare().
4019 if (spa
->spa_spares
.sav_config
) {
4020 nvlist_free(spa
->spa_spares
.sav_config
);
4021 spa
->spa_spares
.sav_config
= NULL
;
4022 spa_load_spares(spa
);
4024 if (spa
->spa_l2cache
.sav_config
) {
4025 nvlist_free(spa
->spa_l2cache
.sav_config
);
4026 spa
->spa_l2cache
.sav_config
= NULL
;
4027 spa_load_l2cache(spa
);
4030 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
4033 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
4036 error
= spa_validate_aux(spa
, nvroot
, -1ULL,
4037 VDEV_ALLOC_L2CACHE
);
4038 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4041 spa_configfile_set(spa
, props
, B_FALSE
);
4043 if (error
!= 0 || (props
&& spa_writeable(spa
) &&
4044 (error
= spa_prop_set(spa
, props
)))) {
4046 spa_deactivate(spa
);
4048 mutex_exit(&spa_namespace_lock
);
4052 spa_async_resume(spa
);
4055 * Override any spares and level 2 cache devices as specified by
4056 * the user, as these may have correct device names/devids, etc.
4058 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
,
4059 &spares
, &nspares
) == 0) {
4060 if (spa
->spa_spares
.sav_config
)
4061 VERIFY(nvlist_remove(spa
->spa_spares
.sav_config
,
4062 ZPOOL_CONFIG_SPARES
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4064 VERIFY(nvlist_alloc(&spa
->spa_spares
.sav_config
,
4065 NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
4066 VERIFY(nvlist_add_nvlist_array(spa
->spa_spares
.sav_config
,
4067 ZPOOL_CONFIG_SPARES
, spares
, nspares
) == 0);
4068 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4069 spa_load_spares(spa
);
4070 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4071 spa
->spa_spares
.sav_sync
= B_TRUE
;
4073 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
,
4074 &l2cache
, &nl2cache
) == 0) {
4075 if (spa
->spa_l2cache
.sav_config
)
4076 VERIFY(nvlist_remove(spa
->spa_l2cache
.sav_config
,
4077 ZPOOL_CONFIG_L2CACHE
, DATA_TYPE_NVLIST_ARRAY
) == 0);
4079 VERIFY(nvlist_alloc(&spa
->spa_l2cache
.sav_config
,
4080 NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
4081 VERIFY(nvlist_add_nvlist_array(spa
->spa_l2cache
.sav_config
,
4082 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
) == 0);
4083 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4084 spa_load_l2cache(spa
);
4085 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4086 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4090 * Check for any removed devices.
4092 if (spa
->spa_autoreplace
) {
4093 spa_aux_check_removed(&spa
->spa_spares
);
4094 spa_aux_check_removed(&spa
->spa_l2cache
);
4097 if (spa_writeable(spa
)) {
4099 * Update the config cache to include the newly-imported pool.
4101 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4105 * It's possible that the pool was expanded while it was exported.
4106 * We kick off an async task to handle this for us.
4108 spa_async_request(spa
, SPA_ASYNC_AUTOEXPAND
);
4110 mutex_exit(&spa_namespace_lock
);
4111 spa_history_log_version(spa
, "import");
4114 zvol_create_minors(pool
);
4121 spa_tryimport(nvlist_t
*tryconfig
)
4123 nvlist_t
*config
= NULL
;
4129 if (nvlist_lookup_string(tryconfig
, ZPOOL_CONFIG_POOL_NAME
, &poolname
))
4132 if (nvlist_lookup_uint64(tryconfig
, ZPOOL_CONFIG_POOL_STATE
, &state
))
4136 * Create and initialize the spa structure.
4138 mutex_enter(&spa_namespace_lock
);
4139 spa
= spa_add(TRYIMPORT_NAME
, tryconfig
, NULL
);
4140 spa_activate(spa
, FREAD
);
4143 * Pass off the heavy lifting to spa_load().
4144 * Pass TRUE for mosconfig because the user-supplied config
4145 * is actually the one to trust when doing an import.
4147 error
= spa_load(spa
, SPA_LOAD_TRYIMPORT
, SPA_IMPORT_EXISTING
, B_TRUE
);
4150 * If 'tryconfig' was at least parsable, return the current config.
4152 if (spa
->spa_root_vdev
!= NULL
) {
4153 config
= spa_config_generate(spa
, NULL
, -1ULL, B_TRUE
);
4154 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
,
4156 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
4158 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_TIMESTAMP
,
4159 spa
->spa_uberblock
.ub_timestamp
) == 0);
4160 VERIFY(nvlist_add_nvlist(config
, ZPOOL_CONFIG_LOAD_INFO
,
4161 spa
->spa_load_info
) == 0);
4162 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_ERRATA
,
4163 spa
->spa_errata
) == 0);
4166 * If the bootfs property exists on this pool then we
4167 * copy it out so that external consumers can tell which
4168 * pools are bootable.
4170 if ((!error
|| error
== EEXIST
) && spa
->spa_bootfs
) {
4171 char *tmpname
= kmem_alloc(MAXPATHLEN
, KM_PUSHPAGE
);
4174 * We have to play games with the name since the
4175 * pool was opened as TRYIMPORT_NAME.
4177 if (dsl_dsobj_to_dsname(spa_name(spa
),
4178 spa
->spa_bootfs
, tmpname
) == 0) {
4182 dsname
= kmem_alloc(MAXPATHLEN
, KM_PUSHPAGE
);
4184 cp
= strchr(tmpname
, '/');
4186 (void) strlcpy(dsname
, tmpname
,
4189 (void) snprintf(dsname
, MAXPATHLEN
,
4190 "%s/%s", poolname
, ++cp
);
4192 VERIFY(nvlist_add_string(config
,
4193 ZPOOL_CONFIG_BOOTFS
, dsname
) == 0);
4194 kmem_free(dsname
, MAXPATHLEN
);
4196 kmem_free(tmpname
, MAXPATHLEN
);
4200 * Add the list of hot spares and level 2 cache devices.
4202 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
4203 spa_add_spares(spa
, config
);
4204 spa_add_l2cache(spa
, config
);
4205 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
4209 spa_deactivate(spa
);
4211 mutex_exit(&spa_namespace_lock
);
4217 * Pool export/destroy
4219 * The act of destroying or exporting a pool is very simple. We make sure there
4220 * is no more pending I/O and any references to the pool are gone. Then, we
4221 * update the pool state and sync all the labels to disk, removing the
4222 * configuration from the cache afterwards. If the 'hardforce' flag is set, then
4223 * we don't sync the labels or remove the configuration cache.
4226 spa_export_common(char *pool
, int new_state
, nvlist_t
**oldconfig
,
4227 boolean_t force
, boolean_t hardforce
)
4234 if (!(spa_mode_global
& FWRITE
))
4235 return (SET_ERROR(EROFS
));
4237 mutex_enter(&spa_namespace_lock
);
4238 if ((spa
= spa_lookup(pool
)) == NULL
) {
4239 mutex_exit(&spa_namespace_lock
);
4240 return (SET_ERROR(ENOENT
));
4244 * Put a hold on the pool, drop the namespace lock, stop async tasks,
4245 * reacquire the namespace lock, and see if we can export.
4247 spa_open_ref(spa
, FTAG
);
4248 mutex_exit(&spa_namespace_lock
);
4249 spa_async_suspend(spa
);
4250 mutex_enter(&spa_namespace_lock
);
4251 spa_close(spa
, FTAG
);
4254 * The pool will be in core if it's openable,
4255 * in which case we can modify its state.
4257 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
&& spa
->spa_sync_on
) {
4259 * Objsets may be open only because they're dirty, so we
4260 * have to force it to sync before checking spa_refcnt.
4262 txg_wait_synced(spa
->spa_dsl_pool
, 0);
4265 * A pool cannot be exported or destroyed if there are active
4266 * references. If we are resetting a pool, allow references by
4267 * fault injection handlers.
4269 if (!spa_refcount_zero(spa
) ||
4270 (spa
->spa_inject_ref
!= 0 &&
4271 new_state
!= POOL_STATE_UNINITIALIZED
)) {
4272 spa_async_resume(spa
);
4273 mutex_exit(&spa_namespace_lock
);
4274 return (SET_ERROR(EBUSY
));
4278 * A pool cannot be exported if it has an active shared spare.
4279 * This is to prevent other pools stealing the active spare
4280 * from an exported pool. At user's own will, such pool can
4281 * be forcedly exported.
4283 if (!force
&& new_state
== POOL_STATE_EXPORTED
&&
4284 spa_has_active_shared_spare(spa
)) {
4285 spa_async_resume(spa
);
4286 mutex_exit(&spa_namespace_lock
);
4287 return (SET_ERROR(EXDEV
));
4291 * We want this to be reflected on every label,
4292 * so mark them all dirty. spa_unload() will do the
4293 * final sync that pushes these changes out.
4295 if (new_state
!= POOL_STATE_UNINITIALIZED
&& !hardforce
) {
4296 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
4297 spa
->spa_state
= new_state
;
4298 spa
->spa_final_txg
= spa_last_synced_txg(spa
) +
4300 vdev_config_dirty(spa
->spa_root_vdev
);
4301 spa_config_exit(spa
, SCL_ALL
, FTAG
);
4305 spa_event_notify(spa
, NULL
, FM_EREPORT_ZFS_POOL_DESTROY
);
4307 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
4309 spa_deactivate(spa
);
4312 if (oldconfig
&& spa
->spa_config
)
4313 VERIFY(nvlist_dup(spa
->spa_config
, oldconfig
, 0) == 0);
4315 if (new_state
!= POOL_STATE_UNINITIALIZED
) {
4317 spa_config_sync(spa
, B_TRUE
, B_TRUE
);
4320 mutex_exit(&spa_namespace_lock
);
4326 * Destroy a storage pool.
4329 spa_destroy(char *pool
)
4331 return (spa_export_common(pool
, POOL_STATE_DESTROYED
, NULL
,
4336 * Export a storage pool.
4339 spa_export(char *pool
, nvlist_t
**oldconfig
, boolean_t force
,
4340 boolean_t hardforce
)
4342 return (spa_export_common(pool
, POOL_STATE_EXPORTED
, oldconfig
,
4347 * Similar to spa_export(), this unloads the spa_t without actually removing it
4348 * from the namespace in any way.
4351 spa_reset(char *pool
)
4353 return (spa_export_common(pool
, POOL_STATE_UNINITIALIZED
, NULL
,
4358 * ==========================================================================
4359 * Device manipulation
4360 * ==========================================================================
4364 * Add a device to a storage pool.
4367 spa_vdev_add(spa_t
*spa
, nvlist_t
*nvroot
)
4371 vdev_t
*rvd
= spa
->spa_root_vdev
;
4373 nvlist_t
**spares
, **l2cache
;
4374 uint_t nspares
, nl2cache
;
4377 ASSERT(spa_writeable(spa
));
4379 txg
= spa_vdev_enter(spa
);
4381 if ((error
= spa_config_parse(spa
, &vd
, nvroot
, NULL
, 0,
4382 VDEV_ALLOC_ADD
)) != 0)
4383 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4385 spa
->spa_pending_vdev
= vd
; /* spa_vdev_exit() will clear this */
4387 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_SPARES
, &spares
,
4391 if (nvlist_lookup_nvlist_array(nvroot
, ZPOOL_CONFIG_L2CACHE
, &l2cache
,
4395 if (vd
->vdev_children
== 0 && nspares
== 0 && nl2cache
== 0)
4396 return (spa_vdev_exit(spa
, vd
, txg
, EINVAL
));
4398 if (vd
->vdev_children
!= 0 &&
4399 (error
= vdev_create(vd
, txg
, B_FALSE
)) != 0)
4400 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4403 * We must validate the spares and l2cache devices after checking the
4404 * children. Otherwise, vdev_inuse() will blindly overwrite the spare.
4406 if ((error
= spa_validate_aux(spa
, nvroot
, txg
, VDEV_ALLOC_ADD
)) != 0)
4407 return (spa_vdev_exit(spa
, vd
, txg
, error
));
4410 * Transfer each new top-level vdev from vd to rvd.
4412 for (c
= 0; c
< vd
->vdev_children
; c
++) {
4415 * Set the vdev id to the first hole, if one exists.
4417 for (id
= 0; id
< rvd
->vdev_children
; id
++) {
4418 if (rvd
->vdev_child
[id
]->vdev_ishole
) {
4419 vdev_free(rvd
->vdev_child
[id
]);
4423 tvd
= vd
->vdev_child
[c
];
4424 vdev_remove_child(vd
, tvd
);
4426 vdev_add_child(rvd
, tvd
);
4427 vdev_config_dirty(tvd
);
4431 spa_set_aux_vdevs(&spa
->spa_spares
, spares
, nspares
,
4432 ZPOOL_CONFIG_SPARES
);
4433 spa_load_spares(spa
);
4434 spa
->spa_spares
.sav_sync
= B_TRUE
;
4437 if (nl2cache
!= 0) {
4438 spa_set_aux_vdevs(&spa
->spa_l2cache
, l2cache
, nl2cache
,
4439 ZPOOL_CONFIG_L2CACHE
);
4440 spa_load_l2cache(spa
);
4441 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
4445 * We have to be careful when adding new vdevs to an existing pool.
4446 * If other threads start allocating from these vdevs before we
4447 * sync the config cache, and we lose power, then upon reboot we may
4448 * fail to open the pool because there are DVAs that the config cache
4449 * can't translate. Therefore, we first add the vdevs without
4450 * initializing metaslabs; sync the config cache (via spa_vdev_exit());
4451 * and then let spa_config_update() initialize the new metaslabs.
4453 * spa_load() checks for added-but-not-initialized vdevs, so that
4454 * if we lose power at any point in this sequence, the remaining
4455 * steps will be completed the next time we load the pool.
4457 (void) spa_vdev_exit(spa
, vd
, txg
, 0);
4459 mutex_enter(&spa_namespace_lock
);
4460 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
4461 mutex_exit(&spa_namespace_lock
);
4467 * Attach a device to a mirror. The arguments are the path to any device
4468 * in the mirror, and the nvroot for the new device. If the path specifies
4469 * a device that is not mirrored, we automatically insert the mirror vdev.
4471 * If 'replacing' is specified, the new device is intended to replace the
4472 * existing device; in this case the two devices are made into their own
4473 * mirror using the 'replacing' vdev, which is functionally identical to
4474 * the mirror vdev (it actually reuses all the same ops) but has a few
4475 * extra rules: you can't attach to it after it's been created, and upon
4476 * completion of resilvering, the first disk (the one being replaced)
4477 * is automatically detached.
4480 spa_vdev_attach(spa_t
*spa
, uint64_t guid
, nvlist_t
*nvroot
, int replacing
)
4482 uint64_t txg
, dtl_max_txg
;
4483 vdev_t
*oldvd
, *newvd
, *newrootvd
, *pvd
, *tvd
;
4485 char *oldvdpath
, *newvdpath
;
4488 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
4490 ASSERT(spa_writeable(spa
));
4492 txg
= spa_vdev_enter(spa
);
4494 oldvd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4497 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4499 if (!oldvd
->vdev_ops
->vdev_op_leaf
)
4500 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4502 pvd
= oldvd
->vdev_parent
;
4504 if ((error
= spa_config_parse(spa
, &newrootvd
, nvroot
, NULL
, 0,
4505 VDEV_ALLOC_ATTACH
)) != 0)
4506 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4508 if (newrootvd
->vdev_children
!= 1)
4509 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4511 newvd
= newrootvd
->vdev_child
[0];
4513 if (!newvd
->vdev_ops
->vdev_op_leaf
)
4514 return (spa_vdev_exit(spa
, newrootvd
, txg
, EINVAL
));
4516 if ((error
= vdev_create(newrootvd
, txg
, replacing
)) != 0)
4517 return (spa_vdev_exit(spa
, newrootvd
, txg
, error
));
4520 * Spares can't replace logs
4522 if (oldvd
->vdev_top
->vdev_islog
&& newvd
->vdev_isspare
)
4523 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4527 * For attach, the only allowable parent is a mirror or the root
4530 if (pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4531 pvd
->vdev_ops
!= &vdev_root_ops
)
4532 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4534 pvops
= &vdev_mirror_ops
;
4537 * Active hot spares can only be replaced by inactive hot
4540 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4541 oldvd
->vdev_isspare
&&
4542 !spa_has_spare(spa
, newvd
->vdev_guid
))
4543 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4546 * If the source is a hot spare, and the parent isn't already a
4547 * spare, then we want to create a new hot spare. Otherwise, we
4548 * want to create a replacing vdev. The user is not allowed to
4549 * attach to a spared vdev child unless the 'isspare' state is
4550 * the same (spare replaces spare, non-spare replaces
4553 if (pvd
->vdev_ops
== &vdev_replacing_ops
&&
4554 spa_version(spa
) < SPA_VERSION_MULTI_REPLACE
) {
4555 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4556 } else if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4557 newvd
->vdev_isspare
!= oldvd
->vdev_isspare
) {
4558 return (spa_vdev_exit(spa
, newrootvd
, txg
, ENOTSUP
));
4561 if (newvd
->vdev_isspare
)
4562 pvops
= &vdev_spare_ops
;
4564 pvops
= &vdev_replacing_ops
;
4568 * Make sure the new device is big enough.
4570 if (newvd
->vdev_asize
< vdev_get_min_asize(oldvd
))
4571 return (spa_vdev_exit(spa
, newrootvd
, txg
, EOVERFLOW
));
4574 * The new device cannot have a higher alignment requirement
4575 * than the top-level vdev.
4577 if (newvd
->vdev_ashift
> oldvd
->vdev_top
->vdev_ashift
)
4578 return (spa_vdev_exit(spa
, newrootvd
, txg
, EDOM
));
4581 * If this is an in-place replacement, update oldvd's path and devid
4582 * to make it distinguishable from newvd, and unopenable from now on.
4584 if (strcmp(oldvd
->vdev_path
, newvd
->vdev_path
) == 0) {
4585 spa_strfree(oldvd
->vdev_path
);
4586 oldvd
->vdev_path
= kmem_alloc(strlen(newvd
->vdev_path
) + 5,
4588 (void) sprintf(oldvd
->vdev_path
, "%s/%s",
4589 newvd
->vdev_path
, "old");
4590 if (oldvd
->vdev_devid
!= NULL
) {
4591 spa_strfree(oldvd
->vdev_devid
);
4592 oldvd
->vdev_devid
= NULL
;
4596 /* mark the device being resilvered */
4597 newvd
->vdev_resilver_txg
= txg
;
4600 * If the parent is not a mirror, or if we're replacing, insert the new
4601 * mirror/replacing/spare vdev above oldvd.
4603 if (pvd
->vdev_ops
!= pvops
)
4604 pvd
= vdev_add_parent(oldvd
, pvops
);
4606 ASSERT(pvd
->vdev_top
->vdev_parent
== rvd
);
4607 ASSERT(pvd
->vdev_ops
== pvops
);
4608 ASSERT(oldvd
->vdev_parent
== pvd
);
4611 * Extract the new device from its root and add it to pvd.
4613 vdev_remove_child(newrootvd
, newvd
);
4614 newvd
->vdev_id
= pvd
->vdev_children
;
4615 newvd
->vdev_crtxg
= oldvd
->vdev_crtxg
;
4616 vdev_add_child(pvd
, newvd
);
4618 tvd
= newvd
->vdev_top
;
4619 ASSERT(pvd
->vdev_top
== tvd
);
4620 ASSERT(tvd
->vdev_parent
== rvd
);
4622 vdev_config_dirty(tvd
);
4625 * Set newvd's DTL to [TXG_INITIAL, dtl_max_txg) so that we account
4626 * for any dmu_sync-ed blocks. It will propagate upward when
4627 * spa_vdev_exit() calls vdev_dtl_reassess().
4629 dtl_max_txg
= txg
+ TXG_CONCURRENT_STATES
;
4631 vdev_dtl_dirty(newvd
, DTL_MISSING
, TXG_INITIAL
,
4632 dtl_max_txg
- TXG_INITIAL
);
4634 if (newvd
->vdev_isspare
) {
4635 spa_spare_activate(newvd
);
4636 spa_event_notify(spa
, newvd
, FM_EREPORT_ZFS_DEVICE_SPARE
);
4639 oldvdpath
= spa_strdup(oldvd
->vdev_path
);
4640 newvdpath
= spa_strdup(newvd
->vdev_path
);
4641 newvd_isspare
= newvd
->vdev_isspare
;
4644 * Mark newvd's DTL dirty in this txg.
4646 vdev_dirty(tvd
, VDD_DTL
, newvd
, txg
);
4649 * Schedule the resilver to restart in the future. We do this to
4650 * ensure that dmu_sync-ed blocks have been stitched into the
4651 * respective datasets.
4653 dsl_resilver_restart(spa
->spa_dsl_pool
, dtl_max_txg
);
4658 (void) spa_vdev_exit(spa
, newrootvd
, dtl_max_txg
, 0);
4660 spa_history_log_internal(spa
, "vdev attach", NULL
,
4661 "%s vdev=%s %s vdev=%s",
4662 replacing
&& newvd_isspare
? "spare in" :
4663 replacing
? "replace" : "attach", newvdpath
,
4664 replacing
? "for" : "to", oldvdpath
);
4666 spa_strfree(oldvdpath
);
4667 spa_strfree(newvdpath
);
4669 if (spa
->spa_bootfs
)
4670 spa_event_notify(spa
, newvd
, FM_EREPORT_ZFS_BOOTFS_VDEV_ATTACH
);
4676 * Detach a device from a mirror or replacing vdev.
4678 * If 'replace_done' is specified, only detach if the parent
4679 * is a replacing vdev.
4682 spa_vdev_detach(spa_t
*spa
, uint64_t guid
, uint64_t pguid
, int replace_done
)
4686 vdev_t
*vd
, *pvd
, *cvd
, *tvd
;
4687 boolean_t unspare
= B_FALSE
;
4688 uint64_t unspare_guid
= 0;
4691 ASSERTV(vdev_t
*rvd
= spa
->spa_root_vdev
);
4692 ASSERT(spa_writeable(spa
));
4694 txg
= spa_vdev_enter(spa
);
4696 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
4699 return (spa_vdev_exit(spa
, NULL
, txg
, ENODEV
));
4701 if (!vd
->vdev_ops
->vdev_op_leaf
)
4702 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4704 pvd
= vd
->vdev_parent
;
4707 * If the parent/child relationship is not as expected, don't do it.
4708 * Consider M(A,R(B,C)) -- that is, a mirror of A with a replacing
4709 * vdev that's replacing B with C. The user's intent in replacing
4710 * is to go from M(A,B) to M(A,C). If the user decides to cancel
4711 * the replace by detaching C, the expected behavior is to end up
4712 * M(A,B). But suppose that right after deciding to detach C,
4713 * the replacement of B completes. We would have M(A,C), and then
4714 * ask to detach C, which would leave us with just A -- not what
4715 * the user wanted. To prevent this, we make sure that the
4716 * parent/child relationship hasn't changed -- in this example,
4717 * that C's parent is still the replacing vdev R.
4719 if (pvd
->vdev_guid
!= pguid
&& pguid
!= 0)
4720 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4723 * Only 'replacing' or 'spare' vdevs can be replaced.
4725 if (replace_done
&& pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4726 pvd
->vdev_ops
!= &vdev_spare_ops
)
4727 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4729 ASSERT(pvd
->vdev_ops
!= &vdev_spare_ops
||
4730 spa_version(spa
) >= SPA_VERSION_SPARES
);
4733 * Only mirror, replacing, and spare vdevs support detach.
4735 if (pvd
->vdev_ops
!= &vdev_replacing_ops
&&
4736 pvd
->vdev_ops
!= &vdev_mirror_ops
&&
4737 pvd
->vdev_ops
!= &vdev_spare_ops
)
4738 return (spa_vdev_exit(spa
, NULL
, txg
, ENOTSUP
));
4741 * If this device has the only valid copy of some data,
4742 * we cannot safely detach it.
4744 if (vdev_dtl_required(vd
))
4745 return (spa_vdev_exit(spa
, NULL
, txg
, EBUSY
));
4747 ASSERT(pvd
->vdev_children
>= 2);
4750 * If we are detaching the second disk from a replacing vdev, then
4751 * check to see if we changed the original vdev's path to have "/old"
4752 * at the end in spa_vdev_attach(). If so, undo that change now.
4754 if (pvd
->vdev_ops
== &vdev_replacing_ops
&& vd
->vdev_id
> 0 &&
4755 vd
->vdev_path
!= NULL
) {
4756 size_t len
= strlen(vd
->vdev_path
);
4758 for (c
= 0; c
< pvd
->vdev_children
; c
++) {
4759 cvd
= pvd
->vdev_child
[c
];
4761 if (cvd
== vd
|| cvd
->vdev_path
== NULL
)
4764 if (strncmp(cvd
->vdev_path
, vd
->vdev_path
, len
) == 0 &&
4765 strcmp(cvd
->vdev_path
+ len
, "/old") == 0) {
4766 spa_strfree(cvd
->vdev_path
);
4767 cvd
->vdev_path
= spa_strdup(vd
->vdev_path
);
4774 * If we are detaching the original disk from a spare, then it implies
4775 * that the spare should become a real disk, and be removed from the
4776 * active spare list for the pool.
4778 if (pvd
->vdev_ops
== &vdev_spare_ops
&&
4780 pvd
->vdev_child
[pvd
->vdev_children
- 1]->vdev_isspare
)
4784 * Erase the disk labels so the disk can be used for other things.
4785 * This must be done after all other error cases are handled,
4786 * but before we disembowel vd (so we can still do I/O to it).
4787 * But if we can't do it, don't treat the error as fatal --
4788 * it may be that the unwritability of the disk is the reason
4789 * it's being detached!
4791 error
= vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
4794 * Remove vd from its parent and compact the parent's children.
4796 vdev_remove_child(pvd
, vd
);
4797 vdev_compact_children(pvd
);
4800 * Remember one of the remaining children so we can get tvd below.
4802 cvd
= pvd
->vdev_child
[pvd
->vdev_children
- 1];
4805 * If we need to remove the remaining child from the list of hot spares,
4806 * do it now, marking the vdev as no longer a spare in the process.
4807 * We must do this before vdev_remove_parent(), because that can
4808 * change the GUID if it creates a new toplevel GUID. For a similar
4809 * reason, we must remove the spare now, in the same txg as the detach;
4810 * otherwise someone could attach a new sibling, change the GUID, and
4811 * the subsequent attempt to spa_vdev_remove(unspare_guid) would fail.
4814 ASSERT(cvd
->vdev_isspare
);
4815 spa_spare_remove(cvd
);
4816 unspare_guid
= cvd
->vdev_guid
;
4817 (void) spa_vdev_remove(spa
, unspare_guid
, B_TRUE
);
4818 cvd
->vdev_unspare
= B_TRUE
;
4822 * If the parent mirror/replacing vdev only has one child,
4823 * the parent is no longer needed. Remove it from the tree.
4825 if (pvd
->vdev_children
== 1) {
4826 if (pvd
->vdev_ops
== &vdev_spare_ops
)
4827 cvd
->vdev_unspare
= B_FALSE
;
4828 vdev_remove_parent(cvd
);
4833 * We don't set tvd until now because the parent we just removed
4834 * may have been the previous top-level vdev.
4836 tvd
= cvd
->vdev_top
;
4837 ASSERT(tvd
->vdev_parent
== rvd
);
4840 * Reevaluate the parent vdev state.
4842 vdev_propagate_state(cvd
);
4845 * If the 'autoexpand' property is set on the pool then automatically
4846 * try to expand the size of the pool. For example if the device we
4847 * just detached was smaller than the others, it may be possible to
4848 * add metaslabs (i.e. grow the pool). We need to reopen the vdev
4849 * first so that we can obtain the updated sizes of the leaf vdevs.
4851 if (spa
->spa_autoexpand
) {
4853 vdev_expand(tvd
, txg
);
4856 vdev_config_dirty(tvd
);
4859 * Mark vd's DTL as dirty in this txg. vdev_dtl_sync() will see that
4860 * vd->vdev_detached is set and free vd's DTL object in syncing context.
4861 * But first make sure we're not on any *other* txg's DTL list, to
4862 * prevent vd from being accessed after it's freed.
4864 vdpath
= spa_strdup(vd
->vdev_path
);
4865 for (t
= 0; t
< TXG_SIZE
; t
++)
4866 (void) txg_list_remove_this(&tvd
->vdev_dtl_list
, vd
, t
);
4867 vd
->vdev_detached
= B_TRUE
;
4868 vdev_dirty(tvd
, VDD_DTL
, vd
, txg
);
4870 spa_event_notify(spa
, vd
, FM_EREPORT_ZFS_DEVICE_REMOVE
);
4872 /* hang on to the spa before we release the lock */
4873 spa_open_ref(spa
, FTAG
);
4875 error
= spa_vdev_exit(spa
, vd
, txg
, 0);
4877 spa_history_log_internal(spa
, "detach", NULL
,
4879 spa_strfree(vdpath
);
4882 * If this was the removal of the original device in a hot spare vdev,
4883 * then we want to go through and remove the device from the hot spare
4884 * list of every other pool.
4887 spa_t
*altspa
= NULL
;
4889 mutex_enter(&spa_namespace_lock
);
4890 while ((altspa
= spa_next(altspa
)) != NULL
) {
4891 if (altspa
->spa_state
!= POOL_STATE_ACTIVE
||
4895 spa_open_ref(altspa
, FTAG
);
4896 mutex_exit(&spa_namespace_lock
);
4897 (void) spa_vdev_remove(altspa
, unspare_guid
, B_TRUE
);
4898 mutex_enter(&spa_namespace_lock
);
4899 spa_close(altspa
, FTAG
);
4901 mutex_exit(&spa_namespace_lock
);
4903 /* search the rest of the vdevs for spares to remove */
4904 spa_vdev_resilver_done(spa
);
4907 /* all done with the spa; OK to release */
4908 mutex_enter(&spa_namespace_lock
);
4909 spa_close(spa
, FTAG
);
4910 mutex_exit(&spa_namespace_lock
);
4916 * Split a set of devices from their mirrors, and create a new pool from them.
4919 spa_vdev_split_mirror(spa_t
*spa
, char *newname
, nvlist_t
*config
,
4920 nvlist_t
*props
, boolean_t exp
)
4923 uint64_t txg
, *glist
;
4925 uint_t c
, children
, lastlog
;
4926 nvlist_t
**child
, *nvl
, *tmp
;
4928 char *altroot
= NULL
;
4929 vdev_t
*rvd
, **vml
= NULL
; /* vdev modify list */
4930 boolean_t activate_slog
;
4932 ASSERT(spa_writeable(spa
));
4934 txg
= spa_vdev_enter(spa
);
4936 /* clear the log and flush everything up to now */
4937 activate_slog
= spa_passivate_log(spa
);
4938 (void) spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
4939 error
= spa_offline_log(spa
);
4940 txg
= spa_vdev_config_enter(spa
);
4943 spa_activate_log(spa
);
4946 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
4948 /* check new spa name before going any further */
4949 if (spa_lookup(newname
) != NULL
)
4950 return (spa_vdev_exit(spa
, NULL
, txg
, EEXIST
));
4953 * scan through all the children to ensure they're all mirrors
4955 if (nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
, &nvl
) != 0 ||
4956 nvlist_lookup_nvlist_array(nvl
, ZPOOL_CONFIG_CHILDREN
, &child
,
4958 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4960 /* first, check to ensure we've got the right child count */
4961 rvd
= spa
->spa_root_vdev
;
4963 for (c
= 0; c
< rvd
->vdev_children
; c
++) {
4964 vdev_t
*vd
= rvd
->vdev_child
[c
];
4966 /* don't count the holes & logs as children */
4967 if (vd
->vdev_islog
|| vd
->vdev_ishole
) {
4975 if (children
!= (lastlog
!= 0 ? lastlog
: rvd
->vdev_children
))
4976 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4978 /* next, ensure no spare or cache devices are part of the split */
4979 if (nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_SPARES
, &tmp
) == 0 ||
4980 nvlist_lookup_nvlist(nvl
, ZPOOL_CONFIG_L2CACHE
, &tmp
) == 0)
4981 return (spa_vdev_exit(spa
, NULL
, txg
, EINVAL
));
4983 vml
= kmem_zalloc(children
* sizeof (vdev_t
*), KM_PUSHPAGE
);
4984 glist
= kmem_zalloc(children
* sizeof (uint64_t), KM_PUSHPAGE
);
4986 /* then, loop over each vdev and validate it */
4987 for (c
= 0; c
< children
; c
++) {
4988 uint64_t is_hole
= 0;
4990 (void) nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_IS_HOLE
,
4994 if (spa
->spa_root_vdev
->vdev_child
[c
]->vdev_ishole
||
4995 spa
->spa_root_vdev
->vdev_child
[c
]->vdev_islog
) {
4998 error
= SET_ERROR(EINVAL
);
5003 /* which disk is going to be split? */
5004 if (nvlist_lookup_uint64(child
[c
], ZPOOL_CONFIG_GUID
,
5006 error
= SET_ERROR(EINVAL
);
5010 /* look it up in the spa */
5011 vml
[c
] = spa_lookup_by_guid(spa
, glist
[c
], B_FALSE
);
5012 if (vml
[c
] == NULL
) {
5013 error
= SET_ERROR(ENODEV
);
5017 /* make sure there's nothing stopping the split */
5018 if (vml
[c
]->vdev_parent
->vdev_ops
!= &vdev_mirror_ops
||
5019 vml
[c
]->vdev_islog
||
5020 vml
[c
]->vdev_ishole
||
5021 vml
[c
]->vdev_isspare
||
5022 vml
[c
]->vdev_isl2cache
||
5023 !vdev_writeable(vml
[c
]) ||
5024 vml
[c
]->vdev_children
!= 0 ||
5025 vml
[c
]->vdev_state
!= VDEV_STATE_HEALTHY
||
5026 c
!= spa
->spa_root_vdev
->vdev_child
[c
]->vdev_id
) {
5027 error
= SET_ERROR(EINVAL
);
5031 if (vdev_dtl_required(vml
[c
])) {
5032 error
= SET_ERROR(EBUSY
);
5036 /* we need certain info from the top level */
5037 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_ARRAY
,
5038 vml
[c
]->vdev_top
->vdev_ms_array
) == 0);
5039 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_METASLAB_SHIFT
,
5040 vml
[c
]->vdev_top
->vdev_ms_shift
) == 0);
5041 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASIZE
,
5042 vml
[c
]->vdev_top
->vdev_asize
) == 0);
5043 VERIFY(nvlist_add_uint64(child
[c
], ZPOOL_CONFIG_ASHIFT
,
5044 vml
[c
]->vdev_top
->vdev_ashift
) == 0);
5048 kmem_free(vml
, children
* sizeof (vdev_t
*));
5049 kmem_free(glist
, children
* sizeof (uint64_t));
5050 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5053 /* stop writers from using the disks */
5054 for (c
= 0; c
< children
; c
++) {
5056 vml
[c
]->vdev_offline
= B_TRUE
;
5058 vdev_reopen(spa
->spa_root_vdev
);
5061 * Temporarily record the splitting vdevs in the spa config. This
5062 * will disappear once the config is regenerated.
5064 VERIFY(nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
5065 VERIFY(nvlist_add_uint64_array(nvl
, ZPOOL_CONFIG_SPLIT_LIST
,
5066 glist
, children
) == 0);
5067 kmem_free(glist
, children
* sizeof (uint64_t));
5069 mutex_enter(&spa
->spa_props_lock
);
5070 VERIFY(nvlist_add_nvlist(spa
->spa_config
, ZPOOL_CONFIG_SPLIT
,
5072 mutex_exit(&spa
->spa_props_lock
);
5073 spa
->spa_config_splitting
= nvl
;
5074 vdev_config_dirty(spa
->spa_root_vdev
);
5076 /* configure and create the new pool */
5077 VERIFY(nvlist_add_string(config
, ZPOOL_CONFIG_POOL_NAME
, newname
) == 0);
5078 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_STATE
,
5079 exp
? POOL_STATE_EXPORTED
: POOL_STATE_ACTIVE
) == 0);
5080 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
5081 spa_version(spa
)) == 0);
5082 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_TXG
,
5083 spa
->spa_config_txg
) == 0);
5084 VERIFY(nvlist_add_uint64(config
, ZPOOL_CONFIG_POOL_GUID
,
5085 spa_generate_guid(NULL
)) == 0);
5086 (void) nvlist_lookup_string(props
,
5087 zpool_prop_to_name(ZPOOL_PROP_ALTROOT
), &altroot
);
5089 /* add the new pool to the namespace */
5090 newspa
= spa_add(newname
, config
, altroot
);
5091 newspa
->spa_config_txg
= spa
->spa_config_txg
;
5092 spa_set_log_state(newspa
, SPA_LOG_CLEAR
);
5094 /* release the spa config lock, retaining the namespace lock */
5095 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5097 if (zio_injection_enabled
)
5098 zio_handle_panic_injection(spa
, FTAG
, 1);
5100 spa_activate(newspa
, spa_mode_global
);
5101 spa_async_suspend(newspa
);
5103 /* create the new pool from the disks of the original pool */
5104 error
= spa_load(newspa
, SPA_LOAD_IMPORT
, SPA_IMPORT_ASSEMBLE
, B_TRUE
);
5108 /* if that worked, generate a real config for the new pool */
5109 if (newspa
->spa_root_vdev
!= NULL
) {
5110 VERIFY(nvlist_alloc(&newspa
->spa_config_splitting
,
5111 NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
5112 VERIFY(nvlist_add_uint64(newspa
->spa_config_splitting
,
5113 ZPOOL_CONFIG_SPLIT_GUID
, spa_guid(spa
)) == 0);
5114 spa_config_set(newspa
, spa_config_generate(newspa
, NULL
, -1ULL,
5119 if (props
!= NULL
) {
5120 spa_configfile_set(newspa
, props
, B_FALSE
);
5121 error
= spa_prop_set(newspa
, props
);
5126 /* flush everything */
5127 txg
= spa_vdev_config_enter(newspa
);
5128 vdev_config_dirty(newspa
->spa_root_vdev
);
5129 (void) spa_vdev_config_exit(newspa
, NULL
, txg
, 0, FTAG
);
5131 if (zio_injection_enabled
)
5132 zio_handle_panic_injection(spa
, FTAG
, 2);
5134 spa_async_resume(newspa
);
5136 /* finally, update the original pool's config */
5137 txg
= spa_vdev_config_enter(spa
);
5138 tx
= dmu_tx_create_dd(spa_get_dsl(spa
)->dp_mos_dir
);
5139 error
= dmu_tx_assign(tx
, TXG_WAIT
);
5142 for (c
= 0; c
< children
; c
++) {
5143 if (vml
[c
] != NULL
) {
5146 spa_history_log_internal(spa
, "detach", tx
,
5147 "vdev=%s", vml
[c
]->vdev_path
);
5151 vdev_config_dirty(spa
->spa_root_vdev
);
5152 spa
->spa_config_splitting
= NULL
;
5156 (void) spa_vdev_exit(spa
, NULL
, txg
, 0);
5158 if (zio_injection_enabled
)
5159 zio_handle_panic_injection(spa
, FTAG
, 3);
5161 /* split is complete; log a history record */
5162 spa_history_log_internal(newspa
, "split", NULL
,
5163 "from pool %s", spa_name(spa
));
5165 kmem_free(vml
, children
* sizeof (vdev_t
*));
5167 /* if we're not going to mount the filesystems in userland, export */
5169 error
= spa_export_common(newname
, POOL_STATE_EXPORTED
, NULL
,
5176 spa_deactivate(newspa
);
5179 txg
= spa_vdev_config_enter(spa
);
5181 /* re-online all offlined disks */
5182 for (c
= 0; c
< children
; c
++) {
5184 vml
[c
]->vdev_offline
= B_FALSE
;
5186 vdev_reopen(spa
->spa_root_vdev
);
5188 nvlist_free(spa
->spa_config_splitting
);
5189 spa
->spa_config_splitting
= NULL
;
5190 (void) spa_vdev_exit(spa
, NULL
, txg
, error
);
5192 kmem_free(vml
, children
* sizeof (vdev_t
*));
5197 spa_nvlist_lookup_by_guid(nvlist_t
**nvpp
, int count
, uint64_t target_guid
)
5201 for (i
= 0; i
< count
; i
++) {
5204 VERIFY(nvlist_lookup_uint64(nvpp
[i
], ZPOOL_CONFIG_GUID
,
5207 if (guid
== target_guid
)
5215 spa_vdev_remove_aux(nvlist_t
*config
, char *name
, nvlist_t
**dev
, int count
,
5216 nvlist_t
*dev_to_remove
)
5218 nvlist_t
**newdev
= NULL
;
5222 newdev
= kmem_alloc((count
- 1) * sizeof (void *), KM_PUSHPAGE
);
5224 for (i
= 0, j
= 0; i
< count
; i
++) {
5225 if (dev
[i
] == dev_to_remove
)
5227 VERIFY(nvlist_dup(dev
[i
], &newdev
[j
++], KM_PUSHPAGE
) == 0);
5230 VERIFY(nvlist_remove(config
, name
, DATA_TYPE_NVLIST_ARRAY
) == 0);
5231 VERIFY(nvlist_add_nvlist_array(config
, name
, newdev
, count
- 1) == 0);
5233 for (i
= 0; i
< count
- 1; i
++)
5234 nvlist_free(newdev
[i
]);
5237 kmem_free(newdev
, (count
- 1) * sizeof (void *));
5241 * Evacuate the device.
5244 spa_vdev_remove_evacuate(spa_t
*spa
, vdev_t
*vd
)
5249 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5250 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5251 ASSERT(vd
== vd
->vdev_top
);
5254 * Evacuate the device. We don't hold the config lock as writer
5255 * since we need to do I/O but we do keep the
5256 * spa_namespace_lock held. Once this completes the device
5257 * should no longer have any blocks allocated on it.
5259 if (vd
->vdev_islog
) {
5260 if (vd
->vdev_stat
.vs_alloc
!= 0)
5261 error
= spa_offline_log(spa
);
5263 error
= SET_ERROR(ENOTSUP
);
5270 * The evacuation succeeded. Remove any remaining MOS metadata
5271 * associated with this vdev, and wait for these changes to sync.
5273 ASSERT0(vd
->vdev_stat
.vs_alloc
);
5274 txg
= spa_vdev_config_enter(spa
);
5275 vd
->vdev_removing
= B_TRUE
;
5276 vdev_dirty_leaves(vd
, VDD_DTL
, txg
);
5277 vdev_config_dirty(vd
);
5278 spa_vdev_config_exit(spa
, NULL
, txg
, 0, FTAG
);
5284 * Complete the removal by cleaning up the namespace.
5287 spa_vdev_remove_from_namespace(spa_t
*spa
, vdev_t
*vd
)
5289 vdev_t
*rvd
= spa
->spa_root_vdev
;
5290 uint64_t id
= vd
->vdev_id
;
5291 boolean_t last_vdev
= (id
== (rvd
->vdev_children
- 1));
5293 ASSERT(MUTEX_HELD(&spa_namespace_lock
));
5294 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == SCL_ALL
);
5295 ASSERT(vd
== vd
->vdev_top
);
5298 * Only remove any devices which are empty.
5300 if (vd
->vdev_stat
.vs_alloc
!= 0)
5303 (void) vdev_label_init(vd
, 0, VDEV_LABEL_REMOVE
);
5305 if (list_link_active(&vd
->vdev_state_dirty_node
))
5306 vdev_state_clean(vd
);
5307 if (list_link_active(&vd
->vdev_config_dirty_node
))
5308 vdev_config_clean(vd
);
5313 vdev_compact_children(rvd
);
5315 vd
= vdev_alloc_common(spa
, id
, 0, &vdev_hole_ops
);
5316 vdev_add_child(rvd
, vd
);
5318 vdev_config_dirty(rvd
);
5321 * Reassess the health of our root vdev.
5327 * Remove a device from the pool -
5329 * Removing a device from the vdev namespace requires several steps
5330 * and can take a significant amount of time. As a result we use
5331 * the spa_vdev_config_[enter/exit] functions which allow us to
5332 * grab and release the spa_config_lock while still holding the namespace
5333 * lock. During each step the configuration is synced out.
5335 * Currently, this supports removing only hot spares, slogs, and level 2 ARC
5339 spa_vdev_remove(spa_t
*spa
, uint64_t guid
, boolean_t unspare
)
5342 metaslab_group_t
*mg
;
5343 nvlist_t
**spares
, **l2cache
, *nv
;
5345 uint_t nspares
, nl2cache
;
5347 boolean_t locked
= MUTEX_HELD(&spa_namespace_lock
);
5349 ASSERT(spa_writeable(spa
));
5352 txg
= spa_vdev_enter(spa
);
5354 vd
= spa_lookup_by_guid(spa
, guid
, B_FALSE
);
5356 if (spa
->spa_spares
.sav_vdevs
!= NULL
&&
5357 nvlist_lookup_nvlist_array(spa
->spa_spares
.sav_config
,
5358 ZPOOL_CONFIG_SPARES
, &spares
, &nspares
) == 0 &&
5359 (nv
= spa_nvlist_lookup_by_guid(spares
, nspares
, guid
)) != NULL
) {
5361 * Only remove the hot spare if it's not currently in use
5364 if (vd
== NULL
|| unspare
) {
5365 spa_vdev_remove_aux(spa
->spa_spares
.sav_config
,
5366 ZPOOL_CONFIG_SPARES
, spares
, nspares
, nv
);
5367 spa_load_spares(spa
);
5368 spa
->spa_spares
.sav_sync
= B_TRUE
;
5370 error
= SET_ERROR(EBUSY
);
5372 } else if (spa
->spa_l2cache
.sav_vdevs
!= NULL
&&
5373 nvlist_lookup_nvlist_array(spa
->spa_l2cache
.sav_config
,
5374 ZPOOL_CONFIG_L2CACHE
, &l2cache
, &nl2cache
) == 0 &&
5375 (nv
= spa_nvlist_lookup_by_guid(l2cache
, nl2cache
, guid
)) != NULL
) {
5377 * Cache devices can always be removed.
5379 spa_vdev_remove_aux(spa
->spa_l2cache
.sav_config
,
5380 ZPOOL_CONFIG_L2CACHE
, l2cache
, nl2cache
, nv
);
5381 spa_load_l2cache(spa
);
5382 spa
->spa_l2cache
.sav_sync
= B_TRUE
;
5383 } else if (vd
!= NULL
&& vd
->vdev_islog
) {
5385 ASSERT(vd
== vd
->vdev_top
);
5390 * Stop allocating from this vdev.
5392 metaslab_group_passivate(mg
);
5395 * Wait for the youngest allocations and frees to sync,
5396 * and then wait for the deferral of those frees to finish.
5398 spa_vdev_config_exit(spa
, NULL
,
5399 txg
+ TXG_CONCURRENT_STATES
+ TXG_DEFER_SIZE
, 0, FTAG
);
5402 * Attempt to evacuate the vdev.
5404 error
= spa_vdev_remove_evacuate(spa
, vd
);
5406 txg
= spa_vdev_config_enter(spa
);
5409 * If we couldn't evacuate the vdev, unwind.
5412 metaslab_group_activate(mg
);
5413 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5417 * Clean up the vdev namespace.
5419 spa_vdev_remove_from_namespace(spa
, vd
);
5421 } else if (vd
!= NULL
) {
5423 * Normal vdevs cannot be removed (yet).
5425 error
= SET_ERROR(ENOTSUP
);
5428 * There is no vdev of any kind with the specified guid.
5430 error
= SET_ERROR(ENOENT
);
5434 return (spa_vdev_exit(spa
, NULL
, txg
, error
));
5440 * Find any device that's done replacing, or a vdev marked 'unspare' that's
5441 * currently spared, so we can detach it.
5444 spa_vdev_resilver_done_hunt(vdev_t
*vd
)
5446 vdev_t
*newvd
, *oldvd
;
5449 for (c
= 0; c
< vd
->vdev_children
; c
++) {
5450 oldvd
= spa_vdev_resilver_done_hunt(vd
->vdev_child
[c
]);
5456 * Check for a completed replacement. We always consider the first
5457 * vdev in the list to be the oldest vdev, and the last one to be
5458 * the newest (see spa_vdev_attach() for how that works). In
5459 * the case where the newest vdev is faulted, we will not automatically
5460 * remove it after a resilver completes. This is OK as it will require
5461 * user intervention to determine which disk the admin wishes to keep.
5463 if (vd
->vdev_ops
== &vdev_replacing_ops
) {
5464 ASSERT(vd
->vdev_children
> 1);
5466 newvd
= vd
->vdev_child
[vd
->vdev_children
- 1];
5467 oldvd
= vd
->vdev_child
[0];
5469 if (vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5470 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5471 !vdev_dtl_required(oldvd
))
5476 * Check for a completed resilver with the 'unspare' flag set.
5478 if (vd
->vdev_ops
== &vdev_spare_ops
) {
5479 vdev_t
*first
= vd
->vdev_child
[0];
5480 vdev_t
*last
= vd
->vdev_child
[vd
->vdev_children
- 1];
5482 if (last
->vdev_unspare
) {
5485 } else if (first
->vdev_unspare
) {
5492 if (oldvd
!= NULL
&&
5493 vdev_dtl_empty(newvd
, DTL_MISSING
) &&
5494 vdev_dtl_empty(newvd
, DTL_OUTAGE
) &&
5495 !vdev_dtl_required(oldvd
))
5499 * If there are more than two spares attached to a disk,
5500 * and those spares are not required, then we want to
5501 * attempt to free them up now so that they can be used
5502 * by other pools. Once we're back down to a single
5503 * disk+spare, we stop removing them.
5505 if (vd
->vdev_children
> 2) {
5506 newvd
= vd
->vdev_child
[1];
5508 if (newvd
->vdev_isspare
&& last
->vdev_isspare
&&
5509 vdev_dtl_empty(last
, DTL_MISSING
) &&
5510 vdev_dtl_empty(last
, DTL_OUTAGE
) &&
5511 !vdev_dtl_required(newvd
))
5520 spa_vdev_resilver_done(spa_t
*spa
)
5522 vdev_t
*vd
, *pvd
, *ppvd
;
5523 uint64_t guid
, sguid
, pguid
, ppguid
;
5525 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5527 while ((vd
= spa_vdev_resilver_done_hunt(spa
->spa_root_vdev
)) != NULL
) {
5528 pvd
= vd
->vdev_parent
;
5529 ppvd
= pvd
->vdev_parent
;
5530 guid
= vd
->vdev_guid
;
5531 pguid
= pvd
->vdev_guid
;
5532 ppguid
= ppvd
->vdev_guid
;
5535 * If we have just finished replacing a hot spared device, then
5536 * we need to detach the parent's first child (the original hot
5539 if (ppvd
->vdev_ops
== &vdev_spare_ops
&& pvd
->vdev_id
== 0 &&
5540 ppvd
->vdev_children
== 2) {
5541 ASSERT(pvd
->vdev_ops
== &vdev_replacing_ops
);
5542 sguid
= ppvd
->vdev_child
[1]->vdev_guid
;
5544 ASSERT(vd
->vdev_resilver_txg
== 0 || !vdev_dtl_required(vd
));
5546 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5547 if (spa_vdev_detach(spa
, guid
, pguid
, B_TRUE
) != 0)
5549 if (sguid
&& spa_vdev_detach(spa
, sguid
, ppguid
, B_TRUE
) != 0)
5551 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
5554 spa_config_exit(spa
, SCL_ALL
, FTAG
);
5558 * Update the stored path or FRU for this vdev.
5561 spa_vdev_set_common(spa_t
*spa
, uint64_t guid
, const char *value
,
5565 boolean_t sync
= B_FALSE
;
5567 ASSERT(spa_writeable(spa
));
5569 spa_vdev_state_enter(spa
, SCL_ALL
);
5571 if ((vd
= spa_lookup_by_guid(spa
, guid
, B_TRUE
)) == NULL
)
5572 return (spa_vdev_state_exit(spa
, NULL
, ENOENT
));
5574 if (!vd
->vdev_ops
->vdev_op_leaf
)
5575 return (spa_vdev_state_exit(spa
, NULL
, ENOTSUP
));
5578 if (strcmp(value
, vd
->vdev_path
) != 0) {
5579 spa_strfree(vd
->vdev_path
);
5580 vd
->vdev_path
= spa_strdup(value
);
5584 if (vd
->vdev_fru
== NULL
) {
5585 vd
->vdev_fru
= spa_strdup(value
);
5587 } else if (strcmp(value
, vd
->vdev_fru
) != 0) {
5588 spa_strfree(vd
->vdev_fru
);
5589 vd
->vdev_fru
= spa_strdup(value
);
5594 return (spa_vdev_state_exit(spa
, sync
? vd
: NULL
, 0));
5598 spa_vdev_setpath(spa_t
*spa
, uint64_t guid
, const char *newpath
)
5600 return (spa_vdev_set_common(spa
, guid
, newpath
, B_TRUE
));
5604 spa_vdev_setfru(spa_t
*spa
, uint64_t guid
, const char *newfru
)
5606 return (spa_vdev_set_common(spa
, guid
, newfru
, B_FALSE
));
5610 * ==========================================================================
5612 * ==========================================================================
5616 spa_scan_stop(spa_t
*spa
)
5618 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5619 if (dsl_scan_resilvering(spa
->spa_dsl_pool
))
5620 return (SET_ERROR(EBUSY
));
5621 return (dsl_scan_cancel(spa
->spa_dsl_pool
));
5625 spa_scan(spa_t
*spa
, pool_scan_func_t func
)
5627 ASSERT(spa_config_held(spa
, SCL_ALL
, RW_WRITER
) == 0);
5629 if (func
>= POOL_SCAN_FUNCS
|| func
== POOL_SCAN_NONE
)
5630 return (SET_ERROR(ENOTSUP
));
5633 * If a resilver was requested, but there is no DTL on a
5634 * writeable leaf device, we have nothing to do.
5636 if (func
== POOL_SCAN_RESILVER
&&
5637 !vdev_resilver_needed(spa
->spa_root_vdev
, NULL
, NULL
)) {
5638 spa_async_request(spa
, SPA_ASYNC_RESILVER_DONE
);
5642 return (dsl_scan(spa
->spa_dsl_pool
, func
));
5646 * ==========================================================================
5647 * SPA async task processing
5648 * ==========================================================================
5652 spa_async_remove(spa_t
*spa
, vdev_t
*vd
)
5656 if (vd
->vdev_remove_wanted
) {
5657 vd
->vdev_remove_wanted
= B_FALSE
;
5658 vd
->vdev_delayed_close
= B_FALSE
;
5659 vdev_set_state(vd
, B_FALSE
, VDEV_STATE_REMOVED
, VDEV_AUX_NONE
);
5662 * We want to clear the stats, but we don't want to do a full
5663 * vdev_clear() as that will cause us to throw away
5664 * degraded/faulted state as well as attempt to reopen the
5665 * device, all of which is a waste.
5667 vd
->vdev_stat
.vs_read_errors
= 0;
5668 vd
->vdev_stat
.vs_write_errors
= 0;
5669 vd
->vdev_stat
.vs_checksum_errors
= 0;
5671 vdev_state_dirty(vd
->vdev_top
);
5674 for (c
= 0; c
< vd
->vdev_children
; c
++)
5675 spa_async_remove(spa
, vd
->vdev_child
[c
]);
5679 spa_async_probe(spa_t
*spa
, vdev_t
*vd
)
5683 if (vd
->vdev_probe_wanted
) {
5684 vd
->vdev_probe_wanted
= B_FALSE
;
5685 vdev_reopen(vd
); /* vdev_open() does the actual probe */
5688 for (c
= 0; c
< vd
->vdev_children
; c
++)
5689 spa_async_probe(spa
, vd
->vdev_child
[c
]);
5693 spa_async_autoexpand(spa_t
*spa
, vdev_t
*vd
)
5697 if (!spa
->spa_autoexpand
)
5700 for (c
= 0; c
< vd
->vdev_children
; c
++) {
5701 vdev_t
*cvd
= vd
->vdev_child
[c
];
5702 spa_async_autoexpand(spa
, cvd
);
5705 if (!vd
->vdev_ops
->vdev_op_leaf
|| vd
->vdev_physpath
== NULL
)
5708 spa_event_notify(vd
->vdev_spa
, vd
, FM_EREPORT_ZFS_DEVICE_AUTOEXPAND
);
5712 spa_async_thread(spa_t
*spa
)
5716 ASSERT(spa
->spa_sync_on
);
5718 mutex_enter(&spa
->spa_async_lock
);
5719 tasks
= spa
->spa_async_tasks
;
5720 spa
->spa_async_tasks
= 0;
5721 mutex_exit(&spa
->spa_async_lock
);
5724 * See if the config needs to be updated.
5726 if (tasks
& SPA_ASYNC_CONFIG_UPDATE
) {
5727 uint64_t old_space
, new_space
;
5729 mutex_enter(&spa_namespace_lock
);
5730 old_space
= metaslab_class_get_space(spa_normal_class(spa
));
5731 spa_config_update(spa
, SPA_CONFIG_UPDATE_POOL
);
5732 new_space
= metaslab_class_get_space(spa_normal_class(spa
));
5733 mutex_exit(&spa_namespace_lock
);
5736 * If the pool grew as a result of the config update,
5737 * then log an internal history event.
5739 if (new_space
!= old_space
) {
5740 spa_history_log_internal(spa
, "vdev online", NULL
,
5741 "pool '%s' size: %llu(+%llu)",
5742 spa_name(spa
), new_space
, new_space
- old_space
);
5747 * See if any devices need to be marked REMOVED.
5749 if (tasks
& SPA_ASYNC_REMOVE
) {
5750 spa_vdev_state_enter(spa
, SCL_NONE
);
5751 spa_async_remove(spa
, spa
->spa_root_vdev
);
5752 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++)
5753 spa_async_remove(spa
, spa
->spa_l2cache
.sav_vdevs
[i
]);
5754 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++)
5755 spa_async_remove(spa
, spa
->spa_spares
.sav_vdevs
[i
]);
5756 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5759 if ((tasks
& SPA_ASYNC_AUTOEXPAND
) && !spa_suspended(spa
)) {
5760 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
5761 spa_async_autoexpand(spa
, spa
->spa_root_vdev
);
5762 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
5766 * See if any devices need to be probed.
5768 if (tasks
& SPA_ASYNC_PROBE
) {
5769 spa_vdev_state_enter(spa
, SCL_NONE
);
5770 spa_async_probe(spa
, spa
->spa_root_vdev
);
5771 (void) spa_vdev_state_exit(spa
, NULL
, 0);
5775 * If any devices are done replacing, detach them.
5777 if (tasks
& SPA_ASYNC_RESILVER_DONE
)
5778 spa_vdev_resilver_done(spa
);
5781 * Kick off a resilver.
5783 if (tasks
& SPA_ASYNC_RESILVER
)
5784 dsl_resilver_restart(spa
->spa_dsl_pool
, 0);
5787 * Let the world know that we're done.
5789 mutex_enter(&spa
->spa_async_lock
);
5790 spa
->spa_async_thread
= NULL
;
5791 cv_broadcast(&spa
->spa_async_cv
);
5792 mutex_exit(&spa
->spa_async_lock
);
5797 spa_async_suspend(spa_t
*spa
)
5799 mutex_enter(&spa
->spa_async_lock
);
5800 spa
->spa_async_suspended
++;
5801 while (spa
->spa_async_thread
!= NULL
)
5802 cv_wait(&spa
->spa_async_cv
, &spa
->spa_async_lock
);
5803 mutex_exit(&spa
->spa_async_lock
);
5807 spa_async_resume(spa_t
*spa
)
5809 mutex_enter(&spa
->spa_async_lock
);
5810 ASSERT(spa
->spa_async_suspended
!= 0);
5811 spa
->spa_async_suspended
--;
5812 mutex_exit(&spa
->spa_async_lock
);
5816 spa_async_dispatch(spa_t
*spa
)
5818 mutex_enter(&spa
->spa_async_lock
);
5819 if (spa
->spa_async_tasks
&& !spa
->spa_async_suspended
&&
5820 spa
->spa_async_thread
== NULL
&&
5821 rootdir
!= NULL
&& !vn_is_readonly(rootdir
))
5822 spa
->spa_async_thread
= thread_create(NULL
, 0,
5823 spa_async_thread
, spa
, 0, &p0
, TS_RUN
, maxclsyspri
);
5824 mutex_exit(&spa
->spa_async_lock
);
5828 spa_async_request(spa_t
*spa
, int task
)
5830 zfs_dbgmsg("spa=%s async request task=%u", spa
->spa_name
, task
);
5831 mutex_enter(&spa
->spa_async_lock
);
5832 spa
->spa_async_tasks
|= task
;
5833 mutex_exit(&spa
->spa_async_lock
);
5837 * ==========================================================================
5838 * SPA syncing routines
5839 * ==========================================================================
5843 bpobj_enqueue_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
5846 bpobj_enqueue(bpo
, bp
, tx
);
5851 spa_free_sync_cb(void *arg
, const blkptr_t
*bp
, dmu_tx_t
*tx
)
5855 zio_nowait(zio_free_sync(zio
, zio
->io_spa
, dmu_tx_get_txg(tx
), bp
,
5861 * Note: this simple function is not inlined to make it easier to dtrace the
5862 * amount of time spent syncing frees.
5865 spa_sync_frees(spa_t
*spa
, bplist_t
*bpl
, dmu_tx_t
*tx
)
5867 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
5868 bplist_iterate(bpl
, spa_free_sync_cb
, zio
, tx
);
5869 VERIFY(zio_wait(zio
) == 0);
5873 * Note: this simple function is not inlined to make it easier to dtrace the
5874 * amount of time spent syncing deferred frees.
5877 spa_sync_deferred_frees(spa_t
*spa
, dmu_tx_t
*tx
)
5879 zio_t
*zio
= zio_root(spa
, NULL
, NULL
, 0);
5880 VERIFY3U(bpobj_iterate(&spa
->spa_deferred_bpobj
,
5881 spa_free_sync_cb
, zio
, tx
), ==, 0);
5882 VERIFY0(zio_wait(zio
));
5886 spa_sync_nvlist(spa_t
*spa
, uint64_t obj
, nvlist_t
*nv
, dmu_tx_t
*tx
)
5888 char *packed
= NULL
;
5893 VERIFY(nvlist_size(nv
, &nvsize
, NV_ENCODE_XDR
) == 0);
5896 * Write full (SPA_CONFIG_BLOCKSIZE) blocks of configuration
5897 * information. This avoids the dmu_buf_will_dirty() path and
5898 * saves us a pre-read to get data we don't actually care about.
5900 bufsize
= P2ROUNDUP((uint64_t)nvsize
, SPA_CONFIG_BLOCKSIZE
);
5901 packed
= vmem_alloc(bufsize
, KM_PUSHPAGE
);
5903 VERIFY(nvlist_pack(nv
, &packed
, &nvsize
, NV_ENCODE_XDR
,
5905 bzero(packed
+ nvsize
, bufsize
- nvsize
);
5907 dmu_write(spa
->spa_meta_objset
, obj
, 0, bufsize
, packed
, tx
);
5909 vmem_free(packed
, bufsize
);
5911 VERIFY(0 == dmu_bonus_hold(spa
->spa_meta_objset
, obj
, FTAG
, &db
));
5912 dmu_buf_will_dirty(db
, tx
);
5913 *(uint64_t *)db
->db_data
= nvsize
;
5914 dmu_buf_rele(db
, FTAG
);
5918 spa_sync_aux_dev(spa_t
*spa
, spa_aux_vdev_t
*sav
, dmu_tx_t
*tx
,
5919 const char *config
, const char *entry
)
5929 * Update the MOS nvlist describing the list of available devices.
5930 * spa_validate_aux() will have already made sure this nvlist is
5931 * valid and the vdevs are labeled appropriately.
5933 if (sav
->sav_object
== 0) {
5934 sav
->sav_object
= dmu_object_alloc(spa
->spa_meta_objset
,
5935 DMU_OT_PACKED_NVLIST
, 1 << 14, DMU_OT_PACKED_NVLIST_SIZE
,
5936 sizeof (uint64_t), tx
);
5937 VERIFY(zap_update(spa
->spa_meta_objset
,
5938 DMU_POOL_DIRECTORY_OBJECT
, entry
, sizeof (uint64_t), 1,
5939 &sav
->sav_object
, tx
) == 0);
5942 VERIFY(nvlist_alloc(&nvroot
, NV_UNIQUE_NAME
, KM_PUSHPAGE
) == 0);
5943 if (sav
->sav_count
== 0) {
5944 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, NULL
, 0) == 0);
5946 list
= kmem_alloc(sav
->sav_count
*sizeof (void *), KM_PUSHPAGE
);
5947 for (i
= 0; i
< sav
->sav_count
; i
++)
5948 list
[i
] = vdev_config_generate(spa
, sav
->sav_vdevs
[i
],
5949 B_FALSE
, VDEV_CONFIG_L2CACHE
);
5950 VERIFY(nvlist_add_nvlist_array(nvroot
, config
, list
,
5951 sav
->sav_count
) == 0);
5952 for (i
= 0; i
< sav
->sav_count
; i
++)
5953 nvlist_free(list
[i
]);
5954 kmem_free(list
, sav
->sav_count
* sizeof (void *));
5957 spa_sync_nvlist(spa
, sav
->sav_object
, nvroot
, tx
);
5958 nvlist_free(nvroot
);
5960 sav
->sav_sync
= B_FALSE
;
5964 spa_sync_config_object(spa_t
*spa
, dmu_tx_t
*tx
)
5968 if (list_is_empty(&spa
->spa_config_dirty_list
))
5971 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
5973 config
= spa_config_generate(spa
, spa
->spa_root_vdev
,
5974 dmu_tx_get_txg(tx
), B_FALSE
);
5977 * If we're upgrading the spa version then make sure that
5978 * the config object gets updated with the correct version.
5980 if (spa
->spa_ubsync
.ub_version
< spa
->spa_uberblock
.ub_version
)
5981 fnvlist_add_uint64(config
, ZPOOL_CONFIG_VERSION
,
5982 spa
->spa_uberblock
.ub_version
);
5984 spa_config_exit(spa
, SCL_STATE
, FTAG
);
5986 if (spa
->spa_config_syncing
)
5987 nvlist_free(spa
->spa_config_syncing
);
5988 spa
->spa_config_syncing
= config
;
5990 spa_sync_nvlist(spa
, spa
->spa_config_object
, config
, tx
);
5994 spa_sync_version(void *arg
, dmu_tx_t
*tx
)
5996 uint64_t *versionp
= arg
;
5997 uint64_t version
= *versionp
;
5998 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6001 * Setting the version is special cased when first creating the pool.
6003 ASSERT(tx
->tx_txg
!= TXG_INITIAL
);
6005 ASSERT(SPA_VERSION_IS_SUPPORTED(version
));
6006 ASSERT(version
>= spa_version(spa
));
6008 spa
->spa_uberblock
.ub_version
= version
;
6009 vdev_config_dirty(spa
->spa_root_vdev
);
6010 spa_history_log_internal(spa
, "set", tx
, "version=%lld", version
);
6014 * Set zpool properties.
6017 spa_sync_props(void *arg
, dmu_tx_t
*tx
)
6019 nvlist_t
*nvp
= arg
;
6020 spa_t
*spa
= dmu_tx_pool(tx
)->dp_spa
;
6021 objset_t
*mos
= spa
->spa_meta_objset
;
6022 nvpair_t
*elem
= NULL
;
6024 mutex_enter(&spa
->spa_props_lock
);
6026 while ((elem
= nvlist_next_nvpair(nvp
, elem
))) {
6028 char *strval
, *fname
;
6030 const char *propname
;
6031 zprop_type_t proptype
;
6034 prop
= zpool_name_to_prop(nvpair_name(elem
));
6035 switch ((int)prop
) {
6038 * We checked this earlier in spa_prop_validate().
6040 ASSERT(zpool_prop_feature(nvpair_name(elem
)));
6042 fname
= strchr(nvpair_name(elem
), '@') + 1;
6043 VERIFY0(zfeature_lookup_name(fname
, &fid
));
6045 spa_feature_enable(spa
, fid
, tx
);
6046 spa_history_log_internal(spa
, "set", tx
,
6047 "%s=enabled", nvpair_name(elem
));
6050 case ZPOOL_PROP_VERSION
:
6051 intval
= fnvpair_value_uint64(elem
);
6053 * The version is synced seperatly before other
6054 * properties and should be correct by now.
6056 ASSERT3U(spa_version(spa
), >=, intval
);
6059 case ZPOOL_PROP_ALTROOT
:
6061 * 'altroot' is a non-persistent property. It should
6062 * have been set temporarily at creation or import time.
6064 ASSERT(spa
->spa_root
!= NULL
);
6067 case ZPOOL_PROP_READONLY
:
6068 case ZPOOL_PROP_CACHEFILE
:
6070 * 'readonly' and 'cachefile' are also non-persisitent
6074 case ZPOOL_PROP_COMMENT
:
6075 strval
= fnvpair_value_string(elem
);
6076 if (spa
->spa_comment
!= NULL
)
6077 spa_strfree(spa
->spa_comment
);
6078 spa
->spa_comment
= spa_strdup(strval
);
6080 * We need to dirty the configuration on all the vdevs
6081 * so that their labels get updated. It's unnecessary
6082 * to do this for pool creation since the vdev's
6083 * configuratoin has already been dirtied.
6085 if (tx
->tx_txg
!= TXG_INITIAL
)
6086 vdev_config_dirty(spa
->spa_root_vdev
);
6087 spa_history_log_internal(spa
, "set", tx
,
6088 "%s=%s", nvpair_name(elem
), strval
);
6092 * Set pool property values in the poolprops mos object.
6094 if (spa
->spa_pool_props_object
== 0) {
6095 spa
->spa_pool_props_object
=
6096 zap_create_link(mos
, DMU_OT_POOL_PROPS
,
6097 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_PROPS
,
6101 /* normalize the property name */
6102 propname
= zpool_prop_to_name(prop
);
6103 proptype
= zpool_prop_get_type(prop
);
6105 if (nvpair_type(elem
) == DATA_TYPE_STRING
) {
6106 ASSERT(proptype
== PROP_TYPE_STRING
);
6107 strval
= fnvpair_value_string(elem
);
6108 VERIFY0(zap_update(mos
,
6109 spa
->spa_pool_props_object
, propname
,
6110 1, strlen(strval
) + 1, strval
, tx
));
6111 spa_history_log_internal(spa
, "set", tx
,
6112 "%s=%s", nvpair_name(elem
), strval
);
6113 } else if (nvpair_type(elem
) == DATA_TYPE_UINT64
) {
6114 intval
= fnvpair_value_uint64(elem
);
6116 if (proptype
== PROP_TYPE_INDEX
) {
6118 VERIFY0(zpool_prop_index_to_string(
6119 prop
, intval
, &unused
));
6121 VERIFY0(zap_update(mos
,
6122 spa
->spa_pool_props_object
, propname
,
6123 8, 1, &intval
, tx
));
6124 spa_history_log_internal(spa
, "set", tx
,
6125 "%s=%lld", nvpair_name(elem
), intval
);
6127 ASSERT(0); /* not allowed */
6131 case ZPOOL_PROP_DELEGATION
:
6132 spa
->spa_delegation
= intval
;
6134 case ZPOOL_PROP_BOOTFS
:
6135 spa
->spa_bootfs
= intval
;
6137 case ZPOOL_PROP_FAILUREMODE
:
6138 spa
->spa_failmode
= intval
;
6140 case ZPOOL_PROP_AUTOEXPAND
:
6141 spa
->spa_autoexpand
= intval
;
6142 if (tx
->tx_txg
!= TXG_INITIAL
)
6143 spa_async_request(spa
,
6144 SPA_ASYNC_AUTOEXPAND
);
6146 case ZPOOL_PROP_DEDUPDITTO
:
6147 spa
->spa_dedup_ditto
= intval
;
6156 mutex_exit(&spa
->spa_props_lock
);
6160 * Perform one-time upgrade on-disk changes. spa_version() does not
6161 * reflect the new version this txg, so there must be no changes this
6162 * txg to anything that the upgrade code depends on after it executes.
6163 * Therefore this must be called after dsl_pool_sync() does the sync
6167 spa_sync_upgrades(spa_t
*spa
, dmu_tx_t
*tx
)
6169 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6171 ASSERT(spa
->spa_sync_pass
== 1);
6173 rrw_enter(&dp
->dp_config_rwlock
, RW_WRITER
, FTAG
);
6175 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_ORIGIN
&&
6176 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_ORIGIN
) {
6177 dsl_pool_create_origin(dp
, tx
);
6179 /* Keeping the origin open increases spa_minref */
6180 spa
->spa_minref
+= 3;
6183 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_NEXT_CLONES
&&
6184 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_NEXT_CLONES
) {
6185 dsl_pool_upgrade_clones(dp
, tx
);
6188 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_DIR_CLONES
&&
6189 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_DIR_CLONES
) {
6190 dsl_pool_upgrade_dir_clones(dp
, tx
);
6192 /* Keeping the freedir open increases spa_minref */
6193 spa
->spa_minref
+= 3;
6196 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_FEATURES
&&
6197 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_FEATURES
) {
6198 spa_feature_create_zap_objects(spa
, tx
);
6200 rrw_exit(&dp
->dp_config_rwlock
, FTAG
);
6204 * Sync the specified transaction group. New blocks may be dirtied as
6205 * part of the process, so we iterate until it converges.
6208 spa_sync(spa_t
*spa
, uint64_t txg
)
6210 dsl_pool_t
*dp
= spa
->spa_dsl_pool
;
6211 objset_t
*mos
= spa
->spa_meta_objset
;
6212 bplist_t
*free_bpl
= &spa
->spa_free_bplist
[txg
& TXG_MASK
];
6213 vdev_t
*rvd
= spa
->spa_root_vdev
;
6219 VERIFY(spa_writeable(spa
));
6222 * Lock out configuration changes.
6224 spa_config_enter(spa
, SCL_CONFIG
, FTAG
, RW_READER
);
6226 spa
->spa_syncing_txg
= txg
;
6227 spa
->spa_sync_pass
= 0;
6230 * If there are any pending vdev state changes, convert them
6231 * into config changes that go out with this transaction group.
6233 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6234 while (list_head(&spa
->spa_state_dirty_list
) != NULL
) {
6236 * We need the write lock here because, for aux vdevs,
6237 * calling vdev_config_dirty() modifies sav_config.
6238 * This is ugly and will become unnecessary when we
6239 * eliminate the aux vdev wart by integrating all vdevs
6240 * into the root vdev tree.
6242 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6243 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_WRITER
);
6244 while ((vd
= list_head(&spa
->spa_state_dirty_list
)) != NULL
) {
6245 vdev_state_clean(vd
);
6246 vdev_config_dirty(vd
);
6248 spa_config_exit(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
);
6249 spa_config_enter(spa
, SCL_CONFIG
| SCL_STATE
, FTAG
, RW_READER
);
6251 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6253 tx
= dmu_tx_create_assigned(dp
, txg
);
6255 spa
->spa_sync_starttime
= gethrtime();
6256 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
6257 spa
->spa_deadman_tqid
= taskq_dispatch_delay(system_taskq
,
6258 spa_deadman
, spa
, TQ_PUSHPAGE
, ddi_get_lbolt() +
6259 NSEC_TO_TICK(spa
->spa_deadman_synctime
));
6262 * If we are upgrading to SPA_VERSION_RAIDZ_DEFLATE this txg,
6263 * set spa_deflate if we have no raid-z vdevs.
6265 if (spa
->spa_ubsync
.ub_version
< SPA_VERSION_RAIDZ_DEFLATE
&&
6266 spa
->spa_uberblock
.ub_version
>= SPA_VERSION_RAIDZ_DEFLATE
) {
6269 for (i
= 0; i
< rvd
->vdev_children
; i
++) {
6270 vd
= rvd
->vdev_child
[i
];
6271 if (vd
->vdev_deflate_ratio
!= SPA_MINBLOCKSIZE
)
6274 if (i
== rvd
->vdev_children
) {
6275 spa
->spa_deflate
= TRUE
;
6276 VERIFY(0 == zap_add(spa
->spa_meta_objset
,
6277 DMU_POOL_DIRECTORY_OBJECT
, DMU_POOL_DEFLATE
,
6278 sizeof (uint64_t), 1, &spa
->spa_deflate
, tx
));
6283 * If anything has changed in this txg, or if someone is waiting
6284 * for this txg to sync (eg, spa_vdev_remove()), push the
6285 * deferred frees from the previous txg. If not, leave them
6286 * alone so that we don't generate work on an otherwise idle
6289 if (!txg_list_empty(&dp
->dp_dirty_datasets
, txg
) ||
6290 !txg_list_empty(&dp
->dp_dirty_dirs
, txg
) ||
6291 !txg_list_empty(&dp
->dp_sync_tasks
, txg
) ||
6292 ((dsl_scan_active(dp
->dp_scan
) ||
6293 txg_sync_waiting(dp
)) && !spa_shutting_down(spa
))) {
6294 spa_sync_deferred_frees(spa
, tx
);
6298 * Iterate to convergence.
6301 int pass
= ++spa
->spa_sync_pass
;
6303 spa_sync_config_object(spa
, tx
);
6304 spa_sync_aux_dev(spa
, &spa
->spa_spares
, tx
,
6305 ZPOOL_CONFIG_SPARES
, DMU_POOL_SPARES
);
6306 spa_sync_aux_dev(spa
, &spa
->spa_l2cache
, tx
,
6307 ZPOOL_CONFIG_L2CACHE
, DMU_POOL_L2CACHE
);
6308 spa_errlog_sync(spa
, txg
);
6309 dsl_pool_sync(dp
, txg
);
6311 if (pass
< zfs_sync_pass_deferred_free
) {
6312 spa_sync_frees(spa
, free_bpl
, tx
);
6314 bplist_iterate(free_bpl
, bpobj_enqueue_cb
,
6315 &spa
->spa_deferred_bpobj
, tx
);
6319 dsl_scan_sync(dp
, tx
);
6321 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, txg
)))
6325 spa_sync_upgrades(spa
, tx
);
6327 } while (dmu_objset_is_dirty(mos
, txg
));
6330 * Rewrite the vdev configuration (which includes the uberblock)
6331 * to commit the transaction group.
6333 * If there are no dirty vdevs, we sync the uberblock to a few
6334 * random top-level vdevs that are known to be visible in the
6335 * config cache (see spa_vdev_add() for a complete description).
6336 * If there *are* dirty vdevs, sync the uberblock to all vdevs.
6340 * We hold SCL_STATE to prevent vdev open/close/etc.
6341 * while we're attempting to write the vdev labels.
6343 spa_config_enter(spa
, SCL_STATE
, FTAG
, RW_READER
);
6345 if (list_is_empty(&spa
->spa_config_dirty_list
)) {
6346 vdev_t
*svd
[SPA_DVAS_PER_BP
];
6348 int children
= rvd
->vdev_children
;
6349 int c0
= spa_get_random(children
);
6351 for (c
= 0; c
< children
; c
++) {
6352 vd
= rvd
->vdev_child
[(c0
+ c
) % children
];
6353 if (vd
->vdev_ms_array
== 0 || vd
->vdev_islog
)
6355 svd
[svdcount
++] = vd
;
6356 if (svdcount
== SPA_DVAS_PER_BP
)
6359 error
= vdev_config_sync(svd
, svdcount
, txg
, B_FALSE
);
6361 error
= vdev_config_sync(svd
, svdcount
, txg
,
6364 error
= vdev_config_sync(rvd
->vdev_child
,
6365 rvd
->vdev_children
, txg
, B_FALSE
);
6367 error
= vdev_config_sync(rvd
->vdev_child
,
6368 rvd
->vdev_children
, txg
, B_TRUE
);
6372 spa
->spa_last_synced_guid
= rvd
->vdev_guid
;
6374 spa_config_exit(spa
, SCL_STATE
, FTAG
);
6378 zio_suspend(spa
, NULL
);
6379 zio_resume_wait(spa
);
6383 taskq_cancel_id(system_taskq
, spa
->spa_deadman_tqid
);
6384 spa
->spa_deadman_tqid
= 0;
6387 * Clear the dirty config list.
6389 while ((vd
= list_head(&spa
->spa_config_dirty_list
)) != NULL
)
6390 vdev_config_clean(vd
);
6393 * Now that the new config has synced transactionally,
6394 * let it become visible to the config cache.
6396 if (spa
->spa_config_syncing
!= NULL
) {
6397 spa_config_set(spa
, spa
->spa_config_syncing
);
6398 spa
->spa_config_txg
= txg
;
6399 spa
->spa_config_syncing
= NULL
;
6402 spa
->spa_ubsync
= spa
->spa_uberblock
;
6404 dsl_pool_sync_done(dp
, txg
);
6407 * Update usable space statistics.
6409 while ((vd
= txg_list_remove(&spa
->spa_vdev_txg_list
, TXG_CLEAN(txg
))))
6410 vdev_sync_done(vd
, txg
);
6412 spa_update_dspace(spa
);
6415 * It had better be the case that we didn't dirty anything
6416 * since vdev_config_sync().
6418 ASSERT(txg_list_empty(&dp
->dp_dirty_datasets
, txg
));
6419 ASSERT(txg_list_empty(&dp
->dp_dirty_dirs
, txg
));
6420 ASSERT(txg_list_empty(&spa
->spa_vdev_txg_list
, txg
));
6422 spa
->spa_sync_pass
= 0;
6424 spa_config_exit(spa
, SCL_CONFIG
, FTAG
);
6426 spa_handle_ignored_writes(spa
);
6429 * If any async tasks have been requested, kick them off.
6431 spa_async_dispatch(spa
);
6435 * Sync all pools. We don't want to hold the namespace lock across these
6436 * operations, so we take a reference on the spa_t and drop the lock during the
6440 spa_sync_allpools(void)
6443 mutex_enter(&spa_namespace_lock
);
6444 while ((spa
= spa_next(spa
)) != NULL
) {
6445 if (spa_state(spa
) != POOL_STATE_ACTIVE
||
6446 !spa_writeable(spa
) || spa_suspended(spa
))
6448 spa_open_ref(spa
, FTAG
);
6449 mutex_exit(&spa_namespace_lock
);
6450 txg_wait_synced(spa_get_dsl(spa
), 0);
6451 mutex_enter(&spa_namespace_lock
);
6452 spa_close(spa
, FTAG
);
6454 mutex_exit(&spa_namespace_lock
);
6458 * ==========================================================================
6459 * Miscellaneous routines
6460 * ==========================================================================
6464 * Remove all pools in the system.
6472 * Remove all cached state. All pools should be closed now,
6473 * so every spa in the AVL tree should be unreferenced.
6475 mutex_enter(&spa_namespace_lock
);
6476 while ((spa
= spa_next(NULL
)) != NULL
) {
6478 * Stop async tasks. The async thread may need to detach
6479 * a device that's been replaced, which requires grabbing
6480 * spa_namespace_lock, so we must drop it here.
6482 spa_open_ref(spa
, FTAG
);
6483 mutex_exit(&spa_namespace_lock
);
6484 spa_async_suspend(spa
);
6485 mutex_enter(&spa_namespace_lock
);
6486 spa_close(spa
, FTAG
);
6488 if (spa
->spa_state
!= POOL_STATE_UNINITIALIZED
) {
6490 spa_deactivate(spa
);
6494 mutex_exit(&spa_namespace_lock
);
6498 spa_lookup_by_guid(spa_t
*spa
, uint64_t guid
, boolean_t aux
)
6503 if ((vd
= vdev_lookup_by_guid(spa
->spa_root_vdev
, guid
)) != NULL
)
6507 for (i
= 0; i
< spa
->spa_l2cache
.sav_count
; i
++) {
6508 vd
= spa
->spa_l2cache
.sav_vdevs
[i
];
6509 if (vd
->vdev_guid
== guid
)
6513 for (i
= 0; i
< spa
->spa_spares
.sav_count
; i
++) {
6514 vd
= spa
->spa_spares
.sav_vdevs
[i
];
6515 if (vd
->vdev_guid
== guid
)
6524 spa_upgrade(spa_t
*spa
, uint64_t version
)
6526 ASSERT(spa_writeable(spa
));
6528 spa_config_enter(spa
, SCL_ALL
, FTAG
, RW_WRITER
);
6531 * This should only be called for a non-faulted pool, and since a
6532 * future version would result in an unopenable pool, this shouldn't be
6535 ASSERT(SPA_VERSION_IS_SUPPORTED(spa
->spa_uberblock
.ub_version
));
6536 ASSERT3U(version
, >=, spa
->spa_uberblock
.ub_version
);
6538 spa
->spa_uberblock
.ub_version
= version
;
6539 vdev_config_dirty(spa
->spa_root_vdev
);
6541 spa_config_exit(spa
, SCL_ALL
, FTAG
);
6543 txg_wait_synced(spa_get_dsl(spa
), 0);
6547 spa_has_spare(spa_t
*spa
, uint64_t guid
)
6551 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
6553 for (i
= 0; i
< sav
->sav_count
; i
++)
6554 if (sav
->sav_vdevs
[i
]->vdev_guid
== guid
)
6557 for (i
= 0; i
< sav
->sav_npending
; i
++) {
6558 if (nvlist_lookup_uint64(sav
->sav_pending
[i
], ZPOOL_CONFIG_GUID
,
6559 &spareguid
) == 0 && spareguid
== guid
)
6567 * Check if a pool has an active shared spare device.
6568 * Note: reference count of an active spare is 2, as a spare and as a replace
6571 spa_has_active_shared_spare(spa_t
*spa
)
6575 spa_aux_vdev_t
*sav
= &spa
->spa_spares
;
6577 for (i
= 0; i
< sav
->sav_count
; i
++) {
6578 if (spa_spare_exists(sav
->sav_vdevs
[i
]->vdev_guid
, &pool
,
6579 &refcnt
) && pool
!= 0ULL && pool
== spa_guid(spa
) &&
6588 * Post a FM_EREPORT_ZFS_* event from sys/fm/fs/zfs.h. The payload will be
6589 * filled in from the spa and (optionally) the vdev. This doesn't do anything
6590 * in the userland libzpool, as we don't want consumers to misinterpret ztest
6591 * or zdb as real changes.
6594 spa_event_notify(spa_t
*spa
, vdev_t
*vd
, const char *name
)
6597 zfs_ereport_post(name
, spa
, vd
, NULL
, 0, 0);
6601 #if defined(_KERNEL) && defined(HAVE_SPL)
6602 /* state manipulation functions */
6603 EXPORT_SYMBOL(spa_open
);
6604 EXPORT_SYMBOL(spa_open_rewind
);
6605 EXPORT_SYMBOL(spa_get_stats
);
6606 EXPORT_SYMBOL(spa_create
);
6607 EXPORT_SYMBOL(spa_import_rootpool
);
6608 EXPORT_SYMBOL(spa_import
);
6609 EXPORT_SYMBOL(spa_tryimport
);
6610 EXPORT_SYMBOL(spa_destroy
);
6611 EXPORT_SYMBOL(spa_export
);
6612 EXPORT_SYMBOL(spa_reset
);
6613 EXPORT_SYMBOL(spa_async_request
);
6614 EXPORT_SYMBOL(spa_async_suspend
);
6615 EXPORT_SYMBOL(spa_async_resume
);
6616 EXPORT_SYMBOL(spa_inject_addref
);
6617 EXPORT_SYMBOL(spa_inject_delref
);
6618 EXPORT_SYMBOL(spa_scan_stat_init
);
6619 EXPORT_SYMBOL(spa_scan_get_stats
);
6621 /* device maniion */
6622 EXPORT_SYMBOL(spa_vdev_add
);
6623 EXPORT_SYMBOL(spa_vdev_attach
);
6624 EXPORT_SYMBOL(spa_vdev_detach
);
6625 EXPORT_SYMBOL(spa_vdev_remove
);
6626 EXPORT_SYMBOL(spa_vdev_setpath
);
6627 EXPORT_SYMBOL(spa_vdev_setfru
);
6628 EXPORT_SYMBOL(spa_vdev_split_mirror
);
6630 /* spare statech is global across all pools) */
6631 EXPORT_SYMBOL(spa_spare_add
);
6632 EXPORT_SYMBOL(spa_spare_remove
);
6633 EXPORT_SYMBOL(spa_spare_exists
);
6634 EXPORT_SYMBOL(spa_spare_activate
);
6636 /* L2ARC statech is global across all pools) */
6637 EXPORT_SYMBOL(spa_l2cache_add
);
6638 EXPORT_SYMBOL(spa_l2cache_remove
);
6639 EXPORT_SYMBOL(spa_l2cache_exists
);
6640 EXPORT_SYMBOL(spa_l2cache_activate
);
6641 EXPORT_SYMBOL(spa_l2cache_drop
);
6644 EXPORT_SYMBOL(spa_scan
);
6645 EXPORT_SYMBOL(spa_scan_stop
);
6648 EXPORT_SYMBOL(spa_sync
); /* only for DMU use */
6649 EXPORT_SYMBOL(spa_sync_allpools
);
6652 EXPORT_SYMBOL(spa_prop_set
);
6653 EXPORT_SYMBOL(spa_prop_get
);
6654 EXPORT_SYMBOL(spa_prop_clear_bootfs
);
6656 /* asynchronous event notification */
6657 EXPORT_SYMBOL(spa_event_notify
);
6660 #if defined(_KERNEL) && defined(HAVE_SPL)
6661 module_param(spa_load_verify_maxinflight
, int, 0644);
6662 MODULE_PARM_DESC(spa_load_verify_maxinflight
,
6663 "Max concurrent traversal I/Os while verifying pool during import -X");
6665 module_param(spa_load_verify_metadata
, int, 0644);
6666 MODULE_PARM_DESC(spa_load_verify_metadata
,
6667 "Set to traverse metadata on pool import");
6669 module_param(spa_load_verify_data
, int, 0644);
6670 MODULE_PARM_DESC(spa_load_verify_data
,
6671 "Set to traverse data on pool import");